ferris-files 0.1.2

A CLI to find large files
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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
use filesize::PathExt;
use indicatif::{MultiProgress, ProgressBar, ProgressStyle};
use rayon::prelude::*;
use std::collections::{HashSet, VecDeque};
use std::error::Error;
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use std::sync::mpsc::{self, Sender};
use std::sync::{Arc, Mutex};
use std::{fs, io, thread};

pub mod traits;
use crate::traits::ByteSize;

pub mod errors;
use crate::errors::SearchError;

pub mod config;
use crate::config::Config;

pub mod top_entries;
use crate::top_entries::TopEntries;

pub mod args;

pub mod tests;

/// Represents a file system entry with its path and processing result.
#[derive(Debug)]
struct FileEntry {
    path: PathBuf,
    result: Result<(), SearchError>,
}

/// Returns a platform specific (Windows or Unix) cap on open file handles.
/// On Unix will return 50% of the system's limit.
/// Windows uses a RAM based approach to allocate 64 file descriptors per 1GB of RAM.
fn get_fd_limit() -> usize {
    #[cfg(unix)]
    {
        use libc::{rlimit, RLIMIT_NOFILE};
        let mut rlim = rlimit {
            rlim_cur: 0,
            rlim_max: 0,
        };
        // Add some debug printing
        let result = unsafe { libc::getrlimit(RLIMIT_NOFILE, &mut rlim) };
        if result == 0 {
            let limit = rlim.rlim_cur as usize;
            return limit / 2;
        } else {
            // Print the error if getrlimit fails
            println!("Error: {}", std::io::Error::last_os_error());
        }
    }

    #[cfg(windows)]
    {
        // Try to get system memory info to make an educated guess
        use windows_sys::Win32::System::SystemInformation::GetPhysicallyInstalledSystemMemory;
        let mut memory_kb: u64 = 0;
        if unsafe { GetPhysicallyInstalledSystemMemory(&mut memory_kb) } != 0 {
            let memory_gb = memory_kb / (1024 * 1024);
            // Scale based on available memory, but cap at reasonable limits
            return usize::min(usize::max(512, (memory_gb * 64) as usize), 8192);
        }
        // Fallback for Windows
        return 2048;
    }

    // Default fallback
    100
}

/// Processes a batch of file entries and updates the top_entries collection.
///
/// This function processes each file entry in parallel, collecting metadata and file sizes.
/// It handles various error conditions (IO errors, invalid paths, mutex lock failures)
/// while maintaining a count of successful and failed operations.
///
/// # Arguments
///
/// * `batch` - Vector of file entries to process. Each entry contains a path and its current processing status
/// * `top_entries` - Thread-safe collection that maintains the N largest files found so far
/// * `error_log` - Thread-safe collection that maintains a record of any errors that occurr
/// * `is_verbose` - A bool used to log error messages if true
///
/// # Returns
///
/// Returns a tuple of `(processed, total)` where:
/// * `processed` - Number of files successfully processed and added to top_entries
/// * `total` - Total number of files attempted to process
///
/// # Error Handling
///
/// The function logs errors when is_verbose is true but does not propagate errors for:
/// * File metadata access failures
/// * File size calculation failures
/// * Invalid UTF-8 in path names
/// * Mutex lock failures
///
/// # Implementation Details
///
/// * Uses parallel iteration for metadata collection
/// * Metadata collection is skipped on entry.result Err variant
/// * Maintains a thread-safe ordering of largest files
fn process_batch(
    batch: Vec<FileEntry>,
    top_entries: &Arc<Mutex<TopEntries>>,
    error_log: Arc<Mutex<Vec<String>>>,
    is_verbose: bool,
) -> (usize, usize) {
    let metadata_results: Vec<_> = batch
        .into_par_iter()
        .map(|entry| match entry.result {
            Ok(()) => match fs::metadata(&entry.path) {
                Ok(metadata) => Some((entry.path, Ok(metadata))),
                Err(err) => Some((entry.path, Err(err))),
            },
            Err(err) => Some((
                entry.path,
                Err(io::Error::new(
                    io::ErrorKind::Other,
                    format!("Previous error: {:?}", err),
                )),
            )),
        })
        .collect();

    let total = metadata_results.len();
    let mut processed = 0;
    let mut errors = Vec::new();

    for result in metadata_results {
        if let Some((path, metadata_result)) = result {
            match metadata_result {
                Ok(metadata) => match path.size_on_disk_fast(&metadata) {
                    Ok(size) => {
                        if let Some(path_str) = path.to_str() {
                            match top_entries.lock() {
                                Ok(mut top) => {
                                    top.insert(path_str.to_string(), size);
                                    processed += 1;
                                }
                                Err(err) => {
                                    errors.push(format!(
                                        "Failed to lock top_entries for {}: {}",
                                        path.display(),
                                        err
                                    ));
                                }
                            }
                        } else {
                            errors.push(format!("Invalid UTF-8 in path: {}", path.display()));
                        }
                    }
                    Err(err) => {
                        errors.push(format!(
                            "Failed to get size for {}: {}",
                            path.display(),
                            err
                        ));
                    }
                },
                Err(err) => {
                    errors.push(format!("Error processing {}: {}", path.display(), err));
                }
            }
        }
    }

    // Log errors if any occurred
    if !errors.is_empty() && is_verbose {
        error_log.lock().unwrap().extend(errors);
    }

    (processed, total)
}

