From e80a7f6447e6b1bf3ec44df83d86eeab6390ea3c Mon Sep 17 00:00:00 2001 From: Mike Frysinger Date: Wed, 2 Mar 2011 01:18:02 +0000 Subject: optimize a bit, and make sure we dont need a trailing slash for the last dir component --- libq/xmkdir.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'libq/xmkdir.c') diff --git a/libq/xmkdir.c b/libq/xmkdir.c index 9cb1227..a82edd0 100644 --- a/libq/xmkdir.c +++ b/libq/xmkdir.c @@ -3,17 +3,25 @@ static int mkdir_p(const char *path, mode_t mode) { char *_p, *p, *s; + /* Assume that most of the time, only the last element + * is missing. So if we can mkdir it right away, bail. */ + if (mkdir(path, mode) == 0 || errno == EEXIST) + return 0; + + /* Build up the whole tree */ _p = p = xstrdup(path); - while (1) { + while (*p) { /* Skip duplicate slashes */ while (*p == '/') ++p; /* Find the next path element */ s = strchr(p, '/'); - if (!s) + if (!s) { + mkdir(_p, mode); break; + } /* Make it */ *s = '\0'; -- cgit v1.2.3-18-g5258