4
0

tar.c 71 KB

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