Mailing Lists: Apple Mailing Lists
Image of Mac OS face in stamp
Re: Obtain directory size.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Obtain directory size.




On May 20, 2005, at 9:09 AM, Julian wrote:

Hello,
Since NSFileManager has no capabilities of returning the size of a folder(a folder + contents), what would be a suitable & fast method for obtaining this besides enumerating as it's slow as christmas. I am looking for perhaps a carbon call to get this in one swoop.

I don't think there's anything that does it all for you.  I use Carbon to iterate, which is must faster than using NSEnumerator, but it still may not be the best way.

Note my comment about the resource forks.  The application I'm using this in doesn't grab the resource forks so it doesn't care about their size.

- (unsigned long long) fastFolderSizeAtFSRef:(FSRef*)theFileRef
{
    FSIterator    thisDirEnum = NULL;
    unsigned long long totalSize = 0;
    
    // Iterate the directory contents, recursing as necessary
    if (FSOpenIterator(theFileRef, kFSIterateFlat, &thisDirEnum) == noErr)
    {
        const ItemCount kMaxEntriesPerFetch = 256;
        ItemCount actualFetched;
        FSRef    fetchedRefs[kMaxEntriesPerFetch];    
        FSCatalogInfo fetchedInfos[kMaxEntriesPerFetch];
        
        // DCJ Note right now this is only fetching data fork sizes... if we decide to include
        // resource forks we will have to add kFSCatInfoRsrcSizes

        OSErr fsErr = FSGetCatalogInfoBulk(thisDirEnum, kMaxEntriesPerFetch, &actualFetched,
                                        NULL, kFSCatInfoDataSizes | kFSCatInfoNodeFlags, fetchedInfos,
                                        fetchedRefs, NULL, NULL);
        while ((fsErr == noErr) || (fsErr == errFSNoMoreItems))
        {
            ItemCount thisIndex;
            for (thisIndex = 0; thisIndex < actualFetched; thisIndex++)
            {
                // Recurse if it's a folder
                if (fetchedInfos[thisIndex].nodeFlags & kFSNodeIsDirectoryMask)
                {
                    totalSize += [self fastFolderSizeAtFSRef:&fetchedRefs[thisIndex]];
                }
                else
                {
                    // add the size for this item
                    totalSize += fetchedInfos[thisIndex].dataLogicalSize;
                }
            }
            
            if (fsErr == errFSNoMoreItems)
            {
                break;
            }
            else
            {
                // get more items
                fsErr = FSGetCatalogInfoBulk(thisDirEnum, kMaxEntriesPerFetch, &actualFetched,
                                        NULL, kFSCatInfoDataSizes | kFSCatInfoNodeFlags, fetchedInfos,
                                        fetchedRefs, NULL, NULL);
            }
        }
        FSCloseIterator(thisDirEnum);
    }
    return totalSize;
}


 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

References: 
 >Obtain directory size. (From: Julian <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2011 Apple Inc. All rights reserved.