|
| 1 | +use std::path::Path; |
| 2 | +use tokio::fs; |
| 3 | +use walkdir::WalkDir; |
| 4 | + |
| 5 | +use crate::package::install::api::{FileAction, TrackedFile}; |
| 6 | +use crate::project::state::{StagedFile, StateEntry}; |
| 7 | + |
| 8 | +use crate::error::Error; |
| 9 | + |
| 10 | +pub trait TrackedFs { |
| 11 | + /// Create a new instance dedicated to tracking filesystem edits during the |
| 12 | + /// installation of the provided package. |
| 13 | + /// |
| 14 | + /// This essentially creates or opens the cooresponding entry within the |
| 15 | + /// tracked_files.json file and writes any tracked fs modifications to it. |
| 16 | + fn new(state: StateEntry) -> Self; |
| 17 | + |
| 18 | + /// Extract the new StateEntry from this instance. |
| 19 | + fn extract_state(self) -> StateEntry; |
| 20 | + |
| 21 | + /// Copy a file from a source to a destination, overwriting it if the file |
| 22 | + /// already exists. |
| 23 | + /// |
| 24 | + /// This will append (or overwrite) a FileAction::Create entry. |
| 25 | + async fn file_copy(&mut self, src: &Path, dst: &Path, stage_dst: Option<&Path>) -> Result<(), Error>; |
| 26 | + |
| 27 | + /// Delete some target file. |
| 28 | + /// |
| 29 | + /// If `tracked` is set this this will append a FileAction::Delete entry, |
| 30 | + /// overwriting one if it already exists for this file. |
| 31 | + async fn file_delete(&mut self, target: &Path, tracked: bool); |
| 32 | + |
| 33 | + /// Recursively copy a source directory to a destination, overwriting it if |
| 34 | + /// it already exists. |
| 35 | + /// |
| 36 | + /// This will append (or overwrite) a FileAction::Create entry for each file |
| 37 | + /// copied while recursing. |
| 38 | + async fn dir_copy(&mut self, src: &Path, dst: &Path) -> Result<(), Error>; |
| 39 | + |
| 40 | + /// Recursively delete some target directory. |
| 41 | + /// |
| 42 | + /// If `tracked` if set then this will append a FileAction::Delete entry |
| 43 | + /// for each file deleted while recursing, otherwise matching entries are |
| 44 | + /// deleted. |
| 45 | + async fn dir_delete(&mut self, target: &Path, tracked: bool); |
| 46 | +} |
| 47 | + |
| 48 | +#[derive(Debug)] |
| 49 | +pub struct ConcreteFs { |
| 50 | + state: StateEntry, |
| 51 | +} |
| 52 | + |
| 53 | +impl TrackedFs for ConcreteFs { |
| 54 | + fn new(state: StateEntry) -> Self { |
| 55 | + ConcreteFs { |
| 56 | + state |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + fn extract_state(self) -> StateEntry { |
| 61 | + self.state |
| 62 | + } |
| 63 | + |
| 64 | + async fn file_copy(&mut self, src: &Path, dst: &Path, stage_dst: Option<&Path>) -> Result<(), Error> { |
| 65 | + fs::copy(src, dst).await?; |
| 66 | + let tracked = TrackedFile { action: FileAction::Create, path: dst.to_path_buf(), context: None }; |
| 67 | + |
| 68 | + if let Some(stage_dst) = stage_dst { |
| 69 | + let mut staged = StagedFile::new(tracked)?; |
| 70 | + staged.dest.push(stage_dst.to_path_buf()); |
| 71 | + self.state.add_staged(staged, false); |
| 72 | + } else { |
| 73 | + self.state.add_linked(tracked, false); |
| 74 | + } |
| 75 | + |
| 76 | + Ok(()) |
| 77 | + } |
| 78 | + |
| 79 | + async fn file_delete(&mut self, target: &Path, tracked: bool) { |
| 80 | + todo!() |
| 81 | + } |
| 82 | + |
| 83 | + async fn dir_copy(&mut self, src: &Path, dst: &Path) -> Result<(), Error> { |
| 84 | + let files = WalkDir::new(&src) |
| 85 | + .into_iter() |
| 86 | + .filter_map(|e| e.ok()) |
| 87 | + .filter(|x| x.path().is_file()); |
| 88 | + |
| 89 | + for file in files { |
| 90 | + let dest = dst.join(file.path().strip_prefix(&src).unwrap()); |
| 91 | + let dest_parent = dest.parent().unwrap(); |
| 92 | + |
| 93 | + if !dest_parent.is_dir() { |
| 94 | + fs::create_dir_all(dest_parent).await?; |
| 95 | + } |
| 96 | + |
| 97 | + self.file_copy(file.path(), &dest, None).await?; |
| 98 | + } |
| 99 | + |
| 100 | + Ok(()) |
| 101 | + } |
| 102 | + |
| 103 | + async fn dir_delete(&mut self, target: &Path, tracked: bool) { |
| 104 | + todo!() |
| 105 | + } |
| 106 | +} |
0 commit comments