create.c 51 KB

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