storing and reading sources works... only need to add the small xml parser and I...
authorDuncan Mac-Vicar P <dmacvicar@suse.de>
Mon, 6 Feb 2006 15:11:02 +0000 (15:11 +0000)
committerDuncan Mac-Vicar P <dmacvicar@suse.de>
Mon, 6 Feb 2006 15:11:02 +0000 (15:11 +0000)
zypp/target/store/Backend.h
zypp/target/store/PersistentStorage.cc
zypp/target/store/PersistentStorage.h
zypp/target/store/XMLFilesBackend.cc
zypp/target/store/XMLFilesBackend.h
zypp/target/store/serialize.cc

index bc1395f..f572e3c 100644 (file)
@@ -83,11 +83,7 @@ public:
   /**
     * Query for installed Sources
     */
-  virtual std::set<PersistentStorage::SourceData> storedSources() const = 0;
-  /**
-    * Query for installed Source
-    */
-  virtual PersistentStorage::SourceData storedSource(const std::string &alias) const = 0;
+  virtual std::list<PersistentStorage::SourceData> storedSources() const = 0;
   /**
     * Query for installed Sources
     */
@@ -96,10 +92,6 @@ public:
     * Query for installed Sources
     */
   virtual void deleteSource(const std::string &alias) = 0;
-  /**
-    * enable disable source
-    */
-  virtual void setSourceEnabled(const std::string &alias, bool enabled) = 0;
 
 private:
   /** Pointer to implementation */
index b83ab96..fd93d7f 100644 (file)
@@ -89,7 +89,6 @@ PersistentStorage::deleteObject( Resolvable::Ptr resolvable )
 
 }
 
