Skip to content

Commit

Permalink
fix: don't add an extra trailing byte when creating the disk sparse file
Browse files Browse the repository at this point in the history
  • Loading branch information
lleyton committed Jun 2, 2024
1 parent a0a5170 commit a9c9a60
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/util.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use color_eyre::Result;
use std::path::Path;
use std::{fs::File, path::Path};
use tracing::{debug, error};

#[macro_export]
Expand Down Expand Up @@ -341,11 +341,12 @@ pub fn run_with_chroot<T>(root: &Path, f: impl FnOnce() -> Result<T>) -> Result<
}

/// Create an empty sparse file with given size
pub fn create_sparse(path: &Path, pos: u64) -> Result<std::fs::File> {
use std::io::{Seek, Write};
debug!(?path, pos, "Creating sparse file");
let mut f = std::fs::File::create(path)?;
f.seek(std::io::SeekFrom::Start(pos))?;
pub fn create_sparse(path: &Path, size: u64) -> Result<File> {
use std::io::{Seek, SeekFrom, Write};
debug!(?path, size, "Creating sparse file");
let mut f = File::create(path)?;
// We seek to size - 1, since we write a null byte at the end
f.seek(SeekFrom::Start(size - 1))?;
f.write_all(&[0])?;
Ok(f)
}
Expand Down Expand Up @@ -374,6 +375,6 @@ pub fn just_write(path: impl AsRef<Path>, content: impl AsRef<str>) -> Result<()
tracing::trace!(?path, content, "Writing content to file");
crate::bail_let!(Some(parent) = path.parent() => "Invalid file path");
let _ = std::fs::create_dir_all(parent);
std::fs::File::create(path)?.write_all(content.as_bytes())?;
File::create(path)?.write_all(content.as_bytes())?;
Ok(())
}

0 comments on commit a9c9a60

Please sign in to comment.