10 #include "wvatomicfile.h"
11 #include "wvfileutils.h"
15 WvAtomicFile::WvAtomicFile(
WvStringParm filename,
int flags, mode_t create_mode)
18 open(filename, flags, create_mode);
21 WvAtomicFile::~WvAtomicFile()
29 bool 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;
83 bool WvAtomicFile::chmod(mode_t mode)
85 if (
getfd() == -1)
return false;
87 if (fchmod(
getfd(), mode) != 0)
97 bool WvAtomicFile::chown(uid_t owner, gid_t
group)
99 if (
getfd() == -1)
return false;