From: Gerd von Egidy Date: Thu, 11 Mar 2010 10:40:51 +0000 (+0100) Subject: implement filemode_on_close, fix var initialization X-Git-Tag: v2.6~112^2~4 X-Git-Url: http://developer.intra2net.com/git/?a=commitdiff_plain;h=a8cbce9799f0f38397799425638e26b89bb938f6;p=libi2ncommon implement filemode_on_close, fix var initialization --- diff --git a/src/tmpfstream.hpp b/src/tmpfstream.hpp index 31f1d7a..e4aac49 100644 --- a/src/tmpfstream.hpp +++ b/src/tmpfstream.hpp @@ -43,13 +43,13 @@ private: public: tmpfstreamTempl() - : bio::stream() + : unlinked(false), bio::stream() { } 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() + : unlinked(false), bio::stream() { open(tmpnametemplate,mode,buffer_size,pback_size); } @@ -85,18 +85,25 @@ class tmpfcopystreamTempl : public tmpfstreamTempl 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() + : full_sync(default_full_sync), + filemode_on_close(default_filemode_on_close), + tmpfstreamTempl() { } 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 (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 (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: diff --git a/src/tmpfstream_impl.hpp b/src/tmpfstream_impl.hpp index 75dd6a4..3534178 100644 --- a/src/tmpfstream_impl.hpp +++ b/src/tmpfstream_impl.hpp @@ -125,17 +125,17 @@ bool tmpfstreamTempl::move(const std::string& targetpath, template< typename Device, typename Tr, typename Alloc > void tmpfcopystreamTempl::close() { - if (!tmpfcopystreamTempl::is_open() || - tmpfcopystreamTempl::is_unlinked()) + if (!tmpfstreamTempl::is_open() || + tmpfstreamTempl::is_unlinked()) return; // TODO full sync - // TODO chmod - // close the underlying filedescriptor tmpfstreamTempl::close(); + tmpfstreamTempl::set_file_mode(filemode_on_close); + move(get_original_filename(),true); } diff --git a/test/test_tmpfstream.cpp b/test/test_tmpfstream.cpp index ca92c49..6b3782a 100644 --- a/test/test_tmpfstream.cpp +++ b/test/test_tmpfstream.cpp @@ -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()