rustdupe 0.2.0

Smart duplicate file finder with interactive TUI
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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
//! Hardlink detection for avoiding false duplicate identification.
//!
//! # Overview
//!
//! Hardlinks are multiple directory entries pointing to the same inode on disk.
//! They share the same content but are NOT duplicates - they're the same file.
//! This module detects hardlinks to prevent counting them as duplicates.
//!
//! # Platform Support
//!
//! - **Unix**: Uses (device_id, inode) pairs from file metadata
//! - **Windows**: Uses file_index from file handle (requires opening file)
//! - **Other**: Hardlink detection disabled (all files treated as unique)
//!
//! # Example
//!
//! ```
//! use rustdupe::scanner::hardlink::HardlinkTracker;
//! use std::path::Path;
//!
//! let mut tracker = HardlinkTracker::new();
//!
//! // On Unix, checking hardlinks from metadata:
//! let path = Path::new("/some/file.txt");
//! let metadata = std::fs::metadata(path).ok();
//!
//! if let Some(meta) = metadata {
//!     if tracker.is_hardlink(&meta) {
//!         println!("Skipping hardlink: {}", path.display());
//!     } else {
//!         println!("Processing file: {}", path.display());
//!     }
//! }
//! ```

use std::collections::HashSet;
use std::fs::Metadata;
#[cfg(windows)]
use std::path::Path;

/// Tracks seen inodes to detect hardlinks.
///
/// Files with the same inode are hardlinks to the same underlying data.
/// The tracker remembers which inodes have been seen and reports subsequent
/// occurrences as hardlinks.
///
/// # Thread Safety
///
/// `HardlinkTracker` is NOT thread-safe. Create one per thread or use
/// external synchronization if sharing across threads.
#[derive(Debug, Default)]
pub struct HardlinkTracker {
    /// Set of seen inode keys
    seen: HashSet<InodeKey>,
}

impl HardlinkTracker {
    /// Create a new hardlink tracker.
    ///
    /// # Example
    ///
    /// ```
    /// use rustdupe::scanner::hardlink::HardlinkTracker;
    ///
    /// let tracker = HardlinkTracker::new();
    /// assert_eq!(tracker.seen_count(), 0);
    /// ```
    #[must_use]
    pub fn new() -> Self {
        Self {
            seen: HashSet::new(),
        }
    }

    /// Create a tracker with pre-allocated capacity.
    ///
    /// Use this when you know approximately how many files you'll process.
    ///
    /// # Arguments
    ///
    /// * `capacity` - Expected number of unique files
    ///
    /// # Example
    ///
    /// ```
    /// use rustdupe::scanner::hardlink::HardlinkTracker;
    ///
    /// let tracker = HardlinkTracker::with_capacity(10000);
    /// ```
    #[must_use]
    pub fn with_capacity(capacity: usize) -> Self {
        Self {
            seen: HashSet::with_capacity(capacity),
        }
    }

    /// Check if a file is a hardlink to a previously seen file.
    ///
    /// Returns `true` if the file's inode was already seen, meaning this
    /// is a hardlink to a previously processed file.
    ///
    /// Returns `false` if:
    /// - This is the first occurrence of this inode
    /// - The platform doesn't support hardlink detection
    /// - The metadata doesn't contain inode information
    ///
    /// # Arguments
    ///
    /// * `metadata` - File metadata from `std::fs::metadata()` or `symlink_metadata()`
    ///
    /// # Side Effects
    ///
    /// If this is the first occurrence of an inode, it is recorded for
    /// future lookups. This makes the check stateful.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use rustdupe::scanner::hardlink::HardlinkTracker;
    /// use std::fs;
    ///
    /// let mut tracker = HardlinkTracker::new();
    ///
    /// let meta1 = fs::metadata("file1.txt").unwrap();
    /// let meta2 = fs::metadata("hardlink_to_file1.txt").unwrap();
    ///
    /// assert!(!tracker.is_hardlink(&meta1)); // First occurrence
    /// assert!(tracker.is_hardlink(&meta2));  // Hardlink detected
    /// ```
    pub fn is_hardlink(&mut self, metadata: &Metadata) -> bool {
        if let Some(key) = InodeKey::from_metadata(metadata) {
            if self.seen.contains(&key) {
                return true;
            }
            self.seen.insert(key);
        }
        false
    }

