"URGENT" : How to find whether a file is already open or not by any application .
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
"URGENT" : How to find whether a file is already open or not by any application .
Hi,
I trying to delete a folder containing some files.
Before deleting i want to check whether any file in the folder is already open by any other application.
I cannot use "lsof" command.
Is there any function or api which will help me in this?
As far as I know, lsof uses the information in /proc/$PID/fd (and others) to find open files. You could do the same manually, provided you have the necessary privileges.
Can i just point out you need a locking mechanism, otherwise you could get a race condition.
That is,
1. you check if any files are open.
2. by the time you've checked eg file3, someone has opened file1
3. even if (2.) does not obtain, it takes a finite amt of time to move (in your code) to your unlink() for each file, by which time, again, someone has opened a file....
Actually, he's asking how to check if a file is open, and stating he can't use lsof. At a guess he wants to do it direct in C, although he doesn't specify the lang, which would help.
I was just pointing out the generic logic, which is a lock/race condition. Just checking if a file is open (and he's talking about a bunch so he can delete an entire dir) doesn't mean its safe to delete the dir.
You'd need to check/delete each file as you go to avoid that.
Its a classic prob in file txfr dirs.
I was just pointing out the generic logic, which is a lock/race condition.
my guess is that, the file is opened by entirely different process, probably by different application or some thing like that, but not by the process he is running.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.