Link to home
Start Free TrialLog in
Avatar of musashi
musashi

asked on

free disk space

i need a function in c that takes as a parameter a pathname. using this the function needs to return the free disk space in bytes of the volume that contains the passed in path.

i have used the sample code from FullPath.c, which i found included with the Metroworks compiler as well as on Apple's website itself. however, this code does not work with macOS8.6.

i need this routine to work with macOS8.6 and 9.0.
my routine only uses carbon compatible stuff and yet it fails.

--------------- here is what i tried (note: some things are defined elsewhere UINT32, UINT64, etc)

UINT32 diskFreeSpace( const TCHAR *szPath ) {
     OSErr result;
     FSSpec spec;
     AliasHandle alias;
     Boolean wasChanged;
     Str32 nullString;
     FSVolumeInfo volInfo;
     UINT64 freeBytes = 0;
     nullString[ 0 ] = 0;
     result = NewAliasMinimalFromFullPath( strlen( szPath ), szPath, nullString, nullString, &alias );
printf( "NewAliasMinimalFromFullPath (result = %d)\n", result );
     if( result == noErr ) {
          result = ResolveAlias(NULL, alias, &spec, &wasChanged);
printf( "ResolveAlias (result = %d)\n", result );
          if( spec.vRefNum == 0 ) {
               spec.parID = 0;
               spec.name[0] =  0;
          }
          DisposeHandle((Handle)alias);
          result = FSGetVolumeInfo( spec.vRefNum, 0, NULL, kFSVolInfoBlocks, &volInfo, NULL, NULL );
printf( "FSGetVolumeInfo (result = %d)\n", result );
          if( result == noErr ) {
printf( "blocksize = %d\nfreeblocks = %d\n", volInfo.blockSize, volInfo.freeBlocks );
               freeBytes = (UINT64)(volInfo.blockSize) * (UINT64)(volInfo.freeBlocks);
               if( freeBytes > (UINT64)0xffffffff ) return (UINT32)0xffffffff;
               return (UINT32)freeBytes;
          }
     }
     return 0;
}

----------------- this is the (correct) output on macOS9.0

path = "Macintosh HD:Desktop Folder:FreeSpaceTest:"
NewAliasMinimalFromFullPath (result = 0)
ResolveAlias (result = 0)
FSGetVolumeInfo (result = 0)
blocksize = 4096
freeblocks = 4421296

----------------- this is the (failed) output on macOS8.6

path = "Macintosh HD:Desktop Folder:FreeSpaceTest:"
NewAliasMinimalFromFullPath (result = 0)
ResolveAlias (result = 0)
FSGetVolumeInfo (result = -50)

ASKER CERTIFIED SOLUTION
Avatar of dioncrannitch
dioncrannitch

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial