-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
ls: round block counts up when scaling to a block size #14330
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Socialpranker
wants to merge
1
commit into
uutils:main
Choose a base branch
from
Socialpranker:ls-block-size-round-up
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+81
−15
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1342,15 +1342,13 @@ fn write_directory_entries<O: LsOutput>( | |
| output: &mut O, | ||
| ) -> UResult<()> { | ||
| if config.format == Format::Long || config.alloc_size { | ||
| let total_size: u64 = entries | ||
| // GNU adds up the allocated bytes and scales the sum once, so a | ||
| // partly used block is rounded up for the total, not once per file. | ||
| let total_bytes: u64 = entries | ||
| .iter() | ||
| .map(|item| { | ||
| item.metadata() | ||
| .as_ref() | ||
| .map_or(0, |md| get_block_size(md, config)) | ||
| }) | ||
| .map(|item| item.metadata().as_ref().map_or(0, |md| get_block_bytes(md))) | ||
| .sum(); | ||
| output.write_total(total_size, config)?; | ||
| output.write_total(scale_block_bytes(total_bytes, config), config)?; | ||
| } | ||
|
|
||
| if matches!(output.stream_mode(), StreamMode::Streaming) { | ||
|
|
@@ -1564,23 +1562,22 @@ fn get_metadata_with_deref_opt(p_buf: &Path, dereference: bool) -> std::io::Resu | |
| } | ||
| } | ||
|
|
||
| /// Allocated size of a file in bytes, before it is scaled to the block size. | ||
| /// | ||
| /// This is the figure the `total` line sums: GNU scales the sum once, so | ||
| /// scaling every file first and adding the results would round several times | ||
| /// over and overshoot the total. | ||
| #[allow(unused_variables)] | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| fn get_block_size(md: &Metadata, config: &Config) -> u64 { | ||
| fn get_block_bytes(md: &Metadata) -> u64 { | ||
| /* GNU ls will display sizes in terms of block size | ||
| md.len() will differ from this value when the file has some holes | ||
| */ | ||
| #[cfg(unix)] | ||
| { | ||
| use uucore::format::human::SizeFormat; | ||
|
|
||
| let raw_blocks = if md.file_type().is_char_device() || md.file_type().is_block_device() { | ||
| if md.file_type().is_char_device() || md.file_type().is_block_device() { | ||
| 0u64 | ||
| } else { | ||
| md.blocks() * 512 | ||
| }; | ||
| match config.size_format { | ||
| SizeFormat::Binary | SizeFormat::Decimal => raw_blocks, | ||
| SizeFormat::Bytes => raw_blocks / config.block_size, | ||
| } | ||
| } | ||
| #[cfg(not(unix))] | ||
|
|
@@ -1590,6 +1587,35 @@ fn get_block_size(md: &Metadata, config: &Config) -> u64 { | |
| } | ||
| } | ||
|
|
||
| /// Scale an allocated size in bytes to `config.block_size`. | ||
| /// | ||
| /// A partly used block still occupies a whole block, so the division rounds | ||
| /// up: with `--block-size=1000`, 4096 allocated bytes are five blocks, not | ||
| /// four. `-h`/`--si` keep the byte count, which the human-readable formatter | ||
| /// scales itself. | ||
| #[allow(unused_variables)] | ||
| fn scale_block_bytes(bytes: u64, config: &Config) -> u64 { | ||
| #[cfg(unix)] | ||
| { | ||
| use uucore::format::human::SizeFormat; | ||
|
|
||
| match config.size_format { | ||
| SizeFormat::Binary | SizeFormat::Decimal => bytes, | ||
| SizeFormat::Bytes => bytes.div_ceil(config.block_size), | ||
| } | ||
| } | ||
| // On non-unix `get_block_bytes` falls back to the file size rather than an | ||
| // allocation figure, which was never scaled here. Left as it was. | ||
| #[cfg(not(unix))] | ||
| { | ||
| bytes | ||
| } | ||
| } | ||
|
|
||
| fn get_block_size(md: &Metadata, config: &Config) -> u64 { | ||
| scale_block_bytes(get_block_bytes(md), config) | ||
| } | ||
|
|
||
| #[cfg(unix)] | ||
| fn file_is_executable(md: &Metadata) -> bool { | ||
| // Mode always returns u32, but the flags might not be, based on the platform | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the "sum then scale once" explanation is in three places (here, the call site and the test), please keep it in one place only