blob: cf420678b0c0f33f4b8ae86284da59be6f56a4c3 [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
20#include <string>
21
Josh Gaoa9eb38d2016-03-04 15:15:56 -080022#include <android-base/macros.h>
23
Josh Gao7d586072015-11-20 15:37:31 -080024void close_stdin();
25
Elliott Hughesa7090b92015-04-17 17:03:59 -070026bool getcwd(std::string* cwd);
Elliott Hughes58305772015-04-17 13:57:15 -070027bool directory_exists(const std::string& path);
Spencer Low22191c32015-08-01 17:29:23 -070028
29// Like the regular basename and dirname, but thread-safe on all
30// platforms and capable of correctly handling exotic Windows paths.
Elliott Hughes5c742702015-07-30 17:42:01 -070031std::string adb_basename(const std::string& path);
Spencer Low22191c32015-08-01 17:29:23 -070032std::string adb_dirname(const std::string& path);
Elliott Hughes58305772015-04-17 13:57:15 -070033
Yurii Zubrytskyia9e2b992016-05-25 15:17:10 -070034// Return the user's home directory.
35// |check_env_first| - if true, on Windows check the ANDROID_SDK_HOME
36// environment variable before trying the WinAPI call (useful when looking for
37// the .android directory)
38std::string adb_get_homedir_path(bool check_env_first);
39
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);
Elliott Hughese67f1f82015-04-30 17:32:03 -070045
Elliott Hughesaa245492015-08-03 10:38:08 -070046std::string perror_str(const char* msg);
47
Yabin Cui6dfef252015-10-06 15:10:05 -070048bool set_file_block_mode(int fd, bool block);
49
Josh Gaoa9eb38d2016-03-04 15:15:56 -080050extern int adb_close(int fd);
51
52// Helper to automatically close an FD when it goes out of scope.
53class ScopedFd {
54 public:
55 ScopedFd() {
56 }
57
58 ~ScopedFd() {
59 Reset();
60 }
61
62 void Reset(int fd = -1) {
63 if (fd != fd_) {
64 if (valid()) {
65 adb_close(fd_);
66 }
67 fd_ = fd;
68 }
69 }
70
71 int Release() {
72 int temp = fd_;
73 fd_ = -1;
74 return temp;
75 }
76
77 bool valid() const {
78 return fd_ >= 0;
79 }
80
81 int fd() const {
82 return fd_;
83 }
84
85 private:
86 int fd_ = -1;
87
88 DISALLOW_COPY_AND_ASSIGN(ScopedFd);
89};
90
Elliott Hughes58305772015-04-17 13:57:15 -070091#endif