10#include "wvatomicfile.h"
11#include "wvfileutils.h"
15WvAtomicFile::WvAtomicFile(
WvStringParm filename,
int flags, mode_t create_mode)
18 open(filename, flags, create_mode);
21WvAtomicFile::~WvAtomicFile()
29bool WvAtomicFile::open(
WvStringParm filename,
int flags, mode_t create_mode)
33 atomic_file = filename;
37 if (lstat(atomic_file, &st) == 0 && !S_ISREG(st.st_mode))
40 WvString new_tmp_file(
"%s/WvXXXXXX", getdirname(filename));
44 mode_t old_umask = ::umask(077);
45 int tmp_fd = ::mkstemp(new_tmp_file.edit());
54 if (::fchmod(tmp_fd, create_mode & ~old_umask) != 0)
57 if (!WvFile::open(tmp_fd))
63 tmp_file = new_tmp_file;
75 if (::rename(tmp_file, atomic_file) != 0)
78 tmp_file = WvString::null;
83bool WvAtomicFile::chmod(mode_t mode)
85 if (
getfd() == -1)
return false;
87 if (fchmod(
getfd(), mode) != 0)
97bool WvAtomicFile::chown(uid_t owner, gid_t
group)
99 if (
getfd() == -1)
return false;
void close()
Closes the file descriptors.
A WvFastString acts exactly like a WvString, but can take (const char *) strings without needing to a...
int getfd() const
Returns the Unix file descriptor for reading and writing.
virtual void close()
Closes the file descriptors.
virtual void seterr(int _errnum)
Override seterr() from WvError so that it auto-closes the stream.
WvString is an implementation of a simple and efficient printable-string class.
Various little string functions.