Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
cloud::CloudChange,
lang::TRANSLATOR,
prelude::StrictPath,
resource::manifest::Os,
resource::manifest::{Os, Tag},
scan::{
BackupError, BackupInfo, DuplicateDetector, OperationStatus, OperationStepDecision, ScanChange, ScanInfo,
TitleMatch, compare_ranked_titles_ref, layout::Backup, registry,
Expand Down Expand Up @@ -98,6 +98,10 @@ pub struct ApiFile {
/// Any other games that also have the same file path.
#[serde(skip_serializing_if = "BTreeSet::is_empty")]
pub duplicated_by: BTreeSet<String>,
/// Manifest tags describing the kind of data, such as `save` or `config`.
/// Only set when backing up, since the tags come from the manifest.
#[serde(skip_serializing_if = "BTreeSet::is_empty")]
pub tags: BTreeSet<Tag>,
}

#[derive(Debug, Default, serde::Serialize, schemars::JsonSchema)]
Expand Down Expand Up @@ -429,6 +433,7 @@ impl Reporter {
.and_then(|x| x.failed_files.get(scan_key).map(SaveError::from)),
ignored: entry.ignored,
change: entry.change(),
tags: entry.tags.clone(),
..Default::default()
};
if !duplicate_detector.is_file_duplicated(scan_key, entry).resolved() {
Expand Down Expand Up @@ -736,6 +741,7 @@ Overall:
change: Default::default(),
container: None,
redirected: None,
tags: Default::default(),
},
"/file2".into(): ScannedFile {
size: 51_200,
Expand All @@ -745,6 +751,7 @@ Overall:
change: Default::default(),
container: None,
redirected: None,
tags: Default::default(),
},
},
found_registry_keys: hash_map! {
Expand Down Expand Up @@ -805,6 +812,7 @@ Overall:
change: ScanChange::Same,
container: None,
redirected: None,
tags: Default::default(),
},
},
found_registry_keys: hash_map! {},
Expand All @@ -828,6 +836,7 @@ Overall:
change: Default::default(),
container: None,
redirected: None,
tags: Default::default(),
},
},
found_registry_keys: hash_map! {},
Expand Down Expand Up @@ -873,6 +882,7 @@ Overall:
change: Default::default(),
container: None,
redirected: None,
tags: Default::default(),
},
"/backup/file2".into(): ScannedFile {
size: 51_200,
Expand All @@ -882,6 +892,7 @@ Overall:
change: Default::default(),
container: None,
redirected: None,
tags: Default::default(),
},
},
found_registry_keys: hash_map! {},
Expand Down Expand Up @@ -1214,6 +1225,7 @@ Overall:
change: Default::default(),
container: None,
redirected: None,
tags: Default::default(),
},
"/backup/file2".into(): ScannedFile {
size: 50,
Expand All @@ -1223,6 +1235,7 @@ Overall:
change: Default::default(),
container: None,
redirected: None,
tags: Default::default(),
},
},
found_registry_keys: hash_map! {},
Expand Down
14 changes: 13 additions & 1 deletion src/resource/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,19 @@ impl ToString for Store {
}
}

