tar.c 76 KB

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