Skip to content

A swift library for observing changes in a file system using inotify

Notifications You must be signed in to change notification settings

timvinc/FileSystemWatcher

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

FileSystemWatcher

A bridge between Linux's C API inotify and Swift.

Use cases

For any async file system event monitoring, we gotcha.

Usage

I made a demonstration app of usage of this library.

The most basic form of usage is as follows:

import FileSystemWatcher

var eventCount : Int

eventCount = 0

func printEvent(event: FileSystemEvent) {
    print("Hey! Something happened!!")

    eventCount += 1
}


print("Starting!")

let delayBetweenEvents = 5.0

let myWatcher = FileSystemWatcher(deferringDelay: delayBetweenEvents)

myWatcher.watch(
    paths: ["/tmp"], 
    for: [FileSystemEventType.inAllEvents],
    thenInvoke: printEvent)


myWatcher.start()

readLine()

myWatcher.stop()

print("Total number of events: " + String(eventCount))

print("Finished!")

Limitations

Only deferred mechanism

For now, I'm only interested in supporting a deferred kind of FS event queue. Maybe in a future release, special flags for customizing the watcher's behavior could be implemented.

Fixed: Events don't have name

Thanks to the Stack Overflow community for helping me fix this issue: C struct instance is missing members after copying it to Swift.

Original problem:

As you can see in this line, the struct inotify_event "has no member name". This is not quite true, though: the member name is optional. I don't know yet how to obtain that CString from the struct. It would be useful, if we wanted to know more about the characteristics of the captured FS events.

For our use case (at SourceKittenDaemon) that is not necessary: we only need to know when a file has changed. But it would be nice to have that feature. If you know how to do it, please open an Issue or a Pull Request: I'll be happy to recieve your help.

About

A swift library for observing changes in a file system using inotify

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Swift 100.0%