kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/cxx/common/kzip_writer.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_WRITER_H_ 18 #define KYTHE_CXX_COMMON_KZIP_WRITER_H_ 19 20 #include <zip.h> 21 22 #include <unordered_map> 23 24 #include "absl/status/status.h" 25 #include "absl/status/statusor.h" 26 #include "absl/strings/string_view.h" 27 #include "kythe/cxx/common/index_writer.h" 28 #include "kythe/cxx/common/kzip_encoding.h" 29 #include "kythe/proto/analysis.pb.h" 30 31 namespace kythe { 32 33 /// \brief Kzip implementation of IndexWriter. 34 /// see https://www.kythe.io/docs/kythe-kzip.html for format description. 35 class KzipWriter : public IndexWriterInterface { 36 public: 37 /// \brief Constructs a Kzip IndexWriter which will create and write to 38 /// \param path Path to the file to create. Must not currently exist. 39 /// \param encoding Encoding to use for compilation units. 40 static absl::StatusOr<IndexWriter> Create( 41 absl::string_view path, KzipEncoding encoding = DefaultEncoding()); 42 /// \brief Constructs an IndexWriter from the libzip source pointer. 43 /// \param source zip_source_t to use as backing store. 44 /// See https://libzip.org/documentation/zip_source.html for ownership. 45 /// \param flags Flags to use when opening `source`. 46 /// \param encoding Encoding to use for compilation units. 47 static absl::StatusOr<IndexWriter> FromSource( 48 zip_source_t* source, KzipEncoding encoding = DefaultEncoding(), 49 int flags = ZIP_CREATE | ZIP_EXCL); 50 51 /// \brief Destroys the KzipWriter. 52 ~KzipWriter() override; 53 54 /// \brief Writes the unit to the kzip file, returning its digest. 55 absl::StatusOr<std::string> WriteUnit( 56 const kythe::proto::IndexedCompilation& unit) override; 57 58 /// \brief Writes the file contents to the kzip file, returning their digest. 59 absl::StatusOr<std::string> WriteFile(absl::string_view content) override; 60 61 /// \brief Flushes accumulated writes and closes the kzip file. 62 /// Close must be called before the KzipWriter is destroyed! 63 absl::Status Close() override; 64 65 private: 66 using Path = std::string; 67 using Contents = std::string; 68 using FileMap = std::unordered_map<Path, Contents>; 69 70 explicit KzipWriter(zip_t* archive, KzipEncoding encoding); 71 72 absl::StatusOr<std::string> InsertFile(absl::string_view path, 73 absl::string_view content); 74 75 absl::Status InitializeArchive(zip_t* archive); 76 77 static KzipEncoding DefaultEncoding(); 78 79 bool initialized_ = false; // Whether or not the `root` entry exists. 80 zip_t* archive_; // Owned, but must be manually deleted via `Close`. 81 // Memory for inserted files must be retained until close and 82 // we don't want to insert identical entries multiple times. 83 // This must be a node-based container to ensure pointer stability of the file 84 // contents. 85 FileMap contents_; 86 KzipEncoding encoding_; 87 }; 88 89 } // namespace kythe 90 91 #endif // KYTHE_CXX_COMMON_KZIP_WRITER_H_