Canorus  0.7
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
/home/iurt/rpm/BUILD/canorus-0.7.R1002/src/core/tar.h
Go to the documentation of this file.
00001 
00008 #ifndef TAR_H_
00009 #define TAR_H_
00010 
00011 #include <QString>
00012 #include <QByteArray>
00013 #include <QFile>
00014 #include <QHash>
00015 #include <memory>
00016 using std::auto_ptr;
00017 
00018 class QIODevice;
00019 
00020 typedef auto_ptr<QIODevice> CAIOPtr;
00021 
00022 class CATar
00023 {
00024 public:
00025         CATar();
00026         CATar(QIODevice&);
00027         virtual ~CATar();
00028         bool addFile(const QString& filename, QIODevice& data, bool replace = true);
00029         bool addFile(const QString& filename, QByteArray data, bool replace = true);
00030         void removeFile(const QString& filename);
00031         inline bool contains(const QString& filename);
00032         CAIOPtr file(const QString& filename);
00033         qint64 write(QIODevice& dest, qint64 chunk);
00034         qint64 write(QIODevice& dest);
00035         inline bool open(QIODevice& dest) { if(_pos.contains(&dest)) { return false; } _pos[&dest].pos = _pos[&dest].file = _pos[&dest].eof = 0; return true; }
00036         inline void close(QIODevice& dest) { _pos.remove(&dest); }
00037         bool eof(QIODevice& dest);
00038         inline bool error() { return !_ok; }
00039 protected:
00040         static const int CHUNK;
00041         typedef struct {                        /* size in bytes (ASCII) */     
00042                 char    name[101];              /* 100 */
00043                 quint32 mode;                   /* 8   */
00044                 quint32 uid;                    /* 8   */
00045                 quint32 gid;                    /* 8   */
00046                 quint64 size;                   /* 12  */
00047                 quint64 mtime;                  /* 12  */
00048                 quint32 chksum;                 /* 8   */
00049                 char    typeflag;               /* 1   */
00050                 char    linkname[101];  /* 100 */
00051                 /* magic "ustar\0" */   /* 6   */                       
00052                 /* version "00" */              /* 2   */
00053                 char    uname[33];              /* 32  */
00054                 char    gname[33];              /* 32  */
00055                 // NULs                                 /* 16  */
00056                 char    prefix[156];    /* 155 */
00057                 // NULs                                 /* 12  */
00058         } CATarHeader;
00059         typedef struct {
00060                 CATarHeader     hdr;
00061                 QFile*          data;
00062         } CATarFile;
00063         QList<CATarFile*> _files;
00064         void parse(QIODevice& data);
00065         bool _ok;
00066         typedef struct {
00067                 qint64 pos;
00068                 qint32 file;
00069                 bool close;
00070                 bool eof;
00071         } CATarBufInfo;
00072         QHash<QIODevice*, CATarBufInfo> _pos;
00073         // helper functions
00074         char *bufncpy(char*, const char*, size_t, int = -1);
00075         char *bufncpyi(char*&, const char*, size_t, int = -1);
00076         char *numToOct(char*, qint64, int);
00077         char *numToOcti(char*&, qint64, int);
00078         void writeHeader(QIODevice& dest, int file);
00079 };
00080 
00081 #endif /* TAR_H_ */