Solved

can i delete/remove file using file descriptor

Posted on 2003-12-04
5
483 Views
Last Modified: 2010-04-21
Hi friends
     i want to remove a file, but i have only file descriptor. and any other way , by which i can get file name by file descriptor so unlink that file
Thanks
0
Comment
Question by:trinabh
5 Comments
 
LVL 45

Accepted Solution

by:
sunnycoder earned 125 total points
ID: 9873502
to get file path from fd

ls -l /proc/<pid>/fd

replace <pid> with current process pid
popen this command ... read in the output .... look for fd in 3rd last column ... the last column of the matching row will have the pathname

p = popen ( "ls -l /proc/<pid>/fd" , "r" );

while ( fgets ( buffer, BUF_SIZE, p ) != NULL )
{
       /* parse buffer to get fd*/
       /* compare it with your fd */
      /* if it matches, get the last colum, display and exit*/
}
0
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 9879800
The comp.lang.c FAQ has this to say about your question:

http://www.eskimo.com/~scs/C-faq/q19.15.html:

Question 19.15

How can I recover the file name given an open stream or file descriptor?

This problem is, in general, insoluble. Under Unix, for instance, a scan of the entire disk (perhaps involving special permissions) would theoretically be required, and would fail if the descriptor were connected to a pipe or referred to a deleted file (and could give a misleading answer for a file with multiple links). It is best to remember the names of files yourself when you open them (perhaps with a wrapper function around fopen).

Linux does give you access to this information, but only if you have access to the proc file system. This is however not necessary to run a Linux system. You may (not very often however) encounter a Linux system that is running without the proc fs. Keep this in mind when you program your solution.
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 9889947
a file descriptor is only unique to a process
so go with sunnycoder's suggestion or debug into your process
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question