| Elliott Hughes | 5830577 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2015 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 |  | 
| Yabin Cui | aed3c61 | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 17 | #define TRACE_TAG ADB | 
| Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 18 |  | 
| Elliott Hughes | 5830577 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 19 | #include "adb_utils.h" | 
|  | 20 |  | 
| Spencer Low | 22191c3 | 2015-08-01 17:29:23 -0700 | [diff] [blame] | 21 | #include <libgen.h> | 
| Elliott Hughes | a7090b9 | 2015-04-17 17:03:59 -0700 | [diff] [blame] | 22 | #include <stdlib.h> | 
| Elliott Hughes | 5830577 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 23 | #include <sys/stat.h> | 
|  | 24 | #include <sys/types.h> | 
|  | 25 | #include <unistd.h> | 
|  | 26 |  | 
| Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 27 | #include <algorithm> | 
|  | 28 |  | 
| Elliott Hughes | 4f71319 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 29 | #include <android-base/logging.h> | 
|  | 30 | #include <android-base/stringprintf.h> | 
|  | 31 | #include <android-base/strings.h> | 
| Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 32 |  | 
| Josh Gao | 7d58607 | 2015-11-20 15:37:31 -0800 | [diff] [blame] | 33 | #include "adb.h" | 
| Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 34 | #include "adb_trace.h" | 
| Elliott Hughes | 53daee6 | 2015-04-19 13:17:01 -0700 | [diff] [blame] | 35 | #include "sysdeps.h" | 
|  | 36 |  | 
| Josh Gao | 787f344 | 2015-11-03 18:52:35 -0800 | [diff] [blame] | 37 | ADB_MUTEX_DEFINE(basename_lock); | 
| Spencer Low | 22191c3 | 2015-08-01 17:29:23 -0700 | [diff] [blame] | 38 | ADB_MUTEX_DEFINE(dirname_lock); | 
|  | 39 |  | 
| Josh Gao | 7d58607 | 2015-11-20 15:37:31 -0800 | [diff] [blame] | 40 | #if defined(_WIN32) | 
|  | 41 | constexpr char kNullFileName[] = "NUL"; | 
|  | 42 | #else | 
|  | 43 | constexpr char kNullFileName[] = "/dev/null"; | 
|  | 44 | #endif | 
|  | 45 |  | 
|  | 46 | void close_stdin() { | 
|  | 47 | int fd = unix_open(kNullFileName, O_RDONLY); | 
|  | 48 | if (fd == -1) { | 
|  | 49 | fatal_errno("failed to open %s", kNullFileName); | 
|  | 50 | } | 
|  | 51 |  | 
|  | 52 | if (TEMP_FAILURE_RETRY(dup2(fd, STDIN_FILENO)) == -1) { | 
|  | 53 | fatal_errno("failed to redirect stdin to %s", kNullFileName); | 
|  | 54 | } | 
|  | 55 | unix_close(fd); | 
|  | 56 | } | 
|  | 57 |  | 
| Elliott Hughes | a7090b9 | 2015-04-17 17:03:59 -0700 | [diff] [blame] | 58 | bool getcwd(std::string* s) { | 
|  | 59 | char* cwd = getcwd(nullptr, 0); | 
|  | 60 | if (cwd != nullptr) *s = cwd; | 
|  | 61 | free(cwd); | 
|  | 62 | return (cwd != nullptr); | 
|  | 63 | } | 
|  | 64 |  | 
| Elliott Hughes | 5830577 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 65 | bool directory_exists(const std::string& path) { | 
|  | 66 | struct stat sb; | 
|  | 67 | return lstat(path.c_str(), &sb) != -1 && S_ISDIR(sb.st_mode); | 
|  | 68 | } | 
|  | 69 |  | 
| Elliott Hughes | 5830577 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 70 | std::string escape_arg(const std::string& s) { | 
| Elliott Hughes | 5498ade | 2015-04-17 20:50:11 -0700 | [diff] [blame] | 71 | std::string result = s; | 
| Elliott Hughes | 5830577 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 72 |  | 
| Elliott Hughes | 84b0bf2 | 2015-05-15 12:06:00 -0700 | [diff] [blame] | 73 | // Escape any ' in the string (before we single-quote the whole thing). | 
|  | 74 | // The correct way to do this for the shell is to replace ' with '\'' --- that is, | 
|  | 75 | // close the existing single-quoted string, escape a single single-quote, and start | 
|  | 76 | // a new single-quoted string. Like the C preprocessor, the shell will concatenate | 
|  | 77 | // these pieces into one string. | 
|  | 78 | for (size_t i = 0; i < s.size(); ++i) { | 
|  | 79 | if (s[i] == '\'') { | 
|  | 80 | result.insert(i, "'\\'"); | 
|  | 81 | i += 2; | 
|  | 82 | } | 
| Elliott Hughes | 5830577 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 83 | } | 
| Elliott Hughes | 5498ade | 2015-04-17 20:50:11 -0700 | [diff] [blame] | 84 |  | 
|  | 85 | // Prefix and suffix the whole string with '. | 
|  | 86 | result.insert(result.begin(), '\''); | 
|  | 87 | result.push_back('\''); | 
| Elliott Hughes | 5830577 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 88 | return result; | 
|  | 89 | } | 
| Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 90 |  | 
| Elliott Hughes | 5c74270 | 2015-07-30 17:42:01 -0700 | [diff] [blame] | 91 | std::string adb_basename(const std::string& path) { | 
| Josh Gao | 787f344 | 2015-11-03 18:52:35 -0800 | [diff] [blame] | 92 | // Copy path because basename may modify the string passed in. | 
|  | 93 | std::string result(path); | 
|  | 94 |  | 
|  | 95 | // Use lock because basename() may write to a process global and return a | 
|  | 96 | // pointer to that. Note that this locking strategy only works if all other | 
|  | 97 | // callers to dirname in the process also grab this same lock. | 
|  | 98 | adb_mutex_lock(&basename_lock); | 
|  | 99 |  | 
|  | 100 | // Note that if std::string uses copy-on-write strings, &str[0] will cause | 
|  | 101 | // the copy to be made, so there is no chance of us accidentally writing to | 
|  | 102 | // the storage for 'path'. | 
|  | 103 | char* name = basename(&result[0]); | 
|  | 104 |  | 
|  | 105 | // In case dirname returned a pointer to a process global, copy that string | 
|  | 106 | // before leaving the lock. | 
|  | 107 | result.assign(name); | 
|  | 108 |  | 
|  | 109 | adb_mutex_unlock(&basename_lock); | 
|  | 110 |  | 
|  | 111 | return result; | 
| Elliott Hughes | 5c74270 | 2015-07-30 17:42:01 -0700 | [diff] [blame] | 112 | } | 
|  | 113 |  | 
| Spencer Low | 22191c3 | 2015-08-01 17:29:23 -0700 | [diff] [blame] | 114 | std::string adb_dirname(const std::string& path) { | 
|  | 115 | // Copy path because dirname may modify the string passed in. | 
| Josh Gao | 787f344 | 2015-11-03 18:52:35 -0800 | [diff] [blame] | 116 | std::string result(path); | 
| Spencer Low | 22191c3 | 2015-08-01 17:29:23 -0700 | [diff] [blame] | 117 |  | 
|  | 118 | // Use lock because dirname() may write to a process global and return a | 
|  | 119 | // pointer to that. Note that this locking strategy only works if all other | 
|  | 120 | // callers to dirname in the process also grab this same lock. | 
|  | 121 | adb_mutex_lock(&dirname_lock); | 
|  | 122 |  | 
|  | 123 | // Note that if std::string uses copy-on-write strings, &str[0] will cause | 
|  | 124 | // the copy to be made, so there is no chance of us accidentally writing to | 
|  | 125 | // the storage for 'path'. | 
| Josh Gao | 787f344 | 2015-11-03 18:52:35 -0800 | [diff] [blame] | 126 | char* parent = dirname(&result[0]); | 
| Spencer Low | 22191c3 | 2015-08-01 17:29:23 -0700 | [diff] [blame] | 127 |  | 
|  | 128 | // In case dirname returned a pointer to a process global, copy that string | 
|  | 129 | // before leaving the lock. | 
| Josh Gao | 787f344 | 2015-11-03 18:52:35 -0800 | [diff] [blame] | 130 | result.assign(parent); | 
| Spencer Low | 22191c3 | 2015-08-01 17:29:23 -0700 | [diff] [blame] | 131 |  | 
|  | 132 | adb_mutex_unlock(&dirname_lock); | 
|  | 133 |  | 
|  | 134 | return result; | 
| Alex Vallée | 1421614 | 2015-05-06 17:22:25 -0400 | [diff] [blame] | 135 | } | 
|  | 136 |  | 
| Spencer Low | 85c45bd | 2016-02-10 15:03:50 -0800 | [diff] [blame] | 137 | // Given a relative or absolute filepath, create the directory hierarchy | 
| Spencer Low | 22191c3 | 2015-08-01 17:29:23 -0700 | [diff] [blame] | 138 | // as needed. Returns true if the hierarchy is/was setup. | 
| Elliott Hughes | 5c74270 | 2015-07-30 17:42:01 -0700 | [diff] [blame] | 139 | bool mkdirs(const std::string& path) { | 
| Spencer Low | 22191c3 | 2015-08-01 17:29:23 -0700 | [diff] [blame] | 140 | // TODO: all the callers do unlink && mkdirs && adb_creat --- | 
|  | 141 | // that's probably the operation we should expose. | 
|  | 142 |  | 
|  | 143 | // Implementation Notes: | 
|  | 144 | // | 
|  | 145 | // Pros: | 
|  | 146 | // - Uses dirname, so does not need to deal with OS_PATH_SEPARATOR. | 
|  | 147 | // - On Windows, uses mingw dirname which accepts '/' and '\\', drive letters | 
|  | 148 | //   (C:\foo), UNC paths (\\server\share\dir\dir\file), and Unicode (when | 
|  | 149 | //   combined with our adb_mkdir() which takes UTF-8). | 
|  | 150 | // - Is optimistic wrt thinking that a deep directory hierarchy will exist. | 
|  | 151 | //   So it does as few stat()s as possible before doing mkdir()s. | 
|  | 152 | // Cons: | 
|  | 153 | // - Recursive, so it uses stack space relative to number of directory | 
|  | 154 | //   components. | 
|  | 155 |  | 
| Josh Gao | 45b6fc8 | 2015-11-04 14:51:23 -0800 | [diff] [blame] | 156 | if (directory_exists(path)) { | 
| Spencer Low | 22191c3 | 2015-08-01 17:29:23 -0700 | [diff] [blame] | 157 | return true; | 
|  | 158 | } | 
|  | 159 |  | 
| Spencer Low | 85c45bd | 2016-02-10 15:03:50 -0800 | [diff] [blame] | 160 | const std::string parent(adb_dirname(path)); | 
|  | 161 |  | 
| Spencer Low | 22191c3 | 2015-08-01 17:29:23 -0700 | [diff] [blame] | 162 | // If dirname returned the same path as what we passed in, don't go recursive. | 
|  | 163 | // This can happen on Windows when walking up the directory hierarchy and not | 
|  | 164 | // finding anything that already exists (unlike POSIX that will eventually | 
|  | 165 | // find . or /). | 
|  | 166 | if (parent == path) { | 
|  | 167 | errno = ENOENT; | 
|  | 168 | return false; | 
|  | 169 | } | 
|  | 170 |  | 
| Josh Gao | 45b6fc8 | 2015-11-04 14:51:23 -0800 | [diff] [blame] | 171 | // Recursively make parent directories of 'path'. | 
| Spencer Low | 22191c3 | 2015-08-01 17:29:23 -0700 | [diff] [blame] | 172 | if (!mkdirs(parent)) { | 
|  | 173 | return false; | 
|  | 174 | } | 
|  | 175 |  | 
| Josh Gao | 45b6fc8 | 2015-11-04 14:51:23 -0800 | [diff] [blame] | 176 | // Now that the parent directory hierarchy of 'path' has been ensured, | 
| Spencer Low | 85c45bd | 2016-02-10 15:03:50 -0800 | [diff] [blame] | 177 | // create path itself. | 
| Josh Gao | 45b6fc8 | 2015-11-04 14:51:23 -0800 | [diff] [blame] | 178 | if (adb_mkdir(path, 0775) == -1) { | 
| Spencer Low | 22191c3 | 2015-08-01 17:29:23 -0700 | [diff] [blame] | 179 | const int saved_errno = errno; | 
| Spencer Low | 85c45bd | 2016-02-10 15:03:50 -0800 | [diff] [blame] | 180 | // If someone else created the directory, that is ok. | 
|  | 181 | if (directory_exists(path)) { | 
| Spencer Low | 22191c3 | 2015-08-01 17:29:23 -0700 | [diff] [blame] | 182 | return true; | 
|  | 183 | } | 
| Spencer Low | 85c45bd | 2016-02-10 15:03:50 -0800 | [diff] [blame] | 184 | // There might be a pre-existing file at 'path', or there might have been some other error. | 
| Spencer Low | 22191c3 | 2015-08-01 17:29:23 -0700 | [diff] [blame] | 185 | errno = saved_errno; | 
|  | 186 | return false; | 
|  | 187 | } | 
|  | 188 |  | 
|  | 189 | return true; | 
| Elliott Hughes | 5c74270 | 2015-07-30 17:42:01 -0700 | [diff] [blame] | 190 | } | 
|  | 191 |  | 
| Yabin Cui | aed3c61 | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 192 | std::string dump_hex(const void* data, size_t byte_count) { | 
| Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 193 | byte_count = std::min(byte_count, size_t(16)); | 
|  | 194 |  | 
|  | 195 | const uint8_t* p = reinterpret_cast<const uint8_t*>(data); | 
|  | 196 |  | 
|  | 197 | std::string line; | 
|  | 198 | for (size_t i = 0; i < byte_count; ++i) { | 
|  | 199 | android::base::StringAppendF(&line, "%02x", p[i]); | 
|  | 200 | } | 
|  | 201 | line.push_back(' '); | 
|  | 202 |  | 
|  | 203 | for (size_t i = 0; i < byte_count; ++i) { | 
| Spencer Low | 363af56 | 2015-11-07 18:51:54 -0800 | [diff] [blame] | 204 | int ch = p[i]; | 
|  | 205 | line.push_back(isprint(ch) ? ch : '.'); | 
| Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 206 | } | 
|  | 207 |  | 
| Yabin Cui | aed3c61 | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 208 | return line; | 
| Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 209 | } | 
| Elliott Hughes | 3d5f60d | 2015-07-18 12:21:30 -0700 | [diff] [blame] | 210 |  | 
| Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 211 | std::string perror_str(const char* msg) { | 
|  | 212 | return android::base::StringPrintf("%s: %s", msg, strerror(errno)); | 
|  | 213 | } | 
| Yabin Cui | 6dfef25 | 2015-10-06 15:10:05 -0700 | [diff] [blame] | 214 |  | 
|  | 215 | #if !defined(_WIN32) | 
| Josh Gao | 3777d2e | 2016-02-16 17:34:53 -0800 | [diff] [blame] | 216 | // Windows version provided in sysdeps_win32.cpp | 
| Yabin Cui | 6dfef25 | 2015-10-06 15:10:05 -0700 | [diff] [blame] | 217 | bool set_file_block_mode(int fd, bool block) { | 
|  | 218 | int flags = fcntl(fd, F_GETFL, 0); | 
|  | 219 | if (flags == -1) { | 
|  | 220 | PLOG(ERROR) << "failed to fcntl(F_GETFL) for fd " << fd; | 
|  | 221 | return false; | 
|  | 222 | } | 
|  | 223 | flags = block ? (flags & ~O_NONBLOCK) : (flags | O_NONBLOCK); | 
|  | 224 | if (fcntl(fd, F_SETFL, flags) != 0) { | 
|  | 225 | PLOG(ERROR) << "failed to fcntl(F_SETFL) for fd " << fd << ", flags " << flags; | 
|  | 226 | return false; | 
|  | 227 | } | 
|  | 228 | return true; | 
|  | 229 | } | 
|  | 230 | #endif |