rigswig 0.3.0

See everything your coding agent loads before you type, and where each piece came from. Harness-agnostic, local, read-only.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
//! Write mode, the applying half: only `rigswig ui --allow-writes` uses it. A plan from rigswig-core is applied
//! atomically (a temporary file in the same folder, then a rename), after a dated backup of what was there, and only
//! if the file is unchanged since the plan was made. Undo puts the backup back, only if the file is still as we left
//! it.

use std::io::Write;
use std::path::{Path, PathBuf};

use rigswig_core::change::Plan;
use rigswig_core::read::redact::sha256_hex;

/// What undo needs: the file, what it held before (None: it did not exist), and what we wrote.
#[derive(Debug, Clone)]
pub struct Applied {
    pub path: PathBuf,
    pub file: String,
    pub before: Option<String>,
    pub after_hash: String,
    pub backup: Option<PathBuf>,
}

fn current(path: &Path) -> Result<Option<String>, String> {
    match std::fs::symlink_metadata(path) {
        Ok(m) if m.file_type().is_symlink() => {
            Err("that settings file is a link; rigswig does not write through links".into())
        }
        Ok(_) => std::fs::read_to_string(path).map(Some).map_err(|_| "that settings file cannot be read".into()),
        Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(None),
        Err(_) => Err("that settings file cannot be read".into()),
    }
}

/// Create a file which did not exist before. On Unix, `mode` is applied at creation, before any contents are written.
fn create_new(path: &Path, _mode: u32) -> std::io::Result<std::fs::File> {
    let mut options = std::fs::OpenOptions::new();
    options.write(true).create_new(true);
    #[cfg(unix)]
    {
        use std::os::unix::fs::OpenOptionsExt;
        options.mode(_mode);
    }
    options.open(path)
}

/// Save one backup without following a backup-directory symlink. Unix uses directory descriptors for every component,
/// so a link swapped in between a metadata check and creation cannot redirect the write.
#[cfg(unix)]
mod backup {
    use super::*;
    use std::os::fd::{AsRawFd, FromRawFd, OwnedFd};
    use std::os::unix::ffi::OsStrExt;

    fn cstring(value: &std::ffi::OsStr) -> Result<std::ffi::CString, String> {
        std::ffi::CString::new(value.as_bytes()).map_err(|_| "backup path contains a NUL byte".into())
    }

    fn open_dir(path: &Path) -> Result<OwnedFd, String> {
        let path = cstring(path.as_os_str())?;
        let raw = unsafe {
            libc::open(path.as_ptr(), libc::O_RDONLY | libc::O_DIRECTORY | libc::O_NOFOLLOW | libc::O_CLOEXEC)
        };
        if raw < 0 {
            return Err(format!("could not open the backup folder: {}", std::io::Error::last_os_error()));
        }
        Ok(unsafe { OwnedFd::from_raw_fd(raw) })
    }

    fn open_child(dir: &OwnedFd, name: &std::ffi::OsStr) -> Result<OwnedFd, String> {
        let name = cstring(name)?;
        let raw = unsafe {
            libc::openat(
                dir.as_raw_fd(),
                name.as_ptr(),
                libc::O_RDONLY | libc::O_DIRECTORY | libc::O_NOFOLLOW | libc::O_CLOEXEC,
            )
        };
        if raw < 0 {
            return Err(format!("could not open the backup folder: {}", std::io::Error::last_os_error()));
        }
        Ok(unsafe { OwnedFd::from_raw_fd(raw) })
    }

