Re: Calculating file size
Yes, nice catch. Thanks, Aaron On Apr 29, 2008, at 7:56 AM, Jean-Daniel Dupas wrote: Shouldn't be !isDirectory ? if (isDirectory) { FileInfo *fileInfo = (FileInfo *) info.finderInfo; fileHFSType = [NSNumber numberWithUnsignedLong:fileInfo-fileType]; fileCreator = [NSNumber numberWithUnsignedLong:fileInfo- fileCreator]; } else { fileHFSType = [NSNumber numberWithUnsignedLong:0]; fileCreator = [NSNumber numberWithUnsignedLong:0]; } ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to [EMAIL PROTECTED]
Re: Calculating file size
I dug up some code that might help. This uses a category to replace NSFileManager's fileAttributesAtPath: traverseLink and provides more attributes than the standard implementation. With this category method, you can continue to use the directory enumerator, so your calculation becomes: NSNumber *dataSize = [attributes objectForKey:NSFileDataSize]; NSNumber *rsrcSize = [attributes objectForKey:NSFileRsrcSize]; totalSize += [dataSize longLongValue] + [rsrcSize longLongValue]; Not the most efficient, but one approach. I am curious to hear comments on the code, too. It worked for my needs, but I may have overlooked some things. Aaron NSFileManagerAddtions: // // NSFileManagerAdditions.h // extern NSString const *NSFileAccessDate; extern NSString const *NSFileBackupDate; extern NSString const *NSFileAttributeModificationDate; extern NSString const *NSFileDataSize; extern NSString const *NSFileRsrcSize; extern NSString *NSFileTypePipe; NSFileManagerAdditions.m: #import NSFileManagerAdditions.h #include sys/stat.h #include sys/param.h @implementation NSFileManager (NSFileManagerAdditions) - (NSDictionary *)fileAttributesAtPath:(NSString *)fullPath traverseLink:(BOOL)flag { OSErr err; OSStatus status; FSRef ref; Boolean isDirectory; FSCatalogInfo info; const char *fullPathRepresentation = [fullPath fileSystemRepresentation]; int statResult; struct stat statBuf; if (flag) statResult = stat( fullPathRepresentation, statBuf); else statResult = lstat( fullPathRepresentation, statBuf); if (statResult) return nil; status = FSPathMakeRef( (const unsigned char*)fullPathRepresentation, ref, isDirectory); FSPermissionInfo *permissions = (FSPermissionInfo *) info.permissions; err = FSGetCatalogInfo( ref, kFSCatInfoGettableInfo, info, NULL, NULL, NULL); if (err) { if ( !(err == nsvErr || err == fnfErr) ) NSLog(@Error getting file attributes: %hd path: %@, err, fullPath); return nil; } // File type NSString *fileType = NSFileTypeUnknown; UInt16 type = statBuf.st_mode S_IFMT; if (type == S_IFREG) fileType = NSFileTypeRegular; else if (type == S_IFDIR) fileType = NSFileTypeDirectory; else if (type == S_IFLNK) fileType = NSFileTypeSymbolicLink; else if (type == S_IFIFO) fileType = NSFileTypePipe; else if (type == S_IFCHR) fileType = NSFileTypeCharacterSpecial; else if (type == S_IFBLK) fileType = NSFileTypeBlockSpecial; else if (type == S_IFSOCK)fileType = NSFileTypeSocket; // Dates NSTimeInterval creationDateInterval = (double)info.createDate.lowSeconds; if (info.createDate.highSeconds) creationDateInterval += info.createDate.highSeconds * 4294967296.0; if (info.createDate.fraction) creationDateInterval += info.createDate.fraction / 65536.0; NSTimeInterval modDateInterval = (double)info.contentModDate.lowSeconds; if (info.contentModDate.highSeconds) modDateInterval += info.contentModDate.highSeconds * 4294967296.0; if (info.contentModDate.fraction) modDateInterval += info.contentModDate.fraction / 65536.0; NSTimeInterval accessDateInterval = (double)info.accessDate.lowSeconds; if (info.accessDate.highSeconds) accessDateInterval += info.accessDate.highSeconds * 4294967296.0; if (info.accessDate.fraction) accessDateInterval += info.accessDate.fraction / 65536.0; NSTimeInterval backupDateInterval = (double)info.backupDate.lowSeconds; if (info.backupDate.highSeconds) backupDateInterval += info.backupDate.highSeconds * 4294967296.0; if (info.backupDate.fraction) backupDateInterval += info.backupDate.fraction / 65536.0; NSTimeInterval attributeModDateInterval = (double)info.attributeModDate.lowSeconds; if (info.attributeModDate.highSeconds) attributeModDateInterval += info.attributeModDate.highSeconds * 4294967296.0; if (info.attributeModDate.fraction) attributeModDateInterval += info.attributeModDate.fraction / 65536.0; // Sizes NSNumber *dataSize = [NSNumber numberWithUnsignedLongLong:info.dataLogicalSize]; NSNumber *rsrcSize = [NSNumber numberWithUnsignedLongLong:info.rsrcLogicalSize]; // HFS Type Creator NSNumber *fileHFSType, *fileCreator; if (isDirectory) { FileInfo *fileInfo = (FileInfo *) info.finderInfo; fileHFSType = [NSNumber numberWithUnsignedLong:fileInfo-fileType]; fileCreator = [NSNumber numberWithUnsignedLong:fileInfo-fileCreator]; } else { fileHFSType = [NSNumber numberWithUnsignedLong:0]; fileCreator = [NSNumber numberWithUnsignedLong:0];
Re: Calculating file size
On 28 Apr 2008, at 07:44, [EMAIL PROTECTED] wrote: Others have answered with good suggestions for other APIs, but I will point out for the record that you can do it in Cocoa, too, because the file system has a path-based mechanism in which ..namedfork/rsrc is appended to the path. For example, in Terminal: $ ls -li Documents//Example.doc 108 -rw-r--r--@ 1 aburgh aburgh 23552 Apr 27 2006 Documents/ Example.doc $ ls -li Documents/Example.doc/..namedfork/rsrc 108 -rw-r--r-- 1 aburgh aburgh 286 Apr 27 2006 Documents/ Example.doc/..namedfork/rsrc Notice that the inode is the same (the Catalog Node ID on HFS+), but size reflects the different forks. You can use this technique from any program that lets you specify a path, such as command line utilities, and you can even read and write the contents of the forks this way. This is documented in the Mac OS X system documentation. Where exactly? I have found a mention of namedfork in man RezWack and some #defines in /usr/include/sys/paths.h - but no other documentation. Kind regards, Gerriet. ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to [EMAIL PROTECTED]
Re: Calculating file size
On Apr 28, 2008, at 5:27 AM, Gerriet M. Denkmann wrote: This is documented in the Mac OS X system documentation. Where exactly? I have found a mention of namedfork in man RezWack and some #defines in /usr/include/sys/paths.h - but no other documentation. Kind regards, Gerriet. When I learned of it, roughly in the timeframe of Jaguar or Panther, there was a document with a title like Mac OS X System Overview, or something similar--I think that it where I read about it. I'm sure it was documented in somewhere in Apple's developer documentation, but it apparently it has been removed. Given the current lack of documentation, I agree with Jean-Daniel Dupas that I would avoid it unless actually required. Aaron ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to [EMAIL PROTECTED]
Re: Calculating file size
On 29/04/2008, at 2:58 AM, Aaron Burghardt wrote: When I learned of it, roughly in the timeframe of Jaguar or Panther, there was a document with a title like Mac OS X System Overview, or something similar--I think that it where I read about it. I'm sure it was documented in somewhere in Apple's developer documentation, but it apparently it has been removed. Given the current lack of documentation, I agree with Jean-Daniel Dupas that I would avoid it unless actually required. I personally wouldn't be too worried about using it. There are enough references around to suggest that it's here to stay and whilst it's not definitive, the kernel sources don't have comments to suggest that it's going to be deprecated like file/rsrc is. If they did deprecate it they'd have the issue of dealing with other file-systems that might be using that pattern (for example NTFS implementations might use it to access named streams). I guess if you wanted to be sure, you could contact DTS. Going back to the original question, there's another API that's worth considering that I don't think has been mentioned: getattrlist. ATTR_FILE_TOTALSIZE will give you the total number of bytes used by all of the file's forks. ATTR_FILE_ALLOCSIZE will give you the physical size (which includes the spare space used for padding). I'm not sure what the application of this information is for, but if it's for seeing if files will fit somewhere, it's a bit difficult to get an exact figure because there's the additional space that might be required in metadata files so care should be taken with how this information is used. - Chris smime.p7s Description: S/MIME cryptographic signature ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to [EMAIL PROTECTED]
Re: Calculating file size
Le 27 avr. 08 à 06:33, Jens Alfke a écrit : On 26 Apr '08, at 6:50 PM, Cocoa Dev wrote: I was wondering what was the best way to calucate folder size with cocoa? I was able to do this but it does not include resource forks: I think you'll need to drop down to a lower-level API to get the resource fork size; probably one of the *CatInfo* calls in Files.h. I can't really give specific advice because I haven't used those much since OS 9, and they've changed a lot since. There is no Cocoa API do to such thing but the CoreServices way is not very difficult. There is some sample in the FS Performance Guidelines (http://developer.apple.com/documentation/Performance/Conceptual/FileSystem/Articles/IteratingFiles.html ) ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to [EMAIL PROTECTED]
Re: Calculating file size
On Apr 27, 2008, at 6:33 AM, Jens Alfke wrote: On 26 Apr '08, at 6:50 PM, Cocoa Dev wrote: I was wondering what was the best way to calucate folder size with cocoa? I was able to do this but it does not include resource forks: I think you'll need to drop down to a lower-level API to get the resource fork size; probably one of the *CatInfo* calls in Files.h. I can't really give specific advice because I haven't used those much since OS 9, and they've changed a lot since. FSGetCatalogInfo with kFSCatInfoDataSizes = 0x4000, /* Data fork logical and physical size */ kFSCatInfoRsrcSizes = 0x8000, /* Resource fork logical and physical size */ HTH laurent ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to [EMAIL PROTECTED]
Re: Calculating file size
Le 27 avr. 08 à 15:02, Laurent Cerveau a écrit : On Apr 27, 2008, at 6:33 AM, Jens Alfke wrote: On 26 Apr '08, at 6:50 PM, Cocoa Dev wrote: I was wondering what was the best way to calucate folder size with cocoa? I was able to do this but it does not include resource forks: I think you'll need to drop down to a lower-level API to get the resource fork size; probably one of the *CatInfo* calls in Files.h. I can't really give specific advice because I haven't used those much since OS 9, and they've changed a lot since. FSGetCatalogInfo with kFSCatInfoDataSizes = 0x4000, /* Data fork logical and physical size */ kFSCatInfoRsrcSizes = 0x8000, /* Resource fork logical and physical size */ HTH laurent Works well for one file but is inefficient when you want to get size of many files. FSOpenIterator and FSGetCatalogInfoBulk are the way to go. ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to [EMAIL PROTECTED]
Re: Calculating file size
At 02:49 -0700 27/04/08, [EMAIL PROTECTED] wrote: From: Jens Alfke [EMAIL PROTECTED] References: [EMAIL PROTECTED] In-Reply-To: [EMAIL PROTECTED] Date: Sat, 26 Apr 2008 21:33:26 -0700 Message-ID: [EMAIL PROTECTED] On 26 Apr '08, at 6:50 PM, Cocoa Dev wrote: I was wondering what was the best way to calucate folder size with cocoa? I was able to do this but it does not include resource forks: I think you'll need to drop down to a lower-level API to get the resource fork size; probably one of the *CatInfo* calls in Files.h. I can't really give specific advice because I haven't used those much since OS 9, and they've changed a lot since. Check out the FolderSweep code at: http://www.brockerhoff.net/src/ (at the end of the page). You would pass in the bit masks asking for both logical fork sizes, and sum them for every file (they're 64bit unsigned integers). -- Rainer Brockerhoff [EMAIL PROTECTED] Belo Horizonte, Brazil In the affairs of others even fools are wise In their own business even sages err. Weblog: http://www.brockerhoff.net/bb/viewtopic.php ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to [EMAIL PROTECTED]
Re: Calculating file size
Others have answered with good suggestions for other APIs, but I will point out for the record that you can do it in Cocoa, too, because the file system has a path-based mechanism in which ..namedfork/rsrc is appended to the path. For example, in Terminal: $ ls -li Documents//Example.doc 108 -rw-r--r--@ 1 aburgh aburgh 23552 Apr 27 2006 Documents/ Example.doc $ ls -li Documents/Example.doc/..namedfork/rsrc 108 -rw-r--r-- 1 aburgh aburgh 286 Apr 27 2006 Documents/ Example.doc/..namedfork/rsrc Notice that the inode is the same (the Catalog Node ID on HFS+), but size reflects the different forks. You can use this technique from any program that lets you specify a path, such as command line utilities, and you can even read and write the contents of the forks this way. This is documented in the Mac OS X system documentation. In your case, you would need to construct the path and use NSFileManager's fileAttributesAtPath to get the attributes for the resource fork. Aaron On Apr 26, 2008, at 9:50 PM, Cocoa Dev wrote: Hello, I was wondering what was the best way to calucate folder size with cocoa? I was able to do this but it does not include resource forks: #import Foundation/Foundation.h int main() { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; NSString *path = [@/Folder stringByExpandingTildeInPath]; NSDirectoryEnumerator *e = [[NSFileManager defaultManager] enumeratorAtPath :path]; NSString *file; unsigned long long totalSize = 0; while ((file = [e nextObject])) { NSDictionary *attributes = [e fileAttributes]; NSNumber *fileSize = [attributes objectForKey:NSFileSize]; totalSize += [fileSize longLongValue]; } printf(Total size: %lld\n, totalSize); [pool release]; } or from the example in documentation - (IBAction)calculateSize:(id)sender { NSFileManager *fileManager = [NSFileManager defaultManager]; NSString *path = @/Folder; NSDictionary *fileAttributes = [fileManager fileAttributesAtPath:path traverseLink:YES]; if (fileAttributes != nil) { NSNumber *fileSize; if (fileSize = [fileAttributes objectForKey:NSFileSize]) { NSLog(@File size: %qi\n, [fileSize unsignedLongLongValue]); [label setIntValue:[fileSize unsignedLongLongValue]]; } } But I was unsure on how to traverse the entire file tree within that directory, and give the same size results as the finder. Thanks, -Mike ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/ajb.lists%40mac.com This email sent to [EMAIL PROTECTED] ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to [EMAIL PROTECTED]
Re: Calculating file size
On 26 Apr '08, at 6:50 PM, Cocoa Dev wrote: I was wondering what was the best way to calucate folder size with cocoa? I was able to do this but it does not include resource forks: I think you'll need to drop down to a lower-level API to get the resource fork size; probably one of the *CatInfo* calls in Files.h. I can't really give specific advice because I haven't used those much since OS 9, and they've changed a lot since. —Jens smime.p7s Description: S/MIME cryptographic signature ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to [EMAIL PROTECTED]