Im creating a FSEvents Stream with a path to directory:
- (BOOL) registerFSEventsObserverForURL:(NSURL*)url error:(NSError **)error
{
BOOL succeeded = YES;
FSEventStreamContext context;
context.version = 0;
context.info = (__bridge void*) self;
context.retain = NULL;
context.release = NULL;
context.copyDescription = NULL;
NSString* path = [url path];
dev_t device = [[self class] deviceIDForPath:path];
NSArray* pathArray = [NSArray arrayWithObject:path];
FSEventStreamRef stream = FSEventStreamCreateRelativeToDevice(NULL, &FSEventCallback,
&context,
device,
(__bridge CFArrayRef)pathArray,
kFSEventStreamEventIdSinceNow,
1.0,
0);
// ...
}
+ (dev_t)deviceIDForPath:(NSString*)path {
struct stat statbuf;
int result = stat([path fileSystemRepresentation], &statbuf);
if (result == -1) {
printf ("error with stat. %d\n", errno);
return EXIT_FAILURE;
}
dev_t device = statbuf.st_dev;
return device;
}
static void FSEventCallback(ConstFSEventStreamRef inStreamRef,
void* inClientCallBackInfo,
size_t inNumEvents,
void* inEventPaths,
const FSEventStreamEventFlags inEventFlags[],
const FSEventStreamEventId inEventIds[])
{
dev_t device = FSEventStreamGetDeviceBeingWatched(inStreamRef);
for (int i=0; i<inNumEvents; i++)
{
NSString* path = ((__bridge NSArray*)inEventPaths)[i];
NSURL* url = [NSURL fileURLWithPath: path]; // Here I'm getting a non valid URL.
}
}
So for example if url is 'file:///Users/user1/Pictures'
, when Im getting the callback the path is as expected: 'Users/user1/Pictures'
but I don't know how to get a file url from that path. I think I need to somehow find the root path for device ID and then call [NSURL fileURLWithPath:relativeToURL:]
.
My question is: how do I get a root url for a device ID?