Annotation of embedaddon/pimd/libite/makepath.c, revision 1.1.1.1

1.1       misho       1: /* mkpath() -- Create all components leading up to a given directory
                      2:  *
                      3:  * Copyright (c) 2013  Joachim Nilsson <troglobit@gmail.com>
                      4:  *
                      5:  * Permission to use, copy, modify, and/or distribute this software for any
                      6:  * purpose with or without fee is hereby granted, provided that the above
                      7:  * copyright notice and this permission notice appear in all copies.
                      8:  *
                      9:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     10:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     11:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     12:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     13:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     14:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     15:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     16:  */
                     17: 
                     18: #include <errno.h>
                     19: #include <libgen.h>
                     20: #include <limits.h>
                     21: #include <stdlib.h>
                     22: #include <string.h>
                     23: #include <sys/stat.h>
                     24: #include <sys/types.h>
                     25: 
                     26: int mkpath(char *dir, mode_t mode)
                     27: {
                     28:        if (!dir) {
                     29:                errno = EINVAL;
                     30:                return 1;
                     31:        }
                     32: 
                     33:        if (strlen(dir) == 1 && dir[0] == '/')
                     34:                return 0;
                     35: 
                     36:        mkpath(dirname(strdupa(dir)), mode);
                     37: 
                     38:        return mkdir(dir, mode);
                     39: }
                     40: 
                     41: /**
                     42:  * makepath - Create all components of the specified directory.
                     43:  * @dir: Directory to create.
                     44:  *
                     45:  * Returns:
                     46:  * POSIX OK (0) on success, otherwise -1 and errno set appropriately.
                     47:  * This function returns EINVAL on bad argument, or ENOMEM when it
                     48:  * fails allocating temporary memory.  For other error codes see the
                     49:  * mkdir() syscall description.
                     50:  */
                     51: int makepath(char *dir)
                     52: {
                     53:        return mkpath(dir, 0777);
                     54: }
                     55: 
                     56: /********************************* UNIT TESTS ************************************/
                     57: #ifdef UNITTEST
                     58: #include "lite.h"
                     59: 
                     60: int checkpath(char *dir)
                     61: {
                     62:        char tmp[256];
                     63:        struct stat sb;
                     64: 
                     65:        snprintf(tmp, sizeof(tmp), "ls -ld %s", dir);
                     66:        if (system(tmp))
                     67:                perror("system");
                     68: 
                     69:        if (!stat(dir, &sb) && S_ISDIR(sb.st_mode))
                     70:                return 0;
                     71: 
                     72:        errno = ENOTDIR;
                     73:        return 1;
                     74: }
                     75: 
                     76: int test_makepath(char *dir)
                     77: {
                     78:        int ret = makepath(dir);
                     79: 
                     80:        if (!ret)
                     81:                ret = checkpath(dir);
                     82:        if (ret)
                     83:                perror("Failed");
                     84: 
                     85:        return ret;
                     86: }
                     87: 
                     88: int main(void)
                     89: {
                     90:        int i, ret = 0;
                     91:        char *list[] = {
                     92:                "/tmp/tok/",
                     93:                "/tmp/tok2",
                     94:                "/tmp/ab",
                     95:                "/tmp/b",
                     96:                "/tmp/a/",
                     97:                "/tmp/a/b",
                     98:                "/tmp/a/c/",
                     99:                NULL
                    100:        };
                    101: 
                    102:        for (i = 0; list[i]; i++)
                    103:                rmdir(list[i]);
                    104: 
                    105:        printf("Testing makepath() ...\n");
                    106:        for (i = 0; list[i] && !ret; i++)
                    107:                ret = test_makepath(list[i]);
                    108: 
                    109:        printf("\nCleaning up ...\n");
                    110:        for (i = 0; list[i]; i++)
                    111:                rmdir(list[i]);
                    112: 
                    113:        return ret;
                    114: }
                    115: #endif  /* UNITTEST */
                    116: 
                    117: /**
                    118:  * Local Variables:
                    119:  *  compile-command: "make V=1 -f makepath.mk"
                    120:  *  version-control: t
                    121:  *  indent-tabs-mode: t
                    122:  *  c-file-style: "linux"
                    123:  * End:
                    124:  */

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>