create.c 38 KB

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