From: Gerd von Egidy Date: Thu, 11 Mar 2010 09:40:29 +0000 (+0100) Subject: working version of tmpfcopystream X-Git-Tag: v2.6~112^2~5 X-Git-Url: http://developer.intra2net.com/git/?a=commitdiff_plain;h=ccee68fd525ab61c53eac8bb76aed55bb8ace151;p=libi2ncommon working version of tmpfcopystream --- diff --git a/src/tmpfstream.hpp b/src/tmpfstream.hpp index 80ad75a..31f1d7a 100644 --- a/src/tmpfstream.hpp +++ b/src/tmpfstream.hpp @@ -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::unlink(); } - bool move(const std::string& targetpath, bool overwrite=false) - { return tmpfcopystreamTempl::move(targetpath,overwrite); } + { return tmpfstreamTempl::move(targetpath,overwrite); } }; typedef tmpfstreamTempl tmpofstream; diff --git a/src/tmpfstream_impl.hpp b/src/tmpfstream_impl.hpp index 0526c4a..75dd6a4 100644 --- a/src/tmpfstream_impl.hpp +++ b/src/tmpfstream_impl.hpp @@ -125,8 +125,18 @@ bool tmpfstreamTempl::move(const std::string& targetpath, template< typename Device, typename Tr, typename Alloc > void tmpfcopystreamTempl::close() { - // TODO + if (!tmpfcopystreamTempl::is_open() || + tmpfcopystreamTempl::is_unlinked()) + return; + // TODO full sync + + // TODO chmod + + // close the underlying filedescriptor + tmpfstreamTempl::close(); + + move(get_original_filename(),true); } } diff --git a/test/test_tmpfstream.cpp b/test/test_tmpfstream.cpp index 1d61719..ca92c49 100644 --- a/test/test_tmpfstream.cpp +++ b/test/test_tmpfstream.cpp @@ -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()