create.c 41 KB

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