create.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979
  1. /* Create a tar archive.
  2. Copyright 1985, 1992-1994, 1996-1997, 1999-2001, 2003-2007,
  3. 2009-2010, 2012-2014, 2016-2017 Free Software Foundation, Inc.
  4. This file is part of GNU tar.
  5. GNU tar is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. GNU tar is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. Written by John Gilmore, on 1985-08-25. */
  16. #include <system.h>
  17. #include <areadlink.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[1];
  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 1 if VALUE was negative before being cast
  146. to uintmax_t, 0 otherwise. 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 (int 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 (int negative, uintmax_t value, size_t valsize,
  172. uintmax_t (*substitute) (int *),
  173. char *where, size_t size, const char *type);
  174. static bool
  175. to_chars_subst (int negative, int gnu_format, uintmax_t value, size_t valsize,
  176. uintmax_t (*substitute) (int *),
  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. int 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 1 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 is nonzero) '\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 (int negative, uintmax_t value, size_t valsize,
  244. uintmax_t (*substitute) (int *),
  245. char *where, size_t size, const char *type)
  246. {
  247. int 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] = 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 int warned_once;
  277. if (! warned_once)
  278. {
  279. warned_once = 1;
  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 (int *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. int 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 = 0;
  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 (int *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 (0, 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. else if (archive_format != GNU_FORMAT && archive_format != OLDGNU_FORMAT)
  763. {
  764. if (!(MAJOR_TO_CHARS (0, header->header.devmajor)
  765. && MINOR_TO_CHARS (0, header->header.devminor)))
  766. return NULL;
  767. }
  768. if (archive_format == POSIX_FORMAT)
  769. {
  770. xheader_store ("atime", st, NULL);
  771. xheader_store ("ctime", st, NULL);
  772. }
  773. else if (incremental_option)
  774. if (archive_format == OLDGNU_FORMAT || archive_format == GNU_FORMAT)
  775. {
  776. TIME_TO_CHARS (st->atime.tv_sec, header->oldgnu_header.atime);
  777. TIME_TO_CHARS (st->ctime.tv_sec, header->oldgnu_header.ctime);
  778. }
  779. header->header.typeflag = archive_format == V7_FORMAT ? AREGTYPE : REGTYPE;
  780. switch (archive_format)
  781. {
  782. case V7_FORMAT:
  783. break;
  784. case OLDGNU_FORMAT:
  785. case GNU_FORMAT: /*FIXME?*/
  786. /* Overwrite header->header.magic and header.version in one blow. */
  787. strcpy (header->buffer + offsetof (struct posix_header, magic),
  788. OLDGNU_MAGIC);
  789. break;
  790. case POSIX_FORMAT:
  791. case USTAR_FORMAT:
  792. memcpy (header->header.magic, TMAGIC, TMAGLEN);
  793. memcpy (header->header.version, TVERSION, TVERSLEN);
  794. break;
  795. default:
  796. abort ();
  797. }
  798. if (archive_format == V7_FORMAT || numeric_owner_option)
  799. {
  800. /* header->header.[ug]name are left as the empty string. */
  801. }
  802. else
  803. {
  804. if (uname)
  805. st->uname = xstrdup (uname);
  806. else
  807. uid_to_uname (st->stat.st_uid, &st->uname);
  808. if (gname)
  809. st->gname = xstrdup (gname);
  810. else
  811. gid_to_gname (st->stat.st_gid, &st->gname);
  812. if (archive_format == POSIX_FORMAT
  813. && (strlen (st->uname) > UNAME_FIELD_SIZE
  814. || !string_ascii_p (st->uname)))
  815. xheader_store ("uname", st, NULL);
  816. UNAME_TO_CHARS (st->uname, header->header.uname);
  817. if (archive_format == POSIX_FORMAT
  818. && (strlen (st->gname) > GNAME_FIELD_SIZE
  819. || !string_ascii_p (st->gname)))
  820. xheader_store ("gname", st, NULL);
  821. GNAME_TO_CHARS (st->gname, header->header.gname);
  822. }
  823. if (archive_format == POSIX_FORMAT)
  824. {
  825. if (acls_option > 0)
  826. {
  827. if (st->acls_a_ptr)
  828. xheader_store ("SCHILY.acl.access", st, NULL);
  829. if (st->acls_d_ptr)
  830. xheader_store ("SCHILY.acl.default", st, NULL);
  831. }
  832. if ((selinux_context_option > 0) && st->cntx_name)
  833. xheader_store ("RHT.security.selinux", st, NULL);
  834. if (xattrs_option > 0)
  835. {
  836. size_t scan_xattr = 0;
  837. struct xattr_array *xattr_map = st->xattr_map;
  838. while (scan_xattr < st->xattr_map_size)
  839. {
  840. xheader_store (xattr_map[scan_xattr].xkey, st, &scan_xattr);
  841. ++scan_xattr;
  842. }
  843. }
  844. }
  845. return header;
  846. }
  847. void
  848. simple_finish_header (union block *header)
  849. {
  850. size_t i;
  851. int sum;
  852. char *p;
  853. memcpy (header->header.chksum, CHKBLANKS, sizeof header->header.chksum);
  854. sum = 0;
  855. p = header->buffer;
  856. for (i = sizeof *header; i-- != 0; )
  857. /* We can't use unsigned char here because of old compilers, e.g. V7. */
  858. sum += 0xFF & *p++;
  859. /* Fill in the checksum field. It's formatted differently from the
  860. other fields: it has [6] digits, a null, then a space -- rather than
  861. digits, then a null. We use to_chars.
  862. The final space is already there, from
  863. checksumming, and to_chars doesn't modify it.
  864. This is a fast way to do:
  865. sprintf(header->header.chksum, "%6o", sum); */
  866. uintmax_to_chars ((uintmax_t) sum, header->header.chksum, 7);
  867. set_next_block_after (header);
  868. }
  869. /* Finish off a filled-in header block and write it out. We also
  870. print the file name and/or full info if verbose is on. If BLOCK_ORDINAL
  871. is not negative, is the block ordinal of the first record for this
  872. file, which may be a preceding long name or long link record. */
  873. void
  874. finish_header (struct tar_stat_info *st,
  875. union block *header, off_t block_ordinal)
  876. {
  877. /* Note: It is important to do this before the call to write_extended(),
  878. so that the actual ustar header is printed */
  879. if (verbose_option
  880. && header->header.typeflag != GNUTYPE_LONGLINK
  881. && header->header.typeflag != GNUTYPE_LONGNAME
  882. && header->header.typeflag != XHDTYPE
  883. && header->header.typeflag != XGLTYPE)
  884. {
  885. /* FIXME: This global is used in print_header, sigh. */
  886. current_format = archive_format;
  887. print_header (st, header, block_ordinal);
  888. }
  889. header = write_extended (false, st, header);
  890. simple_finish_header (header);
  891. }
  892. void
  893. pad_archive (off_t size_left)
  894. {
  895. union block *blk;
  896. while (size_left > 0)
  897. {
  898. blk = find_next_block ();
  899. memset (blk->buffer, 0, BLOCKSIZE);
  900. set_next_block_after (blk);
  901. size_left -= BLOCKSIZE;
  902. }
  903. }
  904. static enum dump_status
  905. dump_regular_file (int fd, struct tar_stat_info *st)
  906. {
  907. off_t size_left = st->stat.st_size;
  908. off_t block_ordinal;
  909. union block *blk;
  910. block_ordinal = current_block_ordinal ();
  911. blk = start_header (st);
  912. if (!blk)
  913. return dump_status_fail;
  914. /* Mark contiguous files, if we support them. */
  915. if (archive_format != V7_FORMAT && S_ISCTG (st->stat.st_mode))
  916. blk->header.typeflag = CONTTYPE;
  917. finish_header (st, blk, block_ordinal);
  918. mv_begin_write (st->file_name, st->stat.st_size, st->stat.st_size);
  919. while (size_left > 0)
  920. {
  921. size_t bufsize, count;
  922. blk = find_next_block ();
  923. bufsize = available_space_after (blk);
  924. if (size_left < bufsize)
  925. {
  926. /* Last read -- zero out area beyond. */
  927. bufsize = size_left;
  928. count = bufsize % BLOCKSIZE;
  929. if (count)
  930. memset (blk->buffer + size_left, 0, BLOCKSIZE - count);
  931. }
  932. count = (fd <= 0) ? bufsize : blocking_read (fd, blk->buffer, bufsize);
  933. if (count == SAFE_READ_ERROR)
  934. {
  935. read_diag_details (st->orig_file_name,
  936. st->stat.st_size - size_left, bufsize);
  937. pad_archive (size_left);
  938. return dump_status_short;
  939. }
  940. size_left -= count;
  941. set_next_block_after (blk + (bufsize - 1) / BLOCKSIZE);
  942. if (count != bufsize)
  943. {
  944. char buf[UINTMAX_STRSIZE_BOUND];
  945. memset (blk->buffer + count, 0, bufsize - count);
  946. WARNOPT (WARN_FILE_SHRANK,
  947. (0, 0,
  948. ngettext ("%s: File shrank by %s byte; padding with zeros",
  949. "%s: File shrank by %s bytes; padding with zeros",
  950. size_left),
  951. quotearg_colon (st->orig_file_name),
  952. STRINGIFY_BIGINT (size_left, buf)));
  953. if (! ignore_failed_read_option)
  954. set_exit_status (TAREXIT_DIFFERS);
  955. pad_archive (size_left - (bufsize - count));
  956. return dump_status_short;
  957. }
  958. }
  959. return dump_status_ok;
  960. }
  961. /* Copy info from the directory identified by ST into the archive.
  962. DIRECTORY contains the directory's entries. */
  963. static void
  964. dump_dir0 (struct tar_stat_info *st, char const *directory)
  965. {
  966. bool top_level = ! st->parent;
  967. const char *tag_file_name;
  968. union block *blk = NULL;
  969. off_t block_ordinal = current_block_ordinal ();
  970. st->stat.st_size = 0; /* force 0 size on dir */
  971. blk = start_header (st);
  972. if (!blk)
  973. return;
  974. info_attach_exclist (st);
  975. if (incremental_option && archive_format != POSIX_FORMAT)
  976. blk->header.typeflag = GNUTYPE_DUMPDIR;
  977. else /* if (standard_option) */
  978. blk->header.typeflag = DIRTYPE;
  979. /* If we're gnudumping, we aren't done yet so don't close it. */
  980. if (!incremental_option)
  981. finish_header (st, blk, block_ordinal);
  982. else if (gnu_list_name->directory)
  983. {
  984. if (archive_format == POSIX_FORMAT)
  985. {
  986. xheader_store ("GNU.dumpdir", st,
  987. safe_directory_contents (gnu_list_name->directory));
  988. finish_header (st, blk, block_ordinal);
  989. }
  990. else
  991. {
  992. off_t size_left;
  993. off_t totsize;
  994. size_t bufsize;
  995. ssize_t count;
  996. const char *buffer, *p_buffer;
  997. block_ordinal = current_block_ordinal ();
  998. buffer = safe_directory_contents (gnu_list_name->directory);
  999. totsize = dumpdir_size (buffer);
  1000. OFF_TO_CHARS (totsize, blk->header.size);
  1001. finish_header (st, blk, block_ordinal);
  1002. p_buffer = buffer;
  1003. size_left = totsize;
  1004. mv_begin_write (st->file_name, totsize, totsize);
  1005. while (size_left > 0)
  1006. {
  1007. blk = find_next_block ();
  1008. bufsize = available_space_after (blk);
  1009. if (size_left < bufsize)
  1010. {
  1011. bufsize = size_left;
  1012. count = bufsize % BLOCKSIZE;
  1013. if (count)
  1014. memset (blk->buffer + size_left, 0, BLOCKSIZE - count);
  1015. }
  1016. memcpy (blk->buffer, p_buffer, bufsize);
  1017. size_left -= bufsize;
  1018. p_buffer += bufsize;
  1019. set_next_block_after (blk + (bufsize - 1) / BLOCKSIZE);
  1020. }
  1021. }
  1022. return;
  1023. }
  1024. if (!recursion_option)
  1025. return;
  1026. if (one_file_system_option
  1027. && !top_level
  1028. && st->parent->stat.st_dev != st->stat.st_dev)
  1029. {
  1030. if (verbose_option)
  1031. WARNOPT (WARN_XDEV,
  1032. (0, 0,
  1033. _("%s: file is on a different filesystem; not dumped"),
  1034. quotearg_colon (st->orig_file_name)));
  1035. }
  1036. else
  1037. {
  1038. char *name_buf;
  1039. size_t name_size;
  1040. switch (check_exclusion_tags (st, &tag_file_name))
  1041. {
  1042. case exclusion_tag_all:
  1043. /* Handled in dump_file0 */
  1044. break;
  1045. case exclusion_tag_none:
  1046. {
  1047. char const *entry;
  1048. size_t entry_len;
  1049. size_t name_len;
  1050. name_buf = xstrdup (st->orig_file_name);
  1051. name_size = name_len = strlen (name_buf);
  1052. /* Now output all the files in the directory. */
  1053. for (entry = directory; (entry_len = strlen (entry)) != 0;
  1054. entry += entry_len + 1)
  1055. {
  1056. if (name_size < name_len + entry_len)
  1057. {
  1058. name_size = name_len + entry_len;
  1059. name_buf = xrealloc (name_buf, name_size + 1);
  1060. }
  1061. strcpy (name_buf + name_len, entry);
  1062. if (!excluded_name (name_buf, st))
  1063. dump_file (st, entry, name_buf);
  1064. }
  1065. free (name_buf);
  1066. }
  1067. break;
  1068. case exclusion_tag_contents:
  1069. exclusion_tag_warning (st->orig_file_name, tag_file_name,
  1070. _("contents not dumped"));
  1071. name_size = strlen (st->orig_file_name) + strlen (tag_file_name) + 1;
  1072. name_buf = xmalloc (name_size);
  1073. strcpy (name_buf, st->orig_file_name);
  1074. strcat (name_buf, tag_file_name);
  1075. dump_file (st, tag_file_name, name_buf);
  1076. free (name_buf);
  1077. break;
  1078. case exclusion_tag_under:
  1079. exclusion_tag_warning (st->orig_file_name, tag_file_name,
  1080. _("contents not dumped"));
  1081. break;
  1082. }
  1083. }
  1084. }
  1085. /* Ensure exactly one trailing slash. */
  1086. static void
  1087. ensure_slash (char **pstr)
  1088. {
  1089. size_t len = strlen (*pstr);
  1090. while (len >= 1 && ISSLASH ((*pstr)[len - 1]))
  1091. len--;
  1092. if (!ISSLASH ((*pstr)[len]))
  1093. *pstr = xrealloc (*pstr, len + 2);
  1094. (*pstr)[len++] = '/';
  1095. (*pstr)[len] = '\0';
  1096. }
  1097. /* If we just ran out of file descriptors, release a file descriptor
  1098. in the directory chain somewhere leading from DIR->parent->parent
  1099. up through the root. Return true if successful, false (preserving
  1100. errno == EMFILE) otherwise.
  1101. Do not release DIR's file descriptor, or DIR's parent, as other
  1102. code assumes that they work. On some operating systems, another
  1103. process can claim file descriptor resources as we release them, and
  1104. some calls or their emulations require multiple file descriptors,
  1105. so callers should not give up if a single release doesn't work. */
  1106. static bool
  1107. open_failure_recover (struct tar_stat_info const *dir)
  1108. {
  1109. if (errno == EMFILE && dir && dir->parent)
  1110. {
  1111. struct tar_stat_info *p;
  1112. for (p = dir->parent->parent; p; p = p->parent)
  1113. if (0 < p->fd && (! p->parent || p->parent->fd <= 0))
  1114. {
  1115. tar_stat_close (p);
  1116. return true;
  1117. }
  1118. errno = EMFILE;
  1119. }
  1120. return false;
  1121. }
  1122. /* Return the directory entries of ST, in a dynamically allocated buffer,
  1123. each entry followed by '\0' and the last followed by an extra '\0'.
  1124. Return null on failure, setting errno. */
  1125. char *
  1126. get_directory_entries (struct tar_stat_info *st)
  1127. {
  1128. while (! (st->dirstream = fdopendir (st->fd)))
  1129. if (! open_failure_recover (st))
  1130. return 0;
  1131. return streamsavedir (st->dirstream, savedir_sort_order);
  1132. }
  1133. /* Dump the directory ST. Return true if successful, false (emitting
  1134. diagnostics) otherwise. Get ST's entries, recurse through its
  1135. subdirectories, and clean up file descriptors afterwards. */
  1136. static bool
  1137. dump_dir (struct tar_stat_info *st)
  1138. {
  1139. char *directory = get_directory_entries (st);
  1140. if (! directory)
  1141. {
  1142. savedir_diag (st->orig_file_name);
  1143. return false;
  1144. }
  1145. dump_dir0 (st, directory);
  1146. restore_parent_fd (st);
  1147. free (directory);
  1148. return true;
  1149. }
  1150. /* Number of links a file can have without having to be entered into
  1151. the link table. Typically this is 1, but in trickier circumstances
  1152. it is 0. */
  1153. static nlink_t trivial_link_count;
  1154. /* Main functions of this module. */
  1155. void
  1156. create_archive (void)
  1157. {
  1158. struct name const *p;
  1159. trivial_link_count = name_count <= 1 && ! dereference_option;
  1160. open_archive (ACCESS_WRITE);
  1161. buffer_write_global_xheader ();
  1162. if (incremental_option)
  1163. {
  1164. size_t buffer_size = 1000;
  1165. char *buffer = xmalloc (buffer_size);
  1166. const char *q;
  1167. collect_and_sort_names ();
  1168. while ((p = name_from_list ()) != NULL)
  1169. if (!excluded_name (p->name, NULL))
  1170. dump_file (0, p->name, p->name);
  1171. blank_name_list ();
  1172. while ((p = name_from_list ()) != NULL)
  1173. if (!excluded_name (p->name, NULL))
  1174. {
  1175. struct tar_stat_info st;
  1176. size_t plen = strlen (p->name);
  1177. if (buffer_size <= plen)
  1178. {
  1179. while ((buffer_size *= 2) <= plen)
  1180. continue;
  1181. buffer = xrealloc (buffer, buffer_size);
  1182. }
  1183. memcpy (buffer, p->name, plen);
  1184. if (! ISSLASH (buffer[plen - 1]))
  1185. buffer[plen++] = DIRECTORY_SEPARATOR;
  1186. tar_stat_init (&st);
  1187. q = directory_contents (p->directory);
  1188. if (q)
  1189. while (*q)
  1190. {
  1191. size_t qlen = strlen (q);
  1192. if (*q == 'Y')
  1193. {
  1194. if (! st.orig_file_name)
  1195. {
  1196. int fd = openat (chdir_fd, p->name,
  1197. open_searchdir_flags);
  1198. if (fd < 0)
  1199. {
  1200. file_removed_diag (p->name, !p->parent,
  1201. open_diag);
  1202. break;
  1203. }
  1204. st.fd = fd;
  1205. if (fstat (fd, &st.stat) != 0)
  1206. {
  1207. file_removed_diag (p->name, !p->parent,
  1208. stat_diag);
  1209. break;
  1210. }
  1211. st.orig_file_name = xstrdup (p->name);
  1212. }
  1213. if (buffer_size < plen + qlen)
  1214. {
  1215. while ((buffer_size *=2 ) < plen + qlen)
  1216. continue;
  1217. buffer = xrealloc (buffer, buffer_size);
  1218. }
  1219. strcpy (buffer + plen, q + 1);
  1220. dump_file (&st, q + 1, buffer);
  1221. }
  1222. q += qlen + 1;
  1223. }
  1224. tar_stat_destroy (&st);
  1225. }
  1226. free (buffer);
  1227. }
  1228. else
  1229. {
  1230. const char *name;
  1231. while ((name = name_next (1)) != NULL)
  1232. if (!excluded_name (name, NULL))
  1233. dump_file (0, name, name);
  1234. }
  1235. write_eot ();
  1236. close_archive ();
  1237. finish_deferred_unlinks ();
  1238. if (listed_incremental_option)
  1239. write_directory_file ();
  1240. }
  1241. /* Calculate the hash of a link. */
  1242. static size_t
  1243. hash_link (void const *entry, size_t n_buckets)
  1244. {
  1245. struct link const *l = entry;
  1246. uintmax_t num = l->dev ^ l->ino;
  1247. return num % n_buckets;
  1248. }
  1249. /* Compare two links for equality. */
  1250. static bool
  1251. compare_links (void const *entry1, void const *entry2)
  1252. {
  1253. struct link const *link1 = entry1;
  1254. struct link const *link2 = entry2;
  1255. return ((link1->dev ^ link2->dev) | (link1->ino ^ link2->ino)) == 0;
  1256. }
  1257. static void
  1258. unknown_file_error (char const *p)
  1259. {
  1260. WARNOPT (WARN_FILE_IGNORED,
  1261. (0, 0, _("%s: Unknown file type; file ignored"),
  1262. quotearg_colon (p)));
  1263. if (!ignore_failed_read_option)
  1264. set_exit_status (TAREXIT_FAILURE);
  1265. }
  1266. /* Handling of hard links */
  1267. /* Table of all non-directories that we've written so far. Any time
  1268. we see another, we check the table and avoid dumping the data
  1269. again if we've done it once already. */
  1270. static Hash_table *link_table;
  1271. /* Try to dump stat as a hard link to another file in the archive.
  1272. Return true if successful. */
  1273. static bool
  1274. dump_hard_link (struct tar_stat_info *st)
  1275. {
  1276. if (link_table
  1277. && (trivial_link_count < st->stat.st_nlink || remove_files_option))
  1278. {
  1279. struct link lp;
  1280. struct link *duplicate;
  1281. off_t block_ordinal;
  1282. union block *blk;
  1283. lp.ino = st->stat.st_ino;
  1284. lp.dev = st->stat.st_dev;
  1285. if ((duplicate = hash_lookup (link_table, &lp)))
  1286. {
  1287. /* We found a link. */
  1288. char const *link_name = safer_name_suffix (duplicate->name, true,
  1289. absolute_names_option);
  1290. if (duplicate->nlink)
  1291. duplicate->nlink--;
  1292. block_ordinal = current_block_ordinal ();
  1293. assign_string (&st->link_name, link_name);
  1294. if (NAME_FIELD_SIZE - (archive_format == OLDGNU_FORMAT)
  1295. < strlen (link_name))
  1296. write_long_link (st);
  1297. st->stat.st_size = 0;
  1298. blk = start_header (st);
  1299. if (!blk)
  1300. return false;
  1301. tar_copy_str (blk->header.linkname, link_name, NAME_FIELD_SIZE);
  1302. blk->header.typeflag = LNKTYPE;
  1303. finish_header (st, blk, block_ordinal);
  1304. if (remove_files_option)
  1305. queue_deferred_unlink (st->orig_file_name, false);
  1306. return true;
  1307. }
  1308. }
  1309. return false;
  1310. }
  1311. static void
  1312. file_count_links (struct tar_stat_info *st)
  1313. {
  1314. if (hard_dereference_option)
  1315. return;
  1316. if (trivial_link_count < st->stat.st_nlink)
  1317. {
  1318. struct link *duplicate;
  1319. char *linkname = NULL;
  1320. struct link *lp;
  1321. assign_string (&linkname, safer_name_suffix (st->orig_file_name, true,
  1322. absolute_names_option));
  1323. transform_name (&linkname, XFORM_LINK);
  1324. lp = xmalloc (offsetof (struct link, name)
  1325. + strlen (linkname) + 1);
  1326. lp->ino = st->stat.st_ino;
  1327. lp->dev = st->stat.st_dev;
  1328. lp->nlink = st->stat.st_nlink;
  1329. strcpy (lp->name, linkname);
  1330. free (linkname);
  1331. if (! ((link_table
  1332. || (link_table = hash_initialize (0, 0, hash_link,
  1333. compare_links, 0)))
  1334. && (duplicate = hash_insert (link_table, lp))))
  1335. xalloc_die ();
  1336. if (duplicate != lp)
  1337. abort ();
  1338. lp->nlink--;
  1339. }
  1340. }
  1341. /* For each dumped file, check if all its links were dumped. Emit
  1342. warnings if it is not so. */
  1343. void
  1344. check_links (void)
  1345. {
  1346. struct link *lp;
  1347. if (!link_table)
  1348. return;
  1349. for (lp = hash_get_first (link_table); lp;
  1350. lp = hash_get_next (link_table, lp))
  1351. {
  1352. if (lp->nlink)
  1353. {
  1354. WARN ((0, 0, _("Missing links to %s."), quote (lp->name)));
  1355. }
  1356. }
  1357. }
  1358. /* Assuming DIR is the working directory, open FILE, using FLAGS to
  1359. control the open. A null DIR means to use ".". If we are low on
  1360. file descriptors, try to release one or more from DIR's parents to
  1361. reuse it. */
  1362. int
  1363. subfile_open (struct tar_stat_info const *dir, char const *file, int flags)
  1364. {
  1365. int fd;
  1366. static bool initialized;
  1367. if (! initialized)
  1368. {
  1369. /* Initialize any tables that might be needed when file
  1370. descriptors are exhausted, and whose initialization might
  1371. require a file descriptor. This includes the system message
  1372. catalog and tar's message catalog. */
  1373. initialized = true;
  1374. strerror (ENOENT);
  1375. gettext ("");
  1376. }
  1377. while ((fd = openat (dir ? dir->fd : chdir_fd, file, flags)) < 0
  1378. && open_failure_recover (dir))
  1379. continue;
  1380. return fd;
  1381. }
  1382. /* Restore the file descriptor for ST->parent, if it was temporarily
  1383. closed to conserve file descriptors. On failure, set the file
  1384. descriptor to the negative of the corresponding errno value. Call
  1385. this every time a subdirectory is ascended from. */
  1386. void
  1387. restore_parent_fd (struct tar_stat_info const *st)
  1388. {
  1389. struct tar_stat_info *parent = st->parent;
  1390. if (parent && ! parent->fd)
  1391. {
  1392. int parentfd = openat (st->fd, "..", open_searchdir_flags);
  1393. struct stat parentstat;
  1394. if (parentfd < 0)
  1395. parentfd = - errno;
  1396. else if (! (fstat (parentfd, &parentstat) == 0
  1397. && parent->stat.st_ino == parentstat.st_ino
  1398. && parent->stat.st_dev == parentstat.st_dev))
  1399. {
  1400. close (parentfd);
  1401. parentfd = IMPOSTOR_ERRNO;
  1402. }
  1403. if (parentfd < 0)
  1404. {
  1405. int origfd = openat (chdir_fd, parent->orig_file_name,
  1406. open_searchdir_flags);
  1407. if (0 <= origfd)
  1408. {
  1409. if (fstat (parentfd, &parentstat) == 0
  1410. && parent->stat.st_ino == parentstat.st_ino
  1411. && parent->stat.st_dev == parentstat.st_dev)
  1412. parentfd = origfd;
  1413. else
  1414. close (origfd);
  1415. }
  1416. }
  1417. parent->fd = parentfd;
  1418. }
  1419. }
  1420. /* Dump a single file, recursing on directories. ST is the file's
  1421. status info, NAME its name relative to the parent directory, and P
  1422. its full name (which may be relative to the working directory). */
  1423. /* FIXME: One should make sure that for *every* path leading to setting
  1424. exit_status to failure, a clear diagnostic has been issued. */
  1425. static void
  1426. dump_file0 (struct tar_stat_info *st, char const *name, char const *p)
  1427. {
  1428. union block *header;
  1429. char type;
  1430. off_t original_size;
  1431. struct timespec original_ctime;
  1432. off_t block_ordinal = -1;
  1433. int fd = 0;
  1434. bool is_dir;
  1435. struct tar_stat_info const *parent = st->parent;
  1436. bool top_level = ! parent;
  1437. int parentfd = top_level ? chdir_fd : parent->fd;
  1438. void (*diag) (char const *) = 0;
  1439. if (interactive_option && !confirm ("add", p))
  1440. return;
  1441. assign_string (&st->orig_file_name, p);
  1442. assign_string (&st->file_name,
  1443. safer_name_suffix (p, false, absolute_names_option));
  1444. transform_name (&st->file_name, XFORM_REGFILE);
  1445. if (parentfd < 0 && ! top_level)
  1446. {
  1447. errno = - parentfd;
  1448. diag = open_diag;
  1449. }
  1450. else if (fstatat (parentfd, name, &st->stat, fstatat_flags) != 0)
  1451. diag = stat_diag;
  1452. else if (file_dumpable_p (&st->stat))
  1453. {
  1454. fd = subfile_open (parent, name, open_read_flags);
  1455. if (fd < 0)
  1456. diag = open_diag;
  1457. else
  1458. {
  1459. st->fd = fd;
  1460. if (fstat (fd, &st->stat) != 0)
  1461. diag = stat_diag;
  1462. }
  1463. }
  1464. if (diag)
  1465. {
  1466. file_removed_diag (p, top_level, diag);
  1467. return;
  1468. }
  1469. st->archive_file_size = original_size = st->stat.st_size;
  1470. st->atime = get_stat_atime (&st->stat);
  1471. st->mtime = get_stat_mtime (&st->stat);
  1472. st->ctime = original_ctime = get_stat_ctime (&st->stat);
  1473. #ifdef S_ISHIDDEN
  1474. if (S_ISHIDDEN (st->stat.st_mode))
  1475. {
  1476. char *new = (char *) alloca (strlen (p) + 2);
  1477. if (new)
  1478. {
  1479. strcpy (new, p);
  1480. strcat (new, "@");
  1481. p = new;
  1482. }
  1483. }
  1484. #endif
  1485. /* See if we want only new files, and check if this one is too old to
  1486. put in the archive.
  1487. This check is omitted if incremental_option is set *and* the
  1488. requested file is not explicitly listed in the command line. */
  1489. if (! (incremental_option && ! top_level)
  1490. && !S_ISDIR (st->stat.st_mode)
  1491. && OLDER_TAR_STAT_TIME (*st, m)
  1492. && (!after_date_option || OLDER_TAR_STAT_TIME (*st, c)))
  1493. {
  1494. if (!incremental_option && verbose_option)
  1495. WARNOPT (WARN_FILE_UNCHANGED,
  1496. (0, 0, _("%s: file is unchanged; not dumped"),
  1497. quotearg_colon (p)));
  1498. return;
  1499. }
  1500. /* See if we are trying to dump the archive. */
  1501. if (sys_file_is_archive (st))
  1502. {
  1503. WARNOPT (WARN_IGNORE_ARCHIVE,
  1504. (0, 0, _("%s: file is the archive; not dumped"),
  1505. quotearg_colon (p)));
  1506. return;
  1507. }
  1508. is_dir = S_ISDIR (st->stat.st_mode) != 0;
  1509. if (!is_dir && dump_hard_link (st))
  1510. return;
  1511. if (is_dir || S_ISREG (st->stat.st_mode) || S_ISCTG (st->stat.st_mode))
  1512. {
  1513. bool ok;
  1514. struct stat final_stat;
  1515. xattrs_acls_get (parentfd, name, st, 0, !is_dir);
  1516. xattrs_selinux_get (parentfd, name, st, fd);
  1517. xattrs_xattrs_get (parentfd, name, st, fd);
  1518. if (is_dir)
  1519. {
  1520. const char *tag_file_name;
  1521. ensure_slash (&st->orig_file_name);
  1522. ensure_slash (&st->file_name);
  1523. if (check_exclusion_tags (st, &tag_file_name) == exclusion_tag_all)
  1524. {
  1525. exclusion_tag_warning (st->orig_file_name, tag_file_name,
  1526. _("directory not dumped"));
  1527. return;
  1528. }
  1529. ok = dump_dir (st);
  1530. fd = st->fd;
  1531. parentfd = top_level ? chdir_fd : parent->fd;
  1532. }
  1533. else
  1534. {
  1535. enum dump_status status;
  1536. if (fd && sparse_option && ST_IS_SPARSE (st->stat))
  1537. {
  1538. status = sparse_dump_file (fd, st);
  1539. if (status == dump_status_not_implemented)
  1540. status = dump_regular_file (fd, st);
  1541. }
  1542. else
  1543. status = dump_regular_file (fd, st);
  1544. switch (status)
  1545. {
  1546. case dump_status_ok:
  1547. case dump_status_short:
  1548. file_count_links (st);
  1549. break;
  1550. case dump_status_fail:
  1551. break;
  1552. case dump_status_not_implemented:
  1553. abort ();
  1554. }
  1555. ok = status == dump_status_ok;
  1556. }
  1557. if (ok)
  1558. {
  1559. if (fd < 0)
  1560. {
  1561. errno = - fd;
  1562. ok = false;
  1563. }
  1564. else if (fd == 0)
  1565. {
  1566. if (parentfd < 0 && ! top_level)
  1567. {
  1568. errno = - parentfd;
  1569. ok = false;
  1570. }
  1571. else
  1572. ok = fstatat (parentfd, name, &final_stat, fstatat_flags) == 0;
  1573. }
  1574. else
  1575. ok = fstat (fd, &final_stat) == 0;
  1576. if (! ok)
  1577. file_removed_diag (p, top_level, stat_diag);
  1578. }
  1579. if (ok)
  1580. {
  1581. if ((timespec_cmp (get_stat_ctime (&final_stat), original_ctime) != 0
  1582. /* Original ctime will change if the file is a directory and
  1583. --remove-files is given */
  1584. && !(remove_files_option && is_dir))
  1585. || original_size < final_stat.st_size)
  1586. {
  1587. WARNOPT (WARN_FILE_CHANGED,
  1588. (0, 0, _("%s: file changed as we read it"),
  1589. quotearg_colon (p)));
  1590. set_exit_status (TAREXIT_DIFFERS);
  1591. }
  1592. else if (atime_preserve_option == replace_atime_preserve
  1593. && fd && (is_dir || original_size != 0)
  1594. && set_file_atime (fd, parentfd, name, st->atime) != 0)
  1595. utime_error (p);
  1596. }
  1597. ok &= tar_stat_close (st);
  1598. if (ok && remove_files_option)
  1599. queue_deferred_unlink (p, is_dir);
  1600. return;
  1601. }
  1602. #ifdef HAVE_READLINK
  1603. else if (S_ISLNK (st->stat.st_mode))
  1604. {
  1605. st->link_name = areadlinkat_with_size (parentfd, name, st->stat.st_size);
  1606. if (!st->link_name)
  1607. {
  1608. if (errno == ENOMEM)
  1609. xalloc_die ();
  1610. file_removed_diag (p, top_level, readlink_diag);
  1611. return;
  1612. }
  1613. transform_name (&st->link_name, XFORM_SYMLINK);
  1614. if (NAME_FIELD_SIZE - (archive_format == OLDGNU_FORMAT)
  1615. < strlen (st->link_name))
  1616. write_long_link (st);
  1617. xattrs_selinux_get (parentfd, name, st, 0);
  1618. xattrs_xattrs_get (parentfd, name, st, 0);
  1619. block_ordinal = current_block_ordinal ();
  1620. st->stat.st_size = 0; /* force 0 size on symlink */
  1621. header = start_header (st);
  1622. if (!header)
  1623. return;
  1624. tar_copy_str (header->header.linkname, st->link_name, NAME_FIELD_SIZE);
  1625. header->header.typeflag = SYMTYPE;
  1626. finish_header (st, header, block_ordinal);
  1627. /* nothing more to do to it */
  1628. if (remove_files_option)
  1629. queue_deferred_unlink (p, false);
  1630. file_count_links (st);
  1631. return;
  1632. }
  1633. #endif
  1634. else if (S_ISCHR (st->stat.st_mode))
  1635. {
  1636. type = CHRTYPE;
  1637. xattrs_acls_get (parentfd, name, st, 0, true);
  1638. xattrs_selinux_get (parentfd, name, st, 0);
  1639. xattrs_xattrs_get (parentfd, name, st, 0);
  1640. }
  1641. else if (S_ISBLK (st->stat.st_mode))
  1642. {
  1643. type = BLKTYPE;
  1644. xattrs_acls_get (parentfd, name, st, 0, true);
  1645. xattrs_selinux_get (parentfd, name, st, 0);
  1646. xattrs_xattrs_get (parentfd, name, st, 0);
  1647. }
  1648. else if (S_ISFIFO (st->stat.st_mode))
  1649. {
  1650. type = FIFOTYPE;
  1651. xattrs_acls_get (parentfd, name, st, 0, true);
  1652. xattrs_selinux_get (parentfd, name, st, 0);
  1653. xattrs_xattrs_get (parentfd, name, st, 0);
  1654. }
  1655. else if (S_ISSOCK (st->stat.st_mode))
  1656. {
  1657. WARNOPT (WARN_FILE_IGNORED,
  1658. (0, 0, _("%s: socket ignored"), quotearg_colon (p)));
  1659. return;
  1660. }
  1661. else if (S_ISDOOR (st->stat.st_mode))
  1662. {
  1663. WARNOPT (WARN_FILE_IGNORED,
  1664. (0, 0, _("%s: door ignored"), quotearg_colon (p)));
  1665. return;
  1666. }
  1667. else
  1668. {
  1669. unknown_file_error (p);
  1670. return;
  1671. }
  1672. if (archive_format == V7_FORMAT)
  1673. {
  1674. unknown_file_error (p);
  1675. return;
  1676. }
  1677. block_ordinal = current_block_ordinal ();
  1678. st->stat.st_size = 0; /* force 0 size */
  1679. header = start_header (st);
  1680. if (!header)
  1681. return;
  1682. header->header.typeflag = type;
  1683. if (type != FIFOTYPE)
  1684. {
  1685. MAJOR_TO_CHARS (major (st->stat.st_rdev),
  1686. header->header.devmajor);
  1687. MINOR_TO_CHARS (minor (st->stat.st_rdev),
  1688. header->header.devminor);
  1689. }
  1690. finish_header (st, header, block_ordinal);
  1691. if (remove_files_option)
  1692. queue_deferred_unlink (p, false);
  1693. }
  1694. /* Dump a file, recursively. PARENT describes the file's parent
  1695. directory, NAME is the file's name relative to PARENT, and FULLNAME
  1696. its full name, possibly relative to the working directory. NAME
  1697. may contain slashes at the top level of invocation. */
  1698. void
  1699. dump_file (struct tar_stat_info *parent, char const *name,
  1700. char const *fullname)
  1701. {
  1702. struct tar_stat_info st;
  1703. tar_stat_init (&st);
  1704. st.parent = parent;
  1705. dump_file0 (&st, name, fullname);
  1706. if (parent && listed_incremental_option)
  1707. update_parent_directory (parent);
  1708. tar_stat_destroy (&st);
  1709. }