Skip to content

Commit 6e2fce3

Browse files
authored
Include entry for the root in the timeline output.
1 parent b7c6978 commit 6e2fce3

File tree

1 file changed

+49
-28
lines changed

1 file changed

+49
-28
lines changed

crates/rrg/src/action/get_filesystem_timeline.rs

Lines changed: 49 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,21 @@ where
4040
// when we process batches.
4141
let entry_count = std::cell::Cell::new(0);
4242

43+
// `walk_dir` does not include entry for the root directory, which is useful
44+
// in the the timeline output. Thus, we grab it explicitly and chain it with
45+
// the rest of the entries.
46+
let root_metadata = std::fs::metadata(&args.root)
47+
.map_err(crate::session::Error::action)?;
48+
4349
let entries = crate::fs::walk_dir(&args.root)
44-
.map_err(crate::session::Error::action)?
50+
.map_err(crate::session::Error::action)?;
51+
52+
let entries = std::iter::once(Ok(crate::fs::Entry {
53+
path: args.root,
54+
metadata: root_metadata,
55+
})).chain(entries);
56+
57+
let entries = entries
4558
.filter_map(|entry| match entry {
4659
Ok(entry) => Some(entry),
4760
Err(error) => {
@@ -188,7 +201,8 @@ mod tests {
188201
assert!(handle(&mut session, request).is_ok());
189202

190203
let entries = entries(&session);
191-
assert_eq!(entries.len(), 0);
204+
assert_eq!(entries.len(), 1);
205+
assert_eq!(path(&entries[0]), Some(tempdir_path));
192206
}
193207

194208
#[test]
@@ -208,10 +222,11 @@ mod tests {
208222
let mut entries = entries(&session);
209223
entries.sort_by_key(|entry| entry.path().to_owned());
210224

211-
assert_eq!(entries.len(), 3);
212-
assert_eq!(path(&entries[0]), Some(tempdir.path().join("a")));
213-
assert_eq!(path(&entries[1]), Some(tempdir.path().join("b")));
214-
assert_eq!(path(&entries[2]), Some(tempdir.path().join("c")));
225+
assert_eq!(entries.len(), 4);
226+
assert_eq!(path(&entries[0]), Some(tempdir.path().to_path_buf()));
227+
assert_eq!(path(&entries[1]), Some(tempdir.path().join("a")));
228+
assert_eq!(path(&entries[2]), Some(tempdir.path().join("b")));
229+
assert_eq!(path(&entries[3]), Some(tempdir.path().join("c")));
215230
}
216231

217232
#[test]
@@ -231,9 +246,10 @@ mod tests {
231246
let mut entries = entries(&session);
232247
entries.sort_by_key(|entry| entry.path().to_owned());
233248

234-
assert_eq!(entries.len(), 2);
235-
assert_eq!(path(&entries[0]), Some(tempdir_path.join("a")));
236-
assert_eq!(path(&entries[1]), Some(tempdir_path.join("a").join("b")));
249+
assert_eq!(entries.len(), 3);
250+
assert_eq!(path(&entries[0]), Some(tempdir_path.to_path_buf()));
251+
assert_eq!(path(&entries[1]), Some(tempdir_path.join("a")));
252+
assert_eq!(path(&entries[2]), Some(tempdir_path.join("a").join("b")));
237253
}
238254

239255
// Symlinking is supported only on Unix-like systems.
@@ -259,9 +275,10 @@ mod tests {
259275
let mut entries = entries(&session);
260276
entries.sort_by_key(|entry| entry.path().to_owned());
261277

262-
assert_eq!(entries.len(), 2);
263-
assert_eq!(path(&entries[0]), Some(dir_path));
264-
assert_eq!(path(&entries[1]), Some(symlink_path));
278+
assert_eq!(entries.len(), 3);
279+
assert_eq!(path(&entries[0]), Some(root_path));
280+
assert_eq!(path(&entries[1]), Some(dir_path));
281+
assert_eq!(path(&entries[2]), Some(symlink_path));
265282
}
266283

267284
#[test]
@@ -285,13 +302,14 @@ mod tests {
285302
let mut entries = entries(&session);
286303
entries.sort_by_key(|entry| entry.path().to_owned());
287304

288-
assert_eq!(entries.len(), 2);
305+
assert_eq!(entries.len(), 3);
306+
assert_eq!(path(&entries[0]), Some(root_path));
289307

290308
// macOS mangles Unicode-specific characters in filenames.
291309
#[cfg(not(target_os = "macos"))]
292310
{
293-
assert_eq!(path(&entries[0]), Some(file_path_1));
294-
assert_eq!(path(&entries[1]), Some(file_path_2));
311+
assert_eq!(path(&entries[1]), Some(file_path_1));
312+
assert_eq!(path(&entries[2]), Some(file_path_2));
295313
}
296314
}
297315

@@ -310,22 +328,23 @@ mod tests {
310328
let mut entries = entries(&session);
311329
entries.sort_by_key(|entry| entry.path().to_owned());
312330

313-
assert_eq!(entries.len(), 1);
314-
assert_eq!(path(&entries[0]), Some(tempdir.path().join("foo")));
315-
assert_eq!(entries[0].size(), 9);
331+
assert_eq!(entries.len(), 2);
332+
assert_eq!(path(&entries[0]), Some(tempdir.path().to_path_buf()));
333+
assert_eq!(path(&entries[1]), Some(tempdir.path().join("foo")));
334+
assert_eq!(entries[1].size(), 9);
316335

317336
// Information about the file mode, user and group identifiers is
318337
// available only on UNIX systems.
319338
#[cfg(target_family = "unix")]
320339
{
321-
let mode = entries[0].unix_mode() as libc::mode_t;
340+
let mode = entries[1].unix_mode() as libc::mode_t;
322341
assert_eq!(mode & libc::S_IFMT, libc::S_IFREG);
323342

324343
let uid = unsafe { libc::getuid() };
325-
assert_eq!(entries[0].unix_uid(), uid.into());
344+
assert_eq!(entries[1].unix_uid(), uid.into());
326345

327346
let gid = unsafe { libc::getgid() };
328-
assert_eq!(entries[0].unix_gid(), gid.into());
347+
assert_eq!(entries[1].unix_gid(), gid.into());
329348
}
330349
}
331350

@@ -350,13 +369,14 @@ mod tests {
350369
let mut entries = entries(&session);
351370
entries.sort_by_key(|entry| entry.path().to_owned());
352371

353-
assert_eq!(entries.len(), 2);
354-
assert_eq!(path(&entries[0]), Some(file_path));
355-
assert_eq!(path(&entries[1]), Some(hardlink_path));
372+
assert_eq!(entries.len(), 3);
373+
assert_eq!(path(&entries[0]), Some(root_path));
374+
assert_eq!(path(&entries[1]), Some(file_path));
375+
assert_eq!(path(&entries[2]), Some(hardlink_path));
356376

357377
// Information about inode is not available on Windows.
358378
#[cfg(not(target_os = "windows"))]
359-
assert_eq!(entries[0].unix_ino(), entries[1].unix_ino());
379+
assert_eq!(entries[1].unix_ino(), entries[2].unix_ino());
360380
}
361381

362382
#[test]
@@ -394,10 +414,11 @@ mod tests {
394414
assert!(handle(&mut session, request).is_ok());
395415

396416
let entries = entries(&session);
397-
assert_eq!(entries.len(), 1);
398-
assert_eq!(path(&entries[0]), Some(temp_path));
417+
assert_eq!(entries.len(), 2);
418+
assert_eq!(path(&entries[0]), Some(temp_dir.path().to_path_buf()));
419+
assert_eq!(path(&entries[1]), Some(temp_path));
399420

400-
let attributes = entries[0].windows_attributes() as u32;
421+
let attributes = entries[1].windows_attributes() as u32;
401422
assert_eq!(attributes & FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_HIDDEN);
402423
}
403424

0 commit comments

Comments
 (0)