diff options
author | Lars Wirzenius <liw@liw.fi> | 2022-04-17 11:15:42 +0300 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2022-04-17 14:26:49 +0300 |
commit | 53ba9fc4a822f9bdc5e951a980af583e94a485f6 (patch) | |
tree | 3fce0b121e1cb9033878e007ff0b8602f9df4713 /src | |
parent | 26a16affedcb38ce402739a8b67f8306f7c77ba9 (diff) | |
download | cachedir-rs-53ba9fc4a822f9bdc5e951a980af583e94a485f6.tar.gz |
fix: use published Subplot crates from crates.io
Sponsored-by: author
Diffstat (limited to 'src')
-rw-r--r-- | src/lib.rs | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..5c01557 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,32 @@ +use std::fs::read; +use std::path::Path; + +/// Name of tag file. +pub const CACHEDIR_TAG: &str = "CACHEDIR.TAG"; + +/// Prefix of contents of tag file. +pub const TAG: &str = "Signature: 8a477f597d28d172789f06886806bc55"; + +/// Is a given file a cache directory tag? +pub fn is_cachedir_tag(filename: &Path) -> Result<bool, std::io::Error> { + // Is the filename OK? + if let Some(basename) = filename.file_name() { + if basename != CACHEDIR_TAG { + return Ok(false); + } + } + + // Does the file exist? It's OK if it doesn't, but we don't want + // to try to read it if it doesn't. + if !filename.exists() { + return Ok(false); + } + + // Does the file start with the right prefix? + let data = read(filename)?; + if data.len() >= TAG.len() { + Ok(&data[..TAG.len()] == TAG.as_bytes()) + } else { + Ok(false) + } +} |