watch mode

This commit is contained in:
Robin Appelman 2025-10-13 18:21:10 +02:00
commit 7500d6ccf4
6 changed files with 452 additions and 151 deletions

354
Cargo.lock generated
View file

@ -2,21 +2,6 @@
# It is not intended for manual editing.
version = 4
[[package]]
name = "addr2line"
version = "0.25.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b5d307320b3181d6d7954e663bd7c774a838b8220fe0593c86d9fb09f498b4b"
dependencies = [
"gimli",
]
[[package]]
name = "adler2"
version = "2.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa"
[[package]]
name = "aho-corasick"
version = "1.1.3"
@ -77,19 +62,10 @@ dependencies = [
]
[[package]]
name = "backtrace"
version = "0.3.76"
name = "bitflags"
version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bb531853791a215d7c62a30daf0dde835f381ab5de4589cfe7c649d2cbe92bd6"
dependencies = [
"addr2line",
"cfg-if",
"libc",
"miniz_oxide",
"object",
"rustc-demangle",
"windows-link",
]
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "bitflags"
@ -103,6 +79,12 @@ version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2fd1289c04a9ea8cb22300a459a72a385d7c73d3259e2ed7dcb2af674838cfa9"
[[package]]
name = "cfg_aliases"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724"
[[package]]
name = "clap"
version = "4.5.48"
@ -149,6 +131,23 @@ version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b05b61dc5112cbb17e4b6cd61790d9845d13888356391624cbe7e41efeac1e75"
[[package]]
name = "ctrlc"
version = "3.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "881c5d0a13b2f1498e2306e82cbada78390e152d4b1378fb28a84f4dcd0dc4f3"
dependencies = [
"dispatch",
"nix",
"windows-sys 0.61.2",
]
[[package]]
name = "dispatch"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bd0c93bb4b0c6d9b77f4435b0ae98c24d17f1c45b2ff844c6151a07256ca923b"
[[package]]
name = "equivalent"
version = "1.0.2"
@ -165,27 +164,42 @@ dependencies = [
"windows-sys 0.60.2",
]
[[package]]
name = "file-id"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e1fc6a637b6dc58414714eddd9170ff187ecb0933d4c7024d1abbd23a3cc26e9"
dependencies = [
"windows-sys 0.60.2",
]
[[package]]
name = "fsevent-sys"
version = "4.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "76ee7a02da4d231650c7cea31349b889be2f45ddb3ef3032d2ec8185f6313fd2"
dependencies = [
"libc",
]
[[package]]
name = "galton"
version = "0.1.0"
dependencies = [
"clap",
"ctrlc",
"home",
"main_error",
"notify-debouncer-full",
"regex",
"serde",
"thiserror",
"tokio",
"toml",
"tracing",
"tracing-subscriber",
"xattr",
]
[[package]]
name = "gimli"
version = "0.32.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e629b9b98ef3dd8afe6ca2bd0f89306cec16d43d907889945bc5d6687f2f13c7"
[[package]]
name = "hashbrown"
version = "0.16.0"
@ -218,13 +232,22 @@ dependencies = [
]
[[package]]
name = "io-uring"
version = "0.7.10"
name = "inotify"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "046fa2d4d00aea763528b4950358d0ead425372445dc8ff86312b3c69ff7727b"
checksum = "f37dccff2791ab604f9babef0ba14fbe0be30bd368dc541e2b08d07c8aa908f3"
dependencies = [
"bitflags 2.9.4",
"inotify-sys",
"libc",
]
[[package]]
name = "inotify-sys"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e05c02b5e89bff3b946cedeca278abc628fe811e604f027c45a8aa3cf793d0eb"
dependencies = [
"bitflags",
"cfg-if",
"libc",
]
@ -234,6 +257,32 @@ version = "1.70.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf"
[[package]]
name = "kqueue"
version = "1.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eac30106d7dce88daf4a3fcb4879ea939476d5074a9b7ddd0fb97fa4bed5596a"
dependencies = [
"kqueue-sys",
"libc",
]
[[package]]
name = "kqueue-sys"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed9625ffda8729b85e45cf04090035ac368927b8cebc34898e7c120f52e4838b"
dependencies = [
"bitflags 1.3.2",
"libc",
]
[[package]]
name = "lazy_static"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe"
[[package]]
name = "libc"
version = "0.2.177"
@ -246,6 +295,12 @@ version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "df1d3c3b53da64cf5760482273a98e575c651a67eec7f77df96b5b642de8f039"
[[package]]
name = "log"
version = "0.4.28"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34080505efa8e45a4b816c349525ebe327ceaa8559756f0356cba97ef3bf7432"
[[package]]
name = "main_error"
version = "0.1.2"
@ -258,15 +313,6 @@ version = "2.7.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f52b00d39961fc5b2736ea853c9cc86238e165017a493d1d5c8eac6bdc4cc273"
[[package]]
name = "miniz_oxide"
version = "0.8.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1fa76a2c86f704bdb222d66965fb3d63269ce38518b83cb0575fca855ebb6316"
dependencies = [
"adler2",
]
[[package]]
name = "mio"
version = "1.0.4"
@ -274,19 +320,75 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "78bed444cc8a2160f01cbcf811ef18cac863ad68ae8ca62092e8db51d51c761c"
dependencies = [
"libc",
"log",
"wasi",
"windows-sys 0.59.0",
]
[[package]]
name = "object"
version = "0.37.3"
name = "nix"
version = "0.30.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ff76201f031d8863c38aa7f905eca4f53abbfa15f609db4277d44cd8938f33fe"
checksum = "74523f3a35e05aba87a1d978330aef40f67b0304ac79c1c00b294c9830543db6"
dependencies = [
"memchr",
"bitflags 2.9.4",
"cfg-if",
"cfg_aliases",
"libc",
]
[[package]]
name = "notify"
version = "8.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4d3d07927151ff8575b7087f245456e549fea62edf0ec4e565a5ee50c8402bc3"
dependencies = [
"bitflags 2.9.4",
"fsevent-sys",
"inotify",
"kqueue",
"libc",
"log",
"mio",
"notify-types",
"walkdir",
"windows-sys 0.60.2",
]
[[package]]
name = "notify-debouncer-full"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "375bd3a138be7bfeff3480e4a623df4cbfb55b79df617c055cd810ba466fa078"
dependencies = [
"file-id",
"log",
"notify",
"notify-types",
"walkdir",
]
[[package]]
name = "notify-types"
version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5e0826a989adedc2a244799e823aece04662b66609d96af8dff7ac6df9a8925d"
[[package]]
name = "nu-ansi-term"
version = "0.50.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7957b9740744892f114936ab4a57b3f487491bbeafaf8083688b16841a4240e5"
dependencies = [
"windows-sys 0.60.2",
]
[[package]]
name = "once_cell"
version = "1.21.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d"
[[package]]
name = "once_cell_polyfill"
version = "1.70.1"
@ -346,25 +448,28 @@ version = "0.8.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c3160422bbd54dd5ecfdca71e5fd59b7b8fe2b1697ab2baf64f6d05dcc66d298"
[[package]]
name = "rustc-demangle"
version = "0.1.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "56f7d92ca342cea22a06f2121d944b4fd82af56988c270852495420f961d4ace"
[[package]]
name = "rustix"
version = "1.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cd15f8a2c5551a84d56efdc1cd049089e409ac19a3072d5037a17fd70719ff3e"
dependencies = [
"bitflags",
"bitflags 2.9.4",
"errno",
"libc",
"linux-raw-sys",
"windows-sys 0.60.2",
]
[[package]]
name = "same-file"
version = "1.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502"
dependencies = [
"winapi-util",
]
[[package]]
name = "serde"
version = "1.0.228"
@ -405,10 +510,19 @@ dependencies = [
]
[[package]]
name = "slab"
version = "0.4.11"
name = "sharded-slab"
version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a2ae44ef20feb57a68b23d846850f861394c2e02dc425a50098ae8c90267589"
checksum = "f40ca3c46823713e0d4209592e8d6e826aa57e928f09752619fc696c499637f6"
dependencies = [
"lazy_static",
]
[[package]]
name = "smallvec"
version = "1.15.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "67b1b7a3b5fe4f1376887184045fcf45c69e92af734b7aaddc05fb777b6fbd03"
[[package]]
name = "strsim"
@ -448,29 +562,12 @@ dependencies = [
]
[[package]]
name = "tokio"
version = "1.47.1"
name = "thread_local"
version = "1.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "89e49afdadebb872d3145a5638b59eb0691ea23e46ca484037cfab3b76b95038"
checksum = "f60246a4944f24f6e018aa17cdeffb7818b76356965d03b07d6a9886e8962185"
dependencies = [
"backtrace",
"io-uring",
"libc",
"mio",
"pin-project-lite",
"slab",
"tokio-macros",
]
[[package]]
name = "tokio-macros"
version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6e06d43f1345a3bcd39f6a56dbb7dcab2ba47e68e8ac134855e7e2bdbaf8cab8"
dependencies = [
"proc-macro2",
"quote",
"syn",
"cfg-if",
]
[[package]]
@ -512,6 +609,63 @@ version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "df8b2b54733674ad286d16267dcfc7a71ed5c776e4ac7aa3c3e2561f7c637bf2"
[[package]]
name = "tracing"
version = "0.1.41"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "784e0ac535deb450455cbfa28a6f0df145ea1bb7ae51b821cf5e7927fdcfbdd0"
dependencies = [
"pin-project-lite",
"tracing-attributes",
"tracing-core",
]
[[package]]
name = "tracing-attributes"
version = "0.1.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "81383ab64e72a7a8b8e13130c49e3dab29def6d0c7d76a03087b3cf71c5c6903"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "tracing-core"
version = "0.1.34"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b9d12581f227e93f094d3af2ae690a574abb8a2b9b7a96e7cfe9647b2b617678"
dependencies = [
"once_cell",
"valuable",
]
[[package]]
name = "tracing-log"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ee855f1f400bd0e5c02d150ae5de3840039a3f54b025156404e34c23c03f47c3"
dependencies = [
"log",
"once_cell",
"tracing-core",
]
[[package]]
name = "tracing-subscriber"
version = "0.3.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2054a14f5307d601f88daf0553e1cbf472acc4f2c51afab632431cdcd72124d5"
dependencies = [
"nu-ansi-term",
"sharded-slab",
"smallvec",
"thread_local",
"tracing-core",
"tracing-log",
]
[[package]]
name = "unicode-ident"
version = "1.0.19"
@ -524,12 +678,37 @@ version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
[[package]]
name = "valuable"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ba73ea9cf16a25df0c8caa16c51acb937d5712a8429db78a3ee29d5dcacd3a65"
[[package]]
name = "walkdir"
version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "29790946404f91d9c5d06f9874efddea1dc06c5efe94541a7d6863108e3a5e4b"
dependencies = [
"same-file",
"winapi-util",
]
[[package]]
name = "wasi"
version = "0.11.1+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b"
[[package]]
name = "winapi-util"
version = "0.1.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22"
dependencies = [
"windows-sys 0.60.2",
]
[[package]]
name = "windows-link"
version = "0.2.1"
@ -554,6 +733,15 @@ dependencies = [
"windows-targets 0.53.5",
]
[[package]]
name = "windows-sys"
version = "0.61.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc"
dependencies = [
"windows-link",
]
[[package]]
name = "windows-targets"
version = "0.52.6"

View file

@ -3,10 +3,10 @@ name = "galton"
description = "Sort your incoming files"
version = "0.1.0"
edition = "2024"
rust-version = "1.85.0"
[dependencies]
home = "0.5.11"
tokio = { version = "1.47.1", features = ["rt-multi-thread", "macros"] }
xattr = "1.6.1"
serde = { version = "1.0.228", features = ["derive"] }
toml = "0.9.8"
@ -14,3 +14,7 @@ regex = "1.12.1"
thiserror = "2.0.17"
clap = { version = "4.5.48", features = ["derive"] }
main_error = "0.1.2"
tracing = "0.1.41"
tracing-subscriber = "0.3.20"
notify-debouncer-full = "0.6.0"
ctrlc = "3.5.0"

View file

@ -1,14 +1,13 @@
use std::fs::read_to_string;
use std::path::{Path, PathBuf};
use crate::rule::Rule;
use home::home_dir;
use regex::Regex;
use serde::Deserialize;
use std::fs::read_to_string;
use std::path::{Path, PathBuf};
use thiserror::Error;
use crate::rule::Rule;
#[derive(Debug, Deserialize)]
pub struct GaltonConfig {
pub global: GlobalConfig,
#[serde(default)]
pub rule: Vec<Rule>,
}
@ -18,29 +17,24 @@ impl GaltonConfig {
let path = path.as_ref();
let raw = read_to_string(path).map_err(|error| ConfigError::Read {
path: path.into(),
error
error,
})?;
toml::from_str(&raw).map_err(|error| ConfigError::Parse {
path: path.into(),
error
error,
})
}
}
#[derive(Debug, Deserialize)]
pub struct GlobalConfig {
directory: String,
pub last: bool,
}
impl GlobalConfig {
pub fn directory<'a>(&'a self) -> PathBuf {
if self.directory.starts_with("~/") {
fn normalize_path(path: String) -> String {
if let Some(suffix) = path.strip_prefix("~/") {
let home = home_dir().unwrap_or_default();
home.join(&self.directory[2..])
home.join(suffix)
.into_os_string()
.into_string()
.expect("non utf8 home directory")
} else {
self.directory.clone().into()
}
path
}
}
@ -62,7 +56,7 @@ impl TryFrom<RuleConfig> for Rule {
return Err(RuleError::NoMatches);
}
if value.rename.is_none() && value.target.is_none() {
return Err(RuleError::NoMatches);
return Err(RuleError::NoAction);
}
fn parse_rule(val: Option<String>) -> Result<Option<Regex>, RuleError> {
@ -70,17 +64,16 @@ impl TryFrom<RuleConfig> for Rule {
return Ok(None);
};
Ok(Some(Regex::new(&val).map_err(|error| RuleError::Regex {
input: val,
error,
})?))
Ok(Some(
Regex::new(&val).map_err(|error| RuleError::Regex { input: val, error })?,
))
}
Ok(Rule {
name: parse_rule(value.name)?,
referrer: parse_rule(value.referrer)?,
url: parse_rule(value.url)?,
target: value.target,
target: value.target.map(normalize_path),
rename: value.rename,
})
}
@ -98,8 +91,6 @@ pub enum ConfigError {
path: PathBuf,
error: toml::de::Error,
},
#[error("Failed to parse rule: {0}")]
InvalidRule(RuleError),
}
#[derive(Debug, Error)]
@ -109,8 +100,5 @@ pub enum RuleError {
#[error("at least one action rule needs to be defined")]
NoAction,
#[error("invalid regex {input}: {error:#}")]
Regex {
input: String,
error: regex::Error,
}
Regex { input: String, error: regex::Error },
}

