Skip to content

Various repository fixes #146

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/composefs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ tempfile = { version = "3.8.0", optional = true, default-features = false }
xxhash-rust = { version = "0.8.2", default-features = false, features = ["xxh32"] }
zerocopy = { version = "0.8.0", default-features = false, features = ["derive", "std"] }
zstd = { version = "0.13.0", default-features = false }
rand = { version = "0.9.1", default-features = true }

[dev-dependencies]
insta = "1.42.2"
Expand Down
61 changes: 36 additions & 25 deletions crates/composefs/src/repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use anyhow::{bail, ensure, Context, Result};
use once_cell::sync::OnceCell;
use rustix::{
fs::{
fdatasync, flock, linkat, mkdirat, open, openat, readlinkat, symlinkat, AtFlags, Dir,
FileType, FlockOperation, Mode, OFlags, CWD,
fdatasync, flock, linkat, mkdirat, open, openat, readlinkat, AtFlags, Dir, FileType,
FlockOperation, Mode, OFlags, CWD,
},
io::{Errno, Result as ErrnoResult},
};
Expand All @@ -25,7 +25,7 @@ use crate::{
},
mount::mount_composefs_at,
splitstream::{DigestMap, SplitStreamReader, SplitStreamWriter},
util::{proc_self_fd, Sha256Digest},
util::{filter_errno, proc_self_fd, replace_symlinkat, Sha256Digest},
};

