archive_entry.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156
  1. /*-
  2. * Copyright (c) 2003-2007 Tim Kientzle
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
  15. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  16. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  17. * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
  18. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  19. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  20. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  21. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  23. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "archive_platform.h"
  26. __FBSDID("$FreeBSD: src/lib/libarchive/archive_entry.c,v 1.55 2008/12/23 05:01:43 kientzle Exp $");
  27. #ifdef HAVE_SYS_STAT_H
  28. #include <sys/stat.h>
  29. #endif
  30. #ifdef HAVE_SYS_TYPES_H
  31. #include <sys/types.h>
  32. #endif
  33. #ifdef MAJOR_IN_MKDEV
  34. #include <sys/mkdev.h>
  35. #else
  36. #ifdef MAJOR_IN_SYSMACROS
  37. #include <sys/sysmacros.h>
  38. #endif
  39. #endif
  40. #ifdef HAVE_LIMITS_H
  41. #include <limits.h>
  42. #endif
  43. #ifdef HAVE_LINUX_FS_H
  44. #include <linux/fs.h> /* for Linux file flags */
  45. #endif
  46. /*
  47. * Some Linux distributions have both linux/ext2_fs.h and ext2fs/ext2_fs.h.
  48. * As the include guards don't agree, the order of include is important.
  49. */
  50. #ifdef HAVE_LINUX_EXT2_FS_H
  51. #include <linux/ext2_fs.h> /* for Linux file flags */
  52. #endif
  53. #if defined(HAVE_EXT2FS_EXT2_FS_H) && !defined(__CYGWIN__)
  54. #include <ext2fs/ext2_fs.h> /* for Linux file flags */
  55. #endif
  56. #include <stddef.h>
  57. #include <stdio.h>
  58. #ifdef HAVE_STDLIB_H
  59. #include <stdlib.h>
  60. #endif
  61. #ifdef HAVE_STRING_H
  62. #include <string.h>
  63. #endif
  64. #ifdef HAVE_WCHAR_H
  65. #include <wchar.h>
  66. #endif
  67. #include "archive.h"
  68. #include "archive_entry.h"
  69. #include "archive_private.h"
  70. #include "archive_entry_private.h"
  71. #undef max
  72. #define max(a, b) ((a)>(b)?(a):(b))
  73. /* Play games to come up with a suitable makedev() definition. */
  74. #ifdef __QNXNTO__
  75. /* QNX. <sigh> */
  76. #include <sys/netmgr.h>
  77. #define ae_makedev(maj, min) makedev(ND_LOCAL_NODE, (maj), (min))
  78. #elif defined makedev
  79. /* There's a "makedev" macro. */
  80. #define ae_makedev(maj, min) makedev((maj), (min))
  81. #elif defined mkdev || ((defined _WIN32 || defined __WIN32__) && !defined(__CYGWIN__))
  82. /* Windows. <sigh> */
  83. #define ae_makedev(maj, min) mkdev((maj), (min))
  84. #else
  85. /* There's a "makedev" function. */
  86. #define ae_makedev(maj, min) makedev((maj), (min))
  87. #endif
  88. static void aes_clean(struct aes *);
  89. static void aes_copy(struct aes *dest, struct aes *src);
  90. static const char * aes_get_mbs(struct aes *);
  91. static const wchar_t * aes_get_wcs(struct aes *);
  92. static int aes_set_mbs(struct aes *, const char *mbs);
  93. static int aes_copy_mbs(struct aes *, const char *mbs);
  94. /* static void aes_set_wcs(struct aes *, const wchar_t *wcs); */
  95. static int aes_copy_wcs(struct aes *, const wchar_t *wcs);
  96. static int aes_copy_wcs_len(struct aes *, const wchar_t *wcs, size_t);
  97. static char * ae_fflagstostr(unsigned long bitset, unsigned long bitclear);
  98. static const wchar_t *ae_wcstofflags(const wchar_t *stringp,
  99. unsigned long *setp, unsigned long *clrp);
  100. static const char *ae_strtofflags(const char *stringp,
  101. unsigned long *setp, unsigned long *clrp);
  102. static void append_entry_w(wchar_t **wp, const wchar_t *prefix, int tag,
  103. const wchar_t *wname, int perm, int id);
  104. static void append_id_w(wchar_t **wp, int id);
  105. static int acl_special(struct archive_entry *entry,
  106. int type, int permset, int tag);
  107. static struct ae_acl *acl_new_entry(struct archive_entry *entry,
  108. int type, int permset, int tag, int id);
  109. static int isint_w(const wchar_t *start, const wchar_t *end, int *result);
  110. static void next_field_w(const wchar_t **wp, const wchar_t **start,
  111. const wchar_t **end, wchar_t *sep);
  112. static int prefix_w(const wchar_t *start, const wchar_t *end,
  113. const wchar_t *test);
  114. static void
  115. archive_entry_acl_add_entry_w_len(struct archive_entry *entry, int type,
  116. int permset, int tag, int id, const wchar_t *name, size_t);
  117. #ifndef HAVE_WCSCPY
  118. static wchar_t * wcscpy(wchar_t *s1, const wchar_t *s2)
  119. {
  120. wchar_t *dest = s1;
  121. while ((*s1 = *s2) != L'\0')
  122. ++s1, ++s2;
  123. return dest;
  124. }
  125. #endif
  126. #ifndef HAVE_WCSLEN
  127. static size_t wcslen(const wchar_t *s)
  128. {
  129. const wchar_t *p = s;
  130. while (*p != L'\0')
  131. ++p;
  132. return p - s;
  133. }
  134. #endif
  135. #ifndef HAVE_WMEMCMP
  136. /* Good enough for simple equality testing, but not for sorting. */
  137. #define wmemcmp(a,b,i) memcmp((a), (b), (i) * sizeof(wchar_t))
  138. #endif
  139. #ifndef HAVE_WMEMCPY
  140. #define wmemcpy(a,b,i) (wchar_t *)memcpy((a), (b), (i) * sizeof(wchar_t))
  141. #endif
  142. static void
  143. aes_clean(struct aes *aes)
  144. {
  145. if (aes->aes_wcs) {
  146. free((wchar_t *)(uintptr_t)aes->aes_wcs);
  147. aes->aes_wcs = NULL;
  148. }
  149. archive_string_free(&(aes->aes_mbs));
  150. archive_string_free(&(aes->aes_utf8));
  151. aes->aes_set = 0;
  152. }
  153. static void
  154. aes_copy(struct aes *dest, struct aes *src)
  155. {
  156. wchar_t *wp;
  157. dest->aes_set = src->aes_set;
  158. archive_string_copy(&(dest->aes_mbs), &(src->aes_mbs));
  159. archive_string_copy(&(dest->aes_utf8), &(src->aes_utf8));
  160. if (src->aes_wcs != NULL) {
  161. wp = (wchar_t *)malloc((wcslen(src->aes_wcs) + 1)
  162. * sizeof(wchar_t));
  163. if (wp == NULL)
  164. __archive_errx(1, "No memory for aes_copy()");
  165. wcscpy(wp, src->aes_wcs);
  166. dest->aes_wcs = wp;
  167. }
  168. }
  169. static const char *
  170. aes_get_utf8(struct aes *aes)
  171. {
  172. if (aes->aes_set & AES_SET_UTF8)
  173. return (aes->aes_utf8.s);
  174. if ((aes->aes_set & AES_SET_WCS)
  175. && archive_strappend_w_utf8(&(aes->aes_utf8), aes->aes_wcs) != NULL) {
  176. aes->aes_set |= AES_SET_UTF8;
  177. return (aes->aes_utf8.s);
  178. }
  179. return (NULL);
  180. }
  181. static const char *
  182. aes_get_mbs(struct aes *aes)
  183. {
  184. /* If we already have an MBS form, return that immediately. */
  185. if (aes->aes_set & AES_SET_MBS)
  186. return (aes->aes_mbs.s);
  187. /* If there's a WCS form, try converting with the native locale. */
  188. if ((aes->aes_set & AES_SET_WCS)
  189. && archive_strappend_w_mbs(&(aes->aes_mbs), aes->aes_wcs) != NULL) {
  190. aes->aes_set |= AES_SET_MBS;
  191. return (aes->aes_mbs.s);
  192. }
  193. /* We'll use UTF-8 for MBS if all else fails. */
  194. return (aes_get_utf8(aes));
  195. }
  196. static const wchar_t *
  197. aes_get_wcs(struct aes *aes)
  198. {
  199. wchar_t *w;
  200. int r;
  201. /* Return WCS form if we already have it. */
  202. if (aes->aes_set & AES_SET_WCS)
  203. return (aes->aes_wcs);
  204. if (aes->aes_set & AES_SET_MBS) {
  205. /* Try converting MBS to WCS using native locale. */
  206. /*
  207. * No single byte will be more than one wide character,
  208. * so this length estimate will always be big enough.
  209. */
  210. size_t wcs_length = aes->aes_mbs.length;
  211. w = (wchar_t *)malloc((wcs_length + 1) * sizeof(wchar_t));
  212. if (w == NULL)
  213. __archive_errx(1, "No memory for aes_get_wcs()");
  214. r = mbstowcs(w, aes->aes_mbs.s, wcs_length);
  215. if (r > 0) {
  216. w[r] = 0;
  217. aes->aes_set |= AES_SET_WCS;
  218. return (aes->aes_wcs = w);
  219. }
  220. free(w);
  221. }
  222. if (aes->aes_set & AES_SET_UTF8) {
  223. /* Try converting UTF8 to WCS. */
  224. aes->aes_wcs = __archive_string_utf8_w(&(aes->aes_utf8));
  225. if (aes->aes_wcs != NULL)
  226. aes->aes_set |= AES_SET_WCS;
  227. return (aes->aes_wcs);
  228. }
  229. return (NULL);
  230. }
  231. static int
  232. aes_set_mbs(struct aes *aes, const char *mbs)
  233. {
  234. return (aes_copy_mbs(aes, mbs));
  235. }
  236. static int
  237. aes_copy_mbs(struct aes *aes, const char *mbs)
  238. {
  239. if (mbs == NULL) {
  240. aes->aes_set = 0;
  241. return (0);
  242. }
  243. aes->aes_set = AES_SET_MBS; /* Only MBS form is set now. */
  244. archive_strcpy(&(aes->aes_mbs), mbs);
  245. archive_string_empty(&(aes->aes_utf8));
  246. if (aes->aes_wcs) {
  247. free((wchar_t *)(uintptr_t)aes->aes_wcs);
  248. aes->aes_wcs = NULL;
  249. }
  250. return (0);
  251. }
  252. /*
  253. * The 'update' form tries to proactively update all forms of
  254. * this string (WCS and MBS) and returns an error if any of
  255. * them fail. This is used by the 'pax' handler, for instance,
  256. * to detect and report character-conversion failures early while
  257. * still allowing clients to get potentially useful values from
  258. * the more tolerant lazy conversions. (get_mbs and get_wcs will
  259. * strive to give the user something useful, so you can get hopefully
  260. * usable values even if some of the character conversions are failing.)
  261. */
  262. static int
  263. aes_update_utf8(struct aes *aes, const char *utf8)
  264. {
  265. if (utf8 == NULL) {
  266. aes->aes_set = 0;
  267. return (1); /* Succeeded in clearing everything. */
  268. }
  269. /* Save the UTF8 string. */
  270. archive_strcpy(&(aes->aes_utf8), utf8);
  271. /* Empty the mbs and wcs strings. */
  272. archive_string_empty(&(aes->aes_mbs));
  273. if (aes->aes_wcs) {
  274. free((wchar_t *)(uintptr_t)aes->aes_wcs);
  275. aes->aes_wcs = NULL;
  276. }
  277. aes->aes_set = AES_SET_UTF8; /* Only UTF8 is set now. */
  278. /* TODO: We should just do a direct UTF-8 to MBS conversion
  279. * here. That would be faster, use less space, and give the
  280. * same information. (If a UTF-8 to MBS conversion succeeds,
  281. * then UTF-8->WCS and Unicode->MBS conversions will both
  282. * succeed.) */
  283. /* Try converting UTF8 to WCS, return false on failure. */
  284. aes->aes_wcs = __archive_string_utf8_w(&(aes->aes_utf8));
  285. if (aes->aes_wcs == NULL)
  286. return (0);
  287. aes->aes_set = AES_SET_UTF8 | AES_SET_WCS; /* Both UTF8 and WCS set. */
  288. /* Try converting WCS to MBS, return false on failure. */
  289. if (archive_strappend_w_mbs(&(aes->aes_mbs), aes->aes_wcs) == NULL)
  290. return (0);
  291. aes->aes_set = AES_SET_UTF8 | AES_SET_WCS | AES_SET_MBS;
  292. /* All conversions succeeded. */
  293. return (1);
  294. }
  295. static int
  296. aes_copy_wcs(struct aes *aes, const wchar_t *wcs)
  297. {
  298. return aes_copy_wcs_len(aes, wcs, wcs == NULL ? 0 : wcslen(wcs));
  299. }
  300. static int
  301. aes_copy_wcs_len(struct aes *aes, const wchar_t *wcs, size_t len)
  302. {
  303. wchar_t *w;
  304. if (wcs == NULL) {
  305. aes->aes_set = 0;
  306. return (0);
  307. }
  308. aes->aes_set = AES_SET_WCS; /* Only WCS form set. */
  309. archive_string_empty(&(aes->aes_mbs));
  310. archive_string_empty(&(aes->aes_utf8));
  311. if (aes->aes_wcs) {
  312. free((wchar_t *)(uintptr_t)aes->aes_wcs);
  313. aes->aes_wcs = NULL;
  314. }
  315. w = (wchar_t *)malloc((len + 1) * sizeof(wchar_t));
  316. if (w == NULL)
  317. __archive_errx(1, "No memory for aes_copy_wcs()");
  318. wmemcpy(w, wcs, len);
  319. w[len] = L'\0';
  320. aes->aes_wcs = w;
  321. return (0);
  322. }
  323. /****************************************************************************
  324. *
  325. * Public Interface
  326. *
  327. ****************************************************************************/
  328. struct archive_entry *
  329. archive_entry_clear(struct archive_entry *entry)
  330. {
  331. if (entry == NULL)
  332. return (NULL);
  333. aes_clean(&entry->ae_fflags_text);
  334. aes_clean(&entry->ae_gname);
  335. aes_clean(&entry->ae_hardlink);
  336. aes_clean(&entry->ae_pathname);
  337. aes_clean(&entry->ae_sourcepath);
  338. aes_clean(&entry->ae_symlink);
  339. aes_clean(&entry->ae_uname);
  340. archive_entry_acl_clear(entry);
  341. archive_entry_xattr_clear(entry);
  342. free(entry->stat);
  343. memset(entry, 0, sizeof(*entry));
  344. return entry;
  345. }
  346. struct archive_entry *
  347. archive_entry_clone(struct archive_entry *entry)
  348. {
  349. struct archive_entry *entry2;
  350. struct ae_acl *ap, *ap2;
  351. struct ae_xattr *xp;
  352. /* Allocate new structure and copy over all of the fields. */
  353. entry2 = (struct archive_entry *)malloc(sizeof(*entry2));
  354. if (entry2 == NULL)
  355. return (NULL);
  356. memset(entry2, 0, sizeof(*entry2));
  357. entry2->ae_stat = entry->ae_stat;
  358. entry2->ae_fflags_set = entry->ae_fflags_set;
  359. entry2->ae_fflags_clear = entry->ae_fflags_clear;
  360. aes_copy(&entry2->ae_fflags_text, &entry->ae_fflags_text);
  361. aes_copy(&entry2->ae_gname, &entry->ae_gname);
  362. aes_copy(&entry2->ae_hardlink, &entry->ae_hardlink);
  363. aes_copy(&entry2->ae_pathname, &entry->ae_pathname);
  364. aes_copy(&entry2->ae_sourcepath, &entry->ae_sourcepath);
  365. aes_copy(&entry2->ae_symlink, &entry->ae_symlink);
  366. entry2->ae_set = entry->ae_set;
  367. aes_copy(&entry2->ae_uname, &entry->ae_uname);
  368. /* Copy ACL data over. */
  369. ap = entry->acl_head;
  370. while (ap != NULL) {
  371. ap2 = acl_new_entry(entry2,
  372. ap->type, ap->permset, ap->tag, ap->id);
  373. if (ap2 != NULL)
  374. aes_copy(&ap2->name, &ap->name);
  375. ap = ap->next;
  376. }
  377. /* Copy xattr data over. */
  378. xp = entry->xattr_head;
  379. while (xp != NULL) {
  380. archive_entry_xattr_add_entry(entry2,
  381. xp->name, xp->value, xp->size);
  382. xp = xp->next;
  383. }
  384. return (entry2);
  385. }
  386. void
  387. archive_entry_free(struct archive_entry *entry)
  388. {
  389. archive_entry_clear(entry);
  390. free(entry);
  391. }
  392. struct archive_entry *
  393. archive_entry_new(void)
  394. {
  395. struct archive_entry *entry;
  396. entry = (struct archive_entry *)malloc(sizeof(*entry));
  397. if (entry == NULL)
  398. return (NULL);
  399. memset(entry, 0, sizeof(*entry));
  400. return (entry);
  401. }
  402. /*
  403. * Functions for reading fields from an archive_entry.
  404. */
  405. time_t
  406. archive_entry_atime(struct archive_entry *entry)
  407. {
  408. return (entry->ae_stat.aest_atime);
  409. }
  410. long
  411. archive_entry_atime_nsec(struct archive_entry *entry)
  412. {
  413. return (entry->ae_stat.aest_atime_nsec);
  414. }
  415. int
  416. archive_entry_atime_is_set(struct archive_entry *entry)
  417. {
  418. return (entry->ae_set & AE_SET_ATIME);
  419. }
  420. time_t
  421. archive_entry_birthtime(struct archive_entry *entry)
  422. {
  423. return (entry->ae_stat.aest_birthtime);
  424. }
  425. long
  426. archive_entry_birthtime_nsec(struct archive_entry *entry)
  427. {
  428. return (entry->ae_stat.aest_birthtime_nsec);
  429. }
  430. int
  431. archive_entry_birthtime_is_set(struct archive_entry *entry)
  432. {
  433. return (entry->ae_set & AE_SET_BIRTHTIME);
  434. }
  435. time_t
  436. archive_entry_ctime(struct archive_entry *entry)
  437. {
  438. return (entry->ae_stat.aest_ctime);
  439. }
  440. int
  441. archive_entry_ctime_is_set(struct archive_entry *entry)
  442. {
  443. return (entry->ae_set & AE_SET_CTIME);
  444. }
  445. long
  446. archive_entry_ctime_nsec(struct archive_entry *entry)
  447. {
  448. return (entry->ae_stat.aest_ctime_nsec);
  449. }
  450. dev_t
  451. archive_entry_dev(struct archive_entry *entry)
  452. {
  453. if (entry->ae_stat.aest_dev_is_broken_down)
  454. return ae_makedev(entry->ae_stat.aest_devmajor,
  455. entry->ae_stat.aest_devminor);
  456. else
  457. return (entry->ae_stat.aest_dev);
  458. }
  459. dev_t
  460. archive_entry_devmajor(struct archive_entry *entry)
  461. {
  462. if (entry->ae_stat.aest_dev_is_broken_down)
  463. return (entry->ae_stat.aest_devmajor);
  464. else
  465. return major(entry->ae_stat.aest_dev);
  466. }
  467. dev_t
  468. archive_entry_devminor(struct archive_entry *entry)
  469. {
  470. if (entry->ae_stat.aest_dev_is_broken_down)
  471. return (entry->ae_stat.aest_devminor);
  472. else
  473. return minor(entry->ae_stat.aest_dev);
  474. }
  475. mode_t
  476. archive_entry_filetype(struct archive_entry *entry)
  477. {
  478. return (AE_IFMT & entry->ae_stat.aest_mode);
  479. }
  480. void
  481. archive_entry_fflags(struct archive_entry *entry,
  482. unsigned long *set, unsigned long *clear)
  483. {
  484. *set = entry->ae_fflags_set;
  485. *clear = entry->ae_fflags_clear;
  486. }
  487. /*
  488. * Note: if text was provided, this just returns that text. If you
  489. * really need the text to be rebuilt in a canonical form, set the
  490. * text, ask for the bitmaps, then set the bitmaps. (Setting the
  491. * bitmaps clears any stored text.) This design is deliberate: if
  492. * we're editing archives, we don't want to discard flags just because
  493. * they aren't supported on the current system. The bitmap<->text
  494. * conversions are platform-specific (see below).
  495. */
  496. const char *
  497. archive_entry_fflags_text(struct archive_entry *entry)
  498. {
  499. const char *f;
  500. char *p;
  501. f = aes_get_mbs(&entry->ae_fflags_text);
  502. if (f != NULL)
  503. return (f);
  504. if (entry->ae_fflags_set == 0 && entry->ae_fflags_clear == 0)
  505. return (NULL);
  506. p = ae_fflagstostr(entry->ae_fflags_set, entry->ae_fflags_clear);
  507. if (p == NULL)
  508. return (NULL);
  509. aes_copy_mbs(&entry->ae_fflags_text, p);
  510. free(p);
  511. f = aes_get_mbs(&entry->ae_fflags_text);
  512. return (f);
  513. }
  514. gid_t
  515. archive_entry_gid(struct archive_entry *entry)
  516. {
  517. return (entry->ae_stat.aest_gid);
  518. }
  519. const char *
  520. archive_entry_gname(struct archive_entry *entry)
  521. {
  522. return (aes_get_mbs(&entry->ae_gname));
  523. }
  524. const wchar_t *
  525. archive_entry_gname_w(struct archive_entry *entry)
  526. {
  527. return (aes_get_wcs(&entry->ae_gname));
  528. }
  529. const char *
  530. archive_entry_hardlink(struct archive_entry *entry)
  531. {
  532. if (entry->ae_set & AE_SET_HARDLINK)
  533. return (aes_get_mbs(&entry->ae_hardlink));
  534. return (NULL);
  535. }
  536. const wchar_t *
  537. archive_entry_hardlink_w(struct archive_entry *entry)
  538. {
  539. if (entry->ae_set & AE_SET_HARDLINK)
  540. return (aes_get_wcs(&entry->ae_hardlink));
  541. return (NULL);
  542. }
  543. ino_t
  544. archive_entry_ino(struct archive_entry *entry)
  545. {
  546. return (entry->ae_stat.aest_ino);
  547. }
  548. mode_t
  549. archive_entry_mode(struct archive_entry *entry)
  550. {
  551. return (entry->ae_stat.aest_mode);
  552. }
  553. time_t
  554. archive_entry_mtime(struct archive_entry *entry)
  555. {
  556. return (entry->ae_stat.aest_mtime);
  557. }
  558. long
  559. archive_entry_mtime_nsec(struct archive_entry *entry)
  560. {
  561. return (entry->ae_stat.aest_mtime_nsec);
  562. }
  563. int
  564. archive_entry_mtime_is_set(struct archive_entry *entry)
  565. {
  566. return (entry->ae_set & AE_SET_MTIME);
  567. }
  568. unsigned int
  569. archive_entry_nlink(struct archive_entry *entry)
  570. {
  571. return (entry->ae_stat.aest_nlink);
  572. }
  573. const char *
  574. archive_entry_pathname(struct archive_entry *entry)
  575. {
  576. return (aes_get_mbs(&entry->ae_pathname));
  577. }
  578. const wchar_t *
  579. archive_entry_pathname_w(struct archive_entry *entry)
  580. {
  581. return (aes_get_wcs(&entry->ae_pathname));
  582. }
  583. dev_t
  584. archive_entry_rdev(struct archive_entry *entry)
  585. {
  586. if (entry->ae_stat.aest_rdev_is_broken_down)
  587. return ae_makedev(entry->ae_stat.aest_rdevmajor,
  588. entry->ae_stat.aest_rdevminor);
  589. else
  590. return (entry->ae_stat.aest_rdev);
  591. }
  592. dev_t
  593. archive_entry_rdevmajor(struct archive_entry *entry)
  594. {
  595. if (entry->ae_stat.aest_rdev_is_broken_down)
  596. return (entry->ae_stat.aest_rdevmajor);
  597. else
  598. return major(entry->ae_stat.aest_rdev);
  599. }
  600. dev_t
  601. archive_entry_rdevminor(struct archive_entry *entry)
  602. {
  603. if (entry->ae_stat.aest_rdev_is_broken_down)
  604. return (entry->ae_stat.aest_rdevminor);
  605. else
  606. return minor(entry->ae_stat.aest_rdev);
  607. }
  608. int64_t
  609. archive_entry_size(struct archive_entry *entry)
  610. {
  611. return (entry->ae_stat.aest_size);
  612. }
  613. int
  614. archive_entry_size_is_set(struct archive_entry *entry)
  615. {
  616. return (entry->ae_set & AE_SET_SIZE);
  617. }
  618. const char *
  619. archive_entry_sourcepath(struct archive_entry *entry)
  620. {
  621. return (aes_get_mbs(&entry->ae_sourcepath));
  622. }
  623. const char *
  624. archive_entry_symlink(struct archive_entry *entry)
  625. {
  626. if (entry->ae_set & AE_SET_SYMLINK)
  627. return (aes_get_mbs(&entry->ae_symlink));
  628. return (NULL);
  629. }
  630. const wchar_t *
  631. archive_entry_symlink_w(struct archive_entry *entry)
  632. {
  633. if (entry->ae_set & AE_SET_SYMLINK)
  634. return (aes_get_wcs(&entry->ae_symlink));
  635. return (NULL);
  636. }
  637. uid_t
  638. archive_entry_uid(struct archive_entry *entry)
  639. {
  640. return (entry->ae_stat.aest_uid);
  641. }
  642. const char *
  643. archive_entry_uname(struct archive_entry *entry)
  644. {
  645. return (aes_get_mbs(&entry->ae_uname));
  646. }
  647. const wchar_t *
  648. archive_entry_uname_w(struct archive_entry *entry)
  649. {
  650. return (aes_get_wcs(&entry->ae_uname));
  651. }
  652. /*
  653. * Functions to set archive_entry properties.
  654. */
  655. void
  656. archive_entry_set_filetype(struct archive_entry *entry, unsigned int type)
  657. {
  658. entry->stat_valid = 0;
  659. entry->ae_stat.aest_mode &= ~AE_IFMT;
  660. entry->ae_stat.aest_mode |= AE_IFMT & type;
  661. }
  662. void
  663. archive_entry_set_fflags(struct archive_entry *entry,
  664. unsigned long set, unsigned long clear)
  665. {
  666. aes_clean(&entry->ae_fflags_text);
  667. entry->ae_fflags_set = set;
  668. entry->ae_fflags_clear = clear;
  669. }
  670. const char *
  671. archive_entry_copy_fflags_text(struct archive_entry *entry,
  672. const char *flags)
  673. {
  674. aes_copy_mbs(&entry->ae_fflags_text, flags);
  675. return (ae_strtofflags(flags,
  676. &entry->ae_fflags_set, &entry->ae_fflags_clear));
  677. }
  678. const wchar_t *
  679. archive_entry_copy_fflags_text_w(struct archive_entry *entry,
  680. const wchar_t *flags)
  681. {
  682. aes_copy_wcs(&entry->ae_fflags_text, flags);
  683. return (ae_wcstofflags(flags,
  684. &entry->ae_fflags_set, &entry->ae_fflags_clear));
  685. }
  686. void
  687. archive_entry_set_gid(struct archive_entry *entry, gid_t g)
  688. {
  689. entry->stat_valid = 0;
  690. entry->ae_stat.aest_gid = g;
  691. }
  692. void
  693. archive_entry_set_gname(struct archive_entry *entry, const char *name)
  694. {
  695. aes_set_mbs(&entry->ae_gname, name);
  696. }
  697. void
  698. archive_entry_copy_gname(struct archive_entry *entry, const char *name)
  699. {
  700. aes_copy_mbs(&entry->ae_gname, name);
  701. }
  702. void
  703. archive_entry_copy_gname_w(struct archive_entry *entry, const wchar_t *name)
  704. {
  705. aes_copy_wcs(&entry->ae_gname, name);
  706. }
  707. int
  708. archive_entry_update_gname_utf8(struct archive_entry *entry, const char *name)
  709. {
  710. return (aes_update_utf8(&entry->ae_gname, name));
  711. }
  712. void
  713. archive_entry_set_ino(struct archive_entry *entry, unsigned long ino)
  714. {
  715. entry->stat_valid = 0;
  716. entry->ae_stat.aest_ino = ino;
  717. }
  718. void
  719. archive_entry_set_hardlink(struct archive_entry *entry, const char *target)
  720. {
  721. aes_set_mbs(&entry->ae_hardlink, target);
  722. if (target != NULL)
  723. entry->ae_set |= AE_SET_HARDLINK;
  724. else
  725. entry->ae_set &= ~AE_SET_HARDLINK;
  726. }
  727. void
  728. archive_entry_copy_hardlink(struct archive_entry *entry, const char *target)
  729. {
  730. aes_copy_mbs(&entry->ae_hardlink, target);
  731. if (target != NULL)
  732. entry->ae_set |= AE_SET_HARDLINK;
  733. else
  734. entry->ae_set &= ~AE_SET_HARDLINK;
  735. }
  736. void
  737. archive_entry_copy_hardlink_w(struct archive_entry *entry, const wchar_t *target)
  738. {
  739. aes_copy_wcs(&entry->ae_hardlink, target);
  740. if (target != NULL)
  741. entry->ae_set |= AE_SET_HARDLINK;
  742. else
  743. entry->ae_set &= ~AE_SET_HARDLINK;
  744. }
  745. void
  746. archive_entry_set_atime(struct archive_entry *entry, time_t t, long ns)
  747. {
  748. entry->stat_valid = 0;
  749. entry->ae_set |= AE_SET_ATIME;
  750. entry->ae_stat.aest_atime = t;
  751. entry->ae_stat.aest_atime_nsec = ns;
  752. }
  753. void
  754. archive_entry_unset_atime(struct archive_entry *entry)
  755. {
  756. archive_entry_set_atime(entry, 0, 0);
  757. entry->ae_set &= ~AE_SET_ATIME;
  758. }
  759. void
  760. archive_entry_set_birthtime(struct archive_entry *entry, time_t m, long ns)
  761. {
  762. entry->stat_valid = 0;
  763. entry->ae_set |= AE_SET_BIRTHTIME;
  764. entry->ae_stat.aest_birthtime = m;
  765. entry->ae_stat.aest_birthtime_nsec = ns;
  766. }
  767. void
  768. archive_entry_unset_birthtime(struct archive_entry *entry)
  769. {
  770. archive_entry_set_birthtime(entry, 0, 0);
  771. entry->ae_set &= ~AE_SET_BIRTHTIME;
  772. }
  773. void
  774. archive_entry_set_ctime(struct archive_entry *entry, time_t t, long ns)
  775. {
  776. entry->stat_valid = 0;
  777. entry->ae_set |= AE_SET_CTIME;
  778. entry->ae_stat.aest_ctime = t;
  779. entry->ae_stat.aest_ctime_nsec = ns;
  780. }
  781. void
  782. archive_entry_unset_ctime(struct archive_entry *entry)
  783. {
  784. archive_entry_set_ctime(entry, 0, 0);
  785. entry->ae_set &= ~AE_SET_CTIME;
  786. }
  787. void
  788. archive_entry_set_dev(struct archive_entry *entry, dev_t d)
  789. {
  790. entry->stat_valid = 0;
  791. entry->ae_stat.aest_dev_is_broken_down = 0;
  792. entry->ae_stat.aest_dev = d;
  793. }
  794. void
  795. archive_entry_set_devmajor(struct archive_entry *entry, dev_t m)
  796. {
  797. entry->stat_valid = 0;
  798. entry->ae_stat.aest_dev_is_broken_down = 1;
  799. entry->ae_stat.aest_devmajor = m;
  800. }
  801. void
  802. archive_entry_set_devminor(struct archive_entry *entry, dev_t m)
  803. {
  804. entry->stat_valid = 0;
  805. entry->ae_stat.aest_dev_is_broken_down = 1;
  806. entry->ae_stat.aest_devminor = m;
  807. }
  808. /* Set symlink if symlink is already set, else set hardlink. */
  809. void
  810. archive_entry_set_link(struct archive_entry *entry, const char *target)
  811. {
  812. if (entry->ae_set & AE_SET_SYMLINK)
  813. aes_set_mbs(&entry->ae_symlink, target);
  814. else
  815. aes_set_mbs(&entry->ae_hardlink, target);
  816. }
  817. /* Set symlink if symlink is already set, else set hardlink. */
  818. void
  819. archive_entry_copy_link(struct archive_entry *entry, const char *target)
  820. {
  821. if (entry->ae_set & AE_SET_SYMLINK)
  822. aes_copy_mbs(&entry->ae_symlink, target);
  823. else
  824. aes_copy_mbs(&entry->ae_hardlink, target);
  825. }
  826. /* Set symlink if symlink is already set, else set hardlink. */
  827. void
  828. archive_entry_copy_link_w(struct archive_entry *entry, const wchar_t *target)
  829. {
  830. if (entry->ae_set & AE_SET_SYMLINK)
  831. aes_copy_wcs(&entry->ae_symlink, target);
  832. else
  833. aes_copy_wcs(&entry->ae_hardlink, target);
  834. }
  835. int
  836. archive_entry_update_link_utf8(struct archive_entry *entry, const char *target)
  837. {
  838. if (entry->ae_set & AE_SET_SYMLINK)
  839. return (aes_update_utf8(&entry->ae_symlink, target));
  840. else
  841. return (aes_update_utf8(&entry->ae_hardlink, target));
  842. }
  843. void
  844. archive_entry_set_mode(struct archive_entry *entry, mode_t m)
  845. {
  846. entry->stat_valid = 0;
  847. entry->ae_stat.aest_mode = m;
  848. }
  849. void
  850. archive_entry_set_mtime(struct archive_entry *entry, time_t m, long ns)
  851. {
  852. entry->stat_valid = 0;
  853. entry->ae_set |= AE_SET_MTIME;
  854. entry->ae_stat.aest_mtime = m;
  855. entry->ae_stat.aest_mtime_nsec = ns;
  856. }
  857. void
  858. archive_entry_unset_mtime(struct archive_entry *entry)
  859. {
  860. archive_entry_set_mtime(entry, 0, 0);
  861. entry->ae_set &= ~AE_SET_MTIME;
  862. }
  863. void
  864. archive_entry_set_nlink(struct archive_entry *entry, unsigned int nlink)
  865. {
  866. entry->stat_valid = 0;
  867. entry->ae_stat.aest_nlink = nlink;
  868. }
  869. void
  870. archive_entry_set_pathname(struct archive_entry *entry, const char *name)
  871. {
  872. aes_set_mbs(&entry->ae_pathname, name);
  873. }
  874. void
  875. archive_entry_copy_pathname(struct archive_entry *entry, const char *name)
  876. {
  877. aes_copy_mbs(&entry->ae_pathname, name);
  878. }
  879. void
  880. archive_entry_copy_pathname_w(struct archive_entry *entry, const wchar_t *name)
  881. {
  882. aes_copy_wcs(&entry->ae_pathname, name);
  883. }
  884. int
  885. archive_entry_update_pathname_utf8(struct archive_entry *entry, const char *name)
  886. {
  887. return (aes_update_utf8(&entry->ae_pathname, name));
  888. }
  889. void
  890. archive_entry_set_perm(struct archive_entry *entry, mode_t p)
  891. {
  892. entry->stat_valid = 0;
  893. entry->ae_stat.aest_mode &= AE_IFMT;
  894. entry->ae_stat.aest_mode |= ~AE_IFMT & p;
  895. }
  896. void
  897. archive_entry_set_rdev(struct archive_entry *entry, dev_t m)
  898. {
  899. entry->stat_valid = 0;
  900. entry->ae_stat.aest_rdev = m;
  901. entry->ae_stat.aest_rdev_is_broken_down = 0;
  902. }
  903. void
  904. archive_entry_set_rdevmajor(struct archive_entry *entry, dev_t m)
  905. {
  906. entry->stat_valid = 0;
  907. entry->ae_stat.aest_rdev_is_broken_down = 1;
  908. entry->ae_stat.aest_rdevmajor = m;
  909. }
  910. void
  911. archive_entry_set_rdevminor(struct archive_entry *entry, dev_t m)
  912. {
  913. entry->stat_valid = 0;
  914. entry->ae_stat.aest_rdev_is_broken_down = 1;
  915. entry->ae_stat.aest_rdevminor = m;
  916. }
  917. void
  918. archive_entry_set_size(struct archive_entry *entry, int64_t s)
  919. {
  920. entry->stat_valid = 0;
  921. entry->ae_stat.aest_size = s;
  922. entry->ae_set |= AE_SET_SIZE;
  923. }
  924. void
  925. archive_entry_unset_size(struct archive_entry *entry)
  926. {
  927. archive_entry_set_size(entry, 0);
  928. entry->ae_set &= ~AE_SET_SIZE;
  929. }
  930. void
  931. archive_entry_copy_sourcepath(struct archive_entry *entry, const char *path)
  932. {
  933. aes_set_mbs(&entry->ae_sourcepath, path);
  934. }
  935. void
  936. archive_entry_set_symlink(struct archive_entry *entry, const char *linkname)
  937. {
  938. aes_set_mbs(&entry->ae_symlink, linkname);
  939. if (linkname != NULL)
  940. entry->ae_set |= AE_SET_SYMLINK;
  941. else
  942. entry->ae_set &= ~AE_SET_SYMLINK;
  943. }
  944. void
  945. archive_entry_copy_symlink(struct archive_entry *entry, const char *linkname)
  946. {
  947. aes_copy_mbs(&entry->ae_symlink, linkname);
  948. if (linkname != NULL)
  949. entry->ae_set |= AE_SET_SYMLINK;
  950. else
  951. entry->ae_set &= ~AE_SET_SYMLINK;
  952. }
  953. void
  954. archive_entry_copy_symlink_w(struct archive_entry *entry, const wchar_t *linkname)
  955. {
  956. aes_copy_wcs(&entry->ae_symlink, linkname);
  957. if (linkname != NULL)
  958. entry->ae_set |= AE_SET_SYMLINK;
  959. else
  960. entry->ae_set &= ~AE_SET_SYMLINK;
  961. }
  962. void
  963. archive_entry_set_uid(struct archive_entry *entry, uid_t u)
  964. {
  965. entry->stat_valid = 0;
  966. entry->ae_stat.aest_uid = u;
  967. }
  968. void
  969. archive_entry_set_uname(struct archive_entry *entry, const char *name)
  970. {
  971. aes_set_mbs(&entry->ae_uname, name);
  972. }
  973. void
  974. archive_entry_copy_uname(struct archive_entry *entry, const char *name)
  975. {
  976. aes_copy_mbs(&entry->ae_uname, name);
  977. }
  978. void
  979. archive_entry_copy_uname_w(struct archive_entry *entry, const wchar_t *name)
  980. {
  981. aes_copy_wcs(&entry->ae_uname, name);
  982. }
  983. int
  984. archive_entry_update_uname_utf8(struct archive_entry *entry, const char *name)
  985. {
  986. return (aes_update_utf8(&entry->ae_uname, name));
  987. }
  988. /*
  989. * ACL management. The following would, of course, be a lot simpler
  990. * if: 1) the last draft of POSIX.1e were a really thorough and
  991. * complete standard that addressed the needs of ACL archiving and 2)
  992. * everyone followed it faithfully. Alas, neither is true, so the
  993. * following is a lot more complex than might seem necessary to the
  994. * uninitiated.
  995. */
  996. void
  997. archive_entry_acl_clear(struct archive_entry *entry)
  998. {
  999. struct ae_acl *ap;
  1000. while (entry->acl_head != NULL) {
  1001. ap = entry->acl_head->next;
  1002. aes_clean(&entry->acl_head->name);
  1003. free(entry->acl_head);
  1004. entry->acl_head = ap;
  1005. }
  1006. if (entry->acl_text_w != NULL) {
  1007. free(entry->acl_text_w);
  1008. entry->acl_text_w = NULL;
  1009. }
  1010. entry->acl_p = NULL;
  1011. entry->acl_state = 0; /* Not counting. */
  1012. }
  1013. /*
  1014. * Add a single ACL entry to the internal list of ACL data.
  1015. */
  1016. void
  1017. archive_entry_acl_add_entry(struct archive_entry *entry,
  1018. int type, int permset, int tag, int id, const char *name)
  1019. {
  1020. struct ae_acl *ap;
  1021. if (acl_special(entry, type, permset, tag) == 0)
  1022. return;
  1023. ap = acl_new_entry(entry, type, permset, tag, id);
  1024. if (ap == NULL) {
  1025. /* XXX Error XXX */
  1026. return;
  1027. }
  1028. if (name != NULL && *name != '\0')
  1029. aes_copy_mbs(&ap->name, name);
  1030. else
  1031. aes_clean(&ap->name);
  1032. }
  1033. /*
  1034. * As above, but with a wide-character name.
  1035. */
  1036. void
  1037. archive_entry_acl_add_entry_w(struct archive_entry *entry,
  1038. int type, int permset, int tag, int id, const wchar_t *name)
  1039. {
  1040. archive_entry_acl_add_entry_w_len(entry, type, permset, tag, id, name, wcslen(name));
  1041. }
  1042. void
  1043. archive_entry_acl_add_entry_w_len(struct archive_entry *entry,
  1044. int type, int permset, int tag, int id, const wchar_t *name, size_t len)
  1045. {
  1046. struct ae_acl *ap;
  1047. if (acl_special(entry, type, permset, tag) == 0)
  1048. return;
  1049. ap = acl_new_entry(entry, type, permset, tag, id);
  1050. if (ap == NULL) {
  1051. /* XXX Error XXX */
  1052. return;
  1053. }
  1054. if (name != NULL && *name != L'\0' && len > 0)
  1055. aes_copy_wcs_len(&ap->name, name, len);
  1056. else
  1057. aes_clean(&ap->name);
  1058. }
  1059. /*
  1060. * If this ACL entry is part of the standard POSIX permissions set,
  1061. * store the permissions in the stat structure and return zero.
  1062. */
  1063. static int
  1064. acl_special(struct archive_entry *entry, int type, int permset, int tag)
  1065. {
  1066. if (type == ARCHIVE_ENTRY_ACL_TYPE_ACCESS) {
  1067. switch (tag) {
  1068. case ARCHIVE_ENTRY_ACL_USER_OBJ:
  1069. entry->ae_stat.aest_mode &= ~0700;
  1070. entry->ae_stat.aest_mode |= (permset & 7) << 6;
  1071. return (0);
  1072. case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
  1073. entry->ae_stat.aest_mode &= ~0070;
  1074. entry->ae_stat.aest_mode |= (permset & 7) << 3;
  1075. return (0);
  1076. case ARCHIVE_ENTRY_ACL_OTHER:
  1077. entry->ae_stat.aest_mode &= ~0007;
  1078. entry->ae_stat.aest_mode |= permset & 7;
  1079. return (0);
  1080. }
  1081. }
  1082. return (1);
  1083. }
  1084. /*
  1085. * Allocate and populate a new ACL entry with everything but the
  1086. * name.
  1087. */
  1088. static struct ae_acl *
  1089. acl_new_entry(struct archive_entry *entry,
  1090. int type, int permset, int tag, int id)
  1091. {
  1092. struct ae_acl *ap;
  1093. if (type != ARCHIVE_ENTRY_ACL_TYPE_ACCESS &&
  1094. type != ARCHIVE_ENTRY_ACL_TYPE_DEFAULT)
  1095. return (NULL);
  1096. if (entry->acl_text_w != NULL) {
  1097. free(entry->acl_text_w);
  1098. entry->acl_text_w = NULL;
  1099. }
  1100. /* XXX TODO: More sanity-checks on the arguments XXX */
  1101. /* If there's a matching entry already in the list, overwrite it. */
  1102. for (ap = entry->acl_head; ap != NULL; ap = ap->next) {
  1103. if (ap->type == type && ap->tag == tag && ap->id == id) {
  1104. ap->permset = permset;
  1105. return (ap);
  1106. }
  1107. }
  1108. /* Add a new entry to the list. */
  1109. ap = (struct ae_acl *)malloc(sizeof(*ap));
  1110. if (ap == NULL)
  1111. return (NULL);
  1112. memset(ap, 0, sizeof(*ap));
  1113. ap->next = entry->acl_head;
  1114. entry->acl_head = ap;
  1115. ap->type = type;
  1116. ap->tag = tag;
  1117. ap->id = id;
  1118. ap->permset = permset;
  1119. return (ap);
  1120. }
  1121. /*
  1122. * Return a count of entries matching "want_type".
  1123. */
  1124. int
  1125. archive_entry_acl_count(struct archive_entry *entry, int want_type)
  1126. {
  1127. int count;
  1128. struct ae_acl *ap;
  1129. count = 0;
  1130. ap = entry->acl_head;
  1131. while (ap != NULL) {
  1132. if ((ap->type & want_type) != 0)
  1133. count++;
  1134. ap = ap->next;
  1135. }
  1136. if (count > 0 && ((want_type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0))
  1137. count += 3;
  1138. return (count);
  1139. }
  1140. /*
  1141. * Prepare for reading entries from the ACL data. Returns a count
  1142. * of entries matching "want_type", or zero if there are no
  1143. * non-extended ACL entries of that type.
  1144. */
  1145. int
  1146. archive_entry_acl_reset(struct archive_entry *entry, int want_type)
  1147. {
  1148. int count, cutoff;
  1149. count = archive_entry_acl_count(entry, want_type);
  1150. /*
  1151. * If the only entries are the three standard ones,
  1152. * then don't return any ACL data. (In this case,
  1153. * client can just use chmod(2) to set permissions.)
  1154. */
  1155. if ((want_type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0)
  1156. cutoff = 3;
  1157. else
  1158. cutoff = 0;
  1159. if (count > cutoff)
  1160. entry->acl_state = ARCHIVE_ENTRY_ACL_USER_OBJ;
  1161. else
  1162. entry->acl_state = 0;
  1163. entry->acl_p = entry->acl_head;
  1164. return (count);
  1165. }
  1166. /*
  1167. * Return the next ACL entry in the list. Fake entries for the
  1168. * standard permissions and include them in the returned list.
  1169. */
  1170. int
  1171. archive_entry_acl_next(struct archive_entry *entry, int want_type, int *type,
  1172. int *permset, int *tag, int *id, const char **name)
  1173. {
  1174. *name = NULL;
  1175. *id = -1;
  1176. /*
  1177. * The acl_state is either zero (no entries available), -1
  1178. * (reading from list), or an entry type (retrieve that type
  1179. * from ae_stat.aest_mode).
  1180. */
  1181. if (entry->acl_state == 0)
  1182. return (ARCHIVE_WARN);
  1183. /* The first three access entries are special. */
  1184. if ((want_type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0) {
  1185. switch (entry->acl_state) {
  1186. case ARCHIVE_ENTRY_ACL_USER_OBJ:
  1187. *permset = (entry->ae_stat.aest_mode >> 6) & 7;
  1188. *type = ARCHIVE_ENTRY_ACL_TYPE_ACCESS;
  1189. *tag = ARCHIVE_ENTRY_ACL_USER_OBJ;
  1190. entry->acl_state = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
  1191. return (ARCHIVE_OK);
  1192. case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
  1193. *permset = (entry->ae_stat.aest_mode >> 3) & 7;
  1194. *type = ARCHIVE_ENTRY_ACL_TYPE_ACCESS;
  1195. *tag = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
  1196. entry->acl_state = ARCHIVE_ENTRY_ACL_OTHER;
  1197. return (ARCHIVE_OK);
  1198. case ARCHIVE_ENTRY_ACL_OTHER:
  1199. *permset = entry->ae_stat.aest_mode & 7;
  1200. *type = ARCHIVE_ENTRY_ACL_TYPE_ACCESS;
  1201. *tag = ARCHIVE_ENTRY_ACL_OTHER;
  1202. entry->acl_state = -1;
  1203. entry->acl_p = entry->acl_head;
  1204. return (ARCHIVE_OK);
  1205. default:
  1206. break;
  1207. }
  1208. }
  1209. while (entry->acl_p != NULL && (entry->acl_p->type & want_type) == 0)
  1210. entry->acl_p = entry->acl_p->next;
  1211. if (entry->acl_p == NULL) {
  1212. entry->acl_state = 0;
  1213. *type = 0;
  1214. *permset = 0;
  1215. *tag = 0;
  1216. *id = -1;
  1217. *name = NULL;
  1218. return (ARCHIVE_EOF); /* End of ACL entries. */
  1219. }
  1220. *type = entry->acl_p->type;
  1221. *permset = entry->acl_p->permset;
  1222. *tag = entry->acl_p->tag;
  1223. *id = entry->acl_p->id;
  1224. *name = aes_get_mbs(&entry->acl_p->name);
  1225. entry->acl_p = entry->acl_p->next;
  1226. return (ARCHIVE_OK);
  1227. }
  1228. /*
  1229. * Generate a text version of the ACL. The flags parameter controls
  1230. * the style of the generated ACL.
  1231. */
  1232. const wchar_t *
  1233. archive_entry_acl_text_w(struct archive_entry *entry, int flags)
  1234. {
  1235. int count;
  1236. size_t length;
  1237. const wchar_t *wname;
  1238. const wchar_t *prefix;
  1239. wchar_t separator;
  1240. struct ae_acl *ap;
  1241. int id;
  1242. wchar_t *wp;
  1243. if (entry->acl_text_w != NULL) {
  1244. free (entry->acl_text_w);
  1245. entry->acl_text_w = NULL;
  1246. }
  1247. separator = L',';
  1248. count = 0;
  1249. length = 0;
  1250. ap = entry->acl_head;
  1251. while (ap != NULL) {
  1252. if ((ap->type & flags) != 0) {
  1253. count++;
  1254. if ((flags & ARCHIVE_ENTRY_ACL_STYLE_MARK_DEFAULT) &&
  1255. (ap->type & ARCHIVE_ENTRY_ACL_TYPE_DEFAULT))
  1256. length += 8; /* "default:" */
  1257. length += 5; /* tag name */
  1258. length += 1; /* colon */
  1259. wname = aes_get_wcs(&ap->name);
  1260. if (wname != NULL)
  1261. length += wcslen(wname);
  1262. else
  1263. length += sizeof(uid_t) * 3 + 1;
  1264. length ++; /* colon */
  1265. length += 3; /* rwx */
  1266. length += 1; /* colon */
  1267. length += max(sizeof(uid_t), sizeof(gid_t)) * 3 + 1;
  1268. length ++; /* newline */
  1269. }
  1270. ap = ap->next;
  1271. }
  1272. if (count > 0 && ((flags & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0)) {
  1273. length += 10; /* "user::rwx\n" */
  1274. length += 11; /* "group::rwx\n" */
  1275. length += 11; /* "other::rwx\n" */
  1276. }
  1277. if (count == 0)
  1278. return (NULL);
  1279. /* Now, allocate the string and actually populate it. */
  1280. wp = entry->acl_text_w = (wchar_t *)malloc(length * sizeof(wchar_t));
  1281. if (wp == NULL)
  1282. __archive_errx(1, "No memory to generate the text version of the ACL");
  1283. count = 0;
  1284. if ((flags & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0) {
  1285. append_entry_w(&wp, NULL, ARCHIVE_ENTRY_ACL_USER_OBJ, NULL,
  1286. entry->ae_stat.aest_mode & 0700, -1);
  1287. *wp++ = ',';
  1288. append_entry_w(&wp, NULL, ARCHIVE_ENTRY_ACL_GROUP_OBJ, NULL,
  1289. entry->ae_stat.aest_mode & 0070, -1);
  1290. *wp++ = ',';
  1291. append_entry_w(&wp, NULL, ARCHIVE_ENTRY_ACL_OTHER, NULL,
  1292. entry->ae_stat.aest_mode & 0007, -1);
  1293. count += 3;
  1294. ap = entry->acl_head;
  1295. while (ap != NULL) {
  1296. if ((ap->type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0) {
  1297. wname = aes_get_wcs(&ap->name);
  1298. *wp++ = separator;
  1299. if (flags & ARCHIVE_ENTRY_ACL_STYLE_EXTRA_ID)
  1300. id = ap->id;
  1301. else
  1302. id = -1;
  1303. append_entry_w(&wp, NULL, ap->tag, wname,
  1304. ap->permset, id);
  1305. count++;
  1306. }
  1307. ap = ap->next;
  1308. }
  1309. }
  1310. if ((flags & ARCHIVE_ENTRY_ACL_TYPE_DEFAULT) != 0) {
  1311. if (flags & ARCHIVE_ENTRY_ACL_STYLE_MARK_DEFAULT)
  1312. prefix = L"default:";
  1313. else
  1314. prefix = NULL;
  1315. ap = entry->acl_head;
  1316. count = 0;
  1317. while (ap != NULL) {
  1318. if ((ap->type & ARCHIVE_ENTRY_ACL_TYPE_DEFAULT) != 0) {
  1319. wname = aes_get_wcs(&ap->name);
  1320. if (count > 0)
  1321. *wp++ = separator;
  1322. if (flags & ARCHIVE_ENTRY_ACL_STYLE_EXTRA_ID)
  1323. id = ap->id;
  1324. else
  1325. id = -1;
  1326. append_entry_w(&wp, prefix, ap->tag,
  1327. wname, ap->permset, id);
  1328. count ++;
  1329. }
  1330. ap = ap->next;
  1331. }
  1332. }
  1333. return (entry->acl_text_w);
  1334. }
  1335. static void
  1336. append_id_w(wchar_t **wp, int id)
  1337. {
  1338. if (id < 0)
  1339. id = 0;
  1340. if (id > 9)
  1341. append_id_w(wp, id / 10);
  1342. *(*wp)++ = L"0123456789"[id % 10];
  1343. }
  1344. static void
  1345. append_entry_w(wchar_t **wp, const wchar_t *prefix, int tag,
  1346. const wchar_t *wname, int perm, int id)
  1347. {
  1348. if (prefix != NULL) {
  1349. wcscpy(*wp, prefix);
  1350. *wp += wcslen(*wp);
  1351. }
  1352. switch (tag) {
  1353. case ARCHIVE_ENTRY_ACL_USER_OBJ:
  1354. wname = NULL;
  1355. id = -1;
  1356. /* FALLTHROUGH */
  1357. case ARCHIVE_ENTRY_ACL_USER:
  1358. wcscpy(*wp, L"user");
  1359. break;
  1360. case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
  1361. wname = NULL;
  1362. id = -1;
  1363. /* FALLTHROUGH */
  1364. case ARCHIVE_ENTRY_ACL_GROUP:
  1365. wcscpy(*wp, L"group");
  1366. break;
  1367. case ARCHIVE_ENTRY_ACL_MASK:
  1368. wcscpy(*wp, L"mask");
  1369. wname = NULL;
  1370. id = -1;
  1371. break;
  1372. case ARCHIVE_ENTRY_ACL_OTHER:
  1373. wcscpy(*wp, L"other");
  1374. wname = NULL;
  1375. id = -1;
  1376. break;
  1377. }
  1378. *wp += wcslen(*wp);
  1379. *(*wp)++ = L':';
  1380. if (wname != NULL) {
  1381. wcscpy(*wp, wname);
  1382. *wp += wcslen(*wp);
  1383. } else if (tag == ARCHIVE_ENTRY_ACL_USER
  1384. || tag == ARCHIVE_ENTRY_ACL_GROUP) {
  1385. append_id_w(wp, id);
  1386. id = -1;
  1387. }
  1388. *(*wp)++ = L':';
  1389. *(*wp)++ = (perm & 0444) ? L'r' : L'-';
  1390. *(*wp)++ = (perm & 0222) ? L'w' : L'-';
  1391. *(*wp)++ = (perm & 0111) ? L'x' : L'-';
  1392. if (id != -1) {
  1393. *(*wp)++ = L':';
  1394. append_id_w(wp, id);
  1395. }
  1396. **wp = L'\0';
  1397. }
  1398. /*
  1399. * Parse a textual ACL. This automatically recognizes and supports
  1400. * extensions described above. The 'type' argument is used to
  1401. * indicate the type that should be used for any entries not
  1402. * explicitly marked as "default:".
  1403. */
  1404. int
  1405. __archive_entry_acl_parse_w(struct archive_entry *entry,
  1406. const wchar_t *text, int default_type)
  1407. {
  1408. struct {
  1409. const wchar_t *start;
  1410. const wchar_t *end;
  1411. } field[4];
  1412. int fields;
  1413. int type, tag, permset, id;
  1414. const wchar_t *p;
  1415. wchar_t sep;
  1416. while (text != NULL && *text != L'\0') {
  1417. /*
  1418. * Parse the fields out of the next entry,
  1419. * advance 'text' to start of next entry.
  1420. */
  1421. fields = 0;
  1422. do {
  1423. const wchar_t *start, *end;
  1424. next_field_w(&text, &start, &end, &sep);
  1425. if (fields < 4) {
  1426. field[fields].start = start;
  1427. field[fields].end = end;
  1428. }
  1429. ++fields;
  1430. } while (sep == L':');
  1431. if (fields < 3)
  1432. return (ARCHIVE_WARN);
  1433. /* Check for a numeric ID in field 1 or 3. */
  1434. id = -1;
  1435. isint_w(field[1].start, field[1].end, &id);
  1436. /* Field 3 is optional. */
  1437. if (id == -1 && fields > 3)
  1438. isint_w(field[3].start, field[3].end, &id);
  1439. /* Parse the permissions from field 2. */
  1440. permset = 0;
  1441. p = field[2].start;
  1442. while (p < field[2].end) {
  1443. switch (*p++) {
  1444. case 'r': case 'R':
  1445. permset |= ARCHIVE_ENTRY_ACL_READ;
  1446. break;
  1447. case 'w': case 'W':
  1448. permset |= ARCHIVE_ENTRY_ACL_WRITE;
  1449. break;
  1450. case 'x': case 'X':
  1451. permset |= ARCHIVE_ENTRY_ACL_EXECUTE;
  1452. break;
  1453. case '-':
  1454. break;
  1455. default:
  1456. return (ARCHIVE_WARN);
  1457. }
  1458. }
  1459. /*
  1460. * Solaris extension: "defaultuser::rwx" is the
  1461. * default ACL corresponding to "user::rwx", etc.
  1462. */
  1463. if (field[0].end-field[0].start > 7
  1464. && wmemcmp(field[0].start, L"default", 7) == 0) {
  1465. type = ARCHIVE_ENTRY_ACL_TYPE_DEFAULT;
  1466. field[0].start += 7;
  1467. } else
  1468. type = default_type;
  1469. if (prefix_w(field[0].start, field[0].end, L"user")) {
  1470. if (id != -1 || field[1].start < field[1].end)
  1471. tag = ARCHIVE_ENTRY_ACL_USER;
  1472. else
  1473. tag = ARCHIVE_ENTRY_ACL_USER_OBJ;
  1474. } else if (prefix_w(field[0].start, field[0].end, L"group")) {
  1475. if (id != -1 || field[1].start < field[1].end)
  1476. tag = ARCHIVE_ENTRY_ACL_GROUP;
  1477. else
  1478. tag = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
  1479. } else if (prefix_w(field[0].start, field[0].end, L"other")) {
  1480. if (id != -1 || field[1].start < field[1].end)
  1481. return (ARCHIVE_WARN);
  1482. tag = ARCHIVE_ENTRY_ACL_OTHER;
  1483. } else if (prefix_w(field[0].start, field[0].end, L"mask")) {
  1484. if (id != -1 || field[1].start < field[1].end)
  1485. return (ARCHIVE_WARN);
  1486. tag = ARCHIVE_ENTRY_ACL_MASK;
  1487. } else
  1488. return (ARCHIVE_WARN);
  1489. /* Add entry to the internal list. */
  1490. archive_entry_acl_add_entry_w_len(entry, type, permset,
  1491. tag, id, field[1].start, field[1].end - field[1].start);
  1492. }
  1493. return (ARCHIVE_OK);
  1494. }
  1495. /*
  1496. * extended attribute handling
  1497. */
  1498. void
  1499. archive_entry_xattr_clear(struct archive_entry *entry)
  1500. {
  1501. struct ae_xattr *xp;
  1502. while (entry->xattr_head != NULL) {
  1503. xp = entry->xattr_head->next;
  1504. free(entry->xattr_head->name);
  1505. free(entry->xattr_head->value);
  1506. free(entry->xattr_head);
  1507. entry->xattr_head = xp;
  1508. }
  1509. entry->xattr_head = NULL;
  1510. }
  1511. void
  1512. archive_entry_xattr_add_entry(struct archive_entry *entry,
  1513. const char *name, const void *value, size_t size)
  1514. {
  1515. struct ae_xattr *xp;
  1516. if ((xp = (struct ae_xattr *)malloc(sizeof(struct ae_xattr))) == NULL)
  1517. __archive_errx(1, "Out of memory");
  1518. if ((xp->name = strdup(name)) == NULL)
  1519. __archive_errx(1, "Out of memory");
  1520. if ((xp->value = malloc(size)) != NULL) {
  1521. memcpy(xp->value, value, size);
  1522. xp->size = size;
  1523. } else
  1524. xp->size = 0;
  1525. xp->next = entry->xattr_head;
  1526. entry->xattr_head = xp;
  1527. }
  1528. /*
  1529. * returns number of the extended attribute entries
  1530. */
  1531. int
  1532. archive_entry_xattr_count(struct archive_entry *entry)
  1533. {
  1534. struct ae_xattr *xp;
  1535. int count = 0;
  1536. for (xp = entry->xattr_head; xp != NULL; xp = xp->next)
  1537. count++;
  1538. return count;
  1539. }
  1540. int
  1541. archive_entry_xattr_reset(struct archive_entry * entry)
  1542. {
  1543. entry->xattr_p = entry->xattr_head;
  1544. return archive_entry_xattr_count(entry);
  1545. }
  1546. int
  1547. archive_entry_xattr_next(struct archive_entry * entry,
  1548. const char **name, const void **value, size_t *size)
  1549. {
  1550. if (entry->xattr_p) {
  1551. *name = entry->xattr_p->name;
  1552. *value = entry->xattr_p->value;
  1553. *size = entry->xattr_p->size;
  1554. entry->xattr_p = entry->xattr_p->next;
  1555. return (ARCHIVE_OK);
  1556. } else {
  1557. *name = NULL;
  1558. *value = NULL;
  1559. *size = (size_t)0;
  1560. return (ARCHIVE_WARN);
  1561. }
  1562. }
  1563. /*
  1564. * end of xattr handling
  1565. */
  1566. /*
  1567. * Parse a string to a positive decimal integer. Returns true if
  1568. * the string is non-empty and consists only of decimal digits,
  1569. * false otherwise.
  1570. */
  1571. static int
  1572. isint_w(const wchar_t *start, const wchar_t *end, int *result)
  1573. {
  1574. int n = 0;
  1575. if (start >= end)
  1576. return (0);
  1577. while (start < end) {
  1578. if (*start < '0' || *start > '9')
  1579. return (0);
  1580. if (n > (INT_MAX / 10))
  1581. n = INT_MAX;
  1582. else {
  1583. n *= 10;
  1584. n += *start - '0';
  1585. }
  1586. start++;
  1587. }
  1588. *result = n;
  1589. return (1);
  1590. }
  1591. /*
  1592. * Match "[:whitespace:]*(.*)[:whitespace:]*[:,\n]". *wp is updated
  1593. * to point to just after the separator. *start points to the first
  1594. * character of the matched text and *end just after the last
  1595. * character of the matched identifier. In particular *end - *start
  1596. * is the length of the field body, not including leading or trailing
  1597. * whitespace.
  1598. */
  1599. static void
  1600. next_field_w(const wchar_t **wp, const wchar_t **start,
  1601. const wchar_t **end, wchar_t *sep)
  1602. {
  1603. /* Skip leading whitespace to find start of field. */
  1604. while (**wp == L' ' || **wp == L'\t' || **wp == L'\n') {
  1605. (*wp)++;
  1606. }
  1607. *start = *wp;
  1608. /* Scan for the separator. */
  1609. while (**wp != L'\0' && **wp != L',' && **wp != L':' &&
  1610. **wp != L'\n') {
  1611. (*wp)++;
  1612. }
  1613. *sep = **wp;
  1614. /* Trim trailing whitespace to locate end of field. */
  1615. *end = *wp - 1;
  1616. while (**end == L' ' || **end == L'\t' || **end == L'\n') {
  1617. (*end)--;
  1618. }
  1619. (*end)++;
  1620. /* Adjust scanner location. */
  1621. if (**wp != L'\0')
  1622. (*wp)++;
  1623. }
  1624. /*
  1625. * Return true if the characters [start...end) are a prefix of 'test'.
  1626. * This makes it easy to handle the obvious abbreviations: 'u' for 'user', etc.
  1627. */
  1628. static int
  1629. prefix_w(const wchar_t *start, const wchar_t *end, const wchar_t *test)
  1630. {
  1631. if (start == end)
  1632. return (0);
  1633. if (*start++ != *test++)
  1634. return (0);
  1635. while (start < end && *start++ == *test++)
  1636. ;
  1637. if (start < end)
  1638. return (0);
  1639. return (1);
  1640. }
  1641. /*
  1642. * Following code is modified from UC Berkeley sources, and
  1643. * is subject to the following copyright notice.
  1644. */
  1645. /*-
  1646. * Copyright (c) 1993
  1647. * The Regents of the University of California. All rights reserved.
  1648. *
  1649. * Redistribution and use in source and binary forms, with or without
  1650. * modification, are permitted provided that the following conditions
  1651. * are met:
  1652. * 1. Redistributions of source code must retain the above copyright
  1653. * notice, this list of conditions and the following disclaimer.
  1654. * 2. Redistributions in binary form must reproduce the above copyright
  1655. * notice, this list of conditions and the following disclaimer in the
  1656. * documentation and/or other materials provided with the distribution.
  1657. * 4. Neither the name of the University nor the names of its contributors
  1658. * may be used to endorse or promote products derived from this software
  1659. * without specific prior written permission.
  1660. *
  1661. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  1662. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  1663. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  1664. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  1665. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  1666. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  1667. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  1668. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  1669. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  1670. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  1671. * SUCH DAMAGE.
  1672. */
  1673. static struct flag {
  1674. const char *name;
  1675. const wchar_t *wname;
  1676. unsigned long set;
  1677. unsigned long clear;
  1678. } flags[] = {
  1679. /* Preferred (shorter) names per flag first, all prefixed by "no" */
  1680. #ifdef SF_APPEND
  1681. { "nosappnd", L"nosappnd", SF_APPEND, 0 },
  1682. { "nosappend", L"nosappend", SF_APPEND, 0 },
  1683. #endif
  1684. #ifdef EXT2_APPEND_FL /* 'a' */
  1685. { "nosappnd", L"nosappnd", EXT2_APPEND_FL, 0 },
  1686. { "nosappend", L"nosappend", EXT2_APPEND_FL, 0 },
  1687. #endif
  1688. #ifdef SF_ARCHIVED
  1689. { "noarch", L"noarch", SF_ARCHIVED, 0 },
  1690. { "noarchived", L"noarchived", SF_ARCHIVED, 0 },
  1691. #endif
  1692. #ifdef SF_IMMUTABLE
  1693. { "noschg", L"noschg", SF_IMMUTABLE, 0 },
  1694. { "noschange", L"noschange", SF_IMMUTABLE, 0 },
  1695. { "nosimmutable", L"nosimmutable", SF_IMMUTABLE, 0 },
  1696. #endif
  1697. #ifdef EXT2_IMMUTABLE_FL /* 'i' */
  1698. { "noschg", L"noschg", EXT2_IMMUTABLE_FL, 0 },
  1699. { "noschange", L"noschange", EXT2_IMMUTABLE_FL, 0 },
  1700. { "nosimmutable", L"nosimmutable", EXT2_IMMUTABLE_FL, 0 },
  1701. #endif
  1702. #ifdef SF_NOUNLINK
  1703. { "nosunlnk", L"nosunlnk", SF_NOUNLINK, 0 },
  1704. { "nosunlink", L"nosunlink", SF_NOUNLINK, 0 },
  1705. #endif
  1706. #ifdef SF_SNAPSHOT
  1707. { "nosnapshot", L"nosnapshot", SF_SNAPSHOT, 0 },
  1708. #endif
  1709. #ifdef UF_APPEND
  1710. { "nouappnd", L"nouappnd", UF_APPEND, 0 },
  1711. { "nouappend", L"nouappend", UF_APPEND, 0 },
  1712. #endif
  1713. #ifdef UF_IMMUTABLE
  1714. { "nouchg", L"nouchg", UF_IMMUTABLE, 0 },
  1715. { "nouchange", L"nouchange", UF_IMMUTABLE, 0 },
  1716. { "nouimmutable", L"nouimmutable", UF_IMMUTABLE, 0 },
  1717. #endif
  1718. #ifdef UF_NODUMP
  1719. { "nodump", L"nodump", 0, UF_NODUMP},
  1720. #endif
  1721. #ifdef EXT2_NODUMP_FL /* 'd' */
  1722. { "nodump", L"nodump", 0, EXT2_NODUMP_FL},
  1723. #endif
  1724. #ifdef UF_OPAQUE
  1725. { "noopaque", L"noopaque", UF_OPAQUE, 0 },
  1726. #endif
  1727. #ifdef UF_NOUNLINK
  1728. { "nouunlnk", L"nouunlnk", UF_NOUNLINK, 0 },
  1729. { "nouunlink", L"nouunlink", UF_NOUNLINK, 0 },
  1730. #endif
  1731. #ifdef EXT2_COMPR_FL /* 'c' */
  1732. { "nocompress", L"nocompress", EXT2_COMPR_FL, 0 },
  1733. #endif
  1734. #ifdef EXT2_NOATIME_FL /* 'A' */
  1735. { "noatime", L"noatime", 0, EXT2_NOATIME_FL},
  1736. #endif
  1737. { NULL, NULL, 0, 0 }
  1738. };
  1739. /*
  1740. * fflagstostr --
  1741. * Convert file flags to a comma-separated string. If no flags
  1742. * are set, return the empty string.
  1743. */
  1744. static char *
  1745. ae_fflagstostr(unsigned long bitset, unsigned long bitclear)
  1746. {
  1747. char *string, *dp;
  1748. const char *sp;
  1749. unsigned long bits;
  1750. struct flag *flag;
  1751. size_t length;
  1752. bits = bitset | bitclear;
  1753. length = 0;
  1754. for (flag = flags; flag->name != NULL; flag++)
  1755. if (bits & (flag->set | flag->clear)) {
  1756. length += strlen(flag->name) + 1;
  1757. bits &= ~(flag->set | flag->clear);
  1758. }
  1759. if (length == 0)
  1760. return (NULL);
  1761. string = (char *)malloc(length);
  1762. if (string == NULL)
  1763. return (NULL);
  1764. dp = string;
  1765. for (flag = flags; flag->name != NULL; flag++) {
  1766. if (bitset & flag->set || bitclear & flag->clear) {
  1767. sp = flag->name + 2;
  1768. } else if (bitset & flag->clear || bitclear & flag->set) {
  1769. sp = flag->name;
  1770. } else
  1771. continue;
  1772. bitset &= ~(flag->set | flag->clear);
  1773. bitclear &= ~(flag->set | flag->clear);
  1774. if (dp > string)
  1775. *dp++ = ',';
  1776. while ((*dp++ = *sp++) != '\0')
  1777. ;
  1778. dp--;
  1779. }
  1780. *dp = '\0';
  1781. return (string);
  1782. }
  1783. /*
  1784. * strtofflags --
  1785. * Take string of arguments and return file flags. This
  1786. * version works a little differently than strtofflags(3).
  1787. * In particular, it always tests every token, skipping any
  1788. * unrecognized tokens. It returns a pointer to the first
  1789. * unrecognized token, or NULL if every token was recognized.
  1790. * This version is also const-correct and does not modify the
  1791. * provided string.
  1792. */
  1793. static const char *
  1794. ae_strtofflags(const char *s, unsigned long *setp, unsigned long *clrp)
  1795. {
  1796. const char *start, *end;
  1797. struct flag *flag;
  1798. unsigned long set, clear;
  1799. const char *failed;
  1800. set = clear = 0;
  1801. start = s;
  1802. failed = NULL;
  1803. /* Find start of first token. */
  1804. while (*start == '\t' || *start == ' ' || *start == ',')
  1805. start++;
  1806. while (*start != '\0') {
  1807. /* Locate end of token. */
  1808. end = start;
  1809. while (*end != '\0' && *end != '\t' &&
  1810. *end != ' ' && *end != ',')
  1811. end++;
  1812. for (flag = flags; flag->name != NULL; flag++) {
  1813. if (memcmp(start, flag->name, end - start) == 0) {
  1814. /* Matched "noXXXX", so reverse the sense. */
  1815. clear |= flag->set;
  1816. set |= flag->clear;
  1817. break;
  1818. } else if (memcmp(start, flag->name + 2, end - start)
  1819. == 0) {
  1820. /* Matched "XXXX", so don't reverse. */
  1821. set |= flag->set;
  1822. clear |= flag->clear;
  1823. break;
  1824. }
  1825. }
  1826. /* Ignore unknown flag names. */
  1827. if (flag->name == NULL && failed == NULL)
  1828. failed = start;
  1829. /* Find start of next token. */
  1830. start = end;
  1831. while (*start == '\t' || *start == ' ' || *start == ',')
  1832. start++;
  1833. }
  1834. if (setp)
  1835. *setp = set;
  1836. if (clrp)
  1837. *clrp = clear;
  1838. /* Return location of first failure. */
  1839. return (failed);
  1840. }
  1841. /*
  1842. * wcstofflags --
  1843. * Take string of arguments and return file flags. This
  1844. * version works a little differently than strtofflags(3).
  1845. * In particular, it always tests every token, skipping any
  1846. * unrecognized tokens. It returns a pointer to the first
  1847. * unrecognized token, or NULL if every token was recognized.
  1848. * This version is also const-correct and does not modify the
  1849. * provided string.
  1850. */
  1851. static const wchar_t *
  1852. ae_wcstofflags(const wchar_t *s, unsigned long *setp, unsigned long *clrp)
  1853. {
  1854. const wchar_t *start, *end;
  1855. struct flag *flag;
  1856. unsigned long set, clear;
  1857. const wchar_t *failed;
  1858. set = clear = 0;
  1859. start = s;
  1860. failed = NULL;
  1861. /* Find start of first token. */
  1862. while (*start == L'\t' || *start == L' ' || *start == L',')
  1863. start++;
  1864. while (*start != L'\0') {
  1865. /* Locate end of token. */
  1866. end = start;
  1867. while (*end != L'\0' && *end != L'\t' &&
  1868. *end != L' ' && *end != L',')
  1869. end++;
  1870. for (flag = flags; flag->wname != NULL; flag++) {
  1871. if (wmemcmp(start, flag->wname, end - start) == 0) {
  1872. /* Matched "noXXXX", so reverse the sense. */
  1873. clear |= flag->set;
  1874. set |= flag->clear;
  1875. break;
  1876. } else if (wmemcmp(start, flag->wname + 2, end - start)
  1877. == 0) {
  1878. /* Matched "XXXX", so don't reverse. */
  1879. set |= flag->set;
  1880. clear |= flag->clear;
  1881. break;
  1882. }
  1883. }
  1884. /* Ignore unknown flag names. */
  1885. if (flag->wname == NULL && failed == NULL)
  1886. failed = start;
  1887. /* Find start of next token. */
  1888. start = end;
  1889. while (*start == L'\t' || *start == L' ' || *start == L',')
  1890. start++;
  1891. }
  1892. if (setp)
  1893. *setp = set;
  1894. if (clrp)
  1895. *clrp = clear;
  1896. /* Return location of first failure. */
  1897. return (failed);
  1898. }
  1899. #ifdef TEST
  1900. #include <stdio.h>
  1901. int
  1902. main(int argc, char **argv)
  1903. {
  1904. struct archive_entry *entry = archive_entry_new();
  1905. unsigned long set, clear;
  1906. const wchar_t *remainder;
  1907. remainder = archive_entry_copy_fflags_text_w(entry, L"nosappnd dump archive,,,,,,,");
  1908. archive_entry_fflags(entry, &set, &clear);
  1909. wprintf(L"set=0x%lX clear=0x%lX remainder='%ls'\n", set, clear, remainder);
  1910. wprintf(L"new flags='%s'\n", archive_entry_fflags_text(entry));
  1911. return (0);
  1912. }
  1913. #endif