10 #include "wvatomicfile.h"
11 #include "wvfileutils.h"
16 WvAtomicFile::WvAtomicFile(
WvStringParm filename,
int flags, mode_t create_mode)
19 open(filename, flags, create_mode);
22 WvAtomicFile::~WvAtomicFile()
30 bool WvAtomicFile::open(
WvStringParm filename,
int flags, mode_t create_mode)
34 atomic_file = filename;
38 if (lstat(atomic_file, &st) == 0 && !S_ISREG(st.st_mode))
41 WvString new_tmp_file(
"%s/WvXXXXXX", getdirname(filename));
45 mode_t old_umask = ::umask(077);
46 int tmp_fd = ::mkstemp(new_tmp_file.edit());
55 if (::fchmod(tmp_fd, create_mode & ~old_umask) != 0)
58 if (!WvFile::open(tmp_fd))
64 tmp_file = new_tmp_file;
76 if (::rename(tmp_file, atomic_file) != 0)
79 tmp_file = WvString::null;
84 bool WvAtomicFile::chmod(mode_t mode)
86 if (
getfd() == -1)
return false;
88 if (fchmod(
getfd(), mode) != 0)
98 bool WvAtomicFile::chown(uid_t owner, gid_t
group)
100 if (
getfd() == -1)
return false;
102 if (fchown(
getfd(), owner, group) != 0)
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.
void close()
Closes the file descriptors.
virtual void close()
Closes the file descriptors.
virtual void seterr(int _errnum)
Override seterr() from WvError so that it auto-closes the stream.
Various little string functions.
WvString is an implementation of a simple and efficient printable-string class.