Swiftpack.co - Package - Ponyboy47/Cinotify


inotify module for swift

Allows using the GNU inotify library in swift.


Add the following dependency to your Package.swift

.package(url: "https://github.com/Ponyboy47/Cinotify.git", .upToNextMinor(from: "3.0.0"))


Just like in C, only swifty

import Cinotify
import CSelect
import Glibc

// Create a file descriptor for inotify to use
var inotifyFileDescriptor = inotify_init()

// Set up inotify to use the file descriptor and watch for whatever inotify events you want
let watchedDescriptor = inotify_add_watch(inotifyFileDescriptor, "~/", UInt32(IN_ALL_EVENTS))

while true {
    // Get the set of file descriptors
    var fileDescriptorSet: fd_set = fd_set()
    fd_setter(inotifyFileDescriptor, &fileDescriptorSet)

    // We wait here until an inotify event is triggered
    let fileDescriptor = select(FD_SETSIZE, &fileDescriptorSet, nil, nil, nil)
    if fileDescriptor > 0 {
        let bufferSize = 1024
        guard let buffer = UnsafeMutableRawPointer(malloc(bufferSize)) else {
            print("Unable to allocate memory for the buffer to read inotify events")
        // If we don't read inotify's buffer, then it doesn't get
        // cleared and this triggers the print infinitely
        let _ = read(self.inotifyFileDescriptor, buffer, bufferSize)
        // Trigger the event on the delegate
        print("An event was triggered!")
        // Free the buffer when we're done to prevent memory leaks

// Close the inotify file descriptor so that it stops watching for events
// Set the inotify file descriptor to -1 to prevent accidental reuse
inotifyFileDescriptor = -1

use man inotify or google to learn how to use inotify


Stars: 2


Used By

Total: 1


3.0.0 - 2017-11-17 00:02:01

1.1.1 - 2017-07-05 23:18:06

Added the CSelect package as a dependency

1.1.0 - 2017-05-08 17:13:36

Thanks to @felix91gr for making this not dependent on the fully hardcoded path to inotify on Ubuntu 16.04! Everything still works the same, but this should definitely work on other linux platforms now.

Removed link - 2017-03-11 00:59:17

No need to link to the nonexistent inotify library

1.0.0 - 2017-03-09 01:03:35

Main release. Added README

0.1.0 - 2017-03-09 00:11:28

First attempt