New To Mac-Forums?

Welcome to our community! Join the discussion today by registering your FREE account. If you have any problems with the registration process, please contact us!

Get your questions answered by community gurus Advice and insight from world-class Apple enthusiasts Exclusive access to members-only contests, giveaways and deals

Join today!

 
Start a Discussion
 

Mac-Forums Brief

Subscribe to Mac-Forums Brief to receive special offers from Mac-Forums partners and sponsors

Join the conversation RSS
OS X - Development and Darwin Discussion and questions about development for Mac OS X.

FSEvents


Post Reply New Thread Subscribe

 
Thread Tools
shr

 
Member Since: May 02, 2013
Posts: 3
shr is on a distinguished road

shr is offline
Hi
I need something like LINUX inotify library (not INotify add in for MAC), I want to monitor a directory in the file system (with sub dirs) to know when any file or dir was changed / added / deleted / moved / renamed from name to another etc. (I need also the names)

In almost any site I'm looking, it said how fabulous and easy is FSEvents.
So I've decided to give it a chance, and tried to understand how to work with FSEvents, but I got nothing when I try it... I can run my program but no prints at all.
I will be very happy if anyone can tell me what is wrong.
I will be more than very happy if someone have a better idea like if and where I can find a 'real' inotify for mac. (somehow I think I saw it somewhere almost a year ago, but I can't find it when I need it...)

Thanks for any help!

I took the example from: here
( I guess it hard to give a full working example, so I needed to collect the code fractals)

here (see below) is my code: it almost the same as example, except some slight changes (c/c++ etc.)

Code:
#include <CoreServices/CoreServices.h>
#include <stdio.h>

void mycallback( ConstFSEventStreamRef streamRef, void *clientCallBackInfo, size_t numEvents,  void *eventPaths,  const FSEventStreamEventFlags eventFlags[],  const FSEventStreamEventId eventIds[])
{
    int i;
    char **paths = (char**)eventPaths;
    // printf("Callback called\n");
    for (i=0; i<numEvents; i++) {
        /* flags are unsigned long, IDs are uint64_t */
        printf("Change %llu in %s, flags %u\n", eventIds[i], paths[i], eventFlags[i]);
   }
}

int main(int argc, char** argv)
{
    if(argc!=2){
        printf("usage:\n %s path/to/dir\n", argv[0]);
        return -1;
    }

    const char* rootdir = argv[1];
    CFStringRef mypath = CFStringCreateWithFileSystemRepresentation(NULL,rootdir);
    CFArrayRef pathsToWatch = CFArrayCreate(NULL, (const void **)&mypath, 1, NULL);
    FSEventStreamContext* callbackInfo = NULL; // could put stream-specific data here.
    FSEventStreamRef stream;
    CFAbsoluteTime latency = 0; /* Latency in seconds */

    /* Create the stream, passing in a callback */
    stream = FSEventStreamCreate(NULL, &mycallback, callbackInfo, pathsToWatch, kFSEventStreamEventIdSinceNow, latency, kFSEventStreamCreateFlagNone);

    /* HERE I shedule the run loop */
    FSEventStreamScheduleWithRunLoop(stream,CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);

    //guess need to run main loop to handle events
    CFRunLoopRun();

    /*** Never reaching here ***/

    FSEventStreamStop(stream);
    FSEventStreamInvalidate(stream);
    FSEventStreamRelease(stream);

    return 0;
}
/*
 * Compile:
 * $ g++ -o test main.cpp -framework CoreServices
 * Run: 
 * $ test /path/to/dir 
 */
QUOTE Thanks
Raz0rEdge

 
Raz0rEdge's Avatar
 
Member Since: Jul 17, 2009
Location: MA
Posts: 7,500
Raz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant future
Mac Specs: 27" i7 iMac, 24" iMac, 13" Macbook Air, iPhone 5 & 5S, iPod Nano 7th Gen, iPad 2 16GB WiFi, iPad 3

Raz0rEdge is offline
From the reference, note
Quote:
Once you have created an event stream, you must schedule it on your application’s run loop. To do this, call FSEventStreamScheduleWithRunLoop, passing in the newly-created stream, a reference to your run loop, and a run loop mode. For more information about run loops, read Run Loops.

If you don’t already have a run loop, you will need to devote a thread to this task. After creating a thread using your API of choice, call CFRunLoopGetCurrent to allocate an initial run loop for that thread. Any future calls to CFRunLoopGetCurrent will return the same run loop.
Read more about Run Loops..

You've created the callback and created the stream, but you need to schedule the stream to run in your own run loop or thread, it doesn't run otherwise and thus won't call your callback with changes..

--
Regards
...Ashwin



Be sure to read the Community Guidelines | The more information you provide, the better answers you get, remember GIGO.
QUOTE Thanks
shr

 
Member Since: May 02, 2013
Posts: 3
shr is on a distinguished road

shr is offline
Quote:
Originally Posted by Raz0rEdge View Post
From the reference, note


Read more about Run Loops..

You've created the callback and created the stream, but you need to schedule the stream to run in your own run loop or thread, it doesn't run otherwise and thus won't call your callback with changes..

Thanks for reply
But see one line after stream create,it is stream schedule.. (or below the middle row)
maybe there is a problem with CFRunLoopRun() ?


Code:
 
 /* Create the stream, passing in a callback */
    stream = FSEventStreamCreate(NULL, &mycallback, callbackInfo, pathsToWatch, kFSEventStreamEventIdSinceNow, latency, kFSEventStreamCreateFlagNone);
 /*  ******************************************************* */
    FSEventStreamScheduleWithRunLoop(stream,CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
 /*  ******************************************************** */
    //guess need to run main loop to handle events
    CFRunLoopRun();
QUOTE Thanks
shr

 
Member Since: May 02, 2013
Posts: 3
shr is on a distinguished road

shr is offline
I've found a working example in here:

Type these commands in Terminal.app

Code:
cd /tmp
git clone https://github.com/alandipert/fswatch
cd fswatch/
make
cp fswatch /usr/local/bin/fswatch
Example usage
Code:
/usr/local/bin/fswatch ~/path/to/watch ~/script/to/run/with/changes.sh
QUOTE Thanks

Post Reply New Thread Subscribe


« AVFoundation Video capture. | Xcodec »
Thread Tools

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread
Thread Starter
Forum
Replies
Last Post
inotify for Mac? PMatos OS X - Development and Darwin 2 04-12-2010 11:40 AM

All times are GMT -4. The time now is 10:14 AM.

Powered by vBulletin
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
X

Welcome to Mac-Forums.com

Create your username to jump into the discussion!

New members like you have made this community the ultimate source for your Mac since 2003!


(4 digit year)

Already a member?