1 /* 2 * Copyright (C) 2009 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 ART_LIBARTBASE_BASE_UNIX_FILE_FD_FILE_H_ 18 #define ART_LIBARTBASE_BASE_UNIX_FILE_FD_FILE_H_ 19 20 #include <fcntl.h> 21 22 #include <string> 23 24 #include "base/macros.h" 25 #include "random_access_file.h" 26 27 namespace unix_file { 28 29 // If true, check whether Flush and Close are called before destruction. 30 static constexpr bool kCheckSafeUsage = true; 31 32 // A RandomAccessFile implementation backed by a file descriptor. 33 // 34 // Not thread safe. 35 class FdFile : public RandomAccessFile { 36 public: 37 static constexpr int kInvalidFd = -1; 38 39 FdFile() = default; 40 // Creates an FdFile using the given file descriptor. 41 // Takes ownership of the file descriptor. 42 FdFile(int fd, bool check_usage); 43 FdFile(int fd, const std::string& path, bool check_usage); 44 FdFile(int fd, const std::string& path, bool check_usage, bool read_only_mode); 45 FdFile(const std::string & path,int flags,bool check_usage)46 FdFile(const std::string& path, int flags, bool check_usage) 47 : FdFile(path, flags, 0640, check_usage) {} 48 FdFile(const std::string& path, int flags, mode_t mode, bool check_usage); 49 50 // Move constructor. 51 FdFile(FdFile&& other) noexcept; 52 53 // Move assignment operator. 54 FdFile& operator=(FdFile&& other) noexcept; 55 56 // Release the file descriptor. This will make further accesses to this FdFile invalid. Disables 57 // all further state checking. 58 int Release(); 59 60 void Reset(int fd, bool check_usage); 61 62 // Destroys an FdFile, closing the file descriptor if Close hasn't already 63 // been called. (If you care about the return value of Close, call it 64 // yourself; this is meant to handle failure cases and read-only accesses. 65 // Note though that calling Close and checking its return value is still no 66 // guarantee that data actually made it to stable storage.) 67 virtual ~FdFile(); 68 69 // RandomAccessFile API. 70 int Close() override WARN_UNUSED; 71 int64_t Read(char* buf, int64_t byte_count, int64_t offset) const override WARN_UNUSED; 72 int SetLength(int64_t new_length) override WARN_UNUSED; 73 int64_t GetLength() const override; 74 int64_t Write(const char* buf, int64_t byte_count, int64_t offset) override WARN_UNUSED; 75 Flush()76 int Flush() override WARN_UNUSED { return Flush(/*flush_metadata=*/false); } 77 78 // Short for SetLength(0); Flush(); Close(); 79 // If the file was opened with a path name and unlink = true, also calls Unlink() on the path. 80 // Note that it is the the caller's responsibility to avoid races. 81 bool Erase(bool unlink = false); 82 83 // Call unlink(), though only if FilePathMatchesFd() returns true. 84 bool Unlink(); 85 86 // Try to Flush(), then try to Close(); If either fails, call Erase(). 87 int FlushCloseOrErase() WARN_UNUSED; 88 89 // Try to Flush and Close(). Attempts both, but returns the first error. 90 int FlushClose() WARN_UNUSED; 91 92 // Bonus API. 93 int Fd() const; 94 bool ReadOnlyMode() const; 95 bool CheckUsage() const; 96 97 // Check whether the underlying file descriptor refers to an open file. 98 bool IsOpened() const; 99 100 // Check whether the numeric value of the underlying file descriptor is valid (Fd() != -1). IsValid()101 bool IsValid() const { return fd_ != kInvalidFd; } 102 GetPath()103 const std::string& GetPath() const { 104 return file_path_; 105 } 106 bool ReadFully(void* buffer, size_t byte_count) WARN_UNUSED; 107 bool PreadFully(void* buffer, size_t byte_count, size_t offset) WARN_UNUSED; 108 bool WriteFully(const void* buffer, size_t byte_count) WARN_UNUSED; 109 bool PwriteFully(const void* buffer, size_t byte_count, size_t offset) WARN_UNUSED; 110 111 // Change the file path, though only if FilePathMatchesFd() returns true. 112 // 113 // If a file at new_path already exists, it will be replaced. 114 // On Linux, the rename syscall will fail unless the source and destination are on the same 115 // mounted filesystem. 116 // This function is not expected to modify the file data itself, instead it modifies the inodes of 117 // the source and destination directories, and therefore the function flushes those file 118 // descriptors following the rename. 119 bool Rename(const std::string& new_path); 120 // Copy data from another file. 121 // On Linux, we only support copies that will append regions to the file, and we require the file 122 // offset of the output file descriptor to be aligned with the filesystem blocksize (see comments 123 // in implementation). 124 bool Copy(FdFile* input_file, int64_t offset, int64_t size); 125 // Clears the file content and resets the file offset to 0. 126 // Returns true upon success, false otherwise. 127 bool ClearContent(); 128 // Resets the file offset to the beginning of the file. 129 bool ResetOffset(); 130 131 // This enum is public so that we can define the << operator over it. 132 enum class GuardState { 133 kBase, // Base, file has not been flushed or closed. 134 kFlushed, // File has been flushed, but not closed. 135 kClosed, // File has been flushed and closed. 136 kNoCheck // Do not check for the current file instance. 137 }; 138 139 // WARNING: Only use this when you know what you're doing! 140 void MarkUnchecked(); 141 142 // Compare against another file. Returns 0 if the files are equivalent, otherwise returns -1 or 1 143 // depending on if the lengths are different. If the lengths are the same, the function returns 144 // the difference of the first byte that differs. 145 int Compare(FdFile* other); 146 147 // Check that `fd` has a valid value (!= kInvalidFd) and refers to an open file. 148 // On Windows, this call only checks that the value of `fd` is valid . 149 static bool IsOpenFd(int fd); 150 151 protected: 152 // If the guard state indicates checking (!=kNoCheck), go to the target state `target`. Print the 153 // given warning if the current state is or exceeds warn_threshold. 154 void moveTo(GuardState target, GuardState warn_threshold, const char* warning); 155 156 // If the guard state indicates checking (<kNoCheck), and is below the target state `target`, go 157 // to `target`. If the current state is higher (excluding kNoCheck) than the target state, print 158 // the warning. 159 void moveUp(GuardState target, const char* warning); 160 161 // Forcefully sets the state to the given one. This can overwrite kNoCheck. resetGuard(GuardState new_state)162 void resetGuard(GuardState new_state) { 163 if (kCheckSafeUsage) { 164 guard_state_ = new_state; 165 } 166 } 167 168 GuardState guard_state_ = GuardState::kClosed; 169 170 // Opens file `file_path` using `flags` and `mode`. 171 bool Open(const std::string& file_path, int flags); 172 bool Open(const std::string& file_path, int flags, mode_t mode); 173 174 private: 175 template <bool kUseOffset> 176 bool WriteFullyGeneric(const void* buffer, size_t byte_count, size_t offset); 177 178 int Flush(bool flush_metadata) WARN_UNUSED; 179 180 // The file path we hold for the file descriptor may be invalid, or may not even exist (e.g. if 181 // the FdFile wasn't initialised with a path). This helper function checks if calling open() on 182 // the file path (if it is set) returns the expected up-to-date file descriptor. This is still 183 // racy, though, and it is up to the caller to ensure correctness in a multi-process setup. 184 bool FilePathMatchesFd(); 185 186 #ifdef __linux__ 187 // Sparse copy of 'size' bytes from an input file, starting at 'off'. Both this file's offset and 188 // the input file's offset will be incremented by 'size' bytes. 189 // 190 // Note: in order to preserve the same sparsity, the input and output files must be on mounted 191 // filesystems that use the same blocksize, and the offsets used for the copy must be aligned to 192 // it. Otherwise, the copied region's sparsity within the output file may differ from its original 193 // sparsity in the input file. 194 bool UserspaceSparseCopy(const FdFile* input_file, off_t off, size_t size, size_t fs_blocksize); 195 196 // Write 'size' bytes from 'data' to the file if any are non-zero. Otherwise, just update the file 197 // offset and skip the write. For efficiency, the function expects a vector of zeroed uint8_t 198 // values to check the data array against. This vector 'zeroes' must have length greater than or 199 // equal to 'size'. 200 // 201 // As filesystems which support sparse files only allocate physical space to blocks that have been 202 // written, any whole filesystem blocks in the output file which are skipped in this way will save 203 // storage space. Subsequent reads of bytes in non-allocated blocks will simply return zeros 204 // without accessing the underlying storage. 205 bool SparseWrite(const uint8_t* data, 206 size_t size, 207 const std::vector<uint8_t>& zeroes); 208 #endif 209 210 void Destroy(); // For ~FdFile and operator=(&&). 211 212 int fd_ = kInvalidFd; 213 std::string file_path_; 214 bool read_only_mode_ = false; 215 216 DISALLOW_COPY_AND_ASSIGN(FdFile); 217 }; 218 219 std::ostream& operator<<(std::ostream& os, FdFile::GuardState kind); 220 221 } // namespace unix_file 222 223 #endif // ART_LIBARTBASE_BASE_UNIX_FILE_FD_FILE_H_ 224