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

Commit 1022f0de authored by Tom Cherry's avatar Tom Cherry Committed by Gerrit Code Review
Browse files

Merge "init: fsync() persistent properties directory after rename()"

parents 7bf14bb9 97437a7a
Loading
Loading
Loading
Loading
+14 −1
Original line number Diff line number Diff line
@@ -31,10 +31,11 @@

#include "util.h"

using android::base::Dirname;
using android::base::ReadFdToString;
using android::base::StartsWith;
using android::base::WriteStringToFd;
using android::base::unique_fd;
using android::base::WriteStringToFd;

namespace android {
namespace init {
@@ -191,6 +192,18 @@ Result<void> WritePersistentPropertyFile(const PersistentProperties& persistent_
        unlink(temp_filename.c_str());
        return Error(saved_errno) << "Unable to rename persistent property file";
    }

    // rename() is atomic with regards to the kernel's filesystem buffers, but the parent
    // directories must be fsync()'ed otherwise, the rename is not necessarily written to storage.
    // Note in this case, that the source and destination directories are the same, so only one
    // fsync() is required.
    auto dir = Dirname(persistent_property_filename);
    auto dir_fd = unique_fd{open(dir.c_str(), O_DIRECTORY | O_RDONLY)};
    if (dir_fd < 0) {
        return ErrnoError() << "Unable to open persistent properties directory for fsync()";
    }
    fsync(dir_fd);

    return {};
}