blob: 22eb4d2f051b7663f3d8e34469ef7a094dda3449 [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
17#ifndef _ADB_UTILS_H_
18#define _ADB_UTILS_H_
19
20#include <string>
21
Josh Gao1b533c82016-03-04 15:15:56 -080022#include <android-base/macros.h>
Josh Gaod91139c2016-05-13 14:52:06 -070023#include <android-base/unique_fd.h>
Josh Gao1b533c82016-03-04 15:15:56 -080024
Josh Gao1eef4782015-11-20 15:37:31 -080025void close_stdin();
26
Elliott Hughes7cf35752015-04-17 17:03:59 -070027bool getcwd(std::string* cwd);
Elliott Hughes38104452015-04-17 13:57:15 -070028bool directory_exists(const std::string& path);
Spencer Low939d0002015-08-01 17:29:23 -070029
30// Like the regular basename and dirname, but thread-safe on all
31// platforms and capable of correctly handling exotic Windows paths.
Elliott Hughesd189cfb2015-07-30 17:42:01 -070032std::string adb_basename(const std::string& path);
Spencer Low939d0002015-08-01 17:29:23 -070033std::string adb_dirname(const std::string& path);
Elliott Hughes38104452015-04-17 13:57:15 -070034
Alex Vallée28d1f8d2015-05-06 16:26:00 -040035bool mkdirs(const std::string& path);
Alex Valléee9163152015-05-06 17:22:25 -040036
Elliott Hughes38104452015-04-17 13:57:15 -070037std::string escape_arg(const std::string& s);
38
Yabin Cui19bec5b2015-09-22 15:52:57 -070039std::string dump_hex(const void* ptr, size_t byte_count);
Elliott Hughes88b4c852015-04-30 17:32:03 -070040
Elliott Hughesb628cb12015-08-03 10:38:08 -070041std::string perror_str(const char* msg);
42
Yabin Cui5fc22312015-10-06 15:10:05 -070043bool set_file_block_mode(int fd, bool block);
44
Josh Gao1b533c82016-03-04 15:15:56 -080045extern int adb_close(int fd);
46
David Pursell19d0c232016-04-07 11:25:48 -070047// Given forward/reverse targets, returns true if they look sane. If an error is found, fills
48// |error| and returns false.
49// Currently this only checks "tcp:" targets. Additional checking could be added for other targets
50// if needed.
51bool forward_targets_are_valid(const std::string& source, const std::string& dest,
52 std::string* error);
53
Josh Gao1b533c82016-03-04 15:15:56 -080054// Helper to automatically close an FD when it goes out of scope.
Josh Gaod91139c2016-05-13 14:52:06 -070055struct AdbCloser {
56 static void Close(int fd) {
57 adb_close(fd);
58 }
59};
60
61using unique_fd = android::base::unique_fd_impl<AdbCloser>;
62
Josh Gao1b533c82016-03-04 15:15:56 -080063class ScopedFd {
64 public:
65 ScopedFd() {
66 }
67
68 ~ScopedFd() {
69 Reset();
70 }
71
72 void Reset(int fd = -1) {
73 if (fd != fd_) {
74 if (valid()) {
75 adb_close(fd_);
76 }
77 fd_ = fd;
78 }
79 }
80
81 int Release() {
82 int temp = fd_;
83 fd_ = -1;
84 return temp;
85 }
86
87 bool valid() const {
88 return fd_ >= 0;
89 }
90
91 int fd() const {
92 return fd_;
93 }
94
95 private:
96 int fd_ = -1;
97
98 DISALLOW_COPY_AND_ASSIGN(ScopedFd);
99};
100
Elliott Hughes38104452015-04-17 13:57:15 -0700101#endif