create.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972
  1. /* Create a tar archive.
  2. Copyright 1985-2023 Free Software Foundation, Inc.
  3. This file is part of GNU tar.
  4. GNU tar is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. GNU tar is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. Written by John Gilmore, on 1985-08-25. */
  15. #include <system.h>
  16. #include <areadlink.h>
  17. #include <flexmember.h>
  18. #include <quotearg.h>
  19. #include "common.h"
  20. #include <hash.h>
  21. /* Error number to use when an impostor is discovered.
  22. Pretend the impostor isn't there. */
  23. enum { IMPOSTOR_ERRNO = ENOENT };
  24. struct link
  25. {
  26. dev_t dev;
  27. ino_t ino;
  28. nlink_t nlink;
  29. char name[FLEXIBLE_ARRAY_MEMBER];
  30. };
  31. struct exclusion_tag
  32. {
  33. const char *name;
  34. size_t length;
  35. enum exclusion_tag_type type;
  36. bool (*predicate) (int fd);
  37. struct exclusion_tag *next;
  38. };
  39. static struct exclusion_tag *exclusion_tags;
  40. void
  41. add_exclusion_tag (const char *name, enum exclusion_tag_type type,
  42. bool (*predicate) (int fd))
  43. {
  44. struct exclusion_tag *tag = xmalloc (sizeof tag[0]);
  45. tag->next = exclusion_tags;
  46. tag->name = name;
  47. tag->type = type;
  48. tag->predicate = predicate;
  49. tag->length = strlen (name);
  50. exclusion_tags = tag;
  51. }
  52. void
  53. exclusion_tag_warning (const char *dirname, const char *tagname,
  54. const char *message)
  55. {
  56. if (verbose_option)
  57. WARNOPT (WARN_CACHEDIR,
  58. (0, 0,
  59. _("%s: contains a cache directory tag %s; %s"),
  60. quotearg_colon (dirname),
  61. quotearg_n (1, tagname),
  62. message));
  63. }
  64. enum exclusion_tag_type
  65. check_exclusion_tags (struct tar_stat_info const *st, char const **tag_file_name)
  66. {
  67. struct exclusion_tag *tag;
  68. for (tag = exclusion_tags; tag; tag = tag->next)
  69. {
  70. int tagfd = subfile_open (st, tag->name, open_read_flags);
  71. if (0 <= tagfd)
  72. {
  73. bool satisfied = !tag->predicate || tag->predicate (tagfd);
  74. close (tagfd);
  75. if (satisfied)
  76. {
  77. if (tag_file_name)
  78. *tag_file_name = tag->name;
  79. return tag->type;
  80. }
  81. }
  82. }
  83. return exclusion_tag_none;
  84. }
  85. /* Exclusion predicate to test if the named file (usually "CACHEDIR.TAG")
  86. contains a valid header, as described at:
  87. http://www.brynosaurus.com/cachedir
  88. Applications can write this file into directories they create
  89. for use as caches containing purely regenerable, non-precious data,
  90. allowing us to avoid archiving them if --exclude-caches is specified. */
  91. #define CACHEDIR_SIGNATURE "Signature: 8a477f597d28d172789f06886806bc55"
  92. #define CACHEDIR_SIGNATURE_SIZE (sizeof CACHEDIR_SIGNATURE - 1)
  93. bool
  94. cachedir_file_p (int fd)
  95. {
  96. char tagbuf[CACHEDIR_SIGNATURE_SIZE];
  97. return
  98. (read (fd, tagbuf, CACHEDIR_SIGNATURE_SIZE) == CACHEDIR_SIGNATURE_SIZE
  99. && memcmp (tagbuf, CACHEDIR_SIGNATURE, CACHEDIR_SIGNATURE_SIZE) == 0);
  100. }
  101. /* The maximum uintmax_t value that can be represented with DIGITS digits,
  102. assuming that each digit is BITS_PER_DIGIT wide. */
  103. #define MAX_VAL_WITH_DIGITS(digits, bits_per_digit) \
  104. ((digits) * (bits_per_digit) < sizeof (uintmax_t) * CHAR_BIT \
  105. ? ((uintmax_t) 1 << ((digits) * (bits_per_digit))) - 1 \
  106. : (uintmax_t) -1)
  107. /* The maximum uintmax_t value that can be represented with octal
  108. digits and a trailing NUL in BUFFER. */
  109. #define MAX_OCTAL_VAL(buffer) MAX_VAL_WITH_DIGITS (sizeof (buffer) - 1, LG_8)
  110. /* Convert VALUE to an octal representation suitable for tar headers.
  111. Output to buffer WHERE with size SIZE.
  112. The result is undefined if SIZE is 0 or if VALUE is too large to fit. */
  113. static void
  114. to_octal (uintmax_t value, char *where, size_t size)
  115. {
  116. uintmax_t v = value;
  117. size_t i = size;
  118. do
  119. {
  120. where[--i] = '0' + (v & ((1 << LG_8) - 1));
  121. v >>= LG_8;
  122. }
  123. while (i);
  124. }
  125. /* Copy at most LEN bytes from the string SRC to DST. Terminate with
  126. NUL unless SRC is LEN or more bytes long. */
  127. static void
  128. tar_copy_str (char *dst, const char *src, size_t len)
  129. {
  130. size_t i;
  131. for (i = 0; i < len; i++)
  132. if (! (dst[i] = src[i]))
  133. break;
  134. }
  135. /* Same as tar_copy_str, but always terminate with NUL if using
  136. is OLDGNU format */
  137. static void
  138. tar_name_copy_str (char *dst, const char *src, size_t len)
  139. {
  140. tar_copy_str (dst, src, len);
  141. if (archive_format == OLDGNU_FORMAT)
  142. dst[len-1] = 0;
  143. }
  144. /* Convert NEGATIVE VALUE to a base-256 representation suitable for
  145. tar headers. NEGATIVE is true iff VALUE was negative before being
  146. cast to uintmax_t. Output to buffer WHERE with size SIZE.
  147. The result is undefined if SIZE is 0 or if VALUE is too large to
  148. fit. */
  149. static void
  150. to_base256 (bool negative, uintmax_t value, char *where, size_t size)
  151. {
  152. uintmax_t v = value;
  153. uintmax_t propagated_sign_bits =
  154. ((uintmax_t) - negative << (CHAR_BIT * sizeof v - LG_256));
  155. size_t i = size;
  156. do
  157. {
  158. where[--i] = v & ((1 << LG_256) - 1);
  159. v = propagated_sign_bits | (v >> LG_256);
  160. }
  161. while (i);
  162. }
  163. #define GID_TO_CHARS(val, where) gid_to_chars (val, where, sizeof (where))
  164. #define MAJOR_TO_CHARS(val, where) major_to_chars (val, where, sizeof (where))
  165. #define MINOR_TO_CHARS(val, where) minor_to_chars (val, where, sizeof (where))
  166. #define MODE_TO_CHARS(val, where) mode_to_chars (val, where, sizeof (where))
  167. #define UID_TO_CHARS(val, where) uid_to_chars (val, where, sizeof (where))
  168. #define UNAME_TO_CHARS(name, buf) string_to_chars (name, buf, sizeof (buf))
  169. #define GNAME_TO_CHARS(name, buf) string_to_chars (name, buf, sizeof (buf))
  170. static bool
  171. to_chars (bool negative, uintmax_t value, size_t valsize,
  172. uintmax_t (*substitute) (bool *),
  173. char *where, size_t size, const char *type);
  174. static bool
  175. to_chars_subst (bool negative, bool gnu_format, uintmax_t value, size_t valsize,
  176. uintmax_t (*substitute) (bool *),
  177. char *where, size_t size, const char *type)
  178. {
  179. uintmax_t maxval = (gnu_format
  180. ? MAX_VAL_WITH_DIGITS (size - 1, LG_256)
  181. : MAX_VAL_WITH_DIGITS (size - 1, LG_8));
  182. char valbuf[UINTMAX_STRSIZE_BOUND + 1];
  183. char maxbuf[UINTMAX_STRSIZE_BOUND];
  184. char minbuf[UINTMAX_STRSIZE_BOUND + 1];
  185. char const *minval_string;
  186. char const *maxval_string = STRINGIFY_BIGINT (maxval, maxbuf);
  187. char const *value_string;
  188. if (gnu_format)
  189. {
  190. uintmax_t m = maxval + 1 ? maxval + 1 : maxval / 2 + 1;
  191. char *p = STRINGIFY_BIGINT (m, minbuf + 1);
  192. *--p = '-';
  193. minval_string = p;
  194. }
  195. else
  196. minval_string = "0";
  197. if (negative)
  198. {
  199. char *p = STRINGIFY_BIGINT (- value, valbuf + 1);
  200. *--p = '-';
  201. value_string = p;
  202. }
  203. else
  204. value_string = STRINGIFY_BIGINT (value, valbuf);
  205. if (substitute)
  206. {
  207. bool negsub;
  208. uintmax_t sub = substitute (&negsub) & maxval;
  209. /* NOTE: This is one of the few places where GNU_FORMAT differs from
  210. OLDGNU_FORMAT. The actual differences are:
  211. 1. In OLDGNU_FORMAT all strings in a tar header end in \0
  212. 2. Incremental archives use oldgnu_header.
  213. Apart from this they are completely identical. */
  214. uintmax_t s = (negsub &= archive_format == GNU_FORMAT) ? - sub : sub;
  215. char subbuf[UINTMAX_STRSIZE_BOUND + 1];
  216. char *sub_string = STRINGIFY_BIGINT (s, subbuf + 1);
  217. if (negsub)
  218. *--sub_string = '-';
  219. WARN ((0, 0, _("value %s out of %s range %s..%s; substituting %s"),
  220. value_string, type, minval_string, maxval_string,
  221. sub_string));
  222. return to_chars (negsub, s, valsize, 0, where, size, type);
  223. }
  224. else
  225. ERROR ((0, 0, _("value %s out of %s range %s..%s"),
  226. value_string, type, minval_string, maxval_string));
  227. return false;
  228. }
  229. /* Convert NEGATIVE VALUE (which was originally of size VALSIZE) to
  230. external form, using SUBSTITUTE (...) if VALUE won't fit. Output
  231. to buffer WHERE with size SIZE. NEGATIVE is true iff VALUE was
  232. negative before being cast to uintmax_t; its original bitpattern
  233. can be deduced from VALSIZE, its original size before casting.
  234. TYPE is the kind of value being output (useful for diagnostics).
  235. Prefer the POSIX format of SIZE - 1 octal digits (with leading zero
  236. digits), followed by '\0'. If this won't work, and if GNU or
  237. OLDGNU format is allowed, use '\200' followed by base-256, or (if
  238. NEGATIVE) '\377' followed by two's complement base-256.
  239. If neither format works, use SUBSTITUTE (...) instead. Pass to
  240. SUBSTITUTE the address of an 0-or-1 flag recording whether the
  241. substitute value is negative. */
  242. static bool
  243. to_chars (bool negative, uintmax_t value, size_t valsize,
  244. uintmax_t (*substitute) (bool *),
  245. char *where, size_t size, const char *type)
  246. {
  247. bool gnu_format = (archive_format == GNU_FORMAT
  248. || archive_format == OLDGNU_FORMAT);
  249. /* Generate the POSIX octal representation if the number fits. */
  250. if (! negative && value <= MAX_VAL_WITH_DIGITS (size - 1, LG_8))
  251. {
  252. where[size - 1] = '\0';
  253. to_octal (value, where, size - 1);
  254. return true;
  255. }
  256. else if (gnu_format)
  257. {
  258. /* Try to cope with the number by using traditional GNU format
  259. methods */
  260. /* Generate the base-256 representation if the number fits. */
  261. if (((negative ? -1 - value : value)
  262. <= MAX_VAL_WITH_DIGITS (size - 1, LG_256)))
  263. {
  264. where[0] = (char) (negative ? -1 : 1 << (LG_256 - 1));
  265. to_base256 (negative, value, where + 1, size - 1);
  266. return true;
  267. }
  268. /* Otherwise, if the number is negative, and if it would not cause
  269. ambiguity on this host by confusing positive with negative
  270. values, then generate the POSIX octal representation of the value
  271. modulo 2**(field bits). The resulting tar file is
  272. machine-dependent, since it depends on the host word size. Yuck!
  273. But this is the traditional behavior. */
  274. else if (negative && valsize * CHAR_BIT <= (size - 1) * LG_8)
  275. {
  276. static bool warned_once;
  277. if (! warned_once)
  278. {
  279. warned_once = true;
  280. WARN ((0, 0, _("Generating negative octal headers")));
  281. }
  282. where[size - 1] = '\0';
  283. to_octal (value & MAX_VAL_WITH_DIGITS (valsize * CHAR_BIT, 1),
  284. where, size - 1);
  285. return true;
  286. }
  287. /* Otherwise fall back to substitution, if possible: */
  288. }
  289. else
  290. substitute = NULL; /* No substitution for formats, other than GNU */
  291. return to_chars_subst (negative, gnu_format, value, valsize, substitute,
  292. where, size, type);
  293. }
  294. static uintmax_t
  295. gid_substitute (bool *negative)
  296. {
  297. gid_t r;
  298. #ifdef GID_NOBODY
  299. r = GID_NOBODY;
  300. #else
  301. static gid_t gid_nobody;
  302. if (!gid_nobody && !gname_to_gid ("nobody", &gid_nobody))
  303. gid_nobody = -2;
  304. r = gid_nobody;
  305. #endif
  306. *negative = r < 0;
  307. return r;
  308. }
  309. static bool
  310. gid_to_chars (gid_t v, char *p, size_t s)
  311. {
  312. return to_chars (v < 0, (uintmax_t) v, sizeof v, gid_substitute, p, s, "gid_t");
  313. }
  314. static bool
  315. major_to_chars (major_t v, char *p, size_t s)
  316. {
  317. return to_chars (v < 0, (uintmax_t) v, sizeof v, 0, p, s, "major_t");
  318. }
  319. static bool
  320. minor_to_chars (minor_t v, char *p, size_t s)
  321. {
  322. return to_chars (v < 0, (uintmax_t) v, sizeof v, 0, p, s, "minor_t");
  323. }
  324. static bool
  325. mode_to_chars (mode_t v, char *p, size_t s)
  326. {
  327. /* In the common case where the internal and external mode bits are the same,
  328. and we are not using POSIX or GNU format,
  329. propagate all unknown bits to the external mode.
  330. This matches historical practice.
  331. Otherwise, just copy the bits we know about. */
  332. bool negative;
  333. uintmax_t u;
  334. if (S_ISUID == TSUID && S_ISGID == TSGID && S_ISVTX == TSVTX
  335. && S_IRUSR == TUREAD && S_IWUSR == TUWRITE && S_IXUSR == TUEXEC
  336. && S_IRGRP == TGREAD && S_IWGRP == TGWRITE && S_IXGRP == TGEXEC
  337. && S_IROTH == TOREAD && S_IWOTH == TOWRITE && S_IXOTH == TOEXEC
  338. && archive_format != POSIX_FORMAT
  339. && archive_format != USTAR_FORMAT
  340. && archive_format != GNU_FORMAT)
  341. {
  342. negative = v < 0;
  343. u = v;
  344. }
  345. else
  346. {
  347. negative = false;
  348. u = ((v & S_ISUID ? TSUID : 0)
  349. | (v & S_ISGID ? TSGID : 0)
  350. | (v & S_ISVTX ? TSVTX : 0)
  351. | (v & S_IRUSR ? TUREAD : 0)
  352. | (v & S_IWUSR ? TUWRITE : 0)
  353. | (v & S_IXUSR ? TUEXEC : 0)
  354. | (v & S_IRGRP ? TGREAD : 0)
  355. | (v & S_IWGRP ? TGWRITE : 0)
  356. | (v & S_IXGRP ? TGEXEC : 0)
  357. | (v & S_IROTH ? TOREAD : 0)
  358. | (v & S_IWOTH ? TOWRITE : 0)
  359. | (v & S_IXOTH ? TOEXEC : 0));
  360. }
  361. return to_chars (negative, u, sizeof v, 0, p, s, "mode_t");
  362. }
  363. bool
  364. off_to_chars (off_t v, char *p, size_t s)
  365. {
  366. return to_chars (v < 0, (uintmax_t) v, sizeof v, 0, p, s, "off_t");
  367. }
  368. bool
  369. time_to_chars (time_t v, char *p, size_t s)
  370. {
  371. return to_chars (v < 0, (uintmax_t) v, sizeof v, 0, p, s, "time_t");
  372. }
  373. static uintmax_t
  374. uid_substitute (bool *negative)
  375. {
  376. uid_t r;
  377. #ifdef UID_NOBODY
  378. r = UID_NOBODY;
  379. #else
  380. static uid_t uid_nobody;
  381. if (!uid_nobody && !uname_to_uid ("nobody", &uid_nobody))
  382. uid_nobody = -2;
  383. r = uid_nobody;
  384. #endif
  385. *negative = r < 0;
  386. return r;
  387. }
  388. static bool
  389. uid_to_chars (uid_t v, char *p, size_t s)
  390. {
  391. return to_chars (v < 0, (uintmax_t) v, sizeof v, uid_substitute, p, s, "uid_t");
  392. }
  393. static bool
  394. uintmax_to_chars (uintmax_t v, char *p, size_t s)
  395. {
  396. return to_chars (false, v, sizeof v, 0, p, s, "uintmax_t");
  397. }
  398. static void
  399. string_to_chars (char const *str, char *p, size_t s)
  400. {
  401. tar_copy_str (p, str, s);
  402. p[s - 1] = '\0';
  403. }
  404. /* A directory is always considered dumpable.
  405. Otherwise, only regular and contiguous files are considered dumpable.
  406. Such a file is dumpable if it is sparse and both --sparse and --totals
  407. are specified.
  408. Otherwise, it is dumpable unless any of the following conditions occur:
  409. a) it is empty *and* world-readable, or
  410. b) current archive is /dev/null */
  411. static bool
  412. file_dumpable_p (struct stat const *st)
  413. {
  414. if (S_ISDIR (st->st_mode))
  415. return true;
  416. if (! (S_ISREG (st->st_mode) || S_ISCTG (st->st_mode)))
  417. return false;
  418. if (dev_null_output)
  419. return totals_option && sparse_option && ST_IS_SPARSE (*st);
  420. return ! (st->st_size == 0 && (st->st_mode & MODE_R) == MODE_R);
  421. }
  422. /* Writing routines. */
  423. /* Write the EOT block(s). Zero at least two blocks, through the end
  424. of the record. Old tar, as previous versions of GNU tar, writes
  425. garbage after two zeroed blocks. */
  426. void
  427. write_eot (void)
  428. {
  429. union block *pointer = find_next_block ();
  430. memset (pointer->buffer, 0, BLOCKSIZE);
  431. set_next_block_after (pointer);
  432. pointer = find_next_block ();
  433. memset (pointer->buffer, 0, available_space_after (pointer));
  434. set_next_block_after (pointer);
  435. }
  436. /* Write a "private" header */
  437. union block *
  438. start_private_header (const char *name, size_t size, time_t t)
  439. {
  440. union block *header = find_next_block ();
  441. memset (header->buffer, 0, sizeof (union block));
  442. tar_name_copy_str (header->header.name, name, NAME_FIELD_SIZE);
  443. OFF_TO_CHARS (size, header->header.size);
  444. TIME_TO_CHARS (t < 0 ? 0 : min (t, MAX_OCTAL_VAL (header->header.mtime)),
  445. header->header.mtime);
  446. MODE_TO_CHARS (S_IFREG|S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH, header->header.mode);
  447. UID_TO_CHARS (0, header->header.uid);
  448. GID_TO_CHARS (0, header->header.gid);
  449. memcpy (header->header.magic, TMAGIC, TMAGLEN);
  450. memcpy (header->header.version, TVERSION, TVERSLEN);
  451. return header;
  452. }
  453. /* Create a new header and store there at most NAME_FIELD_SIZE bytes of
  454. the file name */
  455. static union block *
  456. write_short_name (struct tar_stat_info *st)
  457. {
  458. union block *header = find_next_block ();
  459. memset (header->buffer, 0, sizeof (union block));
  460. tar_name_copy_str (header->header.name, st->file_name, NAME_FIELD_SIZE);
  461. return header;
  462. }
  463. /* Write a GNUTYPE_LONGLINK or GNUTYPE_LONGNAME block. */
  464. static void
  465. write_gnu_long_link (struct tar_stat_info *st, const char *p, char type)
  466. {
  467. size_t size = strlen (p) + 1;
  468. size_t bufsize;
  469. union block *header;
  470. header = start_private_header ("././@LongLink", size, 0);
  471. if (! numeric_owner_option)
  472. {
  473. static char *uname, *gname;
  474. if (!uname)
  475. {
  476. uid_to_uname (0, &uname);
  477. gid_to_gname (0, &gname);
  478. }
  479. UNAME_TO_CHARS (uname, header->header.uname);
  480. GNAME_TO_CHARS (gname, header->header.gname);
  481. }
  482. strcpy (header->buffer + offsetof (struct posix_header, magic),
  483. OLDGNU_MAGIC);
  484. header->header.typeflag = type;
  485. finish_header (st, header, -1);
  486. header = find_next_block ();
  487. bufsize = available_space_after (header);
  488. while (bufsize < size)
  489. {
  490. memcpy (header->buffer, p, bufsize);
  491. p += bufsize;
  492. size -= bufsize;
  493. set_next_block_after (header + (bufsize - 1) / BLOCKSIZE);
  494. header = find_next_block ();
  495. bufsize = available_space_after (header);
  496. }
  497. memcpy (header->buffer, p, size);
  498. memset (header->buffer + size, 0, bufsize - size);
  499. set_next_block_after (header + (size - 1) / BLOCKSIZE);
  500. }
  501. static size_t
  502. split_long_name (const char *name, size_t length)
  503. {
  504. size_t i;
  505. if (length > PREFIX_FIELD_SIZE + 1)
  506. length = PREFIX_FIELD_SIZE + 1;
  507. else if (ISSLASH (name[length - 1]))
  508. length--;
  509. for (i = length - 1; i > 0; i--)
  510. if (ISSLASH (name[i]))
  511. break;
  512. return i;
  513. }
  514. static union block *
  515. write_ustar_long_name (const char *name)
  516. {
  517. size_t length = strlen (name);
  518. size_t i, nlen;
  519. union block *header;
  520. if (length > PREFIX_FIELD_SIZE + NAME_FIELD_SIZE + 1)
  521. {
  522. ERROR ((0, 0, _("%s: file name is too long (max %d); not dumped"),
  523. quotearg_colon (name),
  524. PREFIX_FIELD_SIZE + NAME_FIELD_SIZE + 1));
  525. return NULL;
  526. }
  527. i = split_long_name (name, length);
  528. if (i == 0 || (nlen = length - i - 1) > NAME_FIELD_SIZE || nlen == 0)
  529. {
  530. ERROR ((0, 0,
  531. _("%s: file name is too long (cannot be split); not dumped"),
  532. quotearg_colon (name)));
  533. return NULL;
  534. }
  535. header = find_next_block ();
  536. memset (header->buffer, 0, sizeof (header->buffer));
  537. memcpy (header->header.prefix, name, i);
  538. memcpy (header->header.name, name + i + 1, length - i - 1);
  539. return header;
  540. }
  541. /* Write a long link name, depending on the current archive format */
  542. static void
  543. write_long_link (struct tar_stat_info *st)
  544. {
  545. switch (archive_format)
  546. {
  547. case POSIX_FORMAT:
  548. xheader_store ("linkpath", st, NULL);
  549. break;
  550. case V7_FORMAT: /* old V7 tar format */
  551. case USTAR_FORMAT:
  552. case STAR_FORMAT:
  553. ERROR ((0, 0,
  554. _("%s: link name is too long; not dumped"),
  555. quotearg_colon (st->link_name)));
  556. break;
  557. case OLDGNU_FORMAT:
  558. case GNU_FORMAT:
  559. write_gnu_long_link (st, st->link_name, GNUTYPE_LONGLINK);
  560. break;
  561. default:
  562. abort(); /*FIXME*/
  563. }
  564. }
  565. static union block *
  566. write_long_name (struct tar_stat_info *st)
  567. {
  568. switch (archive_format)
  569. {
  570. case POSIX_FORMAT:
  571. xheader_store ("path", st, NULL);
  572. break;
  573. case V7_FORMAT:
  574. if (strlen (st->file_name) > NAME_FIELD_SIZE-1)
  575. {
  576. ERROR ((0, 0, _("%s: file name is too long (max %d); not dumped"),
  577. quotearg_colon (st->file_name),
  578. NAME_FIELD_SIZE - 1));
  579. return NULL;
  580. }
  581. break;
  582. case USTAR_FORMAT:
  583. case STAR_FORMAT:
  584. return write_ustar_long_name (st->file_name);
  585. case OLDGNU_FORMAT:
  586. case GNU_FORMAT:
  587. write_gnu_long_link (st, st->file_name, GNUTYPE_LONGNAME);
  588. break;
  589. default:
  590. abort(); /*FIXME*/
  591. }
  592. return write_short_name (st);
  593. }
  594. union block *
  595. write_extended (bool global, struct tar_stat_info *st, union block *old_header)
  596. {
  597. union block *header, hp;
  598. char *p;
  599. int type;
  600. time_t t;
  601. if (st->xhdr.buffer || st->xhdr.stk == NULL)
  602. return old_header;
  603. xheader_finish (&st->xhdr);
  604. memcpy (hp.buffer, old_header, sizeof (hp));
  605. if (global)
  606. {
  607. type = XGLTYPE;
  608. p = xheader_ghdr_name ();
  609. t = start_time.tv_sec;
  610. }
  611. else
  612. {
  613. type = XHDTYPE;
  614. p = xheader_xhdr_name (st);
  615. t = set_mtime_option ? mtime_option.tv_sec : st->stat.st_mtime;
  616. }
  617. xheader_write (type, p, t, &st->xhdr);
  618. free (p);
  619. header = find_next_block ();
  620. memcpy (header, &hp.buffer, sizeof (hp.buffer));
  621. return header;
  622. }
  623. static union block *
  624. write_header_name (struct tar_stat_info *st)
  625. {
  626. if (archive_format == POSIX_FORMAT && !string_ascii_p (st->file_name))
  627. {
  628. xheader_store ("path", st, NULL);
  629. return write_short_name (st);
  630. }
  631. else if (NAME_FIELD_SIZE - (archive_format == OLDGNU_FORMAT)
  632. < strlen (st->file_name))
  633. return write_long_name (st);
  634. else
  635. return write_short_name (st);
  636. }
  637. /* Header handling. */
  638. /* Make a header block for the file whose stat info is st,
  639. and return its address. */
  640. union block *
  641. start_header (struct tar_stat_info *st)
  642. {
  643. union block *header;
  644. char const *uname = NULL;
  645. char const *gname = NULL;
  646. header = write_header_name (st);
  647. if (!header)
  648. return NULL;
  649. /* Override some stat fields, if requested to do so. */
  650. owner_map_translate (st->stat.st_uid, &st->stat.st_uid, &uname);
  651. group_map_translate (st->stat.st_gid, &st->stat.st_gid, &gname);
  652. if (mode_option)
  653. st->stat.st_mode =
  654. ((st->stat.st_mode & ~MODE_ALL)
  655. | mode_adjust (st->stat.st_mode, S_ISDIR (st->stat.st_mode) != 0,
  656. initial_umask, mode_option, NULL));
  657. /* Paul Eggert tried the trivial test ($WRITER cf a b; $READER tvf a)
  658. for a few tars and came up with the following interoperability
  659. matrix:
  660. WRITER
  661. 1 2 3 4 5 6 7 8 9 READER
  662. . . . . . . . . . 1 = SunOS 4.2 tar
  663. # . . # # . . # # 2 = NEC SVR4.0.2 tar
  664. . . . # # . . # . 3 = Solaris 2.1 tar
  665. . . . . . . . . . 4 = GNU tar 1.11.1
  666. . . . . . . . . . 5 = HP-UX 8.07 tar
  667. . . . . . . . . . 6 = Ultrix 4.1
  668. . . . . . . . . . 7 = AIX 3.2
  669. . . . . . . . . . 8 = Hitachi HI-UX 1.03
  670. . . . . . . . . . 9 = Omron UNIOS-B 4.3BSD 1.60Beta
  671. . = works
  672. # = "impossible file type"
  673. The following mask for old archive removes the '#'s in column 4
  674. above, thus making GNU tar both a universal donor and a universal
  675. acceptor for Paul's test. */
  676. if (archive_format == V7_FORMAT || archive_format == USTAR_FORMAT)
  677. MODE_TO_CHARS (st->stat.st_mode & MODE_ALL, header->header.mode);
  678. else
  679. MODE_TO_CHARS (st->stat.st_mode, header->header.mode);
  680. {
  681. uid_t uid = st->stat.st_uid;
  682. if (archive_format == POSIX_FORMAT
  683. && MAX_OCTAL_VAL (header->header.uid) < uid)
  684. {
  685. xheader_store ("uid", st, NULL);
  686. uid = 0;
  687. }
  688. if (!UID_TO_CHARS (uid, header->header.uid))
  689. return NULL;
  690. }
  691. {
  692. gid_t gid = st->stat.st_gid;
  693. if (archive_format == POSIX_FORMAT
  694. && MAX_OCTAL_VAL (header->header.gid) < gid)
  695. {
  696. xheader_store ("gid", st, NULL);
  697. gid = 0;
  698. }
  699. if (!GID_TO_CHARS (gid, header->header.gid))
  700. return NULL;
  701. }
  702. {
  703. off_t size = st->stat.st_size;
  704. if (archive_format == POSIX_FORMAT
  705. && MAX_OCTAL_VAL (header->header.size) < size)
  706. {
  707. xheader_store ("size", st, NULL);
  708. size = 0;
  709. }
  710. if (!OFF_TO_CHARS (size, header->header.size))
  711. return NULL;
  712. }
  713. {
  714. struct timespec mtime;
  715. switch (set_mtime_option)
  716. {
  717. case USE_FILE_MTIME:
  718. mtime = st->mtime;
  719. break;
  720. case FORCE_MTIME:
  721. mtime = mtime_option;
  722. break;
  723. case CLAMP_MTIME:
  724. mtime = timespec_cmp (st->mtime, mtime_option) > 0
  725. ? mtime_option : st->mtime;
  726. break;
  727. }
  728. if (archive_format == POSIX_FORMAT)
  729. {
  730. if (MAX_OCTAL_VAL (header->header.mtime) < mtime.tv_sec
  731. || mtime.tv_nsec != 0)
  732. xheader_store ("mtime", st, &mtime);
  733. if (MAX_OCTAL_VAL (header->header.mtime) < mtime.tv_sec)
  734. mtime.tv_sec = 0;
  735. }
  736. if (!TIME_TO_CHARS (mtime.tv_sec, header->header.mtime))
  737. return NULL;
  738. }
  739. /* FIXME */
  740. if (S_ISCHR (st->stat.st_mode)
  741. || S_ISBLK (st->stat.st_mode))
  742. {
  743. major_t devmajor = major (st->stat.st_rdev);
  744. minor_t devminor = minor (st->stat.st_rdev);
  745. if (archive_format == POSIX_FORMAT
  746. && MAX_OCTAL_VAL (header->header.devmajor) < devmajor)
  747. {
  748. xheader_store ("devmajor", st, NULL);
  749. devmajor = 0;
  750. }
  751. if (!MAJOR_TO_CHARS (devmajor, header->header.devmajor))
  752. return NULL;
  753. if (archive_format == POSIX_FORMAT
  754. && MAX_OCTAL_VAL (header->header.devminor) < devminor)
  755. {
  756. xheader_store ("devminor", st, NULL);
  757. devminor = 0;
  758. }
  759. if (!MINOR_TO_CHARS (devminor, header->header.devminor))
  760. return NULL;
  761. }
  762. if (archive_format == POSIX_FORMAT)
  763. {
  764. xheader_store ("atime", st, NULL);
  765. xheader_store ("ctime", st, NULL);
  766. }
  767. else if (incremental_option)
  768. if (archive_format == OLDGNU_FORMAT || archive_format == GNU_FORMAT)
  769. {
  770. TIME_TO_CHARS (st->atime.tv_sec, header->oldgnu_header.atime);
  771. TIME_TO_CHARS (st->ctime.tv_sec, header->oldgnu_header.ctime);
  772. }
  773. header->header.typeflag = archive_format == V7_FORMAT ? AREGTYPE : REGTYPE;
  774. switch (archive_format)
  775. {
  776. case V7_FORMAT:
  777. break;
  778. case OLDGNU_FORMAT:
  779. case GNU_FORMAT: /*FIXME?*/
  780. /* Overwrite header->header.magic and header.version in one blow. */
  781. strcpy (header->buffer + offsetof (struct posix_header, magic),
  782. OLDGNU_MAGIC);
  783. break;
  784. case POSIX_FORMAT:
  785. case USTAR_FORMAT:
  786. memcpy (header->header.magic, TMAGIC, TMAGLEN);
  787. memcpy (header->header.version, TVERSION, TVERSLEN);
  788. break;
  789. default:
  790. abort ();
  791. }
  792. if (archive_format == V7_FORMAT || numeric_owner_option)
  793. {
  794. /* header->header.[ug]name are left as the empty string. */
  795. }
  796. else
  797. {
  798. if (uname)
  799. st->uname = xstrdup (uname);
  800. else
  801. uid_to_uname (st->stat.st_uid, &st->uname);
  802. if (gname)
  803. st->gname = xstrdup (gname);
  804. else
  805. gid_to_gname (st->stat.st_gid, &st->gname);
  806. if (archive_format == POSIX_FORMAT
  807. && (strlen (st->uname) > UNAME_FIELD_SIZE
  808. || !string_ascii_p (st->uname)))
  809. xheader_store ("uname", st, NULL);
  810. UNAME_TO_CHARS (st->uname, header->header.uname);
  811. if (archive_format == POSIX_FORMAT
  812. && (strlen (st->gname) > GNAME_FIELD_SIZE
  813. || !string_ascii_p (st->gname)))
  814. xheader_store ("gname", st, NULL);
  815. GNAME_TO_CHARS (st->gname, header->header.gname);
  816. }
  817. if (archive_format == POSIX_FORMAT)
  818. {
  819. if (acls_option > 0)
  820. {
  821. if (st->acls_a_ptr)
  822. xheader_store ("SCHILY.acl.access", st, NULL);
  823. if (st->acls_d_ptr)
  824. xheader_store ("SCHILY.acl.default", st, NULL);
  825. }
  826. if ((selinux_context_option > 0) && st->cntx_name)
  827. xheader_store ("RHT.security.selinux", st, NULL);
  828. if (xattrs_option > 0)
  829. {
  830. size_t i;
  831. for (i = 0; i < st->xattr_map.xm_size; i++)
  832. xheader_store (st->xattr_map.xm_map[i].xkey, st, &i);
  833. }
  834. }
  835. return header;
  836. }
  837. void
  838. simple_finish_header (union block *header)
  839. {
  840. size_t i;
  841. int sum;
  842. char *p;
  843. memcpy (header->header.chksum, CHKBLANKS, sizeof header->header.chksum);
  844. sum = 0;
  845. p = header->buffer;
  846. for (i = sizeof *header; i-- != 0; )
  847. /* We can't use unsigned char here because of old compilers, e.g. V7. */
  848. sum += 0xFF & *p++;
  849. /* Fill in the checksum field. It's formatted differently from the
  850. other fields: it has [6] digits, a null, then a space -- rather than
  851. digits, then a null. We use to_chars.
  852. The final space is already there, from
  853. checksumming, and to_chars doesn't modify it.
  854. This is a fast way to do:
  855. sprintf(header->header.chksum, "%6o", sum); */
  856. uintmax_to_chars ((uintmax_t) sum, header->header.chksum, 7);
  857. set_next_block_after (header);
  858. }
  859. /* Finish off a filled-in header block and write it out. We also
  860. print the file name and/or full info if verbose is on. If BLOCK_ORDINAL
  861. is not negative, is the block ordinal of the first record for this
  862. file, which may be a preceding long name or long link record. */
  863. void
  864. finish_header (struct tar_stat_info *st,
  865. union block *header, off_t block_ordinal)
  866. {
  867. /* Note: It is important to do this before the call to write_extended(),
  868. so that the actual ustar header is printed */
  869. if (verbose_option
  870. && header->header.typeflag != GNUTYPE_LONGLINK
  871. && header->header.typeflag != GNUTYPE_LONGNAME
  872. && header->header.typeflag != XHDTYPE
  873. && header->header.typeflag != XGLTYPE)
  874. {
  875. /* FIXME: This global is used in print_header, sigh. */
  876. current_format = archive_format;
  877. print_header (st, header, block_ordinal);
  878. }
  879. header = write_extended (false, st, header);
  880. simple_finish_header (header);
  881. }
  882. void
  883. pad_archive (off_t size_left)
  884. {
  885. union block *blk;
  886. while (size_left > 0)
  887. {
  888. blk = find_next_block ();
  889. memset (blk->buffer, 0, BLOCKSIZE);
  890. set_next_block_after (blk);
  891. size_left -= BLOCKSIZE;
  892. }
  893. }
  894. static enum dump_status
  895. dump_regular_file (int fd, struct tar_stat_info *st)
  896. {
  897. off_t size_left = st->stat.st_size;
  898. off_t block_ordinal;
  899. union block *blk;
  900. block_ordinal = current_block_ordinal ();
  901. blk = start_header (st);
  902. if (!blk)
  903. return dump_status_fail;
  904. /* Mark contiguous files, if we support them. */
  905. if (archive_format != V7_FORMAT && S_ISCTG (st->stat.st_mode))
  906. blk->header.typeflag = CONTTYPE;
  907. finish_header (st, blk, block_ordinal);
  908. mv_begin_write (st->file_name, st->stat.st_size, st->stat.st_size);
  909. while (size_left > 0)
  910. {
  911. size_t bufsize, count;
  912. blk = find_next_block ();
  913. bufsize = available_space_after (blk);
  914. if (size_left < bufsize)
  915. {
  916. /* Last read -- zero out area beyond. */
  917. bufsize = size_left;
  918. count = bufsize % BLOCKSIZE;
  919. if (count)
  920. memset (blk->buffer + size_left, 0, BLOCKSIZE - count);
  921. }
  922. count = (fd <= 0) ? bufsize : blocking_read (fd, blk->buffer, bufsize);
  923. if (count == SAFE_READ_ERROR)
  924. {
  925. read_diag_details (st->orig_file_name,
  926. st->stat.st_size - size_left, bufsize);
  927. pad_archive (size_left);
  928. return dump_status_short;
  929. }
  930. size_left -= count;
  931. set_next_block_after (blk + (bufsize - 1) / BLOCKSIZE);
  932. if (count != bufsize)
  933. {
  934. char buf[UINTMAX_STRSIZE_BOUND];
  935. memset (blk->buffer + count, 0, bufsize - count);
  936. WARNOPT (WARN_FILE_SHRANK,
  937. (0, 0,
  938. ngettext ("%s: File shrank by %s byte; padding with zeros",
  939. "%s: File shrank by %s bytes; padding with zeros",
  940. size_left),
  941. quotearg_colon (st->orig_file_name),
  942. STRINGIFY_BIGINT (size_left, buf)));
  943. if (! ignore_failed_read_option)
  944. set_exit_status (TAREXIT_DIFFERS);
  945. pad_archive (size_left - (bufsize - count));
  946. return dump_status_short;
  947. }
  948. }
  949. return dump_status_ok;
  950. }
  951. /* Copy info from the directory identified by ST into the archive.
  952. DIRECTORY contains the directory's entries. */
  953. static void
  954. dump_dir0 (struct tar_stat_info *st, char const *directory)
  955. {
  956. bool top_level = ! st->parent;
  957. const char *tag_file_name;
  958. union block *blk = NULL;
  959. off_t block_ordinal = current_block_ordinal ();
  960. st->stat.st_size = 0; /* force 0 size on dir */
  961. blk = start_header (st);
  962. if (!blk)
  963. return;
  964. info_attach_exclist (st);
  965. if (incremental_option && archive_format != POSIX_FORMAT)
  966. blk->header.typeflag = GNUTYPE_DUMPDIR;
  967. else /* if (standard_option) */
  968. blk->header.typeflag = DIRTYPE;
  969. /* If we're gnudumping, we aren't done yet so don't close it. */
  970. if (!incremental_option)
  971. finish_header (st, blk, block_ordinal);
  972. else if (gnu_list_name->directory)
  973. {
  974. if (archive_format == POSIX_FORMAT)
  975. {
  976. xheader_store ("GNU.dumpdir", st,
  977. safe_directory_contents (gnu_list_name->directory));
  978. finish_header (st, blk, block_ordinal);
  979. }
  980. else
  981. {
  982. off_t size_left;
  983. off_t totsize;
  984. size_t bufsize;
  985. ssize_t count;
  986. const char *buffer, *p_buffer;
  987. block_ordinal = current_block_ordinal ();
  988. buffer = safe_directory_contents (gnu_list_name->directory);
  989. totsize = dumpdir_size (buffer);
  990. OFF_TO_CHARS (totsize, blk->header.size);
  991. finish_header (st, blk, block_ordinal);
  992. p_buffer = buffer;
  993. size_left = totsize;
  994. mv_begin_write (st->file_name, totsize, totsize);
  995. while (size_left > 0)
  996. {
  997. blk = find_next_block ();
  998. bufsize = available_space_after (blk);
  999. if (size_left < bufsize)
  1000. {
  1001. bufsize = size_left;
  1002. count = bufsize % BLOCKSIZE;
  1003. if (count)
  1004. memset (blk->buffer + size_left, 0, BLOCKSIZE - count);
  1005. }
  1006. memcpy (blk->buffer, p_buffer, bufsize);
  1007. size_left -= bufsize;
  1008. p_buffer += bufsize;
  1009. set_next_block_after (blk + (bufsize - 1) / BLOCKSIZE);
  1010. }
  1011. }
  1012. return;
  1013. }
  1014. if (!recursion_option)
  1015. return;
  1016. if (one_file_system_option
  1017. && !top_level
  1018. && st->parent->stat.st_dev != st->stat.st_dev)
  1019. {
  1020. if (verbose_option)
  1021. WARNOPT (WARN_XDEV,
  1022. (0, 0,
  1023. _("%s: file is on a different filesystem; not dumped"),
  1024. quotearg_colon (st->orig_file_name)));
  1025. }
  1026. else
  1027. {
  1028. char *name_buf;
  1029. size_t name_size;
  1030. switch (check_exclusion_tags (st, &tag_file_name))
  1031. {
  1032. case exclusion_tag_all:
  1033. /* Handled in dump_file0 */
  1034. break;
  1035. case exclusion_tag_none:
  1036. {
  1037. char const *entry;
  1038. size_t entry_len;
  1039. size_t name_len;
  1040. name_buf = xstrdup (st->orig_file_name);
  1041. name_size = name_len = strlen (name_buf);
  1042. /* Now output all the files in the directory. */
  1043. for (entry = directory; (entry_len = strlen (entry)) != 0;
  1044. entry += entry_len + 1)
  1045. {
  1046. if (name_size < name_len + entry_len)
  1047. {
  1048. name_size = name_len + entry_len;
  1049. name_buf = xrealloc (name_buf, name_size + 1);
  1050. }
  1051. strcpy (name_buf + name_len, entry);
  1052. if (!excluded_name (name_buf, st))
  1053. dump_file (st, entry, name_buf);
  1054. }
  1055. free (name_buf);
  1056. }
  1057. break;
  1058. case exclusion_tag_contents:
  1059. exclusion_tag_warning (st->orig_file_name, tag_file_name,
  1060. _("contents not dumped"));
  1061. name_size = strlen (st->orig_file_name) + strlen (tag_file_name) + 1;
  1062. name_buf = xmalloc (name_size);
  1063. strcpy (name_buf, st->orig_file_name);
  1064. strcat (name_buf, tag_file_name);
  1065. dump_file (st, tag_file_name, name_buf);
  1066. free (name_buf);
  1067. break;
  1068. case exclusion_tag_under:
  1069. exclusion_tag_warning (st->orig_file_name, tag_file_name,
  1070. _("contents not dumped"));
  1071. break;
  1072. }
  1073. }
  1074. }
  1075. /* Ensure exactly one trailing slash. */
  1076. static void
  1077. ensure_slash (char **pstr)
  1078. {
  1079. size_t len = strlen (*pstr);
  1080. while (len >= 1 && ISSLASH ((*pstr)[len - 1]))
  1081. len--;
  1082. if (!ISSLASH ((*pstr)[len]))
  1083. *pstr = xrealloc (*pstr, len + 2);
  1084. (*pstr)[len++] = '/';
  1085. (*pstr)[len] = '\0';
  1086. }
  1087. /* If we just ran out of file descriptors, release a file descriptor
  1088. in the directory chain somewhere leading from DIR->parent->parent
  1089. up through the root. Return true if successful, false (preserving
  1090. errno == EMFILE) otherwise.
  1091. Do not release DIR's file descriptor, or DIR's parent, as other
  1092. code assumes that they work. On some operating systems, another
  1093. process can claim file descriptor resources as we release them, and
  1094. some calls or their emulations require multiple file descriptors,
  1095. so callers should not give up if a single release doesn't work. */
  1096. static bool
  1097. open_failure_recover (struct tar_stat_info const *dir)
  1098. {
  1099. if (errno == EMFILE && dir && dir->parent)
  1100. {
  1101. struct tar_stat_info *p;
  1102. for (p = dir->parent->parent; p; p = p->parent)
  1103. if (0 < p->fd && (! p->parent || p->parent->fd <= 0))
  1104. {
  1105. tar_stat_close (p);
  1106. return true;
  1107. }
  1108. errno = EMFILE;
  1109. }
  1110. return false;
  1111. }
  1112. /* Return the directory entries of ST, in a dynamically allocated buffer,
  1113. each entry followed by '\0' and the last followed by an extra '\0'.
  1114. Return null on failure, setting errno. */
  1115. char *
  1116. get_directory_entries (struct tar_stat_info *st)
  1117. {
  1118. while (! (st->dirstream = fdopendir (st->fd)))
  1119. if (! open_failure_recover (st))
  1120. return 0;
  1121. return streamsavedir (st->dirstream, savedir_sort_order);
  1122. }
  1123. /* Dump the directory ST. Return true if successful, false (emitting
  1124. diagnostics) otherwise. Get ST's entries, recurse through its
  1125. subdirectories, and clean up file descriptors afterwards. */
  1126. static bool
  1127. dump_dir (struct tar_stat_info *st)
  1128. {
  1129. char *directory = get_directory_entries (st);
  1130. if (! directory)
  1131. {
  1132. savedir_diag (st->orig_file_name);
  1133. return false;
  1134. }
  1135. dump_dir0 (st, directory);
  1136. restore_parent_fd (st);
  1137. free (directory);
  1138. return true;
  1139. }
  1140. /* Number of links a file can have without having to be entered into
  1141. the link table. Typically this is 1, but in trickier circumstances
  1142. it is 0. */
  1143. static nlink_t trivial_link_count;
  1144. /* Main functions of this module. */
  1145. void
  1146. create_archive (void)
  1147. {
  1148. struct name const *p;
  1149. trivial_link_count = filename_args != FILES_MANY && ! dereference_option;
  1150. open_archive (ACCESS_WRITE);
  1151. buffer_write_global_xheader ();
  1152. if (incremental_option)
  1153. {
  1154. size_t buffer_size = 0;
  1155. char *buffer = NULL;
  1156. const char *q;
  1157. collect_and_sort_names ();
  1158. while ((p = name_from_list ()) != NULL)
  1159. if (!excluded_name (p->name, NULL))
  1160. dump_file (0, p->name, p->name);
  1161. blank_name_list ();
  1162. while ((p = name_from_list ()) != NULL)
  1163. if (!excluded_name (p->name, NULL))
  1164. {
  1165. struct tar_stat_info st;
  1166. size_t plen = strlen (p->name);
  1167. while (buffer_size <= plen)
  1168. buffer = x2realloc (buffer, &buffer_size);
  1169. memcpy (buffer, p->name, plen);
  1170. if (! ISSLASH (buffer[plen - 1]))
  1171. buffer[plen++] = DIRECTORY_SEPARATOR;
  1172. tar_stat_init (&st);
  1173. q = directory_contents (p->directory);
  1174. if (q)
  1175. while (*q)
  1176. {
  1177. size_t qlen = strlen (q);
  1178. if (*q == 'Y')
  1179. {
  1180. if (! st.orig_file_name)
  1181. {
  1182. int fd = openat (chdir_fd, p->name,
  1183. open_searchdir_flags);
  1184. if (fd < 0)
  1185. {
  1186. file_removed_diag (p->name, !p->parent,
  1187. open_diag);
  1188. break;
  1189. }
  1190. st.fd = fd;
  1191. if (fstat (fd, &st.stat) != 0)
  1192. {
  1193. file_removed_diag (p->name, !p->parent,
  1194. stat_diag);
  1195. break;
  1196. }
  1197. st.orig_file_name = xstrdup (p->name);
  1198. }
  1199. while (buffer_size < plen + qlen)
  1200. buffer = x2realloc (buffer, &buffer_size);
  1201. strcpy (buffer + plen, q + 1);
  1202. dump_file (&st, q + 1, buffer);
  1203. }
  1204. q += qlen + 1;
  1205. }
  1206. tar_stat_destroy (&st);
  1207. }
  1208. free (buffer);
  1209. }
  1210. else
  1211. {
  1212. const char *name;
  1213. while ((name = name_next (1)) != NULL)
  1214. if (!excluded_name (name, NULL))
  1215. dump_file (0, name, name);
  1216. }
  1217. write_eot ();
  1218. close_archive ();
  1219. finish_deferred_unlinks ();
  1220. if (listed_incremental_option)
  1221. write_directory_file ();
  1222. }
  1223. /* Calculate the hash of a link. */
  1224. static size_t
  1225. hash_link (void const *entry, size_t n_buckets)
  1226. {
  1227. struct link const *l = entry;
  1228. uintmax_t num = l->dev ^ l->ino;
  1229. return num % n_buckets;
  1230. }
  1231. /* Compare two links for equality. */
  1232. static bool
  1233. compare_links (void const *entry1, void const *entry2)
  1234. {
  1235. struct link const *link1 = entry1;
  1236. struct link const *link2 = entry2;
  1237. return ((link1->dev ^ link2->dev) | (link1->ino ^ link2->ino)) == 0;
  1238. }
  1239. static void
  1240. unknown_file_error (char const *p)
  1241. {
  1242. WARNOPT (WARN_FILE_IGNORED,
  1243. (0, 0, _("%s: Unknown file type; file ignored"),
  1244. quotearg_colon (p)));
  1245. if (!ignore_failed_read_option)
  1246. set_exit_status (TAREXIT_FAILURE);
  1247. }
  1248. /* Handling of hard links */
  1249. /* Table of all non-directories that we've written so far. Any time
  1250. we see another, we check the table and avoid dumping the data
  1251. again if we've done it once already. */
  1252. static Hash_table *link_table;
  1253. /* Try to dump stat as a hard link to another file in the archive.
  1254. Return true if successful. */
  1255. static bool
  1256. dump_hard_link (struct tar_stat_info *st)
  1257. {
  1258. if (link_table
  1259. && (trivial_link_count < st->stat.st_nlink || remove_files_option))
  1260. {
  1261. struct link lp;
  1262. struct link *duplicate;
  1263. off_t block_ordinal;
  1264. union block *blk;
  1265. lp.ino = st->stat.st_ino;
  1266. lp.dev = st->stat.st_dev;
  1267. if ((duplicate = hash_lookup (link_table, &lp)))
  1268. {
  1269. /* We found a link. */
  1270. char const *link_name = safer_name_suffix (duplicate->name, true,
  1271. absolute_names_option);
  1272. if (duplicate->nlink)
  1273. duplicate->nlink--;
  1274. block_ordinal = current_block_ordinal ();
  1275. assign_string (&st->link_name, link_name);
  1276. if (NAME_FIELD_SIZE - (archive_format == OLDGNU_FORMAT)
  1277. < strlen (link_name))
  1278. write_long_link (st);
  1279. st->stat.st_size = 0;
  1280. blk = start_header (st);
  1281. if (!blk)
  1282. return false;
  1283. tar_copy_str (blk->header.linkname, link_name, NAME_FIELD_SIZE);
  1284. blk->header.typeflag = LNKTYPE;
  1285. finish_header (st, blk, block_ordinal);
  1286. if (remove_files_option)
  1287. queue_deferred_unlink (st->orig_file_name, false);
  1288. return true;
  1289. }
  1290. }
  1291. return false;
  1292. }
  1293. static void
  1294. file_count_links (struct tar_stat_info *st)
  1295. {
  1296. if (hard_dereference_option)
  1297. return;
  1298. if (trivial_link_count < st->stat.st_nlink)
  1299. {
  1300. struct link *duplicate;
  1301. char *linkname = NULL;
  1302. struct link *lp;
  1303. assign_string (&linkname, safer_name_suffix (st->orig_file_name, true,
  1304. absolute_names_option));
  1305. transform_name (&linkname, XFORM_LINK);
  1306. lp = xmalloc (FLEXNSIZEOF (struct link, name, strlen (linkname) + 1));
  1307. lp->ino = st->stat.st_ino;
  1308. lp->dev = st->stat.st_dev;
  1309. lp->nlink = st->stat.st_nlink;
  1310. strcpy (lp->name, linkname);
  1311. free (linkname);
  1312. if (! ((link_table
  1313. || (link_table = hash_initialize (0, 0, hash_link,
  1314. compare_links, 0)))
  1315. && (duplicate = hash_insert (link_table, lp))))
  1316. xalloc_die ();
  1317. if (duplicate != lp)
  1318. abort ();
  1319. lp->nlink--;
  1320. }
  1321. }
  1322. /* For each dumped file, check if all its links were dumped. Emit
  1323. warnings if it is not so. */
  1324. void
  1325. check_links (void)
  1326. {
  1327. struct link *lp;
  1328. if (!link_table)
  1329. return;
  1330. for (lp = hash_get_first (link_table); lp;
  1331. lp = hash_get_next (link_table, lp))
  1332. {
  1333. if (lp->nlink)
  1334. {
  1335. WARN ((0, 0, _("Missing links to %s."), quote (lp->name)));
  1336. }
  1337. }
  1338. }
  1339. /* Assuming DIR is the working directory, open FILE, using FLAGS to
  1340. control the open. A null DIR means to use ".". If we are low on
  1341. file descriptors, try to release one or more from DIR's parents to
  1342. reuse it. */
  1343. int
  1344. subfile_open (struct tar_stat_info const *dir, char const *file, int flags)
  1345. {
  1346. int fd;
  1347. static bool initialized;
  1348. if (! initialized)
  1349. {
  1350. /* Initialize any tables that might be needed when file
  1351. descriptors are exhausted, and whose initialization might
  1352. require a file descriptor. This includes the system message
  1353. catalog and tar's message catalog. */
  1354. initialized = true;
  1355. strerror (ENOENT);
  1356. gettext ("");
  1357. }
  1358. while ((fd = openat (dir ? dir->fd : chdir_fd, file, flags)) < 0
  1359. && open_failure_recover (dir))
  1360. continue;
  1361. return fd;
  1362. }
  1363. /* Restore the file descriptor for ST->parent, if it was temporarily
  1364. closed to conserve file descriptors. On failure, set the file
  1365. descriptor to the negative of the corresponding errno value. Call
  1366. this every time a subdirectory is ascended from. */
  1367. void
  1368. restore_parent_fd (struct tar_stat_info const *st)
  1369. {
  1370. struct tar_stat_info *parent = st->parent;
  1371. if (parent && ! parent->fd)
  1372. {
  1373. int parentfd = openat (st->fd, "..", open_searchdir_flags);
  1374. struct stat parentstat;
  1375. if (parentfd < 0)
  1376. parentfd = - errno;
  1377. else if (! (fstat (parentfd, &parentstat) == 0
  1378. && parent->stat.st_ino == parentstat.st_ino
  1379. && parent->stat.st_dev == parentstat.st_dev))
  1380. {
  1381. close (parentfd);
  1382. parentfd = IMPOSTOR_ERRNO;
  1383. }
  1384. if (parentfd < 0)
  1385. {
  1386. int origfd = openat (chdir_fd, parent->orig_file_name,
  1387. open_searchdir_flags);
  1388. if (0 <= origfd)
  1389. {
  1390. if (fstat (parentfd, &parentstat) == 0
  1391. && parent->stat.st_ino == parentstat.st_ino
  1392. && parent->stat.st_dev == parentstat.st_dev)
  1393. parentfd = origfd;
  1394. else
  1395. close (origfd);
  1396. }
  1397. }
  1398. parent->fd = parentfd;
  1399. }
  1400. }
  1401. /* Dump a single file, recursing on directories. ST is the file's
  1402. status info, NAME its name relative to the parent directory, and P
  1403. its full name (which may be relative to the working directory). */
  1404. /* FIXME: One should make sure that for *every* path leading to setting
  1405. exit_status to failure, a clear diagnostic has been issued. */
  1406. static void
  1407. dump_file0 (struct tar_stat_info *st, char const *name, char const *p)
  1408. {
  1409. union block *header;
  1410. char type;
  1411. off_t block_ordinal = -1;
  1412. int fd = 0;
  1413. bool is_dir;
  1414. struct tar_stat_info const *parent = st->parent;
  1415. bool top_level = ! parent;
  1416. int parentfd = top_level ? chdir_fd : parent->fd;
  1417. void (*diag) (char const *) = 0;
  1418. if (interactive_option && !confirm ("add", p))
  1419. return;
  1420. assign_string (&st->orig_file_name, p);
  1421. assign_string (&st->file_name,
  1422. safer_name_suffix (p, false, absolute_names_option));
  1423. transform_name (&st->file_name, XFORM_REGFILE);
  1424. if (parentfd < 0 && ! top_level)
  1425. {
  1426. errno = - parentfd;
  1427. diag = open_diag;
  1428. }
  1429. else if (fstatat (parentfd, name, &st->stat, fstatat_flags) != 0)
  1430. diag = stat_diag;
  1431. else if (file_dumpable_p (&st->stat))
  1432. {
  1433. fd = subfile_open (parent, name, open_read_flags);
  1434. if (fd < 0)
  1435. diag = open_diag;
  1436. else
  1437. {
  1438. st->fd = fd;
  1439. if (fstat (fd, &st->stat) != 0)
  1440. diag = stat_diag;
  1441. }
  1442. }
  1443. if (diag)
  1444. {
  1445. file_removed_diag (p, top_level, diag);
  1446. return;
  1447. }
  1448. struct stat st1 = st->stat;
  1449. st->archive_file_size = st->stat.st_size;
  1450. st->atime = get_stat_atime (&st->stat);
  1451. st->mtime = get_stat_mtime (&st->stat);
  1452. st->ctime = get_stat_ctime (&st->stat);
  1453. #ifdef S_ISHIDDEN
  1454. if (S_ISHIDDEN (st->stat.st_mode))
  1455. {
  1456. char *new = (char *) alloca (strlen (p) + 2);
  1457. if (new)
  1458. {
  1459. strcpy (new, p);
  1460. strcat (new, "@");
  1461. p = new;
  1462. }
  1463. }
  1464. #endif
  1465. /* See if we want only new files, and check if this one is too old to
  1466. put in the archive.
  1467. This check is omitted if incremental_option is set *and* the
  1468. requested file is not explicitly listed in the command line. */
  1469. if (! (incremental_option && ! top_level)
  1470. && !S_ISDIR (st->stat.st_mode)
  1471. && OLDER_TAR_STAT_TIME (*st, m)
  1472. && (!after_date_option || OLDER_TAR_STAT_TIME (*st, c)))
  1473. {
  1474. if (!incremental_option && verbose_option)
  1475. WARNOPT (WARN_FILE_UNCHANGED,
  1476. (0, 0, _("%s: file is unchanged; not dumped"),
  1477. quotearg_colon (p)));
  1478. return;
  1479. }
  1480. /* See if we are trying to dump the archive. */
  1481. if (sys_file_is_archive (st))
  1482. {
  1483. WARNOPT (WARN_IGNORE_ARCHIVE,
  1484. (0, 0, _("%s: archive cannot contain itself; not dumped"),
  1485. quotearg_colon (p)));
  1486. return;
  1487. }
  1488. is_dir = S_ISDIR (st->stat.st_mode) != 0;
  1489. if (!is_dir && dump_hard_link (st))
  1490. return;
  1491. if (is_dir || S_ISREG (st->stat.st_mode) || S_ISCTG (st->stat.st_mode))
  1492. {
  1493. bool ok;
  1494. struct stat st2;
  1495. xattrs_acls_get (parentfd, name, st, !is_dir);
  1496. xattrs_selinux_get (parentfd, name, st, fd);
  1497. xattrs_xattrs_get (parentfd, name, st, fd);
  1498. if (is_dir)
  1499. {
  1500. const char *tag_file_name;
  1501. ensure_slash (&st->orig_file_name);
  1502. ensure_slash (&st->file_name);
  1503. if (check_exclusion_tags (st, &tag_file_name) == exclusion_tag_all)
  1504. {
  1505. exclusion_tag_warning (st->orig_file_name, tag_file_name,
  1506. _("directory not dumped"));
  1507. return;
  1508. }
  1509. ok = dump_dir (st);
  1510. fd = st->fd;
  1511. parentfd = top_level ? chdir_fd : parent->fd;
  1512. }
  1513. else
  1514. {
  1515. enum dump_status status;
  1516. if (fd && sparse_option && ST_IS_SPARSE (st->stat))
  1517. {
  1518. status = sparse_dump_file (fd, st);
  1519. if (status == dump_status_not_implemented)
  1520. status = dump_regular_file (fd, st);
  1521. }
  1522. else
  1523. status = dump_regular_file (fd, st);
  1524. switch (status)
  1525. {
  1526. case dump_status_ok:
  1527. case dump_status_short:
  1528. file_count_links (st);
  1529. break;
  1530. case dump_status_fail:
  1531. break;
  1532. case dump_status_not_implemented:
  1533. abort ();
  1534. }
  1535. ok = status == dump_status_ok;
  1536. }
  1537. if (ok)
  1538. {
  1539. if (fd < 0)
  1540. {
  1541. errno = - fd;
  1542. ok = false;
  1543. }
  1544. else if (fd == 0)
  1545. {
  1546. if (parentfd < 0 && ! top_level)
  1547. {
  1548. errno = - parentfd;
  1549. ok = false;
  1550. }
  1551. }
  1552. else
  1553. ok = fstat (fd, &st2) == 0;
  1554. if (! ok)
  1555. file_removed_diag (p, top_level, stat_diag);
  1556. }
  1557. if (ok && fd)
  1558. {
  1559. /* Heuristically check whether the file is the same in all
  1560. attributes that tar cares about and can easily check.
  1561. Although the check is not perfect since it does not
  1562. consult file contents, it is typically good enough.
  1563. Do not check atime which is saved only to replace it later.
  1564. Do not check ctime where changes might be benign (e.g.,
  1565. another process creates a hard link to the file). */
  1566. /* If the file's user ID, group ID or mode changed, tar may
  1567. have output the wrong info for the file. */
  1568. ok &= st1.st_uid == st2.st_uid;
  1569. ok &= st1.st_gid == st2.st_gid;
  1570. ok &= st1.st_mode == st2.st_mode;
  1571. /* Likewise for the file's mtime, but skip this check if it
  1572. is a directory possibly updated by --remove-files. */
  1573. if (! (is_dir && remove_files_option))
  1574. ok &= ! timespec_cmp (get_stat_mtime (&st1),
  1575. get_stat_mtime (&st2));
  1576. /* Likewise for the file's size, but skip this check if it
  1577. is a directory as tar does not output directory sizes.
  1578. Although dump_regular_file caught regular file shrinkage,
  1579. it shouldn't hurt to check for shrinkage again now;
  1580. plus, the file may have grown. */
  1581. if (!is_dir)
  1582. ok &= st1.st_size == st2.st_size;
  1583. if (!ok)
  1584. {
  1585. WARNOPT (WARN_FILE_CHANGED,
  1586. (0, 0, _("%s: file changed as we read it"),
  1587. quotearg_colon (p)));
  1588. if (! ignore_failed_read_option)
  1589. set_exit_status (TAREXIT_DIFFERS);
  1590. }
  1591. else if (atime_preserve_option == replace_atime_preserve
  1592. && timespec_cmp (st->atime, get_stat_atime (&st2)) != 0
  1593. && set_file_atime (fd, parentfd, name, st->atime) != 0)
  1594. utime_error (p);
  1595. }
  1596. ok &= tar_stat_close (st);
  1597. if (ok && remove_files_option)
  1598. queue_deferred_unlink (p, is_dir);
  1599. return;
  1600. }
  1601. #ifdef HAVE_READLINK
  1602. else if (S_ISLNK (st->stat.st_mode))
  1603. {
  1604. st->link_name = areadlinkat_with_size (parentfd, name, st->stat.st_size);
  1605. if (!st->link_name)
  1606. {
  1607. if (errno == ENOMEM)
  1608. xalloc_die ();
  1609. file_removed_diag (p, top_level, readlink_diag);
  1610. return;
  1611. }
  1612. transform_name (&st->link_name, XFORM_SYMLINK);
  1613. if (NAME_FIELD_SIZE - (archive_format == OLDGNU_FORMAT)
  1614. < strlen (st->link_name))
  1615. write_long_link (st);
  1616. xattrs_selinux_get (parentfd, name, st, 0);
  1617. xattrs_xattrs_get (parentfd, name, st, 0);
  1618. block_ordinal = current_block_ordinal ();
  1619. st->stat.st_size = 0; /* force 0 size on symlink */
  1620. header = start_header (st);
  1621. if (!header)
  1622. return;
  1623. tar_copy_str (header->header.linkname, st->link_name, NAME_FIELD_SIZE);
  1624. header->header.typeflag = SYMTYPE;
  1625. finish_header (st, header, block_ordinal);
  1626. /* nothing more to do to it */
  1627. if (remove_files_option)
  1628. queue_deferred_unlink (p, false);
  1629. file_count_links (st);
  1630. return;
  1631. }
  1632. #endif
  1633. else if (S_ISCHR (st->stat.st_mode))
  1634. {
  1635. type = CHRTYPE;
  1636. xattrs_acls_get (parentfd, name, st, true);
  1637. xattrs_selinux_get (parentfd, name, st, 0);
  1638. xattrs_xattrs_get (parentfd, name, st, 0);
  1639. }
  1640. else if (S_ISBLK (st->stat.st_mode))
  1641. {
  1642. type = BLKTYPE;
  1643. xattrs_acls_get (parentfd, name, st, true);
  1644. xattrs_selinux_get (parentfd, name, st, 0);
  1645. xattrs_xattrs_get (parentfd, name, st, 0);
  1646. }
  1647. else if (S_ISFIFO (st->stat.st_mode))
  1648. {
  1649. type = FIFOTYPE;
  1650. xattrs_acls_get (parentfd, name, st, true);
  1651. xattrs_selinux_get (parentfd, name, st, 0);
  1652. xattrs_xattrs_get (parentfd, name, st, 0);
  1653. }
  1654. else if (S_ISSOCK (st->stat.st_mode))
  1655. {
  1656. WARNOPT (WARN_FILE_IGNORED,
  1657. (0, 0, _("%s: socket ignored"), quotearg_colon (p)));
  1658. return;
  1659. }
  1660. else if (S_ISDOOR (st->stat.st_mode))
  1661. {
  1662. WARNOPT (WARN_FILE_IGNORED,
  1663. (0, 0, _("%s: door ignored"), quotearg_colon (p)));
  1664. return;
  1665. }
  1666. else
  1667. {
  1668. unknown_file_error (p);
  1669. return;
  1670. }
  1671. if (archive_format == V7_FORMAT)
  1672. {
  1673. unknown_file_error (p);
  1674. return;
  1675. }
  1676. block_ordinal = current_block_ordinal ();
  1677. st->stat.st_size = 0; /* force 0 size */
  1678. header = start_header (st);
  1679. if (!header)
  1680. return;
  1681. header->header.typeflag = type;
  1682. finish_header (st, header, block_ordinal);
  1683. if (remove_files_option)
  1684. queue_deferred_unlink (p, false);
  1685. }
  1686. /* Dump a file, recursively. PARENT describes the file's parent
  1687. directory, NAME is the file's name relative to PARENT, and FULLNAME
  1688. its full name, possibly relative to the working directory. NAME
  1689. may contain slashes at the top level of invocation. */
  1690. void
  1691. dump_file (struct tar_stat_info *parent, char const *name,
  1692. char const *fullname)
  1693. {
  1694. struct tar_stat_info st;
  1695. tar_stat_init (&st);
  1696. st.parent = parent;
  1697. dump_file0 (&st, name, fullname);
  1698. if (parent && listed_incremental_option)
  1699. update_parent_directory (parent);
  1700. tar_stat_destroy (&st);
  1701. }