misc fixes and including the random string generation used for testing, taken from...
authorDuncan Mac-Vicar P <dmacvicar@suse.de>
Fri, 20 Jan 2006 13:29:26 +0000 (13:29 +0000)
committerDuncan Mac-Vicar P <dmacvicar@suse.de>
Fri, 20 Jan 2006 13:29:26 +0000 (13:29 +0000)
zypp/target/store/Backend.h
zypp/target/store/XMLFilesBackend.cc
zypp/target/store/XMLFilesBackend.h

index 093d843..abe6aa1 100644 (file)
@@ -9,8 +9,8 @@
 /** \file      devel/devel.dmacvicar/Backend.h
 *
 */
-#ifndef DEVEL_DEVEL_DMACVICAR_BACKEND_H
-#define DEVEL_DEVEL_DMACVICAR_BACKEND_H
+#ifndef ZYPP_STORAGE_BACKEND_H
+#define ZYPP_STORAGE_BACKEND_H
 
 #include <iosfwd>
 
 ///////////////////////////////////////////////////////////////////
 namespace zypp
 { /////////////////////////////////////////////////////////////////
-       ///////////////////////////////////////////////////////////////////
-       namespace storage
-       { /////////////////////////////////////////////////////////////////
+///////////////////////////////////////////////////////////////////
+namespace storage
+{ /////////////////////////////////////////////////////////////////
 
-               ///////////////////////////////////////////////////////////////////
-               //
-               //      CLASS NAME : Backend
-               //
-               /** */
-               class Backend
-               {
-                       friend std::ostream & operator<<( std::ostream & str, const Backend & obj );
-               public:
-                       /** Default ctor */
-                       Backend();
-                       /** Dtor */
-                       virtual ~Backend();
-                       virtual void doTest() = 0;
+///////////////////////////////////////////////////////////////////
+//
+//     CLASS NAME : Backend
+//
+/**
+* This class represents a storage backend implementation
+*/
+class Backend
+{
+  friend std::ostream & operator<<( std::ostream & str, const Backend & obj );
+public:
+  /** Default ctor */
+  Backend();
+  /** Dtor */
+  virtual ~Backend();
+  virtual void doTest() = 0;
 
-      /**
-       * is the storage backend initialized
-       */
-                       virtual bool isBackendInitialized() = 0;
-      /**
-       * initialize the storage backend
-       */
-                       virtual void initBackend() = 0;
+  /**
+  * is the storage backend initialized
+  */
+  virtual bool isBackendInitialized() = 0;
+  /**
+  * initialize the storage backend
+  */
+  virtual void initBackend() = 0;
 
-      /**
-       * Stores a Resolvable in the active backend.
-       */
-      virtual void storeObject( Resolvable::Ptr resolvable )  = 0;
-      /**
-       * Deletes a Resolvable from the active backend.
-       */
-      virtual void deleteObject( Resolvable::Ptr resolvable ) = 0;
-     
-      /**
-       * Query for installed Resolvables.
-       */
-      virtual std::list<Resolvable::Ptr> storedObjects() = 0;
-       /**
-       * Query for installed Resolvables of a certain kind
-       */
-      virtual std::list<Resolvable::Ptr> storedObjects(const Resolvable::Kind) = 0;
-       /**
-       * Query for installed Resolvables of a certain kind by name
-       * \a partial_match allows for text search.
-       */
-      virtual std::list<Resolvable::Ptr> storedObjects(const Resolvable::Kind, const std::string & name, bool partial_match = false) = 0;
+  /**
+  * Stores a Resolvable in the active backend.
+  */
+  virtual void storeObject( Resolvable::Ptr resolvable )  = 0;
+  /**
+  * Deletes a Resolvable from the active backend.
+  */
+  virtual void deleteObject( Resolvable::Ptr resolvable ) = 0;
 
-               private:
-                       /** Pointer to implementation */
-                       class Private;
-                       Private *d;
-               };
-               ///////////////////////////////////////////////////////////////////
+  /**
+  * Query for installed Resolvables.
+  */
+  virtual std::list<Resolvable::Ptr> storedObjects() = 0;
+  /**
+  * Query for installed Resolvables of a certain kind
+  */
+  virtual std::list<Resolvable::Ptr> storedObjects(const Resolvable::Kind) = 0;
+  /**
+  * Query for installed Resolvables of a certain kind by name
+  * \a partial_match allows for text search.
+  */
+  virtual std::list<Resolvable::Ptr> storedObjects(const Resolvable::Kind, const std::string & name, bool partial_match = false) = 0;
 
-               /** \relates Backend Stream output */
-               std::ostream & operator<<( std::ostream & str, const Backend & obj );
+private:
+  /** Pointer to implementation */
+  class Private;
+  Private *d;
+};
+///////////////////////////////////////////////////////////////////
+
+/** \relates Backend Stream output */
+std::ostream & operator<<( std::ostream & str, const Backend & obj );
 
-               /////////////////////////////////////////////////////////////////
-       } // namespace devel.dmacvicar
-       ///////////////////////////////////////////////////////////////////
-       /////////////////////////////////////////////////////////////////
+/////////////////////////////////////////////////////////////////
+} // namespace devel.dmacvicar
+///////////////////////////////////////////////////////////////////
+/////////////////////////////////////////////////////////////////
 } // namespace devel
 ///////////////////////////////////////////////////////////////////
 #endif // DEVEL_DEVEL_DMACVICAR_BACKEND_H
