uu_df 0.7.0

df ~ (uutils) display file system information
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
// This file is part of the uutils coreutils package.
//
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.
//! Types for representing and displaying block sizes.
use crate::{OPT_BLOCKSIZE, OPT_PORTABILITY};
use clap::ArgMatches;
use std::{env, fmt};

use uucore::{
    display::Quotable,
    parser::parse_size::{ParseSizeError, parse_size_non_zero_u64, parse_size_u64},
};

/// The first ten powers of 1024.
const IEC_BASES: [u128; 10] = [
    1,
    1_024,
    1_048_576,
    1_073_741_824,
    1_099_511_627_776,
    1_125_899_906_842_624,
    1_152_921_504_606_846_976,
    1_180_591_620_717_411_303_424,
    1_208_925_819_614_629_174_706_176,
    1_237_940_039_285_380_274_899_124_224,
];

/// The first ten powers of 1000.
const SI_BASES: [u128; 10] = [
    1,
    1_000,
    1_000_000,
    1_000_000_000,
    1_000_000_000_000,
    1_000_000_000_000_000,
    1_000_000_000_000_000_000,
    1_000_000_000_000_000_000_000,
    1_000_000_000_000_000_000_000_000,
    1_000_000_000_000_000_000_000_000_000,
];

/// A `SuffixType` determines whether the suffixes are 1000 or 1024 based, and whether they are
/// intended for `HumanReadable` mode or not.
#[derive(Clone, Copy)]
pub(crate) enum SuffixType {
    Iec,
    Si,
    HumanReadable(HumanReadable),
}

impl SuffixType {
    /// The first ten powers of 1024 and 1000, respectively.
    fn bases(self) -> [u128; 10] {
        match self {
            Self::Iec | Self::HumanReadable(HumanReadable::Binary) => IEC_BASES,
            Self::Si | Self::HumanReadable(HumanReadable::Decimal) => SI_BASES,
        }
    }

    /// Suffixes for the first nine multi-byte unit suffixes.
    fn suffixes(self) -> [&'static str; 9] {
        match self {
            // we use "kB" instead of "KB", same as GNU df
            Self::Si => ["B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"],
            Self::Iec => ["B", "K", "M", "G", "T", "P", "E", "Z", "Y"],
            Self::HumanReadable(HumanReadable::Binary) => {
                ["", "K", "M", "G", "T", "P", "E", "Z", "Y"]
            }
            Self::HumanReadable(HumanReadable::Decimal) => {
                ["", "k", "M", "G", "T", "P", "E", "Z", "Y"]
            }
        }
    }
}

/// Convert a number into a magnitude and a multi-byte unit suffix.
///
/// The returned string has a maximum length of 5 chars, for example: "1.1kB", "999kB", "1MB".
/// `add_tracing_zero` allows to add tracing zero for values in 0 < x <= 9
///
pub(crate) fn to_magnitude_and_suffix(
    n: u128,
    suffix_type: SuffixType,
    add_tracing_zero: bool,
) -> String {
    let bases = suffix_type.bases();
    let suffixes = suffix_type.suffixes();
    let mut i = 0;

    while bases[i + 1] - bases[i] < n && i < suffixes.len() {
        i += 1;
    }

    let quot = n / bases[i];
    let rem = n % bases[i];
    let suffix = suffixes[i];

    if rem == 0 {
        if add_tracing_zero && !suffix.is_empty() && quot != 0 && quot <= 9 {
            format!("{quot}.0{suffix}")
        } else {
            format!("{quot}{suffix}")
        }
    } else {
        let tenths_place = rem / (bases[i] / 10);

        if quot >= 100 && rem > 0 {
            format!("{}{suffix}", quot + 1)
        } else if rem.is_multiple_of(bases[i] / 10) {
            format!("{quot}.{tenths_place}{suffix}")
        } else if tenths_place + 1 == 10 || quot >= 10 {
            let quot = quot + 1;
            if add_tracing_zero && !suffix.is_empty() && quot <= 9 {
                format!("{quot}.0{suffix}")
            } else {
                format!("{quot}{suffix}")
            }
        } else {
            format!("{quot}.{}{suffix}", tenths_place + 1)
        }
    }
}

/// A mode to use in condensing the human readable display of a large number
/// of bytes.
///
/// The [`HumanReadable::Decimal`] and[`HumanReadable::Binary`] variants
/// represent dynamic block sizes: as the number of bytes increases, the
/// divisor increases as well (for example, from 1 to 1,000 to 1,000,000
/// and so on in the case of [`HumanReadable::Decimal`]).
#[derive(Clone, Copy)]
pub(crate) enum HumanReadable {
    /// Use the largest divisor corresponding to a unit, like B, K, M, G, etc.
    ///
    /// This variant represents powers of 1,000. Contrast with
    /// [`HumanReadable::Binary`], which represents powers of
    /// 1,024.
    Decimal,

