path-rs 0.1.0

Cross-platform path expansion, normalization, traversal, searching, and caching
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
//! Optional discovery caching.
//!
//! Caching is **opt-in** and must never be used as a security boundary.
//! Stale entries are performance artifacts, not authoritative filesystem state.

use crate::error::PathError;
use crate::metadata::FileEntry;
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::sync::Mutex;
use std::time::{Duration, SystemTime};

#[cfg(feature = "search")]
use crate::search::SearchRequest;

/// Whether / how a search should consult the cache.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[non_exhaustive]
pub enum CachePolicy {
    /// Do not read or write the cache.
    #[default]
    Bypass,
    /// Return a fresh cached value when present; otherwise scan and store.
    ReadThrough,
    /// Ignore any existing value, scan, and store the new result.
    Refresh,
}

/// Cache storage mode.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[non_exhaustive]
pub enum CacheMode {
    /// Caching disabled.
    #[default]
    Disabled,
    /// In-memory process-local cache.
    Memory,
    /// Persistent on-disk cache (requires `persistent-cache` feature).
    Persistent,
}

/// Configuration for a discovery cache instance.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CacheOptions {
    /// Storage mode.
    pub mode: CacheMode,
    /// Optional time-to-live for entries.
    pub ttl: Option<Duration>,
    /// Maximum number of cached keys.
    pub max_entries: usize,
    /// Reserved for future metadata revalidation (directory mtime, etc.).
    ///
    /// Directory timestamps are **not** a perfect invalidation mechanism and are
    /// not used as a security signal even when enabled in future versions.
    pub validate_metadata: bool,
}

impl Default for CacheOptions {
    fn default() -> Self {
        Self {
            mode: CacheMode::Disabled,
            ttl: None,
            max_entries: 128,
            validate_metadata: false,
        }
    }
}

impl CacheOptions {
    /// Create default options (`Self::default()`).
    pub fn new() -> Self {
        Self::default()
    }
}

/// Cache key capturing all inputs that affect discovery results.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct CacheKey {
    /// Search / list root.
    pub root: PathBuf,
    /// Glob patterns (empty for pure listing cache use).
    pub patterns: Vec<String>,
    /// Exclude patterns.
    pub exclude_patterns: Vec<String>,
    /// Recursive walk.
    pub recursive: bool,
    /// Follow symlinks.
    pub follow_symlinks: bool,
    /// Include hidden.
    pub include_hidden: bool,
    /// Include files.
    pub include_files: bool,
    /// Include directories.
    pub include_directories: bool,
    /// Include symlinks.
    pub include_symlinks: bool,
    /// Max depth.
    pub max_depth: Option<usize>,
    /// Sort mode used for the discovery result.
    pub sort: crate::metadata::SortMode,
}

impl CacheKey {
    /// Build a key from a search request.
    #[cfg(feature = "search")]
    pub fn from_search(request: &SearchRequest) -> Self {
        Self {
            root: request.root.clone(),
            patterns: request.patterns.clone(),
            exclude_patterns: request.exclude_patterns.clone(),
            recursive: request.options.recursive,
            follow_symlinks: request.options.follow_symlinks,
            include_hidden: request.options.include_hidden,
            include_files: request.options.include_files,
            include_directories: request.options.include_directories,
            include_symlinks: request.options.include_symlinks,
            max_depth: request.options.max_depth,
            sort: request.options.sort,
        }
    }
}

/// Cached discovery result.
#[derive(Debug, Clone)]
pub struct CacheValue {
    /// Cached entries.
    pub entries: Vec<FileEntry>,
    /// When the value was stored.
    pub stored_at: SystemTime,
    /// Optional TTL override for this value.
    pub ttl: Option<Duration>,
}

impl CacheValue {
    /// Returns true if the entry is past its TTL.
    pub fn is_expired(&self) -> bool {
        let Some(ttl) = self.ttl else {
            return false;
        };
        self.stored_at.elapsed().map(|e| e > ttl).unwrap_or(true)
    }

