mod common;
use std::path::Path;
use std::process::Command;
use common::{TmpDir, fixture_repo, ostree_available};
use futures_lite::AsyncReadExt;
use futures_lite::io::Cursor;
use ostrya::{
Checksum, CreateOptions, Error, FileKind, FileMeta, FsckOptions, Repo, RepoMode, Transaction,
TransactionStats,
};
use ostrya_core::{ObjectType, loose_path};
use ostrya_rt::block_on;
const HELLO: &[u8] = b"hello ostree\n";
const NESTED: &[u8] = b"nested\n";
const EMPTY: &[u8] = b"";
const HELLO_TXT: &str = common::HELLO_TXT;
const EMPTY_TXT: &str = common::EMPTY_TXT;
const NESTED_TXT: &str = common::NESTED_TXT;
const LINK: &str = common::LINK;
fn csum(hex: &str) -> Checksum {
Checksum::from_hex(hex).unwrap()
}
fn reg() -> FileMeta {
FileMeta::regular(0, 0, 0o644)
}
async fn write_fixture_tree(txn: &Transaction) {
assert_eq!(
txn.write_regfile_inline(Some(&csum(HELLO_TXT)), ®(), HELLO)
.await
.unwrap(),
csum(HELLO_TXT),
"hello.txt identity"
);
assert_eq!(
txn.write_content(None, ®(), Cursor::new(NESTED.to_vec()))
.await
.unwrap(),
csum(NESTED_TXT),
"nested.txt identity"
);
assert_eq!(
txn.write_regfile_inline(None, ®(), EMPTY).await.unwrap(),
csum(EMPTY_TXT),
"empty.txt identity"
);
assert_eq!(
txn.write_symlink("hello.txt", &FileMeta::regular(0, 0, 0), Some(&csum(LINK)))
.await
.unwrap(),
csum(LINK),
"link identity"
);
}
fn object_bytes(root: &Path, hex: &str, ty: ObjectType, mode: RepoMode) -> Vec<u8> {
std::fs::read(root.join("objects").join(loose_path(&csum(hex), ty, mode))).unwrap()
}
fn fixture_bytes(mode_dir: &str, hex: &str, ty: ObjectType, mode: RepoMode) -> Vec<u8> {
std::fs::read(
fixture_repo(mode_dir)
.join("objects")
.join(loose_path(&csum(hex), ty, mode)),
)
.unwrap()
}
fn ostreemeta(root: &Path, hex: &str, mode: RepoMode) -> Vec<u8> {
let path = root
.join("objects")
.join(loose_path(&csum(hex), ObjectType::File, mode));
let mut buf = vec![0u8; 256];
let n = rustix::fs::getxattr(&path, "user.ostreemeta", &mut buf).unwrap();
buf.truncate(n);
buf
}
fn inode_perm(root: &Path, hex: &str, ty: ObjectType, mode: RepoMode) -> u32 {
let path = root.join("objects").join(loose_path(&csum(hex), ty, mode));
let stat = rustix::fs::stat(&path).unwrap();
stat.st_mode & 0o7777
}
#[test]
fn archive_objects_are_byte_identical_to_the_fixture() {
let tmp = TmpDir::new("write-archive");
let root = tmp.path().join("repo");
block_on(async {
let repo = Repo::create(&root, CreateOptions::new(RepoMode::Archive))
.await
.unwrap();
let txn = repo.transaction().await.unwrap();
write_fixture_tree(&txn).await;
let stats = txn.commit().await.unwrap();
assert_eq!(stats.content_written, 4);
for hex in [HELLO_TXT, EMPTY_TXT, NESTED_TXT, LINK] {
assert_eq!(
object_bytes(&root, hex, ObjectType::File, RepoMode::Archive),
fixture_bytes("archive", hex, ObjectType::File, RepoMode::Archive),
"archive object {hex}"
);
assert_eq!(
inode_perm(&root, hex, ObjectType::File, RepoMode::Archive),
0o644
);
}
});
}
#[test]
fn bare_user_objects_match_the_fixture() {
let tmp = TmpDir::new("write-bare-user");
let root = tmp.path().join("repo");
block_on(async {
let repo = Repo::create(&root, CreateOptions::new(RepoMode::BareUser))
.await
.unwrap();
let txn = repo.transaction().await.unwrap();
write_fixture_tree(&txn).await;
txn.commit().await.unwrap();
for (hex, payload) in [(HELLO_TXT, HELLO), (EMPTY_TXT, EMPTY), (NESTED_TXT, NESTED)] {
assert_eq!(
object_bytes(&root, hex, ObjectType::File, RepoMode::BareUser),
payload,
"bare-user payload {hex}"
);
assert_eq!(
ostreemeta(&root, hex, RepoMode::BareUser),
fixture_ostreemeta("bare-user", hex),
"bare-user user.ostreemeta {hex}"
);
assert_eq!(
inode_perm(&root, hex, ObjectType::File, RepoMode::BareUser),
0o644
);
}
assert_eq!(
object_bytes(&root, LINK, ObjectType::File, RepoMode::BareUser),
b"hello.txt\0"
);
assert_eq!(
ostreemeta(&root, LINK, RepoMode::BareUser),
fixture_ostreemeta("bare-user", LINK),
);
});
}
fn fixture_ostreemeta(mode_dir: &str, hex: &str) -> Vec<u8> {
let path = fixture_repo(mode_dir).join("objects").join(loose_path(
&csum(hex),
ObjectType::File,
RepoMode::BareUser,
));
let mut buf = vec![0u8; 256];
let n = rustix::fs::getxattr(&path, "user.ostreemeta", &mut buf).unwrap();
buf.truncate(n);
buf
}
#[test]
fn bare_user_shared_shares_bare_user_identity_with_fixed_mode() {
let tmp = TmpDir::new("write-shared");
let root = tmp.path().join("repo");
block_on(async {
let repo = Repo::create(&root, CreateOptions::new(RepoMode::BareUserShared))
.await
.unwrap();
let txn = repo.transaction().await.unwrap();
write_fixture_tree(&txn).await;
txn.commit().await.unwrap();
for (hex, payload) in [(HELLO_TXT, HELLO), (NESTED_TXT, NESTED)] {
assert_eq!(
object_bytes(&root, hex, ObjectType::File, RepoMode::BareUserShared),
payload
);
assert_eq!(
ostreemeta(&root, hex, RepoMode::BareUserShared),
fixture_ostreemeta("bare-user", hex),
);
assert_eq!(
inode_perm(&root, hex, ObjectType::File, RepoMode::BareUserShared),
0o644
);
}
});
}
#[test]
fn bare_user_private_shares_bare_user_identity_with_mode_0400() {
let tmp = TmpDir::new("write-private");
let root = tmp.path().join("repo");
block_on(async {
let repo = Repo::create(&root, CreateOptions::new(RepoMode::BareUserPrivate))
.await
.unwrap();
let txn = repo.transaction().await.unwrap();
write_fixture_tree(&txn).await;
txn.commit().await.unwrap();
for (hex, payload) in [(HELLO_TXT, HELLO), (EMPTY_TXT, EMPTY), (NESTED_TXT, NESTED)] {
assert_eq!(
object_bytes(&root, hex, ObjectType::File, RepoMode::BareUserPrivate),
payload
);
assert_eq!(
ostreemeta(&root, hex, RepoMode::BareUserPrivate),
fixture_ostreemeta("bare-user", hex),
);
assert_eq!(
inode_perm(&root, hex, ObjectType::File, RepoMode::BareUserPrivate),
0o400
);
}
assert_eq!(
object_bytes(&root, LINK, ObjectType::File, RepoMode::BareUserPrivate),
b"hello.txt\0"
);
assert_eq!(
ostreemeta(&root, LINK, RepoMode::BareUserPrivate),
fixture_ostreemeta("bare-user", LINK),
);
assert_eq!(
inode_perm(&root, LINK, ObjectType::File, RepoMode::BareUserPrivate),
0o400
);
});
}
const UMASK_ENV: &str = "OSTRYA_WRITE_UMASK_CHILD";
fn ostreemeta_at(path: &Path) -> Vec<u8> {
let mut buf = vec![0u8; 256];
let n = rustix::fs::getxattr(path, "user.ostreemeta", &mut buf).unwrap();
buf.truncate(n);
buf
}
fn assert_same_content(a: &Path, b: &Path, what: &Path) {
use std::io::Read as _;
const CHUNK: usize = 8192;
let mut a_file = std::fs::File::open(a).unwrap();
let mut b_file = std::fs::File::open(b).unwrap();
let len = a_file.metadata().unwrap().len();
assert_eq!(
len,
b_file.metadata().unwrap().len(),
"the length of {}",
what.display()
);
let mut a_buf = [0u8; CHUNK];
let mut b_buf = [0u8; CHUNK];
let mut offset = 0u64;
while offset < len {
let n = usize::try_from(len - offset).map_or(CHUNK, |rest| rest.min(CHUNK));
a_file.read_exact(&mut a_buf[..n]).unwrap();
b_file.read_exact(&mut b_buf[..n]).unwrap();
assert!(
a_buf[..n] == b_buf[..n],
"{}: the chunk at offset {offset} is different",
what.display()
);
offset += n as u64;
}
}
#[test]
fn bare_user_private_modes_under_umask_0077() {
use common::modes::{build_tree, file_objects, repo_of};
use std::os::unix::fs::PermissionsExt;
if let Some(marker) = std::env::var_os(UMASK_ENV) {
let marker = std::path::PathBuf::from(marker);
rustix::process::umask(rustix::fs::Mode::from_raw_mode(0o077));
if rustix::process::geteuid().is_root() {
eprintln!("note: root skips the permission check, so the write order has no effect");
}
let tmp = TmpDir::new("write-private-umask-child");
let src = build_tree(tmp.path());
block_on(async {
let (private, repo, commit) =
repo_of(tmp.path(), RepoMode::BareUserPrivate, &src).await;
let (plain, _, plain_commit) = repo_of(tmp.path(), RepoMode::BareUser, &src).await;
assert_eq!(
commit, plain_commit,
"the commit is the same as in bare-user"
);
let mut content = 0;
for fanout in std::fs::read_dir(private.join("objects")).unwrap() {
let fanout = fanout.unwrap();
for entry in std::fs::read_dir(fanout.path()).unwrap() {
let path = entry.unwrap().path();
let meta = std::fs::symlink_metadata(&path).unwrap();
assert!(meta.file_type().is_file(), "{}", path.display());
let perm = meta.permissions().mode() & 0o7777;
if path.extension().is_some_and(|e| e == "file") {
content += 1;
assert_eq!(perm, 0o400, "{}", path.display());
let rel = path.strip_prefix(&private).unwrap();
let twin = plain.join(rel);
assert_same_content(&path, &twin, rel);
assert_eq!(
ostreemeta_at(&path),
ostreemeta_at(&twin),
"{}",
rel.display()
);
} else {
assert_eq!(perm, 0o644, "{}", path.display());
}
}
}
assert_eq!(content, 5);
let mut modes = Vec::new();
for checksum in file_objects(&repo, &commit).await {
modes.push(repo.load_file(&checksum).await.unwrap().mode);
}
assert!(modes.contains(&0o100755), "{modes:?}");
let report = repo.fsck(&FsckOptions::default()).await.unwrap();
assert!(report.is_ok(), "fsck reported {:?}", report.errors);
});
std::fs::write(marker, b"ran").unwrap();
return;
}
let tmp = TmpDir::new("write-private-umask");
let marker = tmp.path().join("ran");
let status = Command::new(std::env::current_exe().unwrap())
.args([
"bare_user_private_modes_under_umask_0077",
"--exact",
"--nocapture",
])
.env(UMASK_ENV, &marker)
.status()
.expect("re-execute this test binary");
assert!(status.success(), "the child failed: {status}");
assert!(marker.exists(), "the child ran no check");
}
#[test]
fn write_metadata_stages_a_metadata_object() {
let tmp = TmpDir::new("write-meta");
let root = tmp.path().join("repo");
block_on(async {
let repo = Repo::create(&root, CreateOptions::new(RepoMode::BareUser))
.await
.unwrap();
let dirmeta = ostrya_core::DirMeta {
uid: 0,
gid: 0,
mode: 0o040755,
xattrs: ostrya_core::Xattrs::empty(),
};
let bytes = dirmeta.serialize().unwrap();
let expected = Checksum::sha256(&bytes);
let txn = repo.transaction().await.unwrap();
let c = txn
.write_metadata(ObjectType::DirMeta, Some(&expected), &bytes)
.await
.unwrap();
assert_eq!(
c, expected,
"identity is the sha256 of the normal-form bytes"
);
let stats = txn.commit().await.unwrap();
assert_eq!(stats.metadata_written, 1);
assert_eq!(stats.content_written, 0);
assert_eq!(
object_bytes(&root, &c.to_hex(), ObjectType::DirMeta, RepoMode::BareUser),
bytes
);
assert_eq!(
inode_perm(&root, &c.to_hex(), ObjectType::DirMeta, RepoMode::BareUser),
0o644
);
let repo = Repo::open(&root).await.unwrap();
assert_eq!(repo.load_dirmeta(&c).await.unwrap(), dirmeta);
});
}
#[test]
fn write_metadata_rejects_bare_split_xattrs() {
let tmp = TmpDir::new("write-meta-split");
let root = tmp.path().join("repo");
block_on(async {
let repo = Repo::create(&root, CreateOptions::new(RepoMode::BareSplitXattrs))
.await
.unwrap();
let dirmeta = ostrya_core::DirMeta {
uid: 0,
gid: 0,
mode: 0o040755,
xattrs: ostrya_core::Xattrs::empty(),
};
let bytes = dirmeta.serialize().unwrap();
let txn = repo.transaction().await.unwrap();
let err = txn
.write_metadata(ObjectType::DirMeta, None, &bytes)
.await
.unwrap_err();
assert!(
matches!(err, Error::Unsupported(_)),
"bare-split-xattrs is read-only, got {err:?}"
);
txn.abort().await.unwrap();
});
}
#[test]
fn bare_content_applies_inode_xattrs() {
let tmp = TmpDir::new("write-bare-xattr");
let owned = rustix::fs::stat(tmp.path()).unwrap();
let uid = owned.st_uid;
let gid = owned.st_gid;
let root = tmp.path().join("repo");
block_on(async {
let repo = Repo::create(&root, CreateOptions::new(RepoMode::Bare))
.await
.unwrap();
let txn = repo.transaction().await.unwrap();
let xattrs = ostrya_core::Xattrs::new([
(b"user.one\0".to_vec(), b"first".to_vec()),
(b"user.two\0".to_vec(), b"second".to_vec()),
])
.unwrap();
let mut meta = FileMeta::regular(uid, gid, 0o644);
meta.xattrs = xattrs;
let checksum = txn.write_regfile_inline(None, &meta, HELLO).await.unwrap();
txn.commit().await.unwrap();
let hex = checksum.to_hex();
assert_eq!(
object_bytes(&root, &hex, ObjectType::File, RepoMode::Bare),
HELLO
);
assert_eq!(inode_xattr(&root, &hex, "user.one"), b"first");
assert_eq!(inode_xattr(&root, &hex, "user.two"), b"second");
});
}
fn inode_xattr(root: &Path, hex: &str, name: &str) -> Vec<u8> {
let path = root
.join("objects")
.join(loose_path(&csum(hex), ObjectType::File, RepoMode::Bare));
let mut buf = vec![0u8; 256];
let n = rustix::fs::getxattr(&path, name, &mut buf).unwrap();
buf.truncate(n);
buf
}
#[test]
fn rewriting_an_object_is_a_dedup_noop() {
let tmp = TmpDir::new("write-dedup");
let root = tmp.path().join("repo");
block_on(async {
let repo = Repo::create(&root, CreateOptions::new(RepoMode::BareUser))
.await
.unwrap();
let txn = repo.transaction().await.unwrap();
let a = txn.write_regfile_inline(None, ®(), HELLO).await.unwrap();
let b = txn.write_regfile_inline(None, ®(), HELLO).await.unwrap();
assert_eq!(a, b, "same content, same identity");
let stats: TransactionStats = txn.commit().await.unwrap();
assert_eq!(
stats.content_written, 1,
"the second write is a dedup no-op"
);
let txn = repo.transaction().await.unwrap();
assert_eq!(
txn.write_regfile_inline(None, ®(), HELLO).await.unwrap(),
a
);
let stats = txn.commit().await.unwrap();
assert_eq!(
stats.content_written, 0,
"already published, so no new object"
);
});
}
#[test]
fn free_space_guard_trips_on_an_exhausted_budget() {
let tmp = TmpDir::new("write-space");
let root = tmp.path().join("repo");
block_on(async {
Repo::create(&root, CreateOptions::new(RepoMode::BareUser))
.await
.unwrap();
let config = root.join("config");
let mut text = std::fs::read_to_string(&config).unwrap();
text.push_str("min-free-space-percent=100\n");
std::fs::write(&config, text).unwrap();
let repo = Repo::open(&root).await.unwrap();
let txn = repo.transaction().await.unwrap();
let err = txn
.write_regfile_inline(None, ®(), HELLO)
.await
.unwrap_err();
assert!(
matches!(err, Error::InsufficientFreeSpace { shortfall } if shortfall > 0),
"expected a free-space error, got {err:?}"
);
txn.abort().await.unwrap();
});
}
#[test]
fn a_bad_configured_fsync_is_refused_under_every_override() {
let tmp = TmpDir::new("write-fsync-bad-config");
let root = tmp.path().join("repo");
block_on(async {
Repo::create(&root, CreateOptions::new(RepoMode::BareUser))
.await
.unwrap();
let config = root.join("config");
let mut text = std::fs::read_to_string(&config).unwrap();
text.push_str("fsync=bogus\n");
std::fs::write(&config, text).unwrap();
let repo = Repo::open(&root).await.unwrap();
for override_value in [None, Some(true), Some(false)] {
let mut txn = repo.transaction().await.unwrap();
if let Some(enabled) = override_value {
txn.set_fsync(enabled);
}
let err = txn
.write_regfile_inline(None, ®(), HELLO)
.await
.unwrap_err();
assert!(
matches!(
&err,
Error::Core(ostrya_core::Error::KeyFile(text))
if text.contains("core.fsync")
),
"override {override_value:?} gave {err:?} in place of the config refusal",
);
txn.abort().await.unwrap();
}
});
}
#[test]
fn a_bad_configured_per_object_fsync_is_refused_under_every_override() {
let tmp = TmpDir::new("write-per-object-fsync-bad-config");
let root = tmp.path().join("repo");
block_on(async {
Repo::create(&root, CreateOptions::new(RepoMode::BareUser))
.await
.unwrap();
let config = root.join("config");
let mut text = std::fs::read_to_string(&config).unwrap();
text.push_str("per-object-fsync=bogus\n");
std::fs::write(&config, text).unwrap();
let repo = Repo::open(&root).await.unwrap();
let rows = [
(None, None),
(Some(true), None),
(Some(false), None),
(None, Some(false)),
(Some(true), Some(false)),
];
for (per_object, fsync) in rows {
let mut txn = repo.transaction().await.unwrap();
if let Some(enabled) = per_object {
txn.set_per_object_fsync(enabled);
}
if let Some(enabled) = fsync {
txn.set_fsync(enabled);
}
let err = txn
.write_regfile_inline(None, ®(), HELLO)
.await
.unwrap_err();
assert!(
matches!(
&err,
Error::Core(ostrya_core::Error::KeyFile(text))
if text.contains("core.per-object-fsync")
),
"overrides {per_object:?}/{fsync:?} gave {err:?} in place of the config refusal",
);
txn.abort().await.unwrap();
}
});
}
#[test]
fn concurrent_writers_share_one_transaction() {
let tmp = TmpDir::new("write-concurrent");
let root = tmp.path().join("repo");
let repo = block_on(Repo::create(&root, CreateOptions::new(RepoMode::BareUser))).unwrap();
let txn = block_on(repo.transaction()).unwrap();
const N: usize = 8;
let payloads: Vec<Vec<u8>> = (0..N)
.map(|i| format!("payload number {i}\n").into_bytes())
.collect();
std::thread::scope(|scope| {
for payload in &payloads {
let txn = &txn;
scope.spawn(move || {
block_on(async {
txn.write_content(None, ®(), Cursor::new(payload.clone()))
.await
.unwrap();
});
});
}
});
let stats = block_on(txn.commit()).unwrap();
assert_eq!(
stats.content_written as usize, N,
"each writer staged one object"
);
block_on(async {
let repo = Repo::open(&root).await.unwrap();
for payload in &payloads {
let checksum = object_checksum_of(&repo, ®(), payload).await;
let file = repo.load_file(&checksum).await.unwrap();
let mut got = Vec::new();
file.reader()
.await
.unwrap()
.read_to_end(&mut got)
.await
.unwrap();
assert_eq!(&got, payload);
}
});
}
async fn object_checksum_of(repo: &Repo, meta: &FileMeta, payload: &[u8]) -> Checksum {
let txn = repo.transaction().await.unwrap();
let c = txn.write_regfile_inline(None, meta, payload).await.unwrap();
txn.abort().await.unwrap();
c
}
#[test]
fn content_reads_back_through_load_file() {
let tmp = TmpDir::new("write-roundtrip");
let root = tmp.path().join("repo");
block_on(async {
let repo = Repo::create(&root, CreateOptions::new(RepoMode::Archive))
.await
.unwrap();
let txn = repo.transaction().await.unwrap();
write_fixture_tree(&txn).await;
txn.commit().await.unwrap();
let repo = Repo::open(&root).await.unwrap();
let hello = repo.load_file(&csum(HELLO_TXT)).await.unwrap();
assert_eq!(
hello.kind,
FileKind::Regular {
size: HELLO.len() as u64
}
);
assert_eq!((hello.uid, hello.gid, hello.mode), (0, 0, 0o100644));
let mut got = Vec::new();
hello
.reader()
.await
.unwrap()
.read_to_end(&mut got)
.await
.unwrap();
assert_eq!(got, HELLO);
let link = repo.load_file(&csum(LINK)).await.unwrap();
assert_eq!(
link.kind,
FileKind::Symlink {
target: "hello.txt".to_owned()
}
);
});
}
#[test]
fn a_read_only_mode_is_stored_in_bare_user() {
let tmp = TmpDir::new("write-readonly");
let root = tmp.path().join("repo");
block_on(async {
let repo = Repo::create(&root, CreateOptions::new(RepoMode::BareUser))
.await
.unwrap();
let txn = repo.transaction().await.unwrap();
let meta = FileMeta::regular(0, 0, 0o444);
let inline = txn.write_regfile_inline(None, &meta, HELLO).await.unwrap();
let streamed = txn
.write_content(None, &meta, Cursor::new(NESTED.to_vec()))
.await
.unwrap();
txn.commit().await.unwrap();
for checksum in [inline, streamed] {
let path = root.join("objects").join(loose_path(
&checksum,
ObjectType::File,
RepoMode::BareUser,
));
let stored = rustix::fs::stat(&path).unwrap();
assert_eq!(stored.st_mode & 0o7777, 0o444, "stored mode of {checksum}");
let file = repo.load_file(&checksum).await.unwrap();
assert_eq!((file.uid, file.gid, file.mode), (0, 0, 0o100444));
}
});
}
#[test]
fn a_read_only_mode_with_an_xattr_is_stored_in_bare() {
let tmp = TmpDir::new("write-bare-readonly");
let owned = rustix::fs::stat(tmp.path()).unwrap();
let uid = owned.st_uid;
let gid = owned.st_gid;
let root = tmp.path().join("repo");
block_on(async {
let repo = Repo::create(&root, CreateOptions::new(RepoMode::Bare))
.await
.unwrap();
let txn = repo.transaction().await.unwrap();
let mut meta = FileMeta::regular(uid, gid, 0o444);
meta.xattrs =
ostrya_core::Xattrs::new([(b"user.demo\0".to_vec(), b"value".to_vec())]).unwrap();
let inline = txn.write_regfile_inline(None, &meta, HELLO).await.unwrap();
let streamed = txn
.write_content(None, &meta, Cursor::new(NESTED.to_vec()))
.await
.unwrap();
txn.commit().await.unwrap();
for checksum in [inline, streamed] {
let hex = checksum.to_hex();
assert_eq!(inode_xattr(&root, &hex, "user.demo"), b"value");
let file = repo.load_file(&checksum).await.unwrap();
assert_eq!((file.uid, file.gid, file.mode), (uid, gid, 0o100444));
}
});
}
#[test]
fn bare_objects_match_the_tool() {
if !ostree_available() {
eprintln!("skipping bare_objects_match_the_tool: the ostree tool is unavailable");
return;
}
let tmp = TmpDir::new("write-bare");
let owned = rustix::fs::stat(tmp.path()).unwrap();
let uid = owned.st_uid;
let gid = owned.st_gid;
let port_root = tmp.path().join("port");
block_on(async {
let repo = Repo::create(&port_root, CreateOptions::new(RepoMode::Bare))
.await
.unwrap();
let txn = repo.transaction().await.unwrap();
let meta = FileMeta::regular(uid, gid, 0o644);
txn.write_regfile_inline(None, &meta, HELLO).await.unwrap();
txn.write_regfile_inline(None, &meta, NESTED).await.unwrap();
txn.write_regfile_inline(None, &meta, EMPTY).await.unwrap();
txn.write_symlink("hello.txt", &FileMeta::regular(uid, gid, 0), None)
.await
.unwrap();
txn.commit().await.unwrap();
});
let tool_root = tmp.path().join("tool");
let src = tmp.path().join("src");
std::fs::create_dir_all(&src).unwrap();
std::fs::write(src.join("hello.txt"), HELLO).unwrap();
std::fs::write(src.join("nested.txt"), NESTED).unwrap();
std::fs::write(src.join("empty.txt"), EMPTY).unwrap();
std::os::unix::fs::symlink("hello.txt", src.join("link")).unwrap();
for f in ["hello.txt", "nested.txt", "empty.txt"] {
std::fs::set_permissions(
src.join(f),
std::os::unix::fs::PermissionsExt::from_mode(0o644),
)
.unwrap();
}
let repo_arg = format!("--repo={}", tool_root.display());
run_ostree(&[&repo_arg, "init", "--mode=bare"]);
run_ostree(&[
&repo_arg,
"commit",
"--branch=t",
"--subject=x",
&format!("--owner-uid={uid}"),
&format!("--owner-gid={gid}"),
"--no-xattrs",
"--timestamp=@1700000000",
src.to_str().unwrap(),
]);
for entry in walk_objects(&tool_root.join("objects")) {
if entry.extension().and_then(|e| e.to_str()) != Some("file") {
continue;
}
let rel = entry.strip_prefix(tool_root.join("objects")).unwrap();
let ours = port_root.join("objects").join(rel);
let our_meta = std::fs::symlink_metadata(&ours)
.unwrap_or_else(|_| panic!("port is missing object {rel:?}"));
let tool_meta = std::fs::symlink_metadata(&entry).unwrap();
use std::os::unix::fs::MetadataExt;
assert_eq!(tool_meta.mode(), our_meta.mode(), "mode of {rel:?}");
assert_eq!(tool_meta.uid(), our_meta.uid(), "uid of {rel:?}");
assert_eq!(tool_meta.gid(), our_meta.gid(), "gid of {rel:?}");
if tool_meta.file_type().is_symlink() {
assert_eq!(
std::fs::read_link(&entry).unwrap(),
std::fs::read_link(&ours).unwrap(),
"symlink target of {rel:?}"
);
} else {
assert_eq!(
std::fs::read(&entry).unwrap(),
std::fs::read(&ours).unwrap(),
"content of {rel:?}"
);
}
}
}
#[test]
fn bare_user_only_hashes_the_canonical_header() {
let tmp = TmpDir::new("write-buo-canon");
let buo_root = tmp.path().join("buo");
let bu_root = tmp.path().join("bu");
block_on(async {
let mut meta = FileMeta::regular(4242, 4242, 0o777);
meta.xattrs =
ostrya_core::Xattrs::new([(b"user.demo\0".to_vec(), b"value".to_vec())]).unwrap();
let repo = Repo::create(&buo_root, CreateOptions::new(RepoMode::BareUserOnly))
.await
.unwrap();
let txn = repo.transaction().await.unwrap();
let inline = txn.write_regfile_inline(None, &meta, HELLO).await.unwrap();
let streamed = txn
.write_content(None, &meta, Cursor::new(NESTED.to_vec()))
.await
.unwrap();
let link = txn.write_symlink("hello.txt", &meta, None).await.unwrap();
txn.commit().await.unwrap();
let canon = FileMeta::regular(0, 0, 0o755);
let bu = Repo::create(&bu_root, CreateOptions::new(RepoMode::BareUser))
.await
.unwrap();
let txn = bu.transaction().await.unwrap();
let canon_inline = txn.write_regfile_inline(None, &canon, HELLO).await.unwrap();
let canon_streamed = txn
.write_content(None, &canon, Cursor::new(NESTED.to_vec()))
.await
.unwrap();
let canon_link = txn.write_symlink("hello.txt", &canon, None).await.unwrap();
txn.commit().await.unwrap();
assert_eq!(inline, canon_inline, "inline regular-file identity");
assert_eq!(streamed, canon_streamed, "streamed regular-file identity");
assert_eq!(link, canon_link, "symlink identity");
for checksum in [inline, streamed] {
let file = repo.load_file(&checksum).await.unwrap();
assert_eq!((file.uid, file.gid, file.mode), (0, 0, 0o100755));
assert!(file.xattrs.is_empty(), "xattrs of {checksum}");
}
let file = repo.load_file(&link).await.unwrap();
assert_eq!((file.uid, file.gid), (0, 0));
let report = repo.fsck(&FsckOptions::default()).await.unwrap();
assert!(report.is_ok(), "fsck reported {:?}", report.errors);
});
}
fn dirmeta_object(mode: u32) -> (Vec<u8>, Checksum) {
let bytes = ostrya_core::DirMeta {
uid: 0,
gid: 0,
mode,
xattrs: ostrya_core::Xattrs::empty(),
}
.serialize()
.unwrap();
let checksum = Checksum::sha256(&bytes);
(bytes, checksum)
}
fn invalid_format<T: std::fmt::Debug>(result: ostrya::Result<T>) -> String {
match result {
Err(Error::InvalidFormat(m)) => m,
other => panic!("{other:?}"),
}
}
fn symlink_refusal(path: &str) -> String {
format!("{path} passes through a symlink; ostrya refuses a symlink in the repository")
}
fn symlink_fanout(root: &Path, object: &str, moved: &Path, target: &Path) {
let fanout = root.join("objects").join(&object[..2]);
std::fs::rename(&fanout, moved).unwrap();
std::fs::create_dir(target).unwrap();
std::os::unix::fs::symlink(target, &fanout).unwrap();
}
#[test]
fn a_symlink_at_a_fanout_fails_a_write_below_it() {
let tmp = TmpDir::new("write-fanout-symlink");
let root = tmp.path().join("repo");
let mode = RepoMode::BareUser;
let (bytes, dirmeta) = dirmeta_object(0o040755);
let file_path = loose_path(&csum(HELLO_TXT), ObjectType::File, mode);
let meta_path = loose_path(&dirmeta, ObjectType::DirMeta, mode);
block_on(async {
let repo = Repo::create(&root, CreateOptions::new(mode)).await.unwrap();
let txn = repo.transaction().await.unwrap();
txn.write_regfile_inline(None, ®(), HELLO).await.unwrap();
txn.write_metadata(ObjectType::DirMeta, None, &bytes)
.await
.unwrap();
txn.commit().await.unwrap();
});
let targets = [
tmp.path().join("file-target"),
tmp.path().join("meta-target"),
];
symlink_fanout(
&root,
&file_path,
&tmp.path().join("file-moved"),
&targets[0],
);
if meta_path[..2] != file_path[..2] {
symlink_fanout(
&root,
&meta_path,
&tmp.path().join("meta-moved"),
&targets[1],
);
}
let (file, meta) = block_on(async {
let repo = Repo::open(&root).await.unwrap();
let txn = repo.transaction().await.unwrap();
let file = txn.write_regfile_inline(None, ®(), HELLO).await;
let meta = txn.write_metadata(ObjectType::DirMeta, None, &bytes).await;
txn.abort().await.unwrap();
(file, meta)
});
assert_eq!(
invalid_format(file),
symlink_refusal(&format!("objects/{file_path}"))
);
assert_eq!(
invalid_format(meta),
symlink_refusal(&format!("objects/{meta_path}"))
);
for target in targets.iter().filter(|t| t.exists()) {
assert_eq!(std::fs::read_dir(target).unwrap().count(), 0, "{target:?}");
}
}
#[test]
fn the_publish_refuses_a_fanout_that_is_not_a_directory() {
let mode = RepoMode::BareUser;
let fanout = &loose_path(&csum(HELLO_TXT), ObjectType::File, mode)[..2];
for symlink in [true, false] {
let tmp = TmpDir::new("write-publish-fanout");
let root = tmp.path().join("repo");
let target = tmp.path().join("target");
let result = block_on(async {
let repo = Repo::create(&root, CreateOptions::new(mode)).await.unwrap();
let txn = repo.transaction().await.unwrap();
txn.write_regfile_inline(None, ®(), HELLO).await.unwrap();
let at = root.join("objects").join(fanout);
if symlink {
std::fs::create_dir(&target).unwrap();
std::os::unix::fs::symlink(&target, &at).unwrap();
} else {
std::fs::write(&at, b"").unwrap();
}
txn.commit().await
});
let expected = if symlink {
symlink_refusal(&format!("objects/{fanout}"))
} else {
format!("objects/{fanout} is not a directory")
};
assert_eq!(invalid_format(result), expected);
if symlink {
assert_eq!(std::fs::read_dir(&target).unwrap().count(), 0);
}
}
}
#[test]
fn the_publish_makes_a_missing_fanout_again() {
let tmp = TmpDir::new("write-publish-missing-fanout");
let root = tmp.path().join("repo");
let mode = RepoMode::BareUser;
let path = loose_path(&csum(HELLO_TXT), ObjectType::File, mode);
let fanout = root.join("objects").join(&path[..2]);
block_on(async {
let repo = Repo::create(&root, CreateOptions::new(mode)).await.unwrap();
let first = repo.transaction().await.unwrap();
first
.write_regfile_inline(None, ®(), HELLO)
.await
.unwrap();
first.commit().await.unwrap();
std::fs::remove_dir_all(&fanout).unwrap();
let second = repo.transaction().await.unwrap();
second
.write_regfile_inline(None, ®(), HELLO)
.await
.unwrap();
assert!(!fanout.exists(), "the stage makes no fan-out directory");
second.commit().await.unwrap();
});
assert!(std::fs::symlink_metadata(&fanout).unwrap().is_dir());
assert_eq!(
std::fs::read(root.join("objects").join(&path)).unwrap(),
HELLO
);
}
#[test]
fn a_second_publish_of_an_object_keeps_the_first_inode() {
use std::os::unix::fs::MetadataExt;
let tmp = TmpDir::new("write-keep-published");
let root = tmp.path().join("repo");
let mode = RepoMode::BareUser;
let object = root
.join("objects")
.join(loose_path(&csum(HELLO_TXT), ObjectType::File, mode));
let (first, second) = block_on(async {
let repo = Repo::create(&root, CreateOptions::new(mode)).await.unwrap();
let one = repo.transaction().await.unwrap();
let two = repo.transaction().await.unwrap();
one.write_regfile_inline(None, ®(), HELLO).await.unwrap();
two.write_regfile_inline(None, ®(), HELLO).await.unwrap();
one.commit().await.unwrap();
let first = std::fs::symlink_metadata(&object).unwrap().ino();
two.commit().await.unwrap();
let second = std::fs::symlink_metadata(&object).unwrap().ino();
(first, second)
});
assert_eq!(first, second, "the second publish keeps the first inode");
assert_eq!(std::fs::read(&object).unwrap().len(), HELLO.len());
}
fn staged_entries(root: &Path) -> Vec<(String, u64)> {
use std::os::unix::fs::MetadataExt;
let mut entries = Vec::new();
for entry in std::fs::read_dir(root.join("tmp")).unwrap() {
let entry = entry.unwrap();
if entry.file_name().to_string_lossy().starts_with("staging-")
&& entry.file_type().unwrap().is_dir()
{
for staged in std::fs::read_dir(entry.path()).unwrap() {
let staged = staged.unwrap();
let ino = std::fs::symlink_metadata(staged.path()).unwrap().ino();
entries.push((staged.file_name().to_string_lossy().into_owned(), ino));
}
}
}
entries.sort();
entries
}
#[test]
fn a_second_stage_of_an_object_keeps_the_staged_entry() {
use std::os::unix::fs::MetadataExt;
let tmp = TmpDir::new("write-keep-staged");
let root = tmp.path().join("repo");
let mode = RepoMode::Bare;
let (bytes, dirmeta) = dirmeta_object(0o040700);
let uid = rustix::process::geteuid().as_raw();
let gid = rustix::process::getegid().as_raw();
let meta = FileMeta::regular(uid, gid, 0o777);
let (staged, link) = block_on(async {
let repo = Repo::create(&root, CreateOptions::new(mode)).await.unwrap();
let txn = repo.transaction().await.unwrap();
let mut link = None;
let mut staged = Vec::new();
for _ in 0..2 {
txn.write_metadata(ObjectType::DirMeta, None, &bytes)
.await
.unwrap();
link = Some(txn.write_symlink("target", &meta, None).await.unwrap());
staged.push(staged_entries(&root));
}
txn.commit().await.unwrap();
(staged, link.unwrap())
});
let mut expected = vec![
format!("{}.dirmeta", dirmeta.to_hex()),
format!("{}.file", link.to_hex()),
];
expected.sort();
let names: Vec<String> = staged[0].iter().map(|(name, _)| name.clone()).collect();
assert_eq!(names, expected);
assert_eq!(
staged[1], staged[0],
"the second write keeps each staged inode"
);
let published = root
.join("objects")
.join(loose_path(&link, ObjectType::File, mode));
let stat = std::fs::symlink_metadata(&published).unwrap();
assert!(stat.file_type().is_symlink());
assert_eq!((stat.uid(), stat.gid()), (uid, gid));
assert_eq!(std::fs::read_link(&published).unwrap(), Path::new("target"));
}
fn run_ostree(args: &[&str]) {
let status = Command::new("ostree")
.args(args)
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::null())
.status()
.expect("run ostree");
assert!(status.success(), "ostree {args:?} failed");
}
fn walk_objects(dir: &Path) -> Vec<std::path::PathBuf> {
let mut out = Vec::new();
for fanout in std::fs::read_dir(dir).unwrap().flatten() {
if fanout.file_type().unwrap().is_dir() {
for obj in std::fs::read_dir(fanout.path()).unwrap().flatten() {
out.push(obj.path());
}
}
}
out
}