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 #pragma once 18 19 #include <sys/stat.h> 20 #include <sys/types.h> 21 22 #include <string> 23 24 #include "android-base/macros.h" 25 #include "android-base/off64_t.h" 26 #include "android-base/unique_fd.h" 27 28 #if !defined(_WIN32) && !defined(O_BINARY) 29 /** Windows needs O_BINARY, but Unix never mangles line endings. */ 30 #define O_BINARY 0 31 #endif 32 33 #if defined(_WIN32) && !defined(O_CLOEXEC) 34 /** Windows has O_CLOEXEC but calls it O_NOINHERIT for some reason. */ 35 #define O_CLOEXEC O_NOINHERIT 36 #endif 37 38 class TemporaryFile { 39 public: 40 TemporaryFile(); 41 explicit TemporaryFile(const std::string& tmp_dir); 42 ~TemporaryFile(); 43 44 // Release the ownership of fd, caller is reponsible for closing the 45 // fd or stream properly. 46 int release(); 47 // Don't remove the temporary file in the destructor. DoNotRemove()48 void DoNotRemove() { remove_file_ = false; } 49 50 int fd; 51 char path[1024]; 52 53 private: 54 void init(const std::string& tmp_dir); 55 56 bool remove_file_ = true; 57 58 DISALLOW_COPY_AND_ASSIGN(TemporaryFile); 59 }; 60 61 class TemporaryDir { 62 public: 63 TemporaryDir(); 64 ~TemporaryDir(); 65 // Don't remove the temporary dir in the destructor. DoNotRemove()66 void DoNotRemove() { remove_dir_and_contents_ = false; } 67 68 char path[1024]; 69 70 private: 71 bool init(const std::string& tmp_dir); 72 73 bool remove_dir_and_contents_ = true; 74 75 DISALLOW_COPY_AND_ASSIGN(TemporaryDir); 76 }; 77 78 namespace android { 79 namespace base { 80 81 bool ReadFdToString(borrowed_fd fd, std::string* content); 82 bool ReadFileToString(const std::string& path, std::string* content, 83 bool follow_symlinks = false); 84 85 bool WriteStringToFile(const std::string& content, const std::string& path, 86 bool follow_symlinks = false); 87 bool WriteStringToFd(const std::string& content, borrowed_fd fd); 88 89 #if !defined(_WIN32) 90 bool WriteStringToFile(const std::string& content, const std::string& path, 91 mode_t mode, uid_t owner, gid_t group, 92 bool follow_symlinks = false); 93 #endif 94 95 bool ReadFully(borrowed_fd fd, void* data, size_t byte_count); 96 97 // Reads `byte_count` bytes from the file descriptor at the specified offset. 98 // Returns false if there was an IO error or EOF was reached before reading `byte_count` bytes. 99 // 100 // NOTE: On Linux/Mac, this function wraps pread, which provides atomic read support without 101 // modifying the read pointer of the file descriptor. On Windows, however, the read pointer does 102 // get modified. This means that ReadFullyAtOffset can be used concurrently with other calls to the 103 // same function, but concurrently seeking or reading incrementally can lead to unexpected 104 // behavior. 105 bool ReadFullyAtOffset(borrowed_fd fd, void* data, size_t byte_count, off64_t offset); 106 107 bool WriteFully(borrowed_fd fd, const void* data, size_t byte_count); 108 109 bool RemoveFileIfExists(const std::string& path, std::string* err = nullptr); 110 111 #if !defined(_WIN32) 112 bool Realpath(const std::string& path, std::string* result); 113 bool Readlink(const std::string& path, std::string* result); 114 #endif 115 116 std::string GetExecutablePath(); 117 std::string GetExecutableDirectory(); 118 119 // Like the regular basename and dirname, but thread-safe on all 120 // platforms and capable of correctly handling exotic Windows paths. 121 std::string Basename(const std::string& path); 122 std::string Dirname(const std::string& path); 123 124 } // namespace base 125 } // namespace android 126