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
24#include "absl/strings/string_view.h"
25
29
30namespace cuttlefish {
31
32// CpioReader provides read-only access to files contained within a CPIO
33// archive. It supports the SVR4 (newc), POSIX (odc), and binary (bin) CPIO
34// formats.
35class CpioReader : public ReadFilesystem {
36 public:
38 std::unique_ptr<ReaderSeeker> reader);
39
41 absl::string_view path) override;
42
43 Result<uint32_t> FileAttributes(absl::string_view path) const override;
44
45 private:
46 struct FileEntry {
47 uint64_t offset;
48 uint64_t size;
49 uint32_t mode;
50 };
51
52 using EntriesMap = std::map<std::string, FileEntry, std::less<>>;
53
54 CpioReader(std::unique_ptr<ReaderSeeker> reader, EntriesMap entries);
55
56 static Result<EntriesMap> Parse(const ReaderSeeker& reader);
57 static Result<EntriesMap> ParseOdc(const ReaderSeeker& reader);
58 static Result<EntriesMap> ParseNewc(const ReaderSeeker& reader);
59 static Result<EntriesMap> ParseBin(const ReaderSeeker& reader,
60 bool file_is_big_endian);
61
62 std::unique_ptr<ReaderSeeker> reader_;
64};
65
66} // namespace cuttlefish
Definition: cpio.h:35
static Result< EntriesMap > ParseBin(const ReaderSeeker &reader, bool file_is_big_endian)
Definition: cpio.cc:242
CpioReader(std::unique_ptr< ReaderSeeker > reader, EntriesMap entries)
Definition: cpio.cc:138
std::unique_ptr< ReaderSeeker > reader_
Definition: cpio.h:62
static Result< EntriesMap > ParseNewc(const ReaderSeeker &reader)
Definition: cpio.cc:158
static Result< EntriesMap > ParseOdc(const ReaderSeeker &reader)
Definition: cpio.cc:202
Result< std::unique_ptr< ReaderSeeker > > OpenReadOnly(absl::string_view path) override
Definition: cpio.cc:287
std::map< std::string, FileEntry, std::less<> > EntriesMap
Definition: cpio.h:52
EntriesMap entries_
Definition: cpio.h:63
static Result< EntriesMap > Parse(const ReaderSeeker &reader)
Definition: cpio.cc:141
static Result< std::unique_ptr< CpioReader > > Open(std::unique_ptr< ReaderSeeker > reader)
Definition: cpio.cc:130
Result< uint32_t > FileAttributes(absl::string_view path) const override
Definition: cpio.cc:295
Definition: filesystem.h:29
Definition: reader_seeker.h:29
Definition: alloc_driver.h:21
tl::expected< T, StackTraceError< E > > Result
Definition: result_type.h:30
uint64_t size
Definition: cpio.h:48
uint32_t mode
Definition: cpio.h:49
uint64_t offset
Definition: cpio.h:47