    /// Use the largest divisor corresponding to a unit, like B, K, M, G, etc.
    ///
    /// This variant represents powers of 1,024. Contrast with
    /// [`HumanReadable::Decimal`], which represents powers
    /// of 1,000.
    Binary,
}

/// A block size to use in condensing the display of a large number of bytes.
///
/// The [`BlockSize::Bytes`] variant represents a static block
/// size.
///
/// The default variant is `Bytes(1024)`.
#[derive(Debug, PartialEq)]
pub(crate) enum BlockSize {
    /// A fixed number of bytes.
    ///
    /// The number must be positive.
    Bytes(u64),
}

impl BlockSize {
    /// Returns the associated value
    pub(crate) fn as_u64(&self) -> u64 {
        match *self {
            Self::Bytes(n) => n,
        }
    }

    pub(crate) fn to_header(&self) -> String {
        match self {
            Self::Bytes(n) => {
                if n % 1024 == 0 && n % 1000 != 0 {
                    to_magnitude_and_suffix(*n as u128, SuffixType::Iec, false)
                } else {
                    to_magnitude_and_suffix(*n as u128, SuffixType::Si, false)
                }
            }
        }
    }
}

impl Default for BlockSize {
    fn default() -> Self {
        if env::var("POSIXLY_CORRECT").is_ok() {
            Self::Bytes(512)
        } else {
            Self::Bytes(1024)
        }
    }
}

pub(crate) fn read_block_size(matches: &ArgMatches) -> Result<BlockSize, ParseSizeError> {
    if matches.contains_id(OPT_BLOCKSIZE) {
        let s = matches.get_one::<String>(OPT_BLOCKSIZE).unwrap();
        let bytes = parse_size_u64(s)?;

        if bytes > 0 {
            Ok(BlockSize::Bytes(bytes))
        } else {
            Err(ParseSizeError::ParseFailure(format!("{}", s.quote())))
        }
    } else if matches.get_flag(OPT_PORTABILITY) {
        Ok(BlockSize::default())
    } else if let Some(bytes) = block_size_from_env() {
        Ok(BlockSize::Bytes(bytes))
    } else {
        Ok(BlockSize::default())
    }
}

fn block_size_from_env() -> Option<u64> {
    for env_var in ["DF_BLOCK_SIZE", "BLOCK_SIZE", "BLOCKSIZE"] {
        if let Ok(env_size) = env::var(env_var) {
            return parse_size_non_zero_u64(&env_size).ok();
        }
    }

    None
}

impl fmt::Display for BlockSize {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Self::Bytes(n) => {
                let s = if n % 1024 == 0 && n % 1000 != 0 {
                    to_magnitude_and_suffix(*n as u128, SuffixType::Iec, true)
                } else {
                    to_magnitude_and_suffix(*n as u128, SuffixType::Si, true)
                };

                write!(f, "{s}")
            }
        }
    }
}

#[cfg(test)]
mod tests {

    use std::env;

    use crate::blocks::{BlockSize, SuffixType, to_magnitude_and_suffix};

    #[test]
    fn test_to_magnitude_and_suffix_rounding() {
        assert_eq!(
            to_magnitude_and_suffix(999_440, SuffixType::Si, true),
            "1.0MB"
        );
        assert_eq!(
            to_magnitude_and_suffix(819_200, SuffixType::Si, true),
            "820kB"
        );
        assert_eq!(
            to_magnitude_and_suffix(819_936, SuffixType::Si, true),
            "820kB"
        );
        assert_eq!(
            to_magnitude_and_suffix(818_400, SuffixType::Si, true),
            "819kB"
        );
        assert_eq!(
            to_magnitude_and_suffix(817_600, SuffixType::Si, true),
            "818kB"
        );
        assert_eq!(
            to_magnitude_and_suffix(817_200, SuffixType::Si, true),
            "818kB"
        );
    }

    #[test]
    fn test_to_magnitude_and_suffix_add_tracing_zero() {
        assert_eq!(to_magnitude_and_suffix(1024, SuffixType::Iec, true), "1.0K");
        assert_eq!(to_magnitude_and_suffix(2048, SuffixType::Iec, true), "2.0K");
        assert_eq!(to_magnitude_and_suffix(10240, SuffixType::Iec, true), "10K");

        assert_eq!(to_magnitude_and_suffix(1024, SuffixType::Iec, false), "1K");
        assert_eq!(to_magnitude_and_suffix(2048, SuffixType::Iec, false), "2K");
        assert_eq!(
            to_magnitude_and_suffix(10240, SuffixType::Iec, false),
            "10K"
        );
    }

