Solved

free disk space

Posted on 2001-08-07
1
625 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
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

743 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

10 Experts available now in Live!

Get 1:1 Help Now