(tmpnametemplate,mode,buffer_size,pback_size)
{ }
+ ~tmpfcopystreamTempl()
+ { close(); }
+
bool open(const std::string& filename,
std::ios_base::open_mode mode = std::ios_base::out,
int buffer_size = -1 , int pback_size = -1)
// TODO: filemode on close
+ // calling unlink is a safe way to abort, the original file is not overwritten then
+
private:
// forbid users to call these functions, no need to disturbe the internals here
- bool unlink()
- { return tmpfcopystreamTempl<Device, Tr, Alloc>::unlink(); }
-
bool move(const std::string& targetpath, bool overwrite=false)
- { return tmpfcopystreamTempl<Device, Tr, Alloc>::move(targetpath,overwrite); }
+ { return tmpfstreamTempl<Device, Tr, Alloc>::move(targetpath,overwrite); }
};
typedef tmpfstreamTempl<bio::file_descriptor_sink> tmpofstream;
template< typename Device, typename Tr, typename Alloc >
void tmpfcopystreamTempl<Device,Tr,Alloc>::close()
{
- // TODO
+ if (!tmpfcopystreamTempl<Device,Tr,Alloc>::is_open() ||
+ tmpfcopystreamTempl<Device,Tr,Alloc>::is_unlinked())
+ return;
+
// TODO full sync
+
+ // TODO chmod
+
+ // close the underlying filedescriptor
+ tmpfstreamTempl<Device,Tr,Alloc>::close();
+
+ move(get_original_filename(),true);
}
}
tmpf.close();
}
+BOOST_AUTO_TEST_CASE(Tmpfcopystream)
+{
+ write_file(".foobar","blah");
+
+ tmpfcopystream tmpf(".foobar");
+ BOOST_CHECK_EQUAL( true, tmpf.is_open() );
+
+ tmpf << "hello world";
+ tmpf.flush();
+
+ BOOST_CHECK_EQUAL( "blah", read_file(".foobar") );
+
+ tmpf.close();
+
+ BOOST_CHECK_EQUAL( "hello world", read_file(".foobar") );
+
+ unlink(".foobar");
+}
+
+BOOST_AUTO_TEST_CASE(TmpfcopystreamDestructor)
+{
+ write_file(".foobar","blah");
+
+ {
+ tmpfcopystream tmpf(".foobar");
+ BOOST_CHECK_EQUAL( true, tmpf.is_open() );
+
+ tmpf << "hello world";
+ tmpf.flush();
+
+ BOOST_CHECK_EQUAL( "blah", read_file(".foobar") );
+ }
+
+ BOOST_CHECK_EQUAL( "hello world", read_file(".foobar") );
+
+ unlink(".foobar");
+}
+
+BOOST_AUTO_TEST_CASE(TmpfcopystreamAbort)
+{
+ write_file(".foobar","blah");
+
+ tmpfcopystream tmpf(".foobar");
+ BOOST_CHECK_EQUAL( true, tmpf.is_open() );
+
+ tmpf << "hello world";
+ tmpf.flush();
+
+ // abort the overwriting, keep the old file
+ tmpf.unlink();
+
+ BOOST_CHECK_EQUAL( "blah", read_file(".foobar") );
+
+ unlink(".foobar");
+}
+
BOOST_AUTO_TEST_SUITE_END()