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