archive_entry.c 52 KB

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