Solved

free disk space

Posted on 2001-08-07
1
630 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Add content to output file 4 62
Expand to include initial dialog with two choices. 9 66
if loop in java 3 128
wordcount challenge 11 102
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
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.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

929 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now