I did what follows in order to be able to discriminate the different operations on the files inside the folder that is being watched with the FSEvent
(file added, file renamed, file deleted and file modified).
I created a object File
with the following properties (you can get as many as you want by calling attributesOfItemAtPath:error:
):
@property (strong) NSString *name;
@property (strong) NSString *type;
@property (strong) NSDate *creationDate;
@property (strong) NSDate *modificationDate;
@property (strong) NSString *hash;
@property (strong) NSString *path;
It is possible to populate an NSMutableArray
of File objects in the following way:
NSMutableArray *files = [[NSMutableArray alloc] init];
NSString *dirPath = //directory you are watching
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *theFiles = [fileManager contentsOfDirectoryAtURL:[NSURL fileURLWithPath:dirPath]
includingPropertiesForKeys:[NSArray arrayWithObject:NSURLNameKey]
options:NSDirectoryEnumerationSkipsHiddenFiles
error:nil];
NSArray* fileNames = [theFiles valueForKeyPath:@"lastPathComponent"];
if ( [fileNames count] > 0 ) {
for (NSInteger i=0; i<[fileNames count]; i=i+1) {
NSString *currentPath = [dirPath stringByAppendingString:[fileNames objectAtIndex:i]];
NSError *error;
NSDictionary *fileInfo = [fileManager attributesOfItemAtPath:currentPath error:&error];
File *currentFile = [[File alloc] initWithName:[fileNames objectAtIndex:i]
withType:fileInfo.fileType
withPath:currentPath
withHash: //get file hash
withCreationDate:fileInfo.fileCreationDate
andWithModificationDate:fileInfo.fileModificationDate];
[files addObject:currentFile];
}
If the directory contains subfolders, it is sufficient to iterate this process for every subfolder, returning and array of File objects.
To understand what operation was performed it is now sufficient to confront the information gathered before (saved in NSMutableArray *oldSnap
) and after the callback (saved in NSMutableArray *newSnap
)of the FSEvent
. First it is necessary to confront the files in oldSnap
with the one in newSnap
and then viceversa.
If in oldSnap
there is a file with a given name that is missing in newSnap
, it means this file has been either deleted or renamed. If there is a file which has the same hash in newSnap
, then the file has been renamed; else it has been deleted from the folder.
Finishing with the comparison, if there is a file in newSnap
with a different fileModificationDate
from the one with the same name in oldSnap
, this file has been modified. If there is a file in newSnap that is missing in oldSnap
, this file is a newly added one.
I have not yet come up with a solution regardant both the renaming and modification of a file. Hope this answer can help others to!