mkdir question. 
Author Message
 mkdir question.

Is there a more clever way to test if a group of directories exist and
create then otherwise?

Running something like:
        sprintf(dir_base, "%s/Finput", spool_directory);
        mkdir( dir_base, S_IRWXU);
        sprintf(dir_base, "%s/Finput/%c", spool_directory, message_subdir);
        mkdir( dir_base, S_IRWXU);
all the time is one option, but that's two function calls and after a
while all the dirs will be created and the calls will be a waste of
time.

Doing something like:
        sprintf(dir_base, "%s/Finput", spool_directory);
        if ( stat ( dir_base, &stat_out) == -1 ) {
          mkdir( dir_base, S_IRWXU);
        }
        sprintf(dir_base, "%s/Finput/%c", spool_directory, message_subdir);
        if ( stat ( dir_base, &stat_out) == -1 ) {
          mkdir( dir_base, S_IRWXU);
        }
is a touch better, but still looks ugly(tm)...

Any idea on how to make this more efficent?

PS: for those who wonder, I am hacking the exim (www.exim.org) source
code to allow frozen mails to be moved out the main spool.
--
                                ... abayo ...
yann at kierun.freeserve.co.uk                  No SPAM will be tolerated
--



Sun, 24 Mar 2002 03:00:00 GMT  
 mkdir question.

Quote:

>Is there a more clever way to test if a group of directories exist and
>create then otherwise?

>Running something like:
>    sprintf(dir_base, "%s/Finput", spool_directory);
>    mkdir( dir_base, S_IRWXU);
>    sprintf(dir_base, "%s/Finput/%c", spool_directory, message_subdir);
>    mkdir( dir_base, S_IRWXU);
>all the time is one option, but that's two function calls and after a
>while all the dirs will be created and the calls will be a waste of
>time.

No, this is fine.

Quote:
>Doing something like:
>    sprintf(dir_base, "%s/Finput", spool_directory);
>    if ( stat ( dir_base, &stat_out) == -1 ) {

             /* now someone or something else is creating the directory */

Quote:
>      mkdir( dir_base, S_IRWXU);
>    }
[...]
>Any idea on how to make this more efficent?

No, as to test whether the directory exists is probably going to require
about the same time +/- 10ns that to just create them. In addition just
calling mkdir() avoids the problem above and if the directories do not
exist the waste of time becomes even worse.

One minor hint, it is often faster not to use a full path but to
chdir() if you want to create subdirectories (as Finput/a, Finput/b)
but this is OT, aehm.
[...]

Ta',
Juergen

--
\ Real name     : Jrgen Heinzl                 \       no flames      /

--



Mon, 25 Mar 2002 03:00:00 GMT  
 mkdir question.

Quote:

> Is there a more clever way to test if a group of directories exist and
> create then otherwise?

How about

#include <errno.h>

if (mkdir(name, mode) != 0 && errno != EEXIST)
{
   /* we've got problems */

Quote:
}

Surely the possibly unnecessary creation attempt won't use that much time,
will it?

--
Eric Amick
Columbia, MD

--



Mon, 25 Mar 2002 03:00:00 GMT  
 mkdir question.

Quote:

> Surely the possibly unnecessary creation attempt won't use that much time,
> will it?

The trip through namei in the kernel is what is expensive.  You make
the same trip with mkdir as with stat, so if you use stat you simply
double the effort (modulo inode caching).  Just call mkdir and deal
with the result.

D

--
===============================================================================

http://www-cs-students.stanford.edu/~dsmythe/home.html
--



Mon, 25 Mar 2002 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Mkdir in C

2. linux - mkdir() system call

3. VC 1.52 mkdir problem

4. VC 1.52c - Problem with mkdir

5. - mkdir, cd, rmdir in C ?

6. HELP.. mkdir return code??

7. Anything like "mkdir" in C?

8. question question question

9. This is a biztalk question but probably an easy question for you C# guru's

10. Non-Programming Question... experience question mostly

11. Not C program question, historical question

12. A question regarding FAQs question 12.2

 

 
Powered by phpBB® Forum Software