tar.c 67 KB

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