title: calculate disk usage percentage to match df Use% output
body: <p>I am trying to calculate and display in percentage the current disk usage with this:</p>
  
  <pre><code>use nix::sys::statvfs::statvfs;
  
  macro_rules! cast {
      ($x:expr) =&gt; {
          u64::from($x)
      };
  }
  
  fn main() {
      let stat = statvfs("/var/db".as_bytes()).unwrap();
      // f_frsize
      // let total_space = cast!(stat.fragment_size()) * cast!(stat.blocks());
      // let avail_space = cast!(stat.fragment_size()) * cast!(stat.blocks_available());
      // f_bsize
      let total_space = cast!(stat.block_size()) * cast!(stat.blocks());
      let avail_space = cast!(stat.block_size()) * cast!(stat.blocks_available());
      let used = total_space - avail_space;
      let usage = used * 100 / total_space;
      println!("total: {}", total_space);
      println!("avail: {}", avail_space);
      println!("used:  {}", used);
      println!("use%:  {}%", usage);
  }
  </code></pre>
  
  <p>The output on my current test system is:</p>
  
  <pre><code>total: 6779424768
  avail: 3697811456
  used:  3081613312
  use%:  45%
  </code></pre>
  
  <p>Output of <code>df -B 1 /var/db</code></p>
  
  <pre><code># df -B 1 /var/db
  Filesystem      1B-blocks       Used  Available Use% Mounted on
  /dev/loop3     6779424768 2713636864 3697811456  43% /var/db
  </code></pre>
  
  <p><code>Used</code> field not matching <code>2713636864</code> I am calculating it by doing <code>6779424768-3697811456 = 3081613312</code></p>
  
  <p>Output of <code>df -h /var/db</code>:</p>
  
  <pre><code># df -h /var/db
  Filesystem      Size  Used Avail Use% Mounted on
  /dev/loop3      6.4G  2.6G  3.5G  43% /var/db
  </code></pre>
  
  <p>I think I am missing something probably converting the units, I tried already using <code>f_frsize</code> instead of <code>f_bsize</code> and result still the same, the code above returns <code>45</code> and df <code>43</code>.</p>
  
  <p>Any ideas?</p>