use std::io::Write;
use std::{io, path};
const HEADER: &[u8; 43] = b"Signature: 8a477f597d28d172789f06886806bc55";
pub fn add_tag<P: AsRef<path::Path>>(directory: P) -> io::Result<()> {
let directory = directory.as_ref();
match fs_err::OpenOptions::new()
.write(true)
.create_new(true)
.open(directory.join("CACHEDIR.TAG"))
{
Ok(mut cachedir_tag) => cachedir_tag.write_all(HEADER),
Err(e) => Err(e),
}
}
pub fn ensure_tag<P: AsRef<path::Path>>(directory: P) -> io::Result<()> {
match add_tag(&directory) {
Err(e) => match e.kind() {
io::ErrorKind::AlreadyExists => Ok(()),
io::ErrorKind::PermissionDenied if directory.as_ref().join("CACHEDIR.TAG").exists() => {
Ok(())
}
_ => Err(e),
},
other => other,
}
}