-
 std::list<Resolvable::Ptr>
 PersistentStorage::storedObjects() const
 {
@@ -114,18 +113,12 @@ PersistentStorage::storedObjects(const Resolvable::Kind kind, const std::string
 // SOURCES API
 ////////////////////////////////////////////////////////
 
-std::set<PersistentStorage::SourceData>
+std::list<PersistentStorage::SourceData>
 PersistentStorage::storedSources() const
 {
   return d->backend->storedSources();
 }
 
-PersistentStorage::SourceData
-PersistentStorage::storedSource(const std::string &alias) const
-{
-  return d->backend->storedSource(alias);
-}
-
 void
 PersistentStorage::storeSource(const PersistentStorage::SourceData &data)
 {
@@ -138,13 +131,6 @@ PersistentStorage::deleteSource(const std::string &alias)
   d->backend->deleteSource(alias);
 }
 
-void
-PersistentStorage::setSourceEnabled(const std::string &alias, bool enabled)
-{
-  d->backend->setSourceEnabled(alias, enabled);
-}
-
-
 /******************************************************************
 **
 **     FUNCTION NAME : operator<<
index c0447c2..1fdbe98 100644 (file)
@@ -45,11 +45,18 @@ namespace zypp
       
       struct SourceData
       {
+        SourceData()
+        {
+          enabled = false;
+          autorefresh = false;
+        };
+
         bool enabled;
         bool autorefresh;
         std::string product_dir;
         std::string type;
         std::string url;
+        std::string alias;
       };
 
     public:
@@ -86,11 +93,7 @@ namespace zypp
       /**
        * Query for installed Sources
        */
-      std::set<SourceData> storedSources() const;
-      /**
-       * Query for installed Source
-       */
-      SourceData storedSource(const std::string &alias) const;
+      std::list<SourceData> storedSources() const;
       /**
        * Query for installed Sources
        */
@@ -99,10 +102,6 @@ namespace zypp
        * Query for installed Sources
        */
       void deleteSource(const std::string &alias);
-      /**
-       * enable disable source
-       */
-      void setSourceEnabled(const std::string &alias, bool enabled);
 
     private:
       class Private;
index a3d3db3..045fdaf 100644 (file)
 #include <sstream>
 #include <streambuf>
 
+#include <list>
+
 #include "boost/filesystem/operations.hpp" // includes boost/filesystem/path.hpp
 #include "boost/filesystem/fstream.hpp"    // ditto
 
 #include "XMLFilesBackend.h"
 #include "serialize.h"
-#include <list>
+
+#include "md5.hh"
 
 #define ZYPP_DB_DIR "/var/lib/zypp_db"
 
@@ -168,7 +171,8 @@ XMLFilesBackend::initBackend()
   // FIXME duncan * handle exceptions
   DBG << "Creating directory structure..." << std::endl;
   path topdir = path(d->root.asString()) / path(ZYPP_DB_DIR);
-  create_directory(topdir);
+  if (!exists(topdir))
+    create_directory(topdir);
   MIL << "Created..." << topdir.string() << std::endl;
   std::set<Resolvable::Kind>::const_iterator it_kinds;
   for ( it_kinds = d->kinds.begin() ; it_kinds != d->kinds.end(); ++it_kinds )
@@ -227,9 +231,17 @@ XMLFilesBackend::storeObject( Resolvable::constPtr resolvable )
   //DBG << std::endl << xml << std::endl;
   std::ofstream file;
   //DBG << filename << std::endl;
-  file.open(filename.c_str());
-  file << xml;
-  file.close();
+  try
+  {
+    file.open(filename.c_str());
+    file << xml;
+    file.close();
+  }
+  catch(std::exception &e)
+  {
+    ERR << "Error saving resolvable " << resolvable << std::endl;
+    ZYPP_THROW(Exception(e.what()));
+  }
 }
 
 void
@@ -589,56 +601,71 @@ std::ostream & operator<<( std::ostream & str, const XMLFilesBackend & obj )
 // SOURCES API
 ////////////////////////////////////////////////////////
 
-std::set<PersistentStorage::SourceData>
+std::list<PersistentStorage::SourceData>
 XMLFilesBackend::storedSources() const
 {
   path source_p = path(d->root.asString()) / path(ZYPP_DB_DIR) / path ("source-cache");
-  std::set<PersistentStorage::SourceData> sources;
+  std::list<PersistentStorage::SourceData> sources;
   DBG << "Reading source cache in " << source_p.string() << std::endl;
   directory_iterator end_iter;
   // return empty list if the dir does not exist
   if ( !exists( source_p ) )
   {
     ERR << "path " << source_p.string() << " does not exists. Required to read source cache " << std::endl;
-    return std::set<PersistentStorage::SourceData>();
+    return std::list<PersistentStorage::SourceData>();
   }
 
   for ( directory_iterator dir_itr( source_p ); dir_itr != end_iter; ++dir_itr )
   {
     DBG << "[source-cache] - " << dir_itr->leaf() << std::endl;
     //sources.insert( sourceDataFromCacheFile( source_p + "/" + dir_itr->leaf() ) );
-    //sources.insert(PersistentStorage::SourceData());
+    PersistentStorage::SourceData data;
+    sources.push_back(data);
   }
   MIL << "done reading source cache" << std::endl;
   return sources;
 
 }
 
-PersistentStorage::SourceData
-XMLFilesBackend::storedSource(const std::string &alias) const
-{
-  return PersistentStorage::SourceData();
-}
-
 void
 XMLFilesBackend::storeSource(const PersistentStorage::SourceData &data)
 {
-  
-}
+  std::string xml = toXML(data);
+  path source_p = path(d->root.asString()) / path(ZYPP_DB_DIR) / path ("source-cache");
 
-void
-XMLFilesBackend::deleteSource(const std::string &alias)
-{
+  // generate a filename
+  if (data.alias.size() == 0)
+  {
+    ZYPP_THROW(Exception("Cant save source with empty alias"));
+  }
+
+  stringstream ss(data.alias);
+  MD5 md5(ss);
   
+  //DBG << std::endl << xml << std::endl;
+  std::ofstream file;
+  //DBG << filename << std::endl;
+  try
+  {
+    std::string full_path = (source_p / md5.hex_digest()).string();
+    
+    file.open(full_path.c_str());
+    file << xml;
+    file.close();
+  }
+  catch ( std::exception &e )
+  {
+    ERR << "Error saving source " << data.alias << " in the cache" << std::endl;
+    ZYPP_THROW(Exception(e.what()));
+  }
 }
 
 void
-XMLFilesBackend::setSourceEnabled(const std::string &alias, bool enabled)
+XMLFilesBackend::deleteSource(const std::string &alias)
 {
   
 }
 
-
 /////////////////////////////////////////////////////////////////
 } // namespace storage
 ///////////////////////////////////////////////////////////////////
index 1218eec..ec1558f 100644 (file)
@@ -90,11 +90,7 @@ public:
   /**
     * Query for installed Sources
     */
-  virtual std::set<PersistentStorage::SourceData> storedSources() const;
-  /**
-    * Query for installed Source
-    */
-  virtual PersistentStorage::SourceData storedSource(const std::string &alias) const;
+  virtual std::list<PersistentStorage::SourceData> storedSources() const;
   /**
     * Query for installed Sources
     */
@@ -103,11 +99,7 @@ public:
     * Query for installed Sources
     */
   virtual void deleteSource(const std::string &alias);
-  /**
-    * enable disable source
-    */
-  virtual void setSourceEnabled(const std::string &alias, bool enabled);
-
+  
 
   protected:
   std::string randomString(int length) const;
index 16404de..96c1104 100644 (file)
@@ -314,6 +314,7 @@ std::string toXML( const PersistentStorage::SourceData &obj )
   stringstream out;
   out << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" << std::endl;
   out << "<source-cache  xmlns=\"http://novell.com/package/metadata/suse/source-cache\">" << std::endl;
+  out << "  <enabled>" << obj.enabled << "</enabled>" << std::endl;
   out << "  <auto-refresh>" << obj.autorefresh << "</auto-refresh>" << std::endl;
   out << "  <product-dir>" << obj.product_dir << "</product-dir>" << std::endl;
   out << "  <type>" << obj.type << "</type>" << std::endl;