blob: 1e9c693a34b64e3c09fe8a378c87be0c8d11af1a [file] [log] [blame]
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001/*
2 * Copyright (C) 2007 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
Tao Bao17054c02018-05-03 22:41:23 -070017#include "otautil/dirutil.h"
Elliott Hughes4bbd5bf2016-04-01 18:24:39 -070018
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080019#include <dirent.h>
Tao Baoac3d1ed2017-07-23 00:01:02 -070020#include <errno.h>
21#include <stdlib.h>
22#include <sys/stat.h>
23#include <sys/types.h>
24#include <unistd.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080025
Yabin Cui4425c1d2016-02-10 13:47:32 -080026#include <string>
27
Elliott Hughes4bbd5bf2016-04-01 18:24:39 -070028#include <selinux/label.h>
29#include <selinux/selinux.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080030
Tao Baoac3d1ed2017-07-23 00:01:02 -070031enum class DirStatus { DMISSING, DDIR, DILLEGAL };
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080032
Tao Baoac3d1ed2017-07-23 00:01:02 -070033static DirStatus dir_status(const std::string& path) {
34 struct stat sb;
35 if (stat(path.c_str(), &sb) == 0) {
36 // Something's there; make sure it's a directory.
37 if (S_ISDIR(sb.st_mode)) {
38 return DirStatus::DDIR;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080039 }
Tao Baoac3d1ed2017-07-23 00:01:02 -070040 errno = ENOTDIR;
41 return DirStatus::DILLEGAL;
42 } else if (errno != ENOENT) {
43 // Something went wrong, or something in the path is bad. Can't do anything in this situation.
44 return DirStatus::DILLEGAL;
45 }
46 return DirStatus::DMISSING;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080047}
48
Tao Baoac3d1ed2017-07-23 00:01:02 -070049int mkdir_recursively(const std::string& input_path, mode_t mode, bool strip_filename,
50 const selabel_handle* sehnd) {
Michael Bestas8ac85d82019-09-19 21:42:12 +030051 return mkdir_recursively(input_path, mode, strip_filename, sehnd, NULL);
52}
53
54int mkdir_recursively(const std::string& input_path, mode_t mode, bool strip_filename,
55 const selabel_handle* sehnd, const struct utimbuf* timestamp) {
Tao Baoac3d1ed2017-07-23 00:01:02 -070056 // Check for an empty string before we bother making any syscalls.
57 if (input_path.empty()) {
58 errno = ENOENT;
59 return -1;
60 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080061
Tao Baoac3d1ed2017-07-23 00:01:02 -070062 // Allocate a path that we can modify; stick a slash on the end to make things easier.
63 std::string path = input_path;
64 if (strip_filename) {
65 // Strip everything after the last slash.
66 size_t pos = path.rfind('/');
67 if (pos == std::string::npos) {
68 errno = ENOENT;
69 return -1;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080070 }
Tao Baoac3d1ed2017-07-23 00:01:02 -070071 path.resize(pos + 1);
72 } else {
73 // Make sure that the path ends in a slash.
74 path.push_back('/');
75 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080076
Tao Baoac3d1ed2017-07-23 00:01:02 -070077 // See if it already exists.
78 DirStatus ds = dir_status(path);
79 if (ds == DirStatus::DDIR) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080080 return 0;
Tao Baoac3d1ed2017-07-23 00:01:02 -070081 } else if (ds == DirStatus::DILLEGAL) {
82 return -1;
83 }
84
85 // Walk up the path from the root and make each level.
86 size_t prev_end = 0;
87 while (prev_end < path.size()) {
88 size_t next_end = path.find('/', prev_end + 1);
89 if (next_end == std::string::npos) {
90 break;
91 }
92 std::string dir_path = path.substr(0, next_end);
93 // Check this part of the path and make a new directory if necessary.
94 switch (dir_status(dir_path)) {
95 case DirStatus::DILLEGAL:
96 // Could happen if some other process/thread is messing with the filesystem.
97 return -1;
98 case DirStatus::DMISSING: {
99 char* secontext = nullptr;
100 if (sehnd) {
101 selabel_lookup(const_cast<selabel_handle*>(sehnd), &secontext, dir_path.c_str(), mode);
102 setfscreatecon(secontext);
103 }
104 int err = mkdir(dir_path.c_str(), mode);
105 if (secontext) {
106 freecon(secontext);
107 setfscreatecon(nullptr);
108 }
109 if (err != 0) {
110 return -1;
111 }
Michael Bestas8ac85d82019-09-19 21:42:12 +0300112 if (timestamp != NULL && utime(dir_path.c_str(), timestamp)) {
113 return -1;
114 }
Tao Baoac3d1ed2017-07-23 00:01:02 -0700115 break;
116 }
117 default:
118 // Already exists.
119 break;
120 }
121 prev_end = next_end;
122 }
123 return 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800124}
Michael Bestas8ac85d82019-09-19 21:42:12 +0300125
126int dirUnlinkHierarchy(const char* path) {
127 struct stat st;
128 DIR* dir;
129 struct dirent* de;
130 int fail = 0;
131
132 /* is it a file or directory? */
133 if (lstat(path, &st) < 0) {
134 return -1;
135 }
136
137 /* a file, so unlink it */
138 if (!S_ISDIR(st.st_mode)) {
139 return unlink(path);
140 }
141
142 /* a directory, so open handle */
143 dir = opendir(path);
144 if (dir == NULL) {
145 return -1;
146 }
147
148 /* recurse over components */
149 errno = 0;
150 while ((de = readdir(dir)) != NULL) {
151 // TODO: don't blow the stack
152 char dn[PATH_MAX];
153 if (!strcmp(de->d_name, "..") || !strcmp(de->d_name, ".")) {
154 continue;
155 }
156 snprintf(dn, sizeof(dn), "%s/%s", path, de->d_name);
157 if (dirUnlinkHierarchy(dn) < 0) {
158 fail = 1;
159 break;
160 }
161 errno = 0;
162 }
163 /* in case readdir or unlink_recursive failed */
164 if (fail || errno < 0) {
165 int save = errno;
166 closedir(dir);
167 errno = save;
168 return -1;
169 }
170
171 /* close directory handle */
172 if (closedir(dir) < 0) {
173 return -1;
174 }
175
176 /* delete target directory */
177 return rmdir(path);
178}