Solved

free disk space

Posted on 2001-08-07
1
635 Views
Last Modified: 2013-12-26
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)

0
Comment
Question by:musashi
1 Comment
 

Accepted Solution

by:
dioncrannitch earned 300 total points
ID: 7084855
I think the problem here is that FSGetVolumeInfo (and all the other FSXXX calls) only works on Mac OS 9 and higher. You can tell because in the header information it says it is available "in InterfaceLib 9.0 and later".

I use PBXGetVolInfoSync like this:

     paramBlock.ioNamePtr = nil;
     paramBlock.ioVRefNum = vRef;
     paramBlock.ioXVersion = 0;
     paramBlock.ioVolIndex = 0;

     result = PBXGetVolInfoSync(&paramBlock);
     if (result == noErr)
          freeBytes = paramBlock.ioVFreeBytes;

which should work on 8.5 and higher.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question