index 01778f3..bc5a17a 100644 (file)
@@ -11,6 +11,8 @@
 */
 #include <iostream>
 #include <ctime>
+#include <cstdlib>
+
 #include "zypp/base/Logger.h"
 
 #include "zypp/source/yum/YUMSourceImpl.h"
@@ -50,6 +52,7 @@ namespace storage
 class XMLFilesBackend::Private
 {
   public:
+  bool randomFileName;
        YUMSourceImpl source;
 };
 
@@ -67,6 +70,7 @@ class XMLFilesBackend::Private
 XMLFilesBackend::XMLFilesBackend()
 {
   d = new Private;
+  d->randomFileName = false;
        // check if the db exists
        if (!isBackendInitialized())
        {
@@ -84,6 +88,46 @@ XMLFilesBackend::XMLFilesBackend()
        }
 }
 
+// Taken from KApplication
+int XMLFilesBackend::random() const
+{
+   static bool init = false;
+   if (!init)
+   {
+      unsigned int seed;
+      init = true;
+      int fd = open("/dev/urandom", O_RDONLY);
+      if (fd < 0 || ::read(fd, &seed, sizeof(seed)) != sizeof(seed))
+      {
+            // No /dev/urandom... try something else.
+            srand(getpid());
+            seed = rand()+time(0);
+      }
+      if (fd >= 0) close(fd);
+      srand(seed);
+   }
+   return rand();
+}
+
+// Taken from KApplication
+std::string XMLFilesBackend::randomString(int length) const
+{
+   if (length <=0 ) return std::string();
+
+   std::string str; str.resize( length );
+   int i = 0;
+   while (length--)
+   {
+      int r=random() % 62;
+      r+=48;
+      if (r>57) r+=7;
+      if (r>90) r+=6;
+      str[i++] =  char(r);
+      // so what if I work backwards?
+   }
+   return str;
+}
+
 
 bool
 XMLFilesBackend::isBackendInitialized()
@@ -102,6 +146,11 @@ XMLFilesBackend::initBackend()
   create_directory( path(ZYPP_DB_DIR) / path("products") );
 }
 
+void XMLFilesBackend::setRandomFileNameEnabled( bool enabled )
+{
+  d->randomFileName = enabled;
+}
+
 std::string
 XMLFilesBackend::dirForResolvableKind( Resolvable::Kind kind ) const
 {
@@ -124,7 +173,7 @@ XMLFilesBackend::fullPathForResolvable( Resolvable::Ptr resolvable ) const
 {
   std::string filename;
   filename = dirForResolvable(resolvable) + "/";
-  filename += resolvable->name();
+  filename += d->randomFileName ? randomString(40) : resolvable->name();
   return filename;
 }
 
@@ -133,9 +182,9 @@ XMLFilesBackend::storeObject( Resolvable::Ptr resolvable )
 {
   std::string xml = castedToXML(resolvable);
   std::string filename = fullPathForResolvable(resolvable);
-  DBG << std::endl << xml << std::endl;
+  //DBG << std::endl << xml << std::endl;
   std::ofstream file;
-  DBG << filename << std::endl;
+  //DBG << filename << std::endl;
   file.open(filename.c_str());
   file << xml;
   file.close();
@@ -147,7 +196,7 @@ XMLFilesBackend::deleteObject( Resolvable::Ptr resolvable )
 
 Resolvable::Ptr XMLFilesBackend::resolvableFromFile( std::string file_path, Resolvable::Kind kind ) const
 {
-  DBG << "[" << resolvableKindToString( kind, false ) << "] - " << file_path << std::endl;
+  //DBG << "[" << resolvableKindToString( kind, false ) << "] - " << file_path << std::endl;
   Resolvable::Ptr resolvable;
   std::ifstream res_file(file_path.c_str());
   if ( kind == ResTraits<zypp::Patch>::kind )
@@ -190,7 +239,7 @@ XMLFilesBackend::storedObjects()
       if ( !exists( dir_path ) ) continue;
       for ( directory_iterator dir_itr( dir_path ); dir_itr != end_iter; ++dir_itr )
       {
-        DBG << "[" << resolvableKindToString( kind, false ) << "] - " << dir_itr->leaf() << std::endl;
+        //DBG << "[" << resolvableKindToString( kind, false ) << "] - " << dir_itr->leaf() << std::endl;
         objects.push_back( resolvableFromFile( dir_path + "/" + dir_itr->leaf(), kind) );
       }
     }
index 68831d6..7bade33 100644 (file)
@@ -40,6 +40,7 @@ public:
   XMLFilesBackend();
   /** Dtor */
   ~XMLFilesBackend();
+  void setRandomFileNameEnabled( bool enabled );
   virtual void doTest();
 
   /**
@@ -73,6 +74,9 @@ public:
   virtual std::list<Resolvable::Ptr> storedObjects(const Resolvable::Kind, const std::string & name, bool partial_match = false);
 
   protected:
+  std::string randomString(int length) const;
+  int random() const;
+  
   std::string randomFileName() const;
   /**
     * Directory where the xml file is stored (for the given resolvable)