Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 511
  • Last Modified:

can i delete/remove file using file descriptor

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
trinabh
Asked:
trinabh
1 Solution
 
sunnycoderCommented:
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
 
Karl Heinz KremerCommented:
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
 
ahoffmannCommented:
a file descriptor is only unique to a process
so go with sunnycoder's suggestion or debug into your process
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now