kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/cxx/common/libzip/error.h (about) 1 /* 2 * Copyright 2018 The Kythe Authors. All rights reserved. 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 #ifndef KYTHE_CXX_COMMON_LIBZIP_ERROR_H_ 18 #define KYTHE_CXX_COMMON_LIBZIP_ERROR_H_ 19 20 #include <zip.h> 21 22 #include "absl/status/status.h" 23 24 namespace kythe { 25 namespace libzip { 26 27 /// \brief RAII wrapper around zip_error_t. 28 class Error { 29 public: 30 /// \brief Constructs an Error instance from the libzip error code 31 /// and errno, if necessary. 32 explicit Error(int code) { zip_error_init_with_code(get(), code); } 33 /// \brief Pseudo-copy constructor. Constructs an Error instance copying 34 /// the relevant portions of zip_error_t. 35 explicit Error(const zip_error_t& error) : Error() { 36 zip_error_set(get(), zip_error_code_zip(&error), 37 zip_error_code_system(&error)); 38 } 39 Error() { zip_error_init(get()); } 40 Error(const Error& other) : Error(*other.get()) {} 41 Error& operator=(const Error& other) { 42 zip_error_fini(get()); 43 zip_error_init(get()); 44 zip_error_set(get(), zip_error_code_zip(other.get()), 45 zip_error_code_system(other.get())); 46 return *this; 47 } 48 ~Error() { zip_error_fini(get()); } 49 50 /// \brief Converts the Error into a absl::Status. 51 absl::Status ToStatus() const; 52 absl::Status ToStatus(); 53 54 zip_error_t* get() { return &error_; } 55 const zip_error_t* get() const { return &error_; } 56 57 int system_type() const { return zip_error_system_type(get()); } 58 int zip_code() const { return zip_error_code_zip(get()); } 59 int system_code() const { return zip_error_code_system(get()); } 60 61 private: 62 zip_error_t error_; 63 }; 64 65 /// \brief Converts a zip_error_t into absl::Status. 66 absl::Status ToStatus(zip_error_t* error); 67 68 /// \brief Translates a ZLIB_ER_* constant into a StatusCode. 69 absl::StatusCode ZlibStatusCode(int zlib_error); 70 71 } // namespace libzip 72 } // namespace kythe 73 74 #endif // KYTHE_CXX_COMMON_LIBZIP_ERROR_H_