folly copyright 2015 -> copyright 2016
[folly.git] / folly / detail / FileUtilDetail.h
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 #ifndef FOLLY_DETAIL_FILEUTILDETAIL_H_
18 #define FOLLY_DETAIL_FILEUTILDETAIL_H_
19
20 #include <cerrno>
21 #include <unistd.h>
22
23 #include <sys/uio.h>
24
25 /**
26  * Helper functions and templates for FileUtil.cpp.  Declared here so
27  * they can be unittested.
28  */
29 namespace folly { namespace fileutil_detail {
30
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) {
34   ssize_t r;
35   do {
36     r = f(args...);
37   } while (r == -1 && errno == EINTR);
38   return r;
39 }
40
41 inline void incr(ssize_t /* n */) {}
42 inline void incr(ssize_t n, off_t& offset) { offset += n; }
43
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
48 // is.
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;
53   ssize_t r;
54   do {
55     r = f(fd, b, count, offset...);
56     if (r == -1) {
57       if (errno == EINTR) {
58         continue;
59       }
60       return r;
61     }
62
63     totalBytes += r;
64     b += r;
65     count -= r;
66     incr(r, offset...);
67   } while (r != 0 && count);  // 0 means EOF
68
69   return totalBytes;
70 }
71
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;
77   size_t r;
78   do {
79     r = f(fd, iov, count, offset...);
80     if (r == (size_t)-1) {
81       if (errno == EINTR) {
82         continue;
83       }
84       return r;
85     }
86
87     if (r == 0) {
88       break;  // EOF
89     }
90
91     totalBytes += r;
92     incr(r, offset...);
93     while (r != 0 && count != 0) {
94       if (r >= iov->iov_len) {
95         r -= iov->iov_len;
96         ++iov;
97         --count;
98       } else {
99         iov->iov_base = static_cast<char*>(iov->iov_base) + r;
100         iov->iov_len -= r;
101         r = 0;
102       }
103     }
104   } while (count);
105
106   return totalBytes;
107 }
108
109 }}  // namespaces
110
111 #endif /* FOLLY_DETAIL_FILEUTILDETAIL_H_ */