    /// Check if a file is a hardlink without recording it.
    ///
    /// Unlike [`is_hardlink`](Self::is_hardlink), this method doesn't record
    /// new inodes. Useful for read-only checks.
    ///
    /// # Arguments
    ///
    /// * `metadata` - File metadata to check
    ///
    /// # Returns
    ///
    /// - `Some(true)` - File is a hardlink to a previously seen file
    /// - `Some(false)` - File's inode has not been seen before
    /// - `None` - Platform doesn't support hardlink detection
    #[must_use]
    pub fn check_hardlink(&self, metadata: &Metadata) -> Option<bool> {
        InodeKey::from_metadata(metadata).map(|key| self.seen.contains(&key))
    }

    /// Record a file's inode without checking for hardlinks.
    ///
    /// Use this when you want to pre-populate the tracker with known files.
    ///
    /// # Arguments
    ///
    /// * `metadata` - File metadata to record
    ///
    /// # Returns
    ///
    /// `true` if the inode was newly recorded, `false` if already present
    /// or if the platform doesn't support hardlink detection.
    pub fn record(&mut self, metadata: &Metadata) -> bool {
        if let Some(key) = InodeKey::from_metadata(metadata) {
            return self.seen.insert(key);
        }
        false
    }

    /// Get the number of unique inodes tracked.
    ///
    /// # Example
    ///
    /// ```
    /// use rustdupe::scanner::hardlink::HardlinkTracker;
    ///
    /// let tracker = HardlinkTracker::new();
    /// assert_eq!(tracker.seen_count(), 0);
    /// ```
    #[must_use]
    pub fn seen_count(&self) -> usize {
        self.seen.len()
    }

    /// Clear all tracked inodes.
    ///
    /// After calling this, all files will be treated as first occurrences.
    pub fn clear(&mut self) {
        self.seen.clear();
    }

    /// Check if hardlink detection is supported on this platform.
    ///
    /// # Returns
    ///
    /// - `true` on Unix (uses dev/ino)
    /// - `false` on Windows and other platforms (not yet implemented)
    ///
    /// Note: Windows support requires opening file handles to get file_index,
    /// which is not yet implemented. This may be added in a future version.
    ///
    /// # Example
    ///
    /// ```
    /// use rustdupe::scanner::hardlink::HardlinkTracker;
    ///
    /// if HardlinkTracker::is_supported() {
    ///     println!("Hardlink detection is available");
    /// } else {
    ///     println!("Hardlink detection is not available on this platform");
    /// }
    /// ```
    #[must_use]
    pub const fn is_supported() -> bool {
        // Only Unix is currently implemented
        // Windows requires opening file handles to get file_index
        cfg!(unix)
    }
}

/// Platform-specific inode key for hardlink detection.
///
/// On Unix, this is (device_id, inode).
/// On Windows, this uses file_index from file handle.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
struct InodeKey {
    #[cfg(unix)]
    dev: u64,
    #[cfg(unix)]
    ino: u64,
    #[cfg(windows)]
    volume_serial: u32,
    #[cfg(windows)]
    file_index: u64,
    #[cfg(not(any(unix, windows)))]
    _phantom: (),
}

impl InodeKey {
    /// Create an inode key from file metadata.
    ///
    /// Returns `None` if the platform doesn't support inode tracking
    /// or if the metadata doesn't contain the required information.
    #[cfg(unix)]
    fn from_metadata(metadata: &Metadata) -> Option<Self> {
        use std::os::unix::fs::MetadataExt;
        Some(Self {
            dev: metadata.dev(),
            ino: metadata.ino(),
        })
    }

