Skip to content

Commit 5f9d7e2

Browse files
allisonkarlitskayacgwalters
authored andcommitted
image: wrap LeafContent::Regular{Inline,External}
Create a new LeafContent::Regular which covers both cases and contains another enum which can be either RegularFile::Inline or ::External. This is a bit more typing in some cases but lets us consolidate a couple of match statements and also introduces the possibility of APIs which can access regular files in uniform ways. Signed-off-by: Allison Karlitskaya <[email protected]>
1 parent c8a6328 commit 5f9d7e2

File tree

8 files changed

+35
-30
lines changed

8 files changed

+35
-30
lines changed

src/dumpfile.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use rustix::fs::FileType;
1313

1414
use crate::{
1515
fsverity::Sha256HashValue,
16-
image::{DirEnt, Directory, FileSystem, Inode, Leaf, LeafContent, Stat},
16+
image::{DirEnt, Directory, FileSystem, Inode, Leaf, LeafContent, RegularFile, Stat},
1717
};
1818

1919
fn write_empty(writer: &mut impl fmt::Write) -> fmt::Result {
@@ -109,7 +109,7 @@ pub fn write_leaf(
109109
nlink: usize,
110110
) -> fmt::Result {
111111
match content {
112-
LeafContent::InlineFile(ref data) => write_entry(
112+
LeafContent::Regular(RegularFile::Inline(ref data)) => write_entry(
113113
writer,
114114
path,
115115
stat,
@@ -121,7 +121,7 @@ pub fn write_leaf(
121121
data,
122122
None,
123123
),
124-
LeafContent::ExternalFile(id, size) => write_entry(
124+
LeafContent::Regular(RegularFile::External(id, size)) => write_entry(
125125
writer,
126126
path,
127127
stat,

src/erofs/writer.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -265,14 +265,14 @@ impl<'a> Directory<'a> {
265265
impl Leaf<'_> {
266266
fn inode_meta(&self) -> (format::DataLayout, u32, u64, usize) {
267267
let (layout, u, size) = match &self.content {
268-
image::LeafContent::InlineFile(data) => {
268+
image::LeafContent::Regular(image::RegularFile::Inline(data)) => {
269269
if data.is_empty() {
270270
(format::DataLayout::FlatPlain, 0, data.len() as u64)
271271
} else {
272272
(format::DataLayout::FlatInline, 0, data.len() as u64)
273273
}
274274
}
275-
image::LeafContent::ExternalFile(.., size) => {
275+
image::LeafContent::Regular(image::RegularFile::External(.., size)) => {
276276
(format::DataLayout::ChunkBased, 31, *size)
277277
}
278278
image::LeafContent::CharacterDevice(rdev) | image::LeafContent::BlockDevice(rdev) => {
@@ -290,8 +290,8 @@ impl Leaf<'_> {
290290

291291
fn write_inline(&self, output: &mut impl Output) {
292292
output.write(match self.content {
293-
image::LeafContent::InlineFile(data) => data,
294-
image::LeafContent::ExternalFile(..) => b"\xff\xff\xff\xff", // null chunk
293+
image::LeafContent::Regular(image::RegularFile::Inline(data)) => data,
294+
image::LeafContent::Regular(image::RegularFile::External(..)) => b"\xff\xff\xff\xff", // null chunk
295295
image::LeafContent::Symlink(target) => target.as_bytes(),
296296
_ => &[],
297297
});
@@ -303,9 +303,7 @@ impl Inode<'_> {
303303
match &self.content {
304304
InodeContent::Directory(..) => format::FileType::Directory,
305305
InodeContent::Leaf(leaf) => match &leaf.content {
306-
image::LeafContent::ExternalFile(..) | image::LeafContent::InlineFile(..) => {
307-
format::FileType::RegularFile
308-
}
306+
image::LeafContent::Regular(..) => format::FileType::RegularFile,
309307
image::LeafContent::CharacterDevice(..) => format::FileType::CharacterDevice,
310308
image::LeafContent::BlockDevice(..) => format::FileType::BlockDevice,
311309
image::LeafContent::Fifo => format::FileType::Fifo,
@@ -407,7 +405,7 @@ impl<'a> InodeCollector<'a> {
407405

408406
// We need to record extra xattrs for some files. These come first.
409407
if let InodeContent::Leaf(Leaf {
410-
content: image::LeafContent::ExternalFile(id, ..),
408+
content: image::LeafContent::Regular(image::RegularFile::External(id, ..)),
411409
..
412410
}) = content
413411
{

src/fs.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use zerocopy::IntoBytes;
2424

2525
use crate::{
2626
fsverity::{digest::FsVerityHasher, Sha256HashValue},
27-
image::{DirEnt, Directory, FileSystem, Inode, Leaf, LeafContent, Stat},
27+
image::{DirEnt, Directory, FileSystem, Inode, Leaf, LeafContent, RegularFile, Stat},
2828
repository::Repository,
2929
selabel::selabel,
3030
util::proc_self_fd,
@@ -88,8 +88,10 @@ fn write_leaf(leaf: &Leaf, dirfd: &OwnedFd, name: &OsStr, repo: &Repository) ->
8888
let mode = leaf.stat.st_mode.into();
8989

9090
match leaf.content {
91-
LeafContent::InlineFile(ref data) => set_file_contents(dirfd, name, &leaf.stat, data)?,
92-
LeafContent::ExternalFile(ref id, size) => {
91+
LeafContent::Regular(RegularFile::Inline(ref data)) => {
92+
set_file_contents(dirfd, name, &leaf.stat, data)?
93+
}
94+
LeafContent::Regular(RegularFile::External(ref id, size)) => {
9395
let object = repo.open_object(id)?;
9496
// TODO: make this better. At least needs to be EINTR-safe. Could even do reflink in some cases...
9597
let mut buffer = vec![MaybeUninit::uninit(); size as usize];
@@ -191,9 +193,9 @@ impl FilesystemReader<'_> {
191193
} else {
192194
FsVerityHasher::hash(data)
193195
};
194-
LeafContent::ExternalFile(id, buf.st_size as u64)
196+
LeafContent::Regular(RegularFile::External(id, buf.st_size as u64))
195197
} else {
196-
LeafContent::InlineFile(Vec::from(data))
198+
LeafContent::Regular(RegularFile::Inline(Vec::from(data)))
197199
}
198200
}
199201
FileType::Symlink => {

src/image.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,15 @@ pub struct Stat {
2020
pub xattrs: RefCell<BTreeMap<Box<OsStr>, Box<[u8]>>>,
2121
}
2222

23+
#[derive(Debug)]
24+
pub enum RegularFile {
25+
Inline(Vec<u8>),
26+
External(Sha256HashValue, u64),
27+
}
28+
2329
#[derive(Debug)]
2430
pub enum LeafContent {
25-
InlineFile(Vec<u8>),
26-
ExternalFile(Sha256HashValue, u64),
31+
Regular(RegularFile),
2732
BlockDevice(u64),
2833
CharacterDevice(u64),
2934
Fifo,

src/oci/image.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ pub fn create_image(
107107
}
108108

109109
#[cfg(test)]
110-
use crate::image::{LeafContent, Stat};
110+
use crate::image::{LeafContent, RegularFile, Stat};
111111
#[cfg(test)]
112112
use std::{cell::RefCell, collections::BTreeMap, io::BufRead, path::PathBuf};
113113

@@ -122,7 +122,7 @@ fn file_entry(path: &str) -> oci::tar::TarEntry {
122122
st_mtim_sec: 0,
123123
xattrs: RefCell::new(BTreeMap::new()),
124124
},
125-
item: oci::tar::TarItem::Leaf(LeafContent::InlineFile(vec![])),
125+
item: oci::tar::TarItem::Leaf(LeafContent::Regular(RegularFile::Inline(vec![]))),
126126
}
127127
}
128128

src/oci/tar.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use tokio::io::{AsyncRead, AsyncReadExt};
1515

1616
use crate::{
1717
dumpfile,
18-
image::{LeafContent, Stat},
18+
image::{LeafContent, RegularFile, Stat},
1919
splitstream::{SplitStreamData, SplitStreamReader, SplitStreamWriter},
2020
util::{read_exactish, read_exactish_async},
2121
INLINE_CONTENT_MAX,
@@ -181,7 +181,7 @@ pub fn get_entry<R: Read>(reader: &mut SplitStreamReader<R>) -> Result<Option<Ta
181181
size as usize > INLINE_CONTENT_MAX,
182182
"Splitstream incorrectly stored a small ({size} byte) file external"
183183
);
184-
TarItem::Leaf(LeafContent::ExternalFile(id, size))
184+
TarItem::Leaf(LeafContent::Regular(RegularFile::External(id, size)))
185185
}
186186
_ => bail!(
187187
"Unsupported external-chunked entry {:?} {}",
@@ -224,7 +224,7 @@ pub fn get_entry<R: Read>(reader: &mut SplitStreamReader<R>) -> Result<Option<Ta
224224
"Splitstream incorrectly stored a large ({} byte) file inline",
225225
content.len()
226226
);
227-
TarItem::Leaf(LeafContent::InlineFile(content))
227+
TarItem::Leaf(LeafContent::Regular(RegularFile::Inline(content)))
228228
}
229229
EntryType::Link => TarItem::Hardlink({
230230
let Some(link_name) = header.link_name_bytes() else {

src/selabel.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use anyhow::{bail, ensure, Context, Result};
1111
use regex_automata::{hybrid::dfa, util::syntax, Anchored, Input};
1212

1313
use crate::{
14-
image::{DirEnt, Directory, FileSystem, Inode, Leaf, LeafContent, Stat},
14+
image::{DirEnt, Directory, FileSystem, Inode, Leaf, LeafContent, RegularFile, Stat},
1515
repository::Repository,
1616
};
1717

@@ -116,8 +116,8 @@ pub fn openat<'a>(
116116

117117
match &dir.entries[idx].inode {
118118
Inode::Leaf(leaf) => match &leaf.content {
119-
LeafContent::InlineFile(data) => Ok(Some(Box::new(data.as_slice()))),
120-
LeafContent::ExternalFile(id, ..) => {
119+
LeafContent::Regular(RegularFile::Inline(data)) => Ok(Some(Box::new(data.as_slice()))),
120+
LeafContent::Regular(RegularFile::External(id, ..)) => {
121121
Ok(Some(Box::new(File::from(repo.open_object(id)?))))
122122
}
123123
_ => bail!("Invalid file type"),
@@ -211,7 +211,7 @@ fn relabel(stat: &Stat, path: &Path, ifmt: u8, policy: &mut Policy) {
211211

212212
fn relabel_leaf(leaf: &Leaf, path: &Path, policy: &mut Policy) {
213213
let ifmt = match leaf.content {
214-
LeafContent::InlineFile(..) | LeafContent::ExternalFile(..) => b'-',
214+
LeafContent::Regular(..) => b'-',
215215
LeafContent::Fifo => b'p', // NB: 'pipe', not 'fifo'
216216
LeafContent::Socket => b's',
217217
LeafContent::Symlink(..) => b'l',

tests/mkfs.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use tempfile::NamedTempFile;
1313
use composefs::{
1414
dumpfile::write_dumpfile,
1515
erofs::{debug::debug_img, writer::mkfs_erofs},
16-
image::{Directory, FileSystem, Inode, Leaf, LeafContent, Stat},
16+
image::{Directory, FileSystem, Inode, Leaf, LeafContent, RegularFile, Stat},
1717
};
1818

1919
fn debug_fs(mut fs: FileSystem) -> String {
@@ -54,12 +54,12 @@ fn simple(fs: &mut FileSystem) {
5454
add_leaf(
5555
&mut fs.root,
5656
"regular-inline",
57-
LeafContent::InlineFile(b"hihi".to_vec()),
57+
LeafContent::Regular(RegularFile::Inline(b"hihi".to_vec())),
5858
);
5959
add_leaf(
6060
&mut fs.root,
6161
"regular-external",
62-
LeafContent::ExternalFile([0x5a; 32], 1234),
62+
LeafContent::Regular(RegularFile::External([0x5a; 32], 1234)),
6363
);
6464
add_leaf(&mut fs.root, "chrdev", LeafContent::CharacterDevice(123));
6565
add_leaf(&mut fs.root, "blkdev", LeafContent::BlockDevice(123));

0 commit comments

Comments
 (0)