diff --git a/lix/libstore/local-store.cc b/lix/libstore/local-store.cc index 6426d9bf5..62c04173d 100644 --- a/lix/libstore/local-store.cc +++ b/lix/libstore/local-store.cc @@ -173,7 +173,7 @@ static void migrateCASchema(SQLite& db, Path schemaPath, AutoCloseFD& lockFd, Ne txn.commit(); } - writeFile(schemaPath, fmt("%d", nixCASchemaVersion), 0666, true); + writeFileAndSync(schemaPath, fmt("%d", nixCASchemaVersion), 0666); lockFile(lockFd.get(), ltRead); } } @@ -315,7 +315,7 @@ void LocalStore::initDB(DBState & state) else if (curSchema == 0) { /* new store */ curSchema = nixSchemaVersion; openDB(state, true); - writeFile(schemaPath, fmt("%1%", nixSchemaVersion), 0666, true); + writeFileAndSync(schemaPath, fmt("%1%", nixSchemaVersion), 0666); } else if (curSchema < nixSchemaVersion) { @@ -368,7 +368,7 @@ void LocalStore::initDB(DBState & state) txn.commit(); } - writeFile(schemaPath, fmt("%1%", nixSchemaVersion), 0666, true); + writeFileAndSync(schemaPath, fmt("%1%", nixSchemaVersion), 0666); lockFile(globalLock.get(), ltRead, always_progresses); } diff --git a/lix/libutil/file-system.cc b/lix/libutil/file-system.cc index b29361719..d51536dd8 100644 --- a/lix/libutil/file-system.cc +++ b/lix/libutil/file-system.cc @@ -359,25 +359,44 @@ Generator readFileSource(const Path & path) } -void writeFile(const Path & path, std::string_view s, mode_t mode, bool sync) +void writeFile(const Path & path, std::string_view s, mode_t mode) { AutoCloseFD fd{open(path.c_str(), O_WRONLY | O_TRUNC | O_CREAT | O_CLOEXEC, mode)}; if (!fd) throw SysError("opening file '%1%'", path); + + writeFile(fd, s, mode); + + /* Close explicitly to propagate the exceptions. */ + fd.close(); +} + +void writeFile(AutoCloseFD & fd, std::string_view s, mode_t mode) +{ + assert(fd); try { writeFull(fd.get(), s); } catch (Error & e) { - e.addTrace({}, "writing file '%1%'", path); + e.addTrace({}, "writing file '%1%'", fd.guessOrInventPath()); throw; } - if (sync) - fd.fsync(); - // Explicitly close to make sure exceptions are propagated. - fd.close(); - if (sync) - syncParent(path); } +void writeFileAndSync(const Path & path, std::string_view s, mode_t mode) +{ + { + AutoCloseFD fd{open(path.c_str(), O_WRONLY | O_TRUNC | O_CREAT | O_CLOEXEC, mode)}; + if (!fd) + throw SysError("opening file '%1%'", path); + + writeFile(fd, s, mode); + fd.fsync(); + /* Close explicitly to ensure that exceptions are propagated. */ + fd.close(); + } + + syncParent(path); +} static AutoCloseFD openForWrite(const Path & path, mode_t mode) { @@ -397,7 +416,7 @@ static void closeForWrite(const Path & path, AutoCloseFD & fd, bool sync) syncParent(path); } -void writeFile(const Path & path, Source & source, mode_t mode, bool sync) +void writeFile(const Path & path, Source & source, mode_t mode) { AutoCloseFD fd = openForWrite(path, mode); @@ -414,11 +433,11 @@ void writeFile(const Path & path, Source & source, mode_t mode, bool sync) e.addTrace({}, "writing file '%1%'", path); throw; } - closeForWrite(path, fd, sync); + closeForWrite(path, fd, false); } kj::Promise> -writeFile(const Path & path, AsyncInputStream & source, mode_t mode, bool sync) +writeFile(const Path & path, AsyncInputStream & source, mode_t mode) try { AutoCloseFD fd = openForWrite(path, mode); @@ -436,7 +455,7 @@ try { e.addTrace({}, "writing file '%1%'", path); throw; } - closeForWrite(path, fd, sync); + closeForWrite(path, fd, false); co_return result::success(); } catch (...) { co_return result::current_exception(); diff --git a/lix/libutil/file-system.hh b/lix/libutil/file-system.hh index 67f7102eb..a1295c613 100644 --- a/lix/libutil/file-system.hh +++ b/lix/libutil/file-system.hh @@ -189,11 +189,18 @@ Generator readFileSource(const Path & path); /** * Write a string to a file. */ -void writeFile(const Path & path, std::string_view s, mode_t mode = 0666, bool sync = false); +void writeFile(const Path & path, std::string_view s, mode_t mode = 0666); -void writeFile(const Path & path, Source & source, mode_t mode = 0666, bool sync = false); +void writeFile(const Path & path, Source & source, mode_t mode = 0666); + +void writeFile(AutoCloseFD & fd, std::string_view s, mode_t mode = 0666); kj::Promise> -writeFile(const Path & path, AsyncInputStream & source, mode_t mode = 0666, bool sync = false); +writeFile(const Path & path, AsyncInputStream & source, mode_t mode = 0666); + +/** + * Write a string to a file and flush the file and its parents direcotry to disk. + */ +void writeFileAndSync(const Path & path, std::string_view s, mode_t mode = 0666); /** * Flush a file's parent directory to disk