Skip to content
Open
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
3 changes: 2 additions & 1 deletion src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,8 @@ impl<T> Backend<T> {
}

fn target_is_valid(&self, path: &Path) -> bool {
match validate_target_path(path) {
let allow = config().read().unwrap().current().allow_drive_roots;
match validate_target_path(path, allow) {
Ok(()) => true,
Err(message) => {
self.gui.error("Folder not supported", &message);
Expand Down
3 changes: 3 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ pub struct Config {
pub hdd_single_thread: bool,
#[serde(default = "default_protect_direct_storage")]
pub protect_direct_storage: bool,
#[serde(default)]
pub allow_drive_roots: bool,
#[serde(default = "default_compression_priority")]
pub compression_priority: CompressionPriority,
pub excludes: Vec<String>,
Expand All @@ -102,6 +104,7 @@ impl Default for Config {
max_threads: default_max_threads(),
hdd_single_thread: default_hdd_single_thread(),
protect_direct_storage: default_protect_direct_storage(),
allow_drive_roots: false,
compression_priority: default_compression_priority(),
excludes: vec![
"*:\\Windows*",
Expand Down
4 changes: 4 additions & 0 deletions src/directstorage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use std::path::{Path, PathBuf};

use walkdir::WalkDir;

use crate::folder::{is_protected_root_dir, volume_root_path};

const DIRECT_STORAGE_DLLS: &[&str] = &["dstorage.dll", "dstoragecore.dll"];

// These are library/container directories, not individual games. When a
Expand Down Expand Up @@ -220,9 +222,11 @@ pub fn discover_direct_storage_roots(scan_root: &Path) -> Vec<PathBuf> {
let mut dlls = Vec::new();
let mut exe_dirs = HashSet::new();

let volume_root = volume_root_path(scan_root);
for entry in WalkDir::new(scan_root)
.follow_links(false)
.into_iter()
.filter_entry(|entry| !is_protected_root_dir(entry, volume_root.as_deref()))
.filter_map(Result::ok)
.filter(|entry| entry.file_type().is_file())
{
Expand Down
58 changes: 44 additions & 14 deletions src/folder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ fn wide_null(path: &Path) -> Vec<u16> {
.collect()
}

fn volume_root_path(path: &Path) -> Option<PathBuf> {
pub(crate) fn volume_root_path(path: &Path) -> Option<PathBuf> {
match path.components().next()? {
Component::Prefix(prefix) => match prefix.kind() {
Prefix::Disk(letter) | Prefix::VerbatimDisk(letter) => {
Expand All @@ -265,10 +265,12 @@ fn volume_root_path(path: &Path) -> Option<PathBuf> {
}

fn normalized_path(path: &Path) -> String {
path.to_string_lossy()
.replace('/', "\\")
.trim_end_matches('\\')
.to_ascii_lowercase()
let path = path.to_string_lossy().replace('/', "\\");
let path = match path.strip_prefix(r"\\?\") {
Some(rest) if rest.as_bytes().get(1) == Some(&b':') => rest,
_ => &path,
};
path.trim_end_matches('\\').to_ascii_lowercase()
}

fn path_is_same_or_child(path: &Path, parent: &Path) -> bool {
Expand Down Expand Up @@ -340,7 +342,26 @@ fn cluster_size_for_path(path: &Path) -> u64 {
}
}

pub fn validate_target_path(path: &Path) -> Result<(), String> {
/// Windows-managed top-level directories (recycle bin, System Volume Information,
/// Recovery, other `$*`) are never walked, even when the user selects a whole drive.
pub(crate) fn is_protected_root_dir(entry: &walkdir::DirEntry, root: Option<&Path>) -> bool {
let Some(root) = root else { return false };
let Some(parent) = entry.path().parent() else {
return false;
};
if entry.file_type().is_file() || normalized_path(parent) != normalized_path(root) {
return false;
}
is_protected_root_name(&entry.file_name().to_string_lossy())
}

fn is_protected_root_name(name: &str) -> bool {
name.eq_ignore_ascii_case("system volume information")
|| name.eq_ignore_ascii_case("recovery")
|| name.starts_with('$')
}

pub fn validate_target_path(path: &Path, allow_drive_roots: bool) -> Result<(), String> {
if !path.exists() {
return Err("The selected folder no longer exists.".to_string());
}
Expand All @@ -353,8 +374,16 @@ pub fn validate_target_path(path: &Path) -> Result<(), String> {
.to_string()
})?;

let system_root = std::env::var_os("SystemRoot").map(PathBuf::from);
if normalized_path(path) == normalized_path(&root) {
return Err("Select a folder rather than an entire drive root.".to_string());
if !allow_drive_roots {
return Err("Select a folder or enable Allow scanning entire drives in Settings.".to_string());
}
let windows_volume = system_root.as_deref().and_then(volume_root_path)
.ok_or_else(|| "Unable to identify the active Windows volume.".to_string())?;
if normalized_path(&root) == normalized_path(&windows_volume) {
return Err("The active Windows volume root is protected from compression.".to_string());
}
}

let filesystem = volume_filesystem(path)
Expand All @@ -370,8 +399,8 @@ pub fn validate_target_path(path: &Path) -> Result<(), String> {
));
}

if let Some(system_root) = std::env::var_os("SystemRoot") {
if path_is_same_or_child(path, &PathBuf::from(system_root)) {
if let Some(system_root) = system_root {
if path_is_same_or_child(path, &system_root) {
return Err("The active Windows directory is protected from compression.".to_string());
}
}
Expand All @@ -383,10 +412,7 @@ pub fn validate_target_path(path: &Path) -> Result<(), String> {
.map(|value| value.trim_start_matches('\\'))
{
let first = relative.split('\\').next().unwrap_or_default();
if first.eq_ignore_ascii_case("system volume information")
|| first.eq_ignore_ascii_case("recovery")
|| first.starts_with('$')
{
if is_protected_root_name(first) {
return Err("The selected folder is a protected Windows-managed path.".to_string());
}
}
Expand Down Expand Up @@ -872,9 +898,13 @@ impl Background for FolderScan {

let mut last_status = Instant::now();

let scan_root = volume_root_path(&path);
let walker = WalkDir::new(&path)
.into_iter()
.filter_entry(|e| e.file_type().is_file() || !excludes.is_match(e.path()))
.filter_entry(|e| {
e.file_type().is_file()
|| (!excludes.is_match(e.path()) && !is_protected_root_dir(e, scan_root.as_deref()))
})
.filter_map(|e| e.map_err(|e| eprintln!("Error: {:?}", e)).ok())
.filter(|e| e.file_type().is_file())
.filter_map(|e| e.metadata().map(|md| (e, md)).ok())
Expand Down
7 changes: 7 additions & 0 deletions src/gui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub enum GuiRequest {
max_threads: usize,
hdd_single_thread: bool,
protect_direct_storage: bool,
allow_drive_roots: bool,
compression_priority: String,
excludes: String,
},
Expand Down Expand Up @@ -76,6 +77,7 @@ pub enum GuiResponse {
max_threads: usize,
hdd_single_thread: bool,
protect_direct_storage: bool,
allow_drive_roots: bool,
compression_priority: String,
excludes: String,
},
Expand Down Expand Up @@ -149,6 +151,7 @@ impl<T> GuiWrapper<T> {
max_threads: s.max_threads,
hdd_single_thread: s.hdd_single_thread,
protect_direct_storage: s.protect_direct_storage,
allow_drive_roots: s.allow_drive_roots,
compression_priority: s.compression_priority.to_string(),
excludes: s.excludes.join("\n"),
});
Expand Down Expand Up @@ -262,6 +265,7 @@ pub fn spawn_gui() {
max_threads,
hdd_single_thread,
protect_direct_storage,
allow_drive_roots,
compression_priority,
excludes,
}) => {
Expand All @@ -272,6 +276,7 @@ pub fn spawn_gui() {
max_threads,
hdd_single_thread,
protect_direct_storage,
allow_drive_roots,
compression_priority: compression_priority.parse().unwrap_or_default(),
excludes: excludes.split('\n').map(str::to_owned).collect(),
};
Expand All @@ -292,6 +297,7 @@ pub fn spawn_gui() {
max_threads: s.max_threads,
hdd_single_thread: s.hdd_single_thread,
protect_direct_storage: s.protect_direct_storage,
allow_drive_roots: s.allow_drive_roots,
compression_priority: s.compression_priority.to_string(),
excludes: s.excludes.join("\n"),
},
Expand Down Expand Up @@ -320,6 +326,7 @@ pub fn spawn_gui() {
max_threads: s.max_threads,
hdd_single_thread: s.hdd_single_thread,
protect_direct_storage: s.protect_direct_storage,
allow_drive_roots: s.allow_drive_roots,
compression_priority: s.compression_priority.to_string(),
excludes: s.excludes.join("\n"),
},
Expand Down
6 changes: 6 additions & 0 deletions src/ui/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ var Response = (function() {
Gui.set_compression_priority(msg.compression_priority);
Gui.set_hdd_single_thread(msg.hdd_single_thread);
Gui.set_protect_direct_storage(msg.protect_direct_storage);
Gui.set_allow_drive_roots(msg.allow_drive_roots);
Gui.set_excludes(msg.excludes);
break;

Expand Down Expand Up @@ -337,6 +338,7 @@ var Gui = (function() {
compression_priority: $("#Compression_Priority").val(),
hdd_single_thread: $("#HDD_Single_Thread").prop("checked"),
protect_direct_storage: $("#Protect_DirectStorage").prop("checked"),
allow_drive_roots: $("#Allow_Drive_Roots").prop("checked"),
excludes: $("#Excludes").val()
});
});
Expand Down Expand Up @@ -465,6 +467,10 @@ var Gui = (function() {
$("#HDD_Single_Thread").prop("checked", enabled);
},

set_allow_drive_roots: function(enabled) {
$("#Allow_Drive_Roots").prop("checked", enabled);
},

set_protect_direct_storage: function(enabled) {
$("#Protect_DirectStorage").prop("checked", enabled);
},
Expand Down
7 changes: 7 additions & 0 deletions src/ui/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,13 @@ <h2>Compression</h2>
</span>
<small class="setting-help">Skip detected DirectStorage game roots so WOF compression does not interfere with their fast I/O path.</small>
</label>
<label class="setting-field setting-toggle">
<span class="setting-checkline">
<input id="Allow_Drive_Roots" name="Allow_Drive_Roots" type="checkbox">
<span>Allow scanning entire drives</span>
</span>
<small class="setting-help">Allow selecting the root of a local NTFS drive other than the active Windows volume.</small>
</label>
</div>
</div>

Expand Down