Android-cuttlefish cvd tool
archive.h
Go to the documentation of this file.
1//
2// Copyright (C) 2025 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#pragma once
17
18#include <stddef.h>
19#include <stdint.h>
20
21#include <memory>
22#include <string>
23
24#include "absl/strings/string_view.h"
25
31#include "cuttlefish/io/io.h"
33
34namespace cuttlefish {
35
37 public:
38 friend class WritableZip;
39 friend class WritableZipSource;
40
42
44 virtual ~ReadableZip();
46
47 /* Counts members, including un-finalized ones from AddFile. */
49
50 Result<std::string> EntryName(uint64_t index);
51 Result<uint32_t> EntryAttributes(uint64_t index) const;
52 Result<bool> EntryIsDirectory(uint64_t index);
53
54 /* Decompresses and extract a file from the archive. */
55 Result<SeekableZipSource> GetFile(uint64_t index) const;
56
58 absl::string_view path) override;
59
60 Result<uint32_t> FileAttributes(absl::string_view path) const override;
61
62 protected:
64
65 Result<SeekableZipSource> GetFile(const std::string& name) const;
66
69};
70
71class WritableZip : public ReadableZip {
72 public:
73 enum class OpenBehavior {
74 KeepIfExists,
75 Truncate,
76 };
79
81 ~WritableZip() override = default;
83
84 /* Mutates the archive to add a file. Reading the contents of the sources that
85 * are added is deferred until `Finalize` time. */
86 Result<void> AddFile(const std::string& name, ReadableZipSource);
87
88 /* Performs transfers from the input `ZipSource`s to the output `ZipSource`
89 * and does the archive encoding. */
91
92 protected:
94
96};
97
98} // namespace cuttlefish
Definition: filesystem.h:29
Definition: readable_source.h:24
Definition: archive.h:36
Result< bool > EntryIsDirectory(uint64_t index)
Definition: archive.cc:101
Result< std::unique_ptr< ReaderSeeker > > OpenReadOnly(absl::string_view path) override
Definition: archive.cc:134
static Result< ReadableZip > FromSource(SeekableZipSource)
Definition: archive.cc:42
ReadableZip(ReadableZip &&)
Result< uint32_t > FileAttributes(absl::string_view path) const override
Definition: archive.cc:140
Result< SeekableZipSource > GetFile(uint64_t index) const
Definition: archive.cc:122
friend class WritableZip
Definition: archive.h:38
WritableZipSource source_
Definition: archive.h:68
Result< std::string > EntryName(uint64_t index)
Definition: archive.cc:75
ReadableZip & operator=(ReadableZip &&)
ManagedZip raw_
Definition: archive.h:67
Result< uint32_t > EntryAttributes(uint64_t index) const
Definition: archive.cc:84
Result< uint64_t > NumEntries()
Definition: archive.cc:66
Definition: seekable_source.h:27
Definition: writable_source.h:31
Definition: archive.h:71
WritableZip & operator=(WritableZip &&)=default
~WritableZip() override=default
WritableZip(WritableZip &&)=default
OpenBehavior
Definition: archive.h:73
static Result< void > Finalize(WritableZip)
Definition: archive.cc:197
Result< void > AddFile(const std::string &name, ReadableZipSource)
Definition: archive.cc:183
static Result< WritableZip > FromSource(WritableZipSource, OpenBehavior open_behavior=OpenBehavior::Truncate)
Definition: archive.cc:152
Definition: alloc_driver.h:21
std::unique_ptr< zip_t, ZipDeleter > ManagedZip
Definition: managed.h:35
tl::expected< T, StackTraceError< E > > Result
Definition: result_type.h:30