xref: /aosp_15_r20/system/update_engine/payload_generator/erofs_iterate.h (revision 5a9231315b4521097b8dc3750bc806fcafe0c72f)
1 //
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 //
16 #ifndef UPDATE_ENGINE_PAYLOAD_GENERATOR_EROFS_ITERATE_H_
17 #define UPDATE_ENGINE_PAYLOAD_GENERATOR_EROFS_ITERATE_H_
18 
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
27 struct 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.
36 static_assert(offsetof(erofs_iterate_dir_context, ctx) == 0);
37 
38 // Callable shold be a functor like
39 // std::function<int(struct erofs_inode_info *)>
40 template <typename Callable>
erofs_iterate_root_dir(struct erofs_sb_info * sbi,Callable cb)41 int 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
45   };
46   int err = erofs_read_inode_from_disk(&root_dir);
47   if (err) {
48     LOG(ERROR) << "Failed to read inode " << sbi->root_nid << " from disk "
49                << strerror(-err);
50     return err;
51   }
52   struct erofs_iterate_dir_context param {
53     .ctx.dir = &root_dir, .ctx.pnid = sbi->root_nid,
54     .ctx.cb = [](struct erofs_dir_context* arg) -> int {
55       auto ctx = reinterpret_cast<erofs_iterate_dir_context*>(arg);
56       const auto parent_dir = ctx->ctx.dir;
57       const auto sbi = ctx->ctx.dir->sbi;
58       CHECK_NE(sbi, nullptr);
59       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
68         struct erofs_inode dir {
69           .sbi = sbi, .nid = ctx->ctx.de_nid
70         };
71         if (const int err = erofs_read_inode_from_disk(&dir); err) {
72           LOG(FATAL) << "Failed to erofs_read_inode_from_disk("
73                      << ctx->ctx.de_nid << ") " << strerror(-err);
74           return err;
75         }
76         ctx->ctx.dir = &dir;
77         if (const auto err = erofs_iterate_dir(&ctx->ctx, false); err) {
78           LOG(FATAL) << "Failed to erofs_iterate_dir(" << ctx->ctx.de_nid
79                      << ") " << strerror(-err);
80           return err;
81         }
82         ctx->ctx.dir = parent_dir;
83       }
84       path.resize(len);
85       return err;
86     },
87     .arg = &cb,
88   };
89   return erofs_iterate_dir(&param.ctx, false);
90 }
91 
92 #endif
93