archive_entry.c 51 KB

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