2 * Copyright 2013 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "folly/FileUtil.h"
21 #include "folly/detail/FileUtilDetail.h"
25 using namespace fileutil_detail;
27 int openNoInt(const char* name, int flags, mode_t mode) {
28 return wrapNoInt(open, name, flags, mode);
31 int closeNoInt(int fd) {
33 // Ignore EINTR. On Linux, close() may only return EINTR after the file
34 // descriptor has been closed, so you must not retry close() on EINTR --
35 // in the best case, you'll get EBADF, and in the worst case, you'll end up
36 // closing a different file (one opened from another thread).
38 // Interestingly enough, the Single Unix Specification says that the state
39 // of the file descriptor is unspecified if close returns EINTR. In that
40 // case, the safe thing to do is also not to retry close() -- leaking a file
41 // descriptor is probably better than closing the wrong file.
42 if (r == -1 && errno == EINTR) {
48 int fsyncNoInt(int fd) {
49 return wrapNoInt(fsync, fd);
52 int fdatasyncNoInt(int fd) {
53 return wrapNoInt(fdatasync, fd);
56 int ftruncateNoInt(int fd, off_t len) {
57 return wrapNoInt(ftruncate, fd, len);
60 int truncateNoInt(const char* path, off_t len) {
61 return wrapNoInt(truncate, path, len);
64 ssize_t readNoInt(int fd, void* buf, size_t count) {
65 return wrapNoInt(read, fd, buf, count);
68 ssize_t preadNoInt(int fd, void* buf, size_t count, off_t offset) {
69 return wrapNoInt(pread, fd, buf, count, offset);
72 ssize_t readvNoInt(int fd, const iovec* iov, int count) {
73 return wrapNoInt(writev, fd, iov, count);
76 ssize_t writeNoInt(int fd, const void* buf, size_t count) {
77 return wrapNoInt(write, fd, buf, count);
80 ssize_t pwriteNoInt(int fd, const void* buf, size_t count, off_t offset) {
81 return wrapNoInt(pwrite, fd, buf, count, offset);
84 ssize_t writevNoInt(int fd, const iovec* iov, int count) {
85 return wrapNoInt(writev, fd, iov, count);
88 ssize_t readFull(int fd, void* buf, size_t count) {
89 return wrapFull(read, fd, buf, count);
92 ssize_t preadFull(int fd, void* buf, size_t count, off_t offset) {
93 return wrapFull(pread, fd, buf, count, offset);
96 ssize_t writeFull(int fd, const void* buf, size_t count) {
97 return wrapFull(write, fd, const_cast<void*>(buf), count);
100 ssize_t pwriteFull(int fd, const void* buf, size_t count, off_t offset) {
101 return wrapFull(pwrite, fd, const_cast<void*>(buf), count, offset);
104 ssize_t readvFull(int fd, iovec* iov, int count) {
105 return wrapvFull(readv, fd, iov, count);
108 #ifdef FOLLY_HAVE_PREADV
109 ssize_t preadvFull(int fd, iovec* iov, int count, off_t offset) {
110 return wrapvFull(preadv, fd, iov, count, offset);
114 ssize_t writevFull(int fd, iovec* iov, int count) {
115 return wrapvFull(writev, fd, iov, count);
118 #ifdef FOLLY_HAVE_PWRITEV
119 ssize_t pwritevFull(int fd, iovec* iov, int count, off_t offset) {
120 return wrapvFull(pwritev, fd, iov, count, offset);