From d6e68b2916d30c527b1ce922af6e86f97814f286 Mon Sep 17 00:00:00 2001 From: Michael Andres Date: Thu, 17 Oct 2013 16:35:07 +0200 Subject: [PATCH] Add Package isCached and cachedLocation methods --- zypp/Package.cc | 39 +++++++++++++++++++++++++++++++++++++++ zypp/Package.h | 7 +++++++ 2 files changed, 46 insertions(+) diff --git a/zypp/Package.cc b/zypp/Package.cc index fd138af..ecac7c9 100644 --- a/zypp/Package.cc +++ b/zypp/Package.cc @@ -9,6 +9,8 @@ /** \file zypp/Package.cc * */ +#include + #include "zypp/base/Logger.h" #include "zypp/base/String.h" #include "zypp/Package.h" @@ -148,6 +150,43 @@ namespace zypp OnMediaLocation Package::location() const { return lookupLocation(); } + namespace + { + bool schemeIsLocalDir( const Url & url_r ) + { + std::string s( url_r.getScheme() ); + return s == "dir" || s == "file"; + } + } + + Pathname Package::cachedLocation() const + { + OnMediaLocation loc( location() ); + PathInfo pi( repoInfo().packagesPath() / loc.filename() ); + + if ( ! pi.isExist() ) + return Pathname(); // no file in cache + + if ( loc.checksum().empty() ) + { + Url url( repoInfo().url() ); + if ( ! schemeIsLocalDir( url ) ) + return Pathname(); // same name but no checksum to verify + + // for local repos compare with the checksum in repo + if ( CheckSum( CheckSum::md5Type(), std::ifstream( (url.getPathName() / loc.filename()).c_str() ) ) + != CheckSum( CheckSum::md5Type(), std::ifstream( pi.c_str() ) ) ) + return Pathname(); // same name but wrong checksum + } + else + { + if ( loc.checksum() != CheckSum( loc.checksum().type(), std::ifstream( pi.c_str() ) ) ) + return Pathname(); // same name but wrong checksum + } + + return pi.path(); // the right one + } + std::string Package::sourcePkgName() const { // no id means same as package diff --git a/zypp/Package.h b/zypp/Package.h index de515c7..2219b8d 100644 --- a/zypp/Package.h +++ b/zypp/Package.h @@ -122,6 +122,13 @@ namespace zypp */ OnMediaLocation location() const; + /** Location of the downloaded package in cache or an empty path. */ + Pathname cachedLocation() const; + + /** Whether the package is cached. */ + bool isCached() const + { return ! cachedLocation().empty(); } + protected: friend Ptr make( const sat::Solvable & solvable_r ); /** Ctor */ -- 2.7.4