Home | History | Annotate | Download | only in adb
      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 
     22 #include <android-base/macros.h>
     23 #include <android-base/unique_fd.h>
     24 
     25 void close_stdin();
     26 
     27 bool getcwd(std::string* cwd);
     28 bool directory_exists(const std::string& path);
     29 
     30 // Like the regular basename and dirname, but thread-safe on all
     31 // platforms and capable of correctly handling exotic Windows paths.
     32 std::string adb_basename(const std::string& path);
     33 std::string adb_dirname(const std::string& path);
     34 
     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 
     41 bool mkdirs(const std::string& path);
     42 
     43 std::string escape_arg(const std::string& s);
     44 
     45 std::string dump_hex(const void* ptr, size_t byte_count);
     46 
     47 std::string perror_str(const char* msg);
     48 
     49 bool set_file_block_mode(int fd, bool block);
     50 
     51 extern int adb_close(int fd);
     52 
     53 // Helper to automatically close an FD when it goes out of scope.
     54 struct AdbCloser {
     55     static void Close(int fd) {
     56         adb_close(fd);
     57     }
     58 };
     59 
     60 using unique_fd = android::base::unique_fd_impl<AdbCloser>;
     61 
     62 class ScopedFd {
     63   public:
     64     ScopedFd() {
     65     }
     66 
     67     ~ScopedFd() {
     68         Reset();
     69     }
     70 
     71     void Reset(int fd = -1) {
     72         if (fd != fd_) {
     73             if (valid()) {
     74                 adb_close(fd_);
     75             }
     76             fd_ = fd;
     77         }
     78     }
     79 
     80     int Release() {
     81         int temp = fd_;
     82         fd_ = -1;
     83         return temp;
     84     }
     85 
     86     bool valid() const {
     87         return fd_ >= 0;
     88     }
     89 
     90     int fd() const {
     91         return fd_;
     92     }
     93 
     94   private:
     95     int fd_ = -1;
     96 
     97     DISALLOW_COPY_AND_ASSIGN(ScopedFd);
     98 };
     99 
    100 #endif
    101