mirror of
https://github.com/narrrl/proton-drive-linux.git
synced 2026-11-17 17:55:03 +01:00
feat(gui,core,cli): edit excluded files, open-with rules and the launcher in Preferences (#99)
Preferences gains two pages for the config.json settings that had no place in the app: - Sync: the patterns every synced folder leaves out (own patterns, and each built-in one on or off), and what the conflict sweep may do. - Apps: which program opens which kind of file (open_with), and how the search launcher opens (prompt). Each synced folder kept on this computer gets "Excluded Files…" in its menu for its own .pdfsignore: checked as typed, "Check a path" says which rule decides, and a version changed elsewhere is never written over without asking. `pdfs sync ignore list|add|remove|reset|check` does the same from a terminal, and `--page preferences-sync` (and -apps, -storage) opens Preferences at a page. Every writer of config.json now goes through AppDirs::update_config, which reads, changes and writes under an flock on config.json.lock, so the app, the tray and the daemon no longer undo each other's change (B292). The opener reads its rules on each open, so a new rule applies without a restart. The built-in ignore list also leaves out what Nextcloud, Dropbox and Syncthing leave out: desktop.ini, .directory, trash folders, NFS/FUSE leftovers, partial downloads, Kate swap and office lock files.
This commit is contained in:
1 parent
14b9e0fe64
commit
0df4b3c5f9
40 files changed
+8376
-3303
No files matched your search
@@ -20,6 +20,7 @@ thiserror.workspace = true
|
||||
tracing.workspace = true
|
||||
directories.workspace = true
|
||||
ignore.workspace = true
|
||||
shell-words.workspace = true
|
||||
sha2.workspace = true
|
||||
sha1.workspace = true
|
||||
zip.workspace = true
|
||||
|
||||
+190
-54
@@ -407,6 +407,116 @@ fn refuse_to_replace_unreadable(path: &Path) -> Result<()> {
|
||||
}
|
||||
}
|
||||
|
||||
/// [`AppDirs::load_config`] for the file at `path`.
|
||||
fn load_config_at(path: &Path) -> AppConfig {
|
||||
if path.exists() {
|
||||
match std::fs::read_to_string(path)
|
||||
.map_err(Error::from)
|
||||
.and_then(|content| serde_json::from_str::<AppConfig>(&content).map_err(Error::from))
|
||||
{
|
||||
Ok(config) => return config,
|
||||
Err(e) => {
|
||||
// Preserve the original bytes for diagnosis/recovery. In
|
||||
// particular, never replace a truncated config with defaults
|
||||
// and silently lose mount, device, or ignore settings.
|
||||
// `save_config` refuses to write over it for the same reason.
|
||||
tracing::error!(path = %path.display(), error = %e, "config is unreadable; using in-memory defaults without overwriting it");
|
||||
return AppConfig::default();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let default_config = AppConfig::default();
|
||||
if let Err(e) = save_config_at(path, &default_config) {
|
||||
tracing::warn!(path = %path.display(), error = %e, "could not write the default config");
|
||||
}
|
||||
default_config
|
||||
}
|
||||
|
||||
/// [`AppDirs::save_config`] for the file at `path`.
|
||||
fn save_config_at(path: &Path, config: &AppConfig) -> Result<()> {
|
||||
refuse_to_replace_unreadable(path)?;
|
||||
let parent = path
|
||||
.parent()
|
||||
.ok_or_else(|| Error::Other("configuration path has no parent".into()))?;
|
||||
std::fs::create_dir_all(parent)?;
|
||||
let nonce = std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.map(|duration| duration.as_nanos())
|
||||
.unwrap_or(0);
|
||||
let tmp = path.with_extension(format!("json.tmp-{}-{nonce}", std::process::id()));
|
||||
use std::os::unix::fs::OpenOptionsExt as _;
|
||||
let mut file = std::fs::OpenOptions::new()
|
||||
.write(true)
|
||||
.create_new(true)
|
||||
.mode(0o600)
|
||||
.open(&tmp)?;
|
||||
use std::io::Write as _;
|
||||
file.write_all(&serde_json::to_vec_pretty(config)?)?;
|
||||
file.sync_all()?;
|
||||
std::fs::rename(&tmp, path)?;
|
||||
std::fs::File::open(parent)?.sync_all()?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// [`AppDirs::update_config`] for the file at `path`.
|
||||
fn update_config_at<R>(path: &Path, change: impl FnOnce(&mut AppConfig) -> R) -> Result<R> {
|
||||
let _lock = lock_config(path)?;
|
||||
// Refuse before reading: an unreadable file loads as defaults, and the
|
||||
// change must not be applied to those.
|
||||
refuse_to_replace_unreadable(path)?;
|
||||
let mut config = load_config_at(path);
|
||||
let out = change(&mut config);
|
||||
save_config_at(path, &config)?;
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
/// How long [`lock_config`] waits for another writer. A change holds the lock
|
||||
/// for one small read and write; anything longer is a stuck process, and the
|
||||
/// setting is better reported as not saved than waited on forever.
|
||||
const CONFIG_LOCK_WAIT: std::time::Duration = std::time::Duration::from_secs(5);
|
||||
|
||||
/// Take the lock that orders changes to the config file at `path`, held until
|
||||
/// the returned file is dropped.
|
||||
///
|
||||
/// A separate `config.json.lock`, never the config file itself: that file is
|
||||
/// replaced by a rename on every save, so a lock on it would guard a file
|
||||
/// nobody reads any more.
|
||||
fn lock_config(path: &Path) -> Result<std::fs::File> {
|
||||
use std::os::fd::AsRawFd as _;
|
||||
use std::os::unix::fs::OpenOptionsExt as _;
|
||||
if let Some(parent) = path.parent() {
|
||||
std::fs::create_dir_all(parent)?;
|
||||
}
|
||||
let mut name = path.as_os_str().to_owned();
|
||||
name.push(".lock");
|
||||
let file = std::fs::OpenOptions::new()
|
||||
.create(true)
|
||||
.read(true)
|
||||
.write(true)
|
||||
.truncate(false)
|
||||
.mode(0o600)
|
||||
.open(PathBuf::from(name))?;
|
||||
let deadline = std::time::Instant::now() + CONFIG_LOCK_WAIT;
|
||||
loop {
|
||||
// SAFETY: `file` owns the descriptor and outlives the call.
|
||||
if unsafe { libc::flock(file.as_raw_fd(), libc::LOCK_EX | libc::LOCK_NB) } == 0 {
|
||||
return Ok(file);
|
||||
}
|
||||
let e = std::io::Error::last_os_error();
|
||||
if e.raw_os_error() != Some(libc::EWOULDBLOCK) {
|
||||
return Err(Error::Io(e));
|
||||
}
|
||||
if std::time::Instant::now() >= deadline {
|
||||
return Err(Error::Other(format!(
|
||||
"{} is being changed by another process; try again",
|
||||
path.display()
|
||||
)));
|
||||
}
|
||||
std::thread::sleep(std::time::Duration::from_millis(10));
|
||||
}
|
||||
}
|
||||
|
||||
/// Resolved XDG locations for state, cache, and the default mountpoint.
|
||||
pub struct AppDirs {
|
||||
dirs: ProjectDirs,
|
||||
@@ -426,65 +536,33 @@ impl AppDirs {
|
||||
|
||||
/// Load config from disk, creating default if missing.
|
||||
pub fn load_config(&self) -> AppConfig {
|
||||
let path = self.config_path();
|
||||
if path.exists() {
|
||||
match std::fs::read_to_string(&path)
|
||||
.map_err(Error::from)
|
||||
.and_then(|content| {
|
||||
serde_json::from_str::<AppConfig>(&content).map_err(Error::from)
|
||||
}) {
|
||||
Ok(config) => return config,
|
||||
Err(e) => {
|
||||
// Preserve the original bytes for diagnosis/recovery. In
|
||||
// particular, never replace a truncated config with defaults
|
||||
// and silently lose mount, device, or ignore settings.
|
||||
// `save_config` refuses to write over it for the same reason.
|
||||
tracing::error!(path = %path.display(), error = %e, "config is unreadable; using in-memory defaults without overwriting it");
|
||||
return AppConfig::default();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let default_config = AppConfig::default();
|
||||
if let Err(e) = self.save_config(&default_config) {
|
||||
tracing::warn!(path = %path.display(), error = %e, "could not write the default config");
|
||||
}
|
||||
default_config
|
||||
load_config_at(&self.config_path())
|
||||
}
|
||||
|
||||
/// Persist `config` to [`config_path`](Self::config_path), creating the
|
||||
/// config dir if missing. The Settings page calls this after editing the
|
||||
/// cache budget or mountpoint; the daemon re-reads the file on its next mount.
|
||||
/// config dir if missing.
|
||||
///
|
||||
/// Refuses while the file on disk does not parse. Every caller loads,
|
||||
/// changes one field and saves, and [`load_config`](Self::load_config)
|
||||
/// gives defaults for a file it cannot read, so the first change after
|
||||
/// the file was damaged wrote defaults over the mountpoint, the device and
|
||||
/// the ignore rules the user had (`docs/BUGS.md` B223).
|
||||
/// Refuses while the file on disk does not parse: [`load_config`](Self::load_config)
|
||||
/// gives defaults for a file it cannot read, so the first change after the
|
||||
/// file was damaged would write defaults over the mountpoint, the device
|
||||
/// and the ignore rules the user had (`docs/BUGS.md` B223).
|
||||
///
|
||||
/// Writes the whole file. To change a setting, use
|
||||
/// [`update_config`](Self::update_config), which does not lose another
|
||||
/// process's change.
|
||||
pub fn save_config(&self, config: &AppConfig) -> Result<()> {
|
||||
let path = self.config_path();
|
||||
refuse_to_replace_unreadable(&path)?;
|
||||
let parent = path
|
||||
.parent()
|
||||
.ok_or_else(|| Error::Other("configuration path has no parent".into()))?;
|
||||
std::fs::create_dir_all(parent)?;
|
||||
let nonce = std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.map(|duration| duration.as_nanos())
|
||||
.unwrap_or(0);
|
||||
let tmp = path.with_extension(format!("json.tmp-{}-{nonce}", std::process::id()));
|
||||
use std::os::unix::fs::OpenOptionsExt as _;
|
||||
let mut file = std::fs::OpenOptions::new()
|
||||
.write(true)
|
||||
.create_new(true)
|
||||
.mode(0o600)
|
||||
.open(&tmp)?;
|
||||
use std::io::Write as _;
|
||||
file.write_all(&serde_json::to_vec_pretty(config)?)?;
|
||||
file.sync_all()?;
|
||||
std::fs::rename(&tmp, &path)?;
|
||||
std::fs::File::open(parent)?.sync_all()?;
|
||||
Ok(())
|
||||
save_config_at(&self.config_path(), config)
|
||||
}
|
||||
|
||||
/// Change settings in place: read the file, apply `change`, write it back,
|
||||
/// all under a lock, and return what `change` returned.
|
||||
///
|
||||
/// The app, the tray and the daemon each write the file. Without the lock,
|
||||
/// two of them changing different settings at once both read the old file,
|
||||
/// and the one that wrote last undid the other's change (`docs/BUGS.md`
|
||||
/// B292).
|
||||
pub fn update_config<R>(&self, change: impl FnOnce(&mut AppConfig) -> R) -> Result<R> {
|
||||
update_config_at(&self.config_path(), change)
|
||||
}
|
||||
|
||||
/// This machine's id (e.g. ~/.config/proton-drive-linux/machine-id). See
|
||||
@@ -682,6 +760,64 @@ mod tests {
|
||||
std::fs::metadata(p).unwrap().permissions().mode() & 0o777
|
||||
}
|
||||
|
||||
/// Two writers changing different settings at once both keep their change
|
||||
/// (`docs/BUGS.md` B292).
|
||||
#[test]
|
||||
fn concurrent_changes_to_different_settings_are_all_kept() {
|
||||
let dir = std::env::temp_dir().join(format!(
|
||||
"pdfs-cfg-race-{}-{:?}",
|
||||
std::process::id(),
|
||||
std::thread::current().id()
|
||||
));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let path = dir.join("config.json");
|
||||
|
||||
let a = {
|
||||
let path = path.clone();
|
||||
std::thread::spawn(move || {
|
||||
for i in 1..=100u64 {
|
||||
update_config_at(&path, |c| c.upload_limit = Some(i)).unwrap();
|
||||
}
|
||||
})
|
||||
};
|
||||
let b = {
|
||||
let path = path.clone();
|
||||
std::thread::spawn(move || {
|
||||
for i in 1..=100u64 {
|
||||
update_config_at(&path, |c| c.download_limit = Some(i)).unwrap();
|
||||
}
|
||||
})
|
||||
};
|
||||
a.join().unwrap();
|
||||
b.join().unwrap();
|
||||
|
||||
let config = load_config_at(&path);
|
||||
assert_eq!(config.upload_limit, Some(100));
|
||||
assert_eq!(config.download_limit, Some(100));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_change_is_never_applied_over_an_unreadable_config() {
|
||||
let dir = std::env::temp_dir().join(format!(
|
||||
"pdfs-cfg-update-guard-{}-{:?}",
|
||||
std::process::id(),
|
||||
std::thread::current().id()
|
||||
));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let path = dir.join("config.json");
|
||||
std::fs::write(&path, br#"{"mountpoint": "/home/me/Pro"#).unwrap();
|
||||
|
||||
assert!(update_config_at(&path, |c| c.tray_hidden = true).is_err());
|
||||
assert_eq!(
|
||||
std::fs::read(&path).unwrap(),
|
||||
br#"{"mountpoint": "/home/me/Pro"#
|
||||
);
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
/// A damaged config must not be replaced by defaults the next time a
|
||||
/// setting changes (`docs/BUGS.md` B223).
|
||||
#[test]
|
||||
|
||||
@@ -206,26 +206,23 @@ pub fn reap(mut child: std::process::Child) {
|
||||
});
|
||||
}
|
||||
|
||||
/// [`open`] with the policy from the user's `config.json`, read once per
|
||||
/// process. For callers that open the occasional path and have no reason to
|
||||
/// carry a policy around; long-lived windows should hold their own copy.
|
||||
/// [`open`] with the policy from the user's `config.json`, read again on
|
||||
/// every call: the file is small, a path is opened by hand, and a rule changed
|
||||
/// in Preferences then applies to the next file opened, without a restart.
|
||||
pub fn open_default(path: &Path, is_dir: bool) {
|
||||
open(default_policy(), path, is_dir);
|
||||
open(&default_policy(), path, is_dir);
|
||||
}
|
||||
|
||||
/// [`open_default`] matching the rules against `name` — see
|
||||
/// [`OpenWith::command_for_named`].
|
||||
pub fn open_default_named(path: &Path, name: &str, is_dir: bool) {
|
||||
open_named(default_policy(), path, name, is_dir);
|
||||
open_named(&default_policy(), path, name, is_dir);
|
||||
}
|
||||
|
||||
fn default_policy() -> &'static OpenWith {
|
||||
static POLICY: std::sync::OnceLock<OpenWith> = std::sync::OnceLock::new();
|
||||
POLICY.get_or_init(|| {
|
||||
crate::config::AppDirs::new()
|
||||
.map(|dirs| dirs.load_config().resolved_open_with())
|
||||
.unwrap_or_default()
|
||||
})
|
||||
fn default_policy() -> OpenWith {
|
||||
crate::config::AppDirs::new()
|
||||
.map(|dirs| dirs.load_config().resolved_open_with())
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
/// `$PDFS_TERMINAL`, then `$TERMINAL`, then the first [`KNOWN_TERMINALS`] entry
|
||||
@@ -266,6 +263,33 @@ fn with_exec_flag(program: &str) -> Vec<String> {
|
||||
argv
|
||||
}
|
||||
|
||||
/// Split a command line typed by the user into argv, the way a shell would:
|
||||
/// quotes keep spaces, nothing is expanded. `None` for an unclosed quote.
|
||||
pub fn parse_command(line: &str) -> Option<Vec<String>> {
|
||||
shell_words::split(line).ok()
|
||||
}
|
||||
|
||||
/// Show `argv` as one line that [`parse_command`] turns back into it.
|
||||
pub fn format_command(argv: &[String]) -> String {
|
||||
shell_words::join(argv)
|
||||
}
|
||||
|
||||
/// Whether the program `argv` starts with can be found: an absolute or
|
||||
/// relative path to a file, or a name on `PATH`. A `$VAR` program counts as
|
||||
/// found, since it is looked up only when a file is opened.
|
||||
pub fn command_available(argv: &[String]) -> bool {
|
||||
let Some(program) = argv.first() else {
|
||||
return false;
|
||||
};
|
||||
if program.starts_with('$') {
|
||||
return true;
|
||||
}
|
||||
if program.contains('/') {
|
||||
return Path::new(program).is_file();
|
||||
}
|
||||
on_path(program)
|
||||
}
|
||||
|
||||
fn on_path(program: &str) -> bool {
|
||||
let Some(path) = std::env::var_os("PATH") else {
|
||||
return false;
|
||||
@@ -703,4 +727,24 @@ mod tests {
|
||||
// Unknown terminals get the near-universal `-e`.
|
||||
assert_eq!(with_exec_flag("mystery-term"), vec!["mystery-term", "-e"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_typed_command_line_round_trips() {
|
||||
let argv = parse_command("alacritty -e 'my editor' --flag").unwrap();
|
||||
assert_eq!(argv, ["alacritty", "-e", "my editor", "--flag"]);
|
||||
assert_eq!(parse_command(&format_command(&argv)).unwrap(), argv);
|
||||
assert_eq!(parse_command("nvim 'unclosed"), None);
|
||||
assert_eq!(parse_command(" ").unwrap(), Vec::<String>::new());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_command_is_available_when_its_program_is() {
|
||||
assert!(command_available(&["sh".to_string()]));
|
||||
assert!(command_available(&["$EDITOR".to_string()]));
|
||||
assert!(!command_available(&[]));
|
||||
assert!(!command_available(
|
||||
&["pdfs-no-such-program-xyz".to_string()]
|
||||
));
|
||||
assert!(!command_available(&["/nonexistent/program".to_string()]));
|
||||
}
|
||||
}
|
||||
@@ -30,6 +30,11 @@ use ignore::gitignore::{Gitignore, GitignoreBuilder};
|
||||
/// them. Version-control metadata, dependency and build trees, editor
|
||||
/// leftovers, and OS turds — none of which belong on Drive, all of which are
|
||||
/// large or rewritten constantly.
|
||||
///
|
||||
/// The second half is what Nextcloud, Dropbox and Syncthing leave out as well:
|
||||
/// files a desktop, a file system or a program in the middle of a write leaves
|
||||
/// behind, never anything a person made. Every one is in a test, next to a real
|
||||
/// file of a similar name that must stay synced.
|
||||
pub const DEFAULT_IGNORE_PATTERNS: &[&str] = &[
|
||||
".git/",
|
||||
".hg/",
|
||||
@@ -43,6 +48,23 @@ pub const DEFAULT_IGNORE_PATTERNS: &[&str] = &[
|
||||
"*.tmp",
|
||||
".DS_Store",
|
||||
"Thumbs.db",
|
||||
// Folder settings of Windows and KDE's Dolphin.
|
||||
"desktop.ini",
|
||||
".directory",
|
||||
// Trash folders and file-system leftovers: a file deleted while open on a
|
||||
// FUSE or NFS mount lives on under one of these names until it is closed.
|
||||
".Trash-*/",
|
||||
".Trashes/",
|
||||
".Spotlight-V100/",
|
||||
".fuse_hidden*",
|
||||
".nfs*",
|
||||
// Downloads and saves still being written.
|
||||
"*.part",
|
||||
"*.crdownload",
|
||||
"*.kate-swp",
|
||||
// Office lock files: LibreOffice's `.~lock.<name>#`, Microsoft Office's `~$<name>`.
|
||||
".~lock.*#",
|
||||
"~$*",
|
||||
];
|
||||
|
||||
/// Suffixes that mean a file is incomplete *by construction*: only the program
|
||||
@@ -119,6 +141,149 @@ pub fn is_scratch_name(name: &str) -> bool {
|
||||
/// Filenames accepted as a folder's ignore file, in precedence order.
|
||||
const IGNORE_FILE_NAMES: &[&str] = &[".pdfsignore", ".protonignore"];
|
||||
|
||||
/// Whether `line` is a rule [`IgnoreRules::load`] accepts: a pattern, a
|
||||
/// comment or a blank line. The error says what is wrong with it.
|
||||
pub fn check_pattern(line: &str) -> Result<(), String> {
|
||||
GitignoreBuilder::new("/")
|
||||
.add_line(None, line)
|
||||
.map(|_| ())
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
/// The lines of an ignore file that [`IgnoreRules::load`] would skip, by line
|
||||
/// number (from 1), with what is wrong with each.
|
||||
pub fn check_rules(text: &str) -> Vec<(usize, String)> {
|
||||
text.lines()
|
||||
.enumerate()
|
||||
.filter_map(|(i, line)| check_pattern(line).err().map(|e| (i + 1, e)))
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// A synced folder's own ignore file, read so it can be edited and written
|
||||
/// back. See [`FolderRules::read`].
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct FolderRules {
|
||||
/// The file the rules are in: the one in use, or `.pdfsignore` when there
|
||||
/// is none yet.
|
||||
pub path: std::path::PathBuf,
|
||||
/// The rules as read; empty when there is no file. `None` means there was
|
||||
/// no file, which [`write`](Self::write) checks is still so.
|
||||
pub text: Option<String>,
|
||||
}
|
||||
|
||||
/// Why [`FolderRules::write`] did not write.
|
||||
#[derive(Debug)]
|
||||
pub enum WriteRulesError {
|
||||
/// The file changed after it was read, for example by an edit on another
|
||||
/// computer that sync brought in. The rules on disk are the new ones.
|
||||
Changed,
|
||||
/// Reading or writing failed.
|
||||
Io(std::io::Error),
|
||||
}
|
||||
|
||||
impl std::fmt::Display for WriteRulesError {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
Self::Changed => f.write_str("the file changed since it was opened"),
|
||||
Self::Io(e) => e.fmt(f),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for WriteRulesError {}
|
||||
|
||||
impl From<std::io::Error> for WriteRulesError {
|
||||
fn from(e: std::io::Error) -> Self {
|
||||
Self::Io(e)
|
||||
}
|
||||
}
|
||||
|
||||
impl FolderRules {
|
||||
/// Read the ignore file of the folder at `root`, the same one
|
||||
/// [`IgnoreRules::load`] uses.
|
||||
pub fn read(root: &Path) -> std::io::Result<Self> {
|
||||
for name in IGNORE_FILE_NAMES {
|
||||
let path = root.join(name);
|
||||
match std::fs::read_to_string(&path) {
|
||||
Ok(text) => {
|
||||
return Ok(Self {
|
||||
path,
|
||||
text: Some(text),
|
||||
});
|
||||
}
|
||||
Err(e) if e.kind() == std::io::ErrorKind::NotFound => continue,
|
||||
Err(e) => return Err(e),
|
||||
}
|
||||
}
|
||||
// Like `load`, a missing folder reads as one without rules; the write
|
||||
// is what fails then.
|
||||
Ok(Self {
|
||||
path: root.join(IGNORE_FILE_NAMES[0]),
|
||||
text: None,
|
||||
})
|
||||
}
|
||||
|
||||
/// Replace the rules with `text`, unless the file changed since it was
|
||||
/// read. Rules that are all blank remove the file, so a folder never keeps
|
||||
/// an empty one around.
|
||||
///
|
||||
/// Written to a temporary file and renamed over the old one, so the sync
|
||||
/// pass reading the rules at the same moment sees the old ones or the new
|
||||
/// ones, never half of them.
|
||||
pub fn write(&self, text: &str) -> Result<(), WriteRulesError> {
|
||||
let now = match std::fs::read_to_string(&self.path) {
|
||||
Ok(now) => Some(now),
|
||||
Err(e) if e.kind() == std::io::ErrorKind::NotFound => None,
|
||||
Err(e) => return Err(e.into()),
|
||||
};
|
||||
if now != self.text {
|
||||
return Err(WriteRulesError::Changed);
|
||||
}
|
||||
if text.trim().is_empty() {
|
||||
return match std::fs::remove_file(&self.path) {
|
||||
Err(e) if e.kind() != std::io::ErrorKind::NotFound => Err(e.into()),
|
||||
_ => Ok(()),
|
||||
};
|
||||
}
|
||||
let Some(name) = self.path.file_name() else {
|
||||
return Err(std::io::Error::other("the rules path has no file name").into());
|
||||
};
|
||||
let mut tmp_name = std::ffi::OsString::from(".");
|
||||
tmp_name.push(name);
|
||||
tmp_name.push(format!(".tmp-{}", std::process::id()));
|
||||
let tmp = self.path.with_file_name(tmp_name);
|
||||
let mut body = text.to_string();
|
||||
if !body.ends_with('\n') {
|
||||
body.push('\n');
|
||||
}
|
||||
let written = std::fs::write(&tmp, body).and_then(|()| std::fs::rename(&tmp, &self.path));
|
||||
if written.is_err() {
|
||||
let _ = std::fs::remove_file(&tmp);
|
||||
}
|
||||
Ok(written?)
|
||||
}
|
||||
}
|
||||
|
||||
/// Where a rule that decided about a path came from.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum RuleSource {
|
||||
/// The rules for every synced folder (`ignore_patterns`).
|
||||
AllFolders,
|
||||
/// The folder's own ignore file.
|
||||
ThisFolder,
|
||||
}
|
||||
|
||||
/// What the rules say about one path, and which rule says it.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum Verdict {
|
||||
/// No rule matches: the path is synced.
|
||||
Synced,
|
||||
/// Left out by `pattern`. `pattern` may name a folder the path is in.
|
||||
Excluded { pattern: String, source: RuleSource },
|
||||
/// A rule excluded it, and a later `!pattern` brought it back.
|
||||
Included { pattern: String, source: RuleSource },
|
||||
}
|
||||
|
||||
/// Compiled ignore rules for one synced folder.
|
||||
pub struct IgnoreRules {
|
||||
matcher: Option<Gitignore>,
|
||||
@@ -161,6 +326,27 @@ impl IgnoreRules {
|
||||
}
|
||||
}
|
||||
|
||||
/// [`load`](Self::load) with `text` standing in for the folder's ignore
|
||||
/// file, for showing what rules being edited would do before they are
|
||||
/// saved.
|
||||
pub fn with_folder_text(root: &Path, globals: &[String], text: &str) -> Self {
|
||||
let mut builder = GitignoreBuilder::new(root);
|
||||
let mut any = false;
|
||||
for pattern in globals {
|
||||
any |= builder.add_line(None, pattern).is_ok();
|
||||
}
|
||||
let file = root.join(IGNORE_FILE_NAMES[0]);
|
||||
for line in text.lines() {
|
||||
any |= builder.add_line(Some(file.clone()), line).is_ok();
|
||||
}
|
||||
if !any {
|
||||
return Self { matcher: None };
|
||||
}
|
||||
Self {
|
||||
matcher: builder.build().ok(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Rules matching nothing, for callers with no folder context.
|
||||
pub fn empty() -> Self {
|
||||
Self { matcher: None }
|
||||
@@ -207,6 +393,50 @@ impl IgnoreRules {
|
||||
}
|
||||
}
|
||||
|
||||
impl IgnoreRules {
|
||||
/// What the rules say about `rel`, and which rule decides, the way
|
||||
/// [`is_ignored`](Self::is_ignored) decides it.
|
||||
pub fn explain(&self, rel: &str, is_dir: bool) -> Verdict {
|
||||
let Some(matcher) = &self.matcher else {
|
||||
return Verdict::Synced;
|
||||
};
|
||||
let rel = rel.trim_matches('/');
|
||||
if rel.is_empty() {
|
||||
return Verdict::Synced;
|
||||
}
|
||||
let source = |glob: &ignore::gitignore::Glob| match glob.from() {
|
||||
Some(_) => RuleSource::ThisFolder,
|
||||
None => RuleSource::AllFolders,
|
||||
};
|
||||
let mut prefix = String::new();
|
||||
let mut parts = rel.split('/').peekable();
|
||||
while let Some(part) = parts.next() {
|
||||
if !prefix.is_empty() {
|
||||
prefix.push('/');
|
||||
}
|
||||
prefix.push_str(part);
|
||||
let last = parts.peek().is_none();
|
||||
let as_dir = if last { is_dir } else { true };
|
||||
match matcher.matched(&prefix, as_dir) {
|
||||
ignore::Match::Ignore(glob) => {
|
||||
return Verdict::Excluded {
|
||||
pattern: glob.original().to_string(),
|
||||
source: source(glob),
|
||||
};
|
||||
}
|
||||
ignore::Match::Whitelist(glob) if last => {
|
||||
return Verdict::Included {
|
||||
pattern: glob.original().to_string(),
|
||||
source: source(glob),
|
||||
};
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
Verdict::Synced
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for IgnoreRules {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.debug_struct("IgnoreRules")
|
||||
@@ -255,6 +485,160 @@ mod tests {
|
||||
std::fs::write(root.join(name), body).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_added_defaults_leave_out_clutter_but_not_look_alikes() {
|
||||
let dir = TempDir::new();
|
||||
let rules = IgnoreRules::load(dir.path(), &globals());
|
||||
for (clutter, is_dir) in [
|
||||
("desktop.ini", false),
|
||||
("Photos/.directory", false),
|
||||
(".Trash-1000", true),
|
||||
(".Trashes", true),
|
||||
(".Spotlight-V100", true),
|
||||
(".fuse_hidden0000001a00000002", false),
|
||||
(".nfs000000000123abcd", false),
|
||||
("movie.mkv.part", false),
|
||||
("setup.exe.crdownload", false),
|
||||
("notes.txt.kate-swp", false),
|
||||
(".~lock.report.odt#", false),
|
||||
("~$budget.xlsx", false),
|
||||
] {
|
||||
assert!(rules.is_ignored(clutter, is_dir), "{clutter}");
|
||||
}
|
||||
for kept in [
|
||||
"Desktop/notes.txt",
|
||||
"directory.txt",
|
||||
"Trash-talk.md",
|
||||
"nfs-setup.md",
|
||||
"parts.csv",
|
||||
"part.txt",
|
||||
"report.odt",
|
||||
"budget.xlsx",
|
||||
] {
|
||||
assert!(!rules.is_ignored(kept, false), "{kept}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_bad_pattern_is_reported_with_its_line() {
|
||||
assert!(check_pattern("*.db").is_ok());
|
||||
assert!(check_pattern("# a comment").is_ok());
|
||||
assert!(check_pattern("").is_ok());
|
||||
assert!(check_pattern("*.{jpg,png").is_err());
|
||||
assert!(check_pattern("[z-a]").is_err());
|
||||
let bad = check_rules("*.log\n{oops\nbuild/\n");
|
||||
assert_eq!(bad.len(), 1);
|
||||
assert_eq!(bad[0].0, 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn explain_names_the_rule_and_where_it_came_from() {
|
||||
let dir = TempDir::new();
|
||||
write(dir.path(), ".pdfsignore", "*.db\n!keep.tmp\n");
|
||||
let rules = IgnoreRules::load(dir.path(), &globals());
|
||||
assert_eq!(
|
||||
rules.explain("data/cache.db", false),
|
||||
Verdict::Excluded {
|
||||
pattern: "*.db".into(),
|
||||
source: RuleSource::ThisFolder
|
||||
}
|
||||
);
|
||||
assert_eq!(
|
||||
rules.explain("app/node_modules/x/index.js", false),
|
||||
Verdict::Excluded {
|
||||
pattern: "node_modules/".into(),
|
||||
source: RuleSource::AllFolders
|
||||
}
|
||||
);
|
||||
assert_eq!(
|
||||
rules.explain("keep.tmp", false),
|
||||
Verdict::Included {
|
||||
pattern: "!keep.tmp".into(),
|
||||
source: RuleSource::ThisFolder
|
||||
}
|
||||
);
|
||||
assert_eq!(rules.explain("src/main.rs", false), Verdict::Synced);
|
||||
// `explain` and `is_ignored` never disagree.
|
||||
for (path, is_dir) in [
|
||||
("data/cache.db", false),
|
||||
("keep.tmp", false),
|
||||
("x.tmp", false),
|
||||
] {
|
||||
assert_eq!(
|
||||
rules.is_ignored(path, is_dir),
|
||||
matches!(rules.explain(path, is_dir), Verdict::Excluded { .. }),
|
||||
"{path}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rules_being_edited_explain_like_the_saved_ones() {
|
||||
let dir = TempDir::new();
|
||||
let text = "*.db\n!keep.db\n";
|
||||
write(dir.path(), ".pdfsignore", text);
|
||||
let saved = IgnoreRules::load(dir.path(), &globals());
|
||||
let edited = IgnoreRules::with_folder_text(dir.path(), &globals(), text);
|
||||
for path in ["a.db", "keep.db", "node_modules/x", "src/x.rs"] {
|
||||
assert_eq!(
|
||||
saved.explain(path, false),
|
||||
edited.explain(path, false),
|
||||
"{path}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn folder_rules_are_written_back_and_removed_when_blank() {
|
||||
let dir = TempDir::new();
|
||||
let rules = FolderRules::read(dir.path()).unwrap();
|
||||
assert_eq!(rules.text, None);
|
||||
assert_eq!(rules.path, dir.path().join(".pdfsignore"));
|
||||
rules.write("*.db").unwrap();
|
||||
assert_eq!(
|
||||
std::fs::read_to_string(dir.path().join(".pdfsignore")).unwrap(),
|
||||
"*.db\n"
|
||||
);
|
||||
assert!(IgnoreRules::load(dir.path(), &[]).is_ignored("x.db", false));
|
||||
|
||||
let rules = FolderRules::read(dir.path()).unwrap();
|
||||
rules.write(" \n").unwrap();
|
||||
assert!(!dir.path().join(".pdfsignore").exists());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn folder_rules_keep_the_file_name_in_use() {
|
||||
let dir = TempDir::new();
|
||||
write(dir.path(), ".protonignore", "old/\n");
|
||||
let rules = FolderRules::read(dir.path()).unwrap();
|
||||
assert_eq!(rules.path, dir.path().join(".protonignore"));
|
||||
rules.write("new/\n").unwrap();
|
||||
assert!(!dir.path().join(".pdfsignore").exists());
|
||||
assert_eq!(
|
||||
std::fs::read_to_string(dir.path().join(".protonignore")).unwrap(),
|
||||
"new/\n"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn folder_rules_changed_elsewhere_are_not_written_over() {
|
||||
let dir = TempDir::new();
|
||||
write(dir.path(), ".pdfsignore", "a/\n");
|
||||
let rules = FolderRules::read(dir.path()).unwrap();
|
||||
write(dir.path(), ".pdfsignore", "b/\n");
|
||||
assert!(matches!(rules.write("c/"), Err(WriteRulesError::Changed)));
|
||||
assert_eq!(
|
||||
std::fs::read_to_string(dir.path().join(".pdfsignore")).unwrap(),
|
||||
"b/\n"
|
||||
);
|
||||
|
||||
// A file that appeared after an empty read counts as a change too.
|
||||
let dir = TempDir::new();
|
||||
let rules = FolderRules::read(dir.path()).unwrap();
|
||||
write(dir.path(), ".pdfsignore", "b/\n");
|
||||
assert!(matches!(rules.write("c/"), Err(WriteRulesError::Changed)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_rules_at_all_ignores_nothing() {
|
||||
let dir = TempDir::new();
|
||||
|
||||
Reference in new issue
Block a user