tar.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737
  1. /* A tar (tape archiver) program.
  2. Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1999, 2000,
  3. 2001, 2003, 2004 Free Software Foundation, Inc.
  4. Written by John Gilmore, starting 1985-08-25.
  5. This program is free software; you can redistribute it and/or modify it
  6. under the terms of the GNU General Public License as published by the
  7. Free Software Foundation; either version 2, or (at your option) any later
  8. version.
  9. This program is distributed in the hope that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
  12. Public License for more details.
  13. You should have received a copy of the GNU General Public License along
  14. with this program; if not, write to the Free Software Foundation, Inc.,
  15. 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  16. #include <system.h>
  17. #include <fnmatch.h>
  18. #include <argp.h>
  19. #include <signal.h>
  20. #if ! defined SIGCHLD && defined SIGCLD
  21. # define SIGCHLD SIGCLD
  22. #endif
  23. /* The following causes "common.h" to produce definitions of all the global
  24. variables, rather than just "extern" declarations of them. GNU tar does
  25. depend on the system loader to preset all GLOBAL variables to neutral (or
  26. zero) values; explicit initialization is usually not done. */
  27. #define GLOBAL
  28. #include "common.h"
  29. #include <getdate.h>
  30. #include <localedir.h>
  31. #include <rmt.h>
  32. #include <prepargs.h>
  33. #include <quotearg.h>
  34. #include <xstrtol.h>
  35. /* Local declarations. */
  36. #ifndef DEFAULT_ARCHIVE_FORMAT
  37. # define DEFAULT_ARCHIVE_FORMAT GNU_FORMAT
  38. #endif
  39. #ifndef DEFAULT_ARCHIVE
  40. # define DEFAULT_ARCHIVE "tar.out"
  41. #endif
  42. #ifndef DEFAULT_BLOCKING
  43. # define DEFAULT_BLOCKING 20
  44. #endif
  45. /* Miscellaneous. */
  46. /* Name of option using stdin. */
  47. static const char *stdin_used_by;
  48. /* Doesn't return if stdin already requested. */
  49. void
  50. request_stdin (const char *option)
  51. {
  52. if (stdin_used_by)
  53. USAGE_ERROR ((0, 0, _("Options `-%s' and `-%s' both want standard input"),
  54. stdin_used_by, option));
  55. stdin_used_by = option;
  56. }
  57. /* Returns true if and only if the user typed 'y' or 'Y'. */
  58. int
  59. confirm (const char *message_action, const char *message_name)
  60. {
  61. static FILE *confirm_file;
  62. static int confirm_file_EOF;
  63. if (!confirm_file)
  64. {
  65. if (archive == 0 || stdin_used_by)
  66. {
  67. confirm_file = fopen (TTY_NAME, "r");
  68. if (! confirm_file)
  69. open_fatal (TTY_NAME);
  70. }
  71. else
  72. {
  73. request_stdin ("-w");
  74. confirm_file = stdin;
  75. }
  76. }
  77. fprintf (stdlis, "%s %s?", message_action, quote (message_name));
  78. fflush (stdlis);
  79. {
  80. int reply = confirm_file_EOF ? EOF : getc (confirm_file);
  81. int character;
  82. for (character = reply;
  83. character != '\n';
  84. character = getc (confirm_file))
  85. if (character == EOF)
  86. {
  87. confirm_file_EOF = 1;
  88. fputc ('\n', stdlis);
  89. fflush (stdlis);
  90. break;
  91. }
  92. return reply == 'y' || reply == 'Y';
  93. }
  94. }
  95. static struct fmttab {
  96. char const *name;
  97. enum archive_format fmt;
  98. } const fmttab[] = {
  99. { "v7", V7_FORMAT },
  100. { "oldgnu", OLDGNU_FORMAT },
  101. { "ustar", USTAR_FORMAT },
  102. { "posix", POSIX_FORMAT },
  103. #if 0 /* not fully supported yet */
  104. { "star", STAR_FORMAT },
  105. #endif
  106. { "gnu", GNU_FORMAT },
  107. { "pax", POSIX_FORMAT }, /* An alias for posix */
  108. { NULL, 0 }
  109. };
  110. static void
  111. set_archive_format (char const *name)
  112. {
  113. struct fmttab const *p;
  114. for (p = fmttab; strcmp (p->name, name) != 0; )
  115. if (! (++p)->name)
  116. USAGE_ERROR ((0, 0, _("%s: Invalid archive format"),
  117. quotearg_colon (name)));
  118. archive_format = p->fmt;
  119. }
  120. static const char *
  121. archive_format_string (enum archive_format fmt)
  122. {
  123. struct fmttab const *p;
  124. for (p = fmttab; p->name; p++)
  125. if (p->fmt == fmt)
  126. return p->name;
  127. return "unknown?";
  128. }
  129. #define FORMAT_MASK(n) (1<<(n))
  130. static void
  131. assert_format(unsigned fmt_mask)
  132. {
  133. if ((FORMAT_MASK(archive_format) & fmt_mask) == 0)
  134. USAGE_ERROR ((0, 0,
  135. _("GNU features wanted on incompatible archive format")));
  136. }
  137. /* Options. */
  138. /* For long options that unconditionally set a single flag, we have getopt
  139. do it. For the others, we share the code for the equivalent short
  140. named option, the name of which is stored in the otherwise-unused `val'
  141. field of the `struct option'; for long options that have no equivalent
  142. short option, we use non-characters as pseudo short options,
  143. starting at CHAR_MAX + 1 and going upwards. */
  144. enum
  145. {
  146. ANCHORED_OPTION = CHAR_MAX + 1,
  147. ATIME_PRESERVE_OPTION,
  148. BACKUP_OPTION,
  149. CHECKPOINT_OPTION,
  150. CHECK_LINKS_OPTION,
  151. DELETE_OPTION,
  152. EXCLUDE_OPTION,
  153. EXCLUDE_CACHES_OPTION,
  154. FORCE_LOCAL_OPTION,
  155. GROUP_OPTION,
  156. IGNORE_CASE_OPTION,
  157. IGNORE_FAILED_READ_OPTION,
  158. INDEX_FILE_OPTION,
  159. KEEP_NEWER_FILES_OPTION,
  160. LICENSE_OPTION,
  161. MODE_OPTION,
  162. NEWER_MTIME_OPTION,
  163. NO_ANCHORED_OPTION,
  164. NO_IGNORE_CASE_OPTION,
  165. NO_OVERWRITE_DIR_OPTION,
  166. NO_RECURSION_OPTION,
  167. NO_SAME_OWNER_OPTION,
  168. NO_SAME_PERMISSIONS_OPTION,
  169. NO_WILDCARDS_OPTION,
  170. NO_WILDCARDS_MATCH_SLASH_OPTION,
  171. NULL_OPTION,
  172. NUMERIC_OWNER_OPTION,
  173. OCCURRENCE_OPTION,
  174. OLD_ARCHIVE_OPTION,
  175. ONE_FILE_SYSTEM_OPTION,
  176. OVERWRITE_OPTION,
  177. OWNER_OPTION,
  178. PAX_OPTION,
  179. POSIX_OPTION,
  180. PRESERVE_OPTION,
  181. RECORD_SIZE_OPTION,
  182. RECURSION_OPTION,
  183. RECURSIVE_UNLINK_OPTION,
  184. REMOVE_FILES_OPTION,
  185. RMT_COMMAND_OPTION,
  186. RSH_COMMAND_OPTION,
  187. SAME_OWNER_OPTION,
  188. SHOW_DEFAULTS_OPTION,
  189. SHOW_OMITTED_DIRS_OPTION,
  190. STRIP_COMPONENTS_OPTION,
  191. SUFFIX_OPTION,
  192. TOTALS_OPTION,
  193. USAGE_OPTION,
  194. USE_COMPRESS_PROGRAM_OPTION,
  195. UTC_OPTION,
  196. VERSION_OPTION,
  197. VOLNO_FILE_OPTION,
  198. WILDCARDS_OPTION,
  199. WILDCARDS_MATCH_SLASH_OPTION
  200. };
  201. const char *argp_program_version = "tar (" PACKAGE_NAME ") " VERSION;
  202. const char *argp_program_bug_address = "<" PACKAGE_BUGREPORT ">";
  203. static char doc[] = N_("GNU `tar' saves many files together into a single tape or disk archive, and can restore individual files from the archive.\n\
  204. \n\
  205. Examples:\n\
  206. tar -cf archive.tar foo bar # Create archive.tar from files foo and bar.\n\
  207. tar -tvf archive.tar # List all files in archive.tar verbosely.\n\
  208. tar -xf archive.tar # Extract all files from archive.tar.\n\
  209. \vThe backup suffix is `~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.\n\
  210. The version control may be set with --backup or VERSION_CONTROL, values are:\n\n\
  211. t, numbered make numbered backups\n\
  212. nil, existing numbered if numbered backups exist, simple otherwise\n\
  213. never, simple always make simple backups\n");
  214. /* NOTE:
  215. Available option letters are DEIJQY and aeqy. Consider the following
  216. assignments:
  217. [For Solaris tar compatibility]
  218. e exit immediately with a nonzero exit status if unexpected errors occur
  219. E use extended headers (--format=posix)
  220. [q alias for --occurrence=1 =/= this would better be used for quiet?]
  221. [I same as T =/= will harm star compatibility]
  222. y per-file gzip compression
  223. Y per-block gzip compression */
  224. static struct argp_option options[] = {
  225. {NULL, 0, NULL, 0,
  226. N_("Main operation mode:"), 0},
  227. {"list", 't', 0, 0,
  228. N_("list the contents of an archive"), 10 },
  229. {"extract", 'x', 0, 0,
  230. N_("extract files from an archive"), 10 },
  231. {"get", 0, 0, OPTION_ALIAS, NULL, 0 },
  232. {"create", 'c', 0, 0,
  233. N_("create a new archive"), 10 },
  234. {"diff", 'd', 0, 0,
  235. N_("find differences between archive and file system"), 10 },
  236. {"compare", 0, 0, OPTION_ALIAS, NULL, 10},
  237. {"append", 'r', 0, 0,
  238. N_("append files to the end of an archive"), 10 },
  239. {"update", 'u', 0, 0,
  240. N_("only append files newer than copy in archive"), 10 },
  241. {"catenate", 'A', 0, 0,
  242. N_("append tar files to an archive"), 10 },
  243. {"concatenate", 0, 0, OPTION_ALIAS, NULL, 10},
  244. {"delete", DELETE_OPTION, 0, 0,
  245. N_("delete from the archive (not on mag tapes!)"), 10 },
  246. {NULL, 0, NULL, 0,
  247. N_("Operation modifiers:"), 20},
  248. {"verify", 'W', 0, 0,
  249. N_("attempt to verify the archive after writing it"), 21 },
  250. {"remove-files", REMOVE_FILES_OPTION, 0, 0,
  251. N_("remove files after adding them to the archive"), 21 },
  252. {"keep-old-files", 'k', 0, 0,
  253. N_("don't replace existing files when extracting"), 21 },
  254. {"keep-newer-files", KEEP_NEWER_FILES_OPTION, 0, 0,
  255. N_("don't replace existing files that are newer than their archive copies"), 21 },
  256. {"no-overwrite-dir", NO_OVERWRITE_DIR_OPTION, 0, 0,
  257. N_("preserve metadata of existing directories"), 21 },
  258. {"overwrite", OVERWRITE_OPTION, 0, 0,
  259. N_("overwrite existing files when extracting"), 21 },
  260. {"unlink-first", 'U', 0, 0,
  261. N_("remove each file prior to extracting over it"), 21 },
  262. {"recursive-unlink", RECURSIVE_UNLINK_OPTION, 0, 0,
  263. N_("empty hierarchies prior to extracting directory"), 21 },
  264. {"sparse", 'S', 0, 0,
  265. N_("handle sparse files efficiently"), 21 },
  266. {"to-stdout", 'O', 0, 0,
  267. N_("extract files to standard output"), 21 },
  268. {"incremental", 'G', 0, 0,
  269. N_("handle old GNU-format incremental backup"), 21 },
  270. {"listed-incremental", 'g', N_("FILE"), 0,
  271. N_("handle new GNU-format incremental backup"), 21 },
  272. {"ignore-failed-read", IGNORE_FAILED_READ_OPTION, 0, 0,
  273. N_("do not exit with nonzero on unreadable files"), 21 },
  274. {"occurrence", OCCURRENCE_OPTION, N_("NUMBER"), OPTION_ARG_OPTIONAL,
  275. N_("process only the NUMth occurrence of each file in the archive. This option is valid only in conjunction with one of the subcommands --delete, --diff, --extract or --list and when a list of files is given either on the command line or via -T option. NUMBER defaults to 1."), 21 },
  276. {"seek", 'n', NULL, 0,
  277. N_("Archive is seekable"), 21 },
  278. {NULL, 0, NULL, 0,
  279. N_("Handling of file attributes:"), 30 },
  280. {"owner", OWNER_OPTION, N_("NAME"), 0,
  281. N_("force NAME as owner for added files"), 31 },
  282. {"group", GROUP_OPTION, N_("NAME"), 0,
  283. N_("force NAME as group for added files"), 31 },
  284. {"mode", MODE_OPTION, N_("CHANGES"), 0,
  285. N_("force (symbolic) mode CHANGES for added files"), 31 },
  286. {"atime-preserve", ATIME_PRESERVE_OPTION, 0, 0,
  287. N_("don't change access times on dumped files"), 31 },
  288. {"touch", 'm', 0, 0,
  289. N_("don't extract file modified time"), 31 },
  290. {"same-owner", SAME_OWNER_OPTION, 0, 0,
  291. N_("try extracting files with the same ownership"), 31 },
  292. {"no-same-owner", NO_SAME_OWNER_OPTION, 0, 0,
  293. N_("extract files as yourself"), 31 },
  294. {"numeric-owner", NUMERIC_OWNER_OPTION, 0, 0,
  295. N_("always use numbers for user/group names"), 31 },
  296. {"preserve-permissions", 'p', 0, 0,
  297. N_("extract permissions information"), 31 },
  298. {"same-permissions", 0, 0, OPTION_ALIAS, NULL, 31 },
  299. {"no-same-permissions", NO_SAME_PERMISSIONS_OPTION, 0, 0,
  300. N_("do not extract permissions information"), 31 },
  301. {"preserve-order", 's', 0, 0,
  302. N_("sort names to extract to match archive"), 31 },
  303. {"same-order", 0, 0, OPTION_ALIAS, NULL, 31 },
  304. {"preserve", PRESERVE_OPTION, 0, 0,
  305. N_("same as both -p and -s"), 31 },
  306. {NULL, 0, NULL, 0,
  307. N_("Device selection and switching:"), 40 },
  308. {"file", 'f', N_("ARCHIVE"), 0,
  309. N_("use archive file or device ARCHIVE"), 41 },
  310. {"force-local", FORCE_LOCAL_OPTION, 0, 0,
  311. N_("archive file is local even if has a colon"), 41 },
  312. {"rmt-command", RMT_COMMAND_OPTION, N_("COMMAND"), 0,
  313. N_("use given rmt COMMAND instead of rmt"), 41 },
  314. {"rsh-command", RSH_COMMAND_OPTION, N_("COMMAND"), 0,
  315. N_("use remote COMMAND instead of rsh"), 41 },
  316. #ifdef DEVICE_PREFIX
  317. {"-[0-7][lmh]", 0, NULL, OPTION_DOC, /* It is OK, since `name' will never be
  318. translated */
  319. N_("specify drive and density"), 41 },
  320. #endif
  321. {NULL, '0', NULL, OPTION_HIDDEN, NULL, 41 },
  322. {NULL, '1', NULL, OPTION_HIDDEN, NULL, 41 },
  323. {NULL, '2', NULL, OPTION_HIDDEN, NULL, 41 },
  324. {NULL, '3', NULL, OPTION_HIDDEN, NULL, 41 },
  325. {NULL, '4', NULL, OPTION_HIDDEN, NULL, 41 },
  326. {NULL, '5', NULL, OPTION_HIDDEN, NULL, 41 },
  327. {NULL, '6', NULL, OPTION_HIDDEN, NULL, 41 },
  328. {NULL, '7', NULL, OPTION_HIDDEN, NULL, 41 },
  329. {NULL, '8', NULL, OPTION_HIDDEN, NULL, 41 },
  330. {NULL, '9', NULL, OPTION_HIDDEN, NULL, 41 },
  331. {"multi-volume", 'M', 0, 0,
  332. N_("create/list/extract multi-volume archive"), 41 },
  333. {"tape-length", 'L', N_("NUMBER"), 0,
  334. N_("change tape after writing NUMBER x 1024 bytes"), 41 },
  335. {"info-script", 'F', N_("NAME"), 0,
  336. N_("run script at end of each tape (implies -M)"), 41 },
  337. {"new-volume-script", 0, 0, OPTION_ALIAS, NULL, 41 },
  338. {"volno-file", VOLNO_FILE_OPTION, N_("FILE"), 0,
  339. N_("use/update the volume number in FILE"), 41 },
  340. {NULL, 0, NULL, 0,
  341. N_("Device blocking:"), 50 },
  342. {"blocking-factor", 'b', N_("BLOCKS"), 0,
  343. N_("BLOCKS x 512 bytes per record"), 51 },
  344. {"record-size", RECORD_SIZE_OPTION, N_("NUMBER"), 0,
  345. N_("SIZE bytes per record, multiple of 512"), 51 },
  346. {"ignore-zeros", 'i', 0, 0,
  347. N_("ignore zeroed blocks in archive (means EOF)"), 51 },
  348. {"read-full-records", 'B', 0, 0,
  349. N_("reblock as we read (for 4.2BSD pipes)"), 51 },
  350. {NULL, 0, NULL, 0,
  351. N_("Archive format selection:"), 60 },
  352. {"format", 'H', N_("FORMAT"), 0,
  353. N_("create archive of the given format."), 61 },
  354. {NULL, 0, NULL, 0, N_("FORMAT is one of the following:"), 62 },
  355. {" v7", 0, NULL, OPTION_DOC|OPTION_NO_TRANS, N_("old V7 tar format"), 63},
  356. {" oldgnu", 0, NULL, OPTION_DOC|OPTION_NO_TRANS,
  357. N_("GNU format as per tar <= 1.12"), 63},
  358. {" gnu", 0, NULL, OPTION_DOC|OPTION_NO_TRANS,
  359. N_("GNU tar 1.13.x format"), 63},
  360. {" ustar", 0, NULL, OPTION_DOC|OPTION_NO_TRANS,
  361. N_("POSIX 1003.1-1988 (ustar) format"), 63 },
  362. {" pax", 0, NULL, OPTION_DOC|OPTION_NO_TRANS,
  363. N_("POSIX 1003.1-2001 (pax) format"), 63 },
  364. {" posix", 0, NULL, OPTION_DOC|OPTION_NO_TRANS, N_("Same as pax"), 63 },
  365. {"old-archive", OLD_ARCHIVE_OPTION, 0, 0, /* FIXME */
  366. N_("same as --format=v7"), 68 },
  367. {"portability", 0, 0, OPTION_ALIAS, NULL, 68 },
  368. {"posix", POSIX_OPTION, 0, 0,
  369. N_("same as --format=posix"), 68 },
  370. {"pax-option", PAX_OPTION, N_("keyword[[:]=value][,keyword[[:]=value], ...]"), 0,
  371. N_("control pax keywords"), 68 },
  372. {"label", 'V', N_("TEXT"), 0,
  373. N_("create archive with volume name NAME. At list/extract time, use TEXT as a globbing pattern"), 68 },
  374. {"bzip2", 'j', 0, 0,
  375. N_("filter the archive through bzip2"), 68 },
  376. {"gzip", 'z', 0, 0,
  377. N_("filter the archive through gzip"), 68 },
  378. {"gunzip", 0, 0, OPTION_ALIAS, NULL, 68 },
  379. {"ungzip", 0, 0, OPTION_ALIAS, NULL, 68 },
  380. {"compress", 'Z', 0, 0,
  381. N_("filter the archive through compress"), 68 },
  382. {"uncompress", 0, 0, OPTION_ALIAS, NULL, 68 },
  383. {"use-compress-program", USE_COMPRESS_PROGRAM_OPTION, N_("PROG"), 0,
  384. N_("filter through PROG (must accept -d)"), 68 },
  385. {NULL, 0, NULL, 0,
  386. N_("Local file selection:"), 70 },
  387. {"directory", 'C', N_("DIR"), 0,
  388. N_("change to directory DIR"), 71 },
  389. {"files-from", 'T', N_("FILE-OF-NAMES"), 0,
  390. N_("get names to extract or create from file NAME"), 71 },
  391. {"null", NULL_OPTION, 0, 0,
  392. N_("-T reads null-terminated names, disable -C"), 71 },
  393. {"exclude", EXCLUDE_OPTION, N_("PATTERN"), 0,
  394. N_("exclude files, given as a PATTERN"), 71 },
  395. {"exclude-from", 'X', N_("FILE"), 0,
  396. N_("exclude patterns listed in FILE"), 71 },
  397. {"exclude-caches", EXCLUDE_CACHES_OPTION, 0, 0,
  398. N_("exclude directories containing a cache tag"), 71 },
  399. {"ignore-case", IGNORE_CASE_OPTION, 0, 0,
  400. N_("exclusion ignores case"), 71 },
  401. {"anchored", ANCHORED_OPTION, 0, 0,
  402. N_("exclude patterns match file name start"), 71 },
  403. {"no-anchored", NO_ANCHORED_OPTION, 0, 0,
  404. N_("exclude patterns match after any / (default)"), 71 },
  405. {"no-ignore-case", NO_IGNORE_CASE_OPTION, 0, 0,
  406. N_("exclusion is case sensitive (default)"), 71 },
  407. {"no-wildcards", NO_WILDCARDS_OPTION, 0, 0,
  408. N_("exclude patterns are plain strings"), 71 },
  409. {"no-wildcards-match-slash", NO_WILDCARDS_MATCH_SLASH_OPTION, 0, 0,
  410. N_("exclude pattern wildcards do not match '/'"), 71 },
  411. {"no-recursion", NO_RECURSION_OPTION, 0, 0,
  412. N_("avoid descending automatically in directories"), 71 },
  413. {"one-file-system", ONE_FILE_SYSTEM_OPTION, 0, 0,
  414. N_("stay in local file system when creating archive"), 71 },
  415. {NULL, 'l', 0, OPTION_HIDDEN, "", 71},
  416. {"recursion", RECURSION_OPTION, 0, 0,
  417. N_("recurse into directories (default)"), 71 },
  418. {"absolute-names", 'P', 0, 0,
  419. N_("don't strip leading `/'s from file names"), 71 },
  420. {"dereference", 'h', 0, 0,
  421. N_("dump instead the files symlinks point to"), 71 },
  422. {"starting-file", 'K', N_("MEMBER-NAME"), 0,
  423. N_("begin at member MEMBER-NAME in the archive"), 71 },
  424. {"strip-components", STRIP_COMPONENTS_OPTION, N_("NUMBER"), 0,
  425. N_("strip NUMBER leading components from file names"), 71 },
  426. {"newer", 'N', N_("DATE-OR-FILE"), 0,
  427. N_("only store files newer than DATE-OR-FILE"), 71 },
  428. {"newer-mtime", NEWER_MTIME_OPTION, N_("DATE"), 0,
  429. N_("compare date and time when data changed only"), 71 },
  430. {"after-date", 'N', N_("DATE"), 0,
  431. N_("same as -N"), 71 },
  432. {"backup", BACKUP_OPTION, N_("CONTROL"), OPTION_ARG_OPTIONAL,
  433. N_("backup before removal, choose version CONTROL"), 71 },
  434. {"suffix", SUFFIX_OPTION, N_("STRING"), 0,
  435. N_("backup before removal, override usual suffix ('~' unless overridden by environment variable SIMPLE_BACKUP_SUFFIX"), 71 },
  436. {"wildcards", WILDCARDS_OPTION, 0, 0,
  437. N_("exclude patterns use wildcards (default)"), 71 },
  438. {"wildcards-match-slash", WILDCARDS_MATCH_SLASH_OPTION, 0, 0,
  439. N_("exclude pattern wildcards match '/' (default)"), 71 },
  440. {NULL, 0, NULL, 0,
  441. N_("Informative output:"), 80 },
  442. {"verbose", 'v', 0, 0,
  443. N_("verbosely list files processed"), 81 },
  444. {"checkpoint", CHECKPOINT_OPTION, 0, 0,
  445. N_("display progress messages every 10th record"), 81 },
  446. {"check-links", CHECK_LINKS_OPTION, 0, 0,
  447. N_("print a message if not all links are dumped"), 82 },
  448. {"totals", TOTALS_OPTION, 0, 0,
  449. N_("print total bytes written while creating archive"), 82 },
  450. {"utc", UTC_OPTION, 0, 0,
  451. N_("print file modification dates in UTC"), 82 },
  452. {"index-file", INDEX_FILE_OPTION, N_("FILE"), 0,
  453. N_("send verbose output to FILE"), 82 },
  454. {"block-number", 'R', 0, 0,
  455. N_("show block number within archive with each message"), 82 },
  456. {"interactive", 'w', 0, 0,
  457. N_("ask for confirmation for every action"), 82 },
  458. {"confirmation", 0, 0, OPTION_ALIAS, NULL, 82 },
  459. {"show-defaults", SHOW_DEFAULTS_OPTION, 0, 0,
  460. N_("Show tar defaults"), 82 },
  461. {"show-omitted-dirs", SHOW_OMITTED_DIRS_OPTION, 0, 0,
  462. N_("When listing or extracting, list each directory that does not match search criteria"), 82 },
  463. {NULL, 0, NULL, 0,
  464. N_("Compatibility options:"), 90 },
  465. {NULL, 'o', 0, 0,
  466. N_("when creating, same as --old-archive. When extracting, same as --no-same-owner"), 91 },
  467. {NULL, 0, NULL, 0,
  468. N_("Other options:"), 100 },
  469. {"help", '?', 0, 0, N_("Give this help list"), -1},
  470. {"usage", USAGE_OPTION, 0, 0, N_("Give a short usage message"), -1},
  471. {"license", LICENSE_OPTION, 0, 0, N_("Print license and exit"), -1},
  472. {"version", VERSION_OPTION, 0, 0, N_("Print program version"), -1},
  473. /* FIXME -V (--label) conflicts with the default short option for
  474. --version */
  475. {0, 0, 0, 0, 0, 0}
  476. };
  477. struct tar_args {
  478. char const *textual_date_option;
  479. int exclude_options;
  480. bool o_option;
  481. int pax_option;
  482. char const *backup_suffix_string;
  483. char const *version_control_string;
  484. int input_files;
  485. };
  486. static void
  487. show_default_settings (FILE *stream)
  488. {
  489. fprintf (stream,
  490. "--format=%s -f%s -b%d --rmt-command=%s",
  491. archive_format_string (DEFAULT_ARCHIVE_FORMAT),
  492. DEFAULT_ARCHIVE, DEFAULT_BLOCKING,
  493. DEFAULT_RMT_COMMAND);
  494. #ifdef REMOTE_SHELL
  495. fprintf (stream, " --rsh-command=%s", REMOTE_SHELL);
  496. #endif
  497. fprintf (stream, "\n");
  498. }
  499. static void
  500. set_subcommand_option (enum subcommand subcommand)
  501. {
  502. if (subcommand_option != UNKNOWN_SUBCOMMAND
  503. && subcommand_option != subcommand)
  504. USAGE_ERROR ((0, 0,
  505. _("You may not specify more than one `-Acdtrux' option")));
  506. subcommand_option = subcommand;
  507. }
  508. static void
  509. set_use_compress_program_option (const char *string)
  510. {
  511. if (use_compress_program_option
  512. && strcmp (use_compress_program_option, string) != 0)
  513. USAGE_ERROR ((0, 0, _("Conflicting compression options")));
  514. use_compress_program_option = string;
  515. }
  516. void
  517. license ()
  518. {
  519. printf ("tar (%s) %s\n%s\n", PACKAGE_NAME, PACKAGE_VERSION,
  520. "Copyright (C) 2004 Free Software Foundation, Inc.\n");
  521. puts (_("Based on the work of John Gilmore and Jay Fenlason. See AUTHORS\n\
  522. for complete list of authors.\n"));
  523. printf (_(" GNU tar is free software; you can redistribute it and/or modify\n"
  524. " it under the terms of the GNU General Public License as published by\n"
  525. " the Free Software Foundation; either version 2 of the License, or\n"
  526. " (at your option) any later version.\n"
  527. "\n"
  528. " GNU tar is distributed in the hope that it will be useful,\n"
  529. " but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  530. " MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
  531. " GNU General Public License for more details.\n"
  532. "\n"
  533. " You should have received a copy of the GNU General Public License\n"
  534. " along with GNU tar; if not, write to the Free Software\n"
  535. " Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA\n\n"));
  536. exit (0);
  537. }
  538. static error_t
  539. parse_opt(int key, char *arg, struct argp_state *state)
  540. {
  541. struct tar_args *args = state->input;
  542. switch (key)
  543. {
  544. case 1:
  545. /* File name or non-parsed option, because of ARGP_IN_ORDER */
  546. name_add (optarg);
  547. args->input_files++;
  548. break;
  549. case 'A':
  550. set_subcommand_option (CAT_SUBCOMMAND);
  551. break;
  552. case 'b':
  553. {
  554. uintmax_t u;
  555. if (! (xstrtoumax (arg, 0, 10, &u, "") == LONGINT_OK
  556. && u == (blocking_factor = u)
  557. && 0 < blocking_factor
  558. && u == (record_size = u * BLOCKSIZE) / BLOCKSIZE))
  559. USAGE_ERROR ((0, 0, "%s: %s", quotearg_colon (arg),
  560. _("Invalid blocking factor")));
  561. }
  562. break;
  563. case 'B':
  564. /* Try to reblock input records. For reading 4.2BSD pipes. */
  565. /* It would surely make sense to exchange -B and -R, but it seems
  566. that -B has been used for a long while in Sun tar and most
  567. BSD-derived systems. This is a consequence of the block/record
  568. terminology confusion. */
  569. read_full_records_option = true;
  570. break;
  571. case 'c':
  572. set_subcommand_option (CREATE_SUBCOMMAND);
  573. break;
  574. case 'C':
  575. name_add ("-C");
  576. name_add (arg);
  577. break;
  578. case 'd':
  579. set_subcommand_option (DIFF_SUBCOMMAND);
  580. break;
  581. case 'f':
  582. if (archive_names == allocated_archive_names)
  583. {
  584. allocated_archive_names *= 2;
  585. archive_name_array =
  586. xrealloc (archive_name_array,
  587. sizeof (const char *) * allocated_archive_names);
  588. }
  589. archive_name_array[archive_names++] = arg;
  590. break;
  591. case 'F':
  592. /* Since -F is only useful with -M, make it implied. Run this
  593. script at the end of each tape. */
  594. info_script_option = arg;
  595. multi_volume_option = true;
  596. break;
  597. case 'g':
  598. listed_incremental_option = arg;
  599. after_date_option = true;
  600. /* Fall through. */
  601. case 'G':
  602. /* We are making an incremental dump (FIXME: are we?); save
  603. directories at the beginning of the archive, and include in each
  604. directory its contents. */
  605. incremental_option = true;
  606. break;
  607. case 'h':
  608. /* Follow symbolic links. */
  609. dereference_option = true;
  610. break;
  611. case 'i':
  612. /* Ignore zero blocks (eofs). This can't be the default,
  613. because Unix tar writes two blocks of zeros, then pads out
  614. the record with garbage. */
  615. ignore_zeros_option = true;
  616. break;
  617. case 'I':
  618. USAGE_ERROR ((0, 0,
  619. _("Warning: the -I option is not supported;"
  620. " perhaps you meant -j or -T?")));
  621. break;
  622. case 'j':
  623. set_use_compress_program_option ("bzip2");
  624. break;
  625. case 'k':
  626. /* Don't replace existing files. */
  627. old_files_option = KEEP_OLD_FILES;
  628. break;
  629. case 'K':
  630. starting_file_option = true;
  631. addname (arg, 0);
  632. break;
  633. case 'l':
  634. /* Historically equivalent to --one-file-system. This usage is
  635. incompatible with UNIX98 and POSIX specs and therefore is
  636. deprecated. The semantics of -l option will be changed in
  637. future versions. See TODO.
  638. */
  639. WARN ((0, 0,
  640. _("Semantics of -l option will change in the future releases.")));
  641. WARN ((0, 0,
  642. _("Please use --one-file-system option instead.")));
  643. /* FALL THROUGH */
  644. case ONE_FILE_SYSTEM_OPTION:
  645. /* When dumping directories, don't dump files/subdirectories
  646. that are on other filesystems. */
  647. one_file_system_option = true;
  648. break;
  649. case 'L':
  650. {
  651. uintmax_t u;
  652. if (xstrtoumax (arg, 0, 10, &u, "") != LONGINT_OK)
  653. USAGE_ERROR ((0, 0, "%s: %s", quotearg_colon (arg),
  654. _("Invalid tape length")));
  655. tape_length_option = 1024 * (tarlong) u;
  656. multi_volume_option = true;
  657. }
  658. break;
  659. case 'm':
  660. touch_option = true;
  661. break;
  662. case 'M':
  663. /* Make multivolume archive: when we can't write any more into
  664. the archive, re-open it, and continue writing. */
  665. multi_volume_option = true;
  666. break;
  667. case 'n':
  668. seekable_archive = true;
  669. break;
  670. #if !MSDOS
  671. case 'N':
  672. after_date_option = true;
  673. /* Fall through. */
  674. case NEWER_MTIME_OPTION:
  675. if (NEWER_OPTION_INITIALIZED (newer_mtime_option))
  676. USAGE_ERROR ((0, 0, _("More than one threshold date")));
  677. if (FILE_SYSTEM_PREFIX_LEN (arg) != 0
  678. || ISSLASH (*arg)
  679. || *arg == '.')
  680. {
  681. struct stat st;
  682. if (deref_stat (dereference_option, arg, &st) != 0)
  683. {
  684. stat_error (arg);
  685. USAGE_ERROR ((0, 0, _("Date file not found")));
  686. }
  687. newer_mtime_option.tv_sec = st.st_mtime;
  688. newer_mtime_option.tv_nsec = TIMESPEC_NS (st.st_mtim);
  689. }
  690. else
  691. {
  692. if (! get_date (&newer_mtime_option, arg, NULL))
  693. {
  694. WARN ((0, 0, _("Substituting %s for unknown date format %s"),
  695. tartime (newer_mtime_option.tv_sec), quote (arg)));
  696. newer_mtime_option.tv_nsec = 0;
  697. }
  698. else
  699. args->textual_date_option = arg;
  700. }
  701. break;
  702. #endif /* not MSDOS */
  703. case 'o':
  704. args->o_option = true;
  705. break;
  706. case 'O':
  707. to_stdout_option = true;
  708. break;
  709. case 'p':
  710. same_permissions_option = true;
  711. break;
  712. case 'P':
  713. absolute_names_option = true;
  714. break;
  715. case 'r':
  716. set_subcommand_option (APPEND_SUBCOMMAND);
  717. break;
  718. case 'R':
  719. /* Print block numbers for debugging bad tar archives. */
  720. /* It would surely make sense to exchange -B and -R, but it seems
  721. that -B has been used for a long while in Sun tar ans most
  722. BSD-derived systems. This is a consequence of the block/record
  723. terminology confusion. */
  724. block_number_option = true;
  725. break;
  726. case 's':
  727. /* Names to extr are sorted. */
  728. same_order_option = true;
  729. break;
  730. case 'S':
  731. sparse_option = true;
  732. break;
  733. case 't':
  734. set_subcommand_option (LIST_SUBCOMMAND);
  735. verbose_option++;
  736. break;
  737. case 'T':
  738. files_from_option = arg;
  739. break;
  740. case 'u':
  741. set_subcommand_option (UPDATE_SUBCOMMAND);
  742. break;
  743. case 'U':
  744. old_files_option = UNLINK_FIRST_OLD_FILES;
  745. break;
  746. case UTC_OPTION:
  747. utc_option = true;
  748. break;
  749. case 'v':
  750. verbose_option++;
  751. break;
  752. case 'V':
  753. volume_label_option = arg;
  754. break;
  755. case 'w':
  756. interactive_option = true;
  757. break;
  758. case 'W':
  759. verify_option = true;
  760. break;
  761. case 'x':
  762. set_subcommand_option (EXTRACT_SUBCOMMAND);
  763. break;
  764. case 'X':
  765. if (add_exclude_file (add_exclude, excluded, arg,
  766. args->exclude_options | recursion_option, '\n')
  767. != 0)
  768. {
  769. int e = errno;
  770. FATAL_ERROR ((0, e, "%s", quotearg_colon (arg)));
  771. }
  772. break;
  773. case 'y':
  774. USAGE_ERROR ((0, 0,
  775. _("Warning: the -y option is not supported;"
  776. " perhaps you meant -j?")));
  777. break;
  778. case 'z':
  779. set_use_compress_program_option ("gzip");
  780. break;
  781. case 'Z':
  782. set_use_compress_program_option ("compress");
  783. break;
  784. case ANCHORED_OPTION:
  785. args->exclude_options |= EXCLUDE_ANCHORED;
  786. break;
  787. case ATIME_PRESERVE_OPTION:
  788. atime_preserve_option = true;
  789. break;
  790. case CHECKPOINT_OPTION:
  791. checkpoint_option = true;
  792. break;
  793. case BACKUP_OPTION:
  794. backup_option = true;
  795. if (arg)
  796. args->version_control_string = arg;
  797. break;
  798. case DELETE_OPTION:
  799. set_subcommand_option (DELETE_SUBCOMMAND);
  800. break;
  801. case EXCLUDE_OPTION:
  802. add_exclude (excluded, arg, args->exclude_options | recursion_option);
  803. break;
  804. case EXCLUDE_CACHES_OPTION:
  805. exclude_caches_option = true;
  806. break;
  807. case FORCE_LOCAL_OPTION:
  808. force_local_option = true;
  809. break;
  810. case 'H':
  811. set_archive_format (arg);
  812. break;
  813. case INDEX_FILE_OPTION:
  814. index_file_name = arg;
  815. break;
  816. case IGNORE_CASE_OPTION:
  817. args->exclude_options |= FNM_CASEFOLD;
  818. break;
  819. case IGNORE_FAILED_READ_OPTION:
  820. ignore_failed_read_option = true;
  821. break;
  822. case KEEP_NEWER_FILES_OPTION:
  823. old_files_option = KEEP_NEWER_FILES;
  824. break;
  825. case GROUP_OPTION:
  826. if (! (strlen (arg) < GNAME_FIELD_SIZE
  827. && gname_to_gid (arg, &group_option)))
  828. {
  829. uintmax_t g;
  830. if (xstrtoumax (arg, 0, 10, &g, "") == LONGINT_OK
  831. && g == (gid_t) g)
  832. group_option = g;
  833. else
  834. FATAL_ERROR ((0, 0, "%s: %s", quotearg_colon (arg),
  835. _("%s: Invalid group")));
  836. }
  837. break;
  838. case MODE_OPTION:
  839. mode_option
  840. = mode_compile (arg,
  841. MODE_MASK_EQUALS | MODE_MASK_PLUS | MODE_MASK_MINUS);
  842. if (mode_option == MODE_INVALID)
  843. FATAL_ERROR ((0, 0, _("Invalid mode given on option")));
  844. if (mode_option == MODE_MEMORY_EXHAUSTED)
  845. xalloc_die ();
  846. break;
  847. case NO_ANCHORED_OPTION:
  848. args->exclude_options &= ~ EXCLUDE_ANCHORED;
  849. break;
  850. case NO_IGNORE_CASE_OPTION:
  851. args->exclude_options &= ~ FNM_CASEFOLD;
  852. break;
  853. case NO_OVERWRITE_DIR_OPTION:
  854. old_files_option = NO_OVERWRITE_DIR_OLD_FILES;
  855. break;
  856. case NO_WILDCARDS_OPTION:
  857. args->exclude_options &= ~ EXCLUDE_WILDCARDS;
  858. break;
  859. case NO_WILDCARDS_MATCH_SLASH_OPTION:
  860. args->exclude_options |= FNM_FILE_NAME;
  861. break;
  862. case NULL_OPTION:
  863. filename_terminator = '\0';
  864. break;
  865. case NUMERIC_OWNER_OPTION:
  866. numeric_owner_option = true;
  867. break;
  868. case OCCURRENCE_OPTION:
  869. if (!arg)
  870. occurrence_option = 1;
  871. else
  872. {
  873. uintmax_t u;
  874. if (xstrtoumax (arg, 0, 10, &u, "") == LONGINT_OK)
  875. occurrence_option = u;
  876. else
  877. FATAL_ERROR ((0, 0, "%s: %s", quotearg_colon (arg),
  878. _("Invalid number")));
  879. }
  880. break;
  881. case OVERWRITE_OPTION:
  882. old_files_option = OVERWRITE_OLD_FILES;
  883. break;
  884. case OWNER_OPTION:
  885. if (! (strlen (arg) < UNAME_FIELD_SIZE
  886. && uname_to_uid (arg, &owner_option)))
  887. {
  888. uintmax_t u;
  889. if (xstrtoumax (arg, 0, 10, &u, "") == LONGINT_OK
  890. && u == (uid_t) u)
  891. owner_option = u;
  892. else
  893. FATAL_ERROR ((0, 0, "%s: %s", quotearg_colon (arg),
  894. _("Invalid owner")));
  895. }
  896. break;
  897. case PAX_OPTION:
  898. args->pax_option++;
  899. xheader_set_option (arg);
  900. break;
  901. case POSIX_OPTION:
  902. set_archive_format ("posix");
  903. break;
  904. case PRESERVE_OPTION:
  905. same_permissions_option = true;
  906. same_order_option = true;
  907. break;
  908. case RECORD_SIZE_OPTION:
  909. {
  910. uintmax_t u;
  911. if (! (xstrtoumax (arg, 0, 10, &u, "") == LONGINT_OK
  912. && u == (size_t) u))
  913. USAGE_ERROR ((0, 0, "%s: %s", quotearg_colon (arg),
  914. _("Invalid record size")));
  915. record_size = u;
  916. if (record_size % BLOCKSIZE != 0)
  917. USAGE_ERROR ((0, 0, _("Record size must be a multiple of %d."),
  918. BLOCKSIZE));
  919. blocking_factor = record_size / BLOCKSIZE;
  920. }
  921. break;
  922. case RECURSIVE_UNLINK_OPTION:
  923. recursive_unlink_option = true;
  924. break;
  925. case REMOVE_FILES_OPTION:
  926. remove_files_option = true;
  927. break;
  928. case RMT_COMMAND_OPTION:
  929. rmt_command = arg;
  930. break;
  931. case RSH_COMMAND_OPTION:
  932. rsh_command_option = arg;
  933. break;
  934. case SHOW_DEFAULTS_OPTION:
  935. show_default_settings (stdout);
  936. exit(0);
  937. case STRIP_COMPONENTS_OPTION:
  938. {
  939. uintmax_t u;
  940. if (! (xstrtoumax (arg, 0, 10, &u, "") == LONGINT_OK
  941. && u == (size_t) u))
  942. USAGE_ERROR ((0, 0, "%s: %s", quotearg_colon (arg),
  943. _("Invalid number of elements")));
  944. strip_name_components = u;
  945. }
  946. break;
  947. case SHOW_OMITTED_DIRS_OPTION:
  948. show_omitted_dirs_option = true;
  949. break;
  950. case SUFFIX_OPTION:
  951. backup_option = true;
  952. args->backup_suffix_string = arg;
  953. break;
  954. case TOTALS_OPTION:
  955. totals_option = true;
  956. break;
  957. case USE_COMPRESS_PROGRAM_OPTION:
  958. set_use_compress_program_option (arg);
  959. break;
  960. case VOLNO_FILE_OPTION:
  961. volno_file_option = arg;
  962. break;
  963. case WILDCARDS_OPTION:
  964. args->exclude_options |= EXCLUDE_WILDCARDS;
  965. break;
  966. case WILDCARDS_MATCH_SLASH_OPTION:
  967. args->exclude_options &= ~ FNM_FILE_NAME;
  968. break;
  969. case CHECK_LINKS_OPTION:
  970. check_links_option = 1;
  971. break;
  972. case NO_RECURSION_OPTION:
  973. recursion_option = 0;
  974. break;
  975. case NO_SAME_OWNER_OPTION:
  976. same_owner_option = -1;
  977. break;
  978. case NO_SAME_PERMISSIONS_OPTION:
  979. same_permissions_option = -1;
  980. break;
  981. case RECURSION_OPTION:
  982. recursion_option = FNM_LEADING_DIR;
  983. break;
  984. case SAME_OWNER_OPTION:
  985. same_owner_option = 1;
  986. break;
  987. case '0':
  988. case '1':
  989. case '2':
  990. case '3':
  991. case '4':
  992. case '5':
  993. case '6':
  994. case '7':
  995. #ifdef DEVICE_PREFIX
  996. {
  997. int device = key - '0';
  998. int density;
  999. static char buf[sizeof DEVICE_PREFIX + 10];
  1000. char *cursor;
  1001. if (arg[1])
  1002. argp_error (state, _("Malformed density argument: '%s'"), arg);
  1003. strcpy (buf, DEVICE_PREFIX);
  1004. cursor = buf + strlen (buf);
  1005. #ifdef DENSITY_LETTER
  1006. sprintf (cursor, "%d%c", device, arg[0]);
  1007. #else /* not DENSITY_LETTER */
  1008. switch (arg[0])
  1009. {
  1010. case 'l':
  1011. #ifdef LOW_NUM
  1012. device += LOW_NUM;
  1013. #endif
  1014. break;
  1015. case 'm':
  1016. #ifdef MID_NUM
  1017. device += MID_NUM;
  1018. #else
  1019. device += 8;
  1020. #endif
  1021. break;
  1022. case 'h':
  1023. #ifdef HGH_NUM
  1024. device += HGH_NUM;
  1025. #else
  1026. device += 16;
  1027. #endif
  1028. break;
  1029. default:
  1030. argp_error (state, _("Unknown density: '%c'"), arg[0]);
  1031. }
  1032. sprintf (cursor, "%d", device);
  1033. #endif /* not DENSITY_LETTER */
  1034. if (archive_names == allocated_archive_names)
  1035. {
  1036. allocated_archive_names *= 2;
  1037. archive_name_array =
  1038. xrealloc (archive_name_array,
  1039. sizeof (const char *) * allocated_archive_names);
  1040. }
  1041. archive_name_array[archive_names++] = strdup (buf);
  1042. }
  1043. break;
  1044. #else /* not DEVICE_PREFIX */
  1045. argp_error (state,
  1046. _("Options `-[0-7][lmh]' not supported by *this* tar"));
  1047. #endif /* not DEVICE_PREFIX */
  1048. case '?':
  1049. state->flags |= ARGP_NO_EXIT;
  1050. argp_state_help (state, state->out_stream,
  1051. ARGP_HELP_STD_HELP & ~ARGP_HELP_BUG_ADDR);
  1052. fprintf (state->out_stream, _("\n*This* tar defaults to:\n"));
  1053. show_default_settings (state->out_stream);
  1054. fprintf (state->out_stream, "\n");
  1055. fprintf (state->out_stream, _("Report bugs to %s.\n"),
  1056. argp_program_bug_address);
  1057. exit (0);
  1058. case USAGE_OPTION:
  1059. argp_state_help (state, state->out_stream,
  1060. ARGP_HELP_USAGE | ARGP_HELP_EXIT_OK);
  1061. break;
  1062. case VERSION_OPTION:
  1063. fprintf (state->out_stream, "%s\n", argp_program_version);
  1064. exit (0);
  1065. case LICENSE_OPTION:
  1066. license ();
  1067. break;
  1068. default:
  1069. return ARGP_ERR_UNKNOWN;
  1070. }
  1071. return 0;
  1072. }
  1073. static struct argp argp = {
  1074. options,
  1075. parse_opt,
  1076. N_("[FILE]..."),
  1077. doc,
  1078. NULL,
  1079. NULL,
  1080. NULL
  1081. };
  1082. void
  1083. usage (int status)
  1084. {
  1085. argp_help (&argp, stderr, ARGP_HELP_SEE, (char*) program_name);
  1086. exit (status);
  1087. }
  1088. /* Parse the options for tar. */
  1089. static struct argp_option *
  1090. find_argp_option (struct argp_option *options, int letter)
  1091. {
  1092. for (;
  1093. !(options->name == NULL
  1094. && options->key == 0
  1095. && options->arg == 0
  1096. && options->flags == 0
  1097. && options->doc == NULL); options++)
  1098. if (options->key == letter)
  1099. return options;
  1100. return NULL;
  1101. }
  1102. static void
  1103. decode_options (int argc, char **argv)
  1104. {
  1105. int index;
  1106. struct tar_args args;
  1107. /* Set some default option values. */
  1108. args.textual_date_option = NULL;
  1109. args.exclude_options = EXCLUDE_WILDCARDS;
  1110. args.o_option = 0;
  1111. args.pax_option = 0;
  1112. args.backup_suffix_string = getenv ("SIMPLE_BACKUP_SUFFIX");
  1113. args.version_control_string = 0;
  1114. args.input_files = 0;
  1115. subcommand_option = UNKNOWN_SUBCOMMAND;
  1116. archive_format = DEFAULT_FORMAT;
  1117. blocking_factor = DEFAULT_BLOCKING;
  1118. record_size = DEFAULT_BLOCKING * BLOCKSIZE;
  1119. excluded = new_exclude ();
  1120. newer_mtime_option.tv_sec = TYPE_MINIMUM (time_t);
  1121. newer_mtime_option.tv_nsec = -1;
  1122. recursion_option = FNM_LEADING_DIR;
  1123. owner_option = -1;
  1124. group_option = -1;
  1125. /* Convert old-style tar call by exploding option element and rearranging
  1126. options accordingly. */
  1127. if (argc > 1 && argv[1][0] != '-')
  1128. {
  1129. int new_argc; /* argc value for rearranged arguments */
  1130. char **new_argv; /* argv value for rearranged arguments */
  1131. char *const *in; /* cursor into original argv */
  1132. char **out; /* cursor into rearranged argv */
  1133. const char *letter; /* cursor into old option letters */
  1134. char buffer[3]; /* constructed option buffer */
  1135. /* Initialize a constructed option. */
  1136. buffer[0] = '-';
  1137. buffer[2] = '\0';
  1138. /* Allocate a new argument array, and copy program name in it. */
  1139. new_argc = argc - 1 + strlen (argv[1]);
  1140. new_argv = xmalloc ((new_argc + 1) * sizeof (char *));
  1141. in = argv;
  1142. out = new_argv;
  1143. *out++ = *in++;
  1144. /* Copy each old letter option as a separate option, and have the
  1145. corresponding argument moved next to it. */
  1146. for (letter = *in++; *letter; letter++)
  1147. {
  1148. struct argp_option *opt;
  1149. buffer[1] = *letter;
  1150. *out++ = xstrdup (buffer);
  1151. opt = find_argp_option (options, *letter);
  1152. if (opt && opt->arg)
  1153. {
  1154. if (in < argv + argc)
  1155. *out++ = *in++;
  1156. else
  1157. USAGE_ERROR ((0, 0, _("Old option `%c' requires an argument."),
  1158. *letter));
  1159. }
  1160. }
  1161. /* Copy all remaining options. */
  1162. while (in < argv + argc)
  1163. *out++ = *in++;
  1164. *out = 0;
  1165. /* Replace the old option list by the new one. */
  1166. argc = new_argc;
  1167. argv = new_argv;
  1168. }
  1169. /* Parse all options and non-options as they appear. */
  1170. prepend_default_options (getenv ("TAR_OPTIONS"), &argc, &argv);
  1171. if (argp_parse (&argp, argc, argv, ARGP_IN_ORDER|ARGP_NO_HELP,
  1172. &index, &args))
  1173. exit (1);
  1174. /* Special handling for 'o' option:
  1175. GNU tar used to say "output old format".
  1176. UNIX98 tar says don't chown files after extracting (we use
  1177. "--no-same-owner" for this).
  1178. The old GNU tar semantics is retained when used with --create
  1179. option, otherwise UNIX98 semantics is assumed */
  1180. if (args.o_option)
  1181. {
  1182. if (subcommand_option == CREATE_SUBCOMMAND)
  1183. {
  1184. /* GNU Tar <= 1.13 compatibility */
  1185. set_archive_format ("v7");
  1186. }
  1187. else
  1188. {
  1189. /* UNIX98 compatibility */
  1190. same_owner_option = -1;
  1191. }
  1192. }
  1193. /* Handle operands after any "--" argument. */
  1194. for (; index < argc; index++)
  1195. {
  1196. name_add (argv[index]);
  1197. args.input_files++;
  1198. }
  1199. /* Derive option values and check option consistency. */
  1200. if (archive_format == DEFAULT_FORMAT)
  1201. {
  1202. if (args.pax_option)
  1203. archive_format = POSIX_FORMAT;
  1204. else
  1205. archive_format = DEFAULT_ARCHIVE_FORMAT;
  1206. }
  1207. if (volume_label_option && subcommand_option == CREATE_SUBCOMMAND)
  1208. assert_format (FORMAT_MASK (OLDGNU_FORMAT)
  1209. | FORMAT_MASK (GNU_FORMAT));
  1210. if (incremental_option || multi_volume_option)
  1211. assert_format (FORMAT_MASK (OLDGNU_FORMAT) | FORMAT_MASK (GNU_FORMAT));
  1212. if (sparse_option)
  1213. assert_format (FORMAT_MASK (OLDGNU_FORMAT)
  1214. | FORMAT_MASK (GNU_FORMAT)
  1215. | FORMAT_MASK (POSIX_FORMAT));
  1216. if (occurrence_option)
  1217. {
  1218. if (!args.input_files && !files_from_option)
  1219. USAGE_ERROR ((0, 0,
  1220. _("--occurrence is meaningless without a file list")));
  1221. if (subcommand_option != DELETE_SUBCOMMAND
  1222. && subcommand_option != DIFF_SUBCOMMAND
  1223. && subcommand_option != EXTRACT_SUBCOMMAND
  1224. && subcommand_option != LIST_SUBCOMMAND)
  1225. USAGE_ERROR ((0, 0,
  1226. _("--occurrence cannot be used in the requested operation mode")));
  1227. }
  1228. if (seekable_archive && subcommand_option == DELETE_SUBCOMMAND)
  1229. {
  1230. /* The current code in delete.c is based on the assumption that
  1231. skip_member() reads all data from the archive. So, we should
  1232. make sure it won't use seeks. On the other hand, the same code
  1233. depends on the ability to backspace a record in the archive,
  1234. so setting seekable_archive to false is technically incorrect.
  1235. However, it is tested only in skip_member(), so it's not a
  1236. problem. */
  1237. seekable_archive = false;
  1238. }
  1239. if (archive_names == 0)
  1240. {
  1241. /* If no archive file name given, try TAPE from the environment, or
  1242. else, DEFAULT_ARCHIVE from the configuration process. */
  1243. archive_names = 1;
  1244. archive_name_array[0] = getenv ("TAPE");
  1245. if (! archive_name_array[0])
  1246. archive_name_array[0] = DEFAULT_ARCHIVE;
  1247. }
  1248. /* Allow multiple archives only with `-M'. */
  1249. if (archive_names > 1 && !multi_volume_option)
  1250. USAGE_ERROR ((0, 0,
  1251. _("Multiple archive files require `-M' option")));
  1252. if (listed_incremental_option
  1253. && NEWER_OPTION_INITIALIZED (newer_mtime_option))
  1254. USAGE_ERROR ((0, 0,
  1255. _("Cannot combine --listed-incremental with --newer")));
  1256. if (volume_label_option)
  1257. {
  1258. size_t volume_label_max_len =
  1259. (sizeof current_header->header.name
  1260. - 1 /* for trailing '\0' */
  1261. - (multi_volume_option
  1262. ? (sizeof " Volume "
  1263. - 1 /* for null at end of " Volume " */
  1264. + INT_STRLEN_BOUND (int) /* for volume number */
  1265. - 1 /* for sign, as 0 <= volno */)
  1266. : 0));
  1267. if (volume_label_max_len < strlen (volume_label_option))
  1268. USAGE_ERROR ((0, 0,
  1269. ngettext ("%s: Volume label is too long (limit is %lu byte)",
  1270. "%s: Volume label is too long (limit is %lu bytes)",
  1271. volume_label_max_len),
  1272. quotearg_colon (volume_label_option),
  1273. (unsigned long) volume_label_max_len));
  1274. }
  1275. if (verify_option)
  1276. {
  1277. if (multi_volume_option)
  1278. USAGE_ERROR ((0, 0, _("Cannot verify multi-volume archives")));
  1279. if (use_compress_program_option)
  1280. USAGE_ERROR ((0, 0, _("Cannot verify compressed archives")));
  1281. }
  1282. if (use_compress_program_option)
  1283. {
  1284. if (multi_volume_option)
  1285. USAGE_ERROR ((0, 0, _("Cannot use multi-volume compressed archives")));
  1286. if (subcommand_option == UPDATE_SUBCOMMAND)
  1287. USAGE_ERROR ((0, 0, _("Cannot update compressed archives")));
  1288. }
  1289. /* It is no harm to use --pax-option on non-pax archives in archive
  1290. reading mode. It may even be useful, since it allows to override
  1291. file attributes from tar headers. Therefore I allow such usage.
  1292. --gray */
  1293. if (args.pax_option
  1294. && archive_format != POSIX_FORMAT
  1295. && (subcommand_option != EXTRACT_SUBCOMMAND
  1296. || subcommand_option != DIFF_SUBCOMMAND
  1297. || subcommand_option != LIST_SUBCOMMAND))
  1298. USAGE_ERROR ((0, 0, _("--pax-option can be used only on POSIX archives")));
  1299. /* If ready to unlink hierarchies, so we are for simpler files. */
  1300. if (recursive_unlink_option)
  1301. old_files_option = UNLINK_FIRST_OLD_FILES;
  1302. if (utc_option)
  1303. verbose_option = 2;
  1304. /* Forbid using -c with no input files whatsoever. Check that `-f -',
  1305. explicit or implied, is used correctly. */
  1306. switch (subcommand_option)
  1307. {
  1308. case CREATE_SUBCOMMAND:
  1309. if (args.input_files == 0 && !files_from_option)
  1310. USAGE_ERROR ((0, 0,
  1311. _("Cowardly refusing to create an empty archive")));
  1312. break;
  1313. case EXTRACT_SUBCOMMAND:
  1314. case LIST_SUBCOMMAND:
  1315. case DIFF_SUBCOMMAND:
  1316. for (archive_name_cursor = archive_name_array;
  1317. archive_name_cursor < archive_name_array + archive_names;
  1318. archive_name_cursor++)
  1319. if (!strcmp (*archive_name_cursor, "-"))
  1320. request_stdin ("-f");
  1321. break;
  1322. case CAT_SUBCOMMAND:
  1323. case UPDATE_SUBCOMMAND:
  1324. case APPEND_SUBCOMMAND:
  1325. for (archive_name_cursor = archive_name_array;
  1326. archive_name_cursor < archive_name_array + archive_names;
  1327. archive_name_cursor++)
  1328. if (!strcmp (*archive_name_cursor, "-"))
  1329. USAGE_ERROR ((0, 0,
  1330. _("Options `-Aru' are incompatible with `-f -'")));
  1331. default:
  1332. break;
  1333. }
  1334. archive_name_cursor = archive_name_array;
  1335. /* Prepare for generating backup names. */
  1336. if (args.backup_suffix_string)
  1337. simple_backup_suffix = xstrdup (args.backup_suffix_string);
  1338. if (backup_option)
  1339. backup_type = xget_version ("--backup", args.version_control_string);
  1340. if (verbose_option && args.textual_date_option)
  1341. {
  1342. /* FIXME: tartime should support nanoseconds, too, so that this
  1343. comparison doesn't complain about lost nanoseconds. */
  1344. char const *treated_as = tartime (newer_mtime_option.tv_sec);
  1345. if (strcmp (args.textual_date_option, treated_as) != 0)
  1346. WARN ((0, 0,
  1347. ngettext ("Treating date `%s' as %s + %ld nanosecond",
  1348. "Treating date `%s' as %s + %ld nanoseconds",
  1349. newer_mtime_option.tv_nsec),
  1350. args.textual_date_option, treated_as,
  1351. newer_mtime_option.tv_nsec));
  1352. }
  1353. }
  1354. /* Tar proper. */
  1355. /* Main routine for tar. */
  1356. int
  1357. main (int argc, char **argv)
  1358. {
  1359. set_start_time ();
  1360. program_name = argv[0];
  1361. setlocale (LC_ALL, "");
  1362. bindtextdomain (PACKAGE, LOCALEDIR);
  1363. textdomain (PACKAGE);
  1364. exit_status = TAREXIT_SUCCESS;
  1365. filename_terminator = '\n';
  1366. set_quoting_style (0, escape_quoting_style);
  1367. /* Pre-allocate a few structures. */
  1368. allocated_archive_names = 10;
  1369. archive_name_array =
  1370. xmalloc (sizeof (const char *) * allocated_archive_names);
  1371. archive_names = 0;
  1372. #ifdef SIGCHLD
  1373. /* System V fork+wait does not work if SIGCHLD is ignored. */
  1374. signal (SIGCHLD, SIG_DFL);
  1375. #endif
  1376. init_names ();
  1377. /* Decode options. */
  1378. decode_options (argc, argv);
  1379. name_init ();
  1380. /* Main command execution. */
  1381. if (volno_file_option)
  1382. init_volume_number ();
  1383. switch (subcommand_option)
  1384. {
  1385. case UNKNOWN_SUBCOMMAND:
  1386. USAGE_ERROR ((0, 0,
  1387. _("You must specify one of the `-Acdtrux' options")));
  1388. case CAT_SUBCOMMAND:
  1389. case UPDATE_SUBCOMMAND:
  1390. case APPEND_SUBCOMMAND:
  1391. update_archive ();
  1392. break;
  1393. case DELETE_SUBCOMMAND:
  1394. delete_archive_members ();
  1395. break;
  1396. case CREATE_SUBCOMMAND:
  1397. create_archive ();
  1398. name_close ();
  1399. if (totals_option)
  1400. print_total_written ();
  1401. break;
  1402. case EXTRACT_SUBCOMMAND:
  1403. extr_init ();
  1404. read_and (extract_archive);
  1405. /* FIXME: should extract_finish () even if an ordinary signal is
  1406. received. */
  1407. extract_finish ();
  1408. break;
  1409. case LIST_SUBCOMMAND:
  1410. read_and (list_archive);
  1411. break;
  1412. case DIFF_SUBCOMMAND:
  1413. diff_init ();
  1414. read_and (diff_archive);
  1415. break;
  1416. }
  1417. if (check_links_option)
  1418. check_links ();
  1419. if (volno_file_option)
  1420. closeout_volume_number ();
  1421. /* Dispose of allocated memory, and return. */
  1422. free (archive_name_array);
  1423. name_term ();
  1424. if (stdlis != stderr && (ferror (stdlis) || fclose (stdlis) != 0))
  1425. FATAL_ERROR ((0, 0, _("Error in writing to standard output")));
  1426. if (exit_status == TAREXIT_FAILURE)
  1427. error (0, 0, _("Error exit delayed from previous errors"));
  1428. if (ferror (stderr) || fclose (stderr) != 0)
  1429. exit_status = TAREXIT_FAILURE;
  1430. return exit_status;
  1431. }
  1432. void
  1433. tar_stat_init (struct tar_stat_info *st)
  1434. {
  1435. memset (st, 0, sizeof (*st));
  1436. }
  1437. void
  1438. tar_stat_destroy (struct tar_stat_info *st)
  1439. {
  1440. free (st->orig_file_name);
  1441. free (st->file_name);
  1442. free (st->link_name);
  1443. free (st->uname);
  1444. free (st->gname);
  1445. free (st->sparse_map);
  1446. memset (st, 0, sizeof (*st));
  1447. }