tar.c 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181
  1. /* A tar (tape archiver) program.
  2. Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1999, 2000,
  3. 2001, 2003, 2004, 2005, 2006 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. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
  16. #include <system.h>
  17. #include <fnmatch.h>
  18. #include <getline.h>
  19. #include <argp.h>
  20. #include <argp-namefrob.h>
  21. #include <signal.h>
  22. #if ! defined SIGCHLD && defined SIGCLD
  23. # define SIGCHLD SIGCLD
  24. #endif
  25. /* The following causes "common.h" to produce definitions of all the global
  26. variables, rather than just "extern" declarations of them. GNU tar does
  27. depend on the system loader to preset all GLOBAL variables to neutral (or
  28. zero) values; explicit initialization is usually not done. */
  29. #define GLOBAL
  30. #include "common.h"
  31. #include <argmatch.h>
  32. #include <closeout.h>
  33. #include <exitfail.h>
  34. #include <getdate.h>
  35. #include <localedir.h>
  36. #include <rmt.h>
  37. #include <prepargs.h>
  38. #include <quotearg.h>
  39. #include <version-etc.h>
  40. #include <xstrtol.h>
  41. #include <stdopen.h>
  42. /* Local declarations. */
  43. #ifndef DEFAULT_ARCHIVE_FORMAT
  44. # define DEFAULT_ARCHIVE_FORMAT GNU_FORMAT
  45. #endif
  46. #ifndef DEFAULT_ARCHIVE
  47. # define DEFAULT_ARCHIVE "tar.out"
  48. #endif
  49. #ifndef DEFAULT_BLOCKING
  50. # define DEFAULT_BLOCKING 20
  51. #endif
  52. /* Miscellaneous. */
  53. /* Name of option using stdin. */
  54. static const char *stdin_used_by;
  55. /* Doesn't return if stdin already requested. */
  56. void
  57. request_stdin (const char *option)
  58. {
  59. if (stdin_used_by)
  60. USAGE_ERROR ((0, 0, _("Options `-%s' and `-%s' both want standard input"),
  61. stdin_used_by, option));
  62. stdin_used_by = option;
  63. }
  64. extern int rpmatch (char const *response);
  65. /* Returns true if and only if the user typed an affirmative response. */
  66. int
  67. confirm (const char *message_action, const char *message_name)
  68. {
  69. static FILE *confirm_file;
  70. static int confirm_file_EOF;
  71. bool status = false;
  72. if (!confirm_file)
  73. {
  74. if (archive == 0 || stdin_used_by)
  75. {
  76. confirm_file = fopen (TTY_NAME, "r");
  77. if (! confirm_file)
  78. open_fatal (TTY_NAME);
  79. }
  80. else
  81. {
  82. request_stdin ("-w");
  83. confirm_file = stdin;
  84. }
  85. }
  86. fprintf (stdlis, "%s %s?", message_action, quote (message_name));
  87. fflush (stdlis);
  88. if (!confirm_file_EOF)
  89. {
  90. char *response = NULL;
  91. size_t response_size = 0;
  92. if (getline (&response, &response_size, confirm_file) < 0)
  93. confirm_file_EOF = 1;
  94. else
  95. status = rpmatch (response) > 0;
  96. free (response);
  97. }
  98. if (confirm_file_EOF)
  99. {
  100. fputc ('\n', stdlis);
  101. fflush (stdlis);
  102. }
  103. return status;
  104. }
  105. static struct fmttab {
  106. char const *name;
  107. enum archive_format fmt;
  108. } const fmttab[] = {
  109. { "v7", V7_FORMAT },
  110. { "oldgnu", OLDGNU_FORMAT },
  111. { "ustar", USTAR_FORMAT },
  112. { "posix", POSIX_FORMAT },
  113. #if 0 /* not fully supported yet */
  114. { "star", STAR_FORMAT },
  115. #endif
  116. { "gnu", GNU_FORMAT },
  117. { "pax", POSIX_FORMAT }, /* An alias for posix */
  118. { NULL, 0 }
  119. };
  120. static void
  121. set_archive_format (char const *name)
  122. {
  123. struct fmttab const *p;
  124. for (p = fmttab; strcmp (p->name, name) != 0; )
  125. if (! (++p)->name)
  126. USAGE_ERROR ((0, 0, _("%s: Invalid archive format"),
  127. quotearg_colon (name)));
  128. archive_format = p->fmt;
  129. }
  130. const char *
  131. archive_format_string (enum archive_format fmt)
  132. {
  133. struct fmttab const *p;
  134. for (p = fmttab; p->name; p++)
  135. if (p->fmt == fmt)
  136. return p->name;
  137. return "unknown?";
  138. }
  139. #define FORMAT_MASK(n) (1<<(n))
  140. static void
  141. assert_format(unsigned fmt_mask)
  142. {
  143. if ((FORMAT_MASK (archive_format) & fmt_mask) == 0)
  144. USAGE_ERROR ((0, 0,
  145. _("GNU features wanted on incompatible archive format")));
  146. }
  147. const char *
  148. subcommand_string (enum subcommand c)
  149. {
  150. switch (c)
  151. {
  152. case UNKNOWN_SUBCOMMAND:
  153. return "unknown?";
  154. case APPEND_SUBCOMMAND:
  155. return "-r";
  156. case CAT_SUBCOMMAND:
  157. return "-A";
  158. case CREATE_SUBCOMMAND:
  159. return "-c";
  160. case DELETE_SUBCOMMAND:
  161. return "-D";
  162. case DIFF_SUBCOMMAND:
  163. return "-d";
  164. case EXTRACT_SUBCOMMAND:
  165. return "-x";
  166. case LIST_SUBCOMMAND:
  167. return "-t";
  168. case UPDATE_SUBCOMMAND:
  169. return "-u";
  170. default:
  171. abort ();
  172. }
  173. }
  174. void
  175. tar_list_quoting_styles (FILE *fp, char *prefix)
  176. {
  177. int i;
  178. for (i = 0; quoting_style_args[i]; i++)
  179. fprintf (fp, "%s%s\n", prefix, quoting_style_args[i]);
  180. }
  181. void
  182. tar_set_quoting_style (char *arg)
  183. {
  184. int i;
  185. for (i = 0; quoting_style_args[i]; i++)
  186. if (strcmp (arg, quoting_style_args[i]) == 0)
  187. {
  188. set_quoting_style (NULL, i);
  189. return;
  190. }
  191. FATAL_ERROR ((0, 0,
  192. _("Unknown quoting style `%s'. Try `%s --quoting-style=help' to get a list."), arg, program_invocation_short_name));
  193. }
  194. /* Options. */
  195. enum
  196. {
  197. ANCHORED_OPTION = CHAR_MAX + 1,
  198. ATIME_PRESERVE_OPTION,
  199. BACKUP_OPTION,
  200. CHECKPOINT_OPTION,
  201. DELAY_DIRECTORY_RESTORE_OPTION,
  202. DELETE_OPTION,
  203. EXCLUDE_CACHES_OPTION,
  204. EXCLUDE_OPTION,
  205. FORCE_LOCAL_OPTION,
  206. GROUP_OPTION,
  207. HANG_OPTION,
  208. IGNORE_CASE_OPTION,
  209. IGNORE_COMMAND_ERROR_OPTION,
  210. IGNORE_FAILED_READ_OPTION,
  211. INDEX_FILE_OPTION,
  212. KEEP_NEWER_FILES_OPTION,
  213. MODE_OPTION,
  214. NEWER_MTIME_OPTION,
  215. NO_ANCHORED_OPTION,
  216. NO_DELAY_DIRECTORY_RESTORE_OPTION,
  217. NO_IGNORE_CASE_OPTION,
  218. NO_IGNORE_COMMAND_ERROR_OPTION,
  219. NO_OVERWRITE_DIR_OPTION,
  220. NO_QUOTE_CHARS_OPTION,
  221. NO_RECURSION_OPTION,
  222. NO_SAME_OWNER_OPTION,
  223. NO_SAME_PERMISSIONS_OPTION,
  224. NO_UNQUOTE_OPTION,
  225. NO_WILDCARDS_MATCH_SLASH_OPTION,
  226. NO_WILDCARDS_OPTION,
  227. NULL_OPTION,
  228. NUMERIC_OWNER_OPTION,
  229. OCCURRENCE_OPTION,
  230. OLD_ARCHIVE_OPTION,
  231. ONE_FILE_SYSTEM_OPTION,
  232. OVERWRITE_OPTION,
  233. OWNER_OPTION,
  234. PAX_OPTION,
  235. POSIX_OPTION,
  236. PRESERVE_OPTION,
  237. QUOTE_CHARS_OPTION,
  238. QUOTING_STYLE_OPTION,
  239. RECORD_SIZE_OPTION,
  240. RECURSION_OPTION,
  241. RECURSIVE_UNLINK_OPTION,
  242. REMOVE_FILES_OPTION,
  243. RESTRICT_OPTION,
  244. RMT_COMMAND_OPTION,
  245. RSH_COMMAND_OPTION,
  246. SAME_OWNER_OPTION,
  247. SHOW_DEFAULTS_OPTION,
  248. SHOW_OMITTED_DIRS_OPTION,
  249. SHOW_TRANSFORMED_NAMES_OPTION,
  250. STRIP_COMPONENTS_OPTION,
  251. SUFFIX_OPTION,
  252. TEST_LABEL_OPTION,
  253. TOTALS_OPTION,
  254. TO_COMMAND_OPTION,
  255. TRANSFORM_OPTION,
  256. UNQUOTE_OPTION,
  257. USAGE_OPTION,
  258. USE_COMPRESS_PROGRAM_OPTION,
  259. UTC_OPTION,
  260. VERSION_OPTION,
  261. VOLNO_FILE_OPTION,
  262. WILDCARDS_MATCH_SLASH_OPTION,
  263. WILDCARDS_OPTION
  264. };
  265. const char *argp_program_version = "tar (" PACKAGE_NAME ") " VERSION;
  266. const char *argp_program_bug_address = "<" PACKAGE_BUGREPORT ">";
  267. 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\
  268. \n\
  269. Examples:\n\
  270. tar -cf archive.tar foo bar # Create archive.tar from files foo and bar.\n\
  271. tar -tvf archive.tar # List all files in archive.tar verbosely.\n\
  272. tar -xf archive.tar # Extract all files from archive.tar.\n\
  273. \vThe backup suffix is `~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.\n\
  274. The version control may be set with --backup or VERSION_CONTROL, values are:\n\n\
  275. none, off never make backups\n\
  276. t, numbered make numbered backups\n\
  277. nil, existing numbered if numbered backups exist, simple otherwise\n\
  278. never, simple always make simple backups\n");
  279. /* NOTE:
  280. Available option letters are DEIJQY and aeqy. Consider the following
  281. assignments:
  282. [For Solaris tar compatibility =/= Is it important at all?]
  283. e exit immediately with a nonzero exit status if unexpected errors occur
  284. E use extended headers (--format=posix)
  285. [q alias for --occurrence=1 =/= this would better be used for quiet?]
  286. [I same as T =/= will harm star compatibility]
  287. y per-file gzip compression
  288. Y per-block gzip compression */
  289. static struct argp_option options[] = {
  290. #define GRID 10
  291. {NULL, 0, NULL, 0,
  292. N_("Main operation mode:"), GRID },
  293. {"list", 't', 0, 0,
  294. N_("list the contents of an archive"), GRID+1 },
  295. {"extract", 'x', 0, 0,
  296. N_("extract files from an archive"), GRID+1 },
  297. {"get", 0, 0, OPTION_ALIAS, NULL, GRID+1 },
  298. {"create", 'c', 0, 0,
  299. N_("create a new archive"), GRID+1 },
  300. {"diff", 'd', 0, 0,
  301. N_("find differences between archive and file system"), GRID+1 },
  302. {"compare", 0, 0, OPTION_ALIAS, NULL, GRID+1 },
  303. {"append", 'r', 0, 0,
  304. N_("append files to the end of an archive"), GRID+1 },
  305. {"update", 'u', 0, 0,
  306. N_("only append files newer than copy in archive"), GRID+1 },
  307. {"catenate", 'A', 0, 0,
  308. N_("append tar files to an archive"), GRID+1 },
  309. {"concatenate", 0, 0, OPTION_ALIAS, NULL, GRID+1 },
  310. {"delete", DELETE_OPTION, 0, 0,
  311. N_("delete from the archive (not on mag tapes!)"), GRID+1 },
  312. {"test-label", TEST_LABEL_OPTION, NULL, 0,
  313. N_("test the archive volume label and exit"), GRID+1 },
  314. #undef GRID
  315. #define GRID 20
  316. {NULL, 0, NULL, 0,
  317. N_("Operation modifiers:"), GRID },
  318. {"sparse", 'S', 0, 0,
  319. N_("handle sparse files efficiently"), GRID+1 },
  320. {"incremental", 'G', 0, 0,
  321. N_("handle old GNU-format incremental backup"), GRID+1 },
  322. {"listed-incremental", 'g', N_("FILE"), 0,
  323. N_("handle new GNU-format incremental backup"), GRID+1 },
  324. {"ignore-failed-read", IGNORE_FAILED_READ_OPTION, 0, 0,
  325. N_("do not exit with nonzero on unreadable files"), GRID+1 },
  326. {"occurrence", OCCURRENCE_OPTION, N_("NUMBER"), OPTION_ARG_OPTIONAL,
  327. N_("process only the NUMBERth occurrence of each file in the archive;"
  328. " this option is valid only in conjunction with one of the subcommands"
  329. " --delete, --diff, --extract or --list and when a list of files"
  330. " is given either on the command line or via the -T option;"
  331. " NUMBER defaults to 1"), GRID+1 },
  332. {"seek", 'n', NULL, 0,
  333. N_("archive is seekable"), GRID+1 },
  334. #undef GRID
  335. #define GRID 30
  336. {NULL, 0, NULL, 0,
  337. N_("Overwrite control:\n"), GRID+1 },
  338. {"verify", 'W', 0, 0,
  339. N_("attempt to verify the archive after writing it"), GRID+1 },
  340. {"remove-files", REMOVE_FILES_OPTION, 0, 0,
  341. N_("remove files after adding them to the archive"), GRID+1 },
  342. {"keep-old-files", 'k', 0, 0,
  343. N_("don't replace existing files when extracting"), GRID+1 },
  344. {"keep-newer-files", KEEP_NEWER_FILES_OPTION, 0, 0,
  345. N_("don't replace existing files that are newer than their archive copies"), GRID+1 },
  346. {"overwrite", OVERWRITE_OPTION, 0, 0,
  347. N_("overwrite existing files when extracting"), GRID+1 },
  348. {"unlink-first", 'U', 0, 0,
  349. N_("remove each file prior to extracting over it"), GRID+1 },
  350. {"recursive-unlink", RECURSIVE_UNLINK_OPTION, 0, 0,
  351. N_("empty hierarchies prior to extracting directory"), GRID+1 },
  352. {"no-overwrite-dir", NO_OVERWRITE_DIR_OPTION, 0, 0,
  353. N_("preserve metadata of existing directories"), GRID+1 },
  354. #undef GRID
  355. #define GRID 40
  356. {NULL, 0, NULL, 0,
  357. N_("Select output stream:"), GRID },
  358. {"to-stdout", 'O', 0, 0,
  359. N_("extract files to standard output"), GRID+1 },
  360. {"to-command", TO_COMMAND_OPTION, N_("COMMAND"), 0,
  361. N_("pipe extracted files to another program"), GRID+1 },
  362. {"ignore-command-error", IGNORE_COMMAND_ERROR_OPTION, 0, 0,
  363. N_("ignore exit codes of children"), GRID+1 },
  364. {"no-ignore-command-error", NO_IGNORE_COMMAND_ERROR_OPTION, 0, 0,
  365. N_("treat non-zero exit codes of children as error"), GRID+1 },
  366. #undef GRID
  367. #define GRID 50
  368. {NULL, 0, NULL, 0,
  369. N_("Handling of file attributes:"), GRID },
  370. {"owner", OWNER_OPTION, N_("NAME"), 0,
  371. N_("force NAME as owner for added files"), GRID+1 },
  372. {"group", GROUP_OPTION, N_("NAME"), 0,
  373. N_("force NAME as group for added files"), GRID+1 },
  374. {"mode", MODE_OPTION, N_("CHANGES"), 0,
  375. N_("force (symbolic) mode CHANGES for added files"), GRID+1 },
  376. {"atime-preserve", ATIME_PRESERVE_OPTION,
  377. N_("METHOD"), OPTION_ARG_OPTIONAL,
  378. N_("preserve access times on dumped files, either by restoring the times"
  379. " after reading (METHOD='replace'; default) or by not setting the times"
  380. " in the first place (METHOD='system')"), GRID+1 },
  381. {"touch", 'm', 0, 0,
  382. N_("don't extract file modified time"), GRID+1 },
  383. {"same-owner", SAME_OWNER_OPTION, 0, 0,
  384. N_("try extracting files with the same ownership"), GRID+1 },
  385. {"no-same-owner", NO_SAME_OWNER_OPTION, 0, 0,
  386. N_("extract files as yourself"), GRID+1 },
  387. {"numeric-owner", NUMERIC_OWNER_OPTION, 0, 0,
  388. N_("always use numbers for user/group names"), GRID+1 },
  389. {"preserve-permissions", 'p', 0, 0,
  390. N_("extract information about file permissions (default for superuser)"),
  391. GRID+1 },
  392. {"same-permissions", 0, 0, OPTION_ALIAS, NULL, GRID+1 },
  393. {"no-same-permissions", NO_SAME_PERMISSIONS_OPTION, 0, 0,
  394. N_("apply the user's umask when extracting permissions from the archive (default for ordinary users)"), GRID+1 },
  395. {"preserve-order", 's', 0, 0,
  396. N_("sort names to extract to match archive"), GRID+1 },
  397. {"same-order", 0, 0, OPTION_ALIAS, NULL, GRID+1 },
  398. {"preserve", PRESERVE_OPTION, 0, 0,
  399. N_("same as both -p and -s"), GRID+1 },
  400. {"delay-directory-restore", DELAY_DIRECTORY_RESTORE_OPTION, 0, 0,
  401. N_("delay setting modification times and permissions of extracted"
  402. " directories until the end of extraction"), GRID+1 },
  403. {"no-delay-directory-restore", NO_DELAY_DIRECTORY_RESTORE_OPTION, 0, 0,
  404. N_("cancel the effect of --delay-directory-restore option"), GRID+1 },
  405. #undef GRID
  406. #define GRID 60
  407. {NULL, 0, NULL, 0,
  408. N_("Device selection and switching:\n"), GRID+1 },
  409. {"file", 'f', N_("ARCHIVE"), 0,
  410. N_("use archive file or device ARCHIVE"), GRID+1 },
  411. {"force-local", FORCE_LOCAL_OPTION, 0, 0,
  412. N_("archive file is local even if it has a colon"), GRID+1 },
  413. {"rmt-command", RMT_COMMAND_OPTION, N_("COMMAND"), 0,
  414. N_("use given rmt COMMAND instead of rmt"), GRID+1 },
  415. {"rsh-command", RSH_COMMAND_OPTION, N_("COMMAND"), 0,
  416. N_("use remote COMMAND instead of rsh"), GRID+1 },
  417. #ifdef DEVICE_PREFIX
  418. {"-[0-7][lmh]", 0, NULL, OPTION_DOC, /* It is OK, since `name' will never be
  419. translated */
  420. N_("specify drive and density"), GRID+1 },
  421. #endif
  422. {NULL, '0', NULL, OPTION_HIDDEN, NULL, GRID+1 },
  423. {NULL, '1', NULL, OPTION_HIDDEN, NULL, GRID+1 },
  424. {NULL, '2', NULL, OPTION_HIDDEN, NULL, GRID+1 },
  425. {NULL, '3', NULL, OPTION_HIDDEN, NULL, GRID+1 },
  426. {NULL, '4', NULL, OPTION_HIDDEN, NULL, GRID+1 },
  427. {NULL, '5', NULL, OPTION_HIDDEN, NULL, GRID+1 },
  428. {NULL, '6', NULL, OPTION_HIDDEN, NULL, GRID+1 },
  429. {NULL, '7', NULL, OPTION_HIDDEN, NULL, GRID+1 },
  430. {NULL, '8', NULL, OPTION_HIDDEN, NULL, GRID+1 },
  431. {NULL, '9', NULL, OPTION_HIDDEN, NULL, GRID+1 },
  432. {"multi-volume", 'M', 0, 0,
  433. N_("create/list/extract multi-volume archive"), GRID+1 },
  434. {"tape-length", 'L', N_("NUMBER"), 0,
  435. N_("change tape after writing NUMBER x 1024 bytes"), GRID+1 },
  436. {"info-script", 'F', N_("NAME"), 0,
  437. N_("run script at end of each tape (implies -M)"), GRID+1 },
  438. {"new-volume-script", 0, 0, OPTION_ALIAS, NULL, GRID+1 },
  439. {"volno-file", VOLNO_FILE_OPTION, N_("FILE"), 0,
  440. N_("use/update the volume number in FILE"), GRID+1 },
  441. #undef GRID
  442. #define GRID 70
  443. {NULL, 0, NULL, 0,
  444. N_("Device blocking:"), GRID+1 },
  445. {"blocking-factor", 'b', N_("BLOCKS"), 0,
  446. N_("BLOCKS x 512 bytes per record"), GRID+1 },
  447. {"record-size", RECORD_SIZE_OPTION, N_("NUMBER"), 0,
  448. N_("NUMBER of bytes per record, multiple of 512"), GRID+1 },
  449. {"ignore-zeros", 'i', 0, 0,
  450. N_("ignore zeroed blocks in archive (means EOF)"), GRID+1 },
  451. {"read-full-records", 'B', 0, 0,
  452. N_("reblock as we read (for 4.2BSD pipes)"), GRID+1 },
  453. #undef GRID
  454. #define GRID 80
  455. {NULL, 0, NULL, 0,
  456. N_("Archive format selection:"), GRID },
  457. {"format", 'H', N_("FORMAT"), 0,
  458. N_("create archive of the given format"), GRID+1 },
  459. {NULL, 0, NULL, 0, N_("FORMAT is one of the following:"), GRID+2 },
  460. {" v7", 0, NULL, OPTION_DOC|OPTION_NO_TRANS, N_("old V7 tar format"),
  461. GRID+3 },
  462. {" oldgnu", 0, NULL, OPTION_DOC|OPTION_NO_TRANS,
  463. N_("GNU format as per tar <= 1.12"), GRID+3 },
  464. {" gnu", 0, NULL, OPTION_DOC|OPTION_NO_TRANS,
  465. N_("GNU tar 1.13.x format"), GRID+3 },
  466. {" ustar", 0, NULL, OPTION_DOC|OPTION_NO_TRANS,
  467. N_("POSIX 1003.1-1988 (ustar) format"), GRID+3 },
  468. {" pax", 0, NULL, OPTION_DOC|OPTION_NO_TRANS,
  469. N_("POSIX 1003.1-2001 (pax) format"), GRID+3 },
  470. {" posix", 0, NULL, OPTION_DOC|OPTION_NO_TRANS, N_("same as pax"), GRID+3 },
  471. {"old-archive", OLD_ARCHIVE_OPTION, 0, 0, /* FIXME */
  472. N_("same as --format=v7"), GRID+8 },
  473. {"portability", 0, 0, OPTION_ALIAS, NULL, GRID+8 },
  474. {"posix", POSIX_OPTION, 0, 0,
  475. N_("same as --format=posix"), GRID+8 },
  476. {"pax-option", PAX_OPTION, N_("keyword[[:]=value][,keyword[[:]=value]]..."), 0,
  477. N_("control pax keywords"), GRID+8 },
  478. {"label", 'V', N_("TEXT"), 0,
  479. N_("create archive with volume name TEXT; at list/extract time, use TEXT as a globbing pattern for volume name"), GRID+8 },
  480. {"bzip2", 'j', 0, 0,
  481. N_("filter the archive through bzip2"), GRID+8 },
  482. {"gzip", 'z', 0, 0,
  483. N_("filter the archive through gzip"), GRID+8 },
  484. {"gunzip", 0, 0, OPTION_ALIAS, NULL, GRID+8 },
  485. {"ungzip", 0, 0, OPTION_ALIAS, NULL, GRID+8 },
  486. {"compress", 'Z', 0, 0,
  487. N_("filter the archive through compress"), GRID+8 },
  488. {"uncompress", 0, 0, OPTION_ALIAS, NULL, GRID+8 },
  489. {"use-compress-program", USE_COMPRESS_PROGRAM_OPTION, N_("PROG"), 0,
  490. N_("filter through PROG (must accept -d)"), GRID+8 },
  491. #undef GRID
  492. #define GRID 90
  493. {NULL, 0, NULL, 0,
  494. N_("Local file selection:"), GRID },
  495. {"add-file", ARGP_KEY_ARG, N_("FILE"), 0,
  496. N_("add given FILE to the archive (useful if its name starts with a dash)"), GRID+1 },
  497. {"directory", 'C', N_("DIR"), 0,
  498. N_("change to directory DIR"), GRID+1 },
  499. {"files-from", 'T', N_("FILE"), 0,
  500. N_("get names to extract or create from FILE"), GRID+1 },
  501. {"null", NULL_OPTION, 0, 0,
  502. N_("-T reads null-terminated names, disable -C"), GRID+1 },
  503. {"unquote", UNQUOTE_OPTION, 0, 0,
  504. N_("unquote filenames read with -T (default)"), GRID+1 },
  505. {"no-unquote", NO_UNQUOTE_OPTION, 0, 0,
  506. N_("do not unquote filenames read with -T"), GRID+1 },
  507. {"exclude", EXCLUDE_OPTION, N_("PATTERN"), 0,
  508. N_("exclude files, given as a PATTERN"), GRID+1 },
  509. {"exclude-from", 'X', N_("FILE"), 0,
  510. N_("exclude patterns listed in FILE"), GRID+1 },
  511. {"exclude-caches", EXCLUDE_CACHES_OPTION, 0, 0,
  512. N_("exclude directories containing a cache tag"), GRID+1 },
  513. {"no-recursion", NO_RECURSION_OPTION, 0, 0,
  514. N_("avoid descending automatically in directories"), GRID+1 },
  515. {"one-file-system", ONE_FILE_SYSTEM_OPTION, 0, 0,
  516. N_("stay in local file system when creating archive"), GRID+1 },
  517. {"recursion", RECURSION_OPTION, 0, 0,
  518. N_("recurse into directories (default)"), GRID+1 },
  519. {"absolute-names", 'P', 0, 0,
  520. N_("don't strip leading `/'s from file names"), GRID+1 },
  521. {"dereference", 'h', 0, 0,
  522. N_("follow symlinks; archive and dump the files they point to"), GRID+1 },
  523. {"starting-file", 'K', N_("MEMBER-NAME"), 0,
  524. N_("begin at member MEMBER-NAME in the archive"), GRID+1 },
  525. {"newer", 'N', N_("DATE-OR-FILE"), 0,
  526. N_("only store files newer than DATE-OR-FILE"), GRID+1 },
  527. {"newer-mtime", NEWER_MTIME_OPTION, N_("DATE"), 0,
  528. N_("compare date and time when data changed only"), GRID+1 },
  529. {"after-date", 'N', N_("DATE"), 0,
  530. N_("same as -N"), GRID+1 },
  531. {"backup", BACKUP_OPTION, N_("CONTROL"), OPTION_ARG_OPTIONAL,
  532. N_("backup before removal, choose version CONTROL"), GRID+1 },
  533. {"suffix", SUFFIX_OPTION, N_("STRING"), 0,
  534. N_("backup before removal, override usual suffix ('~' unless overridden by environment variable SIMPLE_BACKUP_SUFFIX)"), GRID+1 },
  535. #undef GRID
  536. #define GRID 92
  537. {NULL, 0, NULL, 0,
  538. N_("File name transformations:"), GRID },
  539. {"strip-components", STRIP_COMPONENTS_OPTION, N_("NUMBER"), 0,
  540. N_("strip NUMBER leading components from file names on extraction"),
  541. GRID+1 },
  542. {"transform", TRANSFORM_OPTION, N_("EXPRESSION"), 0,
  543. N_("Use sed replace EXPRESSION to transform file names"), GRID+1 },
  544. #undef GRID
  545. #define GRID 95
  546. {NULL, 0, NULL, 0,
  547. N_("File name matching options (affect both exclude and include patterns):"),
  548. GRID },
  549. {"ignore-case", IGNORE_CASE_OPTION, 0, 0,
  550. N_("ignore case"), GRID+1 },
  551. {"anchored", ANCHORED_OPTION, 0, 0,
  552. N_("patterns match file name start"), GRID+1 },
  553. {"no-anchored", NO_ANCHORED_OPTION, 0, 0,
  554. N_("patterns match after any `/' (default for exclusion)"), GRID+1 },
  555. {"no-ignore-case", NO_IGNORE_CASE_OPTION, 0, 0,
  556. N_("case sensitive matching (default)"), GRID+1 },
  557. {"wildcards", WILDCARDS_OPTION, 0, 0,
  558. N_("use wildcards (default for exclusion)"), GRID+1 },
  559. {"no-wildcards", NO_WILDCARDS_OPTION, 0, 0,
  560. N_("verbatim string matching"), GRID+1 },
  561. {"no-wildcards-match-slash", NO_WILDCARDS_MATCH_SLASH_OPTION, 0, 0,
  562. N_("wildcards do not match `/'"), GRID+1 },
  563. {"wildcards-match-slash", WILDCARDS_MATCH_SLASH_OPTION, 0, 0,
  564. N_("wildcards match `/' (default for exclusion)"), GRID+1 },
  565. #undef GRID
  566. #define GRID 100
  567. {NULL, 0, NULL, 0,
  568. N_("Informative output:"), GRID },
  569. {"verbose", 'v', 0, 0,
  570. N_("verbosely list files processed"), GRID+1 },
  571. {"checkpoint", CHECKPOINT_OPTION, 0, 0,
  572. N_("display progress messages every 10th record"), GRID+1 },
  573. {"check-links", 'l', 0, 0,
  574. N_("print a message if not all links are dumped"), GRID+1 },
  575. {"totals", TOTALS_OPTION, 0, 0,
  576. N_("print total bytes written while creating archive"), GRID+1 },
  577. {"utc", UTC_OPTION, 0, 0,
  578. N_("print file modification dates in UTC"), GRID+1 },
  579. {"index-file", INDEX_FILE_OPTION, N_("FILE"), 0,
  580. N_("send verbose output to FILE"), GRID+1 },
  581. {"block-number", 'R', 0, 0,
  582. N_("show block number within archive with each message"), GRID+1 },
  583. {"interactive", 'w', 0, 0,
  584. N_("ask for confirmation for every action"), GRID+1 },
  585. {"confirmation", 0, 0, OPTION_ALIAS, NULL, GRID+1 },
  586. {"show-defaults", SHOW_DEFAULTS_OPTION, 0, 0,
  587. N_("show tar defaults"), GRID+1 },
  588. {"show-omitted-dirs", SHOW_OMITTED_DIRS_OPTION, 0, 0,
  589. N_("when listing or extracting, list each directory that does not match search criteria"), GRID+1 },
  590. {"show-transformed-names", SHOW_TRANSFORMED_NAMES_OPTION, 0, 0,
  591. N_("show file or archive names after transformation"),
  592. GRID+1 },
  593. {"show-stored-names", 0, 0, OPTION_ALIAS, NULL, GRID+1 },
  594. {"quoting-style", QUOTING_STYLE_OPTION, N_("STYLE"), 0,
  595. N_("set name quoting style; see below for valid STYLE values"), GRID+1 },
  596. {"quote-chars", QUOTE_CHARS_OPTION, N_("STRING"), 0,
  597. N_("additionally quote characters from STRING"), GRID+1 },
  598. {"no-quote-chars", NO_QUOTE_CHARS_OPTION, N_("STRING"), 0,
  599. N_("disable quoting for characters from STRING"), GRID+1 },
  600. #undef GRID
  601. #define GRID 110
  602. {NULL, 0, NULL, 0,
  603. N_("Compatibility options:"), GRID },
  604. {NULL, 'o', 0, 0,
  605. N_("when creating, same as --old-archive; when extracting, same as --no-same-owner"), GRID+1 },
  606. #undef GRID
  607. #define GRID 120
  608. {NULL, 0, NULL, 0,
  609. N_("Other options:"), GRID },
  610. {"restrict", RESTRICT_OPTION, 0, 0,
  611. N_("disable use of some potentially harmful options"), -1 },
  612. {"help", '?', 0, 0, N_("give this help list"), -1},
  613. {"usage", USAGE_OPTION, 0, 0, N_("give a short usage message"), -1},
  614. {"version", VERSION_OPTION, 0, 0, N_("print program version"), -1},
  615. /* FIXME -V (--label) conflicts with the default short option for
  616. --version */
  617. {"HANG", HANG_OPTION, "SECS", OPTION_ARG_OPTIONAL | OPTION_HIDDEN,
  618. N_("hang for SECS seconds (default 3600)"), 0},
  619. #undef GRID
  620. {0, 0, 0, 0, 0, 0}
  621. };
  622. static char const *const atime_preserve_args[] =
  623. {
  624. "replace", "system", NULL
  625. };
  626. static enum atime_preserve const atime_preserve_types[] =
  627. {
  628. replace_atime_preserve, system_atime_preserve
  629. };
  630. ARGMATCH_VERIFY (atime_preserve_args, atime_preserve_types);
  631. /* Wildcard matching settings */
  632. enum wildcards
  633. {
  634. default_wildcards, /* For exclusion == enable_wildcards,
  635. for inclusion == disable_wildcards */
  636. disable_wildcards,
  637. enable_wildcards
  638. };
  639. struct tar_args /* Variables used during option parsing */
  640. {
  641. char const *textual_date_option; /* Keeps the argument to --newer-mtime
  642. option if it represents a textual date */
  643. enum wildcards wildcards; /* Wildcard settings (--wildcards/
  644. --no-wildcards) */
  645. int matching_flags; /* exclude_fnmatch options */
  646. int include_anchored; /* Pattern anchoring options used for
  647. file inclusion */
  648. bool o_option; /* True if -o option was given */
  649. bool pax_option; /* True if --pax-option was given */
  650. char const *backup_suffix_string; /* --suffix option argument */
  651. char const *version_control_string; /* --backup option argument */
  652. bool input_files; /* True if some input files where given */
  653. };
  654. #define MAKE_EXCL_OPTIONS(args) \
  655. ((((args)->wildcards != disable_wildcards) ? EXCLUDE_WILDCARDS : 0) \
  656. | (args)->matching_flags \
  657. | recursion_option)
  658. #define MAKE_INCL_OPTIONS(args) \
  659. ((((args)->wildcards == enable_wildcards) ? EXCLUDE_WILDCARDS : 0) \
  660. | (args)->include_anchored \
  661. | (args)->matching_flags \
  662. | recursion_option)
  663. static void
  664. show_default_settings (FILE *stream)
  665. {
  666. fprintf (stream,
  667. "--format=%s -f%s -b%d --quoting-style=%s --rmt-command=%s",
  668. archive_format_string (DEFAULT_ARCHIVE_FORMAT),
  669. DEFAULT_ARCHIVE, DEFAULT_BLOCKING,
  670. quoting_style_args[DEFAULT_QUOTING_STYLE],
  671. DEFAULT_RMT_COMMAND);
  672. #ifdef REMOTE_SHELL
  673. fprintf (stream, " --rsh-command=%s", REMOTE_SHELL);
  674. #endif
  675. fprintf (stream, "\n");
  676. }
  677. static void
  678. set_subcommand_option (enum subcommand subcommand)
  679. {
  680. if (subcommand_option != UNKNOWN_SUBCOMMAND
  681. && subcommand_option != subcommand)
  682. USAGE_ERROR ((0, 0,
  683. _("You may not specify more than one `-Acdtrux' option")));
  684. subcommand_option = subcommand;
  685. }
  686. static void
  687. set_use_compress_program_option (const char *string)
  688. {
  689. if (use_compress_program_option
  690. && strcmp (use_compress_program_option, string) != 0)
  691. USAGE_ERROR ((0, 0, _("Conflicting compression options")));
  692. use_compress_program_option = string;
  693. }
  694. static volatile int _argp_hang;
  695. enum read_file_list_state /* Result of reading file name from the list file */
  696. {
  697. file_list_success, /* OK, name read successfully */
  698. file_list_end, /* End of list file */
  699. file_list_zero /* Zero separator encountered where it should not */
  700. };
  701. /* Read from FP a sequence of characters up to FILENAME_TERMINATOR and put them
  702. into STK.
  703. */
  704. static enum read_file_list_state
  705. read_name_from_file (FILE *fp, struct obstack *stk)
  706. {
  707. int c;
  708. size_t counter = 0;
  709. for (c = getc (fp); c != EOF && c != filename_terminator; c = getc (fp))
  710. {
  711. if (c == 0)
  712. {
  713. /* We have read a zero separator. The file possibly is
  714. zero-separated */
  715. /* FATAL_ERROR((0, 0, N_("file name contains null character"))); */
  716. return file_list_zero;
  717. }
  718. obstack_1grow (stk, c);
  719. counter++;
  720. }
  721. obstack_1grow (stk, 0);
  722. return (counter == 0 && c == EOF) ? file_list_end : file_list_success;
  723. }
  724. static bool files_from_option; /* When set, tar will not refuse to create
  725. empty archives */
  726. static struct obstack argv_stk; /* Storage for additional command line options
  727. read using -T option */
  728. /* Prevent recursive inclusion of the same file */
  729. struct file_id_list
  730. {
  731. struct file_id_list *next;
  732. ino_t ino;
  733. dev_t dev;
  734. };
  735. static struct file_id_list *file_id_list;
  736. static void
  737. add_file_id (const char *filename)
  738. {
  739. struct file_id_list *p;
  740. struct stat st;
  741. if (stat (filename, &st))
  742. stat_fatal (filename);
  743. for (p = file_id_list; p; p = p->next)
  744. if (p->ino == st.st_ino && p->dev == st.st_dev)
  745. {
  746. FATAL_ERROR ((0, 0, _("%s: file list already read"),
  747. quotearg_colon (filename)));
  748. }
  749. p = xmalloc (sizeof *p);
  750. p->next = file_id_list;
  751. p->ino = st.st_ino;
  752. p->dev = st.st_dev;
  753. file_id_list = p;
  754. }
  755. /* Default density numbers for [0-9][lmh] device specifications */
  756. #ifndef LOW_DENSITY_NUM
  757. # define LOW_DENSITY_NUM 0
  758. #endif
  759. #ifndef MID_DENSITY_NUM
  760. # define MID_DENSITY_NUM 8
  761. #endif
  762. #ifndef HIGH_DENSITY_NUM
  763. # define HIGH_DENSITY_NUM 16
  764. #endif
  765. static void
  766. update_argv (const char *filename, struct argp_state *state)
  767. {
  768. FILE *fp;
  769. size_t count = 0, i;
  770. char *start, *p;
  771. char **new_argv;
  772. size_t new_argc;
  773. bool is_stdin = false;
  774. enum read_file_list_state read_state;
  775. if (!strcmp (filename, "-"))
  776. {
  777. is_stdin = true;
  778. request_stdin ("-T");
  779. fp = stdin;
  780. }
  781. else
  782. {
  783. add_file_id (filename);
  784. if ((fp = fopen (filename, "r")) == NULL)
  785. open_fatal (filename);
  786. }
  787. while ((read_state = read_name_from_file (fp, &argv_stk)) == file_list_success)
  788. count++;
  789. if (read_state == file_list_zero)
  790. {
  791. size_t size;
  792. WARN ((0, 0, N_("%s: file name read contains nul character"),
  793. quotearg_colon (filename)));
  794. /* Prepare new stack contents */
  795. size = obstack_object_size (&argv_stk);
  796. p = obstack_finish (&argv_stk);
  797. for (; size > 0; size--, p++)
  798. if (*p)
  799. obstack_1grow (&argv_stk, *p);
  800. else
  801. obstack_1grow (&argv_stk, '\n');
  802. obstack_1grow (&argv_stk, 0);
  803. count = 1;
  804. /* Read rest of files using new filename terminator */
  805. filename_terminator = 0;
  806. while (read_name_from_file (fp, &argv_stk) == file_list_success)
  807. count++;
  808. }
  809. if (!is_stdin)
  810. fclose (fp);
  811. if (count == 0)
  812. return;
  813. start = obstack_finish (&argv_stk);
  814. if (filename_terminator == 0)
  815. for (p = start; *p; p += strlen (p) + 1)
  816. if (p[0] == '-')
  817. count++;
  818. new_argc = state->argc + count;
  819. new_argv = xmalloc (sizeof (state->argv[0]) * (new_argc + 1));
  820. memcpy (new_argv, state->argv, sizeof (state->argv[0]) * (state->argc + 1));
  821. state->argv = new_argv;
  822. memmove (&state->argv[state->next + count], &state->argv[state->next],
  823. (state->argc - state->next + 1) * sizeof (state->argv[0]));
  824. state->argc = new_argc;
  825. for (i = state->next, p = start; *p; p += strlen (p) + 1, i++)
  826. {
  827. if (filename_terminator == 0 && p[0] == '-')
  828. state->argv[i++] = "--add-file";
  829. state->argv[i] = p;
  830. }
  831. }
  832. static error_t
  833. parse_opt (int key, char *arg, struct argp_state *state)
  834. {
  835. struct tar_args *args = state->input;
  836. switch (key)
  837. {
  838. case ARGP_KEY_ARG:
  839. /* File name or non-parsed option, because of ARGP_IN_ORDER */
  840. name_add_name (arg, MAKE_INCL_OPTIONS (args));
  841. args->input_files = true;
  842. break;
  843. case 'A':
  844. set_subcommand_option (CAT_SUBCOMMAND);
  845. break;
  846. case 'b':
  847. {
  848. uintmax_t u;
  849. if (! (xstrtoumax (arg, 0, 10, &u, "") == LONGINT_OK
  850. && u == (blocking_factor = u)
  851. && 0 < blocking_factor
  852. && u == (record_size = u * BLOCKSIZE) / BLOCKSIZE))
  853. USAGE_ERROR ((0, 0, "%s: %s", quotearg_colon (arg),
  854. _("Invalid blocking factor")));
  855. }
  856. break;
  857. case 'B':
  858. /* Try to reblock input records. For reading 4.2BSD pipes. */
  859. /* It would surely make sense to exchange -B and -R, but it seems
  860. that -B has been used for a long while in Sun tar and most
  861. BSD-derived systems. This is a consequence of the block/record
  862. terminology confusion. */
  863. read_full_records_option = true;
  864. break;
  865. case 'c':
  866. set_subcommand_option (CREATE_SUBCOMMAND);
  867. break;
  868. case 'C':
  869. name_add_dir (arg);
  870. break;
  871. case 'd':
  872. set_subcommand_option (DIFF_SUBCOMMAND);
  873. break;
  874. case 'f':
  875. if (archive_names == allocated_archive_names)
  876. archive_name_array = x2nrealloc (archive_name_array,
  877. &allocated_archive_names,
  878. sizeof (archive_name_array[0]));
  879. archive_name_array[archive_names++] = arg;
  880. break;
  881. case 'F':
  882. /* Since -F is only useful with -M, make it implied. Run this
  883. script at the end of each tape. */
  884. info_script_option = arg;
  885. multi_volume_option = true;
  886. break;
  887. case 'g':
  888. listed_incremental_option = arg;
  889. after_date_option = true;
  890. /* Fall through. */
  891. case 'G':
  892. /* We are making an incremental dump (FIXME: are we?); save
  893. directories at the beginning of the archive, and include in each
  894. directory its contents. */
  895. incremental_option = true;
  896. break;
  897. case 'h':
  898. /* Follow symbolic links. */
  899. dereference_option = true;
  900. break;
  901. case 'i':
  902. /* Ignore zero blocks (eofs). This can't be the default,
  903. because Unix tar writes two blocks of zeros, then pads out
  904. the record with garbage. */
  905. ignore_zeros_option = true;
  906. break;
  907. case 'I':
  908. USAGE_ERROR ((0, 0,
  909. _("Warning: the -I option is not supported;"
  910. " perhaps you meant -j or -T?")));
  911. break;
  912. case 'j':
  913. set_use_compress_program_option ("bzip2");
  914. break;
  915. case 'k':
  916. /* Don't replace existing files. */
  917. old_files_option = KEEP_OLD_FILES;
  918. break;
  919. case 'K':
  920. starting_file_option = true;
  921. addname (arg, 0);
  922. break;
  923. case ONE_FILE_SYSTEM_OPTION:
  924. /* When dumping directories, don't dump files/subdirectories
  925. that are on other filesystems. */
  926. one_file_system_option = true;
  927. break;
  928. case 'l':
  929. check_links_option = 1;
  930. break;
  931. case 'L':
  932. {
  933. uintmax_t u;
  934. if (xstrtoumax (arg, 0, 10, &u, "") != LONGINT_OK)
  935. USAGE_ERROR ((0, 0, "%s: %s", quotearg_colon (arg),
  936. _("Invalid tape length")));
  937. tape_length_option = 1024 * (tarlong) u;
  938. multi_volume_option = true;
  939. }
  940. break;
  941. case 'm':
  942. touch_option = true;
  943. break;
  944. case 'M':
  945. /* Make multivolume archive: when we can't write any more into
  946. the archive, re-open it, and continue writing. */
  947. multi_volume_option = true;
  948. break;
  949. case 'n':
  950. seekable_archive = true;
  951. break;
  952. case 'N':
  953. after_date_option = true;
  954. /* Fall through. */
  955. case NEWER_MTIME_OPTION:
  956. if (NEWER_OPTION_INITIALIZED (newer_mtime_option))
  957. USAGE_ERROR ((0, 0, _("More than one threshold date")));
  958. if (FILE_SYSTEM_PREFIX_LEN (arg) != 0
  959. || ISSLASH (*arg)
  960. || *arg == '.')
  961. {
  962. struct stat st;
  963. if (deref_stat (dereference_option, arg, &st) != 0)
  964. {
  965. stat_error (arg);
  966. USAGE_ERROR ((0, 0, _("Date sample file not found")));
  967. }
  968. newer_mtime_option = get_stat_mtime (&st);
  969. }
  970. else
  971. {
  972. if (! get_date (&newer_mtime_option, arg, NULL))
  973. {
  974. WARN ((0, 0, _("Substituting %s for unknown date format %s"),
  975. tartime (newer_mtime_option, false), quote (arg)));
  976. newer_mtime_option.tv_nsec = 0;
  977. }
  978. else
  979. args->textual_date_option = arg;
  980. }
  981. break;
  982. case 'o':
  983. args->o_option = true;
  984. break;
  985. case 'O':
  986. to_stdout_option = true;
  987. break;
  988. case 'p':
  989. same_permissions_option = true;
  990. break;
  991. case 'P':
  992. absolute_names_option = true;
  993. break;
  994. case 'r':
  995. set_subcommand_option (APPEND_SUBCOMMAND);
  996. break;
  997. case 'R':
  998. /* Print block numbers for debugging bad tar archives. */
  999. /* It would surely make sense to exchange -B and -R, but it seems
  1000. that -B has been used for a long while in Sun tar ans most
  1001. BSD-derived systems. This is a consequence of the block/record
  1002. terminology confusion. */
  1003. block_number_option = true;
  1004. break;
  1005. case 's':
  1006. /* Names to extr are sorted. */
  1007. same_order_option = true;
  1008. break;
  1009. case 'S':
  1010. sparse_option = true;
  1011. break;
  1012. case 't':
  1013. set_subcommand_option (LIST_SUBCOMMAND);
  1014. verbose_option++;
  1015. break;
  1016. case TEST_LABEL_OPTION:
  1017. set_subcommand_option (LIST_SUBCOMMAND);
  1018. test_label_option = true;
  1019. break;
  1020. case 'T':
  1021. update_argv (arg, state);
  1022. /* Indicate we've been given -T option. This is for backward
  1023. compatibility only, so that `tar cfT archive /dev/null will
  1024. succeed */
  1025. files_from_option = true;
  1026. break;
  1027. case 'u':
  1028. set_subcommand_option (UPDATE_SUBCOMMAND);
  1029. break;
  1030. case 'U':
  1031. old_files_option = UNLINK_FIRST_OLD_FILES;
  1032. break;
  1033. case UTC_OPTION:
  1034. utc_option = true;
  1035. break;
  1036. case 'v':
  1037. verbose_option++;
  1038. break;
  1039. case 'V':
  1040. volume_label_option = arg;
  1041. break;
  1042. case 'w':
  1043. interactive_option = true;
  1044. break;
  1045. case 'W':
  1046. verify_option = true;
  1047. break;
  1048. case 'x':
  1049. set_subcommand_option (EXTRACT_SUBCOMMAND);
  1050. break;
  1051. case 'X':
  1052. if (add_exclude_file (add_exclude, excluded, arg,
  1053. MAKE_EXCL_OPTIONS (args), '\n')
  1054. != 0)
  1055. {
  1056. int e = errno;
  1057. FATAL_ERROR ((0, e, "%s", quotearg_colon (arg)));
  1058. }
  1059. break;
  1060. case 'z':
  1061. set_use_compress_program_option ("gzip");
  1062. break;
  1063. case 'Z':
  1064. set_use_compress_program_option ("compress");
  1065. break;
  1066. case ANCHORED_OPTION:
  1067. args->matching_flags |= EXCLUDE_ANCHORED;
  1068. break;
  1069. case ATIME_PRESERVE_OPTION:
  1070. atime_preserve_option =
  1071. (arg
  1072. ? XARGMATCH ("--atime-preserve", arg,
  1073. atime_preserve_args, atime_preserve_types)
  1074. : replace_atime_preserve);
  1075. if (! O_NOATIME && atime_preserve_option == system_atime_preserve)
  1076. FATAL_ERROR ((0, 0,
  1077. _("--atime-preserve='system' is not supported"
  1078. " on this platform")));
  1079. break;
  1080. case CHECKPOINT_OPTION:
  1081. checkpoint_option = true;
  1082. break;
  1083. case BACKUP_OPTION:
  1084. backup_option = true;
  1085. if (arg)
  1086. args->version_control_string = arg;
  1087. break;
  1088. case DELAY_DIRECTORY_RESTORE_OPTION:
  1089. delay_directory_restore_option = true;
  1090. break;
  1091. case NO_DELAY_DIRECTORY_RESTORE_OPTION:
  1092. delay_directory_restore_option = false;
  1093. break;
  1094. case DELETE_OPTION:
  1095. set_subcommand_option (DELETE_SUBCOMMAND);
  1096. break;
  1097. case EXCLUDE_OPTION:
  1098. add_exclude (excluded, arg, MAKE_EXCL_OPTIONS (args));
  1099. break;
  1100. case EXCLUDE_CACHES_OPTION:
  1101. exclude_caches_option = true;
  1102. break;
  1103. case FORCE_LOCAL_OPTION:
  1104. force_local_option = true;
  1105. break;
  1106. case 'H':
  1107. set_archive_format (arg);
  1108. break;
  1109. case INDEX_FILE_OPTION:
  1110. index_file_name = arg;
  1111. break;
  1112. case IGNORE_CASE_OPTION:
  1113. args->matching_flags |= FNM_CASEFOLD;
  1114. break;
  1115. case IGNORE_COMMAND_ERROR_OPTION:
  1116. ignore_command_error_option = true;
  1117. break;
  1118. case IGNORE_FAILED_READ_OPTION:
  1119. ignore_failed_read_option = true;
  1120. break;
  1121. case KEEP_NEWER_FILES_OPTION:
  1122. old_files_option = KEEP_NEWER_FILES;
  1123. break;
  1124. case GROUP_OPTION:
  1125. if (! (strlen (arg) < GNAME_FIELD_SIZE
  1126. && gname_to_gid (arg, &group_option)))
  1127. {
  1128. uintmax_t g;
  1129. if (xstrtoumax (arg, 0, 10, &g, "") == LONGINT_OK
  1130. && g == (gid_t) g)
  1131. group_option = g;
  1132. else
  1133. FATAL_ERROR ((0, 0, "%s: %s", quotearg_colon (arg),
  1134. _("%s: Invalid group")));
  1135. }
  1136. break;
  1137. case MODE_OPTION:
  1138. mode_option = mode_compile (arg);
  1139. if (!mode_option)
  1140. FATAL_ERROR ((0, 0, _("Invalid mode given on option")));
  1141. initial_umask = umask (0);
  1142. umask (initial_umask);
  1143. break;
  1144. case NO_ANCHORED_OPTION:
  1145. args->include_anchored = 0; /* Clear the default for comman line args */
  1146. args->matching_flags &= ~ EXCLUDE_ANCHORED;
  1147. break;
  1148. case NO_IGNORE_CASE_OPTION:
  1149. args->matching_flags &= ~ FNM_CASEFOLD;
  1150. break;
  1151. case NO_IGNORE_COMMAND_ERROR_OPTION:
  1152. ignore_command_error_option = false;
  1153. break;
  1154. case NO_OVERWRITE_DIR_OPTION:
  1155. old_files_option = NO_OVERWRITE_DIR_OLD_FILES;
  1156. break;
  1157. case NO_QUOTE_CHARS_OPTION:
  1158. for (;*arg; arg++)
  1159. set_char_quoting (NULL, *arg, 0);
  1160. break;
  1161. case NO_WILDCARDS_OPTION:
  1162. args->wildcards = disable_wildcards;
  1163. break;
  1164. case NO_WILDCARDS_MATCH_SLASH_OPTION:
  1165. args->matching_flags |= FNM_FILE_NAME;
  1166. break;
  1167. case NULL_OPTION:
  1168. filename_terminator = '\0';
  1169. break;
  1170. case NUMERIC_OWNER_OPTION:
  1171. numeric_owner_option = true;
  1172. break;
  1173. case OCCURRENCE_OPTION:
  1174. if (!arg)
  1175. occurrence_option = 1;
  1176. else
  1177. {
  1178. uintmax_t u;
  1179. if (xstrtoumax (arg, 0, 10, &u, "") == LONGINT_OK)
  1180. occurrence_option = u;
  1181. else
  1182. FATAL_ERROR ((0, 0, "%s: %s", quotearg_colon (arg),
  1183. _("Invalid number")));
  1184. }
  1185. break;
  1186. case OVERWRITE_OPTION:
  1187. old_files_option = OVERWRITE_OLD_FILES;
  1188. break;
  1189. case OWNER_OPTION:
  1190. if (! (strlen (arg) < UNAME_FIELD_SIZE
  1191. && uname_to_uid (arg, &owner_option)))
  1192. {
  1193. uintmax_t u;
  1194. if (xstrtoumax (arg, 0, 10, &u, "") == LONGINT_OK
  1195. && u == (uid_t) u)
  1196. owner_option = u;
  1197. else
  1198. FATAL_ERROR ((0, 0, "%s: %s", quotearg_colon (arg),
  1199. _("Invalid owner")));
  1200. }
  1201. break;
  1202. case QUOTE_CHARS_OPTION:
  1203. for (;*arg; arg++)
  1204. set_char_quoting (NULL, *arg, 1);
  1205. break;
  1206. case QUOTING_STYLE_OPTION:
  1207. tar_set_quoting_style (arg);
  1208. break;
  1209. case PAX_OPTION:
  1210. args->pax_option = true;
  1211. xheader_set_option (arg);
  1212. break;
  1213. case POSIX_OPTION:
  1214. set_archive_format ("posix");
  1215. break;
  1216. case PRESERVE_OPTION:
  1217. /* FIXME: What it is good for? */
  1218. same_permissions_option = true;
  1219. same_order_option = true;
  1220. break;
  1221. case RECORD_SIZE_OPTION:
  1222. {
  1223. uintmax_t u;
  1224. if (! (xstrtoumax (arg, 0, 10, &u, "") == LONGINT_OK
  1225. && u == (size_t) u))
  1226. USAGE_ERROR ((0, 0, "%s: %s", quotearg_colon (arg),
  1227. _("Invalid record size")));
  1228. record_size = u;
  1229. if (record_size % BLOCKSIZE != 0)
  1230. USAGE_ERROR ((0, 0, _("Record size must be a multiple of %d."),
  1231. BLOCKSIZE));
  1232. blocking_factor = record_size / BLOCKSIZE;
  1233. }
  1234. break;
  1235. case RECURSIVE_UNLINK_OPTION:
  1236. recursive_unlink_option = true;
  1237. break;
  1238. case REMOVE_FILES_OPTION:
  1239. remove_files_option = true;
  1240. break;
  1241. case RESTRICT_OPTION:
  1242. restrict_option = true;
  1243. break;
  1244. case RMT_COMMAND_OPTION:
  1245. rmt_command = arg;
  1246. break;
  1247. case RSH_COMMAND_OPTION:
  1248. rsh_command_option = arg;
  1249. break;
  1250. case SHOW_DEFAULTS_OPTION:
  1251. show_default_settings (stdout);
  1252. close_stdout ();
  1253. exit (0);
  1254. case STRIP_COMPONENTS_OPTION:
  1255. {
  1256. uintmax_t u;
  1257. if (! (xstrtoumax (arg, 0, 10, &u, "") == LONGINT_OK
  1258. && u == (size_t) u))
  1259. USAGE_ERROR ((0, 0, "%s: %s", quotearg_colon (arg),
  1260. _("Invalid number of elements")));
  1261. strip_name_components = u;
  1262. }
  1263. break;
  1264. case SHOW_OMITTED_DIRS_OPTION:
  1265. show_omitted_dirs_option = true;
  1266. break;
  1267. case SHOW_TRANSFORMED_NAMES_OPTION:
  1268. show_transformed_names_option = true;
  1269. break;
  1270. case SUFFIX_OPTION:
  1271. backup_option = true;
  1272. args->backup_suffix_string = arg;
  1273. break;
  1274. case TO_COMMAND_OPTION:
  1275. if (to_command_option)
  1276. USAGE_ERROR ((0, 0, _("Only one --to-command option allowed")));
  1277. to_command_option = arg;
  1278. break;
  1279. case TOTALS_OPTION:
  1280. totals_option = true;
  1281. break;
  1282. case TRANSFORM_OPTION:
  1283. set_transform_expr (arg);
  1284. break;
  1285. case USE_COMPRESS_PROGRAM_OPTION:
  1286. set_use_compress_program_option (arg);
  1287. break;
  1288. case VOLNO_FILE_OPTION:
  1289. volno_file_option = arg;
  1290. break;
  1291. case WILDCARDS_OPTION:
  1292. args->wildcards = enable_wildcards;
  1293. break;
  1294. case WILDCARDS_MATCH_SLASH_OPTION:
  1295. args->matching_flags &= ~ FNM_FILE_NAME;
  1296. break;
  1297. case NO_RECURSION_OPTION:
  1298. recursion_option = 0;
  1299. break;
  1300. case NO_SAME_OWNER_OPTION:
  1301. same_owner_option = -1;
  1302. break;
  1303. case NO_SAME_PERMISSIONS_OPTION:
  1304. same_permissions_option = -1;
  1305. break;
  1306. case RECURSION_OPTION:
  1307. recursion_option = FNM_LEADING_DIR;
  1308. break;
  1309. case SAME_OWNER_OPTION:
  1310. same_owner_option = 1;
  1311. break;
  1312. case UNQUOTE_OPTION:
  1313. unquote_option = true;
  1314. break;
  1315. case NO_UNQUOTE_OPTION:
  1316. unquote_option = false;
  1317. break;
  1318. case '0':
  1319. case '1':
  1320. case '2':
  1321. case '3':
  1322. case '4':
  1323. case '5':
  1324. case '6':
  1325. case '7':
  1326. #ifdef DEVICE_PREFIX
  1327. {
  1328. int device = key - '0';
  1329. int density;
  1330. static char buf[sizeof DEVICE_PREFIX + 10];
  1331. char *cursor;
  1332. if (arg[1])
  1333. argp_error (state, _("Malformed density argument: %s"), quote (arg));
  1334. strcpy (buf, DEVICE_PREFIX);
  1335. cursor = buf + strlen (buf);
  1336. #ifdef DENSITY_LETTER
  1337. sprintf (cursor, "%d%c", device, arg[0]);
  1338. #else /* not DENSITY_LETTER */
  1339. switch (arg[0])
  1340. {
  1341. case 'l':
  1342. device += LOW_DENSITY_NUM;
  1343. break;
  1344. case 'm':
  1345. device += MID_DENSITY_NUM;
  1346. break;
  1347. case 'h':
  1348. device += HIGH_DENSITY_NUM;
  1349. break;
  1350. default:
  1351. argp_error (state, _("Unknown density: `%c'"), arg[0]);
  1352. }
  1353. sprintf (cursor, "%d", device);
  1354. #endif /* not DENSITY_LETTER */
  1355. if (archive_names == allocated_archive_names)
  1356. archive_name_array = x2nrealloc (archive_name_array,
  1357. &allocated_archive_names,
  1358. sizeof (archive_name_array[0]));
  1359. archive_name_array[archive_names++] = xstrdup (buf);
  1360. }
  1361. break;
  1362. #else /* not DEVICE_PREFIX */
  1363. argp_error (state,
  1364. _("Options `-[0-7][lmh]' not supported by *this* tar"));
  1365. #endif /* not DEVICE_PREFIX */
  1366. case '?':
  1367. state->flags |= ARGP_NO_EXIT;
  1368. argp_state_help (state, state->out_stream,
  1369. ARGP_HELP_STD_HELP & ~ARGP_HELP_BUG_ADDR);
  1370. fprintf (state->out_stream, "\n%s\n\n",
  1371. _("Valid arguments for --quoting-style options are:"));
  1372. tar_list_quoting_styles (state->out_stream, " ");
  1373. fprintf (state->out_stream, _("\n*This* tar defaults to:\n"));
  1374. show_default_settings (state->out_stream);
  1375. fprintf (state->out_stream, "\n");
  1376. fprintf (state->out_stream, _("Report bugs to %s.\n"),
  1377. argp_program_bug_address);
  1378. close_stdout ();
  1379. exit (0);
  1380. case USAGE_OPTION:
  1381. argp_state_help (state, state->out_stream, ARGP_HELP_USAGE);
  1382. close_stdout ();
  1383. exit (0);
  1384. case VERSION_OPTION:
  1385. version_etc (state->out_stream, "tar", PACKAGE_NAME, VERSION,
  1386. "John Gilmore", "Jay Fenlason", (char *) NULL);
  1387. close_stdout ();
  1388. exit (0);
  1389. case HANG_OPTION:
  1390. _argp_hang = atoi (arg ? arg : "3600");
  1391. while (_argp_hang-- > 0)
  1392. sleep (1);
  1393. break;
  1394. default:
  1395. return ARGP_ERR_UNKNOWN;
  1396. }
  1397. return 0;
  1398. }
  1399. static struct argp argp = {
  1400. options,
  1401. parse_opt,
  1402. N_("[FILE]..."),
  1403. doc,
  1404. NULL,
  1405. NULL,
  1406. NULL
  1407. };
  1408. void
  1409. usage (int status)
  1410. {
  1411. argp_help (&argp, stderr, ARGP_HELP_SEE, (char*) program_name);
  1412. close_stdout ();
  1413. exit (status);
  1414. }
  1415. /* Parse the options for tar. */
  1416. static struct argp_option *
  1417. find_argp_option (struct argp_option *options, int letter)
  1418. {
  1419. for (;
  1420. !(options->name == NULL
  1421. && options->key == 0
  1422. && options->arg == 0
  1423. && options->flags == 0
  1424. && options->doc == NULL); options++)
  1425. if (options->key == letter)
  1426. return options;
  1427. return NULL;
  1428. }
  1429. static void
  1430. decode_options (int argc, char **argv)
  1431. {
  1432. int index;
  1433. struct tar_args args;
  1434. /* Set some default option values. */
  1435. args.textual_date_option = NULL;
  1436. args.wildcards = default_wildcards;
  1437. args.matching_flags = 0;
  1438. args.include_anchored = EXCLUDE_ANCHORED;
  1439. args.o_option = false;
  1440. args.pax_option = false;
  1441. args.backup_suffix_string = getenv ("SIMPLE_BACKUP_SUFFIX");
  1442. args.version_control_string = 0;
  1443. args.input_files = false;
  1444. subcommand_option = UNKNOWN_SUBCOMMAND;
  1445. archive_format = DEFAULT_FORMAT;
  1446. blocking_factor = DEFAULT_BLOCKING;
  1447. record_size = DEFAULT_BLOCKING * BLOCKSIZE;
  1448. excluded = new_exclude ();
  1449. newer_mtime_option.tv_sec = TYPE_MINIMUM (time_t);
  1450. newer_mtime_option.tv_nsec = -1;
  1451. recursion_option = FNM_LEADING_DIR;
  1452. unquote_option = true;
  1453. owner_option = -1;
  1454. group_option = -1;
  1455. /* Convert old-style tar call by exploding option element and rearranging
  1456. options accordingly. */
  1457. if (argc > 1 && argv[1][0] != '-')
  1458. {
  1459. int new_argc; /* argc value for rearranged arguments */
  1460. char **new_argv; /* argv value for rearranged arguments */
  1461. char *const *in; /* cursor into original argv */
  1462. char **out; /* cursor into rearranged argv */
  1463. const char *letter; /* cursor into old option letters */
  1464. char buffer[3]; /* constructed option buffer */
  1465. /* Initialize a constructed option. */
  1466. buffer[0] = '-';
  1467. buffer[2] = '\0';
  1468. /* Allocate a new argument array, and copy program name in it. */
  1469. new_argc = argc - 1 + strlen (argv[1]);
  1470. new_argv = xmalloc ((new_argc + 1) * sizeof (char *));
  1471. in = argv;
  1472. out = new_argv;
  1473. *out++ = *in++;
  1474. /* Copy each old letter option as a separate option, and have the
  1475. corresponding argument moved next to it. */
  1476. for (letter = *in++; *letter; letter++)
  1477. {
  1478. struct argp_option *opt;
  1479. buffer[1] = *letter;
  1480. *out++ = xstrdup (buffer);
  1481. opt = find_argp_option (options, *letter);
  1482. if (opt && opt->arg)
  1483. {
  1484. if (in < argv + argc)
  1485. *out++ = *in++;
  1486. else
  1487. USAGE_ERROR ((0, 0, _("Old option `%c' requires an argument."),
  1488. *letter));
  1489. }
  1490. }
  1491. /* Copy all remaining options. */
  1492. while (in < argv + argc)
  1493. *out++ = *in++;
  1494. *out = 0;
  1495. /* Replace the old option list by the new one. */
  1496. argc = new_argc;
  1497. argv = new_argv;
  1498. }
  1499. /* Parse all options and non-options as they appear. */
  1500. prepend_default_options (getenv ("TAR_OPTIONS"), &argc, &argv);
  1501. if (argp_parse (&argp, argc, argv, ARGP_IN_ORDER|ARGP_NO_HELP,
  1502. &index, &args))
  1503. exit (TAREXIT_FAILURE);
  1504. /* Special handling for 'o' option:
  1505. GNU tar used to say "output old format".
  1506. UNIX98 tar says don't chown files after extracting (we use
  1507. "--no-same-owner" for this).
  1508. The old GNU tar semantics is retained when used with --create
  1509. option, otherwise UNIX98 semantics is assumed */
  1510. if (args.o_option)
  1511. {
  1512. if (subcommand_option == CREATE_SUBCOMMAND)
  1513. {
  1514. /* GNU Tar <= 1.13 compatibility */
  1515. set_archive_format ("v7");
  1516. }
  1517. else
  1518. {
  1519. /* UNIX98 compatibility */
  1520. same_owner_option = -1;
  1521. }
  1522. }
  1523. /* Handle operands after any "--" argument. */
  1524. for (; index < argc; index++)
  1525. {
  1526. name_add_name (argv[index], MAKE_INCL_OPTIONS (&args));
  1527. args.input_files = true;
  1528. }
  1529. /* Warn about implicit use of the wildcards in command line arguments.
  1530. See TODO */
  1531. warn_regex_usage = args.wildcards == default_wildcards;
  1532. /* Derive option values and check option consistency. */
  1533. if (archive_format == DEFAULT_FORMAT)
  1534. {
  1535. if (args.pax_option)
  1536. archive_format = POSIX_FORMAT;
  1537. else
  1538. archive_format = DEFAULT_ARCHIVE_FORMAT;
  1539. }
  1540. if ((volume_label_option && subcommand_option == CREATE_SUBCOMMAND)
  1541. || incremental_option
  1542. || multi_volume_option
  1543. || sparse_option)
  1544. assert_format (FORMAT_MASK (OLDGNU_FORMAT)
  1545. | FORMAT_MASK (GNU_FORMAT)
  1546. | FORMAT_MASK (POSIX_FORMAT));
  1547. if (multi_volume_option
  1548. && archive_format == POSIX_FORMAT
  1549. && subcommand_option == CREATE_SUBCOMMAND
  1550. && !tape_length_option)
  1551. USAGE_ERROR ((0, 0,
  1552. _("creating multi-volume archives in posix format requires using --tape-length (-L) option")));
  1553. if (occurrence_option)
  1554. {
  1555. if (!args.input_files)
  1556. USAGE_ERROR ((0, 0,
  1557. _("--occurrence is meaningless without a file list")));
  1558. if (subcommand_option != DELETE_SUBCOMMAND
  1559. && subcommand_option != DIFF_SUBCOMMAND
  1560. && subcommand_option != EXTRACT_SUBCOMMAND
  1561. && subcommand_option != LIST_SUBCOMMAND)
  1562. USAGE_ERROR ((0, 0,
  1563. _("--occurrence cannot be used in the requested operation mode")));
  1564. }
  1565. if (seekable_archive && subcommand_option == DELETE_SUBCOMMAND)
  1566. {
  1567. /* The current code in delete.c is based on the assumption that
  1568. skip_member() reads all data from the archive. So, we should
  1569. make sure it won't use seeks. On the other hand, the same code
  1570. depends on the ability to backspace a record in the archive,
  1571. so setting seekable_archive to false is technically incorrect.
  1572. However, it is tested only in skip_member(), so it's not a
  1573. problem. */
  1574. seekable_archive = false;
  1575. }
  1576. if (archive_names == 0)
  1577. {
  1578. /* If no archive file name given, try TAPE from the environment, or
  1579. else, DEFAULT_ARCHIVE from the configuration process. */
  1580. archive_names = 1;
  1581. archive_name_array[0] = getenv ("TAPE");
  1582. if (! archive_name_array[0])
  1583. archive_name_array[0] = DEFAULT_ARCHIVE;
  1584. }
  1585. /* Allow multiple archives only with `-M'. */
  1586. if (archive_names > 1 && !multi_volume_option)
  1587. USAGE_ERROR ((0, 0,
  1588. _("Multiple archive files require `-M' option")));
  1589. if (listed_incremental_option
  1590. && NEWER_OPTION_INITIALIZED (newer_mtime_option))
  1591. USAGE_ERROR ((0, 0,
  1592. _("Cannot combine --listed-incremental with --newer")));
  1593. if (volume_label_option)
  1594. {
  1595. if (archive_format == GNU_FORMAT || archive_format == OLDGNU_FORMAT)
  1596. {
  1597. size_t volume_label_max_len =
  1598. (sizeof current_header->header.name
  1599. - 1 /* for trailing '\0' */
  1600. - (multi_volume_option
  1601. ? (sizeof " Volume "
  1602. - 1 /* for null at end of " Volume " */
  1603. + INT_STRLEN_BOUND (int) /* for volume number */
  1604. - 1 /* for sign, as 0 <= volno */)
  1605. : 0));
  1606. if (volume_label_max_len < strlen (volume_label_option))
  1607. USAGE_ERROR ((0, 0,
  1608. ngettext ("%s: Volume label is too long (limit is %lu byte)",
  1609. "%s: Volume label is too long (limit is %lu bytes)",
  1610. volume_label_max_len),
  1611. quotearg_colon (volume_label_option),
  1612. (unsigned long) volume_label_max_len));
  1613. }
  1614. /* else FIXME
  1615. Label length in PAX format is limited by the volume size. */
  1616. }
  1617. if (verify_option)
  1618. {
  1619. if (multi_volume_option)
  1620. USAGE_ERROR ((0, 0, _("Cannot verify multi-volume archives")));
  1621. if (use_compress_program_option)
  1622. USAGE_ERROR ((0, 0, _("Cannot verify compressed archives")));
  1623. }
  1624. if (use_compress_program_option)
  1625. {
  1626. if (multi_volume_option)
  1627. USAGE_ERROR ((0, 0, _("Cannot use multi-volume compressed archives")));
  1628. if (subcommand_option == UPDATE_SUBCOMMAND
  1629. || subcommand_option == APPEND_SUBCOMMAND
  1630. || subcommand_option == DELETE_SUBCOMMAND)
  1631. USAGE_ERROR ((0, 0, _("Cannot update compressed archives")));
  1632. if (subcommand_option == CAT_SUBCOMMAND)
  1633. USAGE_ERROR ((0, 0, _("Cannot concatenate compressed archives")));
  1634. }
  1635. /* It is no harm to use --pax-option on non-pax archives in archive
  1636. reading mode. It may even be useful, since it allows to override
  1637. file attributes from tar headers. Therefore I allow such usage.
  1638. --gray */
  1639. if (args.pax_option
  1640. && archive_format != POSIX_FORMAT
  1641. && (subcommand_option != EXTRACT_SUBCOMMAND
  1642. || subcommand_option != DIFF_SUBCOMMAND
  1643. || subcommand_option != LIST_SUBCOMMAND))
  1644. USAGE_ERROR ((0, 0, _("--pax-option can be used only on POSIX archives")));
  1645. /* If ready to unlink hierarchies, so we are for simpler files. */
  1646. if (recursive_unlink_option)
  1647. old_files_option = UNLINK_FIRST_OLD_FILES;
  1648. if (test_label_option)
  1649. {
  1650. /* --test-label is silent if the user has specified the label name to
  1651. compare against. */
  1652. if (!args.input_files)
  1653. verbose_option++;
  1654. }
  1655. else if (utc_option)
  1656. verbose_option = 2;
  1657. /* Forbid using -c with no input files whatsoever. Check that `-f -',
  1658. explicit or implied, is used correctly. */
  1659. switch (subcommand_option)
  1660. {
  1661. case CREATE_SUBCOMMAND:
  1662. if (!args.input_files && !files_from_option)
  1663. USAGE_ERROR ((0, 0,
  1664. _("Cowardly refusing to create an empty archive")));
  1665. break;
  1666. case EXTRACT_SUBCOMMAND:
  1667. case LIST_SUBCOMMAND:
  1668. case DIFF_SUBCOMMAND:
  1669. for (archive_name_cursor = archive_name_array;
  1670. archive_name_cursor < archive_name_array + archive_names;
  1671. archive_name_cursor++)
  1672. if (!strcmp (*archive_name_cursor, "-"))
  1673. request_stdin ("-f");
  1674. break;
  1675. case CAT_SUBCOMMAND:
  1676. case UPDATE_SUBCOMMAND:
  1677. case APPEND_SUBCOMMAND:
  1678. for (archive_name_cursor = archive_name_array;
  1679. archive_name_cursor < archive_name_array + archive_names;
  1680. archive_name_cursor++)
  1681. if (!strcmp (*archive_name_cursor, "-"))
  1682. USAGE_ERROR ((0, 0,
  1683. _("Options `-Aru' are incompatible with `-f -'")));
  1684. default:
  1685. break;
  1686. }
  1687. archive_name_cursor = archive_name_array;
  1688. /* Prepare for generating backup names. */
  1689. if (args.backup_suffix_string)
  1690. simple_backup_suffix = xstrdup (args.backup_suffix_string);
  1691. if (backup_option)
  1692. {
  1693. backup_type = xget_version ("--backup", args.version_control_string);
  1694. /* No backup is needed either if explicitely disabled or if
  1695. the extracted files are not being written to disk. */
  1696. if (backup_type == no_backups || EXTRACT_OVER_PIPE)
  1697. backup_option = false;
  1698. }
  1699. if (verbose_option && args.textual_date_option)
  1700. {
  1701. char const *treated_as = tartime (newer_mtime_option, true);
  1702. if (strcmp (args.textual_date_option, treated_as) != 0)
  1703. WARN ((0, 0, _("Treating date `%s' as %s"),
  1704. args.textual_date_option, treated_as));
  1705. }
  1706. }
  1707. /* Tar proper. */
  1708. /* Main routine for tar. */
  1709. int
  1710. main (int argc, char **argv)
  1711. {
  1712. set_start_time ();
  1713. program_name = argv[0];
  1714. setlocale (LC_ALL, "");
  1715. bindtextdomain (PACKAGE, LOCALEDIR);
  1716. textdomain (PACKAGE);
  1717. exit_failure = TAREXIT_FAILURE;
  1718. exit_status = TAREXIT_SUCCESS;
  1719. filename_terminator = '\n';
  1720. set_quoting_style (0, DEFAULT_QUOTING_STYLE);
  1721. /* Make sure we have first three descriptors available */
  1722. stdopen ();
  1723. /* Pre-allocate a few structures. */
  1724. allocated_archive_names = 10;
  1725. archive_name_array =
  1726. xmalloc (sizeof (const char *) * allocated_archive_names);
  1727. archive_names = 0;
  1728. obstack_init (&argv_stk);
  1729. #ifdef SIGCHLD
  1730. /* System V fork+wait does not work if SIGCHLD is ignored. */
  1731. signal (SIGCHLD, SIG_DFL);
  1732. #endif
  1733. /* Decode options. */
  1734. decode_options (argc, argv);
  1735. name_init ();
  1736. /* Main command execution. */
  1737. if (volno_file_option)
  1738. init_volume_number ();
  1739. switch (subcommand_option)
  1740. {
  1741. case UNKNOWN_SUBCOMMAND:
  1742. USAGE_ERROR ((0, 0,
  1743. _("You must specify one of the `-Acdtrux' options")));
  1744. case CAT_SUBCOMMAND:
  1745. case UPDATE_SUBCOMMAND:
  1746. case APPEND_SUBCOMMAND:
  1747. update_archive ();
  1748. break;
  1749. case DELETE_SUBCOMMAND:
  1750. delete_archive_members ();
  1751. break;
  1752. case CREATE_SUBCOMMAND:
  1753. create_archive ();
  1754. if (totals_option)
  1755. print_total_written ();
  1756. break;
  1757. case EXTRACT_SUBCOMMAND:
  1758. extr_init ();
  1759. read_and (extract_archive);
  1760. /* FIXME: should extract_finish () even if an ordinary signal is
  1761. received. */
  1762. extract_finish ();
  1763. break;
  1764. case LIST_SUBCOMMAND:
  1765. read_and (list_archive);
  1766. break;
  1767. case DIFF_SUBCOMMAND:
  1768. diff_init ();
  1769. read_and (diff_archive);
  1770. break;
  1771. }
  1772. if (check_links_option)
  1773. check_links ();
  1774. if (volno_file_option)
  1775. closeout_volume_number ();
  1776. /* Dispose of allocated memory, and return. */
  1777. free (archive_name_array);
  1778. name_term ();
  1779. if (stdlis == stdout)
  1780. close_stdout ();
  1781. if (exit_status == TAREXIT_FAILURE)
  1782. error (0, 0, _("Error exit delayed from previous errors"));
  1783. if (ferror (stderr) || fclose (stderr) != 0)
  1784. exit_status = TAREXIT_FAILURE;
  1785. return exit_status;
  1786. }
  1787. void
  1788. tar_stat_init (struct tar_stat_info *st)
  1789. {
  1790. memset (st, 0, sizeof (*st));
  1791. }
  1792. void
  1793. tar_stat_destroy (struct tar_stat_info *st)
  1794. {
  1795. free (st->orig_file_name);
  1796. free (st->file_name);
  1797. free (st->link_name);
  1798. free (st->uname);
  1799. free (st->gname);
  1800. free (st->sparse_map);
  1801. free (st->dumpdir);
  1802. memset (st, 0, sizeof (*st));
  1803. }
  1804. /* Format mask for all available formats that support nanosecond
  1805. timestamp resolution. */
  1806. #define NS_PRECISION_FORMAT_MASK FORMAT_MASK (POSIX_FORMAT)
  1807. /* Same as timespec_cmp, but ignore nanoseconds if current archive
  1808. format does not provide sufficient resolution. */
  1809. int
  1810. tar_timespec_cmp (struct timespec a, struct timespec b)
  1811. {
  1812. if (!(FORMAT_MASK (current_format) & NS_PRECISION_FORMAT_MASK))
  1813. a.tv_nsec = b.tv_nsec = 0;
  1814. return timespec_cmp (a, b);
  1815. }