1 /*-
2 * Copyright (c) 2003-2010 Tim Kientzle
3 * Copyright (c) 2011-2012 Michihiro NAKAJIMA
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
28 #include "archive_platform.h"
29 __FBSDID("$FreeBSD$");
30
31 #if defined(_WIN32) && !defined(__CYGWIN__)
32
33 #ifdef HAVE_SYS_TYPES_H
34 #include <sys/types.h>
35 #endif
36 #ifdef HAVE_SYS_UTIME_H
37 #include <sys/utime.h>
38 #endif
39 #ifdef HAVE_ERRNO_H
40 #include <errno.h>
41 #endif
42 #ifdef HAVE_FCNTL_H
43 #include <fcntl.h>
44 #endif
45 #ifdef HAVE_LIMITS_H
46 #include <limits.h>
47 #endif
48 #ifdef HAVE_STDLIB_H
49 #include <stdlib.h>
50 #endif
51 #include <winioctl.h>
52
53 /* TODO: Support Mac OS 'quarantine' feature. This is really just a
54 * standard tag to mark files that have been downloaded as "tainted".
55 * On Mac OS, we should mark the extracted files as tainted if the
56 * archive being read was tainted. Windows has a similar feature; we
57 * should investigate ways to support this generically. */
58
59 #include "archive.h"
60 #include "archive_acl_private.h"
61 #include "archive_string.h"
62 #include "archive_entry.h"
63 #include "archive_private.h"
64
65 #ifndef O_BINARY
66 #define O_BINARY 0
67 #endif
68 #ifndef IO_REPARSE_TAG_SYMLINK
69 /* Old SDKs do not provide IO_REPARSE_TAG_SYMLINK */
70 #define IO_REPARSE_TAG_SYMLINK 0xA000000CL
71 #endif
72
SetFilePointerEx_perso(HANDLE hFile,LARGE_INTEGER liDistanceToMove,PLARGE_INTEGER lpNewFilePointer,DWORD dwMoveMethod)73 static BOOL SetFilePointerEx_perso(HANDLE hFile,
74 LARGE_INTEGER liDistanceToMove,
75 PLARGE_INTEGER lpNewFilePointer,
76 DWORD dwMoveMethod)
77 {
78 LARGE_INTEGER li;
79 li.QuadPart = liDistanceToMove.QuadPart;
80 li.LowPart = SetFilePointer(
81 hFile, li.LowPart, &li.HighPart, dwMoveMethod);
82 if(lpNewFilePointer) {
83 lpNewFilePointer->QuadPart = li.QuadPart;
84 }
85 return li.LowPart != (DWORD)-1 || GetLastError() == NO_ERROR;
86 }
87
88 struct fixup_entry {
89 struct fixup_entry *next;
90 struct archive_acl acl;
91 mode_t mode;
92 int64_t atime;
93 int64_t birthtime;
94 int64_t mtime;
95 int64_t ctime;
96 unsigned long atime_nanos;
97 unsigned long birthtime_nanos;
98 unsigned long mtime_nanos;
99 unsigned long ctime_nanos;
100 unsigned long fflags_set;
101 int fixup; /* bitmask of what needs fixing */
102 wchar_t *name;
103 };
104
105 /*
106 * We use a bitmask to track which operations remain to be done for
107 * this file. In particular, this helps us avoid unnecessary
108 * operations when it's possible to take care of one step as a
109 * side-effect of another. For example, mkdir() can specify the mode
110 * for the newly-created object but symlink() cannot. This means we
111 * can skip chmod() if mkdir() succeeded, but we must explicitly
112 * chmod() if we're trying to create a directory that already exists
113 * (mkdir() failed) or if we're restoring a symlink. Similarly, we
114 * need to verify UID/GID before trying to restore SUID/SGID bits;
115 * that verification can occur explicitly through a stat() call or
116 * implicitly because of a successful chown() call.
117 */
118 #define TODO_MODE_FORCE 0x40000000
119 #define TODO_MODE_BASE 0x20000000
120 #define TODO_SUID 0x10000000
121 #define TODO_SUID_CHECK 0x08000000
122 #define TODO_SGID 0x04000000
123 #define TODO_SGID_CHECK 0x02000000
124 #define TODO_MODE (TODO_MODE_BASE|TODO_SUID|TODO_SGID)
125 #define TODO_TIMES ARCHIVE_EXTRACT_TIME
126 #define TODO_OWNER ARCHIVE_EXTRACT_OWNER
127 #define TODO_FFLAGS ARCHIVE_EXTRACT_FFLAGS
128 #define TODO_ACLS ARCHIVE_EXTRACT_ACL
129 #define TODO_XATTR ARCHIVE_EXTRACT_XATTR
130 #define TODO_MAC_METADATA ARCHIVE_EXTRACT_MAC_METADATA
131
132 struct archive_write_disk {
133 struct archive archive;
134
135 mode_t user_umask;
136 struct fixup_entry *fixup_list;
137 struct fixup_entry *current_fixup;
138 int64_t user_uid;
139 int skip_file_set;
140 int64_t skip_file_dev;
141 int64_t skip_file_ino;
142 time_t start_time;
143
144 int64_t (*lookup_gid)(void *private, const char *gname, int64_t gid);
145 void (*cleanup_gid)(void *private);
146 void *lookup_gid_data;
147 int64_t (*lookup_uid)(void *private, const char *uname, int64_t uid);
148 void (*cleanup_uid)(void *private);
149 void *lookup_uid_data;
150
151 /*
152 * Full path of last file to satisfy symlink checks.
153 */
154 struct archive_wstring path_safe;
155
156 /*
157 * Cached stat data from disk for the current entry.
158 * If this is valid, pst points to st. Otherwise,
159 * pst is null.
160 */
161 BY_HANDLE_FILE_INFORMATION st;
162 BY_HANDLE_FILE_INFORMATION *pst;
163
164 /* Information about the object being restored right now. */
165 struct archive_entry *entry; /* Entry being extracted. */
166 wchar_t *name; /* Name of entry, possibly edited. */
167 struct archive_wstring _name_data; /* backing store for 'name' */
168 /* Tasks remaining for this object. */
169 int todo;
170 /* Tasks deferred until end-of-archive. */
171 int deferred;
172 /* Options requested by the client. */
173 int flags;
174 /* Handle for the file we're restoring. */
175 HANDLE fh;
176 /* Current offset for writing data to the file. */
177 int64_t offset;
178 /* Last offset actually written to disk. */
179 int64_t fd_offset;
180 /* Total bytes actually written to files. */
181 int64_t total_bytes_written;
182 /* Maximum size of file, -1 if unknown. */
183 int64_t filesize;
184 /* Dir we were in before this restore; only for deep paths. */
185 int restore_pwd;
186 /* Mode we should use for this entry; affected by _PERM and umask. */
187 mode_t mode;
188 /* UID/GID to use in restoring this entry. */
189 int64_t uid;
190 int64_t gid;
191 };
192
193 /*
194 * Default mode for dirs created automatically (will be modified by umask).
195 * Note that POSIX specifies 0777 for implicitly-created dirs, "modified
196 * by the process' file creation mask."
197 */
198 #define DEFAULT_DIR_MODE 0777
199 /*
200 * Dir modes are restored in two steps: During the extraction, the permissions
201 * in the archive are modified to match the following limits. During
202 * the post-extract fixup pass, the permissions from the archive are
203 * applied.
204 */
205 #define MINIMUM_DIR_MODE 0700
206 #define MAXIMUM_DIR_MODE 0775
207
208 static int check_symlinks(struct archive_write_disk *);
209 static int create_filesystem_object(struct archive_write_disk *);
210 static struct fixup_entry *current_fixup(struct archive_write_disk *,
211 const wchar_t *pathname);
212 static int cleanup_pathname(struct archive_write_disk *);
213 static int create_dir(struct archive_write_disk *, wchar_t *);
214 static int create_parent_dir(struct archive_write_disk *, wchar_t *);
215 static int la_chmod(const wchar_t *, mode_t);
216 static int older(BY_HANDLE_FILE_INFORMATION *, struct archive_entry *);
217 static int permissive_name_w(struct archive_write_disk *);
218 static int restore_entry(struct archive_write_disk *);
219 static int set_acls(struct archive_write_disk *, HANDLE h,
220 const wchar_t *, struct archive_acl *);
221 static int set_xattrs(struct archive_write_disk *);
222 static int set_fflags(struct archive_write_disk *);
223 static int set_ownership(struct archive_write_disk *);
224 static int set_mode(struct archive_write_disk *, int mode);
225 static int set_times(struct archive_write_disk *, HANDLE, int,
226 const wchar_t *, time_t, long, time_t, long, time_t,
227 long, time_t, long);
228 static int set_times_from_entry(struct archive_write_disk *);
229 static struct fixup_entry *sort_dir_list(struct fixup_entry *p);
230 static ssize_t write_data_block(struct archive_write_disk *,
231 const char *, size_t);
232
233 static struct archive_vtable *archive_write_disk_vtable(void);
234
235 static int _archive_write_disk_close(struct archive *);
236 static int _archive_write_disk_free(struct archive *);
237 static int _archive_write_disk_header(struct archive *,
238 struct archive_entry *);
239 static int64_t _archive_write_disk_filter_bytes(struct archive *, int);
240 static int _archive_write_disk_finish_entry(struct archive *);
241 static ssize_t _archive_write_disk_data(struct archive *, const void *,
242 size_t);
243 static ssize_t _archive_write_disk_data_block(struct archive *, const void *,
244 size_t, int64_t);
245
246 #define bhfi_dev(bhfi) ((bhfi)->dwVolumeSerialNumber)
247 /* Treat FileIndex as i-node. We should remove a sequence number
248 * which is high-16-bits of nFileIndexHigh. */
249 #define bhfi_ino(bhfi) \
250 ((((int64_t)((bhfi)->nFileIndexHigh & 0x0000FFFFUL)) << 32) \
251 + (bhfi)->nFileIndexLow)
252 #define bhfi_size(bhfi) \
253 ((((int64_t)(bhfi)->nFileSizeHigh) << 32) + (bhfi)->nFileSizeLow)
254
255 static int
file_information(struct archive_write_disk * a,wchar_t * path,BY_HANDLE_FILE_INFORMATION * st,mode_t * mode,int sim_lstat)256 file_information(struct archive_write_disk *a, wchar_t *path,
257 BY_HANDLE_FILE_INFORMATION *st, mode_t *mode, int sim_lstat)
258 {
259 HANDLE h;
260 int r;
261 DWORD flag = FILE_FLAG_BACKUP_SEMANTICS;
262 WIN32_FIND_DATAW findData;
263
264 if (sim_lstat || mode != NULL) {
265 h = FindFirstFileW(path, &findData);
266 if (h == INVALID_HANDLE_VALUE &&
267 GetLastError() == ERROR_INVALID_NAME) {
268 wchar_t *full;
269 full = __la_win_permissive_name_w(path);
270 h = FindFirstFileW(full, &findData);
271 free(full);
272 }
273 if (h == INVALID_HANDLE_VALUE) {
274 la_dosmaperr(GetLastError());
275 return (-1);
276 }
277 FindClose(h);
278 }
279
280 /* Is symlink file ? */
281 if (sim_lstat &&
282 ((findData.dwFileAttributes
283 & FILE_ATTRIBUTE_REPARSE_POINT) &&
284 (findData.dwReserved0 == IO_REPARSE_TAG_SYMLINK)))
285 flag |= FILE_FLAG_OPEN_REPARSE_POINT;
286
287 h = CreateFileW(a->name, 0, 0, NULL,
288 OPEN_EXISTING, flag, NULL);
289 if (h == INVALID_HANDLE_VALUE &&
290 GetLastError() == ERROR_INVALID_NAME) {
291 wchar_t *full;
292 full = __la_win_permissive_name_w(path);
293 h = CreateFileW(full, 0, 0, NULL,
294 OPEN_EXISTING, flag, NULL);
295 free(full);
296 }
297 if (h == INVALID_HANDLE_VALUE) {
298 la_dosmaperr(GetLastError());
299 return (-1);
300 }
301 r = GetFileInformationByHandle(h, st);
302 CloseHandle(h);
303 if (r == 0) {
304 la_dosmaperr(GetLastError());
305 return (-1);
306 }
307
308 if (mode == NULL)
309 return (0);
310
311 *mode = S_IRUSR | S_IRGRP | S_IROTH;
312 if ((st->dwFileAttributes & FILE_ATTRIBUTE_READONLY) == 0)
313 *mode |= S_IWUSR | S_IWGRP | S_IWOTH;
314 if ((st->dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) &&
315 findData.dwReserved0 == IO_REPARSE_TAG_SYMLINK)
316 *mode |= S_IFLNK;
317 else if (st->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
318 *mode |= S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH;
319 else {
320 const wchar_t *p;
321
322 *mode |= S_IFREG;
323 p = wcsrchr(path, L'.');
324 if (p != NULL && wcslen(p) == 4) {
325 switch (p[1]) {
326 case L'B': case L'b':
327 if ((p[2] == L'A' || p[2] == L'a' ) &&
328 (p[3] == L'T' || p[3] == L't' ))
329 *mode |= S_IXUSR | S_IXGRP | S_IXOTH;
330 break;
331 case L'C': case L'c':
332 if (((p[2] == L'M' || p[2] == L'm' ) &&
333 (p[3] == L'D' || p[3] == L'd' )))
334 *mode |= S_IXUSR | S_IXGRP | S_IXOTH;
335 break;
336 case L'E': case L'e':
337 if ((p[2] == L'X' || p[2] == L'x' ) &&
338 (p[3] == L'E' || p[3] == L'e' ))
339 *mode |= S_IXUSR | S_IXGRP | S_IXOTH;
340 break;
341 default:
342 break;
343 }
344 }
345 }
346 return (0);
347 }
348
349 /*
350 * Note: The path, for example, "aa/a/../b../c" will be converted to "aa/c"
351 * by GetFullPathNameW() W32 API, which __la_win_permissive_name_w uses.
352 * It means we cannot handle multiple dirs in one archive_entry.
353 * So we have to make the full-pathname in another way, which does not
354 * break "../" path string.
355 */
356 static int
permissive_name_w(struct archive_write_disk * a)357 permissive_name_w(struct archive_write_disk *a)
358 {
359 wchar_t *wn, *wnp;
360 wchar_t *ws, *wsp;
361 DWORD l;
362
363 wnp = a->name;
364 if (wnp[0] == L'\\' && wnp[1] == L'\\' &&
365 wnp[2] == L'?' && wnp[3] == L'\\')
366 /* We have already a permissive name. */
367 return (0);
368
369 if (wnp[0] == L'\\' && wnp[1] == L'\\' &&
370 wnp[2] == L'.' && wnp[3] == L'\\') {
371 /* This is a device name */
372 if (((wnp[4] >= L'a' && wnp[4] <= L'z') ||
373 (wnp[4] >= L'A' && wnp[4] <= L'Z')) &&
374 wnp[5] == L':' && wnp[6] == L'\\') {
375 wnp[2] = L'?';/* Not device name. */
376 return (0);
377 }
378 }
379
380 /*
381 * A full-pathname starting with a drive name like "C:\abc".
382 */
383 if (((wnp[0] >= L'a' && wnp[0] <= L'z') ||
384 (wnp[0] >= L'A' && wnp[0] <= L'Z')) &&
385 wnp[1] == L':' && wnp[2] == L'\\') {
386 wn = _wcsdup(wnp);
387 if (wn == NULL)
388 return (-1);
389 archive_wstring_ensure(&(a->_name_data), 4 + wcslen(wn) + 1);
390 a->name = a->_name_data.s;
391 /* Prepend "\\?\" */
392 archive_wstrncpy(&(a->_name_data), L"\\\\?\\", 4);
393 archive_wstrcat(&(a->_name_data), wn);
394 free(wn);
395 return (0);
396 }
397
398 /*
399 * A full-pathname pointing to a network drive
400 * like "\\<server-name>\<share-name>\file".
401 */
402 if (wnp[0] == L'\\' && wnp[1] == L'\\' && wnp[2] != L'\\') {
403 const wchar_t *p = &wnp[2];
404
405 /* Skip server-name letters. */
406 while (*p != L'\\' && *p != L'\0')
407 ++p;
408 if (*p == L'\\') {
409 const wchar_t *rp = ++p;
410 /* Skip share-name letters. */
411 while (*p != L'\\' && *p != L'\0')
412 ++p;
413 if (*p == L'\\' && p != rp) {
414 /* Now, match patterns such as
415 * "\\server-name\share-name\" */
416 wn = _wcsdup(wnp);
417 if (wn == NULL)
418 return (-1);
419 archive_wstring_ensure(&(a->_name_data),
420 8 + wcslen(wn) + 1);
421 a->name = a->_name_data.s;
422 /* Prepend "\\?\UNC\" */
423 archive_wstrncpy(&(a->_name_data),
424 L"\\\\?\\UNC\\", 8);
425 archive_wstrcat(&(a->_name_data), wn+2);
426 free(wn);
427 return (0);
428 }
429 }
430 return (0);
431 }
432
433 /*
434 * Get current working directory.
435 */
436 l = GetCurrentDirectoryW(0, NULL);
437 if (l == 0)
438 return (-1);
439 ws = malloc(l * sizeof(wchar_t));
440 l = GetCurrentDirectoryW(l, ws);
441 if (l == 0) {
442 free(ws);
443 return (-1);
444 }
445 wsp = ws;
446
447 /*
448 * A full-pathname starting without a drive name like "\abc".
449 */
450 if (wnp[0] == L'\\') {
451 wn = _wcsdup(wnp);
452 if (wn == NULL)
453 return (-1);
454 archive_wstring_ensure(&(a->_name_data),
455 4 + 2 + wcslen(wn) + 1);
456 a->name = a->_name_data.s;
457 /* Prepend "\\?\" and drive name. */
458 archive_wstrncpy(&(a->_name_data), L"\\\\?\\", 4);
459 archive_wstrncat(&(a->_name_data), wsp, 2);
460 archive_wstrcat(&(a->_name_data), wn);
461 free(wsp);
462 free(wn);
463 return (0);
464 }
465
466 wn = _wcsdup(wnp);
467 if (wn == NULL)
468 return (-1);
469 archive_wstring_ensure(&(a->_name_data), 4 + l + 1 + wcslen(wn) + 1);
470 a->name = a->_name_data.s;
471 /* Prepend "\\?\" and drive name if not already added. */
472 if (l > 3 && wsp[0] == L'\\' && wsp[1] == L'\\' &&
473 wsp[2] == L'?' && wsp[3] == L'\\')
474 {
475 archive_wstrncpy(&(a->_name_data), wsp, l);
476 }
477 else
478 {
479 archive_wstrncpy(&(a->_name_data), L"\\\\?\\", 4);
480 archive_wstrncat(&(a->_name_data), wsp, l);
481 }
482 archive_wstrncat(&(a->_name_data), L"\\", 1);
483 archive_wstrcat(&(a->_name_data), wn);
484 a->name = a->_name_data.s;
485 free(wsp);
486 free(wn);
487 return (0);
488 }
489
490 static int
la_chmod(const wchar_t * path,mode_t mode)491 la_chmod(const wchar_t *path, mode_t mode)
492 {
493 DWORD attr;
494 BOOL r;
495 wchar_t *fullname;
496 int ret = 0;
497
498 fullname = NULL;
499 attr = GetFileAttributesW(path);
500 if (attr == (DWORD)-1 &&
501 GetLastError() == ERROR_INVALID_NAME) {
502 fullname = __la_win_permissive_name_w(path);
503 attr = GetFileAttributesW(fullname);
504 }
505 if (attr == (DWORD)-1) {
506 la_dosmaperr(GetLastError());
507 ret = -1;
508 goto exit_chmode;
509 }
510 if (mode & _S_IWRITE)
511 attr &= ~FILE_ATTRIBUTE_READONLY;
512 else
513 attr |= FILE_ATTRIBUTE_READONLY;
514 if (fullname != NULL)
515 r = SetFileAttributesW(fullname, attr);
516 else
517 r = SetFileAttributesW(path, attr);
518 if (r == 0) {
519 la_dosmaperr(GetLastError());
520 ret = -1;
521 }
522 exit_chmode:
523 free(fullname);
524 return (ret);
525 }
526
527 static void *
la_GetFunctionKernel32(const char * name)528 la_GetFunctionKernel32(const char *name)
529 {
530 static HINSTANCE lib;
531 static int set;
532 if (!set) {
533 set = 1;
534 lib = LoadLibrary(TEXT("kernel32.dll"));
535 }
536 if (lib == NULL) {
537 fprintf(stderr, "Can't load kernel32.dll?!\n");
538 exit(1);
539 }
540 return (void *)GetProcAddress(lib, name);
541 }
542
543 static int
la_CreateHardLinkW(wchar_t * linkname,wchar_t * target)544 la_CreateHardLinkW(wchar_t *linkname, wchar_t *target)
545 {
546 static BOOLEAN (WINAPI *f)(LPWSTR, LPWSTR, LPSECURITY_ATTRIBUTES);
547 static int set;
548 BOOL ret;
549
550 if (!set) {
551 set = 1;
552 f = la_GetFunctionKernel32("CreateHardLinkW");
553 }
554 if (!f)
555 return (0);
556 ret = (*f)(linkname, target, NULL);
557 if (!ret) {
558 /* Under windows 2000, it is necessary to remove
559 * the "\\?\" prefix. */
560 #define IS_UNC(name) ((name[0] == L'U' || name[0] == L'u') && \
561 (name[1] == L'N' || name[1] == L'n') && \
562 (name[2] == L'C' || name[2] == L'c') && \
563 name[3] == L'\\')
564 if (!wcsncmp(linkname,L"\\\\?\\", 4)) {
565 linkname += 4;
566 if (IS_UNC(linkname))
567 linkname += 4;
568 }
569 if (!wcsncmp(target,L"\\\\?\\", 4)) {
570 target += 4;
571 if (IS_UNC(target))
572 target += 4;
573 }
574 #undef IS_UNC
575 ret = (*f)(linkname, target, NULL);
576 }
577 return (ret);
578 }
579
580 static int
la_ftruncate(HANDLE handle,int64_t length)581 la_ftruncate(HANDLE handle, int64_t length)
582 {
583 LARGE_INTEGER distance;
584
585 if (GetFileType(handle) != FILE_TYPE_DISK) {
586 errno = EBADF;
587 return (-1);
588 }
589 distance.QuadPart = length;
590 if (!SetFilePointerEx_perso(handle, distance, NULL, FILE_BEGIN)) {
591 la_dosmaperr(GetLastError());
592 return (-1);
593 }
594 if (!SetEndOfFile(handle)) {
595 la_dosmaperr(GetLastError());
596 return (-1);
597 }
598 return (0);
599 }
600
601 static int
lazy_stat(struct archive_write_disk * a)602 lazy_stat(struct archive_write_disk *a)
603 {
604 if (a->pst != NULL) {
605 /* Already have stat() data available. */
606 return (ARCHIVE_OK);
607 }
608 if (a->fh != INVALID_HANDLE_VALUE &&
609 GetFileInformationByHandle(a->fh, &a->st) == 0) {
610 a->pst = &a->st;
611 return (ARCHIVE_OK);
612 }
613
614 /*
615 * XXX At this point, symlinks should not be hit, otherwise
616 * XXX a race occurred. Do we want to check explicitly for that?
617 */
618 if (file_information(a, a->name, &a->st, NULL, 1) == 0) {
619 a->pst = &a->st;
620 return (ARCHIVE_OK);
621 }
622 archive_set_error(&a->archive, errno, "Couldn't stat file");
623 return (ARCHIVE_WARN);
624 }
625
626 static struct archive_vtable *
archive_write_disk_vtable(void)627 archive_write_disk_vtable(void)
628 {
629 static struct archive_vtable av;
630 static int inited = 0;
631
632 if (!inited) {
633 av.archive_close = _archive_write_disk_close;
634 av.archive_filter_bytes = _archive_write_disk_filter_bytes;
635 av.archive_free = _archive_write_disk_free;
636 av.archive_write_header = _archive_write_disk_header;
637 av.archive_write_finish_entry
638 = _archive_write_disk_finish_entry;
639 av.archive_write_data = _archive_write_disk_data;
640 av.archive_write_data_block = _archive_write_disk_data_block;
641 inited = 1;
642 }
643 return (&av);
644 }
645
646 static int64_t
_archive_write_disk_filter_bytes(struct archive * _a,int n)647 _archive_write_disk_filter_bytes(struct archive *_a, int n)
648 {
649 struct archive_write_disk *a = (struct archive_write_disk *)_a;
650 (void)n; /* UNUSED */
651 if (n == -1 || n == 0)
652 return (a->total_bytes_written);
653 return (-1);
654 }
655
656
657 int
archive_write_disk_set_options(struct archive * _a,int flags)658 archive_write_disk_set_options(struct archive *_a, int flags)
659 {
660 struct archive_write_disk *a = (struct archive_write_disk *)_a;
661
662 a->flags = flags;
663 return (ARCHIVE_OK);
664 }
665
666
667 /*
668 * Extract this entry to disk.
669 *
670 * TODO: Validate hardlinks. According to the standards, we're
671 * supposed to check each extracted hardlink and squawk if it refers
672 * to a file that we didn't restore. I'm not entirely convinced this
673 * is a good idea, but more importantly: Is there any way to validate
674 * hardlinks without keeping a complete list of filenames from the
675 * entire archive?? Ugh.
676 *
677 */
678 static int
_archive_write_disk_header(struct archive * _a,struct archive_entry * entry)679 _archive_write_disk_header(struct archive *_a, struct archive_entry *entry)
680 {
681 struct archive_write_disk *a = (struct archive_write_disk *)_a;
682 struct fixup_entry *fe;
683 int ret, r;
684
685 archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
686 ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
687 "archive_write_disk_header");
688 archive_clear_error(&a->archive);
689 if (a->archive.state & ARCHIVE_STATE_DATA) {
690 r = _archive_write_disk_finish_entry(&a->archive);
691 if (r == ARCHIVE_FATAL)
692 return (r);
693 }
694
695 /* Set up for this particular entry. */
696 a->pst = NULL;
697 a->current_fixup = NULL;
698 a->deferred = 0;
699 if (a->entry) {
700 archive_entry_free(a->entry);
701 a->entry = NULL;
702 }
703 a->entry = archive_entry_clone(entry);
704 a->fh = INVALID_HANDLE_VALUE;
705 a->fd_offset = 0;
706 a->offset = 0;
707 a->restore_pwd = -1;
708 a->uid = a->user_uid;
709 a->mode = archive_entry_mode(a->entry);
710 if (archive_entry_size_is_set(a->entry))
711 a->filesize = archive_entry_size(a->entry);
712 else
713 a->filesize = -1;
714 archive_wstrcpy(&(a->_name_data), archive_entry_pathname_w(a->entry));
715 a->name = a->_name_data.s;
716 archive_clear_error(&a->archive);
717
718 /*
719 * Clean up the requested path. This is necessary for correct
720 * dir restores; the dir restore logic otherwise gets messed
721 * up by nonsense like "dir/.".
722 */
723 ret = cleanup_pathname(a);
724 if (ret != ARCHIVE_OK)
725 return (ret);
726
727 /*
728 * Generate a full-pathname and use it from here.
729 */
730 if (permissive_name_w(a) < 0) {
731 errno = EINVAL;
732 return (ARCHIVE_FAILED);
733 }
734
735 /*
736 * Query the umask so we get predictable mode settings.
737 * This gets done on every call to _write_header in case the
738 * user edits their umask during the extraction for some
739 * reason.
740 */
741 umask(a->user_umask = umask(0));
742
743 /* Figure out what we need to do for this entry. */
744 a->todo = TODO_MODE_BASE;
745 if (a->flags & ARCHIVE_EXTRACT_PERM) {
746 a->todo |= TODO_MODE_FORCE; /* Be pushy about permissions. */
747 /*
748 * SGID requires an extra "check" step because we
749 * cannot easily predict the GID that the system will
750 * assign. (Different systems assign GIDs to files
751 * based on a variety of criteria, including process
752 * credentials and the gid of the enclosing
753 * directory.) We can only restore the SGID bit if
754 * the file has the right GID, and we only know the
755 * GID if we either set it (see set_ownership) or if
756 * we've actually called stat() on the file after it
757 * was restored. Since there are several places at
758 * which we might verify the GID, we need a TODO bit
759 * to keep track.
760 */
761 if (a->mode & S_ISGID)
762 a->todo |= TODO_SGID | TODO_SGID_CHECK;
763 /*
764 * Verifying the SUID is simpler, but can still be
765 * done in multiple ways, hence the separate "check" bit.
766 */
767 if (a->mode & S_ISUID)
768 a->todo |= TODO_SUID | TODO_SUID_CHECK;
769 } else {
770 /*
771 * User didn't request full permissions, so don't
772 * restore SUID, SGID bits and obey umask.
773 */
774 a->mode &= ~S_ISUID;
775 a->mode &= ~S_ISGID;
776 a->mode &= ~S_ISVTX;
777 a->mode &= ~a->user_umask;
778 }
779 #if 0
780 if (a->flags & ARCHIVE_EXTRACT_OWNER)
781 a->todo |= TODO_OWNER;
782 #endif
783 if (a->flags & ARCHIVE_EXTRACT_TIME)
784 a->todo |= TODO_TIMES;
785 if (a->flags & ARCHIVE_EXTRACT_ACL) {
786 if (archive_entry_filetype(a->entry) == AE_IFDIR)
787 a->deferred |= TODO_ACLS;
788 else
789 a->todo |= TODO_ACLS;
790 }
791 if (a->flags & ARCHIVE_EXTRACT_XATTR)
792 a->todo |= TODO_XATTR;
793 if (a->flags & ARCHIVE_EXTRACT_FFLAGS)
794 a->todo |= TODO_FFLAGS;
795 if (a->flags & ARCHIVE_EXTRACT_SECURE_SYMLINKS) {
796 ret = check_symlinks(a);
797 if (ret != ARCHIVE_OK)
798 return (ret);
799 }
800
801 ret = restore_entry(a);
802
803 /*
804 * TODO: There are rumours that some extended attributes must
805 * be restored before file data is written. If this is true,
806 * then we either need to write all extended attributes both
807 * before and after restoring the data, or find some rule for
808 * determining which must go first and which last. Due to the
809 * many ways people are using xattrs, this may prove to be an
810 * intractable problem.
811 */
812
813 /*
814 * Fixup uses the unedited pathname from archive_entry_pathname(),
815 * because it is relative to the base dir and the edited path
816 * might be relative to some intermediate dir as a result of the
817 * deep restore logic.
818 */
819 if (a->deferred & TODO_MODE) {
820 fe = current_fixup(a, archive_entry_pathname_w(entry));
821 fe->fixup |= TODO_MODE_BASE;
822 fe->mode = a->mode;
823 }
824
825 if ((a->deferred & TODO_TIMES)
826 && (archive_entry_mtime_is_set(entry)
827 || archive_entry_atime_is_set(entry))) {
828 fe = current_fixup(a, archive_entry_pathname_w(entry));
829 fe->mode = a->mode;
830 fe->fixup |= TODO_TIMES;
831 if (archive_entry_atime_is_set(entry)) {
832 fe->atime = archive_entry_atime(entry);
833 fe->atime_nanos = archive_entry_atime_nsec(entry);
834 } else {
835 /* If atime is unset, use start time. */
836 fe->atime = a->start_time;
837 fe->atime_nanos = 0;
838 }
839 if (archive_entry_mtime_is_set(entry)) {
840 fe->mtime = archive_entry_mtime(entry);
841 fe->mtime_nanos = archive_entry_mtime_nsec(entry);
842 } else {
843 /* If mtime is unset, use start time. */
844 fe->mtime = a->start_time;
845 fe->mtime_nanos = 0;
846 }
847 if (archive_entry_birthtime_is_set(entry)) {
848 fe->birthtime = archive_entry_birthtime(entry);
849 fe->birthtime_nanos = archive_entry_birthtime_nsec(entry);
850 } else {
851 /* If birthtime is unset, use mtime. */
852 fe->birthtime = fe->mtime;
853 fe->birthtime_nanos = fe->mtime_nanos;
854 }
855 }
856
857 if (a->deferred & TODO_ACLS) {
858 fe = current_fixup(a, archive_entry_pathname_w(entry));
859 archive_acl_copy(&fe->acl, archive_entry_acl(entry));
860 }
861
862 if (a->deferred & TODO_FFLAGS) {
863 fe = current_fixup(a, archive_entry_pathname_w(entry));
864 fe->fixup |= TODO_FFLAGS;
865 /* TODO: Complete this.. defer fflags from below. */
866 }
867
868 /*
869 * On Windows, A creating sparse file requires a special mark.
870 */
871 if (a->fh != INVALID_HANDLE_VALUE &&
872 archive_entry_sparse_count(entry) > 0) {
873 int64_t base = 0, offset, length;
874 int i, cnt = archive_entry_sparse_reset(entry);
875 int sparse = 0;
876
877 for (i = 0; i < cnt; i++) {
878 archive_entry_sparse_next(entry, &offset, &length);
879 if (offset - base >= 4096) {
880 sparse = 1;/* we have a hole. */
881 break;
882 }
883 base = offset + length;
884 }
885 if (sparse) {
886 DWORD dmy;
887 /* Mark this file as sparse. */
888 DeviceIoControl(a->fh, FSCTL_SET_SPARSE,
889 NULL, 0, NULL, 0, &dmy, NULL);
890 }
891 }
892
893 /* We've created the object and are ready to pour data into it. */
894 if (ret >= ARCHIVE_WARN)
895 a->archive.state = ARCHIVE_STATE_DATA;
896 /*
897 * If it's not open, tell our client not to try writing.
898 * In particular, dirs, links, etc, don't get written to.
899 */
900 if (a->fh == INVALID_HANDLE_VALUE) {
901 archive_entry_set_size(entry, 0);
902 a->filesize = 0;
903 }
904
905 return (ret);
906 }
907
908 int
archive_write_disk_set_skip_file(struct archive * _a,int64_t d,int64_t i)909 archive_write_disk_set_skip_file(struct archive *_a, int64_t d, int64_t i)
910 {
911 struct archive_write_disk *a = (struct archive_write_disk *)_a;
912 archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
913 ARCHIVE_STATE_ANY, "archive_write_disk_set_skip_file");
914 a->skip_file_set = 1;
915 a->skip_file_dev = d;
916 a->skip_file_ino = i;
917 return (ARCHIVE_OK);
918 }
919
920 static ssize_t
write_data_block(struct archive_write_disk * a,const char * buff,size_t size)921 write_data_block(struct archive_write_disk *a, const char *buff, size_t size)
922 {
923 OVERLAPPED ol;
924 uint64_t start_size = size;
925 DWORD bytes_written = 0;
926 ssize_t block_size = 0, bytes_to_write;
927
928 if (size == 0)
929 return (ARCHIVE_OK);
930
931 if (a->filesize == 0 || a->fh == INVALID_HANDLE_VALUE) {
932 archive_set_error(&a->archive, 0,
933 "Attempt to write to an empty file");
934 return (ARCHIVE_WARN);
935 }
936
937 if (a->flags & ARCHIVE_EXTRACT_SPARSE) {
938 /* XXX TODO XXX Is there a more appropriate choice here ? */
939 /* This needn't match the filesystem allocation size. */
940 block_size = 16*1024;
941 }
942
943 /* If this write would run beyond the file size, truncate it. */
944 if (a->filesize >= 0 && (int64_t)(a->offset + size) > a->filesize)
945 start_size = size = (size_t)(a->filesize - a->offset);
946
947 /* Write the data. */
948 while (size > 0) {
949 if (block_size == 0) {
950 bytes_to_write = size;
951 } else {
952 /* We're sparsifying the file. */
953 const char *p, *end;
954 int64_t block_end;
955
956 /* Skip leading zero bytes. */
957 for (p = buff, end = buff + size; p < end; ++p) {
958 if (*p != '\0')
959 break;
960 }
961 a->offset += p - buff;
962 size -= p - buff;
963 buff = p;
964 if (size == 0)
965 break;
966
967 /* Calculate next block boundary after offset. */
968 block_end
969 = (a->offset / block_size + 1) * block_size;
970
971 /* If the adjusted write would cross block boundary,
972 * truncate it to the block boundary. */
973 bytes_to_write = size;
974 if (a->offset + bytes_to_write > block_end)
975 bytes_to_write = (DWORD)(block_end - a->offset);
976 }
977 memset(&ol, 0, sizeof(ol));
978 ol.Offset = (DWORD)(a->offset & 0xFFFFFFFF);
979 ol.OffsetHigh = (DWORD)(a->offset >> 32);
980 if (!WriteFile(a->fh, buff, (uint32_t)bytes_to_write,
981 &bytes_written, &ol)) {
982 DWORD lasterr;
983
984 lasterr = GetLastError();
985 if (lasterr == ERROR_ACCESS_DENIED)
986 errno = EBADF;
987 else
988 la_dosmaperr(lasterr);
989 archive_set_error(&a->archive, errno, "Write failed");
990 return (ARCHIVE_WARN);
991 }
992 buff += bytes_written;
993 size -= bytes_written;
994 a->total_bytes_written += bytes_written;
995 a->offset += bytes_written;
996 a->fd_offset = a->offset;
997 }
998 return ((ssize_t)(start_size - size));
999 }
1000
1001 static ssize_t
_archive_write_disk_data_block(struct archive * _a,const void * buff,size_t size,int64_t offset)1002 _archive_write_disk_data_block(struct archive *_a,
1003 const void *buff, size_t size, int64_t offset)
1004 {
1005 struct archive_write_disk *a = (struct archive_write_disk *)_a;
1006 ssize_t r;
1007
1008 archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
1009 ARCHIVE_STATE_DATA, "archive_write_data_block");
1010
1011 a->offset = offset;
1012 r = write_data_block(a, buff, size);
1013 if (r < ARCHIVE_OK)
1014 return (r);
1015 if ((size_t)r < size) {
1016 archive_set_error(&a->archive, 0,
1017 "Write request too large");
1018 return (ARCHIVE_WARN);
1019 }
1020 #if ARCHIVE_VERSION_NUMBER < 3999000
1021 return (ARCHIVE_OK);
1022 #else
1023 return (size);
1024 #endif
1025 }
1026
1027 static ssize_t
_archive_write_disk_data(struct archive * _a,const void * buff,size_t size)1028 _archive_write_disk_data(struct archive *_a, const void *buff, size_t size)
1029 {
1030 struct archive_write_disk *a = (struct archive_write_disk *)_a;
1031
1032 archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
1033 ARCHIVE_STATE_DATA, "archive_write_data");
1034
1035 return (write_data_block(a, buff, size));
1036 }
1037
1038 static int
_archive_write_disk_finish_entry(struct archive * _a)1039 _archive_write_disk_finish_entry(struct archive *_a)
1040 {
1041 struct archive_write_disk *a = (struct archive_write_disk *)_a;
1042 int ret = ARCHIVE_OK;
1043
1044 archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
1045 ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
1046 "archive_write_finish_entry");
1047 if (a->archive.state & ARCHIVE_STATE_HEADER)
1048 return (ARCHIVE_OK);
1049 archive_clear_error(&a->archive);
1050
1051 /* Pad or truncate file to the right size. */
1052 if (a->fh == INVALID_HANDLE_VALUE) {
1053 /* There's no file. */
1054 } else if (a->filesize < 0) {
1055 /* File size is unknown, so we can't set the size. */
1056 } else if (a->fd_offset == a->filesize) {
1057 /* Last write ended at exactly the filesize; we're done. */
1058 /* Hopefully, this is the common case. */
1059 } else {
1060 if (la_ftruncate(a->fh, a->filesize) == -1) {
1061 archive_set_error(&a->archive, errno,
1062 "File size could not be restored");
1063 return (ARCHIVE_FAILED);
1064 }
1065 }
1066
1067 /* Restore metadata. */
1068
1069 /*
1070 * Look up the "real" UID only if we're going to need it.
1071 * TODO: the TODO_SGID condition can be dropped here, can't it?
1072 */
1073 if (a->todo & (TODO_OWNER | TODO_SUID | TODO_SGID)) {
1074 a->uid = archive_write_disk_uid(&a->archive,
1075 archive_entry_uname(a->entry),
1076 archive_entry_uid(a->entry));
1077 }
1078 /* Look up the "real" GID only if we're going to need it. */
1079 /* TODO: the TODO_SUID condition can be dropped here, can't it? */
1080 if (a->todo & (TODO_OWNER | TODO_SGID | TODO_SUID)) {
1081 a->gid = archive_write_disk_gid(&a->archive,
1082 archive_entry_gname(a->entry),
1083 archive_entry_gid(a->entry));
1084 }
1085
1086 /*
1087 * Restore ownership before set_mode tries to restore suid/sgid
1088 * bits. If we set the owner, we know what it is and can skip
1089 * a stat() call to examine the ownership of the file on disk.
1090 */
1091 if (a->todo & TODO_OWNER)
1092 ret = set_ownership(a);
1093
1094 /*
1095 * set_mode must precede ACLs on systems such as Solaris and
1096 * FreeBSD where setting the mode implicitly clears extended ACLs
1097 */
1098 if (a->todo & TODO_MODE) {
1099 int r2 = set_mode(a, a->mode);
1100 if (r2 < ret) ret = r2;
1101 }
1102
1103 /*
1104 * Security-related extended attributes (such as
1105 * security.capability on Linux) have to be restored last,
1106 * since they're implicitly removed by other file changes.
1107 */
1108 if (a->todo & TODO_XATTR) {
1109 int r2 = set_xattrs(a);
1110 if (r2 < ret) ret = r2;
1111 }
1112
1113 /*
1114 * Some flags prevent file modification; they must be restored after
1115 * file contents are written.
1116 */
1117 if (a->todo & TODO_FFLAGS) {
1118 int r2 = set_fflags(a);
1119 if (r2 < ret) ret = r2;
1120 }
1121
1122 /*
1123 * Time must follow most other metadata;
1124 * otherwise atime will get changed.
1125 */
1126 if (a->todo & TODO_TIMES) {
1127 int r2 = set_times_from_entry(a);
1128 if (r2 < ret) ret = r2;
1129 }
1130
1131 /*
1132 * ACLs must be restored after timestamps because there are
1133 * ACLs that prevent attribute changes (including time).
1134 */
1135 if (a->todo & TODO_ACLS) {
1136 int r2 = set_acls(a, a->fh,
1137 archive_entry_pathname_w(a->entry),
1138 archive_entry_acl(a->entry));
1139 if (r2 < ret) ret = r2;
1140 }
1141
1142 /* If there's an fd, we can close it now. */
1143 if (a->fh != INVALID_HANDLE_VALUE) {
1144 CloseHandle(a->fh);
1145 a->fh = INVALID_HANDLE_VALUE;
1146 }
1147 /* If there's an entry, we can release it now. */
1148 if (a->entry) {
1149 archive_entry_free(a->entry);
1150 a->entry = NULL;
1151 }
1152 a->archive.state = ARCHIVE_STATE_HEADER;
1153 return (ret);
1154 }
1155
1156 int
archive_write_disk_set_group_lookup(struct archive * _a,void * private_data,int64_t (* lookup_gid)(void * private,const char * gname,int64_t gid),void (* cleanup_gid)(void * private))1157 archive_write_disk_set_group_lookup(struct archive *_a,
1158 void *private_data,
1159 int64_t (*lookup_gid)(void *private, const char *gname, int64_t gid),
1160 void (*cleanup_gid)(void *private))
1161 {
1162 struct archive_write_disk *a = (struct archive_write_disk *)_a;
1163 archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
1164 ARCHIVE_STATE_ANY, "archive_write_disk_set_group_lookup");
1165
1166 if (a->cleanup_gid != NULL && a->lookup_gid_data != NULL)
1167 (a->cleanup_gid)(a->lookup_gid_data);
1168
1169 a->lookup_gid = lookup_gid;
1170 a->cleanup_gid = cleanup_gid;
1171 a->lookup_gid_data = private_data;
1172 return (ARCHIVE_OK);
1173 }
1174
1175 int
archive_write_disk_set_user_lookup(struct archive * _a,void * private_data,int64_t (* lookup_uid)(void * private,const char * uname,int64_t uid),void (* cleanup_uid)(void * private))1176 archive_write_disk_set_user_lookup(struct archive *_a,
1177 void *private_data,
1178 int64_t (*lookup_uid)(void *private, const char *uname, int64_t uid),
1179 void (*cleanup_uid)(void *private))
1180 {
1181 struct archive_write_disk *a = (struct archive_write_disk *)_a;
1182 archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
1183 ARCHIVE_STATE_ANY, "archive_write_disk_set_user_lookup");
1184
1185 if (a->cleanup_uid != NULL && a->lookup_uid_data != NULL)
1186 (a->cleanup_uid)(a->lookup_uid_data);
1187
1188 a->lookup_uid = lookup_uid;
1189 a->cleanup_uid = cleanup_uid;
1190 a->lookup_uid_data = private_data;
1191 return (ARCHIVE_OK);
1192 }
1193
1194 int64_t
archive_write_disk_gid(struct archive * _a,const char * name,int64_t id)1195 archive_write_disk_gid(struct archive *_a, const char *name, int64_t id)
1196 {
1197 struct archive_write_disk *a = (struct archive_write_disk *)_a;
1198 archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
1199 ARCHIVE_STATE_ANY, "archive_write_disk_gid");
1200 if (a->lookup_gid)
1201 return (a->lookup_gid)(a->lookup_gid_data, name, id);
1202 return (id);
1203 }
1204
1205 int64_t
archive_write_disk_uid(struct archive * _a,const char * name,int64_t id)1206 archive_write_disk_uid(struct archive *_a, const char *name, int64_t id)
1207 {
1208 struct archive_write_disk *a = (struct archive_write_disk *)_a;
1209 archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
1210 ARCHIVE_STATE_ANY, "archive_write_disk_uid");
1211 if (a->lookup_uid)
1212 return (a->lookup_uid)(a->lookup_uid_data, name, id);
1213 return (id);
1214 }
1215
1216 /*
1217 * Create a new archive_write_disk object and initialize it with global state.
1218 */
1219 struct archive *
archive_write_disk_new(void)1220 archive_write_disk_new(void)
1221 {
1222 struct archive_write_disk *a;
1223
1224 a = (struct archive_write_disk *)calloc(1, sizeof(*a));
1225 if (a == NULL)
1226 return (NULL);
1227 a->archive.magic = ARCHIVE_WRITE_DISK_MAGIC;
1228 /* We're ready to write a header immediately. */
1229 a->archive.state = ARCHIVE_STATE_HEADER;
1230 a->archive.vtable = archive_write_disk_vtable();
1231 a->start_time = time(NULL);
1232 /* Query and restore the umask. */
1233 umask(a->user_umask = umask(0));
1234 if (archive_wstring_ensure(&a->path_safe, 512) == NULL) {
1235 free(a);
1236 return (NULL);
1237 }
1238 return (&a->archive);
1239 }
1240
1241 static int
disk_unlink(wchar_t * path)1242 disk_unlink(wchar_t *path)
1243 {
1244 wchar_t *fullname;
1245 int r;
1246
1247 r = _wunlink(path);
1248 if (r != 0 && GetLastError() == ERROR_INVALID_NAME) {
1249 fullname = __la_win_permissive_name_w(path);
1250 r = _wunlink(fullname);
1251 free(fullname);
1252 }
1253 return (r);
1254 }
1255
1256 static int
disk_rmdir(wchar_t * path)1257 disk_rmdir(wchar_t *path)
1258 {
1259 wchar_t *fullname;
1260 int r;
1261
1262 r = _wrmdir(path);
1263 if (r != 0 && GetLastError() == ERROR_INVALID_NAME) {
1264 fullname = __la_win_permissive_name_w(path);
1265 r = _wrmdir(fullname);
1266 free(fullname);
1267 }
1268 return (r);
1269 }
1270
1271 /*
1272 * The main restore function.
1273 */
1274 static int
restore_entry(struct archive_write_disk * a)1275 restore_entry(struct archive_write_disk *a)
1276 {
1277 int ret = ARCHIVE_OK, en;
1278
1279 if (a->flags & ARCHIVE_EXTRACT_UNLINK && !S_ISDIR(a->mode)) {
1280 /*
1281 * TODO: Fix this. Apparently, there are platforms
1282 * that still allow root to hose the entire filesystem
1283 * by unlinking a dir. The S_ISDIR() test above
1284 * prevents us from using unlink() here if the new
1285 * object is a dir, but that doesn't mean the old
1286 * object isn't a dir.
1287 */
1288 if (disk_unlink(a->name) == 0) {
1289 /* We removed it, reset cached stat. */
1290 a->pst = NULL;
1291 } else if (errno == ENOENT) {
1292 /* File didn't exist, that's just as good. */
1293 } else if (disk_rmdir(a->name) == 0) {
1294 /* It was a dir, but now it's gone. */
1295 a->pst = NULL;
1296 } else {
1297 /* We tried, but couldn't get rid of it. */
1298 archive_set_error(&a->archive, errno,
1299 "Could not unlink");
1300 return(ARCHIVE_FAILED);
1301 }
1302 }
1303
1304 /* Try creating it first; if this fails, we'll try to recover. */
1305 en = create_filesystem_object(a);
1306
1307 if ((en == ENOTDIR || en == ENOENT)
1308 && !(a->flags & ARCHIVE_EXTRACT_NO_AUTODIR)) {
1309 wchar_t *full;
1310 /* If the parent dir doesn't exist, try creating it. */
1311 create_parent_dir(a, a->name);
1312 /* Now try to create the object again. */
1313 full = __la_win_permissive_name_w(a->name);
1314 if (full == NULL) {
1315 en = EINVAL;
1316 } else {
1317 /* Remove multiple directories such as "a/../b../c" */
1318 archive_wstrcpy(&(a->_name_data), full);
1319 a->name = a->_name_data.s;
1320 free(full);
1321 en = create_filesystem_object(a);
1322 }
1323 }
1324
1325 if ((en == EISDIR || en == EEXIST)
1326 && (a->flags & ARCHIVE_EXTRACT_NO_OVERWRITE)) {
1327 /* If we're not overwriting, we're done. */
1328 archive_entry_unset_size(a->entry);
1329 return (ARCHIVE_OK);
1330 }
1331
1332 /*
1333 * Some platforms return EISDIR if you call
1334 * open(O_WRONLY | O_EXCL | O_CREAT) on a directory, some
1335 * return EEXIST. POSIX is ambiguous, requiring EISDIR
1336 * for open(O_WRONLY) on a dir and EEXIST for open(O_EXCL | O_CREAT)
1337 * on an existing item.
1338 */
1339 if (en == EISDIR) {
1340 /* A dir is in the way of a non-dir, rmdir it. */
1341 if (disk_rmdir(a->name) != 0) {
1342 archive_set_error(&a->archive, errno,
1343 "Can't remove already-existing dir");
1344 return (ARCHIVE_FAILED);
1345 }
1346 a->pst = NULL;
1347 /* Try again. */
1348 en = create_filesystem_object(a);
1349 } else if (en == EEXIST) {
1350 mode_t st_mode;
1351 /*
1352 * We know something is in the way, but we don't know what;
1353 * we need to find out before we go any further.
1354 */
1355 int r = 0;
1356 /*
1357 * The SECURE_SYMLINK logic has already removed a
1358 * symlink to a dir if the client wants that. So
1359 * follow the symlink if we're creating a dir.
1360 */
1361 if (S_ISDIR(a->mode))
1362 r = file_information(a, a->name, &a->st, &st_mode, 0);
1363 /*
1364 * If it's not a dir (or it's a broken symlink),
1365 * then don't follow it.
1366 */
1367 if (r != 0 || !S_ISDIR(a->mode))
1368 r = file_information(a, a->name, &a->st, &st_mode, 1);
1369 if (r != 0) {
1370 archive_set_error(&a->archive, errno,
1371 "Can't stat existing object");
1372 return (ARCHIVE_FAILED);
1373 }
1374
1375 /*
1376 * NO_OVERWRITE_NEWER doesn't apply to directories.
1377 */
1378 if ((a->flags & ARCHIVE_EXTRACT_NO_OVERWRITE_NEWER)
1379 && !S_ISDIR(st_mode)) {
1380 if (!older(&(a->st), a->entry)) {
1381 archive_entry_unset_size(a->entry);
1382 return (ARCHIVE_OK);
1383 }
1384 }
1385
1386 /* If it's our archive, we're done. */
1387 if (a->skip_file_set &&
1388 bhfi_dev(&a->st) == a->skip_file_dev &&
1389 bhfi_ino(&a->st) == a->skip_file_ino) {
1390 archive_set_error(&a->archive, 0,
1391 "Refusing to overwrite archive");
1392 return (ARCHIVE_FAILED);
1393 }
1394
1395 if (!S_ISDIR(st_mode)) {
1396 /* A non-dir is in the way, unlink it. */
1397 if (disk_unlink(a->name) != 0) {
1398 archive_set_error(&a->archive, errno,
1399 "Can't unlink already-existing object");
1400 return (ARCHIVE_FAILED);
1401 }
1402 a->pst = NULL;
1403 /* Try again. */
1404 en = create_filesystem_object(a);
1405 } else if (!S_ISDIR(a->mode)) {
1406 /* A dir is in the way of a non-dir, rmdir it. */
1407 if (disk_rmdir(a->name) != 0) {
1408 archive_set_error(&a->archive, errno,
1409 "Can't remove already-existing dir");
1410 return (ARCHIVE_FAILED);
1411 }
1412 /* Try again. */
1413 en = create_filesystem_object(a);
1414 } else {
1415 /*
1416 * There's a dir in the way of a dir. Don't
1417 * waste time with rmdir()/mkdir(), just fix
1418 * up the permissions on the existing dir.
1419 * Note that we don't change perms on existing
1420 * dirs unless _EXTRACT_PERM is specified.
1421 */
1422 if ((a->mode != st_mode)
1423 && (a->todo & TODO_MODE_FORCE))
1424 a->deferred |= (a->todo & TODO_MODE);
1425 /* Ownership doesn't need deferred fixup. */
1426 en = 0; /* Forget the EEXIST. */
1427 }
1428 }
1429
1430 if (en) {
1431 /* Everything failed; give up here. */
1432 archive_set_error(&a->archive, en, "Can't create '%ls'",
1433 a->name);
1434 return (ARCHIVE_FAILED);
1435 }
1436
1437 a->pst = NULL; /* Cached stat data no longer valid. */
1438 return (ret);
1439 }
1440
1441 /*
1442 * Returns 0 if creation succeeds, or else returns errno value from
1443 * the failed system call. Note: This function should only ever perform
1444 * a single system call.
1445 */
1446 static int
create_filesystem_object(struct archive_write_disk * a)1447 create_filesystem_object(struct archive_write_disk *a)
1448 {
1449 /* Create the entry. */
1450 const wchar_t *linkname;
1451 wchar_t *fullname;
1452 mode_t final_mode, mode;
1453 int r;
1454
1455 /* We identify hard/symlinks according to the link names. */
1456 /* Since link(2) and symlink(2) don't handle modes, we're done here. */
1457 linkname = archive_entry_hardlink_w(a->entry);
1458 if (linkname != NULL) {
1459 wchar_t *linkfull, *namefull;
1460
1461 linkfull = __la_win_permissive_name_w(linkname);
1462 namefull = __la_win_permissive_name_w(a->name);
1463 if (linkfull == NULL || namefull == NULL) {
1464 errno = EINVAL;
1465 r = -1;
1466 } else {
1467 r = la_CreateHardLinkW(namefull, linkfull);
1468 if (r == 0) {
1469 la_dosmaperr(GetLastError());
1470 r = errno;
1471 } else
1472 r = 0;
1473 }
1474 /*
1475 * New cpio and pax formats allow hardlink entries
1476 * to carry data, so we may have to open the file
1477 * for hardlink entries.
1478 *
1479 * If the hardlink was successfully created and
1480 * the archive doesn't have carry data for it,
1481 * consider it to be non-authoritative for meta data.
1482 * This is consistent with GNU tar and BSD pax.
1483 * If the hardlink does carry data, let the last
1484 * archive entry decide ownership.
1485 */
1486 if (r == 0 && a->filesize <= 0) {
1487 a->todo = 0;
1488 a->deferred = 0;
1489 } else if (r == 0 && a->filesize > 0) {
1490 a->fh = CreateFileW(namefull, GENERIC_WRITE, 0, NULL,
1491 TRUNCATE_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1492 if (a->fh == INVALID_HANDLE_VALUE) {
1493 la_dosmaperr(GetLastError());
1494 r = errno;
1495 }
1496 }
1497 free(linkfull);
1498 free(namefull);
1499 return (r);
1500 }
1501 linkname = archive_entry_symlink_w(a->entry);
1502 if (linkname != NULL) {
1503 #if HAVE_SYMLINK
1504 return symlink(linkname, a->name) ? errno : 0;
1505 #else
1506 return (EPERM);
1507 #endif
1508 }
1509
1510 /*
1511 * The remaining system calls all set permissions, so let's
1512 * try to take advantage of that to avoid an extra chmod()
1513 * call. (Recall that umask is set to zero right now!)
1514 */
1515
1516 /* Mode we want for the final restored object (w/o file type bits). */
1517 final_mode = a->mode & 07777;
1518 /*
1519 * The mode that will actually be restored in this step. Note
1520 * that SUID, SGID, etc, require additional work to ensure
1521 * security, so we never restore them at this point.
1522 */
1523 mode = final_mode & 0777 & ~a->user_umask;
1524
1525 switch (a->mode & AE_IFMT) {
1526 default:
1527 /* POSIX requires that we fall through here. */
1528 /* FALLTHROUGH */
1529 case AE_IFREG:
1530 fullname = a->name;
1531 /* O_WRONLY | O_CREAT | O_EXCL */
1532 a->fh = CreateFileW(fullname, GENERIC_WRITE, 0, NULL,
1533 CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
1534 if (a->fh == INVALID_HANDLE_VALUE &&
1535 GetLastError() == ERROR_INVALID_NAME &&
1536 fullname == a->name) {
1537 fullname = __la_win_permissive_name_w(a->name);
1538 a->fh = CreateFileW(fullname, GENERIC_WRITE, 0, NULL,
1539 CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
1540 }
1541 if (a->fh == INVALID_HANDLE_VALUE) {
1542 if (GetLastError() == ERROR_ACCESS_DENIED) {
1543 DWORD attr;
1544 /* Simulate an errno of POSIX system. */
1545 attr = GetFileAttributesW(fullname);
1546 if (attr == (DWORD)-1)
1547 la_dosmaperr(GetLastError());
1548 else if (attr & FILE_ATTRIBUTE_DIRECTORY)
1549 errno = EISDIR;
1550 else
1551 errno = EACCES;
1552 } else
1553 la_dosmaperr(GetLastError());
1554 r = 1;
1555 } else
1556 r = 0;
1557 if (fullname != a->name)
1558 free(fullname);
1559 break;
1560 case AE_IFCHR:
1561 case AE_IFBLK:
1562 /* TODO: Find a better way to warn about our inability
1563 * to restore a block device node. */
1564 return (EINVAL);
1565 case AE_IFDIR:
1566 mode = (mode | MINIMUM_DIR_MODE) & MAXIMUM_DIR_MODE;
1567 fullname = a->name;
1568 r = CreateDirectoryW(fullname, NULL);
1569 if (r == 0 && GetLastError() == ERROR_INVALID_NAME &&
1570 fullname == a->name) {
1571 fullname = __la_win_permissive_name_w(a->name);
1572 r = CreateDirectoryW(fullname, NULL);
1573 }
1574 if (r != 0) {
1575 r = 0;
1576 /* Defer setting dir times. */
1577 a->deferred |= (a->todo & TODO_TIMES);
1578 a->todo &= ~TODO_TIMES;
1579 /* Never use an immediate chmod(). */
1580 /* We can't avoid the chmod() entirely if EXTRACT_PERM
1581 * because of SysV SGID inheritance. */
1582 if ((mode != final_mode)
1583 || (a->flags & ARCHIVE_EXTRACT_PERM))
1584 a->deferred |= (a->todo & TODO_MODE);
1585 a->todo &= ~TODO_MODE;
1586 } else {
1587 la_dosmaperr(GetLastError());
1588 r = -1;
1589 }
1590 if (fullname != a->name)
1591 free(fullname);
1592 break;
1593 case AE_IFIFO:
1594 /* TODO: Find a better way to warn about our inability
1595 * to restore a fifo. */
1596 return (EINVAL);
1597 }
1598
1599 /* All the system calls above set errno on failure. */
1600 if (r)
1601 return (errno);
1602
1603 /* If we managed to set the final mode, we've avoided a chmod(). */
1604 if (mode == final_mode)
1605 a->todo &= ~TODO_MODE;
1606 return (0);
1607 }
1608
1609 /*
1610 * Cleanup function for archive_extract. Mostly, this involves processing
1611 * the fixup list, which is used to address a number of problems:
1612 * * Dir permissions might prevent us from restoring a file in that
1613 * dir, so we restore the dir with minimum 0700 permissions first,
1614 * then correct the mode at the end.
1615 * * Similarly, the act of restoring a file touches the directory
1616 * and changes the timestamp on the dir, so we have to touch-up dir
1617 * timestamps at the end as well.
1618 * * Some file flags can interfere with the restore by, for example,
1619 * preventing the creation of hardlinks to those files.
1620 * * Mac OS extended metadata includes ACLs, so must be deferred on dirs.
1621 *
1622 * Note that tar/cpio do not require that archives be in a particular
1623 * order; there is no way to know when the last file has been restored
1624 * within a directory, so there's no way to optimize the memory usage
1625 * here by fixing up the directory any earlier than the
1626 * end-of-archive.
1627 *
1628 * XXX TODO: Directory ACLs should be restored here, for the same
1629 * reason we set directory perms here. XXX
1630 */
1631 static int
_archive_write_disk_close(struct archive * _a)1632 _archive_write_disk_close(struct archive *_a)
1633 {
1634 struct archive_write_disk *a = (struct archive_write_disk *)_a;
1635 struct fixup_entry *next, *p;
1636 int ret;
1637
1638 archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
1639 ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
1640 "archive_write_disk_close");
1641 ret = _archive_write_disk_finish_entry(&a->archive);
1642
1643 /* Sort dir list so directories are fixed up in depth-first order. */
1644 p = sort_dir_list(a->fixup_list);
1645
1646 while (p != NULL) {
1647 a->pst = NULL; /* Mark stat cache as out-of-date. */
1648 if (p->fixup & TODO_TIMES) {
1649 set_times(a, INVALID_HANDLE_VALUE, p->mode, p->name,
1650 p->atime, p->atime_nanos,
1651 p->birthtime, p->birthtime_nanos,
1652 p->mtime, p->mtime_nanos,
1653 p->ctime, p->ctime_nanos);
1654 }
1655 if (p->fixup & TODO_MODE_BASE)
1656 la_chmod(p->name, p->mode);
1657 if (p->fixup & TODO_ACLS)
1658 set_acls(a, INVALID_HANDLE_VALUE, p->name, &p->acl);
1659 next = p->next;
1660 archive_acl_clear(&p->acl);
1661 free(p->name);
1662 free(p);
1663 p = next;
1664 }
1665 a->fixup_list = NULL;
1666 return (ret);
1667 }
1668
1669 static int
_archive_write_disk_free(struct archive * _a)1670 _archive_write_disk_free(struct archive *_a)
1671 {
1672 struct archive_write_disk *a;
1673 int ret;
1674 if (_a == NULL)
1675 return (ARCHIVE_OK);
1676 archive_check_magic(_a, ARCHIVE_WRITE_DISK_MAGIC,
1677 ARCHIVE_STATE_ANY | ARCHIVE_STATE_FATAL, "archive_write_disk_free");
1678 a = (struct archive_write_disk *)_a;
1679 ret = _archive_write_disk_close(&a->archive);
1680 archive_write_disk_set_group_lookup(&a->archive, NULL, NULL, NULL);
1681 archive_write_disk_set_user_lookup(&a->archive, NULL, NULL, NULL);
1682 if (a->entry)
1683 archive_entry_free(a->entry);
1684 archive_wstring_free(&a->_name_data);
1685 archive_string_free(&a->archive.error_string);
1686 archive_wstring_free(&a->path_safe);
1687 a->archive.magic = 0;
1688 __archive_clean(&a->archive);
1689 free(a);
1690 return (ret);
1691 }
1692
1693 /*
1694 * Simple O(n log n) merge sort to order the fixup list. In
1695 * particular, we want to restore dir timestamps depth-first.
1696 */
1697 static struct fixup_entry *
sort_dir_list(struct fixup_entry * p)1698 sort_dir_list(struct fixup_entry *p)
1699 {
1700 struct fixup_entry *a, *b, *t;
1701
1702 if (p == NULL)
1703 return (NULL);
1704 /* A one-item list is already sorted. */
1705 if (p->next == NULL)
1706 return (p);
1707
1708 /* Step 1: split the list. */
1709 t = p;
1710 a = p->next->next;
1711 while (a != NULL) {
1712 /* Step a twice, t once. */
1713 a = a->next;
1714 if (a != NULL)
1715 a = a->next;
1716 t = t->next;
1717 }
1718 /* Now, t is at the mid-point, so break the list here. */
1719 b = t->next;
1720 t->next = NULL;
1721 a = p;
1722
1723 /* Step 2: Recursively sort the two sub-lists. */
1724 a = sort_dir_list(a);
1725 b = sort_dir_list(b);
1726
1727 /* Step 3: Merge the returned lists. */
1728 /* Pick the first element for the merged list. */
1729 if (wcscmp(a->name, b->name) > 0) {
1730 t = p = a;
1731 a = a->next;
1732 } else {
1733 t = p = b;
1734 b = b->next;
1735 }
1736
1737 /* Always put the later element on the list first. */
1738 while (a != NULL && b != NULL) {
1739 if (wcscmp(a->name, b->name) > 0) {
1740 t->next = a;
1741 a = a->next;
1742 } else {
1743 t->next = b;
1744 b = b->next;
1745 }
1746 t = t->next;
1747 }
1748
1749 /* Only one list is non-empty, so just splice it on. */
1750 if (a != NULL)
1751 t->next = a;
1752 if (b != NULL)
1753 t->next = b;
1754
1755 return (p);
1756 }
1757
1758 /*
1759 * Returns a new, initialized fixup entry.
1760 *
1761 * TODO: Reduce the memory requirements for this list by using a tree
1762 * structure rather than a simple list of names.
1763 */
1764 static struct fixup_entry *
new_fixup(struct archive_write_disk * a,const wchar_t * pathname)1765 new_fixup(struct archive_write_disk *a, const wchar_t *pathname)
1766 {
1767 struct fixup_entry *fe;
1768
1769 fe = (struct fixup_entry *)calloc(1, sizeof(struct fixup_entry));
1770 if (fe == NULL)
1771 return (NULL);
1772 fe->next = a->fixup_list;
1773 a->fixup_list = fe;
1774 fe->fixup = 0;
1775 fe->name = _wcsdup(pathname);
1776 return (fe);
1777 }
1778
1779 /*
1780 * Returns a fixup structure for the current entry.
1781 */
1782 static struct fixup_entry *
current_fixup(struct archive_write_disk * a,const wchar_t * pathname)1783 current_fixup(struct archive_write_disk *a, const wchar_t *pathname)
1784 {
1785 if (a->current_fixup == NULL)
1786 a->current_fixup = new_fixup(a, pathname);
1787 return (a->current_fixup);
1788 }
1789
1790 /* TODO: Make this work. */
1791 /*
1792 * TODO: The deep-directory support bypasses this; disable deep directory
1793 * support if we're doing symlink checks.
1794 */
1795 /*
1796 * TODO: Someday, integrate this with the deep dir support; they both
1797 * scan the path and both can be optimized by comparing against other
1798 * recent paths.
1799 */
1800 /* TODO: Extend this to support symlinks on Windows Vista and later. */
1801 static int
check_symlinks(struct archive_write_disk * a)1802 check_symlinks(struct archive_write_disk *a)
1803 {
1804 wchar_t *pn, *p;
1805 wchar_t c;
1806 int r;
1807 BY_HANDLE_FILE_INFORMATION st;
1808 mode_t st_mode;
1809
1810 /*
1811 * Guard against symlink tricks. Reject any archive entry whose
1812 * destination would be altered by a symlink.
1813 */
1814 /* Whatever we checked last time doesn't need to be re-checked. */
1815 pn = a->name;
1816 p = a->path_safe.s;
1817 while ((*pn != '\0') && (*p == *pn))
1818 ++p, ++pn;
1819 c = pn[0];
1820 /* Keep going until we've checked the entire name. */
1821 while (pn[0] != '\0' && (pn[0] != '\\' || pn[1] != '\0')) {
1822 /* Skip the next path element. */
1823 while (*pn != '\0' && *pn != '\\')
1824 ++pn;
1825 c = pn[0];
1826 pn[0] = '\0';
1827 /* Check that we haven't hit a symlink. */
1828 r = file_information(a, a->name, &st, &st_mode, 1);
1829 if (r != 0) {
1830 /* We've hit a dir that doesn't exist; stop now. */
1831 if (errno == ENOENT)
1832 break;
1833 } else if (S_ISLNK(st_mode)) {
1834 if (c == '\0') {
1835 /*
1836 * Last element is symlink; remove it
1837 * so we can overwrite it with the
1838 * item being extracted.
1839 */
1840 if (disk_unlink(a->name)) {
1841 archive_set_error(&a->archive, errno,
1842 "Could not remove symlink %ls",
1843 a->name);
1844 pn[0] = c;
1845 return (ARCHIVE_FAILED);
1846 }
1847 a->pst = NULL;
1848 /*
1849 * Even if we did remove it, a warning
1850 * is in order. The warning is silly,
1851 * though, if we're just replacing one
1852 * symlink with another symlink.
1853 */
1854 if (!S_ISLNK(a->mode)) {
1855 archive_set_error(&a->archive, 0,
1856 "Removing symlink %ls",
1857 a->name);
1858 }
1859 /* Symlink gone. No more problem! */
1860 pn[0] = c;
1861 return (0);
1862 } else if (a->flags & ARCHIVE_EXTRACT_UNLINK) {
1863 /* User asked us to remove problems. */
1864 if (disk_unlink(a->name) != 0) {
1865 archive_set_error(&a->archive, 0,
1866 "Cannot remove intervening "
1867 "symlink %ls", a->name);
1868 pn[0] = c;
1869 return (ARCHIVE_FAILED);
1870 }
1871 a->pst = NULL;
1872 } else {
1873 archive_set_error(&a->archive, 0,
1874 "Cannot extract through symlink %ls",
1875 a->name);
1876 pn[0] = c;
1877 return (ARCHIVE_FAILED);
1878 }
1879 }
1880 }
1881 pn[0] = c;
1882 /* We've checked and/or cleaned the whole path, so remember it. */
1883 archive_wstrcpy(&a->path_safe, a->name);
1884 return (ARCHIVE_OK);
1885 }
1886
1887 static int
guidword(wchar_t * p,int n)1888 guidword(wchar_t *p, int n)
1889 {
1890 int i;
1891
1892 for (i = 0; i < n; i++) {
1893 if ((*p >= L'0' && *p <= L'9') ||
1894 (*p >= L'a' && *p <= L'f') ||
1895 (*p >= L'A' && *p <= L'F'))
1896 p++;
1897 else
1898 return (-1);
1899 }
1900 return (0);
1901 }
1902
1903 /*
1904 * Canonicalize the pathname. In particular, this strips duplicate
1905 * '\' characters, '.' elements, and trailing '\'. It also raises an
1906 * error for an empty path, a trailing '..' or (if _SECURE_NODOTDOT is
1907 * set) any '..' in the path.
1908 */
1909 static int
cleanup_pathname(struct archive_write_disk * a)1910 cleanup_pathname(struct archive_write_disk *a)
1911 {
1912 wchar_t *dest, *src, *p, *top;
1913 wchar_t separator = L'\0';
1914
1915 p = a->name;
1916 if (*p == L'\0') {
1917 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1918 "Invalid empty pathname");
1919 return (ARCHIVE_FAILED);
1920 }
1921
1922 /* Replace '/' by '\' */
1923 for (; *p != L'\0'; p++) {
1924 if (*p == L'/')
1925 *p = L'\\';
1926 }
1927 p = a->name;
1928
1929 /* Skip leading "\\.\" or "\\?\" or "\\?\UNC\" or
1930 * "\\?\Volume{GUID}\"
1931 * (absolute path prefixes used by Windows API) */
1932 if (p[0] == L'\\' && p[1] == L'\\' &&
1933 (p[2] == L'.' || p[2] == L'?') && p[3] == L'\\')
1934 {
1935 /* A path begin with "\\?\UNC\" */
1936 if (p[2] == L'?' &&
1937 (p[4] == L'U' || p[4] == L'u') &&
1938 (p[5] == L'N' || p[5] == L'n') &&
1939 (p[6] == L'C' || p[6] == L'c') &&
1940 p[7] == L'\\')
1941 p += 8;
1942 /* A path begin with "\\?\Volume{GUID}\" */
1943 else if (p[2] == L'?' &&
1944 (p[4] == L'V' || p[4] == L'v') &&
1945 (p[5] == L'O' || p[5] == L'o') &&
1946 (p[6] == L'L' || p[6] == L'l') &&
1947 (p[7] == L'U' || p[7] == L'u') &&
1948 (p[8] == L'M' || p[8] == L'm') &&
1949 (p[9] == L'E' || p[9] == L'e') &&
1950 p[10] == L'{') {
1951 if (guidword(p+11, 8) == 0 && p[19] == L'-' &&
1952 guidword(p+20, 4) == 0 && p[24] == L'-' &&
1953 guidword(p+25, 4) == 0 && p[29] == L'-' &&
1954 guidword(p+30, 4) == 0 && p[34] == L'-' &&
1955 guidword(p+35, 12) == 0 && p[47] == L'}' &&
1956 p[48] == L'\\')
1957 p += 49;
1958 else
1959 p += 4;
1960 /* A path begin with "\\.\PhysicalDriveX" */
1961 } else if (p[2] == L'.' &&
1962 (p[4] == L'P' || p[4] == L'p') &&
1963 (p[5] == L'H' || p[5] == L'h') &&
1964 (p[6] == L'Y' || p[6] == L'y') &&
1965 (p[7] == L'S' || p[7] == L's') &&
1966 (p[8] == L'I' || p[8] == L'i') &&
1967 (p[9] == L'C' || p[9] == L'c') &&
1968 (p[9] == L'A' || p[9] == L'a') &&
1969 (p[9] == L'L' || p[9] == L'l') &&
1970 (p[9] == L'D' || p[9] == L'd') &&
1971 (p[9] == L'R' || p[9] == L'r') &&
1972 (p[9] == L'I' || p[9] == L'i') &&
1973 (p[9] == L'V' || p[9] == L'v') &&
1974 (p[9] == L'E' || p[9] == L'e') &&
1975 (p[10] >= L'0' && p[10] <= L'9') &&
1976 p[11] == L'\0') {
1977 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1978 "Path is a physical drive name");
1979 return (ARCHIVE_FAILED);
1980 } else
1981 p += 4;
1982 }
1983
1984 /* Skip leading drive letter from archives created
1985 * on Windows. */
1986 if (((p[0] >= L'a' && p[0] <= L'z') ||
1987 (p[0] >= L'A' && p[0] <= L'Z')) &&
1988 p[1] == L':') {
1989 if (p[2] == L'\0') {
1990 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1991 "Path is a drive name");
1992 return (ARCHIVE_FAILED);
1993 }
1994 if (p[2] == L'\\')
1995 p += 2;
1996 }
1997
1998 top = dest = src = p;
1999 /* Rewrite the path name if its character is a unusable. */
2000 for (; *p != L'\0'; p++) {
2001 if (*p == L':' || *p == L'*' || *p == L'?' || *p == L'"' ||
2002 *p == L'<' || *p == L'>' || *p == L'|')
2003 *p = L'_';
2004 }
2005 /* Skip leading '\'. */
2006 if (*src == L'\\')
2007 separator = *src++;
2008
2009 /* Scan the pathname one element at a time. */
2010 for (;;) {
2011 /* src points to first char after '\' */
2012 if (src[0] == L'\0') {
2013 break;
2014 } else if (src[0] == L'\\') {
2015 /* Found '\\'('//'), ignore second one. */
2016 src++;
2017 continue;
2018 } else if (src[0] == L'.') {
2019 if (src[1] == L'\0') {
2020 /* Ignore trailing '.' */
2021 break;
2022 } else if (src[1] == L'\\') {
2023 /* Skip '.\'. */
2024 src += 2;
2025 continue;
2026 } else if (src[1] == L'.') {
2027 if (src[2] == L'\\' || src[2] == L'\0') {
2028 /* Conditionally warn about '..' */
2029 if (a->flags &
2030 ARCHIVE_EXTRACT_SECURE_NODOTDOT) {
2031 archive_set_error(&a->archive,
2032 ARCHIVE_ERRNO_MISC,
2033 "Path contains '..'");
2034 return (ARCHIVE_FAILED);
2035 }
2036 }
2037 /*
2038 * Note: Under no circumstances do we
2039 * remove '..' elements. In
2040 * particular, restoring
2041 * '\foo\..\bar\' should create the
2042 * 'foo' dir as a side-effect.
2043 */
2044 }
2045 }
2046
2047 /* Copy current element, including leading '\'. */
2048 if (separator)
2049 *dest++ = L'\\';
2050 while (*src != L'\0' && *src != L'\\') {
2051 *dest++ = *src++;
2052 }
2053
2054 if (*src == L'\0')
2055 break;
2056
2057 /* Skip '\' separator. */
2058 separator = *src++;
2059 }
2060 /*
2061 * We've just copied zero or more path elements, not including the
2062 * final '\'.
2063 */
2064 if (dest == top) {
2065 /*
2066 * Nothing got copied. The path must have been something
2067 * like '.' or '\' or './' or '/././././/./'.
2068 */
2069 if (separator)
2070 *dest++ = L'\\';
2071 else
2072 *dest++ = L'.';
2073 }
2074 /* Terminate the result. */
2075 *dest = L'\0';
2076 return (ARCHIVE_OK);
2077 }
2078
2079 /*
2080 * Create the parent directory of the specified path, assuming path
2081 * is already in mutable storage.
2082 */
2083 static int
create_parent_dir(struct archive_write_disk * a,wchar_t * path)2084 create_parent_dir(struct archive_write_disk *a, wchar_t *path)
2085 {
2086 wchar_t *slash;
2087 int r;
2088
2089 /* Remove tail element to obtain parent name. */
2090 slash = wcsrchr(path, L'\\');
2091 if (slash == NULL)
2092 return (ARCHIVE_OK);
2093 *slash = L'\0';
2094 r = create_dir(a, path);
2095 *slash = L'\\';
2096 return (r);
2097 }
2098
2099 /*
2100 * Create the specified dir, recursing to create parents as necessary.
2101 *
2102 * Returns ARCHIVE_OK if the path exists when we're done here.
2103 * Otherwise, returns ARCHIVE_FAILED.
2104 * Assumes path is in mutable storage; path is unchanged on exit.
2105 */
2106 static int
create_dir(struct archive_write_disk * a,wchar_t * path)2107 create_dir(struct archive_write_disk *a, wchar_t *path)
2108 {
2109 BY_HANDLE_FILE_INFORMATION st;
2110 struct fixup_entry *le;
2111 wchar_t *slash, *base, *full;
2112 mode_t mode_final, mode, st_mode;
2113 int r;
2114
2115 /* Check for special names and just skip them. */
2116 slash = wcsrchr(path, L'\\');
2117 if (slash == NULL)
2118 base = path;
2119 else
2120 base = slash + 1;
2121
2122 if (base[0] == L'\0' ||
2123 (base[0] == L'.' && base[1] == L'\0') ||
2124 (base[0] == L'.' && base[1] == L'.' && base[2] == L'\0')) {
2125 /* Don't bother trying to create null path, '.', or '..'. */
2126 if (slash != NULL) {
2127 *slash = L'\0';
2128 r = create_dir(a, path);
2129 *slash = L'\\';
2130 return (r);
2131 }
2132 return (ARCHIVE_OK);
2133 }
2134
2135 /*
2136 * Yes, this should be stat() and not lstat(). Using lstat()
2137 * here loses the ability to extract through symlinks. Also note
2138 * that this should not use the a->st cache.
2139 */
2140 if (file_information(a, path, &st, &st_mode, 0) == 0) {
2141 if (S_ISDIR(st_mode))
2142 return (ARCHIVE_OK);
2143 if ((a->flags & ARCHIVE_EXTRACT_NO_OVERWRITE)) {
2144 archive_set_error(&a->archive, EEXIST,
2145 "Can't create directory '%ls'", path);
2146 return (ARCHIVE_FAILED);
2147 }
2148 if (disk_unlink(path) != 0) {
2149 archive_set_error(&a->archive, errno,
2150 "Can't create directory '%ls': "
2151 "Conflicting file cannot be removed",
2152 path);
2153 return (ARCHIVE_FAILED);
2154 }
2155 } else if (errno != ENOENT && errno != ENOTDIR) {
2156 /* Stat failed? */
2157 archive_set_error(&a->archive, errno,
2158 "Can't test directory '%ls'", path);
2159 return (ARCHIVE_FAILED);
2160 } else if (slash != NULL) {
2161 *slash = '\0';
2162 r = create_dir(a, path);
2163 *slash = '\\';
2164 if (r != ARCHIVE_OK)
2165 return (r);
2166 }
2167
2168 /*
2169 * Mode we want for the final restored directory. Per POSIX,
2170 * implicitly-created dirs must be created obeying the umask.
2171 * There's no mention whether this is different for privileged
2172 * restores (which the rest of this code handles by pretending
2173 * umask=0). I've chosen here to always obey the user's umask for
2174 * implicit dirs, even if _EXTRACT_PERM was specified.
2175 */
2176 mode_final = DEFAULT_DIR_MODE & ~a->user_umask;
2177 /* Mode we want on disk during the restore process. */
2178 mode = mode_final;
2179 mode |= MINIMUM_DIR_MODE;
2180 mode &= MAXIMUM_DIR_MODE;
2181 /*
2182 * Apply __la_win_permissive_name_w to path in order to
2183 * remove '../' path string.
2184 */
2185 full = __la_win_permissive_name_w(path);
2186 if (full == NULL)
2187 errno = EINVAL;
2188 else if (CreateDirectoryW(full, NULL) != 0) {
2189 if (mode != mode_final) {
2190 le = new_fixup(a, path);
2191 le->fixup |=TODO_MODE_BASE;
2192 le->mode = mode_final;
2193 }
2194 free(full);
2195 return (ARCHIVE_OK);
2196 } else {
2197 la_dosmaperr(GetLastError());
2198 }
2199 free(full);
2200
2201 /*
2202 * Without the following check, a/b/../b/c/d fails at the
2203 * second visit to 'b', so 'd' can't be created. Note that we
2204 * don't add it to the fixup list here, as it's already been
2205 * added.
2206 */
2207 if (file_information(a, path, &st, &st_mode, 0) == 0 &&
2208 S_ISDIR(st_mode))
2209 return (ARCHIVE_OK);
2210
2211 archive_set_error(&a->archive, errno, "Failed to create dir '%ls'",
2212 path);
2213 return (ARCHIVE_FAILED);
2214 }
2215
2216 /*
2217 * Note: Although we can skip setting the user id if the desired user
2218 * id matches the current user, we cannot skip setting the group, as
2219 * many systems set the gid based on the containing directory. So
2220 * we have to perform a chown syscall if we want to set the SGID
2221 * bit. (The alternative is to stat() and then possibly chown(); it's
2222 * more efficient to skip the stat() and just always chown().) Note
2223 * that a successful chown() here clears the TODO_SGID_CHECK bit, which
2224 * allows set_mode to skip the stat() check for the GID.
2225 */
2226 static int
set_ownership(struct archive_write_disk * a)2227 set_ownership(struct archive_write_disk *a)
2228 {
2229 /* unfortunately, on win32 there is no 'root' user with uid 0,
2230 so we just have to try the chown and see if it works */
2231
2232 /* If we know we can't change it, don't bother trying. */
2233 if (a->user_uid != 0 && a->user_uid != a->uid) {
2234 archive_set_error(&a->archive, errno,
2235 "Can't set UID=%jd", (intmax_t)a->uid);
2236 return (ARCHIVE_WARN);
2237 }
2238
2239 archive_set_error(&a->archive, errno,
2240 "Can't set user=%jd/group=%jd for %ls",
2241 (intmax_t)a->uid, (intmax_t)a->gid, a->name);
2242 return (ARCHIVE_WARN);
2243 }
2244
2245 static int
set_times(struct archive_write_disk * a,HANDLE h,int mode,const wchar_t * name,time_t atime,long atime_nanos,time_t birthtime,long birthtime_nanos,time_t mtime,long mtime_nanos,time_t ctime_sec,long ctime_nanos)2246 set_times(struct archive_write_disk *a,
2247 HANDLE h, int mode, const wchar_t *name,
2248 time_t atime, long atime_nanos,
2249 time_t birthtime, long birthtime_nanos,
2250 time_t mtime, long mtime_nanos,
2251 time_t ctime_sec, long ctime_nanos)
2252 {
2253 #define EPOC_TIME ARCHIVE_LITERAL_ULL(116444736000000000)
2254 #define WINTIME(sec, nsec) ((Int32x32To64(sec, 10000000) + EPOC_TIME)\
2255 + (((nsec)/1000)*10))
2256
2257 HANDLE hw = 0;
2258 ULARGE_INTEGER wintm;
2259 FILETIME *pfbtime;
2260 FILETIME fatime, fbtime, fmtime;
2261
2262 (void)ctime_sec; /* UNUSED */
2263 (void)ctime_nanos; /* UNUSED */
2264
2265 if (h != INVALID_HANDLE_VALUE) {
2266 hw = NULL;
2267 } else {
2268 wchar_t *ws;
2269
2270 if (S_ISLNK(mode))
2271 return (ARCHIVE_OK);
2272 ws = __la_win_permissive_name_w(name);
2273 if (ws == NULL)
2274 goto settimes_failed;
2275 hw = CreateFileW(ws, FILE_WRITE_ATTRIBUTES,
2276 0, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
2277 free(ws);
2278 if (hw == INVALID_HANDLE_VALUE)
2279 goto settimes_failed;
2280 h = hw;
2281 }
2282
2283 wintm.QuadPart = WINTIME(atime, atime_nanos);
2284 fatime.dwLowDateTime = wintm.LowPart;
2285 fatime.dwHighDateTime = wintm.HighPart;
2286 wintm.QuadPart = WINTIME(mtime, mtime_nanos);
2287 fmtime.dwLowDateTime = wintm.LowPart;
2288 fmtime.dwHighDateTime = wintm.HighPart;
2289 /*
2290 * SetFileTime() supports birthtime.
2291 */
2292 if (birthtime > 0 || birthtime_nanos > 0) {
2293 wintm.QuadPart = WINTIME(birthtime, birthtime_nanos);
2294 fbtime.dwLowDateTime = wintm.LowPart;
2295 fbtime.dwHighDateTime = wintm.HighPart;
2296 pfbtime = &fbtime;
2297 } else
2298 pfbtime = NULL;
2299 if (SetFileTime(h, pfbtime, &fatime, &fmtime) == 0)
2300 goto settimes_failed;
2301 CloseHandle(hw);
2302 return (ARCHIVE_OK);
2303
2304 settimes_failed:
2305 CloseHandle(hw);
2306 archive_set_error(&a->archive, EINVAL, "Can't restore time");
2307 return (ARCHIVE_WARN);
2308 }
2309
2310 static int
set_times_from_entry(struct archive_write_disk * a)2311 set_times_from_entry(struct archive_write_disk *a)
2312 {
2313 time_t atime, birthtime, mtime, ctime_sec;
2314 long atime_nsec, birthtime_nsec, mtime_nsec, ctime_nsec;
2315
2316 /* Suitable defaults. */
2317 atime = birthtime = mtime = ctime_sec = a->start_time;
2318 atime_nsec = birthtime_nsec = mtime_nsec = ctime_nsec = 0;
2319
2320 /* If no time was provided, we're done. */
2321 if (!archive_entry_atime_is_set(a->entry)
2322 && !archive_entry_birthtime_is_set(a->entry)
2323 && !archive_entry_mtime_is_set(a->entry))
2324 return (ARCHIVE_OK);
2325
2326 if (archive_entry_atime_is_set(a->entry)) {
2327 atime = archive_entry_atime(a->entry);
2328 atime_nsec = archive_entry_atime_nsec(a->entry);
2329 }
2330 if (archive_entry_birthtime_is_set(a->entry)) {
2331 birthtime = archive_entry_birthtime(a->entry);
2332 birthtime_nsec = archive_entry_birthtime_nsec(a->entry);
2333 }
2334 if (archive_entry_mtime_is_set(a->entry)) {
2335 mtime = archive_entry_mtime(a->entry);
2336 mtime_nsec = archive_entry_mtime_nsec(a->entry);
2337 }
2338 if (archive_entry_ctime_is_set(a->entry)) {
2339 ctime_sec = archive_entry_ctime(a->entry);
2340 ctime_nsec = archive_entry_ctime_nsec(a->entry);
2341 }
2342
2343 return set_times(a, a->fh, a->mode, a->name,
2344 atime, atime_nsec,
2345 birthtime, birthtime_nsec,
2346 mtime, mtime_nsec,
2347 ctime_sec, ctime_nsec);
2348 }
2349
2350 static int
set_mode(struct archive_write_disk * a,int mode)2351 set_mode(struct archive_write_disk *a, int mode)
2352 {
2353 int r = ARCHIVE_OK;
2354 mode &= 07777; /* Strip off file type bits. */
2355
2356 if (a->todo & TODO_SGID_CHECK) {
2357 /*
2358 * If we don't know the GID is right, we must stat()
2359 * to verify it. We can't just check the GID of this
2360 * process, since systems sometimes set GID from
2361 * the enclosing dir or based on ACLs.
2362 */
2363 if ((r = lazy_stat(a)) != ARCHIVE_OK)
2364 return (r);
2365 if (0 != a->gid) {
2366 mode &= ~ S_ISGID;
2367 }
2368 /* While we're here, double-check the UID. */
2369 if (0 != a->uid
2370 && (a->todo & TODO_SUID)) {
2371 mode &= ~ S_ISUID;
2372 }
2373 a->todo &= ~TODO_SGID_CHECK;
2374 a->todo &= ~TODO_SUID_CHECK;
2375 } else if (a->todo & TODO_SUID_CHECK) {
2376 /*
2377 * If we don't know the UID is right, we can just check
2378 * the user, since all systems set the file UID from
2379 * the process UID.
2380 */
2381 if (a->user_uid != a->uid) {
2382 mode &= ~ S_ISUID;
2383 }
2384 a->todo &= ~TODO_SUID_CHECK;
2385 }
2386
2387 if (S_ISLNK(a->mode)) {
2388 #ifdef HAVE_LCHMOD
2389 /*
2390 * If this is a symlink, use lchmod(). If the
2391 * platform doesn't support lchmod(), just skip it. A
2392 * platform that doesn't provide a way to set
2393 * permissions on symlinks probably ignores
2394 * permissions on symlinks, so a failure here has no
2395 * impact.
2396 */
2397 if (lchmod(a->name, mode) != 0) {
2398 archive_set_error(&a->archive, errno,
2399 "Can't set permissions to 0%o", (int)mode);
2400 r = ARCHIVE_WARN;
2401 }
2402 #endif
2403 } else if (!S_ISDIR(a->mode)) {
2404 /*
2405 * If it's not a symlink and not a dir, then use
2406 * fchmod() or chmod(), depending on whether we have
2407 * an fd. Dirs get their perms set during the
2408 * post-extract fixup, which is handled elsewhere.
2409 */
2410 #ifdef HAVE_FCHMOD
2411 if (a->fd >= 0) {
2412 if (fchmod(a->fd, mode) != 0) {
2413 archive_set_error(&a->archive, errno,
2414 "Can't set permissions to 0%o", (int)mode);
2415 r = ARCHIVE_WARN;
2416 }
2417 } else
2418 #endif
2419 /* If this platform lacks fchmod(), then
2420 * we'll just use chmod(). */
2421 if (la_chmod(a->name, mode) != 0) {
2422 archive_set_error(&a->archive, errno,
2423 "Can't set permissions to 0%o", (int)mode);
2424 r = ARCHIVE_WARN;
2425 }
2426 }
2427 return (r);
2428 }
2429
2430 static int
set_fflags(struct archive_write_disk * a)2431 set_fflags(struct archive_write_disk *a)
2432 {
2433 (void)a; /* UNUSED */
2434 return (ARCHIVE_OK);
2435 }
2436
2437 /* Default empty function body to satisfy mainline code. */
2438 static int
set_acls(struct archive_write_disk * a,HANDLE h,const wchar_t * name,struct archive_acl * acl)2439 set_acls(struct archive_write_disk *a, HANDLE h, const wchar_t *name,
2440 struct archive_acl *acl)
2441 {
2442 (void)a; /* UNUSED */
2443 (void)h; /* UNUSED */
2444 (void)name; /* UNUSED */
2445 (void)acl; /* UNUSED */
2446 return (ARCHIVE_OK);
2447 }
2448
2449 /*
2450 * Restore extended attributes - stub implementation for unsupported systems
2451 */
2452 static int
set_xattrs(struct archive_write_disk * a)2453 set_xattrs(struct archive_write_disk *a)
2454 {
2455 static int warning_done = 0;
2456
2457 /* If there aren't any extended attributes, then it's okay not
2458 * to extract them, otherwise, issue a single warning. */
2459 if (archive_entry_xattr_count(a->entry) != 0 && !warning_done) {
2460 warning_done = 1;
2461 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2462 "Cannot restore extended attributes on this system");
2463 return (ARCHIVE_WARN);
2464 }
2465 /* Warning was already emitted; suppress further warnings. */
2466 return (ARCHIVE_OK);
2467 }
2468
2469 static void
fileTimeToUtc(const FILETIME * filetime,time_t * t,long * ns)2470 fileTimeToUtc(const FILETIME *filetime, time_t *t, long *ns)
2471 {
2472 ULARGE_INTEGER utc;
2473
2474 utc.HighPart = filetime->dwHighDateTime;
2475 utc.LowPart = filetime->dwLowDateTime;
2476 if (utc.QuadPart >= EPOC_TIME) {
2477 utc.QuadPart -= EPOC_TIME;
2478 /* milli seconds base */
2479 *t = (time_t)(utc.QuadPart / 10000000);
2480 /* nano seconds base */
2481 *ns = (long)(utc.QuadPart % 10000000) * 100;
2482 } else {
2483 *t = 0;
2484 *ns = 0;
2485 }
2486 }
2487 /*
2488 * Test if file on disk is older than entry.
2489 */
2490 static int
older(BY_HANDLE_FILE_INFORMATION * st,struct archive_entry * entry)2491 older(BY_HANDLE_FILE_INFORMATION *st, struct archive_entry *entry)
2492 {
2493 time_t sec;
2494 long nsec;
2495
2496 fileTimeToUtc(&st->ftLastWriteTime, &sec, &nsec);
2497 /* First, test the seconds and return if we have a definite answer. */
2498 /* Definitely older. */
2499 if (sec < archive_entry_mtime(entry))
2500 return (1);
2501 /* Definitely younger. */
2502 if (sec > archive_entry_mtime(entry))
2503 return (0);
2504 if (nsec < archive_entry_mtime_nsec(entry))
2505 return (1);
2506 /* Same age or newer, so not older. */
2507 return (0);
2508 }
2509
2510 #endif /* _WIN32 && !__CYGWIN__ */
2511
2512