1 /* 2 * Copyright (C) 2024 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 "linux_fileops.h" 18 19 #include <unistd.h> 20 21 namespace pdfClient { 22 23 namespace { 24 25 // The maximum number of symlinks to try to dereference before giving up. 26 const int kMaxSymlinkDereferences = 1000; 27 #ifdef O_LARGEFILE 28 constexpr int kOLargefileIfAvailable = O_LARGEFILE; 29 #else 30 constexpr int kOLargefileIfAvailable = 0; 31 #endif 32 33 } // namespace 34 FDCloser(int fd)35LinuxFileOps::FDCloser::FDCloser(int fd) : fd_(fd) {} 36 FDCloser(LinuxFileOps::FDCloser && orig)37LinuxFileOps::FDCloser::FDCloser(LinuxFileOps::FDCloser&& orig) : fd_(orig.Release()) {} 38 operator =(LinuxFileOps::FDCloser && orig)39LinuxFileOps::FDCloser& LinuxFileOps::FDCloser::operator=(LinuxFileOps::FDCloser&& orig) { 40 if (this != &orig) { 41 Close(); 42 fd_ = orig.Release(); 43 } 44 return *this; 45 } 46 ~FDCloser()47LinuxFileOps::FDCloser::~FDCloser() { 48 Close(); 49 } 50 get() const51int LinuxFileOps::FDCloser::get() const { 52 return fd_; 53 } 54 Close()55bool LinuxFileOps::FDCloser::Close() { 56 int fd = Release(); 57 if (fd == kCanonicalInvalidFd) { 58 return false; 59 } 60 return LinuxFileOps::CloseFD(fd); 61 } 62 Release()63int LinuxFileOps::FDCloser::Release() { 64 int ret = fd_; 65 fd_ = kCanonicalInvalidFd; 66 return ret; 67 } 68 Swap(FDCloser * other)69void LinuxFileOps::FDCloser::Swap(FDCloser* other) { 70 std::swap(fd_, other->fd_); 71 } 72 CloseFD(int fd)73bool LinuxFileOps::CloseFD(int fd) { 74 int ret = ::close(fd); 75 if (ret != 0) { 76 if (errno == EINTR) { 77 // Calling close again on EINTR is a bad thing. See 78 // http://lkml.org/lkml/2005/9/11/49 for more details. 79 return true; // COV_NF_LINE 80 } else { 81 return false; 82 } 83 } 84 85 return true; 86 } 87 88 } // namespace pdfClient