From ef2a60622292294b31e8e985729269c356ebd65e Mon Sep 17 00:00:00 2001 From: Gerd von Egidy Date: Thu, 11 Mar 2010 19:00:42 +0100 Subject: [PATCH] add chown to tmpfstreams variants --- src/tmpfstream.hpp | 34 ++++++++++++++++++++++++++++++++++ src/tmpfstream_impl.hpp | 20 ++++++++++++++++++++ test/test_tmpfstream.cpp | 2 +- 3 files changed, 55 insertions(+), 1 deletions(-) diff --git a/src/tmpfstream.hpp b/src/tmpfstream.hpp index 998a4f0..a15df9a 100644 --- a/src/tmpfstream.hpp +++ b/src/tmpfstream.hpp @@ -20,6 +20,8 @@ #include #include +#include + #include namespace I2n @@ -100,6 +102,8 @@ public: bool set_file_mode(mode_t mode); + bool set_owner(const I2n::User& user, const I2n::Group& group= I2n::Group()); + bool unlink(); bool is_unlinked() @@ -153,6 +157,8 @@ private: std::string originalfilename; bool full_sync; mode_t filemode_on_close; + I2n::User UserOnClose; + I2n::Group GroupOnClose; public: static const std::string default_template_suffix; @@ -165,6 +171,7 @@ public: tmpfcopystreamTempl() : full_sync(default_full_sync), filemode_on_close(default_filemode_on_close), + UserOnClose(), GroupOnClose(), tmpfstreamTempl() { } @@ -183,6 +190,7 @@ public: int buffer_size = -1 , int pback_size = -1) : full_sync(default_full_sync), filemode_on_close(default_filemode_on_close), + UserOnClose(), GroupOnClose(), originalfilename(filename), tmpfstreamTempl (filename+default_template_suffix,mode,buffer_size,pback_size) @@ -207,6 +215,7 @@ public: int buffer_size = -1 , int pback_size = -1) : full_sync(default_full_sync), filemode_on_close(default_filemode_on_close), + UserOnClose(), GroupOnClose(), originalfilename(filename), tmpfstreamTempl (tmpnametemplate,mode,buffer_size,pback_size) @@ -297,6 +306,31 @@ public: { return filemode_on_close; } /** + * @brief Change file owner (chown) that will be set on @ref close + * @param user the new owner + * @param group the new group, if left empty the main group of the user is set + */ + void set_owner_on_close(const I2n::User& user, const I2n::Group& group= I2n::Group()) + { + UserOnClose=user; + GroupOnClose=group; + } + + /** + * @brief Get file owner that will be set on @ref close + * @retval user that will be set on @ref close, an empty @ref User() means nothing will be set + */ + I2n::User get_owner_on_close() + { return UserOnClose; } + + /** + * @brief Get file owner group that will be set on @ref close + * @retval group that will be set on @ref close + */ + I2n::Group get_group_on_close() + { return GroupOnClose; } + + /** * @brief Delete the file. * * calling unlink is a safe way to abort, diff --git a/src/tmpfstream_impl.hpp b/src/tmpfstream_impl.hpp index 0bec8e9..dafe706 100644 --- a/src/tmpfstream_impl.hpp +++ b/src/tmpfstream_impl.hpp @@ -84,6 +84,7 @@ bool tmpfstreamTempl::open(const std::string& tmpnametemplate, * @brief Changes permissions (chmod) of the file. * * @param mode the new mode as in chmod +* @retval true if successful */ template< typename Device, typename Tr, typename Alloc > bool tmpfstreamTempl::set_file_mode(mode_t mode) @@ -95,6 +96,22 @@ bool tmpfstreamTempl::set_file_mode(mode_t mode) } /** +* @brief Changes the owner of the file (chown). +* +* @param user the new owner +* @param group the new group, if left empty the main group of the user is set +* @retval true if successful +*/ +template< typename Device, typename Tr, typename Alloc > +bool tmpfstreamTempl::set_owner(const I2n::User& user, const I2n::Group& group) +{ + if (!get_tmp_filename().empty() && !is_unlinked()) + return I2n::chown(get_tmp_filename(),user,group); + else + return false; +} + +/** * @brief Delete the file. * * Can be called while the file is still open. @@ -203,6 +220,9 @@ void tmpfcopystreamTempl::close() tmpfstreamTempl::set_file_mode(filemode_on_close); + if (UserOnClose != I2n::User()) + tmpfstreamTempl::set_owner(UserOnClose,GroupOnClose); + move(get_original_filename(),true); if (get_full_sync()) diff --git a/test/test_tmpfstream.cpp b/test/test_tmpfstream.cpp index c9e2612..3bc8a28 100644 --- a/test/test_tmpfstream.cpp +++ b/test/test_tmpfstream.cpp @@ -334,7 +334,7 @@ BOOST_AUTO_TEST_CASE(TmpfcopystreamAbort) BOOST_AUTO_TEST_CASE(TmpfcopystreamChmod) { write_file(".foobar","blah"); - chmod(".foobar",0644); + I2n::chmod(".foobar",0644); Stat stat_orig(".foobar"); BOOST_CHECK_EQUAL( 0644, stat_orig.mode() ); -- 1.7.1