add a new writeFileAtomic() function
[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
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/SysFile.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 definitely 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 dupNoInt(int fd) {
57   return wrapNoInt(dup, fd);
58 }
59
60 int dup2NoInt(int oldfd, int newfd) {
61   return wrapNoInt(dup2, oldfd, newfd);
62 }
63
64 int fdatasyncNoInt(int fd) {
65 #if defined(__APPLE__)
66   return wrapNoInt(fcntl, fd, F_FULLFSYNC);
67 #elif defined(__FreeBSD__) || defined(_MSC_VER)
68   return wrapNoInt(fsync, fd);
69 #else
70   return wrapNoInt(fdatasync, fd);
71 #endif
72 }
73
74 int ftruncateNoInt(int fd, off_t len) {
75   return wrapNoInt(ftruncate, fd, len);
76 }
77
78 int truncateNoInt(const char* path, off_t len) {
79   return wrapNoInt(truncate, path, len);
80 }
81
82 int flockNoInt(int fd, int operation) {
83   return wrapNoInt(flock, fd, operation);
84 }
85
86 int shutdownNoInt(int fd, int how) {
87   return wrapNoInt(portability::sockets::shutdown, fd, how);
88 }
89
90 ssize_t readNoInt(int fd, void* buf, size_t count) {
91   return wrapNoInt(read, fd, buf, count);
92 }
93
94 ssize_t preadNoInt(int fd, void* buf, size_t count, off_t offset) {
95   return wrapNoInt(pread, fd, buf, count, offset);
96 }
97
98 ssize_t readvNoInt(int fd, const iovec* iov, int count) {
99   return wrapNoInt(readv, fd, iov, count);
100 }
101
102 ssize_t writeNoInt(int fd, const void* buf, size_t count) {
103   return wrapNoInt(write, fd, buf, count);
104 }
105
106 ssize_t pwriteNoInt(int fd, const void* buf, size_t count, off_t offset) {
107   return wrapNoInt(pwrite, fd, buf, count, offset);
108 }
109
110 ssize_t writevNoInt(int fd, const iovec* iov, int count) {
111   return wrapNoInt(writev, fd, iov, count);
112 }
113
114 ssize_t readFull(int fd, void* buf, size_t count) {
115   return wrapFull(read, fd, buf, count);
116 }
117
118 ssize_t preadFull(int fd, void* buf, size_t count, off_t offset) {
119   return wrapFull(pread, fd, buf, count, offset);
120 }
121
122 ssize_t writeFull(int fd, const void* buf, size_t count) {
123   return wrapFull(write, fd, const_cast<void*>(buf), count);
124 }
125
126 ssize_t pwriteFull(int fd, const void* buf, size_t count, off_t offset) {
127   return wrapFull(pwrite, fd, const_cast<void*>(buf), count, offset);
128 }
129
130 ssize_t readvFull(int fd, iovec* iov, int count) {
131   return wrapvFull(readv, fd, iov, count);
132 }
133
134 ssize_t preadvFull(int fd, iovec* iov, int count, off_t offset) {
135   return wrapvFull(preadv, fd, iov, count, offset);
136 }
137
138 ssize_t writevFull(int fd, iovec* iov, int count) {
139   return wrapvFull(writev, fd, iov, count);
140 }
141
142 ssize_t pwritevFull(int fd, iovec* iov, int count, off_t offset) {
143   return wrapvFull(pwritev, fd, iov, count, offset);
144 }
145
146 int writeFileAtomicNoThrow(
147     StringPiece filename,
148     iovec* iov,
149     int count,
150     mode_t permissions) {
151   // We write the data to a temporary file name first, then atomically rename
152   // it into place.  This ensures that the file contents will always be valid,
153   // even if we crash or are killed partway through writing out data.
154   //
155   // Create a buffer that will contain two things:
156   // - A nul-terminated version of the filename
157   // - The temporary file name
158   std::vector<char> pathBuffer;
159   // Note that we have to explicitly pass in the size here to make
160   // sure the nul byte gets included in the data.
161   constexpr folly::StringPiece suffix(".XXXXXX\0", 8);
162   pathBuffer.resize((2 * filename.size()) + 1 + suffix.size());
163   // Copy in the filename and then a nul terminator
164   memcpy(pathBuffer.data(), filename.data(), filename.size());
165   pathBuffer[filename.size()] = '\0';
166   const char* const filenameCStr = pathBuffer.data();
167   // Now prepare the temporary path template
168   char* const tempPath = pathBuffer.data() + filename.size() + 1;
169   memcpy(tempPath, filename.data(), filename.size());
170   memcpy(tempPath + filename.size(), suffix.data(), suffix.size());
171
172   auto tmpFD = mkstemp(tempPath);
173   if (tmpFD == -1) {
174     return errno;
175   }
176   bool success = false;
177   SCOPE_EXIT {
178     if (tmpFD != -1) {
179       close(tmpFD);
180     }
181     if (!success) {
182       unlink(tempPath);
183     }
184   };
185
186   auto rc = writevFull(tmpFD, iov, count);
187   if (rc == -1) {
188     return errno;
189   }
190
191   rc = fchmod(tmpFD, permissions);
192   if (rc == -1) {
193     return errno;
194   }
195
196   // Close the file before renaming to make sure all data has
197   // been successfully written.
198   rc = close(tmpFD);
199   tmpFD = -1;
200   if (rc == -1) {
201     return errno;
202   }
203
204   rc = rename(tempPath, filenameCStr);
205   if (rc == -1) {
206     return errno;
207   }
208   success = true;
209   return 0;
210 }
211
212 void writeFileAtomic(
213     StringPiece filename,
214     iovec* iov,
215     int count,
216     mode_t permissions) {
217   auto rc = writeFileAtomicNoThrow(filename, iov, count, permissions);
218   checkPosixError(rc, "writeFileAtomic() failed to update ", filename);
219 }
220
221 void writeFileAtomic(StringPiece filename, ByteRange data, mode_t permissions) {
222   iovec iov;
223   iov.iov_base = const_cast<unsigned char*>(data.data());
224   iov.iov_len = data.size();
225   auto rc = writeFileAtomicNoThrow(filename, &iov, 1, permissions);
226   checkPosixError(rc, "writeFileAtomic() failed to update ", filename);
227 }
228
229 void writeFileAtomic(
230     StringPiece filename,
231     StringPiece data,
232     mode_t permissions) {
233   writeFileAtomic(filename, ByteRange(data), permissions);
234 }
235
236 }  // namespaces