Skip to content

Commit f76d01e

Browse files
allisonkarlitskayacgwalters
authored andcommitted
image: add Directory::new() helper
We hardcode the implementation of Directory in too many places. Start cleaning that up by introducing a proper constructor. Signed-off-by: Allison Karlitskaya <[email protected]>
1 parent 835431b commit f76d01e

File tree

2 files changed

+17
-19
lines changed

2 files changed

+17
-19
lines changed

src/fs.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -250,10 +250,7 @@ impl FilesystemReader<'_> {
250250
)?;
251251

252252
let (_, stat) = FilesystemReader::stat(&fd, FileType::Directory)?;
253-
let mut directory = Directory {
254-
stat,
255-
entries: vec![],
256-
};
253+
let mut directory = Directory::new(stat);
257254

258255
for item in Dir::read_from(&fd)? {
259256
let entry = item?;

src/image.rs

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,13 @@ impl Inode {
7070
}
7171

7272
impl Directory {
73+
pub fn new(stat: Stat) -> Self {
74+
Self {
75+
stat,
76+
entries: vec![],
77+
}
78+
}
79+
7380
pub fn find_entry(&self, name: &OsStr) -> Result<usize, usize> {
7481
// OCI layer tarballs are typically sorted, with the entries for a particular directory
7582
// written out immediately after that directory was created. That means that it's very
@@ -116,10 +123,7 @@ impl Directory {
116123
idx,
117124
DirEnt {
118125
name: OsString::from(name),
119-
inode: Inode::Directory(Box::new(Directory {
120-
stat,
121-
entries: vec![],
122-
})),
126+
inode: Inode::Directory(Box::new(Directory::new(stat))),
123127
},
124128
);
125129
}
@@ -196,17 +200,14 @@ impl Default for FileSystem {
196200

197201
impl FileSystem {
198202
pub fn new() -> Self {
199-
FileSystem {
200-
root: Directory {
201-
stat: Stat {
202-
st_mode: u32::MAX, // assigned later
203-
st_uid: u32::MAX, // assigned later
204-
st_gid: u32::MAX, // assigned later
205-
st_mtim_sec: -1, // assigned later
206-
xattrs: RefCell::new(BTreeMap::new()),
207-
},
208-
entries: vec![],
209-
},
203+
Self {
204+
root: Directory::new(Stat {
205+
st_mode: u32::MAX, // assigned later
206+
st_uid: u32::MAX, // assigned later
207+
st_gid: u32::MAX, // assigned later
208+
st_mtim_sec: -1, // assigned later
209+
xattrs: RefCell::new(BTreeMap::new()),
210+
}),
210211
}
211212
}
212213

0 commit comments

Comments
 (0)