archive_entry.c 58 KB

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