1*8f0ba417SAndroid Build Coastguard Worker /* 2*8f0ba417SAndroid Build Coastguard Worker * Copyright (C) 2015 The Android Open Source Project 3*8f0ba417SAndroid Build Coastguard Worker * 4*8f0ba417SAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License"); 5*8f0ba417SAndroid Build Coastguard Worker * you may not use this file except in compliance with the License. 6*8f0ba417SAndroid Build Coastguard Worker * You may obtain a copy of the License at 7*8f0ba417SAndroid Build Coastguard Worker * 8*8f0ba417SAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0 9*8f0ba417SAndroid Build Coastguard Worker * 10*8f0ba417SAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software 11*8f0ba417SAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS, 12*8f0ba417SAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13*8f0ba417SAndroid Build Coastguard Worker * See the License for the specific language governing permissions and 14*8f0ba417SAndroid Build Coastguard Worker * limitations under the License. 15*8f0ba417SAndroid Build Coastguard Worker */ 16*8f0ba417SAndroid Build Coastguard Worker 17*8f0ba417SAndroid Build Coastguard Worker #pragma once 18*8f0ba417SAndroid Build Coastguard Worker 19*8f0ba417SAndroid Build Coastguard Worker #include <sys/stat.h> 20*8f0ba417SAndroid Build Coastguard Worker #include <sys/types.h> 21*8f0ba417SAndroid Build Coastguard Worker 22*8f0ba417SAndroid Build Coastguard Worker #include <string> 23*8f0ba417SAndroid Build Coastguard Worker 24*8f0ba417SAndroid Build Coastguard Worker #include "android-base/macros.h" 25*8f0ba417SAndroid Build Coastguard Worker #include "android-base/off64_t.h" 26*8f0ba417SAndroid Build Coastguard Worker #include "android-base/unique_fd.h" 27*8f0ba417SAndroid Build Coastguard Worker 28*8f0ba417SAndroid Build Coastguard Worker #if !defined(_WIN32) && !defined(O_BINARY) 29*8f0ba417SAndroid Build Coastguard Worker /** Windows needs O_BINARY, but Unix never mangles line endings. */ 30*8f0ba417SAndroid Build Coastguard Worker #define O_BINARY 0 31*8f0ba417SAndroid Build Coastguard Worker #endif 32*8f0ba417SAndroid Build Coastguard Worker 33*8f0ba417SAndroid Build Coastguard Worker #if defined(_WIN32) && !defined(O_CLOEXEC) 34*8f0ba417SAndroid Build Coastguard Worker /** Windows has O_CLOEXEC but calls it O_NOINHERIT for some reason. */ 35*8f0ba417SAndroid Build Coastguard Worker #define O_CLOEXEC O_NOINHERIT 36*8f0ba417SAndroid Build Coastguard Worker #endif 37*8f0ba417SAndroid Build Coastguard Worker 38*8f0ba417SAndroid Build Coastguard Worker class TemporaryFile { 39*8f0ba417SAndroid Build Coastguard Worker public: 40*8f0ba417SAndroid Build Coastguard Worker TemporaryFile(); 41*8f0ba417SAndroid Build Coastguard Worker explicit TemporaryFile(const std::string& tmp_dir); 42*8f0ba417SAndroid Build Coastguard Worker ~TemporaryFile(); 43*8f0ba417SAndroid Build Coastguard Worker 44*8f0ba417SAndroid Build Coastguard Worker // Release the ownership of fd, caller is reponsible for closing the 45*8f0ba417SAndroid Build Coastguard Worker // fd or stream properly. 46*8f0ba417SAndroid Build Coastguard Worker int release(); 47*8f0ba417SAndroid Build Coastguard Worker // Don't remove the temporary file in the destructor. DoNotRemove()48*8f0ba417SAndroid Build Coastguard Worker void DoNotRemove() { remove_file_ = false; } 49*8f0ba417SAndroid Build Coastguard Worker 50*8f0ba417SAndroid Build Coastguard Worker int fd; 51*8f0ba417SAndroid Build Coastguard Worker char path[1024]; 52*8f0ba417SAndroid Build Coastguard Worker 53*8f0ba417SAndroid Build Coastguard Worker private: 54*8f0ba417SAndroid Build Coastguard Worker void init(const std::string& tmp_dir); 55*8f0ba417SAndroid Build Coastguard Worker 56*8f0ba417SAndroid Build Coastguard Worker bool remove_file_ = true; 57*8f0ba417SAndroid Build Coastguard Worker 58*8f0ba417SAndroid Build Coastguard Worker DISALLOW_COPY_AND_ASSIGN(TemporaryFile); 59*8f0ba417SAndroid Build Coastguard Worker }; 60*8f0ba417SAndroid Build Coastguard Worker 61*8f0ba417SAndroid Build Coastguard Worker class TemporaryDir { 62*8f0ba417SAndroid Build Coastguard Worker public: 63*8f0ba417SAndroid Build Coastguard Worker TemporaryDir(); 64*8f0ba417SAndroid Build Coastguard Worker ~TemporaryDir(); 65*8f0ba417SAndroid Build Coastguard Worker // Don't remove the temporary dir in the destructor. DoNotRemove()66*8f0ba417SAndroid Build Coastguard Worker void DoNotRemove() { remove_dir_and_contents_ = false; } 67*8f0ba417SAndroid Build Coastguard Worker 68*8f0ba417SAndroid Build Coastguard Worker char path[1024]; 69*8f0ba417SAndroid Build Coastguard Worker 70*8f0ba417SAndroid Build Coastguard Worker private: 71*8f0ba417SAndroid Build Coastguard Worker bool init(const std::string& tmp_dir); 72*8f0ba417SAndroid Build Coastguard Worker 73*8f0ba417SAndroid Build Coastguard Worker bool remove_dir_and_contents_ = true; 74*8f0ba417SAndroid Build Coastguard Worker 75*8f0ba417SAndroid Build Coastguard Worker DISALLOW_COPY_AND_ASSIGN(TemporaryDir); 76*8f0ba417SAndroid Build Coastguard Worker }; 77*8f0ba417SAndroid Build Coastguard Worker 78*8f0ba417SAndroid Build Coastguard Worker namespace android { 79*8f0ba417SAndroid Build Coastguard Worker namespace base { 80*8f0ba417SAndroid Build Coastguard Worker 81*8f0ba417SAndroid Build Coastguard Worker bool ReadFdToString(borrowed_fd fd, std::string* content); 82*8f0ba417SAndroid Build Coastguard Worker bool ReadFileToString(const std::string& path, std::string* content, 83*8f0ba417SAndroid Build Coastguard Worker bool follow_symlinks = false); 84*8f0ba417SAndroid Build Coastguard Worker 85*8f0ba417SAndroid Build Coastguard Worker bool WriteStringToFile(const std::string& content, const std::string& path, 86*8f0ba417SAndroid Build Coastguard Worker bool follow_symlinks = false); 87*8f0ba417SAndroid Build Coastguard Worker bool WriteStringToFd(std::string_view content, borrowed_fd fd); 88*8f0ba417SAndroid Build Coastguard Worker 89*8f0ba417SAndroid Build Coastguard Worker #if !defined(_WIN32) 90*8f0ba417SAndroid Build Coastguard Worker bool WriteStringToFile(const std::string& content, const std::string& path, 91*8f0ba417SAndroid Build Coastguard Worker mode_t mode, uid_t owner, gid_t group, 92*8f0ba417SAndroid Build Coastguard Worker bool follow_symlinks = false); 93*8f0ba417SAndroid Build Coastguard Worker #endif 94*8f0ba417SAndroid Build Coastguard Worker 95*8f0ba417SAndroid Build Coastguard Worker bool ReadFully(borrowed_fd fd, void* data, size_t byte_count); 96*8f0ba417SAndroid Build Coastguard Worker 97*8f0ba417SAndroid Build Coastguard Worker // Reads `byte_count` bytes from the file descriptor at the specified offset. 98*8f0ba417SAndroid Build Coastguard Worker // Returns false if there was an IO error or EOF was reached before reading `byte_count` bytes. 99*8f0ba417SAndroid Build Coastguard Worker // 100*8f0ba417SAndroid Build Coastguard Worker // NOTE: On Linux/Mac, this function wraps pread, which provides atomic read support without 101*8f0ba417SAndroid Build Coastguard Worker // modifying the read pointer of the file descriptor. On Windows, however, the read pointer does 102*8f0ba417SAndroid Build Coastguard Worker // get modified. This means that ReadFullyAtOffset can be used concurrently with other calls to the 103*8f0ba417SAndroid Build Coastguard Worker // same function, but concurrently seeking or reading incrementally can lead to unexpected 104*8f0ba417SAndroid Build Coastguard Worker // behavior. 105*8f0ba417SAndroid Build Coastguard Worker bool ReadFullyAtOffset(borrowed_fd fd, void* data, size_t byte_count, off64_t offset); 106*8f0ba417SAndroid Build Coastguard Worker 107*8f0ba417SAndroid Build Coastguard Worker bool WriteFully(borrowed_fd fd, const void* data, size_t byte_count); 108*8f0ba417SAndroid Build Coastguard Worker bool WriteFullyAtOffset(borrowed_fd fd, const void* data, size_t byte_count, off64_t offset); 109*8f0ba417SAndroid Build Coastguard Worker 110*8f0ba417SAndroid Build Coastguard Worker bool RemoveFileIfExists(const std::string& path, std::string* err = nullptr); 111*8f0ba417SAndroid Build Coastguard Worker 112*8f0ba417SAndroid Build Coastguard Worker #if !defined(_WIN32) 113*8f0ba417SAndroid Build Coastguard Worker bool Realpath(const std::string& path, std::string* result); 114*8f0ba417SAndroid Build Coastguard Worker bool Readlink(const std::string& path, std::string* result); 115*8f0ba417SAndroid Build Coastguard Worker #endif 116*8f0ba417SAndroid Build Coastguard Worker 117*8f0ba417SAndroid Build Coastguard Worker std::string GetExecutablePath(); 118*8f0ba417SAndroid Build Coastguard Worker std::string GetExecutableDirectory(); 119*8f0ba417SAndroid Build Coastguard Worker 120*8f0ba417SAndroid Build Coastguard Worker // Like the regular basename and dirname, but thread-safe on all 121*8f0ba417SAndroid Build Coastguard Worker // platforms and capable of correctly handling exotic Windows paths. 122*8f0ba417SAndroid Build Coastguard Worker std::string Basename(std::string_view path); 123*8f0ba417SAndroid Build Coastguard Worker std::string Dirname(std::string_view path); 124*8f0ba417SAndroid Build Coastguard Worker 125*8f0ba417SAndroid Build Coastguard Worker } // namespace base 126*8f0ba417SAndroid Build Coastguard Worker } // namespace android 127