2017
[folly.git] / folly / portability / SysUio.cpp
1 /*
2  * Copyright 2017 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 #include <folly/portability/SysUio.h>
18
19 #include <errno.h>
20 #include <stdio.h>
21
22 #include <folly/ScopeGuard.h>
23 #include <folly/portability/SysFile.h>
24 #include <folly/portability/Unistd.h>
25
26 template <class F, class... Args>
27 static int wrapPositional(F f, int fd, off_t offset, Args... args) {
28   off_t origLoc = lseek(fd, 0, SEEK_CUR);
29   if (origLoc == off_t(-1)) {
30     return -1;
31   }
32   if (lseek(fd, offset, SEEK_SET) == off_t(-1)) {
33     return -1;
34   }
35
36   int res = (int)f(fd, args...);
37
38   int curErrNo = errno;
39   if (lseek(fd, origLoc, SEEK_SET) == off_t(-1)) {
40     if (res == -1) {
41       errno = curErrNo;
42     }
43     return -1;
44   }
45   errno = curErrNo;
46
47   return res;
48 }
49
50 #if !FOLLY_HAVE_PREADV
51 extern "C" ssize_t preadv(int fd, const iovec* iov, int count, off_t offset) {
52   return wrapPositional(readv, fd, offset, iov, count);
53 }
54 #endif
55
56 #if !FOLLY_HAVE_PWRITEV
57 extern "C" ssize_t pwritev(int fd, const iovec* iov, int count, off_t offset) {
58   return wrapPositional(writev, fd, offset, iov, count);
59 }
60 #endif
61
62 #ifdef _WIN32
63 template <bool isRead>
64 static ssize_t doVecOperation(int fd, const iovec* iov, int count) {
65   if (!count) {
66     return 0;
67   }
68   if (count < 0 || count > folly::kIovMax) {
69     errno = EINVAL;
70     return -1;
71   }
72
73   if (lockf(fd, F_LOCK, 0) == -1) {
74     return -1;
75   }
76   SCOPE_EXIT { lockf(fd, F_ULOCK, 0); };
77
78   ssize_t bytesProcessed = 0;
79   int curIov = 0;
80   void* curBase = iov[0].iov_base;
81   size_t curLen = iov[0].iov_len;
82   while (curIov < count) {
83     ssize_t res = 0;
84     if (isRead) {
85       res = read(fd, curBase, (unsigned int)curLen);
86       if (res == 0 && curLen != 0) {
87         break; // End of File
88       }
89     } else {
90       res = write(fd, curBase, (unsigned int)curLen);
91       // Write of zero bytes is fine.
92     }
93
94     if (res == -1) {
95       return -1;
96     }
97
98     if (size_t(res) == curLen) {
99       curIov++;
100       if (curIov < count) {
101         curBase = iov[curIov].iov_base;
102         curLen = iov[curIov].iov_len;
103       }
104     } else {
105       curBase = (void*)((char*)curBase + res);
106       curLen -= res;
107     }
108
109     if (bytesProcessed + res < 0) {
110       // Overflow
111       errno = EINVAL;
112       return -1;
113     }
114     bytesProcessed += res;
115   }
116
117   return bytesProcessed;
118 }
119
120 extern "C" ssize_t readv(int fd, const iovec* iov, int count) {
121   return doVecOperation<true>(fd, iov, count);
122 }
123
124 extern "C" ssize_t writev(int fd, const iovec* iov, int count) {
125   return doVecOperation<false>(fd, iov, count);
126 }
127 #endif