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