diff options
Diffstat (limited to 'src/lib.rs')
-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) + } +} |