1 /*
2  * Copyright 2018 Google 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 "sys.hpp"
18 
19 #include <fcntl.h>
20 #include <sys/ioctl.h>
21 #include <sys/mman.h>
22 #include <sys/sendfile.h>
23 #include <sys/stat.h>
24 #include <sys/types.h>
25 #include <unistd.h>
26 
27 #include <cstdint>
28 
29 namespace internal
30 {
31 
32 std::int64_t SysImpl::getSize(const char* pathname) const
33 {
34     struct stat results;
35     int rc = ::stat(pathname, &results);
36     if (rc)
37     {
38         return 0;
39     }
40 
41     return static_cast<std::int64_t>(results.st_size);
42 }
43 
44 int SysImpl::open(const char* pathname, int flags) const
45 {
46     return ::open(pathname, flags);
47 }
48 
49 int SysImpl::read(int fd, void* buf, std::size_t count) const
50 {
51     return static_cast<int>(::read(fd, buf, count));
52 }
53 
54 int SysImpl::pread(int fd, void* buf, std::size_t count, off_t offset) const
55 {
56     return static_cast<int>(::pread(fd, buf, count, offset));
57 }
58 
59 int SysImpl::pwrite(int fd, const void* buf, std::size_t count,
60                     off_t offset) const
61 {
62     return static_cast<int>(::pwrite(fd, buf, count, offset));
63 }
64 
65 int SysImpl::close(int fd) const
66 {
67     return ::close(fd);
68 }
69 
70 void* SysImpl::mmap(void* addr, std::size_t length, int prot, int flags, int fd,
71                     off_t offset) const
72 {
73     return ::mmap(addr, length, prot, flags, fd, offset);
74 }
75 
76 int SysImpl::munmap(void* addr, std::size_t length) const
77 {
78     return ::munmap(addr, length);
79 }
80 
81 int SysImpl::getpagesize() const
82 {
83     return ::getpagesize();
84 }
85 
86 int SysImpl::ioctl(int fd, unsigned long request, void* param) const
87 {
88     return ::ioctl(fd, request, param);
89 }
90 
91 int SysImpl::poll(struct pollfd* fds, nfds_t nfds, int timeout) const
92 {
93     return ::poll(fds, nfds, timeout);
94 }
95 
96 int SysImpl::socket(int domain, int type, int protocol) const
97 {
98     return ::socket(domain, type, protocol);
99 }
100 
101 int SysImpl::connect(int sockfd, const struct sockaddr* addr,
102                      socklen_t addrlen) const
103 {
104     return ::connect(sockfd, addr, addrlen);
105 }
106 
107 ssize_t SysImpl::send(int sockfd, const void* buf, size_t len, int flags) const
108 {
109     return ::send(sockfd, buf, len, flags);
110 }
111 
112 ssize_t SysImpl::sendfile(int out_fd, int in_fd, off_t* offset,
113                           size_t count) const
114 {
115     return ::sendfile(out_fd, in_fd, offset, count);
116 }
117 
118 int SysImpl::getaddrinfo(const char* node, const char* service,
119                          const struct addrinfo* hints,
120                          struct addrinfo** res) const
121 {
122     return ::getaddrinfo(node, service, hints, res);
123 }
124 
125 void SysImpl::freeaddrinfo(struct addrinfo* res) const
126 {
127     ::freeaddrinfo(res);
128 }
129 
130 SysImpl sys_impl;
131 
132 } // namespace internal
133