4
0

create.c 43 KB

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