fs.c revision 0ee7d8c68b57c02d02f707d6f71c731234d56eec
1/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "cutils"
18
19/* These defines are only needed because prebuilt headers are out of date */
20#define __USE_XOPEN2K8 1
21#define _ATFILE_SOURCE 1
22#define _GNU_SOURCE 1
23
24#include <cutils/fs.h>
25#include <cutils/log.h>
26
27#include <sys/types.h>
28#include <sys/stat.h>
29#include <fcntl.h>
30#include <unistd.h>
31#include <errno.h>
32#include <string.h>
33#include <limits.h>
34#include <stdlib.h>
35#include <dirent.h>
36
37#define ALL_PERMS (S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO)
38#define BUF_SIZE 64
39
40int fs_prepare_dir(const char* path, mode_t mode, uid_t uid, gid_t gid) {
41    // Check if path needs to be created
42    struct stat sb;
43    if (TEMP_FAILURE_RETRY(lstat(path, &sb)) == -1) {
44        if (errno == ENOENT) {
45            goto create;
46        } else {
47            ALOGE("Failed to lstat(%s): %s", path, strerror(errno));
48            return -1;
49        }
50    }
51
52    // Exists, verify status
53    if (!S_ISDIR(sb.st_mode)) {
54        ALOGE("Not a directory: %s", path);
55        return -1;
56    }
57    if (((sb.st_mode & ALL_PERMS) == mode) && (sb.st_uid == uid) && (sb.st_gid == gid)) {
58        return 0;
59    } else {
60        goto fixup;
61    }
62
63create:
64    if (TEMP_FAILURE_RETRY(mkdir(path, mode)) == -1) {
65        if (errno != EEXIST) {
66            ALOGE("Failed to mkdir(%s): %s", path, strerror(errno));
67            return -1;
68        }
69    }
70
71fixup:
72    if (TEMP_FAILURE_RETRY(chmod(path, mode)) == -1) {
73        ALOGE("Failed to chmod(%s, %d): %s", path, mode, strerror(errno));
74        return -1;
75    }
76    if (TEMP_FAILURE_RETRY(chown(path, uid, gid)) == -1) {
77        ALOGE("Failed to chown(%s, %d, %d): %s", path, uid, gid, strerror(errno));
78        return -1;
79    }
80
81    return 0;
82}
83
84int fs_read_atomic_int(const char* path, int* out_value) {
85    int fd = TEMP_FAILURE_RETRY(open(path, O_RDONLY));
86    if (fd == -1) {
87        ALOGE("Failed to read %s: %s", path, strerror(errno));
88        return -1;
89    }
90
91    char buf[BUF_SIZE];
92    if (TEMP_FAILURE_RETRY(read(fd, buf, BUF_SIZE)) == -1) {
93        ALOGE("Failed to read %s: %s", path, strerror(errno));
94        goto fail;
95    }
96    if (sscanf(buf, "%d", out_value) != 1) {
97        ALOGE("Failed to parse %s: %s", path, strerror(errno));
98        goto fail;
99    }
100    close(fd);
101    return 0;
102
103fail:
104    close(fd);
105    *out_value = -1;
106    return -1;
107}
108
109int fs_write_atomic_int(const char* path, int value) {
110    char temp[PATH_MAX];
111    if (snprintf(temp, PATH_MAX, "%s.XXXXXX", path) >= PATH_MAX) {
112        ALOGE("Path too long");
113        return -1;
114    }
115
116    int fd = TEMP_FAILURE_RETRY(mkstemp(temp));
117    if (fd == -1) {
118        ALOGE("Failed to open %s: %s", temp, strerror(errno));
119        return -1;
120    }
121
122    char buf[BUF_SIZE];
123    int len = snprintf(buf, BUF_SIZE, "%d", value) + 1;
124    if (len > BUF_SIZE) {
125        ALOGE("Value %d too large: %s", value, strerror(errno));
126        goto fail;
127    }
128    if (TEMP_FAILURE_RETRY(write(fd, buf, len)) < len) {
129        ALOGE("Failed to write %s: %s", temp, strerror(errno));
130        goto fail;
131    }
132    if (close(fd) == -1) {
133        ALOGE("Failed to close %s: %s", temp, strerror(errno));
134        goto fail_closed;
135    }
136
137    if (rename(temp, path) == -1) {
138        ALOGE("Failed to rename %s to %s: %s", temp, path, strerror(errno));
139        goto fail_closed;
140    }
141
142    return 0;
143
144fail:
145    close(fd);
146fail_closed:
147    unlink(temp);
148    return -1;
149}
150
151#ifndef __APPLE__
152
153int fs_mkdirs(const char* path, mode_t mode) {
154    int res = 0;
155    int fd = 0;
156    struct stat sb;
157    char* buf = strdup(path);
158
159    if (*buf != '/') {
160        ALOGE("Relative paths are not allowed: %s", buf);
161        res = -EINVAL;
162        goto done;
163    }
164
165    if ((fd = open("/", 0)) == -1) {
166        ALOGE("Failed to open(/): %s", strerror(errno));
167        res = -errno;
168        goto done;
169    }
170
171    char* segment = buf + 1;
172    char* p = segment;
173    while (*p != '\0') {
174        if (*p == '/') {
175            *p = '\0';
176
177            if (!strcmp(segment, "..") || !strcmp(segment, ".") || !strcmp(segment, "")) {
178                ALOGE("Invalid path: %s", buf);
179                res = -EINVAL;
180                goto done_close;
181            }
182
183            if (fstatat(fd, segment, &sb, AT_SYMLINK_NOFOLLOW) != 0) {
184                if (errno == ENOENT) {
185                    /* Nothing there yet; let's create it! */
186                    if (mkdirat(fd, segment, mode) != 0) {
187                        if (errno == EEXIST) {
188                            /* We raced with someone; ignore */
189                        } else {
190                            ALOGE("Failed to mkdirat(%s): %s", buf, strerror(errno));
191                            res = -errno;
192                            goto done_close;
193                        }
194                    }
195                } else {
196                    ALOGE("Failed to fstatat(%s): %s", buf, strerror(errno));
197                    res = -errno;
198                    goto done_close;
199                }
200            } else {
201                if (S_ISLNK(sb.st_mode)) {
202                    ALOGE("Symbolic links are not allowed: %s", buf);
203                    res = -ELOOP;
204                    goto done_close;
205                }
206                if (!S_ISDIR(sb.st_mode)) {
207                    ALOGE("Existing segment not a directory: %s", buf);
208                    res = -ENOTDIR;
209                    goto done_close;
210                }
211            }
212
213            /* Yay, segment is ready for us to step into */
214            int next_fd;
215            if ((next_fd = openat(fd, segment, 0)) == -1) {
216                ALOGE("Failed to openat(%s): %s", buf, strerror(errno));
217                res = -errno;
218                goto done_close;
219            }
220
221            close(fd);
222            fd = next_fd;
223
224            *p = '/';
225            segment = p + 1;
226        }
227        p++;
228    }
229
230done_close:
231    close(fd);
232done:
233    free(buf);
234    return res;
235}
236
237#endif
238