Imported Upstream version 15.0.0
[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   class PublicKey;
28
29   /**
30    * Functor signature used to check files.
31    * \param file File to check.
32    *
33    * \throws FileCheckException when the file does not
34    * validate and the user don't want to continue.
35    */
36   typedef function<void ( const Pathname &file )> FileChecker;
37
38   class FileCheckException : public Exception
39   {
40   public:
41     FileCheckException(const std::string &msg)
42       : Exception(msg)
43     {}
44   };
45
46   class CheckSumCheckException : public FileCheckException
47   {
48     //TODO
49   };
50
51   class SignatureCheckException : public FileCheckException
52   {
53     //TODO
54   };
55
56   /**
57    * Built in file checkers
58    */
59
60   /**
61    * \short Checks for a valid checksum and interacts with the user.
62    */
63    class ChecksumFileChecker
64    {
65    public:
66      /**
67       * Constructor.
68       * \param checksum Checksum that validates the file
69       */
70      ChecksumFileChecker( const CheckSum &checksum );
71      /**
72       * \short Try to validate the file
73       * \param file File to validate.
74       *
75       * \throws CheckSumCheckException if validation fails
76       */
77      void operator()( const Pathname &file ) const;
78    private:
79      CheckSum _checksum;
80    };
81
82    /**
83     * \short Checks for the validity of a signature
84     */
85    class SignatureFileChecker
86    {
87      public:
88       /**
89       * Constructor.
90       * \param signature Signature that validates the file
91       */
92       SignatureFileChecker( const Pathname &signature );
93
94       /**
95       * Default Constructor.
96       * \short Signature for unsigned files
97       * Use it when you dont have a signature but you want
98       * to check the user to accept an unsigned file.
99       */
100       SignatureFileChecker();
101
102       /**
103        * Set context for this checker.
104        *
105        * Use this method if you're not adding the key (with context) via
106        * one of the addPublicKey methods. The addPublicKey method overwrites
107        * the context.
108        */
109       void setKeyContext(const KeyContext & keycontext);
110
111       /**
112        * add a public key to the list of known keys
113        */
114       void addPublicKey( const PublicKey & publickey, const KeyContext & keycontext = KeyContext());
115       /** \overload Convenience taking the public keys pathname. */
116       void addPublicKey( const Pathname & publickey, const KeyContext & keycontext = KeyContext());
117
118       /**
119       * \short Try to validate the file
120       * \param file File to validate.
121       *
122       * \throws SignatureCheckException if validation fails
123       */
124       void operator()( const Pathname &file ) const;
125
126      protected:
127       Pathname _signature;
128       KeyContext _context;
129    };
130
131    /**
132    * \short Checks for nothing
133    * Used as the default checker
134    */
135    class NullFileChecker
136    {
137    public:
138      void operator()( const Pathname &file )  const;
139    };
140
141    /**
142     * \short Checker composed of more checkers.
143     *
144     * Allows to create a checker composed of various
145     * checkers altothether. It will only
146     * validate if all the checkers validate.
147     *
148     * \code
149     * CompositeFileChecker com;
150     * com.add(checker1);
151     * com.add(checker2);
152     * fetcher.enqueue(location, com);
153     * \endcode
154     */
155    class CompositeFileChecker
156    {
157    public:
158      void add( const FileChecker &checker );
159     /**
160      * \throws FileCheckException if validation fails
161      */
162      void operator()( const Pathname &file ) const;
163
164      int checkersSize() const { return _checkers.size(); }
165    private:
166      std::list<FileChecker> _checkers;
167    };
168
169   /** \relates FileChecker Stream output */
170   std::ostream & operator<<( std::ostream & str, const FileChecker & obj );
171
172   /////////////////////////////////////////////////////////////////
173 } // namespace zypp
174 ///////////////////////////////////////////////////////////////////
175 #endif // ZYPP_FILECHECKER_H