@@ -21,7 +21,7 @@ use uucore::translate;
2121
2222use std:: ffi:: OsString ;
2323use std:: iter;
24- use std:: ops:: { Add , AddAssign } ;
24+ use std:: ops:: AddAssign ;
2525
2626/// A row in the filesystem usage data table.
2727///
@@ -41,13 +41,13 @@ pub(crate) struct Row {
4141 fs_mount : OsString ,
4242
4343 /// Total number of bytes in the filesystem regardless of whether they are used.
44- bytes : BytesCell ,
44+ bytes : u64 ,
4545
4646 /// Number of used bytes.
47- bytes_used : BytesCell ,
47+ bytes_used : u64 ,
4848
4949 /// Number of available bytes.
50- bytes_avail : BytesCell ,
50+ bytes_avail : u64 ,
5151
5252 /// Percentage of bytes that are used, given as a float between 0 and 1.
5353 ///
@@ -84,9 +84,9 @@ impl Row {
8484 fs_device : source. into ( ) ,
8585 fs_type : "-" . into ( ) ,
8686 fs_mount : "-" . into ( ) ,
87- bytes : BytesCell :: default ( ) ,
88- bytes_used : BytesCell :: default ( ) ,
89- bytes_avail : BytesCell :: default ( ) ,
87+ bytes : 0 ,
88+ bytes_used : 0 ,
89+ bytes_avail : 0 ,
9090 bytes_usage : None ,
9191 #[ cfg( target_vendor = "apple" ) ]
9292 bytes_capacity : None ,
@@ -117,13 +117,13 @@ impl AddAssign for Row {
117117 bytes,
118118 bytes_used,
119119 bytes_avail,
120- bytes_usage : if bytes. bytes == 0 {
120+ bytes_usage : if bytes == 0 {
121121 None
122122 } else {
123123 // We use "(bytes_used + bytes_avail)" instead of "bytes" because on some filesystems (e.g.
124124 // ext4) "bytes" also includes reserved blocks we ignore for the usage calculation.
125125 // https://www.gnu.org/software/coreutils/faq/coreutils-faq.html#df-Size-and-Used-and-Available-do-not-add-up
126- Some ( bytes_used. bytes as f64 / ( bytes_used. bytes + bytes_avail. bytes ) as f64 )
126+ Some ( bytes_used as f64 / ( bytes_used + bytes_avail) as f64 )
127127 } ,
128128 // TODO Figure out how to compute this.
129129 #[ cfg( target_vendor = "apple" ) ]
@@ -141,7 +141,7 @@ impl AddAssign for Row {
141141}
142142
143143impl Row {
144- fn from_filesystem ( fs : Filesystem , row_block_size : & BlockSize ) -> Self {
144+ fn from_filesystem ( fs : Filesystem ) -> Self {
145145 let MountInfo {
146146 dev_name,
147147 fs_type,
@@ -166,9 +166,9 @@ impl Row {
166166 fs_device : dev_name,
167167 fs_type,
168168 fs_mount : mount_dir,
169- bytes : BytesCell :: new ( blocks * blocksize, row_block_size ) ,
170- bytes_used : BytesCell :: new ( bused * blocksize, row_block_size ) ,
171- bytes_avail : BytesCell :: new ( bavail * blocksize, row_block_size ) ,
169+ bytes : blocks * blocksize,
170+ bytes_used : bused * blocksize,
171+ bytes_avail : bavail * blocksize,
172172 bytes_usage : if blocks == 0 {
173173 None
174174 } else {
@@ -195,48 +195,6 @@ impl Row {
195195 }
196196}
197197
198- #[ derive( Debug , Copy , Clone ) ]
199- struct BytesCell {
200- bytes : u64 ,
201- scaled : u64 ,
202- }
203-
204- /// A bytes column in the filesystem usage data table.
205- ///
206- /// This is used to keep track of the scaled values to properly compute
207- /// the total values.
208- impl Default for BytesCell {
209- fn default ( ) -> Self {
210- Self {
211- bytes : 0 ,
212- scaled : 0 ,
213- }
214- }
215- }
216-
217- impl BytesCell {
218- fn new ( bytes : u64 , block_size : & BlockSize ) -> Self {
219- Self {
220- bytes,
221- scaled : {
222- let BlockSize :: Bytes ( d) = block_size;
223- ( bytes as f64 / * d as f64 ) . ceil ( ) as u64
224- } ,
225- }
226- }
227- }
228-
229- impl Add for BytesCell {
230- type Output = Self ;
231-
232- fn add ( self , rhs : Self ) -> Self {
233- Self {
234- bytes : self . bytes + rhs. bytes ,
235- scaled : self . scaled + rhs. scaled ,
236- }
237- }
238- }
239-
240198/// A `Cell` in the table. We store raw `bytes` as the data (e.g. directory name
241199/// may be non-Unicode). We also record the printed `width` for alignment purpose,
242200/// as it is easier to compute on the original string.
@@ -307,18 +265,12 @@ impl<'a> RowFormatter<'a> {
307265 /// Get a string giving the scaled version of the input number.
308266 ///
309267 /// The scaling factor is defined in the `options` field.
310- fn scaled_bytes ( & self , bytes_column : & BytesCell ) -> Cell {
311- let size = bytes_column. scaled ;
268+ fn scaled_bytes ( & self , bytes : u64 ) -> Cell {
312269 let s = if let Some ( h) = self . options . human_readable {
313- let size = if self . is_total_row {
314- let BlockSize :: Bytes ( d) = self . options . block_size ;
315- d * size
316- } else {
317- bytes_column. bytes
318- } ;
319- to_magnitude_and_suffix ( size. into ( ) , SuffixType :: HumanReadable ( h) , true )
270+ to_magnitude_and_suffix ( bytes. into ( ) , SuffixType :: HumanReadable ( h) , true )
320271 } else {
321- size. to_string ( )
272+ let BlockSize :: Bytes ( d) = self . options . block_size ;
273+ bytes. div_ceil ( d) . to_string ( )
322274 } ;
323275 Cell :: from_ascii_string ( s)
324276 }
@@ -359,9 +311,9 @@ impl<'a> RowFormatter<'a> {
359311 Cell :: from_string ( & self . row . fs_device )
360312 }
361313 }
362- Column :: Size => self . scaled_bytes ( & self . row . bytes ) ,
363- Column :: Used => self . scaled_bytes ( & self . row . bytes_used ) ,
364- Column :: Avail => self . scaled_bytes ( & self . row . bytes_avail ) ,
314+ Column :: Size => self . scaled_bytes ( self . row . bytes ) ,
315+ Column :: Used => self . scaled_bytes ( self . row . bytes_used ) ,
316+ Column :: Avail => self . scaled_bytes ( self . row . bytes_avail ) ,
365317 Column :: Pcent => Self :: percentage ( self . row . bytes_usage ) ,
366318
367319 Column :: Target => {
@@ -488,7 +440,7 @@ impl Table {
488440 // showing all filesystems, then print the data as a row in
489441 // the output table.
490442 if options. show_all_fs || filesystem. usage . blocks > 0 {
491- let row = Row :: from_filesystem ( filesystem, & options . block_size ) ;
443+ let row = Row :: from_filesystem ( filesystem) ;
492444 let fmt = RowFormatter :: new ( & row, options, false ) ;
493445 let values = fmt. get_cells ( ) ;
494446 if options. show_total {
@@ -576,7 +528,7 @@ mod tests {
576528
577529 use crate :: blocks:: HumanReadable ;
578530 use crate :: columns:: Column ;
579- use crate :: table:: { BytesCell , Cell , Header , HeaderMode , Row , RowFormatter , Table } ;
531+ use crate :: table:: { Cell , Header , HeaderMode , Row , RowFormatter , Table } ;
580532 use crate :: { BlockSize , Options } ;
581533
582534 fn init ( ) {
@@ -611,9 +563,9 @@ mod tests {
611563 fs_type : "my_type" . to_string ( ) ,
612564 fs_mount : "my_mount" . into ( ) ,
613565
614- bytes : BytesCell :: new ( 100 , & BlockSize :: Bytes ( 1 ) ) ,
615- bytes_used : BytesCell :: new ( 25 , & BlockSize :: Bytes ( 1 ) ) ,
616- bytes_avail : BytesCell :: new ( 75 , & BlockSize :: Bytes ( 1 ) ) ,
566+ bytes : 100 ,
567+ bytes_used : 25 ,
568+ bytes_avail : 75 ,
617569 bytes_usage : Some ( 0.25 ) ,
618570
619571 #[ cfg( target_vendor = "apple" ) ]
@@ -777,9 +729,9 @@ mod tests {
777729 fs_device : "my_device" . to_string ( ) ,
778730 fs_mount : "my_mount" . into ( ) ,
779731
780- bytes : BytesCell :: new ( 100 , & BlockSize :: Bytes ( 1 ) ) ,
781- bytes_used : BytesCell :: new ( 25 , & BlockSize :: Bytes ( 1 ) ) ,
782- bytes_avail : BytesCell :: new ( 75 , & BlockSize :: Bytes ( 1 ) ) ,
732+ bytes : 100 ,
733+ bytes_used : 25 ,
734+ bytes_avail : 75 ,
783735 bytes_usage : Some ( 0.25 ) ,
784736
785737 ..Default :: default ( )
@@ -804,9 +756,9 @@ mod tests {
804756 fs_type : "my_type" . to_string ( ) ,
805757 fs_mount : "my_mount" . into ( ) ,
806758
807- bytes : BytesCell :: new ( 100 , & BlockSize :: Bytes ( 1 ) ) ,
808- bytes_used : BytesCell :: new ( 25 , & BlockSize :: Bytes ( 1 ) ) ,
809- bytes_avail : BytesCell :: new ( 75 , & BlockSize :: Bytes ( 1 ) ) ,
759+ bytes : 100 ,
760+ bytes_used : 25 ,
761+ bytes_avail : 75 ,
810762 bytes_usage : Some ( 0.25 ) ,
811763
812764 ..Default :: default ( )
@@ -853,7 +805,7 @@ mod tests {
853805 ..Default :: default ( )
854806 } ;
855807 let row = Row {
856- bytes : BytesCell :: new ( 100 , & BlockSize :: Bytes ( 100 ) ) ,
808+ bytes : 100 ,
857809 inodes : 10 ,
858810 ..Default :: default ( )
859811 } ;
@@ -874,9 +826,9 @@ mod tests {
874826 fs_type : "my_type" . to_string ( ) ,
875827 fs_mount : "my_mount" . into ( ) ,
876828
877- bytes : BytesCell :: new ( 40000 , & BlockSize :: default ( ) ) ,
878- bytes_used : BytesCell :: new ( 1000 , & BlockSize :: default ( ) ) ,
879- bytes_avail : BytesCell :: new ( 39000 , & BlockSize :: default ( ) ) ,
829+ bytes : 40000 ,
830+ bytes_used : 1000 ,
831+ bytes_avail : 39000 ,
880832 bytes_usage : Some ( 0.025 ) ,
881833
882834 ..Default :: default ( )
@@ -909,9 +861,9 @@ mod tests {
909861 fs_type : "my_type" . to_string ( ) ,
910862 fs_mount : "my_mount" . into ( ) ,
911863
912- bytes : BytesCell :: new ( 4096 , & BlockSize :: default ( ) ) ,
913- bytes_used : BytesCell :: new ( 1024 , & BlockSize :: default ( ) ) ,
914- bytes_avail : BytesCell :: new ( 3072 , & BlockSize :: default ( ) ) ,
864+ bytes : 4096 ,
865+ bytes_used : 1024 ,
866+ bytes_avail : 3072 ,
915867 bytes_usage : Some ( 0.25 ) ,
916868
917869 ..Default :: default ( )
@@ -956,9 +908,9 @@ mod tests {
956908 } ;
957909
958910 let row = Row {
959- bytes : BytesCell :: new ( bytes , & BlockSize :: Bytes ( 1000 ) ) ,
960- bytes_used : BytesCell :: new ( bytes_used , & BlockSize :: Bytes ( 1000 ) ) ,
961- bytes_avail : BytesCell :: new ( bytes_avail , & BlockSize :: Bytes ( 1000 ) ) ,
911+ bytes,
912+ bytes_used,
913+ bytes_avail,
962914 ..Default :: default ( )
963915 } ;
964916 RowFormatter :: new ( & row, & options, false ) . get_cells ( )
@@ -984,6 +936,78 @@ mod tests {
984936 ) ) ;
985937 }
986938
939+ fn filesystem_with_bytes ( bytes : u64 ) -> crate :: Filesystem {
940+ crate :: Filesystem {
941+ file : None ,
942+ mount_info : crate :: MountInfo {
943+ dev_id : "28" . to_string ( ) ,
944+ dev_name : "none" . to_string ( ) ,
945+ fs_type : "9p" . to_string ( ) ,
946+ mount_dir : "/mnt" . into ( ) ,
947+ mount_option : "rw" . to_string ( ) ,
948+ mount_root : "/" . into ( ) ,
949+ remote : false ,
950+ dummy : false ,
951+ } ,
952+ usage : crate :: table:: FsUsage {
953+ blocksize : 1 ,
954+ blocks : bytes,
955+ bfree : 0 ,
956+ bavail : 0 ,
957+ bavail_top_bit_set : false ,
958+ files : 1 ,
959+ ffree : 0 ,
960+ } ,
961+ }
962+ }
963+
964+ fn last_line ( table : & Table ) -> Vec < String > {
965+ let mut data: Vec < u8 > = vec ! [ ] ;
966+ table. write_to ( & mut data) . expect ( "Write error." ) ;
967+ let output = String :: from_utf8_lossy ( & data) ;
968+ output
969+ . lines ( )
970+ . last ( )
971+ . unwrap ( )
972+ . split_whitespace ( )
973+ . map ( str:: to_string)
974+ . collect ( )
975+ }
976+
977+ #[ test]
978+ fn test_total_row_rounds_the_summed_bytes_once ( ) {
979+ init ( ) ;
980+ // Two filesystems of 1500 bytes are 2 blocks each with 1000-byte
981+ // blocks, but together they hold 3000 bytes, which is 3 blocks, not
982+ // the sum of the rounded-up row values.
983+ let options = Options {
984+ show_total : true ,
985+ block_size : BlockSize :: Bytes ( 1000 ) ,
986+ columns : vec ! [ Column :: Source , Column :: Size , Column :: Used , Column :: Avail ] ,
987+ ..Default :: default ( )
988+ } ;
989+ let filesystems = vec ! [ filesystem_with_bytes( 1500 ) , filesystem_with_bytes( 1500 ) ] ;
990+ let table = Table :: new ( & options, filesystems) ;
991+ assert_eq ! ( last_line( & table) , vec![ "total" , "3" , "3" , "0" ] ) ;
992+ }
993+
994+ #[ test]
995+ fn test_total_row_human_readable_with_large_block_size ( ) {
996+ init ( ) ;
997+ // The block size does not take part in human-readable output, so a
998+ // block size close to `u64::MAX` must not overflow the total row.
999+ let options = Options {
1000+ show_total : true ,
1001+ human_readable : Some ( HumanReadable :: Binary ) ,
1002+ block_size : BlockSize :: Bytes ( 10_000_000_000_000_000_000 ) ,
1003+ columns : vec ! [ Column :: Source , Column :: Size , Column :: Used , Column :: Avail ] ,
1004+ ..Default :: default ( )
1005+ } ;
1006+ let filesystems = vec ! [ filesystem_with_bytes( 1500 ) , filesystem_with_bytes( 1500 ) ] ;
1007+ let table = Table :: new ( & options, filesystems) ;
1008+ assert_eq ! ( last_line( & table) , vec![ "total" , "3.0K" , "3.0K" , "0" ] ) ;
1009+ }
1010+
9871011 #[ test]
9881012 fn test_row_converter_with_invalid_numbers ( ) {
9891013 init ( ) ;
@@ -1011,7 +1035,7 @@ mod tests {
10111035 } ,
10121036 } ;
10131037
1014- let row = Row :: from_filesystem ( d, & BlockSize :: default ( ) ) ;
1038+ let row = Row :: from_filesystem ( d) ;
10151039
10161040 assert_eq ! ( row. inodes_used, 0 ) ;
10171041 }
0 commit comments