HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_Archive.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: UT_Archive.h
7  *
8  * COMMENTS:
9  *
10  */
11 
12 #ifndef __UT_ARCHIVE_H__
13 #define __UT_ARCHIVE_H__
14 
15 #include "UT_API.h"
16 
17 #include "UT_NonCopyable.h"
18 #include "UT_SharedPtr.h"
19 #include "UT_UniquePtr.h"
20 #include "UT_StringHolder.h"
21 #if defined(WIN32)
22  #include "UT_NTUtil.h"
23 #endif
24 
25 #include <SYS/SYS_Compiler.h>
26 
27 struct archive_entry;
28 struct archive;
29 
30 class UT_ArchiveReader;
31 class UT_WorkBuffer;
32 
34 {
35 public:
36  static const int OK;
37  static const int WARN;
38  static const int FATAL;
39  static const int EXTRACT_ACL;
41  static const int EXTRACT_MAC_METADATA;
42  static const int EXTRACT_TIME;
43  static const int EXTRACT_XATTR;
44 };
45 
47 {
48 public:
49  int64_t gid() const;
50  int64_t uid() const;
51  int64_t size() const;
52  int64_t ino64() const;
53  mode_t fileType() const;
54 
55  UT_StringHolder gname() const;
56  UT_StringHolder hardlink() const;
57  UT_StringHolder pathname() const;
58  UT_StringHolder symlink() const;
59  UT_StringHolder uname() const;
60 
61  void setGid(int64_t gid);
62  void setUid(int64_t uid);
63  void setSize(int64_t size);
64  void setIno64(int64_t ino);
65 
66  void setGName(const char* gname);
67  void setHardlink(const char* hardlink);
68  void setPathname(const char* pathname);
69  void setSymlink(const char* symlink);
70  void setUName(const char* uname);
71  void setMTime(time_t t, int64_t v);
72 
73  bool operator==(const UT_ArchiveEntry& entry) const
74  {
75  return myEntry == entry.myEntry;
76  }
77  bool operator!=(const UT_ArchiveEntry& entry) const
78  {
79  return !(*this == entry);
80  }
81  explicit operator bool() const
82  {
83  return myEntry != nullptr;
84  }
85 private:
86  friend class UT_ArchiveReader;
87 
88  UT_ArchiveEntry(archive_entry* entry);
89 
91 };
92 
94 {
95 private:
97 public:
98  ~UT_ArchiveReader() = default;
100 
101  // Movable but non-copyable
103  myArchive(std::move(a.myArchive))
104  {}
106  {
107  myArchive = std::move(a.myArchive);
108  return *this;
109  }
110 
111  enum class format
112  {
113  _ALL,
114  _ZIP,
115  _RAR,
116  _TAR,
117  _RAW,
118  _7ZIP,
119  _AR,
120  _CAB,
121  _CPIO,
122  _ISO9660,
123  _LHA,
124  _MTREE,
125  _XAR
126  };
127  enum class filter
128  {
129  _NONE,
130  _ALL,
131  _BZIP2,
132  _COMPRESS,
133  _GZIP,
134  _LZIP,
135  _LZMA,
136  _XZ,
137  _UU,
138  _RPM,
139  _LRZIP,
140  _LZOP,
141  _GRZIP
142  };
143 
144  /// Deleter for smart pointers
146  {
147  void operator()(archive* a);
148  };
149 
151  {
152  public:
154  : myReader(reader), myEntry(entry)
155  {
156  }
157 
158  bool operator==(const iterator& other) const
159  {
160  return myEntry == other.myEntry;
161  }
162  bool operator!=(const iterator& other) const
163  {
164  return !(*this == other);
165  }
166 
167  UT_ArchiveEntry operator*() { return myEntry; }
168 
170  {
171  myEntry = myReader->nextEntry();
172  return *this;
173  }
174 
175  private:
176  UT_ArchiveReader* myReader;
177  UT_ArchiveEntry myEntry;
178  };
179 
180  /// @{
181  /// Begin and end iterator suitable for iterating over the entries in the
182  /// archive.
183  iterator begin() { return iterator(this, nextEntry()); }
184  iterator end() { return iterator(this, nullptr); }
185  /// @}
186 
187  /// @{
188  /// Create an archive ready for reading.
189  template <format FORMAT = format::_ALL, filter FILTER = filter::_ALL>
191  const char* filename,
192  size_t block_size)
193  {
195  a.initFormat<FORMAT>();
196  a.initFilter<FILTER>();
197  a.openFile(filename, block_size);
198  return a;
199  }
200  template <format FORMAT = format::_ALL, filter FILTER = filter::_ALL>
202  const wchar_t* filename,
203  size_t block_size)
204  {
206  a.initFormat<FORMAT>();
207  a.initFilter<FILTER>();
208  a.openFileW(filename, block_size);
209  return a;
210  }
211  /// @}
212 
213  /// Get the next entry in the archive.
215 
216  /// True if the archive can continue to be used.
217  SYS_NO_DISCARD_RESULT bool isValid() const;
218  explicit operator bool() const { return isValid(); }
219 
220  /// Get the current error string for the archive.
221  SYS_NO_DISCARD_RESULT UT_StringHolder errorString() const;
222 
223  /// Skip the current entry.
224  int readSkip();
225  /// Read the contents of the entry into the workbuffer.
226  int read(UT_ArchiveEntry& entry, UT_WorkBuffer& data);
227  /// Extract the entry from the archive.
228  int extract(UT_ArchiveEntry& entry, int flags);
229 
230  /// Helper to read a specific file from an archive file
231  static bool readFile(
232  const char* filename,
233  const UT_StringHolder& pathname,
235  {
236  UT_ArchiveReader reader(create(filename, 1240));
237  if (!reader)
238  return false;
239 
240  for (auto&& e : reader)
241  {
242  if (e.pathname() != pathname)
243  continue;
244 
245  int r = reader.read(e, data);
246  return r > 0;
247  }
248  return false;
249  }
250 
251  /// @{
252  /// Init the format and filter for the archive. In most cases create()
253  /// should be used in favour of these functions.
254  template <format FORMAT>
255  void initFormat();
256  template <filter FILTER>
257  void initFilter();
258  /// @}
259 
260  /// @{
261  /// Open an archive file. These functions are only used if extra
262  /// functionality is required. create() should be used in most cases.
263  int openFileW(const wchar_t* filename, size_t block_size);
264  int openFile(const char* filename, size_t block_size);
265  /// @}
266 
267 private:
269 };
270 
271 #endif // __UT_ARCHIVE_H__
272 
bool operator==(const UT_ArchiveEntry &entry) const
Definition: UT_Archive.h:73
GLbitfield flags
Definition: glcorearb.h:1596
GT_API const UT_StringHolder filename
const GLdouble * v
Definition: glcorearb.h:837
static bool readFile(const char *filename, const UT_StringHolder &pathname, UT_WorkBuffer &data)
Helper to read a specific file from an archive file.
Definition: UT_Archive.h:231
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1222
#define UT_API
Definition: UT_API.h:14
Deleter for smart pointers.
Definition: UT_Archive.h:145
int openFileW(const wchar_t *filename, size_t block_size)
static const int OK
Definition: UT_Archive.h:36
void read(T &in, bool &v)
Definition: ImfXdr.h:502
static const int EXTRACT_ACL
Definition: UT_Archive.h:39
Definition: UT_Archive.h:46
static const int EXTRACT_TIME
Definition: UT_Archive.h:42
std::unique_ptr< T, Deleter > UT_UniquePtr
A smart pointer for unique ownership of dynamically allocated objects.
Definition: UT_UniquePtr.h:39
OIIO_FORCEINLINE bool extract(const vbool4 &a)
Definition: simd.h:3426
static const int EXTRACT_MAC_METADATA
Definition: UT_Archive.h:41
UT_ArchiveEntry operator*()
Definition: UT_Archive.h:167
iterator end()
Definition: UT_Archive.h:184
std::shared_ptr< T > UT_SharedPtr
Wrapper around std::shared_ptr.
Definition: UT_SharedPtr.h:36
static const int WARN
Definition: UT_Archive.h:37
#define UT_NON_COPYABLE(CLASS)
Define deleted copy constructor and assignment operator inside a class.
bool operator==(const iterator &other) const
Definition: UT_Archive.h:158
#define SYS_NO_DISCARD_RESULT
Definition: SYS_Compiler.h:93
static const int EXTRACT_CLEAR_NOCHANGE_FFLAGS
Definition: UT_Archive.h:40
GLdouble t
Definition: glad.h:2397
static const int EXTRACT_XATTR
Definition: UT_Archive.h:43
GLsizeiptr size
Definition: glcorearb.h:664
int openFile(const char *filename, size_t block_size)
iterator begin()
Definition: UT_Archive.h:183
bool operator!=(const iterator &other) const
Definition: UT_Archive.h:162
UT_ArchiveReader & operator=(UT_ArchiveReader &&a)
Definition: UT_Archive.h:105
static const int FATAL
Definition: UT_Archive.h:38
GLboolean r
Definition: glcorearb.h:1222
static SYS_NO_DISCARD_RESULT UT_ArchiveReader createW(const wchar_t *filename, size_t block_size)
Definition: UT_Archive.h:201
iterator(UT_ArchiveReader *reader, const UT_ArchiveEntry &entry)
Definition: UT_Archive.h:153
bool operator!=(const UT_ArchiveEntry &entry) const
Definition: UT_Archive.h:77
Definition: format.h:895
static SYS_NO_DISCARD_RESULT UT_ArchiveReader create(const char *filename, size_t block_size)
Definition: UT_Archive.h:190