What is the proper way to use inotify?

dubila :

I want to use the inotify mechanism on Linux. I want my application to know when a file aaa was changed. Can you please provide me with a sample how to do that?

joschi :

The inotify C API

inotify provides three system calls to build file system monitors of all kinds:

  • inotify_init() creates an instance of the inotify subsystem in the kernel and returns a file descriptor on success and -1 on failure. Like other system calls, if inotify_init() fails, check errno for diagnostics.
  • inotify_add_watch(), as its name implies, adds a watch. Each watch must provide a pathname and a list of pertinent events, where each event is specified by a constant, such as IN_MODIFY. To monitor more than one event, simply use the logical or — the pipe (|) operator in C—between each event. If inotify_add_watch() succeeds, the call returns a unique identifier for the registered watch; otherwise, it returns -1. Use the identifier to alter or remove the associated watch.
  • inotify_rm_watch() removes a watch.

The read() and close() system calls are also needed. Given the descriptor yielded by inotify_init(), call read() to wait for alerts. Assuming a typical file descriptor, the application blocks pending the receipt of events, which are expressed as data in the stream. The common close() on the file descriptor yielded from inotify_init() deletes and frees all active watches as well as all memory associated with the inotify instance. (The typical reference count caveat applies here, too. All file descriptors associated with an instance must be closed before the memory consumed by the watches and by inotify is freed.)

#include "inotify.h"  
#include "inotify-syscalls.h"  
int wd;   
wd = inotify_add_watch (fd,   
            "/home/rlove/Desktop", IN_MODIFY | IN_CREATE | IN_DELETE);
if (wd < 0)
      perror ("inotify_add_watch");

This example adds a watch on the directory /home/rlove/Desktop for any modifications, file creations or file deletions.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

What is the proper way to use continue?

What is the proper way to use if _ is _ or .isKind(of: )

What is the proper way to use IF THEN in AQL?

What is the proper way to use @property in python

what is the "proper" way to use django REST framework?

What is the proper way to use a .equals method in Java?

What is the proper way to use an alternative binary

What is the proper way to use multiple layouts in ReactJS

What is the proper way to use bit array in Rust?

What is the proper way to use Toolbar and SwipeRefreshLayout?

What is the proper way to use React Memo with Flow?

what is the proper way to use $nin operator with mongoDB

What is the proper/right way to use Async Storage?

What is a Proper way to use Input range listener

What is the proper way to use codecs' encoding in Python?

What's the proper way to use Coroutines in Activity?

In BeautifulSoup, what's the proper way to use a strainer with lxml parsing?

What is proper way to use react hooks when calling javascript functions?

What is the proper way to use descriptors as fields in Python dataclasses?

NodeJS: Request Library - What is the Proper way to use userQueryString?

What is the proper way to use only variables that contain data in Node JS?

What is the proper way to use jQuery to trigger an event on click or focus?

What is the proper way to use the node.js postgresql module?

What is the proper way to use Spring WebSocketConnectionManager when sessions closes

What is the proper way to use the `cfg!` macro to choose between multiple implementations?

What is the proper way to use `pkg-config` from `cmake`?

What is the proper way to use a Logger in a Serializable Java class?

What is the proper way to use Promise.reject with javascript

What is the proper way to use Nat/Natural in a singletons data type?