/// Call openat() on the named subdirectory of "dirfd", possibly creating it first.
Expand Down Expand Up @@ -261,7 +261,7 @@ impl<ObjectID: FsVerityHashValue> Repository<ObjectID> {
let stream_path = format!("streams/{}", hex::encode(sha256));
let object_id = writer.done()?;
let object_path = Self::format_object_path(&object_id);
self.ensure_symlink(&stream_path, &object_path)?;
self.symlink(&stream_path, &object_path)?;

if let Some(name) = reference {
let reference_path = format!("streams/refs/{name}");
Expand Down Expand Up @@ -310,7 +310,7 @@ impl<ObjectID: FsVerityHashValue> Repository<ObjectID> {
let object_id = writer.done()?;

let object_path = Self::format_object_path(&object_id);
self.ensure_symlink(&stream_path, &object_path)?;
self.symlink(&stream_path, &object_path)?;
object_id
}
};
Expand All @@ -331,9 +331,11 @@ impl<ObjectID: FsVerityHashValue> Repository<ObjectID> {
let filename = format!("streams/{name}");

let file = File::from(if let Some(verity_hash) = verity {
self.open_with_verity(&filename, verity_hash)?
self.open_with_verity(&filename, verity_hash)
.with_context(|| format!("Opening ref 'streams/{name}'"))?
} else {
self.openat(&filename, OFlags::RDONLY)?
self.openat(&filename, OFlags::RDONLY)
.with_context(|| format!("Opening ref 'streams/{name}'"))?
});

SplitStreamReader::new(file)
Expand Down Expand Up @@ -366,7 +368,7 @@ impl<ObjectID: FsVerityHashValue> Repository<ObjectID> {
let object_path = Self::format_object_path(&object_id);
let image_path = format!("images/{}", object_id.to_hex());

self.ensure_symlink(&image_path, &object_path)?;
self.symlink(&image_path, &object_path)?;

if let Some(reference) = name {
let ref_path = format!("images/refs/{reference}");
Expand All @@ -384,7 +386,9 @@ impl<ObjectID: FsVerityHashValue> Repository<ObjectID> {
}

pub fn open_image(&self, name: &str) -> Result<OwnedFd> {
let image = self.openat(&format!("images/{name}"), OFlags::RDONLY)?;
let image = self
.openat(&format!("images/{name}"), OFlags::RDONLY)
.with_context(|| format!("Opening ref 'images/{name}'"))?;

if !name.contains("/") {
// A name with no slashes in it is taken to be a sha256 fs-verity digest
Expand Down Expand Up @@ -432,14 +436,8 @@ impl<ObjectID: FsVerityHashValue> Repository<ObjectID> {
relative.push(target_component);
}

symlinkat(relative, &self.repository, name)
}

pub fn ensure_symlink<P: AsRef<Path>>(&self, name: P, target: &str) -> ErrnoResult<()> {
self.symlink(name, target).or_else(|e| match e {
Errno::EXIST => Ok(()),
_ => Err(e),
})
// Atomically replace existing symlink
replace_symlinkat(&relative, &self.repository, name)
}

fn read_symlink_hashvalue(dirfd: &OwnedFd, name: &CStr) -> Result<ObjectID> {
Expand Down Expand Up @@ -485,15 +483,28 @@ impl<ObjectID: FsVerityHashValue> Repository<ObjectID> {
fn gc_category(&self, category: &str) -> Result<HashSet<ObjectID>> {
let mut objects = HashSet::new();

let category_fd = self.openat(category, OFlags::RDONLY | OFlags::DIRECTORY)?;
let Some(category_fd) = filter_errno(
self.openat(category, OFlags::RDONLY | OFlags::DIRECTORY),
Errno::NOENT,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW I maintain https://docs.rs/cap-std-ext/latest/cap_std_ext/dirext/trait.CapStdExtDirExt.html#tymethod.open_dir_optional

We had some previous debates about using cap-std here. I (obviously) like it a lot.

filter_errno makes sense as is but it'd probably be good to have a higher level openat_optional wrapper per above.

)
.context("Opening {category} dir in repository")?
else {
return Ok(objects);
};

let refs = openat(
&category_fd,
"refs",
OFlags::RDONLY | OFlags::DIRECTORY,
Mode::empty(),
)?;
Self::walk_symlinkdir(refs, &mut objects)?;
if let Some(refs) = filter_errno(
openat(
&category_fd,
"refs",
OFlags::RDONLY | OFlags::DIRECTORY,
Mode::empty(),
),
Errno::NOENT,
)
.context("Opening {category}/refs dir in repository")?
{
Self::walk_symlinkdir(refs, &mut objects)?;
}

for item in Dir::read_from(&category_fd)? {
let entry = item?;
Expand Down
71 changes: 70 additions & 1 deletion crates/composefs/src/util.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
use rand::{distr::Alphanumeric, Rng};
use std::{
io::{Error, ErrorKind, Read, Result},
os::fd::{AsFd, AsRawFd},
os::{
fd::{AsFd, AsRawFd, OwnedFd},
unix::ffi::OsStrExt,
},
path::Path,
};

use rustix::{
fs::{readlinkat, renameat, symlinkat, unlinkat, AtFlags},
io::{Errno, Result as ErrnoResult},
};
use tokio::io::{AsyncRead, AsyncReadExt};

/// Formats a string like "/proc/self/fd/3" for the given fd. This can be used to work with kernel
Expand Down Expand Up @@ -97,6 +106,66 @@ pub fn parse_sha256(string: impl AsRef<str>) -> Result<Sha256Digest> {
Ok(value)
}

pub(crate) fn filter_errno<T>(
result: rustix::io::Result<T>,
ignored: Errno,
) -> ErrnoResult<Option<T>> {
match result {
Ok(result) => Ok(Some(result)),
Err(err) if err == ignored => Ok(None),
Err(err) => Err(err),
}
}

fn generate_tmpname(prefix: &str) -> String {
let rand_string: String = rand::rng()
.sample_iter(&Alphanumeric)
.take(12)
.map(char::from)
.collect();
format!("{}{}", prefix, rand_string)
}

pub(crate) fn replace_symlinkat(
target: impl AsRef<Path>,
dirfd: &OwnedFd,
name: impl AsRef<Path>,
) -> ErrnoResult<()> {
let name = name.as_ref();
let target = target.as_ref();

// Step 1: try to create the symlink
if filter_errno(symlinkat(target, dirfd, name), Errno::EXIST)?.is_some() {
return Ok(());
};

// Step 2: the symlink already exists. Maybe it already has the correct target?
if let Some(current_target) = filter_errno(readlinkat(dirfd, name, []), Errno::NOENT)? {
if current_target.into_bytes() == target.as_os_str().as_bytes() {
return Ok(());
}
}

// Step 3: full atomic replace path
for _ in 0..16 {
let tmp_name = generate_tmpname(".symlink-");
if filter_errno(symlinkat(target, dirfd, &tmp_name), Errno::EXIST)?.is_none() {
// This temporary filename already exists, try another
continue;
}

match renameat(dirfd, &tmp_name, dirfd, name) {
Ok(_) => return Ok(()),
Err(e) => {
let _ = unlinkat(dirfd, tmp_name, AtFlags::empty());
return Err(e);
}
}
}

Err(Errno::EXIST)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That could probably use some context...

}

#[cfg(test)]
mod test {
use similar_asserts::assert_eq;
Expand Down
Loading