summaryrefslogtreecommitdiff
path: root/src/io/lock.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/io/lock.cpp')
-rw-r--r--src/io/lock.cpp41
1 files changed, 21 insertions, 20 deletions
diff --git a/src/io/lock.cpp b/src/io/lock.cpp
index 823b168..c7cbda8 100644
--- a/src/io/lock.cpp
+++ b/src/io/lock.cpp
@@ -23,7 +23,8 @@
#include "../strings/zstring.hpp"
-#include "../io/cxxstdio.hpp"
+#include "cxxstdio.hpp"
+#include "fd.hpp"
#include "../poison.hpp"
@@ -35,29 +36,29 @@ const int backup_count = 10;
/// Protected file writing
/// (Until the file is closed, it keeps the old file)
-// Start writing a tmpfile
-static
-int get_lock_open(ZString filename, int *info)
+namespace io
{
- int fd;
- int no = getpid();
-
- // Get a filename that doesn't already exist
- FString newfile;
- do
+ // Start writing a tmpfile
+ static
+ FD get_lock_open(ZString filename, int *info)
{
- newfile = STRPRINTF("%s_%d.tmp", filename, no++);
- fd = open(newfile.c_str(), O_WRONLY | O_CREAT | O_EXCL, 0666);
+ FD fd;
+ int no = getpid();
+
+ // Get a filename that doesn't already exist
+ FString newfile;
+ do
+ {
+ newfile = STRPRINTF("%s_%d.tmp", filename, no++);
+ fd = FD::open(newfile, O_WRONLY | O_CREAT | O_EXCL, 0666);
+ }
+ while (fd == FD() && errno == EEXIST);
+ if (fd == FD())
+ abort();
+ *info = --no;
+ return fd;
}
- while (fd == -1 && errno == EEXIST);
- if (fd == -1)
- abort();
- *info = --no;
- return fd;
-}
-namespace io
-{
WriteLock::WriteLock(FString fn, bool linebuffered)
: WriteFile(get_lock_open(fn, &tmp_suffix), linebuffered), filename(fn)
{}