aboutsummaryrefslogtreecommitdiffstats
path: root/pkgs/populate/src/rsync.rs
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2026-11-20 00:51:27 +0000
committertv <tv@krebsco.de>2026-11-20 00:51:27 +0000
commit61840ec59bc13b9b2ae6d29fff7276c849684941 (patch)
tree0b5299e337758b34cd4982ed4e0c2d8eb3655f0f /pkgs/populate/src/rsync.rs
parent275b9f6102e6f3011099752ec1023a11b326fc45 (diff)
populate: a lock, a journal, and a record of each run
For consumers of the target directory on the target, e.g. a CI service, which should act after a run that delivered everything, and know what it delivered. Populate holds an exclusive flock on .populate/lock from right after checking the sentinel until it's done. The command holding it runs over the shared connection, or directly, keeping the lock until its input ends, which populate's ending ends as well, so no lock stays behind. Runs wait for each other, and for consumers holding a shared lock, saying so. The sentinel is a directory, which a sentinel file becomes, since there's always state to keep now, and the target needs flock. Populate keeps a journal in .populate/journal, a line of JSON for each entry: a run starting, beginning to deliver or change a source, what that changed, and pruning. The entries go with the next command on the target, before what that changes, so the journal needs no commands of its own. After delivering everything, populate replaces .populate/run.json with the record of the run, which sums up the journal, and removes the journal. A run that doesn't finish leaves it, so changed in the record means changed since the last record: a source the journal tells began changing, without what it changed, counts as changed. The record has, for each source, its type, whether it changed, and what got delivered: the commit of a git source, the store path of a flake, flake-input, or derivation source, and the derived one, and a symlink's target. Nothing about secrets, nor files. Each source type is a Kind, whose deliver returns the Outcome: whether anything changed, and what got delivered. rsync tells changes by what it itemizes, the snippets by printing changed: the git source if it cloned, reset, or submodule updates or cleaning printed anything, links if they point elsewhere, and lock files and generated flakes if they differ. Symlinks in place of the lock or the journal get refused. A file or git source whose lock file populate rewrites in place counts as changed on every run, since delivering it brings back the original.
Diffstat (limited to 'pkgs/populate/src/rsync.rs')
-rw-r--r--pkgs/populate/src/rsync.rs37
1 files changed, 30 insertions, 7 deletions
diff --git a/pkgs/populate/src/rsync.rs b/pkgs/populate/src/rsync.rs
index 0ab92e6..7858e4a 100644
--- a/pkgs/populate/src/rsync.rs
+++ b/pkgs/populate/src/rsync.rs
@@ -84,8 +84,8 @@ impl Config {
}
/// Transfers source_path, a directory's contents or a file, to path on the
-/// target.
-pub fn transfer(context: &Context, config: &Config, source_path: &str, path: &str) -> Result<()> {
+/// target, returning whether that changed anything there.
+pub fn transfer(context: &Context, config: &Config, source_path: &str, path: &str) -> Result<bool> {
context.run(&sh::break_hard_links(), &[path, &context.name(path)])?;
let mut source = source_path.to_string();
if Path::new(source_path).is_dir() {
@@ -123,11 +123,24 @@ pub fn transfer(context: &Context, config: &Config, source_path: &str, path: &st
if config.delete_excluded {
command.arg("--delete-excluded");
}
- command
- .arg(&source)
- .arg(context.rsync_destination(path))
- .stdout(run::stderr());
- run::status(&mut command)
+ command.arg(&source).arg(context.rsync_destination(path));
+ let mut changed = false;
+ run::lines(&mut command, |line| {
+ changed |= itemizes_change(line);
+ eprintln!("{line}");
+ })?;
+ Ok(changed)
+}
+
+/// Whether a line rsync prints itemizes a change: what --itemize-changes
+/// prints for an item, eleven characters, the second the type of the item,
+/// or *deleting, followed by a blank.
+fn itemizes_change(line: &str) -> bool {
+ let Some((item, _)) = line.split_once(' ') else {
+ return false;
+ };
+ item == "*deleting"
+ || item.len() == 11 && matches!(item.as_bytes()[1], b'f' | b'd' | b'L' | b'D' | b'S')
}
/// rsync 3.5.0 and later open .rsync-filter files with a stricter walk than
@@ -302,6 +315,16 @@ mod tests {
use super::*;
#[test]
+ fn itemized_changes() {
+ assert!(itemizes_change(">f.st...... flake.nix 82/39"));
+ assert!(itemizes_change("cd+++++++++ .git/objects/25/ 0/4.00K"));
+ assert!(itemizes_change(".f..t...... x 0/1"));
+ assert!(itemizes_change("*deleting old"));
+ assert!(!itemizes_change("skipping non-regular file \"x\""));
+ assert!(!itemizes_change("sent 10 bytes"));
+ }
+
+ #[test]
fn versions() {
assert!(version_at_least("3.5.0", "3.5.0"));
assert!(version_at_least("3.10.1", "3.5.0"));