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#include <string_view>
24
30#include "cuttlefish/io/io.h"
32
33namespace cuttlefish {
34
36 public:
37 friend class WritableZip;
38 friend class WritableZipSource;
39
41
43 virtual ~ReadableZip();
45
46 /* Counts members, including un-finalized ones from AddFile. */
48
49 Result<std::string> EntryName(uint64_t index);
50 Result<uint32_t> EntryAttributes(uint64_t index) const;
51 Result<bool> EntryIsDirectory(uint64_t index);
52
53 /* Decompresses and extract a file from the archive. */
54 Result<SeekableZipSource> GetFile(uint64_t index) const;
55
57 std::string_view path) override;
58
59 Result<uint32_t> FileAttributes(std::string_view path) const override;
60
61 protected:
63
64 Result<SeekableZipSource> GetFile(const std::string& name) const;
65
68};
69
70class WritableZip : public ReadableZip {
71 public:
72 enum class OpenBehavior {
73 KeepIfExists,
74 Truncate,
75 };
78
80 ~WritableZip() override = default;
82
83 /* Mutates the archive to add a file. Reading the contents of the sources that
84 * are added is deferred until `Finalize` time. */
85 Result<void> AddFile(const std::string& name, ReadableZipSource);
86
87 /* Performs transfers from the input `ZipSource`s to the output `ZipSource`
88 * and does the archive encoding. */
90
91 protected:
93
95};
96
97} // namespace cuttlefish
Definition: filesystem.h:27
Definition: readable_source.h:37
Definition: archive.h:35
Result< uint32_t > FileAttributes(std::string_view path) const override
Definition: archive.cc:139
Result< bool > EntryIsDirectory(uint64_t index)
Definition: archive.cc:100
static Result< ReadableZip > FromSource(SeekableZipSource)
Definition: archive.cc:41
ReadableZip(ReadableZip &&)
Result< SeekableZipSource > GetFile(uint64_t index) const
Definition: archive.cc:121
friend class WritableZip
Definition: archive.h:37
WritableZipSource source_
Definition: archive.h:67
Result< std::string > EntryName(uint64_t index)
Definition: archive.cc:74
Result< std::unique_ptr< ReaderSeeker > > OpenReadOnly(std::string_view path) override
Definition: archive.cc:133
ReadableZip & operator=(ReadableZip &&)
ManagedZip raw_
Definition: archive.h:66
Result< uint32_t > EntryAttributes(uint64_t index) const
Definition: archive.cc:83
Result< uint64_t > NumEntries()
Definition: archive.cc:65
Definition: seekable_source.h:31
Definition: writable_source.h:30
Definition: archive.h:70
WritableZip & operator=(WritableZip &&)=default
~WritableZip() override=default
WritableZip(WritableZip &&)=default
OpenBehavior
Definition: archive.h:72
static Result< void > Finalize(WritableZip)
Definition: archive.cc:196
Result< void > AddFile(const std::string &name, ReadableZipSource)
Definition: archive.cc:182
static Result< WritableZip > FromSource(WritableZipSource, OpenBehavior open_behavior=OpenBehavior::Truncate)
Definition: archive.cc:151
char * name
Definition: libf2fs.c:1403
Definition: alloc_driver.h:20
std::unique_ptr< zip_t, ZipDeleter > ManagedZip
Definition: managed.h:35
tl::expected< T, StackTraceError > Result
Definition: result_type.h:30