    fn private_dir(path: &Path) -> Result<OwnedFd, String> {
        if !path.is_absolute() {
            return Err("the backup folder must be an absolute path".into());
        }
        let mut ancestor = path;
        let mut missing = Vec::new();
        loop {
            match std::fs::symlink_metadata(ancestor) {
                Ok(meta) if meta.file_type().is_symlink() || !meta.is_dir() => {
                    return Err("the backup folder and its parents must be real directories, not links".into());
                }
                Ok(_) => break,
                Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
                    missing.push(ancestor.file_name().ok_or("invalid backup folder")?.to_os_string());
                    ancestor = ancestor.parent().ok_or("invalid backup folder")?;
                }
                Err(e) => return Err(format!("could not inspect the backup folder: {e}")),
            }
        }
        let mut dir = open_dir(ancestor)?;
        for name in missing.iter().rev() {
            let c_name = cstring(name)?;
            if unsafe { libc::mkdirat(dir.as_raw_fd(), c_name.as_ptr(), 0o700) } != 0 {
                let error = std::io::Error::last_os_error();
                if error.kind() != std::io::ErrorKind::AlreadyExists {
                    return Err(format!("could not create the backup folder: {error}"));
                }
            }
            dir = open_child(&dir, name)?;
        }
        if unsafe { libc::fchmod(dir.as_raw_fd(), 0o700) } != 0 {
            return Err(format!("could not make the backup folder private: {}", std::io::Error::last_os_error()));
        }
        Ok(dir)
    }

    pub fn save(root: &Path, name: &std::ffi::OsStr, text: &str) -> Result<PathBuf, String> {
        let dir = private_dir(root)?;
        let file_name = name.to_os_string();
        let name = cstring(name)?;
        let raw = unsafe {
            libc::openat(
                dir.as_raw_fd(),
                name.as_ptr(),
                libc::O_WRONLY | libc::O_CREAT | libc::O_EXCL | libc::O_NOFOLLOW | libc::O_CLOEXEC,
                0o600,
            )
        };
        if raw < 0 {
            return Err(format!("could not save a backup: {}", std::io::Error::last_os_error()));
        }
        let mut file = std::fs::File::from(unsafe { OwnedFd::from_raw_fd(raw) });
        file.write_all(text.as_bytes()).map_err(|e| format!("could not save a backup: {e}"))?;
        file.sync_all().map_err(|e| format!("could not save a backup: {e}"))?;
        Ok(root.join(file_name))
    }
}

/// The portable fallback checks every existing and newly created component immediately before `create_new` (which
/// refuses an existing final link). Windows has no std equivalent of Unix's openat/O_NOFOLLOW directory traversal.
#[cfg(not(unix))]
mod backup {
    use super::*;

    fn private_dir(path: &Path) -> Result<(), String> {
        let mut current = PathBuf::new();
        for component in path.components() {
            current.push(component);
            match std::fs::symlink_metadata(&current) {
                Ok(meta) if meta.file_type().is_symlink() || !meta.is_dir() => {
                    return Err("the backup folder and its parents must be real directories, not links".into());
                }
                Ok(_) => {}
                Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
                    std::fs::create_dir(&current).map_err(|e| format!("could not create the backup folder: {e}"))?;
                    let meta = std::fs::symlink_metadata(&current)
                        .map_err(|e| format!("could not inspect the backup folder: {e}"))?;
                    if meta.file_type().is_symlink() || !meta.is_dir() {
                        return Err("the backup folder and its parents must be real directories, not links".into());
                    }
                }
                Err(e) => return Err(format!("could not inspect the backup folder: {e}")),
            }
        }
        Ok(())
    }

    pub fn save(root: &Path, name: &std::ffi::OsStr, text: &str) -> Result<PathBuf, String> {
        private_dir(root)?;
        let path = root.join(name);
        let mut file = create_new(&path, 0o600).map_err(|e| format!("could not save a backup: {e}"))?;
        file.write_all(text.as_bytes()).map_err(|e| format!("could not save a backup: {e}"))?;
        file.sync_all().map_err(|e| format!("could not save a backup: {e}"))?;
        Ok(path)
    }
}

/// Write `text` to `path` atomically: a temporary file beside it, flushed, then renamed over it. An existing file's
/// permissions survive the replacement; a new settings file is owner-only on Unix.
fn write_atomic(path: &Path, text: &str) -> Result<(), String> {
    let dir = path.parent().ok_or("no folder for that file")?;
    std::fs::create_dir_all(dir).map_err(|e| format!("could not create {}: {e}", dir.display()))?;
    let permissions = std::fs::symlink_metadata(path).ok().map(|m| m.permissions());
    #[cfg(unix)]
    let mode = {
        use std::os::unix::fs::PermissionsExt;
        permissions.as_ref().map_or(0o600, |p| p.mode())
    };
    #[cfg(not(unix))]
    let mode = 0;
    let name = path.file_name().map(|n| n.to_string_lossy().into_owned()).unwrap_or_default();
    let random = crate::ui::random_hex().map_err(|e| format!("secure randomness is unavailable: {e}"))?;
    let tmp = dir.join(format!(".{name}.rigswig-{random}.tmp"));
    let result = (|| {
        let mut f = create_new(&tmp, mode)?;
        f.write_all(text.as_bytes())?;
        if let Some(p) = permissions {
            f.set_permissions(p)?;
        }
        f.sync_all()?;
        std::fs::rename(&tmp, path)
    })();
    if result.is_err() {
        let _ = std::fs::remove_file(&tmp);
    }
    result.map_err(|e| format!("could not write {}: {e}", path.display()))
}

/// Apply `plan`. `before_hash` is what the page saw; the file must still match it.
pub fn apply(plan: &Plan, before_hash: Option<&str>, backups: &Path) -> Result<Applied, String> {
    let now = current(&plan.path)?;
    let now_hash = now.as_deref().map(sha256_hex);
    if now_hash.as_deref() != before_hash || now_hash != plan.before_hash {
        return Err("the file changed since you looked at it; review the change again".into());
    }
    let backup = match &now {
        Some(text) => {
            let secs = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map_or(0, |d| d.as_secs());
            let name = plan.path.file_name().map(|n| n.to_string_lossy().into_owned()).unwrap_or_default();
            let random = crate::ui::random_hex().map_err(|e| format!("secure randomness is unavailable: {e}"))?;
            let name = std::ffi::OsString::from(format!("{secs}-{}-{name}", &random[..8]));
            Some(backup::save(backups, &name, text)?)
        }
        None => None,
    };
    write_atomic(&plan.path, &plan.after)?;
    Ok(Applied {
        path: plan.path.clone(),
        file: plan.file.clone(),
        before: now,
        after_hash: sha256_hex(&plan.after),
        backup,
    })
}

/// Undo `a`: put back what was there (or remove the file we created), if the file is still exactly what we wrote.
pub fn undo(a: &Applied) -> Result<(), String> {
    let now = current(&a.path)?;
    if now.as_deref().map(sha256_hex).as_deref() != Some(a.after_hash.as_str()) {
        return Err(format!("{} changed since the change was applied; restore it by hand from the backup", a.file));
    }
    match &a.before {
        Some(text) => write_atomic(&a.path, text),
        None => std::fs::remove_file(&a.path).map_err(|e| format!("could not remove {}: {e}", a.path.display())),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use rigswig_core::change::{Change, Scope};

    fn dir(tag: &str) -> PathBuf {
        let d = std::env::temp_dir().join(format!("rigswig-writes-{tag}-{}", std::process::id()));
        let _ = std::fs::remove_dir_all(&d);
        std::fs::create_dir_all(&d).unwrap();
        d
    }

    fn plan(path: &Path, before: Option<&str>, after: &str) -> Plan {
        Plan {
            change: Change::Plugin { id: "a@b".into(), scope: Scope::Local, on: false },
            file: "settings.local.json".into(),
            path: path.to_path_buf(),
            before_hash: before.map(sha256_hex),
            after: after.into(),
            creates_file: before.is_none(),
            diff: Vec::new(),
            tokens_before: 0,
            tokens_after: 0,
            takes_effect: true,
            notes: Vec::new(),
        }
    }

    #[test]
    fn apply_backs_up_and_undo_restores() {
        let d = dir("roundtrip");
        let f = d.join(".claude/settings.json");
        std::fs::create_dir_all(f.parent().unwrap()).unwrap();
        std::fs::write(&f, "old\n").unwrap();
        let p = plan(&f, Some("old\n"), "new\n");
        let a = apply(&p, p.before_hash.as_deref(), &d.join("backups")).unwrap();
        assert_eq!(std::fs::read_to_string(&f).unwrap(), "new\n");
        assert_eq!(std::fs::read_to_string(a.backup.as_ref().unwrap()).unwrap(), "old\n");
        assert!(
            std::fs::read_dir(f.parent().unwrap()).unwrap().all(|e| !e
                .unwrap()
                .file_name()
                .to_string_lossy()
                .contains(".tmp"))
        );
        undo(&a).unwrap();
        assert_eq!(std::fs::read_to_string(&f).unwrap(), "old\n");
        let _ = std::fs::remove_dir_all(&d);
    }

    #[test]
    fn a_created_file_is_removed_by_undo() {
        let d = dir("create");
        let f = d.join(".claude/settings.local.json");
        let p = plan(&f, None, "{}\n");
        let a = apply(&p, None, &d.join("backups")).unwrap();
        assert!(a.backup.is_none());
        undo(&a).unwrap();
        assert!(!f.exists());
        let _ = std::fs::remove_dir_all(&d);
    }

    #[cfg(unix)]
    fn mode(path: &Path) -> u32 {
        use std::os::unix::fs::PermissionsExt;
        std::fs::metadata(path).unwrap().permissions().mode() & 0o777
    }

    #[cfg(unix)]
    #[test]
    fn atomic_replacement_preserves_the_settings_files_permissions() {
        let d = dir("permissions");
        let f = d.join("settings.json");
        std::fs::write(&f, "old\n").unwrap();
        use std::os::unix::fs::PermissionsExt;
        std::fs::set_permissions(&f, std::fs::Permissions::from_mode(0o600)).unwrap();
        let p = plan(&f, Some("old\n"), "new\n");
        apply(&p, p.before_hash.as_deref(), &d.join("backups")).unwrap();
        assert_eq!(mode(&f), 0o600, "atomic replacement must not broaden the settings file");
        let _ = std::fs::remove_dir_all(&d);
    }

    #[cfg(unix)]
    #[test]
    fn backups_are_private() {
        let d = dir("private-backup");
        let f = d.join("settings.json");
        std::fs::write(&f, "old\n").unwrap();
        let backups = d.join("backups");
        let p = plan(&f, Some("old\n"), "new\n");
        let a = apply(&p, p.before_hash.as_deref(), &backups).unwrap();
        assert_eq!(mode(&backups), 0o700);
        assert_eq!(mode(a.backup.as_ref().unwrap()), 0o600);
        let _ = std::fs::remove_dir_all(&d);
    }

    #[cfg(unix)]
    #[test]
    fn symlinked_backup_paths_are_refused_without_touching_their_targets() {
        for parent_link in [false, true] {
            let d = dir(if parent_link { "backup-parent-link" } else { "backup-dir-link" });
            let f = d.join("settings.json");
            std::fs::write(&f, "old\n").unwrap();
            let outside = d.join("outside");
            std::fs::create_dir(&outside).unwrap();
            use std::os::unix::fs::PermissionsExt;
            std::fs::set_permissions(&outside, std::fs::Permissions::from_mode(0o755)).unwrap();
            let backups = if parent_link {
                let parent = d.join("container");
                std::os::unix::fs::symlink(&outside, &parent).unwrap();
                parent.join("backups")
            } else {
                let backups = d.join("backups");
                std::os::unix::fs::symlink(&outside, &backups).unwrap();
                backups
            };
            let p = plan(&f, Some("old\n"), "new\n");
            let error = apply(&p, p.before_hash.as_deref(), &backups).unwrap_err();
            assert!(error.contains("backup"), "{error}");
            assert_eq!(std::fs::read_to_string(&f).unwrap(), "old\n", "the settings write never happens");
            assert_eq!(mode(&outside), 0o755, "chmod never follows a backup link");
            assert_eq!(std::fs::read_dir(&outside).unwrap().count(), 0, "no backup lands outside");
            let _ = std::fs::remove_dir_all(&d);
        }
    }

    #[cfg(unix)]
    #[test]
    fn a_created_settings_file_is_owner_only() {
        let d = dir("new-permissions");
        let f = d.join("settings.json");
        let p = plan(&f, None, "{}\n");
        apply(&p, None, &d.join("backups")).unwrap();
        assert_eq!(mode(&f), 0o600);
        let _ = std::fs::remove_dir_all(&d);
    }

    #[test]
    fn stale_plans_links_and_edited_files_are_refused() {
        let d = dir("refuse");
        let f = d.join("s.json");
        std::fs::write(&f, "old\n").unwrap();
        let p = plan(&f, Some("old\n"), "new\n");
        std::fs::write(&f, "someone else\n").unwrap();
        assert!(apply(&p, p.before_hash.as_deref(), &d.join("b")).unwrap_err().contains("changed since"));
        std::fs::write(&f, "old\n").unwrap();
        assert!(
            apply(&p, Some("not-the-hash"), &d.join("b")).unwrap_err().contains("changed since"),
            "the page's hash is checked too"
        );

        let a = apply(&p, p.before_hash.as_deref(), &d.join("b")).unwrap();
        std::fs::write(&f, "edited after\n").unwrap();
        assert!(undo(&a).unwrap_err().contains("by hand"));
        assert_eq!(std::fs::read_to_string(&f).unwrap(), "edited after\n", "undo never overwrites someone else's edit");

        #[cfg(unix)]
        {
            let target = d.join("target.json");
            std::fs::write(&target, "t\n").unwrap();
            let link = d.join("link.json");
            std::os::unix::fs::symlink(&target, &link).unwrap();
            let lp = plan(&link, Some("t\n"), "x\n");
            assert!(apply(&lp, lp.before_hash.as_deref(), &d.join("b")).unwrap_err().contains("link"));
            assert_eq!(std::fs::read_to_string(&target).unwrap(), "t\n");
        }
        let _ = std::fs::remove_dir_all(&d);
    }
}