9d50e82ada00692c36750a071d59745d82de07da
[folly.git] / folly / FileUtil.cpp
1 /*
2  * Copyright 2016 Facebook, Inc.
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 <folly/FileUtil.h>
18
19 #include <cerrno>
20 #include <sys/socket.h>
21
22 #include <folly/detail/FileUtilDetail.h>
23 #include <folly/portability/Fcntl.h>
24 #include <folly/portability/SysFile.h>
25
26 namespace folly {
27
28 using namespace fileutil_detail;
29
30 int openNoInt(const char* name, int flags, mode_t mode) {
31   return wrapNoInt(open, name, flags, mode);
32 }
33
34 int closeNoInt(int fd) {
35   int r = close(fd);
36   // Ignore EINTR.  On Linux, close() may only return EINTR after the file
37   // descriptor has been closed, so you must not retry close() on EINTR --
38   // in the best case, you'll get EBADF, and in the worst case, you'll end up
39   // closing a different file (one opened from another thread).
40   //
41   // Interestingly enough, the Single Unix Specification says that the state
42   // of the file descriptor is unspecified if close returns EINTR.  In that
43   // case, the safe thing to do is also not to retry close() -- leaking a file
44   // descriptor is definitely better than closing the wrong file.
45   if (r == -1 && errno == EINTR) {
46     r = 0;
47   }
48   return r;
49 }
50
51 int fsyncNoInt(int fd) {
52   return wrapNoInt(fsync, fd);
53 }
54
55 int dupNoInt(int fd) {
56   return wrapNoInt(dup, fd);
57 }
58
59 int dup2NoInt(int oldfd, int newfd) {
60   return wrapNoInt(dup2, oldfd, newfd);
61 }
62
63 int fdatasyncNoInt(int fd) {
64 #if defined(__APPLE__)
65   return wrapNoInt(fcntl, fd, F_FULLFSYNC);
66 #elif defined(__FreeBSD__) || defined(_MSC_VER)
67   return wrapNoInt(fsync, fd);
68 #else
69   return wrapNoInt(fdatasync, fd);
70 #endif
71 }
72
73 int ftruncateNoInt(int fd, off_t len) {
74   return wrapNoInt(ftruncate, fd, len);
75 }
76
77 int truncateNoInt(const char* path, off_t len) {
78   return wrapNoInt(truncate, path, len);
79 }
80
81 int flockNoInt(int fd, int operation) {
82   return wrapNoInt(flock, fd, operation);
83 }
84
85 int shutdownNoInt(int fd, int how) {
86   return wrapNoInt(shutdown, fd, how);
87 }
88
89 ssize_t readNoInt(int fd, void* buf, size_t count) {
90   return wrapNoInt(read, fd, buf, count);
91 }
92
93 ssize_t preadNoInt(int fd, void* buf, size_t count, off_t offset) {
94   return wrapNoInt(pread, fd, buf, count, offset);
95 }
96
97 ssize_t readvNoInt(int fd, const iovec* iov, int count) {
98   return wrapNoInt(readv, fd, iov, count);
99 }
100
101 ssize_t writeNoInt(int fd, const void* buf, size_t count) {
102   return wrapNoInt(write, fd, buf, count);
103 }
104
105 ssize_t pwriteNoInt(int fd, const void* buf, size_t count, off_t offset) {
106   return wrapNoInt(pwrite, fd, buf, count, offset);
107 }
108
109 ssize_t writevNoInt(int fd, const iovec* iov, int count) {
110   return wrapNoInt(writev, fd, iov, count);
111 }
112
113 ssize_t readFull(int fd, void* buf, size_t count) {
114   return wrapFull(read, fd, buf, count);
115 }
116
117 ssize_t preadFull(int fd, void* buf, size_t count, off_t offset) {
118   return wrapFull(pread, fd, buf, count, offset);
119 }
120
121 ssize_t writeFull(int fd, const void* buf, size_t count) {
122   return wrapFull(write, fd, const_cast<void*>(buf), count);
123 }
124
125 ssize_t pwriteFull(int fd, const void* buf, size_t count, off_t offset) {
126   return wrapFull(pwrite, fd, const_cast<void*>(buf), count, offset);
127 }
128
129 ssize_t readvFull(int fd, iovec* iov, int count) {
130   return wrapvFull(readv, fd, iov, count);
131 }
132
133 ssize_t preadvFull(int fd, iovec* iov, int count, off_t offset) {
134   return wrapvFull(preadv, fd, iov, count, offset);
135 }
136
137 ssize_t writevFull(int fd, iovec* iov, int count) {
138   return wrapvFull(writev, fd, iov, count);
139 }
140
141 ssize_t pwritevFull(int fd, iovec* iov, int count, off_t offset) {
142   return wrapvFull(pwritev, fd, iov, count, offset);
143 }
144
145 }  // namespaces