/**
+ * @brief calls fsync on a given directory to sync all it's metadata
+ * @param path the path of the directory.
+ * @return true if successful
+ */
+bool dirsync(const std::string& path)
+{
+ // sync the directory the file is in
+ DIR* dir=opendir(path.c_str());
+ if (dir == NULL)
+ return false;
+
+ int ret=fsync(dirfd(dir));
+
+ closedir(dir);
+
+ return (ret==0);
+}
+
+/**
* @brief changes the file(/path) mode.
* @param path the path to change the mode for.
* @param mode the new file mode.
std::string normalize_path(const std::string& path);
+bool dirsync(const std::string& path);
+
bool chmod(const std::string& path, int mode);
bool chown(const std::string& path, const I2n::User& user, const I2n::Group& group= I2n::Group());
if (mode & std::ios_base::app)
flags |= O_APPEND;
- int fd=mkostemp(chbuf,flags);
+ fd=mkostemp(chbuf,flags);
tmpfilename=chbuf;
delete[] chbuf;
}
template< typename Device, typename Tr, typename Alloc >
+bool tmpfstreamTempl<Device,Tr,Alloc>::sync()
+{
+ if (!tmpfstreamTempl<Device,Tr,Alloc>::is_open())
+ return false;
+
+ tmpfstreamTempl<Device,Tr,Alloc>::flush();
+
+ // sync the file itself
+ if (fsync(fd) != 0)
+ return false;
+
+ // sync the dir: both is needed for data + metadata sync
+ return dirsync(dirname(get_tmp_filename()));
+}
+
+template< typename Device, typename Tr, typename Alloc >
void tmpfcopystreamTempl<Device,Tr,Alloc>::close()
{
if (!tmpfstreamTempl<Device,Tr,Alloc>::is_open() ||
tmpfstreamTempl<Device,Tr,Alloc>::is_unlinked())
return;
- // TODO full sync
+ if (get_full_sync())
+ tmpfstreamTempl<Device,Tr,Alloc>::sync();
// close the underlying filedescriptor
tmpfstreamTempl<Device,Tr,Alloc>::close();
tmpfstreamTempl<Device,Tr,Alloc>::set_file_mode(filemode_on_close);
move(get_original_filename(),true);
+
+ if (get_full_sync())
+ dirsync(dirname(get_original_filename()));
}
}
BOOST_CHECK_EQUAL( true, stat.is_regular_file() );
tmpf << "hello world" << endl;
- tmpf.flush();
+ BOOST_CHECK_EQUAL( true, tmpf.sync() );
BOOST_CHECK_EQUAL( true, tmpf.move(".foobar",false) );