kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/cxx/common/libzip/error.cc (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 #include "kythe/cxx/common/libzip/error.h" 18 19 #include <zip.h> 20 21 #include "absl/status/status.h" 22 #include "kythe/cxx/common/status.h" 23 24 namespace kythe { 25 namespace libzip { 26 namespace { 27 28 absl::StatusCode GetStatusCode(zip_error_t* error) { 29 switch (zip_error_system_type(error)) { 30 case ZIP_ET_SYS: 31 return ErrnoToStatusCode(zip_error_code_system(error)); 32 case ZIP_ET_ZLIB: 33 return ZlibStatusCode(zip_error_code_system(error)); 34 case ZIP_ET_NONE: 35 default: 36 return ZlibStatusCode(zip_error_code_zip(error)); 37 } 38 } 39 40 } // namespace 41 42 absl::Status Error::ToStatus() { return kythe::libzip::ToStatus(get()); } 43 absl::Status Error::ToStatus() const { 44 // Due to caching in zip_error_strerror, it can't be const so we must copy. 45 return Error(*this).ToStatus(); 46 } 47 48 absl::Status ToStatus(zip_error_t* error) { 49 absl::StatusCode code = GetStatusCode(error); 50 if (code == absl::StatusCode::kOk) { 51 return absl::OkStatus(); 52 } 53 return absl::Status(code, zip_error_strerror(error)); 54 } 55 56 absl::StatusCode ZlibStatusCode(int zlib_error) { 57 using absl::StatusCode; 58 switch (zlib_error) { 59 case ZIP_ER_OK: // No error 60 return StatusCode::kOk; 61 case ZIP_ER_MULTIDISK: // Multi-disk zip archives not supported 62 case ZIP_ER_COMPNOTSUPP: // Compression method not supported 63 case ZIP_ER_ENCRNOTSUPP: // Encryption method not supported 64 case ZIP_ER_OPNOTSUPP: // Operation not supported 65 return StatusCode::kUnimplemented; 66 case ZIP_ER_INVAL: // Invalid argument 67 case ZIP_ER_NOZIP: // Not a zip archive 68 case ZIP_ER_INCONS: // Zip archive inconsistent 69 case ZIP_ER_COMPRESSED_DATA: // Compressed data invalid 70 case ZIP_ER_CRC: // CRC error 71 return StatusCode::kInvalidArgument; 72 case ZIP_ER_RENAME: // Renaming temporary file failed 73 case ZIP_ER_CLOSE: // Closing zip archive failed 74 case ZIP_ER_SEEK: // Seek error 75 case ZIP_ER_READ: // Read error 76 case ZIP_ER_WRITE: // Write error 77 case ZIP_ER_ZLIB: // Zlib error 78 case ZIP_ER_INTERNAL: // Internal error 79 case ZIP_ER_REMOVE: // Can't remove file 80 case ZIP_ER_TELL: // Tell error 81 return StatusCode::kInternal; 82 case ZIP_ER_ZIPCLOSED: // Containing zip archive was closed 83 case ZIP_ER_INUSE: // Resource still in use 84 return StatusCode::kFailedPrecondition; 85 case ZIP_ER_NOENT: // No such file 86 case ZIP_ER_DELETED: // Entry has been deleted 87 return StatusCode::kNotFound; 88 case ZIP_ER_EXISTS: // File already exists 89 return StatusCode::kAlreadyExists; 90 case ZIP_ER_MEMORY: // Malloc failure 91 return StatusCode::kResourceExhausted; 92 case ZIP_ER_CHANGED: // Entry has been changed 93 return StatusCode::kAborted; 94 case ZIP_ER_EOF: // Premature end of file 95 return StatusCode::kOutOfRange; 96 case ZIP_ER_OPEN: // Can't open file 97 case ZIP_ER_TMPOPEN: // Failure to create temporary file 98 default: 99 return StatusCode::kUnknown; 100 } 101 } 102 103 } // namespace libzip 104 } // namespace kythe