create.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582
  1. /* Create a tar archive.
  2. Copyright 1985, 92, 93, 94, 96, 97, 99, 2000 Free Software Foundation, Inc.
  3. Written by John Gilmore, on 1985-08-25.
  4. This program is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by the
  6. Free Software Foundation; either version 2, or (at your option) any later
  7. version.
  8. This program is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
  11. Public License for more details.
  12. You should have received a copy of the GNU General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  15. #include "system.h"
  16. #if !MSDOS
  17. # include <pwd.h>
  18. # include <grp.h>
  19. #endif
  20. #if HAVE_UTIME_H
  21. # include <utime.h>
  22. #else
  23. struct utimbuf
  24. {
  25. long actime;
  26. long modtime;
  27. };
  28. #endif
  29. #include <quotearg.h>
  30. #include "common.h"
  31. #ifndef MSDOS
  32. extern dev_t ar_dev;
  33. extern ino_t ar_ino;
  34. #endif
  35. extern struct name *gnu_list_name;
  36. /* This module is the only one that cares about `struct link's. */
  37. struct link
  38. {
  39. struct link *next;
  40. dev_t dev;
  41. ino_t ino;
  42. char name[1];
  43. };
  44. static struct link *linklist; /* points to first link in list */
  45. /* The maximum uintmax_t value that can be represented with DIGITS digits,
  46. assuming that each digit is BITS_PER_DIGIT wide. */
  47. #define MAX_VAL_WITH_DIGITS(digits, bits_per_digit) \
  48. ((digits) * (bits_per_digit) < sizeof (uintmax_t) * CHAR_BIT \
  49. ? ((uintmax_t) 1 << ((digits) * (bits_per_digit))) - 1 \
  50. : (uintmax_t) -1)
  51. /* Convert VALUE to an octal representation suitable for tar headers.
  52. Output to buffer WHERE with size SIZE.
  53. The result is undefined if SIZE is 0 or if VALUE is too large to fit. */
  54. static void
  55. to_octal (uintmax_t value, char *where, size_t size)
  56. {
  57. uintmax_t v = value;
  58. size_t i = size;
  59. do
  60. {
  61. where[--i] = '0' + (v & ((1 << LG_8) - 1));
  62. v >>= LG_8;
  63. }
  64. while (i);
  65. }
  66. /* Convert NEGATIVE VALUE to a base-256 representation suitable for
  67. tar headers. NEGATIVE is 1 if VALUE was negative before being cast
  68. to uintmax_t, 0 otherwise. Output to buffer WHERE with size SIZE.
  69. The result is undefined if SIZE is 0 or if VALUE is too large to
  70. fit. */
  71. static void
  72. to_base256 (int negative, uintmax_t value, char *where, size_t size)
  73. {
  74. uintmax_t v = value;
  75. uintmax_t propagated_sign_bits =
  76. ((uintmax_t) - negative << (CHAR_BIT * sizeof v - LG_256));
  77. size_t i = size;
  78. do
  79. {
  80. where[--i] = v & ((1 << LG_256) - 1);
  81. v = propagated_sign_bits | (v >> LG_256);
  82. }
  83. while (i);
  84. }
  85. /* Convert NEGATIVE VALUE (which was originally of size VALSIZE) to
  86. external form, using SUBSTITUTE (...) if VALUE won't fit. Output
  87. to buffer WHERE with size SIZE. NEGATIVE is 1 iff VALUE was
  88. negative before being cast to uintmax_t; its original bitpattern
  89. can be deduced from VALSIZE, its original size before casting.
  90. TYPE is the kind of value being output (useful for diagnostics).
  91. Prefer the POSIX format of SIZE - 1 octal digits (with leading zero
  92. digits), followed by '\0'. If this won't work, and if GNU or
  93. OLDGNU format is allowed, use '\200' followed by base-256, or (if
  94. NEGATIVE is nonzero) '\377' followed by two's complement base-256.
  95. If neither format works, use SUBSTITUTE (...) instead. Pass to
  96. SUBSTITUTE the address of an 0-or-1 flag recording whether the
  97. substitute value is negative. */
  98. static void
  99. to_chars (int negative, uintmax_t value, size_t valsize,
  100. uintmax_t (*substitute) PARAMS ((int *)),
  101. char *where, size_t size, const char *type)
  102. {
  103. int base256_allowed = (archive_format == GNU_FORMAT
  104. || archive_format == OLDGNU_FORMAT);
  105. /* Generate the POSIX octal representation if the number fits. */
  106. if (! negative && value <= MAX_VAL_WITH_DIGITS (size - 1, LG_8))
  107. {
  108. where[size - 1] = '\0';
  109. to_octal (value, where, size - 1);
  110. }
  111. /* Otherwise, generate the base-256 representation if we are
  112. generating an old or new GNU format and if the number fits. */
  113. else if (((negative ? -1 - value : value)
  114. <= MAX_VAL_WITH_DIGITS (size - 1, LG_256))
  115. && base256_allowed)
  116. {
  117. where[0] = negative ? -1 : 1 << (LG_256 - 1);
  118. to_base256 (negative, value, where + 1, size - 1);
  119. }
  120. /* Otherwise, if the number is negative, and if it would not cause
  121. ambiguity on this host by confusing positive with negative
  122. values, then generate the POSIX octal representation of the value
  123. modulo 2**(field bits). The resulting tar file is
  124. machine-dependent, since it depends on the host word size. Yuck!
  125. But this is the traditional behavior. */
  126. else if (negative && valsize * CHAR_BIT <= (size - 1) * LG_8)
  127. {
  128. static int warned_once;
  129. if (! warned_once)
  130. {
  131. warned_once = 1;
  132. WARN ((0, 0, _("Generating negative octal headers")));
  133. }
  134. where[size - 1] = '\0';
  135. to_octal (value & MAX_VAL_WITH_DIGITS (valsize * CHAR_BIT, 1),
  136. where, size - 1);
  137. }
  138. /* Otherwise, output a substitute value if possible (with a
  139. warning), and an error message if not. */
  140. else
  141. {
  142. uintmax_t maxval = (base256_allowed
  143. ? MAX_VAL_WITH_DIGITS (size - 1, LG_256)
  144. : MAX_VAL_WITH_DIGITS (size - 1, LG_8));
  145. char valbuf[UINTMAX_STRSIZE_BOUND + 1];
  146. char maxbuf[UINTMAX_STRSIZE_BOUND];
  147. char minbuf[UINTMAX_STRSIZE_BOUND + 1];
  148. char const *minval_string;
  149. char const *maxval_string = STRINGIFY_BIGINT (maxval, maxbuf);
  150. char const *value_string;
  151. if (base256_allowed)
  152. {
  153. uintmax_t m = maxval + 1 ? maxval + 1 : maxval / 2 + 1;
  154. char *p = STRINGIFY_BIGINT (m, minbuf + 1);
  155. *--p = '-';
  156. minval_string = p;
  157. }
  158. else
  159. minval_string = "0";
  160. if (negative)
  161. {
  162. char *p = STRINGIFY_BIGINT (- value, valbuf + 1);
  163. *--p = '-';
  164. value_string = p;
  165. }
  166. else
  167. value_string = STRINGIFY_BIGINT (value, valbuf);
  168. if (substitute)
  169. {
  170. int negsub;
  171. uintmax_t sub = substitute (&negsub) & maxval;
  172. uintmax_t s = (negsub &= archive_format == GNU_FORMAT) ? - sub : sub;
  173. char subbuf[UINTMAX_STRSIZE_BOUND + 1];
  174. char *sub_string = STRINGIFY_BIGINT (s, subbuf + 1);
  175. if (negsub)
  176. *--sub_string = '-';
  177. WARN ((0, 0, _("value %s out of %s range %s..%s; substituting %s"),
  178. value_string, type, minval_string, maxval_string,
  179. sub_string));
  180. to_chars (negsub, s, valsize, 0, where, size, type);
  181. }
  182. else
  183. ERROR ((0, 0, _("value %s out of %s range %s..%s"),
  184. value_string, type, minval_string, maxval_string));
  185. }
  186. }
  187. static uintmax_t
  188. gid_substitute (int *negative)
  189. {
  190. gid_t r;
  191. #ifdef GID_NOBODY
  192. r = GID_NOBODY;
  193. #else
  194. static gid_t gid_nobody;
  195. if (!gid_nobody && !gname_to_gid ("nobody", &gid_nobody))
  196. gid_nobody = -2;
  197. r = gid_nobody;
  198. #endif
  199. *negative = r < 0;
  200. return r;
  201. }
  202. void
  203. gid_to_chars (gid_t v, char *p, size_t s)
  204. {
  205. to_chars (v < 0, (uintmax_t) v, sizeof v, gid_substitute, p, s, "gid_t");
  206. }
  207. void
  208. major_to_chars (major_t v, char *p, size_t s)
  209. {
  210. to_chars (v < 0, (uintmax_t) v, sizeof v, 0, p, s, "major_t");
  211. }
  212. void
  213. minor_to_chars (minor_t v, char *p, size_t s)
  214. {
  215. to_chars (v < 0, (uintmax_t) v, sizeof v, 0, p, s, "minor_t");
  216. }
  217. void
  218. mode_to_chars (mode_t v, char *p, size_t s)
  219. {
  220. /* In the common case where the internal and external mode bits are the same,
  221. and we are not using POSIX or GNU format,
  222. propagate all unknown bits to the external mode.
  223. This matches historical practice.
  224. Otherwise, just copy the bits we know about. */
  225. int negative;
  226. uintmax_t u;
  227. if (S_ISUID == TSUID && S_ISGID == TSGID && S_ISVTX == TSVTX
  228. && S_IRUSR == TUREAD && S_IWUSR == TUWRITE && S_IXUSR == TUEXEC
  229. && S_IRGRP == TGREAD && S_IWGRP == TGWRITE && S_IXGRP == TGEXEC
  230. && S_IROTH == TOREAD && S_IWOTH == TOWRITE && S_IXOTH == TOEXEC
  231. && archive_format != POSIX_FORMAT
  232. && archive_format != GNU_FORMAT)
  233. {
  234. negative = v < 0;
  235. u = v;
  236. }
  237. else
  238. {
  239. negative = 0;
  240. u = ((v & S_ISUID ? TSUID : 0)
  241. | (v & S_ISGID ? TSGID : 0)
  242. | (v & S_ISVTX ? TSVTX : 0)
  243. | (v & S_IRUSR ? TUREAD : 0)
  244. | (v & S_IWUSR ? TUWRITE : 0)
  245. | (v & S_IXUSR ? TUEXEC : 0)
  246. | (v & S_IRGRP ? TGREAD : 0)
  247. | (v & S_IWGRP ? TGWRITE : 0)
  248. | (v & S_IXGRP ? TGEXEC : 0)
  249. | (v & S_IROTH ? TOREAD : 0)
  250. | (v & S_IWOTH ? TOWRITE : 0)
  251. | (v & S_IXOTH ? TOEXEC : 0));
  252. }
  253. to_chars (negative, u, sizeof v, 0, p, s, "mode_t");
  254. }
  255. void
  256. off_to_chars (off_t v, char *p, size_t s)
  257. {
  258. to_chars (v < 0, (uintmax_t) v, sizeof v, 0, p, s, "off_t");
  259. }
  260. void
  261. size_to_chars (size_t v, char *p, size_t s)
  262. {
  263. to_chars (0, (uintmax_t) v, sizeof v, 0, p, s, "size_t");
  264. }
  265. void
  266. time_to_chars (time_t v, char *p, size_t s)
  267. {
  268. to_chars (v < 0, (uintmax_t) v, sizeof v, 0, p, s, "time_t");
  269. }
  270. static uintmax_t
  271. uid_substitute (int *negative)
  272. {
  273. uid_t r;
  274. #ifdef UID_NOBODY
  275. r = UID_NOBODY;
  276. #else
  277. static uid_t uid_nobody;
  278. if (!uid_nobody && !uname_to_uid ("nobody", &uid_nobody))
  279. uid_nobody = -2;
  280. r = uid_nobody;
  281. #endif
  282. *negative = r < 0;
  283. return r;
  284. }
  285. void
  286. uid_to_chars (uid_t v, char *p, size_t s)
  287. {
  288. to_chars (v < 0, (uintmax_t) v, sizeof v, uid_substitute, p, s, "uid_t");
  289. }
  290. void
  291. uintmax_to_chars (uintmax_t v, char *p, size_t s)
  292. {
  293. to_chars (0, v, sizeof v, 0, p, s, "uintmax_t");
  294. }
  295. /* Writing routines. */
  296. /*-----------------------------------------------------------------------.
  297. | Just zeroes out the buffer so we don't confuse ourselves with leftover |
  298. | data. |
  299. `-----------------------------------------------------------------------*/
  300. static void
  301. clear_buffer (char *buffer)
  302. {
  303. memset (buffer, 0, BLOCKSIZE);
  304. }
  305. /*-------------------------------------------------------------------------.
  306. | Write the EOT block(s). We zero at least two blocks, through |
  307. | the end of the record. Old tar, as previous versions of GNU tar, writes |
  308. | garbage after two zeroed blocks. |
  309. `-------------------------------------------------------------------------*/
  310. void
  311. write_eot (void)
  312. {
  313. union block *pointer = find_next_block ();
  314. memset (pointer->buffer, 0, BLOCKSIZE);
  315. set_next_block_after (pointer);
  316. pointer = find_next_block ();
  317. memset (pointer->buffer, 0, available_space_after (pointer));
  318. set_next_block_after (pointer);
  319. }
  320. /*-----------------------------------------------------.
  321. | Write a GNUTYPE_LONGLINK or GNUTYPE_LONGNAME block. |
  322. `-----------------------------------------------------*/
  323. /* FIXME: Cross recursion between start_header and write_long! */
  324. static union block *start_header PARAMS ((const char *, struct stat *));
  325. static void
  326. write_long (const char *p, char type)
  327. {
  328. size_t size = strlen (p) + 1;
  329. size_t bufsize;
  330. union block *header;
  331. struct stat foo;
  332. memset (&foo, 0, sizeof foo);
  333. foo.st_size = size;
  334. header = start_header ("././@LongLink", &foo);
  335. header->header.typeflag = type;
  336. finish_header (header);
  337. header = find_next_block ();
  338. bufsize = available_space_after (header);
  339. while (bufsize < size)
  340. {
  341. memcpy (header->buffer, p, bufsize);
  342. p += bufsize;
  343. size -= bufsize;
  344. set_next_block_after (header + (bufsize - 1) / BLOCKSIZE);
  345. header = find_next_block ();
  346. bufsize = available_space_after (header);
  347. }
  348. memcpy (header->buffer, p, size);
  349. memset (header->buffer + size, 0, bufsize - size);
  350. set_next_block_after (header + (size - 1) / BLOCKSIZE);
  351. }
  352. /* Header handling. */
  353. /* Make a header block for the file whose stat info is st,
  354. and return its address. */
  355. static union block *
  356. start_header (const char *name, struct stat *st)
  357. {
  358. union block *header;
  359. if (!absolute_names_option)
  360. {
  361. size_t prefix_len = FILESYSTEM_PREFIX_LEN (name);
  362. if (prefix_len)
  363. {
  364. static int warned_once;
  365. if (!warned_once)
  366. {
  367. warned_once = 1;
  368. WARN ((0, 0, _("Removing `%.*s' prefix from member names"),
  369. (int) prefix_len, name));
  370. }
  371. name += prefix_len;
  372. }
  373. while (*name == '/')
  374. {
  375. static int warned_once;
  376. if (!warned_once)
  377. {
  378. warned_once = 1;
  379. WARN ((0, 0, _("Removing leading `/' from member names")));
  380. }
  381. name++;
  382. }
  383. {
  384. static int warned_once;
  385. if (! warned_once && contains_dot_dot (name))
  386. {
  387. warned_once = 1;
  388. WARN ((0, 0, _("Member names contain `..'")));
  389. }
  390. }
  391. }
  392. if (sizeof header->header.name <= strlen (name))
  393. write_long (name, GNUTYPE_LONGNAME);
  394. header = find_next_block ();
  395. memset (header->buffer, 0, sizeof (union block));
  396. assign_string (&current_file_name, name);
  397. strncpy (header->header.name, name, NAME_FIELD_SIZE);
  398. header->header.name[NAME_FIELD_SIZE - 1] = '\0';
  399. /* Override some stat fields, if requested to do so. */
  400. if (owner_option != (uid_t) -1)
  401. st->st_uid = owner_option;
  402. if (group_option != (gid_t) -1)
  403. st->st_gid = group_option;
  404. if (mode_option)
  405. st->st_mode = ((st->st_mode & ~MODE_ALL)
  406. | mode_adjust (st->st_mode, mode_option));
  407. /* Paul Eggert tried the trivial test ($WRITER cf a b; $READER tvf a)
  408. for a few tars and came up with the following interoperability
  409. matrix:
  410. WRITER
  411. 1 2 3 4 5 6 7 8 9 READER
  412. . . . . . . . . . 1 = SunOS 4.2 tar
  413. # . . # # . . # # 2 = NEC SVR4.0.2 tar
  414. . . . # # . . # . 3 = Solaris 2.1 tar
  415. . . . . . . . . . 4 = GNU tar 1.11.1
  416. . . . . . . . . . 5 = HP-UX 8.07 tar
  417. . . . . . . . . . 6 = Ultrix 4.1
  418. . . . . . . . . . 7 = AIX 3.2
  419. . . . . . . . . . 8 = Hitachi HI-UX 1.03
  420. . . . . . . . . . 9 = Omron UNIOS-B 4.3BSD 1.60Beta
  421. . = works
  422. # = ``impossible file type''
  423. The following mask for old archive removes the `#'s in column 4
  424. above, thus making GNU tar both a universal donor and a universal
  425. acceptor for Paul's test. */
  426. if (archive_format == V7_FORMAT)
  427. MODE_TO_CHARS (st->st_mode & MODE_ALL, header->header.mode);
  428. else
  429. MODE_TO_CHARS (st->st_mode, header->header.mode);
  430. UID_TO_CHARS (st->st_uid, header->header.uid);
  431. GID_TO_CHARS (st->st_gid, header->header.gid);
  432. OFF_TO_CHARS (st->st_size, header->header.size);
  433. TIME_TO_CHARS (st->st_mtime, header->header.mtime);
  434. if (incremental_option)
  435. if (archive_format == OLDGNU_FORMAT)
  436. {
  437. TIME_TO_CHARS (st->st_atime, header->oldgnu_header.atime);
  438. TIME_TO_CHARS (st->st_ctime, header->oldgnu_header.ctime);
  439. }
  440. header->header.typeflag = archive_format == V7_FORMAT ? AREGTYPE : REGTYPE;
  441. switch (archive_format)
  442. {
  443. case V7_FORMAT:
  444. break;
  445. case OLDGNU_FORMAT:
  446. /* Overwrite header->header.magic and header.version in one blow. */
  447. strcpy (header->header.magic, OLDGNU_MAGIC);
  448. break;
  449. case POSIX_FORMAT:
  450. case GNU_FORMAT:
  451. strncpy (header->header.magic, TMAGIC, TMAGLEN);
  452. strncpy (header->header.version, TVERSION, TVERSLEN);
  453. break;
  454. default:
  455. abort ();
  456. }
  457. if (archive_format == V7_FORMAT || numeric_owner_option)
  458. {
  459. /* header->header.[ug]name are left as the empty string. */
  460. }
  461. else
  462. {
  463. uid_to_uname (st->st_uid, header->header.uname);
  464. gid_to_gname (st->st_gid, header->header.gname);
  465. }
  466. return header;
  467. }
  468. /*-------------------------------------------------------------------------.
  469. | Finish off a filled-in header block and write it out. We also print the |
  470. | file name and/or full info if verbose is on. |
  471. `-------------------------------------------------------------------------*/
  472. void
  473. finish_header (union block *header)
  474. {
  475. size_t i;
  476. int sum;
  477. char *p;
  478. memcpy (header->header.chksum, CHKBLANKS, sizeof header->header.chksum);
  479. sum = 0;
  480. p = header->buffer;
  481. for (i = sizeof *header; i-- != 0; )
  482. /* We can't use unsigned char here because of old compilers, e.g. V7. */
  483. sum += 0xFF & *p++;
  484. /* Fill in the checksum field. It's formatted differently from the
  485. other fields: it has [6] digits, a null, then a space -- rather than
  486. digits, then a null. We use to_chars.
  487. The final space is already there, from
  488. checksumming, and to_chars doesn't modify it.
  489. This is a fast way to do:
  490. sprintf(header->header.chksum, "%6o", sum); */
  491. uintmax_to_chars ((uintmax_t) sum, header->header.chksum, 7);
  492. if (verbose_option
  493. && header->header.typeflag != GNUTYPE_LONGLINK
  494. && header->header.typeflag != GNUTYPE_LONGNAME)
  495. {
  496. /* These globals are parameters to print_header, sigh. */
  497. current_header = header;
  498. /* current_stat is already set up. */
  499. current_format = archive_format;
  500. print_header ();
  501. }
  502. set_next_block_after (header);
  503. }
  504. /* Sparse file processing. */
  505. /*-------------------------------------------------------------------------.
  506. | Takes a blockful of data and basically cruises through it to see if it's |
  507. | made *entirely* of zeros, returning a 0 the instant it finds something |
  508. | that is a nonzero, i.e., useful data. |
  509. `-------------------------------------------------------------------------*/
  510. static int
  511. zero_block_p (char *buffer)
  512. {
  513. int counter;
  514. for (counter = 0; counter < BLOCKSIZE; counter++)
  515. if (buffer[counter] != '\0')
  516. return 0;
  517. return 1;
  518. }
  519. /*---.
  520. | ? |
  521. `---*/
  522. static void
  523. init_sparsearray (void)
  524. {
  525. int counter;
  526. sp_array_size = 10;
  527. /* Make room for our scratch space -- initially is 10 elts long. */
  528. sparsearray = xmalloc (sp_array_size * sizeof (struct sp_array));
  529. for (counter = 0; counter < sp_array_size; counter++)
  530. {
  531. sparsearray[counter].offset = 0;
  532. sparsearray[counter].numbytes = 0;
  533. }
  534. }
  535. /*---.
  536. | ? |
  537. `---*/
  538. static off_t
  539. find_new_file_size (int sparses)
  540. {
  541. int i;
  542. off_t s = 0;
  543. for (i = 0; i < sparses; i++)
  544. s += sparsearray[i].numbytes;
  545. return s;
  546. }
  547. /*-----------------------------------------------------------------------.
  548. | Make one pass over the file NAME, studying where any non-zero data is, |
  549. | that is, how far into the file each instance of data is, and how many |
  550. | bytes are there. Save this information in the sparsearray, which will |
  551. | later be translated into header information. |
  552. `-----------------------------------------------------------------------*/
  553. /* There is little point in trimming small amounts of null data at the head
  554. and tail of blocks, only avoid dumping full null blocks. */
  555. /* FIXME: this routine might accept bits of algorithmic cleanup, it is
  556. too kludgey for my taste... */
  557. static int
  558. deal_with_sparse (char *name, union block *header)
  559. {
  560. size_t numbytes = 0;
  561. off_t offset = 0;
  562. int file;
  563. int sparses = 0;
  564. ssize_t count;
  565. char buffer[BLOCKSIZE];
  566. if (archive_format == OLDGNU_FORMAT)
  567. header->oldgnu_header.isextended = 0;
  568. if (file = open (name, O_RDONLY), file < 0)
  569. /* This problem will be caught later on, so just return. */
  570. return 0;
  571. init_sparsearray ();
  572. clear_buffer (buffer);
  573. while (0 < (count = safe_read (file, buffer, sizeof buffer)))
  574. {
  575. /* Realloc the scratch area as necessary. FIXME: should reallocate
  576. only at beginning of a new instance of non-zero data. */
  577. if (sp_array_size <= sparses)
  578. {
  579. sparsearray =
  580. xrealloc (sparsearray,
  581. 2 * sp_array_size * sizeof (struct sp_array));
  582. sp_array_size *= 2;
  583. }
  584. /* Process one block. */
  585. if (count == sizeof buffer)
  586. if (zero_block_p (buffer))
  587. {
  588. if (numbytes)
  589. {
  590. sparsearray[sparses++].numbytes = numbytes;
  591. numbytes = 0;
  592. }
  593. }
  594. else
  595. {
  596. if (!numbytes)
  597. sparsearray[sparses].offset = offset;
  598. numbytes += count;
  599. }
  600. else
  601. /* Since count < sizeof buffer, we have the last bit of the file. */
  602. if (!zero_block_p (buffer))
  603. {
  604. if (!numbytes)
  605. sparsearray[sparses].offset = offset;
  606. numbytes += count;
  607. }
  608. else
  609. /* The next two lines are suggested by Andreas Degert, who says
  610. they are required for trailing full blocks to be written to the
  611. archive, when all zeroed. Yet, it seems to me that the case
  612. does not apply. Further, at restore time, the file is not as
  613. sparse as it should. So, some serious cleanup is *also* needed
  614. in this area. Just one more... :-(. FIXME. */
  615. if (numbytes)
  616. numbytes += count;
  617. /* Prepare for next block. */
  618. offset += count;
  619. /* FIXME: do not clear unless necessary. */
  620. clear_buffer (buffer);
  621. }
  622. if (numbytes)
  623. sparsearray[sparses++].numbytes = numbytes;
  624. else
  625. {
  626. sparsearray[sparses].offset = offset - 1;
  627. sparsearray[sparses++].numbytes = 1;
  628. }
  629. close (file);
  630. return count < 0 ? 0 : sparses;
  631. }
  632. /*---.
  633. | ? |
  634. `---*/
  635. static int
  636. finish_sparse_file (int file, off_t *sizeleft, off_t fullsize, char *name)
  637. {
  638. union block *start;
  639. size_t bufsize;
  640. int sparses = 0;
  641. ssize_t count;
  642. while (*sizeleft > 0)
  643. {
  644. start = find_next_block ();
  645. memset (start->buffer, 0, BLOCKSIZE);
  646. bufsize = sparsearray[sparses].numbytes;
  647. if (! bufsize)
  648. abort ();
  649. if (lseek (file, sparsearray[sparses++].offset, SEEK_SET) < 0)
  650. {
  651. (ignore_failed_read_option ? seek_warn_details : seek_error_details)
  652. (name, sparsearray[sparses - 1].offset);
  653. break;
  654. }
  655. /* If the number of bytes to be written here exceeds the size of
  656. the temporary buffer, do it in steps. */
  657. while (bufsize > BLOCKSIZE)
  658. {
  659. count = safe_read (file, start->buffer, BLOCKSIZE);
  660. if (count < 0)
  661. {
  662. (ignore_failed_read_option
  663. ? read_warn_details
  664. : read_error_details)
  665. (name, fullsize - *sizeleft, bufsize);
  666. return 1;
  667. }
  668. bufsize -= count;
  669. *sizeleft -= count;
  670. set_next_block_after (start);
  671. start = find_next_block ();
  672. memset (start->buffer, 0, BLOCKSIZE);
  673. }
  674. {
  675. char buffer[BLOCKSIZE];
  676. clear_buffer (buffer);
  677. count = safe_read (file, buffer, bufsize);
  678. memcpy (start->buffer, buffer, BLOCKSIZE);
  679. }
  680. if (count < 0)
  681. {
  682. (ignore_failed_read_option
  683. ? read_warn_details
  684. : read_error_details)
  685. (name, fullsize - *sizeleft, bufsize);
  686. return 1;
  687. }
  688. *sizeleft -= count;
  689. set_next_block_after (start);
  690. }
  691. free (sparsearray);
  692. #if 0
  693. set_next_block_after (start + (count - 1) / BLOCKSIZE);
  694. #endif
  695. return 0;
  696. }
  697. /* Main functions of this module. */
  698. /*---.
  699. | ? |
  700. `---*/
  701. void
  702. create_archive (void)
  703. {
  704. char *p;
  705. open_archive (ACCESS_WRITE);
  706. if (incremental_option)
  707. {
  708. char *buffer = xmalloc (PATH_MAX);
  709. const char *q;
  710. char *bufp;
  711. collect_and_sort_names ();
  712. while (p = name_from_list (), p)
  713. if (!excluded_name (p))
  714. dump_file (p, -1, (dev_t) 0);
  715. blank_name_list ();
  716. while (p = name_from_list (), p)
  717. if (!excluded_name (p))
  718. {
  719. strcpy (buffer, p);
  720. if (p[strlen (p) - 1] != '/')
  721. strcat (buffer, "/");
  722. bufp = buffer + strlen (buffer);
  723. q = gnu_list_name->dir_contents;
  724. if (q)
  725. for (; *q; q += strlen (q) + 1)
  726. if (*q == 'Y')
  727. {
  728. strcpy (bufp, q + 1);
  729. dump_file (buffer, -1, (dev_t) 0);
  730. }
  731. }
  732. free (buffer);
  733. }
  734. else
  735. {
  736. while (p = name_next (1), p)
  737. if (!excluded_name (p))
  738. dump_file (p, 1, (dev_t) 0);
  739. }
  740. write_eot ();
  741. close_archive ();
  742. if (listed_incremental_option)
  743. write_directory_file ();
  744. }
  745. /* Dump a single file, recursing on directories. P is the file name
  746. to dump. TOP_LEVEL tells whether this is a top-level call; zero
  747. means no, positive means yes, and negative means an incremental
  748. dump. PARENT_DEVICE is the device of P's
  749. parent directory; it is examined only if TOP_LEVEL is zero.
  750. Set global CURRENT_STAT to stat output for this file. */
  751. /* FIXME: One should make sure that for *every* path leading to setting
  752. exit_status to failure, a clear diagnostic has been issued. */
  753. void
  754. dump_file (char *p, int top_level, dev_t parent_device)
  755. {
  756. union block *header;
  757. char type;
  758. union block *exhdr;
  759. char save_typeflag;
  760. time_t original_ctime;
  761. struct utimbuf restore_times;
  762. /* FIXME: `header' and `sparses' might be used uninitialized in this
  763. function. Reported by Bruno Haible. */
  764. if (interactive_option && !confirm ("add", p))
  765. return;
  766. if (deref_stat (dereference_option, p, &current_stat) != 0)
  767. {
  768. if (ignore_failed_read_option)
  769. stat_warn (p);
  770. else
  771. stat_error (p);
  772. return;
  773. }
  774. original_ctime = current_stat.st_ctime;
  775. restore_times.actime = current_stat.st_atime;
  776. restore_times.modtime = current_stat.st_mtime;
  777. #ifdef S_ISHIDDEN
  778. if (S_ISHIDDEN (current_stat.st_mode))
  779. {
  780. char *new = (char *) alloca (strlen (p) + 2);
  781. if (new)
  782. {
  783. strcpy (new, p);
  784. strcat (new, "@");
  785. p = new;
  786. }
  787. }
  788. #endif
  789. /* See if we want only new files, and check if this one is too old to
  790. put in the archive. */
  791. if ((0 < top_level || !incremental_option)
  792. && !S_ISDIR (current_stat.st_mode)
  793. && current_stat.st_mtime < newer_mtime_option
  794. && (!after_date_option || current_stat.st_ctime < newer_ctime_option))
  795. {
  796. if (0 < top_level)
  797. WARN ((0, 0, _("%s: file is unchanged; not dumped"),
  798. quotearg_colon (p)));
  799. /* FIXME: recheck this return. */
  800. return;
  801. }
  802. #if !MSDOS
  803. /* See if we are trying to dump the archive. */
  804. if (ar_dev && current_stat.st_dev == ar_dev && current_stat.st_ino == ar_ino)
  805. {
  806. WARN ((0, 0, _("%s: file is the archive; not dumped"),
  807. quotearg_colon (p)));
  808. return;
  809. }
  810. #endif
  811. if (S_ISDIR (current_stat.st_mode))
  812. {
  813. DIR *directory;
  814. struct dirent *entry;
  815. char *namebuf;
  816. size_t buflen;
  817. size_t len;
  818. dev_t our_device = current_stat.st_dev;
  819. errno = 0;
  820. directory = opendir (p);
  821. if (! directory)
  822. {
  823. if (ignore_failed_read_option)
  824. opendir_warn (p);
  825. else
  826. opendir_error (p);
  827. return;
  828. }
  829. /* Build new prototype name. Ensure exactly one trailing slash. */
  830. len = strlen (p);
  831. buflen = len + NAME_FIELD_SIZE;
  832. namebuf = xmalloc (buflen + 1);
  833. strncpy (namebuf, p, buflen);
  834. while (len >= 1 && namebuf[len - 1] == '/')
  835. len--;
  836. namebuf[len++] = '/';
  837. namebuf[len] = '\0';
  838. if (! is_avoided_name (namebuf))
  839. {
  840. /* The condition above used to be "archive_format != V7_FORMAT".
  841. GNU tar was not writing directory blocks at all. Daniel Trinkle
  842. writes: ``All old versions of tar I have ever seen have
  843. correctly archived an empty directory. The really old ones I
  844. checked included HP-UX 7 and Mt. Xinu More/BSD. There may be
  845. some subtle reason for the exclusion that I don't know, but the
  846. current behavior is broken.'' I do not know those subtle
  847. reasons either, so until these are reported (anew?), just allow
  848. directory blocks to be written even with old archives. */
  849. current_stat.st_size = 0; /* force 0 size on dir */
  850. /* FIXME: If people could really read standard archives, this
  851. should be:
  852. header
  853. = start_header (standard_option ? p : namebuf, &current_stat);
  854. but since they'd interpret DIRTYPE blocks as regular
  855. files, we'd better put the / on the name. */
  856. header = start_header (namebuf, &current_stat);
  857. if (incremental_option)
  858. header->header.typeflag = GNUTYPE_DUMPDIR;
  859. else /* if (standard_option) */
  860. header->header.typeflag = DIRTYPE;
  861. /* If we're gnudumping, we aren't done yet so don't close it. */
  862. if (!incremental_option)
  863. finish_header (header); /* done with directory header */
  864. }
  865. if (incremental_option && gnu_list_name->dir_contents)
  866. {
  867. off_t sizeleft;
  868. off_t totsize;
  869. size_t bufsize;
  870. union block *start;
  871. ssize_t count;
  872. const char *buffer, *p_buffer;
  873. buffer = gnu_list_name->dir_contents; /* FOO */
  874. totsize = 0;
  875. for (p_buffer = buffer; p_buffer && *p_buffer;)
  876. {
  877. size_t tmp;
  878. tmp = strlen (p_buffer) + 1;
  879. totsize += tmp;
  880. p_buffer += tmp;
  881. }
  882. totsize++;
  883. OFF_TO_CHARS (totsize, header->header.size);
  884. finish_header (header);
  885. p_buffer = buffer;
  886. sizeleft = totsize;
  887. while (sizeleft > 0)
  888. {
  889. if (multi_volume_option)
  890. {
  891. assign_string (&save_name, p);
  892. save_sizeleft = sizeleft;
  893. save_totsize = totsize;
  894. }
  895. start = find_next_block ();
  896. bufsize = available_space_after (start);
  897. if (sizeleft < bufsize)
  898. {
  899. bufsize = sizeleft;
  900. count = bufsize % BLOCKSIZE;
  901. if (count)
  902. memset (start->buffer + sizeleft, 0, BLOCKSIZE - count);
  903. }
  904. memcpy (start->buffer, p_buffer, bufsize);
  905. sizeleft -= bufsize;
  906. p_buffer += bufsize;
  907. set_next_block_after (start + (bufsize - 1) / BLOCKSIZE);
  908. }
  909. if (multi_volume_option)
  910. assign_string (&save_name, 0);
  911. goto finish_dir;
  912. }
  913. /* See if we are about to recurse into a directory, and avoid doing
  914. so if the user wants that we do not descend into directories. */
  915. if (no_recurse_option)
  916. goto finish_dir;
  917. /* See if we are crossing from one file system to another, and
  918. avoid doing so if the user only wants to dump one file system. */
  919. if (one_file_system_option && !top_level
  920. && parent_device != current_stat.st_dev)
  921. {
  922. if (verbose_option)
  923. WARN ((0, 0,
  924. _("%s: file is on a different filesystem; not dumped"),
  925. quotearg_colon (p)));
  926. goto finish_dir;
  927. }
  928. /* Now output all the files in the directory. */
  929. /* FIXME: Should speed this up by cd-ing into the dir. */
  930. while (errno = 0, (entry = readdir (directory)))
  931. {
  932. /* Skip `.', `..', and excluded file names. */
  933. if (is_dot_or_dotdot (entry->d_name))
  934. continue;
  935. if ((int) NAMLEN (entry) + len >= buflen)
  936. {
  937. buflen = len + NAMLEN (entry);
  938. namebuf = xrealloc (namebuf, buflen + 1);
  939. }
  940. strcpy (namebuf + len, entry->d_name);
  941. if (!excluded_name (namebuf))
  942. dump_file (namebuf, 0, our_device);
  943. }
  944. if (errno)
  945. {
  946. if (ignore_failed_read_option)
  947. readdir_warn (p);
  948. else
  949. readdir_error (p);
  950. }
  951. finish_dir:
  952. if (closedir (directory) != 0)
  953. {
  954. if (ignore_failed_read_option)
  955. closedir_warn (p);
  956. else
  957. closedir_error (p);
  958. }
  959. free (namebuf);
  960. if (atime_preserve_option)
  961. utime (p, &restore_times);
  962. return;
  963. }
  964. else if (is_avoided_name (p))
  965. return;
  966. else
  967. {
  968. /* Check for multiple links.
  969. We maintain a list of all such files that we've written so far. Any
  970. time we see another, we check the list and avoid dumping the data
  971. again if we've done it once already. */
  972. if (1 < current_stat.st_nlink)
  973. {
  974. struct link *lp;
  975. /* FIXME: First quick and dirty. Hashing, etc later. */
  976. for (lp = linklist; lp; lp = lp->next)
  977. if (lp->ino == current_stat.st_ino
  978. && lp->dev == current_stat.st_dev)
  979. {
  980. char *link_name = lp->name;
  981. /* We found a link. */
  982. while (!absolute_names_option && *link_name == '/')
  983. {
  984. static int warned_once;
  985. if (!warned_once)
  986. {
  987. warned_once = 1;
  988. WARN ((0, 0,
  989. _("Removing leading `/' from link names")));
  990. }
  991. link_name++;
  992. }
  993. if (strlen (link_name) >= NAME_FIELD_SIZE)
  994. write_long (link_name, GNUTYPE_LONGLINK);
  995. assign_string (&current_link_name, link_name);
  996. current_stat.st_size = 0;
  997. header = start_header (p, &current_stat);
  998. strncpy (header->header.linkname,
  999. link_name, NAME_FIELD_SIZE);
  1000. /* Force null truncated. */
  1001. header->header.linkname[NAME_FIELD_SIZE - 1] = 0;
  1002. header->header.typeflag = LNKTYPE;
  1003. finish_header (header);
  1004. /* FIXME: Maybe remove from list after all links found? */
  1005. if (remove_files_option)
  1006. if (unlink (p) == -1)
  1007. unlink_error (p);
  1008. /* We dumped it. */
  1009. return;
  1010. }
  1011. /* Not found. Add it to the list of possible links. */
  1012. lp = xmalloc (sizeof (struct link) + strlen (p));
  1013. lp->ino = current_stat.st_ino;
  1014. lp->dev = current_stat.st_dev;
  1015. strcpy (lp->name, p);
  1016. lp->next = linklist;
  1017. linklist = lp;
  1018. }
  1019. /* This is not a link to a previously dumped file, so dump it. */
  1020. if (S_ISREG (current_stat.st_mode)
  1021. || S_ISCTG (current_stat.st_mode))
  1022. {
  1023. int f; /* file descriptor */
  1024. size_t bufsize;
  1025. ssize_t count;
  1026. off_t sizeleft;
  1027. union block *start;
  1028. int header_moved;
  1029. char isextended = 0;
  1030. int sparses;
  1031. header_moved = 0;
  1032. if (sparse_option)
  1033. {
  1034. /* Check the size of the file against the number of blocks
  1035. allocated for it, counting both data and indirect blocks.
  1036. If there is a smaller number of blocks that would be
  1037. necessary to accommodate a file of this size, this is safe
  1038. to say that we have a sparse file: at least one of those
  1039. blocks in the file is just a useless hole. For sparse
  1040. files not having more hole blocks than indirect blocks, the
  1041. sparseness will go undetected. */
  1042. /* Bruno Haible sent me these statistics for Linux. It seems
  1043. that some filesystems count indirect blocks in st_blocks,
  1044. while others do not seem to:
  1045. minix-fs tar: size=7205, st_blocks=18 and ST_NBLOCKS=18
  1046. extfs tar: size=7205, st_blocks=18 and ST_NBLOCKS=18
  1047. ext2fs tar: size=7205, st_blocks=16 and ST_NBLOCKS=16
  1048. msdos-fs tar: size=7205, st_blocks=16 and ST_NBLOCKS=16
  1049. Dick Streefland reports the previous numbers as misleading,
  1050. because ext2fs use 12 direct blocks, while minix-fs uses only
  1051. 6 direct blocks. Dick gets:
  1052. ext2 size=20480 ls listed blocks=21
  1053. minix size=20480 ls listed blocks=21
  1054. msdos size=20480 ls listed blocks=20
  1055. It seems that indirect blocks *are* included in st_blocks.
  1056. The minix filesystem does not account for phantom blocks in
  1057. st_blocks, so `du' and `ls -s' give wrong results. So, the
  1058. --sparse option would not work on a minix filesystem. */
  1059. if (ST_NBLOCKS (current_stat)
  1060. < (current_stat.st_size / ST_NBLOCKSIZE
  1061. + (current_stat.st_size % ST_NBLOCKSIZE != 0)))
  1062. {
  1063. int counter;
  1064. header = start_header (p, &current_stat);
  1065. header->header.typeflag = GNUTYPE_SPARSE;
  1066. header_moved = 1;
  1067. /* Call the routine that figures out the layout of the
  1068. sparse file in question. SPARSES is the index of the
  1069. first unused element of the "sparsearray," i.e.,
  1070. the number of elements it needed to describe the file. */
  1071. sparses = deal_with_sparse (p, header);
  1072. /* See if we'll need an extended header later. */
  1073. if (SPARSES_IN_OLDGNU_HEADER < sparses)
  1074. header->oldgnu_header.isextended = 1;
  1075. /* We store the "real" file size so we can show that in
  1076. case someone wants to list the archive, i.e., tar tvf
  1077. <file>. It might be kind of disconcerting if the
  1078. shrunken file size was the one that showed up. */
  1079. OFF_TO_CHARS (current_stat.st_size,
  1080. header->oldgnu_header.realsize);
  1081. /* This will be the new "size" of the file, i.e., the size
  1082. of the file minus the blocks of holes that we're
  1083. skipping over. */
  1084. current_stat.st_size = find_new_file_size (sparses);
  1085. OFF_TO_CHARS (current_stat.st_size, header->header.size);
  1086. for (counter = 0;
  1087. counter < sparses && counter < SPARSES_IN_OLDGNU_HEADER;
  1088. counter++)
  1089. {
  1090. OFF_TO_CHARS (sparsearray[counter].offset,
  1091. header->oldgnu_header.sp[counter].offset);
  1092. SIZE_TO_CHARS (sparsearray[counter].numbytes,
  1093. header->oldgnu_header.sp[counter].numbytes);
  1094. }
  1095. }
  1096. }
  1097. else
  1098. sparses = SPARSES_IN_OLDGNU_HEADER;
  1099. sizeleft = current_stat.st_size;
  1100. /* Don't bother opening empty, world readable files. Also do not open
  1101. files when archive is meant for /dev/null. */
  1102. if (dev_null_output
  1103. || (sizeleft == 0
  1104. && MODE_R == (MODE_R & current_stat.st_mode)))
  1105. f = -1;
  1106. else
  1107. {
  1108. f = open (p, O_RDONLY | O_BINARY);
  1109. if (f < 0)
  1110. {
  1111. if (! top_level && errno == ENOENT)
  1112. WARN ((0, 0, _("%s: File removed before we read it"),
  1113. quotearg_colon (p)));
  1114. else
  1115. (ignore_failed_read_option ? open_warn : open_error) (p);
  1116. return;
  1117. }
  1118. }
  1119. /* If the file is sparse, we've already taken care of this. */
  1120. if (!header_moved)
  1121. header = start_header (p, &current_stat);
  1122. /* Mark contiguous files, if we support them. */
  1123. if (archive_format != V7_FORMAT && S_ISCTG (current_stat.st_mode))
  1124. header->header.typeflag = CONTTYPE;
  1125. isextended = header->oldgnu_header.isextended;
  1126. save_typeflag = header->header.typeflag;
  1127. finish_header (header);
  1128. if (isextended)
  1129. {
  1130. int counter;
  1131. /* static */ int index_offset = SPARSES_IN_OLDGNU_HEADER;
  1132. extend:
  1133. exhdr = find_next_block ();
  1134. memset (exhdr->buffer, 0, BLOCKSIZE);
  1135. for (counter = 0; counter < SPARSES_IN_SPARSE_HEADER; counter++)
  1136. {
  1137. if (sparses <= counter + index_offset)
  1138. break;
  1139. SIZE_TO_CHARS (sparsearray[counter + index_offset].numbytes,
  1140. exhdr->sparse_header.sp[counter].numbytes);
  1141. OFF_TO_CHARS (sparsearray[counter + index_offset].offset,
  1142. exhdr->sparse_header.sp[counter].offset);
  1143. }
  1144. set_next_block_after (exhdr);
  1145. if (counter + index_offset < sparses)
  1146. {
  1147. index_offset += counter;
  1148. exhdr->sparse_header.isextended = 1;
  1149. goto extend;
  1150. }
  1151. }
  1152. if (save_typeflag == GNUTYPE_SPARSE)
  1153. {
  1154. if (f < 0
  1155. || finish_sparse_file (f, &sizeleft,
  1156. current_stat.st_size, p))
  1157. goto padit;
  1158. }
  1159. else
  1160. while (sizeleft > 0)
  1161. {
  1162. if (multi_volume_option)
  1163. {
  1164. assign_string (&save_name, p);
  1165. save_sizeleft = sizeleft;
  1166. save_totsize = current_stat.st_size;
  1167. }
  1168. start = find_next_block ();
  1169. bufsize = available_space_after (start);
  1170. if (sizeleft < bufsize)
  1171. {
  1172. /* Last read -- zero out area beyond. */
  1173. bufsize = sizeleft;
  1174. count = bufsize % BLOCKSIZE;
  1175. if (count)
  1176. memset (start->buffer + sizeleft, 0, BLOCKSIZE - count);
  1177. }
  1178. if (f < 0)
  1179. count = bufsize;
  1180. else
  1181. count = safe_read (f, start->buffer, bufsize);
  1182. if (count < 0)
  1183. {
  1184. (ignore_failed_read_option
  1185. ? read_warn_details
  1186. : read_error_details)
  1187. (p, current_stat.st_size - sizeleft, bufsize);
  1188. goto padit;
  1189. }
  1190. sizeleft -= count;
  1191. /* This is nonportable (the type of set_next_block_after's arg). */
  1192. set_next_block_after (start + (count - 1) / BLOCKSIZE);
  1193. if (count == bufsize)
  1194. continue;
  1195. else
  1196. {
  1197. char buf[UINTMAX_STRSIZE_BOUND];
  1198. WARN ((0, 0,
  1199. _("%s: File shrank by %s bytes; padding with zeros"),
  1200. quotearg_colon (p),
  1201. STRINGIFY_BIGINT (sizeleft, buf)));
  1202. if (! ignore_failed_read_option)
  1203. exit_status = TAREXIT_FAILURE;
  1204. goto padit; /* short read */
  1205. }
  1206. }
  1207. if (multi_volume_option)
  1208. assign_string (&save_name, 0);
  1209. if (f >= 0)
  1210. {
  1211. struct stat final_stat;
  1212. if (fstat (f, &final_stat) != 0)
  1213. {
  1214. if (ignore_failed_read_option)
  1215. stat_warn (p);
  1216. else
  1217. stat_error (p);
  1218. }
  1219. else if (final_stat.st_ctime != original_ctime)
  1220. {
  1221. char const *qp = quotearg_colon (p);
  1222. WARN ((0, 0, _("%s: file changed as we read it"), qp));
  1223. if (! ignore_failed_read_option)
  1224. exit_status = TAREXIT_FAILURE;
  1225. }
  1226. if (close (f) != 0)
  1227. {
  1228. if (ignore_failed_read_option)
  1229. close_warn (p);
  1230. else
  1231. close_error (p);
  1232. }
  1233. if (atime_preserve_option)
  1234. utime (p, &restore_times);
  1235. }
  1236. if (remove_files_option)
  1237. {
  1238. if (unlink (p) == -1)
  1239. unlink_error (p);
  1240. }
  1241. return;
  1242. /* File shrunk or gave error, pad out tape to match the size we
  1243. specified in the header. */
  1244. padit:
  1245. while (sizeleft > 0)
  1246. {
  1247. save_sizeleft = sizeleft;
  1248. start = find_next_block ();
  1249. memset (start->buffer, 0, BLOCKSIZE);
  1250. set_next_block_after (start);
  1251. sizeleft -= BLOCKSIZE;
  1252. }
  1253. if (multi_volume_option)
  1254. assign_string (&save_name, 0);
  1255. if (f >= 0)
  1256. {
  1257. close (f);
  1258. if (atime_preserve_option)
  1259. utime (p, &restore_times);
  1260. }
  1261. return;
  1262. }
  1263. #ifdef HAVE_READLINK
  1264. else if (S_ISLNK (current_stat.st_mode))
  1265. {
  1266. int size;
  1267. char *buffer = (char *) alloca (PATH_MAX + 1);
  1268. size = readlink (p, buffer, PATH_MAX + 1);
  1269. if (size < 0)
  1270. {
  1271. if (ignore_failed_read_option)
  1272. readlink_warn (p);
  1273. else
  1274. readlink_error (p);
  1275. return;
  1276. }
  1277. buffer[size] = '\0';
  1278. if (size >= NAME_FIELD_SIZE)
  1279. write_long (buffer, GNUTYPE_LONGLINK);
  1280. assign_string (&current_link_name, buffer);
  1281. current_stat.st_size = 0; /* force 0 size on symlink */
  1282. header = start_header (p, &current_stat);
  1283. strncpy (header->header.linkname, buffer, NAME_FIELD_SIZE);
  1284. header->header.linkname[NAME_FIELD_SIZE - 1] = '\0';
  1285. header->header.typeflag = SYMTYPE;
  1286. finish_header (header); /* nothing more to do to it */
  1287. if (remove_files_option)
  1288. {
  1289. if (unlink (p) == -1)
  1290. unlink_error (p);
  1291. }
  1292. return;
  1293. }
  1294. #endif
  1295. else if (S_ISCHR (current_stat.st_mode))
  1296. type = CHRTYPE;
  1297. else if (S_ISBLK (current_stat.st_mode))
  1298. type = BLKTYPE;
  1299. else if (S_ISFIFO (current_stat.st_mode))
  1300. type = FIFOTYPE;
  1301. else if (S_ISSOCK (current_stat.st_mode))
  1302. {
  1303. WARN ((0, 0, _("%s: socket ignored"), quotearg_colon (p)));
  1304. return;
  1305. }
  1306. else if (S_ISDOOR (current_stat.st_mode))
  1307. {
  1308. WARN ((0, 0, _("%s: door ignored"), quotearg_colon (p)));
  1309. return;
  1310. }
  1311. else
  1312. goto unknown;
  1313. }
  1314. if (archive_format == V7_FORMAT)
  1315. goto unknown;
  1316. current_stat.st_size = 0; /* force 0 size */
  1317. header = start_header (p, &current_stat);
  1318. header->header.typeflag = type;
  1319. if (type != FIFOTYPE)
  1320. {
  1321. MAJOR_TO_CHARS (major (current_stat.st_rdev), header->header.devmajor);
  1322. MINOR_TO_CHARS (minor (current_stat.st_rdev), header->header.devminor);
  1323. }
  1324. finish_header (header);
  1325. if (remove_files_option)
  1326. {
  1327. if (unlink (p) == -1)
  1328. unlink_error (p);
  1329. }
  1330. return;
  1331. unknown:
  1332. WARN ((0, 0, _("%s: Unknown file type; file ignored"),
  1333. quotearg_colon (p)));
  1334. if (! ignore_failed_read_option)
  1335. exit_status = TAREXIT_FAILURE;
  1336. }