]> git.proxmox.com Git - ceph.git/blob - ceph/src/seastar/src/core/syscall_result.hh
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / seastar / src / core / syscall_result.hh
1 /*
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.
6 *
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
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
16 * under the License.
17 */
18 /*
19 * Copyright 2019 ScyllaDB
20 */
21
22 namespace seastar {
23
24 namespace fs = std::filesystem;
25
26 #pragma once
27 template <typename T>
28 struct syscall_result {
29 T result;
30 int error;
31 syscall_result(T result, int error) : result{std::move(result)}, error{error} {
32 }
33 void throw_if_error() const {
34 if (long(result) == -1) {
35 throw std::system_error(ec());
36 }
37 }
38
39 void throw_fs_exception(const sstring& reason, const fs::path& path) const {
40 throw fs::filesystem_error(reason, path, ec());
41 }
42
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());
45 }
46
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));
50 }
51 }
52
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));
56 }
57 }
58 protected:
59 std::error_code ec() const {
60 return std::error_code(error, std::system_category());
61 }
62 };
63
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> {
68 Extra extra;
69 syscall_result_extra(int result, int error, Extra e) : syscall_result<int>{result, error}, extra{std::move(e)} {
70 }
71 };
72
73 template <typename T>
74 syscall_result<T>
75 wrap_syscall(T result) {
76 return syscall_result<T>{std::move(result), errno};
77 }
78
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};
83 }
84
85 }