title: total size of a directory with nftw is different from du output
body: <p>I have written a little piece of code that use the nftw system call in order to do a tree walking. </p>
  
  <pre><code>int flags =0;
  flags = flags | FTW_MOUNT;
  int nopenfd = 10;
  
  if( nftw( argv[1], sum_sizes, nopenfd, flags) == -1 )
    return EXIT_FAILURE;
  </code></pre>
  
  <p>With this options, nftw doesn't scan directory if it's a mounted point and dereferences symbolic link (default behavior).</p>
  
  <p>On each file nftw call this function:</p>
  
  <pre><code>/*total_size is the sum of each file/directory/link*/
  long long int total_size, total_real_size = 0;
  
  static int sum_sizes(const char *pathname, const struct stat *statbuf, int typeflag, struct FTW *ftwbuf)
  {
  /*if stat failed on the current file*/
  if(typeflag == FTW_NS)
  {
      printf("No stats (permissions ?) on %s", pathname);
      return 0;
  }
  total_size = total_size + (long long int ) statbuf-&gt;st_size;
  total_real_size = total_real_size + (long long int ) ( statbuf-&gt;st_blocks * 512 );
  return 0;
  }
  </code></pre>
  
  <p>So At the end, I display the cumulative sizes:</p>
  
  <pre><code>printf("total size: %lld (%0.2lf K %0.2lf M)\n", total_size,  (float)total_size /1024.0, (float)total_size /(1024.0*1024.0));
  printf("total real size: %lld (%0.2lf K %0.2lf M)\n", total_real_size,  (float)total_real_size /1024.0, (float)total_real_size /(1024.0*1024.0));
  </code></pre>
  
  <p>When I compare the values with du I have some differences</p>
  
  <pre><code>time ./scan_dir ~/        
  ====&gt;
  total size: 15208413390 (14851966.00 K 14503.87 M)
  total real size: 15708553216 (15340384.00 K 14980.84 M)
  block size : 4096 / fond. block size : 4096
  fs size: 22.7895 G
  ./scan_dir ~/  0,03s user 0,24s system 98% cpu 0,277 total
  
  
  time du -s ~/     
  15119876    /home/cedlemo/
  du -s ~/  0,07s user 0,22s system 98% cpu 0,287 total
  </code></pre>
  
  <p>Note: after reading the man page of du I know that du have almost the same behavior than my little application scan_dir (skip mount points, derefrences symbolic link and use 1024 in order to calculate is value in Ko)</p>
  
  <p>It seems that the closer value found with my application is the total of real size (blocks used) but the value are not really the same.</p>
  
  <p>What could be the reason(s) of this difference ?
  What I am doing wrong?</p>