rkit 0.1.8

Rust CLI Toolkit for Git Repo Management
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
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::env;
use std::fs;
use std::path::{Path, PathBuf};
use std::sync::RwLock;
use std::time::{SystemTime, UNIX_EPOCH};
use thiserror::Error;

use crate::error::RkitError;

/// Configuration options for the cache
#[derive(Debug, Clone)]
pub struct CacheConfig {
    /// Time-to-live for cache entries in seconds
    pub ttl_seconds: u64,
    /// Maximum number of entries in the cache (None for unlimited)
    pub max_entries: Option<usize>,
    /// Custom cache file path (None for default)
    pub cache_path: Option<PathBuf>,
}

impl Default for CacheConfig {
    fn default() -> Self {
        Self {
            ttl_seconds: 24 * 60 * 60, // 24 hours
            max_entries: None,
            cache_path: None,
        }
    }
}

/// Represents errors that can occur in the cache module
#[derive(Debug, Error)]
pub enum CacheError {
    #[error("Failed to acquire lock: {0}")]
    LockError(String),

    #[error("Cache entry expired: {0}")]
    EntryExpired(PathBuf),

    #[error("Invalid cache version: {0}")]
    InvalidVersion(u32),

    #[error("Failed to read/write cache file: {0}")]
    IoError(#[from] std::io::Error),

    #[error("Failed to parse cache: {0}")]
    ParseError(#[from] serde_json::Error),

    #[error("Failed to get system time: {0}")]
    TimeError(String),

    #[error("Failed to create cache directory: {0}")]
    DirectoryError(#[from] RkitError),

    #[error("Cache is full: {0} entries")]
    CacheFull(usize),

    #[error("Invalid cache entry: {0}")]
    InvalidEntryError(String),
}

// Type alias for cache-specific results
type CacheResult<T> = Result<T, CacheError>;

/// Represents a cache entry for a git repository
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct CacheEntry {
    /// The path to the repository
    pub path: PathBuf,
    /// Last modification time of the repository
    pub last_modified: u64,
    /// Last time the cache entry was validated
    pub last_checked: u64,
}

#[derive(Debug, Serialize, Deserialize)]
struct CacheData {
    entries: HashMap<PathBuf, CacheEntry>,
    version: u32,
}

pub struct Cache {
    entries: RwLock<HashMap<PathBuf, CacheEntry>>,
    cache_path: PathBuf,
    pub config: CacheConfig,
}

impl Cache {
    pub fn new() -> Self {
        Self::with_config(CacheConfig::default())
    }

    pub fn with_config(config: CacheConfig) -> Self {
        let cache_path = config
            .cache_path
            .clone()
            .unwrap_or_else(|| match get_cache_path() {
                Ok(path) => path,
                Err(e) => {
                    log::warn!("Failed to get cache path: {}", e);
                    let temp_path = env::temp_dir().join("rkit").join("cache.json");
                    if let Err(e) = validate_cache_path(&temp_path) {
                        log::error!("Failed to validate temp cache path: {}", e);
                    }
                    temp_path
                }
            });

        let entries = match load_cache(&cache_path) {
            Ok(entries) => entries,
            Err(e) => {
                log::warn!("Failed to load cache: {}", e);
                HashMap::new()
            }
        };
        Self {
            entries: RwLock::new(entries),
            cache_path,
            config,
        }
    }

    pub fn get(&self, path: &Path) -> Option<CacheEntry> {
        self.entries
            .read()
            .ok()
            .and_then(|entries| entries.get(path).cloned())
    }

    pub fn insert(&self, path: PathBuf, entry: CacheEntry) -> CacheResult<()> {
        // Validate entry before insertion
        if !Self::validate_entry(&entry, self.config.ttl_seconds) {
            return Err(CacheError::InvalidEntryError(format!(
                "Invalid cache entry for path: {}",
                path.display()
            )));
        }

        let mut entries = self
            .entries
            .write()
            .map_err(|_| CacheError::LockError("Failed to acquire cache write lock".to_string()))?;

        // Check if we need to enforce max entries
        if let Some(max_entries) = self.config.max_entries {
            if entries.len() >= max_entries {
                return Err(CacheError::CacheFull(max_entries));
            }
        }

        log::debug!("Inserting cache entry for path: {}", path.display());
        entries.insert(path, entry);
        log::debug!("Current cache size: {} entries", entries.len());

        // Save without acquiring another lock
        self.save_with_entries(&entries)?;
        Ok(())
    }

    pub fn validate_and_update(&self) -> CacheResult<()> {
        let mut entries = self
            .entries
            .write()
            .map_err(|_| CacheError::LockError("Failed to acquire cache write lock".to_string()))?;

        // Use retain for in-place filtering
        entries.retain(|path, entry| {
            let is_valid = Self::validate_entry(entry, self.config.ttl_seconds);
            if !is_valid {
                log::debug!("Removing invalid cache entry: {}", path.display());
            }
            is_valid
        });

        // Save the updated entries
        self.save_with_entries(&entries)?;

        Ok(())
    }

    /// Validates multiple paths in a single operation
    /// Returns a vector of paths that have valid cache entries
    pub fn validate_entries(&self, paths: &[PathBuf]) -> CacheResult<Vec<PathBuf>> {
        let entries = self
            .entries
            .read()
            .map_err(|_| CacheError::LockError("Failed to acquire cache read lock".to_string()))?;

        Ok(paths
            .iter()
            .filter(|path| {
                entries
                    .get(*path)
                    .map(|entry| Self::validate_entry(entry, self.config.ttl_seconds))
                    .unwrap_or(false)
            })
            .cloned()
            .collect())
    }

    pub fn save(&self) -> CacheResult<()> {
        let entries = self
            .entries
            .read()
            .map_err(|_| CacheError::LockError("Failed to acquire cache read lock".to_string()))?;

        self.save_with_entries(&entries)
    }

    fn save_with_entries(&self, entries: &HashMap<PathBuf, CacheEntry>) -> CacheResult<()> {
        if let Some(parent) = self.cache_path.parent() {
            fs::create_dir_all(parent).map_err(|e| {
                CacheError::DirectoryError(RkitError::DirectoryCreationError {
                    path: parent.to_path_buf(),
                    source: e,
                })
            })?;
        }

        let cache_data = CacheData {
            entries: entries
                .iter()
                .map(|(k, v)| (k.clone(), v.clone()))
                .collect(),
            version: 1,
        };

        log::debug!("Saving cache to: {}", self.cache_path.display());
        log::debug!("Cache entries to save: {}", entries.len());

        // Create a temporary file for atomic write
        let temp_path = self.cache_path.with_extension("tmp");
        let json = serde_json::to_string_pretty(&cache_data)?;

        // Write to temp file
        if let Err(e) = fs::write(&temp_path, json) {
            // Clean up temp file on error
            let _ = fs::remove_file(&temp_path);
            return Err(CacheError::IoError(e));
        }

        // Atomic rename
        if let Err(e) = fs::rename(&temp_path, &self.cache_path) {
            // Clean up temp file on error
            let _ = fs::remove_file(&temp_path);
            return Err(CacheError::IoError(e));
        }

        Ok(())
    }

    pub fn validate_entry(entry: &CacheEntry, ttl_seconds: u64) -> bool {
        let now = match get_current_time() {
            Ok(time) => time,
            Err(e) => {
                log::error!("Failed to get current time: {}", e);
                return false;
            }
        };

        // Check if the entry has expired
        if now - entry.last_checked > ttl_seconds {
            log::debug!("Cache entry expired for path: {}", entry.path.display());
            return false;
        }

        // Check if the path exists and is a git repository
        let path_exists = entry.path.exists();
        let is_git_repo = entry.path.join(".git").exists();

        if !path_exists {
            log::debug!("Cache entry path does not exist: {}", entry.path.display());
        }
        if !is_git_repo {
            log::debug!(
                "Cache entry is not a git repository: {}",
                entry.path.display()
            );
        }

        path_exists && is_git_repo
    }

    pub fn update_entry(path: &Path) -> CacheEntry {
        let now = get_current_time().unwrap_or_else(|e| {
            log::error!("Failed to get current time: {}", e);
            0
        });

        let metadata = fs::metadata(path).ok();
        let last_modified = metadata
            .and_then(|m| m.modified().ok())
            .and_then(|t| t.duration_since(UNIX_EPOCH).ok())
            .map(|d| d.as_secs())
            .unwrap_or(now);

        CacheEntry {
            path: path.to_path_buf(),
            last_modified,
            last_checked: now,
        }
    }

    /// Updates an existing entry and saves it to the cache
    pub fn update_and_save(&self, path: &Path) -> CacheResult<()> {
        log::debug!(
            "Updating and saving cache entry for path: {}",
            path.display()
        );
        let entry = Self::update_entry(path);
        self.insert(path.to_path_buf(), entry)
    }

    /// Updates multiple entries and saves them to the cache
    pub fn update_and_save_many(&self, paths: &[PathBuf]) -> CacheResult<()> {
        log::debug!("Updating and saving {} cache entries", paths.len());
        let mut entries = self
            .entries
            .write()
            .map_err(|_| CacheError::LockError("Failed to acquire cache write lock".to_string()))?;

        for path in paths {
            let entry = Self::update_entry(path);
            entries.insert(path.clone(), entry);
        }

        self.save_with_entries(&entries)
    }

    /// Get the TTL in seconds for cache entries
    pub fn ttl_seconds(&self) -> u64 {
        self.config.ttl_seconds
    }
}

impl Default for Cache {
    fn default() -> Self {
        Self::new()
    }
}

fn get_current_time() -> CacheResult<u64> {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map_err(|e| CacheError::TimeError(format!("Failed to get system time: {}", e)))
        .map(|d| d.as_secs())
}

/// Validates a cache path and ensures its parent directory exists
fn validate_cache_path(path: &Path) -> CacheResult<()> {
    // Check if path is absolute
    if !path.is_absolute() {
        return Err(CacheError::DirectoryError(RkitError::ConfigError(format!(
            "Cache path must be absolute: {}",
            path.display()
        ))));
    }

    // Check for path traversal attempts
    if path.to_string_lossy().contains("..") {
        return Err(CacheError::DirectoryError(RkitError::ConfigError(
            "Path traversal detected in cache path".to_string(),
        )));
    }

    if let Some(parent) = path.parent() {
        if !parent.exists() {
            log::debug!("Creating cache directory: {}", parent.display());
            fs::create_dir_all(parent).map_err(|e| {
                CacheError::DirectoryError(RkitError::DirectoryCreationError {
                    path: parent.to_path_buf(),
                    source: e,
                })
            })?;
        }

        // Verify directory permissions
        if !parent.is_dir() {
            return Err(CacheError::DirectoryError(RkitError::ConfigError(format!(
                "Cache parent path exists but is not a directory: {}",
                parent.display()
            ))));
        }

        // Check if we have write permissions
        if let Err(e) = fs::metadata(parent) {
            return Err(CacheError::DirectoryError(RkitError::ConfigError(format!(
                "Cannot access cache directory: {}",
                e
            ))));
        }
    }

    if path.exists() && !path.is_file() {
        return Err(CacheError::DirectoryError(RkitError::ConfigError(format!(
            "Cache path exists but is not a file: {}",
            path.display()
        ))));
    }

    Ok(())
}

/// Gets the platform-specific cache path
fn get_cache_path() -> CacheResult<PathBuf> {
    let config_dir = if cfg!(windows) {
        dirs::config_dir().ok_or_else(|| {
            CacheError::DirectoryError(RkitError::ConfigError(
                "Failed to get Windows config directory".to_string(),
            ))
        })?
    } else {
        let home = dirs::home_dir().ok_or_else(|| {
            CacheError::DirectoryError(RkitError::ConfigError(
                "Failed to get home directory".to_string(),
            ))
        })?;
        home.join(".config")
    };

    let cache_path = config_dir.join("rkit").join("cache.json");

    // Log the cache path for debugging
    log::debug!("Using cache path: {}", cache_path.display());

    validate_cache_path(&cache_path)?;
    Ok(cache_path)
}

fn load_cache(cache_path: &Path) -> CacheResult<HashMap<PathBuf, CacheEntry>> {
    if !cache_path.exists() {
        return Ok(HashMap::new());
    }

    let contents = fs::read_to_string(cache_path)?;
    match serde_json::from_str::<CacheData>(&contents) {
        Ok(data) if data.version == 1 => Ok(data.entries),
        Ok(data) => {
            log::warn!("Invalid cache version: {}", data.version);
            Err(CacheError::InvalidVersion(data.version))
        }
        Err(e) => {
            log::warn!("Failed to parse cache file: {}", e);
            Err(CacheError::ParseError(e))
        }
    }
}