Add missing FileUtil functions (p?readvFull, p?writevFull) , add test
[folly.git] / folly / FileUtil.cpp
1 /*
2  * Copyright 2013 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/FileUtil.h"
18
19 #include <cerrno>
20
21 #include "folly/detail/FileUtilDetail.h"
22
23 namespace folly {
24
25 using namespace fileutil_detail;
26
27 int closeNoInt(int fd) {
28   int r = close(fd);
29   // Ignore EINTR.  On Linux, close() may only return EINTR after the file
30   // descriptor has been closed, so you must not retry close() on EINTR --
31   // in the best case, you'll get EBADF, and in the worst case, you'll end up
32   // closing a different file (one opened from another thread).
33   //
34   // Interestingly enough, the Single Unix Specification says that the state
35   // of the file descriptor is unspecified if close returns EINTR.  In that
36   // case, the safe thing to do is also not to retry close() -- leaking a file
37   // descriptor is probably better than closing the wrong file.
38   if (r == -1 && errno == EINTR) {
39     r = 0;
40   }
41   return r;
42 }
43
44 ssize_t readNoInt(int fd, void* buf, size_t count) {
45   return wrapNoInt(read, fd, buf, count);
46 }
47
48 ssize_t preadNoInt(int fd, void* buf, size_t count, off_t offset) {
49   return wrapNoInt(pread, fd, buf, count, offset);
50 }
51
52 ssize_t readvNoInt(int fd, const iovec* iov, int count) {
53   return wrapNoInt(writev, fd, iov, count);
54 }
55
56 ssize_t writeNoInt(int fd, const void* buf, size_t count) {
57   return wrapNoInt(write, fd, buf, count);
58 }
59
60 ssize_t pwriteNoInt(int fd, const void* buf, size_t count, off_t offset) {
61   return wrapNoInt(pwrite, fd, buf, count, offset);
62 }
63
64 ssize_t writevNoInt(int fd, const iovec* iov, int count) {
65   return wrapNoInt(writev, fd, iov, count);
66 }
67
68 ssize_t readFull(int fd, void* buf, size_t count) {
69   return wrapFull(read, fd, buf, count);
70 }
71
72 ssize_t preadFull(int fd, void* buf, size_t count, off_t offset) {
73   return wrapFull(pread, fd, buf, count, offset);
74 }
75
76 ssize_t writeFull(int fd, const void* buf, size_t count) {
77   return wrapFull(write, fd, const_cast<void*>(buf), count);
78 }
79
80 ssize_t pwriteFull(int fd, const void* buf, size_t count, off_t offset) {
81   return wrapFull(pwrite, fd, const_cast<void*>(buf), count, offset);
82 }
83
84 ssize_t readvFull(int fd, iovec* iov, int count) {
85   return wrapvFull(readv, fd, iov, count);
86 }
87
88 ssize_t preadvFull(int fd, iovec* iov, int count, off_t offset) {
89   return wrapvFull(preadv, fd, iov, count, offset);
90 }
91
92 ssize_t writevFull(int fd, iovec* iov, int count) {
93   return wrapvFull(writev, fd, iov, count);
94 }
95
96 ssize_t pwritevFull(int fd, iovec* iov, int count, off_t offset) {
97   return wrapvFull(pwritev, fd, iov, count, offset);
98 }
99
100 }  // namespaces
101