Loading fs_mgr/liblp/Android.bp +1 −0 Original line number Diff line number Diff line Loading @@ -36,6 +36,7 @@ cc_library { "builder.cpp", "images.cpp", "partition_opener.cpp", "property_fetcher.cpp", "reader.cpp", "utility.cpp", "writer.cpp", Loading fs_mgr/liblp/builder.cpp +4 −23 Original line number Diff line number Diff line Loading @@ -19,23 +19,17 @@ #include <string.h> #include <algorithm> #include <string_view> #include <android-base/properties.h> #include <android-base/unique_fd.h> #include "liblp/liblp.h" #include "liblp/property_fetcher.h" #include "reader.h" #include "utility.h" namespace android { namespace fs_mgr { std::optional<bool> MetadataBuilder::sABOverride; std::optional<bool> MetadataBuilder::sRetrofitDap; static constexpr std::string_view kDefaultGroup = "default"; bool LinearExtent::AddTo(LpMetadata* out) const { if (device_index_ >= out->block_devices.size()) { LERROR << "Extent references unknown block device."; Loading Loading @@ -211,14 +205,6 @@ std::unique_ptr<MetadataBuilder> MetadataBuilder::NewForUpdate(const IPartitionO return New(*metadata.get(), &opener); } void MetadataBuilder::OverrideABForTesting(bool ab_device) { sABOverride = ab_device; } void MetadataBuilder::OverrideRetrofitDynamicParititonsForTesting(bool retrofit) { sRetrofitDap = retrofit; } MetadataBuilder::MetadataBuilder() : auto_slot_suffixing_(false) { memset(&geometry_, 0, sizeof(geometry_)); geometry_.magic = LP_METADATA_GEOMETRY_MAGIC; Loading Loading @@ -1051,17 +1037,12 @@ void MetadataBuilder::SetAutoSlotSuffixing() { } bool MetadataBuilder::IsABDevice() { if (sABOverride.has_value()) { return *sABOverride; } return !android::base::GetProperty("ro.boot.slot_suffix", "").empty(); return !IPropertyFetcher::GetInstance()->GetProperty("ro.boot.slot_suffix", "").empty(); } bool MetadataBuilder::IsRetrofitDynamicPartitionsDevice() { if (sRetrofitDap.has_value()) { return *sRetrofitDap; } return android::base::GetBoolProperty("ro.boot.dynamic_partitions_retrofit", false); return IPropertyFetcher::GetInstance()->GetBoolProperty("ro.boot.dynamic_partitions_retrofit", false); } bool MetadataBuilder::IsRetrofitMetadata() const { Loading fs_mgr/liblp/builder_test.cpp +20 −14 Original line number Diff line number Diff line Loading @@ -19,36 +19,40 @@ #include <gtest/gtest.h> #include <liblp/builder.h> #include "mock_property_fetcher.h" #include "utility.h" using namespace std; using namespace android::fs_mgr; using ::android::fs_mgr::MockPropertyFetcher; using ::testing::_; using ::testing::AnyNumber; using ::testing::ElementsAre; using ::testing::NiceMock; using ::testing::Return; static void ResetPropertyFetcher() { IPropertyFetcher::OverrideForTesting(std::make_unique<NiceMock<MockPropertyFetcher>>()); } MockPropertyFetcher* GetMockedInstance() { return static_cast<MockPropertyFetcher*>(IPropertyFetcher::GetInstance()); } class Environment : public ::testing::Environment { public: void SetUp() override { MetadataBuilder::OverrideABForTesting(false); MetadataBuilder::OverrideRetrofitDynamicParititonsForTesting(false); } void SetUp() override { ResetPropertyFetcher(); } }; int main(int argc, char** argv) { ::testing::AddGlobalTestEnvironment(new Environment); ::testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); } class BuilderTest : public ::testing::Test { public: void SetUp() override { MetadataBuilder::OverrideABForTesting(false); MetadataBuilder::OverrideRetrofitDynamicParititonsForTesting(false); } void TearDown() override { MetadataBuilder::OverrideABForTesting(false); MetadataBuilder::OverrideRetrofitDynamicParititonsForTesting(false); } void SetUp() override { ResetPropertyFetcher(); } void TearDown() override { ResetPropertyFetcher(); } }; TEST_F(BuilderTest, BuildBasic) { Loading Loading @@ -785,7 +789,9 @@ TEST_F(BuilderTest, ABExtents) { // A and B slots should be allocated from separate halves of the partition, // to mitigate allocating too many extents. (b/120433288) MetadataBuilder::OverrideABForTesting(true); ON_CALL(*GetMockedInstance(), GetProperty("ro.boot.slot_suffix", _)) .WillByDefault(Return("_a")); auto builder = MetadataBuilder::New(device_info, 65536, 2); ASSERT_NE(builder, nullptr); Partition* system_a = builder->AddPartition("system_a", 0); Loading fs_mgr/liblp/include/liblp/builder.h +4 −9 Original line number Diff line number Diff line Loading @@ -24,6 +24,7 @@ #include <memory> #include <optional> #include <set> #include <string_view> #include "liblp.h" #include "partition_opener.h" Loading @@ -37,6 +38,9 @@ class LinearExtent; static const uint32_t kDefaultPartitionAlignment = 1024 * 1024; static const uint32_t kDefaultBlockSize = 4096; // Name of the default group in a metadata. static constexpr std::string_view kDefaultGroup = "default"; // Abstraction around dm-targets that can be encoded into logical partition tables. class Extent { public: Loading Loading @@ -196,12 +200,6 @@ class MetadataBuilder { return New(device_info, metadata_max_size, metadata_slot_count); } // Used by the test harness to override whether the device is "A/B". static void OverrideABForTesting(bool ab_device); // Used by the test harness to override whether the device is "retrofitting dynamic partitions". static void OverrideRetrofitDynamicParititonsForTesting(bool retrofit); // Define a new partition group. By default there is one group called // "default", with an unrestricted size. A non-zero size will restrict the // total space used by all partitions in the group. Loading Loading @@ -347,9 +345,6 @@ class MetadataBuilder { const std::vector<Interval>& free_list, uint64_t sectors_needed) const; static std::optional<bool> sABOverride; static std::optional<bool> sRetrofitDap; LpMetadataGeometry geometry_; LpMetadataHeader header_; std::vector<std::unique_ptr<Partition>> partitions_; Loading fs_mgr/liblp/include/liblp/property_fetcher.h 0 → 100644 +42 −0 Original line number Diff line number Diff line // // Copyright (C) 2019 The Android Open Source Project // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // #pragma once #include <memory> namespace android { namespace fs_mgr { class IPropertyFetcher { public: virtual ~IPropertyFetcher() = default; virtual std::string GetProperty(const std::string& key, const std::string& defaultValue) = 0; virtual bool GetBoolProperty(const std::string& key, bool defaultValue) = 0; static IPropertyFetcher* GetInstance(); static void OverrideForTesting(std::unique_ptr<IPropertyFetcher>&&); }; class PropertyFetcher : public IPropertyFetcher { public: ~PropertyFetcher() = default; std::string GetProperty(const std::string& key, const std::string& defaultValue) override; bool GetBoolProperty(const std::string& key, bool defaultValue) override; }; } // namespace fs_mgr } // namespace android Loading
fs_mgr/liblp/Android.bp +1 −0 Original line number Diff line number Diff line Loading @@ -36,6 +36,7 @@ cc_library { "builder.cpp", "images.cpp", "partition_opener.cpp", "property_fetcher.cpp", "reader.cpp", "utility.cpp", "writer.cpp", Loading
fs_mgr/liblp/builder.cpp +4 −23 Original line number Diff line number Diff line Loading @@ -19,23 +19,17 @@ #include <string.h> #include <algorithm> #include <string_view> #include <android-base/properties.h> #include <android-base/unique_fd.h> #include "liblp/liblp.h" #include "liblp/property_fetcher.h" #include "reader.h" #include "utility.h" namespace android { namespace fs_mgr { std::optional<bool> MetadataBuilder::sABOverride; std::optional<bool> MetadataBuilder::sRetrofitDap; static constexpr std::string_view kDefaultGroup = "default"; bool LinearExtent::AddTo(LpMetadata* out) const { if (device_index_ >= out->block_devices.size()) { LERROR << "Extent references unknown block device."; Loading Loading @@ -211,14 +205,6 @@ std::unique_ptr<MetadataBuilder> MetadataBuilder::NewForUpdate(const IPartitionO return New(*metadata.get(), &opener); } void MetadataBuilder::OverrideABForTesting(bool ab_device) { sABOverride = ab_device; } void MetadataBuilder::OverrideRetrofitDynamicParititonsForTesting(bool retrofit) { sRetrofitDap = retrofit; } MetadataBuilder::MetadataBuilder() : auto_slot_suffixing_(false) { memset(&geometry_, 0, sizeof(geometry_)); geometry_.magic = LP_METADATA_GEOMETRY_MAGIC; Loading Loading @@ -1051,17 +1037,12 @@ void MetadataBuilder::SetAutoSlotSuffixing() { } bool MetadataBuilder::IsABDevice() { if (sABOverride.has_value()) { return *sABOverride; } return !android::base::GetProperty("ro.boot.slot_suffix", "").empty(); return !IPropertyFetcher::GetInstance()->GetProperty("ro.boot.slot_suffix", "").empty(); } bool MetadataBuilder::IsRetrofitDynamicPartitionsDevice() { if (sRetrofitDap.has_value()) { return *sRetrofitDap; } return android::base::GetBoolProperty("ro.boot.dynamic_partitions_retrofit", false); return IPropertyFetcher::GetInstance()->GetBoolProperty("ro.boot.dynamic_partitions_retrofit", false); } bool MetadataBuilder::IsRetrofitMetadata() const { Loading
fs_mgr/liblp/builder_test.cpp +20 −14 Original line number Diff line number Diff line Loading @@ -19,36 +19,40 @@ #include <gtest/gtest.h> #include <liblp/builder.h> #include "mock_property_fetcher.h" #include "utility.h" using namespace std; using namespace android::fs_mgr; using ::android::fs_mgr::MockPropertyFetcher; using ::testing::_; using ::testing::AnyNumber; using ::testing::ElementsAre; using ::testing::NiceMock; using ::testing::Return; static void ResetPropertyFetcher() { IPropertyFetcher::OverrideForTesting(std::make_unique<NiceMock<MockPropertyFetcher>>()); } MockPropertyFetcher* GetMockedInstance() { return static_cast<MockPropertyFetcher*>(IPropertyFetcher::GetInstance()); } class Environment : public ::testing::Environment { public: void SetUp() override { MetadataBuilder::OverrideABForTesting(false); MetadataBuilder::OverrideRetrofitDynamicParititonsForTesting(false); } void SetUp() override { ResetPropertyFetcher(); } }; int main(int argc, char** argv) { ::testing::AddGlobalTestEnvironment(new Environment); ::testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); } class BuilderTest : public ::testing::Test { public: void SetUp() override { MetadataBuilder::OverrideABForTesting(false); MetadataBuilder::OverrideRetrofitDynamicParititonsForTesting(false); } void TearDown() override { MetadataBuilder::OverrideABForTesting(false); MetadataBuilder::OverrideRetrofitDynamicParititonsForTesting(false); } void SetUp() override { ResetPropertyFetcher(); } void TearDown() override { ResetPropertyFetcher(); } }; TEST_F(BuilderTest, BuildBasic) { Loading Loading @@ -785,7 +789,9 @@ TEST_F(BuilderTest, ABExtents) { // A and B slots should be allocated from separate halves of the partition, // to mitigate allocating too many extents. (b/120433288) MetadataBuilder::OverrideABForTesting(true); ON_CALL(*GetMockedInstance(), GetProperty("ro.boot.slot_suffix", _)) .WillByDefault(Return("_a")); auto builder = MetadataBuilder::New(device_info, 65536, 2); ASSERT_NE(builder, nullptr); Partition* system_a = builder->AddPartition("system_a", 0); Loading
fs_mgr/liblp/include/liblp/builder.h +4 −9 Original line number Diff line number Diff line Loading @@ -24,6 +24,7 @@ #include <memory> #include <optional> #include <set> #include <string_view> #include "liblp.h" #include "partition_opener.h" Loading @@ -37,6 +38,9 @@ class LinearExtent; static const uint32_t kDefaultPartitionAlignment = 1024 * 1024; static const uint32_t kDefaultBlockSize = 4096; // Name of the default group in a metadata. static constexpr std::string_view kDefaultGroup = "default"; // Abstraction around dm-targets that can be encoded into logical partition tables. class Extent { public: Loading Loading @@ -196,12 +200,6 @@ class MetadataBuilder { return New(device_info, metadata_max_size, metadata_slot_count); } // Used by the test harness to override whether the device is "A/B". static void OverrideABForTesting(bool ab_device); // Used by the test harness to override whether the device is "retrofitting dynamic partitions". static void OverrideRetrofitDynamicParititonsForTesting(bool retrofit); // Define a new partition group. By default there is one group called // "default", with an unrestricted size. A non-zero size will restrict the // total space used by all partitions in the group. Loading Loading @@ -347,9 +345,6 @@ class MetadataBuilder { const std::vector<Interval>& free_list, uint64_t sectors_needed) const; static std::optional<bool> sABOverride; static std::optional<bool> sRetrofitDap; LpMetadataGeometry geometry_; LpMetadataHeader header_; std::vector<std::unique_ptr<Partition>> partitions_; Loading
fs_mgr/liblp/include/liblp/property_fetcher.h 0 → 100644 +42 −0 Original line number Diff line number Diff line // // Copyright (C) 2019 The Android Open Source Project // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // #pragma once #include <memory> namespace android { namespace fs_mgr { class IPropertyFetcher { public: virtual ~IPropertyFetcher() = default; virtual std::string GetProperty(const std::string& key, const std::string& defaultValue) = 0; virtual bool GetBoolProperty(const std::string& key, bool defaultValue) = 0; static IPropertyFetcher* GetInstance(); static void OverrideForTesting(std::unique_ptr<IPropertyFetcher>&&); }; class PropertyFetcher : public IPropertyFetcher { public: ~PropertyFetcher() = default; std::string GetProperty(const std::string& key, const std::string& defaultValue) override; bool GetBoolProperty(const std::string& key, bool defaultValue) override; }; } // namespace fs_mgr } // namespace android