Skip to content

Commit 29dca61

Browse files
cgwaltersjeckersb
authored andcommitted
fs: Move file reading helper from composefs-boot
Some bits in bootc will end up using this for the same reason composefs-boot internals are. Signed-off-by: Colin Walters <[email protected]>
1 parent 706181c commit 29dca61

File tree

3 files changed

+29
-27
lines changed

3 files changed

+29
-27
lines changed

crates/composefs-boot/src/bootloader.rs

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
use core::ops::Range;
2-
use std::{
3-
collections::HashMap, ffi::OsStr, fs::File, io::Read, os::unix::ffi::OsStrExt, str::from_utf8,
4-
};
2+
use std::{collections::HashMap, ffi::OsStr, os::unix::ffi::OsStrExt, str::from_utf8};
53

6-
use anyhow::{bail, ensure, Result};
4+
use anyhow::{bail, Result};
75

86
use composefs::{
97
fsverity::FsVerityHashValue,
@@ -112,24 +110,6 @@ impl BootLoaderEntryFile {
112110
}
113111
}
114112

115-
pub fn read_file<ObjectID: FsVerityHashValue>(
116-
file: &RegularFile<ObjectID>,
117-
repo: &Repository<ObjectID>,
118-
) -> Result<Box<[u8]>> {
119-
match file {
120-
RegularFile::Inline(data) => Ok(data.clone()),
121-
RegularFile::External(id, size) => {
122-
let mut data = vec![];
123-
File::from(repo.open_object(id)?).read_to_end(&mut data)?;
124-
ensure!(
125-
*size == data.len() as u64,
126-
"File content doesn't have the expected length"
127-
);
128-
Ok(data.into_boxed_slice())
129-
}
130-
}
131-
}
132-
133113
#[derive(Debug)]
134114
pub struct Type1Entry<ObjectID: FsVerityHashValue> {
135115
/// This is the basename of the bootloader entry .conf file
@@ -181,7 +161,7 @@ impl<ObjectID: FsVerityHashValue> Type1Entry<ObjectID> {
181161
root: &Directory<ObjectID>,
182162
repo: &Repository<ObjectID>,
183163
) -> Result<Self> {
184-
let entry = BootLoaderEntryFile::new(from_utf8(&read_file(file, repo)?)?);
164+
let entry = BootLoaderEntryFile::new(from_utf8(&composefs::fs::read_file(file, repo)?)?);
185165

186166
let mut files = HashMap::new();
187167
for key in ["linux", "initrd", "efi"] {

crates/composefs-boot/src/write_boot.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use anyhow::{ensure, Result};
88
use composefs::{fsverity::FsVerityHashValue, repository::Repository};
99

1010
use crate::{
11-
bootloader::{read_file, BootEntry, Type1Entry, Type2Entry},
11+
bootloader::{BootEntry, Type1Entry, Type2Entry},
1212
cmdline::get_cmdline_composefs,
1313
uki,
1414
};
@@ -38,7 +38,7 @@ pub fn write_t1_simple<ObjectID: FsVerityHashValue>(
3838
let file_path = bootdir.join(pathname.strip_prefix(Path::new("/"))?);
3939
// SAFETY: what safety? :)
4040
create_dir_all(file_path.parent().unwrap())?;
41-
write(file_path, read_file(file, repo)?)?;
41+
write(file_path, composefs::fs::read_file(file, repo)?)?;
4242
}
4343

4444
// And now the loader entry itself
@@ -59,7 +59,7 @@ pub fn write_t2_simple<ObjectID: FsVerityHashValue>(
5959
let efi_linux = bootdir.join("EFI/Linux");
6060
create_dir_all(&efi_linux)?;
6161
let filename = efi_linux.join(t2.filename.as_ref());
62-
let content = read_file(&t2.file, repo)?;
62+
let content = composefs::fs::read_file(&t2.file, repo)?;
6363
let (composefs, _) = get_cmdline_composefs::<ObjectID>(uki::get_cmdline(&content)?)?;
6464

6565
ensure!(

crates/composefs/src/fs.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::{
33
collections::{BTreeMap, HashMap},
44
ffi::{CStr, OsStr},
55
fs::File,
6-
io::Write,
6+
io::{Read, Write},
77
mem::MaybeUninit,
88
os::unix::ffi::OsStrExt,
99
path::Path,
@@ -313,6 +313,9 @@ impl<ObjectID: FsVerityHashValue> FilesystemReader<'_, ObjectID> {
313313
}
314314
}
315315

316+
/// Load a filesystem tree from the given path. A repository may
317+
/// be provided; if it is, then all files found in the filesystem
318+
/// are copied in.
316319
pub fn read_filesystem<ObjectID: FsVerityHashValue>(
317320
dirfd: impl AsFd,
318321
path: &Path,
@@ -332,6 +335,25 @@ pub fn read_filesystem<ObjectID: FsVerityHashValue>(
332335
})
333336
}
334337

338+
/// Read the contents of a file.
339+
pub fn read_file<ObjectID: FsVerityHashValue>(
340+
file: &RegularFile<ObjectID>,
341+
repo: &Repository<ObjectID>,
342+
) -> Result<Box<[u8]>> {
343+
match file {
344+
RegularFile::Inline(data) => Ok(data.clone()),
345+
RegularFile::External(id, size) => {
346+
let mut data = Vec::with_capacity(*size as usize);
347+
std::fs::File::from(repo.open_object(id)?).read_to_end(&mut data)?;
348+
ensure!(
349+
*size == data.len() as u64,
350+
"File content doesn't have the expected length"
351+
);
352+
Ok(data.into_boxed_slice())
353+
}
354+
}
355+
}
356+
335357
#[cfg(test)]
336358
mod tests {
337359
use super::*;

0 commit comments

Comments
 (0)