1

I am working on unit tests for code that deals with the File System Events API. It creates a directory tree with random data in files and subdirectories, and then performs a few changes on these files and test that changes are handled properly.

However, this only works when the files are created in some user-space directory, such as on my own Desktop. When I try to put the test files somewhere like /tmp or the location of NSTemporaryDirectory(), the listener does not receive any events. Are changes within directories such as /var and /tmp not recorded by the File System events API at all? Or is this an issue with permissions on those directories? I am not using the Sandbox for this portion of my app.

Update

The issue was elsewhere in my code, File System Events do apply to the entire file system. See the below answer for my somewhat unrelated fix.

Aaron
  • 596
  • 1
  • 5
  • 15
  • From my brief tests there are events fired when I touch things in /tmp and in /var/tmp. How do you create the files in the unit test? How do you set up the FSEventStream? – mahal tertin Apr 16 '15 at 12:42
  • You are correct that events are properly fired for the `/tmp` directory. The issue was actually with my code converting an `NSArray` of `NSString`s to a `CFArrayRef` of `CFStringRef`s for the paths to watch. I'll can put the code for that in an answer, although it is somewhat unrelated to the original question. – Aaron Apr 16 '15 at 15:53

1 Answers1

0

The issue was with my code setting up the directories for my file system listener. For future reference, the syntax that worked for me is as follows. The directories variable could be passed in from elsewhere.

NSArray* directories = @[@"/Users/name/Documents", @"/tmp"];
CFArrayRef pathsToWatch = (__bridge CFArrayRef)(directories);

The pathsToWatch variable is then passed into the FSEventStreamCreate call.

Aaron
  • 596
  • 1
  • 5
  • 15
  • CFArray is “toll-free bridged” with its Cocoa Foundation counterpart, NSArray. So in a function where you see a CFArrayRef parameter, you can pass in an NSArray instance. – mahal tertin Apr 16 '15 at 16:20
  • Indeed, you are correct. I'll update to the simplified answer – Aaron Apr 16 '15 at 21:57