Solved

can i delete/remove file using file descriptor

Posted on 2003-12-04
5
479 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
Comment Utility
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
Comment Utility
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
Comment Utility
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 Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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 fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now