• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
How to get free bytes and total bytes for volumes
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

How to get free bytes and total bytes for volumes


  • Subject: How to get free bytes and total bytes for volumes
  • From: Marc Lohse <email@hidden>
  • Date: Fri, 4 Jan 2008 11:33:04 +0100

I am trying to read the free bytes for a volume before trying to copy
some files to it. Shouldn't be a big problem (even to a newbie - as i am one)
but NSFileManager does not provide methods that do the job. Googling a bit
i found out that i have to use some Carbon file manager functions. BUT
that does not give me correct values (i think). When using this code:


- (unsigned long long) getVolumeTotalBytesForPath: (NSString *) path
{
[path retain];
SInt16 vRefNum = [self _getVolumeNumberForPath: path];
unsigned long long returnValue = 0;
OSErr osErr;
FSVolumeInfo info;
osErr = FSGetVolumeInfo (vRefNum, 0, NULL, kFSVolInfoSizes, &info, NULL, NULL);
if (osErr == noErr) {
returnValue = info.totalBytes;
} else {
NSLog(@"Could not retrive total byte size. Error: %i", osErr);
returnValue = 0;
}
[path release];
return returnValue;
}


- (unsigned long long) getVolumeFreeBytesForPath: (NSString *) path
{
[path retain];
SInt16 vRefNum = [self _getVolumeNumberForPath: path];
unsigned long long returnValue = 0;
OSErr osErr;
FSVolumeInfo info;
osErr = FSGetVolumeInfo (vRefNum, 0, NULL, kFSVolInfoSizes, &info, NULL, NULL);
if (osErr == noErr) {
returnValue = info.freeBytes;
} else {
NSLog(@"Could not retrive free bytes. Error: %i", osErr);
returnValue = 0;
}
[path release];
return returnValue;
}


- (SInt16) _getVolumeNumberForPath: (NSString *) path
{
[path retain];
FSRef pathRef; // why do i have to cast the fileSystemRepresentation to UInt8* anyway?
// according to a thread from 2003 this should not be necessary
FSPathMakeRef((UInt8*)[path fileSystemRepresentation], &pathRef, NULL);
OSErr osErr;
FSCatalogInfo catInfo;
osErr = FSGetCatalogInfo(&pathRef, kFSCatInfoVolume, &catInfo, NULL, NULL, NULL);
SInt16 returnValue = 0;
if(osErr == noErr) {
returnValue = catInfo.volume;
}
[path release];
return returnValue;
}



[...]

NSLog(@"Total volume size: %i bytes", [self getVolumeTotalBytesForPath: @"/Users/marc/Desktop"]);
NSLog(@"Free volume size: %i bytes", [self getVolumeFreeBytesForPath: @"/Users/marc/Desktop"]);



i get something like 3 free bytes and 18 total bytes using a path on my PowerBook's harddrive which has 80GB total and 12GB free space. In the documentation for FSVolumeInfo it says :

[...]
UInt64 totalBytes;
UInt64 freeBytes;
[...]

so using an unsigned long long as the returnValue should be fine.
i realized, though, that the relation of free bytes to total bytes actually more or less fits: 12 GB is roughly 1/6 of 80GB and the same is true for 3 and 18. I am lost and i fear that i am probably making some
stupid mistake...





_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: How to get free bytes and total bytes for volumes
      • From: Jean-Daniel Dupas <email@hidden>
  • Prev by Date: Printing a layer-backed view
  • Next by Date: Re: SBApplication taking forever to instantiate
  • Previous by thread: Re: Printing a layer-backed view
  • Next by thread: Re: How to get free bytes and total bytes for volumes
  • Index(es):
    • Date
    • Thread