| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549 |
- /*-
- * Copyright (c) 2003-2009 Tim Kientzle
- * Copyright (c) 2010-2012 Michihiro NAKAJIMA
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer
- * in this position and unchanged.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
- #include "archive_platform.h"
- #if defined(_WIN32) && !defined(__CYGWIN__)
- #ifdef HAVE_ERRNO_H
- #include <errno.h>
- #endif
- #ifdef HAVE_STDLIB_H
- #include <stdlib.h>
- #endif
- #include <winioctl.h>
- #include "archive.h"
- #include "archive_string.h"
- #include "archive_entry.h"
- #include "archive_private.h"
- #include "archive_read_disk_private.h"
- #include "archive_time_private.h"
- #ifndef O_BINARY
- #define O_BINARY 0
- #endif
- #ifndef IO_REPARSE_TAG_SYMLINK
- /* Old SDKs do not provide IO_REPARSE_TAG_SYMLINK */
- #define IO_REPARSE_TAG_SYMLINK 0xA000000CL
- #endif
- /* To deal with absolute symlink issues */
- #define START_ABSOLUTE_SYMLINK_REPARSE L"\\??\\"
- /*-
- * This is a new directory-walking system that addresses a number
- * of problems I've had with fts(3). In particular, it has no
- * pathname-length limits (other than the size of 'int'), handles
- * deep logical traversals, uses considerably less memory, and has
- * an opaque interface (easier to modify in the future).
- *
- * Internally, it keeps a single list of "tree_entry" items that
- * represent filesystem objects that require further attention.
- * Non-directories are not kept in memory: they are pulled from
- * readdir(), returned to the client, then freed as soon as possible.
- * Any directory entry to be traversed gets pushed onto the stack.
- *
- * There is surprisingly little information that needs to be kept for
- * each item on the stack. Just the name, depth (represented here as the
- * string length of the parent directory's pathname), and some markers
- * indicating how to get back to the parent (via chdir("..") for a
- * regular dir or via fchdir(2) for a symlink).
- */
- struct restore_time {
- const wchar_t *full_path;
- FILETIME lastWriteTime;
- FILETIME lastAccessTime;
- mode_t filetype;
- };
- struct tree_entry {
- int depth;
- struct tree_entry *next;
- struct tree_entry *parent;
- size_t full_path_dir_length;
- struct archive_wstring name;
- struct archive_wstring full_path;
- size_t dirname_length;
- int64_t dev;
- int64_t ino;
- int flags;
- int filesystem_id;
- /* How to restore time of a directory. */
- struct restore_time restore_time;
- };
- struct filesystem {
- int64_t dev;
- int synthetic;
- int remote;
- DWORD bytesPerSector;
- };
- /* Definitions for tree_entry.flags bitmap. */
- #define isDir 1 /* This entry is a regular directory. */
- #define isDirLink 2 /* This entry is a symbolic link to a directory. */
- #define needsFirstVisit 4 /* This is an initial entry. */
- #define needsDescent 8 /* This entry needs to be previsited. */
- #define needsOpen 16 /* This is a directory that needs to be opened. */
- #define needsAscent 32 /* This entry needs to be postvisited. */
- /*
- * On Windows, "first visit" is handled as a pattern to be handed to
- * _findfirst(). This is consistent with Windows conventions that
- * file patterns are handled within the application. On Posix,
- * "first visit" is just returned to the client.
- */
- #define MAX_OVERLAPPED 8
- #define READ_BUFFER_SIZE (1024 * 64) /* Default to 64KB per https://technet.microsoft.com/en-us/library/cc938632.aspx */
- #define DIRECT_IO 0/* Disabled */
- #define ASYNC_IO 1/* Enabled */
- /*
- * Local data for this package.
- */
- struct tree {
- struct tree_entry *stack;
- struct tree_entry *current;
- HANDLE d;
- WIN32_FIND_DATAW _findData;
- WIN32_FIND_DATAW *findData;
- int flags;
- int visit_type;
- /* Error code from last failed operation. */
- int tree_errno;
- /* A full path with "\\?\" prefix. */
- struct archive_wstring full_path;
- size_t full_path_dir_length;
- /* Dynamically-sized buffer for holding path */
- struct archive_wstring path;
- /* Last path element */
- const wchar_t *basename;
- /* Leading dir length */
- size_t dirname_length;
- int depth;
- BY_HANDLE_FILE_INFORMATION lst;
- BY_HANDLE_FILE_INFORMATION st;
- int descend;
- /* How to restore time of a file. */
- struct restore_time restore_time;
- struct entry_sparse {
- int64_t length;
- int64_t offset;
- } *sparse_list, *current_sparse;
- int sparse_count;
- int sparse_list_size;
- char initial_symlink_mode;
- char symlink_mode;
- struct filesystem *current_filesystem;
- struct filesystem *filesystem_table;
- int initial_filesystem_id;
- int current_filesystem_id;
- int max_filesystem_id;
- int allocated_filesystem;
- HANDLE entry_fh;
- int entry_eof;
- int64_t entry_remaining_bytes;
- int64_t entry_total;
- int ol_idx_doing;
- int ol_idx_done;
- int ol_num_doing;
- int ol_num_done;
- int64_t ol_remaining_bytes;
- int64_t ol_total;
- struct la_overlapped {
- OVERLAPPED ol;
- struct archive * _a;
- unsigned char *buff;
- size_t buff_size;
- int64_t offset;
- size_t bytes_expected;
- size_t bytes_transferred;
- } ol[MAX_OVERLAPPED];
- int direct_io;
- int async_io;
- };
- #define bhfi_dev(bhfi) ((bhfi)->dwVolumeSerialNumber)
- /* Treat FileIndex as i-node. We should remove a sequence number
- * which is high-16-bits of nFileIndexHigh. */
- #define bhfi_ino(bhfi) \
- ((((int64_t)((bhfi)->nFileIndexHigh & 0x0000FFFFUL)) << 32) \
- + (bhfi)->nFileIndexLow)
- /* Definitions for tree.flags bitmap. */
- #define hasStat 16 /* The st entry is valid. */
- #define hasLstat 32 /* The lst entry is valid. */
- #define needsRestoreTimes 128
- static int
- tree_dir_next_windows(struct tree *t, const wchar_t *pattern);
- /* Initiate/terminate a tree traversal. */
- static struct tree *tree_open(const wchar_t *, int, int);
- static struct tree *tree_reopen(struct tree *, const wchar_t *, int);
- static void tree_close(struct tree *);
- static void tree_free(struct tree *);
- static void tree_push(struct tree *, const wchar_t *, const wchar_t *,
- int, int64_t, int64_t, struct restore_time *);
- /*
- * tree_next() returns Zero if there is no next entry, non-zero if
- * there is. Note that directories are visited three times.
- * Directories are always visited first as part of enumerating their
- * parent; that is a "regular" visit. If tree_descend() is invoked at
- * that time, the directory is added to a work list and will
- * subsequently be visited two more times: once just after descending
- * into the directory ("postdescent") and again just after ascending
- * back to the parent ("postascent").
- *
- * TREE_ERROR_DIR is returned if the descent failed (because the
- * directory couldn't be opened, for instance). This is returned
- * instead of TREE_POSTDESCENT/TREE_POSTASCENT. TREE_ERROR_DIR is not a
- * fatal error, but it does imply that the relevant subtree won't be
- * visited. TREE_ERROR_FATAL is returned for an error that left the
- * traversal completely hosed. Right now, this is only returned for
- * chdir() failures during ascent.
- */
- #define TREE_REGULAR 1
- #define TREE_POSTDESCENT 2
- #define TREE_POSTASCENT 3
- #define TREE_ERROR_DIR -1
- #define TREE_ERROR_FATAL -2
- static int tree_next(struct tree *);
- /*
- * Return information about the current entry.
- */
- /*
- * The current full pathname, length of the full pathname, and a name
- * that can be used to access the file. Because tree does use chdir
- * extensively, the access path is almost never the same as the full
- * current path.
- *
- */
- static const wchar_t *tree_current_path(struct tree *);
- static const wchar_t *tree_current_access_path(struct tree *);
- /*
- * Request the lstat() or stat() data for the current path. Since the
- * tree package needs to do some of this anyway, and caches the
- * results, you should take advantage of it here if you need it rather
- * than make a redundant stat() or lstat() call of your own.
- */
- static const BY_HANDLE_FILE_INFORMATION *tree_current_stat(struct tree *);
- static const BY_HANDLE_FILE_INFORMATION *tree_current_lstat(struct tree *);
- /* The following functions use tricks to avoid a certain number of
- * stat()/lstat() calls. */
- /* "is_physical_dir" is equivalent to S_ISDIR(tree_current_lstat()->st_mode) */
- static int tree_current_is_physical_dir(struct tree *);
- /* "is_physical_link" is equivalent to S_ISLNK(tree_current_lstat()->st_mode) */
- static int tree_current_is_physical_link(struct tree *);
- /* Instead of archive_entry_copy_stat for BY_HANDLE_FILE_INFORMATION */
- static void tree_archive_entry_copy_bhfi(struct archive_entry *,
- struct tree *, const BY_HANDLE_FILE_INFORMATION *);
- /* "is_dir" is equivalent to S_ISDIR(tree_current_stat()->st_mode) */
- static int tree_current_is_dir(struct tree *);
- static int update_current_filesystem(struct archive_read_disk *a,
- int64_t dev);
- static int setup_current_filesystem(struct archive_read_disk *);
- static int tree_target_is_same_as_parent(struct tree *,
- const BY_HANDLE_FILE_INFORMATION *);
- static int _archive_read_disk_open_w(struct archive *, const wchar_t *);
- static int _archive_read_free(struct archive *);
- static int _archive_read_close(struct archive *);
- static int _archive_read_data_block(struct archive *,
- const void **, size_t *, int64_t *);
- static int _archive_read_next_header(struct archive *,
- struct archive_entry **);
- static int _archive_read_next_header2(struct archive *,
- struct archive_entry *);
- static const char *trivial_lookup_gname(void *, int64_t gid);
- static const char *trivial_lookup_uname(void *, int64_t uid);
- static int setup_sparse(struct archive_read_disk *, struct archive_entry *);
- static int close_and_restore_time(HANDLE, struct tree *,
- struct restore_time *);
- static int setup_sparse_from_disk(struct archive_read_disk *,
- struct archive_entry *, HANDLE);
- static int la_linkname_from_handle(HANDLE, wchar_t **, int *);
- static int la_linkname_from_pathw(const wchar_t *, wchar_t **, int *);
- static void entry_symlink_from_pathw(struct archive_entry *,
- const wchar_t *path);
- typedef struct _REPARSE_DATA_BUFFER {
- ULONG ReparseTag;
- USHORT ReparseDataLength;
- USHORT Reserved;
- union {
- struct {
- USHORT SubstituteNameOffset;
- USHORT SubstituteNameLength;
- USHORT PrintNameOffset;
- USHORT PrintNameLength;
- ULONG Flags;
- WCHAR PathBuffer[1];
- } SymbolicLinkReparseBuffer;
- struct {
- USHORT SubstituteNameOffset;
- USHORT SubstituteNameLength;
- USHORT PrintNameOffset;
- USHORT PrintNameLength;
- WCHAR PathBuffer[1];
- } MountPointReparseBuffer;
- struct {
- UCHAR DataBuffer[1];
- } GenericReparseBuffer;
- } DUMMYUNIONNAME;
- } REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;
- /*
- * Reads the target of a symbolic link
- *
- * Returns 0 on success and -1 on failure
- * outbuf is allocated in the function
- */
- static int
- la_linkname_from_handle(HANDLE h, wchar_t **linkname, int *linktype)
- {
- DWORD inbytes;
- REPARSE_DATA_BUFFER *buf;
- BY_HANDLE_FILE_INFORMATION st;
- size_t len;
- BOOL ret;
- BYTE *indata;
- wchar_t *tbuf;
- ret = GetFileInformationByHandle(h, &st);
- if (ret == 0 ||
- (st.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) == 0) {
- return (-1);
- }
- indata = malloc(MAXIMUM_REPARSE_DATA_BUFFER_SIZE);
- ret = DeviceIoControl(h, FSCTL_GET_REPARSE_POINT, NULL, 0, indata,
- 1024, &inbytes, NULL);
- if (ret == 0) {
- la_dosmaperr(GetLastError());
- free(indata);
- return (-1);
- }
- buf = (REPARSE_DATA_BUFFER *) indata;
- if (buf->ReparseTag != IO_REPARSE_TAG_SYMLINK) {
- free(indata);
- /* File is not a symbolic link */
- errno = EINVAL;
- return (-1);
- }
- len = buf->SymbolicLinkReparseBuffer.SubstituteNameLength;
- if (len <= 0) {
- free(indata);
- return (-1);
- }
- tbuf = malloc(len + sizeof(wchar_t));
- if (tbuf == NULL) {
- free(indata);
- return (-1);
- }
- memcpy(tbuf, &((BYTE *)buf->SymbolicLinkReparseBuffer.PathBuffer)
- [buf->SymbolicLinkReparseBuffer.SubstituteNameOffset], len);
- free(indata);
- tbuf[len / sizeof(wchar_t)] = L'\0';
- if (wcsncmp(tbuf, START_ABSOLUTE_SYMLINK_REPARSE, 4) == 0) {
- /* Absolute symlink, so we'll change the NT path into a verbatim one */
- tbuf[1] = L'\\';
- } else {
- /* Relative symlink, so we can translate backslashes to slashes */
- wchar_t *temp = tbuf;
- do {
- if (*temp == L'\\')
- *temp = L'/';
- temp++;
- } while(*temp != L'\0');
- }
- *linkname = tbuf;
- if ((st.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0)
- *linktype = AE_SYMLINK_TYPE_FILE;
- else
- *linktype = AE_SYMLINK_TYPE_DIRECTORY;
- return (0);
- }
- /*
- * Returns AE_SYMLINK_TYPE_FILE, AE_SYMLINK_TYPE_DIRECTORY or -1 on error
- */
- static int
- la_linkname_from_pathw(const wchar_t *path, wchar_t **outbuf, int *linktype)
- {
- HANDLE h;
- const DWORD flag = FILE_FLAG_BACKUP_SEMANTICS |
- FILE_FLAG_OPEN_REPARSE_POINT;
- int ret;
- # if _WIN32_WINNT >= 0x0602 /* _WIN32_WINNT_WIN8 */
- CREATEFILE2_EXTENDED_PARAMETERS createExParams;
- ZeroMemory(&createExParams, sizeof(createExParams));
- createExParams.dwSize = sizeof(createExParams);
- createExParams.dwFileFlags = flag;
- h = CreateFile2(path, 0,
- FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
- OPEN_EXISTING, &createExParams);
- #else
- h = CreateFileW(path, 0,
- FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
- OPEN_EXISTING, flag, NULL);
- #endif
- if (h == INVALID_HANDLE_VALUE) {
- la_dosmaperr(GetLastError());
- return (-1);
- }
- ret = la_linkname_from_handle(h, outbuf, linktype);
- CloseHandle(h);
- return (ret);
- }
- static void
- entry_symlink_from_pathw(struct archive_entry *entry, const wchar_t *path)
- {
- wchar_t *linkname = NULL;
- int ret, linktype;
- ret = la_linkname_from_pathw(path, &linkname, &linktype);
- if (ret != 0)
- return;
- if (linktype >= 0) {
- archive_entry_copy_symlink_w(entry, linkname);
- archive_entry_set_symlink_type(entry, linktype);
- }
- free(linkname);
- return;
- }
- static const struct archive_vtable
- archive_read_disk_vtable = {
- .archive_free = _archive_read_free,
- .archive_close = _archive_read_close,
- .archive_read_data_block = _archive_read_data_block,
- .archive_read_next_header = _archive_read_next_header,
- .archive_read_next_header2 = _archive_read_next_header2,
- };
- const char *
- archive_read_disk_gname(struct archive *_a, la_int64_t gid)
- {
- struct archive_read_disk *a = (struct archive_read_disk *)_a;
- if (ARCHIVE_OK != __archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
- ARCHIVE_STATE_ANY, "archive_read_disk_gname"))
- return (NULL);
- if (a->lookup_gname == NULL)
- return (NULL);
- return ((*a->lookup_gname)(a->lookup_gname_data, gid));
- }
- const char *
- archive_read_disk_uname(struct archive *_a, la_int64_t uid)
- {
- struct archive_read_disk *a = (struct archive_read_disk *)_a;
- if (ARCHIVE_OK != __archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
- ARCHIVE_STATE_ANY, "archive_read_disk_uname"))
- return (NULL);
- if (a->lookup_uname == NULL)
- return (NULL);
- return ((*a->lookup_uname)(a->lookup_uname_data, uid));
- }
- int
- archive_read_disk_set_gname_lookup(struct archive *_a,
- void *private_data,
- const char * (*lookup_gname)(void *private, la_int64_t gid),
- void (*cleanup_gname)(void *private))
- {
- struct archive_read_disk *a = (struct archive_read_disk *)_a;
- archive_check_magic(&a->archive, ARCHIVE_READ_DISK_MAGIC,
- ARCHIVE_STATE_ANY, "archive_read_disk_set_gname_lookup");
- if (a->cleanup_gname != NULL && a->lookup_gname_data != NULL)
- (a->cleanup_gname)(a->lookup_gname_data);
- a->lookup_gname = lookup_gname;
- a->cleanup_gname = cleanup_gname;
- a->lookup_gname_data = private_data;
- return (ARCHIVE_OK);
- }
- int
- archive_read_disk_set_uname_lookup(struct archive *_a,
- void *private_data,
- const char * (*lookup_uname)(void *private, int64_t uid),
- void (*cleanup_uname)(void *private))
- {
- struct archive_read_disk *a = (struct archive_read_disk *)_a;
- archive_check_magic(&a->archive, ARCHIVE_READ_DISK_MAGIC,
- ARCHIVE_STATE_ANY, "archive_read_disk_set_uname_lookup");
- if (a->cleanup_uname != NULL && a->lookup_uname_data != NULL)
- (a->cleanup_uname)(a->lookup_uname_data);
- a->lookup_uname = lookup_uname;
- a->cleanup_uname = cleanup_uname;
- a->lookup_uname_data = private_data;
- return (ARCHIVE_OK);
- }
- /*
- * Create a new archive_read_disk object and initialize it with global state.
- */
- struct archive *
- archive_read_disk_new(void)
- {
- struct archive_read_disk *a;
- a = calloc(1, sizeof(*a));
- if (a == NULL)
- return (NULL);
- a->archive.magic = ARCHIVE_READ_DISK_MAGIC;
- a->archive.state = ARCHIVE_STATE_NEW;
- a->archive.vtable = &archive_read_disk_vtable;
- a->entry = archive_entry_new2(&a->archive);
- a->lookup_uname = trivial_lookup_uname;
- a->lookup_gname = trivial_lookup_gname;
- a->flags = ARCHIVE_READDISK_MAC_COPYFILE;
- return (&a->archive);
- }
- static int
- _archive_read_free(struct archive *_a)
- {
- struct archive_read_disk *a = (struct archive_read_disk *)_a;
- int r;
- if (_a == NULL)
- return (ARCHIVE_OK);
- archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
- ARCHIVE_STATE_ANY | ARCHIVE_STATE_FATAL, "archive_read_free");
- if (a->archive.state != ARCHIVE_STATE_CLOSED)
- r = _archive_read_close(&a->archive);
- else
- r = ARCHIVE_OK;
- tree_free(a->tree);
- if (a->cleanup_gname != NULL && a->lookup_gname_data != NULL)
- (a->cleanup_gname)(a->lookup_gname_data);
- if (a->cleanup_uname != NULL && a->lookup_uname_data != NULL)
- (a->cleanup_uname)(a->lookup_uname_data);
- archive_string_free(&a->archive.error_string);
- archive_entry_free(a->entry);
- a->archive.magic = 0;
- free(a);
- return (r);
- }
- static int
- _archive_read_close(struct archive *_a)
- {
- struct archive_read_disk *a = (struct archive_read_disk *)_a;
- archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
- ARCHIVE_STATE_ANY | ARCHIVE_STATE_FATAL, "archive_read_close");
- if (a->archive.state != ARCHIVE_STATE_FATAL)
- a->archive.state = ARCHIVE_STATE_CLOSED;
- tree_close(a->tree);
- return (ARCHIVE_OK);
- }
- static void
- setup_symlink_mode(struct archive_read_disk *a, char symlink_mode,
- char follow_symlinks)
- {
- a->symlink_mode = symlink_mode;
- a->follow_symlinks = follow_symlinks;
- if (a->tree != NULL) {
- a->tree->initial_symlink_mode = a->symlink_mode;
- a->tree->symlink_mode = a->symlink_mode;
- }
- }
- int
- archive_read_disk_set_symlink_logical(struct archive *_a)
- {
- struct archive_read_disk *a = (struct archive_read_disk *)_a;
- archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
- ARCHIVE_STATE_ANY, "archive_read_disk_set_symlink_logical");
- setup_symlink_mode(a, 'L', 1);
- return (ARCHIVE_OK);
- }
- int
- archive_read_disk_set_symlink_physical(struct archive *_a)
- {
- struct archive_read_disk *a = (struct archive_read_disk *)_a;
- archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
- ARCHIVE_STATE_ANY, "archive_read_disk_set_symlink_physical");
- setup_symlink_mode(a, 'P', 0);
- return (ARCHIVE_OK);
- }
- int
- archive_read_disk_set_symlink_hybrid(struct archive *_a)
- {
- struct archive_read_disk *a = (struct archive_read_disk *)_a;
- archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
- ARCHIVE_STATE_ANY, "archive_read_disk_set_symlink_hybrid");
- setup_symlink_mode(a, 'H', 1);/* Follow symlinks initially. */
- return (ARCHIVE_OK);
- }
- int
- archive_read_disk_set_atime_restored(struct archive *_a)
- {
- struct archive_read_disk *a = (struct archive_read_disk *)_a;
- archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
- ARCHIVE_STATE_ANY, "archive_read_disk_restore_atime");
- a->flags |= ARCHIVE_READDISK_RESTORE_ATIME;
- if (a->tree != NULL)
- a->tree->flags |= needsRestoreTimes;
- return (ARCHIVE_OK);
- }
- int
- archive_read_disk_set_behavior(struct archive *_a, int flags)
- {
- struct archive_read_disk *a = (struct archive_read_disk *)_a;
- int r = ARCHIVE_OK;
- archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
- ARCHIVE_STATE_ANY, "archive_read_disk_honor_nodump");
- a->flags = flags;
- if (flags & ARCHIVE_READDISK_RESTORE_ATIME)
- r = archive_read_disk_set_atime_restored(_a);
- else {
- if (a->tree != NULL)
- a->tree->flags &= ~needsRestoreTimes;
- }
- return (r);
- }
- /*
- * Trivial implementations of gname/uname lookup functions.
- * These are normally overridden by the client, but these stub
- * versions ensure that we always have something that works.
- */
- static const char *
- trivial_lookup_gname(void *private_data, int64_t gid)
- {
- (void)private_data; /* UNUSED */
- (void)gid; /* UNUSED */
- return (NULL);
- }
- static const char *
- trivial_lookup_uname(void *private_data, int64_t uid)
- {
- (void)private_data; /* UNUSED */
- (void)uid; /* UNUSED */
- return (NULL);
- }
- static int64_t
- align_num_per_sector(struct tree *t, int64_t size)
- {
- int64_t surplus;
- size += t->current_filesystem->bytesPerSector -1;
- surplus = size % t->current_filesystem->bytesPerSector;
- size -= surplus;
- return (size);
- }
- static int
- start_next_async_read(struct archive_read_disk *a, struct tree *t)
- {
- struct la_overlapped *olp;
- DWORD buffbytes, rbytes;
- if (t->ol_remaining_bytes == 0)
- return (ARCHIVE_EOF);
- olp = &(t->ol[t->ol_idx_doing]);
- t->ol_idx_doing = (t->ol_idx_doing + 1) % MAX_OVERLAPPED;
- /* Allocate read buffer. */
- if (olp->buff == NULL) {
- void *p;
- size_t s = (size_t)align_num_per_sector(t, READ_BUFFER_SIZE);
- p = VirtualAlloc(NULL, s, MEM_COMMIT, PAGE_READWRITE);
- if (p == NULL) {
- archive_set_error(&a->archive, ENOMEM,
- "Couldn't allocate memory");
- a->archive.state = ARCHIVE_STATE_FATAL;
- return (ARCHIVE_FATAL);
- }
- olp->buff = p;
- olp->buff_size = s;
- olp->_a = &a->archive;
- olp->ol.hEvent = CreateEventW(NULL, TRUE, FALSE, NULL);
- if (olp->ol.hEvent == NULL) {
- la_dosmaperr(GetLastError());
- archive_set_error(&a->archive, errno,
- "CreateEvent failed");
- a->archive.state = ARCHIVE_STATE_FATAL;
- return (ARCHIVE_FATAL);
- }
- } else
- ResetEvent(olp->ol.hEvent);
- buffbytes = (DWORD)olp->buff_size;
- if (buffbytes > t->current_sparse->length)
- buffbytes = (DWORD)t->current_sparse->length;
- /* Skip hole. */
- if (t->current_sparse->offset > t->ol_total) {
- t->ol_remaining_bytes -=
- t->current_sparse->offset - t->ol_total;
- }
- olp->offset = t->current_sparse->offset;
- olp->ol.Offset = (DWORD)(olp->offset & 0xffffffff);
- olp->ol.OffsetHigh = (DWORD)(olp->offset >> 32);
- if (t->ol_remaining_bytes > buffbytes) {
- olp->bytes_expected = buffbytes;
- t->ol_remaining_bytes -= buffbytes;
- } else {
- olp->bytes_expected = (size_t)t->ol_remaining_bytes;
- t->ol_remaining_bytes = 0;
- }
- olp->bytes_transferred = 0;
- t->current_sparse->offset += buffbytes;
- t->current_sparse->length -= buffbytes;
- t->ol_total = t->current_sparse->offset;
- if (t->current_sparse->length == 0 && t->ol_remaining_bytes > 0)
- t->current_sparse++;
- if (!ReadFile(t->entry_fh, olp->buff, buffbytes, &rbytes, &(olp->ol))) {
- DWORD lasterr;
- lasterr = GetLastError();
- if (lasterr == ERROR_HANDLE_EOF) {
- archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
- "Reading file truncated");
- a->archive.state = ARCHIVE_STATE_FATAL;
- return (ARCHIVE_FATAL);
- } else if (lasterr != ERROR_IO_PENDING) {
- if (lasterr == ERROR_NO_DATA)
- errno = EAGAIN;
- else if (lasterr == ERROR_ACCESS_DENIED)
- errno = EBADF;
- else
- la_dosmaperr(lasterr);
- archive_set_error(&a->archive, errno, "Read error");
- a->archive.state = ARCHIVE_STATE_FATAL;
- return (ARCHIVE_FATAL);
- }
- } else
- olp->bytes_transferred = rbytes;
- t->ol_num_doing++;
- return (t->ol_remaining_bytes == 0)? ARCHIVE_EOF: ARCHIVE_OK;
- }
- static void
- cancel_async(struct tree *t)
- {
- if (t->ol_num_doing != t->ol_num_done) {
- CancelIo(t->entry_fh);
- t->ol_num_doing = t->ol_num_done = 0;
- }
- }
- static int
- _archive_read_data_block(struct archive *_a, const void **buff,
- size_t *size, int64_t *offset)
- {
- struct archive_read_disk *a = (struct archive_read_disk *)_a;
- struct tree *t = a->tree;
- struct la_overlapped *olp;
- DWORD bytes_transferred;
- int r = ARCHIVE_FATAL;
- archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC, ARCHIVE_STATE_DATA,
- "archive_read_data_block");
- if (t->entry_eof || t->entry_remaining_bytes <= 0) {
- r = ARCHIVE_EOF;
- goto abort_read_data;
- }
- /*
- * Make a request to read the file in asynchronous.
- */
- if (t->ol_num_doing == 0) {
- do {
- r = start_next_async_read(a, t);
- if (r == ARCHIVE_FATAL)
- goto abort_read_data;
- if (!t->async_io)
- break;
- } while (r == ARCHIVE_OK && t->ol_num_doing < MAX_OVERLAPPED);
- } else {
- if ((r = start_next_async_read(a, t)) == ARCHIVE_FATAL)
- goto abort_read_data;
- }
- olp = &(t->ol[t->ol_idx_done]);
- t->ol_idx_done = (t->ol_idx_done + 1) % MAX_OVERLAPPED;
- if (olp->bytes_transferred)
- bytes_transferred = (DWORD)olp->bytes_transferred;
- else if (!GetOverlappedResult(t->entry_fh, &(olp->ol),
- &bytes_transferred, TRUE)) {
- la_dosmaperr(GetLastError());
- archive_set_error(&a->archive, errno,
- "GetOverlappedResult failed");
- a->archive.state = ARCHIVE_STATE_FATAL;
- r = ARCHIVE_FATAL;
- goto abort_read_data;
- }
- t->ol_num_done++;
- if (bytes_transferred == 0 ||
- olp->bytes_expected != bytes_transferred) {
- archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
- "Reading file truncated");
- a->archive.state = ARCHIVE_STATE_FATAL;
- r = ARCHIVE_FATAL;
- goto abort_read_data;
- }
- *buff = olp->buff;
- *size = bytes_transferred;
- *offset = olp->offset;
- if (olp->offset > t->entry_total)
- t->entry_remaining_bytes -= olp->offset - t->entry_total;
- t->entry_total = olp->offset + *size;
- t->entry_remaining_bytes -= *size;
- if (t->entry_remaining_bytes == 0) {
- /* Close the current file descriptor */
- close_and_restore_time(t->entry_fh, t, &t->restore_time);
- t->entry_fh = INVALID_HANDLE_VALUE;
- t->entry_eof = 1;
- }
- return (ARCHIVE_OK);
- abort_read_data:
- *buff = NULL;
- *size = 0;
- *offset = t->entry_total;
- if (t->entry_fh != INVALID_HANDLE_VALUE) {
- cancel_async(t);
- /* Close the current file descriptor */
- close_and_restore_time(t->entry_fh, t, &t->restore_time);
- t->entry_fh = INVALID_HANDLE_VALUE;
- }
- return (r);
- }
- static int
- next_entry(struct archive_read_disk *a, struct tree *t,
- struct archive_entry *entry)
- {
- const BY_HANDLE_FILE_INFORMATION *st;
- const BY_HANDLE_FILE_INFORMATION *lst;
- const char*name;
- int descend, r;
- st = NULL;
- lst = NULL;
- t->descend = 0;
- do {
- switch (tree_next(t)) {
- case TREE_ERROR_FATAL:
- archive_set_error(&a->archive, t->tree_errno,
- "%ls: Unable to continue traversing directory tree",
- tree_current_path(t));
- a->archive.state = ARCHIVE_STATE_FATAL;
- return (ARCHIVE_FATAL);
- case TREE_ERROR_DIR:
- archive_set_error(&a->archive, t->tree_errno,
- "%ls: Couldn't visit directory",
- tree_current_path(t));
- return (ARCHIVE_FAILED);
- case 0:
- return (ARCHIVE_EOF);
- case TREE_POSTDESCENT:
- case TREE_POSTASCENT:
- break;
- case TREE_REGULAR:
- lst = tree_current_lstat(t);
- if (lst == NULL) {
- archive_set_error(&a->archive, t->tree_errno,
- "%ls: Cannot stat",
- tree_current_path(t));
- return (ARCHIVE_FAILED);
- }
- break;
- }
- } while (lst == NULL);
- archive_entry_copy_pathname_w(entry, tree_current_path(t));
- /*
- * Perform path matching.
- */
- if (a->matching) {
- r = archive_match_path_excluded(a->matching, entry);
- if (r < 0) {
- archive_set_error(&(a->archive), errno,
- "Failed : %s", archive_error_string(a->matching));
- return (r);
- }
- if (r) {
- if (a->excluded_cb_func)
- a->excluded_cb_func(&(a->archive),
- a->excluded_cb_data, entry);
- return (ARCHIVE_RETRY);
- }
- }
- /*
- * Distinguish 'L'/'P'/'H' symlink following.
- */
- switch(t->symlink_mode) {
- case 'H':
- /* 'H': After the first item, rest like 'P'. */
- t->symlink_mode = 'P';
- /* 'H': First item (from command line) like 'L'. */
- /* FALLTHROUGH */
- case 'L':
- /* 'L': Do descend through a symlink to dir. */
- descend = tree_current_is_dir(t);
- /* 'L': Follow symlinks to files. */
- a->symlink_mode = 'L';
- a->follow_symlinks = 1;
- /* 'L': Archive symlinks as targets, if we can. */
- st = tree_current_stat(t);
- if (st != NULL && !tree_target_is_same_as_parent(t, st))
- break;
- /* If stat fails, we have a broken symlink;
- * in that case, don't follow the link. */
- /* FALLTHROUGH */
- default:
- /* 'P': Don't descend through a symlink to dir. */
- descend = tree_current_is_physical_dir(t);
- /* 'P': Don't follow symlinks to files. */
- a->symlink_mode = 'P';
- a->follow_symlinks = 0;
- /* 'P': Archive symlinks as symlinks. */
- st = lst;
- break;
- }
- if (update_current_filesystem(a, bhfi_dev(st)) != ARCHIVE_OK) {
- a->archive.state = ARCHIVE_STATE_FATAL;
- return (ARCHIVE_FATAL);
- }
- if (t->initial_filesystem_id == -1)
- t->initial_filesystem_id = t->current_filesystem_id;
- if (a->flags & ARCHIVE_READDISK_NO_TRAVERSE_MOUNTS) {
- if (t->initial_filesystem_id != t->current_filesystem_id)
- return (ARCHIVE_RETRY);
- }
- t->descend = descend;
- tree_archive_entry_copy_bhfi(entry, t, st);
- /* Save the times to be restored. This must be in before
- * calling archive_read_disk_descend() or any chance of it,
- * especially, invoking a callback. */
- t->restore_time.lastWriteTime = st->ftLastWriteTime;
- t->restore_time.lastAccessTime = st->ftLastAccessTime;
- t->restore_time.filetype = archive_entry_filetype(entry);
- /*
- * Perform time matching.
- */
- if (a->matching) {
- r = archive_match_time_excluded(a->matching, entry);
- if (r < 0) {
- archive_set_error(&(a->archive), errno,
- "Failed : %s", archive_error_string(a->matching));
- return (r);
- }
- if (r) {
- if (a->excluded_cb_func)
- a->excluded_cb_func(&(a->archive),
- a->excluded_cb_data, entry);
- return (ARCHIVE_RETRY);
- }
- }
- /* Lookup uname/gname */
- name = archive_read_disk_uname(&(a->archive), archive_entry_uid(entry));
- if (name != NULL)
- archive_entry_copy_uname(entry, name);
- name = archive_read_disk_gname(&(a->archive), archive_entry_gid(entry));
- if (name != NULL)
- archive_entry_copy_gname(entry, name);
- /*
- * Perform owner matching.
- */
- if (a->matching) {
- r = archive_match_owner_excluded(a->matching, entry);
- if (r < 0) {
- archive_set_error(&(a->archive), errno,
- "Failed : %s", archive_error_string(a->matching));
- return (r);
- }
- if (r) {
- if (a->excluded_cb_func)
- a->excluded_cb_func(&(a->archive),
- a->excluded_cb_data, entry);
- return (ARCHIVE_RETRY);
- }
- }
- /*
- * File attributes
- */
- if ((a->flags & ARCHIVE_READDISK_NO_FFLAGS) == 0) {
- const int supported_attrs =
- FILE_ATTRIBUTE_READONLY |
- FILE_ATTRIBUTE_HIDDEN |
- FILE_ATTRIBUTE_SYSTEM;
- DWORD file_attrs = st->dwFileAttributes & supported_attrs;
- if (file_attrs != 0)
- archive_entry_set_fflags(entry, file_attrs, 0);
- }
- /*
- * Invoke a meta data filter callback.
- */
- if (a->metadata_filter_func) {
- if (!a->metadata_filter_func(&(a->archive),
- a->metadata_filter_data, entry))
- return (ARCHIVE_RETRY);
- }
- archive_entry_copy_sourcepath_w(entry, tree_current_access_path(t));
- r = ARCHIVE_OK;
- if (archive_entry_filetype(entry) == AE_IFREG &&
- archive_entry_size(entry) > 0) {
- DWORD flags = FILE_FLAG_BACKUP_SEMANTICS;
- #if _WIN32_WINNT >= 0x0602 /* _WIN32_WINNT_WIN8 */
- CREATEFILE2_EXTENDED_PARAMETERS createExParams;
- #endif
- if (t->async_io)
- flags |= FILE_FLAG_OVERLAPPED;
- if (t->direct_io)
- flags |= FILE_FLAG_NO_BUFFERING;
- else
- flags |= FILE_FLAG_SEQUENTIAL_SCAN;
- #if _WIN32_WINNT >= 0x0602 /* _WIN32_WINNT_WIN8 */
- ZeroMemory(&createExParams, sizeof(createExParams));
- createExParams.dwSize = sizeof(createExParams);
- createExParams.dwFileFlags = flags;
- t->entry_fh = CreateFile2(tree_current_access_path(t),
- GENERIC_READ,
- FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
- OPEN_EXISTING, &createExParams);
- #else
- t->entry_fh = CreateFileW(tree_current_access_path(t),
- GENERIC_READ,
- FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
- NULL, OPEN_EXISTING, flags, NULL);
- #endif
- if (t->entry_fh == INVALID_HANDLE_VALUE) {
- la_dosmaperr(GetLastError());
- archive_set_error(&a->archive, errno,
- "Couldn't open %ls", tree_current_path(a->tree));
- return (ARCHIVE_FAILED);
- }
- /* Find sparse data from the disk. */
- if ((a->flags & ARCHIVE_READDISK_NO_SPARSE) == 0) {
- if (archive_entry_hardlink(entry) == NULL &&
- (st->dwFileAttributes & FILE_ATTRIBUTE_SPARSE_FILE) != 0)
- r = setup_sparse_from_disk(a, entry, t->entry_fh);
- }
- }
- return (r);
- }
- static int
- _archive_read_next_header(struct archive *_a, struct archive_entry **entryp)
- {
- int ret;
- struct archive_read_disk *a = (struct archive_read_disk *)_a;
- *entryp = NULL;
- ret = _archive_read_next_header2(_a, a->entry);
- *entryp = a->entry;
- return ret;
- }
- static int
- _archive_read_next_header2(struct archive *_a, struct archive_entry *entry)
- {
- struct archive_read_disk *a = (struct archive_read_disk *)_a;
- struct tree *t;
- int r;
- archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
- ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
- "archive_read_next_header2");
- t = a->tree;
- if (t->entry_fh != INVALID_HANDLE_VALUE) {
- cancel_async(t);
- close_and_restore_time(t->entry_fh, t, &t->restore_time);
- t->entry_fh = INVALID_HANDLE_VALUE;
- }
- archive_entry_clear(entry);
- while ((r = next_entry(a, t, entry)) == ARCHIVE_RETRY)
- archive_entry_clear(entry);
- /*
- * EOF and FATAL are persistent at this layer. By
- * modifying the state, we guarantee that future calls to
- * read a header or read data will fail.
- */
- switch (r) {
- case ARCHIVE_EOF:
- a->archive.state = ARCHIVE_STATE_EOF;
- break;
- case ARCHIVE_OK:
- case ARCHIVE_WARN:
- t->entry_total = 0;
- if (archive_entry_filetype(entry) == AE_IFREG) {
- t->entry_remaining_bytes = archive_entry_size(entry);
- t->entry_eof = (t->entry_remaining_bytes == 0)? 1: 0;
- if (!t->entry_eof &&
- setup_sparse(a, entry) != ARCHIVE_OK)
- return (ARCHIVE_FATAL);
- } else {
- t->entry_remaining_bytes = 0;
- t->entry_eof = 1;
- }
- t->ol_idx_doing = t->ol_idx_done = 0;
- t->ol_num_doing = t->ol_num_done = 0;
- t->ol_remaining_bytes = t->entry_remaining_bytes;
- t->ol_total = 0;
- a->archive.state = ARCHIVE_STATE_DATA;
- break;
- case ARCHIVE_RETRY:
- break;
- case ARCHIVE_FATAL:
- a->archive.state = ARCHIVE_STATE_FATAL;
- break;
- }
- __archive_reset_read_data(&a->archive);
- return (r);
- }
- static int
- setup_sparse(struct archive_read_disk *a, struct archive_entry *entry)
- {
- struct tree *t = a->tree;
- int64_t aligned, length, offset;
- int i;
- t->sparse_count = archive_entry_sparse_reset(entry);
- if (t->sparse_count+1 > t->sparse_list_size) {
- free(t->sparse_list);
- t->sparse_list_size = t->sparse_count + 1;
- t->sparse_list = malloc(sizeof(t->sparse_list[0]) *
- t->sparse_list_size);
- if (t->sparse_list == NULL) {
- t->sparse_list_size = 0;
- archive_set_error(&a->archive, ENOMEM,
- "Can't allocate data");
- a->archive.state = ARCHIVE_STATE_FATAL;
- return (ARCHIVE_FATAL);
- }
- }
- /*
- * Get sparse list and make sure those offsets and lengths are
- * aligned by a sector size.
- */
- for (i = 0; i < t->sparse_count; i++) {
- archive_entry_sparse_next(entry, &offset, &length);
- aligned = align_num_per_sector(t, offset);
- if (aligned != offset) {
- aligned -= t->current_filesystem->bytesPerSector;
- length += offset - aligned;
- }
- t->sparse_list[i].offset = aligned;
- aligned = align_num_per_sector(t, length);
- t->sparse_list[i].length = aligned;
- }
- aligned = align_num_per_sector(t, archive_entry_size(entry));
- if (i == 0) {
- t->sparse_list[i].offset = 0;
- t->sparse_list[i].length = aligned;
- } else {
- int j, last = i;
- t->sparse_list[i].offset = aligned;
- t->sparse_list[i].length = 0;
- for (i = 0; i < last; i++) {
- if ((t->sparse_list[i].offset +
- t->sparse_list[i].length) <=
- t->sparse_list[i+1].offset)
- continue;
- /*
- * Now sparse_list[i+1] is overlapped by sparse_list[i].
- * Merge those two.
- */
- length = t->sparse_list[i+1].offset -
- t->sparse_list[i].offset;
- t->sparse_list[i+1].offset = t->sparse_list[i].offset;
- t->sparse_list[i+1].length += length;
- /* Remove sparse_list[i]. */
- for (j = i; j < last; j++) {
- t->sparse_list[j].offset =
- t->sparse_list[j+1].offset;
- t->sparse_list[j].length =
- t->sparse_list[j+1].length;
- }
- last--;
- }
- }
- t->current_sparse = t->sparse_list;
- return (ARCHIVE_OK);
- }
- int
- archive_read_disk_set_matching(struct archive *_a, struct archive *_ma,
- void (*_excluded_func)(struct archive *, void *, struct archive_entry *),
- void *_client_data)
- {
- struct archive_read_disk *a = (struct archive_read_disk *)_a;
- archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
- ARCHIVE_STATE_ANY, "archive_read_disk_set_matching");
- a->matching = _ma;
- a->excluded_cb_func = _excluded_func;
- a->excluded_cb_data = _client_data;
- return (ARCHIVE_OK);
- }
- int
- archive_read_disk_set_metadata_filter_callback(struct archive *_a,
- int (*_metadata_filter_func)(struct archive *, void *,
- struct archive_entry *), void *_client_data)
- {
- struct archive_read_disk *a = (struct archive_read_disk *)_a;
- archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC, ARCHIVE_STATE_ANY,
- "archive_read_disk_set_metadata_filter_callback");
- a->metadata_filter_func = _metadata_filter_func;
- a->metadata_filter_data = _client_data;
- return (ARCHIVE_OK);
- }
- int
- archive_read_disk_can_descend(struct archive *_a)
- {
- struct archive_read_disk *a = (struct archive_read_disk *)_a;
- struct tree *t = a->tree;
- archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
- ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
- "archive_read_disk_can_descend");
- return (t->visit_type == TREE_REGULAR && t->descend);
- }
- /*
- * Called by the client to mark the directory just returned from
- * tree_next() as needing to be visited.
- */
- int
- archive_read_disk_descend(struct archive *_a)
- {
- struct archive_read_disk *a = (struct archive_read_disk *)_a;
- struct tree *t = a->tree;
- archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
- ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
- "archive_read_disk_descend");
- if (!archive_read_disk_can_descend(_a))
- return (ARCHIVE_OK);
- if (tree_current_is_physical_dir(t)) {
- tree_push(t, t->basename, t->full_path.s,
- t->current_filesystem_id,
- bhfi_dev(&(t->lst)), bhfi_ino(&(t->lst)),
- &t->restore_time);
- t->stack->flags |= isDir;
- } else if (tree_current_is_dir(t)) {
- tree_push(t, t->basename, t->full_path.s,
- t->current_filesystem_id,
- bhfi_dev(&(t->st)), bhfi_ino(&(t->st)),
- &t->restore_time);
- t->stack->flags |= isDirLink;
- }
- t->descend = 0;
- return (ARCHIVE_OK);
- }
- int
- archive_read_disk_open(struct archive *_a, const char *pathname)
- {
- struct archive_read_disk *a = (struct archive_read_disk *)_a;
- struct archive_wstring wpath;
- int ret;
- archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
- ARCHIVE_STATE_NEW | ARCHIVE_STATE_CLOSED,
- "archive_read_disk_open");
- archive_clear_error(&a->archive);
- /* Make a wchar_t string from a char string. */
- archive_string_init(&wpath);
- if (archive_wstring_append_from_mbs(&wpath, pathname,
- strlen(pathname)) != 0) {
- if (errno == ENOMEM)
- archive_set_error(&a->archive, ENOMEM,
- "Can't allocate memory");
- else
- archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
- "Can't convert a path to a wchar_t string");
- a->archive.state = ARCHIVE_STATE_FATAL;
- ret = ARCHIVE_FATAL;
- } else
- ret = _archive_read_disk_open_w(_a, wpath.s);
- archive_wstring_free(&wpath);
- return (ret);
- }
- int
- archive_read_disk_open_w(struct archive *_a, const wchar_t *pathname)
- {
- struct archive_read_disk *a = (struct archive_read_disk *)_a;
- archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
- ARCHIVE_STATE_NEW | ARCHIVE_STATE_CLOSED,
- "archive_read_disk_open_w");
- archive_clear_error(&a->archive);
- return (_archive_read_disk_open_w(_a, pathname));
- }
- static int
- _archive_read_disk_open_w(struct archive *_a, const wchar_t *pathname)
- {
- struct archive_read_disk *a = (struct archive_read_disk *)_a;
- if (a->tree != NULL)
- a->tree = tree_reopen(a->tree, pathname,
- a->flags & ARCHIVE_READDISK_RESTORE_ATIME);
- else
- a->tree = tree_open(pathname, a->symlink_mode,
- a->flags & ARCHIVE_READDISK_RESTORE_ATIME);
- if (a->tree == NULL) {
- archive_set_error(&a->archive, ENOMEM,
- "Can't allocate directory traversal data");
- a->archive.state = ARCHIVE_STATE_FATAL;
- return (ARCHIVE_FATAL);
- }
- a->archive.state = ARCHIVE_STATE_HEADER;
- return (ARCHIVE_OK);
- }
- /*
- * Return a current filesystem ID which is index of the filesystem entry
- * you've visited through archive_read_disk.
- */
- int
- archive_read_disk_current_filesystem(struct archive *_a)
- {
- struct archive_read_disk *a = (struct archive_read_disk *)_a;
- archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC, ARCHIVE_STATE_DATA,
- "archive_read_disk_current_filesystem");
- return (a->tree->current_filesystem_id);
- }
- static int
- update_current_filesystem(struct archive_read_disk *a, int64_t dev)
- {
- struct tree *t = a->tree;
- int i, fid;
- if (t->current_filesystem != NULL &&
- t->current_filesystem->dev == dev)
- return (ARCHIVE_OK);
- for (i = 0; i < t->max_filesystem_id; i++) {
- if (t->filesystem_table[i].dev == dev) {
- /* There is the filesystem ID we've already generated. */
- t->current_filesystem_id = i;
- t->current_filesystem = &(t->filesystem_table[i]);
- return (ARCHIVE_OK);
- }
- }
- /*
- * There is a new filesystem, we generate a new ID for.
- */
- fid = t->max_filesystem_id++;
- if (t->max_filesystem_id > t->allocated_filesystem) {
- size_t s;
- void *p;
- s = t->max_filesystem_id * 2;
- p = realloc(t->filesystem_table,
- s * sizeof(*t->filesystem_table));
- if (p == NULL) {
- archive_set_error(&a->archive, ENOMEM,
- "Can't allocate tar data");
- return (ARCHIVE_FATAL);
- }
- t->filesystem_table = (struct filesystem *)p;
- t->allocated_filesystem = (int)s;
- }
- t->current_filesystem_id = fid;
- t->current_filesystem = &(t->filesystem_table[fid]);
- t->current_filesystem->dev = dev;
- return (setup_current_filesystem(a));
- }
- /*
- * Returns 1 if current filesystem is generated filesystem, 0 if it is not
- * or -1 if it is unknown.
- */
- int
- archive_read_disk_current_filesystem_is_synthetic(struct archive *_a)
- {
- struct archive_read_disk *a = (struct archive_read_disk *)_a;
- archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC, ARCHIVE_STATE_DATA,
- "archive_read_disk_current_filesystem");
- return (a->tree->current_filesystem->synthetic);
- }
- /*
- * Returns 1 if current filesystem is remote filesystem, 0 if it is not
- * or -1 if it is unknown.
- */
- int
- archive_read_disk_current_filesystem_is_remote(struct archive *_a)
- {
- struct archive_read_disk *a = (struct archive_read_disk *)_a;
- archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC, ARCHIVE_STATE_DATA,
- "archive_read_disk_current_filesystem");
- return (a->tree->current_filesystem->remote);
- }
- /*
- * If symlink is broken, statfs or statvfs will fail.
- * Use its directory path instead.
- */
- static wchar_t *
- safe_path_for_statfs(struct tree *t)
- {
- const wchar_t *path;
- wchar_t *cp, *p = NULL;
- path = tree_current_access_path(t);
- if (tree_current_stat(t) == NULL) {
- p = _wcsdup(path);
- cp = wcsrchr(p, '/');
- if (cp != NULL && wcslen(cp) >= 2) {
- cp[1] = '.';
- cp[2] = '\0';
- path = p;
- }
- } else
- p = _wcsdup(path);
- return (p);
- }
- /*
- * Get conditions of synthetic and remote on Windows
- */
- static int
- setup_current_filesystem(struct archive_read_disk *a)
- {
- struct tree *t = a->tree;
- wchar_t vol[256];
- wchar_t *path;
- t->current_filesystem->synthetic = -1;/* Not supported */
- path = safe_path_for_statfs(t);
- if (!GetVolumePathNameW(path, vol, sizeof(vol)/sizeof(vol[0]))) {
- free(path);
- t->current_filesystem->remote = -1;
- t->current_filesystem->bytesPerSector = 0;
- archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
- "GetVolumePathName failed: %d", (int)GetLastError());
- return (ARCHIVE_FAILED);
- }
- free(path);
- switch (GetDriveTypeW(vol)) {
- case DRIVE_UNKNOWN:
- case DRIVE_NO_ROOT_DIR:
- t->current_filesystem->remote = -1;
- break;
- case DRIVE_REMOTE:
- t->current_filesystem->remote = 1;
- break;
- default:
- t->current_filesystem->remote = 0;
- break;
- }
- if (!GetDiskFreeSpaceW(vol, NULL,
- &(t->current_filesystem->bytesPerSector), NULL, NULL)) {
- t->current_filesystem->bytesPerSector = 0;
- archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
- "GetDiskFreeSpace failed: %d", (int)GetLastError());
- return (ARCHIVE_FAILED);
- }
- return (ARCHIVE_OK);
- }
- static int
- close_and_restore_time(HANDLE h, struct tree *t, struct restore_time *rt)
- {
- HANDLE handle;
- int r = 0;
- #if _WIN32_WINNT >= 0x0602 /* _WIN32_WINNT_WIN8 */
- CREATEFILE2_EXTENDED_PARAMETERS createExParams;
- #endif
- if (h == INVALID_HANDLE_VALUE && AE_IFLNK == rt->filetype)
- return (0);
- /* Close a file descriptor.
- * It will not be used for SetFileTime() because it has been opened
- * by a read only mode.
- */
- if (h != INVALID_HANDLE_VALUE)
- CloseHandle(h);
- if ((t->flags & needsRestoreTimes) == 0)
- return (r);
- #if _WIN32_WINNT >= 0x0602 /* _WIN32_WINNT_WIN8 */
- ZeroMemory(&createExParams, sizeof(createExParams));
- createExParams.dwSize = sizeof(createExParams);
- createExParams.dwFileFlags = FILE_FLAG_BACKUP_SEMANTICS;
- handle = CreateFile2(rt->full_path, FILE_WRITE_ATTRIBUTES,
- 0, OPEN_EXISTING, &createExParams);
- #else
- handle = CreateFileW(rt->full_path, FILE_WRITE_ATTRIBUTES,
- 0, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
- #endif
- if (handle == INVALID_HANDLE_VALUE) {
- errno = EINVAL;
- return (-1);
- }
- if (SetFileTime(handle, NULL, &rt->lastAccessTime,
- &rt->lastWriteTime) == 0) {
- errno = EINVAL;
- r = -1;
- } else
- r = 0;
- CloseHandle(handle);
- return (r);
- }
- /*
- * Add a directory path to the current stack.
- */
- static void
- tree_push(struct tree *t, const wchar_t *path, const wchar_t *full_path,
- int filesystem_id, int64_t dev, int64_t ino, struct restore_time *rt)
- {
- struct tree_entry *te;
- te = calloc(1, sizeof(*te));
- te->next = t->stack;
- te->parent = t->current;
- if (te->parent)
- te->depth = te->parent->depth + 1;
- t->stack = te;
- archive_string_init(&te->name);
- archive_wstrcpy(&te->name, path);
- archive_string_init(&te->full_path);
- archive_wstrcpy(&te->full_path, full_path);
- te->flags = needsDescent | needsOpen | needsAscent;
- te->filesystem_id = filesystem_id;
- te->dev = dev;
- te->ino = ino;
- te->dirname_length = t->dirname_length;
- te->full_path_dir_length = t->full_path_dir_length;
- te->restore_time.full_path = te->full_path.s;
- if (rt != NULL) {
- te->restore_time.lastWriteTime = rt->lastWriteTime;
- te->restore_time.lastAccessTime = rt->lastAccessTime;
- te->restore_time.filetype = rt->filetype;
- }
- }
- /*
- * Append a name to the current dir path.
- */
- static int
- tree_append(struct tree *t, const wchar_t *name, size_t name_length)
- {
- size_t size_needed;
- t->path.s[t->dirname_length] = L'\0';
- t->path.length = t->dirname_length;
- /* Strip trailing '/' from name, unless entire name is "/". */
- while (name_length > 1 && name[name_length - 1] == L'/')
- name_length--;
- /* Resize pathname buffer as needed. */
- size_needed = name_length + t->dirname_length + 2;
- if (archive_wstring_ensure(&t->path, size_needed) == NULL)
- return (TREE_ERROR_FATAL);
- /* Add a separating '/' if it's needed. */
- if (t->dirname_length > 0 &&
- t->path.s[archive_strlen(&t->path)-1] != L'/')
- archive_wstrappend_wchar(&t->path, L'/');
- t->basename = t->path.s + archive_strlen(&t->path);
- archive_wstrncat(&t->path, name, name_length);
- t->restore_time.full_path = t->basename;
- if (t->full_path_dir_length > 0) {
- t->full_path.s[t->full_path_dir_length] = L'\0';
- t->full_path.length = t->full_path_dir_length;
- size_needed = name_length + t->full_path_dir_length + 2;
- if (archive_wstring_ensure(&t->full_path, size_needed) == NULL)
- return (TREE_ERROR_FATAL);
- /* Add a separating '\' if it's needed. */
- if (t->full_path.s[archive_strlen(&t->full_path)-1] != L'\\')
- archive_wstrappend_wchar(&t->full_path, L'\\');
- archive_wstrncat(&t->full_path, name, name_length);
- t->restore_time.full_path = t->full_path.s;
- }
- return (0);
- }
- /*
- * Open a directory tree for traversal.
- */
- static struct tree *
- tree_open(const wchar_t *path, int symlink_mode, int restore_time)
- {
- struct tree *t;
- t = calloc(1, sizeof(*t));
- archive_string_init(&(t->full_path));
- archive_string_init(&t->path);
- if (archive_wstring_ensure(&t->path, 15) == NULL) {
- free(t);
- return (NULL);
- }
- t->initial_symlink_mode = symlink_mode;
- return (tree_reopen(t, path, restore_time));
- }
- static struct tree *
- tree_reopen(struct tree *t, const wchar_t *path, int restore_time)
- {
- struct archive_wstring ws;
- wchar_t *pathname, *p, *base;
- t->flags = (restore_time != 0)?needsRestoreTimes:0;
- t->visit_type = 0;
- t->tree_errno = 0;
- t->full_path_dir_length = 0;
- t->dirname_length = 0;
- t->depth = 0;
- t->descend = 0;
- t->current = NULL;
- t->d = INVALID_HANDLE_VALUE;
- t->symlink_mode = t->initial_symlink_mode;
- archive_string_empty(&(t->full_path));
- archive_string_empty(&t->path);
- t->entry_fh = INVALID_HANDLE_VALUE;
- t->entry_eof = 0;
- t->entry_remaining_bytes = 0;
- t->initial_filesystem_id = -1;
- /* Get wchar_t strings from char strings. */
- archive_string_init(&ws);
- archive_wstrcpy(&ws, path);
- pathname = ws.s;
- /* Get a full-path-name. */
- p = __la_win_permissive_name_w(pathname);
- if (p == NULL)
- goto failed;
- archive_wstrcpy(&(t->full_path), p);
- free(p);
- /* Convert path separators from '\' to '/' */
- for (p = pathname; *p != L'\0'; ++p) {
- if (*p == L'\\')
- *p = L'/';
- }
- base = pathname;
- /* First item is set up a lot like a symlink traversal. */
- /* printf("Looking for wildcard in %s\n", path); */
- if ((base[0] == L'/' && base[1] == L'/' &&
- base[2] == L'?' && base[3] == L'/' &&
- (wcschr(base+4, L'*') || wcschr(base+4, L'?'))) ||
- (!(base[0] == L'/' && base[1] == L'/' &&
- base[2] == L'?' && base[3] == L'/') &&
- (wcschr(base, L'*') || wcschr(base, L'?')))) {
- // It has a wildcard in it...
- // Separate the last element.
- p = wcsrchr(base, L'/');
- if (p != NULL) {
- *p = L'\0';
- if (tree_append(t, base, p - base))
- goto failed;
- t->dirname_length = archive_strlen(&t->path);
- base = p + 1;
- }
- p = wcsrchr(t->full_path.s, L'\\');
- if (p != NULL) {
- *p = L'\0';
- t->full_path.length = wcslen(t->full_path.s);
- t->full_path_dir_length = archive_strlen(&t->full_path);
- }
- }
- tree_push(t, base, t->full_path.s, 0, 0, 0, NULL);
- archive_wstring_free(&ws);
- t->stack->flags = needsFirstVisit;
- /*
- * Debug flag for Direct IO(No buffering) or Async IO.
- * Those dependent on environment variable switches
- * will be removed until next release.
- */
- {
- const char *e;
- if ((e = getenv("LIBARCHIVE_DIRECT_IO")) != NULL) {
- if (e[0] == '0')
- t->direct_io = 0;
- else
- t->direct_io = 1;
- fprintf(stderr, "LIBARCHIVE_DIRECT_IO=%s\n",
- (t->direct_io)?"Enabled":"Disabled");
- } else
- t->direct_io = DIRECT_IO;
- if ((e = getenv("LIBARCHIVE_ASYNC_IO")) != NULL) {
- if (e[0] == '0')
- t->async_io = 0;
- else
- t->async_io = 1;
- fprintf(stderr, "LIBARCHIVE_ASYNC_IO=%s\n",
- (t->async_io)?"Enabled":"Disabled");
- } else
- t->async_io = ASYNC_IO;
- }
- return (t);
- failed:
- archive_wstring_free(&ws);
- tree_free(t);
- return (NULL);
- }
- static int
- tree_descent(struct tree *t)
- {
- t->dirname_length = archive_strlen(&t->path);
- t->full_path_dir_length = archive_strlen(&t->full_path);
- t->depth++;
- return (0);
- }
- /*
- * We've finished a directory; ascend back to the parent.
- */
- static int
- tree_ascend(struct tree *t)
- {
- struct tree_entry *te;
- te = t->stack;
- t->depth--;
- close_and_restore_time(INVALID_HANDLE_VALUE, t, &te->restore_time);
- return (0);
- }
- /*
- * Pop the working stack.
- */
- static void
- tree_pop(struct tree *t)
- {
- struct tree_entry *te;
- t->full_path.s[t->full_path_dir_length] = L'\0';
- t->full_path.length = t->full_path_dir_length;
- t->path.s[t->dirname_length] = L'\0';
- t->path.length = t->dirname_length;
- if (t->stack == t->current && t->current != NULL)
- t->current = t->current->parent;
- te = t->stack;
- t->stack = te->next;
- t->dirname_length = te->dirname_length;
- t->basename = t->path.s + t->dirname_length;
- t->full_path_dir_length = te->full_path_dir_length;
- while (t->basename[0] == L'/')
- t->basename++;
- archive_wstring_free(&te->name);
- archive_wstring_free(&te->full_path);
- free(te);
- }
- /*
- * Get the next item in the tree traversal.
- */
- static int
- tree_next(struct tree *t)
- {
- int r;
- while (t->stack != NULL) {
- /* If there's an open dir, get the next entry from there. */
- if (t->d != INVALID_HANDLE_VALUE) {
- r = tree_dir_next_windows(t, NULL);
- if (r == 0)
- continue;
- return (r);
- }
- if (t->stack->flags & needsFirstVisit) {
- wchar_t *d = t->stack->name.s;
- t->stack->flags &= ~needsFirstVisit;
- if (!(d[0] == L'/' && d[1] == L'/' &&
- d[2] == L'?' && d[3] == L'/') &&
- (wcschr(d, L'*') || wcschr(d, L'?'))) {
- r = tree_dir_next_windows(t, d);
- if (r == 0)
- continue;
- return (r);
- } else {
- HANDLE h = FindFirstFileW(t->stack->full_path.s, &t->_findData);
- if (h == INVALID_HANDLE_VALUE) {
- la_dosmaperr(GetLastError());
- t->tree_errno = errno;
- t->visit_type = TREE_ERROR_DIR;
- return (t->visit_type);
- }
- t->findData = &t->_findData;
- FindClose(h);
- }
- /* Top stack item needs a regular visit. */
- t->current = t->stack;
- r = tree_append(t, t->stack->name.s,
- archive_strlen(&(t->stack->name)));
- if (r != 0)
- return (r);
- //t->dirname_length = t->path_length;
- //tree_pop(t);
- t->stack->flags &= ~needsFirstVisit;
- return (t->visit_type = TREE_REGULAR);
- } else if (t->stack->flags & needsDescent) {
- /* Top stack item is dir to descend into. */
- t->current = t->stack;
- r = tree_append(t, t->stack->name.s,
- archive_strlen(&(t->stack->name)));
- if (r != 0)
- return (r);
- t->stack->flags &= ~needsDescent;
- r = tree_descent(t);
- if (r != 0) {
- tree_pop(t);
- t->visit_type = r;
- } else
- t->visit_type = TREE_POSTDESCENT;
- return (t->visit_type);
- } else if (t->stack->flags & needsOpen) {
- t->stack->flags &= ~needsOpen;
- r = tree_dir_next_windows(t, L"*");
- if (r == 0)
- continue;
- return (r);
- } else if (t->stack->flags & needsAscent) {
- /* Top stack item is dir and we're done with it. */
- r = tree_ascend(t);
- tree_pop(t);
- t->visit_type = r != 0 ? r : TREE_POSTASCENT;
- return (t->visit_type);
- } else {
- /* Top item on stack is dead. */
- tree_pop(t);
- t->flags &= ~hasLstat;
- t->flags &= ~hasStat;
- }
- }
- return (t->visit_type = 0);
- }
- static int
- tree_dir_next_windows(struct tree *t, const wchar_t *pattern)
- {
- const wchar_t *name;
- size_t namelen;
- int r;
- for (;;) {
- if (pattern != NULL) {
- struct archive_wstring pt;
- archive_string_init(&pt);
- if (archive_wstring_ensure(&pt,
- archive_strlen(&(t->full_path))
- + 2 + wcslen(pattern)) == NULL)
- return (TREE_ERROR_FATAL);
- archive_wstring_copy(&pt, &(t->full_path));
- archive_wstrappend_wchar(&pt, L'\\');
- archive_wstrcat(&pt, pattern);
- t->d = FindFirstFileW(pt.s, &t->_findData);
- archive_wstring_free(&pt);
- if (t->d == INVALID_HANDLE_VALUE) {
- la_dosmaperr(GetLastError());
- t->tree_errno = errno;
- r = tree_ascend(t); /* Undo "chdir" */
- tree_pop(t);
- t->visit_type = r != 0 ? r : TREE_ERROR_DIR;
- return (t->visit_type);
- }
- /* Top stack item needs a regular visit. */
- t->current = t->stack;
- t->findData = &t->_findData;
- pattern = NULL;
- } else if (!FindNextFileW(t->d, &t->_findData)) {
- FindClose(t->d);
- t->d = INVALID_HANDLE_VALUE;
- t->findData = NULL;
- return (0);
- }
- name = t->findData->cFileName;
- namelen = wcslen(name);
- t->flags &= ~hasLstat;
- t->flags &= ~hasStat;
- if (name[0] == L'.' && name[1] == L'\0')
- continue;
- if (name[0] == L'.' && name[1] == L'.' && name[2] == L'\0')
- continue;
- r = tree_append(t, name, namelen);
- if (r != 0)
- return (r);
- return (t->visit_type = TREE_REGULAR);
- }
- }
- static void
- entry_copy_bhfi(struct archive_entry *entry, const wchar_t *path,
- const WIN32_FIND_DATAW *findData,
- const BY_HANDLE_FILE_INFORMATION *bhfi)
- {
- int64_t secs;
- uint32_t nsecs;
- mode_t mode;
- ntfs_to_unix(FILETIME_to_ntfs(&bhfi->ftLastAccessTime), &secs, &nsecs);
- archive_entry_set_atime(entry, secs, nsecs);
- ntfs_to_unix(FILETIME_to_ntfs(&bhfi->ftLastWriteTime), &secs, &nsecs);
- archive_entry_set_mtime(entry, secs, nsecs);
- ntfs_to_unix(FILETIME_to_ntfs(&bhfi->ftCreationTime), &secs, &nsecs);
- archive_entry_set_birthtime(entry, secs, nsecs);
- archive_entry_set_ctime(entry, secs, nsecs);
- archive_entry_set_dev(entry, bhfi_dev(bhfi));
- archive_entry_set_ino64(entry, bhfi_ino(bhfi));
- if (bhfi->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
- archive_entry_set_nlink(entry, bhfi->nNumberOfLinks + 1);
- else
- archive_entry_set_nlink(entry, bhfi->nNumberOfLinks);
- archive_entry_set_size(entry,
- (((int64_t)bhfi->nFileSizeHigh) << 32)
- + bhfi->nFileSizeLow);
- archive_entry_set_uid(entry, 0);
- archive_entry_set_gid(entry, 0);
- archive_entry_set_rdev(entry, 0);
- mode = S_IRUSR | S_IRGRP | S_IROTH;
- if ((bhfi->dwFileAttributes & FILE_ATTRIBUTE_READONLY) == 0)
- mode |= S_IWUSR | S_IWGRP | S_IWOTH;
- if ((bhfi->dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) &&
- findData != NULL &&
- findData->dwReserved0 == IO_REPARSE_TAG_SYMLINK) {
- mode |= S_IFLNK;
- entry_symlink_from_pathw(entry, path);
- } else if (bhfi->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
- mode |= S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH;
- else {
- const wchar_t *p;
- mode |= S_IFREG;
- p = wcsrchr(path, L'.');
- if (p != NULL && wcslen(p) == 4) {
- switch (p[1]) {
- case L'B': case L'b':
- if ((p[2] == L'A' || p[2] == L'a' ) &&
- (p[3] == L'T' || p[3] == L't' ))
- mode |= S_IXUSR | S_IXGRP | S_IXOTH;
- break;
- case L'C': case L'c':
- if (((p[2] == L'M' || p[2] == L'm' ) &&
- (p[3] == L'D' || p[3] == L'd' )))
- mode |= S_IXUSR | S_IXGRP | S_IXOTH;
- break;
- case L'E': case L'e':
- if ((p[2] == L'X' || p[2] == L'x' ) &&
- (p[3] == L'E' || p[3] == L'e' ))
- mode |= S_IXUSR | S_IXGRP | S_IXOTH;
- break;
- default:
- break;
- }
- }
- }
- archive_entry_set_mode(entry, mode);
- }
- static void
- tree_archive_entry_copy_bhfi(struct archive_entry *entry, struct tree *t,
- const BY_HANDLE_FILE_INFORMATION *bhfi)
- {
- entry_copy_bhfi(entry, tree_current_path(t), t->findData, bhfi);
- }
- static int
- tree_current_file_information(struct tree *t, BY_HANDLE_FILE_INFORMATION *st,
- int sim_lstat)
- {
- HANDLE h;
- int r;
- DWORD flag = FILE_FLAG_BACKUP_SEMANTICS;
- # if _WIN32_WINNT >= 0x0602 /* _WIN32_WINNT_WIN8 */
- CREATEFILE2_EXTENDED_PARAMETERS createExParams;
- #endif
- if (sim_lstat && tree_current_is_physical_link(t))
- flag |= FILE_FLAG_OPEN_REPARSE_POINT;
- # if _WIN32_WINNT >= 0x0602 /* _WIN32_WINNT_WIN8 */
- ZeroMemory(&createExParams, sizeof(createExParams));
- createExParams.dwSize = sizeof(createExParams);
- createExParams.dwFileFlags = flag;
- h = CreateFile2(tree_current_access_path(t), 0,
- FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
- OPEN_EXISTING, &createExParams);
- #else
- h = CreateFileW(tree_current_access_path(t), 0,
- FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
- OPEN_EXISTING, flag, NULL);
- #endif
- if (h == INVALID_HANDLE_VALUE) {
- la_dosmaperr(GetLastError());
- t->tree_errno = errno;
- return (0);
- }
- r = GetFileInformationByHandle(h, st);
- CloseHandle(h);
- return (r);
- }
- /*
- * Get the stat() data for the entry just returned from tree_next().
- */
- static const BY_HANDLE_FILE_INFORMATION *
- tree_current_stat(struct tree *t)
- {
- if (!(t->flags & hasStat)) {
- if (!tree_current_file_information(t, &t->st, 0))
- return NULL;
- t->flags |= hasStat;
- }
- return (&t->st);
- }
- /*
- * Get the lstat() data for the entry just returned from tree_next().
- */
- static const BY_HANDLE_FILE_INFORMATION *
- tree_current_lstat(struct tree *t)
- {
- if (!(t->flags & hasLstat)) {
- if (!tree_current_file_information(t, &t->lst, 1))
- return NULL;
- t->flags |= hasLstat;
- }
- return (&t->lst);
- }
- /*
- * Test whether current entry is a dir or link to a dir.
- */
- static int
- tree_current_is_dir(struct tree *t)
- {
- if (t->findData)
- return (t->findData->dwFileAttributes
- & FILE_ATTRIBUTE_DIRECTORY);
- return (0);
- }
- /*
- * Test whether current entry is a physical directory. Usually, we
- * already have at least one of stat() or lstat() in memory, so we
- * use tricks to try to avoid an extra trip to the disk.
- */
- static int
- tree_current_is_physical_dir(struct tree *t)
- {
- if (tree_current_is_physical_link(t))
- return (0);
- return (tree_current_is_dir(t));
- }
- /*
- * Test whether current entry is a symbolic link.
- */
- static int
- tree_current_is_physical_link(struct tree *t)
- {
- if (t->findData)
- return ((t->findData->dwFileAttributes
- & FILE_ATTRIBUTE_REPARSE_POINT) &&
- (t->findData->dwReserved0
- == IO_REPARSE_TAG_SYMLINK));
- return (0);
- }
- /*
- * Test whether the same file has been in the tree as its parent.
- */
- static int
- tree_target_is_same_as_parent(struct tree *t,
- const BY_HANDLE_FILE_INFORMATION *st)
- {
- struct tree_entry *te;
- int64_t dev = bhfi_dev(st);
- int64_t ino = bhfi_ino(st);
- for (te = t->current->parent; te != NULL; te = te->parent) {
- if (te->dev == dev && te->ino == ino)
- return (1);
- }
- return (0);
- }
- /*
- * Return the access path for the entry just returned from tree_next().
- */
- static const wchar_t *
- tree_current_access_path(struct tree *t)
- {
- return (t->full_path.s);
- }
- /*
- * Return the full path for the entry just returned from tree_next().
- */
- static const wchar_t *
- tree_current_path(struct tree *t)
- {
- return (t->path.s);
- }
- /*
- * Terminate the traversal.
- */
- static void
- tree_close(struct tree *t)
- {
- if (t == NULL)
- return;
- if (t->entry_fh != INVALID_HANDLE_VALUE) {
- cancel_async(t);
- close_and_restore_time(t->entry_fh, t, &t->restore_time);
- t->entry_fh = INVALID_HANDLE_VALUE;
- }
- /* Close the handle of FindFirstFileW */
- if (t->d != INVALID_HANDLE_VALUE) {
- FindClose(t->d);
- t->d = INVALID_HANDLE_VALUE;
- t->findData = NULL;
- }
- /* Release anything remaining in the stack. */
- while (t->stack != NULL)
- tree_pop(t);
- }
- /*
- * Release any resources.
- */
- static void
- tree_free(struct tree *t)
- {
- int i;
- if (t == NULL)
- return;
- archive_wstring_free(&t->path);
- archive_wstring_free(&t->full_path);
- free(t->sparse_list);
- free(t->filesystem_table);
- for (i = 0; i < MAX_OVERLAPPED; i++) {
- if (t->ol[i].buff)
- VirtualFree(t->ol[i].buff, 0, MEM_RELEASE);
- CloseHandle(t->ol[i].ol.hEvent);
- }
- free(t);
- }
- /*
- * Populate the archive_entry with metadata from the disk.
- */
- int
- archive_read_disk_entry_from_file(struct archive *_a,
- struct archive_entry *entry, int fd, const struct stat *st)
- {
- struct archive_read_disk *a = (struct archive_read_disk *)_a;
- const wchar_t *path;
- const wchar_t *wname;
- const char *name;
- HANDLE h;
- BY_HANDLE_FILE_INFORMATION bhfi;
- DWORD fileAttributes = 0;
- int r;
- archive_clear_error(_a);
- wname = archive_entry_sourcepath_w(entry);
- if (wname == NULL)
- wname = archive_entry_pathname_w(entry);
- if (wname == NULL) {
- archive_set_error(&a->archive, EINVAL,
- "Can't get a wide character version of the path");
- return (ARCHIVE_FAILED);
- }
- path = __la_win_permissive_name_w(wname);
- if (st == NULL) {
- /*
- * Get metadata through GetFileInformationByHandle().
- */
- if (fd >= 0) {
- h = (HANDLE)_get_osfhandle(fd);
- r = GetFileInformationByHandle(h, &bhfi);
- if (r == 0) {
- la_dosmaperr(GetLastError());
- archive_set_error(&a->archive, errno,
- "Can't GetFileInformationByHandle");
- return (ARCHIVE_FAILED);
- }
- entry_copy_bhfi(entry, path, NULL, &bhfi);
- } else {
- WIN32_FIND_DATAW findData;
- DWORD flag, desiredAccess;
- # if _WIN32_WINNT >= 0x0602 /* _WIN32_WINNT_WIN8 */
- CREATEFILE2_EXTENDED_PARAMETERS createExParams;
- #endif
- h = FindFirstFileW(path, &findData);
- if (h == INVALID_HANDLE_VALUE) {
- la_dosmaperr(GetLastError());
- archive_set_error(&a->archive, errno,
- "Can't FindFirstFileW");
- return (ARCHIVE_FAILED);
- }
- FindClose(h);
- flag = FILE_FLAG_BACKUP_SEMANTICS;
- if (!a->follow_symlinks &&
- (findData.dwFileAttributes
- & FILE_ATTRIBUTE_REPARSE_POINT) &&
- (findData.dwReserved0 == IO_REPARSE_TAG_SYMLINK)) {
- flag |= FILE_FLAG_OPEN_REPARSE_POINT;
- desiredAccess = 0;
- } else if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
- desiredAccess = 0;
- } else
- desiredAccess = GENERIC_READ;
- # if _WIN32_WINNT >= 0x0602 /* _WIN32_WINNT_WIN8 */
- ZeroMemory(&createExParams, sizeof(createExParams));
- createExParams.dwSize = sizeof(createExParams);
- createExParams.dwFileFlags = flag;
- h = CreateFile2(path, desiredAccess,
- FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
- OPEN_EXISTING, &createExParams);
- #else
- h = CreateFileW(path, desiredAccess,
- FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
- OPEN_EXISTING, flag, NULL);
- #endif
- if (h == INVALID_HANDLE_VALUE) {
- la_dosmaperr(GetLastError());
- archive_set_error(&a->archive, errno,
- "Can't CreateFileW");
- return (ARCHIVE_FAILED);
- }
- r = GetFileInformationByHandle(h, &bhfi);
- if (r == 0) {
- la_dosmaperr(GetLastError());
- archive_set_error(&a->archive, errno,
- "Can't GetFileInformationByHandle");
- CloseHandle(h);
- return (ARCHIVE_FAILED);
- }
- entry_copy_bhfi(entry, path, &findData, &bhfi);
- }
- fileAttributes = bhfi.dwFileAttributes;
- } else {
- archive_entry_copy_stat(entry, st);
- if (st->st_mode & S_IFLNK)
- entry_symlink_from_pathw(entry, path);
- h = INVALID_HANDLE_VALUE;
- }
- /* Lookup uname/gname */
- name = archive_read_disk_uname(_a, archive_entry_uid(entry));
- if (name != NULL)
- archive_entry_copy_uname(entry, name);
- name = archive_read_disk_gname(_a, archive_entry_gid(entry));
- if (name != NULL)
- archive_entry_copy_gname(entry, name);
- /*
- * File attributes
- */
- if ((a->flags & ARCHIVE_READDISK_NO_FFLAGS) == 0) {
- const int supported_attrs =
- FILE_ATTRIBUTE_READONLY |
- FILE_ATTRIBUTE_HIDDEN |
- FILE_ATTRIBUTE_SYSTEM;
- DWORD file_attrs = fileAttributes & supported_attrs;
- if (file_attrs != 0)
- archive_entry_set_fflags(entry, file_attrs, 0);
- }
- /*
- * Can this file be sparse file ?
- */
- if (archive_entry_filetype(entry) != AE_IFREG
- || archive_entry_size(entry) <= 0
- || archive_entry_hardlink(entry) != NULL) {
- if (h != INVALID_HANDLE_VALUE && fd < 0)
- CloseHandle(h);
- return (ARCHIVE_OK);
- }
- if (h == INVALID_HANDLE_VALUE) {
- if (fd >= 0) {
- h = (HANDLE)_get_osfhandle(fd);
- } else {
- # if _WIN32_WINNT >= 0x0602 /* _WIN32_WINNT_WIN8 */
- CREATEFILE2_EXTENDED_PARAMETERS createExParams;
- ZeroMemory(&createExParams, sizeof(createExParams));
- createExParams.dwSize = sizeof(createExParams);
- createExParams.dwFileFlags = FILE_FLAG_BACKUP_SEMANTICS;
- h = CreateFile2(path, GENERIC_READ,
- FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
- OPEN_EXISTING, &createExParams);
- #else
- h = CreateFileW(path, GENERIC_READ,
- FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
- OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
- #endif
- if (h == INVALID_HANDLE_VALUE) {
- la_dosmaperr(GetLastError());
- archive_set_error(&a->archive, errno,
- "Can't CreateFileW");
- return (ARCHIVE_FAILED);
- }
- }
- r = GetFileInformationByHandle(h, &bhfi);
- if (r == 0) {
- la_dosmaperr(GetLastError());
- archive_set_error(&a->archive, errno,
- "Can't GetFileInformationByHandle");
- if (h != INVALID_HANDLE_VALUE && fd < 0)
- CloseHandle(h);
- return (ARCHIVE_FAILED);
- }
- fileAttributes = bhfi.dwFileAttributes;
- }
- /* Sparse file must be set a mark, FILE_ATTRIBUTE_SPARSE_FILE */
- if ((fileAttributes & FILE_ATTRIBUTE_SPARSE_FILE) == 0) {
- if (fd < 0)
- CloseHandle(h);
- return (ARCHIVE_OK);
- }
- r = ARCHIVE_OK;
- if ((a->flags & ARCHIVE_READDISK_NO_SPARSE) == 0) {
- r = setup_sparse_from_disk(a, entry, h);
- if (fd < 0)
- CloseHandle(h);
- }
- return (r);
- }
- /*
- * Windows sparse interface.
- */
- #if defined(__MINGW32__) && !defined(FSCTL_QUERY_ALLOCATED_RANGES)
- #define FSCTL_QUERY_ALLOCATED_RANGES 0x940CF
- typedef struct {
- LARGE_INTEGER FileOffset;
- LARGE_INTEGER Length;
- } FILE_ALLOCATED_RANGE_BUFFER;
- #endif
- static int
- setup_sparse_from_disk(struct archive_read_disk *a,
- struct archive_entry *entry, HANDLE handle)
- {
- FILE_ALLOCATED_RANGE_BUFFER range, *outranges = NULL;
- size_t outranges_size;
- int64_t entry_size = archive_entry_size(entry);
- int exit_sts = ARCHIVE_OK;
- range.FileOffset.QuadPart = 0;
- range.Length.QuadPart = entry_size;
- outranges_size = 2048;
- outranges = malloc(outranges_size);
- if (outranges == NULL) {
- archive_set_error(&a->archive, ENOMEM,
- "Couldn't allocate memory");
- exit_sts = ARCHIVE_FATAL;
- goto exit_setup_sparse;
- }
- for (;;) {
- DWORD retbytes;
- BOOL ret;
- for (;;) {
- ret = DeviceIoControl(handle,
- FSCTL_QUERY_ALLOCATED_RANGES,
- &range, sizeof(range), outranges,
- (DWORD)outranges_size, &retbytes, NULL);
- if (ret == 0 && GetLastError() == ERROR_MORE_DATA) {
- free(outranges);
- outranges_size *= 2;
- outranges = (FILE_ALLOCATED_RANGE_BUFFER *)
- malloc(outranges_size);
- if (outranges == NULL) {
- archive_set_error(&a->archive, ENOMEM,
- "Couldn't allocate memory");
- exit_sts = ARCHIVE_FATAL;
- goto exit_setup_sparse;
- }
- continue;
- } else
- break;
- }
- if (ret != 0) {
- if (retbytes > 0) {
- DWORD i, n;
- n = retbytes / sizeof(outranges[0]);
- if (n == 1 &&
- outranges[0].FileOffset.QuadPart == 0 &&
- outranges[0].Length.QuadPart == entry_size)
- break;/* This is not sparse. */
- for (i = 0; i < n; i++)
- archive_entry_sparse_add_entry(entry,
- outranges[i].FileOffset.QuadPart,
- outranges[i].Length.QuadPart);
- range.FileOffset.QuadPart =
- outranges[n-1].FileOffset.QuadPart
- + outranges[n-1].Length.QuadPart;
- range.Length.QuadPart =
- entry_size - range.FileOffset.QuadPart;
- if (range.Length.QuadPart > 0)
- continue;
- } else {
- /* The entire file is a hole. Add one data block of size 0 at the end. */
- archive_entry_sparse_add_entry(entry,
- entry_size,
- 0);
- }
- break;
- } else {
- la_dosmaperr(GetLastError());
- archive_set_error(&a->archive, errno,
- "DeviceIoControl Failed: %lu", GetLastError());
- exit_sts = ARCHIVE_FAILED;
- goto exit_setup_sparse;
- }
- }
- exit_setup_sparse:
- free(outranges);
- return (exit_sts);
- }
- #endif
|