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