|
| 1 | +use alloc::string::String; |
| 2 | +use alloc::vec::Vec; |
| 3 | +#[cfg(feature = "stdlib")] |
| 4 | +use std::fs; |
| 5 | +#[cfg(feature = "stdlib")] |
| 6 | +use std::path::Path; |
| 7 | +#[cfg(feature = "stdlib")] |
| 8 | +use std::time::SystemTime; |
| 9 | + |
| 10 | +#[cfg(feature = "stdlib")] |
| 11 | +struct FileEntry { |
| 12 | + path: String, |
| 13 | + modified: SystemTime, |
| 14 | +} |
| 15 | + |
| 16 | +#[cfg(feature = "stdlib")] |
| 17 | +pub fn list_recent(path: String, limit: i64) -> Result<Vec<String>, String> { |
| 18 | + let mut entries = Vec::new(); |
| 19 | + let root = Path::new(&path); |
| 20 | + |
| 21 | + if !root.exists() { |
| 22 | + return Err(alloc::format!("Path does not exist: {}", path)); |
| 23 | + } |
| 24 | + |
| 25 | + visit_dirs(root, &mut entries).map_err(|e| e.to_string())?; |
| 26 | + |
| 27 | + // Sort by modified time descending |
| 28 | + entries.sort_by(|a, b| b.modified.cmp(&a.modified)); |
| 29 | + |
| 30 | + // Take limit |
| 31 | + let limit = if limit < 0 { 0 } else { limit as usize }; |
| 32 | + let result = entries.into_iter().take(limit).map(|e| e.path).collect(); |
| 33 | + |
| 34 | + Ok(result) |
| 35 | +} |
| 36 | + |
| 37 | +#[cfg(feature = "stdlib")] |
| 38 | +fn visit_dirs(dir: &Path, cb: &mut Vec<FileEntry>) -> std::io::Result<()> { |
| 39 | + if dir.is_dir() { |
| 40 | + // We use read_dir which returns an iterator over entries. |
| 41 | + // We ignore errors on subdirectories to be robust, similar to `find`. |
| 42 | + if let Ok(entries) = fs::read_dir(dir) { |
| 43 | + for entry in entries { |
| 44 | + if let Ok(entry) = entry { |
| 45 | + let path = entry.path(); |
| 46 | + // Avoid following symlinks to prevent infinite loops |
| 47 | + let is_dir = entry.file_type().map(|ft| ft.is_dir()).unwrap_or(false); |
| 48 | + if is_dir { |
| 49 | + // Recurse, ignoring errors |
| 50 | + let _ = visit_dirs(&path, cb); |
| 51 | + } else { |
| 52 | + if let Ok(metadata) = entry.metadata() { |
| 53 | + if let Ok(modified) = metadata.modified() { |
| 54 | + cb.push(FileEntry { |
| 55 | + path: path.to_string_lossy().into_owned(), |
| 56 | + modified, |
| 57 | + }); |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + Ok(()) |
| 66 | +} |
| 67 | + |
| 68 | +#[cfg(not(feature = "stdlib"))] |
| 69 | +pub fn list_recent(_path: String, _limit: i64) -> Result<Vec<String>, String> { |
| 70 | + Err("list_recent requires stdlib feature".into()) |
| 71 | +} |
| 72 | + |
| 73 | +#[cfg(test)] |
| 74 | +#[cfg(feature = "stdlib")] |
| 75 | +mod tests { |
| 76 | + use super::*; |
| 77 | + use std::fs; |
| 78 | + use std::thread; |
| 79 | + use std::time::Duration; |
| 80 | + use tempfile::TempDir; |
| 81 | + |
| 82 | + #[test] |
| 83 | + fn test_list_recent() { |
| 84 | + let tmp_dir = TempDir::new().unwrap(); |
| 85 | + let base_path = tmp_dir.path(); |
| 86 | + |
| 87 | + // Create files with different modification times |
| 88 | + let file1 = base_path.join("file1.txt"); |
| 89 | + fs::write(&file1, "content1").unwrap(); |
| 90 | + |
| 91 | + // Sleep to ensure different mtime (some FS have low resolution) |
| 92 | + thread::sleep(Duration::from_millis(100)); |
| 93 | + |
| 94 | + let dir1 = base_path.join("dir1"); |
| 95 | + fs::create_dir(&dir1).unwrap(); |
| 96 | + |
| 97 | + thread::sleep(Duration::from_millis(100)); |
| 98 | + |
| 99 | + let file2 = dir1.join("file2.txt"); |
| 100 | + fs::write(&file2, "content2").unwrap(); |
| 101 | + |
| 102 | + thread::sleep(Duration::from_millis(100)); |
| 103 | + |
| 104 | + let file3 = base_path.join("file3.txt"); |
| 105 | + fs::write(&file3, "content3").unwrap(); |
| 106 | + |
| 107 | + let base_path_str = base_path.to_string_lossy().to_string(); |
| 108 | + |
| 109 | + // Test limit 1 (should be file3) |
| 110 | + let res = list_recent(base_path_str.clone(), 1).unwrap(); |
| 111 | + assert_eq!(res.len(), 1); |
| 112 | + assert!(res[0].contains("file3.txt")); |
| 113 | + |
| 114 | + // Test limit 2 (should be file3, file2) |
| 115 | + let res = list_recent(base_path_str.clone(), 2).unwrap(); |
| 116 | + assert_eq!(res.len(), 2); |
| 117 | + assert!(res[0].contains("file3.txt")); |
| 118 | + assert!(res[1].contains("file2.txt")); |
| 119 | + |
| 120 | + // Test limit 3 (should be file3, file2, file1) |
| 121 | + let res = list_recent(base_path_str.clone(), 10).unwrap(); |
| 122 | + assert_eq!(res.len(), 3); |
| 123 | + assert!(res[0].contains("file3.txt")); |
| 124 | + assert!(res[1].contains("file2.txt")); |
| 125 | + assert!(res[2].contains("file1.txt")); |
| 126 | + } |
| 127 | + |
| 128 | + #[test] |
| 129 | + fn test_list_recent_empty() { |
| 130 | + let tmp_dir = TempDir::new().unwrap(); |
| 131 | + let base_path_str = tmp_dir.path().to_string_lossy().to_string(); |
| 132 | + |
| 133 | + let res = list_recent(base_path_str, 10).unwrap(); |
| 134 | + assert!(res.is_empty()); |
| 135 | + } |
| 136 | +} |
0 commit comments