graftfs 0.1.0

A Rust implementation of the GNU stow utility for managing dotfiles. It is a symlink farm manager that takes separate packages of software and/or data and makes them appear to be installed in the same place. Features include stow, delete, and restow operations, simulation mode, directory folding, and regex-based ignore/override patterns.
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
572
573
/*
 * graftfs
 * Copyright (C) 2026 Chris Tisdale
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

use crate::commands::CommandError;
use std::fs::ReadDir;
use std::path::{Path, PathBuf};
use std::{fs, os};
use tracing::{debug, info, instrument, warn};

pub trait CommandOperation<T: Iterator<Item = Result<PathBuf, CommandError>>> {
    /// Creates a symbolic link from the `source` path to the `target` path.
    ///
    /// # Parameters
    /// - `target`: A reference to the path where the symbolic link will be created.
    /// - `source`: A reference to the path that the symbolic link will point to.
    ///
    /// # Returns
    /// - `Ok(())`: If the symbolic link is successfully created.
    /// - `Err(CommandError)`: If there is an error during the symbolic link creation process.
    ///
    /// # Errors
    /// This method returns a `CommandError` if:
    /// - The `target` or `source` path is invalid.
    /// - The underlying operating system fails to create the symbolic link.
    ///
    /// # Platform Compatibility
    /// - This method relies on the operating system's ability to create symbolic links,
    ///   which may have restrictions or require specific permissions on some platforms.
    ///   For example, on Windows, symbolic links require administrative privileges.
    ///
    /// # Examples
    /// ```
    /// use std::path::Path;
    /// use crate::commands::{CommandOperation, CommandOperationImpl};
    ///
    /// let mut instance = CommandOperationImpl::default();
    /// let result = instance.link_item(
    ///     Path::new("/path/to/target"),
    ///     Path::new("/path/to/source")
    /// );
    ///
    /// match result {
    ///     Ok(_) => println!("Symbolic link created successfully."),
    ///     Err(e) => eprintln!("Failed to create symbolic link: {:?}", e),
    /// }
    /// ```
    fn link_item(&mut self, target: &Path, source: &Path) -> Result<(), CommandError>;

    /// Removes a symbolic link or junction point at the specified target path.
    ///
    /// # Parameters
    /// - `target`: A reference to a [`Path`] indicating the target of the link to be removed.
    ///
    /// # Returns
    /// - `Ok(())`: If the link was successfully removed.
    /// - `Err(CommandError)`: If an error occurs during the removal process. This could happen due to
    ///   insufficient permissions, the target not being a link, or other system-level issues.
    ///
    /// # Errors
    /// - Returns a `CommandError` if:
    ///   - The target path does not exist.
    ///   - The target is not a symbolic link or junction.
    ///   - There are not enough permissions to remove the link.
    ///   - Any other I/O or system-level error occurs during the operation.
    ///
    /// # Remarks
    /// This function operates on symbolic links and junction points. It does not follow the link or
    /// delete any file or directory that the link points to. Ensure the target path exists and is
    /// a valid link before calling this function.
    ///
    /// # Example
    /// ```
    /// use std::path::Path;
    /// use crate::commands::{CommandOperation, CommandOperationImpl};
    ///
    /// let mut instance = CommandOperationImpl::default();
    /// let link_path = Path::new("/path/to/symlink");
    /// match instance.remove_link(link_path) {
    ///     Ok(_) => println!("Link removed successfully."),
    ///     Err(err) => eprintln!("Failed to remove link: {}", err),
    /// }
    /// ```
    fn remove_link(&mut self, target: &Path) -> Result<(), CommandError>;

    /// Removes a specified item from a given path.
    ///
    /// This method attempts to remove the item located at the provided `target` path.
    /// If the operation is successful, it returns `Ok(())`. If an error occurs during
    /// the removal process, it will return a `CommandError` with details of the failure.
    ///
    /// # Parameters
    /// - `target`: A reference to a [`Path`] that specifies the location of the item to be removed.
    ///
    /// # Returns
    /// - `Ok(())` if the operation is successful.
    /// - `Err(CommandError)` if there is an issue removing the item.
    ///
    /// # Errors
    /// This method will return a [`CommandError`] in scenarios such as:
    /// - The path does not exist.
    /// - Insufficient permissions to remove the item.
    /// - The target is in use and cannot be removed.
    /// - Any other I/O-related errors.
    ///
    /// # Examples
    /// ```
    /// use std::path::Path;
    /// use crate::commands::{CommandOperation, CommandOperationImpl};
    ///
    /// let mut instance = CommandOperationImpl::default();
    /// let path = Path::new("/path/to/item");
    /// match instance.remove_item(&path) {
    ///     Ok(_) => println!("Item was successfully removed."),
    ///     Err(e) => eprintln!("Failed to remove item: {:?}", e),
    /// }
    /// ```
    fn remove_item(&mut self, target: &Path) -> Result<(), CommandError>;

    /// Creates a new directory at the specified target path.
    ///
    /// # Arguments
    /// * `target` - A reference to the [`Path`] where the directory should be created.
    ///
    /// # Returns
    /// * `Ok(())` if the directory was successfully created.
    /// * `Err(CommandError)` if there was an error during the directory creation process.
    ///
    /// # Errors
    /// This function will return a `CommandError` in the following scenarios:
    /// - If the target path is invalid.
    /// - If the directory already exists and cannot be replaced (depending on the implementation).
    /// - If there are not enough permissions to create the directory at the target location.
    /// - If there is another I/O-related error during the creation process.
    ///
    /// # Examples
    /// ```
    /// use std::path::Path;
    /// use crate::commands::{CommandOperation, CommandOperationImpl};
    ///
    /// let mut instance = CommandOperationImpl::default();
    /// let path = Path::new("/some/directory");
    /// match instance.create_directory(path) {
    ///     Ok(_) => println!("Directory created successfully!"),
    ///     Err(e) => eprintln!("Failed to create directory: {:?}", e),
    /// }
    /// ```
    fn create_directory(&mut self, target: &Path) -> Result<(), CommandError>;

    /// Checks if the given path corresponds to a directory.
    ///
    /// This function takes a reference to a `Path` and determines whether the
    /// path represents a directory on the filesystem. If the metadata for the
    /// given path cannot be retrieved (e.g., the path does not exist or there
    /// are insufficient permissions), the function will return `false`.
    ///
    /// # Arguments
    ///
    /// * `target` - A reference to a `Path` representing the file system entry
    ///   to check.
    ///
    /// # Returns
    ///
    /// * `true` if the path corresponds to a directory.
    /// * `false` if the path is not a directory, does not exist, or if an error
    ///   occurs while accessing the path's metadata.
    ///
    /// # Examples
    ///
    /// ```
    /// use std::path::Path;
    /// use std::fs;
    /// use crate::commands::{CommandOperation, CommandOperationImpl};
    ///
    /// let instance = CommandOperationImpl::default();
    /// let path = Path::new("./example_directory");
    /// let is_dir = instance.is_directory(&path);
    /// println!("Is it a directory? {}", is_dir);
    /// ```
    fn is_directory(&self, target: &Path) -> bool;

    /// Checks whether the given path refers to a file.
    ///
    /// # Parameters
    /// - `target`: A reference to a `Path` that represents the filesystem path to be checked.
    ///
    /// # Returns
    /// - `true` if the specified path exists and points to a regular file.
    /// - `false` if the path does not exist, cannot be accessed, or does not refer to a file.
    ///
    /// # Examples
    /// ```
    /// use std::path::Path;
    /// use crate::commands::{CommandOperation, CommandOperationImpl};
    ///
    /// let instance = CommandOperationImpl::default();
    /// let file_path = Path::new("example.txt");
    /// let result = instance.is_file(file_path);
    /// println!("Is file: {}", result);
    /// ```
    fn is_file(&self, target: &Path) -> bool;

    /// Checks if the given path is a symbolic link.
    ///
    /// # Parameters
    /// - `target`: A reference to a `Path` that represents the target to be checked.
    ///
    /// # Returns
    /// - `true` if the `target` is a symbolic link.
    /// - `false` otherwise.
    ///
    /// # Example
    /// ```
    /// use std::path::Path;
    /// use crate::commands::{CommandOperation, CommandOperationImpl};
    ///
    /// let instance = CommandOperationImpl::default();
    /// let path = Path::new("/some/path");
    /// let is_link = instance.is_symlink(&path);
    /// println!("Is symlink: {}", is_link);
    /// ```
    ///
    /// # Notes
    /// - This function uses the `is_symlink` method provided by the `Path` type
    ///   under the hood to determine if the path is a symbolic link.
    fn is_symlink(&self, target: &Path) -> bool;

    /// Reads the symbolic link at the specified target path and returns the resolved path.
    ///
    /// This function attempts to read the symbolic link located at the given `target` path and
    /// resolves it to the actual path it points to. If the operation is successful, it returns
    /// the resolved path as a `PathBuf`. If an error occurs while reading the symbolic link,
    /// the function returns a `CommandError`.
    ///
    /// # Arguments
    ///
    /// * `target` - A reference to a `Path` representing the location of the symbolic link to read.
    ///
    /// # Returns
    ///
    /// * `Result<PathBuf, CommandError>` - On success, the resolved path is returned as a `PathBuf`.
    ///   On failure, an error of type `CommandError` is returned.
    ///
    /// # Errors
    ///
    /// This function will return an error if:
    /// * The specified `target` does not exist or is not a symbolic link.
    /// * There are not enough permissions to read the symbolic link.
    /// * Other I/O errors occur during the operation.
    ///
    /// # Examples
    /// ```
    /// use std::path::Path;
    /// use crate::commands::{CommandOperation, CommandOperationImpl};
    ///
    /// let instance = CommandOperationImpl::default();
    /// let file_path = Path::new("example.txt");
    /// let result = instance.read_link(file_path);
    /// match result {
    ///     Ok(path) => println!("Resolved path: {}", path.display()),
    ///     Err(e) => println!("Error reading link: {}", e),
    /// }
    /// ```
    fn read_link(&self, target: &Path) -> Result<PathBuf, CommandError>;

    /// Checks if a given file path or directory exists.
    ///
    /// # Arguments
    ///
    /// * `target` - A reference to a `Path` that specifies the file or directory to check.
    ///
    /// # Returns
    ///
    /// * `true` if the specified path exists in the filesystem.
    /// * `false` if the specified path does not exist.
    ///
    /// # Examples
    ///
    /// ```
    /// use std::path::Path;
    /// use crate::commands::{CommandOperation, CommandOperationImpl};
    ///
    /// let instance = CommandOperationImpl::default();
    /// let path = Path::new("/some/file/path");
    /// let result = instance.exists(&path);
    /// println!("Does the path exist? {}", result);
    /// ```
    fn exists(&self, target: &Path) -> bool;

    /// Reads the contents of a directory at the specified path.
    ///
    /// # Parameters
    /// - `target`: A reference to a [`Path`] specifying the directory to read.
    ///
    /// # Returns
    /// - `Ok(ReadDir)`: An iterator over the entries within the directory if successful.
    /// - `Err(CommandError)`: An error if the directory cannot be read.
    ///
    /// # Errors
    /// This function will return an error in the following cases:
    /// - The provided path does not exist.
    /// - The path is not a directory.
    /// - Insufficient permissions to read the directory.
    ///
    /// # Example
    /// ```
    /// use std::path::Path;
    /// use crate::commands::{CommandOperation, CommandOperationImpl};
    ///
    /// let instance = CommandOperationImpl::default();
    /// let path = Path::new("/some/directory");
    /// match instance.read_directory(path) {
    ///     Ok(entries) => {
    ///         for entry in entries {
    ///             println!("{:?}", entry);
    ///         }
    ///     },
    ///     Err(e) => println!("Failed to read directory: {}", e),
    /// }
    /// ```
    fn read_directory(&self, target: &Path) -> Result<T, CommandError>;
}

/// Represents the implementation type of a command operation.
///
/// This enum has two variants:
///
/// - `Default`: The default implementation type, used when no specific operation is specified.
/// - `Simulated`: Represents a simulated implementation type, often used for testing or scenarios where the operation doesn't interact with a real system.
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub enum CommandOperationImpl {
    #[default]
    Default,
    Simulated(SimulatedData),
}

#[derive(Debug, Default, Clone, PartialEq, Eq)]
struct LinkedDirectory {
    path: PathBuf,
    link_path: PathBuf,
}

/// A structure representing simulated data for tracking created directories and symbolic links.
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct SimulatedData {
    created_directories: Vec<PathBuf>,
    created_links: Vec<LinkedDirectory>,
}

/// A struct that provides an iterator for reading entries in a directory.
///
/// The `DirectoryReader` struct wraps a `ReadDir` instance, which is returned
/// by the standard library's `std::fs::read_dir` function. It allows for
/// iterating over the entries of a directory and accessing metadata such as
/// file names and file types.
pub struct DirectoryReader {
    read_dir: ReadDir,
}

impl From<ReadDir> for DirectoryReader {
    fn from(read_dir: ReadDir) -> Self {
        Self { read_dir }
    }
}

impl Iterator for DirectoryReader {
    type Item = Result<PathBuf, CommandError>;

    fn next(&mut self) -> Option<Self::Item> {
        self.read_dir
            .next()
            .map(|res| res.map_err(Into::into).map(|entry| entry.path()))
    }
}

impl CommandOperation<DirectoryReader> for CommandOperationImpl {
    #[cfg(any(target_os = "macos", target_os = "linux"))]
    #[instrument(level = "trace")]
    fn link_item(&mut self, item: &Path, target: &Path) -> Result<(), CommandError> {
        match self {
            Self::Default => {
                info!("Linking {} {}", item.display(), target.display());
                os::unix::fs::symlink(item, target)?;
            }
            Self::Simulated(data) => {
                data.created_links.push(LinkedDirectory {
                    path: item.to_path_buf(),
                    link_path: target.to_path_buf(),
                });

                println!("LINK: {} => {}", item.display(), target.display());
            }
        }

        Ok(())
    }

    #[cfg(target_os = "windows")]
    #[instrument(level = "trace")]
    fn link_item(&mut self, item: &Path, target: &Path) -> Result<(), CommandError> {
        match self {
            Self::Default => {
                info!("Linking {} {}", item.display(), target.display());
                if self.is_directory(item) {
                    os::windows::fs::symlink_dir(item, target)?;
                } else {
                    os::windows::fs::symlink_file(item, target)?;
                }
            }
            Self::Simulated(data) => {
                data.created_links.push(LinkedDirectory {
                    path: item.to_path_buf(),
                    link_path: target.to_path_buf(),
                });

                println!("LINK: {} => {}", item.display(), target.display());
            }
        }

        Ok(())
    }

    #[cfg(any(target_os = "macos", target_os = "linux"))]
    #[instrument(level = "trace")]
    fn remove_link(&mut self, entry_path: &Path) -> Result<(), CommandError> {
        if !self.is_symlink(entry_path) {
            warn!("Not a symlink: {}", entry_path.display());
            return Ok(());
        }

        match self {
            Self::Default => {
                debug!("Deleting symlink: {}", entry_path.display());
                fs::remove_file(entry_path)?;
            }
            Self::Simulated(_) => {
                println!("UNLINK: {}", entry_path.display());
            }
        }

        Ok(())
    }

    #[cfg(target_os = "windows")]
    #[instrument(level = "trace")]
    fn remove_link(&mut self, entry_path: &Path) -> Result<(), CommandError> {
        if !self.is_symlink(entry_path) {
            warn!("Not a symlink: {}", entry_path.display());
            return Ok(());
        }

        match self {
            Self::Default => {
                debug!("Deleting symlink: {}", entry_path.display());
                if self.is_directory(entry_path) {
                    fs::remove_dir(entry_path)?;
                } else {
                    fs::remove_file(entry_path)?;
                }
            }
            Self::Simulated(_) => {
                println!("UNLINK: {}", entry_path.display());
            }
        }

        Ok(())
    }

    #[instrument(level = "trace")]
    fn remove_item(&mut self, target: &Path) -> Result<(), CommandError> {
        match self {
            Self::Default => {
                if self.is_directory(target) {
                    debug!("Deleting directory: {}", target.display());
                    fs::remove_dir(target)?;
                } else {
                    debug!("Deleting file: {}", target.display());
                    fs::remove_file(target)?;
                }
            }
            Self::Simulated(_) => println!("RM: {}", target.display()),
        }

        Ok(())
    }

    #[instrument(level = "trace")]
    fn create_directory(&mut self, target: &Path) -> Result<(), CommandError> {
        match self {
            Self::Default => {
                debug!("Creating directory: {}", target.display());
                fs::create_dir_all(target)?;
            }
            Self::Simulated(data) => {
                data.created_directories.push(target.to_owned());
                println!("MKDIR: {}", target.display());
            }
        }

        Ok(())
    }

    #[instrument(level = "trace")]
    fn is_directory(&self, target: &Path) -> bool {
        if fs::metadata(target).is_ok_and(|meta| meta.is_dir()) {
            return true;
        }

        match self {
            Self::Default => false,
            Self::Simulated(data) => data.created_directories.contains(&target.to_path_buf()),
        }
    }

    #[instrument(level = "trace")]
    fn is_file(&self, target: &Path) -> bool {
        fs::metadata(target).is_ok_and(|meta| meta.is_file())
    }

    #[instrument(level = "trace")]
    fn is_symlink(&self, target: &Path) -> bool {
        fs::symlink_metadata(target).is_ok_and(|meta| meta.is_symlink())
    }

    #[cfg(not(target_os = "windows"))]
    #[instrument(level = "trace")]
    fn read_link(&self, target: &Path) -> Result<PathBuf, CommandError> {
        target.canonicalize().map_err(Into::into)
    }

    #[cfg(target_os = "windows")]
    #[instrument(level = "trace")]
    fn read_link(&self, target: &Path) -> Result<PathBuf, CommandError> {
        target.read_link().map_err(Into::into)
    }

    #[instrument(level = "trace")]
    fn exists(&self, target: &Path) -> bool {
        if fs::exists(target).unwrap_or(false) {
            return true;
        }

        match self {
            Self::Default => false,
            Self::Simulated(data) => {
                data.created_directories.contains(&target.to_path_buf())
                    || data
                        .created_links
                        .iter()
                        .any(|link| link.link_path == target)
            }
        }
    }

    #[instrument(level = "trace")]
    fn read_directory(&self, target: &Path) -> Result<DirectoryReader, CommandError> {
        fs::read_dir(target).map_err(Into::into).map(Into::into)
    }
}