add chown to tmpfstreams variants
authorGerd von Egidy <gerd.von.egidy@intra2net.com>
Thu, 11 Mar 2010 18:00:42 +0000 (19:00 +0100)
committerGerd von Egidy <gerd.von.egidy@intra2net.com>
Thu, 11 Mar 2010 18:01:12 +0000 (19:01 +0100)
src/tmpfstream.hpp
src/tmpfstream_impl.hpp
test/test_tmpfstream.cpp

index 998a4f0..a15df9a 100644 (file)
@@ -20,6 +20,8 @@
 #include <boost/iostreams/stream.hpp>
 #include <boost/iostreams/device/file_descriptor.hpp>
 
+#include <userfunc.hpp>
+
 #include <sys/stat.h>
 
 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<Device, Tr, Alloc>()
         { }
 
@@ -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<Device, Tr, Alloc>
             (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<Device, Tr, Alloc>
             (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, 
index 0bec8e9..dafe706 100644 (file)
@@ -84,6 +84,7 @@ bool tmpfstreamTempl<Device,Tr,Alloc>::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<Device,Tr,Alloc>::set_file_mode(mode_t mode)
@@ -95,6 +96,22 @@ bool tmpfstreamTempl<Device,Tr,Alloc>::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<Device,Tr,Alloc>::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<Device,Tr,Alloc>::close()
 
     tmpfstreamTempl<Device,Tr,Alloc>::set_file_mode(filemode_on_close);
 
+    if (UserOnClose != I2n::User())
+        tmpfstreamTempl<Device,Tr,Alloc>::set_owner(UserOnClose,GroupOnClose);
+
     move(get_original_filename(),true);
 
     if (get_full_sync())
index c9e2612..3bc8a28 100644 (file)
@@ -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() );