The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
Elliott Hughes | 4bbd5bf | 2016-04-01 18:24:39 -0700 | [diff] [blame] | 17 | #include "DirUtil.h" |
| 18 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 19 | #include <dirent.h> |
Tao Bao | ac3d1ed | 2017-07-23 00:01:02 -0700 | [diff] [blame] | 20 | #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 Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 25 | |
Yabin Cui | 4425c1d | 2016-02-10 13:47:32 -0800 | [diff] [blame] | 26 | #include <string> |
| 27 | |
Elliott Hughes | 4bbd5bf | 2016-04-01 18:24:39 -0700 | [diff] [blame] | 28 | #include <selinux/label.h> |
| 29 | #include <selinux/selinux.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 30 | |
Tao Bao | ac3d1ed | 2017-07-23 00:01:02 -0700 | [diff] [blame] | 31 | enum class DirStatus { DMISSING, DDIR, DILLEGAL }; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 32 | |
Tao Bao | ac3d1ed | 2017-07-23 00:01:02 -0700 | [diff] [blame] | 33 | static 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 Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 39 | } |
Tao Bao | ac3d1ed | 2017-07-23 00:01:02 -0700 | [diff] [blame] | 40 | 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 Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 47 | } |
| 48 | |
Tao Bao | ac3d1ed | 2017-07-23 00:01:02 -0700 | [diff] [blame] | 49 | int 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 Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 56 | |
Tao Bao | ac3d1ed | 2017-07-23 00:01:02 -0700 | [diff] [blame] | 57 | // 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 Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 65 | } |
Tao Bao | ac3d1ed | 2017-07-23 00:01:02 -0700 | [diff] [blame] | 66 | 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 Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 71 | |
Tao Bao | ac3d1ed | 2017-07-23 00:01:02 -0700 | [diff] [blame] | 72 | // See if it already exists. |
| 73 | DirStatus ds = dir_status(path); |
| 74 | if (ds == DirStatus::DDIR) { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 75 | return 0; |
Tao Bao | ac3d1ed | 2017-07-23 00:01:02 -0700 | [diff] [blame] | 76 | } 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 Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 116 | } |