gravityfile-tui 0.2.4

Terminal user interface for gravityfile
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
//! Deletion operations with trash support and symlink safety.

use std::fs;
use std::path::PathBuf;

use tokio::sync::mpsc;

use super::constants::SCAN_CHANNEL_SIZE;
use super::state::{DeletionProgress, ScanResult};

/// Deletion mode - determines if files go to trash or are permanently deleted.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum DeletionMode {
    /// Move files to system trash (recoverable).
    #[default]
    Trash,
    /// Permanently delete files (not recoverable).
    /// Reserved for future implementation of permanent deletion option.
    #[allow(dead_code)]
    Permanent,
}

/// Start background deletion of files/directories.
///
/// Takes a list of (path, size) tuples and returns a receiver for progress updates.
/// By default uses trash for safe, recoverable deletion.
pub fn start_deletion(items: Vec<(PathBuf, u64)>) -> mpsc::Receiver<ScanResult> {
    start_deletion_with_mode(items, DeletionMode::Trash)
}

/// Start background deletion with explicit mode selection.
pub fn start_deletion_with_mode(
    items: Vec<(PathBuf, u64)>,
    mode: DeletionMode,
) -> mpsc::Receiver<ScanResult> {
    let (tx, rx) = mpsc::channel(SCAN_CHANNEL_SIZE);
    let total = items.len();

    if total == 0 {
        return rx;
    }

    tokio::spawn(async move {
        let mut deleted = 0;
        let mut failed = 0;
        let mut bytes_freed: u64 = 0;

        for (i, (path, size)) in items.iter().enumerate() {
            // Send progress update
            let _ = tx
                .send(ScanResult::DeletionProgress(DeletionProgress {
                    total,
                    deleted,
                    failed,
                    bytes_freed,
                    current: Some(path.clone()),
                }))
                .await;

            // Perform deletion in blocking task to not block the async runtime
            let path_clone = path.clone();
            let result = tokio::task::spawn_blocking(move || delete_path(&path_clone, mode)).await;

            match result {
                Ok(Ok(())) => {
                    deleted += 1;
                    bytes_freed += size;
                }
                Ok(Err(e)) => {
                    tracing::warn!("Failed to delete {:?}: {}", path, e);
                    failed += 1;
                }
                Err(e) => {
                    tracing::warn!("Deletion task panicked for {:?}: {}", path, e);
                    failed += 1;
                }
            }

            // Send updated progress after each deletion
            if i < total - 1 {
                let _ = tx
                    .send(ScanResult::DeletionProgress(DeletionProgress {
                        total,
                        deleted,
                        failed,
                        bytes_freed,
                        current: items.get(i + 1).map(|(p, _)| p.clone()),
                    }))
                    .await;
            }
        }

        // Send completion
        let _ = tx
            .send(ScanResult::DeletionComplete {
                deleted,
                failed,
                bytes_freed,
            })
            .await;
    });

    rx
}

/// Delete a single path with proper symlink handling.
///
/// Key behaviors:
/// - Symlinks are always removed as files (never follow the target)
/// - Directories use remove_dir_all (which also handles symlinks safely per Rust docs)
/// - Regular files use remove_file
fn delete_path(path: &PathBuf, mode: DeletionMode) -> Result<(), String> {
    // First, check if path exists at all
    // Use symlink_metadata to not follow symlinks
    let metadata = match fs::symlink_metadata(path) {
        Ok(m) => m,
        Err(e) => return Err(format!("Cannot access path: {}", e)),
    };

    match mode {
        DeletionMode::Trash => {
            // Use trash crate for safe, recoverable deletion
            // The trash crate handles symlinks correctly
            trash::delete(path).map_err(|e| format!("Failed to move to trash: {}", e))
        }
        DeletionMode::Permanent => {
            // IMPORTANT: Check symlink FIRST before checking is_dir()
            // is_dir() follows symlinks, so a symlink to a directory would return true
            // But we want to delete the symlink itself, not follow it
            if metadata.is_symlink() {
                // Symlinks are always deleted as files, regardless of what they point to
                fs::remove_file(path).map_err(|e| format!("Failed to remove symlink: {}", e))
            } else if metadata.is_dir() {
                // Regular directory - remove recursively
                fs::remove_dir_all(path).map_err(|e| format!("Failed to remove directory: {}", e))
            } else {
                // Regular file
                fs::remove_file(path).map_err(|e| format!("Failed to remove file: {}", e))
            }
        }
    }
}

