libzypp  10.5.0
MediaBlockList.h
Go to the documentation of this file.
00001 /*---------------------------------------------------------------------\
00002 |                          ____ _   __ __ ___                          |
00003 |                         |__  / \ / / . \ . \                         |
00004 |                           / / \ V /|  _/  _/                         |
00005 |                          / /__ | | | | | |                           |
00006 |                         /_____||_| |_| |_|                           |
00007 |                                                                      |
00008 \---------------------------------------------------------------------*/
00012 #ifndef ZYPP_MEDIA_MEDIABLOCKLIST_H
00013 #define ZYPP_MEDIA_MEDIABLOCKLIST_H
00014 
00015 #include <sys/types.h>
00016 #include <vector>
00017 
00018 #include "zypp/Digest.h"
00019 
00020 namespace zypp {
00021   namespace media {
00022 
00026 struct MediaBlock {
00027   MediaBlock( off_t off_r, size_t size_r )
00028   : off( off_r )
00029   , size( size_r )
00030   {}
00031   off_t off;
00032   size_t size;
00033 };
00034 
00035 class MediaBlockList {
00036 public:
00037   MediaBlockList(off_t filesize=off_t(-1));
00038 
00043   inline bool haveBlocks() const {
00044     return haveblocks;
00045   }
00051   size_t addBlock(off_t off, size_t size);
00052 
00056   inline MediaBlock getBlock(size_t blkno) const {
00057     return blocks[blkno];
00058   }
00062   inline size_t numBlocks() const {
00063     return blocks.size();
00064   }
00065 
00069   inline void setFilesize(off_t newfilesize=off_t(-1)) {
00070     filesize = newfilesize;
00071   }
00072   inline off_t getFilesize() const {
00073     return filesize;
00074   }
00075   inline bool haveFilesize() const {
00076     return filesize != off_t(-1);
00077   }
00078 
00082   void setFileChecksum(std::string ctype, int cl, unsigned char *c);
00083   bool createFileDigest(Digest &digest) const;
00084   bool verifyFileDigest(Digest &digest) const;
00085   inline bool haveFileChecksum() const {
00086     return !fsumtype.empty() && fsum.size();
00087   }
00088 
00092   void setChecksum(size_t blkno, std::string cstype, int csl, unsigned char *cs, size_t cspad=0);
00093   bool checkChecksum(size_t blkno, const unsigned char *buf, size_t bufl) const;
00094   bool createDigest(Digest &digest) const;
00095   bool verifyDigest(size_t blkno, Digest &digest) const;
00096   inline bool haveChecksum(size_t blkno) const {
00097     return chksumlen && chksums.size() >= chksumlen * (blkno + 1);
00098   }
00099 
00103   void setRsum(size_t blkno, int rsl, unsigned int rs, size_t rspad=0);
00104   bool checkRsum(size_t blkno, const unsigned char *buf, size_t bufl) const;
00105   unsigned int updateRsum(unsigned int rs, const char *bytes, size_t len) const;
00106   bool verifyRsum(size_t blkno, unsigned int rs) const;
00107   inline bool haveRsum(size_t blkno) const {
00108     return rsumlen && rsums.size() >= blkno + 1;
00109   }
00110 
00115   void reuseBlocks(FILE *wfp, std::string filename);
00116 
00120   std::string asString() const;
00121 
00122 private:
00123   void writeBlock(size_t blkno, FILE *fp, const unsigned char *buf, size_t bufl, size_t start, std::vector<bool> &found) const;
00124   bool checkChecksumRotated(size_t blkno, const unsigned char *buf, size_t bufl, size_t start) const;
00125 
00126   off_t filesize;
00127   std::string fsumtype;
00128   std::vector<unsigned char> fsum;
00129 
00130   bool haveblocks;
00131   std::vector<MediaBlock> blocks;
00132 
00133   std::string chksumtype;
00134   int chksumlen;
00135   size_t chksumpad;
00136   std::vector<unsigned char> chksums;
00137 
00138   std::string rsumtype;
00139   int rsumlen;
00140   size_t rsumpad;
00141   std::vector<unsigned int> rsums;
00142 };
00143 
00144 inline std::ostream & operator<<(std::ostream &str, const MediaBlockList &bl)
00145 { return str << bl.asString(); }
00146 
00147   } // namespace media
00148 } // namespace zypp
00149 
00150 #endif // ZYPP_MEDIA_MEDIABLOCKLIST_H
00151