create.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686
  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 != GNU_FORMAT)
  224. {
  225. negative = v < 0;
  226. u = v;
  227. }
  228. else
  229. {
  230. negative = 0;
  231. u = ((v & S_ISUID ? TSUID : 0)
  232. | (v & S_ISGID ? TSGID : 0)
  233. | (v & S_ISVTX ? TSVTX : 0)
  234. | (v & S_IRUSR ? TUREAD : 0)
  235. | (v & S_IWUSR ? TUWRITE : 0)
  236. | (v & S_IXUSR ? TUEXEC : 0)
  237. | (v & S_IRGRP ? TGREAD : 0)
  238. | (v & S_IWGRP ? TGWRITE : 0)
  239. | (v & S_IXGRP ? TGEXEC : 0)
  240. | (v & S_IROTH ? TOREAD : 0)
  241. | (v & S_IWOTH ? TOWRITE : 0)
  242. | (v & S_IXOTH ? TOEXEC : 0));
  243. }
  244. to_chars (negative, u, sizeof v, 0, p, s, "mode_t");
  245. }
  246. void
  247. off_to_chars (off_t v, char *p, size_t s)
  248. {
  249. to_chars (v < 0, (uintmax_t) v, sizeof v, 0, p, s, "off_t");
  250. }
  251. void
  252. size_to_chars (size_t v, char *p, size_t s)
  253. {
  254. to_chars (0, (uintmax_t) v, sizeof v, 0, p, s, "size_t");
  255. }
  256. void
  257. time_to_chars (time_t v, char *p, size_t s)
  258. {
  259. to_chars (v < 0, (uintmax_t) v, sizeof v, 0, p, s, "time_t");
  260. }
  261. static uintmax_t
  262. uid_substitute (int *negative)
  263. {
  264. uid_t r;
  265. #ifdef UID_NOBODY
  266. r = UID_NOBODY;
  267. #else
  268. static uid_t uid_nobody;
  269. if (!uid_nobody && !uname_to_uid ("nobody", &uid_nobody))
  270. uid_nobody = -2;
  271. r = uid_nobody;
  272. #endif
  273. *negative = r < 0;
  274. return r;
  275. }
  276. void
  277. uid_to_chars (uid_t v, char *p, size_t s)
  278. {
  279. to_chars (v < 0, (uintmax_t) v, sizeof v, uid_substitute, p, s, "uid_t");
  280. }
  281. void
  282. uintmax_to_chars (uintmax_t v, char *p, size_t s)
  283. {
  284. to_chars (0, v, sizeof v, 0, p, s, "uintmax_t");
  285. }
  286. void
  287. string_to_chars (char *str, char *p, size_t s)
  288. {
  289. strncpy (p, str, s);
  290. p[s-1] = 0;
  291. }
  292. /* Writing routines. */
  293. /* Zero out the buffer so we don't confuse ourselves with leftover
  294. data. */
  295. static void
  296. clear_buffer (char *buffer)
  297. {
  298. memset (buffer, 0, BLOCKSIZE);
  299. }
  300. /* Write the EOT block(s). Zero at least two blocks, through the end
  301. of the record. Old tar, as previous versions of GNU tar, writes
  302. garbage after two zeroed blocks. */
  303. void
  304. write_eot (void)
  305. {
  306. union block *pointer = find_next_block ();
  307. memset (pointer->buffer, 0, BLOCKSIZE);
  308. set_next_block_after (pointer);
  309. pointer = find_next_block ();
  310. memset (pointer->buffer, 0, available_space_after (pointer));
  311. set_next_block_after (pointer);
  312. }
  313. /* Write a GNUTYPE_LONGLINK or GNUTYPE_LONGNAME block. */
  314. /* FIXME: Cross recursion between start_header and write_long! */
  315. static union block *start_header (const char *, struct tar_stat_info *);
  316. static void
  317. write_long (const char *p, char type)
  318. {
  319. size_t size = strlen (p) + 1;
  320. size_t bufsize;
  321. union block *header;
  322. struct tar_stat_info foo;
  323. memset (&foo, 0, sizeof foo);
  324. foo.stat.st_size = size;
  325. header = start_header ("././@LongLink", &foo);
  326. header->header.typeflag = type;
  327. finish_header (header, -1);
  328. header = find_next_block ();
  329. bufsize = available_space_after (header);
  330. while (bufsize < size)
  331. {
  332. memcpy (header->buffer, p, bufsize);
  333. p += bufsize;
  334. size -= bufsize;
  335. set_next_block_after (header + (bufsize - 1) / BLOCKSIZE);
  336. header = find_next_block ();
  337. bufsize = available_space_after (header);
  338. }
  339. memcpy (header->buffer, p, size);
  340. memset (header->buffer + size, 0, bufsize - size);
  341. set_next_block_after (header + (size - 1) / BLOCKSIZE);
  342. }
  343. /* Write a long link name, depending on the current archive format */
  344. static void
  345. write_long_link (struct tar_stat_info *st)
  346. {
  347. if (archive_format == POSIX_FORMAT)
  348. xheader_store ("linkpath", st);
  349. else
  350. write_long (st->link_name, GNUTYPE_LONGNAME);
  351. }
  352. /* NOTE: Cross recursion between start_header and write_extended */
  353. static union block *
  354. write_extended (union block *old_header, char type)
  355. {
  356. union block *header, hp;
  357. struct tar_stat_info foo;
  358. size_t size;
  359. char *p;
  360. if (extended_header.buffer || extended_header.stk == NULL)
  361. return old_header; /* Prevent recursion */
  362. xheader_finish (&extended_header);
  363. size = extended_header.size;
  364. memset (&foo, 0, sizeof foo);
  365. foo.stat.st_mode = S_IFREG|S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH;
  366. time (&foo.stat.st_ctime);
  367. foo.stat.st_atime = foo.stat.st_ctime;
  368. foo.stat.st_mtime = foo.stat.st_ctime;
  369. foo.stat.st_size = size;
  370. memcpy (hp.buffer, old_header, sizeof (hp));
  371. header = start_header ("././@PaxHeader", &foo);
  372. header->header.typeflag = type;
  373. finish_header (header, -1);
  374. p = extended_header.buffer;
  375. do
  376. {
  377. size_t len;
  378. header = find_next_block ();
  379. len = BLOCKSIZE;
  380. if (len > size)
  381. len = size;
  382. memcpy (header->buffer, p, len);
  383. if (len < BLOCKSIZE)
  384. memset (header->buffer + len, 0, BLOCKSIZE - len);
  385. p += len;
  386. size -= len;
  387. set_next_block_after (header);
  388. }
  389. while (size > 0);
  390. xheader_destroy (&extended_header);
  391. header = find_next_block ();
  392. memcpy (header, &hp.buffer, sizeof (hp.buffer));
  393. return header;
  394. }
  395. /* Header handling. */
  396. /* Make a header block for the file whose stat info is st,
  397. and return its address. */
  398. static union block *
  399. start_header (const char *name, struct tar_stat_info *st)
  400. {
  401. union block *header;
  402. name = safer_name_suffix (name, 0);
  403. assign_string (&st->file_name, name);
  404. if (sizeof header->header.name <= strlen (name))
  405. {
  406. if (archive_format == POSIX_FORMAT)
  407. xheader_store ("path", st);
  408. else
  409. write_long (name, GNUTYPE_LONGNAME);
  410. }
  411. header = find_next_block ();
  412. memset (header->buffer, 0, sizeof (union block));
  413. assign_string (&current_stat_info.file_name, name);
  414. strncpy (header->header.name, name, NAME_FIELD_SIZE);
  415. header->header.name[NAME_FIELD_SIZE - 1] = '\0';
  416. /* Override some stat fields, if requested to do so. */
  417. if (owner_option != (uid_t) -1)
  418. st->stat.st_uid = owner_option;
  419. if (group_option != (gid_t) -1)
  420. st->stat.st_gid = group_option;
  421. if (mode_option)
  422. st->stat.st_mode = ((st->stat.st_mode & ~MODE_ALL)
  423. | mode_adjust (st->stat.st_mode, mode_option));
  424. /* Paul Eggert tried the trivial test ($WRITER cf a b; $READER tvf a)
  425. for a few tars and came up with the following interoperability
  426. matrix:
  427. WRITER
  428. 1 2 3 4 5 6 7 8 9 READER
  429. . . . . . . . . . 1 = SunOS 4.2 tar
  430. # . . # # . . # # 2 = NEC SVR4.0.2 tar
  431. . . . # # . . # . 3 = Solaris 2.1 tar
  432. . . . . . . . . . 4 = GNU tar 1.11.1
  433. . . . . . . . . . 5 = HP-UX 8.07 tar
  434. . . . . . . . . . 6 = Ultrix 4.1
  435. . . . . . . . . . 7 = AIX 3.2
  436. . . . . . . . . . 8 = Hitachi HI-UX 1.03
  437. . . . . . . . . . 9 = Omron UNIOS-B 4.3BSD 1.60Beta
  438. . = works
  439. # = ``impossible file type''
  440. The following mask for old archive removes the `#'s in column 4
  441. above, thus making GNU tar both a universal donor and a universal
  442. acceptor for Paul's test. */
  443. if (archive_format == V7_FORMAT)
  444. MODE_TO_CHARS (st->stat.st_mode & MODE_ALL, header->header.mode);
  445. else
  446. MODE_TO_CHARS (st->stat.st_mode, header->header.mode);
  447. if (st->stat.st_uid > MAXOCTAL7 && archive_format == POSIX_FORMAT)
  448. xheader_store ("uid", st);
  449. else
  450. UID_TO_CHARS (st->stat.st_uid, header->header.uid);
  451. if (st->stat.st_gid > MAXOCTAL7 && archive_format == POSIX_FORMAT)
  452. xheader_store ("gid", st);
  453. else
  454. GID_TO_CHARS (st->stat.st_gid, header->header.gid);
  455. if (st->stat.st_size > MAXOCTAL11 && archive_format == POSIX_FORMAT)
  456. xheader_store ("size", st);
  457. else
  458. OFF_TO_CHARS (st->stat.st_size, header->header.size);
  459. TIME_TO_CHARS (st->stat.st_mtime, header->header.mtime);
  460. /* FIXME */
  461. if (S_ISCHR (st->stat.st_mode)
  462. || S_ISBLK (st->stat.st_mode))
  463. {
  464. st->devmajor = major (st->stat.st_rdev);
  465. st->devminor = minor (st->stat.st_rdev);
  466. if (st->devmajor > MAXOCTAL7 && archive_format == POSIX_FORMAT)
  467. xheader_store ("devmajor", st);
  468. else
  469. MAJOR_TO_CHARS (st->devmajor, header->header.devmajor);
  470. if (st->devminor > MAXOCTAL7 && archive_format == POSIX_FORMAT)
  471. xheader_store ("devminor", st);
  472. else
  473. MAJOR_TO_CHARS (st->devminor, header->header.devminor);
  474. }
  475. else
  476. {
  477. MAJOR_TO_CHARS (0, header->header.devmajor);
  478. MINOR_TO_CHARS (0, header->header.devminor);
  479. }
  480. if (archive_format == POSIX_FORMAT)
  481. {
  482. xheader_store ("atime", st);
  483. xheader_store ("ctime", st);
  484. }
  485. else if (incremental_option)
  486. if (archive_format == OLDGNU_FORMAT)
  487. {
  488. TIME_TO_CHARS (st->stat.st_atime, header->oldgnu_header.atime);
  489. TIME_TO_CHARS (st->stat.st_ctime, header->oldgnu_header.ctime);
  490. }
  491. header->header.typeflag = archive_format == V7_FORMAT ? AREGTYPE : REGTYPE;
  492. switch (archive_format)
  493. {
  494. case V7_FORMAT:
  495. break;
  496. case OLDGNU_FORMAT:
  497. /* Overwrite header->header.magic and header.version in one blow. */
  498. strcpy (header->header.magic, OLDGNU_MAGIC);
  499. break;
  500. case POSIX_FORMAT:
  501. case GNU_FORMAT:
  502. strncpy (header->header.magic, TMAGIC, TMAGLEN);
  503. strncpy (header->header.version, TVERSION, TVERSLEN);
  504. break;
  505. default:
  506. abort ();
  507. }
  508. if (archive_format == V7_FORMAT || numeric_owner_option)
  509. {
  510. /* header->header.[ug]name are left as the empty string. */
  511. }
  512. else
  513. {
  514. uid_to_uname (st->stat.st_uid, &st->uname);
  515. gid_to_gname (st->stat.st_gid, &st->gname);
  516. if (archive_format == POSIX_FORMAT
  517. && strlen (st->uname) > UNAME_FIELD_SIZE)
  518. xheader_store ("uname", st);
  519. else
  520. UNAME_TO_CHARS (st->uname, header->header.uname);
  521. if (archive_format == POSIX_FORMAT
  522. && strlen (st->gname) > GNAME_FIELD_SIZE)
  523. xheader_store ("gname", st);
  524. else
  525. GNAME_TO_CHARS (st->gname, header->header.gname);
  526. }
  527. return header;
  528. }
  529. /* Finish off a filled-in header block and write it out. We also
  530. print the file name and/or full info if verbose is on. If BLOCK_ORDINAL
  531. is not negative, is the block ordinal of the first record for this
  532. file, which may be a preceding long name or long link record. */
  533. void
  534. finish_header (union block *header, off_t block_ordinal)
  535. {
  536. size_t i;
  537. int sum;
  538. char *p;
  539. /* Note: It is important to do this before the call to write_extended(),
  540. so that the actual ustar header is printed */
  541. if (verbose_option
  542. && header->header.typeflag != GNUTYPE_LONGLINK
  543. && header->header.typeflag != GNUTYPE_LONGNAME
  544. && header->header.typeflag != XHDTYPE
  545. && header->header.typeflag != XGLTYPE)
  546. {
  547. /* These globals are parameters to print_header, sigh. */
  548. current_header = header;
  549. /* current_stat_info is already set up. */
  550. current_format = archive_format;
  551. print_header (block_ordinal);
  552. }
  553. header = write_extended (header, XHDTYPE);
  554. memcpy (header->header.chksum, CHKBLANKS, sizeof header->header.chksum);
  555. sum = 0;
  556. p = header->buffer;
  557. for (i = sizeof *header; i-- != 0; )
  558. /* We can't use unsigned char here because of old compilers, e.g. V7. */
  559. sum += 0xFF & *p++;
  560. /* Fill in the checksum field. It's formatted differently from the
  561. other fields: it has [6] digits, a null, then a space -- rather than
  562. digits, then a null. We use to_chars.
  563. The final space is already there, from
  564. checksumming, and to_chars doesn't modify it.
  565. This is a fast way to do:
  566. sprintf(header->header.chksum, "%6o", sum); */
  567. uintmax_to_chars ((uintmax_t) sum, header->header.chksum, 7);
  568. set_next_block_after (header);
  569. }
  570. /* Sparse file processing. */
  571. /* Takes a blockful of data and basically cruises through it to see if
  572. it's made *entirely* of zeros, returning a 0 the instant it finds
  573. something that is a nonzero, i.e., useful data. */
  574. static int
  575. zero_block_p (char *buffer)
  576. {
  577. int counter;
  578. for (counter = 0; counter < BLOCKSIZE; counter++)
  579. if (buffer[counter] != '\0')
  580. return 0;
  581. return 1;
  582. }
  583. void
  584. init_sparsearray (void)
  585. {
  586. if (! sp_array_size)
  587. sp_array_size = SPARSES_IN_OLDGNU_HEADER;
  588. sparsearray = xmalloc (sp_array_size * sizeof *sparsearray);
  589. }
  590. static off_t
  591. find_new_file_size (int sparses)
  592. {
  593. int i;
  594. off_t s = 0;
  595. for (i = 0; i < sparses; i++)
  596. s += sparsearray[i].numbytes;
  597. return s;
  598. }
  599. /* Make one pass over the file NAME, studying where any non-zero data
  600. is, that is, how far into the file each instance of data is, and
  601. how many bytes are there. Save this information in the
  602. sparsearray, which will later be translated into header
  603. information. */
  604. /* There is little point in trimming small amounts of null data at the head
  605. and tail of blocks, only avoid dumping full null blocks. */
  606. /* FIXME: this routine might accept bits of algorithmic cleanup, it is
  607. too kludgey for my taste... */
  608. static int
  609. deal_with_sparse (char *name, union block *header)
  610. {
  611. size_t numbytes = 0;
  612. off_t offset = 0;
  613. int file;
  614. int sparses = 0;
  615. ssize_t count;
  616. char buffer[BLOCKSIZE];
  617. if (archive_format == OLDGNU_FORMAT)
  618. header->oldgnu_header.isextended = 0;
  619. if (file = open (name, O_RDONLY), file < 0)
  620. /* This problem will be caught later on, so just return. */
  621. return 0;
  622. init_sparsearray ();
  623. clear_buffer (buffer);
  624. for (;;)
  625. {
  626. /* Realloc the scratch area as necessary. FIXME: should reallocate
  627. only at beginning of a new instance of non-zero data. */
  628. if (sp_array_size <= sparses)
  629. {
  630. sparsearray =
  631. xrealloc (sparsearray,
  632. 2 * sp_array_size * sizeof (struct sp_array));
  633. sp_array_size *= 2;
  634. }
  635. count = safe_read (file, buffer, sizeof buffer);
  636. if (count <= 0)
  637. break;
  638. /* Process one block. */
  639. if (count == sizeof buffer)
  640. if (zero_block_p (buffer))
  641. {
  642. if (numbytes)
  643. {
  644. sparsearray[sparses++].numbytes = numbytes;
  645. numbytes = 0;
  646. }
  647. }
  648. else
  649. {
  650. if (!numbytes)
  651. sparsearray[sparses].offset = offset;
  652. numbytes += count;
  653. }
  654. else
  655. /* Since count < sizeof buffer, we have the last bit of the file. */
  656. if (!zero_block_p (buffer))
  657. {
  658. if (!numbytes)
  659. sparsearray[sparses].offset = offset;
  660. numbytes += count;
  661. }
  662. else
  663. /* The next two lines are suggested by Andreas Degert, who says
  664. they are required for trailing full blocks to be written to the
  665. archive, when all zeroed. Yet, it seems to me that the case
  666. does not apply. Further, at restore time, the file is not as
  667. sparse as it should. So, some serious cleanup is *also* needed
  668. in this area. Just one more... :-(. FIXME. */
  669. if (numbytes)
  670. numbytes += count;
  671. /* Prepare for next block. */
  672. offset += count;
  673. /* FIXME: do not clear unless necessary. */
  674. clear_buffer (buffer);
  675. }
  676. if (numbytes)
  677. sparsearray[sparses++].numbytes = numbytes;
  678. else
  679. {
  680. sparsearray[sparses].offset = offset - 1;
  681. sparsearray[sparses++].numbytes = 1;
  682. }
  683. return close (file) == 0 && 0 <= count ? sparses : 0;
  684. }
  685. static int
  686. finish_sparse_file (int file, off_t *sizeleft, off_t fullsize, char *name)
  687. {
  688. union block *start;
  689. size_t bufsize;
  690. int sparses = 0;
  691. ssize_t count;
  692. while (*sizeleft > 0)
  693. {
  694. start = find_next_block ();
  695. memset (start->buffer, 0, BLOCKSIZE);
  696. bufsize = sparsearray[sparses].numbytes;
  697. if (! bufsize)
  698. abort ();
  699. if (lseek (file, sparsearray[sparses++].offset, SEEK_SET) < 0)
  700. {
  701. (ignore_failed_read_option ? seek_warn_details : seek_error_details)
  702. (name, sparsearray[sparses - 1].offset);
  703. break;
  704. }
  705. /* If the number of bytes to be written here exceeds the size of
  706. the temporary buffer, do it in steps. */
  707. while (bufsize > BLOCKSIZE)
  708. {
  709. count = safe_read (file, start->buffer, BLOCKSIZE);
  710. if (count < 0)
  711. {
  712. (ignore_failed_read_option
  713. ? read_warn_details
  714. : read_error_details)
  715. (name, fullsize - *sizeleft, bufsize);
  716. return 1;
  717. }
  718. bufsize -= count;
  719. *sizeleft -= count;
  720. set_next_block_after (start);
  721. start = find_next_block ();
  722. memset (start->buffer, 0, BLOCKSIZE);
  723. }
  724. {
  725. char buffer[BLOCKSIZE];
  726. clear_buffer (buffer);
  727. count = safe_read (file, buffer, bufsize);
  728. memcpy (start->buffer, buffer, BLOCKSIZE);
  729. }
  730. if (count < 0)
  731. {
  732. (ignore_failed_read_option
  733. ? read_warn_details
  734. : read_error_details)
  735. (name, fullsize - *sizeleft, bufsize);
  736. return 1;
  737. }
  738. *sizeleft -= count;
  739. set_next_block_after (start);
  740. }
  741. free (sparsearray);
  742. #if 0
  743. set_next_block_after (start + (count - 1) / BLOCKSIZE);
  744. #endif
  745. return 0;
  746. }
  747. /* Main functions of this module. */
  748. void
  749. create_archive (void)
  750. {
  751. char *p;
  752. open_archive (ACCESS_WRITE);
  753. if (incremental_option)
  754. {
  755. size_t buffer_size = 1000;
  756. char *buffer = xmalloc (buffer_size);
  757. const char *q;
  758. collect_and_sort_names ();
  759. while (p = name_from_list (), p)
  760. if (!excluded_name (p))
  761. dump_file (p, -1, (dev_t) 0);
  762. blank_name_list ();
  763. while (p = name_from_list (), p)
  764. if (!excluded_name (p))
  765. {
  766. size_t plen = strlen (p);
  767. if (buffer_size <= plen)
  768. {
  769. while ((buffer_size *= 2) <= plen)
  770. continue;
  771. buffer = xrealloc (buffer, buffer_size);
  772. }
  773. memcpy (buffer, p, plen);
  774. if (! ISSLASH (buffer[plen - 1]))
  775. buffer[plen++] = '/';
  776. q = gnu_list_name->dir_contents;
  777. if (q)
  778. while (*q)
  779. {
  780. size_t qlen = strlen (q);
  781. if (*q == 'Y')
  782. {
  783. if (buffer_size < plen + qlen)
  784. {
  785. while ((buffer_size *=2 ) < plen + qlen)
  786. continue;
  787. buffer = xrealloc (buffer, buffer_size);
  788. }
  789. strcpy (buffer + plen, q + 1);
  790. dump_file (buffer, -1, (dev_t) 0);
  791. }
  792. q += qlen + 1;
  793. }
  794. }
  795. free (buffer);
  796. }
  797. else
  798. {
  799. while (p = name_next (1), p)
  800. if (!excluded_name (p))
  801. dump_file (p, 1, (dev_t) 0);
  802. }
  803. write_eot ();
  804. close_archive ();
  805. if (listed_incremental_option)
  806. write_directory_file ();
  807. }
  808. /* Calculate the hash of a link. */
  809. static unsigned
  810. hash_link (void const *entry, unsigned n_buckets)
  811. {
  812. struct link const *link = entry;
  813. return (uintmax_t) (link->dev ^ link->ino) % n_buckets;
  814. }
  815. /* Compare two links for equality. */
  816. static bool
  817. compare_links (void const *entry1, void const *entry2)
  818. {
  819. struct link const *link1 = entry1;
  820. struct link const *link2 = entry2;
  821. return ((link1->dev ^ link2->dev) | (link1->ino ^ link2->ino)) == 0;
  822. }
  823. /* Table of all non-directories that we've written so far. Any time
  824. we see another, we check the table and avoid dumping the data
  825. again if we've done it once already. */
  826. static Hash_table *link_table;
  827. /* Dump a single file, recursing on directories. P is the file name
  828. to dump. TOP_LEVEL tells whether this is a top-level call; zero
  829. means no, positive means yes, and negative means the top level
  830. of an incremental dump. PARENT_DEVICE is the device of P's
  831. parent directory; it is examined only if TOP_LEVEL is zero.
  832. Set global CURRENT_STAT_INFO to stat output for this file. */
  833. /* FIXME: One should make sure that for *every* path leading to setting
  834. exit_status to failure, a clear diagnostic has been issued. */
  835. void
  836. dump_file (char *p, int top_level, dev_t parent_device)
  837. {
  838. union block *header;
  839. char type;
  840. union block *exhdr;
  841. char save_typeflag;
  842. time_t original_ctime;
  843. struct utimbuf restore_times;
  844. off_t block_ordinal = -1;
  845. /* FIXME: `header' might be used uninitialized in this
  846. function. Reported by Bruno Haible. */
  847. if (interactive_option && !confirm ("add", p))
  848. return;
  849. if (deref_stat (dereference_option, p, &current_stat_info.stat) != 0)
  850. {
  851. if (ignore_failed_read_option)
  852. stat_warn (p);
  853. else
  854. stat_error (p);
  855. return;
  856. }
  857. original_ctime = current_stat_info.stat.st_ctime;
  858. restore_times.actime = current_stat_info.stat.st_atime;
  859. restore_times.modtime = current_stat_info.stat.st_mtime;
  860. #ifdef S_ISHIDDEN
  861. if (S_ISHIDDEN (current_stat_info.stat.st_mode))
  862. {
  863. char *new = (char *) alloca (strlen (p) + 2);
  864. if (new)
  865. {
  866. strcpy (new, p);
  867. strcat (new, "@");
  868. p = new;
  869. }
  870. }
  871. #endif
  872. /* See if we want only new files, and check if this one is too old to
  873. put in the archive. */
  874. if ((0 < top_level || !incremental_option)
  875. && !S_ISDIR (current_stat_info.stat.st_mode)
  876. && current_stat_info.stat.st_mtime < newer_mtime_option
  877. && (!after_date_option || current_stat_info.stat.st_ctime < newer_ctime_option))
  878. {
  879. if (0 < top_level)
  880. WARN ((0, 0, _("%s: file is unchanged; not dumped"),
  881. quotearg_colon (p)));
  882. /* FIXME: recheck this return. */
  883. return;
  884. }
  885. /* See if we are trying to dump the archive. */
  886. if (sys_file_is_archive (&current_stat_info))
  887. {
  888. WARN ((0, 0, _("%s: file is the archive; not dumped"),
  889. quotearg_colon (p)));
  890. return;
  891. }
  892. if (S_ISDIR (current_stat_info.stat.st_mode))
  893. {
  894. char *directory;
  895. char const *entry;
  896. size_t entrylen;
  897. char *namebuf;
  898. size_t buflen;
  899. size_t len;
  900. dev_t our_device = current_stat_info.stat.st_dev;
  901. errno = 0;
  902. directory = savedir (p);
  903. if (! directory)
  904. {
  905. if (ignore_failed_read_option)
  906. savedir_warn (p);
  907. else
  908. savedir_error (p);
  909. return;
  910. }
  911. /* Build new prototype name. Ensure exactly one trailing slash. */
  912. len = strlen (p);
  913. buflen = len + NAME_FIELD_SIZE;
  914. namebuf = xmalloc (buflen + 1);
  915. memcpy (namebuf, p, len);
  916. while (len >= 1 && ISSLASH (namebuf[len - 1]))
  917. len--;
  918. namebuf[len++] = '/';
  919. namebuf[len] = '\0';
  920. if (! is_avoided_name (namebuf))
  921. {
  922. /* The condition above used to be "archive_format != V7_FORMAT".
  923. GNU tar was not writing directory blocks at all. Daniel Trinkle
  924. writes: ``All old versions of tar I have ever seen have
  925. correctly archived an empty directory. The really old ones I
  926. checked included HP-UX 7 and Mt. Xinu More/BSD. There may be
  927. some subtle reason for the exclusion that I don't know, but the
  928. current behavior is broken.'' I do not know those subtle
  929. reasons either, so until these are reported (anew?), just allow
  930. directory blocks to be written even with old archives. */
  931. block_ordinal = current_block_ordinal ();
  932. current_stat_info.stat.st_size = 0; /* force 0 size on dir */
  933. /* FIXME: If people could really read standard archives, this
  934. should be:
  935. header
  936. = start_header (standard_option ? p : namebuf, &current_stat_info);
  937. but since they'd interpret DIRTYPE blocks as regular
  938. files, we'd better put the / on the name. */
  939. header = start_header (namebuf, &current_stat_info);
  940. if (incremental_option)
  941. header->header.typeflag = GNUTYPE_DUMPDIR;
  942. else /* if (standard_option) */
  943. header->header.typeflag = DIRTYPE;
  944. /* If we're gnudumping, we aren't done yet so don't close it. */
  945. if (!incremental_option)
  946. finish_header (header, block_ordinal);
  947. }
  948. if (incremental_option && gnu_list_name->dir_contents)
  949. {
  950. off_t sizeleft;
  951. off_t totsize;
  952. size_t bufsize;
  953. union block *start;
  954. ssize_t count;
  955. const char *buffer, *p_buffer;
  956. buffer = gnu_list_name->dir_contents; /* FOO */
  957. totsize = 0;
  958. if (buffer)
  959. for (p_buffer = buffer; *p_buffer; )
  960. {
  961. size_t size = strlen (p_buffer) + 1;
  962. totsize += size;
  963. p_buffer += size;
  964. }
  965. totsize++;
  966. OFF_TO_CHARS (totsize, header->header.size);
  967. finish_header (header, block_ordinal);
  968. p_buffer = buffer;
  969. sizeleft = totsize;
  970. while (sizeleft > 0)
  971. {
  972. if (multi_volume_option)
  973. {
  974. assign_string (&save_name, p);
  975. save_sizeleft = sizeleft;
  976. save_totsize = totsize;
  977. }
  978. start = find_next_block ();
  979. bufsize = available_space_after (start);
  980. if (sizeleft < bufsize)
  981. {
  982. bufsize = sizeleft;
  983. count = bufsize % BLOCKSIZE;
  984. if (count)
  985. memset (start->buffer + sizeleft, 0, BLOCKSIZE - count);
  986. }
  987. memcpy (start->buffer, p_buffer, bufsize);
  988. sizeleft -= bufsize;
  989. p_buffer += bufsize;
  990. set_next_block_after (start + (bufsize - 1) / BLOCKSIZE);
  991. }
  992. if (multi_volume_option)
  993. assign_string (&save_name, 0);
  994. goto finish_dir;
  995. }
  996. /* See if we are about to recurse into a directory, and avoid doing
  997. so if the user wants that we do not descend into directories. */
  998. if (! recursion_option)
  999. goto finish_dir;
  1000. /* See if we are crossing from one file system to another, and
  1001. avoid doing so if the user only wants to dump one file system. */
  1002. if (one_file_system_option && !top_level
  1003. && parent_device != current_stat_info.stat.st_dev)
  1004. {
  1005. if (verbose_option)
  1006. WARN ((0, 0,
  1007. _("%s: file is on a different filesystem; not dumped"),
  1008. quotearg_colon (p)));
  1009. goto finish_dir;
  1010. }
  1011. /* Now output all the files in the directory. */
  1012. /* FIXME: Should speed this up by cd-ing into the dir. */
  1013. for (entry = directory;
  1014. (entrylen = strlen (entry)) != 0;
  1015. entry += entrylen + 1)
  1016. {
  1017. if (buflen < len + entrylen)
  1018. {
  1019. buflen = len + entrylen;
  1020. namebuf = xrealloc (namebuf, buflen + 1);
  1021. }
  1022. strcpy (namebuf + len, entry);
  1023. if (!excluded_name (namebuf))
  1024. dump_file (namebuf, 0, our_device);
  1025. }
  1026. finish_dir:
  1027. free (directory);
  1028. free (namebuf);
  1029. if (atime_preserve_option)
  1030. utime (p, &restore_times);
  1031. return;
  1032. }
  1033. else if (is_avoided_name (p))
  1034. return;
  1035. else
  1036. {
  1037. /* Check for multiple links. */
  1038. if (1 < current_stat_info.stat.st_nlink && link_table)
  1039. {
  1040. struct link lp;
  1041. struct link *dup;
  1042. lp.ino = current_stat_info.stat.st_ino;
  1043. lp.dev = current_stat_info.stat.st_dev;
  1044. if ((dup = hash_lookup (link_table, &lp)))
  1045. {
  1046. /* We found a link. */
  1047. char const *link_name = safer_name_suffix (dup->name, 1);
  1048. dup->nlink--;
  1049. block_ordinal = current_block_ordinal ();
  1050. assign_string (&current_stat_info.link_name, link_name);
  1051. if (NAME_FIELD_SIZE <= strlen (link_name))
  1052. write_long_link (&current_stat_info);
  1053. current_stat_info.stat.st_size = 0;
  1054. header = start_header (p, &current_stat_info);
  1055. strncpy (header->header.linkname, link_name, NAME_FIELD_SIZE);
  1056. /* Force null termination. */
  1057. header->header.linkname[NAME_FIELD_SIZE - 1] = 0;
  1058. header->header.typeflag = LNKTYPE;
  1059. finish_header (header, block_ordinal);
  1060. /* FIXME: Maybe remove from table after all links found? */
  1061. if (remove_files_option && unlink (p) != 0)
  1062. unlink_error (p);
  1063. /* We dumped it, and we don't need to put it in the
  1064. table again. */
  1065. return;
  1066. }
  1067. }
  1068. /* This is not a link to a previously dumped file, so dump it. */
  1069. if (S_ISREG (current_stat_info.stat.st_mode)
  1070. || S_ISCTG (current_stat_info.stat.st_mode))
  1071. {
  1072. int f; /* file descriptor */
  1073. size_t bufsize;
  1074. ssize_t count;
  1075. off_t sizeleft;
  1076. union block *start;
  1077. int header_moved;
  1078. char isextended = 0;
  1079. int sparses = 0;
  1080. header_moved = 0;
  1081. if (sparse_option)
  1082. {
  1083. /* Check the size of the file against the number of blocks
  1084. allocated for it, counting both data and indirect blocks.
  1085. If there is a smaller number of blocks than would be
  1086. necessary to accommodate a file of this size, this is safe
  1087. to say that we have a sparse file: at least one of those
  1088. blocks in the file is just a useless hole. For sparse
  1089. files not having more hole blocks than indirect blocks, the
  1090. sparseness will go undetected. */
  1091. /* Bruno Haible sent me these statistics for Linux. It seems
  1092. that some filesystems count indirect blocks in st_blocks,
  1093. while others do not seem to:
  1094. minix-fs tar: size=7205, st_blocks=18 and ST_NBLOCKS=18
  1095. extfs tar: size=7205, st_blocks=18 and ST_NBLOCKS=18
  1096. ext2fs tar: size=7205, st_blocks=16 and ST_NBLOCKS=16
  1097. msdos-fs tar: size=7205, st_blocks=16 and ST_NBLOCKS=16
  1098. Dick Streefland reports the previous numbers as misleading,
  1099. because ext2fs use 12 direct blocks, while minix-fs uses only
  1100. 6 direct blocks. Dick gets:
  1101. ext2 size=20480 ls listed blocks=21
  1102. minix size=20480 ls listed blocks=21
  1103. msdos size=20480 ls listed blocks=20
  1104. It seems that indirect blocks *are* included in st_blocks.
  1105. The minix filesystem does not account for phantom blocks in
  1106. st_blocks, so `du' and `ls -s' give wrong results. So, the
  1107. --sparse option would not work on a minix filesystem. */
  1108. if (ST_NBLOCKS (current_stat_info.stat)
  1109. < (current_stat_info.stat.st_size / ST_NBLOCKSIZE
  1110. + (current_stat_info.stat.st_size % ST_NBLOCKSIZE != 0)))
  1111. {
  1112. int counter;
  1113. block_ordinal = current_block_ordinal ();
  1114. header = start_header (p, &current_stat_info);
  1115. header->header.typeflag = GNUTYPE_SPARSE;
  1116. header_moved = 1;
  1117. /* Call the routine that figures out the layout of the
  1118. sparse file in question. SPARSES is the index of the
  1119. first unused element of the "sparsearray," i.e.,
  1120. the number of elements it needed to describe the file. */
  1121. sparses = deal_with_sparse (p, header);
  1122. /* See if we'll need an extended header later. */
  1123. if (SPARSES_IN_OLDGNU_HEADER < sparses)
  1124. header->oldgnu_header.isextended = 1;
  1125. /* We store the "real" file size so we can show that in
  1126. case someone wants to list the archive, i.e., tar tvf
  1127. <file>. It might be kind of disconcerting if the
  1128. shrunken file size was the one that showed up. */
  1129. OFF_TO_CHARS (current_stat_info.stat.st_size,
  1130. header->oldgnu_header.realsize);
  1131. /* This will be the new "size" of the file, i.e., the size
  1132. of the file minus the blocks of holes that we're
  1133. skipping over. */
  1134. current_stat_info.stat.st_size = find_new_file_size (sparses);
  1135. OFF_TO_CHARS (current_stat_info.stat.st_size, header->header.size);
  1136. for (counter = 0;
  1137. counter < sparses && counter < SPARSES_IN_OLDGNU_HEADER;
  1138. counter++)
  1139. {
  1140. OFF_TO_CHARS (sparsearray[counter].offset,
  1141. header->oldgnu_header.sp[counter].offset);
  1142. SIZE_TO_CHARS (sparsearray[counter].numbytes,
  1143. header->oldgnu_header.sp[counter].numbytes);
  1144. }
  1145. }
  1146. }
  1147. sizeleft = current_stat_info.stat.st_size;
  1148. /* Don't bother opening empty, world readable files. Also do not open
  1149. files when archive is meant for /dev/null. */
  1150. if (dev_null_output
  1151. || (sizeleft == 0
  1152. && MODE_R == (MODE_R & current_stat_info.stat.st_mode)))
  1153. f = -1;
  1154. else
  1155. {
  1156. f = open (p, O_RDONLY | O_BINARY);
  1157. if (f < 0)
  1158. {
  1159. if (! top_level && errno == ENOENT)
  1160. WARN ((0, 0, _("%s: File removed before we read it"),
  1161. quotearg_colon (p)));
  1162. else
  1163. (ignore_failed_read_option ? open_warn : open_error) (p);
  1164. return;
  1165. }
  1166. }
  1167. /* If the file is sparse, we've already taken care of this. */
  1168. if (!header_moved)
  1169. {
  1170. block_ordinal = current_block_ordinal ();
  1171. header = start_header (p, &current_stat_info);
  1172. }
  1173. /* Mark contiguous files, if we support them. */
  1174. if (archive_format != V7_FORMAT && S_ISCTG (current_stat_info.stat.st_mode))
  1175. header->header.typeflag = CONTTYPE;
  1176. isextended = header->oldgnu_header.isextended;
  1177. save_typeflag = header->header.typeflag;
  1178. finish_header (header, block_ordinal);
  1179. if (isextended)
  1180. {
  1181. int sparses_emitted = SPARSES_IN_OLDGNU_HEADER;
  1182. for (;;)
  1183. {
  1184. int i;
  1185. exhdr = find_next_block ();
  1186. memset (exhdr->buffer, 0, BLOCKSIZE);
  1187. for (i = 0;
  1188. (i < SPARSES_IN_SPARSE_HEADER
  1189. && sparses_emitted + i < sparses);
  1190. i++)
  1191. {
  1192. SIZE_TO_CHARS (sparsearray[sparses_emitted + i].numbytes,
  1193. exhdr->sparse_header.sp[i].numbytes);
  1194. OFF_TO_CHARS (sparsearray[sparses_emitted + i].offset,
  1195. exhdr->sparse_header.sp[i].offset);
  1196. }
  1197. set_next_block_after (exhdr);
  1198. sparses_emitted += i;
  1199. if (sparses == sparses_emitted)
  1200. break;
  1201. exhdr->sparse_header.isextended = 1;
  1202. }
  1203. }
  1204. if (save_typeflag == GNUTYPE_SPARSE)
  1205. {
  1206. if (f < 0
  1207. || finish_sparse_file (f, &sizeleft,
  1208. current_stat_info.stat.st_size, p))
  1209. goto padit;
  1210. }
  1211. else
  1212. while (sizeleft > 0)
  1213. {
  1214. if (multi_volume_option)
  1215. {
  1216. assign_string (&save_name, p);
  1217. save_sizeleft = sizeleft;
  1218. save_totsize = current_stat_info.stat.st_size;
  1219. }
  1220. start = find_next_block ();
  1221. bufsize = available_space_after (start);
  1222. if (sizeleft < bufsize)
  1223. {
  1224. /* Last read -- zero out area beyond. */
  1225. bufsize = sizeleft;
  1226. count = bufsize % BLOCKSIZE;
  1227. if (count)
  1228. memset (start->buffer + sizeleft, 0, BLOCKSIZE - count);
  1229. }
  1230. if (f < 0)
  1231. count = bufsize;
  1232. else
  1233. count = safe_read (f, start->buffer, bufsize);
  1234. if (count < 0)
  1235. {
  1236. (ignore_failed_read_option
  1237. ? read_warn_details
  1238. : read_error_details)
  1239. (p, current_stat_info.stat.st_size - sizeleft, bufsize);
  1240. goto padit;
  1241. }
  1242. sizeleft -= count;
  1243. /* This is nonportable (the type of set_next_block_after's arg). */
  1244. set_next_block_after (start + (bufsize - 1) / BLOCKSIZE);
  1245. if (count != bufsize)
  1246. {
  1247. char buf[UINTMAX_STRSIZE_BOUND];
  1248. memset (start->buffer + count, 0, bufsize - count);
  1249. WARN ((0, 0,
  1250. ngettext ("%s: File shrank by %s byte; padding with zeros",
  1251. "%s: File shrank by %s bytes; padding with zeros",
  1252. sizeleft),
  1253. quotearg_colon (p),
  1254. STRINGIFY_BIGINT (sizeleft, buf)));
  1255. if (! ignore_failed_read_option)
  1256. exit_status = TAREXIT_FAILURE;
  1257. goto padit; /* short read */
  1258. }
  1259. }
  1260. if (multi_volume_option)
  1261. assign_string (&save_name, 0);
  1262. if (f >= 0)
  1263. {
  1264. struct stat final_stat;
  1265. if (fstat (f, &final_stat) != 0)
  1266. {
  1267. if (ignore_failed_read_option)
  1268. stat_warn (p);
  1269. else
  1270. stat_error (p);
  1271. }
  1272. else if (final_stat.st_ctime != original_ctime)
  1273. {
  1274. char const *qp = quotearg_colon (p);
  1275. WARN ((0, 0, _("%s: file changed as we read it"), qp));
  1276. }
  1277. if (close (f) != 0)
  1278. {
  1279. if (ignore_failed_read_option)
  1280. close_warn (p);
  1281. else
  1282. close_error (p);
  1283. }
  1284. if (atime_preserve_option)
  1285. utime (p, &restore_times);
  1286. }
  1287. if (remove_files_option)
  1288. {
  1289. if (unlink (p) == -1)
  1290. unlink_error (p);
  1291. }
  1292. goto file_was_dumped;
  1293. /* File shrunk or gave error, pad out tape to match the size we
  1294. specified in the header. */
  1295. padit:
  1296. while (sizeleft > 0)
  1297. {
  1298. save_sizeleft = sizeleft;
  1299. start = find_next_block ();
  1300. memset (start->buffer, 0, BLOCKSIZE);
  1301. set_next_block_after (start);
  1302. sizeleft -= BLOCKSIZE;
  1303. }
  1304. if (multi_volume_option)
  1305. assign_string (&save_name, 0);
  1306. if (f >= 0)
  1307. {
  1308. close (f);
  1309. if (atime_preserve_option)
  1310. utime (p, &restore_times);
  1311. }
  1312. goto file_was_dumped;
  1313. }
  1314. #ifdef HAVE_READLINK
  1315. else if (S_ISLNK (current_stat_info.stat.st_mode))
  1316. {
  1317. char *buffer;
  1318. int size;
  1319. size_t linklen = current_stat_info.stat.st_size;
  1320. if (linklen != current_stat_info.stat.st_size || linklen + 1 == 0)
  1321. xalloc_die ();
  1322. buffer = (char *) alloca (linklen + 1);
  1323. size = readlink (p, buffer, linklen + 1);
  1324. if (size < 0)
  1325. {
  1326. if (ignore_failed_read_option)
  1327. readlink_warn (p);
  1328. else
  1329. readlink_error (p);
  1330. return;
  1331. }
  1332. buffer[size] = '\0';
  1333. assign_string (&current_stat_info.link_name, buffer);
  1334. if (size >= NAME_FIELD_SIZE)
  1335. write_long_link (&current_stat_info);
  1336. block_ordinal = current_block_ordinal ();
  1337. current_stat_info.stat.st_size = 0; /* force 0 size on symlink */
  1338. header = start_header (p, &current_stat_info);
  1339. strncpy (header->header.linkname, buffer, NAME_FIELD_SIZE);
  1340. header->header.linkname[NAME_FIELD_SIZE - 1] = '\0';
  1341. header->header.typeflag = SYMTYPE;
  1342. finish_header (header, block_ordinal);
  1343. /* nothing more to do to it */
  1344. if (remove_files_option)
  1345. {
  1346. if (unlink (p) == -1)
  1347. unlink_error (p);
  1348. }
  1349. goto file_was_dumped;
  1350. }
  1351. #endif
  1352. else if (S_ISCHR (current_stat_info.stat.st_mode))
  1353. type = CHRTYPE;
  1354. else if (S_ISBLK (current_stat_info.stat.st_mode))
  1355. type = BLKTYPE;
  1356. else if (S_ISFIFO (current_stat_info.stat.st_mode))
  1357. type = FIFOTYPE;
  1358. else if (S_ISSOCK (current_stat_info.stat.st_mode))
  1359. {
  1360. WARN ((0, 0, _("%s: socket ignored"), quotearg_colon (p)));
  1361. return;
  1362. }
  1363. else if (S_ISDOOR (current_stat_info.stat.st_mode))
  1364. {
  1365. WARN ((0, 0, _("%s: door ignored"), quotearg_colon (p)));
  1366. return;
  1367. }
  1368. else
  1369. goto unknown;
  1370. }
  1371. if (archive_format == V7_FORMAT)
  1372. goto unknown;
  1373. block_ordinal = current_block_ordinal ();
  1374. current_stat_info.stat.st_size = 0; /* force 0 size */
  1375. header = start_header (p, &current_stat_info);
  1376. header->header.typeflag = type;
  1377. if (type != FIFOTYPE)
  1378. {
  1379. MAJOR_TO_CHARS (major (current_stat_info.stat.st_rdev), header->header.devmajor);
  1380. MINOR_TO_CHARS (minor (current_stat_info.stat.st_rdev), header->header.devminor);
  1381. }
  1382. finish_header (header, block_ordinal);
  1383. if (remove_files_option)
  1384. {
  1385. if (unlink (p) == -1)
  1386. unlink_error (p);
  1387. }
  1388. goto file_was_dumped;
  1389. unknown:
  1390. WARN ((0, 0, _("%s: Unknown file type; file ignored"),
  1391. quotearg_colon (p)));
  1392. if (! ignore_failed_read_option)
  1393. exit_status = TAREXIT_FAILURE;
  1394. return;
  1395. file_was_dumped:
  1396. if (1 < current_stat_info.stat.st_nlink)
  1397. {
  1398. struct link *dup;
  1399. struct link *lp = xmalloc (offsetof (struct link, name)
  1400. + strlen (p) + 1);
  1401. lp->ino = current_stat_info.stat.st_ino;
  1402. lp->dev = current_stat_info.stat.st_dev;
  1403. lp->nlink = current_stat_info.stat.st_nlink;
  1404. strcpy (lp->name, p);
  1405. if (! ((link_table
  1406. || (link_table = hash_initialize (0, 0, hash_link,
  1407. compare_links, 0)))
  1408. && (dup = hash_insert (link_table, lp))))
  1409. xalloc_die ();
  1410. if (dup != lp)
  1411. abort ();
  1412. lp->nlink--;
  1413. }
  1414. }
  1415. /* For each dumped file, check if all its links were dumped. Emit
  1416. warnings if it is not so. */
  1417. void
  1418. check_links ()
  1419. {
  1420. struct link *lp;
  1421. if (!link_table)
  1422. return;
  1423. for (lp = hash_get_first (link_table); lp;
  1424. lp = hash_get_next (link_table, lp))
  1425. {
  1426. if (lp->nlink)
  1427. {
  1428. WARN ((0, 0, _("Missing links to '%s'.\n"), lp->name));
  1429. }
  1430. }
  1431. }