libzypp 17.38.5

checksumwf.cc
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8\---------------------------------------------------------------------*/
9
10#include "checksumwf.h"
11
12
13#include <map>
14#include <utility>
15
16#include <zypp-core/CheckSum.h>
17#include <zypp-core/Date.h>
18#include <zypp-core/Pathname.h>
19#include <zypp-core/base/Gettext.h>
20#include <zypp-core/fs/PathInfo.h>
21#include <zypp-core/ng/pipelines/Expected>
22#include <zypp-media/FileCheckException>
23#include <zypp/Digest.h>
24#include <zypp/ng/UserRequest>
25#include <zypp/ng/context.h>
26#include <zypp/ng/media/provide.h>
27#include <zypp/ng/reporthelper.h>
28
29 namespace zyppng::CheckSumWorkflow {
30
31 using namespace zyppng::operators;
32
33 struct CheckSumWorkflowLogic
34 {
35
36 using ProvideType = typename Context::ProvideType;
37 using MediaHandle = typename ProvideType::MediaHandle;
38 using ProvideRes = typename ProvideType::Res;
39
40 CheckSumWorkflowLogic( ContextRef zyppContext, zypp::CheckSum &&checksum, zypp::Pathname file )
41 : _context( std::move(zyppContext) )
42 , _report( _context )
43 , _checksum(std::move( checksum ))
44 , _file(std::move( file ))
45 {}
46
47 auto execute()
48 {
49
50 //MIL << "checking " << file << " file against checksum '" << _checksum << "'" << endl;
51 if ( _checksum.empty() ) {
52 MIL << "File " << _file << " has no checksum available." << std::endl;
53 if ( _report.askUserToAcceptNoDigest( _file ) ) {
54 MIL << "User accepted " << _file << " with no checksum." << std::endl;
55 return makeReadyTask( expected<void>::success() );
56 } else {
57 return makeReadyTask( expected<void>::error( ZYPP_EXCPT_PTR(zypp::FileCheckException( _file.basename() + " has no checksum" ) ) ) );
58 }
59
60 } else {
61
62 return _context->provider()->checksumForFile ( _file, _checksum.type() )
63 | [] ( expected<zypp::CheckSum> sum ) {
64 if ( !sum )
65 return zypp::CheckSum( );
66 return sum.get();
67 }
68 | [this, _file=_file]( zypp::CheckSum real_checksum ){
69 if ( (real_checksum != _checksum) )
70 {
71 // Remember askUserToAcceptWrongDigest decision for at most 12hrs in memory;
72 // Actually we just want to prevent asking the same question again when the
73 // previously downloaded file is retrieved from the disk cache.
74 static std::map<std::string,std::string> exceptions;
75 static zypp::Date exceptionsAge;
76 zypp::Date now( zypp::Date::now() );
77 if ( !exceptions.empty() && now-exceptionsAge > 12*zypp::Date::hour )
78 exceptions.clear();
79
80 WAR << "File " << _file << " has wrong checksum " << real_checksum << " (expected " << _checksum << ")" << std::endl;
81 if ( !exceptions.empty() && exceptions[real_checksum.checksum()] == _checksum.checksum() )
82 {
83 WAR << "User accepted " << _file << " with WRONG CHECKSUM. (remembered)" << std::endl;
84 return expected<void>::success();
85 }
86 else if ( _report.askUserToAcceptWrongDigest( _file, _checksum.checksum(), real_checksum.checksum() ) )
87 {
88 WAR << "User accepted " << _file << " with WRONG CHECKSUM." << std::endl;
89 exceptions[real_checksum.checksum()] = _checksum.checksum();
90 exceptionsAge = now;
91 return expected<void>::success();
92 }
93 else
94 {
95 return expected<void>::error( ZYPP_EXCPT_PTR(zypp::FileCheckException( _file.basename() + " has wrong checksum" ) ) );
96 }
97 }
98 return expected<void>::success();
99 };
100 }
101 }
102
103 protected:
104 ContextRef _context;
105 DigestReportHelper _report;
106 zypp::CheckSum _checksum;
107 zypp::Pathname _file;
108
109 };
110
111 MaybeAwaitable<expected<void>> verifyChecksum( ContextRef zyppCtx, zypp::CheckSum checksum, zypp::filesystem::Pathname file )
112 {
113 CheckSumWorkflowLogic impl( std::move(zyppCtx), std::move(checksum), std::move(file) );
114 zypp_co_return zypp_co_await( impl.execute() );
115 }
116
117 std::function<MaybeAwaitable<expected<ProvideRes> > (ProvideRes &&)> checksumFileChecker( ContextRef zyppCtx, zypp::CheckSum checksum )
118 {
119 using zyppng::operators::operator|;
120 return [ zyppCtx = std::move(zyppCtx), checksum=std::move(checksum) ]( ProvideRes res ) mutable -> MaybeAwaitable<expected<ProvideRes>> {
121 return verifyChecksum( zyppCtx, std::move(checksum), res.file() )
122 | [ res ] ( expected<void> result ) mutable {
123 if ( result )
124 return expected<ProvideRes>::success( std::move(res) );
125 else
126 return expected<ProvideRes>::error( std::move(result.error()) );
127 };
128 };
129 }
130
131}
Date.h
ZYPP_EXCPT_PTR
#define ZYPP_EXCPT_PTR(EXCPT)
Drops a logline and returns Exception as a std::exception_ptr.
Definition Exception.h:463
Gettext.h
MIL
#define MIL
Definition Logger.h:100
WAR
#define WAR
Definition Logger.h:101
Pathname.h
checksumwf.h
zypp::CheckSum
Definition CheckSum.h:34
zypp::Date
Store and operate on date (time_t).
Definition Date.h:33
zypp::Date::hour
static const ValueType hour
Definition Date.h:43
zypp::Date::now
static Date now()
Return the current time.
Definition Date.h:78
zypp::FileCheckException
Definition filecheckexception.h:18
zypp::filesystem::Pathname
Pathname.
Definition Pathname.h:48
zyppng::Context::ProvideType
Provide ProvideType
Definition context.h:34
zyppng::DigestReportHelper
Definition reporthelper.h:78
zyppng::ProvideRes
Definition provide.h:44
zyppng::expected
Definition expected.h:56
zyppng::expected::success
static expected success(ConsParams &&...params)
Definition expected.h:178
zyppng::expected::error
static expected error(ConsParams &&...params)
Definition expected.h:189
zyppng::expected::error
E & error()
Definition expected.h:288
context.h
std
Definition ansi.h:855
zyppng::CheckSumWorkflow
Definition checksumwf.cc:29
zyppng::CheckSumWorkflow::verifyChecksum
MaybeAwaitable< expected< void > > verifyChecksum(ContextRef zyppCtx, zypp::CheckSum checksum, zypp::filesystem::Pathname file)
Definition checksumwf.cc:111
zyppng::CheckSumWorkflow::checksumFileChecker
std::function< MaybeAwaitable< expected< ProvideRes > >(ProvideRes &&)> checksumFileChecker(ContextRef zyppCtx, zypp::CheckSum checksum)
Definition checksumwf.cc:117
zyppng::operators
Definition expected.h:699
provide.h
reporthelper.h
zyppng::CheckSumWorkflow::CheckSumWorkflowLogic
Definition checksumwf.cc:34
zyppng::CheckSumWorkflow::CheckSumWorkflowLogic::ProvideRes
typename ProvideType::Res ProvideRes
Definition checksumwf.cc:38
zyppng::CheckSumWorkflow::CheckSumWorkflowLogic::_checksum
zypp::CheckSum _checksum
Definition checksumwf.cc:106
zyppng::CheckSumWorkflow::CheckSumWorkflowLogic::_report
DigestReportHelper _report
Definition checksumwf.cc:105
zyppng::CheckSumWorkflow::CheckSumWorkflowLogic::_file
zypp::Pathname _file
Definition checksumwf.cc:107
zyppng::CheckSumWorkflow::CheckSumWorkflowLogic::execute
auto execute()
Definition checksumwf.cc:47
zyppng::CheckSumWorkflow::CheckSumWorkflowLogic::MediaHandle
typename ProvideType::MediaHandle MediaHandle
Definition checksumwf.cc:37
zyppng::CheckSumWorkflow::CheckSumWorkflowLogic::CheckSumWorkflowLogic
CheckSumWorkflowLogic(ContextRef zyppContext, zypp::CheckSum &&checksum, zypp::Pathname file)
Definition checksumwf.cc:40
zyppng::CheckSumWorkflow::CheckSumWorkflowLogic::ProvideType
typename Context::ProvideType ProvideType
Definition checksumwf.cc:36
zyppng::CheckSumWorkflow::CheckSumWorkflowLogic::_context
ContextRef _context
Definition checksumwf.cc:104
CheckSum.h
PathInfo.h
Digest.h