You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

382 lines
14 KiB

  1. /* Copyright (c) 2011, SmartFile <btimby@smartfile.com>
  2. All rights reserved.
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions are met:
  5. * Redistributions of source code must retain the above copyright
  6. notice, this list of conditions and the following disclaimer.
  7. * Redistributions in binary form must reproduce the above copyright
  8. notice, this list of conditions and the following disclaimer in the
  9. documentation and/or other materials provided with the distribution.
  10. * Neither the name of the organization nor the
  11. names of its contributors may be used to endorse or promote products
  12. derived from this software without specific prior written permission.
  13. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  14. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  15. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  16. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
  17. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  18. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  19. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  20. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  21. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  22. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
  23. %module _libarchive
  24. %{
  25. #include <archive.h>
  26. #include <archive_entry.h>
  27. %}
  28. %include "typemaps.i"
  29. %typemap(in) time_t
  30. {
  31. if (PyLong_Check($input))
  32. $1 = (time_t) PyLong_AsLong($input);
  33. else if (PyInt_Check($input))
  34. $1 = (time_t) PyInt_AsLong($input);
  35. else if (PyFloat_Check($input))
  36. $1 = (time_t) PyFloat_AsDouble($input);
  37. else {
  38. PyErr_SetString(PyExc_TypeError,"Expected a large number");
  39. return NULL;
  40. }
  41. }
  42. %typemap(out) time_t
  43. {
  44. $result = PyLong_FromLong((long)$1);
  45. }
  46. %typemap(in) int64_t
  47. {
  48. if (PyLong_Check($input))
  49. $1 = (int64_t) PyLong_AsLong($input);
  50. else if (PyInt_Check($input))
  51. $1 = (int64_t) PyInt_AsLong($input);
  52. else if (PyFloat_Check($input))
  53. $1 = (int64_t) PyFloat_AsDouble($input);
  54. else {
  55. PyErr_SetString(PyExc_TypeError,"Expected a large number");
  56. return NULL;
  57. }
  58. }
  59. %typemap(out) int64_t
  60. {
  61. $result = PyLong_FromLong((long)$1);
  62. }
  63. typedef unsigned short mode_t;
  64. # Everything below is from the archive.h and archive_entry.h files.
  65. # I excluded functions declarations that are not needed.
  66. /* CONFIGURATION */
  67. #include <sys/types.h>
  68. #include <stddef.h> /* for wchar_t */
  69. #include <time.h>
  70. #if defined(_WIN32) && !defined(__CYGWIN__)
  71. #include <windows.h>
  72. #endif
  73. /* Get appropriate definitions of standard POSIX-style types. */
  74. /* These should match the types used in 'struct stat' */
  75. #if defined(_WIN32) && !defined(__CYGWIN__)
  76. #define __LA_INT64_T __int64
  77. # if defined(__BORLANDC__)
  78. # define __LA_UID_T uid_t /* Remove in libarchive 3.2 */
  79. # define __LA_GID_T gid_t /* Remove in libarchive 3.2 */
  80. # define __LA_DEV_T dev_t
  81. # define __LA_MODE_T mode_t
  82. # else
  83. # define __LA_UID_T short /* Remove in libarchive 3.2 */
  84. # define __LA_GID_T short /* Remove in libarchive 3.2 */
  85. # define __LA_DEV_T unsigned int
  86. # define __LA_MODE_T unsigned short
  87. # endif
  88. #else
  89. #include <unistd.h>
  90. # if defined(_SCO_DS)
  91. # define __LA_INT64_T long long
  92. # else
  93. # define __LA_INT64_T int64_t
  94. # endif
  95. # define __LA_UID_T uid_t /* Remove in libarchive 3.2 */
  96. # define __LA_GID_T gid_t /* Remove in libarchive 3.2 */
  97. # define __LA_DEV_T dev_t
  98. # define __LA_MODE_T mode_t
  99. #endif
  100. /*
  101. * Remove this for libarchive 3.2, since ino_t is no longer used.
  102. */
  103. #define __LA_INO_T ino_t
  104. /*
  105. * On Windows, define LIBARCHIVE_STATIC if you're building or using a
  106. * .lib. The default here assumes you're building a DLL. Only
  107. * libarchive source should ever define __LIBARCHIVE_BUILD.
  108. */
  109. #if ((defined __WIN32__) || (defined _WIN32) || defined(__CYGWIN__)) && (!defined LIBARCHIVE_STATIC)
  110. # ifdef __LIBARCHIVE_BUILD
  111. # ifdef __GNUC__
  112. # define extern __attribute__((dllexport)) extern
  113. # else
  114. # define extern __declspec(dllexport)
  115. # endif
  116. # else
  117. # ifdef __GNUC__
  118. # define extern
  119. # else
  120. # define extern __declspec(dllimport)
  121. # endif
  122. # endif
  123. #else
  124. /* Static libraries on all platforms and shared libraries on non-Windows. */
  125. # define extern
  126. #endif
  127. /* STRUCTURES */
  128. struct archive;
  129. struct archive_entry;
  130. /* ARCHIVE READING */
  131. extern struct archive *archive_read_new(void);
  132. extern int archive_read_free(struct archive *);
  133. /* opening */
  134. extern int archive_read_open_filename(struct archive *,
  135. const char *_filename, size_t _block_size);
  136. extern int archive_read_open_memory(struct archive *,
  137. void * buff, size_t size);
  138. extern int archive_read_open_memory2(struct archive *a, void *buff,
  139. size_t size, size_t read_size);
  140. extern int archive_read_open_fd(struct archive *, int _fd,
  141. size_t _block_size);
  142. /* closing */
  143. extern int archive_read_close(struct archive *);
  144. extern int archive_format(struct archive *);
  145. /* headers */
  146. extern int archive_read_next_header2(struct archive *,
  147. struct archive_entry *);
  148. extern const struct stat *archive_entry_stat(struct archive_entry *);
  149. extern __LA_INT64_T archive_read_header_position(struct archive *);
  150. /* data */
  151. extern int archive_read_data_skip(struct archive *);
  152. extern int archive_read_data_into_fd(struct archive *, int fd);
  153. /* FILTERS */
  154. extern int archive_read_support_filter_all(struct archive *);
  155. extern int archive_read_support_filter_bzip2(struct archive *);
  156. extern int archive_read_support_filter_compress(struct archive *);
  157. extern int archive_read_support_filter_gzip(struct archive *);
  158. extern int archive_read_support_filter_lzip(struct archive *);
  159. extern int archive_read_support_filter_lzma(struct archive *);
  160. extern int archive_read_support_filter_none(struct archive *);
  161. extern int archive_read_support_filter_rpm(struct archive *);
  162. extern int archive_read_support_filter_uu(struct archive *);
  163. extern int archive_read_support_filter_xz(struct archive *);
  164. /* FORMATS */
  165. extern int archive_read_support_format_all(struct archive *);
  166. extern int archive_read_support_format_7zip(struct archive *);
  167. extern int archive_read_support_format_ar(struct archive *);
  168. extern int archive_read_support_format_cab(struct archive *);
  169. extern int archive_read_support_format_cpio(struct archive *);
  170. extern int archive_read_support_format_empty(struct archive *);
  171. extern int archive_read_support_format_gnutar(struct archive *);
  172. extern int archive_read_support_format_iso9660(struct archive *);
  173. extern int archive_read_support_format_lha(struct archive *);
  174. /*extern int archive_read_support_format_mtree(struct archive *);*/
  175. extern int archive_read_support_format_rar(struct archive *);
  176. extern int archive_read_support_format_raw(struct archive *);
  177. extern int archive_read_support_format_tar(struct archive *);
  178. extern int archive_read_support_format_xar(struct archive *);
  179. extern int archive_read_support_format_zip(struct archive *);
  180. /*extern int archive_read_support_format_by_code(struct archive *, int);*/
  181. /* ARCHIVE WRITING */
  182. extern struct archive *archive_write_new(void);
  183. extern int archive_write_free(struct archive *);
  184. /* opening */
  185. extern int archive_write_open(struct archive *, void *,
  186. archive_open_callback *, archive_write_callback *,
  187. archive_close_callback *);
  188. extern int archive_write_open_fd(struct archive *, int _fd);
  189. extern int archive_write_open_filename(struct archive *, const char *_file);
  190. extern int archive_write_open_filename_w(struct archive *,
  191. const wchar_t *_file);
  192. extern int archive_write_open_memory(struct archive *,
  193. void *_buffer, size_t _buffSize, size_t *_used);
  194. /* closing */
  195. extern int archive_write_close(struct archive *);
  196. /* headers */
  197. extern int archive_write_header(struct archive *,
  198. struct archive_entry *);
  199. /* data */
  200. /* commit */
  201. extern int archive_write_finish_entry(struct archive *);
  202. /* FILTERS */
  203. extern int archive_write_add_filter_bzip2(struct archive *);
  204. extern int archive_write_add_filter_compress(struct archive *);
  205. extern int archive_write_add_filter_gzip(struct archive *);
  206. extern int archive_write_add_filter_lzip(struct archive *);
  207. extern int archive_write_add_filter_lzma(struct archive *);
  208. extern int archive_write_add_filter_none(struct archive *);
  209. extern int archive_write_add_filter_xz(struct archive *);
  210. /* FORMATS */
  211. /* A convenience function to set the format based on the code or name. */
  212. extern int archive_write_set_format(struct archive *, int format_code);
  213. extern int archive_write_set_format_by_name(struct archive *,
  214. const char *name);
  215. /* To minimize link pollution, use one or more of the following. */
  216. extern int archive_write_set_format_ar_bsd(struct archive *);
  217. extern int archive_write_set_format_ar_svr4(struct archive *);
  218. extern int archive_write_set_format_cpio(struct archive *);
  219. extern int archive_write_set_format_cpio_newc(struct archive *);
  220. extern int archive_write_set_format_gnutar(struct archive *);
  221. extern int archive_write_set_format_iso9660(struct archive *);
  222. /*extern int archive_write_set_format_mtree(struct archive *);*/
  223. /* TODO: int archive_write_set_format_old_tar(struct archive *); */
  224. extern int archive_write_set_format_pax(struct archive *);
  225. extern int archive_write_set_format_pax_restricted(struct archive *);
  226. extern int archive_write_set_format_shar(struct archive *);
  227. extern int archive_write_set_format_shar_dump(struct archive *);
  228. extern int archive_write_set_format_ustar(struct archive *);
  229. extern int archive_write_set_format_xar(struct archive *);
  230. extern int archive_write_set_format_zip(struct archive *);
  231. /* ARCHIVE ENTRY */
  232. extern struct archive_entry *archive_entry_new(void);
  233. extern void archive_entry_free(struct archive_entry *);
  234. /* ARCHIVE ENTRY PROPERTY ACCESS */
  235. /* reading */
  236. extern const char *archive_entry_pathname(struct archive_entry *);
  237. extern const wchar_t *archive_entry_pathname_w(struct archive_entry *);
  238. extern __LA_INT64_T archive_entry_size(struct archive_entry *);
  239. extern time_t archive_entry_mtime(struct archive_entry *);
  240. extern __LA_MODE_T archive_entry_filetype(struct archive_entry *);
  241. extern __LA_MODE_T archive_entry_perm(struct archive_entry *);
  242. /* writing */
  243. extern void archive_entry_set_pathname(struct archive_entry *, const char *);
  244. extern void archive_entry_set_size(struct archive_entry *, __LA_INT64_T);
  245. extern void archive_entry_set_mtime(struct archive_entry *, time_t, long);
  246. extern void archive_entry_set_filetype(struct archive_entry *, unsigned int);
  247. extern void archive_entry_set_perm(struct archive_entry *, __LA_MODE_T);
  248. /* ERROR HANDLING */
  249. extern int archive_errno(struct archive *);
  250. extern const char *archive_error_string(struct archive *);
  251. /* CONSTANTS */
  252. #define ARCHIVE_VERSION_NUMBER 3000001
  253. #define ARCHIVE_VERSION_STRING "libarchive 3.0.1b"
  254. #define ARCHIVE_EOF 1 /* Found end of archive. */
  255. #define ARCHIVE_OK 0 /* Operation was successful. */
  256. #define ARCHIVE_RETRY (-10) /* Retry might succeed. */
  257. #define ARCHIVE_WARN (-20) /* Partial success. */
  258. #define ARCHIVE_FAILED (-25) /* Current operation cannot complete. */
  259. #define ARCHIVE_FATAL (-30) /* No more operations are possible. */
  260. #define ARCHIVE_FILTER_NONE 0
  261. #define ARCHIVE_FILTER_GZIP 1
  262. #define ARCHIVE_FILTER_BZIP2 2
  263. #define ARCHIVE_FILTER_COMPRESS 3
  264. #define ARCHIVE_FILTER_PROGRAM 4
  265. #define ARCHIVE_FILTER_LZMA 5
  266. #define ARCHIVE_FILTER_XZ 6
  267. #define ARCHIVE_FILTER_UU 7
  268. #define ARCHIVE_FILTER_RPM 8
  269. #define ARCHIVE_FILTER_LZIP 9
  270. #define ARCHIVE_FORMAT_BASE_MASK 0xff0000
  271. #define ARCHIVE_FORMAT_CPIO 0x10000
  272. #define ARCHIVE_FORMAT_CPIO_POSIX (ARCHIVE_FORMAT_CPIO | 1)
  273. #define ARCHIVE_FORMAT_CPIO_BIN_LE (ARCHIVE_FORMAT_CPIO | 2)
  274. #define ARCHIVE_FORMAT_CPIO_BIN_BE (ARCHIVE_FORMAT_CPIO | 3)
  275. #define ARCHIVE_FORMAT_CPIO_SVR4_NOCRC (ARCHIVE_FORMAT_CPIO | 4)
  276. #define ARCHIVE_FORMAT_CPIO_SVR4_CRC (ARCHIVE_FORMAT_CPIO | 5)
  277. #define ARCHIVE_FORMAT_CPIO_AFIO_LARGE (ARCHIVE_FORMAT_CPIO | 6)
  278. #define ARCHIVE_FORMAT_SHAR 0x20000
  279. #define ARCHIVE_FORMAT_SHAR_BASE (ARCHIVE_FORMAT_SHAR | 1)
  280. #define ARCHIVE_FORMAT_SHAR_DUMP (ARCHIVE_FORMAT_SHAR | 2)
  281. #define ARCHIVE_FORMAT_TAR 0x30000
  282. #define ARCHIVE_FORMAT_TAR_USTAR (ARCHIVE_FORMAT_TAR | 1)
  283. #define ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE (ARCHIVE_FORMAT_TAR | 2)
  284. #define ARCHIVE_FORMAT_TAR_PAX_RESTRICTED (ARCHIVE_FORMAT_TAR | 3)
  285. #define ARCHIVE_FORMAT_TAR_GNUTAR (ARCHIVE_FORMAT_TAR | 4)
  286. #define ARCHIVE_FORMAT_ISO9660 0x40000
  287. #define ARCHIVE_FORMAT_ISO9660_ROCKRIDGE (ARCHIVE_FORMAT_ISO9660 | 1)
  288. #define ARCHIVE_FORMAT_ZIP 0x50000
  289. #define ARCHIVE_FORMAT_EMPTY 0x60000
  290. #define ARCHIVE_FORMAT_AR 0x70000
  291. #define ARCHIVE_FORMAT_AR_GNU (ARCHIVE_FORMAT_AR | 1)
  292. #define ARCHIVE_FORMAT_AR_BSD (ARCHIVE_FORMAT_AR | 2)
  293. #define ARCHIVE_FORMAT_MTREE 0x80000
  294. #define ARCHIVE_FORMAT_RAW 0x90000
  295. #define ARCHIVE_FORMAT_XAR 0xA0000
  296. #define ARCHIVE_FORMAT_LHA 0xB0000
  297. #define ARCHIVE_FORMAT_CAB 0xC0000
  298. #define ARCHIVE_FORMAT_RAR 0xD0000
  299. #define ARCHIVE_FORMAT_7ZIP 0xE0000
  300. #define ARCHIVE_EXTRACT_OWNER (0x0001)
  301. #define ARCHIVE_EXTRACT_PERM (0x0002)
  302. #define ARCHIVE_EXTRACT_TIME (0x0004)
  303. #define ARCHIVE_EXTRACT_NO_OVERWRITE (0x0008)
  304. #define ARCHIVE_EXTRACT_UNLINK (0x0010)
  305. #define ARCHIVE_EXTRACT_ACL (0x0020)
  306. #define ARCHIVE_EXTRACT_FFLAGS (0x0040)
  307. #define ARCHIVE_EXTRACT_XATTR (0x0080)
  308. #define ARCHIVE_EXTRACT_SECURE_SYMLINKS (0x0100)
  309. #define ARCHIVE_EXTRACT_SECURE_NODOTDOT (0x0200)
  310. #define ARCHIVE_EXTRACT_NO_AUTODIR (0x0400)
  311. #define ARCHIVE_EXTRACT_NO_OVERWRITE_NEWER (0x0800)
  312. #define ARCHIVE_EXTRACT_SPARSE (0x1000)
  313. #define ARCHIVE_EXTRACT_MAC_METADATA (0x2000)
  314. %inline %{
  315. PyObject *archive_read_data_into_str(struct archive *archive, int len) {
  316. PyObject *str = NULL;
  317. if (!(str = PyString_FromStringAndSize(NULL, len))) {
  318. PyErr_SetString(PyExc_MemoryError, "could not allocate string.");
  319. return NULL;
  320. }
  321. if (len != archive_read_data(archive, PyString_AS_STRING(str), len)) {
  322. PyErr_SetString(PyExc_RuntimeError, "could not read requested data.");
  323. return NULL;
  324. }
  325. return str;
  326. }
  327. PyObject *archive_write_data_from_str(struct archive *archive, PyObject *str) {
  328. int len = PyString_Size(str);
  329. if (!archive_write_data(archive, PyString_AS_STRING(str), len)) {
  330. PyErr_SetString(PyExc_RuntimeError, "could not write requested data.");
  331. return NULL;
  332. }
  333. return PyInt_FromLong(len);
  334. }
  335. %}