• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #pragma once
18 
19 #include <condition_variable>
20 #include <mutex>
21 #include <string>
22 #include <string_view>
23 #include <type_traits>
24 #include <vector>
25 
26 #include <android-base/macros.h>
27 
28 #include "adb.h"
29 
30 void close_stdin();
31 
32 bool getcwd(std::string* cwd);
33 bool directory_exists(const std::string& path);
34 
35 // Return the user's home directory.
36 std::string adb_get_homedir_path();
37 
38 // Return the adb user directory.
39 std::string adb_get_android_dir_path();
40 
41 bool mkdirs(const std::string& path);
42 
43 std::string escape_arg(const std::string& s);
44 
45 std::string dump_hex(const void* ptr, size_t byte_count);
46 std::string dump_header(const amessage* msg);
47 std::string dump_packet(const char* name, const char* func, const apacket* p);
48 
49 std::string perror_str(const char* msg);
50 
51 [[noreturn]] void error_exit(const char* fmt, ...) __attribute__((__format__(__printf__, 1, 2)));
52 [[noreturn]] void perror_exit(const char* fmt, ...) __attribute__((__format__(__printf__, 1, 2)));
53 
54 bool set_file_block_mode(int fd, bool block);
55 
56 // Given forward/reverse targets, returns true if they look sane. If an error is found, fills
57 // |error| and returns false.
58 // Currently this only checks "tcp:" targets. Additional checking could be added for other targets
59 // if needed.
60 bool forward_targets_are_valid(const std::string& source, const std::string& dest,
61                                std::string* error);
62 
63 // A thread-safe blocking queue.
64 template <typename T>
65 class BlockingQueue {
66     std::mutex mutex;
67     std::condition_variable cv;
68     std::vector<T> queue;
69 
70   public:
Push(const T & t)71     void Push(const T& t) {
72         {
73             std::unique_lock<std::mutex> lock(mutex);
74             queue.push_back(t);
75         }
76         cv.notify_one();
77     }
78 
79     template <typename Fn>
PopAll(Fn fn)80     void PopAll(Fn fn) {
81         std::vector<T> popped;
82 
83         {
84             std::unique_lock<std::mutex> lock(mutex);
85             cv.wait(lock, [this]() { return !queue.empty(); });
86             popped = std::move(queue);
87             queue.clear();
88         }
89 
90         for (const T& t : popped) {
91             fn(t);
92         }
93     }
94 };
95 
96 std::string GetLogFilePath();
97 
StripTrailingNulls(std::string_view str)98 inline std::string_view StripTrailingNulls(std::string_view str) {
99     size_t n = 0;
100     for (auto it = str.rbegin(); it != str.rend(); ++it) {
101         if (*it != '\0') {
102             break;
103         }
104         ++n;
105     }
106 
107     str.remove_suffix(n);
108     return str;
109 }
110 
111 // Base-10 stroll on a string_view.
112 template <typename T>
113 inline bool ParseUint(T* result, std::string_view str, std::string_view* remaining = nullptr) {
114     if (str.empty() || !isdigit(str[0])) {
115         return false;
116     }
117 
118     T value = 0;
119     std::string_view::iterator it;
120     constexpr T max = std::numeric_limits<T>::max();
121     for (it = str.begin(); it != str.end() && isdigit(*it); ++it) {
122         if (value > max / 10) {
123             return false;
124         }
125 
126         value *= 10;
127 
128         T digit = *it - '0';
129         if (value > max - digit) {
130             return false;
131         }
132 
133         value += digit;
134     }
135     *result = value;
136     if (remaining) {
137         *remaining = str.substr(it - str.begin());
138     } else {
139       return it == str.end();
140     }
141 
142     return true;
143 }
144 
ConsumePrefix(std::string_view * str,std::string_view prefix)145 inline bool ConsumePrefix(std::string_view* str, std::string_view prefix) {
146   if (str->starts_with(prefix)) {
147     str->remove_prefix(prefix.size());
148     return true;
149   }
150   return false;
151 }
152