Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement symlinks for virtual-fs #4062

Draft
wants to merge 22 commits into
base: main
Choose a base branch
from
Draft
1 change: 1 addition & 0 deletions lib/virtual-fs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ enable-serde = ["typetag", "serde"]
no-time = []
# Enables memory tracking/limiting functionality for the in-memory filesystem.
tracking = []
symlink = []

[package.metadata.docs.rs]
rustc-args = ["--cfg", "docsrs"]
6 changes: 6 additions & 0 deletions lib/virtual-fs/src/arc_fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ impl FileSystem for ArcFileSystem {
self.fs.metadata(path)
}

#[cfg(feature = "symlink")]
fn symlink(&self, original: &Path, link: &Path) -> Result<()> {
self.fs.symlink(original, link)
}

#[cfg(feature = "symlink")]
fn symlink_metadata(&self, path: &Path) -> Result<Metadata> {
self.fs.symlink_metadata(path)
}
Expand Down
6 changes: 6 additions & 0 deletions lib/virtual-fs/src/empty_fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ impl FileSystem for EmptyFileSystem {
}
}

#[cfg(feature = "symlink")]
fn symlink(&self, original: &Path, link: &Path) -> Result<()> {
Err(FsError::EntryNotFound)
}

#[cfg(feature = "symlink")]
fn symlink_metadata(&self, path: &Path) -> Result<Metadata> {
Err(FsError::EntryNotFound)
}
Expand Down
22 changes: 22 additions & 0 deletions lib/virtual-fs/src/host_fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,28 @@ impl crate::FileSystem for FileSystem {
.and_then(TryInto::try_into)
.map_err(Into::into)
}

#[cfg(feature = "symlink")]
fn symlink(&self, original: &Path, link: &Path) -> Result<()> {
#[cfg(taget_os = "windows")]
if original.is_dir() {
std::os::windows::fs::symlink_dir(original, link)?;
} else {
std::os::windows::fs::symlink_file(original, link)?;
}

#[cfg(not(taget_os = "windows"))]
std::os::unix::fs::symlink(original, link)?;

Ok(())
}

#[cfg(feature = "symlink")]
fn symlink_metadata(&self, path: &Path) -> Result<Metadata> {
fs::symlink_metadata(path)
.and_then(TryInto::try_into)
.map_err(Into::into)
}
}

impl TryInto<Metadata> for std::fs::Metadata {
Expand Down
22 changes: 22 additions & 0 deletions lib/virtual-fs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,27 @@ pub trait FileSystem: fmt::Debug + Send + Sync + 'static + Upcastable {
fn remove_dir(&self, path: &Path) -> Result<()>;
fn rename<'a>(&'a self, from: &'a Path, to: &'a Path) -> BoxFuture<'a, Result<()>>;
fn metadata(&self, path: &Path) -> Result<Metadata>;

#[cfg(not(feature = "symlink"))]
/// This method gets metadata without following symlinks in the path.
/// Currently identical to `metadata` because symlinks aren't implemented
/// yet.
fn symlink_metadata(&self, path: &Path) -> Result<Metadata> {
self.metadata(path)
}
#[cfg(feature = "symlink")]
fn symlink_metadata(&self, _path: &Path) -> Result<Metadata> {
unimplemented!()
}

fn remove_file(&self, path: &Path) -> Result<()>;

fn new_open_options(&self) -> OpenOptions;

#[cfg(feature = "symlink")]
fn symlink(&self, _original: &Path, _link: &Path) -> Result<()> {
unimplemented!()
}
}

impl dyn FileSystem + 'static {
Expand Down Expand Up @@ -140,6 +152,16 @@ where
(**self).metadata(path)
}

#[cfg(feature = "symlink")]
fn symlink(&self, original: &Path, link: &Path) -> Result<()> {
(**self).symlink(original, link)
}

#[cfg(feature = "symlink")]
fn symlink_metadata(&self, path: &Path) -> Result<Metadata> {
(**self).symlink_metadata(path)
}

fn remove_file(&self, path: &Path) -> Result<()> {
(**self).remove_file(path)
}
Expand Down
20 changes: 20 additions & 0 deletions lib/virtual-fs/src/mem_fs/file_opener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,21 @@ impl crate::FileOpener for FileSystem {
}
};

// If node is a symlink, follow it.
#[cfg(feature = "symlink")]
{
// Read lock.
let fs = self.inner.read().map_err(|_| FsError::Lock)?;

if let Some(Node::Symlink(SymlinkNode { link, .. })) =
fs.storage.get(inode_of_file)
{
let link = link.clone();
drop(fs);
return self.open(&link, conf);
}
}

// Write lock.
let mut fs = self.inner.write().map_err(|_| FsError::Lock)?;

Expand Down Expand Up @@ -456,6 +471,11 @@ impl crate::FileOpener for FileSystem {
}
}

#[cfg(feature = "symlink")]
Some(Node::Symlink(SymlinkNode { link, .. })) => {
return self.open(dbg!(link), conf);
}

None => return Err(FsError::EntryNotFound),
_ => return Err(FsError::NotAFile),
}
Expand Down
Loading
Loading