Solved

can i delete/remove file using file descriptor

Posted on 2003-12-04
5
490 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Is Your Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.

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…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

623 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