Created
March 5, 2011 14:15
-
-
Save anonymous/856377 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- Source/SpotlightFS.m.orig 2011-03-04 23:50:36.000000000 +0000 | |
+++ Source/SpotlightFS.m 2011-03-05 14:10:42.000000000 +0000 | |
@@ -458,8 +458,46 @@ | |
NSString *lastComponent = [path lastPathComponent]; | |
- if ([lastComponent hasPrefix:@":"]) | |
+ if ([lastComponent hasPrefix:@":"]) { | |
return DecodePath(lastComponent); | |
+ } | |
+ if ([lastComponent isEqualToString:@"the-only-result"]) { | |
+ // We can't use the same optimisation as SpotlightFS does itself of using | |
+ // the encoded path as the name of the symlink because the whole point is | |
+ // that the symlink name is reliable. Instead we must run the search and | |
+ // pull out the first hit. Worst case is that the search runs twice in | |
+ // very quick succession if someone lists the directory then dereferences | |
+ // this link, and in that case the result(s) will be in the cache anyway. | |
+ // It's also a good opportunity to check there's just one hit though. | |
+ | |
+ // Extract query (only stuff under SmarterFolder for now). Path is: | |
+ // SmarterFolder/<query>/the-only-result | |
+ // which comes back from pathComponents as: | |
+ // "/", "SmarterFolder", <some query>, "the-only-result" | |
+ // but directoryContentsAtPath() appears to support: | |
+ // SmarterFolder/any/amount/of/leading/junk/<query>/ | |
+ // so we support this too: | |
+ // SmarterFolder/any/amount/of/leading/junk/<query>/the-only-result | |
+ NSArray *pathComponents = [path pathComponents]; | |
+ NSString *firstDir = [pathComponents objectAtIndex:1]; | |
+ | |
+ if ([firstDir isEqualToString:kSmarterFolder] && [pathComponents count] >= 4) { | |
+ int penultimateComponentIndex = [pathComponents count] - 2; | |
+ NSString *query = [pathComponents objectAtIndex:penultimateComponentIndex]; | |
+ NSArray *scopeDirectories = nil; // only applies to saved searches | |
+ NSArray *resultsSymlinkNames = [self encodedPathResultsForSpotlightQuery:query scope:scopeDirectories]; | |
+ if ([resultsSymlinkNames count] == 1) { | |
+ return DecodePath([resultsSymlinkNames objectAtIndex:0]); | |
+ } else { | |
+ // Someone is trying to dereference: | |
+ // SmarterFolder/<query-with-zero-or-multiple-results>/the-only-result | |
+ // Use the link target as a crude way to return an error message; as | |
+ // this symlink only exists while being dereferenced and won't appear | |
+ // in (clutter up) the directory contents, we can do this. | |
+ return @"error-there-must-be-exactly-one-result"; | |
+ } | |
+ } | |
+ } | |
*error = [NSError errorWithPOSIXCode:ENOENT]; | |
return nil; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment