mirror of
https://github.com/NixOS/nix
synced 2025-06-25 10:41:16 +02:00
Replace 'bool sync' with an enum for clarity
And drop writeFileAndSync().
This commit is contained in:
parent
479c356510
commit
a4b5584fb1
4 changed files with 21 additions and 36 deletions
|
@ -247,7 +247,7 @@ LocalStore::LocalStore(ref<const Config> config)
|
||||||
else if (curSchema == 0) { /* new store */
|
else if (curSchema == 0) { /* new store */
|
||||||
curSchema = nixSchemaVersion;
|
curSchema = nixSchemaVersion;
|
||||||
openDB(*state, true);
|
openDB(*state, true);
|
||||||
writeFileAndSync(schemaPath, fmt("%1%", curSchema), 0666);
|
writeFile(schemaPath, fmt("%1%", curSchema), 0666, FsSync::Yes);
|
||||||
}
|
}
|
||||||
|
|
||||||
else if (curSchema < nixSchemaVersion) {
|
else if (curSchema < nixSchemaVersion) {
|
||||||
|
@ -298,7 +298,7 @@ LocalStore::LocalStore(ref<const Config> config)
|
||||||
txn.commit();
|
txn.commit();
|
||||||
}
|
}
|
||||||
|
|
||||||
writeFileAndSync(schemaPath, fmt("%1%", nixSchemaVersion), 0666);
|
writeFile(schemaPath, fmt("%1%", nixSchemaVersion), 0666, FsSync::Yes);
|
||||||
|
|
||||||
lockFile(globalLock.get(), ltRead, true);
|
lockFile(globalLock.get(), ltRead, true);
|
||||||
}
|
}
|
||||||
|
|
|
@ -93,7 +93,7 @@ void restorePath(
|
||||||
{
|
{
|
||||||
switch (method) {
|
switch (method) {
|
||||||
case FileSerialisationMethod::Flat:
|
case FileSerialisationMethod::Flat:
|
||||||
writeFile(path, source, 0666, startFsync);
|
writeFile(path, source, 0666, startFsync ? FsSync::Yes : FsSync::No);
|
||||||
break;
|
break;
|
||||||
case FileSerialisationMethod::NixArchive:
|
case FileSerialisationMethod::NixArchive:
|
||||||
restorePath(path, source, startFsync);
|
restorePath(path, source, startFsync);
|
||||||
|
|
|
@ -304,7 +304,7 @@ void readFile(const Path & path, Sink & sink, bool memory_map)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void writeFile(const Path & path, std::string_view s, mode_t mode)
|
void writeFile(const Path & path, std::string_view s, mode_t mode, FsSync sync)
|
||||||
{
|
{
|
||||||
AutoCloseFD fd = toDescriptor(open(path.c_str(), O_WRONLY | O_TRUNC | O_CREAT
|
AutoCloseFD fd = toDescriptor(open(path.c_str(), O_WRONLY | O_TRUNC | O_CREAT
|
||||||
// TODO
|
// TODO
|
||||||
|
@ -315,40 +315,28 @@ void writeFile(const Path & path, std::string_view s, mode_t mode)
|
||||||
if (!fd)
|
if (!fd)
|
||||||
throw SysError("opening file '%1%'", path);
|
throw SysError("opening file '%1%'", path);
|
||||||
|
|
||||||
writeFile(fd, path, s, mode);
|
writeFile(fd, path, s, mode, sync);
|
||||||
|
|
||||||
/* Close explicitly to propagate the exceptions. */
|
/* Close explicitly to propagate the exceptions. */
|
||||||
fd.close();
|
fd.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
void writeFile(AutoCloseFD & fd, const Path & origPath, std::string_view s, mode_t mode)
|
void writeFile(AutoCloseFD & fd, const Path & origPath, std::string_view s, mode_t mode, FsSync sync)
|
||||||
{
|
{
|
||||||
assert(fd);
|
assert(fd);
|
||||||
try {
|
try {
|
||||||
writeFull(fd.get(), s);
|
writeFull(fd.get(), s);
|
||||||
|
|
||||||
|
if (sync == FsSync::Yes)
|
||||||
|
fd.fsync();
|
||||||
|
|
||||||
} catch (Error & e) {
|
} catch (Error & e) {
|
||||||
e.addTrace({}, "writing file '%1%'", origPath);
|
e.addTrace({}, "writing file '%1%'", origPath);
|
||||||
throw;
|
throw;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void writeFileAndSync(const Path & path, std::string_view s, mode_t mode)
|
void writeFile(const Path & path, Source & source, mode_t mode, FsSync sync)
|
||||||
{
|
|
||||||
{
|
|
||||||
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, path, s, mode);
|
|
||||||
fd.fsync();
|
|
||||||
/* Close explicitly to ensure that exceptions are propagated. */
|
|
||||||
fd.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
syncParent(path);
|
|
||||||
}
|
|
||||||
|
|
||||||
void writeFile(const Path & path, Source & source, mode_t mode, bool sync)
|
|
||||||
{
|
{
|
||||||
AutoCloseFD fd = toDescriptor(open(path.c_str(), O_WRONLY | O_TRUNC | O_CREAT
|
AutoCloseFD fd = toDescriptor(open(path.c_str(), O_WRONLY | O_TRUNC | O_CREAT
|
||||||
// TODO
|
// TODO
|
||||||
|
@ -372,11 +360,11 @@ void writeFile(const Path & path, Source & source, mode_t mode, bool sync)
|
||||||
e.addTrace({}, "writing file '%1%'", path);
|
e.addTrace({}, "writing file '%1%'", path);
|
||||||
throw;
|
throw;
|
||||||
}
|
}
|
||||||
if (sync)
|
if (sync == FsSync::Yes)
|
||||||
fd.fsync();
|
fd.fsync();
|
||||||
// Explicitly close to make sure exceptions are propagated.
|
// Explicitly close to make sure exceptions are propagated.
|
||||||
fd.close();
|
fd.close();
|
||||||
if (sync)
|
if (sync == FsSync::Yes)
|
||||||
syncParent(path);
|
syncParent(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -169,29 +169,26 @@ std::string readFile(const Path & path);
|
||||||
std::string readFile(const std::filesystem::path & path);
|
std::string readFile(const std::filesystem::path & path);
|
||||||
void readFile(const Path & path, Sink & sink, bool memory_map = true);
|
void readFile(const Path & path, Sink & sink, bool memory_map = true);
|
||||||
|
|
||||||
|
enum struct FsSync { Yes, No };
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Write a string to a file.
|
* Write a string to a file.
|
||||||
*/
|
*/
|
||||||
void writeFile(const Path & path, std::string_view s, mode_t mode = 0666);
|
void writeFile(const Path & path, std::string_view s, mode_t mode = 0666, FsSync sync = FsSync::No);
|
||||||
|
|
||||||
static inline void writeFile(const std::filesystem::path & path, std::string_view s, mode_t mode = 0666)
|
static inline void writeFile(const std::filesystem::path & path, std::string_view s, mode_t mode = 0666, FsSync sync = FsSync::No)
|
||||||
{
|
{
|
||||||
return writeFile(path.string(), s, mode);
|
return writeFile(path.string(), s, mode, sync);
|
||||||
}
|
}
|
||||||
|
|
||||||
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, FsSync sync = FsSync::No);
|
||||||
|
|
||||||
static inline void writeFile(const std::filesystem::path & path, Source & source, mode_t mode = 0666, bool sync = false)
|
static inline void writeFile(const std::filesystem::path & path, Source & source, mode_t mode = 0666, FsSync sync = FsSync::No)
|
||||||
{
|
{
|
||||||
return writeFile(path.string(), source, mode, sync);
|
return writeFile(path.string(), source, mode, sync);
|
||||||
}
|
}
|
||||||
|
|
||||||
void writeFile(AutoCloseFD & fd, const Path & origPath, std::string_view s, mode_t mode = 0666);
|
void writeFile(AutoCloseFD & fd, const Path & origPath, std::string_view s, mode_t mode = 0666, FsSync sync = FsSync::No);
|
||||||
|
|
||||||
/**
|
|
||||||
* 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 path's parent directory to disk.
|
* Flush a path's parent directory to disk.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue