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 #ifndef FOLLY_DETAIL_FILEUTILDETAIL_H_
18 #define FOLLY_DETAIL_FILEUTILDETAIL_H_
26 * Helper functions and templates for FileUtil.cpp. Declared here so
27 * they can be unittested.
29 namespace folly { namespace fileutil_detail {
31 // Wrap call to f(args) in loop to retry on EINTR
32 template<class F, class... Args>
33 ssize_t wrapNoInt(F f, Args... args) {
37 } while (r == -1 && errno == EINTR);
41 inline void incr(ssize_t /* n */) {}
42 inline void incr(ssize_t n, off_t& offset) { offset += n; }
44 // Wrap call to read/pread/write/pwrite(fd, buf, count, offset?) to retry on
45 // incomplete reads / writes. The variadic argument magic is there to support
46 // an additional argument (offset) for pread / pwrite; see the incr() functions
47 // above which do nothing if the offset is not present and increment it if it
49 template <class F, class... Offset>
50 ssize_t wrapFull(F f, int fd, void* buf, size_t count, Offset... offset) {
51 char* b = static_cast<char*>(buf);
52 ssize_t totalBytes = 0;
55 r = f(fd, b, count, offset...);
67 } while (r != 0 && count); // 0 means EOF
72 // Wrap call to readv/preadv/writev/pwritev(fd, iov, count, offset?) to
73 // retry on incomplete reads / writes.
74 template <class F, class... Offset>
75 ssize_t wrapvFull(F f, int fd, iovec* iov, int count, Offset... offset) {
76 ssize_t totalBytes = 0;
79 r = f(fd, iov, count, offset...);
80 if (r == (size_t)-1) {
93 while (r != 0 && count != 0) {
94 if (r >= iov->iov_len) {
99 iov->iov_base = static_cast<char*>(iov->iov_base) + r;
111 #endif /* FOLLY_DETAIL_FILEUTILDETAIL_H_ */