Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > alternative to fstat ?

Reply
Thread Tools

alternative to fstat ?

 
 
solarisss
Guest
Posts: n/a
 
      12-11-2007
I have thousands of files whose exitence needs to be checked.
I think fstat is too costly for this.
Is there any better way for the same ?
 
Reply With Quote
 
 
 
 
Joachim Schmitz
Guest
Posts: n/a
 
      12-11-2007
"solarisss" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed)...
>I have thousands of files whose exitence needs to be checked.
> I think fstat is too costly for this.
> Is there any better way for the same ?

OT here, but in POSIX you'd have stat() (which would save you the fopen())
and access() (which should be much cheaper as it doesn't have to fill the
entire struct stat).

Bye, Jojo


 
Reply With Quote
 
 
 
 
Richard Tobin
Guest
Posts: n/a
 
      12-11-2007
In article <fjleja$lsc$(E-Mail Removed)>,
Joachim Schmitz <(E-Mail Removed)> wrote:

>OT here, but in POSIX you'd have stat() (which would save you the fopen())
>and access() (which should be much cheaper as it doesn't have to fill the
>entire struct stat).


I doubt access() would be significantly cheaper since it still has
to do a system call and get the inode of the file.

-- Richard
--
:wq
 
Reply With Quote
 
Joachim Schmitz
Guest
Posts: n/a
 
      12-11-2007
"Richard Tobin" <(E-Mail Removed)> schrieb im Newsbeitrag
news:fjlrea$1sko$(E-Mail Removed)...
> In article <fjleja$lsc$(E-Mail Removed)>,
> Joachim Schmitz <(E-Mail Removed)> wrote:
>
>>OT here, but in POSIX you'd have stat() (which would save you the fopen())
>>and access() (which should be much cheaper as it doesn't have to fill the
>>entire struct stat).

>
> I doubt access() would be significantly cheaper since it still has
> to do a system call and get the inode of the file.

It is significantly cheaper on a system I frequently work with.

Bye, Jojo


 
Reply With Quote
 
SM Ryan
Guest
Posts: n/a
 
      12-11-2007
solarisss <(E-Mail Removed)> wrote:
# I have thousands of files whose exitence needs to be checked.
# I think fstat is too costly for this.
# Is there any better way for the same ?

stat information is not generally stored as part of the directory;
getting inode information generally generally requires a random
disc access. Some file systems might allow store inodes to improve
reading them, but that would be implementation dependent.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
There are subtler ways of badgering a witness.
 
Reply With Quote
 
Thomas Maier-Komor
Guest
Posts: n/a
 
      12-11-2007
solarisss schrieb:
> I have thousands of files whose exitence needs to be checked.
> I think fstat is too costly for this.
> Is there any better way for the same ?


if the files are located in few common directories, fstatat might be an
option on Solaris. Don't know about other systems...

- Thomas
 
Reply With Quote
 
Kenneth Brody
Guest
Posts: n/a
 
      12-11-2007
Joachim Schmitz wrote:
>
> "solarisss" <(E-Mail Removed)> schrieb im Newsbeitrag
> news:(E-Mail Removed)...
> >I have thousands of files whose exitence needs to be checked.

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> > I think fstat is too costly for this.
> > Is there any better way for the same ?

> OT here, but in POSIX you'd have stat() (which would save you the fopen())
> and access() (which should be much cheaper as it doesn't have to fill the
> entire struct stat).


Well, if you have a file handle to pass to fstat(), I think we can
pretty much agree that the file exists. (Even if POSIX is OT here.)



And, if I recall, a discussion in the past noted that a failed access()
call doesn't necessarily mean the file doesn't exist. (For example,
you may simply not have permission to check for the existence.) Also,
there was a discussion about some sort of "cryptographic filesystem"(?)
in which the existence of a file cannot be determined.

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <(E-Mail Removed)>

 
Reply With Quote
 
Joachim Schmitz
Guest
Posts: n/a
 
      12-11-2007
"Kenneth Brody" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed)...
> Joachim Schmitz wrote:
>>
>> "solarisss" <(E-Mail Removed)> schrieb im Newsbeitrag
>> news:(E-Mail Removed)...
>> >I have thousands of files whose exitence needs to be checked.

> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>> > I think fstat is too costly for this.
>> > Is there any better way for the same ?

>> OT here, but in POSIX you'd have stat() (which would save you the
>> fopen())
>> and access() (which should be much cheaper as it doesn't have to fill the
>> entire struct stat).

>
> Well, if you have a file handle to pass to fstat(), I think we can
> pretty much agree that the file exists. (Even if POSIX is OT here.)
>
>

Oops...

> And, if I recall, a discussion in the past noted that a failed access()
> call doesn't necessarily mean the file doesn't exist. (For example,
> you may simply not have permission to check for the existence.) Also,

Very true, so you would have to check errno in case access() returns a -1.

printf("%s does%s exist\n", filename, ((access(filename) == -1) && (errno==
ENOFILE))? "n't":"");

The same is true for stat BTW(). And for fstat() resp. the open() it needs.

Bye, Jojo


 
Reply With Quote
 
Kenneth Brody
Guest
Posts: n/a
 
      12-11-2007
Joachim Schmitz wrote:
>
> "Kenneth Brody" <(E-Mail Removed)> schrieb im Newsbeitrag
> news:(E-Mail Removed)...
> > Joachim Schmitz wrote:

[... use fstat() to check for file existence ...]
> > Well, if you have a file handle to pass to fstat(), I think we can
> > pretty much agree that the file exists. (Even if POSIX is OT here.)
> >
> >

> Oops...
>
> > And, if I recall, a discussion in the past noted that a failed access()
> > call doesn't necessarily mean the file doesn't exist. (For example,
> > you may simply not have permission to check for the existence.) Also,

> Very true, so you would have to check errno in case access() returns a -1.
>
> printf("%s does%s exist\n", filename, ((access(filename) == -1) && (errno==
> ENOFILE))? "n't":"");
>
> The same is true for stat BTW(). And for fstat() resp. the open() it needs.


(I'm not sure why we're discussing POSIX here, but...)

What if the file doesn't exist in a directory to which you do not
have the proper permissions? I believe the error will be EPERM
(or, at least, not ENOFILE), and you will display "does exist".

The point is, an error other than ENOFILE does not imply that the
file does exist. I believe the same holds true for stat() et al.
The only thing you can reliably check for is whether you can open
the file, by seeing what fopen() returns. (Yes, you can use
something like access() to see if you have permission to a file
_at_the_time_you_call_access()_, but you have race conditions to
take into account.)

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <(E-Mail Removed)>

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      12-11-2007
solarisss <(E-Mail Removed)> writes:
> I have thousands of files whose exitence needs to be checked.
> I think fstat is too costly for this.
> Is there any better way for the same ?


Since fstat() is defined by POSIX, I suggest that comp.unix.programmer
would provide better answers to your question.

--
Keith Thompson (The_Other_Keith) <(E-Mail Removed)>
Looking for software development work in the San Diego area.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
jython newbee general question docu / os.fstat Mark Fink Python 1 02-05-2006 11:23 AM
Quicktime Alternative, RealPlayer Alternative & Media Player Classic John Capleton Computer Support 3 12-05-2005 07:41 AM
WPS DDK Updates or alternative? glenn Wireless Networking 1 11-06-2005 03:20 PM
fstat Martijn C Programming 2 01-11-2004 04:56 PM
fstat for an open fstream =?ISO-8859-1?Q?Klaus_F=FCller?= C++ 4 09-26-2003 11:39 PM



Advertisments