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
use std::path::{Path, PathBuf}; use crate::link_type::{LinkHardness, LinkType, LinkFileType}; use crate::link_error::{LinkError, PreLinkError, DuringLinkError}; pub mod link_type; pub mod link_error; #[cfg(windows)] mod windows; #[cfg(not(windows))] mod wsl; pub fn mklink<'a>(target: &'a Path, link: &'a Path) -> Link<'a> { Link { target, link } } #[derive(Clone, Copy)] pub struct Link<'a> { pub target: &'a Path, pub link: &'a Path, } impl<'a> Link<'a> { pub fn with_hardness(&self, hardness: LinkHardness) -> HardenedLink<'a> { HardenedLink { link: *self, hardness } } pub fn with_type(&self, link_type: LinkType) -> Result<MkLink, LinkError<PreLinkError>> { self.with_hardness(link_type.hardness()) .with_type(link_type.file_type()) } } #[derive(Clone, Copy)] pub struct HardenedLink<'a> { pub link: Link<'a>, pub hardness: LinkHardness, } impl<'a> HardenedLink<'a> { pub fn with_type_unchecked(&self, file_type: LinkFileType) -> MkLink<'a> { let this = *self; MkLink { link: this.link, link_type: LinkType::new(this.hardness, file_type), } } fn get_inferred_file_type(&self) -> Result<LinkFileType, PreLinkError> { match self.link.target.metadata() { Ok(metadata) => { match LinkFileType::from(metadata.file_type()) { Some(file_type) => Ok(file_type), None => Err(PreLinkError::InvalidFileType) } } Err(_) => Err(PreLinkError::InferredNonExistentTarget), } } pub fn with_type(&self, file_type: LinkFileType) -> Result<MkLink<'a>, LinkError<PreLinkError>> { match self.get_inferred_file_type() { Ok(real_file_type) => { if real_file_type == file_type { Ok(self.with_type_unchecked(file_type)) } else { Err(PreLinkError::LinkFileTypeMismatch) } } Err(PreLinkError::InferredNonExistentTarget) => Ok(self.with_type_unchecked(file_type)), Err(e) => Err(e), }.map_err(|it| LinkError::target(it)) } pub fn infer_type(&self) -> Result<MkLink<'a>, LinkError<PreLinkError>> { self.get_inferred_file_type() .map(|it| self.with_type_unchecked(it)) .map_err(|it| LinkError::target(it)) } pub fn maybe_with_type(&self, file_type: Option<LinkFileType>) -> Result<MkLink<'a>, LinkError<PreLinkError>> { match file_type { None => self.infer_type(), Some(file_type) => self.with_type(file_type), } } } #[derive(Clone, Copy)] pub struct MkLink<'a> { pub link: Link<'a>, pub link_type: LinkType, } impl<'a> MkLink<'a> { fn resolve_link(&self) -> Result<PathBuf, LinkError<DuringLinkError>> { let link = self.link.link; let e = Err(LinkError::link(DuringLinkError::LinkAlreadyExists)); match link.metadata() { Ok(metadata) => match metadata.is_dir() { true => match self.link.target.file_name() { Some(file_name) => { let link = link.join(file_name); match link.exists() { true => e, false => Ok(link), } }, None => e, }, false => e, }, Err(_) => Ok(link.to_path_buf()), } } pub fn create_and<F: FnOnce(&MkLink)>(&self, after: F) -> Result<(), LinkError<DuringLinkError>> { let link = self.resolve_link()?; let mk_link = MkLink { link: Link { target: self.link.target, link: link.as_path(), }, link_type: self.link_type, }; mk_link.create_impl()?; after(&mk_link); Ok(()) } pub fn create(&self) -> Result<(), LinkError<DuringLinkError>> { self.create_and(|_| {}) } }