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 | d2aaede | 2018-10-22 17:02:51 -0700 | [diff] [blame] | 17 | #pragma once |
Elliott Hughes | 5830577 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 18 | |
Josh Gao | 0b13c89 | 2017-05-31 11:54:56 -0700 | [diff] [blame] | 19 | #include <condition_variable> |
| 20 | #include <mutex> |
Elliott Hughes | 5830577 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 21 | #include <string> |
Josh Gao | 3add0c4 | 2018-12-20 13:17:04 -0800 | [diff] [blame] | 22 | #include <string_view> |
| 23 | #include <type_traits> |
Josh Gao | 0b13c89 | 2017-05-31 11:54:56 -0700 | [diff] [blame] | 24 | #include <vector> |
Elliott Hughes | 5830577 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 25 | |
Josh Gao | f0d3b4f | 2016-03-04 15:15:56 -0800 | [diff] [blame] | 26 | #include <android-base/macros.h> |
| 27 | |
Josh Gao | 99d3d70 | 2018-10-11 13:49:24 -0700 | [diff] [blame] | 28 | #include "adb.h" |
Josh Gao | 27241a7 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 29 | #include "adb_unique_fd.h" |
Josh Gao | 99d3d70 | 2018-10-11 13:49:24 -0700 | [diff] [blame] | 30 | |
Josh Gao | 7d58607 | 2015-11-20 15:37:31 -0800 | [diff] [blame] | 31 | void close_stdin(); |
| 32 | |
Elliott Hughes | a7090b9 | 2015-04-17 17:03:59 -0700 | [diff] [blame] | 33 | bool getcwd(std::string* cwd); |
Elliott Hughes | 5830577 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 34 | bool directory_exists(const std::string& path); |
Spencer Low | 22191c3 | 2015-08-01 17:29:23 -0700 | [diff] [blame] | 35 | |
Yurii Zubrytskyi | 376b275 | 2016-05-25 15:17:10 -0700 | [diff] [blame] | 36 | // Return the user's home directory. |
Josh Gao | e0b7502 | 2016-08-30 15:23:35 -0700 | [diff] [blame] | 37 | std::string adb_get_homedir_path(); |
| 38 | |
| 39 | // Return the adb user directory. |
| 40 | std::string adb_get_android_dir_path(); |
Yurii Zubrytskyi | 376b275 | 2016-05-25 15:17:10 -0700 | [diff] [blame] | 41 | |
Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 42 | bool mkdirs(const std::string& path); |
Alex Vallée | 1421614 | 2015-05-06 17:22:25 -0400 | [diff] [blame] | 43 | |
Elliott Hughes | 5830577 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 44 | std::string escape_arg(const std::string& s); |
| 45 | |
Yabin Cui | aed3c61 | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 46 | std::string dump_hex(const void* ptr, size_t byte_count); |
Josh Gao | 99d3d70 | 2018-10-11 13:49:24 -0700 | [diff] [blame] | 47 | std::string dump_header(const amessage* msg); |
| 48 | std::string dump_packet(const char* name, const char* func, const apacket* p); |
Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 49 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 50 | std::string perror_str(const char* msg); |
| 51 | |
Elliott Hughes | d2aaede | 2018-10-22 17:02:51 -0700 | [diff] [blame] | 52 | [[noreturn]] void error_exit(const char* fmt, ...) __attribute__((__format__(__printf__, 1, 2))); |
| 53 | [[noreturn]] void perror_exit(const char* fmt, ...) __attribute__((__format__(__printf__, 1, 2))); |
| 54 | |
Josh Gao | 27241a7 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 55 | bool set_file_block_mode(borrowed_fd fd, bool block); |
Yabin Cui | 6dfef25 | 2015-10-06 15:10:05 -0700 | [diff] [blame] | 56 | |
David Pursell | eaae97e | 2016-04-07 11:25:48 -0700 | [diff] [blame] | 57 | // Given forward/reverse targets, returns true if they look sane. If an error is found, fills |
| 58 | // |error| and returns false. |
| 59 | // Currently this only checks "tcp:" targets. Additional checking could be added for other targets |
| 60 | // if needed. |
| 61 | bool forward_targets_are_valid(const std::string& source, const std::string& dest, |
| 62 | std::string* error); |
| 63 | |
Josh Gao | 0b13c89 | 2017-05-31 11:54:56 -0700 | [diff] [blame] | 64 | // A thread-safe blocking queue. |
| 65 | template <typename T> |
| 66 | class BlockingQueue { |
| 67 | std::mutex mutex; |
| 68 | std::condition_variable cv; |
| 69 | std::vector<T> queue; |
| 70 | |
| 71 | public: |
| 72 | void Push(const T& t) { |
| 73 | { |
| 74 | std::unique_lock<std::mutex> lock(mutex); |
| 75 | queue.push_back(t); |
| 76 | } |
| 77 | cv.notify_one(); |
| 78 | } |
| 79 | |
| 80 | template <typename Fn> |
| 81 | void PopAll(Fn fn) { |
Josh Gao | 664a618 | 2017-06-05 14:54:45 -0700 | [diff] [blame] | 82 | std::vector<T> popped; |
Josh Gao | 0b13c89 | 2017-05-31 11:54:56 -0700 | [diff] [blame] | 83 | |
Josh Gao | 664a618 | 2017-06-05 14:54:45 -0700 | [diff] [blame] | 84 | { |
| 85 | std::unique_lock<std::mutex> lock(mutex); |
| 86 | cv.wait(lock, [this]() { return !queue.empty(); }); |
| 87 | popped = std::move(queue); |
| 88 | queue.clear(); |
| 89 | } |
| 90 | |
| 91 | for (const T& t : popped) { |
Josh Gao | 0b13c89 | 2017-05-31 11:54:56 -0700 | [diff] [blame] | 92 | fn(t); |
| 93 | } |
Josh Gao | 0b13c89 | 2017-05-31 11:54:56 -0700 | [diff] [blame] | 94 | } |
| 95 | }; |
| 96 | |
Elliott Hughes | 6eadee8 | 2017-06-15 08:35:24 -0700 | [diff] [blame] | 97 | std::string GetLogFilePath(); |
Josh Gao | e261541 | 2018-12-13 13:06:03 -0800 | [diff] [blame] | 98 | |
| 99 | inline std::string_view StripTrailingNulls(std::string_view str) { |
| 100 | size_t n = 0; |
| 101 | for (auto it = str.rbegin(); it != str.rend(); ++it) { |
| 102 | if (*it != '\0') { |
| 103 | break; |
| 104 | } |
| 105 | ++n; |
| 106 | } |
| 107 | |
| 108 | str.remove_suffix(n); |
| 109 | return str; |
| 110 | } |
Josh Gao | 3add0c4 | 2018-12-20 13:17:04 -0800 | [diff] [blame] | 111 | |
| 112 | // Base-10 stroll on a string_view. |
| 113 | template <typename T> |
Josh Gao | aeca208 | 2019-02-20 19:46:26 -0800 | [diff] [blame] | 114 | inline bool ParseUint(T* result, std::string_view str, std::string_view* remaining = nullptr) { |
Josh Gao | 3add0c4 | 2018-12-20 13:17:04 -0800 | [diff] [blame] | 115 | if (str.empty() || !isdigit(str[0])) { |
| 116 | return false; |
| 117 | } |
| 118 | |
| 119 | T value = 0; |
| 120 | std::string_view::iterator it; |
| 121 | constexpr T max = std::numeric_limits<T>::max(); |
| 122 | for (it = str.begin(); it != str.end() && isdigit(*it); ++it) { |
| 123 | if (value > max / 10) { |
| 124 | return false; |
| 125 | } |
| 126 | |
| 127 | value *= 10; |
| 128 | |
| 129 | T digit = *it - '0'; |
| 130 | if (value > max - digit) { |
| 131 | return false; |
| 132 | } |
| 133 | |
| 134 | value += digit; |
| 135 | } |
| 136 | *result = value; |
| 137 | if (remaining) { |
| 138 | *remaining = str.substr(it - str.begin()); |
Josh Gao | aeca208 | 2019-02-20 19:46:26 -0800 | [diff] [blame] | 139 | } else { |
| 140 | return it == str.end(); |
Josh Gao | 3add0c4 | 2018-12-20 13:17:04 -0800 | [diff] [blame] | 141 | } |
Josh Gao | aeca208 | 2019-02-20 19:46:26 -0800 | [diff] [blame] | 142 | |
Josh Gao | 3add0c4 | 2018-12-20 13:17:04 -0800 | [diff] [blame] | 143 | return true; |
| 144 | } |