Patrick Venture | 8b58856 | 2018-11-18 08:44:33 -0800 | [diff] [blame] | 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> |
Patrick Venture | 7b91cbc | 2018-11-28 14:24:41 -0800 | [diff] [blame] | 20 | #include <sys/ioctl.h> |
Patrick Venture | 8b58856 | 2018-11-18 08:44:33 -0800 | [diff] [blame] | 21 | #include <sys/mman.h> |
Benjamin Fair | f7ccadb | 2019-10-11 17:55:27 -0700 | [diff] [blame^] | 22 | #include <sys/sendfile.h> |
Patrick Venture | cf9b219 | 2019-06-27 12:09:52 -0700 | [diff] [blame] | 23 | #include <sys/stat.h> |
| 24 | #include <sys/types.h> |
Patrick Venture | 8b58856 | 2018-11-18 08:44:33 -0800 | [diff] [blame] | 25 | #include <unistd.h> |
| 26 | |
Patrick Venture | cf9b219 | 2019-06-27 12:09:52 -0700 | [diff] [blame] | 27 | #include <cstdint> |
| 28 | |
Patrick Venture | 8b58856 | 2018-11-18 08:44:33 -0800 | [diff] [blame] | 29 | namespace internal |
| 30 | { |
| 31 | |
Patrick Venture | cf9b219 | 2019-06-27 12:09:52 -0700 | [diff] [blame] | 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 | |
Patrick Venture | 8b58856 | 2018-11-18 08:44:33 -0800 | [diff] [blame] | 44 | int SysImpl::open(const char* pathname, int flags) const |
| 45 | { |
| 46 | return ::open(pathname, flags); |
| 47 | } |
| 48 | |
Patrick Venture | cec0490 | 2019-01-15 13:04:44 -0800 | [diff] [blame] | 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 | |
Brandon Kim | 493b3af | 2019-11-05 16:28:40 -0800 | [diff] [blame] | 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 | |
Patrick Venture | 8b58856 | 2018-11-18 08:44:33 -0800 | [diff] [blame] | 65 | int SysImpl::close(int fd) const |
| 66 | { |
| 67 | return ::close(fd); |
| 68 | } |
| 69 | |
Patrick Venture | 28abae7 | 2018-12-14 09:44:02 -0800 | [diff] [blame] | 70 | void* SysImpl::mmap(void* addr, std::size_t length, int prot, int flags, int fd, |
Patrick Venture | 8b58856 | 2018-11-18 08:44:33 -0800 | [diff] [blame] | 71 | off_t offset) const |
| 72 | { |
| 73 | return ::mmap(addr, length, prot, flags, fd, offset); |
| 74 | } |
| 75 | |
Patrick Venture | 28abae7 | 2018-12-14 09:44:02 -0800 | [diff] [blame] | 76 | int SysImpl::munmap(void* addr, std::size_t length) const |
Patrick Venture | 8b58856 | 2018-11-18 08:44:33 -0800 | [diff] [blame] | 77 | { |
| 78 | return ::munmap(addr, length); |
| 79 | } |
| 80 | |
| 81 | int SysImpl::getpagesize() const |
| 82 | { |
| 83 | return ::getpagesize(); |
| 84 | } |
| 85 | |
Patrick Venture | 7b91cbc | 2018-11-28 14:24:41 -0800 | [diff] [blame] | 86 | int SysImpl::ioctl(int fd, unsigned long request, void* param) const |
| 87 | { |
| 88 | return ::ioctl(fd, request, param); |
| 89 | } |
| 90 | |
Patrick Venture | 7b78aa2 | 2018-12-14 13:56:15 -0800 | [diff] [blame] | 91 | int SysImpl::poll(struct pollfd* fds, nfds_t nfds, int timeout) const |
| 92 | { |
| 93 | return ::poll(fds, nfds, timeout); |
| 94 | } |
| 95 | |
Benjamin Fair | f7ccadb | 2019-10-11 17:55:27 -0700 | [diff] [blame^] | 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::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 | |
| 113 | int 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 | |
| 120 | void SysImpl::freeaddrinfo(struct addrinfo* res) const |
| 121 | { |
| 122 | ::freeaddrinfo(res); |
| 123 | } |
| 124 | |
Patrick Venture | 8b58856 | 2018-11-18 08:44:33 -0800 | [diff] [blame] | 125 | SysImpl sys_impl; |
| 126 | |
| 127 | } // namespace internal |