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 | |
| 28 | #include <base/stringprintf.h> |
| 29 | |
| 30 | #include "adb_trace.h" |
Elliott Hughes | 53daee6 | 2015-04-19 13:17:01 -0700 | [diff] [blame] | 31 | #include "sysdeps.h" |
| 32 | |
Elliott Hughes | a7090b9 | 2015-04-17 17:03:59 -0700 | [diff] [blame] | 33 | bool getcwd(std::string* s) { |
| 34 | char* cwd = getcwd(nullptr, 0); |
| 35 | if (cwd != nullptr) *s = cwd; |
| 36 | free(cwd); |
| 37 | return (cwd != nullptr); |
| 38 | } |
| 39 | |
Elliott Hughes | 5830577 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 40 | bool directory_exists(const std::string& path) { |
| 41 | struct stat sb; |
| 42 | return lstat(path.c_str(), &sb) != -1 && S_ISDIR(sb.st_mode); |
| 43 | } |
| 44 | |
Elliott Hughes | 5830577 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 45 | std::string escape_arg(const std::string& s) { |
Elliott Hughes | 5498ade | 2015-04-17 20:50:11 -0700 | [diff] [blame] | 46 | std::string result = s; |
Elliott Hughes | 5830577 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 47 | |
Elliott Hughes | 5498ade | 2015-04-17 20:50:11 -0700 | [diff] [blame] | 48 | // Insert a \ before any ' in the string. |
Elliott Hughes | 5830577 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 49 | for (auto it = result.begin(); it != result.end(); ++it) { |
Elliott Hughes | 5498ade | 2015-04-17 20:50:11 -0700 | [diff] [blame] | 50 | if (*it == '\'') { |
Elliott Hughes | 5830577 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 51 | it = result.insert(it, '\\') + 1; |
| 52 | } |
| 53 | } |
Elliott Hughes | 5498ade | 2015-04-17 20:50:11 -0700 | [diff] [blame] | 54 | |
| 55 | // Prefix and suffix the whole string with '. |
| 56 | result.insert(result.begin(), '\''); |
| 57 | result.push_back('\''); |
Elliott Hughes | 5830577 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 58 | return result; |
| 59 | } |
Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame^] | 60 | |
| 61 | void dump_hex(const void* data, size_t byte_count) { |
| 62 | byte_count = std::min(byte_count, size_t(16)); |
| 63 | |
| 64 | const uint8_t* p = reinterpret_cast<const uint8_t*>(data); |
| 65 | |
| 66 | std::string line; |
| 67 | for (size_t i = 0; i < byte_count; ++i) { |
| 68 | android::base::StringAppendF(&line, "%02x", p[i]); |
| 69 | } |
| 70 | line.push_back(' '); |
| 71 | |
| 72 | for (size_t i = 0; i < byte_count; ++i) { |
| 73 | int c = p[i]; |
| 74 | if (c < 32 || c > 127) { |
| 75 | c = '.'; |
| 76 | } |
| 77 | line.push_back(c); |
| 78 | } |
| 79 | |
| 80 | DR("%s\n", line.c_str()); |
| 81 | } |