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