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 | |
Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 17 | #define TRACE_TAG TRACE_ADB |
| 18 | |
Elliott Hughes | 5830577 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 19 | #include "adb_utils.h" |
| 20 | |
Elliott Hughes | a7090b9 | 2015-04-17 17:03:59 -0700 | [diff] [blame] | 21 | #include <stdlib.h> |
Elliott Hughes | 5830577 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 22 | #include <sys/stat.h> |
| 23 | #include <sys/types.h> |
| 24 | #include <unistd.h> |
| 25 | |
Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 26 | #include <algorithm> |
| 27 | |
Elliott Hughes | 3d5f60d | 2015-07-18 12:21:30 -0700 | [diff] [blame] | 28 | #include <base/logging.h> |
Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 29 | #include <base/stringprintf.h> |
Elliott Hughes | 3d5f60d | 2015-07-18 12:21:30 -0700 | [diff] [blame] | 30 | #include <base/strings.h> |
Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 31 | |
| 32 | #include "adb_trace.h" |
Elliott Hughes | 53daee6 | 2015-04-19 13:17:01 -0700 | [diff] [blame] | 33 | #include "sysdeps.h" |
| 34 | |
Elliott Hughes | a7090b9 | 2015-04-17 17:03:59 -0700 | [diff] [blame] | 35 | bool getcwd(std::string* s) { |
| 36 | char* cwd = getcwd(nullptr, 0); |
| 37 | if (cwd != nullptr) *s = cwd; |
| 38 | free(cwd); |
| 39 | return (cwd != nullptr); |
| 40 | } |
| 41 | |
Elliott Hughes | 5830577 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 42 | bool directory_exists(const std::string& path) { |
| 43 | struct stat sb; |
| 44 | return lstat(path.c_str(), &sb) != -1 && S_ISDIR(sb.st_mode); |
| 45 | } |
| 46 | |
Elliott Hughes | 5830577 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 47 | std::string escape_arg(const std::string& s) { |
Elliott Hughes | 5498ade | 2015-04-17 20:50:11 -0700 | [diff] [blame] | 48 | std::string result = s; |
Elliott Hughes | 5830577 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 49 | |
Elliott Hughes | 84b0bf2 | 2015-05-15 12:06:00 -0700 | [diff] [blame] | 50 | // Escape any ' in the string (before we single-quote the whole thing). |
| 51 | // The correct way to do this for the shell is to replace ' with '\'' --- that is, |
| 52 | // close the existing single-quoted string, escape a single single-quote, and start |
| 53 | // a new single-quoted string. Like the C preprocessor, the shell will concatenate |
| 54 | // these pieces into one string. |
| 55 | for (size_t i = 0; i < s.size(); ++i) { |
| 56 | if (s[i] == '\'') { |
| 57 | result.insert(i, "'\\'"); |
| 58 | i += 2; |
| 59 | } |
Elliott Hughes | 5830577 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 60 | } |
Elliott Hughes | 5498ade | 2015-04-17 20:50:11 -0700 | [diff] [blame] | 61 | |
| 62 | // Prefix and suffix the whole string with '. |
| 63 | result.insert(result.begin(), '\''); |
| 64 | result.push_back('\''); |
Elliott Hughes | 5830577 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 65 | return result; |
| 66 | } |
Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 67 | |
Alex Vallée | 1421614 | 2015-05-06 17:22:25 -0400 | [diff] [blame] | 68 | int mkdirs(const char *path) |
| 69 | { |
| 70 | int ret; |
| 71 | char *x = (char *)path + 1; |
| 72 | |
| 73 | for(;;) { |
| 74 | x = adb_dirstart(x); |
| 75 | if(x == 0) return 0; |
| 76 | *x = 0; |
| 77 | ret = adb_mkdir(path, 0775); |
| 78 | *x = OS_PATH_SEPARATOR; |
| 79 | if((ret < 0) && (errno != EEXIST)) { |
| 80 | return ret; |
| 81 | } |
| 82 | x++; |
| 83 | } |
| 84 | return 0; |
| 85 | } |
| 86 | |
Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 87 | void dump_hex(const void* data, size_t byte_count) { |
| 88 | byte_count = std::min(byte_count, size_t(16)); |
| 89 | |
| 90 | const uint8_t* p = reinterpret_cast<const uint8_t*>(data); |
| 91 | |
| 92 | std::string line; |
| 93 | for (size_t i = 0; i < byte_count; ++i) { |
| 94 | android::base::StringAppendF(&line, "%02x", p[i]); |
| 95 | } |
| 96 | line.push_back(' '); |
| 97 | |
| 98 | for (size_t i = 0; i < byte_count; ++i) { |
| 99 | int c = p[i]; |
| 100 | if (c < 32 || c > 127) { |
| 101 | c = '.'; |
| 102 | } |
| 103 | line.push_back(c); |
| 104 | } |
| 105 | |
| 106 | DR("%s\n", line.c_str()); |
| 107 | } |
Elliott Hughes | 3d5f60d | 2015-07-18 12:21:30 -0700 | [diff] [blame] | 108 | |
| 109 | bool parse_host_and_port(const std::string& address, |
| 110 | std::string* canonical_address, |
| 111 | std::string* host, int* port, |
| 112 | std::string* error) { |
| 113 | host->clear(); |
| 114 | |
| 115 | bool ipv6 = true; |
| 116 | bool saw_port = false; |
| 117 | size_t colons = std::count(address.begin(), address.end(), ':'); |
| 118 | size_t dots = std::count(address.begin(), address.end(), '.'); |
| 119 | std::string port_str; |
| 120 | if (address[0] == '[') { |
| 121 | // [::1]:123 |
| 122 | if (address.rfind("]:") == std::string::npos) { |
| 123 | *error = android::base::StringPrintf("bad IPv6 address '%s'", address.c_str()); |
| 124 | return false; |
| 125 | } |
| 126 | *host = address.substr(1, (address.find("]:") - 1)); |
| 127 | port_str = address.substr(address.rfind("]:") + 2); |
| 128 | saw_port = true; |
| 129 | } else if (dots == 0 && colons >= 2 && colons <= 7) { |
| 130 | // ::1 |
| 131 | *host = address; |
| 132 | } else if (colons <= 1) { |
| 133 | // 1.2.3.4 or some.accidental.domain.com |
| 134 | ipv6 = false; |
| 135 | std::vector<std::string> pieces = android::base::Split(address, ":"); |
| 136 | *host = pieces[0]; |
| 137 | if (pieces.size() > 1) { |
| 138 | port_str = pieces[1]; |
| 139 | saw_port = true; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | if (host->empty()) { |
| 144 | *error = android::base::StringPrintf("no host in '%s'", address.c_str()); |
| 145 | return false; |
| 146 | } |
| 147 | |
| 148 | if (saw_port) { |
| 149 | if (sscanf(port_str.c_str(), "%d", port) != 1 || *port <= 0 || *port > 65535) { |
| 150 | *error = android::base::StringPrintf("bad port number '%s' in '%s'", |
| 151 | port_str.c_str(), address.c_str()); |
| 152 | return false; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | *canonical_address = android::base::StringPrintf(ipv6 ? "[%s]:%d" : "%s:%d", host->c_str(), *port); |
| 157 | LOG(DEBUG) << "parsed " << address << " as " << *host << " and " << *port |
| 158 | << " (" << *canonical_address << ")"; |
| 159 | return true; |
| 160 | } |