adb_utils.cpp revision 5200c6670f041550c23821fec8e8e49b30ef6d29
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 17#define TRACE_TAG TRACE_ADB 18 19#include "adb_utils.h" 20 21#include <stdlib.h> 22#include <sys/stat.h> 23#include <sys/types.h> 24#include <unistd.h> 25 26#include <algorithm> 27 28#include <base/logging.h> 29#include <base/stringprintf.h> 30#include <base/strings.h> 31 32#include "adb_trace.h" 33#include "sysdeps.h" 34 35bool 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 42bool 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 47std::string escape_arg(const std::string& s) { 48 std::string result = s; 49 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 } 60 } 61 62 // Prefix and suffix the whole string with '. 63 result.insert(result.begin(), '\''); 64 result.push_back('\''); 65 return result; 66} 67 68std::string adb_basename(const std::string& path) { 69 size_t base = path.find_last_of(OS_PATH_SEPARATORS); 70 return (base != std::string::npos) ? path.substr(base + 1) : path; 71} 72 73static bool real_mkdirs(const std::string& path) { 74 std::vector<std::string> path_components = android::base::Split(path, OS_PATH_SEPARATOR_STR); 75 // TODO: all the callers do unlink && mkdirs && adb_creat --- 76 // that's probably the operation we should expose. 77 path_components.pop_back(); 78 std::string partial_path; 79 for (const auto& path_component : path_components) { 80 if (partial_path.back() != OS_PATH_SEPARATOR) partial_path += OS_PATH_SEPARATOR; 81 partial_path += path_component; 82 if (adb_mkdir(partial_path.c_str(), 0775) == -1 && errno != EEXIST) { 83 return false; 84 } 85 } 86 return true; 87} 88 89bool mkdirs(const std::string& path) { 90#if defined(_WIN32) 91 // Replace '/' with '\\' so we can share the code. 92 std::string clean_path = path; 93 std::replace(clean_path.begin(), clean_path.end(), '/', '\\'); 94 return real_mkdirs(clean_path); 95#else 96 return real_mkdirs(path); 97#endif 98} 99 100void dump_hex(const void* data, size_t byte_count) { 101 byte_count = std::min(byte_count, size_t(16)); 102 103 const uint8_t* p = reinterpret_cast<const uint8_t*>(data); 104 105 std::string line; 106 for (size_t i = 0; i < byte_count; ++i) { 107 android::base::StringAppendF(&line, "%02x", p[i]); 108 } 109 line.push_back(' '); 110 111 for (size_t i = 0; i < byte_count; ++i) { 112 int c = p[i]; 113 if (c < 32 || c > 127) { 114 c = '.'; 115 } 116 line.push_back(c); 117 } 118 119 DR("%s\n", line.c_str()); 120} 121 122bool parse_host_and_port(const std::string& address, 123 std::string* canonical_address, 124 std::string* host, int* port, 125 std::string* error) { 126 host->clear(); 127 128 bool ipv6 = true; 129 bool saw_port = false; 130 size_t colons = std::count(address.begin(), address.end(), ':'); 131 size_t dots = std::count(address.begin(), address.end(), '.'); 132 std::string port_str; 133 if (address[0] == '[') { 134 // [::1]:123 135 if (address.rfind("]:") == std::string::npos) { 136 *error = android::base::StringPrintf("bad IPv6 address '%s'", address.c_str()); 137 return false; 138 } 139 *host = address.substr(1, (address.find("]:") - 1)); 140 port_str = address.substr(address.rfind("]:") + 2); 141 saw_port = true; 142 } else if (dots == 0 && colons >= 2 && colons <= 7) { 143 // ::1 144 *host = address; 145 } else if (colons <= 1) { 146 // 1.2.3.4 or some.accidental.domain.com 147 ipv6 = false; 148 std::vector<std::string> pieces = android::base::Split(address, ":"); 149 *host = pieces[0]; 150 if (pieces.size() > 1) { 151 port_str = pieces[1]; 152 saw_port = true; 153 } 154 } 155 156 if (host->empty()) { 157 *error = android::base::StringPrintf("no host in '%s'", address.c_str()); 158 return false; 159 } 160 161 if (saw_port) { 162 if (sscanf(port_str.c_str(), "%d", port) != 1 || *port <= 0 || *port > 65535) { 163 *error = android::base::StringPrintf("bad port number '%s' in '%s'", 164 port_str.c_str(), address.c_str()); 165 return false; 166 } 167 } 168 169 *canonical_address = android::base::StringPrintf(ipv6 ? "[%s]:%d" : "%s:%d", host->c_str(), *port); 170 LOG(DEBUG) << "parsed " << address << " as " << *host << " and " << *port 171 << " (" << *canonical_address << ")"; 172 return true; 173} 174