LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-13-2008, 04:12 AM   #1
Yogish
LQ Newbie
 
Registered: Aug 2008
Posts: 4

Rep: Reputation: 0
Unhappy "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?

Thanks,
Yogish
 
Old 08-13-2008, 04:30 AM   #2
burschik
Member
 
Registered: Jul 2008
Posts: 159

Rep: Reputation: 31
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.
 
Old 08-13-2008, 04:32 AM   #3
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
man fuser?

What's wrong with lsof?
 
Old 08-13-2008, 05:17 AM   #4
chakka.lokesh
Member
 
Registered: Mar 2008
Distribution: Ubuntu
Posts: 270

Rep: Reputation: 33
I think he is asking about any function in c-language rather than a command / tool.
 
Old 08-13-2008, 07:09 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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....
 
Old 08-13-2008, 11:21 PM   #6
chakka.lokesh
Member
 
Registered: Mar 2008
Distribution: Ubuntu
Posts: 270

Rep: Reputation: 33
Quote:
Originally Posted by chrism01 View Post
1. you check if any files are open.
hey chrism01

he is asking how to do that?
 
Old 08-14-2008, 02:20 AM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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.
 
Old 08-15-2008, 01:03 AM   #8
chakka.lokesh
Member
 
Registered: Mar 2008
Distribution: Ubuntu
Posts: 270

Rep: Reputation: 33
Quote:
Originally Posted by chrism01 View Post
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Standard commands give "-bash: open: command not found" even in "su -" and "su root" mibo12 Linux - General 4 11-11-2007 10:18 PM
(URGENT) Fedora Core 6 - Could not find logical volume "VolGroup00" linxnoob Linux - General 3 07-07-2007 05:36 AM
Urgent: "Warning: unable to open an initial console." TGWDNGHN Linux From Scratch 7 12-28-2005 06:08 PM
"open with" mplayer can't find file rocksniffer Linux - Software 2 08-26-2005 11:15 PM
1. shell script "find and replace" on text 2. java GUI application randomx Programming 4 03-05-2004 01:01 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:45 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration