mirror of
https://github.com/NixOS/nix
synced 2025-06-25 14:51:16 +02:00
libutil: writeFile variant for file descriptors
`writeFile` lose its `sync` boolean flag to make things simpler. A new `writeFileAndSync` function is created and all call sites are converted to it. Change-Id: Ib871a5283a9c047db1e4fe48a241506e4aab9192 Signed-off-by: Raito Bezarius <raito@lix.systems>
This commit is contained in:
parent
002d202653
commit
034f59bbb9
3 changed files with 39 additions and 13 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);
|
||||||
writeFile(schemaPath, fmt("%1%", curSchema), 0666, true);
|
writeFileAndSync(schemaPath, fmt("%1%", curSchema), 0666);
|
||||||
}
|
}
|
||||||
|
|
||||||
else if (curSchema < nixSchemaVersion) {
|
else if (curSchema < nixSchemaVersion) {
|
||||||
|
@ -298,7 +298,7 @@ LocalStore::LocalStore(ref<const Config> config)
|
||||||
txn.commit();
|
txn.commit();
|
||||||
}
|
}
|
||||||
|
|
||||||
writeFile(schemaPath, fmt("%1%", nixSchemaVersion), 0666, true);
|
writeFileAndSync(schemaPath, fmt("%1%", nixSchemaVersion), 0666);
|
||||||
|
|
||||||
lockFile(globalLock.get(), ltRead, true);
|
lockFile(globalLock.get(), ltRead, true);
|
||||||
}
|
}
|
||||||
|
|
|
@ -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, bool sync)
|
void writeFile(const Path & path, std::string_view s, mode_t mode)
|
||||||
{
|
{
|
||||||
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
|
||||||
|
@ -314,20 +314,39 @@ void writeFile(const Path & path, std::string_view s, mode_t mode, bool sync)
|
||||||
, mode));
|
, mode));
|
||||||
if (!fd)
|
if (!fd)
|
||||||
throw SysError("opening file '%1%'", path);
|
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 {
|
try {
|
||||||
writeFull(fd.get(), s);
|
writeFull(fd.get(), s);
|
||||||
} catch (Error & e) {
|
} catch (Error & e) {
|
||||||
e.addTrace({}, "writing file '%1%'", path);
|
e.addTrace({}, "writing file '%1%'", fd.guessOrInventPath());
|
||||||
throw;
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
void writeFile(const Path & path, Source & source, mode_t mode, bool sync)
|
void writeFile(const Path & path, Source & source, mode_t mode, bool sync)
|
||||||
{
|
{
|
||||||
|
|
|
@ -172,10 +172,10 @@ void readFile(const Path & path, Sink & sink, bool memory_map = true);
|
||||||
/**
|
/**
|
||||||
* 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, bool sync = false);
|
void writeFile(const 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, bool sync = false)
|
static inline void writeFile(const std::filesystem::path & path, std::string_view s, mode_t mode = 0666)
|
||||||
{
|
{
|
||||||
return writeFile(path.string(), s, mode, sync);
|
return writeFile(path.string(), s, mode);
|
||||||
}
|
}
|
||||||
|
|
||||||
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, bool sync = false);
|
||||||
|
@ -184,6 +184,13 @@ static inline void writeFile(const std::filesystem::path & path, Source & source
|
||||||
return writeFile(path.string(), source, mode, sync);
|
return writeFile(path.string(), source, mode, sync);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void writeFile(AutoCloseFD & fd, std::string_view s, 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 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