2 * Copyright 2016 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <folly/FileUtil.h>
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>
31 using namespace fileutil_detail;
33 int openNoInt(const char* name, int flags, mode_t mode) {
34 return int(wrapNoInt(open, name, flags, mode));
37 int closeNoInt(int 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).
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) {
54 int fsyncNoInt(int fd) {
55 return int(wrapNoInt(fsync, fd));
58 int dupNoInt(int fd) {
59 return int(wrapNoInt(dup, fd));
62 int dup2NoInt(int oldfd, int newfd) {
63 return int(wrapNoInt(dup2, oldfd, newfd));
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));
72 return int(wrapNoInt(fdatasync, fd));
76 int ftruncateNoInt(int fd, off_t len) {
77 return int(wrapNoInt(ftruncate, fd, len));
80 int truncateNoInt(const char* path, off_t len) {
81 return int(wrapNoInt(truncate, path, len));
84 int flockNoInt(int fd, int operation) {
85 return int(wrapNoInt(flock, fd, operation));
88 int shutdownNoInt(int fd, int how) {
89 return int(wrapNoInt(portability::sockets::shutdown, fd, how));
92 ssize_t readNoInt(int fd, void* buf, size_t count) {
93 return wrapNoInt(read, fd, buf, count);
96 ssize_t preadNoInt(int fd, void* buf, size_t count, off_t offset) {
97 return wrapNoInt(pread, fd, buf, count, offset);
100 ssize_t readvNoInt(int fd, const iovec* iov, int count) {
101 return wrapNoInt(readv, fd, iov, count);
104 ssize_t writeNoInt(int fd, const void* buf, size_t count) {
105 return wrapNoInt(write, fd, buf, count);
108 ssize_t pwriteNoInt(int fd, const void* buf, size_t count, off_t offset) {
109 return wrapNoInt(pwrite, fd, buf, count, offset);
112 ssize_t writevNoInt(int fd, const iovec* iov, int count) {
113 return wrapNoInt(writev, fd, iov, count);
116 ssize_t readFull(int fd, void* buf, size_t count) {
117 return wrapFull(read, fd, buf, count);
120 ssize_t preadFull(int fd, void* buf, size_t count, off_t offset) {
121 return wrapFull(pread, fd, buf, count, offset);
124 ssize_t writeFull(int fd, const void* buf, size_t count) {
125 return wrapFull(write, fd, const_cast<void*>(buf), count);
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);
132 ssize_t readvFull(int fd, iovec* iov, int count) {
133 return wrapvFull(readv, fd, iov, count);
136 ssize_t preadvFull(int fd, iovec* iov, int count, off_t offset) {
137 return wrapvFull(preadv, fd, iov, count, offset);
140 ssize_t writevFull(int fd, iovec* iov, int count) {
141 return wrapvFull(writev, fd, iov, count);
144 ssize_t pwritevFull(int fd, iovec* iov, int count, off_t offset) {
145 return wrapvFull(pwritev, fd, iov, count, offset);
148 int writeFileAtomicNoThrow(
149 StringPiece filename,
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.
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());
174 auto tmpFD = mkstemp(tempPath);
178 bool success = false;
188 auto rc = writevFull(tmpFD, iov, count);
193 rc = fchmod(tmpFD, permissions);
198 // Close the file before renaming to make sure all data has
199 // been successfully written.
206 rc = rename(tempPath, filenameCStr);
214 void writeFileAtomic(
215 StringPiece filename,
218 mode_t permissions) {
219 auto rc = writeFileAtomicNoThrow(filename, iov, count, permissions);
220 checkPosixError(rc, "writeFileAtomic() failed to update ", filename);
223 void writeFileAtomic(StringPiece filename, ByteRange data, mode_t permissions) {
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);
231 void writeFileAtomic(
232 StringPiece filename,
234 mode_t permissions) {
235 writeFileAtomic(filename, ByteRange(data), permissions);