Skip to content

Commit 5b9930c

Browse files
Carlos Fernandezmeta-codesync[bot]
authored andcommitted
antlir2_btrfs: Include path in NotBtrfs error message for debugging
Summary: The message "not a btrfs filesystem" without any additional info (at least, which filesystem it's talking about) is quite confusing. Let's add that info. Test Plan: Tested by building a CAF image and got that error (also, left my devserver in a bad state). Reviewed By: vmagro Differential Revision: D87096919 fbshipit-source-id: 62da2d84b4126becba09dfc5088eed5de6727c22
1 parent 5090cba commit 5b9930c

File tree

1 file changed

+16
-22
lines changed
  • antlir/antlir2/antlir2_btrfs/src

1 file changed

+16
-22
lines changed

antlir/antlir2/antlir2_btrfs/src/lib.rs

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ mod ioctl;
3636

3737
#[derive(Debug, Error)]
3838
pub enum Error {
39-
#[error("not a btrfs filesystem")]
40-
NotBtrfs,
39+
#[error("not a btrfs filesystem: {0}")]
40+
NotBtrfs(PathBuf),
4141
#[error("directory is not a btrfs subvolume")]
4242
NotSubvol,
4343
#[error("cannot delete root subvolume")]
@@ -79,10 +79,10 @@ fn name_bytes<const L: usize>(name: &OsStr) -> [u8; L] {
7979
buf
8080
}
8181

82-
fn ensure_is_btrfs(fd: &impl AsFd) -> Result<()> {
82+
fn ensure_is_btrfs(fd: &impl AsFd, path: impl AsRef<Path>) -> Result<()> {
8383
let statfs = fstatfs(fd).map_err(std::io::Error::from)?;
8484
if statfs.filesystem_type() != BTRFS_SUPER_MAGIC {
85-
return Err(Error::NotBtrfs);
85+
return Err(Error::NotBtrfs(path.as_ref().to_path_buf()));
8686
}
8787
Ok(())
8888
}
@@ -107,7 +107,7 @@ impl Subvolume {
107107
let fd = Dir::open(path.as_ref(), OFlag::O_DIRECTORY, Mode::empty())
108108
.map_err(std::io::Error::from)?;
109109

110-
ensure_is_btrfs(&fd)?;
110+
ensure_is_btrfs(&fd, path.as_ref())?;
111111

112112
let stat = fstat(&fd).map_err(std::io::Error::from)?;
113113
if stat.st_ino != INO_SUBVOL {
@@ -136,14 +136,11 @@ impl Subvolume {
136136
}
137137

138138
pub fn create(path: impl AsRef<Path>) -> Result<Self> {
139-
let parent_fd = Dir::open(
140-
path.as_ref().parent().ok_or(Error::CannotCreateRoot)?,
141-
OFlag::O_DIRECTORY,
142-
Mode::empty(),
143-
)
144-
.map_err(std::io::Error::from)?;
139+
let parent_path = path.as_ref().parent().ok_or(Error::CannotCreateRoot)?;
140+
let parent_fd = Dir::open(parent_path, OFlag::O_DIRECTORY, Mode::empty())
141+
.map_err(std::io::Error::from)?;
145142

146-
ensure_is_btrfs(&parent_fd)?;
143+
ensure_is_btrfs(&parent_fd, parent_path)?;
147144

148145
let args = ioctl::vol_args_v2 {
149146
id: ioctl::vol_args_v2_spec {
@@ -165,14 +162,11 @@ impl Subvolume {
165162
}
166163

167164
pub fn snapshot(&self, path: impl AsRef<Path>, flags: SnapshotFlags) -> Result<Self> {
168-
let new_parent_fd = Dir::open(
169-
path.as_ref().parent().ok_or(Error::CannotCreateRoot)?,
170-
OFlag::O_DIRECTORY,
171-
Mode::empty(),
172-
)
173-
.map_err(std::io::Error::from)?;
165+
let new_parent_path = path.as_ref().parent().ok_or(Error::CannotCreateRoot)?;
166+
let new_parent_fd = Dir::open(new_parent_path, OFlag::O_DIRECTORY, Mode::empty())
167+
.map_err(std::io::Error::from)?;
174168

175-
ensure_is_btrfs(&new_parent_fd)?;
169+
ensure_is_btrfs(&new_parent_fd, new_parent_path)?;
176170

177171
let args = ioctl::vol_args_v2 {
178172
id: ioctl::vol_args_v2_spec {
@@ -314,8 +308,8 @@ impl Debug for Info {
314308
}
315309

316310
pub fn ensure_path_is_on_btrfs(path: impl AsRef<Path>) -> Result<()> {
317-
let fd = OpenOptions::new().read(true).open(path)?;
318-
ensure_is_btrfs(&fd)
311+
let fd = OpenOptions::new().read(true).open(&path)?;
312+
ensure_is_btrfs(&fd, path)
319313
}
320314

321315
#[cfg(test)]
@@ -348,7 +342,7 @@ mod tests {
348342
"expected error on subvol lookup for regular directory"
349343
);
350344
assert!(
351-
matches!(Subvolume::open("/tmp"), Err(Error::NotBtrfs)),
345+
matches!(Subvolume::open("/tmp"), Err(Error::NotBtrfs(_))),
352346
"expected error on subvol lookup for non-btrfs"
353347
);
354348
Ok(())

0 commit comments

Comments
 (0)