1 /* 2 * Copyright (C) 2019 The Android Open Source Project 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 #pragma once 18 19 #include <errno.h> 20 #include <stdint.h> 21 22 #include <string> 23 24 namespace android::fiemap { 25 26 // Represent error status of libfiemap classes. 27 class FiemapStatus { 28 public: 29 enum class ErrorCode : int32_t { 30 SUCCESS = 0, 31 // Generic non-recoverable failure. 32 ERROR = INT32_MIN, 33 // Not enough space 34 NO_SPACE = -ENOSPC, 35 }; 36 37 // Create from a given errno (specified in errno,h) FromErrno(int error_num)38 static FiemapStatus FromErrno(int error_num) { return FiemapStatus(CastErrorCode(-error_num)); } 39 40 // Create from an integer error code that is expected to be an ErrorCode 41 // value. If it isn't, Error() is returned. FromErrorCode(int32_t error_code)42 static FiemapStatus FromErrorCode(int32_t error_code) { 43 return FiemapStatus(CastErrorCode(error_code)); 44 } 45 46 // Generic error. Error()47 static FiemapStatus Error() { return FiemapStatus(ErrorCode::ERROR); } 48 49 // Success. Ok()50 static FiemapStatus Ok() { return FiemapStatus(ErrorCode::SUCCESS); } 51 error_code()52 ErrorCode error_code() const { return error_code_; } is_ok()53 bool is_ok() const { return error_code() == ErrorCode::SUCCESS; } 54 operator bool() const { return is_ok(); } 55 56 // For logging and debugging only. 57 std::string string() const; 58 59 protected: FiemapStatus(ErrorCode code)60 FiemapStatus(ErrorCode code) : error_code_(code) {} 61 62 private: 63 ErrorCode error_code_; 64 65 static ErrorCode CastErrorCode(int error); 66 }; 67 68 } // namespace android::fiemap 69