Solved

can i delete/remove file using file descriptor

Posted on 2003-12-04
5
482 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how can i create a game board using shell script 2 78
Linux , how can I inspect a tar GZ file 3 149
how does yum update work 2 58
linux $PWD 2 656
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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

790 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