blob: b4a1ffc2f57e656928fe9a5be0a60473aab8422e [file] [log] [blame]
Patrick Venture8b588562018-11-18 08:44:33 -08001/*
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>
Patrick Venture7b91cbc2018-11-28 14:24:41 -080020#include <sys/ioctl.h>
Patrick Venture8b588562018-11-18 08:44:33 -080021#include <sys/mman.h>
Benjamin Fairf7ccadb2019-10-11 17:55:27 -070022#include <sys/sendfile.h>
Patrick Venturecf9b2192019-06-27 12:09:52 -070023#include <sys/stat.h>
24#include <sys/types.h>
Patrick Venture8b588562018-11-18 08:44:33 -080025#include <unistd.h>
26
Patrick Venturecf9b2192019-06-27 12:09:52 -070027#include <cstdint>
28
Patrick Venture8b588562018-11-18 08:44:33 -080029namespace internal
30{
31
Patrick Venturecf9b2192019-06-27 12:09:52 -070032std::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
Patrick Venture8b588562018-11-18 08:44:33 -080044int SysImpl::open(const char* pathname, int flags) const
45{
46 return ::open(pathname, flags);
47}
48
Patrick Venturecec04902019-01-15 13:04:44 -080049int SysImpl::read(int fd, void* buf, std::size_t count) const
50{
51 return static_cast<int>(::read(fd, buf, count));
52}
53
Brandon Kim493b3af2019-11-05 16:28:40 -080054int 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
59int 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
Patrick Venture8b588562018-11-18 08:44:33 -080065int SysImpl::close(int fd) const
66{
67 return ::close(fd);
68}
69
Patrick Venture28abae72018-12-14 09:44:02 -080070void* SysImpl::mmap(void* addr, std::size_t length, int prot, int flags, int fd,
Patrick Venture8b588562018-11-18 08:44:33 -080071 off_t offset) const
72{
73 return ::mmap(addr, length, prot, flags, fd, offset);
74}
75
Patrick Venture28abae72018-12-14 09:44:02 -080076int SysImpl::munmap(void* addr, std::size_t length) const
Patrick Venture8b588562018-11-18 08:44:33 -080077{
78 return ::munmap(addr, length);
79}
80
81int SysImpl::getpagesize() const
82{
83 return ::getpagesize();
84}
85
Patrick Venture7b91cbc2018-11-28 14:24:41 -080086int SysImpl::ioctl(int fd, unsigned long request, void* param) const
87{
88 return ::ioctl(fd, request, param);
89}
90
Patrick Venture7b78aa22018-12-14 13:56:15 -080091int SysImpl::poll(struct pollfd* fds, nfds_t nfds, int timeout) const
92{
93 return ::poll(fds, nfds, timeout);
94}
95
Benjamin Fairf7ccadb2019-10-11 17:55:27 -070096int SysImpl::socket(int domain, int type, int protocol) const
97{
98 return ::socket(domain, type, protocol);
99}
100
101int SysImpl::connect(int sockfd, const struct sockaddr* addr,
102 socklen_t addrlen) const
103{
104 return ::connect(sockfd, addr, addrlen);
105}
106
107ssize_t SysImpl::sendfile(int out_fd, int in_fd, off_t* offset,
108 size_t count) const
109{
110 return ::sendfile(out_fd, in_fd, offset, count);
111}
112
113int SysImpl::getaddrinfo(const char* node, const char* service,
114 const struct addrinfo* hints,
115 struct addrinfo** res) const
116{
117 return ::getaddrinfo(node, service, hints, res);
118}
119
120void SysImpl::freeaddrinfo(struct addrinfo* res) const
121{
122 ::freeaddrinfo(res);
123}
124
Patrick Venture8b588562018-11-18 08:44:33 -0800125SysImpl sys_impl;
126
127} // namespace internal