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;