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