working version of tmpfcopystream
authorGerd von Egidy <gerd.von.egidy@intra2net.com>
Thu, 11 Mar 2010 09:40:29 +0000 (10:40 +0100)
committerGerd von Egidy <gerd.von.egidy@intra2net.com>
Thu, 11 Mar 2010 09:40:29 +0000 (10:40 +0100)
src/tmpfstream.hpp
src/tmpfstream_impl.hpp
test/test_tmpfstream.cpp

index 80ad75a..31f1d7a 100644 (file)
@@ -110,6 +110,9 @@ public:
             (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)
@@ -142,14 +145,13 @@ public:
 
     // 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;
index 0526c4a..75dd6a4 100644 (file)
@@ -125,8 +125,18 @@ bool tmpfstreamTempl<Device,Tr,Alloc>::move(const std::string& targetpath,
 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);
 }
 
 }
index 1d61719..ca92c49 100644 (file)
@@ -236,4 +236,60 @@ BOOST_AUTO_TEST_CASE(TmpfstreamReadWrite)
     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()