create.c 46 KB

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