create.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623
  1. /* Create a tar archive.
  2. Copyright (C) 1985, 1992, 1993, 1994, 1996, 1997, 1999, 2000, 2001,
  3. 2003, 2004, 2005 Free Software Foundation, Inc.
  4. Written by John Gilmore, on 1985-08-25.
  5. This program is free software; you can redistribute it and/or modify it
  6. under the terms of the GNU General Public License as published by the
  7. Free Software Foundation; either version 2, or (at your option) any later
  8. version.
  9. This program is distributed in the hope that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
  12. Public License for more details.
  13. You should have received a copy of the GNU General Public License along
  14. with this program; if not, write to the Free Software Foundation, Inc.,
  15. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
  16. #include <system.h>
  17. #include <quotearg.h>
  18. #include <utimens.h>
  19. #include "common.h"
  20. #include <hash.h>
  21. struct link
  22. {
  23. dev_t dev;
  24. ino_t ino;
  25. size_t nlink;
  26. char name[1];
  27. };
  28. /* The maximum uintmax_t value that can be represented with DIGITS digits,
  29. assuming that each digit is BITS_PER_DIGIT wide. */
  30. #define MAX_VAL_WITH_DIGITS(digits, bits_per_digit) \
  31. ((digits) * (bits_per_digit) < sizeof (uintmax_t) * CHAR_BIT \
  32. ? ((uintmax_t) 1 << ((digits) * (bits_per_digit))) - 1 \
  33. : (uintmax_t) -1)
  34. /* The maximum uintmax_t value that can be represented with octal
  35. digits and a trailing NUL in BUFFER. */
  36. #define MAX_OCTAL_VAL(buffer) MAX_VAL_WITH_DIGITS (sizeof (buffer) - 1, LG_8)
  37. /* Convert VALUE to an octal representation suitable for tar headers.
  38. Output to buffer WHERE with size SIZE.
  39. The result is undefined if SIZE is 0 or if VALUE is too large to fit. */
  40. static void
  41. to_octal (uintmax_t value, char *where, size_t size)
  42. {
  43. uintmax_t v = value;
  44. size_t i = size;
  45. do
  46. {
  47. where[--i] = '0' + (v & ((1 << LG_8) - 1));
  48. v >>= LG_8;
  49. }
  50. while (i);
  51. }
  52. /* Copy at most LEN bytes from the string SRC to DST. Terminate with
  53. NUL unless SRC is LEN or more bytes long. */
  54. static void
  55. tar_copy_str (char *dst, const char *src, size_t len)
  56. {
  57. size_t i;
  58. for (i = 0; i < len; i++)
  59. if (! (dst[i] = src[i]))
  60. break;
  61. }
  62. /* Same as tar_copy_str, but always terminate with NUL if using
  63. is OLDGNU format */
  64. static void
  65. tar_name_copy_str (char *dst, const char *src, size_t len)
  66. {
  67. tar_copy_str (dst, src, len);
  68. if (archive_format == OLDGNU_FORMAT)
  69. dst[len-1] = 0;
  70. }
  71. /* Convert NEGATIVE VALUE to a base-256 representation suitable for
  72. tar headers. NEGATIVE is 1 if VALUE was negative before being cast
  73. to uintmax_t, 0 otherwise. Output to buffer WHERE with size SIZE.
  74. The result is undefined if SIZE is 0 or if VALUE is too large to
  75. fit. */
  76. static void
  77. to_base256 (int negative, uintmax_t value, char *where, size_t size)
  78. {
  79. uintmax_t v = value;
  80. uintmax_t propagated_sign_bits =
  81. ((uintmax_t) - negative << (CHAR_BIT * sizeof v - LG_256));
  82. size_t i = size;
  83. do
  84. {
  85. where[--i] = v & ((1 << LG_256) - 1);
  86. v = propagated_sign_bits | (v >> LG_256);
  87. }
  88. while (i);
  89. }
  90. /* Convert NEGATIVE VALUE (which was originally of size VALSIZE) to
  91. external form, using SUBSTITUTE (...) if VALUE won't fit. Output
  92. to buffer WHERE with size SIZE. NEGATIVE is 1 iff VALUE was
  93. negative before being cast to uintmax_t; its original bitpattern
  94. can be deduced from VALSIZE, its original size before casting.
  95. TYPE is the kind of value being output (useful for diagnostics).
  96. Prefer the POSIX format of SIZE - 1 octal digits (with leading zero
  97. digits), followed by '\0'. If this won't work, and if GNU or
  98. OLDGNU format is allowed, use '\200' followed by base-256, or (if
  99. NEGATIVE is nonzero) '\377' followed by two's complement base-256.
  100. If neither format works, use SUBSTITUTE (...) instead. Pass to
  101. SUBSTITUTE the address of an 0-or-1 flag recording whether the
  102. substitute value is negative. */
  103. static void
  104. to_chars (int negative, uintmax_t value, size_t valsize,
  105. uintmax_t (*substitute) (int *),
  106. char *where, size_t size, const char *type)
  107. {
  108. int base256_allowed = (archive_format == GNU_FORMAT
  109. || archive_format == OLDGNU_FORMAT);
  110. /* Generate the POSIX octal representation if the number fits. */
  111. if (! negative && value <= MAX_VAL_WITH_DIGITS (size - 1, LG_8))
  112. {
  113. where[size - 1] = '\0';
  114. to_octal (value, where, size - 1);
  115. }
  116. /* Otherwise, generate the base-256 representation if we are
  117. generating an old or new GNU format and if the number fits. */
  118. else if (((negative ? -1 - value : value)
  119. <= MAX_VAL_WITH_DIGITS (size - 1, LG_256))
  120. && base256_allowed)
  121. {
  122. where[0] = negative ? -1 : 1 << (LG_256 - 1);
  123. to_base256 (negative, value, where + 1, size - 1);
  124. }
  125. /* Otherwise, if the number is negative, and if it would not cause
  126. ambiguity on this host by confusing positive with negative
  127. values, then generate the POSIX octal representation of the value
  128. modulo 2**(field bits). The resulting tar file is
  129. machine-dependent, since it depends on the host word size. Yuck!
  130. But this is the traditional behavior. */
  131. else if (negative && valsize * CHAR_BIT <= (size - 1) * LG_8)
  132. {
  133. static int warned_once;
  134. if (! warned_once)
  135. {
  136. warned_once = 1;
  137. WARN ((0, 0, _("Generating negative octal headers")));
  138. }
  139. where[size - 1] = '\0';
  140. to_octal (value & MAX_VAL_WITH_DIGITS (valsize * CHAR_BIT, 1),
  141. where, size - 1);
  142. }
  143. /* Otherwise, output a substitute value if possible (with a
  144. warning), and an error message if not. */
  145. else
  146. {
  147. uintmax_t maxval = (base256_allowed
  148. ? MAX_VAL_WITH_DIGITS (size - 1, LG_256)
  149. : MAX_VAL_WITH_DIGITS (size - 1, LG_8));
  150. char valbuf[UINTMAX_STRSIZE_BOUND + 1];
  151. char maxbuf[UINTMAX_STRSIZE_BOUND];
  152. char minbuf[UINTMAX_STRSIZE_BOUND + 1];
  153. char const *minval_string;
  154. char const *maxval_string = STRINGIFY_BIGINT (maxval, maxbuf);
  155. char const *value_string;
  156. if (base256_allowed)
  157. {
  158. uintmax_t m = maxval + 1 ? maxval + 1 : maxval / 2 + 1;
  159. char *p = STRINGIFY_BIGINT (m, minbuf + 1);
  160. *--p = '-';
  161. minval_string = p;
  162. }
  163. else
  164. minval_string = "0";
  165. if (negative)
  166. {
  167. char *p = STRINGIFY_BIGINT (- value, valbuf + 1);
  168. *--p = '-';
  169. value_string = p;
  170. }
  171. else
  172. value_string = STRINGIFY_BIGINT (value, valbuf);
  173. if (substitute)
  174. {
  175. int negsub;
  176. uintmax_t sub = substitute (&negsub) & maxval;
  177. /* FIXME: This is the only place where GNU_FORMAT differs from
  178. OLDGNU_FORMAT. Apart from this they are completely identical. */
  179. uintmax_t s = (negsub &= archive_format == GNU_FORMAT) ? - sub : sub;
  180. char subbuf[UINTMAX_STRSIZE_BOUND + 1];
  181. char *sub_string = STRINGIFY_BIGINT (s, subbuf + 1);
  182. if (negsub)
  183. *--sub_string = '-';
  184. WARN ((0, 0, _("value %s out of %s range %s..%s; substituting %s"),
  185. value_string, type, minval_string, maxval_string,
  186. sub_string));
  187. to_chars (negsub, s, valsize, 0, where, size, type);
  188. }
  189. else
  190. ERROR ((0, 0, _("value %s out of %s range %s..%s"),
  191. value_string, type, minval_string, maxval_string));
  192. }
  193. }
  194. static uintmax_t
  195. gid_substitute (int *negative)
  196. {
  197. gid_t r;
  198. #ifdef GID_NOBODY
  199. r = GID_NOBODY;
  200. #else
  201. static gid_t gid_nobody;
  202. if (!gid_nobody && !gname_to_gid ("nobody", &gid_nobody))
  203. gid_nobody = -2;
  204. r = gid_nobody;
  205. #endif
  206. *negative = r < 0;
  207. return r;
  208. }
  209. void
  210. gid_to_chars (gid_t v, char *p, size_t s)
  211. {
  212. to_chars (v < 0, (uintmax_t) v, sizeof v, gid_substitute, p, s, "gid_t");
  213. }
  214. void
  215. major_to_chars (major_t v, char *p, size_t s)
  216. {
  217. to_chars (v < 0, (uintmax_t) v, sizeof v, 0, p, s, "major_t");
  218. }
  219. void
  220. minor_to_chars (minor_t v, char *p, size_t s)
  221. {
  222. to_chars (v < 0, (uintmax_t) v, sizeof v, 0, p, s, "minor_t");
  223. }
  224. void
  225. mode_to_chars (mode_t v, char *p, size_t s)
  226. {
  227. /* In the common case where the internal and external mode bits are the same,
  228. and we are not using POSIX or GNU format,
  229. propagate all unknown bits to the external mode.
  230. This matches historical practice.
  231. Otherwise, just copy the bits we know about. */
  232. int negative;
  233. uintmax_t u;
  234. if (S_ISUID == TSUID && S_ISGID == TSGID && S_ISVTX == TSVTX
  235. && S_IRUSR == TUREAD && S_IWUSR == TUWRITE && S_IXUSR == TUEXEC
  236. && S_IRGRP == TGREAD && S_IWGRP == TGWRITE && S_IXGRP == TGEXEC
  237. && S_IROTH == TOREAD && S_IWOTH == TOWRITE && S_IXOTH == TOEXEC
  238. && archive_format != POSIX_FORMAT
  239. && archive_format != USTAR_FORMAT
  240. && archive_format != GNU_FORMAT)
  241. {
  242. negative = v < 0;
  243. u = v;
  244. }
  245. else
  246. {
  247. negative = 0;
  248. u = ((v & S_ISUID ? TSUID : 0)
  249. | (v & S_ISGID ? TSGID : 0)
  250. | (v & S_ISVTX ? TSVTX : 0)
  251. | (v & S_IRUSR ? TUREAD : 0)
  252. | (v & S_IWUSR ? TUWRITE : 0)
  253. | (v & S_IXUSR ? TUEXEC : 0)
  254. | (v & S_IRGRP ? TGREAD : 0)
  255. | (v & S_IWGRP ? TGWRITE : 0)
  256. | (v & S_IXGRP ? TGEXEC : 0)
  257. | (v & S_IROTH ? TOREAD : 0)
  258. | (v & S_IWOTH ? TOWRITE : 0)
  259. | (v & S_IXOTH ? TOEXEC : 0));
  260. }
  261. to_chars (negative, u, sizeof v, 0, p, s, "mode_t");
  262. }
  263. void
  264. off_to_chars (off_t v, char *p, size_t s)
  265. {
  266. to_chars (v < 0, (uintmax_t) v, sizeof v, 0, p, s, "off_t");
  267. }
  268. void
  269. size_to_chars (size_t v, char *p, size_t s)
  270. {
  271. to_chars (0, (uintmax_t) v, sizeof v, 0, p, s, "size_t");
  272. }
  273. void
  274. time_to_chars (time_t v, char *p, size_t s)
  275. {
  276. to_chars (v < 0, (uintmax_t) v, sizeof v, 0, p, s, "time_t");
  277. }
  278. static uintmax_t
  279. uid_substitute (int *negative)
  280. {
  281. uid_t r;
  282. #ifdef UID_NOBODY
  283. r = UID_NOBODY;
  284. #else
  285. static uid_t uid_nobody;
  286. if (!uid_nobody && !uname_to_uid ("nobody", &uid_nobody))
  287. uid_nobody = -2;
  288. r = uid_nobody;
  289. #endif
  290. *negative = r < 0;
  291. return r;
  292. }
  293. void
  294. uid_to_chars (uid_t v, char *p, size_t s)
  295. {
  296. to_chars (v < 0, (uintmax_t) v, sizeof v, uid_substitute, p, s, "uid_t");
  297. }
  298. void
  299. uintmax_to_chars (uintmax_t v, char *p, size_t s)
  300. {
  301. to_chars (0, v, sizeof v, 0, p, s, "uintmax_t");
  302. }
  303. void
  304. string_to_chars (char const *str, char *p, size_t s)
  305. {
  306. tar_copy_str (p, str, s);
  307. p[s - 1] = '\0';
  308. }
  309. /* A file is considered dumpable if it is sparse and both --sparse and --totals
  310. are specified.
  311. Otherwise, it is dumpable unless any of the following conditions occur:
  312. a) it is empty *and* world-readable, or
  313. b) current archive is /dev/null */
  314. bool
  315. file_dumpable_p (struct tar_stat_info *st)
  316. {
  317. if (dev_null_output)
  318. return totals_option && sparse_option && sparse_file_p (st);
  319. return !(st->archive_file_size == 0
  320. && (st->stat.st_mode & MODE_R) == MODE_R);
  321. }
  322. /* Writing routines. */
  323. /* Write the EOT block(s). Zero at least two blocks, through the end
  324. of the record. Old tar, as previous versions of GNU tar, writes
  325. garbage after two zeroed blocks. */
  326. void
  327. write_eot (void)
  328. {
  329. union block *pointer = find_next_block ();
  330. memset (pointer->buffer, 0, BLOCKSIZE);
  331. set_next_block_after (pointer);
  332. pointer = find_next_block ();
  333. memset (pointer->buffer, 0, available_space_after (pointer));
  334. set_next_block_after (pointer);
  335. }
  336. /* Write a "private" header */
  337. union block *
  338. start_private_header (const char *name, size_t size)
  339. {
  340. time_t t;
  341. union block *header = find_next_block ();
  342. memset (header->buffer, 0, sizeof (union block));
  343. tar_name_copy_str (header->header.name, name, NAME_FIELD_SIZE);
  344. OFF_TO_CHARS (size, header->header.size);
  345. time (&t);
  346. TIME_TO_CHARS (t, header->header.mtime);
  347. MODE_TO_CHARS (S_IFREG|S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH, header->header.mode);
  348. UID_TO_CHARS (getuid (), header->header.uid);
  349. GID_TO_CHARS (getgid (), header->header.gid);
  350. MAJOR_TO_CHARS (0, header->header.devmajor);
  351. MINOR_TO_CHARS (0, header->header.devminor);
  352. strncpy (header->header.magic, TMAGIC, TMAGLEN);
  353. strncpy (header->header.version, TVERSION, TVERSLEN);
  354. return header;
  355. }
  356. /* Create a new header and store there at most NAME_FIELD_SIZE bytes of
  357. the file name */
  358. static union block *
  359. write_short_name (struct tar_stat_info *st)
  360. {
  361. union block *header = find_next_block ();
  362. memset (header->buffer, 0, sizeof (union block));
  363. tar_name_copy_str (header->header.name, st->file_name, NAME_FIELD_SIZE);
  364. return header;
  365. }
  366. #define FILL(field,byte) do { \
  367. memset(field, byte, sizeof(field)-1); \
  368. (field)[sizeof(field)-1] = 0; \
  369. } while (0)
  370. /* Write a GNUTYPE_LONGLINK or GNUTYPE_LONGNAME block. */
  371. static void
  372. write_gnu_long_link (struct tar_stat_info *st, const char *p, char type)
  373. {
  374. size_t size = strlen (p) + 1;
  375. size_t bufsize;
  376. union block *header;
  377. char *tmpname;
  378. header = start_private_header ("././@LongLink", size);
  379. FILL(header->header.mtime, '0');
  380. FILL(header->header.mode, '0');
  381. FILL(header->header.uid, '0');
  382. FILL(header->header.gid, '0');
  383. FILL(header->header.devmajor, 0);
  384. FILL(header->header.devminor, 0);
  385. uid_to_uname (0, &tmpname);
  386. UNAME_TO_CHARS (tmpname, header->header.uname);
  387. free (tmpname);
  388. gid_to_gname (0, &tmpname);
  389. GNAME_TO_CHARS (tmpname, header->header.gname);
  390. free (tmpname);
  391. strcpy (header->header.magic, OLDGNU_MAGIC);
  392. header->header.typeflag = type;
  393. finish_header (st, header, -1);
  394. header = find_next_block ();
  395. bufsize = available_space_after (header);
  396. while (bufsize < size)
  397. {
  398. memcpy (header->buffer, p, bufsize);
  399. p += bufsize;
  400. size -= bufsize;
  401. set_next_block_after (header + (bufsize - 1) / BLOCKSIZE);
  402. header = find_next_block ();
  403. bufsize = available_space_after (header);
  404. }
  405. memcpy (header->buffer, p, size);
  406. memset (header->buffer + size, 0, bufsize - size);
  407. set_next_block_after (header + (size - 1) / BLOCKSIZE);
  408. }
  409. static size_t
  410. split_long_name (const char *name, size_t length)
  411. {
  412. size_t i;
  413. if (length > PREFIX_FIELD_SIZE)
  414. length = PREFIX_FIELD_SIZE+2;
  415. for (i = length - 1; i > 0; i--)
  416. if (ISSLASH (name[i]))
  417. break;
  418. return i;
  419. }
  420. static union block *
  421. write_ustar_long_name (const char *name)
  422. {
  423. size_t length = strlen (name);
  424. size_t i;
  425. union block *header;
  426. if (length > PREFIX_FIELD_SIZE + NAME_FIELD_SIZE + 1)
  427. {
  428. ERROR ((0, 0, _("%s: file name is too long (max %d); not dumped"),
  429. quotearg_colon (name),
  430. PREFIX_FIELD_SIZE + NAME_FIELD_SIZE + 1));
  431. return NULL;
  432. }
  433. i = split_long_name (name, length);
  434. if (i == 0 || length - i - 1 > NAME_FIELD_SIZE)
  435. {
  436. ERROR ((0, 0,
  437. _("%s: file name is too long (cannot be split); not dumped"),
  438. quotearg_colon (name)));
  439. return NULL;
  440. }
  441. header = find_next_block ();
  442. memset (header->buffer, 0, sizeof (header->buffer));
  443. memcpy (header->header.prefix, name, i);
  444. memcpy (header->header.name, name + i + 1, length - i - 1);
  445. return header;
  446. }
  447. /* Write a long link name, depending on the current archive format */
  448. static void
  449. write_long_link (struct tar_stat_info *st)
  450. {
  451. switch (archive_format)
  452. {
  453. case POSIX_FORMAT:
  454. xheader_store ("linkpath", st, NULL);
  455. break;
  456. case V7_FORMAT: /* old V7 tar format */
  457. case USTAR_FORMAT:
  458. case STAR_FORMAT:
  459. ERROR ((0, 0,
  460. _("%s: link name is too long; not dumped"),
  461. quotearg_colon (st->link_name)));
  462. break;
  463. case OLDGNU_FORMAT:
  464. case GNU_FORMAT:
  465. write_gnu_long_link (st, st->link_name, GNUTYPE_LONGLINK);
  466. break;
  467. default:
  468. abort(); /*FIXME*/
  469. }
  470. }
  471. static union block *
  472. write_long_name (struct tar_stat_info *st)
  473. {
  474. switch (archive_format)
  475. {
  476. case POSIX_FORMAT:
  477. xheader_store ("path", st, NULL);
  478. break;
  479. case V7_FORMAT:
  480. if (strlen (st->file_name) > NAME_FIELD_SIZE-1)
  481. {
  482. ERROR ((0, 0, _("%s: file name is too long (max %d); not dumped"),
  483. quotearg_colon (st->file_name),
  484. NAME_FIELD_SIZE - 1));
  485. return NULL;
  486. }
  487. break;
  488. case USTAR_FORMAT:
  489. case STAR_FORMAT:
  490. return write_ustar_long_name (st->file_name);
  491. case OLDGNU_FORMAT:
  492. case GNU_FORMAT:
  493. write_gnu_long_link (st, st->file_name, GNUTYPE_LONGNAME);
  494. break;
  495. default:
  496. abort(); /*FIXME*/
  497. }
  498. return write_short_name (st);
  499. }
  500. union block *
  501. write_extended (char type, struct tar_stat_info *st, union block *old_header)
  502. {
  503. union block *header, hp;
  504. char *p;
  505. if (extended_header.buffer || extended_header.stk == NULL)
  506. return old_header;
  507. xheader_finish (&extended_header);
  508. memcpy (hp.buffer, old_header, sizeof (hp));
  509. p = xheader_xhdr_name (st);
  510. xheader_write (type, p, &extended_header);
  511. free (p);
  512. header = find_next_block ();
  513. memcpy (header, &hp.buffer, sizeof (hp.buffer));
  514. return header;
  515. }
  516. static union block *
  517. write_header_name (struct tar_stat_info *st)
  518. {
  519. if (archive_format == POSIX_FORMAT && !string_ascii_p (st->file_name))
  520. {
  521. xheader_store ("path", st, NULL);
  522. return write_short_name (st);
  523. }
  524. else if (NAME_FIELD_SIZE - (archive_format == OLDGNU_FORMAT)
  525. < strlen (st->file_name))
  526. return write_long_name (st);
  527. else
  528. return write_short_name (st);
  529. }
  530. /* Header handling. */
  531. /* Make a header block for the file whose stat info is st,
  532. and return its address. */
  533. union block *
  534. start_header (struct tar_stat_info *st)
  535. {
  536. union block *header;
  537. header = write_header_name (st);
  538. if (!header)
  539. return NULL;
  540. /* Override some stat fields, if requested to do so. */
  541. if (owner_option != (uid_t) -1)
  542. st->stat.st_uid = owner_option;
  543. if (group_option != (gid_t) -1)
  544. st->stat.st_gid = group_option;
  545. if (mode_option)
  546. st->stat.st_mode =
  547. ((st->stat.st_mode & ~MODE_ALL)
  548. | mode_adjust (st->stat.st_mode, mode_option, initial_umask));
  549. /* Paul Eggert tried the trivial test ($WRITER cf a b; $READER tvf a)
  550. for a few tars and came up with the following interoperability
  551. matrix:
  552. WRITER
  553. 1 2 3 4 5 6 7 8 9 READER
  554. . . . . . . . . . 1 = SunOS 4.2 tar
  555. # . . # # . . # # 2 = NEC SVR4.0.2 tar
  556. . . . # # . . # . 3 = Solaris 2.1 tar
  557. . . . . . . . . . 4 = GNU tar 1.11.1
  558. . . . . . . . . . 5 = HP-UX 8.07 tar
  559. . . . . . . . . . 6 = Ultrix 4.1
  560. . . . . . . . . . 7 = AIX 3.2
  561. . . . . . . . . . 8 = Hitachi HI-UX 1.03
  562. . . . . . . . . . 9 = Omron UNIOS-B 4.3BSD 1.60Beta
  563. . = works
  564. # = ``impossible file type''
  565. The following mask for old archive removes the `#'s in column 4
  566. above, thus making GNU tar both a universal donor and a universal
  567. acceptor for Paul's test. */
  568. if (archive_format == V7_FORMAT || archive_format == USTAR_FORMAT)
  569. MODE_TO_CHARS (st->stat.st_mode & MODE_ALL, header->header.mode);
  570. else
  571. MODE_TO_CHARS (st->stat.st_mode, header->header.mode);
  572. {
  573. uid_t uid = st->stat.st_uid;
  574. if (archive_format == POSIX_FORMAT
  575. && MAX_OCTAL_VAL (header->header.uid) < uid)
  576. {
  577. xheader_store ("uid", st, NULL);
  578. uid = 0;
  579. }
  580. UID_TO_CHARS (uid, header->header.uid);
  581. }
  582. {
  583. gid_t gid = st->stat.st_gid;
  584. if (archive_format == POSIX_FORMAT
  585. && MAX_OCTAL_VAL (header->header.gid) < gid)
  586. {
  587. xheader_store ("gid", st, NULL);
  588. gid = 0;
  589. }
  590. GID_TO_CHARS (gid, header->header.gid);
  591. }
  592. {
  593. off_t size = st->stat.st_size;
  594. if (archive_format == POSIX_FORMAT
  595. && MAX_OCTAL_VAL (header->header.size) < size)
  596. {
  597. xheader_store ("size", st, NULL);
  598. size = 0;
  599. }
  600. OFF_TO_CHARS (size, header->header.size);
  601. }
  602. {
  603. struct timespec mtime = st->mtime;
  604. if (archive_format == POSIX_FORMAT)
  605. {
  606. if (MAX_OCTAL_VAL (header->header.mtime) < mtime.tv_sec
  607. || mtime.tv_nsec != 0)
  608. xheader_store ("mtime", st, NULL);
  609. if (MAX_OCTAL_VAL (header->header.mtime) < mtime.tv_sec)
  610. mtime.tv_sec = 0;
  611. }
  612. TIME_TO_CHARS (mtime.tv_sec, header->header.mtime);
  613. }
  614. /* FIXME */
  615. if (S_ISCHR (st->stat.st_mode)
  616. || S_ISBLK (st->stat.st_mode))
  617. {
  618. major_t devmajor = major (st->stat.st_rdev);
  619. minor_t devminor = minor (st->stat.st_rdev);
  620. if (archive_format == POSIX_FORMAT
  621. && MAX_OCTAL_VAL (header->header.devmajor) < devmajor)
  622. {
  623. xheader_store ("devmajor", st, NULL);
  624. devmajor = 0;
  625. }
  626. MAJOR_TO_CHARS (devmajor, header->header.devmajor);
  627. if (archive_format == POSIX_FORMAT
  628. && MAX_OCTAL_VAL (header->header.devminor) < devminor)
  629. {
  630. xheader_store ("devminor", st, NULL);
  631. devminor = 0;
  632. }
  633. MINOR_TO_CHARS (devminor, header->header.devminor);
  634. }
  635. else if (archive_format != GNU_FORMAT && archive_format != OLDGNU_FORMAT)
  636. {
  637. MAJOR_TO_CHARS (0, header->header.devmajor);
  638. MINOR_TO_CHARS (0, header->header.devminor);
  639. }
  640. if (archive_format == POSIX_FORMAT)
  641. {
  642. xheader_store ("atime", st, NULL);
  643. xheader_store ("ctime", st, NULL);
  644. }
  645. else if (incremental_option)
  646. if (archive_format == OLDGNU_FORMAT || archive_format == GNU_FORMAT)
  647. {
  648. TIME_TO_CHARS (st->atime.tv_sec, header->oldgnu_header.atime);
  649. TIME_TO_CHARS (st->ctime.tv_sec, header->oldgnu_header.ctime);
  650. }
  651. header->header.typeflag = archive_format == V7_FORMAT ? AREGTYPE : REGTYPE;
  652. switch (archive_format)
  653. {
  654. case V7_FORMAT:
  655. break;
  656. case OLDGNU_FORMAT:
  657. case GNU_FORMAT: /*FIXME?*/
  658. /* Overwrite header->header.magic and header.version in one blow. */
  659. strcpy (header->header.magic, OLDGNU_MAGIC);
  660. break;
  661. case POSIX_FORMAT:
  662. case USTAR_FORMAT:
  663. strncpy (header->header.magic, TMAGIC, TMAGLEN);
  664. strncpy (header->header.version, TVERSION, TVERSLEN);
  665. break;
  666. default:
  667. abort ();
  668. }
  669. if (archive_format == V7_FORMAT || numeric_owner_option)
  670. {
  671. /* header->header.[ug]name are left as the empty string. */
  672. }
  673. else
  674. {
  675. uid_to_uname (st->stat.st_uid, &st->uname);
  676. gid_to_gname (st->stat.st_gid, &st->gname);
  677. if (archive_format == POSIX_FORMAT
  678. && (strlen (st->uname) > UNAME_FIELD_SIZE
  679. || !string_ascii_p (st->uname)))
  680. xheader_store ("uname", st, NULL);
  681. UNAME_TO_CHARS (st->uname, header->header.uname);
  682. if (archive_format == POSIX_FORMAT
  683. && (strlen (st->gname) > GNAME_FIELD_SIZE
  684. || !string_ascii_p (st->gname)))
  685. xheader_store ("gname", st, NULL);
  686. GNAME_TO_CHARS (st->gname, header->header.gname);
  687. }
  688. return header;
  689. }
  690. void
  691. simple_finish_header (union block *header)
  692. {
  693. size_t i;
  694. int sum;
  695. char *p;
  696. memcpy (header->header.chksum, CHKBLANKS, sizeof header->header.chksum);
  697. sum = 0;
  698. p = header->buffer;
  699. for (i = sizeof *header; i-- != 0; )
  700. /* We can't use unsigned char here because of old compilers, e.g. V7. */
  701. sum += 0xFF & *p++;
  702. /* Fill in the checksum field. It's formatted differently from the
  703. other fields: it has [6] digits, a null, then a space -- rather than
  704. digits, then a null. We use to_chars.
  705. The final space is already there, from
  706. checksumming, and to_chars doesn't modify it.
  707. This is a fast way to do:
  708. sprintf(header->header.chksum, "%6o", sum); */
  709. uintmax_to_chars ((uintmax_t) sum, header->header.chksum, 7);
  710. set_next_block_after (header);
  711. }
  712. /* Finish off a filled-in header block and write it out. We also
  713. print the file name and/or full info if verbose is on. If BLOCK_ORDINAL
  714. is not negative, is the block ordinal of the first record for this
  715. file, which may be a preceding long name or long link record. */
  716. void
  717. finish_header (struct tar_stat_info *st,
  718. union block *header, off_t block_ordinal)
  719. {
  720. /* Note: It is important to do this before the call to write_extended(),
  721. so that the actual ustar header is printed */
  722. if (verbose_option
  723. && header->header.typeflag != GNUTYPE_LONGLINK
  724. && header->header.typeflag != GNUTYPE_LONGNAME
  725. && header->header.typeflag != XHDTYPE
  726. && header->header.typeflag != XGLTYPE)
  727. {
  728. /* These globals are parameters to print_header, sigh. */
  729. current_header = header;
  730. current_format = archive_format;
  731. print_header (st, block_ordinal);
  732. }
  733. header = write_extended (XHDTYPE, st, header);
  734. simple_finish_header (header);
  735. }
  736. void
  737. pad_archive (off_t size_left)
  738. {
  739. union block *blk;
  740. while (size_left > 0)
  741. {
  742. mv_size_left (size_left);
  743. blk = find_next_block ();
  744. memset (blk->buffer, 0, BLOCKSIZE);
  745. set_next_block_after (blk);
  746. size_left -= BLOCKSIZE;
  747. }
  748. }
  749. static enum dump_status
  750. dump_regular_file (int fd, struct tar_stat_info *st)
  751. {
  752. off_t size_left = st->stat.st_size;
  753. off_t block_ordinal;
  754. union block *blk;
  755. block_ordinal = current_block_ordinal ();
  756. blk = start_header (st);
  757. if (!blk)
  758. return dump_status_fail;
  759. /* Mark contiguous files, if we support them. */
  760. if (archive_format != V7_FORMAT && S_ISCTG (st->stat.st_mode))
  761. blk->header.typeflag = CONTTYPE;
  762. finish_header (st, blk, block_ordinal);
  763. mv_begin (st);
  764. while (size_left > 0)
  765. {
  766. size_t bufsize, count;
  767. mv_size_left (size_left);
  768. blk = find_next_block ();
  769. bufsize = available_space_after (blk);
  770. if (size_left < bufsize)
  771. {
  772. /* Last read -- zero out area beyond. */
  773. bufsize = size_left;
  774. count = bufsize % BLOCKSIZE;
  775. if (count)
  776. memset (blk->buffer + size_left, 0, BLOCKSIZE - count);
  777. }
  778. count = (fd < 0) ? bufsize : safe_read (fd, blk->buffer, bufsize);
  779. if (count == SAFE_READ_ERROR)
  780. {
  781. read_diag_details (st->orig_file_name,
  782. st->stat.st_size - size_left, bufsize);
  783. pad_archive (size_left);
  784. return dump_status_short;
  785. }
  786. size_left -= count;
  787. if (count)
  788. set_next_block_after (blk + (bufsize - 1) / BLOCKSIZE);
  789. if (count != bufsize)
  790. {
  791. char buf[UINTMAX_STRSIZE_BOUND];
  792. memset (blk->buffer + count, 0, bufsize - count);
  793. WARN ((0, 0,
  794. ngettext ("%s: File shrank by %s byte; padding with zeros",
  795. "%s: File shrank by %s bytes; padding with zeros",
  796. size_left),
  797. quotearg_colon (st->orig_file_name),
  798. STRINGIFY_BIGINT (size_left, buf)));
  799. if (! ignore_failed_read_option)
  800. exit_status = TAREXIT_FAILURE;
  801. pad_archive (size_left - (bufsize-count));
  802. return dump_status_short;
  803. }
  804. }
  805. return dump_status_ok;
  806. }
  807. static void
  808. dump_regular_finish (int fd, struct tar_stat_info *st,
  809. struct timespec original_ctime)
  810. {
  811. if (fd >= 0)
  812. {
  813. struct stat final_stat;
  814. if (fstat (fd, &final_stat) != 0)
  815. {
  816. stat_diag (st->orig_file_name);
  817. }
  818. else if (timespec_cmp (get_stat_ctime (&final_stat), original_ctime)
  819. != 0)
  820. {
  821. WARN ((0, 0, _("%s: file changed as we read it"),
  822. quotearg_colon (st->orig_file_name)));
  823. }
  824. if (close (fd) != 0)
  825. {
  826. close_diag (st->orig_file_name);
  827. }
  828. }
  829. if (remove_files_option)
  830. {
  831. if (unlink (st->orig_file_name) == -1)
  832. unlink_error (st->orig_file_name);
  833. }
  834. }
  835. /* Look in directory DIRNAME for a cache directory tag file
  836. with the magic name "CACHEDIR.TAG" and a standard header,
  837. as described at:
  838. http://www.brynosaurus.com/cachedir
  839. Applications can write this file into directories they create
  840. for use as caches containing purely regenerable, non-precious data,
  841. allowing us to avoid archiving them if --exclude-caches is specified. */
  842. #define CACHEDIR_SIGNATURE "Signature: 8a477f597d28d172789f06886806bc55"
  843. #define CACHEDIR_SIGNATURE_SIZE (sizeof CACHEDIR_SIGNATURE - 1)
  844. static bool
  845. check_cache_directory (char *dirname)
  846. {
  847. static char tagname[] = "CACHEDIR.TAG";
  848. char *tagpath;
  849. int fd;
  850. int tag_present = false;
  851. tagpath = xmalloc (strlen (dirname) + strlen (tagname) + 1);
  852. strcpy (tagpath, dirname);
  853. strcat (tagpath, tagname);
  854. fd = open (tagpath, O_RDONLY);
  855. if (fd >= 0)
  856. {
  857. static char tagbuf[CACHEDIR_SIGNATURE_SIZE];
  858. if (read (fd, tagbuf, CACHEDIR_SIGNATURE_SIZE)
  859. == CACHEDIR_SIGNATURE_SIZE
  860. && memcmp (tagbuf, CACHEDIR_SIGNATURE, CACHEDIR_SIGNATURE_SIZE) == 0)
  861. tag_present = true;
  862. close (fd);
  863. }
  864. free (tagpath);
  865. return tag_present;
  866. }
  867. static void
  868. dump_dir0 (char *directory,
  869. struct tar_stat_info *st, int top_level, dev_t parent_device)
  870. {
  871. dev_t our_device = st->stat.st_dev;
  872. if (!is_avoided_name (st->orig_file_name))
  873. {
  874. union block *blk = NULL;
  875. off_t block_ordinal = current_block_ordinal ();
  876. st->stat.st_size = 0; /* force 0 size on dir */
  877. blk = start_header (st);
  878. if (!blk)
  879. return;
  880. if (incremental_option && archive_format != POSIX_FORMAT)
  881. blk->header.typeflag = GNUTYPE_DUMPDIR;
  882. else /* if (standard_option) */
  883. blk->header.typeflag = DIRTYPE;
  884. /* If we're gnudumping, we aren't done yet so don't close it. */
  885. if (!incremental_option)
  886. finish_header (st, blk, block_ordinal);
  887. else if (gnu_list_name->dir_contents)
  888. {
  889. if (archive_format == POSIX_FORMAT)
  890. {
  891. xheader_store ("GNU.dumpdir", st, gnu_list_name->dir_contents);
  892. finish_header (st, blk, block_ordinal);
  893. }
  894. else
  895. {
  896. off_t size_left;
  897. off_t totsize;
  898. size_t bufsize;
  899. ssize_t count;
  900. const char *buffer, *p_buffer;
  901. block_ordinal = current_block_ordinal ();
  902. buffer = gnu_list_name->dir_contents;
  903. if (buffer)
  904. totsize = dumpdir_size (buffer);
  905. else
  906. totsize = 0;
  907. OFF_TO_CHARS (totsize, blk->header.size);
  908. finish_header (st, blk, block_ordinal);
  909. p_buffer = buffer;
  910. size_left = totsize;
  911. mv_begin (st);
  912. mv_total_size (totsize);
  913. while (size_left > 0)
  914. {
  915. mv_size_left (size_left);
  916. blk = find_next_block ();
  917. bufsize = available_space_after (blk);
  918. if (size_left < bufsize)
  919. {
  920. bufsize = size_left;
  921. count = bufsize % BLOCKSIZE;
  922. if (count)
  923. memset (blk->buffer + size_left, 0, BLOCKSIZE - count);
  924. }
  925. memcpy (blk->buffer, p_buffer, bufsize);
  926. size_left -= bufsize;
  927. p_buffer += bufsize;
  928. set_next_block_after (blk + (bufsize - 1) / BLOCKSIZE);
  929. }
  930. mv_end ();
  931. }
  932. return;
  933. }
  934. }
  935. if (!recursion_option)
  936. return;
  937. if (one_file_system_option
  938. && !top_level
  939. && parent_device != st->stat.st_dev)
  940. {
  941. if (verbose_option)
  942. WARN ((0, 0,
  943. _("%s: file is on a different filesystem; not dumped"),
  944. quotearg_colon (st->orig_file_name)));
  945. return;
  946. }
  947. if (exclude_caches_option
  948. && check_cache_directory(st->orig_file_name))
  949. {
  950. if (verbose_option)
  951. WARN ((0, 0,
  952. _("%s: contains a cache directory tag; not dumped"),
  953. quotearg_colon (st->orig_file_name)));
  954. return;
  955. }
  956. {
  957. char const *entry;
  958. size_t entry_len;
  959. char *name_buf = xstrdup (st->orig_file_name);
  960. size_t name_size = strlen (name_buf);
  961. size_t name_len = name_size;
  962. /* Now output all the files in the directory. */
  963. /* FIXME: Should speed this up by cd-ing into the dir. */
  964. for (entry = directory; (entry_len = strlen (entry)) != 0;
  965. entry += entry_len + 1)
  966. {
  967. if (name_size < name_len + entry_len)
  968. {
  969. name_size = name_len + entry_len;
  970. name_buf = xrealloc (name_buf, name_size + 1);
  971. }
  972. strcpy (name_buf + name_len, entry);
  973. if (!excluded_name (name_buf))
  974. dump_file (name_buf, 0, our_device);
  975. }
  976. free (name_buf);
  977. }
  978. }
  979. /* Ensure exactly one trailing slash. */
  980. static void
  981. ensure_slash (char **pstr)
  982. {
  983. size_t len = strlen (*pstr);
  984. while (len >= 1 && ISSLASH ((*pstr)[len - 1]))
  985. len--;
  986. if (!ISSLASH ((*pstr)[len]))
  987. *pstr = xrealloc (*pstr, len + 2);
  988. (*pstr)[len++] = '/';
  989. (*pstr)[len] = '\0';
  990. }
  991. static bool
  992. dump_dir (struct tar_stat_info *st, int top_level, dev_t parent_device)
  993. {
  994. char *directory;
  995. directory = savedir (st->orig_file_name);
  996. if (!directory)
  997. {
  998. savedir_diag (st->orig_file_name);
  999. return false;
  1000. }
  1001. ensure_slash (&st->orig_file_name);
  1002. ensure_slash (&st->file_name);
  1003. dump_dir0 (directory, st, top_level, parent_device);
  1004. free (directory);
  1005. return true;
  1006. }
  1007. /* Main functions of this module. */
  1008. void
  1009. create_archive (void)
  1010. {
  1011. char *p;
  1012. open_archive (ACCESS_WRITE);
  1013. xheader_write_global ();
  1014. if (incremental_option)
  1015. {
  1016. size_t buffer_size = 1000;
  1017. char *buffer = xmalloc (buffer_size);
  1018. const char *q;
  1019. collect_and_sort_names ();
  1020. while ((p = name_from_list ()) != NULL)
  1021. if (!excluded_name (p))
  1022. dump_file (p, -1, (dev_t) 0);
  1023. blank_name_list ();
  1024. while ((p = name_from_list ()) != NULL)
  1025. if (!excluded_name (p))
  1026. {
  1027. size_t plen = strlen (p);
  1028. if (buffer_size <= plen)
  1029. {
  1030. while ((buffer_size *= 2) <= plen)
  1031. continue;
  1032. buffer = xrealloc (buffer, buffer_size);
  1033. }
  1034. memcpy (buffer, p, plen);
  1035. if (! ISSLASH (buffer[plen - 1]))
  1036. buffer[plen++] = '/';
  1037. q = gnu_list_name->dir_contents;
  1038. if (q)
  1039. while (*q)
  1040. {
  1041. size_t qlen = strlen (q);
  1042. if (*q == 'Y')
  1043. {
  1044. if (buffer_size < plen + qlen)
  1045. {
  1046. while ((buffer_size *=2 ) < plen + qlen)
  1047. continue;
  1048. buffer = xrealloc (buffer, buffer_size);
  1049. }
  1050. strcpy (buffer + plen, q + 1);
  1051. dump_file (buffer, -1, (dev_t) 0);
  1052. }
  1053. q += qlen + 1;
  1054. }
  1055. }
  1056. free (buffer);
  1057. }
  1058. else
  1059. {
  1060. while ((p = name_next (1)) != NULL)
  1061. if (!excluded_name (p))
  1062. dump_file (p, 1, (dev_t) 0);
  1063. }
  1064. write_eot ();
  1065. close_archive ();
  1066. if (listed_incremental_option)
  1067. write_directory_file ();
  1068. }
  1069. /* Calculate the hash of a link. */
  1070. static size_t
  1071. hash_link (void const *entry, size_t n_buckets)
  1072. {
  1073. struct link const *l = entry;
  1074. uintmax_t num = l->dev ^ l->ino;
  1075. return num % n_buckets;
  1076. }
  1077. /* Compare two links for equality. */
  1078. static bool
  1079. compare_links (void const *entry1, void const *entry2)
  1080. {
  1081. struct link const *link1 = entry1;
  1082. struct link const *link2 = entry2;
  1083. return ((link1->dev ^ link2->dev) | (link1->ino ^ link2->ino)) == 0;
  1084. }
  1085. static void
  1086. unknown_file_error (char *p)
  1087. {
  1088. WARN ((0, 0, _("%s: Unknown file type; file ignored"),
  1089. quotearg_colon (p)));
  1090. if (!ignore_failed_read_option)
  1091. exit_status = TAREXIT_FAILURE;
  1092. }
  1093. /* Handling of hard links */
  1094. /* Table of all non-directories that we've written so far. Any time
  1095. we see another, we check the table and avoid dumping the data
  1096. again if we've done it once already. */
  1097. static Hash_table *link_table;
  1098. /* Try to dump stat as a hard link to another file in the archive. If
  1099. succeeded returns true */
  1100. static bool
  1101. dump_hard_link (struct tar_stat_info *st)
  1102. {
  1103. if (link_table && st->stat.st_nlink > 1)
  1104. {
  1105. struct link lp;
  1106. struct link *duplicate;
  1107. off_t block_ordinal;
  1108. union block *blk;
  1109. lp.ino = st->stat.st_ino;
  1110. lp.dev = st->stat.st_dev;
  1111. if ((duplicate = hash_lookup (link_table, &lp)))
  1112. {
  1113. /* We found a link. */
  1114. char const *link_name = safer_name_suffix (duplicate->name, true,
  1115. absolute_names_option);
  1116. duplicate->nlink--;
  1117. block_ordinal = current_block_ordinal ();
  1118. assign_string (&st->link_name, link_name);
  1119. if (NAME_FIELD_SIZE - (archive_format == OLDGNU_FORMAT)
  1120. < strlen (link_name))
  1121. write_long_link (st);
  1122. st->stat.st_size = 0;
  1123. blk = start_header (st);
  1124. if (!blk)
  1125. return true;
  1126. tar_copy_str (blk->header.linkname, link_name, NAME_FIELD_SIZE);
  1127. blk->header.typeflag = LNKTYPE;
  1128. finish_header (st, blk, block_ordinal);
  1129. if (remove_files_option && unlink (st->orig_file_name) != 0)
  1130. unlink_error (st->orig_file_name);
  1131. return true;
  1132. }
  1133. }
  1134. return false;
  1135. }
  1136. static void
  1137. file_count_links (struct tar_stat_info *st)
  1138. {
  1139. if (st->stat.st_nlink > 1)
  1140. {
  1141. struct link *duplicate;
  1142. struct link *lp = xmalloc (offsetof (struct link, name)
  1143. + strlen (st->orig_file_name) + 1);
  1144. lp->ino = st->stat.st_ino;
  1145. lp->dev = st->stat.st_dev;
  1146. lp->nlink = st->stat.st_nlink;
  1147. strcpy (lp->name, st->orig_file_name);
  1148. if (! ((link_table
  1149. || (link_table = hash_initialize (0, 0, hash_link,
  1150. compare_links, 0)))
  1151. && (duplicate = hash_insert (link_table, lp))))
  1152. xalloc_die ();
  1153. if (duplicate != lp)
  1154. abort ();
  1155. lp->nlink--;
  1156. }
  1157. }
  1158. /* For each dumped file, check if all its links were dumped. Emit
  1159. warnings if it is not so. */
  1160. void
  1161. check_links (void)
  1162. {
  1163. struct link *lp;
  1164. if (!link_table)
  1165. return;
  1166. for (lp = hash_get_first (link_table); lp;
  1167. lp = hash_get_next (link_table, lp))
  1168. {
  1169. if (lp->nlink)
  1170. {
  1171. WARN ((0, 0, _("Missing links to %s.\n"), quote (lp->name)));
  1172. }
  1173. }
  1174. }
  1175. /* Dump a single file, recursing on directories. P is the file name
  1176. to dump. TOP_LEVEL tells whether this is a top-level call; zero
  1177. means no, positive means yes, and negative means the top level
  1178. of an incremental dump. PARENT_DEVICE is the device of P's
  1179. parent directory; it is examined only if TOP_LEVEL is zero. */
  1180. /* FIXME: One should make sure that for *every* path leading to setting
  1181. exit_status to failure, a clear diagnostic has been issued. */
  1182. static void
  1183. dump_file0 (struct tar_stat_info *st, char *p,
  1184. int top_level, dev_t parent_device)
  1185. {
  1186. union block *header;
  1187. char type;
  1188. struct timespec original_ctime;
  1189. struct timespec restore_times[2];
  1190. off_t block_ordinal = -1;
  1191. if (interactive_option && !confirm ("add", p))
  1192. return;
  1193. assign_string (&st->orig_file_name, p);
  1194. assign_string (&st->file_name,
  1195. safer_name_suffix (p, false, absolute_names_option));
  1196. if (deref_stat (dereference_option, p, &st->stat) != 0)
  1197. {
  1198. stat_diag (p);
  1199. return;
  1200. }
  1201. st->archive_file_size = st->stat.st_size;
  1202. st->atime = restore_times[0] = get_stat_atime (&st->stat);
  1203. st->mtime = restore_times[1] = get_stat_mtime (&st->stat);
  1204. st->ctime = original_ctime = get_stat_ctime (&st->stat);
  1205. #ifdef S_ISHIDDEN
  1206. if (S_ISHIDDEN (st->stat.st_mode))
  1207. {
  1208. char *new = (char *) alloca (strlen (p) + 2);
  1209. if (new)
  1210. {
  1211. strcpy (new, p);
  1212. strcat (new, "@");
  1213. p = new;
  1214. }
  1215. }
  1216. #endif
  1217. /* See if we want only new files, and check if this one is too old to
  1218. put in the archive.
  1219. This check is omitted if incremental_option is set *and* the
  1220. requested file is not explicitely listed in the command line. */
  1221. if (!(incremental_option && !is_individual_file (p))
  1222. && !S_ISDIR (st->stat.st_mode)
  1223. && OLDER_TAR_STAT_TIME (*st, m)
  1224. && (!after_date_option || OLDER_TAR_STAT_TIME (*st, c)))
  1225. {
  1226. if (!incremental_option && verbose_option)
  1227. WARN ((0, 0, _("%s: file is unchanged; not dumped"),
  1228. quotearg_colon (p)));
  1229. return;
  1230. }
  1231. /* See if we are trying to dump the archive. */
  1232. if (sys_file_is_archive (st))
  1233. {
  1234. WARN ((0, 0, _("%s: file is the archive; not dumped"),
  1235. quotearg_colon (p)));
  1236. return;
  1237. }
  1238. if (is_avoided_name (p))
  1239. return;
  1240. if (S_ISDIR (st->stat.st_mode))
  1241. {
  1242. dump_dir (st, top_level, parent_device);
  1243. if (atime_preserve_option)
  1244. utimens (p, restore_times);
  1245. return;
  1246. }
  1247. else
  1248. {
  1249. /* Check for multiple links. */
  1250. if (dump_hard_link (st))
  1251. return;
  1252. /* This is not a link to a previously dumped file, so dump it. */
  1253. if (S_ISREG (st->stat.st_mode)
  1254. || S_ISCTG (st->stat.st_mode))
  1255. {
  1256. int fd;
  1257. enum dump_status status;
  1258. if (file_dumpable_p (st))
  1259. {
  1260. fd = open (st->orig_file_name,
  1261. O_RDONLY | O_BINARY);
  1262. if (fd < 0)
  1263. {
  1264. if (!top_level && errno == ENOENT)
  1265. WARN ((0, 0, _("%s: File removed before we read it"),
  1266. quotearg_colon (st->orig_file_name)));
  1267. else
  1268. open_diag (st->orig_file_name);
  1269. return;
  1270. }
  1271. }
  1272. else
  1273. fd = -1;
  1274. if (fd != -1 && sparse_option && sparse_file_p (st))
  1275. {
  1276. status = sparse_dump_file (fd, st);
  1277. if (status == dump_status_not_implemented)
  1278. status = dump_regular_file (fd, st);
  1279. }
  1280. else
  1281. status = dump_regular_file (fd, st);
  1282. switch (status)
  1283. {
  1284. case dump_status_ok:
  1285. mv_end ();
  1286. dump_regular_finish (fd, st, original_ctime);
  1287. break;
  1288. case dump_status_short:
  1289. mv_end ();
  1290. close (fd);
  1291. break;
  1292. case dump_status_fail:
  1293. close (fd);
  1294. return;
  1295. case dump_status_not_implemented:
  1296. abort ();
  1297. }
  1298. if (atime_preserve_option)
  1299. utimens (st->orig_file_name, restore_times);
  1300. file_count_links (st);
  1301. return;
  1302. }
  1303. #ifdef HAVE_READLINK
  1304. else if (S_ISLNK (st->stat.st_mode))
  1305. {
  1306. char *buffer;
  1307. int size;
  1308. size_t linklen = st->stat.st_size;
  1309. if (linklen != st->stat.st_size || linklen + 1 == 0)
  1310. xalloc_die ();
  1311. buffer = (char *) alloca (linklen + 1);
  1312. size = readlink (p, buffer, linklen + 1);
  1313. if (size < 0)
  1314. {
  1315. readlink_diag (p);
  1316. return;
  1317. }
  1318. buffer[size] = '\0';
  1319. assign_string (&st->link_name, buffer);
  1320. if (NAME_FIELD_SIZE - (archive_format == OLDGNU_FORMAT) < size)
  1321. write_long_link (st);
  1322. block_ordinal = current_block_ordinal ();
  1323. st->stat.st_size = 0; /* force 0 size on symlink */
  1324. header = start_header (st);
  1325. if (!header)
  1326. return;
  1327. tar_copy_str (header->header.linkname, buffer, NAME_FIELD_SIZE);
  1328. header->header.typeflag = SYMTYPE;
  1329. finish_header (st, header, block_ordinal);
  1330. /* nothing more to do to it */
  1331. if (remove_files_option)
  1332. {
  1333. if (unlink (p) == -1)
  1334. unlink_error (p);
  1335. }
  1336. file_count_links (st);
  1337. return;
  1338. }
  1339. #endif
  1340. else if (S_ISCHR (st->stat.st_mode))
  1341. type = CHRTYPE;
  1342. else if (S_ISBLK (st->stat.st_mode))
  1343. type = BLKTYPE;
  1344. else if (S_ISFIFO (st->stat.st_mode))
  1345. type = FIFOTYPE;
  1346. else if (S_ISSOCK (st->stat.st_mode))
  1347. {
  1348. WARN ((0, 0, _("%s: socket ignored"), quotearg_colon (p)));
  1349. return;
  1350. }
  1351. else if (S_ISDOOR (st->stat.st_mode))
  1352. {
  1353. WARN ((0, 0, _("%s: door ignored"), quotearg_colon (p)));
  1354. return;
  1355. }
  1356. else
  1357. {
  1358. unknown_file_error (p);
  1359. return;
  1360. }
  1361. }
  1362. if (archive_format == V7_FORMAT)
  1363. {
  1364. unknown_file_error (p);
  1365. return;
  1366. }
  1367. block_ordinal = current_block_ordinal ();
  1368. st->stat.st_size = 0; /* force 0 size */
  1369. header = start_header (st);
  1370. if (!header)
  1371. return;
  1372. header->header.typeflag = type;
  1373. if (type != FIFOTYPE)
  1374. {
  1375. MAJOR_TO_CHARS (major (st->stat.st_rdev),
  1376. header->header.devmajor);
  1377. MINOR_TO_CHARS (minor (st->stat.st_rdev),
  1378. header->header.devminor);
  1379. }
  1380. finish_header (st, header, block_ordinal);
  1381. if (remove_files_option)
  1382. {
  1383. if (unlink (p) == -1)
  1384. unlink_error (p);
  1385. }
  1386. }
  1387. void
  1388. dump_file (char *p, int top_level, dev_t parent_device)
  1389. {
  1390. struct tar_stat_info st;
  1391. tar_stat_init (&st);
  1392. dump_file0 (&st, p, top_level, parent_device);
  1393. if (listed_incremental_option)
  1394. update_parent_directory (p);
  1395. tar_stat_destroy (&st);
  1396. }