archive_entry.c 57 KB

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