From 9215c35975be745628e8188473154c7e476add55 Mon Sep 17 00:00:00 2001 From: Ben Longbons Date: Tue, 4 Feb 2014 23:18:54 -0800 Subject: Wrap remaining FDs in a class --- src/io/lock.cpp | 41 +++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 20 deletions(-) (limited to 'src/io/lock.cpp') 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) {} -- cgit v1.2.3-60-g2f50