From 86f0d3e81320fa3377990ab9f9c960a136e2c788 Mon Sep 17 00:00:00 2001 From: Duncan Mac-Vicar P Date: Thu, 31 Jul 2008 17:08:19 +0000 Subject: [PATCH] generate a unique anonymous unique string per target and add it to the agent string for better statistics --- libzypp.spec.cmake | 3 +++ package/libzypp.changes | 6 +++++ tests/zypp/CMakeLists.txt | 1 + tests/zypp/Target_test.cc | 32 ++++++++++++++++++++++++++ zypp/Target.cc | 3 +++ zypp/Target.h | 10 ++++++++ zypp/media/MediaCurl.cc | 27 ++++++++++++++++++++-- zypp/target/TargetImpl.cc | 58 +++++++++++++++++++++++++++++++++++++++++++++++ zypp/target/TargetImpl.h | 2 ++ 9 files changed, 140 insertions(+), 2 deletions(-) create mode 100644 tests/zypp/Target_test.cc diff --git a/libzypp.spec.cmake b/libzypp.spec.cmake index 285feba..8d07f1f 100644 --- a/libzypp.spec.cmake +++ b/libzypp.spec.cmake @@ -29,6 +29,7 @@ BuildRequires: boost-devel curl-devel dejagnu doxygen gcc-c++ gettext-devel gra %if 0%{?suse_version} BuildRequires: hicolor-icon-theme update-desktop-files rpm-devel +Requires: uuid-runtime %endif %if 0%{?fedora_version} @@ -38,6 +39,8 @@ BuildRequires: glib2-devel popt-devel dbus-glib-devel rpm-devel %if 0%{?mandriva_version} BuildRequires: glib2-devel BuildRequires: librpm-devel +# uuidgen +Requires: e2fsprogs %endif diff --git a/package/libzypp.changes b/package/libzypp.changes index 99fcc1a..27526e3 100644 --- a/package/libzypp.changes +++ b/package/libzypp.changes @@ -6,6 +6,12 @@ Thu Jul 31 19:01:14 CEST 2008 ma@suse.de - revision 10710 ------------------------------------------------------------------- +Thu Jul 31 19:01:54 CEST 2008 - dmacvicar@suse.de + +- generate a unique anonymous unique string per target + and add it to the agent string for better statistics + +------------------------------------------------------------------- Wed Jul 30 19:12:00 CEST 2008 ma@suse.de - /var/lib/zypp and /var/cache/zypp should be owned by libzypp diff --git a/tests/zypp/CMakeLists.txt b/tests/zypp/CMakeLists.txt index 44ed3a1..b6fb032 100644 --- a/tests/zypp/CMakeLists.txt +++ b/tests/zypp/CMakeLists.txt @@ -28,6 +28,7 @@ ADD_TESTS( RepoManager RepoStatus ResKind + Target TranslatedText Url Vendor diff --git a/tests/zypp/Target_test.cc b/tests/zypp/Target_test.cc new file mode 100644 index 0000000..d35db50 --- /dev/null +++ b/tests/zypp/Target_test.cc @@ -0,0 +1,32 @@ + +#include +#include +#include + +// Boost.Test +#include + +#include "zypp/base/Logger.h" +#include "zypp/base/Exception.h" +#include "zypp/ZYppFactory.h" +#include "zypp/ZYpp.h" +#include "zypp/ZYppFactory.h" +#include "zypp/TmpPath.h" + +using boost::unit_test::test_case; +using namespace std; +using namespace zypp; + + +BOOST_AUTO_TEST_CASE(target_test) +{ + + filesystem::TmpDir tmp; + + ZYpp::Ptr z = getZYpp(); + z->initializeTarget( tmp.path() ); + + BOOST_CHECK( ! z->target()->anonymousUniqueId().empty() ); + BOOST_CHECK( PathInfo( tmp.path() / "/var/lib/zypp/AnonymousUniqueId").isExist() ); + BOOST_CHECK( PathInfo( tmp.path() / "/var/lib/zypp/AnonymousUniqueId").size() > 0 ); +} diff --git a/zypp/Target.cc b/zypp/Target.cc index 1b56dc6..90748dd 100644 --- a/zypp/Target.cc +++ b/zypp/Target.cc @@ -100,6 +100,9 @@ namespace zypp std::string Target::release() const { return _pimpl->release(); } + std::string Target::anonymousUniqueId() const + { return _pimpl->anonymousUniqueId(); } + ///////////////////////////////////////////////////////////////// } // namespace zypp /////////////////////////////////////////////////////////////////// diff --git a/zypp/Target.h b/zypp/Target.h index bdcf2d9..db3ad54 100644 --- a/zypp/Target.h +++ b/zypp/Target.h @@ -110,6 +110,16 @@ namespace zypp */ std::string release() const; + /** + * anonymous unique id + * + * This id is generated once and stays in the + * saved in the target. + * It is unique and is used only for statistics. + * + */ + std::string anonymousUniqueId() const; + public: /** Ctor. If \c doRebuild_r is \c true, an already existing * database is rebuilt (rpm --rebuilddb ). diff --git a/zypp/media/MediaCurl.cc b/zypp/media/MediaCurl.cc index bfd4ba5..1ad12fc 100644 --- a/zypp/media/MediaCurl.cc +++ b/zypp/media/MediaCurl.cc @@ -26,6 +26,9 @@ #include "zypp/media/MediaUserAuth.h" #include "zypp/media/CurlConfig.h" #include "zypp/thread/Once.h" +#include "zypp/Target.h" +#include "zypp/ZYppFactory.h" + #include #include #include @@ -183,9 +186,29 @@ Pathname MediaCurl::_cookieFile = "/var/lib/YaST2/cookies"; const char *const MediaCurl::agentString() { - static const std::string _value( str::form( "ZYpp %s (curl %s)", + // we need to add the release and identifier to the + // agent string. + // The target could be not initialized, and then this information + // is not available. + Target_Ptr target; + // FIXME this has to go away as soon as the target + // does not throw when not initialized. + try { + target = zypp::getZYpp()->target(); + } + catch ( const Exception &e ) + { + // nothing to do + } + + static const std::string _value( str::form( "ZYpp %s (curl %s) %s", VERSION, - curl_version_info(CURLVERSION_NOW)->version ) ); + curl_version_info(CURLVERSION_NOW)->version, + target ? str::form( " - %s on '%s'", + target->anonymousUniqueId().c_str(), + target->release().c_str() ).c_str() + : "" ) + ); return _value.c_str(); } diff --git a/zypp/target/TargetImpl.cc b/zypp/target/TargetImpl.cc index 167626e..a078593 100644 --- a/zypp/target/TargetImpl.cc +++ b/zypp/target/TargetImpl.cc @@ -287,6 +287,52 @@ namespace zypp , _hardLocksFile( Pathname::assertprefix( _root, ZConfig::instance().locksFile() ) ) { _rpm.initDatabase( root_r, Pathname(), doRebuild_r ); + + // create the anonymous unique id + // this value is used for statistics + Pathname idpath( home() / "AnonymousUniqueId"); + + if ( ! PathInfo( idpath ).isExist() ) + { + MIL << "creating anonymous unique id" << endl; + + // if the file does not exist we need to generate the uuid file + const char* argv[] = + { + "/usr/bin/uuidgen", + "-r", + "-t", + NULL + }; + + ExternalProgram prog( argv, + ExternalProgram::Normal_Stderr, + false, -1, true); + std::string line; + std::ofstream idfile; + // make sure the path exists + filesystem::assert_dir( home() ); + idfile.open( idpath.c_str() ); + + if ( idfile.good() ) + { + for(line = prog.receiveLine(); + ! line.empty(); + line = prog.receiveLine() ) + { + MIL << line << endl; + + idfile << line; + } + prog.close(); + } + else + { + // FIXME, should we ignore the error? + ZYPP_THROW(Exception("Can't open anonymous id file '" + idpath.asString() + "' for writing")); + } + } + MIL << "Initialized target on " << _root << endl; } @@ -822,6 +868,18 @@ namespace zypp return _("Unknown Distribution"); } + std::string TargetImpl::anonymousUniqueId() const + { + std::ifstream idfile( ( home() / "AnonymousUniqueId" ).c_str() ); + for( iostr::EachLine in( idfile ); in; in.next() ) + { + std::string line( str::trim( *in ) ); + if ( ! line.empty() ) + return line; + } + return std::string(); + } + void TargetImpl::installSrcPackage( const SrcPackage_constPtr & srcPackage_r ) { // provide on local disk diff --git a/zypp/target/TargetImpl.h b/zypp/target/TargetImpl.h index 62bf2a2..a19ae12 100644 --- a/zypp/target/TargetImpl.h +++ b/zypp/target/TargetImpl.h @@ -78,6 +78,8 @@ namespace zypp void buildCache(); + std::string anonymousUniqueId() const; + public: /** The root set for this target */ -- 2.7.4