Novell Home

Get free disk space on a volume with GetVolumeStatistics()

From Developer Community

This code shows how to obtain the amount of available space on a volume with GetVolumeStatistics(). Because the code uses 64-bit arithmetics, you may need to modify it slightly to run it with Watcom C/C++ (only 11.0x and above)

Sample Code

#include <nit/nwdir.h>
#include <nwerrno.h>
#include <stdio.h>
#include <stdlib.h>

static unsigned long GetFreeDiskSpaceInMBs( const char* volumeName) {
  unsigned long availableDiskSpaceInMBs = 0;
  int           volumeNumber;
  if ( GetVolumeNumber( volumeName, &volumeNumber) == ESUCCESS) {
    VOLUME_INFO volumeStatistics;
    if ( GetVolumeStatistics( 0, ( BYTE) volumeNumber, sizeof( volumeStatistics), &volumeStatistics) == ESUCCESS) {
      unsigned __int64 totalUsableBlocks  = ( unsigned long long) volumeStatistics.availableBlocks + volumeStatistics.purgableBlocks;
      unsigned __int64 blockSizeInBytes   = ( unsigned long long) ( volumeStatistics.sectorsPerBlock) * 512;
      unsigned __int64 availableDiskSpace = totalUsableBlocks * blockSizeInBytes;
      availableDiskSpaceInMBs = ( unsigned long)( availableDiskSpace / ( 1024 * 1024));
    }
  }
  return availableDiskSpaceInMBs;
}

int main( int argc, char* argv[]) {
  if ( argc != 2) {
    printf( "Usage: %s <volume name>\n", argv[ 0]);
    return EXIT_FAILURE;
  }
  printf( "Available disk space in MBs: %u\n", GetFreeDiskSpaceInMBs( argv[ 1]));
  return EXIT_SUCCESS;
}

--Dmitry Mityugov

Novell® Making IT Work As One

© 2008 Novell, Inc. All Rights Reserved.