/// Format deletion result as a user-friendly message.
pub fn format_deletion_result(deleted: usize, failed: usize, bytes_freed: u64) -> (bool, String) {
    use crate::ui::format_size;

    let success = failed == 0;
    let msg = if success {
        format!(
            "Moved {} items to trash, freed {}",
            deleted,
            format_size(bytes_freed)
        )
    } else if deleted > 0 {
        format!(
            "Moved {} to trash, {} failed (freed {})",
            deleted,
            failed,
            format_size(bytes_freed)
        )
    } else {
        format!("Failed to delete {} items", failed)
    };

    (success, msg)
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs::{self, File};
    use std::io::Write;
    use tempfile::TempDir;

    #[test]
    fn test_delete_regular_file() {
        let temp_dir = TempDir::new().unwrap();
        let file_path = temp_dir.path().join("test.txt");
        File::create(&file_path).unwrap();

        assert!(file_path.exists());
        delete_path(&file_path, DeletionMode::Permanent).unwrap();
        assert!(!file_path.exists());
    }

    #[test]
    fn test_delete_directory() {
        let temp_dir = TempDir::new().unwrap();
        let dir_path = temp_dir.path().join("subdir");
        fs::create_dir(&dir_path).unwrap();
        File::create(dir_path.join("file.txt")).unwrap();

        assert!(dir_path.exists());
        delete_path(&dir_path, DeletionMode::Permanent).unwrap();
        assert!(!dir_path.exists());
    }

    #[test]
    fn test_delete_nested_directory() {
        let temp_dir = TempDir::new().unwrap();
        let dir_path = temp_dir.path().join("level1");
        fs::create_dir(&dir_path).unwrap();
        fs::create_dir(dir_path.join("level2")).unwrap();
        fs::create_dir(dir_path.join("level2/level3")).unwrap();
        File::create(dir_path.join("level2/level3/deep_file.txt")).unwrap();

        assert!(dir_path.exists());
        delete_path(&dir_path, DeletionMode::Permanent).unwrap();
        assert!(!dir_path.exists());
    }

    #[test]
    fn test_delete_nonexistent_path() {
        let temp_dir = TempDir::new().unwrap();
        let nonexistent = temp_dir.path().join("does_not_exist.txt");

        let result = delete_path(&nonexistent, DeletionMode::Permanent);
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("Cannot access path"));
    }

    #[test]
    fn test_delete_empty_directory() {
        let temp_dir = TempDir::new().unwrap();
        let empty_dir = temp_dir.path().join("empty");
        fs::create_dir(&empty_dir).unwrap();

        assert!(empty_dir.exists());
        delete_path(&empty_dir, DeletionMode::Permanent).unwrap();
        assert!(!empty_dir.exists());
    }

    #[test]
    fn test_delete_file_with_content() {
        let temp_dir = TempDir::new().unwrap();
        let file_path = temp_dir.path().join("content.txt");
        let mut file = File::create(&file_path).unwrap();
        writeln!(file, "Some content that should be deleted").unwrap();
        drop(file);

        assert!(file_path.exists());
        delete_path(&file_path, DeletionMode::Permanent).unwrap();
        assert!(!file_path.exists());
    }

    #[cfg(unix)]
    #[test]
    fn test_delete_symlink_to_file() {
        use std::os::unix::fs::symlink;

        let temp_dir = TempDir::new().unwrap();
        let file_path = temp_dir.path().join("target.txt");
        let link_path = temp_dir.path().join("link.txt");

        File::create(&file_path).unwrap();
        symlink(&file_path, &link_path).unwrap();

        // Verify setup
        assert!(file_path.exists());
        assert!(link_path.symlink_metadata().unwrap().is_symlink());

        // Delete the symlink
        delete_path(&link_path, DeletionMode::Permanent).unwrap();

        // Symlink should be gone, but target should remain
        assert!(!link_path.exists());
        assert!(
            file_path.exists(),
            "Target file should NOT be deleted when deleting symlink"
        );
    }

    #[cfg(unix)]
    #[test]
    fn test_delete_symlink_to_directory() {
        use std::os::unix::fs::symlink;

        let temp_dir = TempDir::new().unwrap();
        let dir_path = temp_dir.path().join("target_dir");
        let link_path = temp_dir.path().join("link_dir");

        fs::create_dir(&dir_path).unwrap();
        File::create(dir_path.join("file.txt")).unwrap();
        symlink(&dir_path, &link_path).unwrap();

        // Verify setup
        assert!(dir_path.exists());
        assert!(link_path.symlink_metadata().unwrap().is_symlink());

        // Delete the symlink
        delete_path(&link_path, DeletionMode::Permanent).unwrap();

        // Symlink should be gone, but target directory and contents should remain
        assert!(!link_path.exists());
        assert!(
            dir_path.exists(),
            "Target directory should NOT be deleted when deleting symlink"
        );
        assert!(
            dir_path.join("file.txt").exists(),
            "Files in target directory should NOT be deleted"
        );
    }

    #[cfg(unix)]
    #[test]
    fn test_delete_broken_symlink() {
        use std::os::unix::fs::symlink;

        let temp_dir = TempDir::new().unwrap();
        let link_path = temp_dir.path().join("broken_link");

        // Create symlink to non-existent target
        symlink("/nonexistent/path/that/does/not/exist", &link_path).unwrap();

        // Verify it's a broken symlink
        assert!(link_path.symlink_metadata().unwrap().is_symlink());
        assert!(!link_path.exists()); // exists() follows symlinks, returns false for broken

        // Should be able to delete broken symlink
        delete_path(&link_path, DeletionMode::Permanent).unwrap();

        // Verify symlink is gone
        assert!(link_path.symlink_metadata().is_err());
    }

    #[cfg(unix)]
    #[test]
    fn test_delete_symlink_chain() {
        use std::os::unix::fs::symlink;

        let temp_dir = TempDir::new().unwrap();
        let file_path = temp_dir.path().join("target.txt");
        let link1_path = temp_dir.path().join("link1");
        let link2_path = temp_dir.path().join("link2");

        File::create(&file_path).unwrap();
        symlink(&file_path, &link1_path).unwrap();
        symlink(&link1_path, &link2_path).unwrap(); // link2 -> link1 -> file

        // Delete the middle link
        delete_path(&link1_path, DeletionMode::Permanent).unwrap();

        // link1 should be gone
        assert!(link1_path.symlink_metadata().is_err());
        // Original file should remain
        assert!(file_path.exists());
        // link2 now points to nothing (broken)
        assert!(link2_path.symlink_metadata().unwrap().is_symlink());
        assert!(!link2_path.exists()); // broken link
    }

    #[cfg(unix)]
    #[test]
    fn test_delete_directory_containing_symlinks() {
        use std::os::unix::fs::symlink;

        let temp_dir = TempDir::new().unwrap();
        let outside_file = temp_dir.path().join("outside.txt");
        let dir_path = temp_dir.path().join("container");
        let link_in_dir = dir_path.join("link_to_outside");

        File::create(&outside_file).unwrap();
        fs::create_dir(&dir_path).unwrap();
        symlink(&outside_file, &link_in_dir).unwrap();

        // Delete the directory containing the symlink
        delete_path(&dir_path, DeletionMode::Permanent).unwrap();

        // Directory and symlink should be gone
        assert!(!dir_path.exists());
        // But the outside file should remain
        assert!(
            outside_file.exists(),
            "File outside directory should NOT be deleted"
        );
    }

    #[test]
    fn test_format_deletion_result_success() {
        let (success, msg) = format_deletion_result(5, 0, 1024 * 1024);
        assert!(success);
        assert!(msg.contains("5 items"));
        assert!(msg.contains("trash"));
    }

    #[test]
    fn test_format_deletion_result_partial_failure() {
        let (success, msg) = format_deletion_result(3, 2, 512 * 1024);
        assert!(!success);
        assert!(msg.contains("3"));
        assert!(msg.contains("2 failed"));
    }

    #[test]
    fn test_format_deletion_result_total_failure() {
        let (success, msg) = format_deletion_result(0, 5, 0);
        assert!(!success);
        assert!(msg.contains("Failed"));
        assert!(msg.contains("5"));
    }

    #[test]
    fn test_deletion_mode_default() {
        let mode = DeletionMode::default();
        assert_eq!(mode, DeletionMode::Trash);
    }
}