blob: 7d8ba01c2c650e2681679d7130d111f40eb7b838 [file] [log] [blame]
Elliott Hughes38104452015-04-17 13:57:15 -07001/*
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 Hughes0119a912018-10-22 17:02:51 -070017#pragma once
Elliott Hughes38104452015-04-17 13:57:15 -070018
Yurii Zubrytskyi1a242f52019-07-12 00:16:59 -070019#include <charconv>
Josh Gao4b640842017-05-31 11:54:56 -070020#include <condition_variable>
21#include <mutex>
Elliott Hughes38104452015-04-17 13:57:15 -070022#include <string>
Josh Gao4219a7b2018-12-20 13:17:04 -080023#include <string_view>
24#include <type_traits>
Josh Gao4b640842017-05-31 11:54:56 -070025#include <vector>
Elliott Hughes38104452015-04-17 13:57:15 -070026
Josh Gao1b533c82016-03-04 15:15:56 -080027#include <android-base/macros.h>
28
Josh Gao7d13c592018-10-11 13:49:24 -070029#include "adb.h"
Josh Gao90228a62019-04-25 14:04:57 -070030#include "adb_unique_fd.h"
Josh Gao7d13c592018-10-11 13:49:24 -070031
Josh Gao1eef4782015-11-20 15:37:31 -080032void close_stdin();
33
Elliott Hughes7cf35752015-04-17 17:03:59 -070034bool getcwd(std::string* cwd);
Elliott Hughes38104452015-04-17 13:57:15 -070035bool directory_exists(const std::string& path);
Spencer Low939d0002015-08-01 17:29:23 -070036
Yurii Zubrytskyi70ae70a2016-05-25 15:17:10 -070037// Return the user's home directory.
Josh Gao62ff9d42016-08-30 15:23:35 -070038std::string adb_get_homedir_path();
39
40// Return the adb user directory.
41std::string adb_get_android_dir_path();
Yurii Zubrytskyi70ae70a2016-05-25 15:17:10 -070042
Alex Vallée28d1f8d2015-05-06 16:26:00 -040043bool mkdirs(const std::string& path);
Alex Valléee9163152015-05-06 17:22:25 -040044
Elliott Hughes38104452015-04-17 13:57:15 -070045std::string escape_arg(const std::string& s);
46
Yabin Cui19bec5b2015-09-22 15:52:57 -070047std::string dump_hex(const void* ptr, size_t byte_count);
Josh Gao7d13c592018-10-11 13:49:24 -070048std::string dump_header(const amessage* msg);
49std::string dump_packet(const char* name, const char* func, const apacket* p);
Elliott Hughes88b4c852015-04-30 17:32:03 -070050
Elliott Hughesb628cb12015-08-03 10:38:08 -070051std::string perror_str(const char* msg);
52
Josh Gao90228a62019-04-25 14:04:57 -070053bool set_file_block_mode(borrowed_fd fd, bool block);
Yabin Cui5fc22312015-10-06 15:10:05 -070054
Jeff Sharkey83c0bce2020-07-31 15:25:43 -060055// Given forward/reverse targets, returns true if they look valid. If an error is found, fills
David Pursell19d0c232016-04-07 11:25:48 -070056// |error| and returns false.
57// Currently this only checks "tcp:" targets. Additional checking could be added for other targets
58// if needed.
59bool forward_targets_are_valid(const std::string& source, const std::string& dest,
60 std::string* error);
61
Josh Gao4b640842017-05-31 11:54:56 -070062// A thread-safe blocking queue.
63template <typename T>
64class BlockingQueue {
65 std::mutex mutex;
66 std::condition_variable cv;
67 std::vector<T> queue;
68
69 public:
70 void Push(const T& t) {
71 {
72 std::unique_lock<std::mutex> lock(mutex);
73 queue.push_back(t);
74 }
75 cv.notify_one();
76 }
77
78 template <typename Fn>
79 void PopAll(Fn fn) {
Josh Gao772d8352017-06-05 14:54:45 -070080 std::vector<T> popped;
Josh Gao4b640842017-05-31 11:54:56 -070081
Josh Gao772d8352017-06-05 14:54:45 -070082 {
83 std::unique_lock<std::mutex> lock(mutex);
84 cv.wait(lock, [this]() { return !queue.empty(); });
85 popped = std::move(queue);
86 queue.clear();
87 }
88
89 for (const T& t : popped) {
Josh Gao4b640842017-05-31 11:54:56 -070090 fn(t);
91 }
Josh Gao4b640842017-05-31 11:54:56 -070092 }
93};
94
Elliott Hughescd61ae32017-06-15 08:35:24 -070095std::string GetLogFilePath();
Josh Gaof0fa1e42018-12-13 13:06:03 -080096
97inline std::string_view StripTrailingNulls(std::string_view str) {
98 size_t n = 0;
99 for (auto it = str.rbegin(); it != str.rend(); ++it) {
100 if (*it != '\0') {
101 break;
102 }
103 ++n;
104 }
105
106 str.remove_suffix(n);
107 return str;
108}
Josh Gao4219a7b2018-12-20 13:17:04 -0800109
110// Base-10 stroll on a string_view.
111template <typename T>
Josh Gao8477cb42019-02-20 19:46:26 -0800112inline bool ParseUint(T* result, std::string_view str, std::string_view* remaining = nullptr) {
Yurii Zubrytskyi1a242f52019-07-12 00:16:59 -0700113 T value;
114 const auto res = std::from_chars(str.begin(), str.end(), value);
115 if (res.ec != std::errc{}) {
Josh Gao4219a7b2018-12-20 13:17:04 -0800116 return false;
117 }
Yurii Zubrytskyi1a242f52019-07-12 00:16:59 -0700118 if (res.ptr != str.end() && !remaining) {
119 return false;
120 }
121 if (remaining) {
122 *remaining = std::string_view(res.ptr, str.end() - res.ptr);
Josh Gao4219a7b2018-12-20 13:17:04 -0800123 }
124 *result = value;
Josh Gao4219a7b2018-12-20 13:17:04 -0800125 return true;
126}