1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/FileChecker.h
12 #ifndef ZYPP_FILECHECKER_H
13 #define ZYPP_FILECHECKER_H
17 #include "zypp/base/DefaultIntegral.h"
18 #include "zypp/base/Exception.h"
19 #include "zypp/base/Function.h"
20 #include "zypp/PathInfo.h"
21 #include "zypp/CheckSum.h"
22 #include "zypp/KeyContext.h"
24 ///////////////////////////////////////////////////////////////////
26 { /////////////////////////////////////////////////////////////////
31 * Functor signature used to check files.
32 * \param file File to check.
34 * \throws FileCheckException when the file does not
35 * validate and the user don't want to continue.
37 typedef function<void ( const Pathname &file )> FileChecker;
39 class FileCheckException : public Exception
42 FileCheckException(const std::string &msg)
47 class CheckSumCheckException : public FileCheckException
50 CheckSumCheckException(const std::string &msg)
51 : FileCheckException(msg)
55 class SignatureCheckException : public FileCheckException
58 SignatureCheckException(const std::string &msg)
59 : FileCheckException(msg)
64 * Built in file checkers
68 * \short Checks for a valid checksum and interacts with the user.
70 class ChecksumFileChecker
73 typedef CheckSumCheckException ExceptionType;
76 * \param checksum Checksum that validates the file
78 ChecksumFileChecker( const CheckSum &checksum );
80 * \short Try to validate the file
81 * \param file File to validate.
83 * \throws CheckSumCheckException if validation fails
85 void operator()( const Pathname &file ) const;
91 * \short Checks for the validity of a signature
93 class SignatureFileChecker
96 typedef SignatureCheckException ExceptionType;
97 typedef function<void ( const SignatureFileChecker & checker, const Pathname & file )> OnSigValidated;
102 * \param signature Signature that validates the file
104 SignatureFileChecker( const Pathname &signature );
107 * Default Constructor.
108 * \short Signature for unsigned files
109 * Use it when you dont have a signature but you want
110 * to check the user to accept an unsigned file.
112 SignatureFileChecker();
115 * Set context for this checker.
117 * Use this method if you're not adding the key (with context) via
118 * one of the addPublicKey methods. The addPublicKey method overwrites
121 void setKeyContext(const KeyContext & keycontext);
123 /** Return the current context */
124 const KeyContext & keyContext() const
127 /** Return whether the last file passed to \ref operator() was accepted.
128 * If this is \ref false \ref operator() was not invoked or threw a
129 * \ref SignatureCheckException.
131 bool fileAccepted() const
132 { return _fileAccepted; }
134 /** Return whether the last file passed to \ref operator() was actually sucessfully verified.
135 * If this is \c false but \ref fileAccepted, the file was accepted due to user interaction or
136 * global settings, but the signature was not verified.
138 bool fileValidated() const
139 { return _fileValidated; }
142 * add a public key to the list of known keys
144 void addPublicKey( const PublicKey & publickey, const KeyContext & keycontext = KeyContext());
145 /** \overload Convenience taking the public keys pathname. */
146 void addPublicKey( const Pathname & publickey, const KeyContext & keycontext = KeyContext());
149 * Calls \ref KeyRing::verifyFileSignatureWorkflow to verify the file.
151 * Keep in mind the the workflow may return \c true (file accepted) due to user interaction
152 * or global defaults even if a signature was not actually sucessfully verified. Whether a
153 * signature was actually sucessfully verified can be determined by checking \ref fileValidated
154 * which is invokes IFF a signature for this file actually validated.
156 * \param file File to validate.fileValidated
158 * \throws SignatureCheckException if validation fails
160 void operator()( const Pathname &file ) const;
165 mutable DefaultIntegral<bool,false> _fileAccepted;
166 mutable DefaultIntegral<bool,false> _fileValidated;
170 * \short Checks for nothing
171 * Used as the default checker
173 class NullFileChecker
176 void operator()( const Pathname &file ) const;
180 * \short Checker composed of more checkers.
182 * Allows to create a checker composed of various
183 * checkers altothether. It will only
184 * validate if all the checkers validate.
187 * CompositeFileChecker com;
190 * fetcher.enqueue(location, com);
193 class CompositeFileChecker
196 void add( const FileChecker &checker );
198 * \throws FileCheckException if validation fails
200 void operator()( const Pathname &file ) const;
202 int checkersSize() const { return _checkers.size(); }
204 std::list<FileChecker> _checkers;
207 /** \relates FileChecker Stream output */
208 std::ostream & operator<<( std::ostream & str, const FileChecker & obj );
210 /////////////////////////////////////////////////////////////////
212 ///////////////////////////////////////////////////////////////////
213 #endif // ZYPP_FILECHECKER_H