tarstrm.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: tarstrm.h
  3. // Purpose: interface of wxTar* classes
  4. // Author: wxWidgets team
  5. // Licence: wxWindows licence
  6. /////////////////////////////////////////////////////////////////////////////
  7. /** wxTarEntry::GetTypeFlag() values */
  8. enum wxTarType
  9. {
  10. wxTAR_REGTYPE = '0', //!< regular file
  11. wxTAR_LNKTYPE = '1', //!< hard link
  12. wxTAR_SYMTYPE = '2', //!< symbolic link
  13. wxTAR_CHRTYPE = '3', //!< character special
  14. wxTAR_BLKTYPE = '4', //!< block special
  15. wxTAR_DIRTYPE = '5', //!< directory
  16. wxTAR_FIFOTYPE = '6', //!< named pipe
  17. wxTAR_CONTTYPE = '7' //!< contiguous file
  18. };
  19. /** Archive Formats (use wxTAR_PAX, it's backward compatible) used by wxTarEntry */
  20. enum wxTarFormat
  21. {
  22. wxTAR_USTAR, //!< POSIX.1-1990 tar format
  23. wxTAR_PAX //!< POSIX.1-2001 tar format
  24. };
  25. /**
  26. @class wxTarInputStream
  27. Input stream for reading tar files.
  28. wxTarInputStream::GetNextEntry() returns a wxTarEntry object containing the
  29. meta-data for the next entry in the tar (and gives away ownership).
  30. Reading from the wxTarInputStream then returns the entry's data.
  31. wxTarInputStream::Eof() becomes @true after an attempt has been made to read
  32. past the end of the entry's data.
  33. When there are no more entries, wxTarInputStream::GetNextEntry() returns @NULL
  34. and sets wxTarInputStream::Eof().
  35. Tar entries are seekable if the parent stream is seekable. In practice this
  36. usually means they are only seekable if the tar is stored as a local file and
  37. is not compressed.
  38. @library{wxbase}
  39. @category{archive,streams}
  40. @see @ref overview_archive_byname
  41. */
  42. class wxTarInputStream : public wxArchiveInputStream
  43. {
  44. public:
  45. //@{
  46. /**
  47. Constructor. In a Unicode build the second parameter @a conv is
  48. used to translate fields from the standard tar header into Unicode.
  49. It has no effect on the stream's data. @a conv is only used for the standard
  50. tar headers, any pax extended headers are always UTF-8 encoded.
  51. If the parent stream is passed as a pointer then the new filter stream
  52. takes ownership of it. If it is passed by reference then it does not.
  53. */
  54. wxTarInputStream(wxInputStream& stream,
  55. wxMBConv& conv = wxConvLocal);
  56. wxTarInputStream(wxInputStream* stream,
  57. wxMBConv& conv = wxConvLocal);
  58. //@}
  59. /**
  60. Closes the current entry.
  61. On a non-seekable stream reads to the end of the current entry first.
  62. */
  63. bool CloseEntry();
  64. /**
  65. Closes the current entry if one is open, then reads the meta-data for
  66. the next entry and returns it in a wxTarEntry object, giving away ownership.
  67. The stream is then open and can be read.
  68. */
  69. wxTarEntry* GetNextEntry();
  70. /**
  71. Closes the current entry if one is open, then opens the entry specified
  72. by the @a entry object.
  73. @a entry should be from the same tar file, and the tar should be on a
  74. seekable stream.
  75. */
  76. bool OpenEntry(wxTarEntry& entry);
  77. };
  78. /**
  79. @class wxTarClassFactory
  80. Class factory for the tar archive format.
  81. See the base class for details.
  82. @library{wxbase}
  83. @category{archive,streams}
  84. @see @ref overview_archive, @ref overview_archive_generic,
  85. wxTarEntry, wxTarInputStream, wxTarOutputStream
  86. */
  87. class wxTarClassFactory : public wxArchiveClassFactory
  88. {
  89. public:
  90. };
  91. /**
  92. @class wxTarOutputStream
  93. Output stream for writing tar files.
  94. wxTarOutputStream::PutNextEntry() is used to create a new entry in the output tar,
  95. then the entry's data is written to the wxTarOutputStream.
  96. Another call to wxTarOutputStream::PutNextEntry() closes the current entry
  97. and begins the next.
  98. @library{wxbase}
  99. @category{streams}
  100. @see @ref overview_archive, wxTarEntry, wxTarInputStream
  101. */
  102. class wxTarOutputStream : public wxArchiveOutputStream
  103. {
  104. public:
  105. //@{
  106. /**
  107. If the parent stream is passed as a pointer then the new filter stream
  108. takes ownership of it. If it is passed by reference then it does not.
  109. In a Unicode build the third parameter @a conv is used to translate the
  110. headers fields into an 8-bit encoding. It has no effect on the stream's data.
  111. When the @a format is @e wxTAR_PAX, pax extended headers are generated
  112. when any header field will not fit the standard tar header block or if it
  113. uses any non-ascii characters.
  114. Extended headers are stored as extra 'files' within the tar, and will be
  115. extracted as such by any other tar program that does not understand them.
  116. The @a conv parameter only affect the standard tar headers, the extended
  117. headers are always UTF-8 encoded.
  118. When the @a format is @e wxTAR_USTAR, no extended headers are generated,
  119. and instead a warning message is logged if any header field overflows.
  120. */
  121. wxTarOutputStream(wxOutputStream& stream,
  122. wxTarFormat format = wxTAR_PAX,
  123. wxMBConv& conv = wxConvLocal);
  124. wxTarOutputStream(wxOutputStream* stream,
  125. wxTarFormat format = wxTAR_PAX,
  126. wxMBConv& conv = wxConvLocal);
  127. //@}
  128. /**
  129. The destructor calls Close() to finish writing the tar if it has
  130. not been called already.
  131. */
  132. virtual ~wxTarOutputStream();
  133. /**
  134. Finishes writing the tar, returning @true if successful.
  135. Called by the destructor if not called explicitly.
  136. */
  137. bool Close();
  138. /**
  139. Close the current entry.
  140. It is called implicitly whenever another new entry is created with
  141. CopyEntry() or PutNextEntry(), or when the tar is closed.
  142. */
  143. bool CloseEntry();
  144. /**
  145. See wxArchiveOutputStream::CopyArchiveMetaData().
  146. For the tar format this function does nothing.
  147. */
  148. bool CopyArchiveMetaData(wxTarInputStream& s);
  149. /**
  150. Takes ownership of @a entry and uses it to create a new entry in the tar.
  151. @a entry is then opened in @a inputStream and its contents copied to this stream.
  152. For some other archive formats CopyEntry() is much more efficient than
  153. transferring the data using Read() and Write() since it will copy them
  154. without decompressing and recompressing them.
  155. For tar however it makes no difference.
  156. For tars on seekable streams, @a entry must be from the same tar file
  157. as @a inputStream. For non-seekable streams, @a entry must also be the
  158. last thing read from @a inputStream.
  159. */
  160. bool CopyEntry(wxTarEntry* entry, wxTarInputStream& inputStream);
  161. //@{
  162. /**
  163. The tar is zero padded to round its size up to @e BlockingFactor * 512 bytes.
  164. The blocking factor defaults to 10 for @e wxTAR_PAX and 20 for @e wxTAR_USTAR
  165. (see wxTarOutputStream()), as specified in the POSIX standards.
  166. */
  167. int GetBlockingFactor() const;
  168. void SetBlockingFactor(int factor);
  169. //@}
  170. /**
  171. Create a new directory entry (see wxArchiveEntry::IsDir()) with the given
  172. name and timestamp.
  173. PutNextEntry() can also be used to create directory entries, by supplying
  174. a name with a trailing path separator.
  175. */
  176. bool PutNextDirEntry(const wxString& name, const wxDateTime& dt = wxDateTime::Now());
  177. /**
  178. Takes ownership of entry and uses it to create a new entry in the tar.
  179. */
  180. bool PutNextEntry(wxTarEntry* entry);
  181. /**
  182. Create a new entry with the given name, timestamp and size.
  183. */
  184. bool PutNextEntry(const wxString& name, const wxDateTime& dt = wxDateTime::Now(),
  185. wxFileOffset size = wxInvalidOffset);
  186. };
  187. /**
  188. @class wxTarEntry
  189. Holds the meta-data for an entry in a tar.
  190. @section tarentry_fields Field availability
  191. The tar format stores all the meta-data for an entry ahead of its data,
  192. therefore GetNextEntry() always returns a fully populated wxTarEntry object,
  193. both when reading from seekable and non-seekable streams.
  194. @library{wxbase}
  195. @category{archive,streams}
  196. @see @ref overview_archive, wxTarInputStream, wxTarOutputStream
  197. */
  198. class wxTarEntry : public wxArchiveEntry
  199. {
  200. public:
  201. /**
  202. Constructor.
  203. The tar archive format stores the entry's size ahead of the entry's data.
  204. Therefore when creating an archive on a non-seekable stream it is necessary
  205. to supply the correct size when each entry is created.
  206. */
  207. wxTarEntry(const wxString& name = wxEmptyString,
  208. const wxDateTime& dt = wxDateTime::Now(),
  209. wxFileOffset size = wxInvalidOffset);
  210. /**
  211. Copy constructor.
  212. */
  213. wxTarEntry(const wxTarEntry& entry);
  214. //@{
  215. /**
  216. Gets/sets the entry's access time stamp.
  217. See also wxArchiveEntry::GetDateTime() and wxArchiveEntry::SetDateTime().
  218. */
  219. wxDateTime GetAccessTime() const;
  220. void SetAccessTime(const wxDateTime& dt);
  221. //@}
  222. //@{
  223. /**
  224. The entry's creation time stamp.
  225. See also wxArchiveEntry::GetDateTime() and wxArchiveEntry::SetDateTime().
  226. */
  227. wxDateTime GetCreateTime() const;
  228. void SetCreateTime(const wxDateTime& dt);
  229. //@}
  230. //@{
  231. /**
  232. OS specific IDs defining a device; these are only meaningful when
  233. wxTarEntry::GetTypeFlag() is @e wxTAR_CHRTYPE or @e wxTAR_BLKTYPE.
  234. */
  235. int GetDevMajor() const;
  236. int GetDevMinor() const;
  237. void SetDevMajor(int dev);
  238. void SetDevMinor(int dev);
  239. //@}
  240. //@{
  241. /**
  242. The user ID and group ID that has permissions (see wxTarEntry::GetMode())
  243. over this entry.
  244. These values aren't usually useful unless the file will only be
  245. restored to the same system it originated from.
  246. wxTarEntry::GetGroupName() and wxTarEntry::GetUserName() can be used instead.
  247. */
  248. int GetGroupId() const;
  249. int GetUserId() const;
  250. void SetGroupId(int id);
  251. void SetUserId(int id);
  252. //@}
  253. //@{
  254. /**
  255. The names of the user and group that has permissions (see wxTarEntry::GetMode())
  256. over this entry. These are not present in very old tars.
  257. */
  258. wxString GetGroupName() const;
  259. wxString GetUserName() const;
  260. void SetGroupName(const wxString& group);
  261. void SetUserName(const wxString& user);
  262. //@}
  263. //@{
  264. /**
  265. The filename of a previous entry in the tar that this entry is a link to.
  266. Only meaningful when wxTarEntry::GetTypeFlag() is set to @e wxTAR_LNKTYPE
  267. or @e wxTAR_SYMTYPE.
  268. */
  269. wxString GetLinkName() const;
  270. void SetLinkName(const wxString& link);
  271. //@}
  272. //@{
  273. /**
  274. UNIX permission bits for this entry.
  275. Giving read, write and execute permissions to the file's user and group
  276. (see GetGroupName() and GetUserName()) and to others.
  277. The integer is one or more ::wxPosixPermissions flags OR-combined.
  278. */
  279. int GetMode() const;
  280. void SetMode(int mode);
  281. //@}
  282. //@{
  283. /**
  284. The size of the entry's data in bytes.
  285. The tar archive format stores the entry's size ahead of the entry's data.
  286. Therefore when creating an archive on a non-seekable stream it is necessary to
  287. supply the correct size when each entry is created.
  288. For seekable streams this is not necessary as wxTarOutputStream will attempt
  289. to seek back and fix the entry's header when the entry is closed, though it is
  290. still more efficient if the size is given beforehand.
  291. */
  292. void SetSize(wxFileOffset size);
  293. wxFileOffset GetSize() const;
  294. //@}
  295. //@{
  296. /**
  297. Returns/Sets the type of the entry as a ::wxTarType value.
  298. When creating archives use only one of ::wxTarType values.
  299. When reading archives, GetTypeFlag() may return a value which does not
  300. match any value of ::wxTarType; in this case the returned value should be
  301. treated as @e wxTAR_REGTYPE.
  302. */
  303. int GetTypeFlag() const;
  304. void SetTypeFlag(int type);
  305. //@}
  306. /**
  307. Returns the entry's filename in the internal format used within the archive.
  308. The name can include directory components, i.e. it can be a full path.
  309. The names of directory entries are returned without any trailing path separator.
  310. This gives a canonical name that can be used in comparisons.
  311. */
  312. wxString GetInternalName() const;
  313. /**
  314. A static member that translates a filename into the internal format used
  315. within the archive.
  316. If the third parameter is provided, the bool pointed to is set to indicate
  317. whether the name looks like a directory name (i.e. has a trailing path separator).
  318. */
  319. static wxString GetInternalName(const wxString& name,
  320. wxPathFormat format = wxPATH_NATIVE,
  321. bool* pIsDir = NULL);
  322. /**
  323. Assignment operator.
  324. */
  325. wxTarEntry& operator operator=(const wxTarEntry& entry);
  326. };