Android-cuttlefish cvd tool
cpio.h
Go to the documentation of this file.
1//
2// Copyright (C) 2026 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 <stdint.h>
19
20#include <map>
21#include <memory>
22#include <string>
23#include <string_view>
24
26#include "cuttlefish/io/io.h"
28
29namespace cuttlefish {
30
31// CpioReader provides read-only access to files contained within a CPIO
32// archive. It supports the SVR4 (newc), POSIX (odc), and binary (bin) CPIO
33// formats.
34class CpioReader : public ReadFilesystem {
35 public:
37 std::unique_ptr<ReaderSeeker> reader);
38
40 std::string_view path) override;
41
42 Result<uint32_t> FileAttributes(std::string_view path) const override;
43
44 private:
45 struct FileEntry {
46 uint64_t offset;
47 uint64_t size;
48 uint32_t mode;
49 };
50
51 using EntriesMap = std::map<std::string, FileEntry, std::less<>>;
52
53 CpioReader(std::unique_ptr<ReaderSeeker> reader, EntriesMap entries);
54
55 static Result<EntriesMap> Parse(const ReaderSeeker& reader);
56 static Result<EntriesMap> ParseOdc(const ReaderSeeker& reader);
57 static Result<EntriesMap> ParseNewc(const ReaderSeeker& reader);
58 static Result<EntriesMap> ParseBin(const ReaderSeeker& reader,
59 bool file_is_big_endian);
60
61 std::unique_ptr<ReaderSeeker> reader_;
63};
64
65} // namespace cuttlefish
Definition: cpio.h:34
static Result< EntriesMap > ParseBin(const ReaderSeeker &reader, bool file_is_big_endian)
Definition: cpio.cc:240
Result< std::unique_ptr< ReaderSeeker > > OpenReadOnly(std::string_view path) override
Definition: cpio.cc:285
CpioReader(std::unique_ptr< ReaderSeeker > reader, EntriesMap entries)
Definition: cpio.cc:136
std::unique_ptr< ReaderSeeker > reader_
Definition: cpio.h:61
Result< uint32_t > FileAttributes(std::string_view path) const override
Definition: cpio.cc:293
static Result< EntriesMap > ParseNewc(const ReaderSeeker &reader)
Definition: cpio.cc:156
static Result< EntriesMap > ParseOdc(const ReaderSeeker &reader)
Definition: cpio.cc:200
std::map< std::string, FileEntry, std::less<> > EntriesMap
Definition: cpio.h:51
EntriesMap entries_
Definition: cpio.h:62
static Result< EntriesMap > Parse(const ReaderSeeker &reader)
Definition: cpio.cc:139
static Result< std::unique_ptr< CpioReader > > Open(std::unique_ptr< ReaderSeeker > reader)
Definition: cpio.cc:128
Definition: filesystem.h:27
Definition: io.h:60
Definition: alloc_driver.h:20
tl::expected< T, StackTraceError > Result
Definition: result_type.h:30
uint64_t size
Definition: cpio.h:47
uint32_t mode
Definition: cpio.h:48
uint64_t offset
Definition: cpio.h:46