create.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781
  1. /* Create a tar archive.
  2. Copyright (C) 1985, 1992, 1993, 1994, 1996, 1997, 1999, 2000, 2001,
  3. 2003, 2004, 2005, 2006, 2007, 2009, 2010 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 3, 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. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
  16. #include <system.h>
  17. #include <quotearg.h>
  18. #include "common.h"
  19. #include <hash.h>
  20. struct link
  21. {
  22. dev_t dev;
  23. ino_t ino;
  24. size_t nlink;
  25. char name[1];
  26. };
  27. struct exclusion_tag
  28. {
  29. const char *name;
  30. size_t length;
  31. enum exclusion_tag_type type;
  32. bool (*predicate) (const char *name);
  33. struct exclusion_tag *next;
  34. };
  35. static struct exclusion_tag *exclusion_tags;
  36. void
  37. add_exclusion_tag (const char *name, enum exclusion_tag_type type,
  38. bool (*predicate) (const char *name))
  39. {
  40. struct exclusion_tag *tag = xmalloc (sizeof tag[0]);
  41. tag->next = exclusion_tags;
  42. tag->name = name;
  43. tag->type = type;
  44. tag->predicate = predicate;
  45. tag->length = strlen (name);
  46. exclusion_tags = tag;
  47. }
  48. void
  49. exclusion_tag_warning (const char *dirname, const char *tagname,
  50. const char *message)
  51. {
  52. if (verbose_option)
  53. WARNOPT (WARN_CACHEDIR,
  54. (0, 0,
  55. _("%s: contains a cache directory tag %s; %s"),
  56. quotearg_colon (dirname),
  57. quotearg_n (1, tagname),
  58. message));
  59. }
  60. enum exclusion_tag_type
  61. check_exclusion_tags (const char *dirname, const char **tag_file_name)
  62. {
  63. static char *tagname;
  64. static size_t tagsize;
  65. struct exclusion_tag *tag;
  66. size_t dlen = strlen (dirname);
  67. int addslash = !ISSLASH (dirname[dlen-1]);
  68. size_t noff = 0;
  69. for (tag = exclusion_tags; tag; tag = tag->next)
  70. {
  71. size_t size = dlen + addslash + tag->length + 1;
  72. if (size > tagsize)
  73. {
  74. tagsize = size;
  75. tagname = xrealloc (tagname, tagsize);
  76. }
  77. if (noff == 0)
  78. {
  79. strcpy (tagname, dirname);
  80. noff = dlen;
  81. if (addslash)
  82. tagname[noff++] = '/';
  83. }
  84. strcpy (tagname + noff, tag->name);
  85. if (access (tagname, F_OK) == 0
  86. && (!tag->predicate || tag->predicate (tagname)))
  87. {
  88. if (tag_file_name)
  89. *tag_file_name = tag->name;
  90. return tag->type;
  91. }
  92. }
  93. return exclusion_tag_none;
  94. }
  95. /* Exclusion predicate to test if the named file (usually "CACHEDIR.TAG")
  96. contains a valid header, as described at:
  97. http://www.brynosaurus.com/cachedir
  98. Applications can write this file into directories they create
  99. for use as caches containing purely regenerable, non-precious data,
  100. allowing us to avoid archiving them if --exclude-caches is specified. */
  101. #define CACHEDIR_SIGNATURE "Signature: 8a477f597d28d172789f06886806bc55"
  102. #define CACHEDIR_SIGNATURE_SIZE (sizeof CACHEDIR_SIGNATURE - 1)
  103. bool
  104. cachedir_file_p (const char *name)
  105. {
  106. bool tag_present = false;
  107. int fd = open (name, O_RDONLY);
  108. if (fd >= 0)
  109. {
  110. static char tagbuf[CACHEDIR_SIGNATURE_SIZE];
  111. if (read (fd, tagbuf, CACHEDIR_SIGNATURE_SIZE)
  112. == CACHEDIR_SIGNATURE_SIZE
  113. && memcmp (tagbuf, CACHEDIR_SIGNATURE, CACHEDIR_SIGNATURE_SIZE) == 0)
  114. tag_present = true;
  115. close (fd);
  116. }
  117. return tag_present;
  118. }
  119. /* The maximum uintmax_t value that can be represented with DIGITS digits,
  120. assuming that each digit is BITS_PER_DIGIT wide. */
  121. #define MAX_VAL_WITH_DIGITS(digits, bits_per_digit) \
  122. ((digits) * (bits_per_digit) < sizeof (uintmax_t) * CHAR_BIT \
  123. ? ((uintmax_t) 1 << ((digits) * (bits_per_digit))) - 1 \
  124. : (uintmax_t) -1)
  125. /* The maximum uintmax_t value that can be represented with octal
  126. digits and a trailing NUL in BUFFER. */
  127. #define MAX_OCTAL_VAL(buffer) MAX_VAL_WITH_DIGITS (sizeof (buffer) - 1, LG_8)
  128. /* Convert VALUE to an octal representation suitable for tar headers.
  129. Output to buffer WHERE with size SIZE.
  130. The result is undefined if SIZE is 0 or if VALUE is too large to fit. */
  131. static void
  132. to_octal (uintmax_t value, char *where, size_t size)
  133. {
  134. uintmax_t v = value;
  135. size_t i = size;
  136. do
  137. {
  138. where[--i] = '0' + (v & ((1 << LG_8) - 1));
  139. v >>= LG_8;
  140. }
  141. while (i);
  142. }
  143. /* Copy at most LEN bytes from the string SRC to DST. Terminate with
  144. NUL unless SRC is LEN or more bytes long. */
  145. static void
  146. tar_copy_str (char *dst, const char *src, size_t len)
  147. {
  148. size_t i;
  149. for (i = 0; i < len; i++)
  150. if (! (dst[i] = src[i]))
  151. break;
  152. }
  153. /* Same as tar_copy_str, but always terminate with NUL if using
  154. is OLDGNU format */
  155. static void
  156. tar_name_copy_str (char *dst, const char *src, size_t len)
  157. {
  158. tar_copy_str (dst, src, len);
  159. if (archive_format == OLDGNU_FORMAT)
  160. dst[len-1] = 0;
  161. }
  162. /* Convert NEGATIVE VALUE to a base-256 representation suitable for
  163. tar headers. NEGATIVE is 1 if VALUE was negative before being cast
  164. to uintmax_t, 0 otherwise. Output to buffer WHERE with size SIZE.
  165. The result is undefined if SIZE is 0 or if VALUE is too large to
  166. fit. */
  167. static void
  168. to_base256 (int negative, uintmax_t value, char *where, size_t size)
  169. {
  170. uintmax_t v = value;
  171. uintmax_t propagated_sign_bits =
  172. ((uintmax_t) - negative << (CHAR_BIT * sizeof v - LG_256));
  173. size_t i = size;
  174. do
  175. {
  176. where[--i] = v & ((1 << LG_256) - 1);
  177. v = propagated_sign_bits | (v >> LG_256);
  178. }
  179. while (i);
  180. }
  181. static bool
  182. to_chars (int negative, uintmax_t value, size_t valsize,
  183. uintmax_t (*substitute) (int *),
  184. char *where, size_t size, const char *type);
  185. static bool
  186. to_chars_subst (int negative, int gnu_format, uintmax_t value, size_t valsize,
  187. uintmax_t (*substitute) (int *),
  188. char *where, size_t size, const char *type)
  189. {
  190. uintmax_t maxval = (gnu_format
  191. ? MAX_VAL_WITH_DIGITS (size - 1, LG_256)
  192. : MAX_VAL_WITH_DIGITS (size - 1, LG_8));
  193. char valbuf[UINTMAX_STRSIZE_BOUND + 1];
  194. char maxbuf[UINTMAX_STRSIZE_BOUND];
  195. char minbuf[UINTMAX_STRSIZE_BOUND + 1];
  196. char const *minval_string;
  197. char const *maxval_string = STRINGIFY_BIGINT (maxval, maxbuf);
  198. char const *value_string;
  199. if (gnu_format)
  200. {
  201. uintmax_t m = maxval + 1 ? maxval + 1 : maxval / 2 + 1;
  202. char *p = STRINGIFY_BIGINT (m, minbuf + 1);
  203. *--p = '-';
  204. minval_string = p;
  205. }
  206. else
  207. minval_string = "0";
  208. if (negative)
  209. {
  210. char *p = STRINGIFY_BIGINT (- value, valbuf + 1);
  211. *--p = '-';
  212. value_string = p;
  213. }
  214. else
  215. value_string = STRINGIFY_BIGINT (value, valbuf);
  216. if (substitute)
  217. {
  218. int negsub;
  219. uintmax_t sub = substitute (&negsub) & maxval;
  220. /* NOTE: This is one of the few places where GNU_FORMAT differs from
  221. OLDGNU_FORMAT. The actual differences are:
  222. 1. In OLDGNU_FORMAT all strings in a tar header end in \0
  223. 2. Incremental archives use oldgnu_header.
  224. Apart from this they are completely identical. */
  225. uintmax_t s = (negsub &= archive_format == GNU_FORMAT) ? - sub : sub;
  226. char subbuf[UINTMAX_STRSIZE_BOUND + 1];
  227. char *sub_string = STRINGIFY_BIGINT (s, subbuf + 1);
  228. if (negsub)
  229. *--sub_string = '-';
  230. WARN ((0, 0, _("value %s out of %s range %s..%s; substituting %s"),
  231. value_string, type, minval_string, maxval_string,
  232. sub_string));
  233. return to_chars (negsub, s, valsize, 0, where, size, type);
  234. }
  235. else
  236. ERROR ((0, 0, _("value %s out of %s range %s..%s"),
  237. value_string, type, minval_string, maxval_string));
  238. return false;
  239. }
  240. /* Convert NEGATIVE VALUE (which was originally of size VALSIZE) to
  241. external form, using SUBSTITUTE (...) if VALUE won't fit. Output
  242. to buffer WHERE with size SIZE. NEGATIVE is 1 iff VALUE was
  243. negative before being cast to uintmax_t; its original bitpattern
  244. can be deduced from VALSIZE, its original size before casting.
  245. TYPE is the kind of value being output (useful for diagnostics).
  246. Prefer the POSIX format of SIZE - 1 octal digits (with leading zero
  247. digits), followed by '\0'. If this won't work, and if GNU or
  248. OLDGNU format is allowed, use '\200' followed by base-256, or (if
  249. NEGATIVE is nonzero) '\377' followed by two's complement base-256.
  250. If neither format works, use SUBSTITUTE (...) instead. Pass to
  251. SUBSTITUTE the address of an 0-or-1 flag recording whether the
  252. substitute value is negative. */
  253. static bool
  254. to_chars (int negative, uintmax_t value, size_t valsize,
  255. uintmax_t (*substitute) (int *),
  256. char *where, size_t size, const char *type)
  257. {
  258. int gnu_format = (archive_format == GNU_FORMAT
  259. || archive_format == OLDGNU_FORMAT);
  260. /* Generate the POSIX octal representation if the number fits. */
  261. if (! negative && value <= MAX_VAL_WITH_DIGITS (size - 1, LG_8))
  262. {
  263. where[size - 1] = '\0';
  264. to_octal (value, where, size - 1);
  265. return true;
  266. }
  267. else if (gnu_format)
  268. {
  269. /* Try to cope with the number by using traditional GNU format
  270. methods */
  271. /* Generate the base-256 representation if the number fits. */
  272. if (((negative ? -1 - value : value)
  273. <= MAX_VAL_WITH_DIGITS (size - 1, LG_256)))
  274. {
  275. where[0] = negative ? -1 : 1 << (LG_256 - 1);
  276. to_base256 (negative, value, where + 1, size - 1);
  277. return true;
  278. }
  279. /* Otherwise, if the number is negative, and if it would not cause
  280. ambiguity on this host by confusing positive with negative
  281. values, then generate the POSIX octal representation of the value
  282. modulo 2**(field bits). The resulting tar file is
  283. machine-dependent, since it depends on the host word size. Yuck!
  284. But this is the traditional behavior. */
  285. else if (negative && valsize * CHAR_BIT <= (size - 1) * LG_8)
  286. {
  287. static int warned_once;
  288. if (! warned_once)
  289. {
  290. warned_once = 1;
  291. WARN ((0, 0, _("Generating negative octal headers")));
  292. }
  293. where[size - 1] = '\0';
  294. to_octal (value & MAX_VAL_WITH_DIGITS (valsize * CHAR_BIT, 1),
  295. where, size - 1);
  296. return true;
  297. }
  298. /* Otherwise fall back to substitution, if possible: */
  299. }
  300. else
  301. substitute = NULL; /* No substitution for formats, other than GNU */
  302. return to_chars_subst (negative, gnu_format, value, valsize, substitute,
  303. where, size, type);
  304. }
  305. static uintmax_t
  306. gid_substitute (int *negative)
  307. {
  308. gid_t r;
  309. #ifdef GID_NOBODY
  310. r = GID_NOBODY;
  311. #else
  312. static gid_t gid_nobody;
  313. if (!gid_nobody && !gname_to_gid ("nobody", &gid_nobody))
  314. gid_nobody = -2;
  315. r = gid_nobody;
  316. #endif
  317. *negative = r < 0;
  318. return r;
  319. }
  320. bool
  321. gid_to_chars (gid_t v, char *p, size_t s)
  322. {
  323. return to_chars (v < 0, (uintmax_t) v, sizeof v, gid_substitute, p, s, "gid_t");
  324. }
  325. bool
  326. major_to_chars (major_t v, char *p, size_t s)
  327. {
  328. return to_chars (v < 0, (uintmax_t) v, sizeof v, 0, p, s, "major_t");
  329. }
  330. bool
  331. minor_to_chars (minor_t v, char *p, size_t s)
  332. {
  333. return to_chars (v < 0, (uintmax_t) v, sizeof v, 0, p, s, "minor_t");
  334. }
  335. bool
  336. mode_to_chars (mode_t v, char *p, size_t s)
  337. {
  338. /* In the common case where the internal and external mode bits are the same,
  339. and we are not using POSIX or GNU format,
  340. propagate all unknown bits to the external mode.
  341. This matches historical practice.
  342. Otherwise, just copy the bits we know about. */
  343. int negative;
  344. uintmax_t u;
  345. if (S_ISUID == TSUID && S_ISGID == TSGID && S_ISVTX == TSVTX
  346. && S_IRUSR == TUREAD && S_IWUSR == TUWRITE && S_IXUSR == TUEXEC
  347. && S_IRGRP == TGREAD && S_IWGRP == TGWRITE && S_IXGRP == TGEXEC
  348. && S_IROTH == TOREAD && S_IWOTH == TOWRITE && S_IXOTH == TOEXEC
  349. && archive_format != POSIX_FORMAT
  350. && archive_format != USTAR_FORMAT
  351. && archive_format != GNU_FORMAT)
  352. {
  353. negative = v < 0;
  354. u = v;
  355. }
  356. else
  357. {
  358. negative = 0;
  359. u = ((v & S_ISUID ? TSUID : 0)
  360. | (v & S_ISGID ? TSGID : 0)
  361. | (v & S_ISVTX ? TSVTX : 0)
  362. | (v & S_IRUSR ? TUREAD : 0)
  363. | (v & S_IWUSR ? TUWRITE : 0)
  364. | (v & S_IXUSR ? TUEXEC : 0)
  365. | (v & S_IRGRP ? TGREAD : 0)
  366. | (v & S_IWGRP ? TGWRITE : 0)
  367. | (v & S_IXGRP ? TGEXEC : 0)
  368. | (v & S_IROTH ? TOREAD : 0)
  369. | (v & S_IWOTH ? TOWRITE : 0)
  370. | (v & S_IXOTH ? TOEXEC : 0));
  371. }
  372. return to_chars (negative, u, sizeof v, 0, p, s, "mode_t");
  373. }
  374. bool
  375. off_to_chars (off_t v, char *p, size_t s)
  376. {
  377. return to_chars (v < 0, (uintmax_t) v, sizeof v, 0, p, s, "off_t");
  378. }
  379. bool
  380. size_to_chars (size_t v, char *p, size_t s)
  381. {
  382. return to_chars (0, (uintmax_t) v, sizeof v, 0, p, s, "size_t");
  383. }
  384. bool
  385. time_to_chars (time_t v, char *p, size_t s)
  386. {
  387. return to_chars (v < 0, (uintmax_t) v, sizeof v, 0, p, s, "time_t");
  388. }
  389. static uintmax_t
  390. uid_substitute (int *negative)
  391. {
  392. uid_t r;
  393. #ifdef UID_NOBODY
  394. r = UID_NOBODY;
  395. #else
  396. static uid_t uid_nobody;
  397. if (!uid_nobody && !uname_to_uid ("nobody", &uid_nobody))
  398. uid_nobody = -2;
  399. r = uid_nobody;
  400. #endif
  401. *negative = r < 0;
  402. return r;
  403. }
  404. bool
  405. uid_to_chars (uid_t v, char *p, size_t s)
  406. {
  407. return to_chars (v < 0, (uintmax_t) v, sizeof v, uid_substitute, p, s, "uid_t");
  408. }
  409. bool
  410. uintmax_to_chars (uintmax_t v, char *p, size_t s)
  411. {
  412. return to_chars (0, v, sizeof v, 0, p, s, "uintmax_t");
  413. }
  414. void
  415. string_to_chars (char const *str, char *p, size_t s)
  416. {
  417. tar_copy_str (p, str, s);
  418. p[s - 1] = '\0';
  419. }
  420. /* A file is considered dumpable if it is sparse and both --sparse and --totals
  421. are specified.
  422. Otherwise, it is dumpable unless any of the following conditions occur:
  423. a) it is empty *and* world-readable, or
  424. b) current archive is /dev/null */
  425. bool
  426. file_dumpable_p (struct tar_stat_info *st)
  427. {
  428. if (dev_null_output)
  429. return totals_option && sparse_option && ST_IS_SPARSE (st->stat);
  430. return !(st->archive_file_size == 0
  431. && (st->stat.st_mode & MODE_R) == MODE_R);
  432. }
  433. /* Writing routines. */
  434. /* Write the EOT block(s). Zero at least two blocks, through the end
  435. of the record. Old tar, as previous versions of GNU tar, writes
  436. garbage after two zeroed blocks. */
  437. void
  438. write_eot (void)
  439. {
  440. union block *pointer = find_next_block ();
  441. memset (pointer->buffer, 0, BLOCKSIZE);
  442. set_next_block_after (pointer);
  443. pointer = find_next_block ();
  444. memset (pointer->buffer, 0, available_space_after (pointer));
  445. set_next_block_after (pointer);
  446. }
  447. /* Write a "private" header */
  448. union block *
  449. start_private_header (const char *name, size_t size, time_t t)
  450. {
  451. union block *header = find_next_block ();
  452. memset (header->buffer, 0, sizeof (union block));
  453. tar_name_copy_str (header->header.name, name, NAME_FIELD_SIZE);
  454. OFF_TO_CHARS (size, header->header.size);
  455. TIME_TO_CHARS (t, header->header.mtime);
  456. MODE_TO_CHARS (S_IFREG|S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH, header->header.mode);
  457. UID_TO_CHARS (getuid (), header->header.uid);
  458. GID_TO_CHARS (getgid (), header->header.gid);
  459. MAJOR_TO_CHARS (0, header->header.devmajor);
  460. MINOR_TO_CHARS (0, header->header.devminor);
  461. strncpy (header->header.magic, TMAGIC, TMAGLEN);
  462. strncpy (header->header.version, TVERSION, TVERSLEN);
  463. return header;
  464. }
  465. /* Create a new header and store there at most NAME_FIELD_SIZE bytes of
  466. the file name */
  467. static union block *
  468. write_short_name (struct tar_stat_info *st)
  469. {
  470. union block *header = find_next_block ();
  471. memset (header->buffer, 0, sizeof (union block));
  472. tar_name_copy_str (header->header.name, st->file_name, NAME_FIELD_SIZE);
  473. return header;
  474. }
  475. #define FILL(field,byte) do { \
  476. memset(field, byte, sizeof(field)-1); \
  477. (field)[sizeof(field)-1] = 0; \
  478. } while (0)
  479. /* Write a GNUTYPE_LONGLINK or GNUTYPE_LONGNAME block. */
  480. static void
  481. write_gnu_long_link (struct tar_stat_info *st, const char *p, char type)
  482. {
  483. size_t size = strlen (p) + 1;
  484. size_t bufsize;
  485. union block *header;
  486. char *tmpname;
  487. header = start_private_header ("././@LongLink", size, time (NULL));
  488. FILL (header->header.mtime, '0');
  489. FILL (header->header.mode, '0');
  490. FILL (header->header.uid, '0');
  491. FILL (header->header.gid, '0');
  492. FILL (header->header.devmajor, 0);
  493. FILL (header->header.devminor, 0);
  494. uid_to_uname (0, &tmpname);
  495. UNAME_TO_CHARS (tmpname, header->header.uname);
  496. free (tmpname);
  497. gid_to_gname (0, &tmpname);
  498. GNAME_TO_CHARS (tmpname, header->header.gname);
  499. free (tmpname);
  500. strcpy (header->header.magic, OLDGNU_MAGIC);
  501. header->header.typeflag = type;
  502. finish_header (st, header, -1);
  503. header = find_next_block ();
  504. bufsize = available_space_after (header);
  505. while (bufsize < size)
  506. {
  507. memcpy (header->buffer, p, bufsize);
  508. p += bufsize;
  509. size -= bufsize;
  510. set_next_block_after (header + (bufsize - 1) / BLOCKSIZE);
  511. header = find_next_block ();
  512. bufsize = available_space_after (header);
  513. }
  514. memcpy (header->buffer, p, size);
  515. memset (header->buffer + size, 0, bufsize - size);
  516. set_next_block_after (header + (size - 1) / BLOCKSIZE);
  517. }
  518. static size_t
  519. split_long_name (const char *name, size_t length)
  520. {
  521. size_t i;
  522. if (length > PREFIX_FIELD_SIZE + 1)
  523. length = PREFIX_FIELD_SIZE + 1;
  524. else if (ISSLASH (name[length - 1]))
  525. length--;
  526. for (i = length - 1; i > 0; i--)
  527. if (ISSLASH (name[i]))
  528. break;
  529. return i;
  530. }
  531. static union block *
  532. write_ustar_long_name (const char *name)
  533. {
  534. size_t length = strlen (name);
  535. size_t i, nlen;
  536. union block *header;
  537. if (length > PREFIX_FIELD_SIZE + NAME_FIELD_SIZE + 1)
  538. {
  539. ERROR ((0, 0, _("%s: file name is too long (max %d); not dumped"),
  540. quotearg_colon (name),
  541. PREFIX_FIELD_SIZE + NAME_FIELD_SIZE + 1));
  542. return NULL;
  543. }
  544. i = split_long_name (name, length);
  545. if (i == 0 || (nlen = length - i - 1) > NAME_FIELD_SIZE || nlen == 0)
  546. {
  547. ERROR ((0, 0,
  548. _("%s: file name is too long (cannot be split); not dumped"),
  549. quotearg_colon (name)));
  550. return NULL;
  551. }
  552. header = find_next_block ();
  553. memset (header->buffer, 0, sizeof (header->buffer));
  554. memcpy (header->header.prefix, name, i);
  555. memcpy (header->header.name, name + i + 1, length - i - 1);
  556. return header;
  557. }
  558. /* Write a long link name, depending on the current archive format */
  559. static void
  560. write_long_link (struct tar_stat_info *st)
  561. {
  562. switch (archive_format)
  563. {
  564. case POSIX_FORMAT:
  565. xheader_store ("linkpath", st, NULL);
  566. break;
  567. case V7_FORMAT: /* old V7 tar format */
  568. case USTAR_FORMAT:
  569. case STAR_FORMAT:
  570. ERROR ((0, 0,
  571. _("%s: link name is too long; not dumped"),
  572. quotearg_colon (st->link_name)));
  573. break;
  574. case OLDGNU_FORMAT:
  575. case GNU_FORMAT:
  576. write_gnu_long_link (st, st->link_name, GNUTYPE_LONGLINK);
  577. break;
  578. default:
  579. abort(); /*FIXME*/
  580. }
  581. }
  582. static union block *
  583. write_long_name (struct tar_stat_info *st)
  584. {
  585. switch (archive_format)
  586. {
  587. case POSIX_FORMAT:
  588. xheader_store ("path", st, NULL);
  589. break;
  590. case V7_FORMAT:
  591. if (strlen (st->file_name) > NAME_FIELD_SIZE-1)
  592. {
  593. ERROR ((0, 0, _("%s: file name is too long (max %d); not dumped"),
  594. quotearg_colon (st->file_name),
  595. NAME_FIELD_SIZE - 1));
  596. return NULL;
  597. }
  598. break;
  599. case USTAR_FORMAT:
  600. case STAR_FORMAT:
  601. return write_ustar_long_name (st->file_name);
  602. case OLDGNU_FORMAT:
  603. case GNU_FORMAT:
  604. write_gnu_long_link (st, st->file_name, GNUTYPE_LONGNAME);
  605. break;
  606. default:
  607. abort(); /*FIXME*/
  608. }
  609. return write_short_name (st);
  610. }
  611. union block *
  612. write_extended (bool global, struct tar_stat_info *st, union block *old_header)
  613. {
  614. union block *header, hp;
  615. char *p;
  616. int type;
  617. time_t t;
  618. if (st->xhdr.buffer || st->xhdr.stk == NULL)
  619. return old_header;
  620. xheader_finish (&st->xhdr);
  621. memcpy (hp.buffer, old_header, sizeof (hp));
  622. if (global)
  623. {
  624. type = XGLTYPE;
  625. p = xheader_ghdr_name ();
  626. time (&t);
  627. }
  628. else
  629. {
  630. type = XHDTYPE;
  631. p = xheader_xhdr_name (st);
  632. t = st->stat.st_mtime;
  633. }
  634. xheader_write (type, p, t, &st->xhdr);
  635. free (p);
  636. header = find_next_block ();
  637. memcpy (header, &hp.buffer, sizeof (hp.buffer));
  638. return header;
  639. }
  640. static union block *
  641. write_header_name (struct tar_stat_info *st)
  642. {
  643. if (archive_format == POSIX_FORMAT && !string_ascii_p (st->file_name))
  644. {
  645. xheader_store ("path", st, NULL);
  646. return write_short_name (st);
  647. }
  648. else if (NAME_FIELD_SIZE - (archive_format == OLDGNU_FORMAT)
  649. < strlen (st->file_name))
  650. return write_long_name (st);
  651. else
  652. return write_short_name (st);
  653. }
  654. /* Header handling. */
  655. /* Make a header block for the file whose stat info is st,
  656. and return its address. */
  657. union block *
  658. start_header (struct tar_stat_info *st)
  659. {
  660. union block *header;
  661. header = write_header_name (st);
  662. if (!header)
  663. return NULL;
  664. /* Override some stat fields, if requested to do so. */
  665. if (owner_option != (uid_t) -1)
  666. st->stat.st_uid = owner_option;
  667. if (group_option != (gid_t) -1)
  668. st->stat.st_gid = group_option;
  669. if (mode_option)
  670. st->stat.st_mode =
  671. ((st->stat.st_mode & ~MODE_ALL)
  672. | mode_adjust (st->stat.st_mode, S_ISDIR (st->stat.st_mode) != 0,
  673. initial_umask, mode_option, NULL));
  674. /* Paul Eggert tried the trivial test ($WRITER cf a b; $READER tvf a)
  675. for a few tars and came up with the following interoperability
  676. matrix:
  677. WRITER
  678. 1 2 3 4 5 6 7 8 9 READER
  679. . . . . . . . . . 1 = SunOS 4.2 tar
  680. # . . # # . . # # 2 = NEC SVR4.0.2 tar
  681. . . . # # . . # . 3 = Solaris 2.1 tar
  682. . . . . . . . . . 4 = GNU tar 1.11.1
  683. . . . . . . . . . 5 = HP-UX 8.07 tar
  684. . . . . . . . . . 6 = Ultrix 4.1
  685. . . . . . . . . . 7 = AIX 3.2
  686. . . . . . . . . . 8 = Hitachi HI-UX 1.03
  687. . . . . . . . . . 9 = Omron UNIOS-B 4.3BSD 1.60Beta
  688. . = works
  689. # = ``impossible file type''
  690. The following mask for old archive removes the `#'s in column 4
  691. above, thus making GNU tar both a universal donor and a universal
  692. acceptor for Paul's test. */
  693. if (archive_format == V7_FORMAT || archive_format == USTAR_FORMAT)
  694. MODE_TO_CHARS (st->stat.st_mode & MODE_ALL, header->header.mode);
  695. else
  696. MODE_TO_CHARS (st->stat.st_mode, header->header.mode);
  697. {
  698. uid_t uid = st->stat.st_uid;
  699. if (archive_format == POSIX_FORMAT
  700. && MAX_OCTAL_VAL (header->header.uid) < uid)
  701. {
  702. xheader_store ("uid", st, NULL);
  703. uid = 0;
  704. }
  705. if (!UID_TO_CHARS (uid, header->header.uid))
  706. return NULL;
  707. }
  708. {
  709. gid_t gid = st->stat.st_gid;
  710. if (archive_format == POSIX_FORMAT
  711. && MAX_OCTAL_VAL (header->header.gid) < gid)
  712. {
  713. xheader_store ("gid", st, NULL);
  714. gid = 0;
  715. }
  716. if (!GID_TO_CHARS (gid, header->header.gid))
  717. return NULL;
  718. }
  719. {
  720. off_t size = st->stat.st_size;
  721. if (archive_format == POSIX_FORMAT
  722. && MAX_OCTAL_VAL (header->header.size) < size)
  723. {
  724. xheader_store ("size", st, NULL);
  725. size = 0;
  726. }
  727. if (!OFF_TO_CHARS (size, header->header.size))
  728. return NULL;
  729. }
  730. {
  731. struct timespec mtime = set_mtime_option ? mtime_option : st->mtime;
  732. if (archive_format == POSIX_FORMAT)
  733. {
  734. if (MAX_OCTAL_VAL (header->header.mtime) < mtime.tv_sec
  735. || mtime.tv_nsec != 0)
  736. xheader_store ("mtime", st, &mtime);
  737. if (MAX_OCTAL_VAL (header->header.mtime) < mtime.tv_sec)
  738. mtime.tv_sec = 0;
  739. }
  740. if (!TIME_TO_CHARS (mtime.tv_sec, header->header.mtime))
  741. return NULL;
  742. }
  743. /* FIXME */
  744. if (S_ISCHR (st->stat.st_mode)
  745. || S_ISBLK (st->stat.st_mode))
  746. {
  747. major_t devmajor = major (st->stat.st_rdev);
  748. minor_t devminor = minor (st->stat.st_rdev);
  749. if (archive_format == POSIX_FORMAT
  750. && MAX_OCTAL_VAL (header->header.devmajor) < devmajor)
  751. {
  752. xheader_store ("devmajor", st, NULL);
  753. devmajor = 0;
  754. }
  755. if (!MAJOR_TO_CHARS (devmajor, header->header.devmajor))
  756. return NULL;
  757. if (archive_format == POSIX_FORMAT
  758. && MAX_OCTAL_VAL (header->header.devminor) < devminor)
  759. {
  760. xheader_store ("devminor", st, NULL);
  761. devminor = 0;
  762. }
  763. if (!MINOR_TO_CHARS (devminor, header->header.devminor))
  764. return NULL;
  765. }
  766. else if (archive_format != GNU_FORMAT && archive_format != OLDGNU_FORMAT)
  767. {
  768. if (!(MAJOR_TO_CHARS (0, header->header.devmajor)
  769. && MINOR_TO_CHARS (0, header->header.devminor)))
  770. return NULL;
  771. }
  772. if (archive_format == POSIX_FORMAT)
  773. {
  774. xheader_store ("atime", st, NULL);
  775. xheader_store ("ctime", st, NULL);
  776. }
  777. else if (incremental_option)
  778. if (archive_format == OLDGNU_FORMAT || archive_format == GNU_FORMAT)
  779. {
  780. TIME_TO_CHARS (st->atime.tv_sec, header->oldgnu_header.atime);
  781. TIME_TO_CHARS (st->ctime.tv_sec, header->oldgnu_header.ctime);
  782. }
  783. header->header.typeflag = archive_format == V7_FORMAT ? AREGTYPE : REGTYPE;
  784. switch (archive_format)
  785. {
  786. case V7_FORMAT:
  787. break;
  788. case OLDGNU_FORMAT:
  789. case GNU_FORMAT: /*FIXME?*/
  790. /* Overwrite header->header.magic and header.version in one blow. */
  791. strcpy (header->header.magic, OLDGNU_MAGIC);
  792. break;
  793. case POSIX_FORMAT:
  794. case USTAR_FORMAT:
  795. strncpy (header->header.magic, TMAGIC, TMAGLEN);
  796. strncpy (header->header.version, TVERSION, TVERSLEN);
  797. break;
  798. default:
  799. abort ();
  800. }
  801. if (archive_format == V7_FORMAT || numeric_owner_option)
  802. {
  803. /* header->header.[ug]name are left as the empty string. */
  804. }
  805. else
  806. {
  807. uid_to_uname (st->stat.st_uid, &st->uname);
  808. gid_to_gname (st->stat.st_gid, &st->gname);
  809. if (archive_format == POSIX_FORMAT
  810. && (strlen (st->uname) > UNAME_FIELD_SIZE
  811. || !string_ascii_p (st->uname)))
  812. xheader_store ("uname", st, NULL);
  813. UNAME_TO_CHARS (st->uname, header->header.uname);
  814. if (archive_format == POSIX_FORMAT
  815. && (strlen (st->gname) > GNAME_FIELD_SIZE
  816. || !string_ascii_p (st->gname)))
  817. xheader_store ("gname", st, NULL);
  818. GNAME_TO_CHARS (st->gname, header->header.gname);
  819. }
  820. return header;
  821. }
  822. void
  823. simple_finish_header (union block *header)
  824. {
  825. size_t i;
  826. int sum;
  827. char *p;
  828. memcpy (header->header.chksum, CHKBLANKS, sizeof header->header.chksum);
  829. sum = 0;
  830. p = header->buffer;
  831. for (i = sizeof *header; i-- != 0; )
  832. /* We can't use unsigned char here because of old compilers, e.g. V7. */
  833. sum += 0xFF & *p++;
  834. /* Fill in the checksum field. It's formatted differently from the
  835. other fields: it has [6] digits, a null, then a space -- rather than
  836. digits, then a null. We use to_chars.
  837. The final space is already there, from
  838. checksumming, and to_chars doesn't modify it.
  839. This is a fast way to do:
  840. sprintf(header->header.chksum, "%6o", sum); */
  841. uintmax_to_chars ((uintmax_t) sum, header->header.chksum, 7);
  842. set_next_block_after (header);
  843. }
  844. /* Finish off a filled-in header block and write it out. We also
  845. print the file name and/or full info if verbose is on. If BLOCK_ORDINAL
  846. is not negative, is the block ordinal of the first record for this
  847. file, which may be a preceding long name or long link record. */
  848. void
  849. finish_header (struct tar_stat_info *st,
  850. union block *header, off_t block_ordinal)
  851. {
  852. /* Note: It is important to do this before the call to write_extended(),
  853. so that the actual ustar header is printed */
  854. if (verbose_option
  855. && header->header.typeflag != GNUTYPE_LONGLINK
  856. && header->header.typeflag != GNUTYPE_LONGNAME
  857. && header->header.typeflag != XHDTYPE
  858. && header->header.typeflag != XGLTYPE)
  859. {
  860. /* FIXME: This global is used in print_header, sigh. */
  861. current_format = archive_format;
  862. print_header (st, header, block_ordinal);
  863. }
  864. header = write_extended (false, st, header);
  865. simple_finish_header (header);
  866. }
  867. void
  868. pad_archive (off_t size_left)
  869. {
  870. union block *blk;
  871. while (size_left > 0)
  872. {
  873. mv_size_left (size_left);
  874. blk = find_next_block ();
  875. memset (blk->buffer, 0, BLOCKSIZE);
  876. set_next_block_after (blk);
  877. size_left -= BLOCKSIZE;
  878. }
  879. }
  880. static enum dump_status
  881. dump_regular_file (int fd, struct tar_stat_info *st)
  882. {
  883. off_t size_left = st->stat.st_size;
  884. off_t block_ordinal;
  885. union block *blk;
  886. block_ordinal = current_block_ordinal ();
  887. blk = start_header (st);
  888. if (!blk)
  889. return dump_status_fail;
  890. /* Mark contiguous files, if we support them. */
  891. if (archive_format != V7_FORMAT && S_ISCTG (st->stat.st_mode))
  892. blk->header.typeflag = CONTTYPE;
  893. finish_header (st, blk, block_ordinal);
  894. mv_begin (st);
  895. while (size_left > 0)
  896. {
  897. size_t bufsize, count;
  898. mv_size_left (size_left);
  899. blk = find_next_block ();
  900. bufsize = available_space_after (blk);
  901. if (size_left < bufsize)
  902. {
  903. /* Last read -- zero out area beyond. */
  904. bufsize = size_left;
  905. count = bufsize % BLOCKSIZE;
  906. if (count)
  907. memset (blk->buffer + size_left, 0, BLOCKSIZE - count);
  908. }
  909. count = (fd < 0) ? bufsize : safe_read (fd, blk->buffer, bufsize);
  910. if (count == SAFE_READ_ERROR)
  911. {
  912. read_diag_details (st->orig_file_name,
  913. st->stat.st_size - size_left, bufsize);
  914. pad_archive (size_left);
  915. return dump_status_short;
  916. }
  917. size_left -= count;
  918. set_next_block_after (blk + (bufsize - 1) / BLOCKSIZE);
  919. if (count != bufsize)
  920. {
  921. char buf[UINTMAX_STRSIZE_BOUND];
  922. memset (blk->buffer + count, 0, bufsize - count);
  923. WARNOPT (WARN_FILE_SHRANK,
  924. (0, 0,
  925. ngettext ("%s: File shrank by %s byte; padding with zeros",
  926. "%s: File shrank by %s bytes; padding with zeros",
  927. size_left),
  928. quotearg_colon (st->orig_file_name),
  929. STRINGIFY_BIGINT (size_left, buf)));
  930. if (! ignore_failed_read_option)
  931. set_exit_status (TAREXIT_DIFFERS);
  932. pad_archive (size_left - (bufsize - count));
  933. return dump_status_short;
  934. }
  935. }
  936. return dump_status_ok;
  937. }
  938. static void
  939. dump_dir0 (char *directory,
  940. struct tar_stat_info *st, bool top_level, dev_t parent_device)
  941. {
  942. dev_t our_device = st->stat.st_dev;
  943. const char *tag_file_name;
  944. union block *blk = NULL;
  945. off_t block_ordinal = current_block_ordinal ();
  946. st->stat.st_size = 0; /* force 0 size on dir */
  947. blk = start_header (st);
  948. if (!blk)
  949. return;
  950. if (incremental_option && archive_format != POSIX_FORMAT)
  951. blk->header.typeflag = GNUTYPE_DUMPDIR;
  952. else /* if (standard_option) */
  953. blk->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 (st, blk, block_ordinal);
  957. else if (gnu_list_name->directory)
  958. {
  959. if (archive_format == POSIX_FORMAT)
  960. {
  961. xheader_store ("GNU.dumpdir", st,
  962. safe_directory_contents (gnu_list_name->directory));
  963. finish_header (st, blk, block_ordinal);
  964. }
  965. else
  966. {
  967. off_t size_left;
  968. off_t totsize;
  969. size_t bufsize;
  970. ssize_t count;
  971. const char *buffer, *p_buffer;
  972. block_ordinal = current_block_ordinal ();
  973. buffer = safe_directory_contents (gnu_list_name->directory);
  974. totsize = dumpdir_size (buffer);
  975. OFF_TO_CHARS (totsize, blk->header.size);
  976. finish_header (st, blk, block_ordinal);
  977. p_buffer = buffer;
  978. size_left = totsize;
  979. mv_begin (st);
  980. mv_total_size (totsize);
  981. while (size_left > 0)
  982. {
  983. mv_size_left (size_left);
  984. blk = find_next_block ();
  985. bufsize = available_space_after (blk);
  986. if (size_left < bufsize)
  987. {
  988. bufsize = size_left;
  989. count = bufsize % BLOCKSIZE;
  990. if (count)
  991. memset (blk->buffer + size_left, 0, BLOCKSIZE - count);
  992. }
  993. memcpy (blk->buffer, p_buffer, bufsize);
  994. size_left -= bufsize;
  995. p_buffer += bufsize;
  996. set_next_block_after (blk + (bufsize - 1) / BLOCKSIZE);
  997. }
  998. mv_end ();
  999. }
  1000. return;
  1001. }
  1002. if (!recursion_option)
  1003. return;
  1004. if (one_file_system_option
  1005. && !top_level
  1006. && parent_device != st->stat.st_dev)
  1007. {
  1008. if (verbose_option)
  1009. WARNOPT (WARN_XDEV,
  1010. (0, 0,
  1011. _("%s: file is on a different filesystem; not dumped"),
  1012. quotearg_colon (st->orig_file_name)));
  1013. }
  1014. else
  1015. {
  1016. char *name_buf;
  1017. size_t name_size;
  1018. switch (check_exclusion_tags (st->orig_file_name, &tag_file_name))
  1019. {
  1020. case exclusion_tag_all:
  1021. /* Handled in dump_file0 */
  1022. break;
  1023. case exclusion_tag_none:
  1024. {
  1025. char const *entry;
  1026. size_t entry_len;
  1027. size_t name_len;
  1028. name_buf = xstrdup (st->orig_file_name);
  1029. name_size = name_len = strlen (name_buf);
  1030. /* Now output all the files in the directory. */
  1031. /* FIXME: Should speed this up by cd-ing into the dir. */
  1032. for (entry = directory; (entry_len = strlen (entry)) != 0;
  1033. entry += entry_len + 1)
  1034. {
  1035. if (name_size < name_len + entry_len)
  1036. {
  1037. name_size = name_len + entry_len;
  1038. name_buf = xrealloc (name_buf, name_size + 1);
  1039. }
  1040. strcpy (name_buf + name_len, entry);
  1041. if (!excluded_name (name_buf))
  1042. dump_file (name_buf, false, our_device);
  1043. }
  1044. free (name_buf);
  1045. }
  1046. break;
  1047. case exclusion_tag_contents:
  1048. exclusion_tag_warning (st->orig_file_name, tag_file_name,
  1049. _("contents not dumped"));
  1050. name_size = strlen (st->orig_file_name) + strlen (tag_file_name) + 1;
  1051. name_buf = xmalloc (name_size);
  1052. strcpy (name_buf, st->orig_file_name);
  1053. strcat (name_buf, tag_file_name);
  1054. dump_file (name_buf, false, our_device);
  1055. free (name_buf);
  1056. break;
  1057. case exclusion_tag_under:
  1058. exclusion_tag_warning (st->orig_file_name, tag_file_name,
  1059. _("contents not dumped"));
  1060. break;
  1061. }
  1062. }
  1063. }
  1064. /* Ensure exactly one trailing slash. */
  1065. static void
  1066. ensure_slash (char **pstr)
  1067. {
  1068. size_t len = strlen (*pstr);
  1069. while (len >= 1 && ISSLASH ((*pstr)[len - 1]))
  1070. len--;
  1071. if (!ISSLASH ((*pstr)[len]))
  1072. *pstr = xrealloc (*pstr, len + 2);
  1073. (*pstr)[len++] = '/';
  1074. (*pstr)[len] = '\0';
  1075. }
  1076. static bool
  1077. dump_dir (int fd, struct tar_stat_info *st, bool top_level,
  1078. dev_t parent_device)
  1079. {
  1080. char *directory = fdsavedir (fd);
  1081. if (!directory)
  1082. {
  1083. savedir_diag (st->orig_file_name);
  1084. return false;
  1085. }
  1086. dump_dir0 (directory, st, top_level, parent_device);
  1087. free (directory);
  1088. return true;
  1089. }
  1090. /* Main functions of this module. */
  1091. void
  1092. create_archive (void)
  1093. {
  1094. struct name const *p;
  1095. open_archive (ACCESS_WRITE);
  1096. buffer_write_global_xheader ();
  1097. if (incremental_option)
  1098. {
  1099. size_t buffer_size = 1000;
  1100. char *buffer = xmalloc (buffer_size);
  1101. const char *q;
  1102. collect_and_sort_names ();
  1103. while ((p = name_from_list ()) != NULL)
  1104. if (!excluded_name (p->name))
  1105. dump_file (p->name, p->cmdline, (dev_t) 0);
  1106. blank_name_list ();
  1107. while ((p = name_from_list ()) != NULL)
  1108. if (!excluded_name (p->name))
  1109. {
  1110. size_t plen = strlen (p->name);
  1111. if (buffer_size <= plen)
  1112. {
  1113. while ((buffer_size *= 2) <= plen)
  1114. continue;
  1115. buffer = xrealloc (buffer, buffer_size);
  1116. }
  1117. memcpy (buffer, p->name, plen);
  1118. if (! ISSLASH (buffer[plen - 1]))
  1119. buffer[plen++] = DIRECTORY_SEPARATOR;
  1120. q = directory_contents (gnu_list_name->directory);
  1121. if (q)
  1122. while (*q)
  1123. {
  1124. size_t qlen = strlen (q);
  1125. if (*q == 'Y')
  1126. {
  1127. if (buffer_size < plen + qlen)
  1128. {
  1129. while ((buffer_size *=2 ) < plen + qlen)
  1130. continue;
  1131. buffer = xrealloc (buffer, buffer_size);
  1132. }
  1133. strcpy (buffer + plen, q + 1);
  1134. dump_file (buffer, false, (dev_t) 0);
  1135. }
  1136. q += qlen + 1;
  1137. }
  1138. }
  1139. free (buffer);
  1140. }
  1141. else
  1142. {
  1143. const char *name;
  1144. while ((name = name_next (1)) != NULL)
  1145. if (!excluded_name (name))
  1146. dump_file (name, true, (dev_t) 0);
  1147. }
  1148. write_eot ();
  1149. close_archive ();
  1150. finish_deferred_unlinks ();
  1151. if (listed_incremental_option)
  1152. write_directory_file ();
  1153. }
  1154. /* Calculate the hash of a link. */
  1155. static size_t
  1156. hash_link (void const *entry, size_t n_buckets)
  1157. {
  1158. struct link const *l = entry;
  1159. uintmax_t num = l->dev ^ l->ino;
  1160. return num % n_buckets;
  1161. }
  1162. /* Compare two links for equality. */
  1163. static bool
  1164. compare_links (void const *entry1, void const *entry2)
  1165. {
  1166. struct link const *link1 = entry1;
  1167. struct link const *link2 = entry2;
  1168. return ((link1->dev ^ link2->dev) | (link1->ino ^ link2->ino)) == 0;
  1169. }
  1170. static void
  1171. unknown_file_error (char const *p)
  1172. {
  1173. WARNOPT (WARN_FILE_IGNORED,
  1174. (0, 0, _("%s: Unknown file type; file ignored"),
  1175. quotearg_colon (p)));
  1176. if (!ignore_failed_read_option)
  1177. set_exit_status (TAREXIT_FAILURE);
  1178. }
  1179. /* Handling of hard links */
  1180. /* Table of all non-directories that we've written so far. Any time
  1181. we see another, we check the table and avoid dumping the data
  1182. again if we've done it once already. */
  1183. static Hash_table *link_table;
  1184. /* Try to dump stat as a hard link to another file in the archive.
  1185. Return true if successful. */
  1186. static bool
  1187. dump_hard_link (struct tar_stat_info *st)
  1188. {
  1189. if (link_table && (st->stat.st_nlink > 1 || remove_files_option))
  1190. {
  1191. struct link lp;
  1192. struct link *duplicate;
  1193. off_t block_ordinal;
  1194. union block *blk;
  1195. lp.ino = st->stat.st_ino;
  1196. lp.dev = st->stat.st_dev;
  1197. if ((duplicate = hash_lookup (link_table, &lp)))
  1198. {
  1199. /* We found a link. */
  1200. char const *link_name = safer_name_suffix (duplicate->name, true,
  1201. absolute_names_option);
  1202. duplicate->nlink--;
  1203. block_ordinal = current_block_ordinal ();
  1204. assign_string (&st->link_name, link_name);
  1205. if (NAME_FIELD_SIZE - (archive_format == OLDGNU_FORMAT)
  1206. < strlen (link_name))
  1207. write_long_link (st);
  1208. st->stat.st_size = 0;
  1209. blk = start_header (st);
  1210. if (!blk)
  1211. return false;
  1212. tar_copy_str (blk->header.linkname, link_name, NAME_FIELD_SIZE);
  1213. blk->header.typeflag = LNKTYPE;
  1214. finish_header (st, blk, block_ordinal);
  1215. if (remove_files_option)
  1216. queue_deferred_unlink (st->orig_file_name, false);
  1217. return true;
  1218. }
  1219. }
  1220. return false;
  1221. }
  1222. static void
  1223. file_count_links (struct tar_stat_info *st)
  1224. {
  1225. if (hard_dereference_option)
  1226. return;
  1227. if (st->stat.st_nlink > 1)
  1228. {
  1229. struct link *duplicate;
  1230. char *linkname = NULL;
  1231. struct link *lp;
  1232. assign_string (&linkname, st->orig_file_name);
  1233. transform_name (&linkname, XFORM_LINK);
  1234. lp = xmalloc (offsetof (struct link, name)
  1235. + strlen (linkname) + 1);
  1236. lp->ino = st->stat.st_ino;
  1237. lp->dev = st->stat.st_dev;
  1238. lp->nlink = st->stat.st_nlink;
  1239. strcpy (lp->name, linkname);
  1240. free (linkname);
  1241. if (! ((link_table
  1242. || (link_table = hash_initialize (0, 0, hash_link,
  1243. compare_links, 0)))
  1244. && (duplicate = hash_insert (link_table, lp))))
  1245. xalloc_die ();
  1246. if (duplicate != lp)
  1247. abort ();
  1248. lp->nlink--;
  1249. }
  1250. }
  1251. /* For each dumped file, check if all its links were dumped. Emit
  1252. warnings if it is not so. */
  1253. void
  1254. check_links (void)
  1255. {
  1256. struct link *lp;
  1257. if (!link_table)
  1258. return;
  1259. for (lp = hash_get_first (link_table); lp;
  1260. lp = hash_get_next (link_table, lp))
  1261. {
  1262. if (lp->nlink)
  1263. {
  1264. WARN ((0, 0, _("Missing links to %s."), quote (lp->name)));
  1265. }
  1266. }
  1267. }
  1268. /* Dump a single file, recursing on directories. P is the file name
  1269. to dump. TOP_LEVEL tells whether this is a top-level call; zero
  1270. means no, positive means yes, and negative means the top level
  1271. of an incremental dump. PARENT_DEVICE is the device of P's
  1272. parent directory; it is examined only if TOP_LEVEL is zero. */
  1273. /* FIXME: One should make sure that for *every* path leading to setting
  1274. exit_status to failure, a clear diagnostic has been issued. */
  1275. static void
  1276. dump_file0 (struct tar_stat_info *st, const char *p,
  1277. bool top_level, dev_t parent_device)
  1278. {
  1279. union block *header;
  1280. char type;
  1281. off_t original_size;
  1282. struct timespec original_ctime;
  1283. struct timespec restore_times[2];
  1284. off_t block_ordinal = -1;
  1285. bool is_dir;
  1286. if (interactive_option && !confirm ("add", p))
  1287. return;
  1288. assign_string (&st->orig_file_name, p);
  1289. assign_string (&st->file_name,
  1290. safer_name_suffix (p, false, absolute_names_option));
  1291. transform_name (&st->file_name, XFORM_REGFILE);
  1292. if (deref_stat (dereference_option, p, &st->stat) != 0)
  1293. {
  1294. file_removed_diag (p, top_level, stat_diag);
  1295. return;
  1296. }
  1297. st->archive_file_size = original_size = st->stat.st_size;
  1298. st->atime = restore_times[0] = get_stat_atime (&st->stat);
  1299. st->mtime = restore_times[1] = get_stat_mtime (&st->stat);
  1300. st->ctime = original_ctime = get_stat_ctime (&st->stat);
  1301. #ifdef S_ISHIDDEN
  1302. if (S_ISHIDDEN (st->stat.st_mode))
  1303. {
  1304. char *new = (char *) alloca (strlen (p) + 2);
  1305. if (new)
  1306. {
  1307. strcpy (new, p);
  1308. strcat (new, "@");
  1309. p = new;
  1310. }
  1311. }
  1312. #endif
  1313. /* See if we want only new files, and check if this one is too old to
  1314. put in the archive.
  1315. This check is omitted if incremental_option is set *and* the
  1316. requested file is not explicitely listed in the command line. */
  1317. if (!(incremental_option && !is_individual_file (p))
  1318. && !S_ISDIR (st->stat.st_mode)
  1319. && OLDER_TAR_STAT_TIME (*st, m)
  1320. && (!after_date_option || OLDER_TAR_STAT_TIME (*st, c)))
  1321. {
  1322. if (!incremental_option && verbose_option)
  1323. WARNOPT (WARN_FILE_UNCHANGED,
  1324. (0, 0, _("%s: file is unchanged; not dumped"),
  1325. quotearg_colon (p)));
  1326. return;
  1327. }
  1328. /* See if we are trying to dump the archive. */
  1329. if (sys_file_is_archive (st))
  1330. {
  1331. WARNOPT (WARN_IGNORE_ARCHIVE,
  1332. (0, 0, _("%s: file is the archive; not dumped"),
  1333. quotearg_colon (p)));
  1334. return;
  1335. }
  1336. is_dir = S_ISDIR (st->stat.st_mode) != 0;
  1337. if (!is_dir && dump_hard_link (st))
  1338. return;
  1339. if (is_dir || S_ISREG (st->stat.st_mode) || S_ISCTG (st->stat.st_mode))
  1340. {
  1341. bool ok;
  1342. int fd = -1;
  1343. struct stat final_stat;
  1344. if (is_dir || file_dumpable_p (st))
  1345. {
  1346. fd = open (p,
  1347. (O_RDONLY | O_BINARY
  1348. | (is_dir ? O_DIRECTORY | O_NONBLOCK : 0)
  1349. | (atime_preserve_option == system_atime_preserve
  1350. ? O_NOATIME
  1351. : 0)));
  1352. if (fd < 0)
  1353. {
  1354. file_removed_diag (p, top_level, open_diag);
  1355. return;
  1356. }
  1357. }
  1358. if (is_dir)
  1359. {
  1360. const char *tag_file_name;
  1361. ensure_slash (&st->orig_file_name);
  1362. ensure_slash (&st->file_name);
  1363. if (check_exclusion_tags (st->orig_file_name, &tag_file_name)
  1364. == exclusion_tag_all)
  1365. {
  1366. exclusion_tag_warning (st->orig_file_name, tag_file_name,
  1367. _("directory not dumped"));
  1368. if (fd >= 0)
  1369. close (fd);
  1370. return;
  1371. }
  1372. ok = dump_dir (fd, st, top_level, parent_device);
  1373. /* dump_dir consumes FD if successful. */
  1374. if (ok)
  1375. fd = -1;
  1376. }
  1377. else
  1378. {
  1379. enum dump_status status;
  1380. if (fd != -1 && sparse_option && ST_IS_SPARSE (st->stat))
  1381. {
  1382. status = sparse_dump_file (fd, st);
  1383. if (status == dump_status_not_implemented)
  1384. status = dump_regular_file (fd, st);
  1385. }
  1386. else
  1387. status = dump_regular_file (fd, st);
  1388. switch (status)
  1389. {
  1390. case dump_status_ok:
  1391. case dump_status_short:
  1392. mv_end ();
  1393. file_count_links (st);
  1394. break;
  1395. case dump_status_fail:
  1396. break;
  1397. case dump_status_not_implemented:
  1398. abort ();
  1399. }
  1400. ok = status == dump_status_ok;
  1401. }
  1402. if (ok)
  1403. {
  1404. /* If possible, reopen a directory if we are preserving
  1405. atimes, so that we can set just the atime on systems with
  1406. _FIOSATIME. */
  1407. if (fd < 0 && is_dir
  1408. && atime_preserve_option == replace_atime_preserve)
  1409. fd = open (p, O_RDONLY | O_BINARY | O_DIRECTORY | O_NONBLOCK);
  1410. if ((fd < 0
  1411. ? deref_stat (dereference_option, p, &final_stat)
  1412. : fstat (fd, &final_stat))
  1413. != 0)
  1414. {
  1415. file_removed_diag (p, top_level, stat_diag);
  1416. ok = false;
  1417. }
  1418. }
  1419. if (ok)
  1420. {
  1421. if ((timespec_cmp (get_stat_ctime (&final_stat), original_ctime) != 0
  1422. /* Original ctime will change if the file is a directory and
  1423. --remove-files is given */
  1424. && !(remove_files_option && is_dir))
  1425. || original_size < final_stat.st_size)
  1426. {
  1427. WARNOPT (WARN_FILE_CHANGED,
  1428. (0, 0, _("%s: file changed as we read it"),
  1429. quotearg_colon (p)));
  1430. set_exit_status (TAREXIT_DIFFERS);
  1431. }
  1432. else if (atime_preserve_option == replace_atime_preserve
  1433. && set_file_atime (fd, p, restore_times) != 0)
  1434. utime_error (p);
  1435. }
  1436. if (0 <= fd && close (fd) != 0)
  1437. {
  1438. close_diag (p);
  1439. ok = false;
  1440. }
  1441. if (ok && remove_files_option)
  1442. queue_deferred_unlink (p, is_dir);
  1443. return;
  1444. }
  1445. #ifdef HAVE_READLINK
  1446. else if (S_ISLNK (st->stat.st_mode))
  1447. {
  1448. char *buffer;
  1449. int size;
  1450. size_t linklen = st->stat.st_size;
  1451. if (linklen != st->stat.st_size || linklen + 1 == 0)
  1452. xalloc_die ();
  1453. buffer = (char *) alloca (linklen + 1);
  1454. size = readlink (p, buffer, linklen + 1);
  1455. if (size < 0)
  1456. {
  1457. file_removed_diag (p, top_level, readlink_diag);
  1458. return;
  1459. }
  1460. buffer[size] = '\0';
  1461. assign_string (&st->link_name, buffer);
  1462. transform_name (&st->link_name, XFORM_SYMLINK);
  1463. if (NAME_FIELD_SIZE - (archive_format == OLDGNU_FORMAT) < size)
  1464. write_long_link (st);
  1465. block_ordinal = current_block_ordinal ();
  1466. st->stat.st_size = 0; /* force 0 size on symlink */
  1467. header = start_header (st);
  1468. if (!header)
  1469. return;
  1470. tar_copy_str (header->header.linkname, st->link_name, NAME_FIELD_SIZE);
  1471. header->header.typeflag = SYMTYPE;
  1472. finish_header (st, header, block_ordinal);
  1473. /* nothing more to do to it */
  1474. if (remove_files_option)
  1475. queue_deferred_unlink (p, false);
  1476. file_count_links (st);
  1477. return;
  1478. }
  1479. #endif
  1480. else if (S_ISCHR (st->stat.st_mode))
  1481. type = CHRTYPE;
  1482. else if (S_ISBLK (st->stat.st_mode))
  1483. type = BLKTYPE;
  1484. else if (S_ISFIFO (st->stat.st_mode))
  1485. type = FIFOTYPE;
  1486. else if (S_ISSOCK (st->stat.st_mode))
  1487. {
  1488. WARNOPT (WARN_FILE_IGNORED,
  1489. (0, 0, _("%s: socket ignored"), quotearg_colon (p)));
  1490. return;
  1491. }
  1492. else if (S_ISDOOR (st->stat.st_mode))
  1493. {
  1494. WARNOPT (WARN_FILE_IGNORED,
  1495. (0, 0, _("%s: door ignored"), quotearg_colon (p)));
  1496. return;
  1497. }
  1498. else
  1499. {
  1500. unknown_file_error (p);
  1501. return;
  1502. }
  1503. if (archive_format == V7_FORMAT)
  1504. {
  1505. unknown_file_error (p);
  1506. return;
  1507. }
  1508. block_ordinal = current_block_ordinal ();
  1509. st->stat.st_size = 0; /* force 0 size */
  1510. header = start_header (st);
  1511. if (!header)
  1512. return;
  1513. header->header.typeflag = type;
  1514. if (type != FIFOTYPE)
  1515. {
  1516. MAJOR_TO_CHARS (major (st->stat.st_rdev),
  1517. header->header.devmajor);
  1518. MINOR_TO_CHARS (minor (st->stat.st_rdev),
  1519. header->header.devminor);
  1520. }
  1521. finish_header (st, header, block_ordinal);
  1522. if (remove_files_option)
  1523. queue_deferred_unlink (p, false);
  1524. }
  1525. void
  1526. dump_file (const char *p, bool top_level, dev_t parent_device)
  1527. {
  1528. struct tar_stat_info st;
  1529. tar_stat_init (&st);
  1530. dump_file0 (&st, p, top_level, parent_device);
  1531. if (listed_incremental_option)
  1532. update_parent_directory (p);
  1533. tar_stat_destroy (&st);
  1534. }