* Testing if a file or directory exist
@ 2007-09-09 9:26 Holger Kiehl
2007-09-09 10:07 ` Robert P. J. Day
2007-09-09 13:15 ` Glynn Clements
0 siblings, 2 replies; 8+ messages in thread
From: Holger Kiehl @ 2007-09-09 9:26 UTC (permalink / raw)
To: linux-c-programming
Hello
What is the quickest way to test if a file or directory exist.
I can think of three different system calls that can be used:
access(), stat() and open(). Writting a little test program I
found that this is also the order of which is the quickest,
that is access() is the quickest and open() the slowest. The code
for the test programms is shown below.
The question I have is there any other system call that I can use
that would be cheaper then access(). Even if they are linux specific
system calls I would like to know.
Thanks,
Holger
access.c
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#define MAX_LOOPS 5000000
int main(void)
{
unsigned int i;
for (i = 0; i < MAX_LOOPS; i++)
{
if (access("abcd", R_OK) != 0)
{
(void)fprintf(stderr, "access() error : %s\n", strerror(errno));
return 1;
}
}
return 0;
}
stat.c
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#define MAX_LOOPS 5000000
int main(void)
{
unsigned int i;
struct stat stat_buf;
for (i = 0; i < MAX_LOOPS; i++)
{
if (stat("abcd", &stat_buf) == -1)
{
(void)fprintf(stderr, "stat() error : %s\n", strerror(errno));
return 1;
}
}
return 0;
}
open.c
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#define MAX_LOOPS 5000000
int main(void)
{
int fd;
unsigned int i;
for (i = 0; i < MAX_LOOPS; i++)
{
if ((fd = open("abcd", O_RDONLY)) == -1)
{
(void)fprintf(stderr, "open() error : %s\n", strerror(errno));
return 1;
}
else
{
(void)close(fd);
}
}
return 0;
}
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Testing if a file or directory exist
2007-09-09 9:26 Testing if a file or directory exist Holger Kiehl
@ 2007-09-09 10:07 ` Robert P. J. Day
2007-09-09 10:16 ` Benoit Rouits
2007-09-10 15:53 ` Holger Kiehl
2007-09-09 13:15 ` Glynn Clements
1 sibling, 2 replies; 8+ messages in thread
From: Robert P. J. Day @ 2007-09-09 10:07 UTC (permalink / raw)
To: Holger Kiehl; +Cc: linux-c-programming
On Sun, 9 Sep 2007, Holger Kiehl wrote:
> Hello
>
> What is the quickest way to test if a file or directory exist. I can
> think of three different system calls that can be used: access(),
> stat() and open(). Writting a little test program I found that this
> is also the order of which is the quickest, that is access() is the
> quickest and open() the slowest.
if all you want to do is check for existence, then, execution time
notwithstanding, you should use the method which accomplishes that and
nothing more, so the obvious solution would be stat().
it would be illogical to call open() since a side-effect would be that
you then had an open file. in short, if you just want to test, then
just test.
rday
--
========================================================================
Robert P. J. Day
Linux Consulting, Training and Annoying Kernel Pedantry
Waterloo, Ontario, CANADA
http://crashcourse.ca
========================================================================
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Testing if a file or directory exist
2007-09-09 10:07 ` Robert P. J. Day
@ 2007-09-09 10:16 ` Benoit Rouits
2007-09-09 15:04 ` LDB
2007-09-10 15:53 ` Holger Kiehl
1 sibling, 1 reply; 8+ messages in thread
From: Benoit Rouits @ 2007-09-09 10:16 UTC (permalink / raw)
To: linux-c-programming
I think, as information are not needed, even access() is better than
stat() and also uses less memory/cpu.
- ben
Le dimanche 09 septembre 2007 à 06:07 -0400, Robert P. J. Day a écrit :
> if all you want to do is check for existence, then, execution time
> notwithstanding, you should use the method which accomplishes that and
> nothing more, so the obvious solution would be stat().
>
> it would be illogical to call open() since a side-effect would be that
> you then had an open file. in short, if you just want to test, then
> just test.
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Testing if a file or directory exist
2007-09-09 9:26 Testing if a file or directory exist Holger Kiehl
2007-09-09 10:07 ` Robert P. J. Day
@ 2007-09-09 13:15 ` Glynn Clements
2007-09-10 15:39 ` Holger Kiehl
1 sibling, 1 reply; 8+ messages in thread
From: Glynn Clements @ 2007-09-09 13:15 UTC (permalink / raw)
To: Holger Kiehl; +Cc: linux-c-programming
Holger Kiehl wrote:
> What is the quickest way to test if a file or directory exist.
> I can think of three different system calls that can be used:
> access(), stat() and open(). Writting a little test program I
> found that this is also the order of which is the quickest,
> that is access() is the quickest and open() the slowest. The code
> for the test programms is shown below.
>
> The question I have is there any other system call that I can use
> that would be cheaper then access(). Even if they are linux specific
> system calls I would like to know.
>
> Thanks,
> Holger
>
> access.c
>
> #include <stdio.h>
> #include <string.h>
> #include <unistd.h>
> #include <errno.h>
>
> #define MAX_LOOPS 5000000
>
> int main(void)
> {
> unsigned int i;
>
> for (i = 0; i < MAX_LOOPS; i++)
> {
> if (access("abcd", R_OK) != 0)
If you just want to check for existence, use F_OK. If you use R_OK,
the call will fail if you don't have read permission for the file.
OTOH, if access(..., R_OK) succeeds, that doesn't necessarily mean
that opening the file for read will succeed.
--
Glynn Clements <glynn@gclements.plus.com>
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Testing if a file or directory exist
2007-09-09 10:16 ` Benoit Rouits
@ 2007-09-09 15:04 ` LDB
0 siblings, 0 replies; 8+ messages in thread
From: LDB @ 2007-09-09 15:04 UTC (permalink / raw)
Cc: linux-c-programming
Agreed, by definition access() does the following:
access() checks whether the process would be allowed to read, write or test for
existence of the file (or other file system object) whose name is pathname.
LDB
Benoit Rouits wrote:
> I think, as information are not needed, even access() is better than
> stat() and also uses less memory/cpu.
> - ben
> Le dimanche 09 septembre 2007 à 06:07 -0400, Robert P. J. Day a écrit :
>> if all you want to do is check for existence, then, execution time
>> notwithstanding, you should use the method which accomplishes that and
>> nothing more, so the obvious solution would be stat().
>>
>> it would be illogical to call open() since a side-effect would be that
>> you then had an open file. in short, if you just want to test, then
>> just test.
> -
> To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at http://vger.kernel.org/majordomo-info.html
>
>
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Testing if a file or directory exist
2007-09-09 13:15 ` Glynn Clements
@ 2007-09-10 15:39 ` Holger Kiehl
0 siblings, 0 replies; 8+ messages in thread
From: Holger Kiehl @ 2007-09-10 15:39 UTC (permalink / raw)
To: Glynn Clements; +Cc: linux-c-programming
On Sun, 9 Sep 2007, Glynn Clements wrote:
>
> Holger Kiehl wrote:
>
>> What is the quickest way to test if a file or directory exist.
>> I can think of three different system calls that can be used:
>> access(), stat() and open(). Writting a little test program I
>> found that this is also the order of which is the quickest,
>> that is access() is the quickest and open() the slowest. The code
>> for the test programms is shown below.
>>
>> The question I have is there any other system call that I can use
>> that would be cheaper then access(). Even if they are linux specific
>> system calls I would like to know.
>>
>> Thanks,
>> Holger
>>
>> access.c
>>
>> #include <stdio.h>
>> #include <string.h>
>> #include <unistd.h>
>> #include <errno.h>
>>
>> #define MAX_LOOPS 5000000
>>
>> int main(void)
>> {
>> unsigned int i;
>>
>> for (i = 0; i < MAX_LOOPS; i++)
>> {
>> if (access("abcd", R_OK) != 0)
>
> If you just want to check for existence, use F_OK. If you use R_OK,
> the call will fail if you don't have read permission for the file.
>
Right. I thought F_OK was just for files and not directories, but that is
wrong, a directory is also just a file. Also, testing showed that F_OK is
quicker then R_OK.
Thanks a lot for the advice!
Holger
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Testing if a file or directory exist
2007-09-09 10:07 ` Robert P. J. Day
2007-09-09 10:16 ` Benoit Rouits
@ 2007-09-10 15:53 ` Holger Kiehl
2007-09-10 18:01 ` Glynn Clements
1 sibling, 1 reply; 8+ messages in thread
From: Holger Kiehl @ 2007-09-10 15:53 UTC (permalink / raw)
To: linux-c-programming
On Sun, 9 Sep 2007, Robert P. J. Day wrote:
> On Sun, 9 Sep 2007, Holger Kiehl wrote:
>
>> Hello
>>
>> What is the quickest way to test if a file or directory exist. I can
>> think of three different system calls that can be used: access(),
>> stat() and open(). Writting a little test program I found that this
>> is also the order of which is the quickest, that is access() is the
>> quickest and open() the slowest.
>
> if all you want to do is check for existence, then, execution time
> notwithstanding, you should use the method which accomplishes that and
> nothing more, so the obvious solution would be stat().
>
That is what I first thought as well. But I think the problem is that
stat() needs to fill up the structure with all the data, is what takes
most the time. So I thought calling stat() as follows would solve it:
stat("abcd", NULL)
But that would fail with EFAULT (Bad address). I wonder why this is
the case, because then I assume it should be as quick as access("abcd", F_OK)
or maybe even quicker. By providing NULL as argument I tell the function
not to fillup the structure and just test if the file exist. Most proberly
POSIX defines it that way.
Holger
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Testing if a file or directory exist
2007-09-10 15:53 ` Holger Kiehl
@ 2007-09-10 18:01 ` Glynn Clements
0 siblings, 0 replies; 8+ messages in thread
From: Glynn Clements @ 2007-09-10 18:01 UTC (permalink / raw)
To: Holger Kiehl; +Cc: linux-c-programming
Holger Kiehl wrote:
> >> What is the quickest way to test if a file or directory exist. I can
> >> think of three different system calls that can be used: access(),
> >> stat() and open(). Writting a little test program I found that this
> >> is also the order of which is the quickest, that is access() is the
> >> quickest and open() the slowest.
> >
> > if all you want to do is check for existence, then, execution time
> > notwithstanding, you should use the method which accomplishes that and
> > nothing more, so the obvious solution would be stat().
>
> That is what I first thought as well. But I think the problem is that
> stat() needs to fill up the structure with all the data, is what takes
> most the time.
That isn't the case. All of the information returned by stat() is in
one place: the inode. stat() just copies various fields from the inode
into the "struct stat".
Most of the time taken would be in resolving the filename to an inode,
and in the system call overhead.
--
Glynn Clements <glynn@gclements.plus.com>
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2007-09-10 18:01 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-09-09 9:26 Testing if a file or directory exist Holger Kiehl
2007-09-09 10:07 ` Robert P. J. Day
2007-09-09 10:16 ` Benoit Rouits
2007-09-09 15:04 ` LDB
2007-09-10 15:53 ` Holger Kiehl
2007-09-10 18:01 ` Glynn Clements
2007-09-09 13:15 ` Glynn Clements
2007-09-10 15:39 ` Holger Kiehl
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).