| Kelvin Zhang | 446989a | 2021-12-08 13:49:07 -0800 | [diff] [blame] | 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 | // | 
| Kelvin Zhang | 18e97e0 | 2023-09-25 09:54:16 -0700 | [diff] [blame] | 16 | #ifndef UPDATE_ENGINE_PAYLOAD_GENERATOR_EROFS_ITERATE_H_ | 
 | 17 | #define UPDATE_ENGINE_PAYLOAD_GENERATOR_EROFS_ITERATE_H_ | 
| Kelvin Zhang | 446989a | 2021-12-08 13:49:07 -0800 | [diff] [blame] | 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> | 
| Kelvin Zhang | 18e97e0 | 2023-09-25 09:54:16 -0700 | [diff] [blame] | 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 | 
| Kelvin Zhang | 446989a | 2021-12-08 13:49:07 -0800 | [diff] [blame] | 45 |   }; | 
| Kelvin Zhang | 18e97e0 | 2023-09-25 09:54:16 -0700 | [diff] [blame] | 46 |   int err = erofs_read_inode_from_disk(&root_dir); | 
| Kelvin Zhang | 446989a | 2021-12-08 13:49:07 -0800 | [diff] [blame] | 47 |   if (err) { | 
| Kelvin Zhang | 18e97e0 | 2023-09-25 09:54:16 -0700 | [diff] [blame] | 48 |     LOG(ERROR) << "Failed to read inode " << sbi->root_nid << " from disk " | 
 | 49 |                << strerror(-err); | 
| Kelvin Zhang | 446989a | 2021-12-08 13:49:07 -0800 | [diff] [blame] | 50 |     return err; | 
 | 51 |   } | 
 | 52 |   struct erofs_iterate_dir_context param { | 
| Kelvin Zhang | 18e97e0 | 2023-09-25 09:54:16 -0700 | [diff] [blame] | 53 |     .ctx.dir = &root_dir, .ctx.pnid = sbi->root_nid, | 
| Kelvin Zhang | 446989a | 2021-12-08 13:49:07 -0800 | [diff] [blame] | 54 |     .ctx.cb = [](struct erofs_dir_context* arg) -> int { | 
 | 55 |       auto ctx = reinterpret_cast<erofs_iterate_dir_context*>(arg); | 
| Kelvin Zhang | 18e97e0 | 2023-09-25 09:54:16 -0700 | [diff] [blame] | 56 |       const auto parent_dir = ctx->ctx.dir; | 
 | 57 |       const auto sbi = ctx->ctx.dir->sbi; | 
 | 58 |       CHECK_NE(sbi, nullptr); | 
| Kelvin Zhang | 446989a | 2021-12-08 13:49:07 -0800 | [diff] [blame] | 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 | 
| Kelvin Zhang | 18e97e0 | 2023-09-25 09:54:16 -0700 | [diff] [blame] | 68 |         struct erofs_inode dir { | 
 | 69 |           .sbi = sbi, .nid = ctx->ctx.de_nid | 
 | 70 |         }; | 
| Kelvin Zhang | 446989a | 2021-12-08 13:49:07 -0800 | [diff] [blame] | 71 |         if (const int err = erofs_read_inode_from_disk(&dir); err) { | 
| Kelvin Zhang | 18e97e0 | 2023-09-25 09:54:16 -0700 | [diff] [blame] | 72 |           LOG(FATAL) << "Failed to erofs_read_inode_from_disk(" | 
 | 73 |                      << ctx->ctx.de_nid << ") " << strerror(-err); | 
| Kelvin Zhang | 446989a | 2021-12-08 13:49:07 -0800 | [diff] [blame] | 74 |           return err; | 
 | 75 |         } | 
 | 76 |         ctx->ctx.dir = &dir; | 
 | 77 |         if (const auto err = erofs_iterate_dir(&ctx->ctx, false); err) { | 
| Kelvin Zhang | 18e97e0 | 2023-09-25 09:54:16 -0700 | [diff] [blame] | 78 |           LOG(FATAL) << "Failed to erofs_iterate_dir(" << ctx->ctx.de_nid | 
 | 79 |                      << ") " << strerror(-err); | 
| Kelvin Zhang | 446989a | 2021-12-08 13:49:07 -0800 | [diff] [blame] | 80 |           return err; | 
 | 81 |         } | 
| Kelvin Zhang | 18e97e0 | 2023-09-25 09:54:16 -0700 | [diff] [blame] | 82 |         ctx->ctx.dir = parent_dir; | 
| Kelvin Zhang | 446989a | 2021-12-08 13:49:07 -0800 | [diff] [blame] | 83 |       } | 
 | 84 |       path.resize(len); | 
 | 85 |       return err; | 
 | 86 |     }, | 
 | 87 |     .arg = &cb, | 
 | 88 |   }; | 
 | 89 |   return erofs_iterate_dir(¶m.ctx, false); | 
 | 90 | } | 
| Kelvin Zhang | 18e97e0 | 2023-09-25 09:54:16 -0700 | [diff] [blame] | 91 |  | 
 | 92 | #endif |