Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve the full path name from inotify_event

Tags:

c

linux

inotify

The inotify_event struct looks like this :

struct inotify_event {
int      wd;       /* Watch descriptor */
uint32_t mask;     /* Mask of events */
uint32_t cookie;   /* Unique cookie associating related
                      events (for rename(2)) */
uint32_t len;      /* Size of name field */
char     name[];   /* Optional null-terminated name */

};

The name part stores only the file name(not the path to the file). How do we get the fully qualified path from the inotify_event struct or do I have to wrap my own struct around it ?

Edit: I wait for the events for around 2s and then process it on one go.I maintain a queue of events. My question is whether I can get the complete path to my file from the inotify_event struct only ?

The number of events per second is large.

like image 780
bsd Avatar asked Feb 16 '12 14:02

bsd


People also ask

How do I find the full path in Linux?

To obtain the full path of a file, we use the readlink command. readlink prints the absolute path of a symbolic link, but as a side-effect, it also prints the absolute path for a relative path. In the case of the first command, readlink resolves the relative path of foo/ to the absolute path of /home/example/foo/.

How do I find the absolute path of a file?

You can determine the absolute path of any file in Windows by right-clicking a file and then clicking Properties. In the file properties first look at the "Location:" which is the path to the file.

How do I copy a path in Linux?

In order to copy a directory on Linux, you have to execute the “cp” command with the “-R” option for recursive and specify the source and destination directories to be copied.

What is Inotify used for?

inotify (inode notify) is a Linux kernel subsystem created by John McCutchan, which monitors changes to the filesystem, and reports those changes to applications. It can be used to automatically update directory views, reload configuration files, log changes, backup, synchronize, and upload.


2 Answers

There are two options:

  1. You're watching a file. You have passed its name to inotify_add_watch, and got a watch descriptor. If you get an event, you can figure out the file by the watch descriptor.

  2. You're watching a directory. Again, you have passed the directory name to inotify_add_watch, and can find which name it was, based on the watch ID. Now inotify_event.name contains the file name. Concatenate the two and you're done.

like image 178
ugoren Avatar answered Nov 05 '22 15:11

ugoren


I think you need to keep track of each of the watch descriptors as you add them in some structure (all with the full path when you add it). Then, when you receive an event, look up the full path in that structure by using the wd in the event.

wd identifies the watch for which this event occurs. It is one of the watch descriptors returned by a previous call to inotify_add_watch(2).

http://www.kernel.org/doc/man-pages/online/pages/man7/inotify.7.html

Hope that helps...

like image 24
Homer6 Avatar answered Nov 05 '22 15:11

Homer6