implement filemode_on_close, fix var initialization
authorGerd von Egidy <gerd.von.egidy@intra2net.com>
Thu, 11 Mar 2010 10:40:51 +0000 (11:40 +0100)
committerGerd von Egidy <gerd.von.egidy@intra2net.com>
Thu, 11 Mar 2010 10:40:51 +0000 (11:40 +0100)
src/tmpfstream.hpp
src/tmpfstream_impl.hpp
test/test_tmpfstream.cpp

index 31f1d7a..e4aac49 100644 (file)
@@ -43,13 +43,13 @@ private:
 
 public:
     tmpfstreamTempl()
-        : bio::stream<Device, Tr, Alloc>()
+        : unlinked(false), bio::stream<Device, Tr, Alloc>()
         { }
 
     tmpfstreamTempl(const std::string& tmpnametemplate, 
                std::ios_base::open_mode mode = std::ios_base::out,
                int buffer_size = -1 , int pback_size = -1)
-        : bio::stream<Device, Tr, Alloc>()
+        : unlinked(false), bio::stream<Device, Tr, Alloc>()
     {
         open(tmpnametemplate,mode,buffer_size,pback_size);
     }
@@ -85,18 +85,25 @@ class tmpfcopystreamTempl : public tmpfstreamTempl<Device, Tr, Alloc>
 private:
     std::string originalfilename;
     bool full_sync;
+    int filemode_on_close;
 
 public:
     static const std::string default_template_suffix;
+    static const bool default_full_sync=false;
+    static const int default_filemode_on_close=0644;
 
     tmpfcopystreamTempl()
-        : tmpfstreamTempl<Device, Tr, Alloc>()
+        : full_sync(default_full_sync),
+          filemode_on_close(default_filemode_on_close),
+          tmpfstreamTempl<Device, Tr, Alloc>()
         { }
 
     tmpfcopystreamTempl(const std::string& filename,
                std::ios_base::open_mode mode = std::ios_base::out,
                int buffer_size = -1 , int pback_size = -1)
-        : originalfilename(filename),
+        : full_sync(default_full_sync),
+          filemode_on_close(default_filemode_on_close),
+          originalfilename(filename),
           tmpfstreamTempl<Device, Tr, Alloc>
             (filename+default_template_suffix,mode,buffer_size,pback_size)
         { }
@@ -105,7 +112,9 @@ public:
                const std::string& tmpnametemplate,
                std::ios_base::open_mode mode = std::ios_base::out,
                int buffer_size = -1 , int pback_size = -1)
-        : originalfilename(filename), 
+        : full_sync(default_full_sync),
+          filemode_on_close(default_filemode_on_close),
+          originalfilename(filename), 
           tmpfstreamTempl<Device, Tr, Alloc>
             (tmpnametemplate,mode,buffer_size,pback_size)
         { }
@@ -132,6 +141,8 @@ public:
             ::open(tmpnametemplate,mode,buffer_size,pback_size);
     }
 
+    void close();
+
     std::string get_original_filename()
         { return originalfilename; }
 
@@ -141,11 +152,14 @@ public:
     bool get_full_sync()
         { return full_sync; }
 
-    void close();
+    void set_filemode_on_close(int mode)
+        { filemode_on_close=mode; }
 
-    // TODO: filemode on close
+    int get_filemode_on_close()
+        { return filemode_on_close; }
 
-    // calling unlink is a safe way to abort, the original file is not overwritten then
+    // calling unlink is a safe way to abort, 
+    // the original file is not overwritten then
 
 private:
 
index 75dd6a4..3534178 100644 (file)
@@ -125,17 +125,17 @@ bool tmpfstreamTempl<Device,Tr,Alloc>::move(const std::string& targetpath,
 template< typename Device, typename Tr, typename Alloc >
 void tmpfcopystreamTempl<Device,Tr,Alloc>::close()
 {
-    if (!tmpfcopystreamTempl<Device,Tr,Alloc>::is_open() || 
-         tmpfcopystreamTempl<Device,Tr,Alloc>::is_unlinked())
+    if (!tmpfstreamTempl<Device,Tr,Alloc>::is_open() || 
+         tmpfstreamTempl<Device,Tr,Alloc>::is_unlinked())
         return;
 
     // TODO full sync
 
-    // TODO chmod
-
     // close the underlying filedescriptor
     tmpfstreamTempl<Device,Tr,Alloc>::close();
 
+    tmpfstreamTempl<Device,Tr,Alloc>::set_file_mode(filemode_on_close);
+
     move(get_original_filename(),true);
 }
 
index ca92c49..6b3782a 100644 (file)
@@ -249,6 +249,7 @@ BOOST_AUTO_TEST_CASE(Tmpfcopystream)
     BOOST_CHECK_EQUAL( "blah", read_file(".foobar") );
 
     tmpf.close();
+    BOOST_CHECK_EQUAL( false, tmpf.is_open() );
 
     BOOST_CHECK_EQUAL( "hello world", read_file(".foobar") );
 
@@ -292,4 +293,30 @@ BOOST_AUTO_TEST_CASE(TmpfcopystreamAbort)
     unlink(".foobar");
 }
 
+BOOST_AUTO_TEST_CASE(TmpfcopystreamChmod)
+{
+    write_file(".foobar","blah");
+    chmod(".foobar",0644);
+    Stat stat_orig(".foobar");
+    BOOST_CHECK_EQUAL( 0644, stat_orig.mode() );
+
+    tmpfcopystream tmpf(".foobar");
+
+    tmpf << "hello world";
+    tmpf.flush();
+
+    tmpf.set_filemode_on_close(0640);
+
+    Stat stat_tmp(tmpf.get_tmp_filename());
+    BOOST_CHECK_EQUAL( 0600, stat_tmp.mode() );
+
+    tmpf.close();
+
+    stat_orig.recheck();
+    BOOST_CHECK_EQUAL( 0640, stat_orig.mode() );
+
+    unlink(".foobar");
+}
+
+
 BOOST_AUTO_TEST_SUITE_END()