Commits

Jeff Laing committed 235b869

Added recursiveDirectoryContents:

  • Participants
  • Parent commits d8ab183

Comments (0)

Files changed (2)

MobileDeviceAccess.h

 - (NSArray*)directoryContents:(NSString*)path;
 
 /**
+ * Return a array containing a list of full pathnames found
+ * in the specified directory, and all subordinate directories.
+ * Entries for directories will end in "/"
+ * The entries for "." and ".." are not included.
+ * @param path Full pathname to the directory to scan
+ */
+- (NSArray*)recursiveDirectoryContents:(NSString*)path;
+
+/**
  * Open a file for reading.
  * @param path Full pathname to the file to open
  */

MobileDeviceAccess.m

 	return nil;
 }
 
+static BOOL read_dir( AFCDirectoryAccess *self, afc_connection afc, NSString *path, NSMutableArray *files )
+{
+	BOOL result;
+
+	afc_directory dir;
+	int ret = AFCDirectoryOpen(afc,[path UTF8String],&dir);
+
+	if (ret == 4) {
+		// its a file, so add it in and return
+		[files addObject:path];
+		return YES;
+	}
+
+	if (ret != 0) {
+		// something other than a file causes us to fail
+		return [self checkStatus:ret from:"AFCDirectoryOpen"];
+	}
+
+	// collect us, with a trailing slash, since we are a directory
+	[files addObject:[NSString stringWithFormat:@"%@/",path]];
+
+	// build a list of all the files located here.
+	NSMutableArray *here = [NSMutableArray new];
+	while (1) {
+		char *d = NULL;
+		AFCDirectoryRead(afc,dir,&d);
+		if (!d) break;
+		if (*d=='.') {
+			if (d[1]=='\000') continue;			// skip '.'
+			if (d[1]=='.') {
+				if (d[2]=='\000'); continue;	// skip '..'
+			}
+		}
+		[here addObject:[NSString stringWithFormat:@"%@/%s",path,d]];
+	}
+	AFCDirectoryClose(afc,dir);
+
+	// step through everything we found and add to the array
+	result = YES;
+	for (NSString *f in here) {
+		if (!read_dir(self, afc, f, files)) {
+			result = NO;
+			break;
+		}
+	}
+	[here release];
+
+	return result;
+}
+
+- (NSArray*)recursiveDirectoryContents:(NSString*)path
+{
+	if (!path) {
+		[self setLastError:@"Input path is nil"];
+		return nil;
+	}
+
+	if (![self ensureConnectionIsOpen]) return nil;
+
+	NSMutableArray *unsorted = [NSMutableArray new];
+	if (read_dir(self, _afc, path, unsorted)) {
+		[unsorted sortUsingSelector:@selector(compare:)];
+		NSArray *result = [NSArray arrayWithArray:unsorted];
+		[unsorted release];
+		return result;
+	}
+	[unsorted release];
+	return nil;
+}
+
 - (BOOL)mkdir:(NSString*)path
 {
 	if (!path) {
 			if (CFReadStreamSetClient (_readstream,flags, &AMSyslogRelayCallBack, &ctx )) {
 				CFReadStreamScheduleWithRunLoop (_readstream,CFRunLoopGetMain(),kCFRunLoopCommonModes);
 				if (CFReadStreamOpen(_readstream)) {
-					NSLog(@"stream opened ok");
+					// NSLog(@"stream opened ok");
 				} else {
 					NSLog(@"stream did not open");
 				}