    #[test]
    fn test_to_magnitude_and_suffix_powers_of_1024() {
        assert_eq!(to_magnitude_and_suffix(1024, SuffixType::Iec, false), "1K");
        assert_eq!(
            to_magnitude_and_suffix(10240, SuffixType::Iec, false),
            "10K"
        );
        assert_eq!(to_magnitude_and_suffix(2048, SuffixType::Iec, false), "2K");
        assert_eq!(
            to_magnitude_and_suffix(1024 * 40, SuffixType::Iec, false),
            "40K"
        );
        assert_eq!(
            to_magnitude_and_suffix(1024 * 1024, SuffixType::Iec, false),
            "1M"
        );
        assert_eq!(
            to_magnitude_and_suffix(2 * 1024 * 1024, SuffixType::Iec, false),
            "2M"
        );
        assert_eq!(
            to_magnitude_and_suffix(1024 * 1024 * 1024, SuffixType::Iec, false),
            "1G"
        );
        assert_eq!(
            to_magnitude_and_suffix(34 * 1024 * 1024 * 1024, SuffixType::Iec, false),
            "34G"
        );
    }

    #[test]
    #[allow(clippy::cognitive_complexity)]
    fn test_to_magnitude_and_suffix_not_powers_of_1024() {
        assert_eq!(to_magnitude_and_suffix(1, SuffixType::Si, true), "1.0B");
        assert_eq!(to_magnitude_and_suffix(999, SuffixType::Si, true), "999B");

        assert_eq!(to_magnitude_and_suffix(1000, SuffixType::Si, true), "1.0kB");
        assert_eq!(to_magnitude_and_suffix(1001, SuffixType::Si, true), "1.1kB");
        assert_eq!(to_magnitude_and_suffix(1023, SuffixType::Si, true), "1.1kB");
        assert_eq!(to_magnitude_and_suffix(1025, SuffixType::Si, true), "1.1kB");
        assert_eq!(
            to_magnitude_and_suffix(10_001, SuffixType::Si, true),
            "11kB"
        );
        assert_eq!(
            to_magnitude_and_suffix(999_000, SuffixType::Si, true),
            "999kB"
        );

        assert_eq!(
            to_magnitude_and_suffix(999_001, SuffixType::Si, true),
            "1.0MB"
        );
        assert_eq!(
            to_magnitude_and_suffix(999_999, SuffixType::Si, true),
            "1.0MB"
        );
        assert_eq!(
            to_magnitude_and_suffix(1_000_000, SuffixType::Si, true),
            "1.0MB"
        );
        assert_eq!(
            to_magnitude_and_suffix(1_000_001, SuffixType::Si, true),
            "1.1MB"
        );
        assert_eq!(
            to_magnitude_and_suffix(1_100_000, SuffixType::Si, true),
            "1.1MB"
        );
        assert_eq!(
            to_magnitude_and_suffix(1_100_001, SuffixType::Si, true),
            "1.2MB"
        );
        assert_eq!(
            to_magnitude_and_suffix(1_900_000, SuffixType::Si, true),
            "1.9MB"
        );
        assert_eq!(
            to_magnitude_and_suffix(1_900_001, SuffixType::Si, true),
            "2.0MB"
        );
        assert_eq!(
            to_magnitude_and_suffix(9_900_000, SuffixType::Si, true),
            "9.9MB"
        );
        assert_eq!(
            to_magnitude_and_suffix(9_900_001, SuffixType::Si, true),
            "10MB"
        );
        assert_eq!(
            to_magnitude_and_suffix(999_000_000, SuffixType::Si, true),
            "999MB"
        );

        assert_eq!(
            to_magnitude_and_suffix(999_000_001, SuffixType::Si, true),
            "1.0GB"
        );
        assert_eq!(
            to_magnitude_and_suffix(1_000_000_000, SuffixType::Si, true),
            "1.0GB"
        );
        assert_eq!(
            to_magnitude_and_suffix(1_000_000_001, SuffixType::Si, true),
            "1.1GB"
        );
    }

    #[test]
    fn test_block_size_display() {
        assert_eq!(format!("{}", BlockSize::Bytes(1024)), "1.0K");
        assert_eq!(format!("{}", BlockSize::Bytes(2 * 1024)), "2.0K");
        assert_eq!(format!("{}", BlockSize::Bytes(3 * 1024 * 1024)), "3.0M");
    }

    #[test]
    fn test_block_size_display_multiples_of_1000_and_1024() {
        assert_eq!(format!("{}", BlockSize::Bytes(128_000)), "128kB");
        assert_eq!(format!("{}", BlockSize::Bytes(1000 * 1024)), "1.1MB");
        assert_eq!(format!("{}", BlockSize::Bytes(1_000_000_000_000)), "1.0TB");
    }

    #[test]
    fn test_default_block_size() {
        assert_eq!(BlockSize::Bytes(1024), BlockSize::default());
        unsafe { env::set_var("POSIXLY_CORRECT", "1") };
        assert_eq!(BlockSize::Bytes(512), BlockSize::default());
        unsafe { env::remove_var("POSIXLY_CORRECT") };
    }
}