Search code examples
c++linuxfanotify

What's the difference between access a file and opening a file


I have this doubt: ¿Wich are the differences between: open a file and access a file?

I'm working with fanotify, and I'm only interested on FAN_ACCESS events. I start my program and when I try to edit some file, the program does what it has to do.

Said that, I have wrote some tests, and some of test cases uses code like this:

filesystem::ofstream acces_to_file;
acces_to_file.open('/tmp/test_file.txt');
acces_to_file <<  "Some text" << endl;
acces_to_file.close();

but surprisingly, this access is no detected by my program. I access the same file from nano (even without restart the so mentioned program) and all works again.

Thats why I think it has to be some difference between access and open a file.

The program is always executed by root user.

I set the flags as following:

fanotify_mark(fd, FAN_MARK_ADD, FAN_ACCESS | FAN_EVENT_ON_CHILD, AT_FDCWD, MONITOR_ROOT_PATH);

Solution

  • In general, opening a file tells the OS you want to operate with the file.

    In general, file access is how you operate with the file: read only, write only, read & write. Some access permission also include sequential or random.