blob: ef3741877e5da9bbad36a3a0af113ebf2b232761 [file] [log] [blame]
Kelvin Zhang446989a2021-12-08 13:49:07 -08001//
2// Copyright (C) 2021 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//
Kelvin Zhang18e97e02023-09-25 09:54:16 -070016#ifndef UPDATE_ENGINE_PAYLOAD_GENERATOR_EROFS_ITERATE_H_
17#define UPDATE_ENGINE_PAYLOAD_GENERATOR_EROFS_ITERATE_H_
Kelvin Zhang446989a2021-12-08 13:49:07 -080018
19#include <string>
20
21#include <erofs/dir.h>
22
23#include "update_engine/common/utils.h"
24
25// The only way to pass extra information to callback function is to use a
26// wrapper type for erofs_dir_context. So here we go
27struct erofs_iterate_dir_context {
28 struct erofs_dir_context ctx;
29 std::string path;
30 void* arg;
31};
32
33// Dear compiler, please don't reoder fields inside erofs_iterate_dir_context.
34// Because EROFS expects us to pass a wrapper type. So |ctx| member of
35// erofs_iterate_dir_context must be put at 0 offset.
36static_assert(offsetof(erofs_iterate_dir_context, ctx) == 0);
37
38// Callable shold be a functor like
39// std::function<int(struct erofs_inode_info *)>
40template <typename Callable>
Kelvin Zhang18e97e02023-09-25 09:54:16 -070041int erofs_iterate_root_dir(struct erofs_sb_info* sbi, Callable cb) {
42 CHECK_NE(sbi, nullptr);
43 struct erofs_inode root_dir {
44 .sbi = sbi, .nid = sbi->root_nid
Kelvin Zhang446989a2021-12-08 13:49:07 -080045 };
Kelvin Zhang18e97e02023-09-25 09:54:16 -070046 int err = erofs_read_inode_from_disk(&root_dir);
Kelvin Zhang446989a2021-12-08 13:49:07 -080047 if (err) {
Kelvin Zhang18e97e02023-09-25 09:54:16 -070048 LOG(ERROR) << "Failed to read inode " << sbi->root_nid << " from disk "
49 << strerror(-err);
Kelvin Zhang446989a2021-12-08 13:49:07 -080050 return err;
51 }
52 struct erofs_iterate_dir_context param {
Kelvin Zhang18e97e02023-09-25 09:54:16 -070053 .ctx.dir = &root_dir, .ctx.pnid = sbi->root_nid,
Kelvin Zhang446989a2021-12-08 13:49:07 -080054 .ctx.cb = [](struct erofs_dir_context* arg) -> int {
55 auto ctx = reinterpret_cast<erofs_iterate_dir_context*>(arg);
Kelvin Zhang18e97e02023-09-25 09:54:16 -070056 const auto parent_dir = ctx->ctx.dir;
57 const auto sbi = ctx->ctx.dir->sbi;
58 CHECK_NE(sbi, nullptr);
Kelvin Zhang446989a2021-12-08 13:49:07 -080059 auto& path = ctx->path;
60 const auto len = path.size();
61 path.push_back('/');
62 path.insert(
63 path.end(), ctx->ctx.dname, ctx->ctx.dname + ctx->ctx.de_namelen);
64 auto cb = static_cast<Callable*>(ctx->arg);
65 const auto err = (*cb)(ctx);
66 if (!err && !ctx->ctx.dot_dotdot && ctx->ctx.de_ftype == EROFS_FT_DIR) {
67 // recursively walk into subdirectories
Kelvin Zhang18e97e02023-09-25 09:54:16 -070068 struct erofs_inode dir {
69 .sbi = sbi, .nid = ctx->ctx.de_nid
70 };
Kelvin Zhang446989a2021-12-08 13:49:07 -080071 if (const int err = erofs_read_inode_from_disk(&dir); err) {
Kelvin Zhang18e97e02023-09-25 09:54:16 -070072 LOG(FATAL) << "Failed to erofs_read_inode_from_disk("
73 << ctx->ctx.de_nid << ") " << strerror(-err);
Kelvin Zhang446989a2021-12-08 13:49:07 -080074 return err;
75 }
76 ctx->ctx.dir = &dir;
77 if (const auto err = erofs_iterate_dir(&ctx->ctx, false); err) {
Kelvin Zhang18e97e02023-09-25 09:54:16 -070078 LOG(FATAL) << "Failed to erofs_iterate_dir(" << ctx->ctx.de_nid
79 << ") " << strerror(-err);
Kelvin Zhang446989a2021-12-08 13:49:07 -080080 return err;
81 }
Kelvin Zhang18e97e02023-09-25 09:54:16 -070082 ctx->ctx.dir = parent_dir;
Kelvin Zhang446989a2021-12-08 13:49:07 -080083 }
84 path.resize(len);
85 return err;
86 },
87 .arg = &cb,
88 };
89 return erofs_iterate_dir(&param.ctx, false);
90}
Kelvin Zhang18e97e02023-09-25 09:54:16 -070091
92#endif