ncoxide 0.4.0

Modal dual-pane file commander with helix-inspired interface
Documentation
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
use std::fs;
use std::path::{Path, PathBuf};

use crate::error::{NcError, Result};
use crate::platform;

/// Refuse a transfer whose destination entry is the source itself or lies
/// inside a source directory (`cp`/`mv`-style guard). Both are destructive:
/// `fs::copy` onto the same path truncates the file to zero bytes before
/// reading, and copying a directory into its own subtree recurses into the
/// tree it is creating.
fn ensure_safe_transfer(source: &Path, target_dir: &Path) -> Result<()> {
    let name = source
        .file_name()
        .ok_or_else(|| NcError::FileOperation("Invalid source path".into()))?;
    // Canonicalize the source's *parent* and re-attach the final component,
    // so a symlink source compares as the link entry itself, not its target.
    let src_parent = match source.parent() {
        Some(p) if !p.as_os_str().is_empty() => p,
        _ => Path::new("."),
    };
    let src = fs::canonicalize(src_parent)
        .map_err(NcError::Io)?
        .join(name);
    let dest = fs::canonicalize(target_dir)
        .map_err(NcError::Io)?
        .join(name);

    if dest == src {
        return Err(NcError::FileOperation(format!(
            "source and destination are the same: {}",
            src.display()
        )));
    }
    if dest.starts_with(&src) {
        return Err(NcError::FileOperation(format!(
            "cannot transfer '{}' into itself ('{}')",
            source.display(),
            dest.display()
        )));
    }
    // A destination entry that is a symlink or hard link back to the source
    // defeats the path comparison above: fs::copy would open through it and
    // still truncate the source. Compare device+inode when the destination
    // exists (both stats follow symlinks), like cp's same-file check.
    use std::os::unix::fs::MetadataExt;
    if let (Ok(s), Ok(d)) = (fs::metadata(source), fs::metadata(&dest))
        && s.dev() == d.dev()
        && s.ino() == d.ino()
    {
        return Err(NcError::FileOperation(format!(
            "source and destination are the same file: {}",
            source.display()
        )));
    }
    Ok(())
}

/// Copy a file or directory to a target directory.
/// Symlinks are preserved as symlinks rather than followed.
/// Refuses self- and nested-destination transfers (see [`ensure_safe_transfer`]).
pub fn copy_to(source: &Path, target_dir: &Path) -> Result<()> {
    ensure_safe_transfer(source, target_dir)?;
    let name = source
        .file_name()
        .ok_or_else(|| NcError::FileOperation("Invalid source path".into()))?;
    let dest = target_dir.join(name);

    let meta = fs::symlink_metadata(source).map_err(NcError::Io)?;
    if meta.is_symlink() {
        let link_target = fs::read_link(source).map_err(NcError::Io)?;
        // Remove existing destination if present so symlink creation succeeds
        if dest.symlink_metadata().is_ok() {
            fs::remove_file(&dest).map_err(NcError::Io)?;
        }
        std::os::unix::fs::symlink(&link_target, &dest).map_err(NcError::Io)?;
        Ok(())
    } else if meta.is_dir() {
        copy_dir_recursive(source, &dest)
    } else {
        fs::copy(source, &dest).map_err(NcError::Io)?;
        Ok(())
    }
}

fn copy_dir_recursive(src: &Path, dst: &Path) -> Result<()> {
    fs::create_dir_all(dst).map_err(NcError::Io)?;
    for entry in fs::read_dir(src).map_err(NcError::Io)? {
        let entry = entry.map_err(NcError::Io)?;
        let src_path = entry.path();
        let dst_path = dst.join(entry.file_name());
        let meta = fs::symlink_metadata(&src_path).map_err(NcError::Io)?;
        if meta.is_symlink() {
            let link_target = fs::read_link(&src_path).map_err(NcError::Io)?;
            if dst_path.symlink_metadata().is_ok() {
                fs::remove_file(&dst_path).map_err(NcError::Io)?;
            }
            std::os::unix::fs::symlink(&link_target, &dst_path).map_err(NcError::Io)?;
        } else if meta.is_dir() {
            copy_dir_recursive(&src_path, &dst_path)?;
        } else {
            fs::copy(&src_path, &dst_path).map_err(NcError::Io)?;
        }
    }
    Ok(())
}

/// Calculate total size of a path (recursive for directories).
/// Uses symlink_metadata to avoid following symlinks.
pub fn path_size(path: &Path) -> Result<u64> {
    let meta = fs::symlink_metadata(path).map_err(NcError::Io)?;
    if meta.is_symlink() || meta.is_file() {
        Ok(meta.len())
    } else if meta.is_dir() {
        let mut total = 0u64;
        for entry in fs::read_dir(path).map_err(NcError::Io)? {
            let entry = entry.map_err(NcError::Io)?;
            // Propagate errors so an unreadable subtree doesn't silently
            // under-count the size used by the disk-space pre-check.
            total = total.saturating_add(path_size(&entry.path())?);
        }
        Ok(total)
    } else {
        Ok(0)
    }
}

