blob: 442ca2b36a70a6ac06d239bf2d821b75892951e7 [file] [log] [blame]
Elliott Hughes58305772015-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
17#ifndef _ADB_UTILS_H_
18#define _ADB_UTILS_H_
19
Josh Gao0b13c892017-05-31 11:54:56 -070020#include <condition_variable>
21#include <mutex>
Elliott Hughes58305772015-04-17 13:57:15 -070022#include <string>
Josh Gao0b13c892017-05-31 11:54:56 -070023#include <vector>
Elliott Hughes58305772015-04-17 13:57:15 -070024
Josh Gaof0d3b4f2016-03-04 15:15:56 -080025#include <android-base/macros.h>
26
Josh Gao99d3d702018-10-11 13:49:24 -070027#include "adb.h"
28
Josh Gao7d586072015-11-20 15:37:31 -080029void close_stdin();
30
Elliott Hughesa7090b92015-04-17 17:03:59 -070031bool getcwd(std::string* cwd);
Elliott Hughes58305772015-04-17 13:57:15 -070032bool directory_exists(const std::string& path);
Spencer Low22191c32015-08-01 17:29:23 -070033
Yurii Zubrytskyi376b2752016-05-25 15:17:10 -070034// Return the user's home directory.
Josh Gaoe0b75022016-08-30 15:23:35 -070035std::string adb_get_homedir_path();
36
37// Return the adb user directory.
38std::string adb_get_android_dir_path();
Yurii Zubrytskyi376b2752016-05-25 15:17:10 -070039
Alex Vallée47d67c92015-05-06 16:26:00 -040040bool mkdirs(const std::string& path);
Alex Vallée14216142015-05-06 17:22:25 -040041
Elliott Hughes58305772015-04-17 13:57:15 -070042std::string escape_arg(const std::string& s);
43
Yabin Cuiaed3c612015-09-22 15:52:57 -070044std::string dump_hex(const void* ptr, size_t byte_count);
Josh Gao99d3d702018-10-11 13:49:24 -070045std::string dump_header(const amessage* msg);
46std::string dump_packet(const char* name, const char* func, const apacket* p);
Elliott Hughese67f1f82015-04-30 17:32:03 -070047
Elliott Hughesaa245492015-08-03 10:38:08 -070048std::string perror_str(const char* msg);
49
Yabin Cui6dfef252015-10-06 15:10:05 -070050bool set_file_block_mode(int fd, bool block);
51
Josh Gaof0d3b4f2016-03-04 15:15:56 -080052extern int adb_close(int fd);
53
David Purselleaae97e2016-04-07 11:25:48 -070054// Given forward/reverse targets, returns true if they look sane. If an error is found, fills
55// |error| and returns false.
56// Currently this only checks "tcp:" targets. Additional checking could be added for other targets
57// if needed.
58bool forward_targets_are_valid(const std::string& source, const std::string& dest,
59 std::string* error);
60
Josh Gao0b13c892017-05-31 11:54:56 -070061// A thread-safe blocking queue.
62template <typename T>
63class BlockingQueue {
64 std::mutex mutex;
65 std::condition_variable cv;
66 std::vector<T> queue;
67
68 public:
69 void Push(const T& t) {
70 {
71 std::unique_lock<std::mutex> lock(mutex);
72 queue.push_back(t);
73 }
74 cv.notify_one();
75 }
76
77 template <typename Fn>
78 void PopAll(Fn fn) {
Josh Gao664a6182017-06-05 14:54:45 -070079 std::vector<T> popped;
Josh Gao0b13c892017-05-31 11:54:56 -070080
Josh Gao664a6182017-06-05 14:54:45 -070081 {
82 std::unique_lock<std::mutex> lock(mutex);
83 cv.wait(lock, [this]() { return !queue.empty(); });
84 popped = std::move(queue);
85 queue.clear();
86 }
87
88 for (const T& t : popped) {
Josh Gao0b13c892017-05-31 11:54:56 -070089 fn(t);
90 }
Josh Gao0b13c892017-05-31 11:54:56 -070091 }
92};
93
Elliott Hughes6eadee82017-06-15 08:35:24 -070094std::string GetLogFilePath();
95
Elliott Hughes58305772015-04-17 13:57:15 -070096#endif