Android-cuttlefish cvd tool
adb_utils.h
Go to the documentation of this file.
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 <charconv>
20#include <condition_variable>
21#include <mutex>
22#include <string>
23#include <string_view>
24#include <type_traits>
25#include <vector>
26
27#include <android-base/macros.h>
28
29#include "adb.h"
30#include "adb_unique_fd.h"
31
32void close_stdin();
33
34bool getcwd(std::string* cwd);
35bool directory_exists(const std::string& path);
36
37// Return the user's home directory.
38std::string adb_get_homedir_path();
39
40// Return the adb user directory.
41std::string adb_get_android_dir_path();
42
43bool mkdirs(const std::string& path);
44
45std::string escape_arg(const std::string& s);
46
47std::string dump_hex(const void* ptr, size_t byte_count);
48std::string dump_header(const amessage* msg);
49std::string dump_packet(const char* name, const char* func, const apacket* p);
50
51std::string perror_str(const char* msg);
52
53bool set_file_block_mode(borrowed_fd fd, bool block);
54
55// Given forward/reverse targets, returns true if they look valid. If an error is found, fills
56// |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
62// A thread-safe blocking queue.
63template <typename T>
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) {
80 std::vector<T> popped;
81
82 {
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) {
90 fn(t);
91 }
92 }
93};
94
95std::string GetLogFilePath();
96
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}
109
110// Base-10 stroll on a string_view.
111template <typename T>
112inline bool ParseUint(T* result, std::string_view str, std::string_view* remaining = nullptr) {
113 T value;
114 const auto res = std::from_chars(str.begin(), str.end(), value);
115 if (res.ec != std::errc{}) {
116 return false;
117 }
118 if (res.ptr != str.end() && !remaining) {
119 return false;
120 }
121 if (remaining) {
122 *remaining = std::string_view(res.ptr, str.end() - res.ptr);
123 }
124 *result = value;
125 return true;
126}
std::string adb_get_android_dir_path()
Definition: adb_utils.cpp:310
bool mkdirs(const std::string &path)
Definition: adb_utils.cpp:106
std::string perror_str(const char *msg)
Definition: adb_utils.cpp:231
bool forward_targets_are_valid(const std::string &source, const std::string &dest, std::string *error)
Definition: adb_utils.cpp:252
bool ParseUint(T *result, std::string_view str, std::string_view *remaining=nullptr)
Definition: adb_utils.h:112
std::string GetLogFilePath()
Definition: adb_utils.cpp:322
bool set_file_block_mode(borrowed_fd fd, bool block)
Definition: adb_utils.cpp:237
bool getcwd(std::string *cwd)
Definition: adb_utils.cpp:69
bool directory_exists(const std::string &path)
Definition: adb_utils.cpp:76
std::string_view StripTrailingNulls(std::string_view str)
Definition: adb_utils.h:97
std::string dump_header(const amessage *msg)
Definition: adb_utils.cpp:189
std::string escape_arg(const std::string &s)
Definition: adb_utils.cpp:81
std::string adb_get_homedir_path()
Definition: adb_utils.cpp:275
void close_stdin()
Definition: adb_utils.cpp:57
std::string dump_packet(const char *name, const char *func, const apacket *p)
Definition: adb_utils.cpp:221
std::string dump_hex(const void *ptr, size_t byte_count)
Definition: adb_utils.cpp:161
Definition: adb_utils.h:64
void Push(const T &t)
Definition: adb_utils.h:70
std::mutex mutex
Definition: adb_utils.h:65
void PopAll(Fn fn)
Definition: adb_utils.h:79
std::condition_variable cv
Definition: adb_utils.h:66
std::vector< T > queue
Definition: adb_utils.h:67
#define error(format, args...)
Definition: fec_private.h:201
constexpr const char * str
Definition: utils.h:180
Definition: types.h:148
Definition: unique_fd.h:292
Definition: types.h:157