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.
21 #include <folly/portability/SysUio.h>
22 #include <folly/portability/Unistd.h>
25 * Helper functions and templates for FileUtil.cpp. Declared here so
26 * they can be unittested.
28 namespace folly { namespace fileutil_detail {
30 // Wrap call to f(args) in loop to retry on EINTR
31 template<class F, class... Args>
32 ssize_t wrapNoInt(F f, Args... args) {
36 } while (r == -1 && errno == EINTR);
40 inline void incr(ssize_t /* n */) {}
41 inline void incr(ssize_t n, off_t& offset) { offset += n; }
43 // Wrap call to read/pread/write/pwrite(fd, buf, count, offset?) to retry on
44 // incomplete reads / writes. The variadic argument magic is there to support
45 // an additional argument (offset) for pread / pwrite; see the incr() functions
46 // above which do nothing if the offset is not present and increment it if it
48 template <class F, class... Offset>
49 ssize_t wrapFull(F f, int fd, void* buf, size_t count, Offset... offset) {
50 char* b = static_cast<char*>(buf);
51 ssize_t totalBytes = 0;
54 r = f(fd, b, count, offset...);
66 } while (r != 0 && count); // 0 means EOF
71 // Wrap call to readv/preadv/writev/pwritev(fd, iov, count, offset?) to
72 // retry on incomplete reads / writes.
73 template <class F, class... Offset>
74 ssize_t wrapvFull(F f, int fd, iovec* iov, int count, Offset... offset) {
75 ssize_t totalBytes = 0;
78 r = f(fd, iov, std::min<int>(count, kIovMax), offset...);
79 if (r == (size_t)-1) {
92 while (r != 0 && count != 0) {
93 if (r >= iov->iov_len) {
98 iov->iov_base = static_cast<char*>(iov->iov_base) + r;