Re: File name from stream

From: Casper H.S. Dik (Casper.Dik_at_Sun.COM)
Date: 04/27/05


Date: 27 Apr 2005 09:28:21 GMT

James Antill <james-netnews@and.org> writes:

>On Tue, 26 Apr 2005 23:25:23 +0200, Pascal Bourguignon wrote:

>> Måns Rullgård <mru@inprovide.com> writes:
>>
>>> Pascal Bourguignon <pjb@informatimago.com> writes:
>>>> If there's only one file system mounted:
>>>>
>>>> struct stat stat;
>>>> int res=fstat(fileno(stream),&stat);
>>>> char buffer[100];
>>>> FILE* names=popen(sprintf(buffer,"find / -inode %d -print",stat.st_ino),"r");
>>>> while(!feof(names)){
>>>> char name[10000];
>>>> if(fgets(name,sizeof(name),names)){
>>>> printf("%s\n",name); }}
>>>>
>>>> Otherwise, use st_dev to identify the mountpoint and use find $mp -xdev ...
>>>
>>> While this will work, it is by no means efficient. On Linux systems,
>>> a quicker way is to look in /proc/self/fd
>>
>> Quicker, but the point is that find may find 0, 1, or more names!
>> What will /proc/self/fd show when the file has been deleted?
>>
>> It's always possible to do things faster when you don't care for the bugs.

> The find method may well find 0, 1 or more names as it cannot atomically
>check the entire FS (although an FS that supports snapshots will work
>around that).

> The /proc/self/fd method will always produce 0 or 1 valid names ... which
>is not only faster but less buggy.

But not entirely portable (Solaris uses /proc/self/fd for real files;
and /proc/self/path for pathnames-as-symlinks)

Casper

-- 
Expressed in this posting are my opinions.  They are in no way related
to opinions held by my employer, Sun Microsystems.
Statements on Sun products included here are not gospel and may
be fiction rather than truth.

Quantcast