    /// Returns true if expired given a default TTL when `self.ttl` is `None`.
    pub fn is_expired_with_default(&self, default_ttl: Option<Duration>) -> bool {
        let ttl = self.ttl.or(default_ttl);
        let Some(ttl) = ttl else {
            return false;
        };
        self.stored_at.elapsed().map(|e| e > ttl).unwrap_or(true)
    }
}

/// Trait for discovery result caches.
pub trait DiscoveryCache: Send + Sync {
    /// Get a cached value by key.
    fn get(&self, key: &CacheKey) -> Result<Option<CacheValue>, PathError>;
    /// Store a value.
    fn put(&self, key: CacheKey, value: CacheValue) -> Result<(), PathError>;
    /// Invalidate all keys whose root equals or is under `root`.
    fn invalidate(&self, root: &Path) -> Result<(), PathError>;
    /// Clear the entire cache.
    fn clear(&self) -> Result<(), PathError>;
}

/// Thread-safe in-memory discovery cache.
#[derive(Debug)]
pub struct MemoryCache {
    options: CacheOptions,
    inner: Mutex<HashMap<CacheKey, CacheValue>>,
}

impl MemoryCache {
    /// Create a new memory cache with the given options.
    ///
    /// If `mode` is [`CacheMode::Disabled`], get always returns `None` and put is a no-op.
    pub fn new(options: CacheOptions) -> Self {
        Self {
            options,
            inner: Mutex::new(HashMap::new()),
        }
    }

    /// Number of entries currently stored (for tests).
    pub fn len(&self) -> Result<usize, PathError> {
        let guard = self
            .inner
            .lock()
            .map_err(|_| PathError::cache("memory cache lock poisoned"))?;
        Ok(guard.len())
    }

    /// Returns true if the cache is empty.
    pub fn is_empty(&self) -> Result<bool, PathError> {
        Ok(self.len()? == 0)
    }

    #[cfg(feature = "persistent-cache")]
    pub(crate) fn snapshot(&self) -> Result<Vec<(CacheKey, CacheValue)>, PathError> {
        let guard = self
            .inner
            .lock()
            .map_err(|_| PathError::cache("memory cache lock poisoned"))?;
        Ok(guard.iter().map(|(k, v)| (k.clone(), v.clone())).collect())
    }
}

impl DiscoveryCache for MemoryCache {
    fn get(&self, key: &CacheKey) -> Result<Option<CacheValue>, PathError> {
        if self.options.mode == CacheMode::Disabled {
            return Ok(None);
        }
        let mut guard = self
            .inner
            .lock()
            .map_err(|_| PathError::cache("memory cache lock poisoned"))?;

        if let Some(value) = guard.get(key) {
            if value.is_expired_with_default(self.options.ttl) {
                guard.remove(key);
                return Ok(None);
            }
            return Ok(Some(value.clone()));
        }
        Ok(None)
    }

    fn put(&self, key: CacheKey, mut value: CacheValue) -> Result<(), PathError> {
        if self.options.mode == CacheMode::Disabled {
            return Ok(());
        }
        if value.ttl.is_none() {
            value.ttl = self.options.ttl;
        }
        let mut guard = self
            .inner
            .lock()
            .map_err(|_| PathError::cache("memory cache lock poisoned"))?;

        if guard.len() >= self.options.max_entries && !guard.contains_key(&key) {
            // Evict an arbitrary entry to enforce the bound (not LRU).
            if let Some(evict) = guard.keys().next().cloned() {
                guard.remove(&evict);
            }
        }
        guard.insert(key, value);
        Ok(())
    }

    fn invalidate(&self, root: &Path) -> Result<(), PathError> {
        let mut guard = self
            .inner
            .lock()
            .map_err(|_| PathError::cache("memory cache lock poisoned"))?;
        guard.retain(|k, _| k.root != root && !k.root.starts_with(root));
        Ok(())
    }

    fn clear(&self) -> Result<(), PathError> {
        let mut guard = self
            .inner
            .lock()
            .map_err(|_| PathError::cache("memory cache lock poisoned"))?;
        guard.clear();
        Ok(())
    }
}

