Skip to content

Commit

Permalink
jwalk 0.5 has landed - now we don't follow symlinks during traversal!
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Mar 15, 2020
1 parent 5b696d4 commit 0d6116e
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 43 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ license = "MIT"
failure = "0.1.1"
failure-tools = "4.0.2"
structopt = "0.3"
jwalk = "0.4.0"
jwalk = "0.5.0"
byte-unit = "3"
termion = "1.5.2"
atty = "0.2.11"
Expand Down
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,6 @@ Thanks to [jwalk][jwalk], all there was left to do is to write a command-line in
### Limitations

* Interactive mode only looks good in dark terminals (see [this issue](https://github.com/Byron/dua-cli/issues/13))
* _Symlinks_ are followed and we obtain the logical size of the file they point to. Ideally, we only
count their actual size.
* _easy fix_: file names in main window are not truncated if too large. They are cut off on the right.
* There are plenty of examples in `tests/fixtures` which don't render correctly in interactive mode.
This can be due to graphemes not interpreted correctly. With Chinese characters for instance,
Expand Down
19 changes: 10 additions & 9 deletions src/aggregate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,27 @@ pub fn aggregate(
stats.entries_traversed += 1;
match entry {
Ok(entry) => {
let file_size = match entry.metadata {
Some(Ok(ref m)) if !m.is_dir() && (options.count_hard_links || inodes.add(m)) => {
let file_size = match entry.client_state {
Some(Ok(ref m))
if !m.is_dir() && (options.count_hard_links || inodes.add(m)) =>
{
if options.apparent_size {
m.len()
} else {
filesize::file_real_size_fast(&entry.path(), m)
.unwrap_or_else(|_| {
filesize::file_real_size_fast(&entry.path(), m).unwrap_or_else(
|_| {
num_errors += 1;
0
})
},
)
}
},
}
Some(Ok(_)) => 0,
Some(Err(_)) => {
num_errors += 1;
0
}
None => unreachable!(
"we ask for metadata, so we at least have Some(Err(..))). Issue in jwalk?"
),
None => unreachable!("must have populated client state for metadata"),
};
stats.largest_file_in_bytes = stats.largest_file_in_bytes.max(file_size);
stats.smallest_file_in_bytes = stats.smallest_file_in_bytes.min(file_size);
Expand Down
18 changes: 15 additions & 3 deletions src/common.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::traverse::{EntryData, Tree, TreeIndex};
use byte_unit::{n_gb_bytes, n_gib_bytes, n_mb_bytes, n_mib_bytes, ByteUnit};
use jwalk::WalkDir;
use std::{fmt, path::Path};

pub fn get_entry_or_panic(tree: &Tree, node_idx: TreeIndex) -> &EntryData {
Expand Down Expand Up @@ -158,16 +157,29 @@ pub struct WalkOptions {
pub sorting: TraversalSorting,
}

type WalkDir = jwalk::WalkDirGeneric<((), Option<Result<std::fs::Metadata, jwalk::Error>>)>;

impl WalkOptions {
pub(crate) fn iter_from_path(&self, path: &Path) -> WalkDir {
WalkDir::new(path)
.preload_metadata(true)
.follow_links(false)
.sort(match self.sorting {
TraversalSorting::None => false,
TraversalSorting::AlphabeticalByFileName => true,
})
.skip_hidden(false)
.num_threads(self.threads)
.process_read_dir(|_, dir_entry_results| {
dir_entry_results.iter_mut().for_each(|dir_entry_result| {
if let Ok(dir_entry) = dir_entry_result {
dir_entry.client_state = Some(dir_entry.metadata());
}
})
})
.parallelism(if self.threads == 0 {
jwalk::Parallelism::RayonDefaultPool
} else {
jwalk::Parallelism::RayonNewPool(self.threads)
})
}
}

Expand Down
14 changes: 10 additions & 4 deletions src/interactive/app_test/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,11 @@ fn delete_recursive(path: impl AsRef<Path>) -> Result<(), Error> {
let mut files: Vec<_> = Vec::new();
let mut dirs: Vec<_> = Vec::new();

for entry in WalkDir::new(&path).num_threads(1).into_iter() {
let entry: DirEntry = entry?;
for entry in WalkDir::new(&path)
.parallelism(jwalk::Parallelism::Serial)
.into_iter()
{
let entry: DirEntry<_> = entry?;
let p = entry.path();
match p.is_dir() {
true => dirs.push(p),
Expand All @@ -99,8 +102,11 @@ fn delete_recursive(path: impl AsRef<Path>) -> Result<(), Error> {
}

fn copy_recursive(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> Result<(), Error> {
for entry in WalkDir::new(&src).num_threads(1).into_iter() {
let entry: DirEntry = entry?;
for entry in WalkDir::new(&src)
.parallelism(jwalk::Parallelism::Serial)
.into_iter()
{
let entry: DirEntry<_> = entry?;
let entry_path = entry.path();
entry_path
.strip_prefix(&src)
Expand Down
46 changes: 24 additions & 22 deletions src/traverse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,29 +93,31 @@ impl Traversal {
} else {
entry.file_name
};
let file_size = match entry.metadata {
Some(Ok(ref m)) if !m.is_dir() && (walk_options.count_hard_links || inodes.add(m)) => {
if walk_options.apparent_size {
m.len()
} else {
filesize::file_real_size_fast(&data.name, m)
.unwrap_or_else(|_| {
t.io_errors += 1;
data.metadata_io_error = true;
0
})
}
},
Some(Ok(_)) => 0,
Some(Err(_)) => {
t.io_errors += 1;
data.metadata_io_error = true;
0
let file_size = match entry.client_state {
Some(Ok(ref m))
if !m.is_dir()
&& (walk_options.count_hard_links || inodes.add(m)) =>
{
if walk_options.apparent_size {
m.len()
} else {
filesize::file_real_size_fast(&data.name, m).unwrap_or_else(
|_| {
t.io_errors += 1;
data.metadata_io_error = true;
0
},
)
}
None => unreachable!(
"we ask for metadata, so we at least have Some(Err(..))). Issue in jwalk?"
),
};
}
Some(Ok(_)) => 0,
Some(Err(_)) => {
t.io_errors += 1;
data.metadata_io_error = true;
0
}
None => unreachable!("must have populated client state for metadata"),
};

match (entry.depth, previous_depth) {
(n, p) if n > p => {
Expand Down

0 comments on commit 0d6116e

Please sign in to comment.