/// Move a file or directory to a target directory.
/// Refuses self- and nested-destination transfers (see [`ensure_safe_transfer`]).
pub fn move_to(source: &Path, target_dir: &Path) -> Result<()> {
    ensure_safe_transfer(source, target_dir)?;
    let name = source
        .file_name()
        .ok_or_else(|| NcError::FileOperation("Invalid source path".into()))?;
    let dest = target_dir.join(name);

    // Try rename first (same filesystem), fall back to copy+delete
    match fs::rename(source, &dest) {
        Ok(()) => Ok(()),
        Err(_) => {
            // Unlike the rename above, copy+delete needs free space at the
            // destination — fail before copying anything partial. Fail-open
            // when free space or source size can't be determined.
            check_fallback_space(source, target_dir)?;
            copy_to(source, target_dir)?;
            if let Err(e) = delete(source) {
                return Err(NcError::FileOperation(format!(
                    "Copied to {} but failed to remove source: {e}",
                    dest.display()
                )));
            }
            Ok(())
        }
    }
}

/// Space check for `move_to`'s copy+delete fallback. Errors only when both
/// the destination's free space and the source size are known and the source
/// doesn't fit (see [`space_shortfall`]).
fn check_fallback_space(source: &Path, target_dir: &Path) -> Result<()> {
    let Some(free) = platform::get_free_disk_space(target_dir) else {
        return Ok(());
    };
    let Ok(needed) = path_size(source) else {
        return Ok(());
    };
    match space_shortfall(needed, free) {
        Some(msg) => Err(NcError::FileOperation(msg)),
        None => Ok(()),
    }
}

/// `Some(message)` when `needed` bytes don't fit into `free` bytes.
fn space_shortfall(needed: u64, free: u64) -> Option<String> {
    (needed > free).then(|| {
        format!(
            "not enough disk space: need {} but only {} available",
            platform::format_file_size(needed),
            platform::format_file_size(free),
        )
    })
}

/// Delete a file or directory (recursive for directories).
/// Uses symlink_metadata so symlinks are removed as links, not followed.
pub fn delete(path: &Path) -> Result<()> {
    let meta = fs::symlink_metadata(path).map_err(NcError::Io)?;
    if meta.is_dir() {
        fs::remove_dir_all(path).map_err(NcError::Io)
    } else {
        // Covers regular files, symlinks, and other non-directory types
        fs::remove_file(path).map_err(NcError::Io)
    }
}

/// Rename a file or directory.
pub fn rename(source: &Path, new_name: &str) -> Result<PathBuf> {
    let parent = source
        .parent()
        .ok_or_else(|| NcError::FileOperation("Cannot rename root".into()))?;
    let dest = parent.join(new_name);
    fs::rename(source, &dest).map_err(NcError::Io)?;
    Ok(dest)
}