    #[cfg(windows)]
    fn from_metadata(_metadata: &Metadata) -> Option<Self> {
        // Windows metadata doesn't expose file_index directly.
        // We would need to open the file handle to get this info.
        // For now, return None to disable hardlink detection in walker.
        // Hardlinks will still be handled correctly by hashing (same content = same hash).
        //
        // To properly implement Windows hardlink detection, we would need to:
        // 1. Use winapi or windows-sys crate
        // 2. Call GetFileInformationByHandle
        // 3. Extract nFileIndexHigh, nFileIndexLow, dwVolumeSerialNumber
        //
        // This is deferred for now as it requires additional dependencies.
        None
    }

    #[cfg(not(any(unix, windows)))]
    fn from_metadata(_metadata: &Metadata) -> Option<Self> {
        None
    }
}

/// Get the inode key from a file path (Windows-specific helper).
///
/// On Windows, we need to open the file to get the file index.
/// This function handles that complexity.
///
/// # Arguments
///
/// * `path` - Path to the file
///
/// # Returns
///
/// The inode key if available, or `None` if the file cannot be opened
/// or the platform doesn't support this operation.
#[cfg(windows)]
// Currently unused as Windows hardlink detection is stubbed
#[allow(dead_code)]
fn get_inode_key_from_path(path: &Path) -> Option<InodeKey> {
    // This would require winapi/windows-sys to implement properly.
    // For now, we don't support Windows hardlink detection via path.
    let _ = path;
    None
}

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

    fn create_test_file(dir: &TempDir, name: &str, content: &str) -> std::path::PathBuf {
        let path = dir.path().join(name);
        let mut file = File::create(&path).unwrap();
        writeln!(file, "{}", content).unwrap();
        path
    }

    #[test]
    fn test_tracker_new() {
        let tracker = HardlinkTracker::new();
        assert_eq!(tracker.seen_count(), 0);
    }

    #[test]
    fn test_tracker_with_capacity() {
        let tracker = HardlinkTracker::with_capacity(100);
        assert_eq!(tracker.seen_count(), 0);
    }

    #[test]
    fn test_tracker_clear() {
        let dir = TempDir::new().unwrap();
        let path = create_test_file(&dir, "test.txt", "content");
        let metadata = std::fs::metadata(&path).unwrap();

        let mut tracker = HardlinkTracker::new();
        tracker.is_hardlink(&metadata);

        // On supported platforms, count should be > 0
        // On unsupported platforms (Windows), count stays 0
        if HardlinkTracker::is_supported() {
            assert!(tracker.seen_count() > 0);
        } else {
            assert_eq!(tracker.seen_count(), 0);
        }

        tracker.clear();
        assert_eq!(tracker.seen_count(), 0);
    }

    #[test]
    fn test_same_file_not_hardlink() {
        let dir = TempDir::new().unwrap();
        let path = create_test_file(&dir, "test.txt", "content");
        let metadata = std::fs::metadata(&path).unwrap();

        let mut tracker = HardlinkTracker::new();

        // First check should NOT report hardlink
        assert!(!tracker.is_hardlink(&metadata));
    }

    #[test]
    fn test_different_files_not_hardlinks() {
        let dir = TempDir::new().unwrap();
        let path1 = create_test_file(&dir, "file1.txt", "content1");
        let path2 = create_test_file(&dir, "file2.txt", "content2");

        let meta1 = std::fs::metadata(&path1).unwrap();
        let meta2 = std::fs::metadata(&path2).unwrap();

        let mut tracker = HardlinkTracker::new();

        assert!(!tracker.is_hardlink(&meta1));
        assert!(!tracker.is_hardlink(&meta2));
    }

    #[test]
    #[cfg(unix)]
    fn test_hardlink_detected() {
        use std::fs::hard_link;

        let dir = TempDir::new().unwrap();
        let original = create_test_file(&dir, "original.txt", "content");
        let link_path = dir.path().join("hardlink.txt");
        hard_link(&original, &link_path).unwrap();

        let meta_original = std::fs::metadata(&original).unwrap();
        let meta_link = std::fs::metadata(&link_path).unwrap();

        let mut tracker = HardlinkTracker::new();

        // First occurrence is not a hardlink
        assert!(!tracker.is_hardlink(&meta_original));
        // Second occurrence IS a hardlink
        assert!(tracker.is_hardlink(&meta_link));
    }

    #[test]
    fn test_check_hardlink_readonly() {
        let dir = TempDir::new().unwrap();
        let path = create_test_file(&dir, "test.txt", "content");
        let metadata = std::fs::metadata(&path).unwrap();

        let mut tracker = HardlinkTracker::new();

        // On unsupported platforms, check_hardlink returns None
        if !HardlinkTracker::is_supported() {
            assert_eq!(tracker.check_hardlink(&metadata), None);
            return;
        }

        // Read-only check before recording
        assert_eq!(tracker.check_hardlink(&metadata), Some(false));

        // Record the inode
        tracker.is_hardlink(&metadata);

        // Now it should show as seen (but it's not a "hardlink" since it's the first occurrence)
        // The check_hardlink just tells us if we've seen this inode
        assert_eq!(tracker.check_hardlink(&metadata), Some(true));
    }

    #[test]
    fn test_record_without_check() {
        let dir = TempDir::new().unwrap();
        let path = create_test_file(&dir, "test.txt", "content");
        let metadata = std::fs::metadata(&path).unwrap();

        let mut tracker = HardlinkTracker::new();

        if HardlinkTracker::is_supported() {
            // First record returns true (newly inserted)
            assert!(tracker.record(&metadata));
            // Second record returns false (already present)
            assert!(!tracker.record(&metadata));
        } else {
            // On unsupported platforms, record always returns false
            assert!(!tracker.record(&metadata));
        }
    }

    #[test]
    fn test_is_supported() {
        let supported = HardlinkTracker::is_supported();
        // Should be true on Unix, potentially false on Windows (until implemented)
        #[cfg(unix)]
        assert!(supported);

        // Just make sure it doesn't panic
        println!("Hardlink detection supported: {}", supported);
    }

    #[test]
    #[cfg(unix)]
    fn test_seen_count_increases() {
        let dir = TempDir::new().unwrap();
        let path1 = create_test_file(&dir, "file1.txt", "content1");
        let path2 = create_test_file(&dir, "file2.txt", "content2");

        let meta1 = std::fs::metadata(&path1).unwrap();
        let meta2 = std::fs::metadata(&path2).unwrap();

        let mut tracker = HardlinkTracker::new();
        assert_eq!(tracker.seen_count(), 0);

        tracker.is_hardlink(&meta1);
        assert_eq!(tracker.seen_count(), 1);

        tracker.is_hardlink(&meta2);
        assert_eq!(tracker.seen_count(), 2);

        // Checking same file again shouldn't increase count
        tracker.is_hardlink(&meta1);
        assert_eq!(tracker.seen_count(), 2);
    }

    #[test]
    #[cfg(unix)]
    fn test_multiple_hardlinks_same_inode() {
        use std::fs::hard_link;

        let dir = TempDir::new().unwrap();
        let original = create_test_file(&dir, "original.txt", "content");
        let link1 = dir.path().join("link1.txt");
        let link2 = dir.path().join("link2.txt");
        hard_link(&original, &link1).unwrap();
        hard_link(&original, &link2).unwrap();

        let meta_original = std::fs::metadata(&original).unwrap();
        let meta_link1 = std::fs::metadata(&link1).unwrap();
        let meta_link2 = std::fs::metadata(&link2).unwrap();

        let mut tracker = HardlinkTracker::new();

        // Only the first should NOT be a hardlink
        assert!(!tracker.is_hardlink(&meta_original));
        assert!(tracker.is_hardlink(&meta_link1));
        assert!(tracker.is_hardlink(&meta_link2));

        // Only one unique inode
        assert_eq!(tracker.seen_count(), 1);
    }
}