archive_windows.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908
  1. /*-
  2. * Copyright (c) 2009-2011 Michihiro NAKAJIMA
  3. * Copyright (c) 2003-2007 Kees Zeelenberg
  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. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
  16. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  17. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  18. * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
  19. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  20. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  21. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  22. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  24. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. *
  26. * $FreeBSD$
  27. */
  28. /*
  29. * A set of compatibility glue for building libarchive on Windows platforms.
  30. *
  31. * Originally created as "libarchive-nonposix.c" by Kees Zeelenberg
  32. * for the GnuWin32 project, trimmed significantly by Tim Kientzle.
  33. *
  34. * Much of the original file was unnecessary for libarchive, because
  35. * many of the features it emulated were not strictly necessary for
  36. * libarchive. I hope for this to shrink further as libarchive
  37. * internals are gradually reworked to sit more naturally on both
  38. * POSIX and Windows. Any ideas for this are greatly appreciated.
  39. *
  40. * The biggest remaining issue is the dev/ino emulation; libarchive
  41. * has a couple of public APIs that rely on dev/ino uniquely
  42. * identifying a file. This doesn't match well with Windows. I'm
  43. * considering alternative APIs.
  44. */
  45. #if defined(_WIN32) && !defined(__CYGWIN__)
  46. #include "archive_platform.h"
  47. #include "archive_private.h"
  48. #include "archive_entry.h"
  49. #include <ctype.h>
  50. #include <errno.h>
  51. #include <stddef.h>
  52. #ifdef HAVE_SYS_UTIME_H
  53. #include <sys/utime.h>
  54. #endif
  55. #include <sys/stat.h>
  56. #include <locale.h>
  57. #include <process.h>
  58. #include <stdlib.h>
  59. #include <wchar.h>
  60. #include <windows.h>
  61. #include <share.h>
  62. #define EPOC_TIME ARCHIVE_LITERAL_ULL(116444736000000000)
  63. #if defined(__LA_LSEEK_NEEDED)
  64. static BOOL SetFilePointerEx_perso(HANDLE hFile,
  65. LARGE_INTEGER liDistanceToMove,
  66. PLARGE_INTEGER lpNewFilePointer,
  67. DWORD dwMoveMethod)
  68. {
  69. LARGE_INTEGER li;
  70. li.QuadPart = liDistanceToMove.QuadPart;
  71. li.LowPart = SetFilePointer(
  72. hFile, li.LowPart, &li.HighPart, dwMoveMethod);
  73. if(lpNewFilePointer) {
  74. lpNewFilePointer->QuadPart = li.QuadPart;
  75. }
  76. return li.LowPart != -1 || GetLastError() == NO_ERROR;
  77. }
  78. #endif
  79. struct ustat {
  80. int64_t st_atime;
  81. uint32_t st_atime_nsec;
  82. int64_t st_ctime;
  83. uint32_t st_ctime_nsec;
  84. int64_t st_mtime;
  85. uint32_t st_mtime_nsec;
  86. gid_t st_gid;
  87. /* 64bits ino */
  88. int64_t st_ino;
  89. mode_t st_mode;
  90. uint32_t st_nlink;
  91. uint64_t st_size;
  92. uid_t st_uid;
  93. dev_t st_dev;
  94. dev_t st_rdev;
  95. };
  96. /* Transform 64-bits ino into 32-bits by hashing.
  97. * You do not forget that really unique number size is 64-bits.
  98. */
  99. #define INOSIZE (8*sizeof(ino_t)) /* 32 */
  100. static __inline ino_t
  101. getino(struct ustat *ub)
  102. {
  103. ULARGE_INTEGER ino64;
  104. ino64.QuadPart = ub->st_ino;
  105. /* I don't know this hashing is correct way */
  106. return ((ino_t)(ino64.LowPart ^ (ino64.LowPart >> INOSIZE)));
  107. }
  108. /*
  109. * Prepend "\\?\" to the path name and convert it to unicode to permit
  110. * an extended-length path for a maximum total path length of 32767
  111. * characters.
  112. * see also http://msdn.microsoft.com/en-us/library/aa365247.aspx
  113. */
  114. wchar_t *
  115. __la_win_permissive_name(const char *name)
  116. {
  117. wchar_t *wn;
  118. wchar_t *ws;
  119. size_t ll;
  120. ll = strlen(name);
  121. wn = malloc((ll + 1) * sizeof(wchar_t));
  122. if (wn == NULL)
  123. return (NULL);
  124. ll = mbstowcs(wn, name, ll);
  125. if (ll == (size_t)-1) {
  126. free(wn);
  127. return (NULL);
  128. }
  129. wn[ll] = L'\0';
  130. ws = __la_win_permissive_name_w(wn);
  131. free(wn);
  132. return (ws);
  133. }
  134. wchar_t *
  135. __la_win_permissive_name_w(const wchar_t *wname)
  136. {
  137. wchar_t *wn, *wnp;
  138. wchar_t *ws, *wsp;
  139. DWORD l, len, slen;
  140. int unc;
  141. /* Get a full-pathname. */
  142. l = GetFullPathNameW(wname, 0, NULL, NULL);
  143. if (l == 0)
  144. return (NULL);
  145. /* NOTE: GetFullPathNameW has a bug that if the length of the file
  146. * name is just 1 then it returns incomplete buffer size. Thus, we
  147. * have to add three to the size to allocate a sufficient buffer
  148. * size for the full-pathname of the file name. */
  149. l += 3;
  150. wnp = malloc(l * sizeof(wchar_t));
  151. if (wnp == NULL)
  152. return (NULL);
  153. len = GetFullPathNameW(wname, l, wnp, NULL);
  154. wn = wnp;
  155. if (wnp[0] == L'\\' && wnp[1] == L'\\' &&
  156. wnp[2] == L'?' && wnp[3] == L'\\')
  157. /* We have already a permissive name. */
  158. return (wn);
  159. if (wnp[0] == L'\\' && wnp[1] == L'\\' &&
  160. wnp[2] == L'.' && wnp[3] == L'\\') {
  161. /* This is a device name */
  162. if (((wnp[4] >= L'a' && wnp[4] <= L'z') ||
  163. (wnp[4] >= L'A' && wnp[4] <= L'Z')) &&
  164. wnp[5] == L':' && wnp[6] == L'\\')
  165. wnp[2] = L'?';/* Not device name. */
  166. return (wn);
  167. }
  168. unc = 0;
  169. if (wnp[0] == L'\\' && wnp[1] == L'\\' && wnp[2] != L'\\') {
  170. wchar_t *p = &wnp[2];
  171. /* Skip server-name letters. */
  172. while (*p != L'\\' && *p != L'\0')
  173. ++p;
  174. if (*p == L'\\') {
  175. wchar_t *rp = ++p;
  176. /* Skip share-name letters. */
  177. while (*p != L'\\' && *p != L'\0')
  178. ++p;
  179. if (*p == L'\\' && p != rp) {
  180. /* Now, match patterns such as
  181. * "\\server-name\share-name\" */
  182. wnp += 2;
  183. len -= 2;
  184. unc = 1;
  185. }
  186. }
  187. }
  188. slen = 4 + (unc * 4) + len + 1;
  189. ws = wsp = malloc(slen * sizeof(wchar_t));
  190. if (ws == NULL) {
  191. free(wn);
  192. return (NULL);
  193. }
  194. /* prepend "\\?\" */
  195. wcsncpy(wsp, L"\\\\?\\", 4);
  196. wsp += 4;
  197. slen -= 4;
  198. if (unc) {
  199. /* append "UNC\" ---> "\\?\UNC\" */
  200. wcsncpy(wsp, L"UNC\\", 4);
  201. wsp += 4;
  202. slen -= 4;
  203. }
  204. wcsncpy(wsp, wnp, slen);
  205. wsp[slen - 1] = L'\0'; /* Ensure null termination. */
  206. free(wn);
  207. return (ws);
  208. }
  209. /*
  210. * Create a file handle.
  211. * This can exceed MAX_PATH limitation.
  212. */
  213. static HANDLE
  214. la_CreateFile(const char *path, DWORD dwDesiredAccess, DWORD dwShareMode,
  215. LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition,
  216. DWORD dwFlagsAndAttributes, HANDLE hTemplateFile)
  217. {
  218. wchar_t *wpath;
  219. HANDLE handle;
  220. handle = CreateFileA(path, dwDesiredAccess, dwShareMode,
  221. lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes,
  222. hTemplateFile);
  223. if (handle != INVALID_HANDLE_VALUE)
  224. return (handle);
  225. if (GetLastError() != ERROR_PATH_NOT_FOUND)
  226. return (handle);
  227. wpath = __la_win_permissive_name(path);
  228. if (wpath == NULL)
  229. return (handle);
  230. handle = CreateFileW(wpath, dwDesiredAccess, dwShareMode,
  231. lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes,
  232. hTemplateFile);
  233. free(wpath);
  234. return (handle);
  235. }
  236. #if defined(__LA_LSEEK_NEEDED)
  237. __int64
  238. __la_lseek(int fd, __int64 offset, int whence)
  239. {
  240. LARGE_INTEGER distance;
  241. LARGE_INTEGER newpointer;
  242. HANDLE handle;
  243. if (fd < 0) {
  244. errno = EBADF;
  245. return (-1);
  246. }
  247. handle = (HANDLE)_get_osfhandle(fd);
  248. if (GetFileType(handle) != FILE_TYPE_DISK) {
  249. errno = EBADF;
  250. return (-1);
  251. }
  252. distance.QuadPart = offset;
  253. if (!SetFilePointerEx_perso(handle, distance, &newpointer, whence)) {
  254. DWORD lasterr;
  255. lasterr = GetLastError();
  256. if (lasterr == ERROR_BROKEN_PIPE)
  257. return (0);
  258. if (lasterr == ERROR_ACCESS_DENIED)
  259. errno = EBADF;
  260. else
  261. la_dosmaperr(lasterr);
  262. return (-1);
  263. }
  264. return (newpointer.QuadPart);
  265. }
  266. #endif
  267. /* This can exceed MAX_PATH limitation. */
  268. int
  269. __la_open(const char *path, int flags, ...)
  270. {
  271. va_list ap;
  272. wchar_t *ws;
  273. int r, pmode;
  274. DWORD attr;
  275. va_start(ap, flags);
  276. pmode = va_arg(ap, int);
  277. va_end(ap);
  278. ws = NULL;
  279. if ((flags & ~O_BINARY) == O_RDONLY) {
  280. /*
  281. * When we open a directory, _open function returns
  282. * "Permission denied" error.
  283. */
  284. attr = GetFileAttributesA(path);
  285. if (attr == (DWORD)-1 && GetLastError() == ERROR_PATH_NOT_FOUND) {
  286. ws = __la_win_permissive_name(path);
  287. if (ws == NULL) {
  288. errno = EINVAL;
  289. return (-1);
  290. }
  291. attr = GetFileAttributesW(ws);
  292. }
  293. if (attr == (DWORD)-1) {
  294. la_dosmaperr(GetLastError());
  295. free(ws);
  296. return (-1);
  297. }
  298. if (attr & FILE_ATTRIBUTE_DIRECTORY) {
  299. HANDLE handle;
  300. if (ws != NULL)
  301. handle = CreateFileW(ws, 0, 0, NULL,
  302. OPEN_EXISTING,
  303. FILE_FLAG_BACKUP_SEMANTICS |
  304. FILE_ATTRIBUTE_READONLY,
  305. NULL);
  306. else
  307. handle = CreateFileA(path, 0, 0, NULL,
  308. OPEN_EXISTING,
  309. FILE_FLAG_BACKUP_SEMANTICS |
  310. FILE_ATTRIBUTE_READONLY,
  311. NULL);
  312. free(ws);
  313. if (handle == INVALID_HANDLE_VALUE) {
  314. la_dosmaperr(GetLastError());
  315. return (-1);
  316. }
  317. r = _open_osfhandle((intptr_t)handle, _O_RDONLY);
  318. return (r);
  319. }
  320. }
  321. if (ws == NULL) {
  322. #if defined(__BORLANDC__)
  323. /* Borland has no mode argument.
  324. TODO: Fix mode of new file. */
  325. r = _open(path, flags);
  326. #else
  327. r = _open(path, flags, pmode);
  328. #endif
  329. if (r < 0 && errno == EACCES && (flags & O_CREAT) != 0) {
  330. /* Simulate other POSIX system action to pass our test suite. */
  331. attr = GetFileAttributesA(path);
  332. if (attr == (DWORD)-1)
  333. la_dosmaperr(GetLastError());
  334. else if (attr & FILE_ATTRIBUTE_DIRECTORY)
  335. errno = EISDIR;
  336. else
  337. errno = EACCES;
  338. return (-1);
  339. }
  340. if (r >= 0 || errno != ENOENT)
  341. return (r);
  342. ws = __la_win_permissive_name(path);
  343. if (ws == NULL) {
  344. errno = EINVAL;
  345. return (-1);
  346. }
  347. }
  348. r = _wopen(ws, flags, pmode);
  349. if (r < 0 && errno == EACCES && (flags & O_CREAT) != 0) {
  350. /* Simulate other POSIX system action to pass our test suite. */
  351. attr = GetFileAttributesW(ws);
  352. if (attr == (DWORD)-1)
  353. la_dosmaperr(GetLastError());
  354. else if (attr & FILE_ATTRIBUTE_DIRECTORY)
  355. errno = EISDIR;
  356. else
  357. errno = EACCES;
  358. }
  359. free(ws);
  360. return (r);
  361. }
  362. ssize_t
  363. __la_read(int fd, void *buf, size_t nbytes)
  364. {
  365. HANDLE handle;
  366. DWORD bytes_read, lasterr;
  367. int r;
  368. #ifdef _WIN64
  369. if (nbytes > UINT32_MAX)
  370. nbytes = UINT32_MAX;
  371. #endif
  372. if (fd < 0) {
  373. errno = EBADF;
  374. return (-1);
  375. }
  376. /* Do not pass 0 to third parameter of ReadFile(), read bytes.
  377. * This will not return to application side. */
  378. if (nbytes == 0)
  379. return (0);
  380. handle = (HANDLE)_get_osfhandle(fd);
  381. r = ReadFile(handle, buf, (uint32_t)nbytes,
  382. &bytes_read, NULL);
  383. if (r == 0) {
  384. lasterr = GetLastError();
  385. if (lasterr == ERROR_NO_DATA) {
  386. errno = EAGAIN;
  387. return (-1);
  388. }
  389. if (lasterr == ERROR_BROKEN_PIPE)
  390. return (0);
  391. if (lasterr == ERROR_ACCESS_DENIED)
  392. errno = EBADF;
  393. else
  394. la_dosmaperr(lasterr);
  395. return (-1);
  396. }
  397. return ((ssize_t)bytes_read);
  398. }
  399. /* Convert Windows FILETIME to UTC */
  400. __inline static void
  401. fileTimeToUTC(const FILETIME *filetime, time_t *t, long *ns)
  402. {
  403. ULARGE_INTEGER utc;
  404. utc.HighPart = filetime->dwHighDateTime;
  405. utc.LowPart = filetime->dwLowDateTime;
  406. if (utc.QuadPart >= EPOC_TIME) {
  407. utc.QuadPart -= EPOC_TIME;
  408. *t = (time_t)(utc.QuadPart / 10000000); /* milli seconds base */
  409. *ns = (long)(utc.QuadPart % 10000000) * 100;/* nano seconds base */
  410. } else {
  411. *t = 0;
  412. *ns = 0;
  413. }
  414. }
  415. /* Stat by handle
  416. * Windows' stat() does not accept the path added "\\?\" especially "?"
  417. * character.
  418. * It means we cannot access the long name path longer than MAX_PATH.
  419. * So I've implemented simular Windows' stat() to access the long name path.
  420. * And I've added some feature.
  421. * 1. set st_ino by nFileIndexHigh and nFileIndexLow of
  422. * BY_HANDLE_FILE_INFORMATION.
  423. * 2. set st_nlink by nNumberOfLinks of BY_HANDLE_FILE_INFORMATION.
  424. * 3. set st_dev by dwVolumeSerialNumber by BY_HANDLE_FILE_INFORMATION.
  425. */
  426. static int
  427. __hstat(HANDLE handle, struct ustat *st)
  428. {
  429. BY_HANDLE_FILE_INFORMATION info;
  430. ULARGE_INTEGER ino64;
  431. DWORD ftype;
  432. mode_t mode;
  433. time_t t;
  434. long ns;
  435. switch (ftype = GetFileType(handle)) {
  436. case FILE_TYPE_UNKNOWN:
  437. errno = EBADF;
  438. return (-1);
  439. case FILE_TYPE_CHAR:
  440. case FILE_TYPE_PIPE:
  441. if (ftype == FILE_TYPE_CHAR) {
  442. st->st_mode = S_IFCHR;
  443. st->st_size = 0;
  444. } else {
  445. DWORD avail;
  446. st->st_mode = S_IFIFO;
  447. if (PeekNamedPipe(handle, NULL, 0, NULL, &avail, NULL))
  448. st->st_size = avail;
  449. else
  450. st->st_size = 0;
  451. }
  452. st->st_atime = 0;
  453. st->st_atime_nsec = 0;
  454. st->st_mtime = 0;
  455. st->st_mtime_nsec = 0;
  456. st->st_ctime = 0;
  457. st->st_ctime_nsec = 0;
  458. st->st_ino = 0;
  459. st->st_nlink = 1;
  460. st->st_uid = 0;
  461. st->st_gid = 0;
  462. st->st_rdev = 0;
  463. st->st_dev = 0;
  464. return (0);
  465. case FILE_TYPE_DISK:
  466. break;
  467. default:
  468. /* This ftype is undocumented type. */
  469. la_dosmaperr(GetLastError());
  470. return (-1);
  471. }
  472. ZeroMemory(&info, sizeof(info));
  473. if (!GetFileInformationByHandle (handle, &info)) {
  474. la_dosmaperr(GetLastError());
  475. return (-1);
  476. }
  477. mode = S_IRUSR | S_IRGRP | S_IROTH;
  478. if ((info.dwFileAttributes & FILE_ATTRIBUTE_READONLY) == 0)
  479. mode |= S_IWUSR | S_IWGRP | S_IWOTH;
  480. if (info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  481. mode |= S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH;
  482. else
  483. mode |= S_IFREG;
  484. st->st_mode = mode;
  485. fileTimeToUTC(&info.ftLastAccessTime, &t, &ns);
  486. st->st_atime = t;
  487. st->st_atime_nsec = ns;
  488. fileTimeToUTC(&info.ftLastWriteTime, &t, &ns);
  489. st->st_mtime = t;
  490. st->st_mtime_nsec = ns;
  491. fileTimeToUTC(&info.ftCreationTime, &t, &ns);
  492. st->st_ctime = t;
  493. st->st_ctime_nsec = ns;
  494. st->st_size =
  495. ((int64_t)(info.nFileSizeHigh) * ((int64_t)MAXDWORD + 1))
  496. + (int64_t)(info.nFileSizeLow);
  497. #ifdef SIMULATE_WIN_STAT
  498. st->st_ino = 0;
  499. st->st_nlink = 1;
  500. st->st_dev = 0;
  501. #else
  502. /* Getting FileIndex as i-node. We should remove a sequence which
  503. * is high-16-bits of nFileIndexHigh. */
  504. ino64.HighPart = info.nFileIndexHigh & 0x0000FFFFUL;
  505. ino64.LowPart = info.nFileIndexLow;
  506. st->st_ino = ino64.QuadPart;
  507. st->st_nlink = info.nNumberOfLinks;
  508. if (info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  509. ++st->st_nlink;/* Add parent directory. */
  510. st->st_dev = info.dwVolumeSerialNumber;
  511. #endif
  512. st->st_uid = 0;
  513. st->st_gid = 0;
  514. st->st_rdev = 0;
  515. return (0);
  516. }
  517. static void
  518. copy_stat(struct stat *st, struct ustat *us)
  519. {
  520. st->st_atime = us->st_atime;
  521. st->st_ctime = us->st_ctime;
  522. st->st_mtime = us->st_mtime;
  523. st->st_gid = us->st_gid;
  524. st->st_ino = getino(us);
  525. st->st_mode = us->st_mode;
  526. st->st_nlink = us->st_nlink;
  527. st->st_size = (off_t)us->st_size;
  528. st->st_uid = us->st_uid;
  529. st->st_dev = us->st_dev;
  530. st->st_rdev = us->st_rdev;
  531. }
  532. /*
  533. * TODO: Remove a use of __la_fstat and __la_stat.
  534. * We should use GetFileInformationByHandle in place
  535. * where We still use the *stat functions.
  536. */
  537. int
  538. __la_fstat(int fd, struct stat *st)
  539. {
  540. struct ustat u;
  541. int ret;
  542. if (fd < 0) {
  543. errno = EBADF;
  544. return (-1);
  545. }
  546. ret = __hstat((HANDLE)_get_osfhandle(fd), &u);
  547. if (ret >= 0) {
  548. copy_stat(st, &u);
  549. if (u.st_mode & (S_IFCHR | S_IFIFO)) {
  550. st->st_dev = fd;
  551. st->st_rdev = fd;
  552. }
  553. }
  554. return (ret);
  555. }
  556. /* This can exceed MAX_PATH limitation. */
  557. int
  558. __la_stat(const char *path, struct stat *st)
  559. {
  560. HANDLE handle;
  561. struct ustat u;
  562. int ret;
  563. handle = la_CreateFile(path, 0, FILE_SHARE_READ, NULL, OPEN_EXISTING,
  564. FILE_FLAG_BACKUP_SEMANTICS,
  565. NULL);
  566. if (handle == INVALID_HANDLE_VALUE) {
  567. la_dosmaperr(GetLastError());
  568. return (-1);
  569. }
  570. ret = __hstat(handle, &u);
  571. CloseHandle(handle);
  572. if (ret >= 0) {
  573. char *p;
  574. copy_stat(st, &u);
  575. p = strrchr(path, '.');
  576. if (p != NULL && strlen(p) == 4) {
  577. char exttype[4];
  578. ++ p;
  579. exttype[0] = toupper(*p++);
  580. exttype[1] = toupper(*p++);
  581. exttype[2] = toupper(*p++);
  582. exttype[3] = '\0';
  583. if (!strcmp(exttype, "EXE") || !strcmp(exttype, "CMD") ||
  584. !strcmp(exttype, "BAT") || !strcmp(exttype, "COM"))
  585. st->st_mode |= S_IXUSR | S_IXGRP | S_IXOTH;
  586. }
  587. }
  588. return (ret);
  589. }
  590. /*
  591. * This waitpid is limited implementation.
  592. */
  593. pid_t
  594. __la_waitpid(HANDLE child, int *status, int option)
  595. {
  596. DWORD cs;
  597. (void)option;/* UNUSED */
  598. do {
  599. if (GetExitCodeProcess(child, &cs) == 0) {
  600. CloseHandle(child);
  601. la_dosmaperr(GetLastError());
  602. *status = 0;
  603. return (-1);
  604. }
  605. } while (cs == STILL_ACTIVE);
  606. *status = (int)(cs & 0xff);
  607. return (0);
  608. }
  609. ssize_t
  610. __la_write(int fd, const void *buf, size_t nbytes)
  611. {
  612. DWORD bytes_written;
  613. #ifdef _WIN64
  614. if (nbytes > UINT32_MAX)
  615. nbytes = UINT32_MAX;
  616. #endif
  617. if (fd < 0) {
  618. errno = EBADF;
  619. return (-1);
  620. }
  621. if (!WriteFile((HANDLE)_get_osfhandle(fd), buf, (uint32_t)nbytes,
  622. &bytes_written, NULL)) {
  623. DWORD lasterr;
  624. lasterr = GetLastError();
  625. if (lasterr == ERROR_ACCESS_DENIED)
  626. errno = EBADF;
  627. else
  628. la_dosmaperr(lasterr);
  629. return (-1);
  630. }
  631. return (bytes_written);
  632. }
  633. /*
  634. * Replace the Windows path separator '\' with '/'.
  635. */
  636. static int
  637. replace_pathseparator(struct archive_wstring *ws, const wchar_t *wp)
  638. {
  639. wchar_t *w;
  640. size_t path_length;
  641. if (wp == NULL)
  642. return(0);
  643. if (wcschr(wp, L'\\') == NULL)
  644. return(0);
  645. path_length = wcslen(wp);
  646. if (archive_wstring_ensure(ws, path_length) == NULL)
  647. return(-1);
  648. archive_wstrncpy(ws, wp, path_length);
  649. for (w = ws->s; *w; w++) {
  650. if (*w == L'\\')
  651. *w = L'/';
  652. }
  653. return(1);
  654. }
  655. static int
  656. fix_pathseparator(struct archive_entry *entry)
  657. {
  658. struct archive_wstring ws;
  659. const wchar_t *wp;
  660. int ret = ARCHIVE_OK;
  661. archive_string_init(&ws);
  662. wp = archive_entry_pathname_w(entry);
  663. switch (replace_pathseparator(&ws, wp)) {
  664. case 0: /* Not replaced. */
  665. break;
  666. case 1: /* Replaced. */
  667. archive_entry_copy_pathname_w(entry, ws.s);
  668. break;
  669. default:
  670. ret = ARCHIVE_FAILED;
  671. }
  672. wp = archive_entry_hardlink_w(entry);
  673. switch (replace_pathseparator(&ws, wp)) {
  674. case 0: /* Not replaced. */
  675. break;
  676. case 1: /* Replaced. */
  677. archive_entry_copy_hardlink_w(entry, ws.s);
  678. break;
  679. default:
  680. ret = ARCHIVE_FAILED;
  681. }
  682. wp = archive_entry_symlink_w(entry);
  683. switch (replace_pathseparator(&ws, wp)) {
  684. case 0: /* Not replaced. */
  685. break;
  686. case 1: /* Replaced. */
  687. archive_entry_copy_symlink_w(entry, ws.s);
  688. break;
  689. default:
  690. ret = ARCHIVE_FAILED;
  691. }
  692. archive_wstring_free(&ws);
  693. return(ret);
  694. }
  695. struct archive_entry *
  696. __la_win_entry_in_posix_pathseparator(struct archive_entry *entry)
  697. {
  698. struct archive_entry *entry_main;
  699. const wchar_t *wp;
  700. int has_backslash = 0;
  701. int ret;
  702. wp = archive_entry_pathname_w(entry);
  703. if (wp != NULL && wcschr(wp, L'\\') != NULL)
  704. has_backslash = 1;
  705. if (!has_backslash) {
  706. wp = archive_entry_hardlink_w(entry);
  707. if (wp != NULL && wcschr(wp, L'\\') != NULL)
  708. has_backslash = 1;
  709. }
  710. if (!has_backslash) {
  711. wp = archive_entry_symlink_w(entry);
  712. if (wp != NULL && wcschr(wp, L'\\') != NULL)
  713. has_backslash = 1;
  714. }
  715. /*
  716. * If there is no backslash chars, return the original.
  717. */
  718. if (!has_backslash)
  719. return (entry);
  720. /* Copy entry so we can modify it as needed. */
  721. entry_main = archive_entry_clone(entry);
  722. if (entry_main == NULL)
  723. return (NULL);
  724. /* Replace the Windows path-separator '\' with '/'. */
  725. ret = fix_pathseparator(entry_main);
  726. if (ret < ARCHIVE_WARN) {
  727. archive_entry_free(entry_main);
  728. return (NULL);
  729. }
  730. return (entry_main);
  731. }
  732. /*
  733. * The following function was modified from PostgreSQL sources and is
  734. * subject to the copyright below.
  735. */
  736. /*-------------------------------------------------------------------------
  737. *
  738. * win32error.c
  739. * Map win32 error codes to errno values
  740. *
  741. * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
  742. *
  743. * IDENTIFICATION
  744. * $PostgreSQL: pgsql/src/port/win32error.c,v 1.4 2008/01/01 19:46:00 momjian Exp $
  745. *
  746. *-------------------------------------------------------------------------
  747. */
  748. /*
  749. PostgreSQL Database Management System
  750. (formerly known as Postgres, then as Postgres95)
  751. Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
  752. Portions Copyright (c) 1994, The Regents of the University of California
  753. Permission to use, copy, modify, and distribute this software and its
  754. documentation for any purpose, without fee, and without a written agreement
  755. is hereby granted, provided that the above copyright notice and this
  756. paragraph and the following two paragraphs appear in all copies.
  757. IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
  758. DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING
  759. LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
  760. DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE
  761. POSSIBILITY OF SUCH DAMAGE.
  762. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
  763. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  764. AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
  765. ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATIONS TO
  766. PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  767. */
  768. static const struct {
  769. DWORD winerr;
  770. int doserr;
  771. } doserrors[] =
  772. {
  773. { ERROR_INVALID_FUNCTION, EINVAL },
  774. { ERROR_FILE_NOT_FOUND, ENOENT },
  775. { ERROR_PATH_NOT_FOUND, ENOENT },
  776. { ERROR_TOO_MANY_OPEN_FILES, EMFILE },
  777. { ERROR_ACCESS_DENIED, EACCES },
  778. { ERROR_INVALID_HANDLE, EBADF },
  779. { ERROR_ARENA_TRASHED, ENOMEM },
  780. { ERROR_NOT_ENOUGH_MEMORY, ENOMEM },
  781. { ERROR_INVALID_BLOCK, ENOMEM },
  782. { ERROR_BAD_ENVIRONMENT, E2BIG },
  783. { ERROR_BAD_FORMAT, ENOEXEC },
  784. { ERROR_INVALID_ACCESS, EINVAL },
  785. { ERROR_INVALID_DATA, EINVAL },
  786. { ERROR_INVALID_DRIVE, ENOENT },
  787. { ERROR_CURRENT_DIRECTORY, EACCES },
  788. { ERROR_NOT_SAME_DEVICE, EXDEV },
  789. { ERROR_NO_MORE_FILES, ENOENT },
  790. { ERROR_LOCK_VIOLATION, EACCES },
  791. { ERROR_SHARING_VIOLATION, EACCES },
  792. { ERROR_BAD_NETPATH, ENOENT },
  793. { ERROR_NETWORK_ACCESS_DENIED, EACCES },
  794. { ERROR_BAD_NET_NAME, ENOENT },
  795. { ERROR_FILE_EXISTS, EEXIST },
  796. { ERROR_CANNOT_MAKE, EACCES },
  797. { ERROR_FAIL_I24, EACCES },
  798. { ERROR_INVALID_PARAMETER, EINVAL },
  799. { ERROR_NO_PROC_SLOTS, EAGAIN },
  800. { ERROR_DRIVE_LOCKED, EACCES },
  801. { ERROR_BROKEN_PIPE, EPIPE },
  802. { ERROR_DISK_FULL, ENOSPC },
  803. { ERROR_INVALID_TARGET_HANDLE, EBADF },
  804. { ERROR_INVALID_HANDLE, EINVAL },
  805. { ERROR_WAIT_NO_CHILDREN, ECHILD },
  806. { ERROR_CHILD_NOT_COMPLETE, ECHILD },
  807. { ERROR_DIRECT_ACCESS_HANDLE, EBADF },
  808. { ERROR_NEGATIVE_SEEK, EINVAL },
  809. { ERROR_SEEK_ON_DEVICE, EACCES },
  810. { ERROR_DIR_NOT_EMPTY, ENOTEMPTY },
  811. { ERROR_NOT_LOCKED, EACCES },
  812. { ERROR_BAD_PATHNAME, ENOENT },
  813. { ERROR_MAX_THRDS_REACHED, EAGAIN },
  814. { ERROR_LOCK_FAILED, EACCES },
  815. { ERROR_ALREADY_EXISTS, EEXIST },
  816. { ERROR_FILENAME_EXCED_RANGE, ENOENT },
  817. { ERROR_NESTING_NOT_ALLOWED, EAGAIN },
  818. { ERROR_NOT_ENOUGH_QUOTA, ENOMEM }
  819. };
  820. void
  821. __la_dosmaperr(unsigned long e)
  822. {
  823. int i;
  824. if (e == 0)
  825. {
  826. errno = 0;
  827. return;
  828. }
  829. for (i = 0; i < (int)(sizeof(doserrors)/sizeof(doserrors[0])); i++)
  830. {
  831. if (doserrors[i].winerr == e)
  832. {
  833. errno = doserrors[i].doserr;
  834. return;
  835. }
  836. }
  837. /* fprintf(stderr, "unrecognized win32 error code: %lu", e); */
  838. errno = EINVAL;
  839. return;
  840. }
  841. #endif /* _WIN32 && !__CYGWIN__ */