blob: 116526dca5662ca1125a94e077324e1e474ee8ad [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>
Nick Kralevich69ce4892012-10-15 15:51:33 -070029#include <stdlib.h>
Jeff Sharkey96851942012-08-27 15:03:37 -070030
31#define ALL_PERMS (S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO)
32#define BUF_SIZE 64
33
34int fs_prepare_dir(const char* path, mode_t mode, uid_t uid, gid_t gid) {
35 // Check if path needs to be created
36 struct stat sb;
Jeff Sharkeyddb17332012-09-13 14:47:23 -070037 if (TEMP_FAILURE_RETRY(lstat(path, &sb)) == -1) {
Jeff Sharkey96851942012-08-27 15:03:37 -070038 if (errno == ENOENT) {
39 goto create;
40 } else {
Jeff Sharkeyddb17332012-09-13 14:47:23 -070041 ALOGE("Failed to lstat(%s): %s", path, strerror(errno));
Jeff Sharkey96851942012-08-27 15:03:37 -070042 return -1;
43 }
44 }
45
46 // Exists, verify status
47 if (!S_ISDIR(sb.st_mode)) {
48 ALOGE("Not a directory: %s", path);
49 return -1;
50 }
51 if (((sb.st_mode & ALL_PERMS) == mode) && (sb.st_uid == uid) && (sb.st_gid == gid)) {
52 return 0;
53 } else {
54 goto fixup;
55 }
56
57create:
Jeff Sharkeyddb17332012-09-13 14:47:23 -070058 if (TEMP_FAILURE_RETRY(mkdir(path, mode)) == -1) {
Jeff Sharkey489609b2012-09-25 11:10:16 -070059 if (errno != EEXIST) {
60 ALOGE("Failed to mkdir(%s): %s", path, strerror(errno));
61 return -1;
62 }
Jeff Sharkey96851942012-08-27 15:03:37 -070063 }
64
65fixup:
Jeff Sharkeyddb17332012-09-13 14:47:23 -070066 if (TEMP_FAILURE_RETRY(chmod(path, mode)) == -1) {
67 ALOGE("Failed to chmod(%s, %d): %s", path, mode, strerror(errno));
Jeff Sharkey96851942012-08-27 15:03:37 -070068 return -1;
69 }
Jeff Sharkeyddb17332012-09-13 14:47:23 -070070 if (TEMP_FAILURE_RETRY(chown(path, uid, gid)) == -1) {
Jeff Sharkey96851942012-08-27 15:03:37 -070071 ALOGE("Failed to chown(%s, %d, %d): %s", path, uid, gid, strerror(errno));
72 return -1;
73 }
74
75 return 0;
76}
77
78int fs_read_atomic_int(const char* path, int* out_value) {
Jeff Sharkey6de70262012-09-13 15:11:42 -070079 int fd = TEMP_FAILURE_RETRY(open(path, O_RDONLY));
Jeff Sharkey96851942012-08-27 15:03:37 -070080 if (fd == -1) {
81 ALOGE("Failed to read %s: %s", path, strerror(errno));
82 return -1;
83 }
84
85 char buf[BUF_SIZE];
Jeff Sharkey6de70262012-09-13 15:11:42 -070086 if (TEMP_FAILURE_RETRY(read(fd, buf, BUF_SIZE)) == -1) {
Jeff Sharkey96851942012-08-27 15:03:37 -070087 ALOGE("Failed to read %s: %s", path, strerror(errno));
88 goto fail;
89 }
90 if (sscanf(buf, "%d", out_value) != 1) {
91 ALOGE("Failed to parse %s: %s", path, strerror(errno));
92 goto fail;
93 }
94 close(fd);
95 return 0;
96
97fail:
98 close(fd);
99 *out_value = -1;
100 return -1;
101}
102
103int fs_write_atomic_int(const char* path, int value) {
104 char temp[PATH_MAX];
105 if (snprintf(temp, PATH_MAX, "%s.XXXXXX", path) >= PATH_MAX) {
106 ALOGE("Path too long");
107 return -1;
108 }
109
Jeff Sharkey6de70262012-09-13 15:11:42 -0700110 int fd = TEMP_FAILURE_RETRY(mkstemp(temp));
Jeff Sharkey96851942012-08-27 15:03:37 -0700111 if (fd == -1) {
112 ALOGE("Failed to open %s: %s", temp, strerror(errno));
113 return -1;
114 }
115
116 char buf[BUF_SIZE];
117 int len = snprintf(buf, BUF_SIZE, "%d", value) + 1;
118 if (len > BUF_SIZE) {
119 ALOGE("Value %d too large: %s", value, strerror(errno));
120 goto fail;
121 }
Jeff Sharkey6de70262012-09-13 15:11:42 -0700122 if (TEMP_FAILURE_RETRY(write(fd, buf, len)) < len) {
Jeff Sharkey96851942012-08-27 15:03:37 -0700123 ALOGE("Failed to write %s: %s", temp, strerror(errno));
124 goto fail;
125 }
126 if (close(fd) == -1) {
127 ALOGE("Failed to close %s: %s", temp, strerror(errno));
128 goto fail_closed;
129 }
130
131 if (rename(temp, path) == -1) {
132 ALOGE("Failed to rename %s to %s: %s", temp, path, strerror(errno));
133 goto fail_closed;
134 }
135
136 return 0;
137
138fail:
139 close(fd);
140fail_closed:
141 unlink(temp);
142 return -1;
143}