Swiftpack.co - Package - Ponyboy47/Cinotify

inotify

inotify module for swift

Allows using the GNU inotify library in swift.

Installation

Add the following dependency to your Package.swift

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

Usage

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_zero(&fileDescriptorSet)
    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")
            return
        }
        // 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
        free(buffer)
    }
}

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

use man inotify or google to learn how to use inotify

Github

link
Stars: 1
Help us keep the lights on

Dependencies

Used By

Total: 1

Releases

3.0.0 - Nov 17, 2017

1.1.1 - Jul 5, 2017

Added the CSelect package as a dependency

1.1.0 - May 8, 2017

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.

1.0.1 - Mar 11, 2017

No need to link to the nonexistent inotify library

1.0.0 - Mar 9, 2017

Main release. Added README