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 #include "fd_file.h"
18 
19 #include <errno.h>
20 #include <stdio.h>
21 #include <sys/stat.h>
22 #include <sys/types.h>
23 #include <unistd.h>
24 
25 #if defined(__BIONIC__)
26 #include <android/fdsan.h>
27 #endif
28 
29 #if defined(_WIN32)
30 #include <windows.h>
31 #endif
32 
33 #include <limits>
34 #include <vector>
35 
36 #include <android-base/file.h>
37 #include <android-base/logging.h>
38 
39 // Includes needed for FdFile::Copy().
40 #include "base/globals.h"
41 #ifdef __linux__
42 #include "base/bit_utils.h"
43 #include "base/mem_map.h"
44 #include "sys/mman.h"
45 #else
46 #include <algorithm>
47 #include "base/stl_util.h"
48 #endif
49 
50 namespace unix_file {
51 
52 #if defined(_WIN32)
53 // RAII wrapper for an event object to allow asynchronous I/O to correctly signal completion.
54 class ScopedEvent {
55  public:
ScopedEvent()56   ScopedEvent() {
57     handle_ = CreateEventA(/*lpEventAttributes*/ nullptr,
58                            /*bManualReset*/ true,
59                            /*bInitialState*/ false,
60                            /*lpName*/ nullptr);
61   }
62 
~ScopedEvent()63   ~ScopedEvent() { CloseHandle(handle_); }
64 
handle()65   HANDLE handle() { return handle_; }
66 
67  private:
68   HANDLE handle_;
69   DISALLOW_COPY_AND_ASSIGN(ScopedEvent);
70 };
71 
72 // Windows implementation of pread/pwrite. Note that these DO move the file descriptor's read/write
73 // position, but do so atomically.
pread(int fd,void * data,size_t byte_count,off64_t offset)74 static ssize_t pread(int fd, void* data, size_t byte_count, off64_t offset) {
75   ScopedEvent event;
76   if (event.handle() == INVALID_HANDLE_VALUE) {
77     PLOG(ERROR) << "Could not create event handle.";
78     errno = EIO;
79     return static_cast<ssize_t>(-1);
80   }
81 
82   auto handle = reinterpret_cast<HANDLE>(_get_osfhandle(fd));
83   DWORD bytes_read = 0;
84   OVERLAPPED overlapped = {};
85   overlapped.Offset = static_cast<DWORD>(offset);
86   overlapped.OffsetHigh = static_cast<DWORD>(offset >> 32);
87   overlapped.hEvent = event.handle();
88   if (!ReadFile(handle, data, static_cast<DWORD>(byte_count), &bytes_read, &overlapped)) {
89     // If the read failed with other than ERROR_IO_PENDING, return an error.
90     // ERROR_IO_PENDING signals the write was begun asynchronously.
91     // Block until the asynchronous operation has finished or fails, and return
92     // result accordingly.
93     if (::GetLastError() != ERROR_IO_PENDING ||
94         !::GetOverlappedResult(handle, &overlapped, &bytes_read, TRUE)) {
95       // In case someone tries to read errno (since this is masquerading as a POSIX call).
96       errno = EIO;
97       return static_cast<ssize_t>(-1);
98     }
99   }
100   return static_cast<ssize_t>(bytes_read);
101 }
102 
pwrite(int fd,const void * buf,size_t count,off64_t offset)103 static ssize_t pwrite(int fd, const void* buf, size_t count, off64_t offset) {
104   ScopedEvent event;
105   if (event.handle() == INVALID_HANDLE_VALUE) {
106     PLOG(ERROR) << "Could not create event handle.";
107     errno = EIO;
108     return static_cast<ssize_t>(-1);
109   }
110 
111   auto handle = reinterpret_cast<HANDLE>(_get_osfhandle(fd));
112   DWORD bytes_written = 0;
113   OVERLAPPED overlapped = {};
114   overlapped.Offset = static_cast<DWORD>(offset);
115   overlapped.OffsetHigh = static_cast<DWORD>(offset >> 32);
116   overlapped.hEvent = event.handle();
117   if (!::WriteFile(handle, buf, count, &bytes_written, &overlapped)) {
118     // If the write failed with other than ERROR_IO_PENDING, return an error.
119     // ERROR_IO_PENDING signals the write was begun asynchronously.
120     // Block until the asynchronous operation has finished or fails, and return
121     // result accordingly.
122     if (::GetLastError() != ERROR_IO_PENDING ||
123         !::GetOverlappedResult(handle, &overlapped, &bytes_written, TRUE)) {
124       // In case someone tries to read errno (since this is masquerading as a POSIX call).
125       errno = EIO;
126       return static_cast<ssize_t>(-1);
127     }
128   }
129   return static_cast<ssize_t>(bytes_written);
130 }
131 
fsync(int fd)132 static int fsync(int fd) {
133   auto handle = reinterpret_cast<HANDLE>(_get_osfhandle(fd));
134   if (handle != INVALID_HANDLE_VALUE && ::FlushFileBuffers(handle)) {
135     return 0;
136   }
137   errno = EINVAL;
138   return -1;
139 }
140 #endif
141 
142 #if defined(__BIONIC__)
GetFdFileOwnerTag(FdFile * fd_file)143 static uint64_t GetFdFileOwnerTag(FdFile* fd_file) {
144   return android_fdsan_create_owner_tag(ANDROID_FDSAN_OWNER_TYPE_ART_FDFILE,
145                                         reinterpret_cast<uint64_t>(fd_file));
146 }
147 #endif
148 
FdFile(int fd,bool check_usage)149 FdFile::FdFile(int fd, bool check_usage)
150     : FdFile(fd, std::string(), check_usage) {}
151 
FdFile(int fd,const std::string & path,bool check_usage)152 FdFile::FdFile(int fd, const std::string& path, bool check_usage)
153     : FdFile(fd, path, check_usage, false) {}
154 
FdFile(int fd,const std::string & path,bool check_usage,bool read_only_mode)155 FdFile::FdFile(int fd, const std::string& path, bool check_usage,
156                bool read_only_mode)
157     : guard_state_(check_usage ? GuardState::kBase : GuardState::kNoCheck),
158       fd_(fd),
159       file_path_(path),
160       read_only_mode_(read_only_mode) {
161 #if defined(__BIONIC__)
162   if (fd >= 0) {
163     android_fdsan_exchange_owner_tag(fd, 0, GetFdFileOwnerTag(this));
164   }
165 #endif
166 }
167 
FdFile(const std::string & path,int flags,mode_t mode,bool check_usage)168 FdFile::FdFile(const std::string& path, int flags, mode_t mode,
169                bool check_usage) {
170   Open(path, flags, mode);
171   if (!check_usage || !IsOpened()) {
172     guard_state_ = GuardState::kNoCheck;
173   }
174 }
175 
Destroy()176 void FdFile::Destroy() {
177   if (kCheckSafeUsage && (guard_state_ < GuardState::kNoCheck)) {
178     if (guard_state_ < GuardState::kFlushed) {
179       LOG(ERROR) << "File " << file_path_ << " wasn't explicitly flushed before destruction.";
180     }
181     if (guard_state_ < GuardState::kClosed) {
182       LOG(ERROR) << "File " << file_path_ << " wasn't explicitly closed before destruction.";
183     }
184     DCHECK_GE(guard_state_, GuardState::kClosed);
185   }
186   if (fd_ != kInvalidFd) {
187     if (Close() != 0) {
188       PLOG(WARNING) << "Failed to close file with fd=" << fd_ << " path=" << file_path_;
189     }
190   }
191 }
192 
FdFile(FdFile && other)193 FdFile::FdFile(FdFile&& other) noexcept
194     : guard_state_(other.guard_state_),
195       fd_(other.fd_),
196       file_path_(std::move(other.file_path_)),
197       read_only_mode_(other.read_only_mode_) {
198 #if defined(__BIONIC__)
199   if (fd_ >= 0) {
200     android_fdsan_exchange_owner_tag(fd_, GetFdFileOwnerTag(&other), GetFdFileOwnerTag(this));
201   }
202 #endif
203   other.guard_state_ = GuardState::kClosed;
204   other.fd_ = kInvalidFd;
205 }
206 
operator =(FdFile && other)207 FdFile& FdFile::operator=(FdFile&& other) noexcept {
208   if (this == &other) {
209     return *this;
210   }
211 
212   if (this->fd_ != other.fd_) {
213     Destroy();  // Free old state.
214   }
215 
216   guard_state_ = other.guard_state_;
217   fd_ = other.fd_;
218   file_path_ = std::move(other.file_path_);
219   read_only_mode_ = other.read_only_mode_;
220 
221 #if defined(__BIONIC__)
222   if (fd_ >= 0) {
223     android_fdsan_exchange_owner_tag(fd_, GetFdFileOwnerTag(&other), GetFdFileOwnerTag(this));
224   }
225 #endif
226   other.guard_state_ = GuardState::kClosed;
227   other.fd_ = kInvalidFd;
228   return *this;
229 }
230 
~FdFile()231 FdFile::~FdFile() {
232   Destroy();
233 }
234 
Release()235 int FdFile::Release() {
236   int tmp_fd = fd_;
237   fd_ = kInvalidFd;
238   guard_state_ = GuardState::kNoCheck;
239 #if defined(__BIONIC__)
240   if (tmp_fd >= 0) {
241     android_fdsan_exchange_owner_tag(tmp_fd, GetFdFileOwnerTag(this), 0);
242   }
243 #endif
244   return tmp_fd;
245 }
246 
Reset(int fd,bool check_usage)247 void FdFile::Reset(int fd, bool check_usage) {
248   CHECK_NE(fd, fd_);
249 
250   if (fd_ != kInvalidFd) {
251     Destroy();
252   }
253   fd_ = fd;
254 
255 #if defined(__BIONIC__)
256   if (fd_ >= 0) {
257     android_fdsan_exchange_owner_tag(fd_, 0, GetFdFileOwnerTag(this));
258   }
259 #endif
260 
261   if (check_usage) {
262     guard_state_ = fd == kInvalidFd ? GuardState::kNoCheck : GuardState::kBase;
263   } else {
264     guard_state_ = GuardState::kNoCheck;
265   }
266 }
267 
moveTo(GuardState target,GuardState warn_threshold,const char * warning)268 void FdFile::moveTo(GuardState target, GuardState warn_threshold, const char* warning) {
269   if (kCheckSafeUsage) {
270     if (guard_state_ < GuardState::kNoCheck) {
271       if (warn_threshold < GuardState::kNoCheck && guard_state_ >= warn_threshold) {
272         LOG(ERROR) << warning;
273       }
274       guard_state_ = target;
275     }
276   }
277 }
278 
moveUp(GuardState target,const char * warning)279 void FdFile::moveUp(GuardState target, const char* warning) {
280   if (kCheckSafeUsage) {
281     if (guard_state_ < GuardState::kNoCheck) {
282       if (guard_state_ < target) {
283         guard_state_ = target;
284       } else if (target < guard_state_) {
285         LOG(ERROR) << warning;
286       }
287     }
288   }
289 }
290 
Open(const std::string & path,int flags)291 bool FdFile::Open(const std::string& path, int flags) {
292   return Open(path, flags, 0640);
293 }
294 
Open(const std::string & path,int flags,mode_t mode)295 bool FdFile::Open(const std::string& path, int flags, mode_t mode) {
296   static_assert(O_RDONLY == 0, "Readonly flag has unexpected value.");
297   DCHECK_EQ(fd_, kInvalidFd) << path;
298   read_only_mode_ = ((flags & O_ACCMODE) == O_RDONLY);
299   fd_ = TEMP_FAILURE_RETRY(open(path.c_str(), flags, mode));
300   if (fd_ == kInvalidFd) {
301     return false;
302   }
303 
304 #if defined(__BIONIC__)
305   android_fdsan_exchange_owner_tag(fd_, 0, GetFdFileOwnerTag(this));
306 #endif
307 
308   file_path_ = path;
309   if (kCheckSafeUsage && (flags & (O_RDWR | O_CREAT | O_WRONLY)) != 0) {
310     // Start in the base state (not flushed, not closed).
311     guard_state_ = GuardState::kBase;
312   } else {
313     // We are not concerned with read-only files. In that case, proper flushing and closing is
314     // not important.
315     guard_state_ = GuardState::kNoCheck;
316   }
317   return true;
318 }
319 
Close()320 int FdFile::Close() {
321 #if defined(__BIONIC__)
322   int result = android_fdsan_close_with_tag(fd_, GetFdFileOwnerTag(this));
323 #else
324   int result = close(fd_);
325 #endif
326 
327   // Test here, so the file is closed and not leaked.
328   if (kCheckSafeUsage) {
329     DCHECK_GE(guard_state_, GuardState::kFlushed) << "File " << file_path_
330         << " has not been flushed before closing.";
331     moveUp(GuardState::kClosed, nullptr);
332   }
333 
334 #if defined(__linux__)
335   // close always succeeds on linux, even if failure is reported.
336   UNUSED(result);
337 #else
338   if (result == -1) {
339     return -errno;
340   }
341 #endif
342 
343   fd_ = kInvalidFd;
344   file_path_ = "";
345   return 0;
346 }
347 
Flush(bool flush_metadata)348 int FdFile::Flush(bool flush_metadata) {
349   DCHECK(flush_metadata || !read_only_mode_);
350 
351 #ifdef __linux__
352   int rc;
353   if (flush_metadata) {
354     rc = TEMP_FAILURE_RETRY(fsync(fd_));
355   } else {
356     rc = TEMP_FAILURE_RETRY(fdatasync(fd_));
357   }
358 #else
359   int rc = TEMP_FAILURE_RETRY(fsync(fd_));
360 #endif
361 
362   moveUp(GuardState::kFlushed, "Flushing closed file.");
363   if (rc == 0) {
364     return 0;
365   }
366 
367   // Don't report failure if we just tried to flush a pipe or socket.
368   return errno == EINVAL ? 0 : -errno;
369 }
370 
Read(char * buf,int64_t byte_count,int64_t offset) const371 int64_t FdFile::Read(char* buf, int64_t byte_count, int64_t offset) const {
372 #ifdef __linux__
373   int rc = TEMP_FAILURE_RETRY(pread64(fd_, buf, byte_count, offset));
374 #else
375   int rc = TEMP_FAILURE_RETRY(pread(fd_, buf, byte_count, offset));
376 #endif
377   return (rc == -1) ? -errno : rc;
378 }
379 
SetLength(int64_t new_length)380 int FdFile::SetLength(int64_t new_length) {
381   DCHECK(!read_only_mode_);
382 #ifdef __linux__
383   int rc = TEMP_FAILURE_RETRY(ftruncate64(fd_, new_length));
384 #else
385   int rc = TEMP_FAILURE_RETRY(ftruncate(fd_, new_length));
386 #endif
387   moveTo(GuardState::kBase, GuardState::kClosed, "Truncating closed file.");
388   return (rc == -1) ? -errno : rc;
389 }
390 
GetLength() const391 int64_t FdFile::GetLength() const {
392   struct stat s;
393   int rc = TEMP_FAILURE_RETRY(fstat(fd_, &s));
394   return (rc == -1) ? -errno : s.st_size;
395 }
396 
Write(const char * buf,int64_t byte_count,int64_t offset)397 int64_t FdFile::Write(const char* buf, int64_t byte_count, int64_t offset) {
398   DCHECK(!read_only_mode_);
399 #ifdef __linux__
400   int rc = TEMP_FAILURE_RETRY(pwrite64(fd_, buf, byte_count, offset));
401 #else
402   int rc = TEMP_FAILURE_RETRY(pwrite(fd_, buf, byte_count, offset));
403 #endif
404   moveTo(GuardState::kBase, GuardState::kClosed, "Writing into closed file.");
405   return (rc == -1) ? -errno : rc;
406 }
407 
Fd() const408 int FdFile::Fd() const {
409   return fd_;
410 }
411 
ReadOnlyMode() const412 bool FdFile::ReadOnlyMode() const {
413   return read_only_mode_;
414 }
415 
CheckUsage() const416 bool FdFile::CheckUsage() const {
417   return guard_state_ != GuardState::kNoCheck;
418 }
419 
IsOpened() const420 bool FdFile::IsOpened() const {
421   return FdFile::IsOpenFd(fd_);
422 }
423 
ReadIgnoreOffset(int fd,void * buf,size_t count,off_t offset)424 static ssize_t ReadIgnoreOffset(int fd, void *buf, size_t count, off_t offset) {
425   DCHECK_EQ(offset, 0);
426   return read(fd, buf, count);
427 }
428 
429 template <ssize_t (*read_func)(int, void*, size_t, off_t)>
ReadFullyGeneric(int fd,void * buffer,size_t byte_count,size_t offset)430 static bool ReadFullyGeneric(int fd, void* buffer, size_t byte_count, size_t offset) {
431   char* ptr = static_cast<char*>(buffer);
432   while (byte_count > 0) {
433     ssize_t bytes_read = TEMP_FAILURE_RETRY(read_func(fd, ptr, byte_count, offset));
434     if (bytes_read <= 0) {
435       // 0: end of file
436       // -1: error
437       return false;
438     }
439     byte_count -= bytes_read;  // Reduce the number of remaining bytes.
440     ptr += bytes_read;  // Move the buffer forward.
441     offset += static_cast<size_t>(bytes_read);  // Move the offset forward.
442   }
443   return true;
444 }
445 
ReadFully(void * buffer,size_t byte_count)446 bool FdFile::ReadFully(void* buffer, size_t byte_count) {
447   return ReadFullyGeneric<ReadIgnoreOffset>(fd_, buffer, byte_count, 0);
448 }
449 
PreadFully(void * buffer,size_t byte_count,size_t offset)450 bool FdFile::PreadFully(void* buffer, size_t byte_count, size_t offset) {
451   return ReadFullyGeneric<pread>(fd_, buffer, byte_count, offset);
452 }
453 
454 template <bool kUseOffset>
WriteFullyGeneric(const void * buffer,size_t byte_count,size_t offset)455 bool FdFile::WriteFullyGeneric(const void* buffer, size_t byte_count, size_t offset) {
456   DCHECK(!read_only_mode_);
457   moveTo(GuardState::kBase, GuardState::kClosed, "Writing into closed file.");
458   DCHECK(kUseOffset || offset == 0u);
459   const char* ptr = static_cast<const char*>(buffer);
460   while (byte_count > 0) {
461     ssize_t bytes_written = kUseOffset
462         ? TEMP_FAILURE_RETRY(pwrite(fd_, ptr, byte_count, offset))
463         : TEMP_FAILURE_RETRY(write(fd_, ptr, byte_count));
464     if (bytes_written == -1) {
465       return false;
466     }
467     byte_count -= bytes_written;  // Reduce the number of remaining bytes.
468     ptr += bytes_written;  // Move the buffer forward.
469     offset += static_cast<size_t>(bytes_written);
470   }
471   return true;
472 }
473 
PwriteFully(const void * buffer,size_t byte_count,size_t offset)474 bool FdFile::PwriteFully(const void* buffer, size_t byte_count, size_t offset) {
475   return WriteFullyGeneric<true>(buffer, byte_count, offset);
476 }
477 
WriteFully(const void * buffer,size_t byte_count)478 bool FdFile::WriteFully(const void* buffer, size_t byte_count) {
479   return WriteFullyGeneric<false>(buffer, byte_count, 0u);
480 }
481 
Rename(const std::string & new_path)482 bool FdFile::Rename(const std::string& new_path) {
483   if (kCheckSafeUsage) {
484     // Filesystems that use delayed allocation (e.g., ext4) may journal a rename before a data
485     // update is written to disk. Therefore on system crash, the data update may not persist.
486     // Guard against this by ensuring the file has been flushed prior to rename.
487     if (guard_state_ < GuardState::kFlushed) {
488       LOG(ERROR) << "File " << file_path_ << " has not been flushed before renaming.";
489     }
490     DCHECK_GE(guard_state_, GuardState::kFlushed);
491   }
492 
493   if (!FilePathMatchesFd()) {
494     LOG(ERROR) << "Failed rename because the file descriptor is not backed by the expected file "
495                << "path: " << file_path_;
496     return false;
497   }
498 
499   std::string old_path = file_path_;
500   int rc = std::rename(old_path.c_str(), new_path.c_str());
501   if (rc != 0) {
502     LOG(ERROR) << "Rename from '" << old_path << "' to '" << new_path << "' failed.";
503     return false;
504   }
505   file_path_ = new_path;
506 
507   // Rename modifies the directory entries mapped within the parent directory file descriptor(s),
508   // rather than the file, so flushing the file will not persist the change to disk. Therefore, we
509   // flush the parent directory file descriptor(s).
510   std::string old_dir = android::base::Dirname(old_path);
511   std::string new_dir = android::base::Dirname(new_path);
512   std::vector<std::string> sync_dirs = {new_dir};
513   if (new_dir != old_dir) {
514     sync_dirs.emplace_back(old_dir);
515   }
516   for (auto& dirname : sync_dirs) {
517     FdFile dir = FdFile(dirname, O_RDONLY, /*check_usage=*/false);
518     rc = dir.Flush(/*flush_metadata=*/true);
519     if (rc != 0) {
520       LOG(ERROR) << "Flushing directory '" << dirname << "' during rename failed.";
521       return false;
522     }
523     rc = dir.Close();
524     if (rc != 0) {
525       LOG(ERROR) << "Closing directory '" << dirname << "' during rename failed.";
526       return false;
527     }
528   }
529   return true;
530 }
531 
532 #ifdef __linux__
SparseWrite(const uint8_t * data,size_t size,const std::vector<uint8_t> & zeroes)533 bool FdFile::SparseWrite(const uint8_t* data,
534                          size_t size,
535                          const std::vector<uint8_t>& zeroes) {
536   DCHECK_GE(zeroes.size(), size);
537   if (memcmp(zeroes.data(), data, size) == 0) {
538     // These bytes are all zeroes, skip them by moving the file offset via lseek SEEK_CUR (available
539     // since linux kernel 3.1).
540     if (TEMP_FAILURE_RETRY(lseek(Fd(), size, SEEK_CUR)) < 0) {
541       return false;
542     }
543   } else {
544     if (!WriteFully(data, size)) {
545       return false;
546     }
547   }
548   return true;
549 }
550 
UserspaceSparseCopy(const FdFile * input_file,off_t off,size_t size,size_t fs_blocksize)551 bool FdFile::UserspaceSparseCopy(const FdFile* input_file,
552                                  off_t off,
553                                  size_t size,
554                                  size_t fs_blocksize) {
555   // Map the input file. We will begin the copy 'off' bytes into the map.
556   art::MemMap::Init();
557   std::string error_msg;
558   art::MemMap mmap = art::MemMap::MapFile(off + size,
559                                           PROT_READ,
560                                           MAP_PRIVATE,
561                                           input_file->Fd(),
562                                           /*start=*/0,
563                                           /*low_4gb=*/false,
564                                           input_file->GetPath().c_str(),
565                                           &error_msg);
566   if (!mmap.IsValid()) {
567     LOG(ERROR) << "Failed to mmap " << input_file->GetPath() << " for copying: " << error_msg;
568     return false;
569   }
570 
571   std::vector<uint8_t> zeroes(/*n=*/fs_blocksize, /*val=*/0);
572 
573   // Iterate through each fs_blocksize of the copy region.
574   uint8_t* input_ptr = mmap.Begin() + off;
575   for (; (input_ptr + fs_blocksize) <= mmap.End(); input_ptr += fs_blocksize) {
576     if (!SparseWrite(input_ptr, fs_blocksize, zeroes)) {
577       return false;
578     }
579   }
580   // Finish copying any remaining bytes.
581   const size_t remaining_bytes = size % fs_blocksize;
582   if (remaining_bytes > 0) {
583     if (!SparseWrite(input_ptr, remaining_bytes, zeroes)) {
584       return false;
585     }
586   }
587   // Update the input file FD offset to the end of the copy region.
588   off_t input_offset = TEMP_FAILURE_RETRY(lseek(input_file->Fd(), off + size, SEEK_SET));
589   if (input_offset != (off + static_cast<off_t>(size))) {
590     return false;
591   }
592   return true;
593 }
594 #endif
595 
Copy(FdFile * input_file,int64_t offset,int64_t size)596 bool FdFile::Copy(FdFile* input_file, int64_t offset, int64_t size) {
597   DCHECK(!read_only_mode_);
598   off_t off = static_cast<off_t>(offset);
599   off_t sz = static_cast<off_t>(size);
600   if (offset < 0 || static_cast<int64_t>(off) != offset ||
601       size < 0 || static_cast<int64_t>(sz) != size ||
602       sz > std::numeric_limits<off_t>::max() - off) {
603     errno = EINVAL;
604     return false;
605   }
606   if (size == 0) {
607     return true;
608   }
609 
610 #ifdef __linux__
611   off_t current_offset = TEMP_FAILURE_RETRY(lseek(Fd(), 0, SEEK_CUR));
612   if (GetLength() > current_offset) {
613     // Copying to an existing region of the destination file is not supported. The current
614     // implementation would incorrectly preserve all existing data regions within the output file
615     // which match the locations of holes within the input file.
616     LOG(ERROR) << "Cannot copy into an existing region of the destination file.";
617     errno = EINVAL;
618     return false;
619   }
620   struct stat output_stat;
621   if (TEMP_FAILURE_RETRY(fstat(Fd(), &output_stat)) < 0) {
622     return false;
623   }
624   const off_t fs_blocksize = output_stat.st_blksize;
625   if (!art::IsAlignedParam(current_offset, fs_blocksize)) {
626     // The input region is copied (skipped or written) in chunks of the output file's blocksize. For
627     // those chunks to be represented as holes or data, they should land as aligned blocks in the
628     // output file. Therefore, here we enforce that the current output offset is aligned.
629     LOG(ERROR) << "Copy destination FD offset (" << current_offset << ") must be aligned with"
630                << " blocksize (" << fs_blocksize << ").";
631     errno = EINVAL;
632     return false;
633   }
634   const size_t end_length = GetLength() + sz;
635   if (!UserspaceSparseCopy(input_file, off, sz, fs_blocksize)) {
636     return false;
637   }
638   // In case the last blocks of the input file were a hole, fix the length to what would have been
639   // set if they had been data.
640   if (SetLength(end_length) != 0) {
641     return false;
642   }
643 #else
644   if (lseek(input_file->Fd(), off, SEEK_SET) != off) {
645     return false;
646   }
647   constexpr size_t kMaxBufferSize = 16 * ::art::KB;
648   const size_t buffer_size = std::min<uint64_t>(size, kMaxBufferSize);
649   art::UniqueCPtr<void> buffer(malloc(buffer_size));
650   if (buffer == nullptr) {
651     errno = ENOMEM;
652     return false;
653   }
654   while (size != 0) {
655     size_t chunk_size = std::min<uint64_t>(buffer_size, size);
656     if (!input_file->ReadFully(buffer.get(), chunk_size) ||
657         !WriteFully(buffer.get(), chunk_size)) {
658       return false;
659     }
660     size -= chunk_size;
661   }
662 #endif
663   return true;
664 }
665 
FilePathMatchesFd()666 bool FdFile::FilePathMatchesFd() {
667   if (file_path_.empty()) {
668     return false;
669   }
670   // Try to figure out whether file_path_ is still referring to the one on disk.
671   bool is_current = false;
672   struct stat this_stat, current_stat;
673   int cur_fd = TEMP_FAILURE_RETRY(open(file_path_.c_str(), O_RDONLY | O_CLOEXEC));
674   if (cur_fd > 0) {
675     // File still exists.
676     if (fstat(fd_, &this_stat) == 0 && fstat(cur_fd, &current_stat) == 0) {
677       is_current = (this_stat.st_dev == current_stat.st_dev) &&
678                    (this_stat.st_ino == current_stat.st_ino);
679     }
680     close(cur_fd);
681   }
682   return is_current;
683 }
684 
Unlink()685 bool FdFile::Unlink() {
686   bool is_current = FilePathMatchesFd();
687   if (is_current) {
688     unlink(file_path_.c_str());
689   }
690 
691   return is_current;
692 }
693 
Erase(bool unlink)694 bool FdFile::Erase(bool unlink) {
695   DCHECK(!read_only_mode_);
696 
697   bool ret_result = true;
698   if (unlink) {
699     ret_result = Unlink();
700   }
701 
702   int result;
703   result = SetLength(0);
704   result = Flush();
705   result = Close();
706   // Ignore the errors.
707   (void) result;
708 
709   return ret_result;
710 }
711 
FlushCloseOrErase()712 int FdFile::FlushCloseOrErase() {
713   DCHECK(!read_only_mode_);
714   int flush_result = Flush();
715   if (flush_result != 0) {
716     LOG(ERROR) << "CloseOrErase failed while flushing a file.";
717     Erase();
718     return flush_result;
719   }
720   int close_result = Close();
721   if (close_result != 0) {
722     LOG(ERROR) << "CloseOrErase failed while closing a file.";
723     Erase();
724     return close_result;
725   }
726   return 0;
727 }
728 
FlushClose()729 int FdFile::FlushClose() {
730   DCHECK(!read_only_mode_);
731   int flush_result = Flush();
732   if (flush_result != 0) {
733     LOG(ERROR) << "FlushClose failed while flushing a file.";
734   }
735   int close_result = Close();
736   if (close_result != 0) {
737     LOG(ERROR) << "FlushClose failed while closing a file.";
738   }
739   return (flush_result != 0) ? flush_result : close_result;
740 }
741 
MarkUnchecked()742 void FdFile::MarkUnchecked() {
743   guard_state_ = GuardState::kNoCheck;
744 }
745 
ClearContent()746 bool FdFile::ClearContent() {
747   DCHECK(!read_only_mode_);
748   if (SetLength(0) < 0) {
749     PLOG(ERROR) << "Failed to reset the length";
750     return false;
751   }
752   return ResetOffset();
753 }
754 
ResetOffset()755 bool FdFile::ResetOffset() {
756   DCHECK(!read_only_mode_);
757   off_t rc =  TEMP_FAILURE_RETRY(lseek(fd_, 0, SEEK_SET));
758   if (rc == static_cast<off_t>(-1)) {
759     PLOG(ERROR) << "Failed to reset the offset";
760     return false;
761   }
762   return true;
763 }
764 
Compare(FdFile * other)765 int FdFile::Compare(FdFile* other) {
766   int64_t length = GetLength();
767   int64_t length2 = other->GetLength();
768   if (length != length2) {
769     return length < length2 ? -1 : 1;
770   }
771   static const size_t kBufferSize = 4096;
772   std::unique_ptr<uint8_t[]> buffer1(new uint8_t[kBufferSize]);
773   std::unique_ptr<uint8_t[]> buffer2(new uint8_t[kBufferSize]);
774   size_t offset = 0;
775   while (length > 0) {
776     size_t len = std::min(kBufferSize, static_cast<size_t>(length));
777     if (!PreadFully(&buffer1[0], len, offset)) {
778       return -1;
779     }
780     if (!other->PreadFully(&buffer2[0], len, offset)) {
781       return 1;
782     }
783     int result = memcmp(&buffer1[0], &buffer2[0], len);
784     if (result != 0) {
785       return result;
786     }
787     length -= len;
788     offset += len;
789   }
790   return 0;
791 }
792 
IsOpenFd(int fd)793 bool FdFile::IsOpenFd(int fd) {
794   if (fd == kInvalidFd) {
795     return false;
796   }
797   #ifdef _WIN32  // Windows toolchain does not support F_GETFD.
798     return true;
799   #else
800     int saved_errno = errno;
801     bool is_open = (fcntl(fd, F_GETFD) != -1);
802     errno = saved_errno;
803     return is_open;
804   #endif
805 }
806 
807 }  // namespace unix_file
808