[futurebasic] Re: [FB] Sandboxing: Data migration fails

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : May 2013 : Group Archive : Group : All Groups

From: Ken Shmidheiser <kshmidheiser@...>
Date: Fri, 10 May 2013 02:13:37 -0400
I revisited recursive file scanning when I got home tonight and built the following. Removing the search for specific file types is as easy as remarking out a few lines.

I should warn that if you were to search your entire root drive with a @"/", this could run for quite a long time. I am guessing that most of the time would be taken up printing to the Console, but I have done no timing tests to see how quickly the enumerator works.

In this demo-- which I think would pass Sandboxing-- you recurse all directories in the OS X Preview application bundle for PNG files.

The function must be fed a full path -- UNIX shortcuts like ~/Desktop/ don't work unless you wanted to write some Objective-C code to expand the tilde, which is only one line.

Ken

/*
   Recursive File Scanning

   Uses standard Objective-C methods for Sandbox compatibility

   Ken Shmidheiser
   May 10, 2013
*/

compile as "Objective-C"
include "ConsoleWindow"

BeginCDeclaration
CFStringRef listFiles( CFStringRef directoryPath, CFStringRef fileType );
EndC
 
BeginCFunction
CFStringRef recursiveFileScan( CFStringRef directoryPath, CFStringRef fileType ) {
NSAutoreleasePool      *pool;
NSMutableArray         *filePaths;
NSString               *filePath;
NSString               *filesFound;
NSDirectoryEnumerator  *enumerator;

pool       = [[NSAutoreleasePool alloc] init];
filePaths  = [[NSMutableArray alloc] init];
enumerator = [[[NSFileManager defaultManager] enumeratorAtPath:(NSString *)directoryPath] retain];
    
  while ( ( filePath = [enumerator nextObject] ) != nil )
  {
    // Add files with extension specified by fileType
    if( [[filePath pathExtension] isEqualToString:(NSString *)fileType]) 
    {
       [filePaths addObject:[(NSString *)directoryPath stringByAppendingString:filePath]];
    }
  }
  [enumerator release];

  filesFound = [[filePaths valueForKey:@"description"] componentsJoinedByString:@"\r"];

  [filePaths release];
  [filesFound retain];
  [pool drain];

return (CFStringRef)filesFound;
}
EndC
toolbox fn recursiveFileScan( CFStringRef directoryPath, CFStringRef fileType ) = CFStringRef

dim as CFStringRef fileList

// Find all .png files located inside the Preview.app bundle
fileList = fn recursiveFileScan( @"/Applications/Preview.app/", @"png" )
fn HIViewSetText( sConsoleHITextView, fileList )
CFRelease( fileList )