/// Create a new directory.
///
/// Uses `create_dir` (not `create_dir_all`) so it creates exactly one
/// directory and errors if the target already exists, preserving the
/// caller's collision handling.
pub fn mkdir(parent: &Path, name: &str) -> Result<PathBuf> {
    let path = parent.join(name);
    fs::create_dir(&path).map_err(NcError::Io)?;
    Ok(path)
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;

    #[test]
    fn test_copy_file() {
        let tmp = std::env::temp_dir().join(format!("ncoxide_test_copy_{}", std::process::id()));
        let _ = fs::remove_dir_all(&tmp);
        let src = tmp.join("src");
        let dst = tmp.join("dst");
        fs::create_dir_all(&src).unwrap();
        fs::create_dir_all(&dst).unwrap();
        fs::write(src.join("file.txt"), "hello").unwrap();

        copy_to(&src.join("file.txt"), &dst).unwrap();
        assert!(dst.join("file.txt").exists());
        assert_eq!(fs::read_to_string(dst.join("file.txt")).unwrap(), "hello");

        let _ = fs::remove_dir_all(&tmp);
    }

    #[test]
    fn test_copy_dir() {
        let tmp = std::env::temp_dir().join(format!("ncoxide_test_copydir_{}", std::process::id()));
        let _ = fs::remove_dir_all(&tmp);
        let src = tmp.join("src");
        let dst = tmp.join("dst");
        let subdir = src.join("inner");
        fs::create_dir_all(&subdir).unwrap();
        fs::create_dir_all(&dst).unwrap();
        fs::write(subdir.join("file.txt"), "nested").unwrap();

        copy_to(&src, &dst).unwrap();
        assert!(dst.join("src").join("inner").join("file.txt").exists());

        let _ = fs::remove_dir_all(&tmp);
    }

    #[test]
    fn test_move_file() {
        let tmp = std::env::temp_dir().join(format!("ncoxide_test_move_{}", std::process::id()));
        let _ = fs::remove_dir_all(&tmp);
        let src_dir = tmp.join("src");
        let dst_dir = tmp.join("dst");
        fs::create_dir_all(&src_dir).unwrap();
        fs::create_dir_all(&dst_dir).unwrap();
        fs::write(src_dir.join("file.txt"), "moveme").unwrap();

        move_to(&src_dir.join("file.txt"), &dst_dir).unwrap();
        assert!(!src_dir.join("file.txt").exists());
        assert!(dst_dir.join("file.txt").exists());

        let _ = fs::remove_dir_all(&tmp);
    }

    #[test]
    fn test_delete() {
        let tmp = std::env::temp_dir().join(format!("ncoxide_test_delete_{}", std::process::id()));
        let _ = fs::remove_dir_all(&tmp);
        fs::create_dir_all(&tmp).unwrap();
        fs::write(tmp.join("file.txt"), "deleteme").unwrap();

        delete(&tmp.join("file.txt")).unwrap();
        assert!(!tmp.join("file.txt").exists());

        let _ = fs::remove_dir_all(&tmp);
    }

    #[test]
    fn test_rename() {
        let tmp = std::env::temp_dir().join(format!("ncoxide_test_rename_{}", std::process::id()));
        let _ = fs::remove_dir_all(&tmp);
        fs::create_dir_all(&tmp).unwrap();
        fs::write(tmp.join("old.txt"), "renamed").unwrap();

        let new_path = rename(&tmp.join("old.txt"), "new.txt").unwrap();
        assert!(!tmp.join("old.txt").exists());
        assert_eq!(new_path, tmp.join("new.txt"));
        assert!(new_path.exists());

        let _ = fs::remove_dir_all(&tmp);
    }

    #[test]
    fn test_mkdir() {
        let tmp = std::env::temp_dir().join(format!("ncoxide_test_mkdir_{}", std::process::id()));
        let _ = fs::remove_dir_all(&tmp);
        fs::create_dir_all(&tmp).unwrap();

        let new_dir = mkdir(&tmp, "newdir").unwrap();
        assert!(new_dir.is_dir());

        let _ = fs::remove_dir_all(&tmp);
    }

    #[test]
    fn test_mkdir_existing_errors() {
        let tmp = std::env::temp_dir().join(format!(
            "ncoxide_test_mkdir_existing_{}",
            std::process::id()
        ));
        let _ = fs::remove_dir_all(&tmp);
        fs::create_dir_all(&tmp).unwrap();

        // First creation succeeds; a second with the same name must error
        // (create_dir, not create_dir_all) so collision handling is preserved.
        mkdir(&tmp, "dup").unwrap();
        assert!(mkdir(&tmp, "dup").is_err());

        let _ = fs::remove_dir_all(&tmp);
    }

    #[test]
    fn test_space_shortfall_comparison() {
        // Locks the comparison direction for the fallback space check (R5):
        // only a genuine shortfall errors; exact fit passes.
        assert!(space_shortfall(10, 5).is_some());
        assert!(space_shortfall(5, 10).is_none());
        assert!(space_shortfall(5, 5).is_none());
    }

    /// Unique per-process temp dir for the transfer-guard tests (R1).
    fn guard_dir(name: &str) -> std::path::PathBuf {
        let tmp = std::env::temp_dir().join(format!("ncoxide_guard_{name}_{}", std::process::id()));
        let _ = fs::remove_dir_all(&tmp);
        fs::create_dir_all(&tmp).unwrap();
        tmp
    }

    #[test]
    fn test_copy_file_onto_itself_refused() {
        // fs::copy(src, src) truncates the file to zero bytes — must refuse.
        let tmp = guard_dir("self_file");
        fs::write(tmp.join("data.txt"), "important").unwrap();

        assert!(copy_to(&tmp.join("data.txt"), &tmp).is_err());
        assert_eq!(
            fs::read_to_string(tmp.join("data.txt")).unwrap(),
            "important"
        );

        let _ = fs::remove_dir_all(&tmp);
    }

    #[test]
    fn test_copy_dir_onto_itself_refused() {
        let tmp = guard_dir("self_dir");
        let sub = tmp.join("sub");
        fs::create_dir_all(&sub).unwrap();
        fs::write(sub.join("f.txt"), "keep").unwrap();

        assert!(copy_to(&sub, &tmp).is_err());
        assert_eq!(fs::read_to_string(sub.join("f.txt")).unwrap(), "keep");

        let _ = fs::remove_dir_all(&tmp);
    }

    #[test]
    fn test_copy_dir_into_own_subdir_refused() {
        // Copying /x into /x/inner would recurse into the tree being created.
        let tmp = guard_dir("nested");
        let src = tmp.join("src_dir");
        let inner = src.join("inner");
        fs::create_dir_all(&inner).unwrap();

        assert!(copy_to(&src, &inner).is_err());
        assert!(!inner.join("src_dir").exists());

        let _ = fs::remove_dir_all(&tmp);
    }

    #[test]
    fn test_copy_into_symlinked_same_dir_refused() {
        // The target dir is a symlink resolving to the source's own directory;
        // the guard must see through it.
        let tmp = guard_dir("symlink_target");
        fs::write(tmp.join("data.txt"), "important").unwrap();
        let alias =
            std::env::temp_dir().join(format!("ncoxide_guard_alias_{}", std::process::id()));
        let _ = fs::remove_file(&alias);
        std::os::unix::fs::symlink(&tmp, &alias).unwrap();

        assert!(copy_to(&tmp.join("data.txt"), &alias).is_err());
        assert_eq!(
            fs::read_to_string(tmp.join("data.txt")).unwrap(),
            "important"
        );

        let _ = fs::remove_file(&alias);
        let _ = fs::remove_dir_all(&tmp);
    }

    #[test]
    fn test_copy_onto_symlinked_destination_refused() {
        // dest/f.txt is a symlink back to the source: the path guard can't
        // see it, but the inode check must (verification review finding 1).
        let tmp = guard_dir("symdest");
        let a = tmp.join("a");
        let b = tmp.join("b");
        fs::create_dir_all(&a).unwrap();
        fs::create_dir_all(&b).unwrap();
        fs::write(a.join("f.txt"), "important").unwrap();
        std::os::unix::fs::symlink(a.join("f.txt"), b.join("f.txt")).unwrap();

        assert!(copy_to(&a.join("f.txt"), &b).is_err());
        assert_eq!(fs::read_to_string(a.join("f.txt")).unwrap(), "important");

        let _ = fs::remove_dir_all(&tmp);
    }

    #[test]
    fn test_copy_onto_hardlinked_destination_refused() {
        let tmp = guard_dir("harddest");
        let a = tmp.join("a");
        let b = tmp.join("b");
        fs::create_dir_all(&a).unwrap();
        fs::create_dir_all(&b).unwrap();
        fs::write(a.join("f.txt"), "important").unwrap();
        fs::hard_link(a.join("f.txt"), b.join("f.txt")).unwrap();

        assert!(copy_to(&a.join("f.txt"), &b).is_err());
        assert!(move_to(&a.join("f.txt"), &b).is_err());
        assert_eq!(fs::read_to_string(a.join("f.txt")).unwrap(), "important");

        let _ = fs::remove_dir_all(&tmp);
    }

    #[test]
    fn test_move_file_onto_itself_refused() {
        let tmp = guard_dir("move_self");
        fs::write(tmp.join("data.txt"), "important").unwrap();

        assert!(move_to(&tmp.join("data.txt"), &tmp).is_err());
        assert_eq!(
            fs::read_to_string(tmp.join("data.txt")).unwrap(),
            "important"
        );

        let _ = fs::remove_dir_all(&tmp);
    }

    #[test]
    fn test_move_dir_into_own_subdir_refused() {
        let tmp = guard_dir("move_nested");
        let src = tmp.join("src_dir");
        let inner = src.join("inner");
        fs::create_dir_all(&inner).unwrap();
        fs::write(src.join("f.txt"), "keep").unwrap();

        assert!(move_to(&src, &inner).is_err());
        assert!(src.is_dir());
        assert_eq!(fs::read_to_string(src.join("f.txt")).unwrap(), "keep");

        let _ = fs::remove_dir_all(&tmp);
    }

    #[test]
    fn test_path_size_recursive() {
        let tmp =
            std::env::temp_dir().join(format!("ncoxide_test_path_size_{}", std::process::id()));
        let _ = fs::remove_dir_all(&tmp);
        let inner = tmp.join("inner");
        fs::create_dir_all(&inner).unwrap();
        fs::write(tmp.join("a.txt"), "12345").unwrap(); // 5 bytes
        fs::write(inner.join("b.txt"), "678").unwrap(); // 3 bytes

        let size = path_size(&tmp).unwrap();
        assert_eq!(size, 8, "expected sum of nested file sizes");

        let _ = fs::remove_dir_all(&tmp);
    }
}