/** \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
*/
#include <iostream>
#include <ctime>
+#include <cstdlib>
+
#include "zypp/base/Logger.h"
#include "zypp/source/yum/YUMSourceImpl.h"
class XMLFilesBackend::Private
{
public:
+ bool randomFileName;
YUMSourceImpl source;
};
XMLFilesBackend::XMLFilesBackend()
{
d = new Private;
+ d->randomFileName = false;
// check if the db exists
if (!isBackendInitialized())
{
}
}
+// 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()
create_directory( path(ZYPP_DB_DIR) / path("products") );
}
+void XMLFilesBackend::setRandomFileNameEnabled( bool enabled )
+{
+ d->randomFileName = enabled;
+}
+
std::string
XMLFilesBackend::dirForResolvableKind( Resolvable::Kind kind ) const
{
{
std::string filename;
filename = dirForResolvable(resolvable) + "/";
- filename += resolvable->name();
+ filename += d->randomFileName ? randomString(40) : resolvable->name();
return filename;
}
{
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();
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 )
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) );
}
}