/// Performs a parallel search of files in a directory tree, sending batches of file paths to a channel.
///
/// # Arguments
///
/// * `root_dir` - The root directory to start the search from
/// * `tx` - A channel sender to transmit batches of discovered file paths
/// * `config` - Arc reference to a config instance
/// * `error_log` - Thread safe collection of errors ocurring during runtime
///
/// # Returns
///
/// Returns an `io::Result<(), SearchError>` indicating whether the operation completed successfully
/// or if a SearchError occurred
///
/// # Details
///
/// This function performs parallel directory traversal that:
/// - Uses multiple threads (based on available CPU cores) to search directories recursively
/// - Manages a shared work queue for distributing directory scanning work
/// - Limits the number of simultaneously open file handles to a platofrm specific limit or default of 100
/// - Skips symbolic links and non-existent paths
/// - Respects a set of directories to exclude from scanning
/// - Batches results to reduce channel communication overhead
///
fn parallel_search(
    root_dir: &Path,
    tx: Sender<Vec<FileEntry>>,
    progress: ProgressBar,
    config: Arc<Config>,
    error_log: Arc<Mutex<Vec<String>>>,
) -> Result<(), SearchError> {
    let work_queue = Arc::new(Mutex::new(VecDeque::new()));
    let is_scanning = Arc::new(AtomicBool::new(true));

    // Canonicalize directories to ignore
    let skip_dirs: HashSet<PathBuf> = config
        .skip_dirs
        .iter()
        .filter_map(|dir| match PathBuf::from(dir).canonicalize() {
            Ok(path) => Some(path),
            Err(err) => {
                if config.verbose {
                    error_log.lock().unwrap().push(format!(
                        "Warning: Could not canonicalize skip directory '{}': {}",
                        dir, err
                    ));
                }

                None
            }
        })
        .collect();

    // Initialize work queue with root directory
    match root_dir.canonicalize() {
        Ok(root) => work_queue.lock().unwrap().push_back(root),
        Err(err) => {
            if config.verbose {
                error_log
                    .lock()
                    .unwrap()
                    .push(format!("Failed to canonicalize root directory: {}", err));
            }
        }
    }

    let mut handles = vec![];
    let open_files = Arc::new(AtomicUsize::new(0));
    let errors_count = Arc::new(AtomicUsize::new(0));

    for _ in 0..config.num_threads {
        let work_queue = Arc::clone(&work_queue);
        let tx = tx.clone();
        let progress = progress.clone();
        let open_files = Arc::clone(&open_files);
        let is_scanning = Arc::clone(&is_scanning);
        let skip_dirs = skip_dirs.clone();
        let errors_count = Arc::clone(&errors_count);
        let config_clone = config.clone();
        let error_log = error_log.clone();

        handles.push(thread::spawn(move || -> Result<(), SearchError> {
            let mut batch = Vec::with_capacity(config_clone.batch_size);

            'outer: loop {
                let dir = {
                    match work_queue.lock() {
                        Ok( mut q) => {
                            q.pop_front()
                        }
                        Err(e) => {
                            if config_clone.verbose {
                                error_log
                                    .lock()
                                    .unwrap()
                                    .push(format!("Failed to lock work queue: {}", e));
                            }
                            None
                        }
                    }
                };

                match dir {
                    Some(dir) => {
                        progress.set_message(format!("Scanning: {}", dir.display()));

                        // Check if directory should be skipped
                        match dir.canonicalize() {
                            Ok(canonical_dir) => {
                                if skip_dirs
                                    .iter()
                                    .any(|skip_dir| canonical_dir.starts_with(skip_dir))
                                {
                                    continue;
                                }
                            }
                            Err(e) => {
                                if config_clone.verbose {
                                    error_log.lock().unwrap().push(format!("Failed to canonicalize directory {:#?} : {}", dir, e));
                                }
                            }
                        }

                        // Wait for available file handle with timeout
                        let mut wait_time = 1;
                        while open_files.load(Ordering::Relaxed) >= config_clone.max_open_files {
                            thread::sleep(std::time::Duration::from_millis(wait_time));
                            wait_time = wait_time.saturating_mul(2).min(100); // Exponential backoff
                        }
                        open_files.fetch_add(1, Ordering::SeqCst);

                        match fs::read_dir(&dir) {
                            Ok(entries) => {
                                for entry in entries.flatten() {
                                    let path = entry.path();
                                    if path.is_symlink() {
                                        continue;
                                    }

                                    let file_entry = match path.metadata() {
                                        Ok(metadata) => {
                                            if metadata.is_dir() {
                                                match work_queue.lock() {
                                                    Ok(mut q) => {
                                                        q.push_back(path);
                                                    }
                                                    Err(e) => {
                                                        if config_clone.verbose {
                                                            error_log.lock().unwrap().push(format!("Error obtaining lock on work queue: {}", e));
                                                        }
                                                    }
                                                }
                                                continue;
                                            }
                                            FileEntry {
                                                path,
                                                result: Ok(()),
                                            }
                                        }
                                        Err(err) => {
                                            errors_count.fetch_add(1, Ordering::Relaxed);
                                            FileEntry {
                                                path,
                                                result: Err(SearchError::IoError(err)),
                                            }
                                        }
                                    };

                                    batch.push(file_entry);
                                    if batch.len() >= config_clone.batch_size {
                                        tx.send(batch).map_err(|e| {
                                            SearchError::SendError(format!(
                                                "Failed to send batch: {}",
                                                e
                                            ))
                                        })?;
                                        batch = Vec::with_capacity(config_clone.batch_size);
                                    }
                                }
                            }
                            Err(err) => {
                                errors_count.fetch_add(1, Ordering::Relaxed);
                                if config_clone.verbose {
                                    error_log.lock().unwrap().push(format!("Error reading directory {}: {}", dir.display(), err));
                                }
                            }
                        }

                        open_files.fetch_sub(1, Ordering::SeqCst);
                    }
                    None => {
                        if !is_scanning.load(Ordering::SeqCst) {
                            if work_queue
                                .lock()
                                .map_err(|e| {
                                    SearchError::ThreadError(format!(
                                        "Failed to lock work queue: {}",
                                        e
                                    ))
                                })?
                                .is_empty()
                            {
                                break 'outer;
                            }
                        }
                        thread::sleep(std::time::Duration::from_millis(10));
                    }
                }
            }

            if !batch.is_empty() {
                tx.send(batch).map_err(|e| {
                    SearchError::SendError(format!("Failed to send final batch: {}", e))
                })?;
            }

            Ok(())
        }));
    }

    is_scanning.store(false, Ordering::SeqCst);

    // Join threads and collect errors
    let thread_results: Vec<Result<(), SearchError>> = handles
        .into_iter()
        .map(|handle| {
            handle
                .join()
                .map_err(|e| SearchError::ThreadError(format!("Thread panicked: {:?}", e)))?
        })
        .collect();

    // Check for any thread errors
    for result in thread_results {
        if let Err(err) = result {
            if config.verbose {
                error_log
                    .lock()
                    .unwrap()
                    .push(format!("Thread error: {:?}", err));
            }
        }
    }

    let error_count = errors_count.load(Ordering::Relaxed);
    progress.finish_with_message(format!(
        "Directory scan complete ({} errors encountered: run with -v for details)",
        error_count
    ));

    Ok(())
}

