kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/cxx/common/kzip_reader.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_KZIP_READER_H_ 18 #define KYTHE_CXX_COMMON_KZIP_READER_H_ 19 20 #include <zip.h> 21 22 #include <functional> 23 #include <string> 24 25 #include "absl/status/status.h" 26 #include "absl/status/statusor.h" 27 #include "absl/strings/string_view.h" 28 #include "kythe/cxx/common/index_reader.h" 29 #include "kythe/cxx/common/kzip_encoding.h" 30 #include "kythe/proto/analysis.pb.h" 31 32 namespace kythe { 33 34 class KzipReader : public IndexReaderInterface { 35 public: 36 static absl::StatusOr<IndexReader> Open(absl::string_view path); 37 38 /// \brief Constructs an `IndexReader` from the provided source. 39 /// `zip_source_t` is reference counted, see 40 /// https://libzip.org/documentation/zip_source.html 41 /// for detailed ownership semantics. 42 /// Following from that, a reference will be retained on success, 43 /// but the caller is responsible for `source` on error. 44 static absl::StatusOr<IndexReader> FromSource(zip_source_t* source); 45 46 absl::Status Scan(const ScanCallback& callback) override; 47 48 absl::StatusOr<kythe::proto::IndexedCompilation> ReadUnit( 49 absl::string_view digest) override; 50 51 absl::StatusOr<std::string> ReadFile(absl::string_view digest) override; 52 53 private: 54 struct Discard { 55 void operator()(zip_t* archive) { 56 if (archive) zip_discard(archive); 57 } 58 }; 59 using ZipHandle = std::unique_ptr<zip_t, Discard>; 60 61 explicit KzipReader(ZipHandle archive, absl::string_view root, 62 KzipEncoding encoding); 63 64 zip_t* archive() { return archive_.get(); } 65 66 std::optional<absl::string_view> UnitDigest(absl::string_view path); 67 68 ZipHandle archive_; 69 KzipEncoding encoding_; 70 std::string files_prefix_; 71 std::string unit_prefix_; 72 }; 73 74 } // namespace kythe 75 76 #endif // KYTHE_CXX_COMMON_KZIP_READER_H_