tar.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256
  1. /* A tar (tape archiver) program.
  2. Copyright 1988, 92,93,94,95,96,97, 1999 Free Software Foundation, Inc.
  3. Written by John Gilmore, starting 1985-08-25.
  4. This program is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by the
  6. Free Software Foundation; either version 2, or (at your option) any later
  7. version.
  8. This program is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
  11. Public License for more details.
  12. You should have received a copy of the GNU General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  15. #include "system.h"
  16. #include <getopt.h>
  17. #include <signal.h>
  18. #if ! defined SIGCHLD && defined SIGCLD
  19. # define SIGCHLD SIGCLD
  20. #endif
  21. #include <time.h>
  22. #ifndef time
  23. time_t time ();
  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 "quotearg.h"
  32. #include "xstrtol.h"
  33. time_t get_date ();
  34. /* Local declarations. */
  35. #ifndef DEFAULT_ARCHIVE
  36. # define DEFAULT_ARCHIVE "tar.out"
  37. #endif
  38. #ifndef DEFAULT_BLOCKING
  39. # define DEFAULT_BLOCKING 20
  40. #endif
  41. static void usage PARAMS ((int)) __attribute__ ((noreturn));
  42. /* Miscellaneous. */
  43. /*----------------------------------------------.
  44. | Doesn't return if stdin already requested. |
  45. `----------------------------------------------*/
  46. /* Name of option using stdin. */
  47. static const char *stdin_used_by;
  48. void
  49. request_stdin (const char *option)
  50. {
  51. if (stdin_used_by)
  52. USAGE_ERROR ((0, 0, _("Options `-%s' and `-%s' both want standard input"),
  53. stdin_used_by, option));
  54. stdin_used_by = option;
  55. }
  56. /*--------------------------------------------------------.
  57. | Returns true if and only if the user typed 'y' or 'Y'. |
  58. `--------------------------------------------------------*/
  59. int
  60. confirm (const char *message_action, const char *message_name)
  61. {
  62. static FILE *confirm_file;
  63. static int confirm_file_EOF;
  64. if (!confirm_file)
  65. {
  66. if (archive == 0 || stdin_used_by)
  67. {
  68. confirm_file = fopen (TTY_NAME, "r");
  69. if (! confirm_file)
  70. open_fatal (TTY_NAME);
  71. }
  72. else
  73. {
  74. request_stdin ("-w");
  75. confirm_file = stdin;
  76. }
  77. }
  78. fprintf (stdlis, "%s %s?", message_action, quote (message_name));
  79. fflush (stdlis);
  80. {
  81. int reply = confirm_file_EOF ? EOF : getc (confirm_file);
  82. int character;
  83. for (character = reply;
  84. character != '\n';
  85. character = getc (confirm_file))
  86. if (character == EOF)
  87. {
  88. confirm_file_EOF = 1;
  89. fputc ('\n', stdlis);
  90. fflush (stdlis);
  91. break;
  92. }
  93. return reply == 'y' || reply == 'Y';
  94. }
  95. }
  96. /* Options. */
  97. /* For long options that unconditionally set a single flag, we have getopt
  98. do it. For the others, we share the code for the equivalent short
  99. named option, the name of which is stored in the otherwise-unused `val'
  100. field of the `struct option'; for long options that have no equivalent
  101. short option, we use non-characters as pseudo short options,
  102. starting at CHAR_MAX + 1 and going upwards. */
  103. enum
  104. {
  105. BACKUP_OPTION = CHAR_MAX + 1,
  106. DELETE_OPTION,
  107. EXCLUDE_OPTION,
  108. GROUP_OPTION,
  109. MODE_OPTION,
  110. NEWER_MTIME_OPTION,
  111. NO_RECURSE_OPTION,
  112. NULL_OPTION,
  113. OVERWRITE_OPTION,
  114. OWNER_OPTION,
  115. POSIX_OPTION,
  116. PRESERVE_OPTION,
  117. RECORD_SIZE_OPTION,
  118. RSH_COMMAND_OPTION,
  119. SUFFIX_OPTION,
  120. USE_COMPRESS_PROGRAM_OPTION,
  121. VOLNO_FILE_OPTION,
  122. /* Some cleanup is being made in GNU tar long options. Using old names is
  123. allowed for a while, but will also send a warning to stderr. Take old
  124. names out in 1.14, or in summer 1997, whichever happens last. */
  125. OBSOLETE_ABSOLUTE_NAMES,
  126. OBSOLETE_BLOCK_COMPRESS,
  127. OBSOLETE_BLOCKING_FACTOR,
  128. OBSOLETE_BLOCK_NUMBER,
  129. OBSOLETE_READ_FULL_RECORDS,
  130. OBSOLETE_TOUCH,
  131. OBSOLETE_VERSION_CONTROL
  132. };
  133. /* If nonzero, display usage information and exit. */
  134. static int show_help;
  135. /* If nonzero, print the version on standard output and exit. */
  136. static int show_version;
  137. struct option long_options[] =
  138. {
  139. {"absolute-names", no_argument, 0, 'P'},
  140. {"absolute-paths", no_argument, 0, OBSOLETE_ABSOLUTE_NAMES},
  141. {"after-date", required_argument, 0, 'N'},
  142. {"append", no_argument, 0, 'r'},
  143. {"atime-preserve", no_argument, &atime_preserve_option, 1},
  144. {"backup", optional_argument, 0, BACKUP_OPTION},
  145. {"block-compress", no_argument, 0, OBSOLETE_BLOCK_COMPRESS},
  146. {"block-number", no_argument, 0, 'R'},
  147. {"block-size", required_argument, 0, OBSOLETE_BLOCKING_FACTOR},
  148. {"blocking-factor", required_argument, 0, 'b'},
  149. {"bzip2", no_argument, 0, 'I'},
  150. {"catenate", no_argument, 0, 'A'},
  151. {"checkpoint", no_argument, &checkpoint_option, 1},
  152. {"compare", no_argument, 0, 'd'},
  153. {"compress", no_argument, 0, 'Z'},
  154. {"concatenate", no_argument, 0, 'A'},
  155. {"confirmation", no_argument, 0, 'w'},
  156. /* FIXME: --selective as a synonym for --confirmation? */
  157. {"create", no_argument, 0, 'c'},
  158. {"delete", no_argument, 0, DELETE_OPTION},
  159. {"dereference", no_argument, 0, 'h'},
  160. {"diff", no_argument, 0, 'd'},
  161. {"directory", required_argument, 0, 'C'},
  162. {"exclude", required_argument, 0, EXCLUDE_OPTION},
  163. {"exclude-from", required_argument, 0, 'X'},
  164. {"extract", no_argument, 0, 'x'},
  165. {"file", required_argument, 0, 'f'},
  166. {"files-from", required_argument, 0, 'T'},
  167. {"force-local", no_argument, &force_local_option, 1},
  168. {"get", no_argument, 0, 'x'},
  169. {"group", required_argument, 0, GROUP_OPTION},
  170. {"gunzip", no_argument, 0, 'z'},
  171. {"gzip", no_argument, 0, 'z'},
  172. {"help", no_argument, &show_help, 1},
  173. {"ignore-failed-read", no_argument, &ignore_failed_read_option, 1},
  174. {"ignore-zeros", no_argument, 0, 'i'},
  175. /* FIXME: --ignore-end as a new name for --ignore-zeros? */
  176. {"incremental", no_argument, 0, 'G'},
  177. {"info-script", required_argument, 0, 'F'},
  178. {"interactive", no_argument, 0, 'w'},
  179. {"keep-old-files", no_argument, 0, 'k'},
  180. {"label", required_argument, 0, 'V'},
  181. {"list", no_argument, 0, 't'},
  182. {"listed-incremental", required_argument, 0, 'g'},
  183. {"mode", required_argument, 0, MODE_OPTION},
  184. {"modification-time", no_argument, 0, OBSOLETE_TOUCH},
  185. {"multi-volume", no_argument, 0, 'M'},
  186. {"new-volume-script", required_argument, 0, 'F'},
  187. {"newer", required_argument, 0, 'N'},
  188. {"newer-mtime", required_argument, 0, NEWER_MTIME_OPTION},
  189. {"null", no_argument, 0, NULL_OPTION},
  190. {"no-recursion", no_argument, 0, NO_RECURSE_OPTION},
  191. {"no-same-owner", no_argument, &same_owner_option, -1},
  192. {"no-same-permissions", no_argument, &same_permissions_option, -1},
  193. {"numeric-owner", no_argument, &numeric_owner_option, 1},
  194. {"old-archive", no_argument, 0, 'o'},
  195. {"one-file-system", no_argument, 0, 'l'},
  196. {"overwrite", no_argument, 0, OVERWRITE_OPTION},
  197. {"owner", required_argument, 0, OWNER_OPTION},
  198. {"portability", no_argument, 0, 'o'},
  199. {"posix", no_argument, 0, POSIX_OPTION},
  200. {"preserve", no_argument, 0, PRESERVE_OPTION},
  201. {"preserve-order", no_argument, 0, 's'},
  202. {"preserve-permissions", no_argument, 0, 'p'},
  203. {"recursive-unlink", no_argument, &recursive_unlink_option, 1},
  204. {"read-full-blocks", no_argument, 0, OBSOLETE_READ_FULL_RECORDS},
  205. {"read-full-records", no_argument, 0, 'B'},
  206. /* FIXME: --partial-blocks might be a synonym for --read-full-records? */
  207. {"record-number", no_argument, 0, OBSOLETE_BLOCK_NUMBER},
  208. {"record-size", required_argument, 0, RECORD_SIZE_OPTION},
  209. {"remove-files", no_argument, &remove_files_option, 1},
  210. {"rsh-command", required_argument, 0, RSH_COMMAND_OPTION},
  211. {"same-order", no_argument, 0, 's'},
  212. {"same-owner", no_argument, &same_owner_option, 1},
  213. {"same-permissions", no_argument, 0, 'p'},
  214. {"show-omitted-dirs", no_argument, &show_omitted_dirs_option, 1},
  215. {"sparse", no_argument, 0, 'S'},
  216. {"starting-file", required_argument, 0, 'K'},
  217. {"suffix", required_argument, 0, SUFFIX_OPTION},
  218. {"tape-length", required_argument, 0, 'L'},
  219. {"to-stdout", no_argument, 0, 'O'},
  220. {"totals", no_argument, &totals_option, 1},
  221. {"touch", no_argument, 0, 'm'},
  222. {"uncompress", no_argument, 0, 'Z'},
  223. {"ungzip", no_argument, 0, 'z'},
  224. {"unlink-first", no_argument, 0, 'U'},
  225. {"update", no_argument, 0, 'u'},
  226. {"use-compress-program", required_argument, 0, USE_COMPRESS_PROGRAM_OPTION},
  227. {"verbose", no_argument, 0, 'v'},
  228. {"verify", no_argument, 0, 'W'},
  229. {"version", no_argument, &show_version, 1},
  230. {"version-control", required_argument, 0, OBSOLETE_VERSION_CONTROL},
  231. {"volno-file", required_argument, 0, VOLNO_FILE_OPTION},
  232. {0, 0, 0, 0}
  233. };
  234. /*---------------------------------------------.
  235. | Print a usage message and exit with STATUS. |
  236. `---------------------------------------------*/
  237. static void
  238. usage (int status)
  239. {
  240. if (status != TAREXIT_SUCCESS)
  241. fprintf (stderr, _("Try `%s --help' for more information.\n"),
  242. program_name);
  243. else
  244. {
  245. fputs (_("\
  246. GNU `tar' saves many files together into a single tape or disk archive, and\n\
  247. can restore individual files from the archive.\n"),
  248. stdout);
  249. printf (_("\nUsage: %s [OPTION]... [FILE]...\n\
  250. \n\
  251. Examples:\n\
  252. %s -cf archive.tar foo bar # Create archive.tar from files foo and bar.\n\
  253. %s -tvf archive.tar # List all files in archive.tar verbosely.\n\
  254. %s -xf archive.tar # Extract all files from archive.tar.\n"),
  255. program_name, program_name, program_name, program_name);
  256. fputs (_("\
  257. \n\
  258. If a long option shows an argument as mandatory, then it is mandatory\n\
  259. for the equivalent short option also. Similarly for optional arguments.\n"),
  260. stdout);
  261. fputs(_("\
  262. \n\
  263. Main operation mode:\n\
  264. -t, --list list the contents of an archive\n\
  265. -x, --extract, --get extract files from an archive\n\
  266. -c, --create create a new archive\n\
  267. -d, --diff, --compare find differences between archive and file system\n\
  268. -r, --append append files to the end of an archive\n\
  269. -u, --update only append files newer than copy in archive\n\
  270. -A, --catenate append tar files to an archive\n\
  271. --concatenate same as -A\n\
  272. --delete delete from the archive (not on mag tapes!)\n"),
  273. stdout);
  274. fputs (_("\
  275. \n\
  276. Operation modifiers:\n\
  277. -W, --verify attempt to verify the archive after writing it\n\
  278. --remove-files remove files after adding them to the archive\n\
  279. -k, --keep-old-files don't replace existing files when extracting\n\
  280. --overwrite overwrite existing files when extracting\n\
  281. -U, --unlink-first remove each file prior to extracting over it\n\
  282. --recursive-unlink empty hierarchies prior to extracting directory\n\
  283. -S, --sparse handle sparse files efficiently\n\
  284. -O, --to-stdout extract files to standard output\n\
  285. -G, --incremental handle old GNU-format incremental backup\n\
  286. -g, --listed-incremental=FILE\n\
  287. handle new GNU-format incremental backup\n\
  288. --ignore-failed-read do not exit with nonzero on unreadable files\n"),
  289. stdout);
  290. fputs (_("\
  291. \n\
  292. Handling of file attributes:\n\
  293. --owner=NAME force NAME as owner for added files\n\
  294. --group=NAME force NAME as group for added files\n\
  295. --mode=CHANGES force (symbolic) mode CHANGES for added files\n\
  296. --atime-preserve don't change access times on dumped files\n\
  297. -m, --modification-time don't extract file modified time\n\
  298. --same-owner try extracting files with the same ownership\n\
  299. --no-same-owner extract files as yourself\n\
  300. --numeric-owner always use numbers for user/group names\n\
  301. -p, --same-permissions extract permissions information\n\
  302. --no-same-permissions do not extract permissions information\n\
  303. --preserve-permissions same as -p\n\
  304. -s, --same-order sort names to extract to match archive\n\
  305. --preserve-order same as -s\n\
  306. --preserve same as both -p and -s\n"),
  307. stdout);
  308. fputs (_("\
  309. \n\
  310. Device selection and switching:\n\
  311. -f, --file=ARCHIVE use archive file or device ARCHIVE\n\
  312. --force-local archive file is local even if has a colon\n\
  313. --rsh-command=COMMAND use remote COMMAND instead of rsh\n\
  314. -[0-7][lmh] specify drive and density\n\
  315. -M, --multi-volume create/list/extract multi-volume archive\n\
  316. -L, --tape-length=NUM change tape after writing NUM x 1024 bytes\n\
  317. -F, --info-script=FILE run script at end of each tape (implies -M)\n\
  318. --new-volume-script=FILE same as -F FILE\n\
  319. --volno-file=FILE use/update the volume number in FILE\n"),
  320. stdout);
  321. fputs (_("\
  322. \n\
  323. Device blocking:\n\
  324. -b, --blocking-factor=BLOCKS BLOCKS x 512 bytes per record\n\
  325. --record-size=SIZE SIZE bytes per record, multiple of 512\n\
  326. -i, --ignore-zeros ignore zeroed blocks in archive (means EOF)\n\
  327. -B, --read-full-records reblock as we read (for 4.2BSD pipes)\n"),
  328. stdout);
  329. fputs (_("\
  330. \n\
  331. Archive format selection:\n\
  332. -V, --label=NAME create archive with volume name NAME\n\
  333. PATTERN at list/extract time, a globbing PATTERN\n\
  334. -o, --old-archive, --portability write a V7 format archive\n\
  335. --posix write a POSIX format archive\n\
  336. -I, --bzip2 filter the archive through bzip2\n\
  337. -z, --gzip, --ungzip filter the archive through gzip\n\
  338. -Z, --compress, --uncompress filter the archive through compress\n\
  339. --use-compress-program=PROG filter through PROG (must accept -d)\n"),
  340. stdout);
  341. fputs (_("\
  342. \n\
  343. Local file selection:\n\
  344. -C, --directory=DIR change to directory DIR\n\
  345. -T, --files-from=NAME get names to extract or create from file NAME\n\
  346. --null -T reads null-terminated names, disable -C\n\
  347. --exclude=PATTERN exclude files, given as a globbing PATTERN\n\
  348. -X, --exclude-from=FILE exclude globbing patterns listed in FILE\n\
  349. -P, --absolute-names don't strip leading `/'s from file names\n\
  350. -h, --dereference dump instead the files symlinks point to\n\
  351. --no-recursion avoid descending automatically in directories\n\
  352. -l, --one-file-system stay in local file system when creating archive\n\
  353. -K, --starting-file=NAME begin at file NAME in the archive\n"),
  354. stdout);
  355. #if !MSDOS
  356. fputs (_("\
  357. -N, --newer=DATE only store files newer than DATE\n\
  358. --newer-mtime compare date and time when data changed only\n\
  359. --after-date=DATE same as -N\n"),
  360. stdout);
  361. #endif
  362. fputs (_("\
  363. --backup[=CONTROL] backup before removal, choose version control\n\
  364. --suffix=SUFFIX backup before removal, override usual suffix\n"),
  365. stdout);
  366. fputs (_("\
  367. \n\
  368. Informative output:\n\
  369. --help print this help, then exit\n\
  370. --version print tar program version number, then exit\n\
  371. -v, --verbose verbosely list files processed\n\
  372. --checkpoint print directory names while reading the archive\n\
  373. --totals print total bytes written while creating archive\n\
  374. -R, --block-number show block number within archive with each message\n\
  375. -w, --interactive ask for confirmation for every action\n\
  376. --confirmation same as -w\n"),
  377. stdout);
  378. fputs (_("\
  379. \n\
  380. The backup suffix is `~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.\n\
  381. The version control may be set with --backup or VERSION_CONTROL, values are:\n\
  382. \n\
  383. t, numbered make numbered backups\n\
  384. nil, existing numbered if numbered backups exist, simple otherwise\n\
  385. never, simple always make simple backups\n"),
  386. stdout);
  387. printf (_("\
  388. \n\
  389. GNU tar cannot read nor produce `--posix' archives. If POSIXLY_CORRECT\n\
  390. is set in the environment, GNU extensions are disallowed with `--posix'.\n\
  391. Support for POSIX is only partially implemented, don't count on it yet.\n\
  392. ARCHIVE may be FILE, HOST:FILE or USER@HOST:FILE; and FILE may be a file\n\
  393. or a device. *This* `tar' defaults to `-f%s -b%d'.\n"),
  394. DEFAULT_ARCHIVE, DEFAULT_BLOCKING);
  395. fputs (_("\nReport bugs to <bug-tar@gnu.org>.\n"), stdout);
  396. }
  397. exit (status);
  398. }
  399. /*----------------------------.
  400. | Parse the options for tar. |
  401. `----------------------------*/
  402. /* Available option letters are DEHJQY and aejnqy. Some are reserved:
  403. y per-file gzip compression
  404. Y per-block gzip compression */
  405. #define OPTION_STRING \
  406. "-01234567ABC:F:GIK:L:MN:OPRST:UV:WX:Zb:cdf:g:hiklmoprstuvwxz"
  407. static void
  408. set_subcommand_option (enum subcommand subcommand)
  409. {
  410. if (subcommand_option != UNKNOWN_SUBCOMMAND
  411. && subcommand_option != subcommand)
  412. USAGE_ERROR ((0, 0,
  413. _("You may not specify more than one `-Acdtrux' option")));
  414. subcommand_option = subcommand;
  415. }
  416. static void
  417. set_use_compress_program_option (const char *string)
  418. {
  419. if (use_compress_program_option && strcmp (use_compress_program_option, string) != 0)
  420. USAGE_ERROR ((0, 0, _("Conflicting compression options")));
  421. use_compress_program_option = string;
  422. }
  423. /* Ignore DUMMY (which will always be null in practice), and add
  424. PATTERN to the proper set of patterns to be excluded -- either
  425. patterns with slashes, or patterns without. */
  426. static void
  427. add_filtered_exclude (struct exclude *dummy, char const *pattern)
  428. {
  429. add_exclude ((strchr (pattern, '/')
  430. ? excluded_with_slash
  431. : excluded_without_slash),
  432. pattern);
  433. }
  434. static void
  435. decode_options (int argc, char *const *argv)
  436. {
  437. int optchar; /* option letter */
  438. int input_files; /* number of input files */
  439. const char *backup_suffix_string;
  440. const char *version_control_string = 0;
  441. /* Set some default option values. */
  442. subcommand_option = UNKNOWN_SUBCOMMAND;
  443. archive_format = DEFAULT_FORMAT;
  444. blocking_factor = DEFAULT_BLOCKING;
  445. record_size = DEFAULT_BLOCKING * BLOCKSIZE;
  446. excluded_with_slash = new_exclude ();
  447. excluded_without_slash = new_exclude ();
  448. newer_mtime_option = TYPE_MINIMUM (time_t);
  449. owner_option = -1;
  450. group_option = -1;
  451. backup_suffix_string = getenv ("SIMPLE_BACKUP_SUFFIX");
  452. /* Convert old-style tar call by exploding option element and rearranging
  453. options accordingly. */
  454. if (argc > 1 && argv[1][0] != '-')
  455. {
  456. int new_argc; /* argc value for rearranged arguments */
  457. char **new_argv; /* argv value for rearranged arguments */
  458. char *const *in; /* cursor into original argv */
  459. char **out; /* cursor into rearranged argv */
  460. const char *letter; /* cursor into old option letters */
  461. char buffer[3]; /* constructed option buffer */
  462. const char *cursor; /* cursor in OPTION_STRING */
  463. /* Initialize a constructed option. */
  464. buffer[0] = '-';
  465. buffer[2] = '\0';
  466. /* Allocate a new argument array, and copy program name in it. */
  467. new_argc = argc - 1 + strlen (argv[1]);
  468. new_argv = xmalloc (new_argc * sizeof (char *));
  469. in = argv;
  470. out = new_argv;
  471. *out++ = *in++;
  472. /* Copy each old letter option as a separate option, and have the
  473. corresponding argument moved next to it. */
  474. for (letter = *in++; *letter; letter++)
  475. {
  476. buffer[1] = *letter;
  477. *out++ = xstrdup (buffer);
  478. cursor = strchr (OPTION_STRING, *letter);
  479. if (cursor && cursor[1] == ':')
  480. {
  481. if (in < argv + argc)
  482. *out++ = *in++;
  483. else
  484. USAGE_ERROR ((0, 0, _("Old option `%c' requires an argument."),
  485. *letter));
  486. }
  487. }
  488. /* Copy all remaining options. */
  489. while (in < argv + argc)
  490. *out++ = *in++;
  491. /* Replace the old option list by the new one. */
  492. argc = new_argc;
  493. argv = new_argv;
  494. }
  495. /* Parse all options and non-options as they appear. */
  496. input_files = 0;
  497. while (optchar = getopt_long (argc, argv, OPTION_STRING, long_options, 0),
  498. optchar != -1)
  499. switch (optchar)
  500. {
  501. case '?':
  502. usage (TAREXIT_FAILURE);
  503. case 0:
  504. break;
  505. case 1:
  506. /* File name or non-parsed option, because of RETURN_IN_ORDER
  507. ordering triggered by the leading dash in OPTION_STRING. */
  508. name_add (optarg);
  509. input_files++;
  510. break;
  511. case 'A':
  512. set_subcommand_option (CAT_SUBCOMMAND);
  513. break;
  514. case OBSOLETE_BLOCK_COMPRESS:
  515. WARN ((0, 0, _("Obsolete option, now implied by --blocking-factor")));
  516. break;
  517. case OBSOLETE_BLOCKING_FACTOR:
  518. WARN ((0, 0, _("Obsolete option name replaced by --blocking-factor")));
  519. /* Fall through. */
  520. case 'b':
  521. {
  522. uintmax_t u;
  523. if (! (xstrtoumax (optarg, 0, 10, &u, "") == LONGINT_OK
  524. && u == (blocking_factor = u)
  525. && 0 < blocking_factor
  526. && u == (record_size = u * BLOCKSIZE) / BLOCKSIZE))
  527. USAGE_ERROR ((0, 0, _("Invalid blocking factor")));
  528. }
  529. break;
  530. case OBSOLETE_READ_FULL_RECORDS:
  531. WARN ((0, 0,
  532. _("Obsolete option name replaced by --read-full-records")));
  533. /* Fall through. */
  534. case 'B':
  535. /* Try to reblock input records. For reading 4.2BSD pipes. */
  536. /* It would surely make sense to exchange -B and -R, but it seems
  537. that -B has been used for a long while in Sun tar ans most
  538. BSD-derived systems. This is a consequence of the block/record
  539. terminology confusion. */
  540. read_full_records_option = 1;
  541. break;
  542. case 'c':
  543. set_subcommand_option (CREATE_SUBCOMMAND);
  544. break;
  545. case 'C':
  546. name_add ("-C");
  547. name_add (optarg);
  548. break;
  549. case 'd':
  550. set_subcommand_option (DIFF_SUBCOMMAND);
  551. break;
  552. case 'f':
  553. if (archive_names == allocated_archive_names)
  554. {
  555. allocated_archive_names *= 2;
  556. archive_name_array =
  557. xrealloc (archive_name_array,
  558. sizeof (const char *) * allocated_archive_names);
  559. }
  560. archive_name_array[archive_names++] = optarg;
  561. break;
  562. case 'F':
  563. /* Since -F is only useful with -M, make it implied. Run this
  564. script at the end of each tape. */
  565. info_script_option = optarg;
  566. multi_volume_option = 1;
  567. break;
  568. case 'g':
  569. listed_incremental_option = optarg;
  570. after_date_option = 1;
  571. /* Fall through. */
  572. case 'G':
  573. /* We are making an incremental dump (FIXME: are we?); save
  574. directories at the beginning of the archive, and include in each
  575. directory its contents. */
  576. incremental_option = 1;
  577. break;
  578. case 'h':
  579. /* Follow symbolic links. */
  580. dereference_option = 1;
  581. break;
  582. case 'i':
  583. /* Ignore zero blocks (eofs). This can't be the default,
  584. because Unix tar writes two blocks of zeros, then pads out
  585. the record with garbage. */
  586. ignore_zeros_option = 1;
  587. break;
  588. case 'I':
  589. set_use_compress_program_option ("bzip2");
  590. break;
  591. case 'k':
  592. /* Don't replace existing files. */
  593. old_files_option = KEEP_OLD_FILES;
  594. break;
  595. case 'K':
  596. starting_file_option = 1;
  597. addname (optarg, 0);
  598. break;
  599. case 'l':
  600. /* When dumping directories, don't dump files/subdirectories
  601. that are on other filesystems. */
  602. one_file_system_option = 1;
  603. break;
  604. case 'L':
  605. {
  606. uintmax_t u;
  607. if (xstrtoumax (optarg, 0, 10, &u, "") != LONGINT_OK)
  608. USAGE_ERROR ((0, 0, _("Invalid tape length")));
  609. tape_length_option = 1024 * (tarlong) u;
  610. multi_volume_option = 1;
  611. }
  612. break;
  613. case OBSOLETE_TOUCH:
  614. WARN ((0, 0, _("Obsolete option name replaced by --touch")));
  615. /* Fall through. */
  616. case 'm':
  617. touch_option = 1;
  618. break;
  619. case 'M':
  620. /* Make multivolume archive: when we can't write any more into
  621. the archive, re-open it, and continue writing. */
  622. multi_volume_option = 1;
  623. break;
  624. #if !MSDOS
  625. case 'N':
  626. after_date_option = 1;
  627. /* Fall through. */
  628. case NEWER_MTIME_OPTION:
  629. if (newer_mtime_option != TYPE_MINIMUM (time_t))
  630. USAGE_ERROR ((0, 0, _("More than one threshold date")));
  631. newer_mtime_option = get_date (optarg, 0);
  632. if (newer_mtime_option == (time_t) -1)
  633. WARN ((0, 0, _("Substituting %s for unknown date format %s"),
  634. tartime (newer_mtime_option), quote (optarg)));
  635. break;
  636. #endif /* not MSDOS */
  637. case 'o':
  638. if (archive_format == DEFAULT_FORMAT)
  639. archive_format = V7_FORMAT;
  640. else if (archive_format != V7_FORMAT)
  641. USAGE_ERROR ((0, 0, _("Conflicting archive format options")));
  642. break;
  643. case 'O':
  644. to_stdout_option = 1;
  645. break;
  646. case 'p':
  647. same_permissions_option = 1;
  648. break;
  649. case OBSOLETE_ABSOLUTE_NAMES:
  650. WARN ((0, 0, _("Obsolete option name replaced by --absolute-names")));
  651. /* Fall through. */
  652. case 'P':
  653. absolute_names_option = 1;
  654. break;
  655. case 'r':
  656. set_subcommand_option (APPEND_SUBCOMMAND);
  657. break;
  658. case OBSOLETE_BLOCK_NUMBER:
  659. WARN ((0, 0, _("Obsolete option name replaced by --block-number")));
  660. /* Fall through. */
  661. case 'R':
  662. /* Print block numbers for debugging bad tar archives. */
  663. /* It would surely make sense to exchange -B and -R, but it seems
  664. that -B has been used for a long while in Sun tar ans most
  665. BSD-derived systems. This is a consequence of the block/record
  666. terminology confusion. */
  667. block_number_option = 1;
  668. break;
  669. case 's':
  670. /* Names to extr are sorted. */
  671. same_order_option = 1;
  672. break;
  673. case 'S':
  674. sparse_option = 1;
  675. break;
  676. case 't':
  677. set_subcommand_option (LIST_SUBCOMMAND);
  678. verbose_option++;
  679. break;
  680. case 'T':
  681. files_from_option = optarg;
  682. break;
  683. case 'u':
  684. set_subcommand_option (UPDATE_SUBCOMMAND);
  685. break;
  686. case 'U':
  687. old_files_option = UNLINK_FIRST_OLD_FILES;
  688. break;
  689. case 'v':
  690. verbose_option++;
  691. break;
  692. case 'V':
  693. volume_label_option = optarg;
  694. break;
  695. case 'w':
  696. interactive_option = 1;
  697. break;
  698. case 'W':
  699. verify_option = 1;
  700. break;
  701. case 'x':
  702. set_subcommand_option (EXTRACT_SUBCOMMAND);
  703. break;
  704. case 'X':
  705. if (add_exclude_file (add_filtered_exclude, 0, optarg, '\n') != 0)
  706. {
  707. int e = errno;
  708. FATAL_ERROR ((0, e, "%s", quotearg_colon (optarg)));
  709. }
  710. break;
  711. case 'z':
  712. set_use_compress_program_option ("gzip");
  713. break;
  714. case 'Z':
  715. set_use_compress_program_option ("compress");
  716. break;
  717. case OBSOLETE_VERSION_CONTROL:
  718. WARN ((0, 0, _("Obsolete option name replaced by --backup")));
  719. /* Fall through. */
  720. case BACKUP_OPTION:
  721. backup_option = 1;
  722. if (optarg)
  723. version_control_string = optarg;
  724. break;
  725. case DELETE_OPTION:
  726. set_subcommand_option (DELETE_SUBCOMMAND);
  727. break;
  728. case EXCLUDE_OPTION:
  729. add_filtered_exclude (0, optarg);
  730. break;
  731. case GROUP_OPTION:
  732. if (! (strlen (optarg) < GNAME_FIELD_SIZE
  733. && gname_to_gid (optarg, &group_option)))
  734. {
  735. uintmax_t g;
  736. if (xstrtoumax (optarg, 0, 10, &g, "") == LONGINT_OK
  737. && g == (gid_t) g)
  738. group_option = g;
  739. else
  740. FATAL_ERROR ((0, 0, _("Invalid group given on option")));
  741. }
  742. break;
  743. case MODE_OPTION:
  744. mode_option
  745. = mode_compile (optarg,
  746. MODE_MASK_EQUALS | MODE_MASK_PLUS | MODE_MASK_MINUS);
  747. if (mode_option == MODE_INVALID)
  748. FATAL_ERROR ((0, 0, _("Invalid mode given on option")));
  749. if (mode_option == MODE_MEMORY_EXHAUSTED)
  750. xalloc_die ();
  751. break;
  752. case NO_RECURSE_OPTION:
  753. no_recurse_option = 1;
  754. break;
  755. case NULL_OPTION:
  756. filename_terminator = '\0';
  757. break;
  758. case OVERWRITE_OPTION:
  759. old_files_option = OVERWRITE_OLD_FILES;
  760. break;
  761. case OWNER_OPTION:
  762. if (! (strlen (optarg) < UNAME_FIELD_SIZE
  763. && uname_to_uid (optarg, &owner_option)))
  764. {
  765. uintmax_t u;
  766. if (xstrtoumax (optarg, 0, 10, &u, "") == LONGINT_OK
  767. && u == (uid_t) u)
  768. owner_option = u;
  769. else
  770. FATAL_ERROR ((0, 0, _("Invalid owner given on option")));
  771. }
  772. break;
  773. case POSIX_OPTION:
  774. #if OLDGNU_COMPATIBILITY
  775. if (archive_format == DEFAULT_FORMAT)
  776. archive_format = GNU_FORMAT;
  777. else if (archive_format != GNU_FORMAT)
  778. USAGE_ERROR ((0, 0, _("Conflicting archive format options")));
  779. #else
  780. if (archive_format == DEFAULT_FORMAT)
  781. archive_format = POSIX_FORMAT;
  782. else if (archive_format != POSIX_FORMAT)
  783. USAGE_ERROR ((0, 0, _("Conflicting archive format options")));
  784. #endif
  785. break;
  786. case PRESERVE_OPTION:
  787. same_permissions_option = 1;
  788. same_order_option = 1;
  789. break;
  790. case RECORD_SIZE_OPTION:
  791. {
  792. uintmax_t u;
  793. if (! (xstrtoumax (optarg, 0, 10, &u, "") == LONGINT_OK
  794. && u == (size_t) u))
  795. USAGE_ERROR ((0, 0, _("Invalid record size")));
  796. record_size = u;
  797. if (record_size % BLOCKSIZE != 0)
  798. USAGE_ERROR ((0, 0, _("Record size must be a multiple of %d."),
  799. BLOCKSIZE));
  800. blocking_factor = record_size / BLOCKSIZE;
  801. }
  802. break;
  803. case RSH_COMMAND_OPTION:
  804. rsh_command_option = optarg;
  805. break;
  806. case SUFFIX_OPTION:
  807. backup_option = 1;
  808. backup_suffix_string = optarg;
  809. break;
  810. case VOLNO_FILE_OPTION:
  811. volno_file_option = optarg;
  812. break;
  813. case USE_COMPRESS_PROGRAM_OPTION:
  814. set_use_compress_program_option (optarg);
  815. break;
  816. case '0':
  817. case '1':
  818. case '2':
  819. case '3':
  820. case '4':
  821. case '5':
  822. case '6':
  823. case '7':
  824. #ifdef DEVICE_PREFIX
  825. {
  826. int device = optchar - '0';
  827. int density;
  828. static char buf[sizeof DEVICE_PREFIX + 10];
  829. char *cursor;
  830. density = getopt_long (argc, argv, "lmh", 0, 0);
  831. strcpy (buf, DEVICE_PREFIX);
  832. cursor = buf + strlen (buf);
  833. #ifdef DENSITY_LETTER
  834. sprintf (cursor, "%d%c", device, density);
  835. #else /* not DENSITY_LETTER */
  836. switch (density)
  837. {
  838. case 'l':
  839. #ifdef LOW_NUM
  840. device += LOW_NUM;
  841. #endif
  842. break;
  843. case 'm':
  844. #ifdef MID_NUM
  845. device += MID_NUM;
  846. #else
  847. device += 8;
  848. #endif
  849. break;
  850. case 'h':
  851. #ifdef HGH_NUM
  852. device += HGH_NUM;
  853. #else
  854. device += 16;
  855. #endif
  856. break;
  857. default:
  858. usage (TAREXIT_FAILURE);
  859. }
  860. sprintf (cursor, "%d", device);
  861. #endif /* not DENSITY_LETTER */
  862. if (archive_names == allocated_archive_names)
  863. {
  864. allocated_archive_names *= 2;
  865. archive_name_array =
  866. xrealloc (archive_name_array,
  867. sizeof (const char *) * allocated_archive_names);
  868. }
  869. archive_name_array[archive_names++] = buf;
  870. /* FIXME: How comes this works for many archives when buf is
  871. not xstrdup'ed? */
  872. }
  873. break;
  874. #else /* not DEVICE_PREFIX */
  875. USAGE_ERROR ((0, 0,
  876. _("Options `-[0-7][lmh]' not supported by *this* tar")));
  877. #endif /* not DEVICE_PREFIX */
  878. }
  879. /* Handle operands after any "--" argument. */
  880. for (; optind < argc; optind++)
  881. {
  882. name_add (argv[optind]);
  883. input_files++;
  884. }
  885. /* Process trivial options. */
  886. if (show_version)
  887. {
  888. printf ("tar (GNU %s) %s\n%s\n%s\n%s\n", PACKAGE, VERSION,
  889. "Copyright 1999 Free Software Foundation, Inc.",
  890. _("\
  891. This program comes with NO WARRANTY, to the extent permitted by law.\n\
  892. You may redistribute it under the terms of the GNU General Public License;\n\
  893. see the file named COPYING for details."),
  894. _("Written by John Gilmore and Jay Fenlason."));
  895. exit (TAREXIT_SUCCESS);
  896. }
  897. if (show_help)
  898. usage (TAREXIT_SUCCESS);
  899. /* Derive option values and check option consistency. */
  900. if (archive_format == DEFAULT_FORMAT)
  901. {
  902. #if OLDGNU_COMPATIBILITY
  903. archive_format = OLDGNU_FORMAT;
  904. #else
  905. archive_format = GNU_FORMAT;
  906. #endif
  907. }
  908. if (archive_format == GNU_FORMAT && getenv ("POSIXLY_CORRECT"))
  909. archive_format = POSIX_FORMAT;
  910. if ((volume_label_option
  911. || incremental_option || multi_volume_option || sparse_option)
  912. && archive_format != OLDGNU_FORMAT && archive_format != GNU_FORMAT)
  913. USAGE_ERROR ((0, 0,
  914. _("GNU features wanted on incompatible archive format")));
  915. if (archive_names == 0)
  916. {
  917. /* If no archive file name given, try TAPE from the environment, or
  918. else, DEFAULT_ARCHIVE from the configuration process. */
  919. archive_names = 1;
  920. archive_name_array[0] = getenv ("TAPE");
  921. if (! archive_name_array[0])
  922. archive_name_array[0] = DEFAULT_ARCHIVE;
  923. }
  924. /* Allow multiple archives only with `-M'. */
  925. if (archive_names > 1 && !multi_volume_option)
  926. USAGE_ERROR ((0, 0,
  927. _("Multiple archive files requires `-M' option")));
  928. if (listed_incremental_option
  929. && newer_mtime_option != TYPE_MINIMUM (time_t))
  930. USAGE_ERROR ((0, 0,
  931. _("Cannot combine --listed-incremental with --newer")));
  932. /* If ready to unlink hierarchies, so we are for simpler files. */
  933. if (recursive_unlink_option)
  934. old_files_option = UNLINK_FIRST_OLD_FILES;
  935. /* Forbid using -c with no input files whatsoever. Check that `-f -',
  936. explicit or implied, is used correctly. */
  937. switch (subcommand_option)
  938. {
  939. case CREATE_SUBCOMMAND:
  940. if (input_files == 0 && !files_from_option)
  941. USAGE_ERROR ((0, 0,
  942. _("Cowardly refusing to create an empty archive")));
  943. break;
  944. case EXTRACT_SUBCOMMAND:
  945. case LIST_SUBCOMMAND:
  946. case DIFF_SUBCOMMAND:
  947. for (archive_name_cursor = archive_name_array;
  948. archive_name_cursor < archive_name_array + archive_names;
  949. archive_name_cursor++)
  950. if (!strcmp (*archive_name_cursor, "-"))
  951. request_stdin ("-f");
  952. break;
  953. case CAT_SUBCOMMAND:
  954. case UPDATE_SUBCOMMAND:
  955. case APPEND_SUBCOMMAND:
  956. case DELETE_SUBCOMMAND:
  957. for (archive_name_cursor = archive_name_array;
  958. archive_name_cursor < archive_name_array + archive_names;
  959. archive_name_cursor++)
  960. if (!strcmp (*archive_name_cursor, "-"))
  961. USAGE_ERROR ((0, 0,
  962. _("Options `-Aru' and `--delete' are incompatible with `-f -'")));
  963. default:
  964. break;
  965. }
  966. archive_name_cursor = archive_name_array;
  967. /* Prepare for generating backup names. */
  968. if (backup_suffix_string)
  969. simple_backup_suffix = xstrdup (backup_suffix_string);
  970. if (backup_option)
  971. backup_type = xget_version ("--backup", version_control_string);
  972. }
  973. /* Tar proper. */
  974. /*-----------------------.
  975. | Main routine for tar. |
  976. `-----------------------*/
  977. int
  978. main (int argc, char *const *argv)
  979. {
  980. #if HAVE_CLOCK_GETTIME
  981. if (clock_gettime (CLOCK_REALTIME, &start_timespec) != 0)
  982. #endif
  983. start_time = time (0);
  984. program_name = argv[0];
  985. setlocale (LC_ALL, "");
  986. bindtextdomain (PACKAGE, LOCALEDIR);
  987. textdomain (PACKAGE);
  988. exit_status = TAREXIT_SUCCESS;
  989. filename_terminator = '\n';
  990. set_quoting_style (0, escape_quoting_style);
  991. /* Pre-allocate a few structures. */
  992. allocated_archive_names = 10;
  993. archive_name_array =
  994. xmalloc (sizeof (const char *) * allocated_archive_names);
  995. archive_names = 0;
  996. #ifdef SIGCHLD
  997. /* System V fork+wait does not work if SIGCHLD is ignored. */
  998. signal (SIGCHLD, SIG_DFL);
  999. #endif
  1000. init_names ();
  1001. /* Decode options. */
  1002. decode_options (argc, argv);
  1003. name_init (argc, argv);
  1004. /* Main command execution. */
  1005. if (volno_file_option)
  1006. init_volume_number ();
  1007. switch (subcommand_option)
  1008. {
  1009. case UNKNOWN_SUBCOMMAND:
  1010. USAGE_ERROR ((0, 0,
  1011. _("You must specify one of the `-Acdtrux' options")));
  1012. case CAT_SUBCOMMAND:
  1013. case UPDATE_SUBCOMMAND:
  1014. case APPEND_SUBCOMMAND:
  1015. update_archive ();
  1016. break;
  1017. case DELETE_SUBCOMMAND:
  1018. delete_archive_members ();
  1019. break;
  1020. case CREATE_SUBCOMMAND:
  1021. create_archive ();
  1022. name_close ();
  1023. if (totals_option)
  1024. print_total_written ();
  1025. break;
  1026. case EXTRACT_SUBCOMMAND:
  1027. extr_init ();
  1028. read_and (extract_archive);
  1029. break;
  1030. case LIST_SUBCOMMAND:
  1031. read_and (list_archive);
  1032. break;
  1033. case DIFF_SUBCOMMAND:
  1034. diff_init ();
  1035. read_and (diff_archive);
  1036. break;
  1037. }
  1038. if (volno_file_option)
  1039. closeout_volume_number ();
  1040. /* Dispose of allocated memory, and return. */
  1041. free (archive_name_array);
  1042. name_term ();
  1043. if (stdlis == stdout && (ferror (stdout) || fclose (stdout) != 0))
  1044. FATAL_ERROR ((0, 0, _("Error in writing to standard output")));
  1045. if (exit_status == TAREXIT_FAILURE)
  1046. error (0, 0, _("Error exit delayed from previous errors"));
  1047. exit (exit_status);
  1048. }