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.
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/.
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.
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.
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.
There are two options:
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.
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.
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...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With