source: rtems/cpukit/libcsupport/src/rtems_mkdir.c @ b81c8a69

4.115
Last change on this file since b81c8a69 was b81c8a69, checked in by Sebastian Huber <sebastian.huber@…>, on 06/08/10 at 13:05:11

2010-06-08 Sebastian Huber <sebastian.huber@…>

PR 1524/filesystem

  • libcsupport/src/rtems_mkdir.c: New file.
  • libcsupport/src/Makefile.am: Reflect change above.
  • libcsupport/include/rtems/libio.h: Added rtems_mkdir().
  • libmisc/fsmount/fsmount.h, libmisc/fsmount/fsmount.c, libblock/src/bdpart-mount.c, libnetworking/rtems/mkrootfs.h, libnetworking/rtems/mkrootfs.c, libfs/src/pipe/pipe.c: Use rtems_mkdir(). Removed rtems_fsmount_create_mount_point() and rtems_rootfs_mkdir().
  • Property mode set to 100644
File size: 3.6 KB
Line 
1/**
2 * @file
3 *
4 * @ingroup LibIO
5 *
6 * @brief rtems_mkdir() implementation.
7 *
8 * The implementation is based on FreeBSD 'bin/mkdir/mkdir.c' revision 163213.
9 */
10
11/*-
12 * Copyright (c) 2010 embedded brains GmbH.
13 *
14 * Copyright (c) 1983, 1992, 1993
15 *      The Regents of the University of California.  All rights reserved.
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
19 * are met:
20 * 1. Redistributions of source code must retain the above copyright
21 *    notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 *    notice, this list of conditions and the following disclaimer in the
24 *    documentation and/or other materials provided with the distribution.
25 * 4. Neither the name of the University nor the names of its contributors
26 *    may be used to endorse or promote products derived from this software
27 *    without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * SUCH DAMAGE.
40 */
41
42#if HAVE_CONFIG_H
43#include "config.h"
44#endif
45
46#include <sys/types.h>
47#include <sys/stat.h>
48
49#include <errno.h>
50#include <stdlib.h>
51#include <string.h>
52#include <unistd.h>
53
54#include <rtems/libio.h>
55
56/*
57 * Returns 1 if a directory has been created,
58 * 2 if it already existed, and 0 on failure.
59 */
60static int
61build(char *path, mode_t omode)
62{
63        struct stat sb;
64        mode_t numask, oumask;
65        int first, last, retval;
66        char *p;
67
68        p = path;
69        oumask = 0;
70        retval = 1;
71        if (p[0] == '/')                /* Skip leading '/'. */
72                ++p;
73        for (first = 1, last = 0; !last ; ++p) {
74                if (p[0] == '\0')
75                        last = 1;
76                else if (p[0] != '/')
77                        continue;
78                *p = '\0';
79                if (!last && p[1] == '\0')
80                        last = 1;
81                if (first) {
82                        /*
83                         * POSIX 1003.2:
84                         * For each dir operand that does not name an existing
85                         * directory, effects equivalent to those caused by the
86                         * following command shall occcur:
87                         *
88                         * mkdir -p -m $(umask -S),u+wx $(dirname dir) &&
89                         *    mkdir [-m mode] dir
90                         *
91                         * We change the user's umask and then restore it,
92                         * instead of doing chmod's.
93                         */
94                        oumask = umask(0);
95                        numask = oumask & ~(S_IWUSR | S_IXUSR);
96                        (void)umask(numask);
97                        first = 0;
98                }
99                if (last)
100                        (void)umask(oumask);
101                if (mkdir(path, last ? omode : S_IRWXU | S_IRWXG | S_IRWXO) < 0) {
102                        if (errno == EEXIST || errno == EISDIR) {
103                                if (stat(path, &sb) < 0) {
104                                        retval = 0;
105                                        break;
106                                } else if (!S_ISDIR(sb.st_mode)) {
107                                        if (last)
108                                                errno = EEXIST;
109                                        else
110                                                errno = ENOTDIR;
111                                        retval = 0;
112                                        break;
113                                }
114                                if (last)
115                                        retval = 2;
116                        } else {
117                                retval = 0;
118                                break;
119                        }
120                }
121                if (!last)
122                    *p = '/';
123        }
124        if (!first && !last)
125                (void)umask(oumask);
126        return (retval);
127}
128
129int
130rtems_mkdir(const char *path, mode_t mode)
131{
132        int success = 0;
133        char *dup_path = strdup(path);
134
135        if (dup_path != NULL) {
136                success = build(dup_path, mode);
137                free(dup_path);
138        }
139
140        return success != 0 ? 0 : -1;
141}
Note: See TracBrowser for help on using the repository browser.