-1

I am working on a monitoring project using inotify instance. I searched online to see if there is any way to monitor if the file or directory name is renamed.

If it is not possible in inotify instance, how can I monitor a file or a directory in C that its name is changed?

Sky wifibrand
  • 109
  • 1
  • 6
  • Brainstorming (I have no experience with inotify interface): the name of a file (or directory) is not part of that specific file (or directory)... it's an entry in the directory containing the file so you should monitor the parent directory for changes. – pmg Feb 27 '21 at 10:35
  • @pmg How can I monitor file or directory name in C? – Sky wifibrand Feb 27 '21 at 10:37
  • I have no experience with the inotify interface ==> see the man pages: https://man7.org/linux/man-pages/man7/inotify.7.html. – pmg Feb 27 '21 at 10:39
  • C may not be the right tool for this, because inevitably you'll end up doing syscalls , so why not use a shell script ? – An Ant Feb 27 '21 at 11:06
  • Trivially , if you knew the name already , you could `fopen(name, "rb")` every so often until it fails, then you'll know it has been deleted/renamed or possibly some other problem. – An Ant Feb 27 '21 at 11:08
  • @AnAnt How to do this in a directory also? – Sky wifibrand Feb 27 '21 at 11:14
  • @Skywifibrand directories are just special files, at least as far as *NIX systems are concerned. the same fopen should work , AFAIK . ~I'll try it an get back in a sec~ yes this works (on macOS, and should everywhere that is POSIX compliant) – An Ant Feb 27 '21 at 11:17
  • 1
    Iirc, you need to watch for `IN_MOVE_SELF` events. – Shawn Feb 27 '21 at 11:21
  • What do you mean? If `/p/a/t/h` exists and is a regular file and I execute `ln /p/a/t/h /p/a/t/n`, has the name of the file changed? There used to be N links to the file, and now there are N + 1. If I then execute `ln /p/a/t/n /p/a/z/h`, there are N + 2 links to the file. Now, `rm /p/a/t/h /p/a/t/n`. The basename of the file is `h`, as it was at the start. When did the name of the file change? Has it changed? Entries in a directory are names; they are not files. – William Pursell Feb 27 '21 at 13:51

1 Answers1

1

Add a watch specifying the path to the directory the file or directory is in, for IN_MOVE_FROM and IN_MOVED_TO events. Whenever you receive such an event, compare the event name field to the file or directory name(s) you are interested in.

In general, you probably end up watching for IN_CLOSE_WRITE and IN_MOVED_TO events and their name fields if one of the interesting files have been created or modified; and IN_DELETE and IN_MOVED_FROM events if an interesting file vanishes.

Inotify events are anchored in the parent directory inode. That is, if a directory you have a watch descriptor for is moved, you get an IN_MOVE_SELF event, but you keep receiving events for files and directories inside it even though it now resides somewhere else.

If you are interested in specific paths, regardless of whether they exist when the program is started or not – you cannot set a watch on a directory that does not exist yet – you'll need to create a watch descriptor for every directory along that path up to the root directory, like a chain. Some of these you may need to create dynamically. If one of them is deleted or moved, IN_DELETE_SELF or IN_MOVE_SELF, it and all its dependent watch descriptors are to be deleted. Whenever an existing directory gets an IN_CREATE or IN_MOVED_TO event with name being the name of an interesting subdirectory, you create a watch for it; but you also need to scan the subdirectory tree for further interesting sub-subdirectories, because those may already exist, or may have been created between the time you got this event and when you added a watch to the new subdirectory.

Because of that complexity, it is easier to use "flat" tree hierarchy, where you monitor a set of fixed directories that are required to exist when the program starts. (That is, you monitor those directories for new/deleted files and directories, but not their subdirectories.)

Glärbo
  • 146
  • 2