Re: Directory enumeration gives wrong file type for aliases
Re: Directory enumeration gives wrong file type for aliases
- Subject: Re: Directory enumeration gives wrong file type for aliases
- From: Gabriel Zachmann <email@hidden>
- Date: Sat, 08 Oct 2016 02:03:21 +0200
Thanks a lot or your quick response!
>> I would at least attributesOfItemAtPath:error expect to show NSFileTypeSymbolicLink for the aliases and symlinks.
>
> No, aliases are files, not symbolic links. Symbolic links are a special category of file system objects in a Unix-compatible file system.
I understand.
> (And you should be using URLs generally, instead of paths.)
Will try to do.
>>
>> [NSNumber numberWithBool: YES] == [fattrs objectForKey: NSFileExtensionHidden]
>
> Also, this is really bad. These are objects, and there’s no guarantee that the object pointers are identical,
Right, of course, thanks, that was stupid.
I have now written a small test program using
enumeratorAtURL:includingPropertiesForKeys:options:errorHandler:
Code is below.
One thing that surprised me a little, in light of the fact that aliases are completely different from symlinks,
is that
[url getResourceValue: &isAlias forKey: NSURLIsAliasFileKey error: nil]
yields true for both aliases and symlinks.
Similarly, the method URLByResolvingAliasFileAtURL: seems to resolve both aliases and symlinks just fine.
Which is nice, because it makes the code easier.
Best regards,
Gabriel.
Code for testing (error handling omitted for sake of clarity):
NSFileManager * filemanager = [NSFileManager defaultManager];
NSURL * directoryURL = ...
NSArray * keys = [NSArray arrayWithObjects:
NSURLIsDirectoryKey, NSURLIsAliasFileKey, NSURLIsSymbolicLinkKey,
NSURLLocalizedNameKey, nil];
NSDirectoryEnumerator *enumerator = [filemanager
enumeratorAtURL: directoryURL
includingPropertiesForKeys: keys
options: (NSDirectoryEnumerationSkipsHiddenFiles)
errorHandler: nil ];
for ( NSURL * url in enumerator )
{
NSNumber * isAlias = nil;
[url getResourceValue: &isAlias forKey: NSURLIsAliasFileKey error: NULL];
NSNumber * isSymlink = nil;
[url getResourceValue: &isSymlink forKey: NSURLIsSymbolicLinkKey error: NULL];
NSURL * resolvedurl;
if ( [isAlias boolValue] )
{
resolvedurl = [NSURL URLByResolvingAliasFileAtURL: url
options: (NSURLBookmarkResolutionWithoutUI|NSURLBookmarkResolutionWithoutMounting)
error: nil ];
if ( ! resolvedurl )
continue;
}
else
if ( [isSymlink boolValue] )
{
// this gets never executed
NSString * resolvedpath = [ filemanager destinationOfSymbolicLinkAtPath: [url path] error: nil ];
if ( ! resolvedpath )
continue;
resolvedurl = [NSURL URLWithString: resolvedpath];
}
else
resolvedurl = url;
NSLog( @" Resolved path: %@", [resolvedurl path] );
NSLog( @" Alias = %hhd, Symlink = %hhd.",
[isAlias boolValue], [isSymlink boolValue] );
}
_______________________________________________
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