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/SysFile.h>
29 using namespace fileutil_detail;
31 int openNoInt(const char* name, int flags, mode_t mode) {
32 return int(wrapNoInt(open, name, flags, mode));
35 int closeNoInt(int 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).
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) {
52 int fsyncNoInt(int fd) {
53 return int(wrapNoInt(fsync, fd));
56 int dupNoInt(int fd) {
57 return int(wrapNoInt(dup, fd));
60 int dup2NoInt(int oldfd, int newfd) {
61 return int(wrapNoInt(dup2, oldfd, newfd));
64 int fdatasyncNoInt(int fd) {
65 #if defined(__APPLE__)
66 return int(wrapNoInt(fcntl, fd, F_FULLFSYNC));
67 #elif defined(__FreeBSD__) || defined(_MSC_VER)
68 return int(wrapNoInt(fsync, fd));
70 return int(wrapNoInt(fdatasync, fd));
74 int ftruncateNoInt(int fd, off_t len) {
75 return int(wrapNoInt(ftruncate, fd, len));
78 int truncateNoInt(const char* path, off_t len) {
79 return int(wrapNoInt(truncate, path, len));
82 int flockNoInt(int fd, int operation) {
83 return int(wrapNoInt(flock, fd, operation));
86 int shutdownNoInt(int fd, int how) {
87 return int(wrapNoInt(portability::sockets::shutdown, fd, how));
90 ssize_t readNoInt(int fd, void* buf, size_t count) {
91 return wrapNoInt(read, fd, buf, count);
94 ssize_t preadNoInt(int fd, void* buf, size_t count, off_t offset) {
95 return wrapNoInt(pread, fd, buf, count, offset);
98 ssize_t readvNoInt(int fd, const iovec* iov, int count) {
99 return wrapNoInt(readv, fd, iov, count);
102 ssize_t writeNoInt(int fd, const void* buf, size_t count) {
103 return wrapNoInt(write, fd, buf, count);
106 ssize_t pwriteNoInt(int fd, const void* buf, size_t count, off_t offset) {
107 return wrapNoInt(pwrite, fd, buf, count, offset);
110 ssize_t writevNoInt(int fd, const iovec* iov, int count) {
111 return wrapNoInt(writev, fd, iov, count);
114 ssize_t readFull(int fd, void* buf, size_t count) {
115 return wrapFull(read, fd, buf, count);
118 ssize_t preadFull(int fd, void* buf, size_t count, off_t offset) {
119 return wrapFull(pread, fd, buf, count, offset);
122 ssize_t writeFull(int fd, const void* buf, size_t count) {
123 return wrapFull(write, fd, const_cast<void*>(buf), count);
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);
130 ssize_t readvFull(int fd, iovec* iov, int count) {
131 return wrapvFull(readv, fd, iov, count);
134 ssize_t preadvFull(int fd, iovec* iov, int count, off_t offset) {
135 return wrapvFull(preadv, fd, iov, count, offset);
138 ssize_t writevFull(int fd, iovec* iov, int count) {
139 return wrapvFull(writev, fd, iov, count);
142 ssize_t pwritevFull(int fd, iovec* iov, int count, off_t offset) {
143 return wrapvFull(pwritev, fd, iov, count, offset);
146 int writeFileAtomicNoThrow(
147 StringPiece filename,
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.
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());
172 auto tmpFD = mkstemp(tempPath);
176 bool success = false;
186 auto rc = writevFull(tmpFD, iov, count);
191 rc = fchmod(tmpFD, permissions);
196 // Close the file before renaming to make sure all data has
197 // been successfully written.
204 rc = rename(tempPath, filenameCStr);
212 void writeFileAtomic(
213 StringPiece filename,
216 mode_t permissions) {
217 auto rc = writeFileAtomicNoThrow(filename, iov, count, permissions);
218 checkPosixError(rc, "writeFileAtomic() failed to update ", filename);
221 void writeFileAtomic(StringPiece filename, ByteRange data, mode_t permissions) {
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);
229 void writeFileAtomic(
230 StringPiece filename,
232 mode_t permissions) {
233 writeFileAtomic(filename, ByteRange(data), permissions);