title: How can GetFreeDiskSpaceEx return the (seemingly) wrong amount of disk space?
body: <p>So I work on a device that outputs large images (anywhere from 30MB to 2GB+).  Before we begin creating one of these images we check to see if there is sufficient disk space via <code>GetDiskFreeSpaceEx</code>.  Typically (and in this case) we are writing to a shared folder on the same network.  There are no user quotas on disk space at play.</p>
  <p>Last night, in preparation for a demo, we kicked off a test run.  During the run we experienced a failure.  We needed <code>327391776</code> bytes and were told that we only had <code>186580992</code> available.  The numbers from <code>GetDiskFreeSpaceEx</code> were:</p>
  <blockquote>
  <p>User free space available: 186580992</p>
  <p>Total free space available: 186580992</p>
  </blockquote>
  <p>Those correspond to the <code>QuadPart</code> variables in the two (output) arguments <code>lpFreeBytesAvailable</code> and <code>lpTotalNumberOfFreeBytes</code> to <code>GetDiskFreeSpaceAvailable</code>.</p>
  <p>This code has been in use for years now and I have never seen a false negative.  Here is the complete function:</p>
  <pre><code>long IsDiskSpaceAvailable( const char* inDirectory, 
                             const _int64&amp; inRequestedSize, 
                             _int64&amp; outUserFree, 
                             _int64&amp; outTotalFree, 
                             _int64&amp; outCalcRequest  )
  {
      ULARGE_INTEGER  fba;
      ULARGE_INTEGER  tnb;
      ULARGE_INTEGER  tnfba;
      ULARGE_INTEGER  reqsize;
      string          dir;
      size_t          len;
  
      dir = inDirectory;
      len = strlen( inDirectory );
  
      outUserFree     = 0;
      outTotalFree    = 0;
      outCalcRequest  = 0;
  
      if( inDirectory[len-1] != '\\' )
          dir += &quot;\\&quot;;
      
      // this is the value of inRequestSize that was passed in
      // inRequestedSize = 3273917760;
      if( GetDiskFreeSpaceEx( dir.c_str(), &amp;fba, &amp;tnb, &amp;tnfba ) )
      {
          outUserFree         = fba.QuadPart;
          outTotalFree        = tnfba.QuadPart;
  
          // this is computed dynamically given a specific compression
          // type, but for simplicity I had hard-coded the value that was used
          float compressionRatio = 10.0;
          reqsize.QuadPart = (ULONGLONG) (inRequestedSize / compressionRatio);
          outCalcRequest   = reqsize.QuadPart;
  
          // this is what was triggered to cause the failure,
          // i.e., user free space was &lt; the request size
          if( fba.QuadPart &lt; reqsize.QuadPart )
              return( RetCode_OutOfSpace );
      }
      else
      {
          return( RetCode_Failure );
      }
      
      return( RetCode_OK );
  }
  </code></pre>
  <p>So, a value of <code>3273917760</code> was passed to the function which is the total amount of disk space needed before compression.  The function divides this by the compression ratio of <code>10</code> to get the actual size needed.</p>
  <p>When I checked the disk that the share resides on it had ~177GB free, far more than what was reported.  After starting the test again without changing anything it worked.</p>
  <p>So my question here is; what could cause something like this?  As far as I can tell it is not a programming error and, as I mentioned earlier, this code has been in use for a very long time now with no problems.</p>
  <p>I checked the event log of the remote machine and found nothing of interest around the time of the failure.  I'm hoping that someone out there has seen something similar before, thanks in advance.</p>