Elliott Hughes | 5830577 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 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 | #ifndef _ADB_UTILS_H_ |
| 18 | #define _ADB_UTILS_H_ |
| 19 | |
| 20 | #include <string> |
| 21 | |
Josh Gao | f0d3b4f | 2016-03-04 15:15:56 -0800 | [diff] [blame] | 22 | #include <android-base/macros.h> |
Josh Gao | 13ea01d | 2016-05-13 14:52:06 -0700 | [diff] [blame] | 23 | #include <android-base/unique_fd.h> |
Josh Gao | f0d3b4f | 2016-03-04 15:15:56 -0800 | [diff] [blame] | 24 | |
Josh Gao | 7d58607 | 2015-11-20 15:37:31 -0800 | [diff] [blame] | 25 | void close_stdin(); |
| 26 | |
Elliott Hughes | a7090b9 | 2015-04-17 17:03:59 -0700 | [diff] [blame] | 27 | bool getcwd(std::string* cwd); |
Elliott Hughes | 5830577 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 28 | bool directory_exists(const std::string& path); |
Spencer Low | 22191c3 | 2015-08-01 17:29:23 -0700 | [diff] [blame] | 29 | |
| 30 | // Like the regular basename and dirname, but thread-safe on all |
| 31 | // platforms and capable of correctly handling exotic Windows paths. |
Elliott Hughes | 5c74270 | 2015-07-30 17:42:01 -0700 | [diff] [blame] | 32 | std::string adb_basename(const std::string& path); |
Spencer Low | 22191c3 | 2015-08-01 17:29:23 -0700 | [diff] [blame] | 33 | std::string adb_dirname(const std::string& path); |
Elliott Hughes | 5830577 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 34 | |
Yurii Zubrytskyi | 376b275 | 2016-05-25 15:17:10 -0700 | [diff] [blame^] | 35 | // Return the user's home directory. |
| 36 | // |check_env_first| - if true, on Windows check the ANDROID_SDK_HOME |
| 37 | // environment variable before trying the WinAPI call (useful when looking for |
| 38 | // the .android directory) |
| 39 | std::string adb_get_homedir_path(bool check_env_first); |
| 40 | |
Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 41 | bool mkdirs(const std::string& path); |
Alex Vallée | 1421614 | 2015-05-06 17:22:25 -0400 | [diff] [blame] | 42 | |
Elliott Hughes | 5830577 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 43 | std::string escape_arg(const std::string& s); |
| 44 | |
Yabin Cui | aed3c61 | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 45 | std::string dump_hex(const void* ptr, size_t byte_count); |
Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 46 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 47 | std::string perror_str(const char* msg); |
| 48 | |
Yabin Cui | 6dfef25 | 2015-10-06 15:10:05 -0700 | [diff] [blame] | 49 | bool set_file_block_mode(int fd, bool block); |
| 50 | |
Josh Gao | f0d3b4f | 2016-03-04 15:15:56 -0800 | [diff] [blame] | 51 | extern int adb_close(int fd); |
| 52 | |
David Pursell | eaae97e | 2016-04-07 11:25:48 -0700 | [diff] [blame] | 53 | // Given forward/reverse targets, returns true if they look sane. If an error is found, fills |
| 54 | // |error| and returns false. |
| 55 | // Currently this only checks "tcp:" targets. Additional checking could be added for other targets |
| 56 | // if needed. |
| 57 | bool forward_targets_are_valid(const std::string& source, const std::string& dest, |
| 58 | std::string* error); |
| 59 | |
Josh Gao | f0d3b4f | 2016-03-04 15:15:56 -0800 | [diff] [blame] | 60 | // Helper to automatically close an FD when it goes out of scope. |
Josh Gao | 13ea01d | 2016-05-13 14:52:06 -0700 | [diff] [blame] | 61 | struct AdbCloser { |
| 62 | static void Close(int fd) { |
| 63 | adb_close(fd); |
| 64 | } |
| 65 | }; |
| 66 | |
| 67 | using unique_fd = android::base::unique_fd_impl<AdbCloser>; |
| 68 | |
Josh Gao | f0d3b4f | 2016-03-04 15:15:56 -0800 | [diff] [blame] | 69 | class ScopedFd { |
| 70 | public: |
| 71 | ScopedFd() { |
| 72 | } |
| 73 | |
| 74 | ~ScopedFd() { |
| 75 | Reset(); |
| 76 | } |
| 77 | |
| 78 | void Reset(int fd = -1) { |
| 79 | if (fd != fd_) { |
| 80 | if (valid()) { |
| 81 | adb_close(fd_); |
| 82 | } |
| 83 | fd_ = fd; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | int Release() { |
| 88 | int temp = fd_; |
| 89 | fd_ = -1; |
| 90 | return temp; |
| 91 | } |
| 92 | |
| 93 | bool valid() const { |
| 94 | return fd_ >= 0; |
| 95 | } |
| 96 | |
| 97 | int fd() const { |
| 98 | return fd_; |
| 99 | } |
| 100 | |
| 101 | private: |
| 102 | int fd_ = -1; |
| 103 | |
| 104 | DISALLOW_COPY_AND_ASSIGN(ScopedFd); |
| 105 | }; |
| 106 | |
Elliott Hughes | 5830577 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 107 | #endif |