View file

@ -6,6 +6,7 @@ pub struct FileInfo {
pub path: String,
pub url: Option<String>,
pub referrer: Option<String>,
#[allow(dead_code)]
pub mtime: u64,
pub mtime_str: String,
}
@ -16,18 +17,21 @@ impl FileInfo {
let stat = path.metadata().map_err(|error| FileError::Stat {
path: path.into(),
error
error,
})?;
let path = path.to_str().ok_or_else(|| FileError::InvalidPath {
path: path.into(),
})?;
let path = path
.to_str()
.ok_or_else(|| FileError::InvalidPath { path: path.into() })?;
let mtime = stat.modified().map_err(|error| FileError::Stat {
path: path.into(),
error
error,
})?;
let mtime = mtime.duration_since(UNIX_EPOCH).unwrap_or_default().as_secs();
let mtime = mtime
.duration_since(UNIX_EPOCH)
.unwrap_or_default()
.as_secs();
let mut file = FileInfo {
path: path.into(),
@ -56,6 +60,20 @@ impl FileInfo {
Ok(file)
}
pub fn name(&self) -> &str {
self.path
.rsplit_once('/')
.map(|(_, name)| name)
.unwrap_or(self.path.as_str())
}
pub fn parent(&self) -> &str {
self.path
.rsplit_once('/')
.map(|(parent, _)| parent)
.unwrap_or("")
}
}
#[derive(Debug, Error)]
@ -66,5 +84,5 @@ pub enum FileError {
Stat {
path: PathBuf,
error: std::io::Error,
}
},
}

