简体   繁体   中英

How to determine changes in a Directory and it's subdirectories between application runs?

how can I determine the changes that happend in a given directory in the time my application was not running? The directory contains only Directories and symbolic links but whenever a User copies a new File in the Directory or creates a new subdirectory m Application has to deal with it. espacially if this change happens while my application was not running. The first naiv way is to store the whole Directory and Fileslist somewhere and with the next start of my application I crawl the whole directory again and compare it with my saved state but that takes way to much time. Is there a way to create a Hash for every subdirectory and compare only the Hash? Will this be a faster way? Do you have any best practice for this task?

Kind regards

Take a look at the FSEvents documentation. Point 3. describes your Problem and a possible solution:

FSEventStreamStart() -> Starts receiving events and servicing them from the client's runloop(s) using the callback supplied by the client when the stream was created. If a value was supplied for the sinceWhen parameter then "historical" events will be sent via your callback first, then a HistoryDone event, then "contemporary" events will be sent on an ongoing basis (as though you had supplied kFSEventStreamEventIdSinceNow for sinceWhen).

If you specify your desired directory at the creation, you will only get historical events that did happen under or at that path:

FSEventStreamRef FSEventStreamCreate(
   CFAllocatorRef allocator,
   FSEventStreamCallback callback,
   FSEventStreamContext *context,
   CFArrayRef pathsToWatch,
   FSEventStreamEventId sinceWhen,
   CFTimeInterval latency,
   FSEventStreamCreateFlags flags);

You need to specify a FSEventStreamCallback . This callback gets sent arrays of paths and event flags on that paths. You then should be able to sift through the events. Take a look at the FSEventStreamEventFlags to pick the ones you need.

Also take a look at the File System Events Programming guide . There is a section that deals with your exact problem. The suggestion there is to combine the event system with a hierarchy cache.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM