create.c 43 KB

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