473,395 Members | 1,969 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Is it possible to get a file name from the file descriptor returnedby Low-Level I/O API open?

I know it's very strange to do that since we have the file name when
we call:
int open(const char *pathname, int oflag,...);
And we can store the file name for later usage.

But I just wonder if we can get a file name from the file descriptor
(on Windows/Linux), then the solution may looks simpler (if it's not
so hard to get the file name, :) ).
Sep 9 '08 #1
9 9268
not all news readers display the subject in an easily accessible
manner.
Please include the subject in the body of your post

Subject: Is it possible to get a file name from the file descriptor
returned by Low-Level I/O API open?
On 9 Sep, 08:17, Bill David <billdavi...@gmail.comwrote:
I know it's very strange to do that since we have the file name when
we call:
int open(const char *pathname, int oflag,...);
this isn't standard C

And we can store the file name for later usage.

But I just wonder if we can get a file name from the file descriptor
(on Windows/Linux), then the solution may looks simpler (if it's not
so hard to get the file name, :) ).
You need to ask in a ng specific to the platform(s) you are interested
in.

This is a a comp.lang.c FAQ

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

It doesn't give good news...
--
Nick Keighley

The beginning of wisdom for a [software engineer] is to recognize the
difference between getting a program to work, and getting it right.
-- M A Jackson, 1975
Sep 9 '08 #2
>I know it's very strange to do that since we have the file name when
>we call:
int open(const char *pathname, int oflag,...);
And we can store the file name for later usage.
The POSIX API is more on-topic in comp.unix.programmer. Standard
C doesn't define an open() call, and not all systems are POSIX.
>But I just wonder if we can get a file name from the file descriptor
(on Windows/Linux), then the solution may looks simpler (if it's not
so hard to get the file name, :) ).
First of all, under POSIX, there isn't *THE* name: a given open
file may have zero or more names. It might have zero if the name
has been remove()d or rename()d over, but it's still open. It might
have several names due to hard links. That's not even counting
symlinks and variant pathnames involving /../ in the path which
might have been used in the open() call.

Under POSIX, it is possible, *if* you have the permissions necessary,
to find some name(s), if any, assuming they are not changing out
from under you. However, nobody said it would be efficient.
On a certain large newsserver, treewalking one filesystem looking
for a particular inode could take *hours*. If it was also handling
news readers at the time, it might take a whole day.

The following procedure works on POSIX, but I'm not sure about
Windows, especially with a FAT or NTFS filesystem:

fstat() the open file. Note the st_dev and st_ino fields returned.
Treewalk the entire file system. Find names with the same st_dev
and st_ino fields. These are the names for the file, or at least
were when they were found. Note that there are some shortcuts (but
system-dependent, even for POSIX) you can use with mount points and
the mount table to only treewalk the relevant filesystem, rather
than the whole system.

Sep 9 '08 #3
Nick Keighley <ni******************@hotmail.comwrites:
not all news readers display the subject in an easily accessible
manner.
Utter nonsense. The subject is the subject of the contents. If your news
reader does not properly display the Subject then I suggest you find one
that does.
Please include the subject in the body of your post

Subject: Is it possible to get a file name from the file descriptor
returned by Low-Level I/O API open?
Please do no top post.
>

On 9 Sep, 08:17, Bill David <billdavi...@gmail.comwrote:
>I know it's very strange to do that since we have the file name when
we call:
int open(const char *pathname, int oflag,...);

this isn't standard C

>And we can store the file name for later usage.

But I just wonder if we can get a file name from the file descriptor
(on Windows/Linux), then the solution may looks simpler (if it's not
so hard to get the file name, :) ).

You need to ask in a ng specific to the platform(s) you are interested
in.

This is a a comp.lang.c FAQ

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

It doesn't give good news...
--
Sep 9 '08 #4
Gordon Burditt wrote:
[some scheme relying on POSIX only]
I'd like to add a scheme, that works for Linux:
The directory /proc/$PID/fd/ contains symlinks with numeric
names, which are the open file descriptors. And the symlinks
point to the filename that was originally opened with open. Of
course if the file has been unlinked in the meantime, the
symlink is broken. But you can still retrieve a name.

BTW /proc/self is a shortcut for /proc/$PID, so that you don't
have to find out the current process' PID.

Wolfgang Draxinger
--
E-Mail address works, Jabber: he******@jabber.org, ICQ: 134682867

Sep 9 '08 #5
In article <72**********************************@p25g2000hsf. googlegroups.com>,
Nick Keighley <ni******************@hotmail.comwrote:
>not all news readers display the subject in an easily accessible
manner.
This seems to fall into the same category as implementations of malloc()
that delete all your files.

If your newsreader doesn't display the subject clearly, get another
newsreader!

-- Richard
--
Please remember to mention me / in tapes you leave behind.
Sep 9 '08 #6
On 9 Sep, 11:27, Richard<rgr...@gmail.comwrote:
Nick Keighley <nick_keighley_nos...@hotmail.comwrites:
not all news readers display the subject in an easily accessible
manner.

Utter nonsense. The subject is the subject of the contents. If your news
reader does not properly display the Subject then I suggest you find one
that does.
Please include the subject in the body of your post
Subject: Is it possible to get a file name from the file descriptor
returned by Low-Level I/O API open?

