1 /*
2    Copyright 2017 Skytechnology sp. z o.o..
3 
4    This file is part of LizardFS.
5 
6    LizardFS is free software: you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation, version 3.
9 
10    LizardFS is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14 
15    You should have received a copy of the GNU General Public License
16    along with LizardFS  If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #include <string>
20 #include "common/platform.h"
21 #include "common/mfserr.h"
22 
23 #include "client_error_code.h"
24 
25 lizardfs::detail::lizardfs_error_category lizardfs::detail::lizardfs_error_category::instance_;
26 
message(int ev) const27 std::string lizardfs::detail::lizardfs_error_category::message(int ev) const {
28 	return lizardfs_error_string(ev);
29 }
30 
equivalent(int code,const std::error_condition & condition) const31 bool lizardfs::detail::lizardfs_error_category::equivalent(
32 		int code, const std::error_condition &condition) const noexcept {
33 	if (default_error_condition(code) == condition) {
34 		return true;
35 	}
36 
37 	switch (code) {
38 	case (int)lizardfs::error::operation_not_permitted:
39 		return std::make_error_code(std::errc::operation_not_permitted) == condition;
40 	case (int)lizardfs::error::not_a_directory:
41 		return std::make_error_code(std::errc::not_a_directory) == condition;
42 	case (int)lizardfs::error::no_such_file_or_directory:
43 		return std::make_error_code(std::errc::no_such_file_or_directory) == condition;
44 	case (int)lizardfs::error::permission_denied:
45 		return std::make_error_code(std::errc::permission_denied) == condition;
46 	case (int)lizardfs::error::file_exists:
47 		return std::make_error_code(std::errc::file_exists) == condition;
48 	case (int)lizardfs::error::invalid_argument:
49 		return std::make_error_code(std::errc::invalid_argument) == condition;
50 	case (int)lizardfs::error::directory_not_empty:
51 		return std::make_error_code(std::errc::directory_not_empty) == condition;
52 	case (int)lizardfs::error::no_space_left:
53 		return std::make_error_code(std::errc::no_space_on_device) == condition;
54 	case (int)lizardfs::error::io_error:
55 		return std::make_error_code(std::errc::io_error) == condition;
56 	case (int)lizardfs::error::read_only_file_system:
57 		return std::make_error_code(std::errc::read_only_file_system) == condition;
58 	case (int)lizardfs::error::attribute_not_found:
59 #if defined(__APPLE__) || defined(__FreeBSD__)
60 		return std::make_error_code(std::errc::no_message) == condition;
61 #else
62 		return std::make_error_code(std::errc::no_message_available) == condition;
63 #endif
64 	case (int)lizardfs::error::not_supported:
65 		return std::make_error_code(std::errc::not_supported) == condition;
66 	case (int)lizardfs::error::result_out_of_range:
67 		return std::make_error_code(std::errc::result_out_of_range) == condition;
68 	case (int)lizardfs::error::no_lock_available:
69 		return std::make_error_code(std::errc::no_lock_available) == condition;
70 	case (int)lizardfs::error::filename_too_long:
71 		return std::make_error_code(std::errc::filename_too_long) == condition;
72 	case (int)lizardfs::error::file_too_large:
73 		return std::make_error_code(std::errc::file_too_large) == condition;
74 	case (int)lizardfs::error::bad_file_descriptor:
75 		return std::make_error_code(std::errc::bad_file_descriptor) == condition;
76 #if defined(__APPLE__) || defined(__FreeBSD__)
77 	case (int)lizardfs::error::no_message:
78 		return std::make_error_code(std::errc::no_message) == condition;
79 #else
80 	case (int)lizardfs::error::no_message_available:
81 		return std::make_error_code(std::errc::no_message_available) == condition;
82 #endif
83 	case (int)lizardfs::error::not_enough_memory:
84 		return std::make_error_code(std::errc::not_enough_memory) == condition;
85 	case (int)lizardfs::error::argument_list_too_long:
86 		return std::make_error_code(std::errc::argument_list_too_long) == condition;
87 	}
88 
89 	return false;
90 }
91 
equivalent(const std::error_code & code,int condition) const92 bool lizardfs::detail::lizardfs_error_category::equivalent(const std::error_code &code,
93 		int condition) const noexcept {
94 	if (code.category() == *this && code.value() == condition) {
95 		return true;
96 	}
97 
98 	switch (condition) {
99 	case (int)lizardfs::error::operation_not_permitted:
100 		return code == std::make_error_condition(std::errc::operation_not_permitted);
101 	case (int)lizardfs::error::not_a_directory:
102 		return code == std::make_error_condition(std::errc::not_a_directory);
103 	case (int)lizardfs::error::no_such_file_or_directory:
104 		return code == std::make_error_condition(std::errc::no_such_file_or_directory);
105 	case (int)lizardfs::error::permission_denied:
106 		return code == std::make_error_condition(std::errc::permission_denied);
107 	case (int)lizardfs::error::file_exists:
108 		return code == std::make_error_condition(std::errc::file_exists);
109 	case (int)lizardfs::error::invalid_argument:
110 		return code == std::make_error_condition(std::errc::invalid_argument);
111 	case (int)lizardfs::error::directory_not_empty:
112 		return code == std::make_error_condition(std::errc::directory_not_empty);
113 	case (int)lizardfs::error::no_space_left:
114 		return code == std::make_error_condition(std::errc::no_space_on_device);
115 	case (int)lizardfs::error::io_error:
116 		return code == std::make_error_condition(std::errc::io_error);
117 	case (int)lizardfs::error::read_only_file_system:
118 		return code == std::make_error_condition(std::errc::read_only_file_system);
119 	case (int)lizardfs::error::attribute_not_found:
120 #if defined(__APPLE__) || defined(__FreeBSD__)
121 		return code == std::make_error_condition(std::errc::no_message);
122 #else
123 		return code == std::make_error_condition(std::errc::no_message_available);
124 #endif
125 	case (int)lizardfs::error::not_supported:
126 		return code == std::make_error_condition(std::errc::not_supported);
127 	case (int)lizardfs::error::result_out_of_range:
128 		return code == std::make_error_condition(std::errc::result_out_of_range);
129 	case (int)lizardfs::error::no_lock_available:
130 		return code == std::make_error_condition(std::errc::no_lock_available);
131 	case (int)lizardfs::error::filename_too_long:
132 		return code == std::make_error_condition(std::errc::filename_too_long);
133 	case (int)lizardfs::error::file_too_large:
134 		return code == std::make_error_condition(std::errc::file_too_large);
135 	case (int)lizardfs::error::bad_file_descriptor:
136 		return code == std::make_error_condition(std::errc::bad_file_descriptor);
137 #if defined(__APPLE__) || defined(__FreeBSD__)
138 	case (int)lizardfs::error::no_message:
139 		return code == std::make_error_condition(std::errc::no_message);
140 #else
141 	case (int)lizardfs::error::no_message_available:
142 		return code == std::make_error_condition(std::errc::no_message_available);
143 #endif
144 	case (int)lizardfs::error::not_enough_memory:
145 		return code == std::make_error_condition(std::errc::not_enough_memory);
146 	case (int)lizardfs::error::argument_list_too_long:
147 		return code == std::make_error_condition(std::errc::argument_list_too_long);
148 	}
149 
150 	return false;
151 }
152