Hi,

I'd like to get some comments from ObjC developers about the following
changes I made to the SpotlightFS source code to only see the name of
the files in the Spotlight search results, instead of the full path to
the original file. I'm a programmer, but know only ObjC in theory, so
I might have made mistakes in there that 'real' ObjC programmers will
easily spot. (In particular, I'm pretty unsure when I need/should
autorelease, retain or copy variables I initialize, use or return.)

Thanks.

$ svn diff SpotlightFS.h SpotlightFS.m
Index: SpotlightFS.h
===================================================================
--- SpotlightFS.h       (revision 691)
+++ SpotlightFS.h       (working copy)
@@ -27,7 +27,7 @@
 // See the documentation for FUSEFileSystem for more details.
 //
 @interface SpotlightFS : FUSEFileSystem {
-  // Empty
+  NSMutableDictionary* symlinks_;
 }

 // Returns an array of all *.savedSearch files in "~/Library/Saved
Searches".
@@ -72,6 +72,7 @@
 // all forward slashes have been replaced by colons.
 //
 - (NSArray *)encodedPathResultsForSpotlightQuery:(NSString
*)queryString
-                                           scope:(NSArray
*)scopeDirectories;
+                                           scope:(NSArray
*)scopeDirectories
+                                           path:(NSString *)path;

 @end
Index: SpotlightFS.m
===================================================================
--- SpotlightFS.m       (revision 691)
+++ SpotlightFS.m       (working copy)
@@ -210,7 +210,8 @@
 // an NSArray of all the matching file paths (encoded).
 //
 - (NSArray *)encodedPathResultsForSpotlightQuery:(NSString
*)queryString
-                                           scope:(NSArray
*)scopeDirectories {
+                                           scope:(NSArray
*)scopeDirectories
+                                           path:(NSString *)path {
   // Try to create an MDQueryRef from the given queryString.
   MDQueryRef query = MDQueryCreate(kCFAllocatorDefault,
                                    (CFStringRef)queryString,
@@ -247,11 +248,20 @@
   int count = MDQueryGetResultCount(query);
   NSMutableArray *symlinkNames = [NSMutableArray array];

+  if (symlinks_ == nil) {
+    symlinks_ = [[NSMutableDictionary alloc] initWithCapacity:count];
+    [symlinks_ retain];
+  }
+
   for (int i = 0; i < count; i++) {
     MDItemRef item = (MDItemRef)MDQueryGetResultAtIndex(query, i);
-    NSString *name = (NSString *)MDItemCopyAttribute(item,
kMDItemPath);
+    NSString *name = (NSString *)MDItemCopyAttribute(item,
kMDItemFSName);
     [name autorelease];
     [symlinkNames addObject:EncodePath(name)];
+    NSString *symlink = (NSString *)MDItemCopyAttribute(item,
kMDItemPath);
+    [symlink autorelease];
+    NSString *symlinkKey = [path
stringByAppendingPathComponent:name];
+    [symlinks_ setObject:symlink forKey:symlinkKey];
   }

   CFRelease(query);
@@ -292,7 +302,7 @@
 
objectForKey:@"FXScopeArrayOfPaths"];
   }

-  return [self encodedPathResultsForSpotlightQuery:query
scope:scopeDirectories];
+  return [self encodedPathResultsForSpotlightQuery:query
scope:scopeDirectories path:path];
 }

 - (BOOL)createDirectoryAtPath:(NSString *)path
@@ -396,9 +406,11 @@

   } else {

-    NSString *decodedPath = DecodePath([path lastPathComponent]);
-    NSFileManager *fm = [NSFileManager defaultManager];
-    attr = [[[fm fileAttributesAtPath:decodedPath traverseLink:NO]
mutableCopy] autorelease];
+    NSString *decodedPath = [symlinks_ objectForKey:path];
+    if (decodedPath) {
+      NSFileManager *fm = [NSFileManager defaultManager];
+      attr = [[[fm fileAttributesAtPath:decodedPath traverseLink:NO]
mutableCopy] autorelease];
+    }
     if (!attr)
       attr = [NSMutableDictionary dictionary];
     [attr setObject:NSFileTypeSymbolicLink forKey:NSFileType];
@@ -416,10 +428,10 @@
     return nil;
   }

-  NSString *lastComponent = [path lastPathComponent];
+  NSString *decodedPath = [symlinks_ objectForKey:path];

-  if ([lastComponent hasPrefix:@":"])
-    return DecodePath(lastComponent);
+  if (decodedPath)
+    return [[decodedPath copy] autorelease];

   *error = [FUSEFileSystem errorWithCode:ENOENT];
   return nil;

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"macfuse-devel" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/macfuse-devel?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to