Le 19 août 2009 à 17:29, PCWiz a écrit :

Hi,

It would probably be a good idea to tell you guys some of the methods I have tried. First of all, I've tried just using NSFileManager and NSEnumerator to enumerate through the directory. This didn't add up resource forks AND it didn't round up the sizes to Finder's 4KB minimum block size so it was obviously inaccurate. I also tried using the du -sk shell command (which returned folder size in KB) with NSTask, and while it was mostly accurate, it collapsed under heavy load. The next thing I tried was using the Carbon File Manager API using this method by Dave DeLong:

http://github.com/davedelong/BuildCleaner/blob/b2712242b4eea1fff0e78a08b393a417e3019c8a/NSFileManager+FileSize.m

This method worked for some folders/files, but for others it returned wildly innacurate numbers. The method I'm using right now is probably the closest I've ever gotten:

#define MINBLOCK 4096

+ (long long)sizeOfFile:(NSString *)filePath
{
        long long fileSize = 0;
        // Check if item is file or folder
if ([[[[NSFileManager defaultManager] fileAttributesAtPath:filePath traverseLink:NO] objectForKey:NSFileType] isEqualTo:@"NSFileTypeRegular"]) { fileSize = ((([[[NSFileManager defaultManager] fileAttributesAtPath:filePath traverseLink:YES] fileSize] + MINBLOCK - 1) / MINBLOCK) * MINBLOCK);
        } else {
                NSString *file;
                NSNumber *fsize;
                NSDictionary *fattrs;
                NSDirectoryEnumerator *de;
                de = [[NSFileManager defaultManager] enumeratorAtPath:filePath];
                while(file = [de nextObject]) {
rsrcPath = [NSString stringWithFormat:@"%@/..namedfork/rsrc", file];
                        fattrs = [de fileAttributes];
                        fsize  = [fattrs objectForKey:NSFileSize];
if (![[fattrs valueForKey:NSFileType] isEqualTo:NSFileTypeDirectory]) { fileSize += ((([[fattrs valueForKey:NSFileSize] longLongValue] + MINBLOCK - 1) / MINBLOCK) * MINBLOCK);
                        }
                }
        }
        return fileSize;        
}

As you can see it performs a quick check to see whether its a file or a folder, then takes the appropriate actions to find the file size. The folder size method rounds up the files to the 4KB minimum block size. I think what this method needs is just to add up the resource forks and then it would be totally accurate. I'm just confused as to how I would do that.

Thanks

Use The Core Services File Manager API :

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 arch...@mail-archive.com

Reply via email to