#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash, serde::Serialize, serde::Deserialize)]
#[derive(
Clone,
Debug,
Default,
PartialEq,
Eq,
PartialOrd,
Ord,
Hash,
serde::Serialize,
serde::Deserialize,
schemars::JsonSchema,
)]
#[serde(rename_all = "camelCase")]
pub enum Tag {
Save,
Expand Down
88 changes: 75 additions & 13 deletions src/scan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub mod steam;
pub mod title;

use std::{
collections::{HashMap, HashSet},
collections::{BTreeSet, HashMap, HashSet},
sync::LazyLock,
};

Expand All @@ -37,7 +37,7 @@ use crate::{
config::{
BackupFilter, Config, RedirectConfig, RedirectKind, Root, SortKey, ToggledPaths, ToggledRegistry, root,
},
manifest::{Game, GameFileEntry, IdSet, Os, Store},
manifest::{Game, GameFileEntry, IdSet, Os, Store, Tag},
},
scan::layout::{BackupSemantics, DirectorySemantics, LatestBackup, SemanticDirKind},
};
Expand Down Expand Up @@ -542,14 +542,18 @@ pub fn scan_game_for_backup(
) -> ScanInfo {
log::trace!("[{name}] beginning scan for backup");

let mut found_files = HashMap::new();
let mut found_files: HashMap<StrictPath, ScannedFile> = HashMap::new();
#[cfg_attr(not(target_os = "windows"), allow(unused))]
let mut found_registry_keys = HashMap::new();
#[allow(unused)]
let mut dumped_registry = None;
let has_backups = previous.is_some();

let mut paths_to_check = HashSet::<(StrictPath, Option<bool>)>::new();
// Manifest tags for each candidate path, so found files can report whether
// they are saves, configs, and so on. Paths that overlap accumulate all of
// their tags.
let mut path_tags = HashMap::<StrictPath, BTreeSet<Tag>>::new();

// Add a dummy root for checking paths without `<root>`.
let mut roots_to_check: Vec<Root> = vec![Root::new(SKIP, Store::Other)];
Expand Down Expand Up @@ -650,6 +654,12 @@ pub fn scan_game_for_backup(

for (candidate, case_sensitive) in candidates {
log::trace!("[{name}] parsed candidate: {candidate:?}");
if !path_data.tags.is_empty() {
path_tags
.entry(candidate.clone())
.or_default()
.extend(path_data.tags.iter().cloned());
}
paths_to_check.insert((candidate, Some(case_sensitive)));
}
}
Expand Down Expand Up @@ -716,6 +726,7 @@ pub fn scan_game_for_backup(
log::debug!("[{name}] excluded: {path:?}");
continue;
}
let tags = path_tags.get(&path).cloned().unwrap_or_default();
let paths = match case_sensitive {
None => path.glob(),
Some(cs) => path.glob_case_sensitive(cs),
Expand Down Expand Up @@ -743,18 +754,19 @@ pub fn scan_game_for_backup(
);
let change =
ScanChange::evaluate_backup(&hash, previous_files.get(redirected.as_ref().unwrap_or(&scan_key)));
found_files.insert(
scan_key,
ScannedFile {
found_files
.entry(scan_key)
.and_modify(|existing| existing.tags.extend(tags.iter().cloned()))
.or_insert_with(|| ScannedFile {
change,
size,
hash,
redirected,
original_path: None,
ignored,
container: None,
},
);
tags: tags.clone(),
});
} else if p.is_dir() {
log::trace!("[{name}] looking for files in: {p:?}");
for child in walkdir::WalkDir::new(p.as_std_path_buf().unwrap())
Expand Down Expand Up @@ -794,18 +806,19 @@ pub fn scan_game_for_backup(
&hash,
previous_files.get(redirected.as_ref().unwrap_or(&scan_key)),
);
found_files.insert(
scan_key,
ScannedFile {
found_files
.entry(scan_key)
.and_modify(|existing| existing.tags.extend(tags.iter().cloned()))
.or_insert_with(|| ScannedFile {
change,
size,
hash,
redirected,
original_path: None,
ignored,
container: None,
},
);
tags: tags.clone(),
});
}
}
}
Expand Down Expand Up @@ -840,6 +853,7 @@ pub fn scan_game_for_backup(
original_path: None,
ignored: ignored_paths.is_ignored(name, previous_file),
container: None,
tags: Default::default(),
},
);
}
Expand Down Expand Up @@ -1348,6 +1362,51 @@ mod tests {
);
}

#[test]
fn can_scan_game_for_backup_with_tags() {
let manifest = Manifest::load_from_string(
r#"
game1:
files:
<base>/file1.txt:
tags:
- config
<base>/subdir:
tags:
- save
"#,
)
.unwrap();

assert_eq!(
ScanInfo {
game_name: s("game1"),
found_files: hash_map! {
format!("{}/tests/root1/game1/subdir/file2.txt", repo()).into(): ScannedFile::with_tags(2, "9d891e731f75deae56884d79e9816736b7488080", BTreeSet::from([Tag::Save])).change_new(),
format!("{}/tests/root2/game1/file1.txt", repo()).into(): ScannedFile::with_tags(1, "3a52ce780950d4d969792a2559cd519d7ee8c727", BTreeSet::from([Tag::Config])).change_new(),
},
found_registry_keys: hash_map! {},
..Default::default()
},
scan_game_for_backup(
&manifest.0["game1"],
"game1",
&config().roots,
&StrictPath::new(repo()),
&Launchers::scan_dirs(&config().roots, &manifest, &["game1".to_string()]),
&BackupFilter::default(),
None,
&ToggledPaths::default(),
&ToggledRegistry::default(),
None,
&[],
false,
&Default::default(),
ONLY_CONSTRUCTIVE,
),
);
}

#[test]
fn can_scan_game_for_backup_deduplicating_symlinks() {
let roots = &[Root::new(format!("{}/tests/root3", repo()), Store::Other)];
Expand Down Expand Up @@ -1394,6 +1453,7 @@ mod tests {
change: ScanChange::New,
container: None,
redirected: Some(StrictPath::new(format!("{}/tests/root3/game5/data-symlink/file1.txt", repo()))),
tags: Default::default(),
},
},
found_registry_keys: hash_map! {},
Expand Down Expand Up @@ -1762,6 +1822,7 @@ mod tests {
change: ScanChange::Unknown,
container: None,
redirected: Some(current_path.clone()),
tags: Default::default(),
},
},
..Default::default()
Expand Down Expand Up @@ -1809,6 +1870,7 @@ mod tests {
change: ScanChange::Unknown,
container: None,
redirected: None,
tags: Default::default(),
},
},
..Default::default()
Expand Down
4 changes: 4 additions & 0 deletions src/scan/duplicate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,7 @@ mod tests {
change: Default::default(),
container: None,
redirected: None,
tags: Default::default(),
};
let scan_key_1b = StrictPath::from("file1b.txt");
let file1b = ScannedFile {
Expand All @@ -474,6 +475,7 @@ mod tests {
change: Default::default(),
container: None,
redirected: None,
tags: Default::default(),
};

detector.add_game(
Expand Down Expand Up @@ -516,6 +518,7 @@ mod tests {
change: Default::default(),
container: None,
redirected: None,
tags: Default::default(),
}
)
);
Expand Down Expand Up @@ -543,6 +546,7 @@ mod tests {
change: Default::default(),
container: None,
redirected: None,
tags: Default::default(),
}
)
);
Expand Down
Loading
Loading