/// Persistent on-disk cache (feature `persistent-cache`).
///
/// Schema is versioned. Corrupt or mismatched files are treated as a miss
/// (and optionally replaced on next put). Atomic writes use a temp file + rename.
///
/// Store persistent cache under the platform cache directory, not beside source files.
#[cfg(feature = "persistent-cache")]
#[derive(Debug)]
pub struct PersistentCache {
    path: PathBuf,
    options: CacheOptions,
    memory: MemoryCache,
}

#[cfg(feature = "persistent-cache")]
mod persistent_schema {
    use serde::{Deserialize, Serialize};

    pub const SCHEMA_VERSION: u32 = 1;

    #[derive(Serialize, Deserialize)]
    pub struct PersistentCacheFile {
        pub schema_version: u32,
        pub entries: Vec<PersistentCacheRecord>,
    }

    #[derive(Serialize, Deserialize)]
    pub struct PersistentCacheRecord {
        pub key: PersistentKey,
        pub paths: Vec<String>,
        pub stored_at_epoch_ms: u128,
        pub ttl_ms: Option<u128>,
    }

    #[derive(Serialize, Deserialize, Clone)]
    pub struct PersistentKey {
        pub root: String,
        pub patterns: Vec<String>,
        pub exclude_patterns: Vec<String>,
        pub recursive: bool,
        pub follow_symlinks: bool,
        pub include_hidden: bool,
        pub include_files: bool,
        pub include_directories: bool,
        pub include_symlinks: bool,
        pub max_depth: Option<usize>,
        pub sort: u8,
    }
}

#[cfg(feature = "persistent-cache")]
fn sort_to_u8(sort: crate::metadata::SortMode) -> u8 {
    use crate::metadata::SortMode;
    match sort {
        SortMode::None => 0,
        SortMode::Path => 1,
        SortMode::Name => 2,
        SortMode::DirsFirst => 3,
    }
}

#[cfg(feature = "persistent-cache")]
fn sort_from_u8(v: u8) -> crate::metadata::SortMode {
    use crate::metadata::SortMode;
    match v {
        0 => SortMode::None,
        2 => SortMode::Name,
        3 => SortMode::DirsFirst,
        _ => SortMode::Path,
    }
}

#[cfg(feature = "persistent-cache")]
impl PersistentCache {
    /// Open or create a persistent cache file under the platform cache directory.
    pub fn open(app_name: &str, file_name: &str, options: CacheOptions) -> Result<Self, PathError> {
        use std::fs;
        let dir = crate::dirs::cache_dir(app_name)?;
        fs::create_dir_all(&dir).map_err(|e| PathError::filesystem(&dir, e))?;
        let path = dir.join(file_name);
        let memory = MemoryCache::new(CacheOptions {
            mode: CacheMode::Memory,
            ttl: options.ttl,
            max_entries: options.max_entries,
            validate_metadata: options.validate_metadata,
        });
        let cache = Self {
            path,
            options,
            memory,
        };
        let _ = cache.load_into_memory();
        Ok(cache)
    }

    /// Path to the on-disk cache file.
    pub fn path(&self) -> &Path {
        &self.path
    }

    fn load_into_memory(&self) -> Result<(), PathError> {
        use persistent_schema::{PersistentCacheFile, SCHEMA_VERSION};
        use std::fs;

        if !self.path.exists() {
            return Ok(());
        }
        let data = fs::read(&self.path).map_err(|e| PathError::filesystem(&self.path, e))?;
        let parsed: PersistentCacheFile = match serde_json::from_slice(&data) {
            Ok(v) => v,
            Err(_) => return Ok(()),
        };
        if parsed.schema_version != SCHEMA_VERSION {
            return Ok(());
        }
        for rec in parsed.entries {
            let key = CacheKey {
                root: PathBuf::from(&rec.key.root),
                patterns: rec.key.patterns,
                exclude_patterns: rec.key.exclude_patterns,
                recursive: rec.key.recursive,
                follow_symlinks: rec.key.follow_symlinks,
                include_hidden: rec.key.include_hidden,
                include_files: rec.key.include_files,
                include_directories: rec.key.include_directories,
                include_symlinks: rec.key.include_symlinks,
                max_depth: rec.key.max_depth,
                sort: sort_from_u8(rec.key.sort),
            };
            let stored_at =
                SystemTime::UNIX_EPOCH + Duration::from_millis(rec.stored_at_epoch_ms as u64);
            let ttl = rec.ttl_ms.map(|ms| Duration::from_millis(ms as u64));
            let entries = rec
                .paths
                .into_iter()
                .map(|p| FileEntry::new(PathBuf::from(p), crate::metadata::EntryKind::Other))
                .collect();
            let value = CacheValue {
                entries,
                stored_at,
                ttl,
            };
            if !value.is_expired_with_default(self.options.ttl) {
                self.memory.put(key, value)?;
            }
        }
        Ok(())
    }

