Canorus  0.7
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
tar.h
Go to the documentation of this file.
1 
8 #ifndef TAR_H_
9 #define TAR_H_
10 
11 #include <QString>
12 #include <QByteArray>
13 #include <QFile>
14 #include <QHash>
15 #include <memory>
16 using std::auto_ptr;
17 
18 class QIODevice;
19 
20 typedef auto_ptr<QIODevice> CAIOPtr;
21 
22 class CATar
23 {
24 public:
25  CATar();
26  CATar(QIODevice&);
27  virtual ~CATar();
28  bool addFile(const QString& filename, QIODevice& data, bool replace = true);
29  bool addFile(const QString& filename, QByteArray data, bool replace = true);
30  void removeFile(const QString& filename);
31  inline bool contains(const QString& filename);
32  CAIOPtr file(const QString& filename);
33  qint64 write(QIODevice& dest, qint64 chunk);
34  qint64 write(QIODevice& dest);
35  inline bool open(QIODevice& dest) { if(_pos.contains(&dest)) { return false; } _pos[&dest].pos = _pos[&dest].file = _pos[&dest].eof = 0; return true; }
36  inline void close(QIODevice& dest) { _pos.remove(&dest); }
37  bool eof(QIODevice& dest);
38  inline bool error() { return !_ok; }
39 protected:
40  static const int CHUNK;
41  typedef struct { /* size in bytes (ASCII) */
42  char name[101]; /* 100 */
43  quint32 mode; /* 8 */
44  quint32 uid; /* 8 */
45  quint32 gid; /* 8 */
46  quint64 size; /* 12 */
47  quint64 mtime; /* 12 */
48  quint32 chksum; /* 8 */
49  char typeflag; /* 1 */
50  char linkname[101]; /* 100 */
51  /* magic "ustar\0" */ /* 6 */
52  /* version "00" */ /* 2 */
53  char uname[33]; /* 32 */
54  char gname[33]; /* 32 */
55  // NULs /* 16 */
56  char prefix[156]; /* 155 */
57  // NULs /* 12 */
58  } CATarHeader;
59  typedef struct {
61  QFile* data;
62  } CATarFile;
63  QList<CATarFile*> _files;
64  void parse(QIODevice& data);
65  bool _ok;
66  typedef struct {
67  qint64 pos;
68  qint32 file;
69  bool close;
70  bool eof;
71  } CATarBufInfo;
72  QHash<QIODevice*, CATarBufInfo> _pos;
73  // helper functions
74  char *bufncpy(char*, const char*, size_t, int = -1);
75  char *bufncpyi(char*&, const char*, size_t, int = -1);
76  char *numToOct(char*, qint64, int);
77  char *numToOcti(char*&, qint64, int);
78  void writeHeader(QIODevice& dest, int file);
79 };
80 
81 #endif /* TAR_H_ */