blob: 89fcd66ec83391b051affbfed4348cf81f8c9554 [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>
23
Josh Gao1eef4782015-11-20 15:37:31 -080024void close_stdin();
25
Elliott Hughes7cf35752015-04-17 17:03:59 -070026bool getcwd(std::string* cwd);
Elliott Hughes38104452015-04-17 13:57:15 -070027bool directory_exists(const std::string& path);
Spencer Low939d0002015-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 Hughesd189cfb2015-07-30 17:42:01 -070031std::string adb_basename(const std::string& path);
Spencer Low939d0002015-08-01 17:29:23 -070032std::string adb_dirname(const std::string& path);
Elliott Hughes38104452015-04-17 13:57:15 -070033
Alex Vallée28d1f8d2015-05-06 16:26:00 -040034bool mkdirs(const std::string& path);
Alex Valléee9163152015-05-06 17:22:25 -040035
Elliott Hughes38104452015-04-17 13:57:15 -070036std::string escape_arg(const std::string& s);
37
Yabin Cui19bec5b2015-09-22 15:52:57 -070038std::string dump_hex(const void* ptr, size_t byte_count);
Elliott Hughes88b4c852015-04-30 17:32:03 -070039
Elliott Hughesb628cb12015-08-03 10:38:08 -070040std::string perror_str(const char* msg);
41
Yabin Cui5fc22312015-10-06 15:10:05 -070042bool set_file_block_mode(int fd, bool block);
43
Josh Gao1b533c82016-03-04 15:15:56 -080044extern int adb_close(int fd);
45
46// Helper to automatically close an FD when it goes out of scope.
47class ScopedFd {
48 public:
49 ScopedFd() {
50 }
51
52 ~ScopedFd() {
53 Reset();
54 }
55
56 void Reset(int fd = -1) {
57 if (fd != fd_) {
58 if (valid()) {
59 adb_close(fd_);
60 }
61 fd_ = fd;
62 }
63 }
64
65 int Release() {
66 int temp = fd_;
67 fd_ = -1;
68 return temp;
69 }
70
71 bool valid() const {
72 return fd_ >= 0;
73 }
74
75 int fd() const {
76 return fd_;
77 }
78
79 private:
80 int fd_ = -1;
81
82 DISALLOW_COPY_AND_ASSIGN(ScopedFd);
83};
84
Elliott Hughes38104452015-04-17 13:57:15 -070085#endif