Extend ZConfig to store a user defined string value (fate#312521)
authorMichael Andres <ma@suse.de>
Wed, 7 Nov 2012 11:42:28 +0000 (12:42 +0100)
committerMichael Andres <ma@suse.de>
Wed, 7 Nov 2012 13:42:50 +0000 (14:42 +0100)
The value will be used as transaction id and will be passed to e.g.
the history file and commit plugins.

doc/autoinclude/UserData.doc [new file with mode: 0644]
zypp/ZConfig.cc
zypp/ZConfig.h

diff --git a/doc/autoinclude/UserData.doc b/doc/autoinclude/UserData.doc
new file mode 100644 (file)
index 0000000..3317492
--- /dev/null
@@ -0,0 +1,13 @@
+/**
+\page zypp-userdata User data as transaction id
+
+\author Michael Andres <ma@suse.de>
+
+<HR><!-- ====================================================================== -->
+\section userdata-introduction Introduction
+
+It is possible to store a user defined data string via \ref zypp::ZConfig::setUserData inside libzypp for use as a transaction id. \c Zypper e.g. supports this via its global <tt>'--userdata <string>'</tt> option.
+
+\li Setting a new user data string, a line will be written to the log file. So you can \c grep for it, or for  <tt>'(setUserData)'</tt> to find all locations where user data strings were set.
+
+*/
index 4103af5..5c98209 100644 (file)
@@ -577,6 +577,8 @@ namespace zypp
     Pathname credentials_global_dir_path;
     Pathname credentials_global_file_path;
 
+    std::string userData;
+
     Option<Pathname> pluginsPath;
 
   private:
@@ -705,6 +707,31 @@ namespace zypp
   }
 
   ///////////////////////////////////////////////////////////////////
+  // user data
+  ///////////////////////////////////////////////////////////////////
+
+  bool ZConfig::hasUserData() const
+  { return !_pimpl->userData.empty(); }
+
+  std::string ZConfig::userData() const
+  { return _pimpl->userData; }
+
+  bool ZConfig::setUserData( const std::string & str_r )
+  {
+    for_( ch, str_r.begin(), str_r.end() )
+    {
+      if ( *ch < ' ' && *ch != '\t' )
+      {
+       ERR << "New user data string rejectded: char " << (int)*ch << " at position " <<  (ch - str_r.begin()) << endl;
+       return false;
+      }
+    }
+    MIL << "Set user data string to '" << str_r << "'" << endl;
+    _pimpl->userData = str_r;
+    return true;
+  }
+
+  ///////////////////////////////////////////////////////////////////
 
   Pathname ZConfig::repoCachePath() const
   {
index f2937b6..c8b9793 100644 (file)
@@ -109,6 +109,24 @@ namespace zypp
       { setTextLocale( defaultTextLocale() ); }
 
     public:
+      /** \name Maintain user data
+       * \see \ref zypp-userdata
+       */
+      //@{
+      /** Whether a (non empty) user data sting is defined. */
+      bool hasUserData() const;
+
+      /** User defined string value to be passed to log, history, plugins... */
+      std::string userData() const;
+
+      /** Set a new \ref userData string.
+       * \returns \c TRUE if the string was accepted; \c FALSE if the
+       * string was rejected due to nonprintable characters or newlines.
+       */
+      bool setUserData( const std::string & str_r );
+      //@}
+
+    public:
       /**
        * Path where the caches are kept (/var/cache/zypp)
        * \ingroup g_ZC_REPOCACHE