You are not logged in.
Im working on application, which scan some folder for new files. I need to obtain if new file in folder is used by other process. For example, when user will copy large file to this folder it may take some time. If my program will detect, that there is new file in folder I'm must be sure if no other process is using it, before I'll try to modify it.
I had read GLibc documentation and I have some questions:
1. If other process had open file in RW mode, would I be able to open this file in RW mode too? (it doesn't matter if I'll open it using low-level 'open ()' or higher 'fopen ()' function). If I want be what error would be returned?
2. Does function 'open lock opened file descriptor automatically? I found that fcntl' can obtain file's lock status via F_GETLK option. I would like to know if file's lock status obtained by 'fcntl' can be useful in my situation?
1. Yes, nothing would prevent multiple processes from opening the same file for
writing at the same time...
2. No, there is no file locking by default... You have to explicitly perform file locking,
if you want it... And, even then, the locking is only advisory/cooperative, so if you
have any app which doesn't bother with locking, it will be able to come in and write
to the file even if you've got it locked... (Some systems may implement some form
of mandatory locking, but it usually requires mounting your filesystem specially
and/or changing perms on the file to something like g+s,g-x...)
If your only goal is to see if anyone currently has a file open at all, you can do things
like "lsof" or "fuser" does... Eg: on a Linux system (and maybe others with "/proc"),
you can just read through "/proc/<pid>/fd/", looking for the file... (Assuming you have
permission to read through all the other processes' dirs, anyway...)
However, using that approach to guarantee exclusive access to the file isn't safe,
if that's your goal... (Because, someone could open the file after you've already
searched their open FDs for it...) For that, you really need locking, and you either
need all processes to cooperatively use advisory locks (ie: guarantee there will
never be a rogue app accessing that file which does no locking), or go through the
hassle of setting up mandatory locks (if your system even supports them at all)...
So I need to find a workaround... I was thinking, that I may check for file's last modification day or it's content last modification date and if file was accessed or modified for the last time, for example, 3 seconds ago it would mean, that I can use it. It's important for me, because i need to change hard link to this file.
Um, that's an even worse and more unsafe method than just scanning "/proc"...
Someone might still have the file open for writing, but not currently be writing
anything to it... Yet, if you go and modify it, and THEN they write to it, you can end
up trashing each other's changes...
Like I said, if you want to ensure exclusive access, you really NEED locking... The
best method is to control all apps that will possibly be accessing the file, and make
sure they all do cooperative advisory locking... Failing that (eg: you have no control
over what apps will be accessing the file), then you need to look into setting up
mandatory locking... Anything else is just a pure kluge, and there will always be a
race condition where it can fail...