blob: 61c83281398e3289a77307e22afe45059e8c33df [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 Baocfe53c22017-10-03 14:37:21 -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) {
51 // Check for an empty string before we bother making any syscalls.
52 if (input_path.empty()) {
53 errno = ENOENT;
54 return -1;
55 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080056
Tao Baoac3d1ed2017-07-23 00:01:02 -070057 // Allocate a path that we can modify; stick a slash on the end to make things easier.
58 std::string path = input_path;
59 if (strip_filename) {
60 // Strip everything after the last slash.
61 size_t pos = path.rfind('/');
62 if (pos == std::string::npos) {
63 errno = ENOENT;
64 return -1;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080065 }
Tao Baoac3d1ed2017-07-23 00:01:02 -070066 path.resize(pos + 1);
67 } else {
68 // Make sure that the path ends in a slash.
69 path.push_back('/');
70 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080071
Tao Baoac3d1ed2017-07-23 00:01:02 -070072 // See if it already exists.
73 DirStatus ds = dir_status(path);
74 if (ds == DirStatus::DDIR) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080075 return 0;
Tao Baoac3d1ed2017-07-23 00:01:02 -070076 } else if (ds == DirStatus::DILLEGAL) {
77 return -1;
78 }
79
80 // Walk up the path from the root and make each level.
81 size_t prev_end = 0;
82 while (prev_end < path.size()) {
83 size_t next_end = path.find('/', prev_end + 1);
84 if (next_end == std::string::npos) {
85 break;
86 }
87 std::string dir_path = path.substr(0, next_end);
88 // Check this part of the path and make a new directory if necessary.
89 switch (dir_status(dir_path)) {
90 case DirStatus::DILLEGAL:
91 // Could happen if some other process/thread is messing with the filesystem.
92 return -1;
93 case DirStatus::DMISSING: {
94 char* secontext = nullptr;
95 if (sehnd) {
96 selabel_lookup(const_cast<selabel_handle*>(sehnd), &secontext, dir_path.c_str(), mode);
97 setfscreatecon(secontext);
98 }
99 int err = mkdir(dir_path.c_str(), mode);
100 if (secontext) {
101 freecon(secontext);
102 setfscreatecon(nullptr);
103 }
104 if (err != 0) {
105 return -1;
106 }
107 break;
108 }
109 default:
110 // Already exists.
111 break;
112 }
113 prev_end = next_end;
114 }
115 return 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800116}