summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAlexander Batischev <eual.jp@gmail.com>2021-04-21 00:38:29 +0300
committerAlexander Batischev <eual.jp@gmail.com>2021-04-21 02:05:55 +0300
commit9c3893cf616279e4101b1ccf52d0a82b7a334b3b (patch)
treeb8ff5b6438252a5c08ae00c796625a00ae453643 /src
parent645392adb2c12649fc72164422fecb15993046e8 (diff)
downloadobnam2-9c3893cf616279e4101b1ccf52d0a82b7a334b3b.tar.gz
feat: add support for CACHEDIR.TAG
Fixes #78.
Diffstat (limited to 'src')
-rw-r--r--src/cmd/backup.rs4
-rw-r--r--src/config.rs4
-rw-r--r--src/fsiter.rs98
3 files changed, 93 insertions, 13 deletions
diff --git a/src/cmd/backup.rs b/src/cmd/backup.rs
index a0e0599..0479844 100644
--- a/src/cmd/backup.rs
+++ b/src/cmd/backup.rs
@@ -65,7 +65,7 @@ fn initial_backup(
let count = {
let mut new = NascentGeneration::create(newtemp.path())?;
for root in &config.roots {
- let iter = FsIterator::new(root);
+ let iter = FsIterator::new(root, config.exclude_cache_tag_directories);
let warnings = new.insert_iter(iter.map(|entry| run.backup(entry)))?;
for w in warnings {
all_warnings.push(w);
@@ -95,7 +95,7 @@ fn incremental_backup(
run.start_backup(&old)?;
let mut new = NascentGeneration::create(newtemp.path())?;
for root in &config.roots {
- let iter = FsIterator::new(root);
+ let iter = FsIterator::new(root, config.exclude_cache_tag_directories);
let warnings = new.insert_iter(iter.map(|entry| run.backup(entry, &old)))?;
for w in warnings {
all_warnings.push(w);
diff --git a/src/config.rs b/src/config.rs
index d6ffbc5..6881959 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -17,6 +17,7 @@ struct TentativeClientConfig {
roots: Vec<PathBuf>,
log: Option<PathBuf>,
encrypt: Option<bool>,
+ exclude_cache_tag_directories: Option<bool>,
}
#[derive(Debug, Serialize, Clone)]
@@ -59,6 +60,7 @@ pub struct ClientConfigWithoutPasswords {
pub roots: Vec<PathBuf>,
pub log: PathBuf,
pub encrypt: bool,
+ pub exclude_cache_tag_directories: bool,
}
#[derive(Debug, thiserror::Error)]
@@ -91,6 +93,7 @@ impl ClientConfigWithoutPasswords {
let tentative: TentativeClientConfig = serde_yaml::from_str(&config)?;
let encrypt = tentative.encrypt.or(Some(false)).unwrap();
+ let exclude_cache_tag_directories = tentative.exclude_cache_tag_directories.unwrap_or(true);
let config = Self {
filename: filename.to_path_buf(),
@@ -103,6 +106,7 @@ impl ClientConfigWithoutPasswords {
.or_else(|| Some(PathBuf::from(DEVNULL)))
.unwrap(),
encrypt,
+ exclude_cache_tag_directories,
};
config.check()?;
diff --git a/src/fsiter.rs b/src/fsiter.rs
index b778cf3..6c18404 100644
--- a/src/fsiter.rs
+++ b/src/fsiter.rs
@@ -5,7 +5,7 @@ use walkdir::{DirEntry, IntoIter, WalkDir};
/// Iterator over file system entries in a directory tree.
pub struct FsIterator {
- iter: IntoIter,
+ iter: SkipCachedirs,
}
#[derive(Debug, thiserror::Error)]
@@ -23,9 +23,12 @@ pub enum FsIterError {
pub type FsIterResult<T> = Result<T, FsIterError>;
impl FsIterator {
- pub fn new(root: &Path) -> Self {
+ pub fn new(root: &Path, exclude_cache_tag_directories: bool) -> Self {
Self {
- iter: WalkDir::new(root).into_iter(),
+ iter: SkipCachedirs::new(
+ WalkDir::new(root).into_iter(),
+ exclude_cache_tag_directories,
+ ),
}
}
}
@@ -33,18 +36,91 @@ impl FsIterator {
impl Iterator for FsIterator {
type Item = FsIterResult<FilesystemEntry>;
fn next(&mut self) -> Option<Self::Item> {
- let next = self.iter.next();
- debug!("walkdir found: {:?}", next);
- match next {
- None => None,
- Some(Ok(entry)) => Some(new_entry(&entry)),
- Some(Err(err)) => Some(Err(err.into())),
+ self.iter.next()
+ }
+}
+
+/// Cachedir-aware adaptor for WalkDir: it skips the contents of dirs that contain CACHEDIR.TAG,
+/// but still yields entries for the dir and the tag themselves.
+struct SkipCachedirs {
+ iter: IntoIter,
+ exclude_cache_tag_directories: bool,
+ // This is the last tag we've found. `next()` will yield it before asking `iter` for more
+ // entries.
+ cachedir_tag: Option<FsIterResult<FilesystemEntry>>,
+}
+
+impl SkipCachedirs {
+ fn new(iter: IntoIter, exclude_cache_tag_directories: bool) -> Self {
+ Self {
+ iter,
+ exclude_cache_tag_directories,
+ cachedir_tag: None,
+ }
+ }
+
+ fn try_enqueue_cachedir_tag(&mut self, entry: &DirEntry) {
+ if !self.exclude_cache_tag_directories {
+ return;
}
+
+ // If this entry is not a directory, it means we already processed its
+ // parent dir and decided that it's not cached.
+ if !entry.file_type().is_dir() {
+ return;
+ }
+
+ let mut tag_path = entry.path().to_owned();
+ tag_path.push("CACHEDIR.TAG");
+
+ // Tags are required to be regular files -- not even symlinks are allowed.
+ if !tag_path.is_file() {
+ return;
+ };
+
+ const CACHEDIR_TAG: &[u8] = b"Signature: 8a477f597d28d172789f06886806bc55";
+ let mut content = [0u8; CACHEDIR_TAG.len()];
+
+ let mut file = if let Ok(file) = std::fs::File::open(&tag_path) {
+ file
+ } else {
+ return;
+ };
+
+ use std::io::Read;
+ match file.read_exact(&mut content) {
+ Ok(_) => (),
+ // If we can't read the tag file, proceed as if's not there
+ Err(_) => return,
+ }
+
+ if content == CACHEDIR_TAG {
+ self.iter.skip_current_dir();
+ self.cachedir_tag = Some(new_entry(&tag_path));
+ }
+ }
+}
+
+impl Iterator for SkipCachedirs {
+ type Item = FsIterResult<FilesystemEntry>;
+
+ fn next(&mut self) -> Option<Self::Item> {
+ self.cachedir_tag.take().or_else(|| {
+ let next = self.iter.next();
+ debug!("walkdir found: {:?}", next);
+ match next {
+ None => None,
+ Some(Err(err)) => Some(Err(err.into())),
+ Some(Ok(entry)) => {
+ self.try_enqueue_cachedir_tag(&entry);
+ Some(new_entry(entry.path()))
+ }
+ }
+ })
}
}
-fn new_entry(e: &DirEntry) -> FsIterResult<FilesystemEntry> {
- let path = e.path();
+fn new_entry(path: &Path) -> FsIterResult<FilesystemEntry> {
let meta = std::fs::symlink_metadata(path);
debug!("metadata for {:?}: {:?}", path, meta);
let meta = match meta {