folly: avoid compile warning/failure due to lvalue-to-rvalue conversion
[folly.git] / folly / FileUtil.cpp
1 /*
2  * Copyright 2017 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
21 #include <folly/Exception.h>
22 #include <folly/detail/FileUtilDetail.h>
23 #include <folly/portability/Fcntl.h>
24 #include <folly/portability/Sockets.h>
25 #include <folly/portability/Stdlib.h>
26 #include <folly/portability/SysFile.h>
27 #include <folly/portability/SysStat.h>
28
29 namespace folly {
30
31 using namespace fileutil_detail;
32
33 int openNoInt(const char* name, int flags, mode_t mode) {
34   return int(wrapNoInt(open, name, flags, mode));
35 }
36
37 int closeNoInt(int fd) {
38   int r = close(fd);
39   // Ignore EINTR.  On Linux, close() may only return EINTR after the file
40   // descriptor has been closed, so you must not retry close() on EINTR --
41   // in the best case, you'll get EBADF, and in the worst case, you'll end up
42   // closing a different file (one opened from another thread).
43   //
44   // Interestingly enough, the Single Unix Specification says that the state
45   // of the file descriptor is unspecified if close returns EINTR.  In that
46   // case, the safe thing to do is also not to retry close() -- leaking a file
47   // descriptor is definitely better than closing the wrong file.
48   if (r == -1 && errno == EINTR) {
49     r = 0;
50   }
51   return r;
52 }
53
54 int fsyncNoInt(int fd) {
55   return int(wrapNoInt(fsync, fd));
56 }
57
58 int dupNoInt(int fd) {
59   return int(wrapNoInt(dup, fd));
60 }
61
62 int dup2NoInt(int oldfd, int newfd) {
63   return int(wrapNoInt(dup2, oldfd, newfd));
64 }
65
66 int fdatasyncNoInt(int fd) {
67 #if defined(__APPLE__)
68   return int(wrapNoInt(fcntl, fd, F_FULLFSYNC));
69 #elif defined(__FreeBSD__) || defined(_MSC_VER)
70   return int(wrapNoInt(fsync, fd));
71 #else
72   return int(wrapNoInt(fdatasync, fd));
73 #endif
74 }
75
76 int ftruncateNoInt(int fd, off_t len) {
77   return int(wrapNoInt(ftruncate, fd, len));
78 }
79
80 int truncateNoInt(const char* path, off_t len) {
81   return int(wrapNoInt(truncate, path, len));
82 }
83
84 int flockNoInt(int fd, int operation) {
85   return int(wrapNoInt(flock, fd, operation));
86 }
87
88 int shutdownNoInt(int fd, int how) {
89   return int(wrapNoInt(portability::sockets::shutdown, fd, how));
90 }
91
92 ssize_t readNoInt(int fd, void* buf, size_t count) {
93   return wrapNoInt(read, fd, buf, count);
94 }
95
96 ssize_t preadNoInt(int fd, void* buf, size_t count, off_t offset) {
97   return wrapNoInt(pread, fd, buf, count, offset);
98 }
99
100 ssize_t readvNoInt(int fd, const iovec* iov, int count) {
101   return wrapNoInt(readv, fd, iov, count);
102 }
103
104 ssize_t writeNoInt(int fd, const void* buf, size_t count) {
105   return wrapNoInt(write, fd, buf, count);
106 }
107
108 ssize_t pwriteNoInt(int fd, const void* buf, size_t count, off_t offset) {
109   return wrapNoInt(pwrite, fd, buf, count, offset);
110 }
111
112 ssize_t writevNoInt(int fd, const iovec* iov, int count) {
113   return wrapNoInt(writev, fd, iov, count);
114 }
115
116 ssize_t readFull(int fd, void* buf, size_t count) {
117   return wrapFull(read, fd, buf, count);
118 }
119
120 ssize_t preadFull(int fd, void* buf, size_t count, off_t offset) {
121   return wrapFull(pread, fd, buf, count, offset);
122 }
123
124 ssize_t writeFull(int fd, const void* buf, size_t count) {
125   return wrapFull(write, fd, const_cast<void*>(buf), count);
126 }
127
128 ssize_t pwriteFull(int fd, const void* buf, size_t count, off_t offset) {
129   return wrapFull(pwrite, fd, const_cast<void*>(buf), count, offset);
130 }
131
132 ssize_t readvFull(int fd, iovec* iov, int count) {
133   return wrapvFull(readv, fd, iov, count);
134 }
135
136 ssize_t preadvFull(int fd, iovec* iov, int count, off_t offset) {
137   return wrapvFull(preadv, fd, iov, count, offset);
138 }
139
140 ssize_t writevFull(int fd, iovec* iov, int count) {
141   return wrapvFull(writev, fd, iov, count);
142 }
143
144 ssize_t pwritevFull(int fd, iovec* iov, int count, off_t offset) {
145   return wrapvFull(pwritev, fd, iov, count, offset);
146 }
147
148 int writeFileAtomicNoThrow(
149     StringPiece filename,
150     iovec* iov,
151     int count,
152     mode_t permissions) {
153   // We write the data to a temporary file name first, then atomically rename
154   // it into place.  This ensures that the file contents will always be valid,
155   // even if we crash or are killed partway through writing out data.
156   //
157   // Create a buffer that will contain two things:
158   // - A nul-terminated version of the filename
159   // - The temporary file name
160   std::vector<char> pathBuffer;
161   // Note that we have to explicitly pass in the size here to make
162   // sure the nul byte gets included in the data.
163   constexpr folly::StringPiece suffix(".XXXXXX\0", 8);
164   pathBuffer.resize((2 * filename.size()) + 1 + suffix.size());
165   // Copy in the filename and then a nul terminator
166   memcpy(pathBuffer.data(), filename.data(), filename.size());
167   pathBuffer[filename.size()] = '\0';
168   const char* const filenameCStr = pathBuffer.data();
169   // Now prepare the temporary path template
170   char* const tempPath = pathBuffer.data() + filename.size() + 1;
171   memcpy(tempPath, filename.data(), filename.size());
172   memcpy(tempPath + filename.size(), suffix.data(), suffix.size());
173
174   auto tmpFD = mkstemp(tempPath);
175   if (tmpFD == -1) {
176     return errno;
177   }
178   bool success = false;
179   SCOPE_EXIT {
180     if (tmpFD != -1) {
181       close(tmpFD);
182     }
183     if (!success) {
184       unlink(tempPath);
185     }
186   };
187
188   auto rc = writevFull(tmpFD, iov, count);
189   if (rc == -1) {
190     return errno;
191   }
192
193   rc = fchmod(tmpFD, permissions);
194   if (rc == -1) {
195     return errno;
196   }
197
198   // Close the file before renaming to make sure all data has
199   // been successfully written.
200   rc = close(tmpFD);
201   tmpFD = -1;
202   if (rc == -1) {
203     return errno;
204   }
205
206   rc = rename(tempPath, filenameCStr);
207   if (rc == -1) {
208     return errno;
209   }
210   success = true;
211   return 0;
212 }
213
214 void writeFileAtomic(
215     StringPiece filename,
216     iovec* iov,
217     int count,
218     mode_t permissions) {
219   auto rc = writeFileAtomicNoThrow(filename, iov, count, permissions);
220   checkPosixError(rc, "writeFileAtomic() failed to update ", filename);
221 }
222
223 void writeFileAtomic(StringPiece filename, ByteRange data, mode_t permissions) {
224   iovec iov;
225   iov.iov_base = const_cast<unsigned char*>(data.data());
226   iov.iov_len = data.size();
227   auto rc = writeFileAtomicNoThrow(filename, &iov, 1, permissions);
228   checkPosixError(rc, "writeFileAtomic() failed to update ", filename);
229 }
230
231 void writeFileAtomic(
232     StringPiece filename,
233     StringPiece data,
234     mode_t permissions) {
235   writeFileAtomic(filename, ByteRange(data), permissions);
236 }
237
238 } // namespace folly