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;
101 * \param signature Signature that validates the file
103 SignatureFileChecker( const Pathname &signature );
106 * Default Constructor.
107 * \short Signature for unsigned files
108 * Use it when you dont have a signature but you want
109 * to check the user to accept an unsigned file.
111 SignatureFileChecker();
114 * Set context for this checker.
116 * Use this method if you're not adding the key (with context) via
117 * one of the addPublicKey methods. The addPublicKey method overwrites
120 void setKeyContext(const KeyContext & keycontext);
122 /** Return the current context */
123 const KeyContext & keyContext() const
126 /** Return whether the last file passed to \ref operator() was accepted.
127 * If this is \ref false \ref operator() was not invoked or threw a
128 * \ref SignatureCheckException.
130 bool fileAccepted() const
131 { return _fileAccepted; }
133 /** Return whether the last file passed to \ref operator() was actually sucessfully verified.
134 * If this is \c false but \ref fileAccepted, the file was accepted due to user interaction or
135 * global settings, but the signature was not verified.
137 bool fileValidated() const
138 { return _fileValidated; }
141 * add a public key to the list of known keys
143 void addPublicKey( const PublicKey & publickey, const KeyContext & keycontext = KeyContext());
144 /** \overload Convenience taking the public keys pathname. */
145 void addPublicKey( const Pathname & publickey, const KeyContext & keycontext = KeyContext());
148 * Calls \ref KeyRing::verifyFileSignatureWorkflow to verify the file.
150 * Keep in mind the the workflow may return \c true (file accepted) due to user interaction
151 * or global defaults even if a signature was not actually sucessfully verified. Whether a
152 * signature was actually sucessfully verified can be determined by checking \ref fileValidated
153 * which is invokes IFF a signature for this file actually validated.
155 * \param file File to validate.fileValidated
157 * \throws SignatureCheckException if validation fails
159 void operator()( const Pathname &file ) const;
164 mutable DefaultIntegral<bool,false> _fileAccepted;
165 mutable DefaultIntegral<bool,false> _fileValidated;
169 * \short Checks for nothing
170 * Used as the default checker
172 class NullFileChecker
175 void operator()( const Pathname &file ) const;
179 * \short Checker composed of more checkers.
181 * Allows to create a checker composed of various
182 * checkers altothether. It will only
183 * validate if all the checkers validate.
186 * CompositeFileChecker com;
189 * fetcher.enqueue(location, com);
192 class CompositeFileChecker
195 void add( const FileChecker &checker );
197 * \throws FileCheckException if validation fails
199 void operator()( const Pathname &file ) const;
201 int checkersSize() const { return _checkers.size(); }
203 std::list<FileChecker> _checkers;
206 /** \relates FileChecker Stream output */
207 std::ostream & operator<<( std::ostream & str, const FileChecker & obj );
209 /////////////////////////////////////////////////////////////////
211 ///////////////////////////////////////////////////////////////////
212 #endif // ZYPP_FILECHECKER_H