tar.c 59 KB

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