Elliott Hughes | 3810445 | 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 | 0119a91 | 2018-10-22 17:02:51 -0700 | [diff] [blame] | 17 | #pragma once |
Elliott Hughes | 3810445 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 18 | |
Yurii Zubrytskyi | 1a242f5 | 2019-07-12 00:16:59 -0700 | [diff] [blame] | 19 | #include <charconv> |
Josh Gao | 4b64084 | 2017-05-31 11:54:56 -0700 | [diff] [blame] | 20 | #include <condition_variable> |
| 21 | #include <mutex> |
Elliott Hughes | 3810445 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 22 | #include <string> |
Josh Gao | 4219a7b | 2018-12-20 13:17:04 -0800 | [diff] [blame] | 23 | #include <string_view> |
| 24 | #include <type_traits> |
Josh Gao | 4b64084 | 2017-05-31 11:54:56 -0700 | [diff] [blame] | 25 | #include <vector> |
Elliott Hughes | 3810445 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 26 | |
Josh Gao | 1b533c8 | 2016-03-04 15:15:56 -0800 | [diff] [blame] | 27 | #include <android-base/macros.h> |
| 28 | |
Josh Gao | 7d13c59 | 2018-10-11 13:49:24 -0700 | [diff] [blame] | 29 | #include "adb.h" |
Josh Gao | 90228a6 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 30 | #include "adb_unique_fd.h" |
Josh Gao | 7d13c59 | 2018-10-11 13:49:24 -0700 | [diff] [blame] | 31 | |
Josh Gao | 1eef478 | 2015-11-20 15:37:31 -0800 | [diff] [blame] | 32 | void close_stdin(); |
| 33 | |
Elliott Hughes | 7cf3575 | 2015-04-17 17:03:59 -0700 | [diff] [blame] | 34 | bool getcwd(std::string* cwd); |
Elliott Hughes | 3810445 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 35 | bool directory_exists(const std::string& path); |
Spencer Low | 939d000 | 2015-08-01 17:29:23 -0700 | [diff] [blame] | 36 | |
Yurii Zubrytskyi | 70ae70a | 2016-05-25 15:17:10 -0700 | [diff] [blame] | 37 | // Return the user's home directory. |
Josh Gao | 62ff9d4 | 2016-08-30 15:23:35 -0700 | [diff] [blame] | 38 | std::string adb_get_homedir_path(); |
| 39 | |
| 40 | // Return the adb user directory. |
| 41 | std::string adb_get_android_dir_path(); |
Yurii Zubrytskyi | 70ae70a | 2016-05-25 15:17:10 -0700 | [diff] [blame] | 42 | |
Alex Vallée | 28d1f8d | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 43 | bool mkdirs(const std::string& path); |
Alex Vallée | e916315 | 2015-05-06 17:22:25 -0400 | [diff] [blame] | 44 | |
Elliott Hughes | 3810445 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 45 | std::string escape_arg(const std::string& s); |
| 46 | |
Yabin Cui | 19bec5b | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 47 | std::string dump_hex(const void* ptr, size_t byte_count); |
Josh Gao | 7d13c59 | 2018-10-11 13:49:24 -0700 | [diff] [blame] | 48 | std::string dump_header(const amessage* msg); |
| 49 | std::string dump_packet(const char* name, const char* func, const apacket* p); |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 50 | |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 51 | std::string perror_str(const char* msg); |
| 52 | |
Elliott Hughes | 0119a91 | 2018-10-22 17:02:51 -0700 | [diff] [blame] | 53 | [[noreturn]] void error_exit(const char* fmt, ...) __attribute__((__format__(__printf__, 1, 2))); |
| 54 | [[noreturn]] void perror_exit(const char* fmt, ...) __attribute__((__format__(__printf__, 1, 2))); |
| 55 | |
Josh Gao | 90228a6 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 56 | bool set_file_block_mode(borrowed_fd fd, bool block); |
Yabin Cui | 5fc2231 | 2015-10-06 15:10:05 -0700 | [diff] [blame] | 57 | |
Jeff Sharkey | 83c0bce | 2020-07-31 15:25:43 -0600 | [diff] [blame] | 58 | // Given forward/reverse targets, returns true if they look valid. If an error is found, fills |
David Pursell | 19d0c23 | 2016-04-07 11:25:48 -0700 | [diff] [blame] | 59 | // |error| and returns false. |
| 60 | // Currently this only checks "tcp:" targets. Additional checking could be added for other targets |
| 61 | // if needed. |
| 62 | bool forward_targets_are_valid(const std::string& source, const std::string& dest, |
| 63 | std::string* error); |
| 64 | |
Josh Gao | 4b64084 | 2017-05-31 11:54:56 -0700 | [diff] [blame] | 65 | // A thread-safe blocking queue. |
| 66 | template <typename T> |
| 67 | class BlockingQueue { |
| 68 | std::mutex mutex; |
| 69 | std::condition_variable cv; |
| 70 | std::vector<T> queue; |
| 71 | |
| 72 | public: |
| 73 | void Push(const T& t) { |
| 74 | { |
| 75 | std::unique_lock<std::mutex> lock(mutex); |
| 76 | queue.push_back(t); |
| 77 | } |
| 78 | cv.notify_one(); |
| 79 | } |
| 80 | |
| 81 | template <typename Fn> |
| 82 | void PopAll(Fn fn) { |
Josh Gao | 772d835 | 2017-06-05 14:54:45 -0700 | [diff] [blame] | 83 | std::vector<T> popped; |
Josh Gao | 4b64084 | 2017-05-31 11:54:56 -0700 | [diff] [blame] | 84 | |
Josh Gao | 772d835 | 2017-06-05 14:54:45 -0700 | [diff] [blame] | 85 | { |
| 86 | std::unique_lock<std::mutex> lock(mutex); |
| 87 | cv.wait(lock, [this]() { return !queue.empty(); }); |
| 88 | popped = std::move(queue); |
| 89 | queue.clear(); |
| 90 | } |
| 91 | |
| 92 | for (const T& t : popped) { |
Josh Gao | 4b64084 | 2017-05-31 11:54:56 -0700 | [diff] [blame] | 93 | fn(t); |
| 94 | } |
Josh Gao | 4b64084 | 2017-05-31 11:54:56 -0700 | [diff] [blame] | 95 | } |
| 96 | }; |
| 97 | |
Elliott Hughes | cd61ae3 | 2017-06-15 08:35:24 -0700 | [diff] [blame] | 98 | std::string GetLogFilePath(); |
Josh Gao | f0fa1e4 | 2018-12-13 13:06:03 -0800 | [diff] [blame] | 99 | |
| 100 | inline std::string_view StripTrailingNulls(std::string_view str) { |
| 101 | size_t n = 0; |
| 102 | for (auto it = str.rbegin(); it != str.rend(); ++it) { |
| 103 | if (*it != '\0') { |
| 104 | break; |
| 105 | } |
| 106 | ++n; |
| 107 | } |
| 108 | |
| 109 | str.remove_suffix(n); |
| 110 | return str; |
| 111 | } |
Josh Gao | 4219a7b | 2018-12-20 13:17:04 -0800 | [diff] [blame] | 112 | |
| 113 | // Base-10 stroll on a string_view. |
| 114 | template <typename T> |
Josh Gao | 8477cb4 | 2019-02-20 19:46:26 -0800 | [diff] [blame] | 115 | inline bool ParseUint(T* result, std::string_view str, std::string_view* remaining = nullptr) { |
Yurii Zubrytskyi | 1a242f5 | 2019-07-12 00:16:59 -0700 | [diff] [blame] | 116 | T value; |
| 117 | const auto res = std::from_chars(str.begin(), str.end(), value); |
| 118 | if (res.ec != std::errc{}) { |
Josh Gao | 4219a7b | 2018-12-20 13:17:04 -0800 | [diff] [blame] | 119 | return false; |
| 120 | } |
Yurii Zubrytskyi | 1a242f5 | 2019-07-12 00:16:59 -0700 | [diff] [blame] | 121 | if (res.ptr != str.end() && !remaining) { |
| 122 | return false; |
| 123 | } |
| 124 | if (remaining) { |
| 125 | *remaining = std::string_view(res.ptr, str.end() - res.ptr); |
Josh Gao | 4219a7b | 2018-12-20 13:17:04 -0800 | [diff] [blame] | 126 | } |
| 127 | *result = value; |
Josh Gao | 4219a7b | 2018-12-20 13:17:04 -0800 | [diff] [blame] | 128 | return true; |
| 129 | } |