2012-07-19 13 views

Respuesta

13

Sí, es posible en C. Debe buscar Kernel Colas.

He aquí una muestra pequeña para ver el directorio:

#include <errno.h>  // for errno 
#include <fcntl.h>  // for O_RDONLY 
#include <stdio.h>  // for fprintf() 
#include <stdlib.h>  // for EXIT_SUCCESS 
#include <string.h>  // for strerror() 
#include <sys/event.h> // for kqueue() etc. 
#include <unistd.h>  // for close() 

int main (int argc, const char *argv[]) 
{ 
    int kq = kqueue(); 
    // dir name is in argv[1], NO checks for errors here 
    int dirfd = open (argv[1], O_RDONLY); 

    struct kevent direvent; 
    EV_SET (&direvent, dirfd, EVFILT_VNODE, EV_ADD | EV_CLEAR | EV_ENABLE, 
      NOTE_WRITE, 0, (void *)dirname); 

    kevent(kq, &direvent, 1, NULL, 0, NULL); 

    // Register interest in SIGINT with the queue. The user data 
    // is NULL, which is how we'll differentiate between 
    // a directory-modification event and a SIGINT-received event. 
    struct kevent sigevent; 
    EV_SET (&sigevent, SIGINT, EVFILT_SIGNAL, EV_ADD | EV_ENABLE, 0, 0, NULL); 
    // kqueue event handling happens after the legacy API, so make 
    // sure it doesn eat the signal before the kqueue can see it. 
    signal (SIGINT, SIG_IGN); 

    // Register the signal event. 
    kevent(kq, &sigevent, 1, NULL, 0, NULL); 

    while (1) { 
     // camp on kevent() until something interesting happens 
     struct kevent change; 
     if (kevent(kq, NULL, 0, &change, 1, NULL) == -1) { exit(1); } 
     // The signal event has NULL in the user data. Check for that first. 
     if (change.udata == NULL) { 
      break; 
     } else { 
     // udata is non-null, so it's the name of the directory 
     printf ("%s\n", (char*)change.udata); 
     } 
    } 
    close (kq); 
    return 0; 
} 

Los detalles se pueden encontrar en el cap. 16 (kqueues y FSEvents) de "Programación avanzada de Mac OSX" por Mark Dalrymple. La información adicional se puede encontrar en la documentación * BSD para kqueues.

O use esta API de FSEvents (en su mayoría está basada en C).

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

para crear la secuencia de eventos FSEvents con devolución de llamada de C puro.

A continuación, conecte esta secuencia de eventos a su runloop utilizando el

void FSEventStreamScheduleWithRunLoop (FSEventStreamRef streamRef, 
            CFRunLoopRef runLoop, 
            CFStringRef runLoopMode); 

Sí, aquí que es mejor usar una línea de Obj-C para obtener el identificador Runloop: obtener la CFRunLoop de un NSRunLoop utilizando -getCFRunLoop

CFRunLoop* loopRef = [[NSRunLoop currentRunLoop] getCFRunLoop]; 

o usar la llamada C pura

CFRunLoop* loopRef = CFRunLoopGetCurrent(); 

Iniciar el evento transmitir con

Boolean FSEventStreamStart (FSEventStreamRef streamRef); 

Detener el flujo de eventos con

void FSEventStreamStop (FSEventStreamRef streamRef); 

Y luego desprogramar desde el runloop con esto:

void FSEventStreamUnscheduleFromRunLoop (FSEventStreamRef streamRef, 
            CFRunLoopRef runLoop, 
            CFStringRef runLoopMode); 

invalidar la corriente (limpieza):

void FSEventStreamInvalidate (FSEventStreamRef streamRef); 

Espero que esto te ayude tarted.

+0

En mis experimentos, kqueue no es funcionalmente equivalente a FSEvents. ¡Gracias por perfilar el bit de CFRunLoop! – berkus