create.c 44 KB

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