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 ANDROID_BASE_FILE_H 18 #define ANDROID_BASE_FILE_H 19 20 #include <sys/stat.h> 21 #include <sys/types.h> 22 #include <string> 23 24 #if !defined(_WIN32) && !defined(O_BINARY) 25 #define O_BINARY 0 26 #endif 27 28 #if defined(__APPLE__) 29 /* Mac OS has always had a 64-bit off_t, so it doesn't have off64_t. */ 30 typedef off_t off64_t; 31 #endif 32 33 namespace android { 34 namespace base { 35 36 bool ReadFdToString(int fd, std::string* content); 37 bool ReadFileToString(const std::string& path, std::string* content, 38 bool follow_symlinks = false); 39 40 bool WriteStringToFile(const std::string& content, const std::string& path, 41 bool follow_symlinks = false); 42 bool WriteStringToFd(const std::string& content, int fd); 43 44 #if !defined(_WIN32) 45 bool WriteStringToFile(const std::string& content, const std::string& path, 46 mode_t mode, uid_t owner, gid_t group, 47 bool follow_symlinks = false); 48 #endif 49 50 bool ReadFully(int fd, void* data, size_t byte_count); 51 52 // Reads `byte_count` bytes from the file descriptor at the specified offset. 53 // Returns false if there was an IO error or EOF was reached before reading `byte_count` bytes. 54 // 55 // NOTE: On Linux/Mac, this function wraps pread, which provides atomic read support without 56 // modifying the read pointer of the file descriptor. On Windows, however, the read pointer does 57 // get modified. This means that ReadFullyAtOffset can be used concurrently with other calls to the 58 // same function, but concurrently seeking or reading incrementally can lead to unexpected 59 // behavior. 60 bool ReadFullyAtOffset(int fd, void* data, size_t byte_count, off64_t offset); 61 62 bool WriteFully(int fd, const void* data, size_t byte_count); 63 64 bool RemoveFileIfExists(const std::string& path, std::string* err = nullptr); 65 66 #if !defined(_WIN32) 67 bool Realpath(const std::string& path, std::string* result); 68 bool Readlink(const std::string& path, std::string* result); 69 #endif 70 71 std::string GetExecutablePath(); 72 std::string GetExecutableDirectory(); 73 74 // Like the regular basename and dirname, but thread-safe on all 75 // platforms and capable of correctly handling exotic Windows paths. 76 std::string Basename(const std::string& path); 77 std::string Dirname(const std::string& path); 78 79 } // namespace base 80 } // namespace android 81 82 #endif // ANDROID_BASE_FILE_H 83