Giter Club home page Giter Club logo

inotify-cpp's Introduction

Inotify-cpp

Inotify-cpp is a C++ wrapper for linux inotify. It lets you watch for filesystem events on your filesystem tree. The following usage example shows the implementation of a simple filesystem event watcher for the commandline.

Usage

#include <inotify-cpp/NotifierBuilder.h>

#include <filesystem>

#include <iostream>
#include <thread>
#include <chrono>

using namespace inotify;

int main(int argc, char** argv)
{
  if (argc <= 1) {
      std::cout << "Usage: ./inotify_example /path/to/dir" << std::endl;
      exit(0);
  }

  // Parse the directory to watch
  std::filesystem::path path(argv[1]);

  // Set the event handler which will be used to process particular events
  auto handleNotification = [&](Notification notification) {
      std::cout << "Event " << notification.event << " on " << notification.path << " at "
                << notification.time.time_since_epoch().count() << " was triggered." << std::endl;
  };

  // Set the a separate unexpected event handler for all other events. An exception is thrown by
  // default.
  auto handleUnexpectedNotification = [](Notification notification) {
      std::cout << "Event " << notification.event << " on " << notification.path << " at "
                << notification.time.time_since_epoch().count()
                << " was triggered, but was not expected" << std::endl;
  };

  // Set the events to be notified for
  auto events = { Event::open | Event::is_dir, // some events occur in combinations
                  Event::access,
                  Event::create,
                  Event::modify,
                  Event::remove,
                  Event::move };

  // The notifier is configured to watch the parsed path for the defined events. Particular files
  // or paths can be ignored(once).
  auto notifier = BuildNotifier()
                      .watchPathRecursively(path)
                      .ignoreFileOnce("fileIgnoredOnce")
                      .ignoreFile("fileIgnored")
                      .onEvents(events, handleNotification)
                      .onUnexpectedEvent(handleUnexpectedNotification);

  // The event loop is started in a separate thread context.
  std::thread thread([&](){ notifier.run(); });

  // Terminate the event loop after 60 seconds
  std::this_thread::sleep_for(std::chrono::seconds(60));
  notifier.stop();
  thread.join();
  return 0;
}

Build Library

mkdir build; cd bulid
cmake -DCMAKE_INSTALL_PREFIX:PATH=/usr ..
make

# run tests
cmake INOTIFY_BUILD_TESTS=ON ..
ctest -VV

# install the library
cmake INOTIFY_BUILD_SHARED_LIBS=ON ..
make && make install

Build Example

Build and install the library before you run the following commands:

mkdir build; cd build
cmake INOTIFY_BUILD_EXAMPLES=ON ../example
cmake --build inotify_example
./inotify_example

Dependencies

  • lib
    • c++17
    • linux 2.6.13
  • build
    • cmake 3.8

Licence

MIT

Author

Written by Erik Zenker ([email protected])

Thanks for Contribution

inotify-cpp's People

Contributors

erikzenker avatar spelcaster avatar james-neill avatar 314159 avatar

Stargazers

 avatar

Watchers

 avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.