Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit c98b1efd authored by David Anderson's avatar David Anderson Committed by Gerrit Code Review
Browse files

Merge "Expose IsFilePinned through FiemapWriter."

parents e72308c6 c7ca3b67
Loading
Loading
Loading
Loading
+15 −0
Original line number Original line Diff line number Diff line
@@ -359,6 +359,21 @@ static bool IsFilePinned(int file_fd, const std::string& file_path, uint32_t fs_
    return moved_blocks_nr == 0;
    return moved_blocks_nr == 0;
}
}


bool FiemapWriter::HasPinnedExtents(const std::string& file_path) {
    android::base::unique_fd fd(open(file_path.c_str(), O_NOFOLLOW | O_CLOEXEC | O_RDONLY));
    if (fd < 0) {
        PLOG(ERROR) << "open: " << file_path;
        return false;
    }

    struct statfs64 sfs;
    if (fstatfs64(fd, &sfs)) {
        PLOG(ERROR) << "fstatfs64: " << file_path;
        return false;
    }
    return IsFilePinned(fd, file_path, sfs.f_type);
}

static void LogExtent(uint32_t num, const struct fiemap_extent& ext) {
static void LogExtent(uint32_t num, const struct fiemap_extent& ext) {
    LOG(INFO) << "Extent #" << num;
    LOG(INFO) << "Extent #" << num;
    LOG(INFO) << "  fe_logical:  " << ext.fe_logical;
    LOG(INFO) << "  fe_logical:  " << ext.fe_logical;
+8 −0
Original line number Original line Diff line number Diff line
@@ -51,6 +51,8 @@ class FiemapWriterTest : public ::testing::Test {
        testfile = ::android::base::StringPrintf("%s/testdata/%s", exec_dir.c_str(), tinfo->name());
        testfile = ::android::base::StringPrintf("%s/testdata/%s", exec_dir.c_str(), tinfo->name());
    }
    }


    void TearDown() override { unlink(testfile.c_str()); }

    // name of the file we use for testing
    // name of the file we use for testing
    std::string testfile;
    std::string testfile;
};
};
@@ -102,6 +104,12 @@ TEST_F(FiemapWriterTest, CheckProgress) {
    EXPECT_EQ(invocations, 2);
    EXPECT_EQ(invocations, 2);
}
}


TEST_F(FiemapWriterTest, CheckPinning) {
    auto ptr = FiemapWriter::Open(testfile, 4096);
    ASSERT_NE(ptr, nullptr);
    EXPECT_TRUE(FiemapWriter::HasPinnedExtents(testfile));
}

TEST_F(FiemapWriterTest, CheckBlockDevicePath) {
TEST_F(FiemapWriterTest, CheckBlockDevicePath) {
    FiemapUniquePtr fptr = FiemapWriter::Open(testfile, 4096);
    FiemapUniquePtr fptr = FiemapWriter::Open(testfile, 4096);
    EXPECT_EQ(fptr->size(), 4096);
    EXPECT_EQ(fptr->size(), 4096);
+12 −0
Original line number Original line Diff line number Diff line
@@ -45,6 +45,18 @@ class FiemapWriter final {
                                bool create = true,
                                bool create = true,
                                std::function<bool(uint64_t, uint64_t)> progress = {});
                                std::function<bool(uint64_t, uint64_t)> progress = {});


    // Check that a file still has the same extents since it was last opened with FiemapWriter,
    // assuming the file was not resized outside of FiemapWriter. Returns false either on error
    // or if the file was not pinned.
    //
    // This will always return true on Ext4. On F2FS, it will return true if either of the
    // following cases are true:
    //   - The file was never pinned.
    //   - The file is pinned and has not been moved by the GC.
    // Thus, this method should only be called for pinned files (such as those returned by
    // FiemapWriter::Open).
    static bool HasPinnedExtents(const std::string& file_path);

    // Syncs block device writes.
    // Syncs block device writes.
    bool Flush() const;
    bool Flush() const;