From d45750aa2ccf48ee200ea0de6a5e20199a927fca Mon Sep 17 00:00:00 2001 From: Yi-Yo Chiang Date: Mon, 12 Dec 2022 23:57:04 +0800 Subject: [PATCH] libfstab: Add const overload of GetEntryForMountPoint() Const version of GetEntryForMountPoint() accepts a pointer to const Fstab and returns a pointer to const FstabEntry. In order to refrain from adding more boilderplate, simplify the implementation of GetEntryForMountPoint() to "return first entry of GetEntriesForMountPoint()". The added overhead should be negligible as fstab is usually small (around dozens of entries max), so iterating the whole list is not computationally expensive. Also templatize the implementation of GetEntriesByPred() to reduce the amount of boilerplate. Bug: 243501054 Test: Presubmit Change-Id: I046d61d68385825656bb7bc7177c0d1d3e8b36e1 --- fs_mgr/fs_mgr_fstab.cpp | 46 ++++++++++++++++++------------ fs_mgr/include_fstab/fstab/fstab.h | 5 +++- 2 files changed, 32 insertions(+), 19 deletions(-) diff --git a/fs_mgr/fs_mgr_fstab.cpp b/fs_mgr/fs_mgr_fstab.cpp index 7189a71a8..36bd75d42 100644 --- a/fs_mgr/fs_mgr_fstab.cpp +++ b/fs_mgr/fs_mgr_fstab.cpp @@ -488,13 +488,21 @@ std::set ExtraBootDevices(const Fstab& fstab) { return boot_devices; } -template -std::vector GetEntriesByPred(Fstab* fstab, const Pred& pred) { +// Helper class that maps Fstab* -> FstabEntry; const Fstab* -> const FstabEntry. +template +struct FstabPtrEntry { + using is_const_fstab = std::is_const>; + using type = std::conditional_t; +}; + +template ::type, + typename Pred> +std::vector GetEntriesByPred(FstabPtr fstab, const Pred& pred) { if (fstab == nullptr) { return {}; } - std::vector entries; - for (auto&& entry : *fstab) { + std::vector entries; + for (FstabPtrEntryType& entry : *fstab) { if (pred(entry)) { entries.push_back(&entry); } @@ -835,25 +843,27 @@ bool ReadDefaultFstab(Fstab* fstab) { return !fstab->empty(); } -FstabEntry* GetEntryForMountPoint(Fstab* fstab, const std::string& path) { - if (fstab == nullptr) { - return nullptr; - } - - for (auto& entry : *fstab) { - if (entry.mount_point == path) { - return &entry; - } - } - - return nullptr; -} - std::vector GetEntriesForMountPoint(Fstab* fstab, const std::string& path) { return GetEntriesByPred(fstab, [&path](const FstabEntry& entry) { return entry.mount_point == path; }); } +std::vector GetEntriesForMountPoint(const Fstab* fstab, + const std::string& path) { + return GetEntriesByPred(fstab, + [&path](const FstabEntry& entry) { return entry.mount_point == path; }); +} + +FstabEntry* GetEntryForMountPoint(Fstab* fstab, const std::string& path) { + std::vector entries = GetEntriesForMountPoint(fstab, path); + return entries.empty() ? nullptr : entries.front(); +} + +const FstabEntry* GetEntryForMountPoint(const Fstab* fstab, const std::string& path) { + std::vector entries = GetEntriesForMountPoint(fstab, path); + return entries.empty() ? nullptr : entries.front(); +} + std::set GetBootDevices() { // First check bootconfig, then kernel commandline, then the device tree std::string dt_file_name = get_android_dt_dir() + "/boot_devices"; diff --git a/fs_mgr/include_fstab/fstab/fstab.h b/fs_mgr/include_fstab/fstab/fstab.h index 124f0706e..a914b53bb 100644 --- a/fs_mgr/include_fstab/fstab/fstab.h +++ b/fs_mgr/include_fstab/fstab/fstab.h @@ -105,10 +105,13 @@ bool ReadFstabFromDt(Fstab* fstab, bool verbose = true); bool ReadDefaultFstab(Fstab* fstab); bool SkipMountingPartitions(Fstab* fstab, bool verbose = false); -FstabEntry* GetEntryForMountPoint(Fstab* fstab, const std::string& path); // The Fstab can contain multiple entries for the same mount point with different configurations. std::vector GetEntriesForMountPoint(Fstab* fstab, const std::string& path); +// Like GetEntriesForMountPoint() but return only the first entry or nullptr if no entry is found. +FstabEntry* GetEntryForMountPoint(Fstab* fstab, const std::string& path); +const FstabEntry* GetEntryForMountPoint(const Fstab* fstab, const std::string& path); + // This method builds DSU fstab entries and transfer the fstab. // // fstab points to the unmodified fstab.