create.c 45 KB

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