View file

@ -1,10 +1,20 @@
use std::path::PathBuf;
use clap::{Parser, Subcommand};
use clap::builder::Styles;
use clap::builder::styling::{AnsiColor, Effects};
use main_error::MainResult;
use crate::config::GaltonConfig;
use crate::file::FileInfo;
use crate::rule::{Rule, RuleMatch};
use clap::builder::styling::{AnsiColor, Effects};
use clap::builder::Styles;
use clap::{Parser, Subcommand};
use main_error::MainResult;
use notify_debouncer_full::notify::event::{AccessKind, AccessMode};
use notify_debouncer_full::notify::{EventKind, RecursiveMode};
use notify_debouncer_full::{new_debouncer, DebounceEventResult};
use std::fs::{copy, create_dir_all, remove_file, rename};
use std::io::ErrorKind;
use std::path::{Path, PathBuf};
use std::sync::mpsc::channel;
use std::thread::sleep;
use std::time::Duration;
use tracing::{debug, error, info, instrument};
mod config;
mod file;
@ -36,30 +46,128 @@ enum Commands {
path: PathBuf,
},
/// Watch for new files and apply rules on them
Watch
Watch {
/// Directory to watch for new files
path: PathBuf,
#[arg(long)]
recursive: bool,
},
}
#[tokio::main]
async fn main() -> MainResult {
fn main() -> MainResult {
let args: Args = Args::parse();
tracing_subscriber::fmt::init();
let config = GaltonConfig::load(&args.config)?;
match args.command {
Commands::File {path} => {
Commands::File { path } => {
let file = FileInfo::load(path)?;
handle_file(&file, &config.rule);
}
Commands::Watch { path, recursive } => {
let rules = config.rule;
let (tx, rx) = channel();
let mut watcher = new_debouncer(Duration::from_secs(1), None, tx)?;
watcher.watch(
&path,
if recursive {
RecursiveMode::Recursive
} else {
RecursiveMode::NonRecursive
},
)?;
for rule in &config.rule {
if let Some(target) = rule.matches(&file) {
dbg!(target);
todo!()
let mut watcher = Some(watcher);
ctrlc::set_handler(move || {
if let Some(watcher) = watcher.take() {
watcher.stop();
}
})?;
for res in rx {
handle_watch_events(res, &rules);
}
}
}
Commands::Watch => {
todo!()
}
}
Ok(())
}
fn handle_watch_events(result: DebounceEventResult, rules: &[Rule]) {
match result {
Ok(events) => {
for event in events {
if event.kind == EventKind::Access(AccessKind::Close(AccessMode::Write)) {
for path in &event.paths {
debug!("write event for {}", path.display());
// give originfox time to set xattr
sleep(Duration::from_millis(200));
match FileInfo::load(path) {
Ok(file) => {
handle_file(&file, rules);
}
Err(error) => {
error!(%error, "failed to load file info");
}
}
}
}
}
}
Err(errors) => {
for error in errors {
error!(%error, "watch error")
}
}
}
}
fn match_file(file: &FileInfo, rules: &[Rule]) -> Option<RuleMatch> {
for rule in rules {
if let Some(result) = rule.matches(file) {
debug!(?rule, ?result, "found matching rule");
return Some(result);
}
}
None
}
#[instrument(skip_all, fields(file = file.path))]
fn handle_file(file: &FileInfo, rules: &[Rule]) {
let Some(result) = match_file(file, rules) else {
info!("no matches");
return;
};
let parent = result.target.as_deref().unwrap_or_else(|| file.parent());
let name = result.rename.as_deref().unwrap_or_else(|| file.name());
if let Err(error) = create_dir_all(parent) {
error!(%error, "failed to create target directory");
return;
}
let target = format!("{parent}/{name}");
match cross_storage_move(&file.path, &target) {
Ok(()) => {
info!(target, "moved file");
}
Err(error) => {
info!(target, ?error, "failed to moved file");
}
}
}
fn cross_storage_move(source: impl AsRef<Path>, target: impl AsRef<Path>) -> std::io::Result<()> {
let source = source.as_ref();
let target = target.as_ref();
match rename(source, target) {
Ok(()) => Ok(()),
Err(error) if error.kind() == ErrorKind::CrossesDevices => {
copy(source, target)?;
remove_file(source)
}
Err(error) => Err(error),
}
}

View file

@ -51,12 +51,7 @@ impl Rule {
captures.insert(CaptureName::Named("mtime"), &file.mtime_str);
if let Some(name) = &self.name {
let file_name = file
.path
.rsplit_once('/')
.map(|(_, name)| name)
.unwrap_or(file.path.as_str());
if !extract_matches(name, file_name, &mut captures) {
if !extract_matches(name, file.name(), &mut captures) {
return None;
}
}
@ -64,7 +59,7 @@ impl Rule {
if let Some(referrer) = &self.referrer {
if !extract_matches(
referrer,
&file.referrer.as_deref().unwrap_or_default(),
file.referrer.as_deref().unwrap_or_default(),
&mut captures,
) {
return None;
@ -72,7 +67,7 @@ impl Rule {
}
if let Some(url) = &self.url {
if !extract_matches(url, &file.url.as_deref().unwrap_or_default(), &mut captures) {
if !extract_matches(url, file.url.as_deref().unwrap_or_default(), &mut captures) {
return None;
}
}