Skip to content

Commit

Permalink
Adding back absolute path methods
Browse files Browse the repository at this point in the history
  • Loading branch information
NicholasLYang committed May 8, 2023
1 parent 94abe0b commit 08fe50f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
20 changes: 19 additions & 1 deletion crates/turbopath/src/absolute_system_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ use std::os::unix::fs::symlink as symlink_file;
use std::os::unix::fs::symlink as symlink_dir;
#[cfg(windows)]
use std::os::windows::fs::{symlink_dir, symlink_file};
use std::{borrow::Cow, fmt, fs, fs::Metadata, io, path::Path};
use std::{
borrow::Cow,
fmt, fs,
fs::Metadata,
io,
path::{Path, PathBuf},
};

use path_slash::PathExt;

Expand Down Expand Up @@ -112,4 +118,16 @@ impl AbsoluteSystemPath {
pub fn stat(&self) -> Result<Metadata, PathError> {
Ok(fs::metadata(&self.0)?)
}

pub fn symlink_metadata(&self) -> Result<Metadata, PathError> {
Ok(fs::symlink_metadata(&self.0)?)
}

pub fn read_link(&self) -> Result<PathBuf, io::Error> {
fs::read_link(&self.0)
}

pub fn remove_file(&self) -> Result<(), io::Error> {
fs::remove_file(&self.0)
}
}
14 changes: 7 additions & 7 deletions crates/turborepo-fs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use turbopath::{AbsoluteSystemPath, AnchoredSystemPathBuf};
use walkdir::WalkDir;

pub fn recursive_copy(src: &AbsoluteSystemPath, dst: &AbsoluteSystemPath) -> Result<()> {
let src_metadata = fs::symlink_metadata(src)?;
let src_metadata = src.symlink_metadata()?;
if src_metadata.is_dir() {
let walker = WalkDir::new(src.as_path()).follow_links(false);
for entry in walker.into_iter() {
Expand Down Expand Up @@ -70,7 +70,7 @@ fn make_dir_copy(dir: &AbsoluteSystemPath, src_metadata: &Metadata) -> Result<()
}

pub fn copy_file(from: &AbsoluteSystemPath, to: &AbsoluteSystemPath) -> Result<()> {
let metadata = fs::symlink_metadata(from)?;
let metadata = from.symlink_metadata()?;
copy_file_with_type(from, metadata.file_type(), to)
}

Expand All @@ -80,10 +80,10 @@ fn copy_file_with_type(
to: &AbsoluteSystemPath,
) -> Result<()> {
if from_type.is_symlink() {
let target = fs::read_link(from)?;
let target = from.read_link()?;
to.ensure_dir()?;
if fs::symlink_metadata(to).is_ok() {
fs::remove_file(to)?;
if to.symlink_metadata().is_ok() {
to.remove_file()?;
}
to.symlink_to_file(target)?;
Ok(())
Expand Down Expand Up @@ -244,8 +244,8 @@ mod tests {
assert_eq!(a_contents, b_contents);
}

fn assert_target_matches<P: AsRef<Path>>(link: &AbsoluteSystemPathBuf, expected: P) {
let path = fs::read_link(link).unwrap();
fn assert_target_matches<P: AsRef<Path>>(link: &AbsoluteSystemPath, expected: P) {
let path = link.read_link().unwrap();
assert_eq!(path.as_path(), expected.as_ref());
}
}

0 comments on commit 08fe50f

Please sign in to comment.