2 * This file is open source software, licensed to you under the terms
3 * of the Apache License, Version 2.0 (the "License"). See the NOTICE file
4 * distributed with this work for additional information regarding copyright
5 * ownership. You may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing,
12 * software distributed under the License is distributed on an
13 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 * KIND, either express or implied. See the License for the
15 * specific language governing permissions and limitations
19 * Copyright 2019 ScyllaDB
24 namespace fs = std::filesystem;
28 struct syscall_result {
31 syscall_result(T result, int error) : result{std::move(result)}, error{error} {
33 void throw_if_error() const {
34 if (long(result) == -1) {
35 throw std::system_error(ec());
39 void throw_fs_exception(const sstring& reason, const fs::path& path) const {
40 throw fs::filesystem_error(reason, path, ec());
43 void throw_fs_exception(const sstring& reason, const fs::path& path1, const fs::path& path2) const {
44 throw fs::filesystem_error(reason, path1, path2, ec());
47 void throw_fs_exception_if_error(const sstring& reason, const sstring& path) const {
48 if (long(result) == -1) {
49 throw_fs_exception(reason, fs::path(path));
53 void throw_fs_exception_if_error(const sstring& reason, const sstring& path1, const sstring& path2) const {
54 if (long(result) == -1) {
55 throw_fs_exception(reason, fs::path(path1), fs::path(path2));
59 std::error_code ec() const {
60 return std::error_code(error, std::system_category());
64 // Wrapper for a system call result containing the return value,
65 // an output parameter that was returned from the syscall, and errno.
66 template <typename Extra>
67 struct syscall_result_extra : public syscall_result<int> {
69 syscall_result_extra(int result, int error, Extra e) : syscall_result<int>{result, error}, extra{std::move(e)} {
75 wrap_syscall(T result) {
76 return syscall_result<T>{std::move(result), errno};
79 template <typename Extra>
80 syscall_result_extra<Extra>
81 wrap_syscall(int result, const Extra& extra) {
82 return syscall_result_extra<Extra>{result, errno, extra};