From dfd250636158dc7363ecc7aa734df17cb79ec279 Mon Sep 17 00:00:00 2001 From: ldy Date: Wed, 30 Apr 2025 08:02:28 +0000 Subject: [PATCH] =?UTF-8?q?writer=E6=94=AF=E6=8C=81sync?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: ldy --- services/applypatch/data_writer.cpp | 6 ++++++ services/applypatch/raw_writer.cpp | 14 ++++++++++++++ services/applypatch/raw_writer.h | 2 +- services/include/applypatch/data_writer.h | 1 + 4 files changed, 22 insertions(+), 1 deletion(-) diff --git a/services/applypatch/data_writer.cpp b/services/applypatch/data_writer.cpp index a42ac3df..3accdfdc 100644 --- a/services/applypatch/data_writer.cpp +++ b/services/applypatch/data_writer.cpp @@ -56,6 +56,12 @@ int DataWriter::OpenPath(const std::string &path) return fd; } +bool DataWriter::Sync(void) +{ + LOG(WARNING) << "sync unimplemented"; + return true; +} + std::unique_ptr DataWriter::CreateDataWriter(WriteMode mode, const std::string &path, uint64_t offset) { diff --git a/services/applypatch/raw_writer.cpp b/services/applypatch/raw_writer.cpp index 50bdfcc2..dae3d589 100644 --- a/services/applypatch/raw_writer.cpp +++ b/services/applypatch/raw_writer.cpp @@ -77,4 +77,18 @@ int RawWriter::WriteInternal(int fd, const uint8_t *data, size_t len) offset_ += static_cast(len); return 0; } + +bool RawWriter::Sync(void) +{ + if (fd_ < 0) { + LOG(ERROR) << "invalid fd " << fd_; + return false; + } + if (fsync(fd_) != 0) { + LOG(ERROR) << "fsync failed, errno is " << errno; + return false; + } + LOG(INFO) << "fsync successfully " << fd_; + return true; +} } // namespace Updater diff --git a/services/applypatch/raw_writer.h b/services/applypatch/raw_writer.h index 2057225c..f5d7a350 100644 --- a/services/applypatch/raw_writer.h +++ b/services/applypatch/raw_writer.h @@ -26,7 +26,7 @@ namespace Updater { class RawWriter : public DataWriter { public: bool Write(const uint8_t *addr, size_t len, const void *context) override; - + bool Sync(void) override; RawWriter(const std::string path, uint64_t offset) : fd_(-1), path_(path), offset_(offset) {} RawWriter(const std::string path, uint64_t startAddr, uint64_t offset) : fd_(-1), path_(path), offset_(startAddr + offset) {} diff --git a/services/include/applypatch/data_writer.h b/services/include/applypatch/data_writer.h index 21503f41..9df5d81f 100644 --- a/services/include/applypatch/data_writer.h +++ b/services/include/applypatch/data_writer.h @@ -35,6 +35,7 @@ public: using WriterConstructor = std::unique_ptr (*)(const std::string &, const std::string &, uint64_t, uint64_t); virtual bool Write(const uint8_t *addr, size_t len, const void *context) = 0; + virtual bool Sync(void); virtual ~DataWriter() {} static std::unique_ptr CreateDataWriter(WriteMode mode, const std::string &path, UpdaterEnv *env, uint64_t offset = 0); -- Gitee