Skip to content

Commit 8985afa

Browse files
committed
du: Simplify Stat creation further
No need to have separate struct creation code for Windows and Unix. Also remove is_dir, we can get it from metadata.
1 parent 51dbe09 commit 8985afa

File tree

1 file changed

+29
-39
lines changed

1 file changed

+29
-39
lines changed

src/uu/du/src/du.rs

Lines changed: 29 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,6 @@ struct FileInfo {
113113

114114
struct Stat {
115115
path: PathBuf,
116-
is_dir: bool,
117116
size: u64,
118117
blocks: u64,
119118
inodes: u64,
@@ -145,44 +144,27 @@ impl Stat {
145144
fs::symlink_metadata(path)
146145
}?;
147146

148-
#[cfg(not(windows))]
149-
{
150-
let file_info = FileInfo {
151-
file_id: metadata.ino() as u128,
152-
dev_id: metadata.dev(),
153-
};
154-
155-
Ok(Self {
156-
path: path.to_path_buf(),
157-
is_dir: metadata.is_dir(),
158-
size: if metadata.is_dir() { 0 } else { metadata.len() },
159-
blocks: metadata.blocks(),
160-
inodes: 1,
161-
inode: Some(file_info),
162-
metadata,
163-
})
164-
}
165-
166-
#[cfg(windows)]
167-
{
168-
let size_on_disk = get_size_on_disk(path);
169-
let file_info = get_file_info(path);
170-
171-
Ok(Self {
172-
path: path.to_path_buf(),
173-
is_dir: metadata.is_dir(),
174-
size: if metadata.is_dir() { 0 } else { metadata.len() },
175-
blocks: size_on_disk / 1024 * 2,
176-
inodes: 1,
177-
inode: file_info,
178-
metadata,
179-
})
180-
}
147+
let file_info = get_file_info(path, &metadata);
148+
let blocks = get_blocks(path, &metadata);
149+
150+
Ok(Self {
151+
path: path.to_path_buf(),
152+
size: if metadata.is_dir() { 0 } else { metadata.len() },
153+
blocks,
154+
inodes: 1,
155+
inode: file_info,
156+
metadata,
157+
})
181158
}
182159
}
183160

161+
#[cfg(not(windows))]
162+
fn get_blocks(_path: &Path, metadata: &Metadata) -> u64 {
163+
metadata.blocks()
164+
}
165+
184166
#[cfg(windows)]
185-
fn get_size_on_disk(path: &Path) -> u64 {
167+
fn get_blocks(path: &Path, _metadata: &Metadata) -> u64 {
186168
let mut size_on_disk = 0;
187169

188170
// bind file so it stays in scope until end of function
@@ -207,11 +189,19 @@ fn get_size_on_disk(path: &Path) -> u64 {
207189
}
208190
}
209191

210-
size_on_disk
192+
size_on_disk / 1024 * 2
193+
}
194+
195+
#[cfg(not(windows))]
196+
fn get_file_info(_path: &Path, metadata: &Metadata) -> Option<FileInfo> {
197+
Some(FileInfo {
198+
file_id: metadata.ino() as u128,
199+
dev_id: metadata.dev(),
200+
})
211201
}
212202

213203
#[cfg(windows)]
214-
fn get_file_info(path: &Path) -> Option<FileInfo> {
204+
fn get_file_info(path: &Path, _metadata: &Metadata) -> Option<FileInfo> {
215205
let mut result = None;
216206

217207
let Ok(file) = File::open(path) else {
@@ -269,7 +259,7 @@ fn du(
269259
seen_inodes: &mut HashSet<FileInfo>,
270260
print_tx: &mpsc::Sender<UResult<StatPrintInfo>>,
271261
) -> Result<Stat, Box<mpsc::SendError<UResult<StatPrintInfo>>>> {
272-
if my_stat.is_dir {
262+
if my_stat.metadata.is_dir() {
273263
let read = match fs::read_dir(&my_stat.path) {
274264
Ok(read) => read,
275265
Err(e) => {
@@ -330,7 +320,7 @@ fn du(
330320
seen_inodes.insert(inode);
331321
}
332322

333-
if this_stat.is_dir {
323+
if this_stat.metadata.is_dir() {
334324
if options.one_file_system {
335325
if let (Some(this_inode), Some(my_inode)) =
336326
(this_stat.inode, my_stat.inode)

0 commit comments

Comments
 (0)