Deprecate MediaAccess::downloads (accidentally deleted)
[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       /**
104        * add a public key to the list of known keys
105        */
106       void addPublicKey( const PublicKey & publickey, const KeyContext & keycontext = KeyContext());
107       /** \overload Convenience taking the public keys pathname. */
108       void addPublicKey( const Pathname & publickey, const KeyContext & keycontext = KeyContext());
109
110       /**
111       * \short Try to validate the file
112       * \param file File to validate.
113       *
114       * \throws SignatureCheckException if validation fails
115       */
116       void operator()( const Pathname &file ) const;
117
118      protected:
119       Pathname _signature;
120       KeyContext _context;
121    };
122
123    /**
124    * \short Checks for nothing
125    * Used as the default checker
126    */
127    class NullFileChecker
128    {
129    public:
130      void operator()( const Pathname &file )  const;
131    };
132
133    /**
134     * \short Checker composed of more checkers.
135     *
136     * Allows to create a checker composed of various
137     * checkers altothether. It will only
138     * validate if all the checkers validate.
139     *
140     * \code
141     * CompositeFileChecker com;
142     * com.add(checker1);
143     * com.add(checker2);
144     * fetcher.enqueue(location, com);
145     * \endcode
146     */
147    class CompositeFileChecker
148    {
149    public:
150      void add( const FileChecker &checker );
151     /**
152      * \throws FileCheckException if validation fails
153      */
154      void operator()( const Pathname &file ) const;
155
156      int checkersSize() const { return _checkers.size(); }
157    private:
158      std::list<FileChecker> _checkers;
159    };
160
161   /** \relates FileChecker Stream output */
162   std::ostream & operator<<( std::ostream & str, const FileChecker & obj );
163
164   /////////////////////////////////////////////////////////////////
165 } // namespace zypp
166 ///////////////////////////////////////////////////////////////////
167 #endif // ZYPP_FILECHECKER_H