4
0

create.c 53 KB

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