archive_windows.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /*-
  2. * Copyright (c) 2009-2011 Michihiro NAKAJIMA
  3. * Copyright (c) 2003-2006 Tim Kientzle
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer
  11. * in this position and unchanged.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
  17. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  18. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  19. * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  21. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  22. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  23. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  25. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. *
  27. * $FreeBSD$
  28. */
  29. #ifndef __LIBARCHIVE_BUILD
  30. #error This header is only to be used internally to libarchive.
  31. #endif
  32. /*
  33. * TODO: A lot of stuff in here isn't actually used by libarchive and
  34. * can be trimmed out. Note that this file is used by libarchive and
  35. * libarchive_test but nowhere else. (But note that it gets compiled
  36. * with many different Windows environments, including MinGW, Visual
  37. * Studio, and Cygwin. Significant changes should be tested in all three.)
  38. */
  39. /*
  40. * TODO: Don't use off_t in here. Use __int64 instead. Note that
  41. * Visual Studio and the Windows SDK define off_t as 32 bits; Win32's
  42. * more modern file handling APIs all use __int64 instead of off_t.
  43. */
  44. #ifndef LIBARCHIVE_ARCHIVE_WINDOWS_H_INCLUDED
  45. #define LIBARCHIVE_ARCHIVE_WINDOWS_H_INCLUDED
  46. /* Start of configuration for native Win32 */
  47. #ifndef MINGW_HAS_SECURE_API
  48. #define MINGW_HAS_SECURE_API 1
  49. #endif
  50. #include <errno.h>
  51. #define set_errno(val) ((errno)=val)
  52. #include <io.h>
  53. #include <stdlib.h> //brings in NULL
  54. #if defined(HAVE_STDINT_H)
  55. #include <stdint.h>
  56. #endif
  57. #include <stdio.h>
  58. #include <fcntl.h>
  59. #include <sys/stat.h>
  60. #include <process.h>
  61. #include <direct.h>
  62. #if defined(__MINGW32__) && defined(HAVE_UNISTD_H)
  63. /* Prevent build error from a type mismatch of ftruncate().
  64. * This unistd.h defines it as ftruncate(int, off_t). */
  65. #include <unistd.h>
  66. #endif
  67. #define NOCRYPT
  68. #include <windows.h>
  69. //#define EFTYPE 7
  70. #if defined(__BORLANDC__)
  71. #pragma warn -8068 /* Constant out of range in comparison. */
  72. #pragma warn -8072 /* Suspicious pointer arithmetic. */
  73. #endif
  74. #ifndef NULL
  75. #ifdef __cplusplus
  76. #define NULL 0
  77. #else
  78. #define NULL ((void *)0)
  79. #endif
  80. #endif
  81. /* Alias the Windows _function to the POSIX equivalent. */
  82. #define close _close
  83. #define fcntl(fd, cmd, flg) /* No operation. */
  84. #ifndef fileno
  85. #define fileno _fileno
  86. #endif
  87. #ifdef fstat
  88. #undef fstat
  89. #endif
  90. #define fstat __la_fstat
  91. #if !defined(__BORLANDC__)
  92. #ifdef lseek
  93. #undef lseek
  94. #endif
  95. #define lseek _lseeki64
  96. #else
  97. #define lseek __la_lseek
  98. #define __LA_LSEEK_NEEDED
  99. #endif
  100. #define lstat __la_stat
  101. #define open __la_open
  102. #define read __la_read
  103. #if !defined(__BORLANDC__)
  104. #define setmode _setmode
  105. #endif
  106. #ifdef stat
  107. #undef stat
  108. #endif
  109. #define stat(path,stref) __la_stat(path,stref)
  110. #if !defined(__BORLANDC__)
  111. #define strdup _strdup
  112. #endif
  113. #define tzset _tzset
  114. #if !defined(__BORLANDC__)
  115. #define umask _umask
  116. #endif
  117. #define waitpid __la_waitpid
  118. #define write __la_write
  119. #ifndef O_RDONLY
  120. #define O_RDONLY _O_RDONLY
  121. #define O_WRONLY _O_WRONLY
  122. #define O_TRUNC _O_TRUNC
  123. #define O_CREAT _O_CREAT
  124. #define O_EXCL _O_EXCL
  125. #define O_BINARY _O_BINARY
  126. #endif
  127. #ifndef _S_IFIFO
  128. #define _S_IFIFO 0010000 /* pipe */
  129. #endif
  130. #ifndef _S_IFCHR
  131. #define _S_IFCHR 0020000 /* character special */
  132. #endif
  133. #ifndef _S_IFDIR
  134. #define _S_IFDIR 0040000 /* directory */
  135. #endif
  136. #ifndef _S_IFBLK
  137. #define _S_IFBLK 0060000 /* block special */
  138. #endif
  139. #ifndef _S_IFLNK
  140. #define _S_IFLNK 0120000 /* symbolic link */
  141. #endif
  142. #ifndef _S_IFSOCK
  143. #define _S_IFSOCK 0140000 /* socket */
  144. #endif
  145. #ifndef _S_IFREG
  146. #define _S_IFREG 0100000 /* regular */
  147. #endif
  148. #ifndef _S_IFMT
  149. #define _S_IFMT 0170000 /* file type mask */
  150. #endif
  151. #ifndef S_IFIFO
  152. #define S_IFIFO _S_IFIFO
  153. #endif
  154. //#define S_IFCHR _S_IFCHR
  155. //#define S_IFDIR _S_IFDIR
  156. #ifndef S_IFBLK
  157. #define S_IFBLK _S_IFBLK
  158. #endif
  159. #ifndef S_IFLNK
  160. #define S_IFLNK _S_IFLNK
  161. #endif
  162. #ifndef S_IFSOCK
  163. #define S_IFSOCK _S_IFSOCK
  164. #endif
  165. //#define S_IFREG _S_IFREG
  166. //#define S_IFMT _S_IFMT
  167. #ifndef S_ISBLK
  168. #define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK) /* block special */
  169. #define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO) /* fifo or socket */
  170. #define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR) /* char special */
  171. #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR) /* directory */
  172. #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG) /* regular file */
  173. #endif
  174. #define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK) /* Symbolic link */
  175. #define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK) /* Socket */
  176. #define _S_ISUID 0004000 /* set user id on execution */
  177. #define _S_ISGID 0002000 /* set group id on execution */
  178. #define _S_ISVTX 0001000 /* save swapped text even after use */
  179. #define S_ISUID _S_ISUID
  180. #define S_ISGID _S_ISGID
  181. #define S_ISVTX _S_ISVTX
  182. #define _S_IRWXU (_S_IREAD | _S_IWRITE | _S_IEXEC)
  183. #define _S_IXUSR _S_IEXEC /* read permission, user */
  184. #define _S_IWUSR _S_IWRITE /* write permission, user */
  185. #define _S_IRUSR _S_IREAD /* execute/search permission, user */
  186. #define _S_IRWXG (_S_IRWXU >> 3)
  187. #define _S_IXGRP (_S_IXUSR >> 3) /* read permission, group */
  188. #define _S_IWGRP (_S_IWUSR >> 3) /* write permission, group */
  189. #define _S_IRGRP (_S_IRUSR >> 3) /* execute/search permission, group */
  190. #define _S_IRWXO (_S_IRWXG >> 3)
  191. #define _S_IXOTH (_S_IXGRP >> 3) /* read permission, other */
  192. #define _S_IWOTH (_S_IWGRP >> 3) /* write permission, other */
  193. #define _S_IROTH (_S_IRGRP >> 3) /* execute/search permission, other */
  194. #ifndef S_IRWXU
  195. #define S_IRWXU _S_IRWXU
  196. #define S_IXUSR _S_IXUSR
  197. #define S_IWUSR _S_IWUSR
  198. #define S_IRUSR _S_IRUSR
  199. #endif
  200. #define S_IRWXG _S_IRWXG
  201. #define S_IXGRP _S_IXGRP
  202. #define S_IWGRP _S_IWGRP
  203. #define S_IRGRP _S_IRGRP
  204. #define S_IRWXO _S_IRWXO
  205. #define S_IXOTH _S_IXOTH
  206. #define S_IWOTH _S_IWOTH
  207. #define S_IROTH _S_IROTH
  208. #define F_DUPFD 0 /* Duplicate file descriptor. */
  209. #define F_GETFD 1 /* Get file descriptor flags. */
  210. #define F_SETFD 2 /* Set file descriptor flags. */
  211. #define F_GETFL 3 /* Get file status flags. */
  212. #define F_SETFL 4 /* Set file status flags. */
  213. #define F_GETOWN 5 /* Get owner (receiver of SIGIO). */
  214. #define F_SETOWN 6 /* Set owner (receiver of SIGIO). */
  215. #define F_GETLK 7 /* Get record locking info. */
  216. #define F_SETLK 8 /* Set record locking info (non-blocking). */
  217. #define F_SETLKW 9 /* Set record locking info (blocking). */
  218. /* XXX missing */
  219. #define F_GETLK64 7 /* Get record locking info. */
  220. #define F_SETLK64 8 /* Set record locking info (non-blocking). */
  221. #define F_SETLKW64 9 /* Set record locking info (blocking). */
  222. /* File descriptor flags used with F_GETFD and F_SETFD. */
  223. #define FD_CLOEXEC 1 /* Close on exec. */
  224. //NOT SURE IF O_NONBLOCK is OK here but at least the 0x0004 flag is not used by anything else...
  225. #define O_NONBLOCK 0x0004 /* Non-blocking I/O. */
  226. //#define O_NDELAY O_NONBLOCK
  227. /* Symbolic constants for the access() function */
  228. #if !defined(F_OK)
  229. #define R_OK 4 /* Test for read permission */
  230. #define W_OK 2 /* Test for write permission */
  231. #define X_OK 1 /* Test for execute permission */
  232. #define F_OK 0 /* Test for existence of file */
  233. #endif
  234. /* Replacement POSIX function */
  235. extern int __la_fstat(int fd, struct stat *st);
  236. extern int __la_lstat(const char *path, struct stat *st);
  237. #if defined(__LA_LSEEK_NEEDED)
  238. extern __int64 __la_lseek(int fd, __int64 offset, int whence);
  239. #endif
  240. extern int __la_open(const char *path, int flags, ...);
  241. extern ssize_t __la_read(int fd, void *buf, size_t nbytes);
  242. extern int __la_stat(const char *path, struct stat *st);
  243. extern pid_t __la_waitpid(HANDLE child, int *status, int option);
  244. extern ssize_t __la_write(int fd, const void *buf, size_t nbytes);
  245. #define _stat64i32(path, st) __la_stat(path, st)
  246. #define _stat64(path, st) __la_stat(path, st)
  247. /* for status returned by la_waitpid */
  248. #define WIFEXITED(sts) ((sts & 0x100) == 0)
  249. #define WEXITSTATUS(sts) (sts & 0x0FF)
  250. extern wchar_t *__la_win_permissive_name(const char *name);
  251. extern wchar_t *__la_win_permissive_name_w(const wchar_t *wname);
  252. extern void __la_dosmaperr(unsigned long e);
  253. #define la_dosmaperr(e) __la_dosmaperr(e)
  254. extern struct archive_entry *__la_win_entry_in_posix_pathseparator(
  255. struct archive_entry *);
  256. #if defined(HAVE_WCRTOMB) && defined(__BORLANDC__)
  257. typedef int mbstate_t;
  258. size_t wcrtomb(char *, wchar_t, mbstate_t *);
  259. #endif
  260. #if defined(_MSC_VER) && _MSC_VER < 1300
  261. WINBASEAPI BOOL WINAPI GetVolumePathNameW(
  262. LPCWSTR lpszFileName,
  263. LPWSTR lpszVolumePathName,
  264. DWORD cchBufferLength
  265. );
  266. # if _WIN32_WINNT < 0x0500 /* windows.h not providing 0x500 API */
  267. typedef struct _FILE_ALLOCATED_RANGE_BUFFER {
  268. LARGE_INTEGER FileOffset;
  269. LARGE_INTEGER Length;
  270. } FILE_ALLOCATED_RANGE_BUFFER, *PFILE_ALLOCATED_RANGE_BUFFER;
  271. # define FSCTL_SET_SPARSE \
  272. CTL_CODE(FILE_DEVICE_FILE_SYSTEM, 49, METHOD_BUFFERED, FILE_WRITE_DATA)
  273. # define FSCTL_QUERY_ALLOCATED_RANGES \
  274. CTL_CODE(FILE_DEVICE_FILE_SYSTEM, 51, METHOD_NEITHER, FILE_READ_DATA)
  275. # endif
  276. #endif
  277. #endif /* LIBARCHIVE_ARCHIVE_WINDOWS_H_INCLUDED */