create.c 41 KB

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