Please do no top post.
I top-post information that is addressed to the post itself
rather than its content.

On 9 Sep, 08:17, Bill David <billdavi...@gmail.comwrote:
I know it's very strange to do that since we have the file name when
we call:
int open(const char *pathname, int oflag,...);
this isn't standard C
And we can store the file name for later usage.
But I just wonder if we can get a file name from the file descriptor
(on Windows/Linux), then the solution may looks simpler (if it's not
so hard to get the file name, :) ).
You need to ask in a ng specific to the platform(s) you are interested
in.
This is a a comp.lang.c FAQ
FAQ 19.15 *"How can I recover the file name given an open stream or
file
descriptor?"
It doesn't give good news...

--
Nick Keighley
Infinitely many bits doesn't give you "100% accuracy". You will
only be able to represent the algebraic numbers.

Sep 9 '08 #7
On Sep 9, 3:04 pm, Nick Keighley <nick_keighley_nos...@hotmail.com>
wrote:
On 9 Sep, 11:27, Richard the fool <rgr...@gmail.comwrote:
Nick Keighley <nick_keighley_nos...@hotmail.comwrites:
not all news readers display the subject in an easily accessible
manner.
Utter nonsense. The subject is the subject of the contents. If your news
reader does not properly display the Subject then I suggest you find one
that does.
Please include the subject in the body of your post
Subject: Is it possible to get a file name from the file descriptor
returned by Low-Level I/O API open?
Please do no top post.

I top-post information that is addressed to the post itself
rather than its content.
You did not top post. Your post was fine, and your observation
regarding the subject field accurate. Richard was trolling once more.
Sep 9 '08 #8
Gordon Burditt wrote, On 09/09/08 09:42:

<snip>
fstat() the open file. Note the st_dev and st_ino fields returned.
Treewalk the entire file system. Find names with the same st_dev
and st_ino fields. These are the names for the file, or at least
were when they were found. Note that there are some shortcuts (but
system-dependent, even for POSIX) you can use with mount points and
the mount table to only treewalk the relevant filesystem, rather
than the whole system.
That shortcut could prevent you from finding valid names for a file
under Linux. In fact, I administer two servers where it is guaranteed
that you would not find all of the names for a file using that method.
--
Flash Gordon
Sep 9 '08 #9
Bill David wrote:
>
I know it's very strange to do that since we have the file name
when we call:
int open(const char *pathname, int oflag,...);
And we can store the file name for later usage.

But I just wonder if we can get a file name from the file
descriptor (on Windows/Linux), then the solution may looks
simpler (if it's not so hard to get the file name, :) ).
No you can't. Unix/Linux filesystems allow files to have multiple
names, and the name by which selected is abandoned once the file is
open. Read up on the structure of those file systems. If you want
the name, remember it when you open the file. But remember that
name may no longer be valid; someone could rename the file while
you have it open!!!

If you want to post a followup via groups.google.com, ensure
you quote enough for the article to make sense. Google is only
an interface to Usenet; it's not Usenet itself. Don't assume
your readers can, or ever will, see any previous articles.

More details at: <http://cfaj.freeshell.org/google/>
--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
Sep 10 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: J Peterman | last post by:
I need to do this exercise, but am having problems. I need to write a program that firstly, sleeps for 5 seconds, then reads a line of input from file descriptor 0 and then writes the line back...
9
by: Ben Dewey | last post by:
Project: ---------------------------- I am creating a HTTPS File Transfer App using ASP.NET and C#. I am utilizing ActiveDirectory and windows security to manage the permissions. Why reinvent...
2
by: John Regan | last post by:
Hello All I am trying to find the owner of a file or folder on our network (Windows 2000 Server) using VB.Net and/or API. so I can search for Folders that don't follow our company's specified...
1
by: Geoff Cox | last post by:
Hello, I have a few hundred zip files (each one has 1 MS Word doc in it) in the c:\docs folder I run the following script and get "bad file descriptor" after the first doc file has been...
7
by: news | last post by:
Recently our mail from our e-commerce site has been rejected by AOL due to an IP block because someone was using our PHP scripts to send spam. Well, I got that fixed. But our legitimate...
2
by: levimc | last post by:
I know that that topic may be old to you but I looked at other more- than-two-year-old topics related to mine. However, I didn't find them working for my project at all because its errors return...
3
by: Yang | last post by:
Hi, I'm experiencing a problem when trying to close the file descriptor for a socket, creating another socket, and then closing the file descriptor for that second socket. I can't tell if my issue...
3
by: akaarj | last post by:
I am using Linux gcc 4.1.1 version I have a file descriptor and I want to have a FILE* pointer to the file from the file descriptor. OR if I can have the file name using the file descriptor.....
3
by: sejal17 | last post by:
hello Can any one tell me how to read multiple worksheets from a single excel file.I have stored that excel in xml file.so i want to read that xml that has multiple worksheet.And i want to store...
3
by: sejal17 | last post by:
hello Can any one tell me how to read multiple worksheets from a single excel file.I have stored that excel in xml file.so i want to read that xml that has multiple worksheet.And i want to store...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.