create.c 41 KB

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