diff options
| author | diogo464 <[email protected]> | 2021-12-24 21:00:40 +0000 |
|---|---|---|
| committer | diogo464 <[email protected]> | 2021-12-24 21:00:40 +0000 |
| commit | 793b726b1393210bbfc934bb8236066ae52ebdab (patch) | |
| tree | f86d81c7b266529495829f7de658846ac471ffe2 | |
| parent | b725422dd7db3c0da595575b444b3725b9b33a9d (diff) | |
added some more functions to depot
| -rw-r--r-- | dotup/src/depot.rs | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/dotup/src/depot.rs b/dotup/src/depot.rs index d6f84e8..c8801b9 100644 --- a/dotup/src/depot.rs +++ b/dotup/src/depot.rs | |||
| @@ -179,10 +179,43 @@ impl Depot { | |||
| 179 | depot_get_link(self, link_id) | 179 | depot_get_link(self, link_id) |
| 180 | } | 180 | } |
| 181 | 181 | ||
| 182 | pub fn get_link_by_path(&self, path: &Path) -> Option<&Link> { | ||
| 183 | self.get_link_id_by_path(path) | ||
| 184 | .map(|id| self.get_link(id).unwrap()) | ||
| 185 | } | ||
| 186 | |||
| 187 | pub fn get_link_id_by_path(&self, path: &Path) -> Option<LinkID> { | ||
| 188 | let weak = utils::weakly_canonical(path); | ||
| 189 | for link in self.links() { | ||
| 190 | if link.origin_canonical() == weak { | ||
| 191 | return Some(link.id()); | ||
| 192 | } | ||
| 193 | } | ||
| 194 | None | ||
| 195 | } | ||
| 196 | |||
| 197 | /// checks if there are any linked files/directories under the path `path` | ||
| 198 | /// if `path` is a path to a file and that file is linked then this function returns true | ||
| 199 | pub fn subpath_has_links(&self, path: &Path) -> bool { | ||
| 200 | let canonical = utils::weakly_canonical(path); | ||
| 201 | for link in self.links() { | ||
| 202 | if link.origin_canonical().starts_with(&canonical) { | ||
| 203 | return true; | ||
| 204 | } | ||
| 205 | } | ||
| 206 | false | ||
| 207 | } | ||
| 208 | |||
| 182 | pub fn remove_link(&mut self, link_id: LinkID) { | 209 | pub fn remove_link(&mut self, link_id: LinkID) { |
| 183 | depot_remove_link(self, link_id) | 210 | depot_remove_link(self, link_id) |
| 184 | } | 211 | } |
| 185 | 212 | ||
| 213 | pub fn remove_link_by_path(&mut self, path: &Path) { | ||
| 214 | if let Some(id) = self.get_link_id_by_path(path) { | ||
| 215 | self.remove_link(id); | ||
| 216 | } | ||
| 217 | } | ||
| 218 | |||
| 186 | /// Archives this depot so it can be serialized | 219 | /// Archives this depot so it can be serialized |
| 187 | pub fn archive(&self) -> Archive { | 220 | pub fn archive(&self) -> Archive { |
| 188 | depot_archive(self) | 221 | depot_archive(self) |
| @@ -200,6 +233,10 @@ impl Depot { | |||
| 200 | depot_install_link(self, link, install_base.as_ref()) | 233 | depot_install_link(self, link, install_base.as_ref()) |
| 201 | } | 234 | } |
| 202 | 235 | ||
| 236 | pub fn is_link_installed(&self, link: &Link, install_base: impl AsRef<Path>) -> bool { | ||
| 237 | depot_is_link_installed(link, install_base.as_ref()) | ||
| 238 | } | ||
| 239 | |||
| 203 | pub fn uninstall_link(&self, link: &Link, install_base: impl AsRef<Path>) -> Result<()> { | 240 | pub fn uninstall_link(&self, link: &Link, install_base: impl AsRef<Path>) -> Result<()> { |
| 204 | depot_uninstall_link(self, link, install_base.as_ref()) | 241 | depot_uninstall_link(self, link, install_base.as_ref()) |
| 205 | } | 242 | } |
| @@ -372,6 +409,15 @@ fn depot_install_link( | |||
| 372 | Ok(()) | 409 | Ok(()) |
| 373 | } | 410 | } |
| 374 | 411 | ||
| 412 | fn depot_is_link_installed(link: &Link, install_base: &Path) -> bool { | ||
| 413 | let origin_canonical = link.origin_canonical(); | ||
| 414 | let install_destination = link.install_destination(install_base); | ||
| 415 | match std::fs::read_link(&install_destination) { | ||
| 416 | Ok(target) if target == origin_canonical => true, | ||
| 417 | _ => false, | ||
| 418 | } | ||
| 419 | } | ||
| 420 | |||
| 375 | fn depot_uninstall_link(_depot: &Depot, link: &Link, install_base: &Path) -> Result<()> { | 421 | fn depot_uninstall_link(_depot: &Depot, link: &Link, install_base: &Path) -> Result<()> { |
| 376 | let origin_canonical = link.origin_canonical(); | 422 | let origin_canonical = link.origin_canonical(); |
| 377 | let install_destination = link.install_destination(install_base); | 423 | let install_destination = link.install_destination(install_base); |
