4
0

tar.c 76 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907
  1. /* A tar (tape archiver) program.
  2. Copyright 1988, 1992-1997, 1999-2001, 2003-2007, 2012-2013 Free
  3. 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 3, or (at your option) any later
  8. version.
  9. This program is distributed in the hope that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
  12. Public License for more details.
  13. You should have received a copy of the GNU General Public License along
  14. with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. #include <system.h>
  16. #include <fnmatch.h>
  17. #include <argp.h>
  18. #include <argp-namefrob.h>
  19. #include <argp-fmtstream.h>
  20. #include <argp-version-etc.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 <configmake.h>
  34. #include <exitfail.h>
  35. #include <parse-datetime.h>
  36. #include <rmt.h>
  37. #include <rmt-command.h>
  38. #include <prepargs.h>
  39. #include <quotearg.h>
  40. #include <version-etc.h>
  41. #include <xstrtol.h>
  42. #include <stdopen.h>
  43. #include <priv-set.h>
  44. /* Local declarations. */
  45. #ifndef DEFAULT_ARCHIVE_FORMAT
  46. # define DEFAULT_ARCHIVE_FORMAT GNU_FORMAT
  47. #endif
  48. #ifndef DEFAULT_ARCHIVE
  49. # define DEFAULT_ARCHIVE "tar.out"
  50. #endif
  51. #ifndef DEFAULT_BLOCKING
  52. # define DEFAULT_BLOCKING 20
  53. #endif
  54. /* Print a message if not all links are dumped */
  55. static int check_links_option;
  56. /* Number of allocated tape drive names. */
  57. static size_t allocated_archive_names;
  58. /* Miscellaneous. */
  59. /* Name of option using stdin. */
  60. static const char *stdin_used_by;
  61. /* Doesn't return if stdin already requested. */
  62. static void
  63. request_stdin (const char *option)
  64. {
  65. if (stdin_used_by)
  66. USAGE_ERROR ((0, 0, _("Options '-%s' and '-%s' both want standard input"),
  67. stdin_used_by, option));
  68. stdin_used_by = option;
  69. }
  70. extern int rpmatch (char const *response);
  71. /* Returns true if and only if the user typed an affirmative response. */
  72. int
  73. confirm (const char *message_action, const char *message_name)
  74. {
  75. static FILE *confirm_file;
  76. static int confirm_file_EOF;
  77. bool status = false;
  78. if (!confirm_file)
  79. {
  80. if (archive == 0 || stdin_used_by)
  81. {
  82. confirm_file = fopen (TTY_NAME, "r");
  83. if (! confirm_file)
  84. open_fatal (TTY_NAME);
  85. }
  86. else
  87. {
  88. request_stdin ("-w");
  89. confirm_file = stdin;
  90. }
  91. }
  92. fprintf (stdlis, "%s %s?", message_action, quote (message_name));
  93. fflush (stdlis);
  94. if (!confirm_file_EOF)
  95. {
  96. char *response = NULL;
  97. size_t response_size = 0;
  98. if (getline (&response, &response_size, confirm_file) < 0)
  99. confirm_file_EOF = 1;
  100. else
  101. status = rpmatch (response) > 0;
  102. free (response);
  103. }
  104. if (confirm_file_EOF)
  105. {
  106. fputc ('\n', stdlis);
  107. fflush (stdlis);
  108. }
  109. return status;
  110. }
  111. static struct fmttab {
  112. char const *name;
  113. enum archive_format fmt;
  114. } const fmttab[] = {
  115. { "v7", V7_FORMAT },
  116. { "oldgnu", OLDGNU_FORMAT },
  117. { "ustar", USTAR_FORMAT },
  118. { "posix", POSIX_FORMAT },
  119. #if 0 /* not fully supported yet */
  120. { "star", STAR_FORMAT },
  121. #endif
  122. { "gnu", GNU_FORMAT },
  123. { "pax", POSIX_FORMAT }, /* An alias for posix */
  124. { NULL, 0 }
  125. };
  126. static void
  127. set_archive_format (char const *name)
  128. {
  129. struct fmttab const *p;
  130. for (p = fmttab; strcmp (p->name, name) != 0; )
  131. if (! (++p)->name)
  132. USAGE_ERROR ((0, 0, _("%s: Invalid archive format"),
  133. quotearg_colon (name)));
  134. archive_format = p->fmt;
  135. }
  136. const char *
  137. archive_format_string (enum archive_format fmt)
  138. {
  139. struct fmttab const *p;
  140. for (p = fmttab; p->name; p++)
  141. if (p->fmt == fmt)
  142. return p->name;
  143. return "unknown?";
  144. }
  145. #define FORMAT_MASK(n) (1<<(n))
  146. static void
  147. assert_format(unsigned fmt_mask)
  148. {
  149. if ((FORMAT_MASK (archive_format) & fmt_mask) == 0)
  150. USAGE_ERROR ((0, 0,
  151. _("GNU features wanted on incompatible archive format")));
  152. }
  153. const char *
  154. subcommand_string (enum subcommand c)
  155. {
  156. switch (c)
  157. {
  158. case UNKNOWN_SUBCOMMAND:
  159. return "unknown?";
  160. case APPEND_SUBCOMMAND:
  161. return "-r";
  162. case CAT_SUBCOMMAND:
  163. return "-A";
  164. case CREATE_SUBCOMMAND:
  165. return "-c";
  166. case DELETE_SUBCOMMAND:
  167. return "-D";
  168. case DIFF_SUBCOMMAND:
  169. return "-d";
  170. case EXTRACT_SUBCOMMAND:
  171. return "-x";
  172. case LIST_SUBCOMMAND:
  173. return "-t";
  174. case UPDATE_SUBCOMMAND:
  175. return "-u";
  176. case TEST_LABEL_SUBCOMMAND:
  177. return "--test-label";
  178. }
  179. abort ();
  180. }
  181. static void
  182. tar_list_quoting_styles (struct obstack *stk, char const *prefix)
  183. {
  184. int i;
  185. size_t prefixlen = strlen (prefix);
  186. for (i = 0; quoting_style_args[i]; i++)
  187. {
  188. obstack_grow (stk, prefix, prefixlen);
  189. obstack_grow (stk, quoting_style_args[i],
  190. strlen (quoting_style_args[i]));
  191. obstack_1grow (stk, '\n');
  192. }
  193. }
  194. static void
  195. tar_set_quoting_style (char *arg)
  196. {
  197. int i;
  198. for (i = 0; quoting_style_args[i]; i++)
  199. if (strcmp (arg, quoting_style_args[i]) == 0)
  200. {
  201. set_quoting_style (NULL, i);
  202. return;
  203. }
  204. FATAL_ERROR ((0, 0,
  205. _("Unknown quoting style '%s'. Try '%s --quoting-style=help' to get a list."), arg, program_invocation_short_name));
  206. }
  207. /* Options. */
  208. enum
  209. {
  210. ACLS_OPTION = CHAR_MAX + 1,
  211. ANCHORED_OPTION,
  212. ATIME_PRESERVE_OPTION,
  213. BACKUP_OPTION,
  214. CHECK_DEVICE_OPTION,
  215. CHECKPOINT_OPTION,
  216. CHECKPOINT_ACTION_OPTION,
  217. DELAY_DIRECTORY_RESTORE_OPTION,
  218. HARD_DEREFERENCE_OPTION,
  219. DELETE_OPTION,
  220. EXCLUDE_BACKUPS_OPTION,
  221. EXCLUDE_CACHES_OPTION,
  222. EXCLUDE_CACHES_UNDER_OPTION,
  223. EXCLUDE_CACHES_ALL_OPTION,
  224. EXCLUDE_OPTION,
  225. EXCLUDE_TAG_OPTION,
  226. EXCLUDE_TAG_UNDER_OPTION,
  227. EXCLUDE_TAG_ALL_OPTION,
  228. EXCLUDE_VCS_OPTION,
  229. FORCE_LOCAL_OPTION,
  230. FULL_TIME_OPTION,
  231. GROUP_OPTION,
  232. IGNORE_CASE_OPTION,
  233. IGNORE_COMMAND_ERROR_OPTION,
  234. IGNORE_FAILED_READ_OPTION,
  235. INDEX_FILE_OPTION,
  236. KEEP_NEWER_FILES_OPTION,
  237. LEVEL_OPTION,
  238. LZIP_OPTION,
  239. LZMA_OPTION,
  240. LZOP_OPTION,
  241. MODE_OPTION,
  242. MTIME_OPTION,
  243. NEWER_MTIME_OPTION,
  244. NO_ACLS_OPTION,
  245. NO_ANCHORED_OPTION,
  246. NO_AUTO_COMPRESS_OPTION,
  247. NO_CHECK_DEVICE_OPTION,
  248. NO_DELAY_DIRECTORY_RESTORE_OPTION,
  249. NO_IGNORE_CASE_OPTION,
  250. NO_IGNORE_COMMAND_ERROR_OPTION,
  251. NO_NULL_OPTION,
  252. NO_OVERWRITE_DIR_OPTION,
  253. NO_QUOTE_CHARS_OPTION,
  254. NO_RECURSION_OPTION,
  255. NO_SAME_OWNER_OPTION,
  256. NO_SAME_PERMISSIONS_OPTION,
  257. NO_SEEK_OPTION,
  258. NO_SELINUX_CONTEXT_OPTION,
  259. NO_UNQUOTE_OPTION,
  260. NO_WILDCARDS_MATCH_SLASH_OPTION,
  261. NO_WILDCARDS_OPTION,
  262. NO_XATTR_OPTION,
  263. NULL_OPTION,
  264. NUMERIC_OWNER_OPTION,
  265. OCCURRENCE_OPTION,
  266. OLD_ARCHIVE_OPTION,
  267. ONE_FILE_SYSTEM_OPTION,
  268. OVERWRITE_DIR_OPTION,
  269. OVERWRITE_OPTION,
  270. OWNER_OPTION,
  271. PAX_OPTION,
  272. POSIX_OPTION,
  273. PRESERVE_OPTION,
  274. QUOTE_CHARS_OPTION,
  275. QUOTING_STYLE_OPTION,
  276. RECORD_SIZE_OPTION,
  277. RECURSION_OPTION,
  278. RECURSIVE_UNLINK_OPTION,
  279. REMOVE_FILES_OPTION,
  280. RESTRICT_OPTION,
  281. RMT_COMMAND_OPTION,
  282. RSH_COMMAND_OPTION,
  283. SAME_OWNER_OPTION,
  284. SELINUX_CONTEXT_OPTION,
  285. SHOW_DEFAULTS_OPTION,
  286. SHOW_OMITTED_DIRS_OPTION,
  287. SHOW_TRANSFORMED_NAMES_OPTION,
  288. SKIP_OLD_FILES_OPTION,
  289. SPARSE_VERSION_OPTION,
  290. STRIP_COMPONENTS_OPTION,
  291. SUFFIX_OPTION,
  292. TEST_LABEL_OPTION,
  293. TOTALS_OPTION,
  294. TO_COMMAND_OPTION,
  295. TRANSFORM_OPTION,
  296. UNQUOTE_OPTION,
  297. UTC_OPTION,
  298. VOLNO_FILE_OPTION,
  299. WARNING_OPTION,
  300. WILDCARDS_MATCH_SLASH_OPTION,
  301. WILDCARDS_OPTION,
  302. XATTR_OPTION,
  303. XATTR_EXCLUDE,
  304. XATTR_INCLUDE
  305. };
  306. const char *argp_program_version = "tar (" PACKAGE_NAME ") " VERSION;
  307. const char *argp_program_bug_address = "<" PACKAGE_BUGREPORT ">";
  308. static char const doc[] = N_("\
  309. GNU 'tar' saves many files together into a single tape or disk archive, \
  310. and can restore individual files from the archive.\n\
  311. \n\
  312. Examples:\n\
  313. tar -cf archive.tar foo bar # Create archive.tar from files foo and bar.\n\
  314. tar -tvf archive.tar # List all files in archive.tar verbosely.\n\
  315. tar -xf archive.tar # Extract all files from archive.tar.\n")
  316. "\v"
  317. N_("The backup suffix is '~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.\n\
  318. The version control may be set with --backup or VERSION_CONTROL, values are:\n\n\
  319. none, off never make backups\n\
  320. t, numbered make numbered backups\n\
  321. nil, existing numbered if numbered backups exist, simple otherwise\n\
  322. never, simple always make simple backups\n");
  323. /* NOTE:
  324. Available option letters are DEQY and eqy. Consider the following
  325. assignments:
  326. [For Solaris tar compatibility =/= Is it important at all?]
  327. e exit immediately with a nonzero exit status if unexpected errors occur
  328. E use extended headers (--format=posix)
  329. [q alias for --occurrence=1 =/= this would better be used for quiet?]
  330. y per-file gzip compression
  331. Y per-block gzip compression.
  332. Additionally, the 'n' letter is assigned for option --seek, which
  333. is probably not needed and should be marked as deprecated, so that
  334. -n may become available in the future.
  335. */
  336. static struct argp_option options[] = {
  337. #define GRID 10
  338. {NULL, 0, NULL, 0,
  339. N_("Main operation mode:"), GRID },
  340. {"list", 't', 0, 0,
  341. N_("list the contents of an archive"), GRID+1 },
  342. {"extract", 'x', 0, 0,
  343. N_("extract files from an archive"), GRID+1 },
  344. {"get", 0, 0, OPTION_ALIAS, NULL, GRID+1 },
  345. {"create", 'c', 0, 0,
  346. N_("create a new archive"), GRID+1 },
  347. {"diff", 'd', 0, 0,
  348. N_("find differences between archive and file system"), GRID+1 },
  349. {"compare", 0, 0, OPTION_ALIAS, NULL, GRID+1 },
  350. {"append", 'r', 0, 0,
  351. N_("append files to the end of an archive"), GRID+1 },
  352. {"update", 'u', 0, 0,
  353. N_("only append files newer than copy in archive"), GRID+1 },
  354. {"catenate", 'A', 0, 0,
  355. N_("append tar files to an archive"), GRID+1 },
  356. {"concatenate", 0, 0, OPTION_ALIAS, NULL, GRID+1 },
  357. {"delete", DELETE_OPTION, 0, 0,
  358. N_("delete from the archive (not on mag tapes!)"), GRID+1 },
  359. {"test-label", TEST_LABEL_OPTION, NULL, 0,
  360. N_("test the archive volume label and exit"), GRID+1 },
  361. #undef GRID
  362. #define GRID 20
  363. {NULL, 0, NULL, 0,
  364. N_("Operation modifiers:"), GRID },
  365. {"sparse", 'S', 0, 0,
  366. N_("handle sparse files efficiently"), GRID+1 },
  367. {"sparse-version", SPARSE_VERSION_OPTION, N_("MAJOR[.MINOR]"), 0,
  368. N_("set version of the sparse format to use (implies --sparse)"), GRID+1},
  369. {"incremental", 'G', 0, 0,
  370. N_("handle old GNU-format incremental backup"), GRID+1 },
  371. {"listed-incremental", 'g', N_("FILE"), 0,
  372. N_("handle new GNU-format incremental backup"), GRID+1 },
  373. {"level", LEVEL_OPTION, N_("NUMBER"), 0,
  374. N_("dump level for created listed-incremental archive"), GRID+1 },
  375. {"ignore-failed-read", IGNORE_FAILED_READ_OPTION, 0, 0,
  376. N_("do not exit with nonzero on unreadable files"), GRID+1 },
  377. {"occurrence", OCCURRENCE_OPTION, N_("NUMBER"), OPTION_ARG_OPTIONAL,
  378. N_("process only the NUMBERth occurrence of each file in the archive;"
  379. " this option is valid only in conjunction with one of the subcommands"
  380. " --delete, --diff, --extract or --list and when a list of files"
  381. " is given either on the command line or via the -T option;"
  382. " NUMBER defaults to 1"), GRID+1 },
  383. {"seek", 'n', NULL, 0,
  384. N_("archive is seekable"), GRID+1 },
  385. {"no-seek", NO_SEEK_OPTION, NULL, 0,
  386. N_("archive is not seekable"), GRID+1 },
  387. {"no-check-device", NO_CHECK_DEVICE_OPTION, NULL, 0,
  388. N_("do not check device numbers when creating incremental archives"),
  389. GRID+1 },
  390. {"check-device", CHECK_DEVICE_OPTION, NULL, 0,
  391. N_("check device numbers when creating incremental archives (default)"),
  392. GRID+1 },
  393. #undef GRID
  394. #define GRID 30
  395. {NULL, 0, NULL, 0,
  396. N_("Overwrite control:"), GRID },
  397. {"verify", 'W', 0, 0,
  398. N_("attempt to verify the archive after writing it"), GRID+1 },
  399. {"remove-files", REMOVE_FILES_OPTION, 0, 0,
  400. N_("remove files after adding them to the archive"), GRID+1 },
  401. {"keep-old-files", 'k', 0, 0,
  402. N_("don't replace existing files when extracting, "
  403. "treat them as errors"), GRID+1 },
  404. {"skip-old-files", SKIP_OLD_FILES_OPTION, 0, 0,
  405. N_("don't replace existing files when extracting, silently skip over them"),
  406. GRID+1 },
  407. {"keep-newer-files", KEEP_NEWER_FILES_OPTION, 0, 0,
  408. N_("don't replace existing files that are newer than their archive copies"), GRID+1 },
  409. {"overwrite", OVERWRITE_OPTION, 0, 0,
  410. N_("overwrite existing files when extracting"), GRID+1 },
  411. {"unlink-first", 'U', 0, 0,
  412. N_("remove each file prior to extracting over it"), GRID+1 },
  413. {"recursive-unlink", RECURSIVE_UNLINK_OPTION, 0, 0,
  414. N_("empty hierarchies prior to extracting directory"), GRID+1 },
  415. {"no-overwrite-dir", NO_OVERWRITE_DIR_OPTION, 0, 0,
  416. N_("preserve metadata of existing directories"), GRID+1 },
  417. {"overwrite-dir", OVERWRITE_DIR_OPTION, 0, 0,
  418. N_("overwrite metadata of existing directories when extracting (default)"),
  419. GRID+1 },
  420. #undef GRID
  421. #define GRID 40
  422. {NULL, 0, NULL, 0,
  423. N_("Select output stream:"), GRID },
  424. {"to-stdout", 'O', 0, 0,
  425. N_("extract files to standard output"), GRID+1 },
  426. {"to-command", TO_COMMAND_OPTION, N_("COMMAND"), 0,
  427. N_("pipe extracted files to another program"), GRID+1 },
  428. {"ignore-command-error", IGNORE_COMMAND_ERROR_OPTION, 0, 0,
  429. N_("ignore exit codes of children"), GRID+1 },
  430. {"no-ignore-command-error", NO_IGNORE_COMMAND_ERROR_OPTION, 0, 0,
  431. N_("treat non-zero exit codes of children as error"), GRID+1 },
  432. #undef GRID
  433. #define GRID 50
  434. {NULL, 0, NULL, 0,
  435. N_("Handling of file attributes:"), GRID },
  436. {"owner", OWNER_OPTION, N_("NAME"), 0,
  437. N_("force NAME as owner for added files"), GRID+1 },
  438. {"group", GROUP_OPTION, N_("NAME"), 0,
  439. N_("force NAME as group for added files"), GRID+1 },
  440. {"mtime", MTIME_OPTION, N_("DATE-OR-FILE"), 0,
  441. N_("set mtime for added files from DATE-OR-FILE"), GRID+1 },
  442. {"mode", MODE_OPTION, N_("CHANGES"), 0,
  443. N_("force (symbolic) mode CHANGES for added files"), GRID+1 },
  444. {"atime-preserve", ATIME_PRESERVE_OPTION,
  445. N_("METHOD"), OPTION_ARG_OPTIONAL,
  446. N_("preserve access times on dumped files, either by restoring the times"
  447. " after reading (METHOD='replace'; default) or by not setting the times"
  448. " in the first place (METHOD='system')"), GRID+1 },
  449. {"touch", 'm', 0, 0,
  450. N_("don't extract file modified time"), GRID+1 },
  451. {"same-owner", SAME_OWNER_OPTION, 0, 0,
  452. N_("try extracting files with the same ownership as exists in the archive (default for superuser)"), GRID+1 },
  453. {"no-same-owner", NO_SAME_OWNER_OPTION, 0, 0,
  454. N_("extract files as yourself (default for ordinary users)"), GRID+1 },
  455. {"numeric-owner", NUMERIC_OWNER_OPTION, 0, 0,
  456. N_("always use numbers for user/group names"), GRID+1 },
  457. {"preserve-permissions", 'p', 0, 0,
  458. N_("extract information about file permissions (default for superuser)"),
  459. GRID+1 },
  460. {"same-permissions", 0, 0, OPTION_ALIAS, NULL, GRID+1 },
  461. {"no-same-permissions", NO_SAME_PERMISSIONS_OPTION, 0, 0,
  462. N_("apply the user's umask when extracting permissions from the archive (default for ordinary users)"), GRID+1 },
  463. {"preserve-order", 's', 0, 0,
  464. N_("sort names to extract to match archive"), GRID+1 },
  465. {"same-order", 0, 0, OPTION_ALIAS, NULL, GRID+1 },
  466. {"preserve", PRESERVE_OPTION, 0, 0,
  467. N_("same as both -p and -s"), GRID+1 },
  468. {"delay-directory-restore", DELAY_DIRECTORY_RESTORE_OPTION, 0, 0,
  469. N_("delay setting modification times and permissions of extracted"
  470. " directories until the end of extraction"), GRID+1 },
  471. {"no-delay-directory-restore", NO_DELAY_DIRECTORY_RESTORE_OPTION, 0, 0,
  472. N_("cancel the effect of --delay-directory-restore option"), GRID+1 },
  473. #undef GRID
  474. #define GRID 55
  475. {NULL, 0, NULL, 0,
  476. N_("Handling of extended file attributes:"), GRID },
  477. {"xattrs", XATTR_OPTION, 0, 0,
  478. N_("Enable extended attributes support"), GRID+1 },
  479. {"no-xattrs", NO_XATTR_OPTION, 0, 0,
  480. N_("Disable extended attributes support"), GRID+1 },
  481. {"xattrs-include", XATTR_INCLUDE, N_("MASK"), 0,
  482. N_("specify the include pattern for xattr keys"), GRID+1 },
  483. {"xattrs-exclude", XATTR_EXCLUDE, N_("MASK"), 0,
  484. N_("specify the exclude pattern for xattr keys"), GRID+1 },
  485. {"selinux", SELINUX_CONTEXT_OPTION, 0, 0,
  486. N_("Enable the SELinux context support"), GRID+1 },
  487. {"no-selinux", NO_SELINUX_CONTEXT_OPTION, 0, 0,
  488. N_("Disable the SELinux context support"), GRID+1 },
  489. {"acls", ACLS_OPTION, 0, 0,
  490. N_("Enable the POSIX ACLs support"), GRID+1 },
  491. {"no-acls", NO_ACLS_OPTION, 0, 0,
  492. N_("Disable the POSIX ACLs support"), GRID+1 },
  493. #undef GRID
  494. #define GRID 60
  495. {NULL, 0, NULL, 0,
  496. N_("Device selection and switching:"), GRID },
  497. {"file", 'f', N_("ARCHIVE"), 0,
  498. N_("use archive file or device ARCHIVE"), GRID+1 },
  499. {"force-local", FORCE_LOCAL_OPTION, 0, 0,
  500. N_("archive file is local even if it has a colon"), GRID+1 },
  501. {"rmt-command", RMT_COMMAND_OPTION, N_("COMMAND"), 0,
  502. N_("use given rmt COMMAND instead of rmt"), GRID+1 },
  503. {"rsh-command", RSH_COMMAND_OPTION, N_("COMMAND"), 0,
  504. N_("use remote COMMAND instead of rsh"), GRID+1 },
  505. #ifdef DEVICE_PREFIX
  506. {"-[0-7][lmh]", 0, NULL, OPTION_DOC, /* It is OK, since 'name' will never be
  507. translated */
  508. N_("specify drive and density"), GRID+1 },
  509. #endif
  510. {NULL, '0', NULL, OPTION_HIDDEN, NULL, GRID+1 },
  511. {NULL, '1', NULL, OPTION_HIDDEN, NULL, GRID+1 },
  512. {NULL, '2', NULL, OPTION_HIDDEN, NULL, GRID+1 },
  513. {NULL, '3', NULL, OPTION_HIDDEN, NULL, GRID+1 },
  514. {NULL, '4', NULL, OPTION_HIDDEN, NULL, GRID+1 },
  515. {NULL, '5', NULL, OPTION_HIDDEN, NULL, GRID+1 },
  516. {NULL, '6', NULL, OPTION_HIDDEN, NULL, GRID+1 },
  517. {NULL, '7', NULL, OPTION_HIDDEN, NULL, GRID+1 },
  518. {NULL, '8', NULL, OPTION_HIDDEN, NULL, GRID+1 },
  519. {NULL, '9', NULL, OPTION_HIDDEN, NULL, GRID+1 },
  520. {"multi-volume", 'M', 0, 0,
  521. N_("create/list/extract multi-volume archive"), GRID+1 },
  522. {"tape-length", 'L', N_("NUMBER"), 0,
  523. N_("change tape after writing NUMBER x 1024 bytes"), GRID+1 },
  524. {"info-script", 'F', N_("NAME"), 0,
  525. N_("run script at end of each tape (implies -M)"), GRID+1 },
  526. {"new-volume-script", 0, 0, OPTION_ALIAS, NULL, GRID+1 },
  527. {"volno-file", VOLNO_FILE_OPTION, N_("FILE"), 0,
  528. N_("use/update the volume number in FILE"), GRID+1 },
  529. #undef GRID
  530. #define GRID 70
  531. {NULL, 0, NULL, 0,
  532. N_("Device blocking:"), GRID },
  533. {"blocking-factor", 'b', N_("BLOCKS"), 0,
  534. N_("BLOCKS x 512 bytes per record"), GRID+1 },
  535. {"record-size", RECORD_SIZE_OPTION, N_("NUMBER"), 0,
  536. N_("NUMBER of bytes per record, multiple of 512"), GRID+1 },
  537. {"ignore-zeros", 'i', 0, 0,
  538. N_("ignore zeroed blocks in archive (means EOF)"), GRID+1 },
  539. {"read-full-records", 'B', 0, 0,
  540. N_("reblock as we read (for 4.2BSD pipes)"), GRID+1 },
  541. #undef GRID
  542. #define GRID 80
  543. {NULL, 0, NULL, 0,
  544. N_("Archive format selection:"), GRID },
  545. {"format", 'H', N_("FORMAT"), 0,
  546. N_("create archive of the given format"), GRID+1 },
  547. {NULL, 0, NULL, 0, N_("FORMAT is one of the following:"), GRID+2 },
  548. {" v7", 0, NULL, OPTION_DOC|OPTION_NO_TRANS, N_("old V7 tar format"),
  549. GRID+3 },
  550. {" oldgnu", 0, NULL, OPTION_DOC|OPTION_NO_TRANS,
  551. N_("GNU format as per tar <= 1.12"), GRID+3 },
  552. {" gnu", 0, NULL, OPTION_DOC|OPTION_NO_TRANS,
  553. N_("GNU tar 1.13.x format"), GRID+3 },
  554. {" ustar", 0, NULL, OPTION_DOC|OPTION_NO_TRANS,
  555. N_("POSIX 1003.1-1988 (ustar) format"), GRID+3 },
  556. {" pax", 0, NULL, OPTION_DOC|OPTION_NO_TRANS,
  557. N_("POSIX 1003.1-2001 (pax) format"), GRID+3 },
  558. {" posix", 0, NULL, OPTION_DOC|OPTION_NO_TRANS, N_("same as pax"), GRID+3 },
  559. {"old-archive", OLD_ARCHIVE_OPTION, 0, 0, /* FIXME */
  560. N_("same as --format=v7"), GRID+8 },
  561. {"portability", 0, 0, OPTION_ALIAS, NULL, GRID+8 },
  562. {"posix", POSIX_OPTION, 0, 0,
  563. N_("same as --format=posix"), GRID+8 },
  564. {"pax-option", PAX_OPTION, N_("keyword[[:]=value][,keyword[[:]=value]]..."), 0,
  565. N_("control pax keywords"), GRID+8 },
  566. {"label", 'V', N_("TEXT"), 0,
  567. N_("create archive with volume name TEXT; at list/extract time, use TEXT as a globbing pattern for volume name"), GRID+8 },
  568. #undef GRID
  569. #define GRID 90
  570. {NULL, 0, NULL, 0,
  571. N_("Compression options:"), GRID },
  572. {"auto-compress", 'a', 0, 0,
  573. N_("use archive suffix to determine the compression program"), GRID+1 },
  574. {"no-auto-compress", NO_AUTO_COMPRESS_OPTION, 0, 0,
  575. N_("do not use archive suffix to determine the compression program"),
  576. GRID+1 },
  577. {"use-compress-program", 'I', N_("PROG"), 0,
  578. N_("filter through PROG (must accept -d)"), GRID+1 },
  579. /* Note: docstrings for the options below are generated by tar_help_filter */
  580. {"bzip2", 'j', 0, 0, NULL, GRID+1 },
  581. {"gzip", 'z', 0, 0, NULL, GRID+1 },
  582. {"gunzip", 0, 0, OPTION_ALIAS, NULL, GRID+1 },
  583. {"ungzip", 0, 0, OPTION_ALIAS, NULL, GRID+1 },
  584. {"compress", 'Z', 0, 0, NULL, GRID+1 },
  585. {"uncompress", 0, 0, OPTION_ALIAS, NULL, GRID+1 },
  586. {"lzip", LZIP_OPTION, 0, 0, NULL, GRID+1 },
  587. {"lzma", LZMA_OPTION, 0, 0, NULL, GRID+1 },
  588. {"lzop", LZOP_OPTION, 0, 0, NULL, GRID+1 },
  589. {"xz", 'J', 0, 0, NULL, GRID+1 },
  590. #undef GRID
  591. #define GRID 100
  592. {NULL, 0, NULL, 0,
  593. N_("Local file selection:"), GRID },
  594. {"add-file", ARGP_KEY_ARG, N_("FILE"), 0,
  595. N_("add given FILE to the archive (useful if its name starts with a dash)"), GRID+1 },
  596. {"directory", 'C', N_("DIR"), 0,
  597. N_("change to directory DIR"), GRID+1 },
  598. {"files-from", 'T', N_("FILE"), 0,
  599. N_("get names to extract or create from FILE"), GRID+1 },
  600. {"null", NULL_OPTION, 0, 0,
  601. N_("-T reads null-terminated names, disable -C"), GRID+1 },
  602. {"no-null", NO_NULL_OPTION, 0, 0,
  603. N_("disable the effect of the previous --null option"), GRID+1 },
  604. {"unquote", UNQUOTE_OPTION, 0, 0,
  605. N_("unquote filenames read with -T (default)"), GRID+1 },
  606. {"no-unquote", NO_UNQUOTE_OPTION, 0, 0,
  607. N_("do not unquote filenames read with -T"), GRID+1 },
  608. {"exclude", EXCLUDE_OPTION, N_("PATTERN"), 0,
  609. N_("exclude files, given as a PATTERN"), GRID+1 },
  610. {"exclude-from", 'X', N_("FILE"), 0,
  611. N_("exclude patterns listed in FILE"), GRID+1 },
  612. {"exclude-caches", EXCLUDE_CACHES_OPTION, 0, 0,
  613. N_("exclude contents of directories containing CACHEDIR.TAG, "
  614. "except for the tag file itself"), GRID+1 },
  615. {"exclude-caches-under", EXCLUDE_CACHES_UNDER_OPTION, 0, 0,
  616. N_("exclude everything under directories containing CACHEDIR.TAG"),
  617. GRID+1 },
  618. {"exclude-caches-all", EXCLUDE_CACHES_ALL_OPTION, 0, 0,
  619. N_("exclude directories containing CACHEDIR.TAG"), GRID+1 },
  620. {"exclude-tag", EXCLUDE_TAG_OPTION, N_("FILE"), 0,
  621. N_("exclude contents of directories containing FILE, except"
  622. " for FILE itself"), GRID+1 },
  623. {"exclude-tag-under", EXCLUDE_TAG_UNDER_OPTION, N_("FILE"), 0,
  624. N_("exclude everything under directories containing FILE"), GRID+1 },
  625. {"exclude-tag-all", EXCLUDE_TAG_ALL_OPTION, N_("FILE"), 0,
  626. N_("exclude directories containing FILE"), GRID+1 },
  627. {"exclude-vcs", EXCLUDE_VCS_OPTION, NULL, 0,
  628. N_("exclude version control system directories"), GRID+1 },
  629. {"exclude-backups", EXCLUDE_BACKUPS_OPTION, NULL, 0,
  630. N_("exclude backup and lock files"), GRID+1 },
  631. {"no-recursion", NO_RECURSION_OPTION, 0, 0,
  632. N_("avoid descending automatically in directories"), GRID+1 },
  633. {"one-file-system", ONE_FILE_SYSTEM_OPTION, 0, 0,
  634. N_("stay in local file system when creating archive"), GRID+1 },
  635. {"recursion", RECURSION_OPTION, 0, 0,
  636. N_("recurse into directories (default)"), GRID+1 },
  637. {"absolute-names", 'P', 0, 0,
  638. N_("don't strip leading '/'s from file names"), GRID+1 },
  639. {"dereference", 'h', 0, 0,
  640. N_("follow symlinks; archive and dump the files they point to"), GRID+1 },
  641. {"hard-dereference", HARD_DEREFERENCE_OPTION, 0, 0,
  642. N_("follow hard links; archive and dump the files they refer to"), GRID+1 },
  643. {"starting-file", 'K', N_("MEMBER-NAME"), 0,
  644. N_("begin at member MEMBER-NAME in the archive"), GRID+1 },
  645. {"newer", 'N', N_("DATE-OR-FILE"), 0,
  646. N_("only store files newer than DATE-OR-FILE"), GRID+1 },
  647. {"after-date", 0, 0, OPTION_ALIAS, NULL, GRID+1 },
  648. {"newer-mtime", NEWER_MTIME_OPTION, N_("DATE"), 0,
  649. N_("compare date and time when data changed only"), GRID+1 },
  650. {"backup", BACKUP_OPTION, N_("CONTROL"), OPTION_ARG_OPTIONAL,
  651. N_("backup before removal, choose version CONTROL"), GRID+1 },
  652. {"suffix", SUFFIX_OPTION, N_("STRING"), 0,
  653. N_("backup before removal, override usual suffix ('~' unless overridden by environment variable SIMPLE_BACKUP_SUFFIX)"), GRID+1 },
  654. #undef GRID
  655. #define GRID 110
  656. {NULL, 0, NULL, 0,
  657. N_("File name transformations:"), GRID },
  658. {"strip-components", STRIP_COMPONENTS_OPTION, N_("NUMBER"), 0,
  659. N_("strip NUMBER leading components from file names on extraction"),
  660. GRID+1 },
  661. {"transform", TRANSFORM_OPTION, N_("EXPRESSION"), 0,
  662. N_("use sed replace EXPRESSION to transform file names"), GRID+1 },
  663. {"xform", 0, 0, OPTION_ALIAS, NULL, GRID+1 },
  664. #undef GRID
  665. #define GRID 120
  666. {NULL, 0, NULL, 0,
  667. N_("File name matching options (affect both exclude and include patterns):"),
  668. GRID },
  669. {"ignore-case", IGNORE_CASE_OPTION, 0, 0,
  670. N_("ignore case"), GRID+1 },
  671. {"anchored", ANCHORED_OPTION, 0, 0,
  672. N_("patterns match file name start"), GRID+1 },
  673. {"no-anchored", NO_ANCHORED_OPTION, 0, 0,
  674. N_("patterns match after any '/' (default for exclusion)"), GRID+1 },
  675. {"no-ignore-case", NO_IGNORE_CASE_OPTION, 0, 0,
  676. N_("case sensitive matching (default)"), GRID+1 },
  677. {"wildcards", WILDCARDS_OPTION, 0, 0,
  678. N_("use wildcards (default for exclusion)"), GRID+1 },
  679. {"no-wildcards", NO_WILDCARDS_OPTION, 0, 0,
  680. N_("verbatim string matching"), GRID+1 },
  681. {"no-wildcards-match-slash", NO_WILDCARDS_MATCH_SLASH_OPTION, 0, 0,
  682. N_("wildcards do not match '/'"), GRID+1 },
  683. {"wildcards-match-slash", WILDCARDS_MATCH_SLASH_OPTION, 0, 0,
  684. N_("wildcards match '/' (default for exclusion)"), GRID+1 },
  685. #undef GRID
  686. #define GRID 130
  687. {NULL, 0, NULL, 0,
  688. N_("Informative output:"), GRID },
  689. {"verbose", 'v', 0, 0,
  690. N_("verbosely list files processed"), GRID+1 },
  691. {"warning", WARNING_OPTION, N_("KEYWORD"), 0,
  692. N_("warning control"), GRID+1 },
  693. {"checkpoint", CHECKPOINT_OPTION, N_("NUMBER"), OPTION_ARG_OPTIONAL,
  694. N_("display progress messages every NUMBERth record (default 10)"),
  695. GRID+1 },
  696. {"checkpoint-action", CHECKPOINT_ACTION_OPTION, N_("ACTION"), 0,
  697. N_("execute ACTION on each checkpoint"),
  698. GRID+1 },
  699. {"check-links", 'l', 0, 0,
  700. N_("print a message if not all links are dumped"), GRID+1 },
  701. {"totals", TOTALS_OPTION, N_("SIGNAL"), OPTION_ARG_OPTIONAL,
  702. N_("print total bytes after processing the archive; "
  703. "with an argument - print total bytes when this SIGNAL is delivered; "
  704. "Allowed signals are: SIGHUP, SIGQUIT, SIGINT, SIGUSR1 and SIGUSR2; "
  705. "the names without SIG prefix are also accepted"), GRID+1 },
  706. {"utc", UTC_OPTION, 0, 0,
  707. N_("print file modification times in UTC"), GRID+1 },
  708. {"full-time", FULL_TIME_OPTION, 0, 0,
  709. N_("print file time to its full resolution"), GRID+1 },
  710. {"index-file", INDEX_FILE_OPTION, N_("FILE"), 0,
  711. N_("send verbose output to FILE"), GRID+1 },
  712. {"block-number", 'R', 0, 0,
  713. N_("show block number within archive with each message"), GRID+1 },
  714. {"interactive", 'w', 0, 0,
  715. N_("ask for confirmation for every action"), GRID+1 },
  716. {"confirmation", 0, 0, OPTION_ALIAS, NULL, GRID+1 },
  717. {"show-defaults", SHOW_DEFAULTS_OPTION, 0, 0,
  718. N_("show tar defaults"), GRID+1 },
  719. {"show-omitted-dirs", SHOW_OMITTED_DIRS_OPTION, 0, 0,
  720. N_("when listing or extracting, list each directory that does not match search criteria"), GRID+1 },
  721. {"show-transformed-names", SHOW_TRANSFORMED_NAMES_OPTION, 0, 0,
  722. N_("show file or archive names after transformation"),
  723. GRID+1 },
  724. {"show-stored-names", 0, 0, OPTION_ALIAS, NULL, GRID+1 },
  725. {"quoting-style", QUOTING_STYLE_OPTION, N_("STYLE"), 0,
  726. N_("set name quoting style; see below for valid STYLE values"), GRID+1 },
  727. {"quote-chars", QUOTE_CHARS_OPTION, N_("STRING"), 0,
  728. N_("additionally quote characters from STRING"), GRID+1 },
  729. {"no-quote-chars", NO_QUOTE_CHARS_OPTION, N_("STRING"), 0,
  730. N_("disable quoting for characters from STRING"), GRID+1 },
  731. #undef GRID
  732. #define GRID 140
  733. {NULL, 0, NULL, 0,
  734. N_("Compatibility options:"), GRID },
  735. {NULL, 'o', 0, 0,
  736. N_("when creating, same as --old-archive; when extracting, same as --no-same-owner"), GRID+1 },
  737. #undef GRID
  738. #define GRID 150
  739. {NULL, 0, NULL, 0,
  740. N_("Other options:"), GRID },
  741. {"restrict", RESTRICT_OPTION, 0, 0,
  742. N_("disable use of some potentially harmful options"), -1 },
  743. #undef GRID
  744. {0, 0, 0, 0, 0, 0}
  745. };
  746. static char const *const atime_preserve_args[] =
  747. {
  748. "replace", "system", NULL
  749. };
  750. static enum atime_preserve const atime_preserve_types[] =
  751. {
  752. replace_atime_preserve, system_atime_preserve
  753. };
  754. /* Make sure atime_preserve_types has as much entries as atime_preserve_args
  755. (minus 1 for NULL guard) */
  756. ARGMATCH_VERIFY (atime_preserve_args, atime_preserve_types);
  757. /* Wildcard matching settings */
  758. enum wildcards
  759. {
  760. default_wildcards, /* For exclusion == enable_wildcards,
  761. for inclusion == disable_wildcards */
  762. disable_wildcards,
  763. enable_wildcards
  764. };
  765. struct tar_args /* Variables used during option parsing */
  766. {
  767. struct textual_date *textual_date; /* Keeps the arguments to --newer-mtime
  768. and/or --date option if they are
  769. textual dates */
  770. enum wildcards wildcards; /* Wildcard settings (--wildcards/
  771. --no-wildcards) */
  772. int matching_flags; /* exclude_fnmatch options */
  773. int include_anchored; /* Pattern anchoring options used for
  774. file inclusion */
  775. bool o_option; /* True if -o option was given */
  776. bool pax_option; /* True if --pax-option was given */
  777. char const *backup_suffix_string; /* --suffix option argument */
  778. char const *version_control_string; /* --backup option argument */
  779. bool input_files; /* True if some input files where given */
  780. int compress_autodetect; /* True if compression autodetection should
  781. be attempted when creating archives */
  782. };
  783. #define MAKE_EXCL_OPTIONS(args) \
  784. ((((args)->wildcards != disable_wildcards) ? EXCLUDE_WILDCARDS : 0) \
  785. | (args)->matching_flags \
  786. | recursion_option)
  787. #define MAKE_INCL_OPTIONS(args) \
  788. ((((args)->wildcards == enable_wildcards) ? EXCLUDE_WILDCARDS : 0) \
  789. | (args)->include_anchored \
  790. | (args)->matching_flags \
  791. | recursion_option)
  792. static char const * const vcs_file_table[] = {
  793. /* CVS: */
  794. "CVS",
  795. ".cvsignore",
  796. /* RCS: */
  797. "RCS",
  798. /* SCCS: */
  799. "SCCS",
  800. /* SVN: */
  801. ".svn",
  802. /* git: */
  803. ".git",
  804. ".gitignore",
  805. /* Arch: */
  806. ".arch-ids",
  807. "{arch}",
  808. "=RELEASE-ID",
  809. "=meta-update",
  810. "=update",
  811. /* Bazaar */
  812. ".bzr",
  813. ".bzrignore",
  814. ".bzrtags",
  815. /* Mercurial */
  816. ".hg",
  817. ".hgignore",
  818. ".hgtags",
  819. /* darcs */
  820. "_darcs",
  821. NULL
  822. };
  823. static char const * const backup_file_table[] = {
  824. ".#*",
  825. "*~",
  826. "#*#",
  827. NULL
  828. };
  829. static void
  830. add_exclude_array (char const * const * fv, int opts)
  831. {
  832. int i;
  833. for (i = 0; fv[i]; i++)
  834. add_exclude (excluded, fv[i], opts);
  835. }
  836. static char *
  837. format_default_settings (void)
  838. {
  839. return xasprintf (
  840. "--format=%s -f%s -b%d --quoting-style=%s --rmt-command=%s"
  841. #ifdef REMOTE_SHELL
  842. " --rsh-command=%s"
  843. #endif
  844. ,
  845. archive_format_string (DEFAULT_ARCHIVE_FORMAT),
  846. DEFAULT_ARCHIVE, DEFAULT_BLOCKING,
  847. quoting_style_args[DEFAULT_QUOTING_STYLE],
  848. DEFAULT_RMT_COMMAND
  849. #ifdef REMOTE_SHELL
  850. , REMOTE_SHELL
  851. #endif
  852. );
  853. }
  854. static void
  855. set_subcommand_option (enum subcommand subcommand)
  856. {
  857. if (subcommand_option != UNKNOWN_SUBCOMMAND
  858. && subcommand_option != subcommand)
  859. USAGE_ERROR ((0, 0,
  860. _("You may not specify more than one '-Acdtrux' or '--test-label' option")));
  861. subcommand_option = subcommand;
  862. }
  863. static void
  864. set_use_compress_program_option (const char *string)
  865. {
  866. if (use_compress_program_option
  867. && strcmp (use_compress_program_option, string) != 0)
  868. USAGE_ERROR ((0, 0, _("Conflicting compression options")));
  869. use_compress_program_option = string;
  870. }
  871. static RETSIGTYPE
  872. sigstat (int signo)
  873. {
  874. compute_duration ();
  875. print_total_stats ();
  876. #ifndef HAVE_SIGACTION
  877. signal (signo, sigstat);
  878. #endif
  879. }
  880. static void
  881. stat_on_signal (int signo)
  882. {
  883. #ifdef HAVE_SIGACTION
  884. # ifndef SA_RESTART
  885. # define SA_RESTART 0
  886. # endif
  887. struct sigaction act;
  888. act.sa_handler = sigstat;
  889. sigemptyset (&act.sa_mask);
  890. act.sa_flags = SA_RESTART;
  891. sigaction (signo, &act, NULL);
  892. #else
  893. signal (signo, sigstat);
  894. #endif
  895. }
  896. static void
  897. set_stat_signal (const char *name)
  898. {
  899. static struct sigtab
  900. {
  901. char const *name;
  902. int signo;
  903. } const sigtab[] = {
  904. { "SIGUSR1", SIGUSR1 },
  905. { "USR1", SIGUSR1 },
  906. { "SIGUSR2", SIGUSR2 },
  907. { "USR2", SIGUSR2 },
  908. { "SIGHUP", SIGHUP },
  909. { "HUP", SIGHUP },
  910. { "SIGINT", SIGINT },
  911. { "INT", SIGINT },
  912. { "SIGQUIT", SIGQUIT },
  913. { "QUIT", SIGQUIT }
  914. };
  915. struct sigtab const *p;
  916. for (p = sigtab; p < sigtab + sizeof (sigtab) / sizeof (sigtab[0]); p++)
  917. if (strcmp (p->name, name) == 0)
  918. {
  919. stat_on_signal (p->signo);
  920. return;
  921. }
  922. FATAL_ERROR ((0, 0, _("Unknown signal name: %s"), name));
  923. }
  924. struct textual_date
  925. {
  926. struct textual_date *next;
  927. struct timespec ts;
  928. const char *option;
  929. char *date;
  930. };
  931. static int
  932. get_date_or_file (struct tar_args *args, const char *option,
  933. const char *str, struct timespec *ts)
  934. {
  935. if (FILE_SYSTEM_PREFIX_LEN (str) != 0
  936. || ISSLASH (*str)
  937. || *str == '.')
  938. {
  939. struct stat st;
  940. if (stat (str, &st) != 0)
  941. {
  942. stat_error (str);
  943. USAGE_ERROR ((0, 0, _("Date sample file not found")));
  944. }
  945. *ts = get_stat_mtime (&st);
  946. }
  947. else
  948. {
  949. if (! parse_datetime (ts, str, NULL))
  950. {
  951. WARN ((0, 0, _("Substituting %s for unknown date format %s"),
  952. tartime (*ts, false), quote (str)));
  953. ts->tv_nsec = 0;
  954. return 1;
  955. }
  956. else
  957. {
  958. struct textual_date *p = xmalloc (sizeof (*p));
  959. p->ts = *ts;
  960. p->option = option;
  961. p->date = xstrdup (str);
  962. p->next = args->textual_date;
  963. args->textual_date = p;
  964. }
  965. }
  966. return 0;
  967. }
  968. static void
  969. report_textual_dates (struct tar_args *args)
  970. {
  971. struct textual_date *p;
  972. for (p = args->textual_date; p; )
  973. {
  974. struct textual_date *next = p->next;
  975. if (verbose_option)
  976. {
  977. char const *treated_as = tartime (p->ts, true);
  978. if (strcmp (p->date, treated_as) != 0)
  979. WARN ((0, 0, _("Option %s: Treating date '%s' as %s"),
  980. p->option, p->date, treated_as));
  981. }
  982. free (p->date);
  983. free (p);
  984. p = next;
  985. }
  986. }
  987. /* Either NL or NUL, as decided by the --null option. */
  988. static char filename_terminator;
  989. enum read_file_list_state /* Result of reading file name from the list file */
  990. {
  991. file_list_success, /* OK, name read successfully */
  992. file_list_end, /* End of list file */
  993. file_list_zero, /* Zero separator encountered where it should not */
  994. file_list_skip /* Empty (zero-length) entry encountered, skip it */
  995. };
  996. /* Read from FP a sequence of characters up to TERM and put them
  997. into STK.
  998. */
  999. static enum read_file_list_state
  1000. read_name_from_file (FILE *fp, struct obstack *stk, int term)
  1001. {
  1002. int c;
  1003. size_t counter = 0;
  1004. for (c = getc (fp); c != EOF && c != term; c = getc (fp))
  1005. {
  1006. if (c == 0)
  1007. {
  1008. /* We have read a zero separator. The file possibly is
  1009. zero-separated */
  1010. return file_list_zero;
  1011. }
  1012. obstack_1grow (stk, c);
  1013. counter++;
  1014. }
  1015. if (counter == 0 && c != EOF)
  1016. return file_list_skip;
  1017. obstack_1grow (stk, 0);
  1018. return (counter == 0 && c == EOF) ? file_list_end : file_list_success;
  1019. }
  1020. static bool files_from_option; /* When set, tar will not refuse to create
  1021. empty archives */
  1022. static struct obstack argv_stk; /* Storage for additional command line options
  1023. read using -T option */
  1024. /* Prevent recursive inclusion of the same file */
  1025. struct file_id_list
  1026. {
  1027. struct file_id_list *next;
  1028. ino_t ino;
  1029. dev_t dev;
  1030. };
  1031. static struct file_id_list *file_id_list;
  1032. static void
  1033. add_file_id (const char *filename)
  1034. {
  1035. struct file_id_list *p;
  1036. struct stat st;
  1037. if (stat (filename, &st))
  1038. stat_fatal (filename);
  1039. for (p = file_id_list; p; p = p->next)
  1040. if (p->ino == st.st_ino && p->dev == st.st_dev)
  1041. {
  1042. FATAL_ERROR ((0, 0, _("%s: file list already read"),
  1043. quotearg_colon (filename)));
  1044. }
  1045. p = xmalloc (sizeof *p);
  1046. p->next = file_id_list;
  1047. p->ino = st.st_ino;
  1048. p->dev = st.st_dev;
  1049. file_id_list = p;
  1050. }
  1051. /* Default density numbers for [0-9][lmh] device specifications */
  1052. #if defined DEVICE_PREFIX && !defined DENSITY_LETTER
  1053. # ifndef LOW_DENSITY_NUM
  1054. # define LOW_DENSITY_NUM 0
  1055. # endif
  1056. # ifndef MID_DENSITY_NUM
  1057. # define MID_DENSITY_NUM 8
  1058. # endif
  1059. # ifndef HIGH_DENSITY_NUM
  1060. # define HIGH_DENSITY_NUM 16
  1061. # endif
  1062. #endif
  1063. static void
  1064. update_argv (const char *filename, struct argp_state *state)
  1065. {
  1066. FILE *fp;
  1067. size_t count = 0, i;
  1068. char *start, *p;
  1069. char **new_argv;
  1070. size_t new_argc;
  1071. bool is_stdin = false;
  1072. enum read_file_list_state read_state;
  1073. int term = filename_terminator;
  1074. if (!strcmp (filename, "-"))
  1075. {
  1076. is_stdin = true;
  1077. request_stdin ("-T");
  1078. fp = stdin;
  1079. }
  1080. else
  1081. {
  1082. add_file_id (filename);
  1083. if ((fp = fopen (filename, "r")) == NULL)
  1084. open_fatal (filename);
  1085. }
  1086. while ((read_state = read_name_from_file (fp, &argv_stk, term))
  1087. != file_list_end)
  1088. {
  1089. switch (read_state)
  1090. {
  1091. case file_list_success:
  1092. count++;
  1093. break;
  1094. case file_list_end: /* won't happen, just to pacify gcc */
  1095. break;
  1096. case file_list_zero:
  1097. {
  1098. size_t size;
  1099. WARNOPT (WARN_FILENAME_WITH_NULS,
  1100. (0, 0, N_("%s: file name read contains nul character"),
  1101. quotearg_colon (filename)));
  1102. /* Prepare new stack contents */
  1103. size = obstack_object_size (&argv_stk);
  1104. p = obstack_finish (&argv_stk);
  1105. for (; size > 0; size--, p++)
  1106. if (*p)
  1107. obstack_1grow (&argv_stk, *p);
  1108. else
  1109. obstack_1grow (&argv_stk, '\n');
  1110. obstack_1grow (&argv_stk, 0);
  1111. count = 1;
  1112. /* Read rest of files using new filename terminator */
  1113. term = 0;
  1114. break;
  1115. }
  1116. case file_list_skip:
  1117. break;
  1118. }
  1119. }
  1120. if (!is_stdin)
  1121. fclose (fp);
  1122. if (count == 0)
  1123. return;
  1124. start = obstack_finish (&argv_stk);
  1125. if (term == 0)
  1126. for (p = start; *p; p += strlen (p) + 1)
  1127. if (p[0] == '-')
  1128. count++;
  1129. new_argc = state->argc + count;
  1130. new_argv = xmalloc (sizeof (state->argv[0]) * (new_argc + 1));
  1131. memcpy (new_argv, state->argv, sizeof (state->argv[0]) * (state->argc + 1));
  1132. state->argv = new_argv;
  1133. memmove (&state->argv[state->next + count], &state->argv[state->next],
  1134. (state->argc - state->next + 1) * sizeof (state->argv[0]));
  1135. state->argc = new_argc;
  1136. for (i = state->next, p = start; *p; p += strlen (p) + 1, i++)
  1137. {
  1138. if (term == 0 && p[0] == '-')
  1139. state->argv[i++] = (char *) "--add-file";
  1140. state->argv[i] = p;
  1141. }
  1142. }
  1143. static char *
  1144. tar_help_filter (int key, const char *text, void *input)
  1145. {
  1146. struct obstack stk;
  1147. char *s;
  1148. switch (key)
  1149. {
  1150. default:
  1151. s = (char*) text;
  1152. break;
  1153. case 'j':
  1154. s = xasprintf (_("filter the archive through %s"), BZIP2_PROGRAM);
  1155. break;
  1156. case 'z':
  1157. s = xasprintf (_("filter the archive through %s"), GZIP_PROGRAM);
  1158. break;
  1159. case 'Z':
  1160. s = xasprintf (_("filter the archive through %s"), COMPRESS_PROGRAM);
  1161. break;
  1162. case LZIP_OPTION:
  1163. s = xasprintf (_("filter the archive through %s"), LZIP_PROGRAM);
  1164. break;
  1165. case LZMA_OPTION:
  1166. s = xasprintf (_("filter the archive through %s"), LZMA_PROGRAM);
  1167. break;
  1168. case 'J':
  1169. s = xasprintf (_("filter the archive through %s"), XZ_PROGRAM);
  1170. break;
  1171. case ARGP_KEY_HELP_EXTRA:
  1172. {
  1173. const char *tstr;
  1174. obstack_init (&stk);
  1175. tstr = _("Valid arguments for the --quoting-style option are:");
  1176. obstack_grow (&stk, tstr, strlen (tstr));
  1177. obstack_grow (&stk, "\n\n", 2);
  1178. tar_list_quoting_styles (&stk, " ");
  1179. tstr = _("\n*This* tar defaults to:\n");
  1180. obstack_grow (&stk, tstr, strlen (tstr));
  1181. s = format_default_settings ();
  1182. obstack_grow (&stk, s, strlen (s));
  1183. obstack_1grow (&stk, '\n');
  1184. obstack_1grow (&stk, 0);
  1185. s = xstrdup (obstack_finish (&stk));
  1186. obstack_free (&stk, NULL);
  1187. }
  1188. }
  1189. return s;
  1190. }
  1191. static char *
  1192. expand_pax_option (struct tar_args *targs, const char *arg)
  1193. {
  1194. struct obstack stk;
  1195. char *res;
  1196. obstack_init (&stk);
  1197. while (*arg)
  1198. {
  1199. size_t seglen = strcspn (arg, ",");
  1200. char *p = memchr (arg, '=', seglen);
  1201. if (p)
  1202. {
  1203. size_t len = p - arg + 1;
  1204. obstack_grow (&stk, arg, len);
  1205. len = seglen - len;
  1206. for (++p; *p && isspace ((unsigned char) *p); p++)
  1207. len--;
  1208. if (*p == '{' && p[len-1] == '}')
  1209. {
  1210. struct timespec ts;
  1211. char *tmp = xmalloc (len);
  1212. memcpy (tmp, p + 1, len-2);
  1213. tmp[len-2] = 0;
  1214. if (get_date_or_file (targs, "--pax-option", tmp, &ts) == 0)
  1215. {
  1216. char buf[TIMESPEC_STRSIZE_BOUND];
  1217. char const *s = code_timespec (ts, buf);
  1218. obstack_grow (&stk, s, strlen (s));
  1219. }
  1220. else
  1221. obstack_grow (&stk, p, len);
  1222. free (tmp);
  1223. }
  1224. else
  1225. obstack_grow (&stk, p, len);
  1226. }
  1227. else
  1228. obstack_grow (&stk, arg, seglen);
  1229. arg += seglen;
  1230. if (*arg)
  1231. {
  1232. obstack_1grow (&stk, *arg);
  1233. arg++;
  1234. }
  1235. }
  1236. obstack_1grow (&stk, 0);
  1237. res = xstrdup (obstack_finish (&stk));
  1238. obstack_free (&stk, NULL);
  1239. return res;
  1240. }
  1241. static uintmax_t
  1242. parse_owner_group (char *arg, uintmax_t field_max, char const **name_option)
  1243. {
  1244. uintmax_t u = UINTMAX_MAX;
  1245. char *end;
  1246. char const *name = 0;
  1247. char const *invalid_num = 0;
  1248. char *colon = strchr (arg, ':');
  1249. if (colon)
  1250. {
  1251. char const *num = colon + 1;
  1252. *colon = '\0';
  1253. if (*arg)
  1254. name = arg;
  1255. if (num && (! (xstrtoumax (num, &end, 10, &u, "") == LONGINT_OK
  1256. && u <= field_max)))
  1257. invalid_num = num;
  1258. }
  1259. else
  1260. {
  1261. uintmax_t u1;
  1262. switch ('0' <= *arg && *arg <= '9'
  1263. ? xstrtoumax (arg, &end, 10, &u1, "")
  1264. : LONGINT_INVALID)
  1265. {
  1266. default:
  1267. name = arg;
  1268. break;
  1269. case LONGINT_OK:
  1270. if (u1 <= field_max)
  1271. {
  1272. u = u1;
  1273. break;
  1274. }
  1275. /* Fall through. */
  1276. case LONGINT_OVERFLOW:
  1277. invalid_num = arg;
  1278. break;
  1279. }
  1280. }
  1281. if (invalid_num)
  1282. FATAL_ERROR ((0, 0, "%s: %s", quotearg_colon (invalid_num),
  1283. _("Invalid owner or group ID")));
  1284. if (name)
  1285. *name_option = name;
  1286. return u;
  1287. }
  1288. #define TAR_SIZE_SUFFIXES "bBcGgkKMmPTtw"
  1289. static error_t
  1290. parse_opt (int key, char *arg, struct argp_state *state)
  1291. {
  1292. struct tar_args *args = state->input;
  1293. switch (key)
  1294. {
  1295. case ARGP_KEY_ARG:
  1296. /* File name or non-parsed option, because of ARGP_IN_ORDER */
  1297. name_add_name (arg, MAKE_INCL_OPTIONS (args));
  1298. args->input_files = true;
  1299. break;
  1300. case 'A':
  1301. set_subcommand_option (CAT_SUBCOMMAND);
  1302. break;
  1303. case 'a':
  1304. args->compress_autodetect = true;
  1305. break;
  1306. case NO_AUTO_COMPRESS_OPTION:
  1307. args->compress_autodetect = false;
  1308. break;
  1309. case 'b':
  1310. {
  1311. uintmax_t u;
  1312. if (! (xstrtoumax (arg, 0, 10, &u, "") == LONGINT_OK
  1313. && u == (blocking_factor = u)
  1314. && 0 < blocking_factor
  1315. && u == (record_size = u * BLOCKSIZE) / BLOCKSIZE))
  1316. USAGE_ERROR ((0, 0, "%s: %s", quotearg_colon (arg),
  1317. _("Invalid blocking factor")));
  1318. }
  1319. break;
  1320. case 'B':
  1321. /* Try to reblock input records. For reading 4.2BSD pipes. */
  1322. /* It would surely make sense to exchange -B and -R, but it seems
  1323. that -B has been used for a long while in Sun tar and most
  1324. BSD-derived systems. This is a consequence of the block/record
  1325. terminology confusion. */
  1326. read_full_records_option = true;
  1327. break;
  1328. case 'c':
  1329. set_subcommand_option (CREATE_SUBCOMMAND);
  1330. break;
  1331. case 'C':
  1332. name_add_dir (arg);
  1333. break;
  1334. case 'd':
  1335. set_subcommand_option (DIFF_SUBCOMMAND);
  1336. break;
  1337. case 'f':
  1338. if (archive_names == allocated_archive_names)
  1339. archive_name_array = x2nrealloc (archive_name_array,
  1340. &allocated_archive_names,
  1341. sizeof (archive_name_array[0]));
  1342. archive_name_array[archive_names++] = arg;
  1343. break;
  1344. case 'F':
  1345. /* Since -F is only useful with -M, make it implied. Run this
  1346. script at the end of each tape. */
  1347. info_script_option = arg;
  1348. multi_volume_option = true;
  1349. break;
  1350. case FULL_TIME_OPTION:
  1351. full_time_option = true;
  1352. break;
  1353. case 'g':
  1354. listed_incremental_option = arg;
  1355. after_date_option = true;
  1356. /* Fall through. */
  1357. case 'G':
  1358. /* We are making an incremental dump (FIXME: are we?); save
  1359. directories at the beginning of the archive, and include in each
  1360. directory its contents. */
  1361. incremental_option = true;
  1362. break;
  1363. case 'h':
  1364. /* Follow symbolic links. */
  1365. dereference_option = true;
  1366. break;
  1367. case HARD_DEREFERENCE_OPTION:
  1368. hard_dereference_option = true;
  1369. break;
  1370. case 'i':
  1371. /* Ignore zero blocks (eofs). This can't be the default,
  1372. because Unix tar writes two blocks of zeros, then pads out
  1373. the record with garbage. */
  1374. ignore_zeros_option = true;
  1375. break;
  1376. case 'j':
  1377. set_use_compress_program_option (BZIP2_PROGRAM);
  1378. break;
  1379. case 'J':
  1380. set_use_compress_program_option (XZ_PROGRAM);
  1381. break;
  1382. case 'k':
  1383. /* Don't replace existing files. */
  1384. old_files_option = KEEP_OLD_FILES;
  1385. break;
  1386. case 'K':
  1387. starting_file_option = true;
  1388. addname (arg, 0, true, NULL);
  1389. break;
  1390. case ONE_FILE_SYSTEM_OPTION:
  1391. /* When dumping directories, don't dump files/subdirectories
  1392. that are on other filesystems. */
  1393. one_file_system_option = true;
  1394. break;
  1395. case 'l':
  1396. check_links_option = 1;
  1397. break;
  1398. case 'L':
  1399. {
  1400. uintmax_t u;
  1401. char *p;
  1402. if (xstrtoumax (arg, &p, 10, &u, TAR_SIZE_SUFFIXES) != LONGINT_OK)
  1403. USAGE_ERROR ((0, 0, "%s: %s", quotearg_colon (arg),
  1404. _("Invalid tape length")));
  1405. if (p > arg && !strchr (TAR_SIZE_SUFFIXES, p[-1]))
  1406. tape_length_option = 1024 * (tarlong) u;
  1407. else
  1408. tape_length_option = (tarlong) u;
  1409. multi_volume_option = true;
  1410. }
  1411. break;
  1412. case LEVEL_OPTION:
  1413. {
  1414. char *p;
  1415. incremental_level = strtoul (arg, &p, 10);
  1416. if (*p)
  1417. USAGE_ERROR ((0, 0, _("Invalid incremental level value")));
  1418. }
  1419. break;
  1420. case LZIP_OPTION:
  1421. set_use_compress_program_option (LZIP_PROGRAM);
  1422. break;
  1423. case LZMA_OPTION:
  1424. set_use_compress_program_option (LZMA_PROGRAM);
  1425. break;
  1426. case LZOP_OPTION:
  1427. set_use_compress_program_option (LZOP_PROGRAM);
  1428. break;
  1429. case 'm':
  1430. touch_option = true;
  1431. break;
  1432. case 'M':
  1433. /* Make multivolume archive: when we can't write any more into
  1434. the archive, re-open it, and continue writing. */
  1435. multi_volume_option = true;
  1436. break;
  1437. case MTIME_OPTION:
  1438. get_date_or_file (args, "--mtime", arg, &mtime_option);
  1439. set_mtime_option = true;
  1440. break;
  1441. case 'n':
  1442. seek_option = 1;
  1443. break;
  1444. case NO_SEEK_OPTION:
  1445. seek_option = 0;
  1446. break;
  1447. case 'N':
  1448. after_date_option = true;
  1449. /* Fall through. */
  1450. case NEWER_MTIME_OPTION:
  1451. if (NEWER_OPTION_INITIALIZED (newer_mtime_option))
  1452. USAGE_ERROR ((0, 0, _("More than one threshold date")));
  1453. get_date_or_file (args,
  1454. key == NEWER_MTIME_OPTION ? "--newer-mtime"
  1455. : "--after-date", arg, &newer_mtime_option);
  1456. break;
  1457. case 'o':
  1458. args->o_option = true;
  1459. break;
  1460. case 'O':
  1461. to_stdout_option = true;
  1462. break;
  1463. case 'p':
  1464. same_permissions_option = true;
  1465. break;
  1466. case 'P':
  1467. absolute_names_option = true;
  1468. break;
  1469. case 'r':
  1470. set_subcommand_option (APPEND_SUBCOMMAND);
  1471. break;
  1472. case 'R':
  1473. /* Print block numbers for debugging bad tar archives. */
  1474. /* It would surely make sense to exchange -B and -R, but it seems
  1475. that -B has been used for a long while in Sun tar and most
  1476. BSD-derived systems. This is a consequence of the block/record
  1477. terminology confusion. */
  1478. block_number_option = true;
  1479. break;
  1480. case 's':
  1481. /* Names to extract are sorted. */
  1482. same_order_option = true;
  1483. break;
  1484. case 'S':
  1485. sparse_option = true;
  1486. break;
  1487. case SKIP_OLD_FILES_OPTION:
  1488. old_files_option = SKIP_OLD_FILES;
  1489. break;
  1490. case SPARSE_VERSION_OPTION:
  1491. sparse_option = true;
  1492. {
  1493. char *p;
  1494. tar_sparse_major = strtoul (arg, &p, 10);
  1495. if (*p)
  1496. {
  1497. if (*p != '.')
  1498. USAGE_ERROR ((0, 0, _("Invalid sparse version value")));
  1499. tar_sparse_minor = strtoul (p + 1, &p, 10);
  1500. if (*p)
  1501. USAGE_ERROR ((0, 0, _("Invalid sparse version value")));
  1502. }
  1503. }
  1504. break;
  1505. case 't':
  1506. set_subcommand_option (LIST_SUBCOMMAND);
  1507. verbose_option++;
  1508. break;
  1509. case TEST_LABEL_OPTION:
  1510. set_subcommand_option (TEST_LABEL_SUBCOMMAND);
  1511. break;
  1512. case 'T':
  1513. update_argv (arg, state);
  1514. /* Indicate we've been given -T option. This is for backward
  1515. compatibility only, so that `tar cfT archive /dev/null will
  1516. succeed */
  1517. files_from_option = true;
  1518. break;
  1519. case 'u':
  1520. set_subcommand_option (UPDATE_SUBCOMMAND);
  1521. break;
  1522. case 'U':
  1523. old_files_option = UNLINK_FIRST_OLD_FILES;
  1524. break;
  1525. case UTC_OPTION:
  1526. utc_option = true;
  1527. break;
  1528. case 'v':
  1529. verbose_option++;
  1530. warning_option |= WARN_VERBOSE_WARNINGS;
  1531. break;
  1532. case 'V':
  1533. volume_label_option = arg;
  1534. break;
  1535. case 'w':
  1536. interactive_option = true;
  1537. break;
  1538. case 'W':
  1539. verify_option = true;
  1540. break;
  1541. case 'x':
  1542. set_subcommand_option (EXTRACT_SUBCOMMAND);
  1543. break;
  1544. case 'X':
  1545. if (add_exclude_file (add_exclude, excluded, arg,
  1546. MAKE_EXCL_OPTIONS (args), '\n')
  1547. != 0)
  1548. {
  1549. int e = errno;
  1550. FATAL_ERROR ((0, e, "%s", quotearg_colon (arg)));
  1551. }
  1552. break;
  1553. case 'z':
  1554. set_use_compress_program_option (GZIP_PROGRAM);
  1555. break;
  1556. case 'Z':
  1557. set_use_compress_program_option (COMPRESS_PROGRAM);
  1558. break;
  1559. case ANCHORED_OPTION:
  1560. args->matching_flags |= EXCLUDE_ANCHORED;
  1561. break;
  1562. case ATIME_PRESERVE_OPTION:
  1563. atime_preserve_option =
  1564. (arg
  1565. ? XARGMATCH ("--atime-preserve", arg,
  1566. atime_preserve_args, atime_preserve_types)
  1567. : replace_atime_preserve);
  1568. if (! O_NOATIME && atime_preserve_option == system_atime_preserve)
  1569. FATAL_ERROR ((0, 0,
  1570. _("--atime-preserve='system' is not supported"
  1571. " on this platform")));
  1572. break;
  1573. case CHECK_DEVICE_OPTION:
  1574. check_device_option = true;
  1575. break;
  1576. case NO_CHECK_DEVICE_OPTION:
  1577. check_device_option = false;
  1578. break;
  1579. case CHECKPOINT_OPTION:
  1580. if (arg)
  1581. {
  1582. char *p;
  1583. if (*arg == '.')
  1584. {
  1585. checkpoint_compile_action (".");
  1586. arg++;
  1587. }
  1588. checkpoint_option = strtoul (arg, &p, 0);
  1589. if (*p)
  1590. FATAL_ERROR ((0, 0,
  1591. _("--checkpoint value is not an integer")));
  1592. }
  1593. else
  1594. checkpoint_option = DEFAULT_CHECKPOINT;
  1595. break;
  1596. case CHECKPOINT_ACTION_OPTION:
  1597. checkpoint_compile_action (arg);
  1598. break;
  1599. case BACKUP_OPTION:
  1600. backup_option = true;
  1601. if (arg)
  1602. args->version_control_string = arg;
  1603. break;
  1604. case DELAY_DIRECTORY_RESTORE_OPTION:
  1605. delay_directory_restore_option = true;
  1606. break;
  1607. case NO_DELAY_DIRECTORY_RESTORE_OPTION:
  1608. delay_directory_restore_option = false;
  1609. break;
  1610. case DELETE_OPTION:
  1611. set_subcommand_option (DELETE_SUBCOMMAND);
  1612. break;
  1613. case EXCLUDE_BACKUPS_OPTION:
  1614. add_exclude_array (backup_file_table, EXCLUDE_WILDCARDS);
  1615. break;
  1616. case EXCLUDE_OPTION:
  1617. add_exclude (excluded, arg, MAKE_EXCL_OPTIONS (args));
  1618. break;
  1619. case EXCLUDE_CACHES_OPTION:
  1620. add_exclusion_tag ("CACHEDIR.TAG", exclusion_tag_contents,
  1621. cachedir_file_p);
  1622. break;
  1623. case EXCLUDE_CACHES_UNDER_OPTION:
  1624. add_exclusion_tag ("CACHEDIR.TAG", exclusion_tag_under,
  1625. cachedir_file_p);
  1626. break;
  1627. case EXCLUDE_CACHES_ALL_OPTION:
  1628. add_exclusion_tag ("CACHEDIR.TAG", exclusion_tag_all,
  1629. cachedir_file_p);
  1630. break;
  1631. case EXCLUDE_TAG_OPTION:
  1632. add_exclusion_tag (arg, exclusion_tag_contents, NULL);
  1633. break;
  1634. case EXCLUDE_TAG_UNDER_OPTION:
  1635. add_exclusion_tag (arg, exclusion_tag_under, NULL);
  1636. break;
  1637. case EXCLUDE_TAG_ALL_OPTION:
  1638. add_exclusion_tag (arg, exclusion_tag_all, NULL);
  1639. break;
  1640. case EXCLUDE_VCS_OPTION:
  1641. add_exclude_array (vcs_file_table, 0);
  1642. break;
  1643. case FORCE_LOCAL_OPTION:
  1644. force_local_option = true;
  1645. break;
  1646. case 'H':
  1647. set_archive_format (arg);
  1648. break;
  1649. case INDEX_FILE_OPTION:
  1650. index_file_name = arg;
  1651. break;
  1652. case IGNORE_CASE_OPTION:
  1653. args->matching_flags |= FNM_CASEFOLD;
  1654. break;
  1655. case IGNORE_COMMAND_ERROR_OPTION:
  1656. ignore_command_error_option = true;
  1657. break;
  1658. case IGNORE_FAILED_READ_OPTION:
  1659. ignore_failed_read_option = true;
  1660. break;
  1661. case KEEP_NEWER_FILES_OPTION:
  1662. old_files_option = KEEP_NEWER_FILES;
  1663. break;
  1664. case GROUP_OPTION:
  1665. {
  1666. uintmax_t u = parse_owner_group (arg, TYPE_MAXIMUM (gid_t),
  1667. &group_name_option);
  1668. if (u == UINTMAX_MAX)
  1669. {
  1670. group_option = -1;
  1671. if (group_name_option)
  1672. gname_to_gid (group_name_option, &group_option);
  1673. }
  1674. else
  1675. group_option = u;
  1676. }
  1677. break;
  1678. case MODE_OPTION:
  1679. mode_option = mode_compile (arg);
  1680. if (!mode_option)
  1681. FATAL_ERROR ((0, 0, _("Invalid mode given on option")));
  1682. initial_umask = umask (0);
  1683. umask (initial_umask);
  1684. break;
  1685. case NO_ANCHORED_OPTION:
  1686. args->include_anchored = 0; /* Clear the default for comman line args */
  1687. args->matching_flags &= ~ EXCLUDE_ANCHORED;
  1688. break;
  1689. case NO_IGNORE_CASE_OPTION:
  1690. args->matching_flags &= ~ FNM_CASEFOLD;
  1691. break;
  1692. case NO_IGNORE_COMMAND_ERROR_OPTION:
  1693. ignore_command_error_option = false;
  1694. break;
  1695. case NO_OVERWRITE_DIR_OPTION:
  1696. old_files_option = NO_OVERWRITE_DIR_OLD_FILES;
  1697. break;
  1698. case NO_QUOTE_CHARS_OPTION:
  1699. for (;*arg; arg++)
  1700. set_char_quoting (NULL, *arg, 0);
  1701. break;
  1702. case NO_WILDCARDS_OPTION:
  1703. args->wildcards = disable_wildcards;
  1704. break;
  1705. case NO_WILDCARDS_MATCH_SLASH_OPTION:
  1706. args->matching_flags |= FNM_FILE_NAME;
  1707. break;
  1708. case NULL_OPTION:
  1709. filename_terminator = '\0';
  1710. break;
  1711. case NO_NULL_OPTION:
  1712. filename_terminator = '\n';
  1713. break;
  1714. case NUMERIC_OWNER_OPTION:
  1715. numeric_owner_option = true;
  1716. break;
  1717. case OCCURRENCE_OPTION:
  1718. if (!arg)
  1719. occurrence_option = 1;
  1720. else
  1721. {
  1722. uintmax_t u;
  1723. if (xstrtoumax (arg, 0, 10, &u, "") == LONGINT_OK)
  1724. occurrence_option = u;
  1725. else
  1726. FATAL_ERROR ((0, 0, "%s: %s", quotearg_colon (arg),
  1727. _("Invalid number")));
  1728. }
  1729. break;
  1730. case OLD_ARCHIVE_OPTION:
  1731. set_archive_format ("v7");
  1732. break;
  1733. case OVERWRITE_DIR_OPTION:
  1734. old_files_option = DEFAULT_OLD_FILES;
  1735. break;
  1736. case OVERWRITE_OPTION:
  1737. old_files_option = OVERWRITE_OLD_FILES;
  1738. break;
  1739. case OWNER_OPTION:
  1740. {
  1741. uintmax_t u = parse_owner_group (arg, TYPE_MAXIMUM (uid_t),
  1742. &owner_name_option);
  1743. if (u == UINTMAX_MAX)
  1744. {
  1745. owner_option = -1;
  1746. if (owner_name_option)
  1747. uname_to_uid (owner_name_option, &owner_option);
  1748. }
  1749. else
  1750. owner_option = u;
  1751. }
  1752. break;
  1753. case QUOTE_CHARS_OPTION:
  1754. for (;*arg; arg++)
  1755. set_char_quoting (NULL, *arg, 1);
  1756. break;
  1757. case QUOTING_STYLE_OPTION:
  1758. tar_set_quoting_style (arg);
  1759. break;
  1760. case PAX_OPTION:
  1761. {
  1762. char *tmp = expand_pax_option (args, arg);
  1763. args->pax_option = true;
  1764. xheader_set_option (tmp);
  1765. free (tmp);
  1766. }
  1767. break;
  1768. case POSIX_OPTION:
  1769. set_archive_format ("posix");
  1770. break;
  1771. case PRESERVE_OPTION:
  1772. /* FIXME: What it is good for? */
  1773. same_permissions_option = true;
  1774. same_order_option = true;
  1775. WARN ((0, 0, _("The --preserve option is deprecated, "
  1776. "use --preserve-permissions --preserve-order instead")));
  1777. break;
  1778. case RECORD_SIZE_OPTION:
  1779. {
  1780. uintmax_t u;
  1781. if (! (xstrtoumax (arg, NULL, 10, &u, TAR_SIZE_SUFFIXES) == LONGINT_OK
  1782. && u == (size_t) u))
  1783. USAGE_ERROR ((0, 0, "%s: %s", quotearg_colon (arg),
  1784. _("Invalid record size")));
  1785. record_size = u;
  1786. if (record_size % BLOCKSIZE != 0)
  1787. USAGE_ERROR ((0, 0, _("Record size must be a multiple of %d."),
  1788. BLOCKSIZE));
  1789. blocking_factor = record_size / BLOCKSIZE;
  1790. }
  1791. break;
  1792. case RECURSIVE_UNLINK_OPTION:
  1793. recursive_unlink_option = true;
  1794. break;
  1795. case REMOVE_FILES_OPTION:
  1796. remove_files_option = true;
  1797. break;
  1798. case RESTRICT_OPTION:
  1799. restrict_option = true;
  1800. break;
  1801. case RMT_COMMAND_OPTION:
  1802. rmt_command = arg;
  1803. break;
  1804. case RSH_COMMAND_OPTION:
  1805. rsh_command_option = arg;
  1806. break;
  1807. case SHOW_DEFAULTS_OPTION:
  1808. {
  1809. char *s = format_default_settings ();
  1810. printf ("%s\n", s);
  1811. close_stdout ();
  1812. free (s);
  1813. exit (0);
  1814. }
  1815. case STRIP_COMPONENTS_OPTION:
  1816. {
  1817. uintmax_t u;
  1818. if (! (xstrtoumax (arg, 0, 10, &u, "") == LONGINT_OK
  1819. && u == (size_t) u))
  1820. USAGE_ERROR ((0, 0, "%s: %s", quotearg_colon (arg),
  1821. _("Invalid number of elements")));
  1822. strip_name_components = u;
  1823. }
  1824. break;
  1825. case SHOW_OMITTED_DIRS_OPTION:
  1826. show_omitted_dirs_option = true;
  1827. break;
  1828. case SHOW_TRANSFORMED_NAMES_OPTION:
  1829. show_transformed_names_option = true;
  1830. break;
  1831. case SUFFIX_OPTION:
  1832. backup_option = true;
  1833. args->backup_suffix_string = arg;
  1834. break;
  1835. case TO_COMMAND_OPTION:
  1836. if (to_command_option)
  1837. USAGE_ERROR ((0, 0, _("Only one --to-command option allowed")));
  1838. to_command_option = arg;
  1839. break;
  1840. case TOTALS_OPTION:
  1841. if (arg)
  1842. set_stat_signal (arg);
  1843. else
  1844. totals_option = true;
  1845. break;
  1846. case TRANSFORM_OPTION:
  1847. set_transform_expr (arg);
  1848. break;
  1849. case 'I':
  1850. set_use_compress_program_option (arg);
  1851. break;
  1852. case VOLNO_FILE_OPTION:
  1853. volno_file_option = arg;
  1854. break;
  1855. case WILDCARDS_OPTION:
  1856. args->wildcards = enable_wildcards;
  1857. break;
  1858. case WILDCARDS_MATCH_SLASH_OPTION:
  1859. args->matching_flags &= ~ FNM_FILE_NAME;
  1860. break;
  1861. case NO_RECURSION_OPTION:
  1862. recursion_option = 0;
  1863. break;
  1864. case NO_SAME_OWNER_OPTION:
  1865. same_owner_option = -1;
  1866. break;
  1867. case NO_SAME_PERMISSIONS_OPTION:
  1868. same_permissions_option = -1;
  1869. break;
  1870. case ACLS_OPTION:
  1871. set_archive_format ("posix");
  1872. acls_option = 1;
  1873. break;
  1874. case NO_ACLS_OPTION:
  1875. acls_option = -1;
  1876. break;
  1877. case SELINUX_CONTEXT_OPTION:
  1878. set_archive_format ("posix");
  1879. selinux_context_option = 1;
  1880. break;
  1881. case NO_SELINUX_CONTEXT_OPTION:
  1882. selinux_context_option = -1;
  1883. break;
  1884. case XATTR_OPTION:
  1885. set_archive_format ("posix");
  1886. xattrs_option = 1;
  1887. break;
  1888. case NO_XATTR_OPTION:
  1889. xattrs_option = -1;
  1890. break;
  1891. case XATTR_INCLUDE:
  1892. case XATTR_EXCLUDE:
  1893. xattrs_mask_add (arg, (key == XATTR_INCLUDE));
  1894. break;
  1895. case RECURSION_OPTION:
  1896. recursion_option = FNM_LEADING_DIR;
  1897. break;
  1898. case SAME_OWNER_OPTION:
  1899. same_owner_option = 1;
  1900. break;
  1901. case UNQUOTE_OPTION:
  1902. unquote_option = true;
  1903. break;
  1904. case NO_UNQUOTE_OPTION:
  1905. unquote_option = false;
  1906. break;
  1907. case WARNING_OPTION:
  1908. set_warning_option (arg);
  1909. break;
  1910. case '0':
  1911. case '1':
  1912. case '2':
  1913. case '3':
  1914. case '4':
  1915. case '5':
  1916. case '6':
  1917. case '7':
  1918. #ifdef DEVICE_PREFIX
  1919. {
  1920. int device = key - '0';
  1921. int density;
  1922. static char buf[sizeof DEVICE_PREFIX + 10];
  1923. char *cursor;
  1924. if (arg[1])
  1925. argp_error (state, _("Malformed density argument: %s"), quote (arg));
  1926. strcpy (buf, DEVICE_PREFIX);
  1927. cursor = buf + strlen (buf);
  1928. #ifdef DENSITY_LETTER
  1929. sprintf (cursor, "%d%c", device, arg[0]);
  1930. #else /* not DENSITY_LETTER */
  1931. switch (arg[0])
  1932. {
  1933. case 'l':
  1934. device += LOW_DENSITY_NUM;
  1935. break;
  1936. case 'm':
  1937. device += MID_DENSITY_NUM;
  1938. break;
  1939. case 'h':
  1940. device += HIGH_DENSITY_NUM;
  1941. break;
  1942. default:
  1943. argp_error (state, _("Unknown density: '%c'"), arg[0]);
  1944. }
  1945. sprintf (cursor, "%d", device);
  1946. #endif /* not DENSITY_LETTER */
  1947. if (archive_names == allocated_archive_names)
  1948. archive_name_array = x2nrealloc (archive_name_array,
  1949. &allocated_archive_names,
  1950. sizeof (archive_name_array[0]));
  1951. archive_name_array[archive_names++] = xstrdup (buf);
  1952. }
  1953. break;
  1954. #else /* not DEVICE_PREFIX */
  1955. argp_error (state,
  1956. _("Options '-[0-7][lmh]' not supported by *this* tar"));
  1957. #endif /* not DEVICE_PREFIX */
  1958. default:
  1959. return ARGP_ERR_UNKNOWN;
  1960. }
  1961. return 0;
  1962. }
  1963. static struct argp argp = {
  1964. options,
  1965. parse_opt,
  1966. N_("[FILE]..."),
  1967. doc,
  1968. NULL,
  1969. tar_help_filter,
  1970. NULL
  1971. };
  1972. void
  1973. usage (int status)
  1974. {
  1975. argp_help (&argp, stderr, ARGP_HELP_SEE, (char*) program_name);
  1976. close_stdout ();
  1977. exit (status);
  1978. }
  1979. /* Parse the options for tar. */
  1980. static struct argp_option *
  1981. find_argp_option (struct argp_option *o, int letter)
  1982. {
  1983. for (;
  1984. !(o->name == NULL
  1985. && o->key == 0
  1986. && o->arg == 0
  1987. && o->flags == 0
  1988. && o->doc == NULL); o++)
  1989. if (o->key == letter)
  1990. return o;
  1991. return NULL;
  1992. }
  1993. static const char *tar_authors[] = {
  1994. "John Gilmore",
  1995. "Jay Fenlason",
  1996. NULL
  1997. };
  1998. static void
  1999. decode_options (int argc, char **argv)
  2000. {
  2001. int idx;
  2002. struct tar_args args;
  2003. argp_version_setup ("tar", tar_authors);
  2004. /* Set some default option values. */
  2005. args.textual_date = NULL;
  2006. args.wildcards = default_wildcards;
  2007. args.matching_flags = 0;
  2008. args.include_anchored = EXCLUDE_ANCHORED;
  2009. args.o_option = false;
  2010. args.pax_option = false;
  2011. args.backup_suffix_string = getenv ("SIMPLE_BACKUP_SUFFIX");
  2012. args.version_control_string = 0;
  2013. args.input_files = false;
  2014. args.compress_autodetect = false;
  2015. subcommand_option = UNKNOWN_SUBCOMMAND;
  2016. archive_format = DEFAULT_FORMAT;
  2017. blocking_factor = DEFAULT_BLOCKING;
  2018. record_size = DEFAULT_BLOCKING * BLOCKSIZE;
  2019. excluded = new_exclude ();
  2020. newer_mtime_option.tv_sec = TYPE_MINIMUM (time_t);
  2021. newer_mtime_option.tv_nsec = -1;
  2022. recursion_option = FNM_LEADING_DIR;
  2023. unquote_option = true;
  2024. tar_sparse_major = 1;
  2025. tar_sparse_minor = 0;
  2026. owner_option = -1; owner_name_option = NULL;
  2027. group_option = -1; group_name_option = NULL;
  2028. check_device_option = true;
  2029. incremental_level = -1;
  2030. seek_option = -1;
  2031. /* Convert old-style tar call by exploding option element and rearranging
  2032. options accordingly. */
  2033. if (argc > 1 && argv[1][0] != '-')
  2034. {
  2035. int new_argc; /* argc value for rearranged arguments */
  2036. char **new_argv; /* argv value for rearranged arguments */
  2037. char *const *in; /* cursor into original argv */
  2038. char **out; /* cursor into rearranged argv */
  2039. const char *letter; /* cursor into old option letters */
  2040. char buffer[3]; /* constructed option buffer */
  2041. /* Initialize a constructed option. */
  2042. buffer[0] = '-';
  2043. buffer[2] = '\0';
  2044. /* Allocate a new argument array, and copy program name in it. */
  2045. new_argc = argc - 1 + strlen (argv[1]);
  2046. new_argv = xmalloc ((new_argc + 1) * sizeof (char *));
  2047. in = argv;
  2048. out = new_argv;
  2049. *out++ = *in++;
  2050. /* Copy each old letter option as a separate option, and have the
  2051. corresponding argument moved next to it. */
  2052. for (letter = *in++; *letter; letter++)
  2053. {
  2054. struct argp_option *opt;
  2055. buffer[1] = *letter;
  2056. *out++ = xstrdup (buffer);
  2057. opt = find_argp_option (options, *letter);
  2058. if (opt && opt->arg)
  2059. {
  2060. if (in < argv + argc)
  2061. *out++ = *in++;
  2062. else
  2063. USAGE_ERROR ((0, 0, _("Old option '%c' requires an argument."),
  2064. *letter));
  2065. }
  2066. }
  2067. /* Copy all remaining options. */
  2068. while (in < argv + argc)
  2069. *out++ = *in++;
  2070. *out = 0;
  2071. /* Replace the old option list by the new one. */
  2072. argc = new_argc;
  2073. argv = new_argv;
  2074. }
  2075. /* Parse all options and non-options as they appear. */
  2076. prepend_default_options (getenv ("TAR_OPTIONS"), &argc, &argv);
  2077. if (argp_parse (&argp, argc, argv, ARGP_IN_ORDER, &idx, &args))
  2078. exit (TAREXIT_FAILURE);
  2079. /* Special handling for 'o' option:
  2080. GNU tar used to say "output old format".
  2081. UNIX98 tar says don't chown files after extracting (we use
  2082. "--no-same-owner" for this).
  2083. The old GNU tar semantics is retained when used with --create
  2084. option, otherwise UNIX98 semantics is assumed */
  2085. if (args.o_option)
  2086. {
  2087. if (subcommand_option == CREATE_SUBCOMMAND)
  2088. {
  2089. /* GNU Tar <= 1.13 compatibility */
  2090. set_archive_format ("v7");
  2091. }
  2092. else
  2093. {
  2094. /* UNIX98 compatibility */
  2095. same_owner_option = -1;
  2096. }
  2097. }
  2098. /* Handle operands after any "--" argument. */
  2099. for (; idx < argc; idx++)
  2100. {
  2101. name_add_name (argv[idx], MAKE_INCL_OPTIONS (&args));
  2102. args.input_files = true;
  2103. }
  2104. /* Warn about implicit use of the wildcards in command line arguments.
  2105. See TODO */
  2106. warn_regex_usage = args.wildcards == default_wildcards;
  2107. /* Derive option values and check option consistency. */
  2108. if (archive_format == DEFAULT_FORMAT)
  2109. {
  2110. if (args.pax_option)
  2111. archive_format = POSIX_FORMAT;
  2112. else
  2113. archive_format = DEFAULT_ARCHIVE_FORMAT;
  2114. }
  2115. if ((volume_label_option && subcommand_option == CREATE_SUBCOMMAND)
  2116. || incremental_option
  2117. || multi_volume_option
  2118. || sparse_option)
  2119. assert_format (FORMAT_MASK (OLDGNU_FORMAT)
  2120. | FORMAT_MASK (GNU_FORMAT)
  2121. | FORMAT_MASK (POSIX_FORMAT));
  2122. if (occurrence_option)
  2123. {
  2124. if (!args.input_files)
  2125. USAGE_ERROR ((0, 0,
  2126. _("--occurrence is meaningless without a file list")));
  2127. if (subcommand_option != DELETE_SUBCOMMAND
  2128. && subcommand_option != DIFF_SUBCOMMAND
  2129. && subcommand_option != EXTRACT_SUBCOMMAND
  2130. && subcommand_option != LIST_SUBCOMMAND)
  2131. USAGE_ERROR ((0, 0,
  2132. _("--occurrence cannot be used in the requested operation mode")));
  2133. }
  2134. if (archive_names == 0)
  2135. {
  2136. /* If no archive file name given, try TAPE from the environment, or
  2137. else, DEFAULT_ARCHIVE from the configuration process. */
  2138. archive_names = 1;
  2139. archive_name_array[0] = getenv ("TAPE");
  2140. if (! archive_name_array[0])
  2141. archive_name_array[0] = DEFAULT_ARCHIVE;
  2142. }
  2143. /* Allow multiple archives only with '-M'. */
  2144. if (archive_names > 1 && !multi_volume_option)
  2145. USAGE_ERROR ((0, 0,
  2146. _("Multiple archive files require '-M' option")));
  2147. if (listed_incremental_option
  2148. && NEWER_OPTION_INITIALIZED (newer_mtime_option))
  2149. USAGE_ERROR ((0, 0,
  2150. _("Cannot combine --listed-incremental with --newer")));
  2151. if (incremental_level != -1 && !listed_incremental_option)
  2152. WARN ((0, 0,
  2153. _("--level is meaningless without --listed-incremental")));
  2154. if (volume_label_option)
  2155. {
  2156. if (archive_format == GNU_FORMAT || archive_format == OLDGNU_FORMAT)
  2157. {
  2158. size_t volume_label_max_len =
  2159. (sizeof current_header->header.name
  2160. - 1 /* for trailing '\0' */
  2161. - (multi_volume_option
  2162. ? (sizeof " Volume "
  2163. - 1 /* for null at end of " Volume " */
  2164. + INT_STRLEN_BOUND (int) /* for volume number */
  2165. - 1 /* for sign, as 0 <= volno */)
  2166. : 0));
  2167. if (volume_label_max_len < strlen (volume_label_option))
  2168. USAGE_ERROR ((0, 0,
  2169. ngettext ("%s: Volume label is too long (limit is %lu byte)",
  2170. "%s: Volume label is too long (limit is %lu bytes)",
  2171. volume_label_max_len),
  2172. quotearg_colon (volume_label_option),
  2173. (unsigned long) volume_label_max_len));
  2174. }
  2175. /* else FIXME
  2176. Label length in PAX format is limited by the volume size. */
  2177. }
  2178. if (verify_option)
  2179. {
  2180. if (multi_volume_option)
  2181. USAGE_ERROR ((0, 0, _("Cannot verify multi-volume archives")));
  2182. if (use_compress_program_option)
  2183. USAGE_ERROR ((0, 0, _("Cannot verify compressed archives")));
  2184. }
  2185. if (use_compress_program_option)
  2186. {
  2187. if (multi_volume_option)
  2188. USAGE_ERROR ((0, 0, _("Cannot use multi-volume compressed archives")));
  2189. if (subcommand_option == UPDATE_SUBCOMMAND
  2190. || subcommand_option == APPEND_SUBCOMMAND
  2191. || subcommand_option == DELETE_SUBCOMMAND)
  2192. USAGE_ERROR ((0, 0, _("Cannot update compressed archives")));
  2193. if (subcommand_option == CAT_SUBCOMMAND)
  2194. USAGE_ERROR ((0, 0, _("Cannot concatenate compressed archives")));
  2195. }
  2196. /* It is no harm to use --pax-option on non-pax archives in archive
  2197. reading mode. It may even be useful, since it allows to override
  2198. file attributes from tar headers. Therefore I allow such usage.
  2199. --gray */
  2200. if (args.pax_option
  2201. && archive_format != POSIX_FORMAT
  2202. && !READ_LIKE_SUBCOMMAND)
  2203. USAGE_ERROR ((0, 0, _("--pax-option can be used only on POSIX archives")));
  2204. /* star creates non-POSIX typed archives with xattr support, so allow the
  2205. extra headers whenn reading */
  2206. if ((acls_option > 0)
  2207. && archive_format != POSIX_FORMAT
  2208. && !READ_LIKE_SUBCOMMAND)
  2209. USAGE_ERROR ((0, 0, _("--acls can be used only on POSIX archives")));
  2210. if ((selinux_context_option > 0)
  2211. && archive_format != POSIX_FORMAT
  2212. && !READ_LIKE_SUBCOMMAND)
  2213. USAGE_ERROR ((0, 0, _("--selinux can be used only on POSIX archives")));
  2214. if ((xattrs_option > 0)
  2215. && archive_format != POSIX_FORMAT
  2216. && !READ_LIKE_SUBCOMMAND)
  2217. USAGE_ERROR ((0, 0, _("--xattrs can be used only on POSIX archives")));
  2218. /* If ready to unlink hierarchies, so we are for simpler files. */
  2219. if (recursive_unlink_option)
  2220. old_files_option = UNLINK_FIRST_OLD_FILES;
  2221. /* Flags for accessing files to be read from or copied into. POSIX says
  2222. O_NONBLOCK has unspecified effect on most types of files, but in
  2223. practice it never harms and sometimes helps. */
  2224. {
  2225. int base_open_flags =
  2226. (O_BINARY | O_CLOEXEC | O_NOCTTY | O_NONBLOCK
  2227. | (dereference_option ? 0 : O_NOFOLLOW)
  2228. | (atime_preserve_option == system_atime_preserve ? O_NOATIME : 0));
  2229. open_read_flags = O_RDONLY | base_open_flags;
  2230. open_searchdir_flags = O_SEARCH | O_DIRECTORY | base_open_flags;
  2231. }
  2232. fstatat_flags = dereference_option ? 0 : AT_SYMLINK_NOFOLLOW;
  2233. if (subcommand_option == TEST_LABEL_SUBCOMMAND)
  2234. {
  2235. /* --test-label is silent if the user has specified the label name to
  2236. compare against. */
  2237. if (!args.input_files)
  2238. verbose_option++;
  2239. }
  2240. else if (utc_option)
  2241. verbose_option = 2;
  2242. if (tape_length_option && tape_length_option < record_size)
  2243. USAGE_ERROR ((0, 0, _("Volume length cannot be less than record size")));
  2244. if (same_order_option && listed_incremental_option)
  2245. USAGE_ERROR ((0, 0, _("--preserve-order is not compatible with "
  2246. "--listed-incremental")));
  2247. /* Forbid using -c with no input files whatsoever. Check that '-f -',
  2248. explicit or implied, is used correctly. */
  2249. switch (subcommand_option)
  2250. {
  2251. case CREATE_SUBCOMMAND:
  2252. if (!args.input_files && !files_from_option)
  2253. USAGE_ERROR ((0, 0,
  2254. _("Cowardly refusing to create an empty archive")));
  2255. if (args.compress_autodetect && archive_names
  2256. && strcmp (archive_name_array[0], "-"))
  2257. set_compression_program_by_suffix (archive_name_array[0],
  2258. use_compress_program_option);
  2259. break;
  2260. case EXTRACT_SUBCOMMAND:
  2261. case LIST_SUBCOMMAND:
  2262. case DIFF_SUBCOMMAND:
  2263. case TEST_LABEL_SUBCOMMAND:
  2264. for (archive_name_cursor = archive_name_array;
  2265. archive_name_cursor < archive_name_array + archive_names;
  2266. archive_name_cursor++)
  2267. if (!strcmp (*archive_name_cursor, "-"))
  2268. request_stdin ("-f");
  2269. break;
  2270. case CAT_SUBCOMMAND:
  2271. case UPDATE_SUBCOMMAND:
  2272. case APPEND_SUBCOMMAND:
  2273. for (archive_name_cursor = archive_name_array;
  2274. archive_name_cursor < archive_name_array + archive_names;
  2275. archive_name_cursor++)
  2276. if (!strcmp (*archive_name_cursor, "-"))
  2277. USAGE_ERROR ((0, 0,
  2278. _("Options '-Aru' are incompatible with '-f -'")));
  2279. default:
  2280. break;
  2281. }
  2282. /* Initialize stdlis */
  2283. if (index_file_name)
  2284. {
  2285. stdlis = fopen (index_file_name, "w");
  2286. if (! stdlis)
  2287. open_fatal (index_file_name);
  2288. }
  2289. else
  2290. stdlis = to_stdout_option ? stderr : stdout;
  2291. archive_name_cursor = archive_name_array;
  2292. /* Prepare for generating backup names. */
  2293. if (args.backup_suffix_string)
  2294. simple_backup_suffix = xstrdup (args.backup_suffix_string);
  2295. if (backup_option)
  2296. {
  2297. backup_type = xget_version ("--backup", args.version_control_string);
  2298. /* No backup is needed either if explicitely disabled or if
  2299. the extracted files are not being written to disk. */
  2300. if (backup_type == no_backups || EXTRACT_OVER_PIPE)
  2301. backup_option = false;
  2302. }
  2303. checkpoint_finish_compile ();
  2304. report_textual_dates (&args);
  2305. }
  2306. /* Tar proper. */
  2307. /* Main routine for tar. */
  2308. int
  2309. main (int argc, char **argv)
  2310. {
  2311. set_start_time ();
  2312. set_program_name (argv[0]);
  2313. setlocale (LC_ALL, "");
  2314. bindtextdomain (PACKAGE, LOCALEDIR);
  2315. textdomain (PACKAGE);
  2316. exit_failure = TAREXIT_FAILURE;
  2317. exit_status = TAREXIT_SUCCESS;
  2318. filename_terminator = '\n';
  2319. set_quoting_style (0, DEFAULT_QUOTING_STYLE);
  2320. /* Make sure we have first three descriptors available */
  2321. stdopen ();
  2322. /* Pre-allocate a few structures. */
  2323. allocated_archive_names = 10;
  2324. archive_name_array =
  2325. xmalloc (sizeof (const char *) * allocated_archive_names);
  2326. archive_names = 0;
  2327. obstack_init (&argv_stk);
  2328. /* System V fork+wait does not work if SIGCHLD is ignored. */
  2329. signal (SIGCHLD, SIG_DFL);
  2330. /* Try to disable the ability to unlink a directory. */
  2331. priv_set_remove_linkdir ();
  2332. /* Decode options. */
  2333. decode_options (argc, argv);
  2334. name_init ();
  2335. /* Main command execution. */
  2336. if (volno_file_option)
  2337. init_volume_number ();
  2338. switch (subcommand_option)
  2339. {
  2340. case UNKNOWN_SUBCOMMAND:
  2341. USAGE_ERROR ((0, 0,
  2342. _("You must specify one of the '-Acdtrux' or '--test-label' options")));
  2343. case CAT_SUBCOMMAND:
  2344. case UPDATE_SUBCOMMAND:
  2345. case APPEND_SUBCOMMAND:
  2346. update_archive ();
  2347. break;
  2348. case DELETE_SUBCOMMAND:
  2349. delete_archive_members ();
  2350. break;
  2351. case CREATE_SUBCOMMAND:
  2352. create_archive ();
  2353. break;
  2354. case EXTRACT_SUBCOMMAND:
  2355. extr_init ();
  2356. read_and (extract_archive);
  2357. /* FIXME: should extract_finish () even if an ordinary signal is
  2358. received. */
  2359. extract_finish ();
  2360. break;
  2361. case LIST_SUBCOMMAND:
  2362. read_and (list_archive);
  2363. break;
  2364. case DIFF_SUBCOMMAND:
  2365. diff_init ();
  2366. read_and (diff_archive);
  2367. break;
  2368. case TEST_LABEL_SUBCOMMAND:
  2369. test_archive_label ();
  2370. }
  2371. if (totals_option)
  2372. print_total_stats ();
  2373. if (check_links_option)
  2374. check_links ();
  2375. if (volno_file_option)
  2376. closeout_volume_number ();
  2377. /* Dispose of allocated memory, and return. */
  2378. free (archive_name_array);
  2379. xattrs_clear_setup ();
  2380. name_term ();
  2381. if (exit_status == TAREXIT_FAILURE)
  2382. error (0, 0, _("Exiting with failure status due to previous errors"));
  2383. if (stdlis == stdout)
  2384. close_stdout ();
  2385. else if (ferror (stderr) || fclose (stderr) != 0)
  2386. set_exit_status (TAREXIT_FAILURE);
  2387. return exit_status;
  2388. }
  2389. void
  2390. tar_stat_init (struct tar_stat_info *st)
  2391. {
  2392. memset (st, 0, sizeof (*st));
  2393. }
  2394. /* Close the stream or file descriptor associated with ST, and remove
  2395. all traces of it from ST. Return true if successful, false (with a
  2396. diagnostic) otherwise. */
  2397. bool
  2398. tar_stat_close (struct tar_stat_info *st)
  2399. {
  2400. int status = (st->dirstream ? closedir (st->dirstream)
  2401. : 0 < st->fd ? close (st->fd)
  2402. : 0);
  2403. st->dirstream = 0;
  2404. st->fd = 0;
  2405. if (status == 0)
  2406. return true;
  2407. else
  2408. {
  2409. close_diag (st->orig_file_name);
  2410. return false;
  2411. }
  2412. }
  2413. void
  2414. tar_stat_destroy (struct tar_stat_info *st)
  2415. {
  2416. tar_stat_close (st);
  2417. xheader_xattr_free (st->xattr_map, st->xattr_map_size);
  2418. free (st->orig_file_name);
  2419. free (st->file_name);
  2420. free (st->link_name);
  2421. free (st->uname);
  2422. free (st->gname);
  2423. free (st->cntx_name);
  2424. free (st->acls_a_ptr);
  2425. free (st->acls_d_ptr);
  2426. free (st->sparse_map);
  2427. free (st->dumpdir);
  2428. xheader_destroy (&st->xhdr);
  2429. memset (st, 0, sizeof (*st));
  2430. }
  2431. /* Format mask for all available formats that support nanosecond
  2432. timestamp resolution. */
  2433. #define NS_PRECISION_FORMAT_MASK FORMAT_MASK (POSIX_FORMAT)
  2434. /* Same as timespec_cmp, but ignore nanoseconds if current archive
  2435. format does not provide sufficient resolution. */
  2436. int
  2437. tar_timespec_cmp (struct timespec a, struct timespec b)
  2438. {
  2439. if (!(FORMAT_MASK (current_format) & NS_PRECISION_FORMAT_MASK))
  2440. a.tv_nsec = b.tv_nsec = 0;
  2441. return timespec_cmp (a, b);
  2442. }
  2443. /* Set tar exit status to VAL, unless it is already indicating
  2444. a more serious condition. This relies on the fact that the
  2445. values of TAREXIT_ constants are ranged by severity. */
  2446. void
  2447. set_exit_status (int val)
  2448. {
  2449. if (val > exit_status)
  2450. exit_status = val;
  2451. }