- provided context to keyring callbacks (bnc #370223)
[platform/upstream/libzypp.git] / zypp / FileChecker.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/FileChecker.h
10  *
11 */
12 #ifndef ZYPP_FILECHECKER_H
13 #define ZYPP_FILECHECKER_H
14
15 #include <iosfwd>
16 #include <list>
17 #include "zypp/base/Exception.h"
18 #include "zypp/base/Function.h"
19 #include "zypp/PathInfo.h"
20 #include "zypp/CheckSum.h"
21 #include "zypp/KeyContext.h"
22
23 ///////////////////////////////////////////////////////////////////
24 namespace zypp
25 { /////////////////////////////////////////////////////////////////
26
27   /**
28    * Functor signature used to check files.
29    * \param file File to check.
30    *
31    * \throws FileCheckException when the file does not
32    * validate and the user don't want to continue.
33    */
34   typedef function<void ( const Pathname &file )> FileChecker;
35   
36   class FileCheckException : public Exception
37   {
38   public:
39     FileCheckException(const std::string &msg)
40       : Exception(msg)
41     {}
42   };
43   
44   class CheckSumCheckException : public FileCheckException
45   {
46     //TODO
47   };
48   
49   class SignatureCheckException : public FileCheckException
50   {
51     //TODO
52   };
53   
54   /**
55    * Built in file checkers
56    */
57   
58   /**
59    * \short Checks for a valid checksum and interacts with the user.
60    */
61    class ChecksumFileChecker
62    {
63    public:
64      /**
65       * Constructor.
66       * \param checksum Checksum that validates the file
67       */
68      ChecksumFileChecker( const CheckSum &checksum );
69      /**
70       * \short Try to validate the file
71       * \param file File to validate.
72       *
73       * \throws CheckSumCheckException if validation fails
74       */
75      void operator()( const Pathname &file ) const;
76    private:
77      CheckSum _checksum;
78    };
79    
80    /**
81     * \short Checks for the validity of a signature
82     */
83    class SignatureFileChecker
84    {
85      public:
86       /**
87       * Constructor.
88       * \param signature Signature that validates the file
89       */
90       SignatureFileChecker( const Pathname &signature );
91
92       /**
93       * Default Constructor.
94       * \short Signature for unsigned files
95       * Use it when you dont have a signature but you want
96       * to check the user to accept an unsigned file.
97       */
98       SignatureFileChecker();
99
100
101       /**
102        * add a public key to the list of known keys
103        */
104       void addPublicKey( const Pathname &publickey, const KeyContext & keycontext = KeyContext());
105       /**
106       * \short Try to validate the file
107       * \param file File to validate.
108       *
109       * \throws SignatureCheckException if validation fails
110       */
111       void operator()( const Pathname &file ) const;
112
113      protected:
114       Pathname _signature;
115       KeyContext _context;
116    };
117
118    /**
119    * \short Checks for nothing
120    * Used as the default checker
121    */
122    class NullFileChecker
123    {
124    public:
125      void operator()( const Pathname &file )  const;
126    };
127     
128    /**
129     * \short Checker composed of more checkers.
130     * 
131     * Allows to create a checker composed of various
132     * checkers altothether. It will only
133     * validate if all the checkers validate.
134     *
135     * \code
136     * CompositeFileChecker com;
137     * com.add(checker1);
138     * com.add(checker2);
139     * fetcher.enqueue(location, com);
140     * \endcode
141     */
142    class CompositeFileChecker
143    {
144    public:
145      void add( const FileChecker &checker );
146     /**
147      * \throws FileCheckException if validation fails
148      */
149      void operator()( const Pathname &file ) const;
150
151      int checkersSize() const { return _checkers.size(); }
152    private:
153      std::list<FileChecker> _checkers;
154    };
155
156   /** \relates FileChecker Stream output */
157   std::ostream & operator<<( std::ostream & str, const FileChecker & obj );
158
159   /////////////////////////////////////////////////////////////////
160 } // namespace zypp
161 ///////////////////////////////////////////////////////////////////
162 #endif // ZYPP_FILECHECKER_H