    fn flush(&self) -> Result<(), PathError> {
        use persistent_schema::{
            PersistentCacheFile, PersistentCacheRecord, PersistentKey, SCHEMA_VERSION,
        };
        use std::fs;
        use std::io::Write;

        let snapshot = self.memory.snapshot()?;
        let mut records = Vec::new();
        for (key, value) in snapshot {
            if value.is_expired_with_default(self.options.ttl) {
                continue;
            }
            let stored_at_epoch_ms = value
                .stored_at
                .duration_since(SystemTime::UNIX_EPOCH)
                .map(|d| d.as_millis())
                .unwrap_or(0);
            records.push(PersistentCacheRecord {
                key: PersistentKey {
                    root: key.root.to_string_lossy().into_owned(),
                    patterns: key.patterns.clone(),
                    exclude_patterns: key.exclude_patterns.clone(),
                    recursive: key.recursive,
                    follow_symlinks: key.follow_symlinks,
                    include_hidden: key.include_hidden,
                    include_files: key.include_files,
                    include_directories: key.include_directories,
                    include_symlinks: key.include_symlinks,
                    max_depth: key.max_depth,
                    sort: sort_to_u8(key.sort),
                },
                paths: value
                    .entries
                    .iter()
                    .map(|e| e.path.to_string_lossy().into_owned())
                    .collect(),
                stored_at_epoch_ms,
                ttl_ms: value.ttl.map(|d| d.as_millis()),
            });
        }

        let file = PersistentCacheFile {
            schema_version: SCHEMA_VERSION,
            entries: records,
        };
        let json = serde_json::to_vec_pretty(&file)
            .map_err(|e| PathError::cache(format!("serialize failed: {e}")))?;

        let tmp_path = self.path.with_extension("json.tmp");
        {
            let mut f =
                fs::File::create(&tmp_path).map_err(|e| PathError::filesystem(&tmp_path, e))?;
            f.write_all(&json)
                .map_err(|e| PathError::filesystem(&tmp_path, e))?;
            f.sync_all()
                .map_err(|e| PathError::filesystem(&tmp_path, e))?;
        }
        fs::rename(&tmp_path, &self.path).map_err(|e| PathError::filesystem(&self.path, e))?;
        Ok(())
    }
}

#[cfg(feature = "persistent-cache")]
impl DiscoveryCache for PersistentCache {
    fn get(&self, key: &CacheKey) -> Result<Option<CacheValue>, PathError> {
        if matches!(self.options.mode, CacheMode::Disabled) {
            return Ok(None);
        }
        self.memory.get(key)
    }

    fn put(&self, key: CacheKey, value: CacheValue) -> Result<(), PathError> {
        if matches!(self.options.mode, CacheMode::Disabled) {
            return Ok(());
        }
        self.memory.put(key, value)?;
        self.flush()
    }

    fn invalidate(&self, root: &Path) -> Result<(), PathError> {
        self.memory.invalidate(root)?;
        self.flush()
    }

    fn clear(&self) -> Result<(), PathError> {
        use std::fs;
        self.memory.clear()?;
        if self.path.exists() {
            fs::remove_file(&self.path).map_err(|e| PathError::filesystem(&self.path, e))?;
        }
        Ok(())
    }
}