Giter VIP home page Giter VIP logo

fswatch's Introduction

fswatch

Highlights

  • Single header file
  • Requires C++17 and std::filesystem
  • MIT License
  • For now, ONLY works in Linux - based on inotify

Quick Start

Simply include fswatch.hpp and you're good to go.

#include <fswatch.hpp>

To start watching files, create an fswatch object and provide a variadic list of directories to watch.

The constructor takes variadic arguments - Simply provide a list of directories to watch. This watcher will observe your home directory, /opt, /tmp and the current working directory.

auto watcher = fswatch("~", "/opt", "/tmp", ".");

try {
  watcher.start();
} catch (const std::runtime_error& error) {
  std::cout << error.what() << std::endl;
}

Register callbacks to events

To add callbacks to events, use the watcher.on(...) method like so:

watcher.on(fswatch::Event::FILE_CREATED, [](auto &event) {
  std::cout << "File created: " << event.path << std::endl;
});

You can register a single callback for multiple events like this:

watcher.on({ fswatch::Event::FILE_OPENED, fswatch::Event::FILE_CLOSED },
  [](auto &event) {
    if (event.type == fswatch::Event::FILE_OPENED)
      std::cout << "File opened: " << event.path << std::endl;
    else
      std::cout << "File closed: " << event.path << std::endl;
});

Here are the list of events that fswatch can handle:

File Events

Event Description
FILE_CREATED File created in watched directory
FILE_OPENED File opened in watched directory
FILE_MODIFIED File modified in watched directory (e.g., write, truncate)
FILE_CLOSED File closed in watched directory
FILE_DELETED File deleted from watched directory

Directory Events

Event Description
DIR_CREATED Directory created in watched directory
DIR_OPENED Directory opened in watched directory (e.g., when running ls)
DIR_MODIFIED Directory modified in watched directory
DIR_CLOSED Directory closed in watched directory
DIR_DELETED Directory deleted from watched directory

Todo

  1. Suppport Win32, FreeBSD, and OSX

fswatch's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

fswatch's Issues

Add support for non existing files

Hi, an awesome library, thanks.

I have a problem when I point a non-existing file.

      wd = inotify_add_watch(fd, root, WATCH_FLAGS);
      // add wd and directory name to Watch map
      watch.insert(-1, root, wd);

In this case the wd is -1 and select waits forever.

When I run fswatch on an additional thread that starts, it cannot be closed by CTRL C. Why is this happening?

#include <fswatch.hpp>
#include <iostream>
#include <thread>
void go()
{
  auto watcher = fswatch("~", "/opt", ".");

  watcher.on(fswatch::Event::FILE_CREATED, [](auto &event) {
    std::cout << "File created: " << event.path << std::endl;
  });

  watcher.on(fswatch::Event::FILE_MODIFIED, [](auto &event) {
    std::cout << "File modified: " << event.path << std::endl;
  });

  watcher.on(fswatch::Event::FILE_DELETED, [](auto &event) {
    std::cout << "File deleted: " << event.path << std::endl;
  });

  try {
    watcher.start();
  } catch (std::filesystem::filesystem_error &error) {
    std::cout << error.what() << std::endl;
  }
}
int main() {
  std::thread t1(go);
  t1.join();
}

Warnings about an unused parameter warnings

Current Behavior

Both gcc-11 and clang-12 will output warnings if the following compiler option is set:

add_compile_options(-Wall -Wextra -Wpedantic)

Outputs:

In file included from main.cpp: fswatch.hpp: In function void sig_callback(int): fswatch.hpp:27:23: warning: unused parameter sig [-Wunused-parameter]
   27 | void sig_callback(int sig) { run = false; }
      |                   ~~~~^~~
fswatch.hpp: In member function void fswatch::start(): fswatch.hpp:170:11: warning: 
unused variable current_event [-Wunused-variable]
  170 |     Event current_event;

Possible Solution

Use c++ standard attribute

[[maybe_unused]](since C++17) | suppresses compiler warnings on unused entities, if any

Pointer to memory no more allocated

At line 197 of fswatch.hpp
const char *root = path.string().c_str();
this is a problem as the pointer will continue to reference a string in memory after it is going to be potentially destroyed. The compiler itself generated a warning about this.
I would suggest the following fix:
auto tmp=path.string(); const char *root = tmp.c_str();

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.