From: Duncan Mac-Vicar P Date: Mon, 6 Feb 2006 15:11:02 +0000 (+0000) Subject: storing and reading sources works... only need to add the small xml parser and I... X-Git-Tag: BASE-SuSE-SLE-10-SP2-Branch~2577 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=8cae573f1e2ce3d5b636df6fac8d15820435b1b5;p=platform%2Fupstream%2Flibzypp.git storing and reading sources works... only need to add the small xml parser and I am done with source cache --- diff --git a/zypp/target/store/Backend.h b/zypp/target/store/Backend.h index bc1395f..f572e3c 100644 --- a/zypp/target/store/Backend.h +++ b/zypp/target/store/Backend.h @@ -83,11 +83,7 @@ public: /** * Query for installed Sources */ - virtual std::set storedSources() const = 0; - /** - * Query for installed Source - */ - virtual PersistentStorage::SourceData storedSource(const std::string &alias) const = 0; + virtual std::list 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 */ diff --git a/zypp/target/store/PersistentStorage.cc b/zypp/target/store/PersistentStorage.cc index b83ab96..fd93d7f 100644 --- a/zypp/target/store/PersistentStorage.cc +++ b/zypp/target/store/PersistentStorage.cc @@ -89,7 +89,6 @@ PersistentStorage::deleteObject( Resolvable::Ptr resolvable ) } - std::list PersistentStorage::storedObjects() const { @@ -114,18 +113,12 @@ PersistentStorage::storedObjects(const Resolvable::Kind kind, const std::string // SOURCES API //////////////////////////////////////////////////////// -std::set +std::list 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<< diff --git a/zypp/target/store/PersistentStorage.h b/zypp/target/store/PersistentStorage.h index c0447c2..1fdbe98 100644 --- a/zypp/target/store/PersistentStorage.h +++ b/zypp/target/store/PersistentStorage.h @@ -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 storedSources() const; - /** - * Query for installed Source - */ - SourceData storedSource(const std::string &alias) const; + std::list 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; diff --git a/zypp/target/store/XMLFilesBackend.cc b/zypp/target/store/XMLFilesBackend.cc index a3d3db3..045fdaf 100644 --- a/zypp/target/store/XMLFilesBackend.cc +++ b/zypp/target/store/XMLFilesBackend.cc @@ -31,12 +31,15 @@ #include #include +#include + #include "boost/filesystem/operations.hpp" // includes boost/filesystem/path.hpp #include "boost/filesystem/fstream.hpp" // ditto #include "XMLFilesBackend.h" #include "serialize.h" -#include + +#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::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 +std::list XMLFilesBackend::storedSources() const { path source_p = path(d->root.asString()) / path(ZYPP_DB_DIR) / path ("source-cache"); - std::set sources; + std::list 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(); + return std::list(); } 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 /////////////////////////////////////////////////////////////////// diff --git a/zypp/target/store/XMLFilesBackend.h b/zypp/target/store/XMLFilesBackend.h index 1218eec..ec1558f 100644 --- a/zypp/target/store/XMLFilesBackend.h +++ b/zypp/target/store/XMLFilesBackend.h @@ -90,11 +90,7 @@ public: /** * Query for installed Sources */ - virtual std::set storedSources() const; - /** - * Query for installed Source - */ - virtual PersistentStorage::SourceData storedSource(const std::string &alias) const; + virtual std::list 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; diff --git a/zypp/target/store/serialize.cc b/zypp/target/store/serialize.cc index 16404de..96c1104 100644 --- a/zypp/target/store/serialize.cc +++ b/zypp/target/store/serialize.cc @@ -314,6 +314,7 @@ std::string toXML( const PersistentStorage::SourceData &obj ) stringstream out; out << "" << std::endl; out << "" << std::endl; + out << " " << obj.enabled << "" << std::endl; out << " " << obj.autorefresh << "" << std::endl; out << " " << obj.product_dir << "" << std::endl; out << " " << obj.type << "" << std::endl;