/// Responsible for initiating the directory traversdal and analyzing files as they are discovered
///
/// # Arguments
///
/// * `config` - An instance of a `Config` struct
///
/// # Returns
///
/// * `Result<(), Box<dyn Error>>` - Ok(()) if successful, or an Error if something fails
///
/// # Progress Display
///
/// The function shows two progress indicators:
/// 1. A spinner showing the directory scanning progress
/// 2. A spinner showing file processing progress with counts of total and successfully processed files
///
/// # Output
///
/// Upon completion, prints a list of the largest files found, with their paths and sizes.
/// If verbsoity was enabled, errors will be printed before file size results.
///
/// # Implementation Details
///
/// - Uses a channel (`mpsc`) for communication between scanner and processor threads
/// - Maintains thread-safe access to the top entries using `Arc<Mutex<TopEntries>>`
/// - Processes files in batches for better performance
/// - Shows real-time progress using the `indicatif` crate's progress bars
///
pub fn run(config: Config) -> Result<(), Box<dyn Error>> {
    let is_verbose = config.verbose;
    let error_log: Arc<Mutex<Vec<String>>> = Arc::new(Mutex::new(Vec::new()));
    let error_log_clone = error_log.clone();
    let config_arc: Arc<Config> = Arc::new(config.clone());

    print!(
        "Searching for {0} largest entries in {1}:\n",
        config.num_entries,
        config.root_path.display()
    );

    let multi_progress = MultiProgress::new();
    let scan_progress = multi_progress.add(ProgressBar::new_spinner());
    scan_progress.set_style(
        ProgressStyle::default_spinner()
            .template("{spinner:.green} [{elapsed_precise}] {msg}")
            .unwrap(),
    );

    let process_progress = multi_progress.add(ProgressBar::new_spinner());
    process_progress.set_style(
        ProgressStyle::default_spinner()
            .template("{spinner:.green} [{elapsed_precise}] {msg}")
            .unwrap(),
    );

    let (tx, rx) = mpsc::channel();
    let top_entries = Arc::new(Mutex::new(TopEntries::new(config.num_entries)));

    // Directory scanner thread
    let root_path = config.root_path.clone();
    let scan_handle = thread::spawn(move || {
        parallel_search(
            &root_path,
            tx,
            scan_progress,
            config_arc.clone(),
            error_log_clone.clone(),
        )
    });

    // Process files as received
    let mut total_files = 0;
    let mut total_processed = 0;
    let mut total_attempts = 0;

    while let Ok(batch) = rx.recv() {
        total_files += batch.len();
        let (processed, attempted) =
            process_batch(batch, &top_entries, error_log.clone(), is_verbose);
        total_processed += processed;
        total_attempts += attempted;

        process_progress.set_message(format!(
            "Processing {} files (successfully processed: {}, failed: {})...",
            total_files,
            total_processed,
            total_attempts - total_processed
        ));
    }

    // Handle scanner thread result
    match scan_handle.join() {
        Ok(result) => result.map_err(|e| Box::new(e))?,
        Err(e) => {
            if is_verbose {
                error_log
                    .lock()
                    .unwrap()
                    .push(format!("Scanner thread panicked: {:?}", e));
            }
        }
    }

    process_progress.finish_with_message(format!(
        "Processed {} files ({} successful, {} failed)",
        total_attempts,
        total_processed,
        total_attempts - total_processed
    ));

    if is_verbose {
        println!();
        error_log.lock().unwrap().iter().for_each(|e| {
            eprintln!("{}", e);
        });
    }

    println!("\n");

    match top_entries.lock() {
        Ok(top) => {
            if top.entries.is_empty() {
                println!("No files found - run with -v flag for error output");
            } else {
                for (path, size) in top.entries.iter() {
                    println!("{}: {}", path, size.format_size());
                }
            }
        }
        Err(e) => {
            return Err(Box::new(io::Error::new(
                io::ErrorKind::Other,
                format!("Failed to lock top entries for final output: {}", e),
            )));
        }
    }

    Ok(())
}