Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSFileManager:enumeratorAtURL: returns a different form of URL to NSFileManager:URLForDirectory

If I fetch my sandbox's Application Support directory using NSFileManager:URLForDirectory then I get back the following URL:

file://localhost/var/mobile/Applications/EA80DE91-394C-4EAB-B269-1081C859BB0F/Library/Application%20Support/

However if I use NSFileManager:enumeratorAtURL to search the directory I get back URLs of the form:

file://localhost/private/var/mobile/Applications/EA80DE91-394C-4EAB-B269-1081C859BB0F/Library/Application%20Support/

This is causing me problems because I'm doing URL search/replace manipulations and the difference causes a mismatch. I've run into this sort of difference elsewhere previously and my solution then was to use the path of the urls (using NSURL:path) instead of the raw URLs, however that won't work in this situation as the paths produced are:

/var/mobile/Applications/EA80DE91-394C-4EAB-B269-1081C859BB0F/Library/Application Support
/private/var/mobile/Applications/EA80DE91-394C-4EAB-B269-1081C859BB0F/Library/Application Support/

What I am trying to do is this: I can have any number of files in my Application Support directory with the same name but different locations, and I need to extract the path relative to the Application Support directory. i.e. if I have the following

/Application Support/abc/file.dat
/ApplicationSupport/abc/def/file.dat

I want to be able to extract "abc" and "abc/def". I was doing this using the following code:

NSArray *keys = [NSArray arrayWithObject:NSURLIsRegularFileKey];
NSDirectoryEnumerator *dirEnumerator = [self.fileManager enumeratorAtURL:[MyManager appSupportDirectory]
                                          includingPropertiesForKeys:keys
                                                             options:NSDirectoryEnumerationSkipsHiddenFiles
                                                        errorHandler:nil];
    for (NSURL *url in dirEnumerator)
    {
        if ( NSOrderedSame == [[url lastPathComponent] caseInsensitiveCompare:kFilename])
        {
            NSString *appSupportPath = [[MyManager appSupportDirectory] path];
            NSString *pathToFile = [url path];
            pathToFile = [pathToFile URLByDeletingLastPathComponent];
            NSString *subPath = [pathToFile  stringByReplacingOccurrencesOfString:appSupportPath withString:@""];

(MyManager:appSupportDirectory calls NSFileManager:URLForDirectory: using NSApplicationSupportDirectory)

This works fine on the simulator where enumeratorAtURL: and URLFOrDirectory: return identical paths, but due to their differences on hardware it fails.

Is there a way to get enumeratorAtURL: and URLForDirectory: to return identical paths, or if not is there an alternative elegant way of extracting the subpaths to what I am currently doing?

like image 964
Gruntcakes Avatar asked Jul 18 '12 17:07

Gruntcakes


2 Answers

var is symlinked to /private/var/

calling [url URLByResolvingSymlinksInPath] fixes it.

see What does the /private prefix on an iOS file path indicate? for more discussion.

like image 117
Skotch Avatar answered Nov 09 '22 20:11

Skotch


I had to switch to doing this instead:

NSDirectoryEnumerator *dirEnum = [self.fileManager enumeratorAtPath: [NSHomeDirectory() stringByAppendingPathComponent:@"/Library/Application Support/"]];
like image 26
Gruntcakes Avatar answered Nov 09 '22 21:11

Gruntcakes