blob: 6508b6787a6b9e49af68100bb5540a891a5312d6 [file] [log] [blame]
Jeff Sharkey96851942012-08-27 15:03:37 -07001/*
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
Jeff Sharkeyddb17332012-09-13 14:47:23 -070017#define LOG_TAG "cutils"
18
Jeff Sharkey96851942012-08-27 15:03:37 -070019#include <cutils/fs.h>
20#include <cutils/log.h>
21
22#include <sys/types.h>
23#include <sys/stat.h>
24#include <fcntl.h>
25#include <unistd.h>
26#include <errno.h>
27#include <string.h>
28#include <limits.h>
29
30#define ALL_PERMS (S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO)
31#define BUF_SIZE 64
32
33int fs_prepare_dir(const char* path, mode_t mode, uid_t uid, gid_t gid) {
34 // Check if path needs to be created
35 struct stat sb;
Jeff Sharkeyddb17332012-09-13 14:47:23 -070036 if (TEMP_FAILURE_RETRY(lstat(path, &sb)) == -1) {
Jeff Sharkey96851942012-08-27 15:03:37 -070037 if (errno == ENOENT) {
38 goto create;
39 } else {
Jeff Sharkeyddb17332012-09-13 14:47:23 -070040 ALOGE("Failed to lstat(%s): %s", path, strerror(errno));
Jeff Sharkey96851942012-08-27 15:03:37 -070041 return -1;
42 }
43 }
44
45 // Exists, verify status
46 if (!S_ISDIR(sb.st_mode)) {
47 ALOGE("Not a directory: %s", path);
48 return -1;
49 }
50 if (((sb.st_mode & ALL_PERMS) == mode) && (sb.st_uid == uid) && (sb.st_gid == gid)) {
51 return 0;
52 } else {
53 goto fixup;
54 }
55
56create:
Jeff Sharkeyddb17332012-09-13 14:47:23 -070057 if (TEMP_FAILURE_RETRY(mkdir(path, mode)) == -1) {
Jeff Sharkey96851942012-08-27 15:03:37 -070058 ALOGE("Failed to mkdir(%s): %s", path, strerror(errno));
59 return -1;
60 }
61
62fixup:
Jeff Sharkeyddb17332012-09-13 14:47:23 -070063 if (TEMP_FAILURE_RETRY(chmod(path, mode)) == -1) {
64 ALOGE("Failed to chmod(%s, %d): %s", path, mode, strerror(errno));
Jeff Sharkey96851942012-08-27 15:03:37 -070065 return -1;
66 }
Jeff Sharkeyddb17332012-09-13 14:47:23 -070067 if (TEMP_FAILURE_RETRY(chown(path, uid, gid)) == -1) {
Jeff Sharkey96851942012-08-27 15:03:37 -070068 ALOGE("Failed to chown(%s, %d, %d): %s", path, uid, gid, strerror(errno));
69 return -1;
70 }
71
72 return 0;
73}
74
75int fs_read_atomic_int(const char* path, int* out_value) {
76 int fd = open(path, O_RDONLY);
77 if (fd == -1) {
78 ALOGE("Failed to read %s: %s", path, strerror(errno));
79 return -1;
80 }
81
82 char buf[BUF_SIZE];
83 if (read(fd, buf, BUF_SIZE) == -1) {
84 ALOGE("Failed to read %s: %s", path, strerror(errno));
85 goto fail;
86 }
87 if (sscanf(buf, "%d", out_value) != 1) {
88 ALOGE("Failed to parse %s: %s", path, strerror(errno));
89 goto fail;
90 }
91 close(fd);
92 return 0;
93
94fail:
95 close(fd);
96 *out_value = -1;
97 return -1;
98}
99
100int fs_write_atomic_int(const char* path, int value) {
101 char temp[PATH_MAX];
102 if (snprintf(temp, PATH_MAX, "%s.XXXXXX", path) >= PATH_MAX) {
103 ALOGE("Path too long");
104 return -1;
105 }
106
107 int fd = mkstemp(temp);
108 if (fd == -1) {
109 ALOGE("Failed to open %s: %s", temp, strerror(errno));
110 return -1;
111 }
112
113 char buf[BUF_SIZE];
114 int len = snprintf(buf, BUF_SIZE, "%d", value) + 1;
115 if (len > BUF_SIZE) {
116 ALOGE("Value %d too large: %s", value, strerror(errno));
117 goto fail;
118 }
119 if (write(fd, buf, len) < len) {
120 ALOGE("Failed to write %s: %s", temp, strerror(errno));
121 goto fail;
122 }
123 if (close(fd) == -1) {
124 ALOGE("Failed to close %s: %s", temp, strerror(errno));
125 goto fail_closed;
126 }
127
128 if (rename(temp, path) == -1) {
129 ALOGE("Failed to rename %s to %s: %s", temp, path, strerror(errno));
130 goto fail_closed;
131 }
132
133 return 0;
134
135fail:
136 close(fd);
137fail_closed:
138 unlink(temp);
139 return -1;
140}