diff options
-rw-r--r-- | fs_mgr/libdm/dm_target.cpp | 43 | ||||
-rw-r--r-- | fs_mgr/libdm/dm_test.cpp | 27 |
2 files changed, 46 insertions, 24 deletions
diff --git a/fs_mgr/libdm/dm_target.cpp b/fs_mgr/libdm/dm_target.cpp index da1013e44..1a483ecaf 100644 --- a/fs_mgr/libdm/dm_target.cpp +++ b/fs_mgr/libdm/dm_target.cpp @@ -16,6 +16,7 @@ #include "libdm/dm_target.h" +#include <inttypes.h> #include <stdio.h> #include <sys/types.h> @@ -165,35 +166,29 @@ bool DmTargetSnapshot::ReportsOverflow(const std::string& target_type) { } bool DmTargetSnapshot::ParseStatusText(const std::string& text, Status* status) { - auto sections = android::base::Split(text, " "); - if (sections.size() == 1) { - // This is probably an error code, "Invalid" is possible as is "Overflow" - // on 4.4+. - status->error = text; + // Try to parse the line as it should be + int args = sscanf(text.c_str(), "%" PRIu64 "/%" PRIu64 " %" PRIu64, &status->sectors_allocated, + &status->total_sectors, &status->metadata_sectors); + if (args == 3) { return true; } - if (sections.size() != 2) { - LOG(ERROR) << "snapshot status should have two components"; - return false; - } - auto sector_info = android::base::Split(sections[0], "/"); - if (sector_info.size() != 2) { - LOG(ERROR) << "snapshot sector info should have two components"; - return false; - } - if (!android::base::ParseUint(sections[1], &status->metadata_sectors)) { - LOG(ERROR) << "could not parse metadata sectors"; - return false; - } - if (!android::base::ParseUint(sector_info[0], &status->sectors_allocated)) { - LOG(ERROR) << "could not parse sectors allocated"; + auto sections = android::base::Split(text, " "); + if (sections.size() == 0) { + LOG(ERROR) << "could not parse empty status"; return false; } - if (!android::base::ParseUint(sector_info[1], &status->total_sectors)) { - LOG(ERROR) << "could not parse total sectors"; - return false; + // Error codes are: "Invalid", "Overflow" and "Merge failed" + if (sections.size() == 1) { + if (text == "Invalid" || text == "Overflow") { + status->error = text; + return true; + } + } else if (sections.size() == 2 && text == "Merge failed") { + status->error = text; + return true; } - return true; + LOG(ERROR) << "could not parse snapshot status: wrong format"; + return false; } std::string DmTargetCrypt::GetParameterString() const { diff --git a/fs_mgr/libdm/dm_test.cpp b/fs_mgr/libdm/dm_test.cpp index da1c4a9b0..16be0d5fc 100644 --- a/fs_mgr/libdm/dm_test.cpp +++ b/fs_mgr/libdm/dm_test.cpp @@ -456,6 +456,33 @@ TEST(libdm, DmSnapshotOverflow) { } } +TEST(libdm, ParseStatusText) { + DmTargetSnapshot::Status status; + + // Bad inputs + EXPECT_FALSE(DmTargetSnapshot::ParseStatusText("", &status)); + EXPECT_FALSE(DmTargetSnapshot::ParseStatusText("X", &status)); + EXPECT_FALSE(DmTargetSnapshot::ParseStatusText("123", &status)); + EXPECT_FALSE(DmTargetSnapshot::ParseStatusText("123/456", &status)); + EXPECT_FALSE(DmTargetSnapshot::ParseStatusText("123 456", &status)); + EXPECT_FALSE(DmTargetSnapshot::ParseStatusText("123 456", &status)); + EXPECT_FALSE(DmTargetSnapshot::ParseStatusText("123 456 789", &status)); + EXPECT_FALSE(DmTargetSnapshot::ParseStatusText("123 456/789", &status)); + EXPECT_FALSE(DmTargetSnapshot::ParseStatusText("123/456/789", &status)); + EXPECT_FALSE(DmTargetSnapshot::ParseStatusText("123 / 456 789", &status)); + + // Good input + EXPECT_TRUE(DmTargetSnapshot::ParseStatusText("123/456 789", &status)); + EXPECT_EQ(status.sectors_allocated, 123); + EXPECT_EQ(status.total_sectors, 456); + EXPECT_EQ(status.metadata_sectors, 789); + + // Known error codes + EXPECT_TRUE(DmTargetSnapshot::ParseStatusText("Invalid", &status)); + EXPECT_TRUE(DmTargetSnapshot::ParseStatusText("Merge failed", &status)); + EXPECT_TRUE(DmTargetSnapshot::ParseStatusText("Overflow", &status)); +} + TEST(libdm, CryptArgs) { DmTargetCrypt target1(0, 512, "sha1", "abcdefgh", 50, "/dev/loop0", 100); ASSERT_EQ(target1.name(), "crypt"); |