4
0

tar.c 75 KB

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