diff -Naur tar-1.26.orig//configure.ac tar-1.26/configure.ac --- tar-1.26.orig//configure.ac 2011-03-12 04:47:13.000000000 -0500 +++ tar-1.26/configure.ac 2011-09-06 13:22:06.000000000 -0400 @@ -44,7 +44,7 @@ sys/param.h sys/device.h sys/gentape.h \ sys/inet.h sys/io/trioctl.h \ sys/mtio.h sys/time.h sys/tprintf.h sys/tape.h \ - unistd.h locale.h) + unistd.h locale.h attr/xattr.h sys/acl.h) AC_CHECK_HEADERS([sys/buf.h], [], [], [#if HAVE_SYS_PARAM_H @@ -91,6 +91,12 @@ tar_PAXUTILS AC_CHECK_FUNCS_ONCE([fchmod fchown fsync lstat mkfifo readlink symlink]) +AC_CHECK_FUNCS(getxattr fgetxattr lgetxattr \ + setxattr fsetxattr lsetxattr \ + listxattr flistxattr llistxattr, + AC_DEFINE(HAVE_XATTRS,,[Define if we have a working extended attributes]),) +AC_CHECK_LIB(acl, acl_get_fd) + AC_CHECK_DECLS([getgrgid],,, [#include ]) AC_CHECK_DECLS([getpwuid],,, [#include ]) AC_CHECK_DECLS([time],,, [#include ]) @@ -214,6 +220,7 @@ # Iconv AM_ICONV AC_CHECK_HEADERS(iconv.h) +AC_CHECK_HEADERS(attr/xattr.h) AC_CHECK_TYPE(iconv_t,:, AC_DEFINE(iconv_t, int, [Conversion descriptor type]), @@ -223,6 +230,17 @@ #endif ]) +AC_ARG_ENABLE(selinux, + AC_HELP_STRING([--enable-selinux], + [enable SELinux support (disabled by default)]), + [selinux_enabled=$enableval], + [selinux_enabled=no]) + +if test "x$selinux_enabled" = xyes; then + AC_CHECK_LIB(selinux, getfilecon) + AC_CHECK_HEADERS(selinux/selinux.h) +fi + # Gettext. AM_GNU_GETTEXT([external], [need-formatstring-macros]) AM_GNU_GETTEXT_VERSION([0.16]) diff -Naur tar-1.26.orig//doc/tar.texi tar-1.26/doc/tar.texi --- tar-1.26.orig//doc/tar.texi 2011-03-12 04:09:33.000000000 -0500 +++ tar-1.26/doc/tar.texi 2011-09-06 13:22:06.000000000 -0400 @@ -2369,6 +2369,10 @@ @samp{/} from member names. This option disables that behavior. @xref{absolute}. +@opsummary{acl} +@item --acls +Causes @command{tar} to store ACL's. @xref{Attributes}. + @opsummary{after-date} @item --after-date @@ -2914,6 +2918,10 @@ also back up files for which any status information has changed). @xref{after}. +@opsummary{no-acl} +@item --no-acls +Causes @command{tar} not to store and not to extract ACL's. @xref{Attributes}. + @opsummary{no-anchored} @item --no-anchored An exclude pattern can match any subsequence of the name's components. @@ -2997,11 +3005,21 @@ the archive can be seeked or not. Use this option to disable this mechanism. +@opsummary{no-selinux} +@item --no-selinux +Causes @command{tar} not to store and not to extract SELinux security context. +@xref{Attributes}. + @opsummary{no-unquote} @item --no-unquote Treat all input file or member names literally, do not interpret escape sequences. @xref{input name quoting}. +@opsummary{no-xattrs} +@item --no-xattrs +Causes @command{tar} not to store and not to extract xattrs. This option also +enables @option{--no-selinux} and @option{--no-acls}. @xref{Attributes}. + @opsummary{no-wildcards} @item --no-wildcards Do not use wildcards. @@ -3234,6 +3252,11 @@ archive is open for reading (e.g. with @option{--list} or @option{--extract} options). +@opsummary{selinux} +@item --selinux +Causes @command{tar} to store SElinux security context. @xref{Attributes}. + + @opsummary{show-defaults} @item --show-defaults @@ -3447,6 +3470,11 @@ messages are suppressed if @var{keyword} is prefixed with @samp{no-}. @xref{warnings}. +@opsummary{xattrs} +@item --xattrs +Causes @command{tar} to store xattrs. This option also enables +@option{--selinux} and @option{--acls}. @xref{Attributes}. + @opsummary{wildcards} @item --wildcards Use wildcards when matching member names with patterns. @@ -8659,6 +8687,8 @@ most @samp{posix} archives as well, with the only exception that any additional information (such as long file names etc.) will in such case be extracted as plain text files along with the files it refers to. +This is the only format that can store ACLs, SELinux context and extended +attributes. This archive format will be the default format for future versions of @GNUTAR{}. @@ -9293,6 +9323,51 @@ This option is deprecated, and will be removed in @GNUTAR{} version 1.23. +@opindex acls +@item --acls +This option causes @command{tar} to store the current ACL in the archive. + +The @option{--acls} option has no equivalent short option name. + +@opindex selinux +@item --selinux +This option causes @command{tar} to store the current SELinux security context +information in the archive. + +The @option{--selinux} option has no equivalent short option name. + +@opindex xattrs +@item --xattrs +This option causes @command{tar} to store the current extended attributes in +the archive. This option also enables @option{--acls} and @option{--selinux} if +they haven't been set already. + +The @option{--xattrs} option has no equivalent short option name. + +@opindex no-acls +@item --no-acls +This option causes @command{tar} not to store the current ACL in the archive +and not to extract any ACL information in an archive. + +The @option{--no-acls} option has no equivalent short option name. + +@opindex no-selinux +@item --no-selinux +This option causes @command{tar} not to store the current SELinux security +context information in the archive and not to extract any SELinux information in +an archive. + +The @option{--no-selinux} option has no equivalent short option name. + +@opindex no-xattrs +@item --no-xattrs +This option causes @command{tar} not to store the current extended attributes in +the archive and not to extract any extended attributes in an archive. This +option also enables @option{--no-acls} and @option{--no-selinux} if +they haven't been set already. + +The @option{--no-xattrs} option has no equivalent short option name. + @end table @node Portability diff -Naur tar-1.26.orig//doc/tar.texi.orig tar-1.26/doc/tar.texi.orig --- tar-1.26.orig//doc/tar.texi.orig 1969-12-31 19:00:00.000000000 -0500 +++ tar-1.26/doc/tar.texi.orig 2011-03-12 04:09:33.000000000 -0500 @@ -0,0 +1,12375 @@ +\input texinfo @c -*-texinfo-*- +@comment %**start of header +@setfilename tar.info +@include version.texi +@settitle GNU tar @value{VERSION} +@setchapternewpage odd + +@finalout + +@smallbook +@c %**end of header + +@c Maintenance notes: +@c 1. Pay attention to @FIXME{}s and @UNREVISED{}s +@c 2. Before creating final variant: +@c 2.1. Run `make check-options' to make sure all options are properly +@c documented; +@c 2.2. Run `make master-menu' (see comment before the master menu). + +@include rendition.texi +@include value.texi + +@defcodeindex op +@defcodeindex kw + +@c Put everything in one index (arbitrarily chosen to be the concept index). +@syncodeindex fn cp +@syncodeindex ky cp +@syncodeindex pg cp +@syncodeindex vr cp +@syncodeindex kw cp + +@copying + +This manual is for @acronym{GNU} @command{tar} (version +@value{VERSION}, @value{UPDATED}), which creates and extracts files +from archives. + +Copyright @copyright{} 1992, 1994, 1995, 1996, 1997, 1999, 2000, 2001, +2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc. + +@quotation +Permission is granted to copy, distribute and/or modify this document +under the terms of the GNU Free Documentation License, Version 1.3 or +any later version published by the Free Software Foundation; with no +Invariant Sections, with the Front-Cover Texts being ``A GNU Manual'', +and with the Back-Cover Texts as in (a) below. A copy of the license +is included in the section entitled ``GNU Free Documentation +License''. + +(a) The FSF's Back-Cover Text is: ``You have the freedom to +copy and modify this GNU manual. Buying copies from the FSF +supports it in developing GNU and promoting software freedom.'' +@end quotation +@end copying + +@dircategory Archiving +@direntry +* Tar: (tar). Making tape (or disk) archives. +@end direntry + +@dircategory Individual utilities +@direntry +* tar: (tar)tar invocation. Invoking @GNUTAR{}. +@end direntry + +@shorttitlepage @acronym{GNU} @command{tar} + +@titlepage +@title @acronym{GNU} tar: an archiver tool +@subtitle @value{RENDITION} @value{VERSION}, @value{UPDATED} +@author John Gilmore, Jay Fenlason et al. + +@page +@vskip 0pt plus 1filll +@insertcopying +@end titlepage + +@ifnottex +@node Top +@top @acronym{GNU} tar: an archiver tool + +@insertcopying + +@cindex file archival +@cindex archiving files + +The first part of this master menu lists the major nodes in this Info +document. The rest of the menu lists all the lower level nodes. +@end ifnottex + +@c The master menu goes here. +@c +@c NOTE: To update it from within Emacs, make sure mastermenu.el is +@c loaded and run texinfo-master-menu. +@c To update it from the command line, run +@c +@c make master-menu + +@menu +* Introduction:: +* Tutorial:: +* tar invocation:: +* operations:: +* Backups:: +* Choosing:: +* Date input formats:: +* Formats:: +* Media:: +* Reliability and security:: + +Appendices + +* Changes:: +* Configuring Help Summary:: +* Fixing Snapshot Files:: +* Tar Internals:: +* Genfile:: +* Free Software Needs Free Documentation:: +* GNU Free Documentation License:: +* Index of Command Line Options:: +* Index:: + +@detailmenu + --- The Detailed Node Listing --- + +Introduction + +* Book Contents:: What this Book Contains +* Definitions:: Some Definitions +* What tar Does:: What @command{tar} Does +* Naming tar Archives:: How @command{tar} Archives are Named +* Authors:: @GNUTAR{} Authors +* Reports:: Reporting bugs or suggestions + +Tutorial Introduction to @command{tar} + +* assumptions:: +* stylistic conventions:: +* basic tar options:: Basic @command{tar} Operations and Options +* frequent operations:: +* Two Frequent Options:: +* create:: How to Create Archives +* list:: How to List Archives +* extract:: How to Extract Members from an Archive +* going further:: + +Two Frequently Used Options + +* file tutorial:: +* verbose tutorial:: +* help tutorial:: + +How to Create Archives + +* prepare for examples:: +* Creating the archive:: +* create verbose:: +* short create:: +* create dir:: + +How to List Archives + +* list dir:: + +How to Extract Members from an Archive + +* extracting archives:: +* extracting files:: +* extract dir:: +* extracting untrusted archives:: +* failing commands:: + +Invoking @GNUTAR{} + +* Synopsis:: +* using tar options:: +* Styles:: +* All Options:: +* help:: +* defaults:: +* verbose:: +* checkpoints:: +* warnings:: +* interactive:: + +The Three Option Styles + +* Long Options:: Long Option Style +* Short Options:: Short Option Style +* Old Options:: Old Option Style +* Mixing:: Mixing Option Styles + +All @command{tar} Options + +* Operation Summary:: +* Option Summary:: +* Short Option Summary:: + +@GNUTAR{} Operations + +* Basic tar:: +* Advanced tar:: +* create options:: +* extract options:: +* backup:: +* Applications:: +* looking ahead:: + +Advanced @GNUTAR{} Operations + +* Operations:: +* append:: +* update:: +* concatenate:: +* delete:: +* compare:: + +How to Add Files to Existing Archives: @option{--append} + +* appending files:: Appending Files to an Archive +* multiple:: + +Updating an Archive + +* how to update:: + +Options Used by @option{--create} + +* override:: Overriding File Metadata. +* Ignore Failed Read:: + +Options Used by @option{--extract} + +* Reading:: Options to Help Read Archives +* Writing:: Changing How @command{tar} Writes Files +* Scarce:: Coping with Scarce Resources + +Options to Help Read Archives + +* read full records:: +* Ignore Zeros:: + +Changing How @command{tar} Writes Files + +* Dealing with Old Files:: +* Overwrite Old Files:: +* Keep Old Files:: +* Keep Newer Files:: +* Unlink First:: +* Recursive Unlink:: +* Data Modification Times:: +* Setting Access Permissions:: +* Directory Modification Times and Permissions:: +* Writing to Standard Output:: +* Writing to an External Program:: +* remove files:: + +Coping with Scarce Resources + +* Starting File:: +* Same Order:: + +Performing Backups and Restoring Files + +* Full Dumps:: Using @command{tar} to Perform Full Dumps +* Incremental Dumps:: Using @command{tar} to Perform Incremental Dumps +* Backup Levels:: Levels of Backups +* Backup Parameters:: Setting Parameters for Backups and Restoration +* Scripted Backups:: Using the Backup Scripts +* Scripted Restoration:: Using the Restore Script + +Setting Parameters for Backups and Restoration + +* General-Purpose Variables:: +* Magnetic Tape Control:: +* User Hooks:: +* backup-specs example:: An Example Text of @file{Backup-specs} + +Choosing Files and Names for @command{tar} + +* file:: Choosing the Archive's Name +* Selecting Archive Members:: +* files:: Reading Names from a File +* exclude:: Excluding Some Files +* wildcards:: Wildcards Patterns and Matching +* quoting styles:: Ways of Quoting Special Characters in Names +* transform:: Modifying File and Member Names +* after:: Operating Only on New Files +* recurse:: Descending into Directories +* one:: Crossing File System Boundaries + +Reading Names from a File + +* nul:: + +Excluding Some Files + +* problems with exclude:: + +Wildcards Patterns and Matching + +* controlling pattern-matching:: + +Crossing File System Boundaries + +* directory:: Changing Directory +* absolute:: Absolute File Names + +Date input formats + +* General date syntax:: Common rules. +* Calendar date items:: 19 Dec 1994. +* Time of day items:: 9:20pm. +* Time zone items:: @sc{est}, @sc{pdt}, @sc{gmt}. +* Day of week items:: Monday and others. +* Relative items in date strings:: next tuesday, 2 years ago. +* Pure numbers in date strings:: 19931219, 1440. +* Seconds since the Epoch:: @@1078100502. +* Specifying time zone rules:: TZ="America/New_York", TZ="UTC0". +* Authors of parse_datetime:: Bellovin, Eggert, Salz, Berets, et al. + +Controlling the Archive Format + +* Compression:: Using Less Space through Compression +* Attributes:: Handling File Attributes +* Portability:: Making @command{tar} Archives More Portable +* cpio:: Comparison of @command{tar} and @command{cpio} + +Using Less Space through Compression + +* gzip:: Creating and Reading Compressed Archives +* sparse:: Archiving Sparse Files + +Creating and Reading Compressed Archives + +* lbzip2:: Using lbzip2 with @GNUTAR{}. + +Making @command{tar} Archives More Portable + +* Portable Names:: Portable Names +* dereference:: Symbolic Links +* hard links:: Hard Links +* old:: Old V7 Archives +* ustar:: Ustar Archives +* gnu:: GNU and old GNU format archives. +* posix:: @acronym{POSIX} archives +* Checksumming:: Checksumming Problems +* Large or Negative Values:: Large files, negative time stamps, etc. +* Other Tars:: How to Extract GNU-Specific Data Using + Other @command{tar} Implementations + +@GNUTAR{} and @acronym{POSIX} @command{tar} + +* PAX keywords:: Controlling Extended Header Keywords. + +How to Extract GNU-Specific Data Using Other @command{tar} Implementations + +* Split Recovery:: Members Split Between Volumes +* Sparse Recovery:: Sparse Members + +Tapes and Other Archive Media + +* Device:: Device selection and switching +* Remote Tape Server:: +* Common Problems and Solutions:: +* Blocking:: Blocking +* Many:: Many archives on one tape +* Using Multiple Tapes:: Using Multiple Tapes +* label:: Including a Label in the Archive +* verify:: +* Write Protection:: + +Blocking + +* Format Variations:: Format Variations +* Blocking Factor:: The Blocking Factor of an Archive + +Many Archives on One Tape + +* Tape Positioning:: Tape Positions and Tape Marks +* mt:: The @command{mt} Utility + +Using Multiple Tapes + +* Multi-Volume Archives:: Archives Longer than One Tape or Disk +* Tape Files:: Tape Files +* Tarcat:: Concatenate Volumes into a Single Archive + + +Tar Internals + +* Standard:: Basic Tar Format +* Extensions:: @acronym{GNU} Extensions to the Archive Format +* Sparse Formats:: Storing Sparse Files +* Snapshot Files:: +* Dumpdir:: + +Storing Sparse Files + +* Old GNU Format:: +* PAX 0:: PAX Format, Versions 0.0 and 0.1 +* PAX 1:: PAX Format, Version 1.0 + +Genfile + +* Generate Mode:: File Generation Mode. +* Status Mode:: File Status Mode. +* Exec Mode:: Synchronous Execution mode. + +Copying This Manual + +* GNU Free Documentation License:: License for copying this manual + +@end detailmenu +@end menu + +@node Introduction +@chapter Introduction + +@GNUTAR{} creates +and manipulates @dfn{archives} which are actually collections of +many other files; the program provides users with an organized and +systematic method for controlling a large amount of data. +The name ``tar'' originally came from the phrase ``Tape ARchive'', but +archives need not (and these days, typically do not) reside on tapes. + +@menu +* Book Contents:: What this Book Contains +* Definitions:: Some Definitions +* What tar Does:: What @command{tar} Does +* Naming tar Archives:: How @command{tar} Archives are Named +* Authors:: @GNUTAR{} Authors +* Reports:: Reporting bugs or suggestions +@end menu + +@node Book Contents +@section What this Book Contains + +The first part of this chapter introduces you to various terms that will +recur throughout the book. It also tells you who has worked on @GNUTAR{} +and its documentation, and where you should send bug reports +or comments. + +The second chapter is a tutorial (@pxref{Tutorial}) which provides a +gentle introduction for people who are new to using @command{tar}. It is +meant to be self-contained, not requiring any reading from subsequent +chapters to make sense. It moves from topic to topic in a logical, +progressive order, building on information already explained. + +Although the tutorial is paced and structured to allow beginners to +learn how to use @command{tar}, it is not intended solely for beginners. +The tutorial explains how to use the three most frequently used +operations (@samp{create}, @samp{list}, and @samp{extract}) as well as +two frequently used options (@samp{file} and @samp{verbose}). The other +chapters do not refer to the tutorial frequently; however, if a section +discusses something which is a complex variant of a basic concept, there +may be a cross-reference to that basic concept. (The entire book, +including the tutorial, assumes that the reader understands some basic +concepts of using a Unix-type operating system; @pxref{Tutorial}.) + +The third chapter presents the remaining five operations, and +information about using @command{tar} options and option syntax. + +The other chapters are meant to be used as a reference. Each chapter +presents everything that needs to be said about a specific topic. + +One of the chapters (@pxref{Date input formats}) exists in its +entirety in other @acronym{GNU} manuals, and is mostly self-contained. +In addition, one section of this manual (@pxref{Standard}) contains a +big quote which is taken directly from @command{tar} sources. + +In general, we give both long and short (abbreviated) option names +at least once in each section where the relevant option is covered, so +that novice readers will become familiar with both styles. (A few +options have no short versions, and the relevant sections will +indicate this.) + +@node Definitions +@section Some Definitions + +@cindex archive +@cindex tar archive +The @command{tar} program is used to create and manipulate @command{tar} +archives. An @dfn{archive} is a single file which contains the contents +of many files, while still identifying the names of the files, their +owner(s), and so forth. (In addition, archives record access +permissions, user and group, size in bytes, and data modification time. +Some archives also record the file names in each archived directory, as +well as other file and directory information.) You can use @command{tar} +to @dfn{create} a new archive in a specified directory. + +@cindex member +@cindex archive member +@cindex file name +@cindex member name +The files inside an archive are called @dfn{members}. Within this +manual, we use the term @dfn{file} to refer only to files accessible in +the normal ways (by @command{ls}, @command{cat}, and so forth), and the term +@dfn{member} to refer only to the members of an archive. Similarly, a +@dfn{file name} is the name of a file, as it resides in the file system, +and a @dfn{member name} is the name of an archive member within the +archive. + +@cindex extraction +@cindex unpacking +The term @dfn{extraction} refers to the process of copying an archive +member (or multiple members) into a file in the file system. Extracting +all the members of an archive is often called @dfn{extracting the +archive}. The term @dfn{unpack} can also be used to refer to the +extraction of many or all the members of an archive. Extracting an +archive does not destroy the archive's structure, just as creating an +archive does not destroy the copies of the files that exist outside of +the archive. You may also @dfn{list} the members in a given archive +(this is often thought of as ``printing'' them to the standard output, +or the command line), or @dfn{append} members to a pre-existing archive. +All of these operations can be performed using @command{tar}. + +@node What tar Does +@section What @command{tar} Does + +@cindex tar +The @command{tar} program provides the ability to create @command{tar} +archives, as well as various other kinds of manipulation. For example, +you can use @command{tar} on previously created archives to extract files, +to store additional files, or to update or list files which were already +stored. + +Initially, @command{tar} archives were used to store files conveniently on +magnetic tape. The name @command{tar} comes from this use; it stands for +@code{t}ape @code{ar}chiver. Despite the utility's name, @command{tar} can +direct its output to available devices, files, or other programs (using +pipes). @command{tar} may even access remote devices or files (as archives). + +You can use @command{tar} archives in many ways. We want to stress a few +of them: storage, backup, and transportation. + +@FIXME{the following table entries need a bit of work.} +@table @asis +@item Storage +Often, @command{tar} archives are used to store related files for +convenient file transfer over a network. For example, the +@acronym{GNU} Project distributes its software bundled into +@command{tar} archives, so that all the files relating to a particular +program (or set of related programs) can be transferred as a single +unit. + +A magnetic tape can store several files in sequence. However, the tape +has no names for these files; it only knows their relative position on +the tape. One way to store several files on one tape and retain their +names is by creating a @command{tar} archive. Even when the basic transfer +mechanism can keep track of names, as FTP can, the nuisance of handling +multiple files, directories, and multiple links makes @command{tar} +archives useful. + +Archive files are also used for long-term storage. You can think of +this as transportation from the present into the future. (It is a +science-fiction idiom that you can move through time as well as in +space; the idea here is that @command{tar} can be used to move archives in +all dimensions, even time!) + +@item Backup +Because the archive created by @command{tar} is capable of preserving +file information and directory structure, @command{tar} is commonly +used for performing full and incremental backups of disks. A backup +puts a collection of files (possibly pertaining to many users and +projects) together on a disk or a tape. This guards against +accidental destruction of the information in those files. +@GNUTAR{} has special features that allow it to be +used to make incremental and full dumps of all the files in a +file system. + +@item Transportation +You can create an archive on one system, transfer it to another system, +and extract the contents there. This allows you to transport a group of +files from one system to another. +@end table + +@node Naming tar Archives +@section How @command{tar} Archives are Named + +Conventionally, @command{tar} archives are given names ending with +@samp{.tar}. This is not necessary for @command{tar} to operate properly, +but this manual follows that convention in order to accustom readers to +it and to make examples more clear. + +@cindex tar file +@cindex entry +@cindex tar entry +Often, people refer to @command{tar} archives as ``@command{tar} files,'' and +archive members as ``files'' or ``entries''. For people familiar with +the operation of @command{tar}, this causes no difficulty. However, in +this manual, we consistently refer to ``archives'' and ``archive +members'' to make learning to use @command{tar} easier for novice users. + +@node Authors +@section @GNUTAR{} Authors + +@GNUTAR{} was originally written by John Gilmore, +and modified by many people. The @acronym{GNU} enhancements were +written by Jay Fenlason, then Joy Kendall, and the whole package has +been further maintained by Thomas Bushnell, n/BSG, Fran@,{c}ois +Pinard, Paul Eggert, and finally Sergey Poznyakoff with the help of +numerous and kind users. + +We wish to stress that @command{tar} is a collective work, and owes much to +all those people who reported problems, offered solutions and other +insights, or shared their thoughts and suggestions. An impressive, yet +partial list of those contributors can be found in the @file{THANKS} +file from the @GNUTAR{} distribution. + +@FIXME{i want all of these names mentioned, Absolutely. BUT, i'm not +sure i want to spell out the history in this detail, at least not for +the printed book. i'm just not sure it needs to be said this way. +i'll think about it.} + +@FIXME{History is more important, and surely more interesting, than +actual names. Quoting names without history would be meaningless. FP} + +Jay Fenlason put together a draft of a @GNUTAR{} +manual, borrowing notes from the original man page from John Gilmore. +This was withdrawn in version 1.11. Thomas Bushnell, n/BSG and Amy +Gorin worked on a tutorial and manual for @GNUTAR{}. +Fran@,{c}ois Pinard put version 1.11.8 of the manual together by +taking information from all these sources and merging them. Melissa +Weisshaus finally edited and redesigned the book to create version +1.12. The book for versions from 1.14 up to @value{VERSION} were edited +by the current maintainer, Sergey Poznyakoff. + +For version 1.12, Daniel Hagerty contributed a great deal of technical +consulting. In particular, he is the primary author of @ref{Backups}. + +In July, 2003 @GNUTAR{} was put on CVS at savannah.gnu.org +(see @url{http://savannah.gnu.org/projects/tar}), and +active development and maintenance work has started +again. Currently @GNUTAR{} is being maintained by Paul Eggert, Sergey +Poznyakoff and Jeff Bailey. + +Support for @acronym{POSIX} archives was added by Sergey Poznyakoff. + +@node Reports +@section Reporting bugs or suggestions + +@cindex bug reports +@cindex reporting bugs +If you find problems or have suggestions about this program or manual, +please report them to @file{bug-tar@@gnu.org}. + +When reporting a bug, please be sure to include as much detail as +possible, in order to reproduce it. @FIXME{Be more specific, I'd +like to make this node as detailed as 'Bug reporting' node in Emacs +manual.} + +@node Tutorial +@chapter Tutorial Introduction to @command{tar} + +This chapter guides you through some basic examples of three @command{tar} +operations: @option{--create}, @option{--list}, and @option{--extract}. If +you already know how to use some other version of @command{tar}, then you +may not need to read this chapter. This chapter omits most complicated +details about how @command{tar} works. + +@menu +* assumptions:: +* stylistic conventions:: +* basic tar options:: Basic @command{tar} Operations and Options +* frequent operations:: +* Two Frequent Options:: +* create:: How to Create Archives +* list:: How to List Archives +* extract:: How to Extract Members from an Archive +* going further:: +@end menu + +@node assumptions +@section Assumptions this Tutorial Makes + +This chapter is paced to allow beginners to learn about @command{tar} +slowly. At the same time, we will try to cover all the basic aspects of +these three operations. In order to accomplish both of these tasks, we +have made certain assumptions about your knowledge before reading this +manual, and the hardware you will be using: + +@itemize @bullet +@item +Before you start to work through this tutorial, you should understand +what the terms ``archive'' and ``archive member'' mean +(@pxref{Definitions}). In addition, you should understand something +about how Unix-type operating systems work, and you should know how to +use some basic utilities. For example, you should know how to create, +list, copy, rename, edit, and delete files and directories; how to +change between directories; and how to figure out where you are in the +file system. You should have some basic understanding of directory +structure and how files are named according to which directory they are +in. You should understand concepts such as standard output and standard +input, what various definitions of the term @samp{argument} mean, and the +differences between relative and absolute file names. @FIXME{and what +else?} + +@item +This manual assumes that you are working from your own home directory +(unless we state otherwise). In this tutorial, you will create a +directory to practice @command{tar} commands in. When we show file names, +we will assume that those names are relative to your home directory. +For example, my home directory is @file{/home/fsf/melissa}. All of +my examples are in a subdirectory of the directory named by that file +name; the subdirectory is called @file{practice}. + +@item +In general, we show examples of archives which exist on (or can be +written to, or worked with from) a directory on a hard disk. In most +cases, you could write those archives to, or work with them on any other +device, such as a tape drive. However, some of the later examples in +the tutorial and next chapter will not work on tape drives. +Additionally, working with tapes is much more complicated than working +with hard disks. For these reasons, the tutorial does not cover working +with tape drives. @xref{Media}, for complete information on using +@command{tar} archives with tape drives. + +@FIXME{this is a cop out. need to add some simple tape drive info.} +@end itemize + +@node stylistic conventions +@section Stylistic Conventions + +In the examples, @samp{$} represents a typical shell prompt. It +precedes lines you should type; to make this more clear, those lines are +shown in @kbd{this font}, as opposed to lines which represent the +computer's response; those lines are shown in @code{this font}, or +sometimes @samp{like this}. + +@c When we have lines which are too long to be +@c displayed in any other way, we will show them like this: + +@node basic tar options +@section Basic @command{tar} Operations and Options + +@command{tar} can take a wide variety of arguments which specify and define +the actions it will have on the particular set of files or the archive. +The main types of arguments to @command{tar} fall into one of two classes: +operations, and options. + +Some arguments fall into a class called @dfn{operations}; exactly one of +these is both allowed and required for any instance of using @command{tar}; +you may @emph{not} specify more than one. People sometimes speak of +@dfn{operating modes}. You are in a particular operating mode when you +have specified the operation which specifies it; there are eight +operations in total, and thus there are eight operating modes. + +The other arguments fall into the class known as @dfn{options}. You are +not required to specify any options, and you are allowed to specify more +than one at a time (depending on the way you are using @command{tar} at +that time). Some options are used so frequently, and are so useful for +helping you type commands more carefully that they are effectively +``required''. We will discuss them in this chapter. + +You can write most of the @command{tar} operations and options in any +of three forms: long (mnemonic) form, short form, and old style. Some +of the operations and options have no short or ``old'' forms; however, +the operations and options which we will cover in this tutorial have +corresponding abbreviations. We will indicate those abbreviations +appropriately to get you used to seeing them. Note, that the ``old +style'' option forms exist in @GNUTAR{} for compatibility with Unix +@command{tar}. In this book we present a full discussion of this way +of writing options and operations (@pxref{Old Options}), and we discuss +the other two styles of writing options (@xref{Long Options}, and +@pxref{Short Options}). + +In the examples and in the text of this tutorial, we usually use the +long forms of operations and options; but the ``short'' forms produce +the same result and can make typing long @command{tar} commands easier. +For example, instead of typing + +@smallexample +@kbd{tar --create --verbose --file=afiles.tar apple angst aspic} +@end smallexample + +@noindent +you can type +@smallexample +@kbd{tar -c -v -f afiles.tar apple angst aspic} +@end smallexample + +@noindent +or even +@smallexample +@kbd{tar -cvf afiles.tar apple angst aspic} +@end smallexample + +@noindent +For more information on option syntax, see @ref{Advanced tar}. In +discussions in the text, when we name an option by its long form, we +also give the corresponding short option in parentheses. + +The term, ``option'', can be confusing at times, since ``operations'' +are often lumped in with the actual, @emph{optional} ``options'' in certain +general class statements. For example, we just talked about ``short and +long forms of options and operations''. However, experienced @command{tar} +users often refer to these by shorthand terms such as, ``short and long +options''. This term assumes that the ``operations'' are included, also. +Context will help you determine which definition of ``options'' to use. + +Similarly, the term ``command'' can be confusing, as it is often used in +two different ways. People sometimes refer to @command{tar} ``commands''. +A @command{tar} @dfn{command} is the entire command line of user input +which tells @command{tar} what to do --- including the operation, options, +and any arguments (file names, pipes, other commands, etc.). However, +you will also sometimes hear the term ``the @command{tar} command''. When +the word ``command'' is used specifically like this, a person is usually +referring to the @command{tar} @emph{operation}, not the whole line. +Again, use context to figure out which of the meanings the speaker +intends. + +@node frequent operations +@section The Three Most Frequently Used Operations + +Here are the three most frequently used operations (both short and long +forms), as well as a brief description of their meanings. The rest of +this chapter will cover how to use these operations in detail. We will +present the rest of the operations in the next chapter. + +@table @option +@item --create +@itemx -c +Create a new @command{tar} archive. +@item --list +@itemx -t +List the contents of an archive. +@item --extract +@itemx -x +Extract one or more members from an archive. +@end table + +@node Two Frequent Options +@section Two Frequently Used Options + +To understand how to run @command{tar} in the three operating modes listed +previously, you also need to understand how to use two of the options to +@command{tar}: @option{--file} (which takes an archive file as an argument) +and @option{--verbose}. (You are usually not @emph{required} to specify +either of these options when you run @command{tar}, but they can be very +useful in making things more clear and helping you avoid errors.) + +@menu +* file tutorial:: +* verbose tutorial:: +* help tutorial:: +@end menu + +@node file tutorial +@unnumberedsubsec The @option{--file} Option + +@table @option +@xopindex{file, tutorial} +@item --file=@var{archive-name} +@itemx -f @var{archive-name} +Specify the name of an archive file. +@end table + +You can specify an argument for the @option{--file=@var{archive-name}} (@option{-f @var{archive-name}}) option whenever you +use @command{tar}; this option determines the name of the archive file +that @command{tar} will work on. + +@vrindex TAPE +If you don't specify this argument, then @command{tar} will examine +the environment variable @env{TAPE}. If it is set, its value will be +used as the archive name. Otherwise, @command{tar} will use the +default archive, determined at compile time. Usually it is +standard output or some physical tape drive attached to your machine +(you can verify what the default is by running @kbd{tar +--show-defaults}, @pxref{defaults}). If there is no tape drive +attached, or the default is not meaningful, then @command{tar} will +print an error message. The error message might look roughly like one +of the following: + +@smallexample +tar: can't open /dev/rmt8 : No such device or address +tar: can't open /dev/rsmt0 : I/O error +@end smallexample + +@noindent +To avoid confusion, we recommend that you always specify an archive file +name by using @option{--file=@var{archive-name}} (@option{-f @var{archive-name}}) when writing your @command{tar} commands. +For more information on using the @option{--file=@var{archive-name}} (@option{-f @var{archive-name}}) option, see +@ref{file}. + +@node verbose tutorial +@unnumberedsubsec The @option{--verbose} Option + +@table @option +@xopindex{verbose, introduced} +@item --verbose +@itemx -v +Show the files being worked on as @command{tar} is running. +@end table + +@option{--verbose} (@option{-v}) shows details about the results of running +@command{tar}. This can be especially useful when the results might not be +obvious. For example, if you want to see the progress of @command{tar} as +it writes files into the archive, you can use the @option{--verbose} +option. In the beginning, you may find it useful to use +@option{--verbose} at all times; when you are more accustomed to +@command{tar}, you will likely want to use it at certain times but not at +others. We will use @option{--verbose} at times to help make something +clear, and we will give many examples both using and not using +@option{--verbose} to show the differences. + +Each instance of @option{--verbose} on the command line increases the +verbosity level by one, so if you need more details on the output, +specify it twice. + +When reading archives (@option{--list}, @option{--extract}, +@option{--diff}), @command{tar} by default prints only the names of +the members being extracted. Using @option{--verbose} will show a full, +@command{ls} style member listing. + +In contrast, when writing archives (@option{--create}, @option{--append}, +@option{--update}), @command{tar} does not print file names by +default. So, a single @option{--verbose} option shows the file names +being added to the archive, while two @option{--verbose} options +enable the full listing. + +For example, to create an archive in verbose mode: + +@smallexample +$ @kbd{tar -cvf afiles.tar apple angst aspic} +apple +angst +aspic +@end smallexample + +@noindent +Creating the same archive with the verbosity level 2 could give: + +@smallexample +$ @kbd{tar -cvvf afiles.tar apple angst aspic} +-rw-r--r-- gray/staff 62373 2006-06-09 12:06 apple +-rw-r--r-- gray/staff 11481 2006-06-09 12:06 angst +-rw-r--r-- gray/staff 23152 2006-06-09 12:06 aspic +@end smallexample + +@noindent +This works equally well using short or long forms of options. Using +long forms, you would simply write out the mnemonic form of the option +twice, like this: + +@smallexample +$ @kbd{tar --create --verbose --verbose @dots{}} +@end smallexample + +@noindent +Note that you must double the hyphens properly each time. + +Later in the tutorial, we will give examples using @w{@option{--verbose +--verbose}}. + +@anchor{verbose member listing} +The full output consists of six fields: + +@itemize @bullet +@item File type and permissions in symbolic form. +These are displayed in the same format as the first column of +@command{ls -l} output (@pxref{What information is listed, +format=verbose, Verbose listing, fileutils, GNU file utilities}). + +@item Owner name and group separated by a slash character. +If these data are not available (for example, when listing a @samp{v7} format +archive), numeric @acronym{ID} values are printed instead. + +@item Size of the file, in bytes. + +@item File modification date in ISO 8601 format. + +@item File modification time. + +@item File name. +If the name contains any special characters (white space, newlines, +etc.) these are displayed in an unambiguous form using so called +@dfn{quoting style}. For the detailed discussion of available styles +and on how to use them, see @ref{quoting styles}. + +Depending on the file type, the name can be followed by some +additional information, described in the following table: + +@table @samp +@item -> @var{link-name} +The file or archive member is a @dfn{symbolic link} and +@var{link-name} is the name of file it links to. + +@item link to @var{link-name} +The file or archive member is a @dfn{hard link} and @var{link-name} is +the name of file it links to. + +@item --Long Link-- +The archive member is an old GNU format long link. You will normally +not encounter this. + +@item --Long Name-- +The archive member is an old GNU format long name. You will normally +not encounter this. + +@item --Volume Header-- +The archive member is a GNU @dfn{volume header} (@pxref{Tape Files}). + +@item --Continued at byte @var{n}-- +Encountered only at the beginning of a multi-volume archive +(@pxref{Using Multiple Tapes}). This archive member is a continuation +from the previous volume. The number @var{n} gives the offset where +the original file was split. + +@item unknown file type @var{c} +An archive member of unknown type. @var{c} is the type character from +the archive header. If you encounter such a message, it means that +either your archive contains proprietary member types @GNUTAR{} is not +able to handle, or the archive is corrupted. +@end table + +@end itemize + +For example, here is an archive listing containing most of the special +suffixes explained above: + +@smallexample +@group +V--------- 0/0 1536 2006-06-09 13:07 MyVolume--Volume Header-- +-rw-r--r-- gray/staff 456783 2006-06-09 12:06 aspic--Continued at byte 32456-- +-rw-r--r-- gray/staff 62373 2006-06-09 12:06 apple +lrwxrwxrwx gray/staff 0 2006-06-09 13:01 angst -> apple +-rw-r--r-- gray/staff 35793 2006-06-09 12:06 blues +hrw-r--r-- gray/staff 0 2006-06-09 12:06 music link to blues +@end group +@end smallexample + +@smallexample +@end smallexample + +@node help tutorial +@unnumberedsubsec Getting Help: Using the @option{--help} Option + +@table @option +@opindex help +@item --help + +The @option{--help} option to @command{tar} prints out a very brief list of +all operations and option available for the current version of +@command{tar} available on your system. +@end table + +@node create +@section How to Create Archives +@UNREVISED + +@cindex Creation of the archive +@cindex Archive, creation of +One of the basic operations of @command{tar} is @option{--create} (@option{-c}), which +you use to create a @command{tar} archive. We will explain +@option{--create} first because, in order to learn about the other +operations, you will find it useful to have an archive available to +practice on. + +To make this easier, in this section you will first create a directory +containing three files. Then, we will show you how to create an +@emph{archive} (inside the new directory). Both the directory, and +the archive are specifically for you to practice on. The rest of this +chapter and the next chapter will show many examples using this +directory and the files you will create: some of those files may be +other directories and other archives. + +The three files you will archive in this example are called +@file{blues}, @file{folk}, and @file{jazz}. The archive is called +@file{collection.tar}. + +This section will proceed slowly, detailing how to use @option{--create} +in @code{verbose} mode, and showing examples using both short and long +forms. In the rest of the tutorial, and in the examples in the next +chapter, we will proceed at a slightly quicker pace. This section +moves more slowly to allow beginning users to understand how +@command{tar} works. + +@menu +* prepare for examples:: +* Creating the archive:: +* create verbose:: +* short create:: +* create dir:: +@end menu + +@node prepare for examples +@subsection Preparing a Practice Directory for Examples + +To follow along with this and future examples, create a new directory +called @file{practice} containing files called @file{blues}, @file{folk} +and @file{jazz}. The files can contain any information you like: +ideally, they should contain information which relates to their names, +and be of different lengths. Our examples assume that @file{practice} +is a subdirectory of your home directory. + +Now @command{cd} to the directory named @file{practice}; @file{practice} +is now your @dfn{working directory}. (@emph{Please note}: Although +the full file name of this directory is +@file{/@var{homedir}/practice}, in our examples we will refer to +this directory as @file{practice}; the @var{homedir} is presumed.) + +In general, you should check that the files to be archived exist where +you think they do (in the working directory) by running @command{ls}. +Because you just created the directory and the files and have changed to +that directory, you probably don't need to do that this time. + +It is very important to make sure there isn't already a file in the +working directory with the archive name you intend to use (in this case, +@samp{collection.tar}), or that you don't care about its contents. +Whenever you use @samp{create}, @command{tar} will erase the current +contents of the file named by @option{--file=@var{archive-name}} (@option{-f @var{archive-name}}) if it exists. @command{tar} +will not tell you if you are about to overwrite an archive unless you +specify an option which does this (@pxref{backup}, for the +information on how to do so). To add files to an existing archive, +you need to use a different option, such as @option{--append} (@option{-r}); see +@ref{append} for information on how to do this. + +@node Creating the archive +@subsection Creating the Archive + +@xopindex{create, introduced} +To place the files @file{blues}, @file{folk}, and @file{jazz} into an +archive named @file{collection.tar}, use the following command: + +@smallexample +$ @kbd{tar --create --file=collection.tar blues folk jazz} +@end smallexample + +The order of the arguments is not very important, @emph{when using long +option forms}. You could also say: + +@smallexample +$ @kbd{tar blues --create folk --file=collection.tar jazz} +@end smallexample + +@noindent +However, you can see that this order is harder to understand; this is +why we will list the arguments in the order that makes the commands +easiest to understand (and we encourage you to do the same when you use +@command{tar}, to avoid errors). + +Note that the sequence +@option{--file=@-collection.tar} is considered to be @emph{one} argument. +If you substituted any other string of characters for +@kbd{collection.tar}, then that string would become the name of the +archive file you create. + +The order of the options becomes more important when you begin to use +short forms. With short forms, if you type commands in the wrong order +(even if you type them correctly in all other ways), you may end up with +results you don't expect. For this reason, it is a good idea to get +into the habit of typing options in the order that makes inherent sense. +@xref{short create}, for more information on this. + +In this example, you type the command as shown above: @option{--create} +is the operation which creates the new archive +(@file{collection.tar}), and @option{--file} is the option which lets +you give it the name you chose. The files, @file{blues}, @file{folk}, +and @file{jazz}, are now members of the archive, @file{collection.tar} +(they are @dfn{file name arguments} to the @option{--create} operation. +@xref{Choosing}, for the detailed discussion on these.) Now that they are +in the archive, they are called @emph{archive members}, not files. +(@pxref{Definitions,members}). + +When you create an archive, you @emph{must} specify which files you +want placed in the archive. If you do not specify any archive +members, @GNUTAR{} will complain. + +If you now list the contents of the working directory (@command{ls}), you will +find the archive file listed as well as the files you saw previously: + +@smallexample +blues folk jazz collection.tar +@end smallexample + +@noindent +Creating the archive @samp{collection.tar} did not destroy the copies of +the files in the directory. + +Keep in mind that if you don't indicate an operation, @command{tar} will not +run and will prompt you for one. If you don't name any files, @command{tar} +will complain. You must have write access to the working directory, +or else you will not be able to create an archive in that directory. + +@emph{Caution}: Do not attempt to use @option{--create} (@option{-c}) to add files to +an existing archive; it will delete the archive and write a new one. +Use @option{--append} (@option{-r}) instead. @xref{append}. + +@node create verbose +@subsection Running @option{--create} with @option{--verbose} + +@xopindex{create, using with @option{--verbose}} +@xopindex{verbose, using with @option{--create}} +If you include the @option{--verbose} (@option{-v}) option on the command line, +@command{tar} will list the files it is acting on as it is working. In +verbose mode, the @code{create} example above would appear as: + +@smallexample +$ @kbd{tar --create --verbose --file=collection.tar blues folk jazz} +blues +folk +jazz +@end smallexample + +This example is just like the example we showed which did not use +@option{--verbose}, except that @command{tar} generated the remaining +@iftex +lines (note the different font styles). +@end iftex +@ifinfo +lines. +@end ifinfo + +In the rest of the examples in this chapter, we will frequently use +@code{verbose} mode so we can show actions or @command{tar} responses that +you would otherwise not see, and which are important for you to +understand. + +@node short create +@subsection Short Forms with @samp{create} + +As we said before, the @option{--create} (@option{-c}) operation is one of the most +basic uses of @command{tar}, and you will use it countless times. +Eventually, you will probably want to use abbreviated (or ``short'') +forms of options. A full discussion of the three different forms that +options can take appears in @ref{Styles}; for now, here is what the +previous example (including the @option{--verbose} (@option{-v}) option) looks like +using short option forms: + +@smallexample +$ @kbd{tar -cvf collection.tar blues folk jazz} +blues +folk +jazz +@end smallexample + +@noindent +As you can see, the system responds the same no matter whether you use +long or short option forms. + +@FIXME{i don't like how this is worded:} One difference between using +short and long option forms is that, although the exact placement of +arguments following options is no more specific when using short forms, +it is easier to become confused and make a mistake when using short +forms. For example, suppose you attempted the above example in the +following way: + +@smallexample +$ @kbd{tar -cfv collection.tar blues folk jazz} +@end smallexample + +@noindent +In this case, @command{tar} will make an archive file called @file{v}, +containing the files @file{blues}, @file{folk}, and @file{jazz}, because +the @samp{v} is the closest ``file name'' to the @option{-f} option, and +is thus taken to be the chosen archive file name. @command{tar} will try +to add a file called @file{collection.tar} to the @file{v} archive file; +if the file @file{collection.tar} did not already exist, @command{tar} will +report an error indicating that this file does not exist. If the file +@file{collection.tar} does already exist (e.g., from a previous command +you may have run), then @command{tar} will add this file to the archive. +Because the @option{-v} option did not get registered, @command{tar} will not +run under @samp{verbose} mode, and will not report its progress. + +The end result is that you may be quite confused about what happened, +and possibly overwrite a file. To illustrate this further, we will show +you how an example we showed previously would look using short forms. + +This example, + +@smallexample +$ @kbd{tar blues --create folk --file=collection.tar jazz} +@end smallexample + +@noindent +is confusing as it is. When shown using short forms, however, it +becomes much more so: + +@smallexample +$ @kbd{tar blues -c folk -f collection.tar jazz} +@end smallexample + +@noindent +It would be very easy to put the wrong string of characters +immediately following the @option{-f}, but doing that could sacrifice +valuable data. + +For this reason, we recommend that you pay very careful attention to +the order of options and placement of file and archive names, +especially when using short option forms. Not having the option name +written out mnemonically can affect how well you remember which option +does what, and therefore where different names have to be placed. + +@node create dir +@subsection Archiving Directories + +@cindex Archiving Directories +@cindex Directories, Archiving +You can archive a directory by specifying its directory name as a +file name argument to @command{tar}. The files in the directory will be +archived relative to the working directory, and the directory will be +re-created along with its contents when the archive is extracted. + +To archive a directory, first move to its superior directory. If you +have followed the previous instructions in this tutorial, you should +type: + +@smallexample +$ @kbd{cd ..} +$ +@end smallexample + +@noindent +This will put you into the directory which contains @file{practice}, +i.e., your home directory. Once in the superior directory, you can +specify the subdirectory, @file{practice}, as a file name argument. To +store @file{practice} in the new archive file @file{music.tar}, type: + +@smallexample +$ @kbd{tar --create --verbose --file=music.tar practice} +@end smallexample + +@noindent +@command{tar} should output: + +@smallexample +practice/ +practice/blues +practice/folk +practice/jazz +practice/collection.tar +@end smallexample + +Note that the archive thus created is not in the subdirectory +@file{practice}, but rather in the current working directory---the +directory from which @command{tar} was invoked. Before trying to archive a +directory from its superior directory, you should make sure you have +write access to the superior directory itself, not only the directory +you are trying archive with @command{tar}. For example, you will probably +not be able to store your home directory in an archive by invoking +@command{tar} from the root directory; @xref{absolute}. (Note +also that @file{collection.tar}, the original archive file, has itself +been archived. @command{tar} will accept any file as a file to be +archived, regardless of its content. When @file{music.tar} is +extracted, the archive file @file{collection.tar} will be re-written +into the file system). + +If you give @command{tar} a command such as + +@smallexample +$ @kbd{tar --create --file=foo.tar .} +@end smallexample + +@noindent +@command{tar} will report @samp{tar: ./foo.tar is the archive; not +dumped}. This happens because @command{tar} creates the archive +@file{foo.tar} in the current directory before putting any files into +it. Then, when @command{tar} attempts to add all the files in the +directory @file{.} to the archive, it notices that the file +@file{./foo.tar} is the same as the archive @file{foo.tar}, and skips +it. (It makes no sense to put an archive into itself.) @GNUTAR{} +will continue in this case, and create the archive +normally, except for the exclusion of that one file. (@emph{Please +note:} Other implementations of @command{tar} may not be so clever; +they will enter an infinite loop when this happens, so you should not +depend on this behavior unless you are certain you are running +@GNUTAR{}. In general, it is wise to always place the archive outside +of the directory being dumped.) + +@node list +@section How to List Archives + +@opindex list +Frequently, you will find yourself wanting to determine exactly what a +particular archive contains. You can use the @option{--list} +(@option{-t}) operation to get the member names as they currently +appear in the archive, as well as various attributes of the files at +the time they were archived. For example, you can examine the archive +@file{collection.tar} that you created in the last section with the +command, + +@smallexample +$ @kbd{tar --list --file=collection.tar} +@end smallexample + +@noindent +The output of @command{tar} would then be: + +@smallexample +blues +folk +jazz +@end smallexample + +@noindent +The archive @file{bfiles.tar} would list as follows: + +@smallexample +./birds +baboon +./box +@end smallexample + +@noindent +Be sure to use a @option{--file=@var{archive-name}} (@option{-f +@var{archive-name}}) option just as with @option{--create} +(@option{-c}) to specify the name of the archive. + +@xopindex{list, using with @option{--verbose}} +@xopindex{verbose, using with @option{--list}} +If you use the @option{--verbose} (@option{-v}) option with +@option{--list}, then @command{tar} will print out a listing +reminiscent of @w{@samp{ls -l}}, showing owner, file size, and so +forth. This output is described in detail in @ref{verbose member listing}. + +If you had used @option{--verbose} (@option{-v}) mode, the example +above would look like: + +@smallexample +$ @kbd{tar --list --verbose --file=collection.tar folk} +-rw-r--r-- myself/user 62 1990-05-23 10:55 folk +@end smallexample + +@cindex listing member and file names +@anchor{listing member and file names} +It is important to notice that the output of @kbd{tar --list +--verbose} does not necessarily match that produced by @kbd{tar +--create --verbose} while creating the archive. It is because +@GNUTAR{}, unless told explicitly not to do so, removes some directory +prefixes from file names before storing them in the archive +(@xref{absolute}, for more information). In other +words, in verbose mode @GNUTAR{} shows @dfn{file names} when creating +an archive and @dfn{member names} when listing it. Consider this +example: + +@smallexample +@group +$ @kbd{tar --create --verbose --file archive /etc/mail} +tar: Removing leading `/' from member names +/etc/mail/ +/etc/mail/sendmail.cf +/etc/mail/aliases +$ @kbd{tar --test --file archive} +etc/mail/ +etc/mail/sendmail.cf +etc/mail/aliases +@end group +@end smallexample + +@opindex show-stored-names + This default behavior can sometimes be inconvenient. You can force +@GNUTAR{} show member names when creating archive by supplying +@option{--show-stored-names} option. + +@table @option +@item --show-stored-names +Print member (as opposed to @emph{file}) names when creating the archive. +@end table + +@cindex File name arguments, using @option{--list} with +@xopindex{list, using with file name arguments} +You can specify one or more individual member names as arguments when +using @samp{list}. In this case, @command{tar} will only list the +names of members you identify. For example, @w{@kbd{tar --list +--file=afiles.tar apple}} would only print @file{apple}. + +Because @command{tar} preserves file names, these must be specified as +they appear in the archive (i.e., relative to the directory from which +the archive was created). Therefore, it is essential when specifying +member names to @command{tar} that you give the exact member names. +For example, @w{@kbd{tar --list --file=bfiles.tar birds}} would produce an +error message something like @samp{tar: birds: Not found in archive}, +because there is no member named @file{birds}, only one named +@file{./birds}. While the names @file{birds} and @file{./birds} name +the same file, @emph{member} names by default are compared verbatim. + +However, @w{@kbd{tar --list --file=bfiles.tar baboon}} would respond +with @file{baboon}, because this exact member name is in the archive file +@file{bfiles.tar}. If you are not sure of the exact file name, +use @dfn{globbing patterns}, for example: + +@smallexample +$ @kbd{tar --list --file=bfiles.tar --wildcards '*b*'} +@end smallexample + +@noindent +will list all members whose name contains @samp{b}. @xref{wildcards}, +for a detailed discussion of globbing patterns and related +@command{tar} command line options. + +@menu +* list dir:: +@end menu + +@node list dir +@unnumberedsubsec Listing the Contents of a Stored Directory + +To get information about the contents of an archived directory, +use the directory name as a file name argument in conjunction with +@option{--list} (@option{-t}). To find out file attributes, include the +@option{--verbose} (@option{-v}) option. + +For example, to find out about files in the directory @file{practice}, in +the archive file @file{music.tar}, type: + +@smallexample +$ @kbd{tar --list --verbose --file=music.tar practice} +@end smallexample + +@command{tar} responds: + +@smallexample +drwxrwxrwx myself/user 0 1990-05-31 21:49 practice/ +-rw-r--r-- myself/user 42 1990-05-21 13:29 practice/blues +-rw-r--r-- myself/user 62 1990-05-23 10:55 practice/folk +-rw-r--r-- myself/user 40 1990-05-21 13:30 practice/jazz +-rw-r--r-- myself/user 10240 1990-05-31 21:49 practice/collection.tar +@end smallexample + +When you use a directory name as a file name argument, @command{tar} acts on +all the files (including sub-directories) in that directory. + +@node extract +@section How to Extract Members from an Archive +@cindex Extraction +@cindex Retrieving files from an archive +@cindex Resurrecting files from an archive + +@opindex extract +Creating an archive is only half the job---there is no point in storing +files in an archive if you can't retrieve them. The act of retrieving +members from an archive so they can be used and manipulated as +unarchived files again is called @dfn{extraction}. To extract files +from an archive, use the @option{--extract} (@option{--get} or +@option{-x}) operation. As with @option{--create}, specify the name +of the archive with @option{--file} (@option{-f}) option. Extracting +an archive does not modify the archive in any way; you can extract it +multiple times if you want or need to. + +Using @option{--extract}, you can extract an entire archive, or specific +files. The files can be directories containing other files, or not. As +with @option{--create} (@option{-c}) and @option{--list} (@option{-t}), you may use the short or the +long form of the operation without affecting the performance. + +@menu +* extracting archives:: +* extracting files:: +* extract dir:: +* extracting untrusted archives:: +* failing commands:: +@end menu + +@node extracting archives +@subsection Extracting an Entire Archive + +To extract an entire archive, specify the archive file name only, with +no individual file names as arguments. For example, + +@smallexample +$ @kbd{tar -xvf collection.tar} +@end smallexample + +@noindent +produces this: + +@smallexample +-rw-r--r-- me/user 28 1996-10-18 16:31 jazz +-rw-r--r-- me/user 21 1996-09-23 16:44 blues +-rw-r--r-- me/user 20 1996-09-23 16:44 folk +@end smallexample + +@node extracting files +@subsection Extracting Specific Files + +To extract specific archive members, give their exact member names as +arguments, as printed by @option{--list} (@option{-t}). If you had +mistakenly deleted one of the files you had placed in the archive +@file{collection.tar} earlier (say, @file{blues}), you can extract it +from the archive without changing the archive's structure. Its +contents will be identical to the original file @file{blues} that you +deleted. + +First, make sure you are in the @file{practice} directory, and list the +files in the directory. Now, delete the file, @samp{blues}, and list +the files in the directory again. + +You can now extract the member @file{blues} from the archive file +@file{collection.tar} like this: + +@smallexample +$ @kbd{tar --extract --file=collection.tar blues} +@end smallexample + +@noindent +If you list the files in the directory again, you will see that the file +@file{blues} has been restored, with its original permissions, data +modification times, and owner.@footnote{This is only accidentally +true, but not in general. Whereas modification times are always +restored, in most cases, one has to be root for restoring the owner, +and use a special option for restoring permissions. Here, it just +happens that the restoring user is also the owner of the archived +members, and that the current @code{umask} is compatible with original +permissions.} (These parameters will be identical to those which +the file had when you originally placed it in the archive; any changes +you may have made before deleting the file from the file system, +however, will @emph{not} have been made to the archive member.) The +archive file, @samp{collection.tar}, is the same as it was before you +extracted @samp{blues}. You can confirm this by running @command{tar} with +@option{--list} (@option{-t}). + +Remember that as with other operations, specifying the exact member +name is important. @w{@kbd{tar --extract --file=bfiles.tar birds}} +will fail, because there is no member named @file{birds}. To extract +the member named @file{./birds}, you must specify @w{@kbd{tar +--extract --file=bfiles.tar ./birds}}. If you don't remember the +exact member names, use @option{--list} (@option{-t}) option +(@pxref{list}). You can also extract those members that match a +specific @dfn{globbing pattern}. For example, to extract from +@file{bfiles.tar} all files that begin with @samp{b}, no matter their +directory prefix, you could type: + +@smallexample +$ @kbd{tar -x -f bfiles.tar --wildcards --no-anchored 'b*'} +@end smallexample + +@noindent +Here, @option{--wildcards} instructs @command{tar} to treat +command line arguments as globbing patterns and @option{--no-anchored} +informs it that the patterns apply to member names after any @samp{/} +delimiter. The use of globbing patterns is discussed in detail in +@xref{wildcards}. + +You can extract a file to standard output by combining the above options +with the @option{--to-stdout} (@option{-O}) option (@pxref{Writing to Standard +Output}). + +If you give the @option{--verbose} option, then @option{--extract} +will print the names of the archive members as it extracts them. + +@node extract dir +@subsection Extracting Files that are Directories + +Extracting directories which are members of an archive is similar to +extracting other files. The main difference to be aware of is that if +the extracted directory has the same name as any directory already in +the working directory, then files in the extracted directory will be +placed into the directory of the same name. Likewise, if there are +files in the pre-existing directory with the same names as the members +which you extract, the files from the extracted archive will replace +the files already in the working directory (and possible +subdirectories). This will happen regardless of whether or not the +files in the working directory were more recent than those extracted +(there exist, however, special options that alter this behavior +@pxref{Writing}). + +However, if a file was stored with a directory name as part of its file +name, and that directory does not exist under the working directory when +the file is extracted, @command{tar} will create the directory. + +We can demonstrate how to use @option{--extract} to extract a directory +file with an example. Change to the @file{practice} directory if you +weren't there, and remove the files @file{folk} and @file{jazz}. Then, +go back to the parent directory and extract the archive +@file{music.tar}. You may either extract the entire archive, or you may +extract only the files you just deleted. To extract the entire archive, +don't give any file names as arguments after the archive name +@file{music.tar}. To extract only the files you deleted, use the +following command: + +@smallexample +$ @kbd{tar -xvf music.tar practice/folk practice/jazz} +practice/folk +practice/jazz +@end smallexample + +@noindent +If you were to specify two @option{--verbose} (@option{-v}) options, @command{tar} +would have displayed more detail about the extracted files, as shown +in the example below: + +@smallexample +$ @kbd{tar -xvvf music.tar practice/folk practice/jazz} +-rw-r--r-- me/user 28 1996-10-18 16:31 practice/jazz +-rw-r--r-- me/user 20 1996-09-23 16:44 practice/folk +@end smallexample + +@noindent +Because you created the directory with @file{practice} as part of the +file names of each of the files by archiving the @file{practice} +directory as @file{practice}, you must give @file{practice} as part +of the file names when you extract those files from the archive. + +@node extracting untrusted archives +@subsection Extracting Archives from Untrusted Sources + +Extracting files from archives can overwrite files that already exist. +If you receive an archive from an untrusted source, you should make a +new directory and extract into that directory, so that you don't have +to worry about the extraction overwriting one of your existing files. +For example, if @file{untrusted.tar} came from somewhere else on the +Internet, and you don't necessarily trust its contents, you can +extract it as follows: + +@smallexample +$ @kbd{mkdir newdir} +$ @kbd{cd newdir} +$ @kbd{tar -xvf ../untrusted.tar} +@end smallexample + +It is also a good practice to examine contents of the archive +before extracting it, using @option{--list} (@option{-t}) option, possibly combined +with @option{--verbose} (@option{-v}). + +@node failing commands +@subsection Commands That Will Fail + +Here are some sample commands you might try which will not work, and why +they won't work. + +If you try to use this command, + +@smallexample +$ @kbd{tar -xvf music.tar folk jazz} +@end smallexample + +@noindent +you will get the following response: + +@smallexample +tar: folk: Not found in archive +tar: jazz: Not found in archive +@end smallexample + +@noindent +This is because these files were not originally @emph{in} the parent +directory @file{..}, where the archive is located; they were in the +@file{practice} directory, and their file names reflect this: + +@smallexample +$ @kbd{tar -tvf music.tar} +practice/blues +practice/folk +practice/jazz +@end smallexample + +@FIXME{make sure the above works when going through the examples in +order...} + +@noindent +Likewise, if you try to use this command, + +@smallexample +$ @kbd{tar -tvf music.tar folk jazz} +@end smallexample + +@noindent +you would get a similar response. Members with those names are not in the +archive. You must use the correct member names, or wildcards, in order +to extract the files from the archive. + +If you have forgotten the correct names of the files in the archive, +use @w{@kbd{tar --list --verbose}} to list them correctly. + +@FIXME{more examples, here? hag thinks it's a good idea.} + +@node going further +@section Going Further Ahead in this Manual +@UNREVISED + +@FIXME{need to write up a node here about the things that are going to +be in the rest of the manual.} + +@node tar invocation +@chapter Invoking @GNUTAR{} + +This chapter is about how one invokes the @GNUTAR{} +command, from the command synopsis (@pxref{Synopsis}). There are +numerous options, and many styles for writing them. One mandatory +option specifies the operation @command{tar} should perform +(@pxref{Operation Summary}), other options are meant to detail how +this operation should be performed (@pxref{Option Summary}). +Non-option arguments are not always interpreted the same way, +depending on what the operation is. + +You will find in this chapter everything about option styles and rules for +writing them (@pxref{Styles}). On the other hand, operations and options +are fully described elsewhere, in other chapters. Here, you will find +only synthetic descriptions for operations and options, together with +pointers to other parts of the @command{tar} manual. + +Some options are so special they are fully described right in this +chapter. They have the effect of inhibiting the normal operation of +@command{tar} or else, they globally alter the amount of feedback the user +receives about what is going on. These are the @option{--help} and +@option{--version} (@pxref{help}), @option{--verbose} (@pxref{verbose}) +and @option{--interactive} options (@pxref{interactive}). + +@menu +* Synopsis:: +* using tar options:: +* Styles:: +* All Options:: +* help:: +* defaults:: +* verbose:: +* checkpoints:: +* warnings:: +* interactive:: +@end menu + +@node Synopsis +@section General Synopsis of @command{tar} + +The @GNUTAR{} program is invoked as either one of: + +@smallexample +@kbd{tar @var{option}@dots{} [@var{name}]@dots{}} +@kbd{tar @var{letter}@dots{} [@var{argument}]@dots{} [@var{option}]@dots{} [@var{name}]@dots{}} +@end smallexample + +The second form is for when old options are being used. + +You can use @command{tar} to store files in an archive, to extract them from +an archive, and to do other types of archive manipulation. The primary +argument to @command{tar}, which is called the @dfn{operation}, specifies +which action to take. The other arguments to @command{tar} are either +@dfn{options}, which change the way @command{tar} performs an operation, +or file names or archive members, which specify the files or members +@command{tar} is to act on. + +You can actually type in arguments in any order, even if in this manual +the options always precede the other arguments, to make examples easier +to understand. Further, the option stating the main operation mode +(the @command{tar} main command) is usually given first. + +Each @var{name} in the synopsis above is interpreted as an archive member +name when the main command is one of @option{--compare} +(@option{--diff}, @option{-d}), @option{--delete}, @option{--extract} +(@option{--get}, @option{-x}), @option{--list} (@option{-t}) or +@option{--update} (@option{-u}). When naming archive members, you +must give the exact name of the member in the archive, as it is +printed by @option{--list}. For @option{--append} (@option{-r}) and +@option{--create} (@option{-c}), these @var{name} arguments specify +the names of either files or directory hierarchies to place in the archive. +These files or hierarchies should already exist in the file system, +prior to the execution of the @command{tar} command. + +@command{tar} interprets relative file names as being relative to the +working directory. @command{tar} will make all file names relative +(by removing leading slashes when archiving or restoring files), +unless you specify otherwise (using the @option{--absolute-names} +option). @xref{absolute}, for more information about +@option{--absolute-names}. + +If you give the name of a directory as either a file name or a member +name, then @command{tar} acts recursively on all the files and directories +beneath that directory. For example, the name @file{/} identifies all +the files in the file system to @command{tar}. + +The distinction between file names and archive member names is especially +important when shell globbing is used, and sometimes a source of confusion +for newcomers. @xref{wildcards}, for more information about globbing. +The problem is that shells may only glob using existing files in the +file system. Only @command{tar} itself may glob on archive members, so when +needed, you must ensure that wildcard characters reach @command{tar} without +being interpreted by the shell first. Using a backslash before @samp{*} +or @samp{?}, or putting the whole argument between quotes, is usually +sufficient for this. + +Even if @var{name}s are often specified on the command line, they +can also be read from a text file in the file system, using the +@option{--files-from=@var{file-of-names}} (@option{-T @var{file-of-names}}) option. + +If you don't use any file name arguments, @option{--append} (@option{-r}), +@option{--delete} and @option{--concatenate} (@option{--catenate}, +@option{-A}) will do nothing, while @option{--create} (@option{-c}) +will usually yield a diagnostic and inhibit @command{tar} execution. +The other operations of @command{tar} (@option{--list}, +@option{--extract}, @option{--compare}, and @option{--update}) +will act on the entire contents of the archive. + +@cindex exit status +@cindex return status +Besides successful exits, @GNUTAR{} may fail for +many reasons. Some reasons correspond to bad usage, that is, when the +@command{tar} command line is improperly written. Errors may be +encountered later, while processing the archive or the files. Some +errors are recoverable, in which case the failure is delayed until +@command{tar} has completed all its work. Some errors are such that +it would be not meaningful, or at least risky, to continue processing: +@command{tar} then aborts processing immediately. All abnormal exits, +whether immediate or delayed, should always be clearly diagnosed on +@code{stderr}, after a line stating the nature of the error. + +Possible exit codes of @GNUTAR{} are summarized in the following +table: + +@table @asis +@item 0 +@samp{Successful termination}. + +@item 1 +@samp{Some files differ}. If tar was invoked with @option{--compare} +(@option{--diff}, @option{-d}) command line option, this means that +some files in the archive differ from their disk counterparts +(@pxref{compare}). If tar was given @option{--create}, +@option{--append} or @option{--update} option, this exit code means +that some files were changed while being archived and so the resulting +archive does not contain the exact copy of the file set. + +@item 2 +@samp{Fatal error}. This means that some fatal, unrecoverable error +occurred. +@end table + +If @command{tar} has invoked a subprocess and that subprocess exited with a +nonzero exit code, @command{tar} exits with that code as well. +This can happen, for example, if @command{tar} was given some +compression option (@pxref{gzip}) and the external compressor program +failed. Another example is @command{rmt} failure during backup to the +remote device (@pxref{Remote Tape Server}). + +@node using tar options +@section Using @command{tar} Options + +@GNUTAR{} has a total of eight operating modes which +allow you to perform a variety of tasks. You are required to choose +one operating mode each time you employ the @command{tar} program by +specifying one, and only one operation as an argument to the +@command{tar} command (the corresponding options may be found +at @ref{frequent operations} and @ref{Operations}). Depending on +circumstances, you may also wish to customize how the chosen operating +mode behaves. For example, you may wish to change the way the output +looks, or the format of the files that you wish to archive may require +you to do something special in order to make the archive look right. + +You can customize and control @command{tar}'s performance by running +@command{tar} with one or more options (such as @option{--verbose} +(@option{-v}), which we used in the tutorial). As we said in the +tutorial, @dfn{options} are arguments to @command{tar} which are (as +their name suggests) optional. Depending on the operating mode, you +may specify one or more options. Different options will have different +effects, but in general they all change details of the operation, such +as archive format, archive name, or level of user interaction. Some +options make sense with all operating modes, while others are +meaningful only with particular modes. You will likely use some +options frequently, while you will only use others infrequently, or +not at all. (A full list of options is available in @pxref{All Options}.) + +@vrindex TAR_OPTIONS, environment variable +@anchor{TAR_OPTIONS} +The @env{TAR_OPTIONS} environment variable specifies default options to +be placed in front of any explicit options. For example, if +@code{TAR_OPTIONS} is @samp{-v --unlink-first}, @command{tar} behaves as +if the two options @option{-v} and @option{--unlink-first} had been +specified before any explicit options. Option specifications are +separated by whitespace. A backslash escapes the next character, so it +can be used to specify an option containing whitespace or a backslash. + +Note that @command{tar} options are case sensitive. For example, the +options @option{-T} and @option{-t} are different; the first requires an +argument for stating the name of a file providing a list of @var{name}s, +while the second does not require an argument and is another way to +write @option{--list} (@option{-t}). + +In addition to the eight operations, there are many options to +@command{tar}, and three different styles for writing both: long (mnemonic) +form, short form, and old style. These styles are discussed below. +Both the options and the operations can be written in any of these three +styles. + +@FIXME{menu at end of this node. need to think of an actual outline +for this chapter; probably do that after stuff from chapter 4 is +incorporated.} + +@node Styles +@section The Three Option Styles + +There are three styles for writing operations and options to the command +line invoking @command{tar}. The different styles were developed at +different times during the history of @command{tar}. These styles will be +presented below, from the most recent to the oldest. + +Some options must take an argument@footnote{For example, @option{--file} +(@option{-f}) takes the name of an archive file as an argument. If +you do not supply an archive file name, @command{tar} will use a +default, but this can be confusing; thus, we recommend that you always +supply a specific archive file name.}. Where you @emph{place} the +arguments generally depends on which style of options you choose. We +will detail specific information relevant to each option style in the +sections on the different option styles, below. The differences are +subtle, yet can often be very important; incorrect option placement +can cause you to overwrite a number of important files. We urge you +to note these differences, and only use the option style(s) which +makes the most sense to you until you feel comfortable with the others. + +Some options @emph{may} take an argument. Such options may have at +most long and short forms, they do not have old style equivalent. The +rules for specifying an argument for such options are stricter than +those for specifying mandatory arguments. Please, pay special +attention to them. + +@menu +* Long Options:: Long Option Style +* Short Options:: Short Option Style +* Old Options:: Old Option Style +* Mixing:: Mixing Option Styles +@end menu + +@node Long Options +@subsection Long Option Style + +@cindex long options +@cindex options, long style +@cindex options, GNU style +@cindex options, mnemonic names +Each option has at least one @dfn{long} (or @dfn{mnemonic}) name starting with two +dashes in a row, e.g., @option{--list}. The long names are more clear than +their corresponding short or old names. It sometimes happens that a +single long option has many different names which are +synonymous, such as @option{--compare} and @option{--diff}. In addition, +long option names can be given unique abbreviations. For example, +@option{--cre} can be used in place of @option{--create} because there is no +other long option which begins with @samp{cre}. (One way to find +this out is by trying it and seeing what happens; if a particular +abbreviation could represent more than one option, @command{tar} will tell +you that that abbreviation is ambiguous and you'll know that that +abbreviation won't work. You may also choose to run @samp{tar --help} +to see a list of options. Be aware that if you run @command{tar} with a +unique abbreviation for the long name of an option you didn't want to +use, you are stuck; @command{tar} will perform the command as ordered.) + +Long options are meant to be obvious and easy to remember, and their +meanings are generally easier to discern than those of their +corresponding short options (see below). For example: + +@smallexample +$ @kbd{tar --create --verbose --blocking-factor=20 --file=/dev/rmt0} +@end smallexample + +@noindent +gives a fairly good set of hints about what the command does, even +for those not fully acquainted with @command{tar}. + +@cindex arguments to long options +@cindex long options with mandatory arguments +Long options which require arguments take those arguments +immediately following the option name. There are two ways of +specifying a mandatory argument. It can be separated from the +option name either by an equal sign, or by any amount of +white space characters. For example, the @option{--file} option (which +tells the name of the @command{tar} archive) is given a file such as +@file{archive.tar} as argument by using any of the following notations: +@option{--file=archive.tar} or @option{--file archive.tar}. + +@cindex optional arguments to long options +@cindex long options with optional arguments +In contrast, optional arguments must always be introduced using +an equal sign. For example, the @option{--backup} option takes +an optional argument specifying backup type. It must be used +as @option{--backup=@var{backup-type}}. + +@node Short Options +@subsection Short Option Style + +@cindex short options +@cindex options, short style +@cindex options, traditional +Most options also have a @dfn{short option} name. Short options start with +a single dash, and are followed by a single character, e.g., @option{-t} +(which is equivalent to @option{--list}). The forms are absolutely +identical in function; they are interchangeable. + +The short option names are faster to type than long option names. + +@cindex arguments to short options +@cindex short options with mandatory arguments +Short options which require arguments take their arguments immediately +following the option, usually separated by white space. It is also +possible to stick the argument right after the short option name, using +no intervening space. For example, you might write @w{@option{-f +archive.tar}} or @option{-farchive.tar} instead of using +@option{--file=archive.tar}. Both @option{--file=@var{archive-name}} and +@w{@option{-f @var{archive-name}}} denote the option which indicates a +specific archive, here named @file{archive.tar}. + +@cindex optional arguments to short options +@cindex short options with optional arguments +Short options which take optional arguments take their arguments +immediately following the option letter, @emph{without any intervening +white space characters}. + +Short options' letters may be clumped together, but you are not +required to do this (as compared to old options; see below). When +short options are clumped as a set, use one (single) dash for them +all, e.g., @w{@samp{@command{tar} -cvf}}. Only the last option in +such a set is allowed to have an argument@footnote{Clustering many +options, the last of which has an argument, is a rather opaque way to +write options. Some wonder if @acronym{GNU} @code{getopt} should not +even be made helpful enough for considering such usages as invalid.}. + +When the options are separated, the argument for each option which requires +an argument directly follows that option, as is usual for Unix programs. +For example: + +@smallexample +$ @kbd{tar -c -v -b 20 -f /dev/rmt0} +@end smallexample + +If you reorder short options' locations, be sure to move any arguments +that belong to them. If you do not move the arguments properly, you may +end up overwriting files. + +@node Old Options +@subsection Old Option Style +@cindex options, old style +@cindex old option style + +Like short options, @dfn{old options} are single letters. However, old options +must be written together as a single clumped set, without spaces separating +them or dashes preceding them@footnote{Beware that if you precede options +with a dash, you are announcing the short option style instead of the +old option style; short options are decoded differently.}. This set +of letters must be the first to appear on the command line, after the +@command{tar} program name and some white space; old options cannot appear +anywhere else. The letter of an old option is exactly the same letter as +the corresponding short option. For example, the old option @samp{t} is +the same as the short option @option{-t}, and consequently, the same as the +long option @option{--list}. So for example, the command @w{@samp{tar +cv}} specifies the option @option{-v} in addition to the operation @option{-c}. + +@cindex arguments to old options +@cindex old options with mandatory arguments +When options that need arguments are given together with the command, +all the associated arguments follow, in the same order as the options. +Thus, the example given previously could also be written in the old +style as follows: + +@smallexample +$ @kbd{tar cvbf 20 /dev/rmt0} +@end smallexample + +@noindent +Here, @samp{20} is the argument of @option{-b} and @samp{/dev/rmt0} is +the argument of @option{-f}. + +On the other hand, this old style syntax makes it difficult to match +option letters with their corresponding arguments, and is often +confusing. In the command @w{@samp{tar cvbf 20 /dev/rmt0}}, for example, +@samp{20} is the argument for @option{-b}, @samp{/dev/rmt0} is the +argument for @option{-f}, and @option{-v} does not have a corresponding +argument. Even using short options like in @w{@samp{tar -c -v -b 20 -f +/dev/rmt0}} is clearer, putting all arguments next to the option they +pertain to. + +If you want to reorder the letters in the old option argument, be +sure to reorder any corresponding argument appropriately. + +This old way of writing @command{tar} options can surprise even experienced +users. For example, the two commands: + +@smallexample +@kbd{tar cfz archive.tar.gz file} +@kbd{tar -cfz archive.tar.gz file} +@end smallexample + +@noindent +are quite different. The first example uses @file{archive.tar.gz} as +the value for option @samp{f} and recognizes the option @samp{z}. The +second example, however, uses @file{z} as the value for option +@samp{f} --- probably not what was intended. + +Old options are kept for compatibility with old versions of @command{tar}. + +This second example could be corrected in many ways, among which the +following are equivalent: + +@smallexample +@kbd{tar -czf archive.tar.gz file} +@kbd{tar -cf archive.tar.gz -z file} +@kbd{tar cf archive.tar.gz -z file} +@end smallexample + +@cindex option syntax, traditional +As far as we know, all @command{tar} programs, @acronym{GNU} and +non-@acronym{GNU}, support old options. @GNUTAR{} +supports them not only for historical reasons, but also because many +people are used to them. For compatibility with Unix @command{tar}, +the first argument is always treated as containing command and option +letters even if it doesn't start with @samp{-}. Thus, @samp{tar c} is +equivalent to @w{@samp{tar -c}:} both of them specify the +@option{--create} (@option{-c}) command to create an archive. + +@node Mixing +@subsection Mixing Option Styles + +@cindex options, mixing different styles +All three styles may be intermixed in a single @command{tar} command, +so long as the rules for each style are fully +respected@footnote{Before @GNUTAR{} version 1.11.6, +a bug prevented intermixing old style options with long options in +some cases.}. Old style options and either of the modern styles of +options may be mixed within a single @command{tar} command. However, +old style options must be introduced as the first arguments only, +following the rule for old options (old options must appear directly +after the @command{tar} command and some white space). Modern options +may be given only after all arguments to the old options have been +collected. If this rule is not respected, a modern option might be +falsely interpreted as the value of the argument to one of the old +style options. + +For example, all the following commands are wholly equivalent, and +illustrate the many combinations and orderings of option styles. + +@smallexample +@kbd{tar --create --file=archive.tar} +@kbd{tar --create -f archive.tar} +@kbd{tar --create -farchive.tar} +@kbd{tar --file=archive.tar --create} +@kbd{tar --file=archive.tar -c} +@kbd{tar -c --file=archive.tar} +@kbd{tar -c -f archive.tar} +@kbd{tar -c -farchive.tar} +@kbd{tar -cf archive.tar} +@kbd{tar -cfarchive.tar} +@kbd{tar -f archive.tar --create} +@kbd{tar -f archive.tar -c} +@kbd{tar -farchive.tar --create} +@kbd{tar -farchive.tar -c} +@kbd{tar c --file=archive.tar} +@kbd{tar c -f archive.tar} +@kbd{tar c -farchive.tar} +@kbd{tar cf archive.tar} +@kbd{tar f archive.tar --create} +@kbd{tar f archive.tar -c} +@kbd{tar fc archive.tar} +@end smallexample + +On the other hand, the following commands are @emph{not} equivalent to +the previous set: + +@smallexample +@kbd{tar -f -c archive.tar} +@kbd{tar -fc archive.tar} +@kbd{tar -fcarchive.tar} +@kbd{tar -farchive.tarc} +@kbd{tar cfarchive.tar} +@end smallexample + +@noindent +These last examples mean something completely different from what the +user intended (judging based on the example in the previous set which +uses long options, whose intent is therefore very clear). The first +four specify that the @command{tar} archive would be a file named +@option{-c}, @samp{c}, @samp{carchive.tar} or @samp{archive.tarc}, +respectively. The first two examples also specify a single non-option, +@var{name} argument having the value @samp{archive.tar}. The last +example contains only old style option letters (repeating option +@samp{c} twice), not all of which are meaningful (eg., @samp{.}, +@samp{h}, or @samp{i}), with no argument value. @FIXME{not sure i liked +the first sentence of this paragraph..} + +@node All Options +@section All @command{tar} Options + +The coming manual sections contain an alphabetical listing of all +@command{tar} operations and options, with brief descriptions and +cross-references to more in-depth explanations in the body of the manual. +They also contain an alphabetically arranged table of the short option +forms with their corresponding long option. You can use this table as +a reference for deciphering @command{tar} commands in scripts. + +@menu +* Operation Summary:: +* Option Summary:: +* Short Option Summary:: +@end menu + +@node Operation Summary +@subsection Operations + +@table @option + +@opsummary{append} +@item --append +@itemx -r + +Appends files to the end of the archive. @xref{append}. + +@opsummary{catenate} +@item --catenate +@itemx -A + +Same as @option{--concatenate}. @xref{concatenate}. + +@opsummary{compare} +@item --compare +@itemx -d + +Compares archive members with their counterparts in the file +system, and reports differences in file size, mode, owner, +modification date and contents. @xref{compare}. + +@opsummary{concatenate} +@item --concatenate +@itemx -A + +Appends other @command{tar} archives to the end of the archive. +@xref{concatenate}. + +@opsummary{create} +@item --create +@itemx -c + +Creates a new @command{tar} archive. @xref{create}. + +@opsummary{delete} +@item --delete + +Deletes members from the archive. Don't try this on an archive on a +tape! @xref{delete}. + +@opsummary{diff} +@item --diff +@itemx -d + +Same @option{--compare}. @xref{compare}. + +@opsummary{extract} +@item --extract +@itemx -x + +Extracts members from the archive into the file system. @xref{extract}. + +@opsummary{get} +@item --get +@itemx -x + +Same as @option{--extract}. @xref{extract}. + +@opsummary{list} +@item --list +@itemx -t + +Lists the members in an archive. @xref{list}. + +@opsummary{update} +@item --update +@itemx -u + +Adds files to the end of the archive, but only if they are newer than +their counterparts already in the archive, or if they do not already +exist in the archive. @xref{update}. + +@end table + +@node Option Summary +@subsection @command{tar} Options + +@table @option + +@opsummary{absolute-names} +@item --absolute-names +@itemx -P + +Normally when creating an archive, @command{tar} strips an initial +@samp{/} from member names. This option disables that behavior. +@xref{absolute}. + +@opsummary{after-date} +@item --after-date + +(See @option{--newer}, @pxref{after}) + +@opsummary{anchored} +@item --anchored +A pattern must match an initial subsequence of the name's components. +@xref{controlling pattern-matching}. + +@opsummary{atime-preserve} +@item --atime-preserve +@itemx --atime-preserve=replace +@itemx --atime-preserve=system + +Attempt to preserve the access time of files when reading them. This +option currently is effective only on files that you own, unless you +have superuser privileges. + +@option{--atime-preserve=replace} remembers the access time of a file +before reading it, and then restores the access time afterwards. This +may cause problems if other programs are reading the file at the same +time, as the times of their accesses will be lost. On most platforms +restoring the access time also requires @command{tar} to restore the +data modification time too, so this option may also cause problems if +other programs are writing the file at the same time (@command{tar} attempts +to detect this situation, but cannot do so reliably due to race +conditions). Worse, on most platforms restoring the access time also +updates the status change time, which means that this option is +incompatible with incremental backups. + +@option{--atime-preserve=system} avoids changing time stamps on files, +without interfering with time stamp updates +caused by other programs, so it works better with incremental backups. +However, it requires a special @code{O_NOATIME} option from the +underlying operating and file system implementation, and it also requires +that searching directories does not update their access times. As of +this writing (November 2005) this works only with Linux, and only with +Linux kernels 2.6.8 and later. Worse, there is currently no reliable +way to know whether this feature actually works. Sometimes +@command{tar} knows that it does not work, and if you use +@option{--atime-preserve=system} then @command{tar} complains and +exits right away. But other times @command{tar} might think that the +option works when it actually does not. + +Currently @option{--atime-preserve} with no operand defaults to +@option{--atime-preserve=replace}, but this may change in the future +as support for @option{--atime-preserve=system} improves. + +If your operating or file system does not support +@option{--atime-preserve=@-system}, you might be able to preserve access +times reliably by using the @command{mount} command. For example, +you can mount the file system read-only, or access the file system via +a read-only loopback mount, or use the @samp{noatime} mount option +available on some systems. However, mounting typically requires +superuser privileges and can be a pain to manage. + +@opsummary{auto-compress} +@item --auto-compress +@itemx -a + +During a @option{--create} operation, enables automatic compressed +format recognition based on the archive suffix. The effect of this +option is cancelled by @option{--no-auto-compress}. @xref{gzip}. + +@opsummary{backup} +@item --backup=@var{backup-type} + +Rather than deleting files from the file system, @command{tar} will +back them up using simple or numbered backups, depending upon +@var{backup-type}. @xref{backup}. + +@opsummary{block-number} +@item --block-number +@itemx -R + +With this option present, @command{tar} prints error messages for read errors +with the block number in the archive file. @xref{block-number}. + +@opsummary{blocking-factor} +@item --blocking-factor=@var{blocking} +@itemx -b @var{blocking} + +Sets the blocking factor @command{tar} uses to @var{blocking} x 512 bytes per +record. @xref{Blocking Factor}. + +@opsummary{bzip2} +@item --bzip2 +@itemx -j + +This option tells @command{tar} to read or write archives through +@code{bzip2}. @xref{gzip}. + +@opsummary{check-device} +@item --check-device +Check device numbers when creating a list of modified files for +incremental archiving. This is the default. @xref{device numbers}, +for a detailed description. + +@opsummary{checkpoint} +@item --checkpoint[=@var{number}] + +This option directs @command{tar} to print periodic checkpoint +messages as it reads through the archive. It is intended for when you +want a visual indication that @command{tar} is still running, but +don't want to see @option{--verbose} output. You can also instruct +@command{tar} to execute a list of actions on each checkpoint, see +@option{--checkpoint-action} below. For a detailed description, see +@ref{checkpoints}. + +@opsummary{checkpoint-action} +@item --checkpoint-action=@var{action} +Instruct @command{tar} to execute an action upon hitting a +breakpoint. Here we give only a brief outline. @xref{checkpoints}, +for a complete description. + +The @var{action} argument can be one of the following: + +@table @asis +@item bell +Produce an audible bell on the console. + +@item dot +@itemx . +Print a single dot on the standard listing stream. + +@item echo +Display a textual message on the standard error, with the status and +number of the checkpoint. This is the default. + +@item echo=@var{string} +Display @var{string} on the standard error. Before output, the string +is subject to meta-character expansion. + +@item exec=@var{command} +Execute the given @var{command}. + +@item sleep=@var{time} +Wait for @var{time} seconds. + +@item ttyout=@var{string} +Output @var{string} on the current console (@file{/dev/tty}). +@end table + +Several @option{--checkpoint-action} options can be specified. The +supplied actions will be executed in order of their appearance in the +command line. + +Using @option{--checkpoint-action} without @option{--checkpoint} +assumes default checkpoint frequency of one checkpoint per 10 records. + +@opsummary{check-links} +@item --check-links +@itemx -l +If this option was given, @command{tar} will check the number of links +dumped for each processed file. If this number does not match the +total number of hard links for the file, a warning message will be +output @footnote{Earlier versions of @GNUTAR{} understood @option{-l} as a +synonym for @option{--one-file-system}. The current semantics, which +complies to UNIX98, was introduced with version +1.15.91. @xref{Changes}, for more information.}. + +@xref{hard links}. + +@opsummary{compress} +@opsummary{uncompress} +@item --compress +@itemx --uncompress +@itemx -Z + +@command{tar} will use the @command{compress} program when reading or +writing the archive. This allows you to directly act on archives +while saving space. @xref{gzip}. + +@opsummary{confirmation} +@item --confirmation + +(See @option{--interactive}.) @xref{interactive}. + +@opsummary{delay-directory-restore} +@item --delay-directory-restore + +Delay setting modification times and permissions of extracted +directories until the end of extraction. @xref{Directory Modification Times and Permissions}. + +@opsummary{dereference} +@item --dereference +@itemx -h + +When reading or writing a file to be archived, @command{tar} accesses +the file that a symbolic link points to, rather than the symlink +itself. @xref{dereference}. + +@opsummary{directory} +@item --directory=@var{dir} +@itemx -C @var{dir} + +When this option is specified, @command{tar} will change its current directory +to @var{dir} before performing any operations. When this option is used +during archive creation, it is order sensitive. @xref{directory}. + +@opsummary{exclude} +@item --exclude=@var{pattern} + +When performing operations, @command{tar} will skip files that match +@var{pattern}. @xref{exclude}. + +@opsummary{exclude-backups} +@item --exclude-backups +Exclude backup and lock files. @xref{exclude,, exclude-backups}. + +@opsummary{exclude-from} +@item --exclude-from=@var{file} +@itemx -X @var{file} + +Similar to @option{--exclude}, except @command{tar} will use the list of +patterns in the file @var{file}. @xref{exclude}. + +@opsummary{exclude-caches} +@item --exclude-caches + +Exclude from dump any directory containing a valid cache directory +tag file, but still dump the directory node and the tag file itself. + +@xref{exclude,, exclude-caches}. + +@opsummary{exclude-caches-under} +@item --exclude-caches-under + +Exclude from dump any directory containing a valid cache directory +tag file, but still dump the directory node itself. + +@xref{exclude}. + +@opsummary{exclude-caches-all} +@item --exclude-caches-all + +Exclude from dump any directory containing a valid cache directory +tag file. @xref{exclude}. + +@opsummary{exclude-tag} +@item --exclude-tag=@var{file} + +Exclude from dump any directory containing file named @var{file}, but +dump the directory node and @var{file} itself. @xref{exclude,, exclude-tag}. + +@opsummary{exclude-tag-under} +@item --exclude-tag-under=@var{file} + +Exclude from dump the contents of any directory containing file +named @var{file}, but dump the directory node itself. @xref{exclude,, +exclude-tag-under}. + +@opsummary{exclude-tag-all} +@item --exclude-tag-all=@var{file} + +Exclude from dump any directory containing file named @var{file}. +@xref{exclude,,exclude-tag-all}. + +@opsummary{exclude-vcs} +@item --exclude-vcs + +Exclude from dump directories and files, that are internal for some +widely used version control systems. + +@xref{exclude,,exclude-vcs}. + +@opsummary{file} +@item --file=@var{archive} +@itemx -f @var{archive} + +@command{tar} will use the file @var{archive} as the @command{tar} archive it +performs operations on, rather than @command{tar}'s compilation dependent +default. @xref{file tutorial}. + +@opsummary{files-from} +@item --files-from=@var{file} +@itemx -T @var{file} + +@command{tar} will use the contents of @var{file} as a list of archive members +or files to operate on, in addition to those specified on the +command-line. @xref{files}. + +@opsummary{force-local} +@item --force-local + +Forces @command{tar} to interpret the file name given to @option{--file} +as a local file, even if it looks like a remote tape drive name. +@xref{local and remote archives}. + +@opsummary{format} +@item --format=@var{format} +@itemx -H @var{format} + +Selects output archive format. @var{Format} may be one of the +following: + +@table @samp +@item v7 +Creates an archive that is compatible with Unix V7 @command{tar}. + +@item oldgnu +Creates an archive that is compatible with GNU @command{tar} version +1.12 or earlier. + +@item gnu +Creates archive in GNU tar 1.13 format. Basically it is the same as +@samp{oldgnu} with the only difference in the way it handles long +numeric fields. + +@item ustar +Creates a @acronym{POSIX.1-1988} compatible archive. + +@item posix +Creates a @acronym{POSIX.1-2001 archive}. + +@end table + +@xref{Formats}, for a detailed discussion of these formats. + +@opsummary{full-time} +@item --full-time +This option instructs @command{tar} to print file times to their full +resolution. Usually this means 1-second resolution, but that depends +on the underlying file system. The @option{--full-time} option takes +effect only when detailed output (verbosity level 2 or higher) has +been requested using the @option{--verbose} option, e.g., when listing +or extracting archives: + +@smallexample +$ @kbd{tar -t -v --full-time -f archive.tar} +@end smallexample + +@noindent +or, when creating an archive: + +@smallexample +$ @kbd{tar -c -vv --full-time -f archive.tar .} +@end smallexample + +Notice, thar when creating the archive you need to specify +@option{--verbose} twice to get a detailed output (@pxref{verbose +tutorial}). + +@opsummary{group} +@item --group=@var{group} + +Files added to the @command{tar} archive will have a group @acronym{ID} of @var{group}, +rather than the group from the source file. @var{group} is first decoded +as a group symbolic name, but if this interpretation fails, it has to be +a decimal numeric group @acronym{ID}. @xref{override}. + +Also see the comments for the @option{--owner=@var{user}} option. + +@opsummary{gzip} +@opsummary{gunzip} +@opsummary{ungzip} +@item --gzip +@itemx --gunzip +@itemx --ungzip +@itemx -z + +This option tells @command{tar} to read or write archives through +@command{gzip}, allowing @command{tar} to directly operate on several +kinds of compressed archives transparently. @xref{gzip}. + +@opsummary{hard-dereference} +@item --hard-dereference +When creating an archive, dereference hard links and store the files +they refer to, instead of creating usual hard link members. + +@xref{hard links}. + +@opsummary{help} +@item --help +@itemx -? + +@command{tar} will print out a short message summarizing the operations and +options to @command{tar} and exit. @xref{help}. + +@opsummary{ignore-case} +@item --ignore-case +Ignore case when matching member or file names with +patterns. @xref{controlling pattern-matching}. + +@opsummary{ignore-command-error} +@item --ignore-command-error +Ignore exit codes of subprocesses. @xref{Writing to an External Program}. + +@opsummary{ignore-failed-read} +@item --ignore-failed-read + +Do not exit unsuccessfully merely because an unreadable file was encountered. +@xref{Reading}. + +@opsummary{ignore-zeros} +@item --ignore-zeros +@itemx -i + +With this option, @command{tar} will ignore zeroed blocks in the +archive, which normally signals EOF. @xref{Reading}. + +@opsummary{incremental} +@item --incremental +@itemx -G + +Informs @command{tar} that it is working with an old +@acronym{GNU}-format incremental backup archive. It is intended +primarily for backwards compatibility only. @xref{Incremental Dumps}, +for a detailed discussion of incremental archives. + +@opsummary{index-file} +@item --index-file=@var{file} + +Send verbose output to @var{file} instead of to standard output. + +@opsummary{info-script} +@opsummary{new-volume-script} +@item --info-script=@var{script-file} +@itemx --new-volume-script=@var{script-file} +@itemx -F @var{script-file} + +When @command{tar} is performing multi-tape backups, @var{script-file} is run +at the end of each tape. If @var{script-file} exits with nonzero status, +@command{tar} fails immediately. @xref{info-script}, for a detailed +discussion of @var{script-file}. + +@opsummary{interactive} +@item --interactive +@itemx --confirmation +@itemx -w + +Specifies that @command{tar} should ask the user for confirmation before +performing potentially destructive options, such as overwriting files. +@xref{interactive}. + +@opsummary{keep-newer-files} +@item --keep-newer-files + +Do not replace existing files that are newer than their archive copies +when extracting files from an archive. + +@opsummary{keep-old-files} +@item --keep-old-files +@itemx -k + +Do not overwrite existing files when extracting files from an archive. +@xref{Keep Old Files}. + +@opsummary{label} +@item --label=@var{name} +@itemx -V @var{name} + +When creating an archive, instructs @command{tar} to write @var{name} +as a name record in the archive. When extracting or listing archives, +@command{tar} will only operate on archives that have a label matching +the pattern specified in @var{name}. @xref{Tape Files}. + +@opsummary{level} +@item --level=@var{n} +Force incremental backup of level @var{n}. As of @GNUTAR version +@value{VERSION}, the option @option{--level=0} truncates the snapshot +file, thereby forcing the level 0 dump. Other values of @var{n} are +effectively ignored. @xref{--level=0}, for details and examples. + +The use of this option is valid only in conjunction with the +@option{--listed-incremental} option. @xref{Incremental Dumps}, +for a detailed description. + +@opsummary{listed-incremental} +@item --listed-incremental=@var{snapshot-file} +@itemx -g @var{snapshot-file} + +During a @option{--create} operation, specifies that the archive that +@command{tar} creates is a new @acronym{GNU}-format incremental +backup, using @var{snapshot-file} to determine which files to backup. +With other operations, informs @command{tar} that the archive is in +incremental format. @xref{Incremental Dumps}. + +@opsummary{lzip} +@item --lzip + +This option tells @command{tar} to read or write archives through +@command{lzip}. @xref{gzip}. + +@opsummary{lzma} +@item --lzma + +This option tells @command{tar} to read or write archives through +@command{lzma}. @xref{gzip}. + +@item --lzop + +This option tells @command{tar} to read or write archives through +@command{lzop}. @xref{gzip}. + +@opsummary{mode} +@item --mode=@var{permissions} + +When adding files to an archive, @command{tar} will use +@var{permissions} for the archive members, rather than the permissions +from the files. @var{permissions} can be specified either as an octal +number or as symbolic permissions, like with +@command{chmod}. @xref{override}. + +@opsummary{mtime} +@item --mtime=@var{date} + +When adding files to an archive, @command{tar} will use @var{date} as +the modification time of members when creating archives, instead of +their actual modification times. The value of @var{date} can be +either a textual date representation (@pxref{Date input formats}) or a +name of the existing file, starting with @samp{/} or @samp{.}. In the +latter case, the modification time of that file is used. @xref{override}. + +@opsummary{multi-volume} +@item --multi-volume +@itemx -M + +Informs @command{tar} that it should create or otherwise operate on a +multi-volume @command{tar} archive. @xref{Using Multiple Tapes}. + +@opsummary{new-volume-script} +@item --new-volume-script + +(see @option{--info-script}) + +@opsummary{newer} +@item --newer=@var{date} +@itemx --after-date=@var{date} +@itemx -N + +When creating an archive, @command{tar} will only add files that have changed +since @var{date}. If @var{date} begins with @samp{/} or @samp{.}, it +is taken to be the name of a file whose data modification time specifies +the date. @xref{after}. + +@opsummary{newer-mtime} +@item --newer-mtime=@var{date} + +Like @option{--newer}, but add only files whose +contents have changed (as opposed to just @option{--newer}, which will +also back up files for which any status information has +changed). @xref{after}. + +@opsummary{no-anchored} +@item --no-anchored +An exclude pattern can match any subsequence of the name's components. +@xref{controlling pattern-matching}. + +@opsummary{no-auto-compress} +@item --no-auto-compress + +Disables automatic compressed format recognition based on the archive +suffix. @xref{--auto-compress}. @xref{gzip}. + +@opsummary{no-check-device} +@item --no-check-device +Do not check device numbers when creating a list of modified files +for incremental archiving. @xref{device numbers}, for +a detailed description. + +@opsummary{no-delay-directory-restore} +@item --no-delay-directory-restore + +Modification times and permissions of extracted +directories are set when all files from this directory have been +extracted. This is the default. +@xref{Directory Modification Times and Permissions}. + +@opsummary{no-ignore-case} +@item --no-ignore-case +Use case-sensitive matching. +@xref{controlling pattern-matching}. + +@opsummary{no-ignore-command-error} +@item --no-ignore-command-error +Print warnings about subprocesses that terminated with a nonzero exit +code. @xref{Writing to an External Program}. + +@opsummary{no-null} +@item --no-null + +If the @option{--null} option was given previously, this option +cancels its effect, so that any following @option{--files-from} +options will expect their file lists to be newline-terminated. + +@opsummary{no-overwrite-dir} +@item --no-overwrite-dir + +Preserve metadata of existing directories when extracting files +from an archive. @xref{Overwrite Old Files}. + +@opsummary{no-quote-chars} +@item --no-quote-chars=@var{string} +Remove characters listed in @var{string} from the list of quoted +characters set by the previous @option{--quote-chars} option +(@pxref{quoting styles}). + +@opsummary{no-recursion} +@item --no-recursion + +With this option, @command{tar} will not recurse into directories. +@xref{recurse}. + +@opsummary{no-same-owner} +@item --no-same-owner +@itemx -o + +When extracting an archive, do not attempt to preserve the owner +specified in the @command{tar} archive. This the default behavior +for ordinary users. + +@opsummary{no-same-permissions} +@item --no-same-permissions + +When extracting an archive, subtract the user's umask from files from +the permissions specified in the archive. This is the default behavior +for ordinary users. + +@opsummary{no-seek} +@item --no-seek + +The archive media does not support seeks to arbitrary +locations. Usually @command{tar} determines automatically whether +the archive can be seeked or not. Use this option to disable this +mechanism. + +@opsummary{no-unquote} +@item --no-unquote +Treat all input file or member names literally, do not interpret +escape sequences. @xref{input name quoting}. + +@opsummary{no-wildcards} +@item --no-wildcards +Do not use wildcards. +@xref{controlling pattern-matching}. + +@opsummary{no-wildcards-match-slash} +@item --no-wildcards-match-slash +Wildcards do not match @samp{/}. +@xref{controlling pattern-matching}. + +@opsummary{null} +@item --null + +When @command{tar} is using the @option{--files-from} option, this option +instructs @command{tar} to expect file names terminated with @acronym{NUL}, so +@command{tar} can correctly work with file names that contain newlines. +@xref{nul}. + +@opsummary{numeric-owner} +@item --numeric-owner + +This option will notify @command{tar} that it should use numeric user +and group IDs when creating a @command{tar} file, rather than names. +@xref{Attributes}. + +@item -o +The function of this option depends on the action @command{tar} is +performing. When extracting files, @option{-o} is a synonym for +@option{--no-same-owner}, i.e., it prevents @command{tar} from +restoring ownership of files being extracted. + +When creating an archive, it is a synonym for +@option{--old-archive}. This behavior is for compatibility +with previous versions of @GNUTAR{}, and will be +removed in future releases. + +@xref{Changes}, for more information. + +@opsummary{occurrence} +@item --occurrence[=@var{number}] + +This option can be used in conjunction with one of the subcommands +@option{--delete}, @option{--diff}, @option{--extract} or +@option{--list} when a list of files is given either on the command +line or via @option{-T} option. + +This option instructs @command{tar} to process only the @var{number}th +occurrence of each named file. @var{Number} defaults to 1, so + +@smallexample +tar -x -f archive.tar --occurrence filename +@end smallexample + +@noindent +will extract the first occurrence of the member @file{filename} from @file{archive.tar} +and will terminate without scanning to the end of the archive. + +@opsummary{old-archive} +@item --old-archive +Synonym for @option{--format=v7}. + +@opsummary{one-file-system} +@item --one-file-system +Used when creating an archive. Prevents @command{tar} from recursing into +directories that are on different file systems from the current +directory. + +@opsummary{overwrite} +@item --overwrite + +Overwrite existing files and directory metadata when extracting files +from an archive. @xref{Overwrite Old Files}. + +@opsummary{overwrite-dir} +@item --overwrite-dir + +Overwrite the metadata of existing directories when extracting files +from an archive. @xref{Overwrite Old Files}. + +@opsummary{owner} +@item --owner=@var{user} + +Specifies that @command{tar} should use @var{user} as the owner of members +when creating archives, instead of the user associated with the source +file. @var{user} is first decoded as a user symbolic name, but if +this interpretation fails, it has to be a decimal numeric user @acronym{ID}. +@xref{override}. + +This option does not affect extraction from archives. + +@opsummary{pax-option} +@item --pax-option=@var{keyword-list} +This option enables creation of the archive in @acronym{POSIX.1-2001} +format (@pxref{posix}) and modifies the way @command{tar} handles the +extended header keywords. @var{Keyword-list} is a comma-separated +list of keyword options. @xref{PAX keywords}, for a detailed +discussion. + +@opsummary{portability} +@item --portability +@itemx --old-archive +Synonym for @option{--format=v7}. + +@opsummary{posix} +@item --posix +Same as @option{--format=posix}. + +@opsummary{preserve} +@item --preserve + +Synonymous with specifying both @option{--preserve-permissions} and +@option{--same-order}. @xref{Setting Access Permissions}. + +@opsummary{preserve-order} +@item --preserve-order + +(See @option{--same-order}; @pxref{Reading}.) + +@opsummary{preserve-permissions} +@opsummary{same-permissions} +@item --preserve-permissions +@itemx --same-permissions +@itemx -p + +When @command{tar} is extracting an archive, it normally subtracts the +users' umask from the permissions specified in the archive and uses +that number as the permissions to create the destination file. +Specifying this option instructs @command{tar} that it should use the +permissions directly from the archive. @xref{Setting Access Permissions}. + +@opsummary{quote-chars} +@item --quote-chars=@var{string} +Always quote characters from @var{string}, even if the selected +quoting style would not quote them (@pxref{quoting styles}). + +@opsummary{quoting-style} +@item --quoting-style=@var{style} +Set quoting style to use when printing member and file names +(@pxref{quoting styles}). Valid @var{style} values are: +@code{literal}, @code{shell}, @code{shell-always}, @code{c}, +@code{escape}, @code{locale}, and @code{clocale}. Default quoting +style is @code{escape}, unless overridden while configuring the +package. + +@opsummary{read-full-records} +@item --read-full-records +@itemx -B + +Specifies that @command{tar} should reblock its input, for reading +from pipes on systems with buggy implementations. @xref{Reading}. + +@opsummary{record-size} +@item --record-size=@var{size}[@var{suf}] + +Instructs @command{tar} to use @var{size} bytes per record when accessing the +archive. The argument can be suffixed with a @dfn{size suffix}, e.g. +@option{--record-size=10K} for 10 Kilobytes. @xref{size-suffixes}, +for a list of valid suffixes. @xref{Blocking Factor}, for a detailed +description of this option. + +@opsummary{recursion} +@item --recursion + +With this option, @command{tar} recurses into directories (default). +@xref{recurse}. + +@opsummary{recursive-unlink} +@item --recursive-unlink + +Remove existing +directory hierarchies before extracting directories of the same name +from the archive. @xref{Recursive Unlink}. + +@opsummary{remove-files} +@item --remove-files + +Directs @command{tar} to remove the source file from the file system after +appending it to an archive. @xref{remove files}. + +@opsummary{restrict} +@item --restrict + +Disable use of some potentially harmful @command{tar} options. +Currently this option disables shell invocation from multi-volume menu +(@pxref{Using Multiple Tapes}). + +@opsummary{rmt-command} +@item --rmt-command=@var{cmd} + +Notifies @command{tar} that it should use @var{cmd} instead of +the default @file{/usr/libexec/rmt} (@pxref{Remote Tape Server}). + +@opsummary{rsh-command} +@item --rsh-command=@var{cmd} + +Notifies @command{tar} that is should use @var{cmd} to communicate with remote +devices. @xref{Device}. + +@opsummary{same-order} +@item --same-order +@itemx --preserve-order +@itemx -s + +This option is an optimization for @command{tar} when running on machines with +small amounts of memory. It informs @command{tar} that the list of file +arguments has already been sorted to match the order of files in the +archive. @xref{Reading}. + +@opsummary{same-owner} +@item --same-owner + +When extracting an archive, @command{tar} will attempt to preserve the owner +specified in the @command{tar} archive with this option present. +This is the default behavior for the superuser; this option has an +effect only for ordinary users. @xref{Attributes}. + +@opsummary{same-permissions} +@item --same-permissions + +(See @option{--preserve-permissions}; @pxref{Setting Access Permissions}.) + +@opsummary{seek} +@item --seek +@itemx -n + +Assume that the archive media supports seeks to arbitrary +locations. Usually @command{tar} determines automatically whether +the archive can be seeked or not. This option is intended for use +in cases when such recognition fails. It takes effect only if the +archive is open for reading (e.g. with @option{--list} or +@option{--extract} options). + +@opsummary{show-defaults} +@item --show-defaults + +Displays the default options used by @command{tar} and exits +successfully. This option is intended for use in shell scripts. +Here is an example of what you can see using this option: + +@smallexample +$ @kbd{tar --show-defaults} +--format=gnu -f- -b20 --quoting-style=escape +--rmt-command=/usr/libexec/rmt --rsh-command=/usr/bin/rsh +@end smallexample + +@noindent +Notice, that this option outputs only one line. The example output +above has been split to fit page boundaries. + +@opsummary{show-omitted-dirs} +@item --show-omitted-dirs + +Instructs @command{tar} to mention the directories it is skipping when +operating on a @command{tar} archive. @xref{show-omitted-dirs}. + +@opsummary{show-transformed-names} +@opsummary{show-stored-names} +@item --show-transformed-names +@itemx --show-stored-names + +Display file or member names after applying any transformations +(@pxref{transform}). In particular, when used in conjunction with one of +the archive creation operations it instructs @command{tar} to list the +member names stored in the archive, as opposed to the actual file +names. @xref{listing member and file names}. + +@opsummary{sparse} +@item --sparse +@itemx -S + +Invokes a @acronym{GNU} extension when adding files to an archive that handles +sparse files efficiently. @xref{sparse}. + +@opsummary{sparse-version} +@item --sparse-version=@var{version} + +Specifies the @dfn{format version} to use when archiving sparse +files. Implies @option{--sparse}. @xref{sparse}. For the description +of the supported sparse formats, @xref{Sparse Formats}. + +@opsummary{starting-file} +@item --starting-file=@var{name} +@itemx -K @var{name} + +This option affects extraction only; @command{tar} will skip extracting +files in the archive until it finds one that matches @var{name}. +@xref{Scarce}. + +@opsummary{strip-components} +@item --strip-components=@var{number} +Strip given @var{number} of leading components from file names before +extraction. For example, if archive @file{archive.tar} contained +@file{/some/file/name}, then running + +@smallexample +tar --extract --file archive.tar --strip-components=2 +@end smallexample + +@noindent +would extract this file to file @file{name}. + +@opsummary{suffix} +@item --suffix=@var{suffix} + +Alters the suffix @command{tar} uses when backing up files from the default +@samp{~}. @xref{backup}. + +@opsummary{tape-length} +@item --tape-length=@var{num}[@var{suf}] +@itemx -L @var{num}[@var{suf}] + +Specifies the length of tapes that @command{tar} is writing as being +@w{@var{num} x 1024} bytes long. If optional @var{suf} is given, it +specifies a multiplicative factor to be used instead of 1024. For +example, @samp{-L2M} means 2 megabytes. @xref{size-suffixes}, for a +list of allowed suffixes. @xref{Using Multiple Tapes}, for a detailed +discussion of this option. + +@opsummary{test-label} +@item --test-label + +Reads the volume label. If an argument is specified, test whether it +matches the volume label. @xref{--test-label option}. + +@opsummary{to-command} +@item --to-command=@var{command} + +During extraction @command{tar} will pipe extracted files to the +standard input of @var{command}. @xref{Writing to an External Program}. + +@opsummary{to-stdout} +@item --to-stdout +@itemx -O + +During extraction, @command{tar} will extract files to stdout rather +than to the file system. @xref{Writing to Standard Output}. + +@opsummary{totals} +@item --totals[=@var{signo}] + +Displays the total number of bytes transferred when processing an +archive. If an argument is given, these data are displayed on +request, when signal @var{signo} is delivered to @command{tar}. +@xref{totals}. + +@opsummary{touch} +@item --touch +@itemx -m + +Sets the data modification time of extracted files to the extraction time, +rather than the data modification time stored in the archive. +@xref{Data Modification Times}. + +@opsummary{transform} +@opsummary{xform} +@item --transform=@var{sed-expr} +@itemx --xform=@var{sed-expr} +Transform file or member names using @command{sed} replacement expression +@var{sed-expr}. For example, + +@smallexample +$ @kbd{tar cf archive.tar --transform 's,^\./,usr/,' .} +@end smallexample + +@noindent +will add to @file{archive} files from the current working directory, +replacing initial @samp{./} prefix with @samp{usr/}. For the detailed +discussion, @xref{transform}. + +To see transformed member names in verbose listings, use +@option{--show-transformed-names} option +(@pxref{show-transformed-names}). + +@opsummary{uncompress} +@item --uncompress + +(See @option{--compress}, @pxref{gzip}) + +@opsummary{ungzip} +@item --ungzip + +(See @option{--gzip}, @pxref{gzip}) + +@opsummary{unlink-first} +@item --unlink-first +@itemx -U + +Directs @command{tar} to remove the corresponding file from the file +system before extracting it from the archive. @xref{Unlink First}. + +@opsummary{unquote} +@item --unquote +Enable unquoting input file or member names (default). @xref{input +name quoting}. + +@opsummary{use-compress-program} +@item --use-compress-program=@var{prog} +@itemx -I=@var{prog} + +Instructs @command{tar} to access the archive through @var{prog}, which is +presumed to be a compression program of some sort. @xref{gzip}. + +@opsummary{utc} +@item --utc + +Display file modification dates in @acronym{UTC}. This option implies +@option{--verbose}. + +@opsummary{verbose} +@item --verbose +@itemx -v + +Specifies that @command{tar} should be more verbose about the +operations it is performing. This option can be specified multiple +times for some operations to increase the amount of information displayed. +@xref{verbose}. + +@opsummary{verify} +@item --verify +@itemx -W + +Verifies that the archive was correctly written when creating an +archive. @xref{verify}. + +@opsummary{version} +@item --version + +Print information about the program's name, version, origin and legal +status, all on standard output, and then exit successfully. +@xref{help}. + +@opsummary{volno-file} +@item --volno-file=@var{file} + +Used in conjunction with @option{--multi-volume}. @command{tar} will +keep track of which volume of a multi-volume archive it is working in +@var{file}. @xref{volno-file}. + +@opsummary{warning} +@item --warning=@var{keyword} + +Enable or disable warning messages identified by @var{keyword}. The +messages are suppressed if @var{keyword} is prefixed with @samp{no-}. +@xref{warnings}. + +@opsummary{wildcards} +@item --wildcards +Use wildcards when matching member names with patterns. +@xref{controlling pattern-matching}. + +@opsummary{wildcards-match-slash} +@item --wildcards-match-slash +Wildcards match @samp{/}. +@xref{controlling pattern-matching}. + +@opsummary{xz} +@item --xz +@itemx -J +Use @command{xz} for compressing or decompressing the archives. @xref{gzip}. + +@end table + +@node Short Option Summary +@subsection Short Options Cross Reference + +Here is an alphabetized list of all of the short option forms, matching +them with the equivalent long option. + +@multitable @columnfractions 0.20 0.80 +@headitem Short Option @tab Reference + +@item -A @tab @ref{--concatenate}. + +@item -B @tab @ref{--read-full-records}. + +@item -C @tab @ref{--directory}. + +@item -F @tab @ref{--info-script}. + +@item -G @tab @ref{--incremental}. + +@item -J @tab @ref{--xz}. + +@item -K @tab @ref{--starting-file}. + +@item -L @tab @ref{--tape-length}. + +@item -M @tab @ref{--multi-volume}. + +@item -N @tab @ref{--newer}. + +@item -O @tab @ref{--to-stdout}. + +@item -P @tab @ref{--absolute-names}. + +@item -R @tab @ref{--block-number}. + +@item -S @tab @ref{--sparse}. + +@item -T @tab @ref{--files-from}. + +@item -U @tab @ref{--unlink-first}. + +@item -V @tab @ref{--label}. + +@item -W @tab @ref{--verify}. + +@item -X @tab @ref{--exclude-from}. + +@item -Z @tab @ref{--compress}. + +@item -b @tab @ref{--blocking-factor}. + +@item -c @tab @ref{--create}. + +@item -d @tab @ref{--compare}. + +@item -f @tab @ref{--file}. + +@item -g @tab @ref{--listed-incremental}. + +@item -h @tab @ref{--dereference}. + +@item -i @tab @ref{--ignore-zeros}. + +@item -j @tab @ref{--bzip2}. + +@item -k @tab @ref{--keep-old-files}. + +@item -l @tab @ref{--check-links}. + +@item -m @tab @ref{--touch}. + +@item -o @tab When creating, @ref{--no-same-owner}, when extracting --- +@ref{--portability}. + +The latter usage is deprecated. It is retained for compatibility with +the earlier versions of @GNUTAR{}. In future releases +@option{-o} will be equivalent to @option{--no-same-owner} only. + +@item -p @tab @ref{--preserve-permissions}. + +@item -r @tab @ref{--append}. + +@item -s @tab @ref{--same-order}. + +@item -t @tab @ref{--list}. + +@item -u @tab @ref{--update}. + +@item -v @tab @ref{--verbose}. + +@item -w @tab @ref{--interactive}. + +@item -x @tab @ref{--extract}. + +@item -z @tab @ref{--gzip}. + +@end multitable + +@node help +@section @GNUTAR{} documentation + +@cindex Getting program version number +@opindex version +@cindex Version of the @command{tar} program +Being careful, the first thing is really checking that you are using +@GNUTAR{}, indeed. The @option{--version} option +causes @command{tar} to print information about its name, version, +origin and legal status, all on standard output, and then exit +successfully. For example, @w{@samp{tar --version}} might print: + +@smallexample +tar (GNU tar) @value{VERSION} +Copyright (C) 2010 Free Software Foundation, Inc. +Copyright (C) 2010 Free Software Foundation, Inc. +License GPLv3+: GNU GPL version 3 or later . +This is free software: you are free to change and redistribute it. +There is NO WARRANTY, to the extent permitted by law. + +Written by John Gilmore and Jay Fenlason. +@end smallexample + +@noindent +The first occurrence of @samp{tar} in the result above is the program +name in the package (for example, @command{rmt} is another program), +while the second occurrence of @samp{tar} is the name of the package +itself, containing possibly many programs. The package is currently +named @samp{tar}, after the name of the main program it +contains@footnote{There are plans to merge the @command{cpio} and +@command{tar} packages into a single one which would be called +@code{paxutils}. So, who knows if, one of this days, the +@option{--version} would not output @w{@samp{tar (@acronym{GNU} +paxutils) 3.2}}.}. + +@cindex Obtaining help +@cindex Listing all @command{tar} options +@xopindex{help, introduction} +Another thing you might want to do is checking the spelling or meaning +of some particular @command{tar} option, without resorting to this +manual, for once you have carefully read it. @GNUTAR{} +has a short help feature, triggerable through the +@option{--help} option. By using this option, @command{tar} will +print a usage message listing all available options on standard +output, then exit successfully, without doing anything else and +ignoring all other options. Even if this is only a brief summary, it +may be several screens long. So, if you are not using some kind of +scrollable window, you might prefer to use something like: + +@smallexample +$ @kbd{tar --help | less} +@end smallexample + +@noindent +presuming, here, that you like using @command{less} for a pager. Other +popular pagers are @command{more} and @command{pg}. If you know about some +@var{keyword} which interests you and do not want to read all the +@option{--help} output, another common idiom is doing: + +@smallexample +tar --help | grep @var{keyword} +@end smallexample + +@noindent +for getting only the pertinent lines. Notice, however, that some +@command{tar} options have long description lines and the above +command will list only the first of them. + +The exact look of the option summary displayed by @kbd{tar --help} is +configurable. @xref{Configuring Help Summary}, for a detailed description. + +@opindex usage +If you only wish to check the spelling of an option, running @kbd{tar +--usage} may be a better choice. This will display a terse list of +@command{tar} options without accompanying explanations. + +The short help output is quite succinct, and you might have to get +back to the full documentation for precise points. If you are reading +this paragraph, you already have the @command{tar} manual in some +form. This manual is available in a variety of forms from +@url{http://www.gnu.org/software/tar/manual}. It may be printed out of the @GNUTAR{} +distribution, provided you have @TeX{} already installed somewhere, +and a laser printer around. Just configure the distribution, execute +the command @w{@samp{make dvi}}, then print @file{doc/tar.dvi} the +usual way (contact your local guru to know how). If @GNUTAR{} +has been conveniently installed at your place, this +manual is also available in interactive, hypertextual form as an Info +file. Just call @w{@samp{info tar}} or, if you do not have the +@command{info} program handy, use the Info reader provided within +@acronym{GNU} Emacs, calling @samp{tar} from the main Info menu. + +There is currently no @code{man} page for @GNUTAR{}. +If you observe such a @code{man} page on the system you are running, +either it does not belong to @GNUTAR{}, or it has not +been produced by @acronym{GNU}. Some package maintainers convert +@kbd{tar --help} output to a man page, using @command{help2man}. In +any case, please bear in mind that the authoritative source of +information about @GNUTAR{} is this Texinfo documentation. + +@node defaults +@section Obtaining @GNUTAR{} default values + +@opindex show-defaults +@GNUTAR{} has some predefined defaults that are used when you do not +explicitly specify another values. To obtain a list of such +defaults, use @option{--show-defaults} option. This will output the +values in the form of @command{tar} command line options: + +@smallexample +@group +$ @kbd{tar --show-defaults} +--format=gnu -f- -b20 --quoting-style=escape +--rmt-command=/etc/rmt --rsh-command=/usr/bin/rsh +@end group +@end smallexample + +@noindent +Notice, that this option outputs only one line. The example output above +has been split to fit page boundaries. + +@noindent +The above output shows that this version of @GNUTAR{} defaults to +using @samp{gnu} archive format (@pxref{Formats}), it uses standard +output as the archive, if no @option{--file} option has been given +(@pxref{file tutorial}), the default blocking factor is 20 +(@pxref{Blocking Factor}). It also shows the default locations where +@command{tar} will look for @command{rmt} and @command{rsh} binaries. + +@node verbose +@section Checking @command{tar} progress + +Typically, @command{tar} performs most operations without reporting any +information to the user except error messages. When using @command{tar} +with many options, particularly ones with complicated or +difficult-to-predict behavior, it is possible to make serious mistakes. +@command{tar} provides several options that make observing @command{tar} +easier. These options cause @command{tar} to print information as it +progresses in its job, and you might want to use them just for being +more careful about what is going on, or merely for entertaining +yourself. If you have encountered a problem when operating on an +archive, however, you may need more information than just an error +message in order to solve the problem. The following options can be +helpful diagnostic tools. + +@cindex Verbose operation +@opindex verbose +Normally, the @option{--list} (@option{-t}) command to list an archive +prints just the file names (one per line) and the other commands are +silent. When used with most operations, the @option{--verbose} +(@option{-v}) option causes @command{tar} to print the name of each +file or archive member as it is processed. This and the other options +which make @command{tar} print status information can be useful in +monitoring @command{tar}. + +With @option{--create} or @option{--extract}, @option{--verbose} used +once just prints the names of the files or members as they are processed. +Using it twice causes @command{tar} to print a longer listing +(@xref{verbose member listing}, for the description) for each member. +Since @option{--list} already prints the names of the members, +@option{--verbose} used once with @option{--list} causes @command{tar} +to print an @samp{ls -l} type listing of the files in the archive. +The following examples both extract members with long list output: + +@smallexample +$ @kbd{tar --extract --file=archive.tar --verbose --verbose} +$ @kbd{tar xvvf archive.tar} +@end smallexample + +Verbose output appears on the standard output except when an archive is +being written to the standard output, as with @samp{tar --create +--file=- --verbose} (@samp{tar cfv -}, or even @samp{tar cv}---if the +installer let standard output be the default archive). In that case +@command{tar} writes verbose output to the standard error stream. + +If @option{--index-file=@var{file}} is specified, @command{tar} sends +verbose output to @var{file} rather than to standard output or standard +error. + +@anchor{totals} +@cindex Obtaining total status information +@opindex totals +The @option{--totals} option causes @command{tar} to print on the +standard error the total amount of bytes transferred when processing +an archive. When creating or appending to an archive, this option +prints the number of bytes written to the archive and the average +speed at which they have been written, e.g.: + +@smallexample +@group +$ @kbd{tar -c -f archive.tar --totals /home} +Total bytes written: 7924664320 (7.4GiB, 85MiB/s) +@end group +@end smallexample + +When reading an archive, this option displays the number of bytes +read: + +@smallexample +@group +$ @kbd{tar -x -f archive.tar --totals} +Total bytes read: 7924664320 (7.4GiB, 95MiB/s) +@end group +@end smallexample + +Finally, when deleting from an archive, the @option{--totals} option +displays both numbers plus number of bytes removed from the archive: + +@smallexample +@group +$ @kbd{tar --delete -f foo.tar --totals --wildcards '*~'} +Total bytes read: 9543680 (9.2MiB, 201MiB/s) +Total bytes written: 3829760 (3.7MiB, 81MiB/s) +Total bytes deleted: 1474048 +@end group +@end smallexample + +You can also obtain this information on request. When +@option{--totals} is used with an argument, this argument is +interpreted as a symbolic name of a signal, upon delivery of which the +statistics is to be printed: + +@table @option +@item --totals=@var{signo} +Print statistics upon delivery of signal @var{signo}. Valid arguments +are: @code{SIGHUP}, @code{SIGQUIT}, @code{SIGINT}, @code{SIGUSR1} and +@code{SIGUSR2}. Shortened names without @samp{SIG} prefix are also +accepted. +@end table + +Both forms of @option{--totals} option can be used simultaneously. +Thus, @kbd{tar -x --totals --totals=USR1} instructs @command{tar} to +extract all members from its default archive and print statistics +after finishing the extraction, as well as when receiving signal +@code{SIGUSR1}. + +@anchor{Progress information} +@cindex Progress information +The @option{--checkpoint} option prints an occasional message +as @command{tar} reads or writes the archive. It is designed for +those who don't need the more detailed (and voluminous) output of +@option{--block-number} (@option{-R}), but do want visual confirmation +that @command{tar} is actually making forward progress. By default it +prints a message each 10 records read or written. This can be changed +by giving it a numeric argument after an equal sign: + +@smallexample +$ @kbd{tar -c --checkpoint=1000} /var +tar: Write checkpoint 1000 +tar: Write checkpoint 2000 +tar: Write checkpoint 3000 +@end smallexample + +This example shows the default checkpoint message used by +@command{tar}. If you place a dot immediately after the equal +sign, it will print a @samp{.} at each checkpoint@footnote{This is +actually a shortcut for @option{--checkpoint=@var{n} +--checkpoint-action=dot}. @xref{checkpoints, dot}.}. For example: + +@smallexample +$ @kbd{tar -c --checkpoint=.1000} /var +... +@end smallexample + +The @option{--checkpoint} option provides a flexible mechanism for +executing arbitrary actions upon hitting checkpoints, see the next +section (@pxref{checkpoints}), for more information on it. + +@opindex show-omitted-dirs +@anchor{show-omitted-dirs} +The @option{--show-omitted-dirs} option, when reading an archive---with +@option{--list} or @option{--extract}, for example---causes a message +to be printed for each directory in the archive which is skipped. +This happens regardless of the reason for skipping: the directory might +not have been named on the command line (implicitly or explicitly), +it might be excluded by the use of the +@option{--exclude=@var{pattern}} option, or some other reason. + +@opindex block-number +@cindex Block number where error occurred +@anchor{block-number} +If @option{--block-number} (@option{-R}) is used, @command{tar} prints, along with +every message it would normally produce, the block number within the +archive where the message was triggered. Also, supplementary messages +are triggered when reading blocks full of NULs, or when hitting end of +file on the archive. As of now, if the archive is properly terminated +with a NUL block, the reading of the file may stop before end of file +is met, so the position of end of file will not usually show when +@option{--block-number} (@option{-R}) is used. Note that @GNUTAR{} +drains the archive before exiting when reading the +archive from a pipe. + +@cindex Error message, block number of +This option is especially useful when reading damaged archives, since +it helps pinpoint the damaged sections. It can also be used with +@option{--list} (@option{-t}) when listing a file-system backup tape, allowing you to +choose among several backup tapes when retrieving a file later, in +favor of the tape where the file appears earliest (closest to the +front of the tape). @xref{backup}. + +@node checkpoints +@section Checkpoints +@cindex checkpoints, defined +@opindex checkpoint +@opindex checkpoint-action + +A @dfn{checkpoint} is a moment of time before writing @var{n}th record to +the archive (a @dfn{write checkpoint}), or before reading @var{n}th record +from the archive (a @dfn{read checkpoint}). Checkpoints allow to +periodically execute arbitrary actions. + +The checkpoint facility is enabled using the following option: + +@table @option +@xopindex{checkpoint, defined} +@item --checkpoint[=@var{n}] +Schedule checkpoints before writing or reading each @var{n}th record. +The default value for @var{n} is 10. +@end table + +A list of arbitrary @dfn{actions} can be executed at each checkpoint. +These actions include: pausing, displaying textual messages, and +executing arbitrary external programs. Actions are defined using +the @option{--checkpoint-action} option. + +@table @option +@xopindex{checkpoint-action, defined} +@item --checkpoint-action=@var{action} +Execute an @var{action} at each checkpoint. +@end table + +@cindex @code{echo}, checkpoint action +The simplest value of @var{action} is @samp{echo}. It instructs +@command{tar} to display the default message on the standard error +stream upon arriving at each checkpoint. The default message is (in +@acronym{POSIX} locale) @samp{Write checkpoint @var{n}}, for write +checkpoints, and @samp{Read checkpoint @var{n}}, for read checkpoints. +Here, @var{n} represents ordinal number of the checkpoint. + +In another locales, translated versions of this message are used. + +This is the default action, so running: + +@smallexample +$ @kbd{tar -c --checkpoint=1000 --checkpoint-action=echo} /var +@end smallexample + +@noindent +is equivalent to: + +@smallexample +$ @kbd{tar -c --checkpoint=1000} /var +@end smallexample + +The @samp{echo} action also allows to supply a customized message. +You do so by placing an equals sign and the message right after it, +e.g.: + +@smallexample +--checkpoint-action="echo=Hit %s checkpoint #%u" +@end smallexample + +The @samp{%s} and @samp{%u} in the above example are +@dfn{meta-characters}. The @samp{%s} meta-character is replaced with +the @dfn{type} of the checkpoint: @samp{write} or +@samp{read} (or a corresponding translated version in locales other +than @acronym{POSIX}). The @samp{%u} meta-character is replaced with +the ordinal number of the checkpoint. Thus, the above example could +produce the following output when used with the @option{--create} +option: + +@smallexample +tar: Hit write checkpoint #10 +tar: Hit write checkpoint #20 +tar: Hit write checkpoint #30 +@end smallexample + +Aside from meta-character expansion, the message string is subject to +@dfn{unquoting}, during which the backslash @dfn{escape sequences} are +replaced with their corresponding @acronym{ASCII} characters +(@pxref{escape sequences}). E.g. the following action will produce an +audible bell and the message described above at each checkpoint: + +@smallexample +--checkpoint-action='echo=\aHit %s checkpoint #%u' +@end smallexample + +@cindex @code{bell}, checkpoint action +There is also a special action which produces an audible signal: +@samp{bell}. It is not equivalent to @samp{echo='\a'}, because +@samp{bell} sends the bell directly to the console (@file{/dev/tty}), +whereas @samp{echo='\a'} sends it to the standard error. + +@cindex @code{ttyout}, checkpoint action +The @samp{ttyout=@var{string}} action outputs @var{string} to +@file{/dev/tty}, so it can be used even if the standard output is +redirected elsewhere. The @var{string} is subject to the same +modifications as with @samp{echo} action. In contrast to the latter, +@samp{ttyout} does not prepend @command{tar} executable name to the +string, nor does it output a newline after it. For example, the +following action will print the checkpoint message at the same screen +line, overwriting any previous message: + +@smallexample +--checkpoint-action="ttyout=\rHit %s checkpoint #%u" +@end smallexample + +@cindex @code{dot}, checkpoint action +Another available checkpoint action is @samp{dot} (or @samp{.}). It +instructs @command{tar} to print a single dot on the standard listing +stream, e.g.: + +@smallexample +$ @kbd{tar -c --checkpoint=1000 --checkpoint-action=dot} /var +... +@end smallexample + +For compatibility with previous @GNUTAR{} versions, this action can +be abbreviated by placing a dot in front of the checkpoint frequency, +as shown in the previous section. + +@cindex @code{sleep}, checkpoint action +Yet another action, @samp{sleep}, pauses @command{tar} for a specified +amount of seconds. The following example will stop for 30 seconds at each +checkpoint: + +@smallexample +$ @kbd{tar -c --checkpoint=1000 --checkpoint-action=sleep=30} +@end smallexample + +@cindex @code{exec}, checkpoint action +Finally, the @code{exec} action executes a given external program. +For example: + +@smallexample +$ @kbd{tar -c --checkpoint=1000 --checkpoint-action=exec=/sbin/cpoint} +@end smallexample + +This program is executed using @command{/bin/sh -c}, with no +additional arguments. Its exit code is ignored. It gets a copy of +@command{tar}'s environment plus the following variables: + +@table @env +@vrindex TAR_VERSION, checkpoint script environment +@item TAR_VERSION +@GNUTAR{} version number. + +@vrindex TAR_ARCHIVE, checkpoint script environment +@item TAR_ARCHIVE +The name of the archive @command{tar} is processing. + +@vrindex TAR_BLOCKING_FACTOR, checkpoint script environment +@item TAR_BLOCKING_FACTOR +Current blocking factor (@pxref{Blocking}). + +@vrindex TAR_CHECKPOINT, checkpoint script environment +@item TAR_CHECKPOINT +Number of the checkpoint. + +@vrindex TAR_SUBCOMMAND, checkpoint script environment +@item TAR_SUBCOMMAND +A short option describing the operation @command{tar} is executing. +@xref{Operations}, for a complete list of subcommand options. + +@vrindex TAR_FORMAT, checkpoint script environment +@item TAR_FORMAT +Format of the archive being processed. @xref{Formats}, for a complete +list of archive format names. +@end table + +Any number of actions can be defined, by supplying several +@option{--checkpoint-action} options in the command line. For +example, the command below displays two messages, pauses +execution for 30 seconds and executes the @file{/sbin/cpoint} script: + +@example +@group +$ @kbd{tar -c -f arc.tar \ + --checkpoint-action='\aecho=Hit %s checkpoint #%u' \ + --checkpoint-action='echo=Sleeping for 30 seconds' \ + --checkpoint-action='sleep=30' \ + --checkpoint-action='exec=/sbin/cpoint'} +@end group +@end example + +This example also illustrates the fact that +@option{--checkpoint-action} can be used without +@option{--checkpoint}. In this case, the default checkpoint frequency +(at each 10th record) is assumed. + +@node warnings +@section Controlling Warning Messages + +Sometimes, while performing the requested task, @GNUTAR{} notices +some conditions that are not exactly errors, but which the user +should be aware of. When this happens, @command{tar} issues a +@dfn{warning message} describing the condition. Warning messages +are output to the standard error and they do not affect the exit +code of @command{tar} command. + +@xopindex{warning, explained} +@GNUTAR{} allows the user to suppress some or all of its warning +messages: + +@table @option +@item --warning=@var{keyword} +Control display of the warning messages identified by @var{keyword}. +If @var{keyword} starts with the prefix @samp{no-}, such messages are +suppressed. Otherwise, they are enabled. + +Multiple @option{--warning} messages accumulate. + +The tables below list allowed values for @var{keyword} along with the +warning messages they control. +@end table + +@subheading Keywords controlling @command{tar} operation +@table @asis +@kwindex all +@item all +Enable all warning messages. This is the default. +@kwindex none +@item none +Disable all warning messages. +@kwindex filename-with-nuls +@cindex @samp{file name read contains nul character}, warning message +@item filename-with-nuls +@samp{%s: file name read contains nul character} +@kwindex alone-zero-block +@cindex @samp{A lone zero block at}, warning message +@item alone-zero-block +@samp{A lone zero block at %s} +@end table + +@subheading Keywords applicable for @command{tar --create} +@table @asis +@kwindex cachedir +@cindex @samp{contains a cache directory tag}, warning message +@item cachedir +@samp{%s: contains a cache directory tag %s; %s} +@kwindex file-shrank +@cindex @samp{File shrank by %s bytes}, warning message +@item file-shrank +@samp{%s: File shrank by %s bytes; padding with zeros} +@kwindex xdev +@cindex @samp{file is on a different filesystem}, warning message +@item xdev +@samp{%s: file is on a different filesystem; not dumped} +@kwindex file-ignored +@cindex @samp{Unknown file type; file ignored}, warning message +@cindex @samp{socket ignored}, warning message +@cindex @samp{door ignored}, warning message +@item file-ignored +@samp{%s: Unknown file type; file ignored} +@*@samp{%s: socket ignored} +@*@samp{%s: door ignored} +@kwindex file-unchanged +@cindex @samp{file is unchanged; not dumped}, warning message +@item file-unchanged +@samp{%s: file is unchanged; not dumped} +@kwindex ignore-archive +@cindex @samp{file is the archive; not dumped}, warning message +@kwindex ignore-archive +@cindex @samp{file is the archive; not dumped}, warning message +@item ignore-archive +@samp{%s: file is the archive; not dumped} +@kwindex file-removed +@cindex @samp{File removed before we read it}, warning message +@item file-removed +@samp{%s: File removed before we read it} +@kwindex file-changed +@cindex @samp{file changed as we read it}, warning message +@item file-changed +@samp{%s: file changed as we read it} +@end table + +@subheading Keywords applicable for @command{tar --extract} +@table @asis +@kwindex timestamp +@cindex @samp{implausibly old time stamp %s}, warning message +@cindex @samp{time stamp %s is %s s in the future}, warning message +@item timestamp +@samp{%s: implausibly old time stamp %s} +@*@samp{%s: time stamp %s is %s s in the future} +@kwindex contiguous-cast +@cindex @samp{Extracting contiguous files as regular files}, warning message +@item contiguous-cast +@samp{Extracting contiguous files as regular files} +@kwindex symlink-cast +@cindex @samp{Attempting extraction of symbolic links as hard links}, warning message +@item symlink-cast +@samp{Attempting extraction of symbolic links as hard links} +@kwindex unknown-cast +@cindex @samp{Unknown file type `%c', extracted as normal file}, warning message +@item unknown-cast +@samp{%s: Unknown file type `%c', extracted as normal file} +@kwindex ignore-newer +@cindex @samp{Current %s is newer or same age}, warning message +@item ignore-newer +@samp{Current %s is newer or same age} +@kwindex unknown-keyword +@cindex @samp{Ignoring unknown extended header keyword `%s'}, warning message +@item unknown-keyword +@samp{Ignoring unknown extended header keyword `%s'} +@kwindex decompress-program +@item decompress-program +Controls verbose description of failures occurring when trying to run +alternative decompressor programs (@pxref{alternative decompression +programs}). This warning is disabled by default (unless +@option{--verbose} is used). A common example of what you can get +when using this warning is: + +@smallexample +$ @kbd{tar --warning=decompress-program -x -f archive.Z} +tar (child): cannot run compress: No such file or directory +tar (child): trying gzip +@end smallexample + +This means that @command{tar} first tried to decompress +@file{archive.Z} using @command{compress}, and, when that +failed, switched to @command{gzip}. +@end table + +@subheading Keywords controlling incremental extraction: +@table @asis +@kwindex rename-directory +@cindex @samp{%s: Directory has been renamed from %s}, warning message +@cindex @samp{%s: Directory has been renamed}, warning message +@item rename-directory +@samp{%s: Directory has been renamed from %s} +@*@samp{%s: Directory has been renamed} +@kwindex new-directory +@cindex @samp{%s: Directory is new}, warning message +@item new-directory +@samp{%s: Directory is new} +@kwindex xdev +@cindex @samp{%s: directory is on a different device: not purging}, warning message +@item xdev +@samp{%s: directory is on a different device: not purging} +@kwindex bad-dumpdir +@cindex @samp{Malformed dumpdir: 'X' never used}, warning message +@item bad-dumpdir +@samp{Malformed dumpdir: 'X' never used} +@end table + +@node interactive +@section Asking for Confirmation During Operations +@cindex Interactive operation + +Typically, @command{tar} carries out a command without stopping for +further instructions. In some situations however, you may want to +exclude some files and archive members from the operation (for instance +if disk or storage space is tight). You can do this by excluding +certain files automatically (@pxref{Choosing}), or by performing +an operation interactively, using the @option{--interactive} (@option{-w}) option. +@command{tar} also accepts @option{--confirmation} for this option. + +@opindex interactive +When the @option{--interactive} (@option{-w}) option is specified, before +reading, writing, or deleting files, @command{tar} first prints a message +for each such file, telling what operation it intends to take, then asks +for confirmation on the terminal. The actions which require +confirmation include adding a file to the archive, extracting a file +from the archive, deleting a file from the archive, and deleting a file +from disk. To confirm the action, you must type a line of input +beginning with @samp{y}. If your input line begins with anything other +than @samp{y}, @command{tar} skips that file. + +If @command{tar} is reading the archive from the standard input, +@command{tar} opens the file @file{/dev/tty} to support the interactive +communications. + +Verbose output is normally sent to standard output, separate from +other error messages. However, if the archive is produced directly +on standard output, then verbose output is mixed with errors on +@code{stderr}. Producing the archive on standard output may be used +as a way to avoid using disk space, when the archive is soon to be +consumed by another process reading it, say. Some people felt the need +of producing an archive on stdout, still willing to segregate between +verbose output and error output. A possible approach would be using a +named pipe to receive the archive, and having the consumer process to +read from that named pipe. This has the advantage of letting standard +output free to receive verbose output, all separate from errors. + +@node operations +@chapter @GNUTAR{} Operations + +@menu +* Basic tar:: +* Advanced tar:: +* create options:: +* extract options:: +* backup:: +* Applications:: +* looking ahead:: +@end menu + +@node Basic tar +@section Basic @GNUTAR{} Operations + +The basic @command{tar} operations, @option{--create} (@option{-c}), +@option{--list} (@option{-t}) and @option{--extract} (@option{--get}, +@option{-x}), are currently presented and described in the tutorial +chapter of this manual. This section provides some complementary notes +for these operations. + +@table @option +@xopindex{create, complementary notes} +@item --create +@itemx -c + +Creating an empty archive would have some kind of elegance. One can +initialize an empty archive and later use @option{--append} +(@option{-r}) for adding all members. Some applications would not +welcome making an exception in the way of adding the first archive +member. On the other hand, many people reported that it is +dangerously too easy for @command{tar} to destroy a magnetic tape with +an empty archive@footnote{This is well described in @cite{Unix-haters +Handbook}, by Simson Garfinkel, Daniel Weise & Steven Strassmann, IDG +Books, ISBN 1-56884-203-1.}. The two most common errors are: + +@enumerate +@item +Mistakingly using @code{create} instead of @code{extract}, when the +intent was to extract the full contents of an archive. This error +is likely: keys @kbd{c} and @kbd{x} are right next to each other on +the QWERTY keyboard. Instead of being unpacked, the archive then +gets wholly destroyed. When users speak about @dfn{exploding} an +archive, they usually mean something else :-). + +@item +Forgetting the argument to @code{file}, when the intent was to create +an archive with a single file in it. This error is likely because a +tired user can easily add the @kbd{f} key to the cluster of option +letters, by the mere force of habit, without realizing the full +consequence of doing so. The usual consequence is that the single +file, which was meant to be saved, is rather destroyed. +@end enumerate + +So, recognizing the likelihood and the catastrophic nature of these +errors, @GNUTAR{} now takes some distance from elegance, and +cowardly refuses to create an archive when @option{--create} option is +given, there are no arguments besides options, and +@option{--files-from} (@option{-T}) option is @emph{not} used. To get +around the cautiousness of @GNUTAR{} and nevertheless create an +archive with nothing in it, one may still use, as the value for the +@option{--files-from} option, a file with no names in it, as shown in +the following commands: + +@smallexample +@kbd{tar --create --file=empty-archive.tar --files-from=/dev/null} +@kbd{tar cfT empty-archive.tar /dev/null} +@end smallexample + +@xopindex{extract, complementary notes} +@item --extract +@itemx --get +@itemx -x + +A socket is stored, within a @GNUTAR{} archive, as a pipe. + +@item @option{--list} (@option{-t}) + +@GNUTAR{} now shows dates as @samp{1996-08-30}, +while it used to show them as @samp{Aug 30 1996}. Preferably, +people should get used to ISO 8601 dates. Local American dates should +be made available again with full date localization support, once +ready. In the meantime, programs not being localizable for dates +should prefer international dates, that's really the way to go. + +Look up @url{http://www.cl.cam.ac.uk/@/~mgk25/@/iso-time.html} if you +are curious, it contains a detailed explanation of the ISO 8601 standard. + +@end table + +@node Advanced tar +@section Advanced @GNUTAR{} Operations + +Now that you have learned the basics of using @GNUTAR{}, you may want +to learn about further ways in which @command{tar} can help you. + +This chapter presents five, more advanced operations which you probably +won't use on a daily basis, but which serve more specialized functions. +We also explain the different styles of options and why you might want +to use one or another, or a combination of them in your @command{tar} +commands. Additionally, this chapter includes options which allow you to +define the output from @command{tar} more carefully, and provide help and +error correction in special circumstances. + +@FIXME{check this after the chapter is actually revised to make sure +it still introduces the info in the chapter correctly : ).} + +@menu +* Operations:: +* append:: +* update:: +* concatenate:: +* delete:: +* compare:: +@end menu + +@node Operations +@subsection The Five Advanced @command{tar} Operations + +@cindex basic operations +In the last chapter, you learned about the first three operations to +@command{tar}. This chapter presents the remaining five operations to +@command{tar}: @option{--append}, @option{--update}, @option{--concatenate}, +@option{--delete}, and @option{--compare}. + +You are not likely to use these operations as frequently as those +covered in the last chapter; however, since they perform specialized +functions, they are quite useful when you do need to use them. We +will give examples using the same directory and files that you created +in the last chapter. As you may recall, the directory is called +@file{practice}, the files are @samp{jazz}, @samp{blues}, @samp{folk}, +and the two archive files you created are +@samp{collection.tar} and @samp{music.tar}. + +We will also use the archive files @samp{afiles.tar} and +@samp{bfiles.tar}. The archive @samp{afiles.tar} contains the members @samp{apple}, +@samp{angst}, and @samp{aspic}; @samp{bfiles.tar} contains the members +@samp{./birds}, @samp{baboon}, and @samp{./box}. + +Unless we state otherwise, all practicing you do and examples you follow +in this chapter will take place in the @file{practice} directory that +you created in the previous chapter; see @ref{prepare for examples}. +(Below in this section, we will remind you of the state of the examples +where the last chapter left them.) + +The five operations that we will cover in this chapter are: + +@table @option +@item --append +@itemx -r +Add new entries to an archive that already exists. +@item --update +@itemx -u +Add more recent copies of archive members to the end of an archive, if +they exist. +@item --concatenate +@itemx --catenate +@itemx -A +Add one or more pre-existing archives to the end of another archive. +@item --delete +Delete items from an archive (does not work on tapes). +@item --compare +@itemx --diff +@itemx -d +Compare archive members to their counterparts in the file system. +@end table + +@node append +@subsection How to Add Files to Existing Archives: @option{--append} + +@cindex appending files to existing archive +@opindex append +If you want to add files to an existing archive, you don't need to +create a new archive; you can use @option{--append} (@option{-r}). +The archive must already exist in order to use @option{--append}. (A +related operation is the @option{--update} operation; you can use this +to add newer versions of archive members to an existing archive. To learn how to +do this with @option{--update}, @pxref{update}.) + +If you use @option{--append} to add a file that has the same name as an +archive member to an archive containing that archive member, then the +old member is not deleted. What does happen, however, is somewhat +complex. @command{tar} @emph{allows} you to have infinite number of files +with the same name. Some operations treat these same-named members no +differently than any other set of archive members: for example, if you +view an archive with @option{--list} (@option{-t}), you will see all +of those members listed, with their data modification times, owners, etc. + +Other operations don't deal with these members as perfectly as you might +prefer; if you were to use @option{--extract} to extract the archive, +only the most recently added copy of a member with the same name as +other members would end up in the working directory. This is because +@option{--extract} extracts an archive in the order the members appeared +in the archive; the most recently archived members will be extracted +last. Additionally, an extracted member will @emph{replace} a file of +the same name which existed in the directory already, and @command{tar} +will not prompt you about this@footnote{Unless you give it +@option{--keep-old-files} option, or the disk copy is newer than +the one in the archive and you invoke @command{tar} with +@option{--keep-newer-files} option.}. Thus, only the most recently archived +member will end up being extracted, as it will replace the one +extracted before it, and so on. + +@cindex extracting @var{n}th copy of the file +@xopindex{occurrence, described} +There exists a special option that allows you to get around this +behavior and extract (or list) only a particular copy of the file. +This is @option{--occurrence} option. If you run @command{tar} with +this option, it will extract only the first copy of the file. You +may also give this option an argument specifying the number of +copy to be extracted. Thus, for example if the archive +@file{archive.tar} contained three copies of file @file{myfile}, then +the command + +@smallexample +tar --extract --file archive.tar --occurrence=2 myfile +@end smallexample + +@noindent +would extract only the second copy. @xref{Option +Summary,---occurrence}, for the description of @option{--occurrence} +option. + +@FIXME{ hag -- you might want to incorporate some of the above into the +MMwtSN node; not sure. i didn't know how to make it simpler... + +There are a few ways to get around this. Xref to Multiple Members +with the Same Name, maybe.} + +@cindex Members, replacing with other members +@cindex Replacing members with other members +@xopindex{delete, using before --append} +If you want to replace an archive member, use @option{--delete} to +delete the member you want to remove from the archive, and then use +@option{--append} to add the member you want to be in the archive. Note +that you can not change the order of the archive; the most recently +added member will still appear last. In this sense, you cannot truly +``replace'' one member with another. (Replacing one member with another +will not work on certain types of media, such as tapes; see @ref{delete} +and @ref{Media}, for more information.) + +@menu +* appending files:: Appending Files to an Archive +* multiple:: +@end menu + +@node appending files +@subsubsection Appending Files to an Archive +@cindex Adding files to an Archive +@cindex Appending files to an Archive +@cindex Archives, Appending files to +@opindex append + +The simplest way to add a file to an already existing archive is the +@option{--append} (@option{-r}) operation, which writes specified +files into the archive whether or not they are already among the +archived files. + +When you use @option{--append}, you @emph{must} specify file name +arguments, as there is no default. If you specify a file that already +exists in the archive, another copy of the file will be added to the +end of the archive. As with other operations, the member names of the +newly added files will be exactly the same as their names given on the +command line. The @option{--verbose} (@option{-v}) option will print +out the names of the files as they are written into the archive. + +@option{--append} cannot be performed on some tape drives, unfortunately, +due to deficiencies in the formats those tape drives use. The archive +must be a valid @command{tar} archive, or else the results of using this +operation will be unpredictable. @xref{Media}. + +To demonstrate using @option{--append} to add a file to an archive, +create a file called @file{rock} in the @file{practice} directory. +Make sure you are in the @file{practice} directory. Then, run the +following @command{tar} command to add @file{rock} to +@file{collection.tar}: + +@smallexample +$ @kbd{tar --append --file=collection.tar rock} +@end smallexample + +@noindent +If you now use the @option{--list} (@option{-t}) operation, you will see that +@file{rock} has been added to the archive: + +@smallexample +$ @kbd{tar --list --file=collection.tar} +-rw-r--r-- me/user 28 1996-10-18 16:31 jazz +-rw-r--r-- me/user 21 1996-09-23 16:44 blues +-rw-r--r-- me/user 20 1996-09-23 16:44 folk +-rw-r--r-- me/user 20 1996-09-23 16:44 rock +@end smallexample + +@node multiple +@subsubsection Multiple Members with the Same Name +@cindex members, multiple +@cindex multiple members + +You can use @option{--append} (@option{-r}) to add copies of files +which have been updated since the archive was created. (However, we +do not recommend doing this since there is another @command{tar} +option called @option{--update}; @xref{update}, for more information. +We describe this use of @option{--append} here for the sake of +completeness.) When you extract the archive, the older version will +be effectively lost. This works because files are extracted from an +archive in the order in which they were archived. Thus, when the +archive is extracted, a file archived later in time will replace a +file of the same name which was archived earlier, even though the +older version of the file will remain in the archive unless you delete +all versions of the file. + +Supposing you change the file @file{blues} and then append the changed +version to @file{collection.tar}. As you saw above, the original +@file{blues} is in the archive @file{collection.tar}. If you change the +file and append the new version of the file to the archive, there will +be two copies in the archive. When you extract the archive, the older +version of the file will be extracted first, and then replaced by the +newer version when it is extracted. + +You can append the new, changed copy of the file @file{blues} to the +archive in this way: + +@smallexample +$ @kbd{tar --append --verbose --file=collection.tar blues} +blues +@end smallexample + +@noindent +Because you specified the @option{--verbose} option, @command{tar} has +printed the name of the file being appended as it was acted on. Now +list the contents of the archive: + +@smallexample +$ @kbd{tar --list --verbose --file=collection.tar} +-rw-r--r-- me/user 28 1996-10-18 16:31 jazz +-rw-r--r-- me/user 21 1996-09-23 16:44 blues +-rw-r--r-- me/user 20 1996-09-23 16:44 folk +-rw-r--r-- me/user 20 1996-09-23 16:44 rock +-rw-r--r-- me/user 58 1996-10-24 18:30 blues +@end smallexample + +@noindent +The newest version of @file{blues} is now at the end of the archive +(note the different creation dates and file sizes). If you extract +the archive, the older version of the file @file{blues} will be +replaced by the newer version. You can confirm this by extracting +the archive and running @samp{ls} on the directory. + +If you wish to extract the first occurrence of the file @file{blues} +from the archive, use @option{--occurrence} option, as shown in +the following example: + +@smallexample +$ @kbd{tar --extract -vv --occurrence --file=collection.tar blues} +-rw-r--r-- me/user 21 1996-09-23 16:44 blues +@end smallexample + +@xref{Writing}, for more information on @option{--extract} and +see @ref{Option Summary, --occurrence}, for a description of +@option{--occurrence} option. + +@node update +@subsection Updating an Archive +@cindex Updating an archive +@opindex update + +In the previous section, you learned how to use @option{--append} to +add a file to an existing archive. A related operation is +@option{--update} (@option{-u}). The @option{--update} operation +updates a @command{tar} archive by comparing the date of the specified +archive members against the date of the file with the same name. If +the file has been modified more recently than the archive member, then +the newer version of the file is added to the archive (as with +@option{--append}). + +Unfortunately, you cannot use @option{--update} with magnetic tape drives. +The operation will fail. + +@FIXME{other examples of media on which --update will fail? need to ask +charles and/or mib/thomas/dave shevett..} + +Both @option{--update} and @option{--append} work by adding to the end +of the archive. When you extract a file from the archive, only the +version stored last will wind up in the file system, unless you use +the @option{--backup} option. @xref{multiple}, for a detailed discussion. + +@menu +* how to update:: +@end menu + +@node how to update +@subsubsection How to Update an Archive Using @option{--update} +@opindex update + +You must use file name arguments with the @option{--update} +(@option{-u}) operation. If you don't specify any files, +@command{tar} won't act on any files and won't tell you that it didn't +do anything (which may end up confusing you). + +@c note: the above parenthetical added because in fact, this +@c behavior just confused the author. :-) + +To see the @option{--update} option at work, create a new file, +@file{classical}, in your practice directory, and some extra text to the +file @file{blues}, using any text editor. Then invoke @command{tar} with +the @samp{update} operation and the @option{--verbose} (@option{-v}) +option specified, using the names of all the files in the @file{practice} +directory as file name arguments: + +@smallexample +$ @kbd{tar --update -v -f collection.tar blues folk rock classical} +blues +classical +$ +@end smallexample + +@noindent +Because we have specified verbose mode, @command{tar} prints out the names +of the files it is working on, which in this case are the names of the +files that needed to be updated. If you run @samp{tar --list} and look +at the archive, you will see @file{blues} and @file{classical} at its +end. There will be a total of two versions of the member @samp{blues}; +the one at the end will be newer and larger, since you added text before +updating it. + +The reason @command{tar} does not overwrite the older file when updating +it is because writing to the middle of a section of tape is a difficult +process. Tapes are not designed to go backward. @xref{Media}, for more +information about tapes. + +@option{--update} (@option{-u}) is not suitable for performing backups for two +reasons: it does not change directory content entries, and it +lengthens the archive every time it is used. The @GNUTAR{} +options intended specifically for backups are more +efficient. If you need to run backups, please consult @ref{Backups}. + +@node concatenate +@subsection Combining Archives with @option{--concatenate} + +@cindex Adding archives to an archive +@cindex Concatenating Archives +@opindex concatenate +@opindex catenate +@c @cindex @option{-A} described +Sometimes it may be convenient to add a second archive onto the end of +an archive rather than adding individual files to the archive. To add +one or more archives to the end of another archive, you should use the +@option{--concatenate} (@option{--catenate}, @option{-A}) operation. + +To use @option{--concatenate}, give the first archive with +@option{--file} option and name the rest of archives to be +concatenated on the command line. The members, and their member +names, will be copied verbatim from those archives to the first +one@footnote{This can cause multiple members to have the same name. For +information on how this affects reading the archive, see @ref{multiple}.}. +The new, concatenated archive will be called by the same name as the +one given with the @option{--file} option. As usual, if you omit +@option{--file}, @command{tar} will use the value of the environment +variable @env{TAPE}, or, if this has not been set, the default archive name. + +@FIXME{There is no way to specify a new name...} + +To demonstrate how @option{--concatenate} works, create two small archives +called @file{bluesrock.tar} and @file{folkjazz.tar}, using the relevant +files from @file{practice}: + +@smallexample +$ @kbd{tar -cvf bluesrock.tar blues rock} +blues +rock +$ @kbd{tar -cvf folkjazz.tar folk jazz} +folk +jazz +@end smallexample + +@noindent +If you like, You can run @samp{tar --list} to make sure the archives +contain what they are supposed to: + +@smallexample +$ @kbd{tar -tvf bluesrock.tar} +-rw-r--r-- melissa/user 105 1997-01-21 19:42 blues +-rw-r--r-- melissa/user 33 1997-01-20 15:34 rock +$ @kbd{tar -tvf jazzfolk.tar} +-rw-r--r-- melissa/user 20 1996-09-23 16:44 folk +-rw-r--r-- melissa/user 65 1997-01-30 14:15 jazz +@end smallexample + +We can concatenate these two archives with @command{tar}: + +@smallexample +$ @kbd{cd ..} +$ @kbd{tar --concatenate --file=bluesrock.tar jazzfolk.tar} +@end smallexample + +If you now list the contents of the @file{bluesrock.tar}, you will see +that now it also contains the archive members of @file{jazzfolk.tar}: + +@smallexample +$ @kbd{tar --list --file=bluesrock.tar} +blues +rock +folk +jazz +@end smallexample + +When you use @option{--concatenate}, the source and target archives must +already exist and must have been created using compatible format +parameters. Notice, that @command{tar} does not check whether the +archives it concatenates have compatible formats, it does not +even check if the files are really tar archives. + +Like @option{--append} (@option{-r}), this operation cannot be performed on some +tape drives, due to deficiencies in the formats those tape drives use. + +@cindex @code{concatenate} vs @command{cat} +@cindex @command{cat} vs @code{concatenate} +It may seem more intuitive to you to want or try to use @command{cat} to +concatenate two archives instead of using the @option{--concatenate} +operation; after all, @command{cat} is the utility for combining files. + +However, @command{tar} archives incorporate an end-of-file marker which +must be removed if the concatenated archives are to be read properly as +one archive. @option{--concatenate} removes the end-of-archive marker +from the target archive before each new archive is appended. If you use +@command{cat} to combine the archives, the result will not be a valid +@command{tar} format archive. If you need to retrieve files from an +archive that was added to using the @command{cat} utility, use the +@option{--ignore-zeros} (@option{-i}) option. @xref{Ignore Zeros}, for further +information on dealing with archives improperly combined using the +@command{cat} shell utility. + +@node delete +@subsection Removing Archive Members Using @option{--delete} +@cindex Deleting files from an archive +@cindex Removing files from an archive + +@opindex delete +You can remove members from an archive by using the @option{--delete} +option. Specify the name of the archive with @option{--file} +(@option{-f}) and then specify the names of the members to be deleted; +if you list no member names, nothing will be deleted. The +@option{--verbose} option will cause @command{tar} to print the names +of the members as they are deleted. As with @option{--extract}, you +must give the exact member names when using @samp{tar --delete}. +@option{--delete} will remove all versions of the named file from the +archive. The @option{--delete} operation can run very slowly. + +Unlike other operations, @option{--delete} has no short form. + +@cindex Tapes, using @option{--delete} and +@cindex Deleting from tape archives +This operation will rewrite the archive. You can only use +@option{--delete} on an archive if the archive device allows you to +write to any point on the media, such as a disk; because of this, it +does not work on magnetic tapes. Do not try to delete an archive member +from a magnetic tape; the action will not succeed, and you will be +likely to scramble the archive and damage your tape. There is no safe +way (except by completely re-writing the archive) to delete files from +most kinds of magnetic tape. @xref{Media}. + +To delete all versions of the file @file{blues} from the archive +@file{collection.tar} in the @file{practice} directory, make sure you +are in that directory, and then, + +@smallexample +$ @kbd{tar --list --file=collection.tar} +blues +folk +jazz +rock +$ @kbd{tar --delete --file=collection.tar blues} +$ @kbd{tar --list --file=collection.tar} +folk +jazz +rock +@end smallexample + +@FIXME{Check if the above listing is actually produced after running +all the examples on collection.tar.} + +The @option{--delete} option has been reported to work properly when +@command{tar} acts as a filter from @code{stdin} to @code{stdout}. + +@node compare +@subsection Comparing Archive Members with the File System +@cindex Verifying the currency of an archive + +@opindex compare +The @option{--compare} (@option{-d}), or @option{--diff} operation compares +specified archive members against files with the same names, and then +reports differences in file size, mode, owner, modification date and +contents. You should @emph{only} specify archive member names, not file +names. If you do not name any members, then @command{tar} will compare the +entire archive. If a file is represented in the archive but does not +exist in the file system, @command{tar} reports a difference. + +You have to specify the record size of the archive when modifying an +archive with a non-default record size. + +@command{tar} ignores files in the file system that do not have +corresponding members in the archive. + +The following example compares the archive members @file{rock}, +@file{blues} and @file{funk} in the archive @file{bluesrock.tar} with +files of the same name in the file system. (Note that there is no file, +@file{funk}; @command{tar} will report an error message.) + +@smallexample +$ @kbd{tar --compare --file=bluesrock.tar rock blues funk} +rock +blues +tar: funk not found in archive +@end smallexample + +The spirit behind the @option{--compare} (@option{--diff}, +@option{-d}) option is to check whether the archive represents the +current state of files on disk, more than validating the integrity of +the archive media. For this latter goal, see @ref{verify}. + +@node create options +@section Options Used by @option{--create} + +@xopindex{create, additional options} +The previous chapter described the basics of how to use +@option{--create} (@option{-c}) to create an archive from a set of files. +@xref{create}. This section described advanced options to be used with +@option{--create}. + +@menu +* override:: Overriding File Metadata. +* Ignore Failed Read:: +@end menu + +@node override +@subsection Overriding File Metadata + +As described above, a @command{tar} archive keeps, for each member it contains, +its @dfn{metadata}, such as modification time, mode and ownership of +the file. @GNUTAR{} allows to replace these data with other values +when adding files to the archive. The options described in this +section affect creation of archives of any type. For POSIX archives, +see also @ref{PAX keywords}, for additional ways of controlling +metadata, stored in the archive. + +@table @option +@opindex mode +@item --mode=@var{permissions} + +When adding files to an archive, @command{tar} will use +@var{permissions} for the archive members, rather than the permissions +from the files. @var{permissions} can be specified either as an octal +number or as symbolic permissions, like with +@command{chmod} (@xref{File permissions, Permissions, File +permissions, fileutils, @acronym{GNU} file utilities}. This reference +also has useful information for those not being overly familiar with +the UNIX permission system). Using latter syntax allows for +more flexibility. For example, the value @samp{a+rw} adds read and write +permissions for everybody, while retaining executable bits on directories +or on any other file already marked as executable: + +@smallexample +$ @kbd{tar -c -f archive.tar --mode='a+rw' .} +@end smallexample + +@item --mtime=@var{date} +@opindex mtime + +When adding files to an archive, @command{tar} will use @var{date} as +the modification time of members when creating archives, instead of +their actual modification times. The argument @var{date} can be +either a textual date representation in almost arbitrary format +(@pxref{Date input formats}) or a name of an existing file, starting +with @samp{/} or @samp{.}. In the latter case, the modification time +of that file will be used. + +The following example will set the modification date to 00:00:00, +January 1, 1970: + +@smallexample +$ @kbd{tar -c -f archive.tar --mtime='1970-01-01' .} +@end smallexample + +@noindent +When used with @option{--verbose} (@pxref{verbose tutorial}) @GNUTAR{} +will try to convert the specified date back to its textual +representation and compare it with the one given with +@option{--mtime} options. If the two dates differ, @command{tar} will +print a warning saying what date it will use. This is to help user +ensure he is using the right date. + +For example: + +@smallexample +$ @kbd{tar -c -f archive.tar -v --mtime=yesterday .} +tar: Option --mtime: Treating date `yesterday' as 2006-06-20 +13:06:29.152478 +@dots{} +@end smallexample + +@item --owner=@var{user} +@opindex owner + +Specifies that @command{tar} should use @var{user} as the owner of members +when creating archives, instead of the user associated with the source +file. The argument @var{user} can be either an existing user symbolic +name, or a decimal numeric user @acronym{ID}. + +There is no value indicating a missing number, and @samp{0} usually means +@code{root}. Some people like to force @samp{0} as the value to offer in +their distributions for the owner of files, because the @code{root} user is +anonymous anyway, so that might as well be the owner of anonymous +archives. For example: + +@smallexample +$ @kbd{tar -c -f archive.tar --owner=0 .} +@end smallexample + +@noindent +or: + +@smallexample +$ @kbd{tar -c -f archive.tar --owner=root .} +@end smallexample + +@item --group=@var{group} +@opindex group + +Files added to the @command{tar} archive will have a group @acronym{ID} of @var{group}, +rather than the group from the source file. The argument @var{group} +can be either an existing group symbolic name, or a decimal numeric group @acronym{ID}. +@end table + +@node Ignore Failed Read +@subsection Ignore Fail Read + +@table @option +@item --ignore-failed-read +@opindex ignore-failed-read +Do not exit with nonzero on unreadable files or directories. +@end table + +@node extract options +@section Options Used by @option{--extract} +@cindex options for use with @option{--extract} + +@xopindex{extract, additional options} +The previous chapter showed how to use @option{--extract} to extract +an archive into the file system. Various options cause @command{tar} to +extract more information than just file contents, such as the owner, +the permissions, the modification date, and so forth. This section +presents options to be used with @option{--extract} when certain special +considerations arise. You may review the information presented in +@ref{extract} for more basic information about the +@option{--extract} operation. + +@menu +* Reading:: Options to Help Read Archives +* Writing:: Changing How @command{tar} Writes Files +* Scarce:: Coping with Scarce Resources +@end menu + +@node Reading +@subsection Options to Help Read Archives +@cindex Options when reading archives + +@cindex Reading incomplete records +@cindex Records, incomplete +@opindex read-full-records +Normally, @command{tar} will request data in full record increments from +an archive storage device. If the device cannot return a full record, +@command{tar} will report an error. However, some devices do not always +return full records, or do not require the last record of an archive to +be padded out to the next record boundary. To keep reading until you +obtain a full record, or to accept an incomplete record if it contains +an end-of-archive marker, specify the @option{--read-full-records} (@option{-B}) option +in conjunction with the @option{--extract} or @option{--list} operations. +@xref{Blocking}. + +The @option{--read-full-records} (@option{-B}) option is turned on by default when +@command{tar} reads an archive from standard input, or from a remote +machine. This is because on @acronym{BSD} Unix systems, attempting to read a +pipe returns however much happens to be in the pipe, even if it is +less than was requested. If this option were not enabled, @command{tar} +would fail as soon as it read an incomplete record from the pipe. + +If you're not sure of the blocking factor of an archive, you can +read the archive by specifying @option{--read-full-records} (@option{-B}) and +@option{--blocking-factor=@var{512-size}} (@option{-b +@var{512-size}}), using a blocking factor larger than what the archive +uses. This lets you avoid having to determine the blocking factor +of an archive. @xref{Blocking Factor}. + +@menu +* read full records:: +* Ignore Zeros:: +@end menu + +@node read full records +@unnumberedsubsubsec Reading Full Records + +@FIXME{need sentence or so of intro here} + +@table @option +@opindex read-full-records +@item --read-full-records +@item -B +Use in conjunction with @option{--extract} (@option{--get}, +@option{-x}) to read an archive which contains incomplete records, or +one which has a blocking factor less than the one specified. +@end table + +@node Ignore Zeros +@unnumberedsubsubsec Ignoring Blocks of Zeros + +@cindex End-of-archive blocks, ignoring +@cindex Ignoring end-of-archive blocks +@opindex ignore-zeros +Normally, @command{tar} stops reading when it encounters a block of zeros +between file entries (which usually indicates the end of the archive). +@option{--ignore-zeros} (@option{-i}) allows @command{tar} to +completely read an archive which contains a block of zeros before the +end (i.e., a damaged archive, or one that was created by concatenating +several archives together). + +The @option{--ignore-zeros} (@option{-i}) option is turned off by default because many +versions of @command{tar} write garbage after the end-of-archive entry, +since that part of the media is never supposed to be read. @GNUTAR{} +does not write after the end of an archive, but seeks to +maintain compatibility among archiving utilities. + +@table @option +@item --ignore-zeros +@itemx -i +To ignore blocks of zeros (i.e., end-of-archive entries) which may be +encountered while reading an archive. Use in conjunction with +@option{--extract} or @option{--list}. +@end table + +@node Writing +@subsection Changing How @command{tar} Writes Files +@UNREVISED + +@FIXME{Introductory paragraph} + +@menu +* Dealing with Old Files:: +* Overwrite Old Files:: +* Keep Old Files:: +* Keep Newer Files:: +* Unlink First:: +* Recursive Unlink:: +* Data Modification Times:: +* Setting Access Permissions:: +* Directory Modification Times and Permissions:: +* Writing to Standard Output:: +* Writing to an External Program:: +* remove files:: +@end menu + +@node Dealing with Old Files +@unnumberedsubsubsec Options Controlling the Overwriting of Existing Files + +@xopindex{overwrite-dir, introduced} +When extracting files, if @command{tar} discovers that the extracted +file already exists, it normally replaces the file by removing it before +extracting it, to prevent confusion in the presence of hard or symbolic +links. (If the existing file is a symbolic link, it is removed, not +followed.) However, if a directory cannot be removed because it is +nonempty, @command{tar} normally overwrites its metadata (ownership, +permission, etc.). The @option{--overwrite-dir} option enables this +default behavior. To be more cautious and preserve the metadata of +such a directory, use the @option{--no-overwrite-dir} option. + +@cindex Overwriting old files, prevention +@xopindex{keep-old-files, introduced} +To be even more cautious and prevent existing files from being replaced, use +the @option{--keep-old-files} (@option{-k}) option. It causes @command{tar} to refuse +to replace or update a file that already exists, i.e., a file with the +same name as an archive member prevents extraction of that archive +member. Instead, it reports an error. + +@xopindex{overwrite, introduced} +To be more aggressive about altering existing files, use the +@option{--overwrite} option. It causes @command{tar} to overwrite +existing files and to follow existing symbolic links when extracting. + +@cindex Protecting old files +Some people argue that @GNUTAR{} should not hesitate +to overwrite files with other files when extracting. When extracting +a @command{tar} archive, they expect to see a faithful copy of the +state of the file system when the archive was created. It is debatable +that this would always be a proper behavior. For example, suppose one +has an archive in which @file{usr/local} is a link to +@file{usr/local2}. Since then, maybe the site removed the link and +renamed the whole hierarchy from @file{/usr/local2} to +@file{/usr/local}. Such things happen all the time. I guess it would +not be welcome at all that @GNUTAR{} removes the +whole hierarchy just to make room for the link to be reinstated +(unless it @emph{also} simultaneously restores the full +@file{/usr/local2}, of course!) @GNUTAR{} is indeed +able to remove a whole hierarchy to reestablish a symbolic link, for +example, but @emph{only if} @option{--recursive-unlink} is specified +to allow this behavior. In any case, single files are silently +removed. + +@xopindex{unlink-first, introduced} +Finally, the @option{--unlink-first} (@option{-U}) option can improve performance in +some cases by causing @command{tar} to remove files unconditionally +before extracting them. + +@node Overwrite Old Files +@unnumberedsubsubsec Overwrite Old Files + +@table @option +@opindex overwrite +@item --overwrite +Overwrite existing files and directory metadata when extracting files +from an archive. + +This causes @command{tar} to write extracted files into the file system without +regard to the files already on the system; i.e., files with the same +names as archive members are overwritten when the archive is extracted. +It also causes @command{tar} to extract the ownership, permissions, +and time stamps onto any preexisting files or directories. +If the name of a corresponding file name is a symbolic link, the file +pointed to by the symbolic link will be overwritten instead of the +symbolic link itself (if this is possible). Moreover, special devices, +empty directories and even symbolic links are automatically removed if +they are in the way of extraction. + +Be careful when using the @option{--overwrite} option, particularly when +combined with the @option{--absolute-names} (@option{-P}) option, as this combination +can change the contents, ownership or permissions of any file on your +system. Also, many systems do not take kindly to overwriting files that +are currently being executed. + +@opindex overwrite-dir +@item --overwrite-dir +Overwrite the metadata of directories when extracting files from an +archive, but remove other files before extracting. +@end table + +@node Keep Old Files +@unnumberedsubsubsec Keep Old Files + +@table @option +@opindex keep-old-files +@item --keep-old-files +@itemx -k +Do not replace existing files from archive. The +@option{--keep-old-files} (@option{-k}) option prevents @command{tar} +from replacing existing files with files with the same name from the +archive. The @option{--keep-old-files} option is meaningless with +@option{--list} (@option{-t}). Prevents @command{tar} from replacing +files in the file system during extraction. +@end table + +@node Keep Newer Files +@unnumberedsubsubsec Keep Newer Files + +@table @option +@opindex keep-newer-files +@item --keep-newer-files +Do not replace existing files that are newer than their archive +copies. This option is meaningless with @option{--list} (@option{-t}). +@end table + +@node Unlink First +@unnumberedsubsubsec Unlink First + +@table @option +@opindex unlink-first +@item --unlink-first +@itemx -U +Remove files before extracting over them. +This can make @command{tar} run a bit faster if you know in advance +that the extracted files all need to be removed. Normally this option +slows @command{tar} down slightly, so it is disabled by default. +@end table + +@node Recursive Unlink +@unnumberedsubsubsec Recursive Unlink + +@table @option +@opindex recursive-unlink +@item --recursive-unlink +When this option is specified, try removing files and directory hierarchies +before extracting over them. @emph{This is a dangerous option!} +@end table + +If you specify the @option{--recursive-unlink} option, +@command{tar} removes @emph{anything} that keeps you from extracting a file +as far as current permissions will allow it. This could include removal +of the contents of a full directory hierarchy. + +@node Data Modification Times +@unnumberedsubsubsec Setting Data Modification Times + +@cindex Data modification times of extracted files +@cindex Modification times of extracted files +Normally, @command{tar} sets the data modification times of extracted +files to the corresponding times recorded for the files in the archive, but +limits the permissions of extracted files by the current @code{umask} +setting. + +To set the data modification times of extracted files to the time when +the files were extracted, use the @option{--touch} (@option{-m}) option in +conjunction with @option{--extract} (@option{--get}, @option{-x}). + +@table @option +@opindex touch +@item --touch +@itemx -m +Sets the data modification time of extracted archive members to the time +they were extracted, not the time recorded for them in the archive. +Use in conjunction with @option{--extract} (@option{--get}, @option{-x}). +@end table + +@node Setting Access Permissions +@unnumberedsubsubsec Setting Access Permissions + +@cindex Permissions of extracted files +@cindex Modes of extracted files +To set the modes (access permissions) of extracted files to those +recorded for those files in the archive, use @option{--same-permissions} +in conjunction with the @option{--extract} (@option{--get}, +@option{-x}) operation. + +@table @option +@opindex preserve-permissions +@opindex same-permissions +@item --preserve-permissions +@itemx --same-permissions +@c @itemx --ignore-umask +@itemx -p +Set modes of extracted archive members to those recorded in the +archive, instead of current umask settings. Use in conjunction with +@option{--extract} (@option{--get}, @option{-x}). +@end table + +@node Directory Modification Times and Permissions +@unnumberedsubsubsec Directory Modification Times and Permissions + +After successfully extracting a file member, @GNUTAR{} normally +restores its permissions and modification times, as described in the +previous sections. This cannot be done for directories, because +after extracting a directory @command{tar} will almost certainly +extract files into that directory and this will cause the directory +modification time to be updated. Moreover, restoring that directory +permissions may not permit file creation within it. Thus, restoring +directory permissions and modification times must be delayed at least +until all files have been extracted into that directory. @GNUTAR{} +restores directories using the following approach. + +The extracted directories are created with the mode specified in the +archive, as modified by the umask of the user, which gives sufficient +permissions to allow file creation. The meta-information about the +directory is recorded in the temporary list of directories. When +preparing to extract next archive member, @GNUTAR{} checks if the +directory prefix of this file contains the remembered directory. If +it does not, the program assumes that all files have been extracted +into that directory, restores its modification time and permissions +and removes its entry from the internal list. This approach allows +to correctly restore directory meta-information in the majority of +cases, while keeping memory requirements sufficiently small. It is +based on the fact, that most @command{tar} archives use the predefined +order of members: first the directory, then all the files and +subdirectories in that directory. + +However, this is not always true. The most important exception are +incremental archives (@pxref{Incremental Dumps}). The member order in +an incremental archive is reversed: first all directory members are +stored, followed by other (non-directory) members. So, when extracting +from incremental archives, @GNUTAR{} alters the above procedure. It +remembers all restored directories, and restores their meta-data +only after the entire archive has been processed. Notice, that you do +not need to specify any special options for that, as @GNUTAR{} +automatically detects archives in incremental format. + +There may be cases, when such processing is required for normal archives +too. Consider the following example: + +@smallexample +@group +$ @kbd{tar --no-recursion -cvf archive \ + foo foo/file1 bar bar/file foo/file2} +foo/ +foo/file1 +bar/ +bar/file +foo/file2 +@end group +@end smallexample + +During the normal operation, after encountering @file{bar} +@GNUTAR{} will assume that all files from the directory @file{foo} +were already extracted and will therefore restore its timestamp and +permission bits. However, after extracting @file{foo/file2} the +directory timestamp will be offset again. + +To correctly restore directory meta-information in such cases, use +the @option{--delay-directory-restore} command line option: + +@table @option +@opindex delay-directory-restore +@item --delay-directory-restore +Delays restoring of the modification times and permissions of extracted +directories until the end of extraction. This way, correct +meta-information is restored even if the archive has unusual member +ordering. + +@opindex no-delay-directory-restore +@item --no-delay-directory-restore +Cancel the effect of the previous @option{--delay-directory-restore}. +Use this option if you have used @option{--delay-directory-restore} in +@env{TAR_OPTIONS} variable (@pxref{TAR_OPTIONS}) and wish to +temporarily disable it. +@end table + +@node Writing to Standard Output +@unnumberedsubsubsec Writing to Standard Output + +@cindex Writing extracted files to standard output +@cindex Standard output, writing extracted files to +To write the extracted files to the standard output, instead of +creating the files on the file system, use @option{--to-stdout} (@option{-O}) in +conjunction with @option{--extract} (@option{--get}, @option{-x}). This option is useful if you are +extracting files to send them through a pipe, and do not need to +preserve them in the file system. If you extract multiple members, +they appear on standard output concatenated, in the order they are +found in the archive. + +@table @option +@opindex to-stdout +@item --to-stdout +@itemx -O +Writes files to the standard output. Use only in conjunction with +@option{--extract} (@option{--get}, @option{-x}). When this option is +used, instead of creating the files specified, @command{tar} writes +the contents of the files extracted to its standard output. This may +be useful if you are only extracting the files in order to send them +through a pipe. This option is meaningless with @option{--list} +(@option{-t}). +@end table + +This can be useful, for example, if you have a tar archive containing +a big file and don't want to store the file on disk before processing +it. You can use a command like this: + +@smallexample +tar -xOzf foo.tgz bigfile | process +@end smallexample + +or even like this if you want to process the concatenation of the files: + +@smallexample +tar -xOzf foo.tgz bigfile1 bigfile2 | process +@end smallexample + +However, @option{--to-command} may be more convenient for use with +multiple files. See the next section. + +@node Writing to an External Program +@unnumberedsubsubsec Writing to an External Program + +You can instruct @command{tar} to send the contents of each extracted +file to the standard input of an external program: + +@table @option +@opindex to-command +@item --to-command=@var{command} +Extract files and pipe their contents to the standard input of +@var{command}. When this option is used, instead of creating the +files specified, @command{tar} invokes @var{command} and pipes the +contents of the files to its standard output. The @var{command} may +contain command line arguments. The program is executed via +@code{sh -c}. Notice, that @var{command} is executed once for each regular file +extracted. Non-regular files (directories, etc.) are ignored when this +option is used. +@end table + +The command can obtain the information about the file it processes +from the following environment variables: + +@table @env +@vrindex TAR_FILETYPE, to-command environment +@item TAR_FILETYPE +Type of the file. It is a single letter with the following meaning: + +@multitable @columnfractions 0.10 0.90 +@item f @tab Regular file +@item d @tab Directory +@item l @tab Symbolic link +@item h @tab Hard link +@item b @tab Block device +@item c @tab Character device +@end multitable + +Currently only regular files are supported. + +@vrindex TAR_MODE, to-command environment +@item TAR_MODE +File mode, an octal number. + +@vrindex TAR_FILENAME, to-command environment +@item TAR_FILENAME +The name of the file. + +@vrindex TAR_REALNAME, to-command environment +@item TAR_REALNAME +Name of the file as stored in the archive. + +@vrindex TAR_UNAME, to-command environment +@item TAR_UNAME +Name of the file owner. + +@vrindex TAR_GNAME, to-command environment +@item TAR_GNAME +Name of the file owner group. + +@vrindex TAR_ATIME, to-command environment +@item TAR_ATIME +Time of last access. It is a decimal number, representing seconds +since the Epoch. If the archive provides times with nanosecond +precision, the nanoseconds are appended to the timestamp after a +decimal point. + +@vrindex TAR_MTIME, to-command environment +@item TAR_MTIME +Time of last modification. + +@vrindex TAR_CTIME, to-command environment +@item TAR_CTIME +Time of last status change. + +@vrindex TAR_SIZE, to-command environment +@item TAR_SIZE +Size of the file. + +@vrindex TAR_UID, to-command environment +@item TAR_UID +UID of the file owner. + +@vrindex TAR_GID, to-command environment +@item TAR_GID +GID of the file owner. +@end table + +Additionally, the following variables contain information about +tar mode and the archive being processed: + +@table @env +@vrindex TAR_VERSION, to-command environment +@item TAR_VERSION +@GNUTAR{} version number. + +@vrindex TAR_ARCHIVE, to-command environment +@item TAR_ARCHIVE +The name of the archive @command{tar} is processing. + +@vrindex TAR_BLOCKING_FACTOR, to-command environment +@item TAR_BLOCKING_FACTOR +Current blocking factor (@pxref{Blocking}). + +@vrindex TAR_VOLUME, to-command environment +@item TAR_VOLUME +Ordinal number of the volume @command{tar} is processing. + +@vrindex TAR_FORMAT, to-command environment +@item TAR_FORMAT +Format of the archive being processed. @xref{Formats}, for a complete +list of archive format names. +@end table + +If @var{command} exits with a non-0 status, @command{tar} will print +an error message similar to the following: + +@smallexample +tar: 2345: Child returned status 1 +@end smallexample + +Here, @samp{2345} is the PID of the finished process. + +If this behavior is not wanted, use @option{--ignore-command-error}: + +@table @option +@opindex ignore-command-error +@item --ignore-command-error +Ignore exit codes of subprocesses. Notice that if the program +exits on signal or otherwise terminates abnormally, the error message +will be printed even if this option is used. + +@opindex no-ignore-command-error +@item --no-ignore-command-error +Cancel the effect of any previous @option{--ignore-command-error} +option. This option is useful if you have set +@option{--ignore-command-error} in @env{TAR_OPTIONS} +(@pxref{TAR_OPTIONS}) and wish to temporarily cancel it. +@end table + +@node remove files +@unnumberedsubsubsec Removing Files + +@FIXME{The section is too terse. Something more to add? An example, +maybe?} + +@table @option +@opindex remove-files +@item --remove-files +Remove files after adding them to the archive. +@end table + +@node Scarce +@subsection Coping with Scarce Resources +@UNREVISED + +@cindex Small memory +@cindex Running out of space + +@menu +* Starting File:: +* Same Order:: +@end menu + +@node Starting File +@unnumberedsubsubsec Starting File + +@table @option +@opindex starting-file +@item --starting-file=@var{name} +@itemx -K @var{name} +Starts an operation in the middle of an archive. Use in conjunction +with @option{--extract} (@option{--get}, @option{-x}) or @option{--list} (@option{-t}). +@end table + +@cindex Middle of the archive, starting in the +If a previous attempt to extract files failed due to lack of disk +space, you can use @option{--starting-file=@var{name}} (@option{-K +@var{name}}) to start extracting only after member @var{name} of the +archive. This assumes, of course, that there is now free space, or +that you are now extracting into a different file system. (You could +also choose to suspend @command{tar}, remove unnecessary files from +the file system, and then resume the same @command{tar} operation. +In this case, @option{--starting-file} is not necessary.) See also +@ref{interactive}, and @ref{exclude}. + +@node Same Order +@unnumberedsubsubsec Same Order + +@table @option +@cindex Large lists of file names on small machines +@opindex same-order +@opindex preserve-order +@item --same-order +@itemx --preserve-order +@itemx -s +To process large lists of file names on machines with small amounts of +memory. Use in conjunction with @option{--compare} (@option{--diff}, +@option{-d}), @option{--list} (@option{-t}) or @option{--extract} +(@option{--get}, @option{-x}). +@end table + +The @option{--same-order} (@option{--preserve-order}, @option{-s}) option tells @command{tar} that the list of file +names to be listed or extracted is sorted in the same order as the +files in the archive. This allows a large list of names to be used, +even on a small machine that would not otherwise be able to hold all +the names in memory at the same time. Such a sorted list can easily be +created by running @samp{tar -t} on the archive and editing its output. + +This option is probably never needed on modern computer systems. + +@node backup +@section Backup options + +@cindex backup options + +@GNUTAR{} offers options for making backups of files +before writing new versions. These options control the details of +these backups. They may apply to the archive itself before it is +created or rewritten, as well as individual extracted members. Other +@acronym{GNU} programs (@command{cp}, @command{install}, @command{ln}, +and @command{mv}, for example) offer similar options. + +Backup options may prove unexpectedly useful when extracting archives +containing many members having identical name, or when extracting archives +on systems having file name limitations, making different members appear +as having similar names through the side-effect of name truncation. +@FIXME{This is true only if we have a good scheme for truncated backup names, +which I'm not sure at all: I suspect work is needed in this area.} +When any existing file is backed up before being overwritten by extraction, +then clashing files are automatically be renamed to be unique, and the +true name is kept for only the last file of a series of clashing files. +By using verbose mode, users may track exactly what happens. + +At the detail level, some decisions are still experimental, and may +change in the future, we are waiting comments from our users. So, please +do not learn to depend blindly on the details of the backup features. +For example, currently, directories themselves are never renamed through +using these options, so, extracting a file over a directory still has +good chances to fail. Also, backup options apply to created archives, +not only to extracted members. For created archives, backups will not +be attempted when the archive is a block or character device, or when it +refers to a remote file. + +For the sake of simplicity and efficiency, backups are made by renaming old +files prior to creation or extraction, and not by copying. The original +name is restored if the file creation fails. If a failure occurs after a +partial extraction of a file, both the backup and the partially extracted +file are kept. + +@table @samp +@item --backup[=@var{method}] +@opindex backup +@vindex VERSION_CONTROL +@cindex backups +Back up files that are about to be overwritten or removed. +Without this option, the original versions are destroyed. + +Use @var{method} to determine the type of backups made. +If @var{method} is not specified, use the value of the @env{VERSION_CONTROL} +environment variable. And if @env{VERSION_CONTROL} is not set, +use the @samp{existing} method. + +@vindex version-control @r{Emacs variable} +This option corresponds to the Emacs variable @samp{version-control}; +the same values for @var{method} are accepted as in Emacs. This option +also allows more descriptive names. The valid @var{method}s are: + +@table @samp +@item t +@itemx numbered +@cindex numbered @r{backup method} +Always make numbered backups. + +@item nil +@itemx existing +@cindex existing @r{backup method} +Make numbered backups of files that already have them, simple backups +of the others. + +@item never +@itemx simple +@cindex simple @r{backup method} +Always make simple backups. + +@end table + +@item --suffix=@var{suffix} +@opindex suffix +@cindex backup suffix +@vindex SIMPLE_BACKUP_SUFFIX +Append @var{suffix} to each backup file made with @option{--backup}. If this +option is not specified, the value of the @env{SIMPLE_BACKUP_SUFFIX} +environment variable is used. And if @env{SIMPLE_BACKUP_SUFFIX} is not +set, the default is @samp{~}, just as in Emacs. + +@end table + +@node Applications +@section Notable @command{tar} Usages +@UNREVISED + +@FIXME{Using Unix file linking capability to recreate directory +structures---linking files into one subdirectory and then +@command{tar}ring that directory.} + +@FIXME{Nice hairy example using absolute-names, newer, etc.} + +@findex uuencode +You can easily use archive files to transport a group of files from +one system to another: put all relevant files into an archive on one +computer system, transfer the archive to another system, and extract +the contents there. The basic transfer medium might be magnetic tape, +Internet FTP, or even electronic mail (though you must encode the +archive with @command{uuencode} in order to transport it properly by +mail). Both machines do not have to use the same operating system, as +long as they both support the @command{tar} program. + +For example, here is how you might copy a directory's contents from +one disk to another, while preserving the dates, modes, owners and +link-structure of all the files therein. In this case, the transfer +medium is a @dfn{pipe}: + +@smallexample +$ @kbd{(cd sourcedir; tar -cf - .) | (cd targetdir; tar -xf -)} +@end smallexample + +@noindent +You can avoid subshells by using @option{-C} option: + +@smallexample +$ @kbd{tar -C sourcedir -cf - . | tar -C targetdir -xf -} +@end smallexample + +@noindent +The command also works using long option forms: + +@smallexample +@group +$ @kbd{(cd sourcedir; tar --create --file=- . ) \ + | (cd targetdir; tar --extract --file=-)} +@end group +@end smallexample + +@noindent +or + +@smallexample +@group +$ @kbd{tar --directory sourcedir --create --file=- . \ + | tar --directory targetdir --extract --file=-} +@end group +@end smallexample + +@noindent +This is one of the easiest methods to transfer a @command{tar} archive. + +@node looking ahead +@section Looking Ahead: The Rest of this Manual + +You have now seen how to use all eight of the operations available to +@command{tar}, and a number of the possible options. The next chapter +explains how to choose and change file and archive names, how to use +files to store names of other files which you can then call as +arguments to @command{tar} (this can help you save time if you expect to +archive the same list of files a number of times), and so forth. +@FIXME{in case it's not obvious, i'm making this up in some sense +based on my limited memory of what the next chapter *really* does. i +just wanted to flesh out this final section a little bit so i'd +remember to stick it in here. :-)} + +If there are too many files to conveniently list on the command line, +you can list the names in a file, and @command{tar} will read that file. +@xref{files}. + +There are various ways of causing @command{tar} to skip over some files, +and not archive them. @xref{Choosing}. + +@node Backups +@chapter Performing Backups and Restoring Files +@cindex backups + +@GNUTAR{} is distributed along with the scripts for performing backups +and restores. Even if there is a good chance those scripts may be +satisfying to you, they are not the only scripts or methods available for doing +backups and restore. You may well create your own, or use more +sophisticated packages dedicated to that purpose. + +Some users are enthusiastic about @code{Amanda} (The Advanced Maryland +Automatic Network Disk Archiver), a backup system developed by James +da Silva @file{jds@@cs.umd.edu} and available on many Unix systems. +This is free software, and it is available from @uref{http://www.amanda.org}. + +@FIXME{ + +Here is a possible plan for a future documentation about the backuping +scripts which are provided within the @GNUTAR{} +distribution. + +@itemize @bullet +@item dumps + @itemize @minus + @item what are dumps + @item different levels of dumps + @itemize + + @item full dump = dump everything + @item level 1, level 2 dumps etc + A level @var{n} dump dumps everything changed since the last level + @var{n}-1 dump (?) + @end itemize + @item how to use scripts for dumps (ie, the concept) + @itemize + + @item scripts to run after editing backup specs (details) + @end itemize + @item Backup Specs, what is it. + @itemize + + @item how to customize + @item actual text of script [/sp/dump/backup-specs] + @end itemize + @item Problems + @itemize + + @item rsh doesn't work + @item rtape isn't installed + @item (others?) + @end itemize + @item the @option{--incremental} option of tar + @item tapes + @itemize + + @item write protection + @item types of media, different sizes and types, useful for different things + @item files and tape marks + one tape mark between files, two at end. + @item positioning the tape + MT writes two at end of write, + backspaces over one when writing again. + @end itemize + @end itemize +@end itemize +} + +This chapter documents both the provided shell scripts and @command{tar} +options which are more specific to usage as a backup tool. + +To @dfn{back up} a file system means to create archives that contain +all the files in that file system. Those archives can then be used to +restore any or all of those files (for instance if a disk crashes or a +file is accidentally deleted). File system @dfn{backups} are also +called @dfn{dumps}. + +@menu +* Full Dumps:: Using @command{tar} to Perform Full Dumps +* Incremental Dumps:: Using @command{tar} to Perform Incremental Dumps +* Backup Levels:: Levels of Backups +* Backup Parameters:: Setting Parameters for Backups and Restoration +* Scripted Backups:: Using the Backup Scripts +* Scripted Restoration:: Using the Restore Script +@end menu + +@node Full Dumps +@section Using @command{tar} to Perform Full Dumps +@UNREVISED + +@cindex full dumps +@cindex dumps, full + +@cindex corrupted archives +Full dumps should only be made when no other people or programs +are modifying files in the file system. If files are modified while +@command{tar} is making the backup, they may not be stored properly in +the archive, in which case you won't be able to restore them if you +have to. (Files not being modified are written with no trouble, and do +not corrupt the entire archive.) + +You will want to use the @option{--label=@var{archive-label}} +(@option{-V @var{archive-label}}) option to give the archive a +volume label, so you can tell what this archive is even if the label +falls off the tape, or anything like that. + +Unless the file system you are dumping is guaranteed to fit on +one volume, you will need to use the @option{--multi-volume} (@option{-M}) option. +Make sure you have enough tapes on hand to complete the backup. + +If you want to dump each file system separately you will need to use +the @option{--one-file-system} option to prevent +@command{tar} from crossing file system boundaries when storing +(sub)directories. + +The @option{--incremental} (@option{-G}) (@pxref{Incremental Dumps}) +option is not needed, since this is a complete copy of everything in +the file system, and a full restore from this backup would only be +done onto a completely +empty disk. + +Unless you are in a hurry, and trust the @command{tar} program (and your +tapes), it is a good idea to use the @option{--verify} (@option{-W}) +option, to make sure your files really made it onto the dump properly. +This will also detect cases where the file was modified while (or just +after) it was being archived. Not all media (notably cartridge tapes) +are capable of being verified, unfortunately. + +@node Incremental Dumps +@section Using @command{tar} to Perform Incremental Dumps + +@dfn{Incremental backup} is a special form of @GNUTAR{} archive that +stores additional metadata so that exact state of the file system +can be restored when extracting the archive. + +@GNUTAR{} currently offers two options for handling incremental +backups: @option{--listed-incremental=@var{snapshot-file}} (@option{-g +@var{snapshot-file}}) and @option{--incremental} (@option{-G}). + +@xopindex{listed-incremental, described} +The option @option{--listed-incremental} instructs tar to operate on +an incremental archive with additional metadata stored in a standalone +file, called a @dfn{snapshot file}. The purpose of this file is to help +determine which files have been changed, added or deleted since the +last backup, so that the next incremental backup will contain only +modified files. The name of the snapshot file is given as an argument +to the option: + +@table @option +@item --listed-incremental=@var{file} +@itemx -g @var{file} + Handle incremental backups with snapshot data in @var{file}. +@end table + +To create an incremental backup, you would use +@option{--listed-incremental} together with @option{--create} +(@pxref{create}). For example: + +@smallexample +$ @kbd{tar --create \ + --file=archive.1.tar \ + --listed-incremental=/var/log/usr.snar \ + /usr} +@end smallexample + +This will create in @file{archive.1.tar} an incremental backup of +the @file{/usr} file system, storing additional metadata in the file +@file{/var/log/usr.snar}. If this file does not exist, it will be +created. The created archive will then be a @dfn{level 0 backup}; +please see the next section for more on backup levels. + +Otherwise, if the file @file{/var/log/usr.snar} exists, it +determines which files are modified. In this case only these files will be +stored in the archive. Suppose, for example, that after running the +above command, you delete file @file{/usr/doc/old} and create +directory @file{/usr/local/db} with the following contents: + +@smallexample +$ @kbd{ls /usr/local/db} +/usr/local/db/data +/usr/local/db/index +@end smallexample + +Some time later you create another incremental backup. You will +then see: + +@smallexample +$ @kbd{tar --create \ + --file=archive.2.tar \ + --listed-incremental=/var/log/usr.snar \ + /usr} +tar: usr/local/db: Directory is new +usr/local/db/ +usr/local/db/data +usr/local/db/index +@end smallexample + +@noindent +The created archive @file{archive.2.tar} will contain only these +three members. This archive is called a @dfn{level 1 backup}. Notice +that @file{/var/log/usr.snar} will be updated with the new data, so if +you plan to create more @samp{level 1} backups, it is necessary to +create a working copy of the snapshot file before running +@command{tar}. The above example will then be modified as follows: + +@smallexample +$ @kbd{cp /var/log/usr.snar /var/log/usr.snar-1} +$ @kbd{tar --create \ + --file=archive.2.tar \ + --listed-incremental=/var/log/usr.snar-1 \ + /usr} +@end smallexample + +@anchor{--level=0} +@xopindex{level, described} +You can force @samp{level 0} backups either by removing the snapshot +file before running @command{tar}, or by supplying the +@option{--level=0} option, e.g.: + +@smallexample +$ @kbd{tar --create \ + --file=archive.2.tar \ + --listed-incremental=/var/log/usr.snar-0 \ + --level=0 \ + /usr} +@end smallexample + +Incremental dumps depend crucially on time stamps, so the results are +unreliable if you modify a file's time stamps during dumping (e.g., +with the @option{--atime-preserve=replace} option), or if you set the clock +backwards. + +@anchor{device numbers} +@cindex Device numbers, using in incremental backups +Metadata stored in snapshot files include device numbers, which, +obviously are supposed to be non-volatile values. However, it turns +out that @acronym{NFS} devices have undependable values when an automounter +gets in the picture. This can lead to a great deal of spurious +redumping in incremental dumps, so it is somewhat useless to compare +two @acronym{NFS} devices numbers over time. The solution implemented +currently is to consider all @acronym{NFS} devices as being equal +when it comes to comparing directories; this is fairly gross, but +there does not seem to be a better way to go. + +Apart from using @acronym{NFS}, there are a number of cases where +relying on device numbers can cause spurious redumping of unmodified +files. For example, this occurs when archiving @acronym{LVM} snapshot +volumes. To avoid this, use @option{--no-check-device} option: + +@table @option +@xopindex{no-check-device, described} +@item --no-check-device +Do not rely on device numbers when preparing a list of changed files +for an incremental dump. + +@xopindex{check-device, described} +@item --check-device +Use device numbers when preparing a list of changed files +for an incremental dump. This is the default behavior. The purpose +of this option is to undo the effect of the @option{--no-check-device} +if it was given in @env{TAR_OPTIONS} environment variable +(@pxref{TAR_OPTIONS}). +@end table + +There is also another way to cope with changing device numbers. It is +described in detail in @ref{Fixing Snapshot Files}. + +Note that incremental archives use @command{tar} extensions and may +not be readable by non-@acronym{GNU} versions of the @command{tar} program. + +@xopindex{listed-incremental, using with @option{--extract}} +@xopindex{extract, using with @option{--listed-incremental}} +To extract from the incremental dumps, use +@option{--listed-incremental} together with @option{--extract} +option (@pxref{extracting files}). In this case, @command{tar} does +not need to access snapshot file, since all the data necessary for +extraction are stored in the archive itself. So, when extracting, you +can give whatever argument to @option{--listed-incremental}, the usual +practice is to use @option{--listed-incremental=/dev/null}. +Alternatively, you can use @option{--incremental}, which needs no +arguments. In general, @option{--incremental} (@option{-G}) can be +used as a shortcut for @option{--listed-incremental} when listing or +extracting incremental backups (for more information regarding this +option, @pxref{incremental-op}). + +When extracting from the incremental backup @GNUTAR{} attempts to +restore the exact state the file system had when the archive was +created. In particular, it will @emph{delete} those files in the file +system that did not exist in their directories when the archive was +created. If you have created several levels of incremental files, +then in order to restore the exact contents the file system had when +the last level was created, you will need to restore from all backups +in turn. Continuing our example, to restore the state of @file{/usr} +file system, one would do@footnote{Notice, that since both archives +were created without @option{-P} option (@pxref{absolute}), these +commands should be run from the root file system.}: + +@smallexample +$ @kbd{tar --extract \ + --listed-incremental=/dev/null \ + --file archive.1.tar} +$ @kbd{tar --extract \ + --listed-incremental=/dev/null \ + --file archive.2.tar} +@end smallexample + +To list the contents of an incremental archive, use @option{--list} +(@pxref{list}), as usual. To obtain more information about the +archive, use @option{--listed-incremental} or @option{--incremental} +combined with two @option{--verbose} options@footnote{Two +@option{--verbose} options were selected to avoid breaking usual +verbose listing output (@option{--list --verbose}) when using in +scripts. + +@xopindex{incremental, using with @option{--list}} +@xopindex{listed-incremental, using with @option{--list}} +@xopindex{list, using with @option{--incremental}} +@xopindex{list, using with @option{--listed-incremental}} +Versions of @GNUTAR{} up to 1.15.1 used to dump verbatim binary +contents of the DUMPDIR header (with terminating nulls) when +@option{--incremental} or @option{--listed-incremental} option was +given, no matter what the verbosity level. This behavior, and, +especially, the binary output it produced were considered inconvenient +and were changed in version 1.16.}: + +@smallexample +@kbd{tar --list --incremental --verbose --verbose archive.tar} +@end smallexample + +This command will print, for each directory in the archive, the list +of files in that directory at the time the archive was created. This +information is put out in a format which is both human-readable and +unambiguous for a program: each file name is printed as + +@smallexample +@var{x} @var{file} +@end smallexample + +@noindent +where @var{x} is a letter describing the status of the file: @samp{Y} +if the file is present in the archive, @samp{N} if the file is not +included in the archive, or a @samp{D} if the file is a directory (and +is included in the archive). @xref{Dumpdir}, for the detailed +description of dumpdirs and status codes. Each such +line is terminated by a newline character. The last line is followed +by an additional newline to indicate the end of the data. + +@anchor{incremental-op}The option @option{--incremental} (@option{-G}) +gives the same behavior as @option{--listed-incremental} when used +with @option{--list} and @option{--extract} options. When used with +@option{--create} option, it creates an incremental archive without +creating snapshot file. Thus, it is impossible to create several +levels of incremental backups with @option{--incremental} option. + +@node Backup Levels +@section Levels of Backups + +An archive containing all the files in the file system is called a +@dfn{full backup} or @dfn{full dump}. You could insure your data by +creating a full dump every day. This strategy, however, would waste a +substantial amount of archive media and user time, as unchanged files +are daily re-archived. + +It is more efficient to do a full dump only occasionally. To back up +files between full dumps, you can use @dfn{incremental dumps}. A @dfn{level +one} dump archives all the files that have changed since the last full +dump. + +A typical dump strategy would be to perform a full dump once a week, +and a level one dump once a day. This means some versions of files +will in fact be archived more than once, but this dump strategy makes +it possible to restore a file system to within one day of accuracy by +only extracting two archives---the last weekly (full) dump and the +last daily (level one) dump. The only information lost would be in +files changed or created since the last daily backup. (Doing dumps +more than once a day is usually not worth the trouble.) + +@GNUTAR{} comes with scripts you can use to do full +and level-one (actually, even level-two and so on) dumps. Using +scripts (shell programs) to perform backups and restoration is a +convenient and reliable alternative to typing out file name lists +and @command{tar} commands by hand. + +Before you use these scripts, you need to edit the file +@file{backup-specs}, which specifies parameters used by the backup +scripts and by the restore script. This file is usually located +in @file{/etc/backup} directory. @xref{Backup Parameters}, for its +detailed description. Once the backup parameters are set, you can +perform backups or restoration by running the appropriate script. + +The name of the backup script is @code{backup}. The name of the +restore script is @code{restore}. The following sections describe +their use in detail. + +@emph{Please Note:} The backup and restoration scripts are +designed to be used together. While it is possible to restore files by +hand from an archive which was created using a backup script, and to create +an archive by hand which could then be extracted using the restore script, +it is easier to use the scripts. @xref{Incremental Dumps}, before +making such an attempt. + +@node Backup Parameters +@section Setting Parameters for Backups and Restoration + +The file @file{backup-specs} specifies backup parameters for the +backup and restoration scripts provided with @command{tar}. You must +edit @file{backup-specs} to fit your system configuration and schedule +before using these scripts. + +Syntactically, @file{backup-specs} is a shell script, containing +mainly variable assignments. However, any valid shell construct +is allowed in this file. Particularly, you may wish to define +functions within that script (e.g., see @code{RESTORE_BEGIN} below). +For more information about shell script syntax, please refer to +@url{http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#ta +g_02, the definition of the Shell Command Language}. See also +@ref{Top,,Bash Features,bashref,Bash Reference Manual}. + +The shell variables controlling behavior of @code{backup} and +@code{restore} are described in the following subsections. + +@menu +* General-Purpose Variables:: +* Magnetic Tape Control:: +* User Hooks:: +* backup-specs example:: An Example Text of @file{Backup-specs} +@end menu + +@node General-Purpose Variables +@subsection General-Purpose Variables + +@defvr {Backup variable} ADMINISTRATOR +The user name of the backup administrator. @code{Backup} scripts +sends a backup report to this address. +@end defvr + +@defvr {Backup variable} BACKUP_HOUR +The hour at which the backups are done. This can be a number from 0 +to 23, or the time specification in form @var{hours}:@var{minutes}, +or the string @samp{now}. + +This variable is used by @code{backup}. Its value may be overridden +using @option{--time} option (@pxref{Scripted Backups}). +@end defvr + +@defvr {Backup variable} TAPE_FILE + +The device @command{tar} writes the archive to. If @var{TAPE_FILE} +is a remote archive (@pxref{remote-dev}), backup script will suppose +that your @command{mt} is able to access remote devices. If @var{RSH} +(@pxref{RSH}) is set, @option{--rsh-command} option will be added to +invocations of @command{mt}. +@end defvr + +@defvr {Backup variable} BLOCKING + +The blocking factor @command{tar} will use when writing the dump archive. +@xref{Blocking Factor}. +@end defvr + +@defvr {Backup variable} BACKUP_DIRS + +A list of file systems to be dumped (for @code{backup}), or restored +(for @code{restore}). You can include any directory +name in the list --- subdirectories on that file system will be +included, regardless of how they may look to other networked machines. +Subdirectories on other file systems will be ignored. + +The host name specifies which host to run @command{tar} on, and should +normally be the host that actually contains the file system. However, +the host machine must have @GNUTAR{} installed, and +must be able to access the directory containing the backup scripts and +their support files using the same file name that is used on the +machine where the scripts are run (i.e., what @command{pwd} will print +when in that directory on that machine). If the host that contains +the file system does not have this capability, you can specify another +host as long as it can access the file system through @acronym{NFS}. + +If the list of file systems is very long you may wish to put it +in a separate file. This file is usually named +@file{/etc/backup/dirs}, but this name may be overridden in +@file{backup-specs} using @code{DIRLIST} variable. +@end defvr + +@defvr {Backup variable} DIRLIST + +The name of the file that contains a list of file systems to backup +or restore. By default it is @file{/etc/backup/dirs}. +@end defvr + +@defvr {Backup variable} BACKUP_FILES + +A list of individual files to be dumped (for @code{backup}), or restored +(for @code{restore}). These should be accessible from the machine on +which the backup script is run. + +If the list of individual files is very long you may wish to store it +in a separate file. This file is usually named +@file{/etc/backup/files}, but this name may be overridden in +@file{backup-specs} using @code{FILELIST} variable. +@end defvr + +@defvr {Backup variable} FILELIST + +The name of the file that contains a list of individual files to backup +or restore. By default it is @file{/etc/backup/files}. +@end defvr + +@defvr {Backup variable} MT + +Full file name of @command{mt} binary. +@end defvr + +@defvr {Backup variable} RSH +@anchor{RSH} +Full file name of @command{rsh} binary or its equivalent. You may wish to +set it to @code{ssh}, to improve security. In this case you will have +to use public key authentication. +@end defvr + +@defvr {Backup variable} RSH_COMMAND + +Full file name of @command{rsh} binary on remote machines. This will +be passed via @option{--rsh-command} option to the remote invocation +of @GNUTAR{}. +@end defvr + +@defvr {Backup variable} VOLNO_FILE + +Name of temporary file to hold volume numbers. This needs to be accessible +by all the machines which have file systems to be dumped. +@end defvr + +@defvr {Backup variable} XLIST + +Name of @dfn{exclude file list}. An @dfn{exclude file list} is a file +located on the remote machine and containing the list of files to +be excluded from the backup. Exclude file lists are searched in +/etc/tar-backup directory. A common use for exclude file lists +is to exclude files containing security-sensitive information +(e.g., @file{/etc/shadow} from backups). + +This variable affects only @code{backup}. +@end defvr + +@defvr {Backup variable} SLEEP_TIME + +Time to sleep between dumps of any two successive file systems + +This variable affects only @code{backup}. +@end defvr + +@defvr {Backup variable} DUMP_REMIND_SCRIPT + +Script to be run when it's time to insert a new tape in for the next +volume. Administrators may want to tailor this script for their site. +If this variable isn't set, @GNUTAR{} will display its built-in +prompt, and will expect confirmation from the console. For the +description of the default prompt, see @ref{change volume prompt}. + +@end defvr + +@defvr {Backup variable} SLEEP_MESSAGE + +Message to display on the terminal while waiting for dump time. Usually +this will just be some literal text. +@end defvr + +@defvr {Backup variable} TAR + +Full file name of the @GNUTAR{} executable. If this is not set, backup +scripts will search @command{tar} in the current shell path. +@end defvr + +@node Magnetic Tape Control +@subsection Magnetic Tape Control + +Backup scripts access tape device using special @dfn{hook functions}. +These functions take a single argument --- the name of the tape +device. Their names are kept in the following variables: + +@defvr {Backup variable} MT_BEGIN +The name of @dfn{begin} function. This function is called before +accessing the drive. By default it retensions the tape: + +@smallexample +MT_BEGIN=mt_begin + +mt_begin() @{ + mt -f "$1" retension +@} +@end smallexample +@end defvr + +@defvr {Backup variable} MT_REWIND +The name of @dfn{rewind} function. The default definition is as +follows: + +@smallexample +MT_REWIND=mt_rewind + +mt_rewind() @{ + mt -f "$1" rewind +@} +@end smallexample + +@end defvr + +@defvr {Backup variable} MT_OFFLINE +The name of the function switching the tape off line. By default +it is defined as follows: + +@smallexample +MT_OFFLINE=mt_offline + +mt_offline() @{ + mt -f "$1" offl +@} +@end smallexample +@end defvr + +@defvr {Backup variable} MT_STATUS +The name of the function used to obtain the status of the archive device, +including error count. Default definition: + +@smallexample +MT_STATUS=mt_status + +mt_status() @{ + mt -f "$1" status +@} +@end smallexample +@end defvr + +@node User Hooks +@subsection User Hooks + +@dfn{User hooks} are shell functions executed before and after +each @command{tar} invocation. Thus, there are @dfn{backup +hooks}, which are executed before and after dumping each file +system, and @dfn{restore hooks}, executed before and +after restoring a file system. Each user hook is a shell function +taking four arguments: + +@deffn {User Hook Function} hook @var{level} @var{host} @var{fs} @var{fsname} +Its arguments are: + +@table @var +@item level +Current backup or restore level. + +@item host +Name or IP address of the host machine being dumped or restored. + +@item fs +Full file name of the file system being dumped or restored. + +@item fsname +File system name with directory separators replaced with colons. This +is useful, e.g., for creating unique files. +@end table +@end deffn + +Following variables keep the names of user hook functions: + +@defvr {Backup variable} DUMP_BEGIN +Dump begin function. It is executed before dumping the file system. +@end defvr + +@defvr {Backup variable} DUMP_END +Executed after dumping the file system. +@end defvr + +@defvr {Backup variable} RESTORE_BEGIN +Executed before restoring the file system. +@end defvr + +@defvr {Backup variable} RESTORE_END +Executed after restoring the file system. +@end defvr + +@node backup-specs example +@subsection An Example Text of @file{Backup-specs} + +The following is an example of @file{backup-specs}: + +@smallexample +# site-specific parameters for file system backup. + +ADMINISTRATOR=friedman +BACKUP_HOUR=1 +TAPE_FILE=/dev/nrsmt0 + +# Use @code{ssh} instead of the less secure @code{rsh} +RSH=/usr/bin/ssh +RSH_COMMAND=/usr/bin/ssh + +# Override MT_STATUS function: +my_status() @{ + mts -t $TAPE_FILE +@} +MT_STATUS=my_status + +# Disable MT_OFFLINE function +MT_OFFLINE=: + +BLOCKING=124 +BACKUP_DIRS=" + albert:/fs/fsf + apple-gunkies:/gd + albert:/fs/gd2 + albert:/fs/gp + geech:/usr/jla + churchy:/usr/roland + albert:/ + albert:/usr + apple-gunkies:/ + apple-gunkies:/usr + gnu:/hack + gnu:/u + apple-gunkies:/com/mailer/gnu + apple-gunkies:/com/archive/gnu" + +BACKUP_FILES="/com/mailer/aliases /com/mailer/league*[a-z]" + +@end smallexample + +@node Scripted Backups +@section Using the Backup Scripts + +The syntax for running a backup script is: + +@smallexample +backup --level=@var{level} --time=@var{time} +@end smallexample + +The @option{--level} option requests the dump level. Thus, to produce +a full dump, specify @code{--level=0} (this is the default, so +@option{--level} may be omitted if its value is +@code{0})@footnote{For backward compatibility, the @code{backup} will also +try to deduce the requested dump level from the name of the +script itself. If the name consists of a string @samp{level-} +followed by a single decimal digit, that digit is taken as +the dump level number. Thus, you may create a link from @code{backup} +to @code{level-1} and then run @code{level-1} whenever you need to +create a level one dump.}. + +The @option{--time} option determines when should the backup be +run. @var{Time} may take three forms: + +@table @asis +@item @var{hh}:@var{mm} + +The dump must be run at @var{hh} hours @var{mm} minutes. + +@item @var{hh} + +The dump must be run at @var{hh} hours. + +@item now + +The dump must be run immediately. +@end table + +You should start a script with a tape or disk mounted. Once you +start a script, it prompts you for new tapes or disks as it +needs them. Media volumes don't have to correspond to archive +files --- a multi-volume archive can be started in the middle of a +tape that already contains the end of another multi-volume archive. +The @code{restore} script prompts for media by its archive volume, +so to avoid an error message you should keep track of which tape +(or disk) contains which volume of the archive (@pxref{Scripted +Restoration}). + +The backup scripts write two files on the file system. The first is a +record file in @file{/etc/tar-backup/}, which is used by the scripts +to store and retrieve information about which files were dumped. This +file is not meant to be read by humans, and should not be deleted by +them. @xref{Snapshot Files}, for a more detailed explanation of this +file. + +The second file is a log file containing the names of the file systems +and files dumped, what time the backup was made, and any error +messages that were generated, as well as how much space was left in +the media volume after the last volume of the archive was written. +You should check this log file after every backup. The file name is +@file{log-@var{mm-dd-yyyy}-level-@var{n}}, where @var{mm-dd-yyyy} +represents current date, and @var{n} represents current dump level number. + +The script also prints the name of each system being dumped to the +standard output. + +Following is the full list of options accepted by @code{backup} +script: + +@table @option +@item -l @var{level} +@itemx --level=@var{level} +Do backup level @var{level} (default 0). + +@item -f +@itemx --force +Force backup even if today's log file already exists. + +@item -v[@var{level}] +@itemx --verbose[=@var{level}] +Set verbosity level. The higher the level is, the more debugging +information will be output during execution. Default @var{level} +is 100, which means the highest debugging level. + +@item -t @var{start-time} +@itemx --time=@var{start-time} +Wait till @var{time}, then do backup. + +@item -h +@itemx --help +Display short help message and exit. + +@item -V +@itemx --version +Display information about the program's name, version, origin and legal +status, all on standard output, and then exit successfully. +@end table + + +@node Scripted Restoration +@section Using the Restore Script + +To restore files that were archived using a scripted backup, use the +@code{restore} script. Its usage is quite straightforward. In the +simplest form, invoke @code{restore --all}, it will +then restore all the file systems and files specified in +@file{backup-specs} (@pxref{General-Purpose Variables,BACKUP_DIRS}). + +You may select the file systems (and/or files) to restore by +giving @code{restore} a list of @dfn{patterns} in its command +line. For example, running + +@smallexample +restore 'albert:*' +@end smallexample + +@noindent +will restore all file systems on the machine @samp{albert}. A more +complicated example: + +@smallexample +restore 'albert:*' '*:/var' +@end smallexample + +@noindent +This command will restore all file systems on the machine @samp{albert} +as well as @file{/var} file system on all machines. + +By default @code{restore} will start restoring files from the lowest +available dump level (usually zero) and will continue through +all available dump levels. There may be situations where such a +thorough restore is not necessary. For example, you may wish to +restore only files from the recent level one backup. To do so, +use @option{--level} option, as shown in the example below: + +@smallexample +restore --level=1 +@end smallexample + +The full list of options accepted by @code{restore} follows: + +@table @option +@item -a +@itemx --all +Restore all file systems and files specified in @file{backup-specs}. + +@item -l @var{level} +@itemx --level=@var{level} +Start restoring from the given backup level, instead of the default 0. + +@item -v[@var{level}] +@itemx --verbose[=@var{level}] +Set verbosity level. The higher the level is, the more debugging +information will be output during execution. Default @var{level} +is 100, which means the highest debugging level. + +@item -h +@itemx --help +Display short help message and exit. + +@item -V +@itemx --version +Display information about the program's name, version, origin and legal +status, all on standard output, and then exit successfully. +@end table + +You should start the restore script with the media containing the +first volume of the archive mounted. The script will prompt for other +volumes as they are needed. If the archive is on tape, you don't need +to rewind the tape to to its beginning---if the tape head is +positioned past the beginning of the archive, the script will rewind +the tape as needed. @xref{Tape Positioning}, for a discussion of tape +positioning. + +@quotation +@strong{Warning:} The script will delete files from the active file +system if they were not in the file system when the archive was made. +@end quotation + +@xref{Incremental Dumps}, for an explanation of how the script makes +that determination. + +@node Choosing +@chapter Choosing Files and Names for @command{tar} + +Certain options to @command{tar} enable you to specify a name for your +archive. Other options let you decide which files to include or exclude +from the archive, based on when or whether files were modified, whether +the file names do or don't match specified patterns, or whether files +are in specified directories. + +This chapter discusses these options in detail. + +@menu +* file:: Choosing the Archive's Name +* Selecting Archive Members:: +* files:: Reading Names from a File +* exclude:: Excluding Some Files +* wildcards:: Wildcards Patterns and Matching +* quoting styles:: Ways of Quoting Special Characters in Names +* transform:: Modifying File and Member Names +* after:: Operating Only on New Files +* recurse:: Descending into Directories +* one:: Crossing File System Boundaries +@end menu + +@node file +@section Choosing and Naming Archive Files + +@cindex Naming an archive +@cindex Archive Name +@cindex Choosing an archive file +@cindex Where is the archive? +@opindex file +By default, @command{tar} uses an archive file name that was compiled when +it was built on the system; usually this name refers to some physical +tape drive on the machine. However, the person who installed @command{tar} +on the system may not have set the default to a meaningful value as far as +most users are concerned. As a result, you will usually want to tell +@command{tar} where to find (or create) the archive. The +@option{--file=@var{archive-name}} (@option{-f @var{archive-name}}) +option allows you to either specify or name a file to use as the archive +instead of the default archive file location. + +@table @option +@xopindex{file, short description} +@item --file=@var{archive-name} +@itemx -f @var{archive-name} +Name the archive to create or operate on. Use in conjunction with +any operation. +@end table + +For example, in this @command{tar} command, + +@smallexample +$ @kbd{tar -cvf collection.tar blues folk jazz} +@end smallexample + +@noindent +@file{collection.tar} is the name of the archive. It must directly +follow the @option{-f} option, since whatever directly follows @option{-f} +@emph{will} end up naming the archive. If you neglect to specify an +archive name, you may end up overwriting a file in the working directory +with the archive you create since @command{tar} will use this file's name +for the archive name. + +An archive can be saved as a file in the file system, sent through a +pipe or over a network, or written to an I/O device such as a tape, +floppy disk, or CD write drive. + +@cindex Writing new archives +@cindex Archive creation +If you do not name the archive, @command{tar} uses the value of the +environment variable @env{TAPE} as the file name for the archive. If +that is not available, @command{tar} uses a default, compiled-in archive +name, usually that for tape unit zero (i.e., @file{/dev/tu00}). + +@cindex Standard input and output +@cindex tar to standard input and output +If you use @file{-} as an @var{archive-name}, @command{tar} reads the +archive from standard input (when listing or extracting files), or +writes it to standard output (when creating an archive). If you use +@file{-} as an @var{archive-name} when modifying an archive, +@command{tar} reads the original archive from its standard input and +writes the entire new archive to its standard output. + +The following example is a convenient way of copying directory +hierarchy from @file{sourcedir} to @file{targetdir}. + +@smallexample +$ @kbd{(cd sourcedir; tar -cf - .) | (cd targetdir; tar -xpf -)} +@end smallexample + +The @option{-C} option allows to avoid using subshells: + +@smallexample +$ @kbd{tar -C sourcedir -cf - . | tar -C targetdir -xpf -} +@end smallexample + +In both examples above, the leftmost @command{tar} invocation archives +the contents of @file{sourcedir} to the standard output, while the +rightmost one reads this archive from its standard input and +extracts it. The @option{-p} option tells it to restore permissions +of the extracted files. + +@cindex Remote devices +@cindex tar to a remote device +@anchor{remote-dev} +To specify an archive file on a device attached to a remote machine, +use the following: + +@smallexample +@kbd{--file=@var{hostname}:/@var{dev}/@var{file-name}} +@end smallexample + +@noindent +@command{tar} will set up the remote connection, if possible, and +prompt you for a username and password. If you use +@option{--file=@@@var{hostname}:/@var{dev}/@var{file-name}}, @command{tar} +will attempt to set up the remote connection using your username +as the username on the remote machine. + +@cindex Local and remote archives +@anchor{local and remote archives} +If the archive file name includes a colon (@samp{:}), then it is assumed +to be a file on another machine. If the archive file is +@samp{@var{user}@@@var{host}:@var{file}}, then @var{file} is used on the +host @var{host}. The remote host is accessed using the @command{rsh} +program, with a username of @var{user}. If the username is omitted +(along with the @samp{@@} sign), then your user name will be used. +(This is the normal @command{rsh} behavior.) It is necessary for the +remote machine, in addition to permitting your @command{rsh} access, to +have the @file{rmt} program installed (this command is included in +the @GNUTAR{} distribution and by default is installed under +@file{@var{prefix}/libexec/rmt}, where @var{prefix} means your +installation prefix). If you need to use a file whose name includes a +colon, then the remote tape drive behavior +can be inhibited by using the @option{--force-local} option. + +When the archive is being created to @file{/dev/null}, @GNUTAR{} +tries to minimize input and output operations. The Amanda backup +system, when used with @GNUTAR{}, has an initial sizing pass which +uses this feature. + +@node Selecting Archive Members +@section Selecting Archive Members +@cindex Specifying files to act on +@cindex Specifying archive members + +@dfn{File Name arguments} specify which files in the file system +@command{tar} operates on, when creating or adding to an archive, or which +archive members @command{tar} operates on, when reading or deleting from +an archive. @xref{Operations}. + +To specify file names, you can include them as the last arguments on +the command line, as follows: +@smallexample +@kbd{tar} @var{operation} [@var{option1} @var{option2} @dots{}] [@var{file name-1} @var{file name-2} @dots{}] +@end smallexample + +If a file name begins with dash (@samp{-}), precede it with +@option{--add-file} option to prevent it from being treated as an +option. + +@anchor{input name quoting} +By default @GNUTAR{} attempts to @dfn{unquote} each file or member +name, replacing @dfn{escape sequences} according to the following +table: + +@multitable @columnfractions 0.20 0.60 +@headitem Escape @tab Replaced with +@item \a @tab Audible bell (@acronym{ASCII} 7) +@item \b @tab Backspace (@acronym{ASCII} 8) +@item \f @tab Form feed (@acronym{ASCII} 12) +@item \n @tab New line (@acronym{ASCII} 10) +@item \r @tab Carriage return (@acronym{ASCII} 13) +@item \t @tab Horizontal tabulation (@acronym{ASCII} 9) +@item \v @tab Vertical tabulation (@acronym{ASCII} 11) +@item \? @tab @acronym{ASCII} 127 +@item \@var{n} @tab @acronym{ASCII} @var{n} (@var{n} should be an octal number + of up to 3 digits) +@end multitable + +A backslash followed by any other symbol is retained. + +This default behavior is controlled by the following command line +option: + +@table @option +@opindex unquote +@item --unquote +Enable unquoting input file or member names (default). + +@opindex no-unquote +@item --no-unquote +Disable unquoting input file or member names. +@end table + +If you specify a directory name as a file name argument, all the files +in that directory are operated on by @command{tar}. + +If you do not specify files, @command{tar} behavior differs depending +on the operation mode as described below: + +When @command{tar} is invoked with @option{--create} (@option{-c}), +@command{tar} will stop immediately, reporting the following: + +@smallexample +@group +$ @kbd{tar cf a.tar} +tar: Cowardly refusing to create an empty archive +Try `tar --help' or `tar --usage' for more information. +@end group +@end smallexample + +If you specify either @option{--list} (@option{-t}) or +@option{--extract} (@option{--get}, @option{-x}), @command{tar} +operates on all the archive members in the archive. + +If run with @option{--diff} option, tar will compare the archive with +the contents of the current working directory. + +If you specify any other operation, @command{tar} does nothing. + +By default, @command{tar} takes file names from the command line. However, +there are other ways to specify file or member names, or to modify the +manner in which @command{tar} selects the files or members upon which to +operate. In general, these methods work both for specifying the names +of files and archive members. + +@node files +@section Reading Names from a File + +@cindex Reading file names from a file +@cindex Lists of file names +@cindex File Name arguments, alternatives +@cindex @command{find}, using with @command{tar} +Instead of giving the names of files or archive members on the command +line, you can put the names into a file, and then use the +@option{--files-from=@var{file-of-names}} (@option{-T +@var{file-of-names}}) option to @command{tar}. Give the name of the +file which contains the list of files to include as the argument to +@option{--files-from}. In the list, the file names should be separated by +newlines. You will frequently use this option when you have generated +the list of files to archive with the @command{find} utility. + +@table @option +@opindex files-from +@item --files-from=@var{file-name} +@itemx -T @var{file-name} +Get names to extract or create from file @var{file-name}. +@end table + +If you give a single dash as a file name for @option{--files-from}, (i.e., +you specify either @code{--files-from=-} or @code{-T -}), then the file +names are read from standard input. + +Unless you are running @command{tar} with @option{--create}, you can not use +both @code{--files-from=-} and @code{--file=-} (@code{-f -}) in the same +command. + +Any number of @option{-T} options can be given in the command line. + +The following example shows how to use @command{find} to generate a list of +files smaller than 400K in length and put that list into a file +called @file{small-files}. You can then use the @option{-T} option to +@command{tar} to specify the files from that file, @file{small-files}, to +create the archive @file{little.tgz}. (The @option{-z} option to +@command{tar} compresses the archive with @command{gzip}; @pxref{gzip} for +more information.) + +@smallexample +$ @kbd{find . -size -400 -print > small-files} +$ @kbd{tar -c -v -z -T small-files -f little.tgz} +@end smallexample + +@noindent +In the file list given by @option{-T} option, any file name beginning +with @samp{-} character is considered a @command{tar} option and is +processed accordingly@footnote{Versions of @GNUTAR{} up to 1.15.1 +recognized only @option{-C} option in file lists, and only if the +option and its argument occupied two consecutive lines.}. For example, +the common use of this feature is to change to another directory by +specifying @option{-C} option: + +@smallexample +@group +$ @kbd{cat list} +-C/etc +passwd +hosts +-C/lib +libc.a +$ @kbd{tar -c -f foo.tar --files-from list} +@end group +@end smallexample + +@noindent +In this example, @command{tar} will first switch to @file{/etc} +directory and add files @file{passwd} and @file{hosts} to the +archive. Then it will change to @file{/lib} directory and will archive +the file @file{libc.a}. Thus, the resulting archive @file{foo.tar} will +contain: + +@smallexample +@group +$ @kbd{tar tf foo.tar} +passwd +hosts +libc.a +@end group +@end smallexample + +@noindent +@xopindex{directory, using in @option{--files-from} argument} +Notice that the option parsing algorithm used with @option{-T} is +stricter than the one used by shell. Namely, when specifying option +arguments, you should observe the following rules: + +@itemize @bullet +@item +When using short (single-letter) option form, its argument must +immediately follow the option letter, without any intervening +whitespace. For example: @code{-Cdir}. + +@item +When using long option form, the option argument must be separated +from the option by a single equal sign. No whitespace is allowed on +any side of the equal sign. For example: @code{--directory=dir}. + +@item +For both short and long option forms, the option argument can be given +on the next line after the option name, e.g.: + +@smallexample +@group +--directory +dir +@end group +@end smallexample + +@noindent +and + +@smallexample +@group +-C +dir +@end group +@end smallexample +@end itemize + +@opindex add-file +If you happen to have a file whose name starts with @samp{-}, +precede it with @option{--add-file} option to prevent it from +being recognized as an option. For example: @code{--add-file=--my-file}. + +@menu +* nul:: +@end menu + +@node nul +@subsection @code{NUL}-Terminated File Names + +@cindex File names, terminated by @code{NUL} +@cindex @code{NUL}-terminated file names +The @option{--null} option causes +@option{--files-from=@var{file-of-names}} (@option{-T @var{file-of-names}}) +to read file names terminated by a @code{NUL} instead of a newline, so +files whose names contain newlines can be archived using +@option{--files-from}. + +@table @option +@xopindex{null, described} +@item --null +Only consider @code{NUL}-terminated file names, instead of files that +terminate in a newline. + +@xopindex{no-null, described} +@item --no-null +Undo the effect of any previous @option{--null} option. +@end table + +The @option{--null} option is just like the one in @acronym{GNU} +@command{xargs} and @command{cpio}, and is useful with the +@option{-print0} predicate of @acronym{GNU} @command{find}. In +@command{tar}, @option{--null} also disables special handling for +file names that begin with dash. + +This example shows how to use @command{find} to generate a list of files +larger than 800K in length and put that list into a file called +@file{long-files}. The @option{-print0} option to @command{find} is just +like @option{-print}, except that it separates files with a @code{NUL} +rather than with a newline. You can then run @command{tar} with both the +@option{--null} and @option{-T} options to specify that @command{tar} gets the +files from that file, @file{long-files}, to create the archive +@file{big.tgz}. The @option{--null} option to @command{tar} will cause +@command{tar} to recognize the @code{NUL} separator between files. + +@smallexample +$ @kbd{find . -size +800 -print0 > long-files} +$ @kbd{tar -c -v --null --files-from=long-files --file=big.tar} +@end smallexample + +The @option{--no-null} option can be used if you need to read both +@code{NUL}-terminated and newline-terminated files on the same command line. +For example, if @file{flist} is a newline-terminated file, then the +following command can be used to combine it with the above command: + +@smallexample +@group +$ @kbd{find . -size +800 -print0 | + tar -c -f big.tar --null -T - --no-null -T flist} +@end group +@end smallexample + +This example uses short options for typographic reasons, to avoid +very long lines. + +@GNUTAR is able to automatically detect @code{NUL}-terminated file lists, so +it is safe to use them even without the @option{--null} option. In +this case @command{tar} will print a warning and continue reading such +a file as if @option{--null} were actually given: + +@smallexample +@group +$ @kbd{find . -size +800 -print0 | tar -c -f big.tar -T -} +tar: -: file name read contains nul character +@end group +@end smallexample + +The null terminator, however, remains in effect only for this +particular file, any following @option{-T} options will assume +newline termination. Of course, the null autodetection applies +to these eventual surplus @option{-T} options as well. + +@node exclude +@section Excluding Some Files + +@cindex File names, excluding files by +@cindex Excluding files by name and pattern +@cindex Excluding files by file system +@opindex exclude +@opindex exclude-from +To avoid operating on files whose names match a particular pattern, +use the @option{--exclude} or @option{--exclude-from} options. + +@table @option +@opindex exclude +@item --exclude=@var{pattern} +Causes @command{tar} to ignore files that match the @var{pattern}. +@end table + +@findex exclude +The @option{--exclude=@var{pattern}} option prevents any file or +member whose name matches the shell wildcard (@var{pattern}) from +being operated on. +For example, to create an archive with all the contents of the directory +@file{src} except for files whose names end in @file{.o}, use the +command @samp{tar -cf src.tar --exclude='*.o' src}. + +You may give multiple @option{--exclude} options. + +@table @option +@opindex exclude-from +@item --exclude-from=@var{file} +@itemx -X @var{file} +Causes @command{tar} to ignore files that match the patterns listed in +@var{file}. +@end table + +@findex exclude-from +Use the @option{--exclude-from} option to read a +list of patterns, one per line, from @var{file}; @command{tar} will +ignore files matching those patterns. Thus if @command{tar} is +called as @w{@samp{tar -c -X foo .}} and the file @file{foo} contains a +single line @file{*.o}, no files whose names end in @file{.o} will be +added to the archive. + +Notice, that lines from @var{file} are read verbatim. One of the +frequent errors is leaving some extra whitespace after a file name, +which is difficult to catch using text editors. + +However, empty lines are OK. + +@table @option +@cindex version control system, excluding files +@cindex VCS, excluding files +@cindex SCCS, excluding files +@cindex RCS, excluding files +@cindex CVS, excluding files +@cindex SVN, excluding files +@cindex git, excluding files +@cindex Bazaar, excluding files +@cindex Arch, excluding files +@cindex Mercurial, excluding files +@cindex Darcs, excluding files +@opindex exclude-vcs +@item --exclude-vcs +Exclude files and directories used by following version control +systems: @samp{CVS}, @samp{RCS}, @samp{SCCS}, @samp{SVN}, @samp{Arch}, +@samp{Bazaar}, @samp{Mercurial}, and @samp{Darcs}. + +As of version @value{VERSION}, the following files are excluded: + +@itemize @bullet +@item @file{CVS/}, and everything under it +@item @file{RCS/}, and everything under it +@item @file{SCCS/}, and everything under it +@item @file{.git/}, and everything under it +@item @file{.gitignore} +@item @file{.cvsignore} +@item @file{.svn/}, and everything under it +@item @file{.arch-ids/}, and everything under it +@item @file{@{arch@}/}, and everything under it +@item @file{=RELEASE-ID} +@item @file{=meta-update} +@item @file{=update} +@item @file{.bzr} +@item @file{.bzrignore} +@item @file{.bzrtags} +@item @file{.hg} +@item @file{.hgignore} +@item @file{.hgrags} +@item @file{_darcs} +@end itemize + +@opindex exclude-backups +@item --exclude-backups +Exclude backup and lock files. This option causes exclusion of files +that match the following shell globbing patterns: + +@table @asis +@item .#* +@item *~ +@item #*# +@end table + +@end table + +@findex exclude-caches +When creating an archive, the @option{--exclude-caches} option family +causes @command{tar} to exclude all directories that contain a @dfn{cache +directory tag}. A cache directory tag is a short file with the +well-known name @file{CACHEDIR.TAG} and having a standard header +specified in @url{http://www.brynosaurus.com/cachedir/spec.html}. +Various applications write cache directory tags into directories they +use to hold regenerable, non-precious data, so that such data can be +more easily excluded from backups. + +There are three @samp{exclude-caches} options, each providing a different +exclusion semantics: + +@table @option +@opindex exclude-caches +@item --exclude-caches +Do not archive the contents of the directory, but archive the +directory itself and the @file{CACHEDIR.TAG} file. + +@opindex exclude-caches-under +@item --exclude-caches-under +Do not archive the contents of the directory, nor the +@file{CACHEDIR.TAG} file, archive only the directory itself. + +@opindex exclude-caches-all +@item --exclude-caches-all +Omit directories containing @file{CACHEDIR.TAG} file entirely. +@end table + +@findex exclude-tag +Another option family, @option{--exclude-tag}, provides a generalization of +this concept. It takes a single argument, a file name to look for. +Any directory that contains this file will be excluded from the dump. +Similarly to @samp{exclude-caches}, there are three options in this +option family: + +@table @option +@opindex exclude-tag +@item --exclude-tag=@var{file} +Do not dump the contents of the directory, but dump the +directory itself and the @var{file}. + +@opindex exclude-tag-under +@item --exclude-tag-under=@var{file} +Do not dump the contents of the directory, nor the +@var{file}, archive only the directory itself. + +@opindex exclude-tag-all +@item --exclude-tag-all=@var{file} +Omit directories containing @var{file} file entirely. +@end table + +Multiple @option{--exclude-tag*} options can be given. + +For example, given this directory: + +@smallexample +@group +$ @kbd{find dir} +dir +dir/blues +dir/jazz +dir/folk +dir/folk/tagfile +dir/folk/sanjuan +dir/folk/trote +@end group +@end smallexample + +The @option{--exclude-tag} will produce the following: + +@smallexample +$ @kbd{tar -cf archive.tar --exclude-tag=tagfile -v dir} +dir/ +dir/blues +dir/jazz +dir/folk/ +tar: dir/folk/: contains a cache directory tag tagfile; + contents not dumped +dir/folk/tagfile +@end smallexample + +Both the @file{dir/folk} directory and its tagfile are preserved in +the archive, however the rest of files in this directory are not. + +Now, using the @option{--exclude-tag-under} option will exclude +@file{tagfile} from the dump, while still preserving the directory +itself, as shown in this example: + +@smallexample +$ @kbd{tar -cf archive.tar --exclude-tag-under=tagfile -v dir} +dir/ +dir/blues +dir/jazz +dir/folk/ +./tar: dir/folk/: contains a cache directory tag tagfile; + contents not dumped +@end smallexample + +Finally, using @option{--exclude-tag-all} omits the @file{dir/folk} +directory entirely: + +@smallexample +$ @kbd{tar -cf archive.tar --exclude-tag-all=tagfile -v dir} +dir/ +dir/blues +dir/jazz +./tar: dir/folk/: contains a cache directory tag tagfile; + directory not dumped +@end smallexample + +@menu +* problems with exclude:: +@end menu + +@node problems with exclude +@unnumberedsubsec Problems with Using the @code{exclude} Options + +@xopindex{exclude, potential problems with} +Some users find @samp{exclude} options confusing. Here are some common +pitfalls: + +@itemize @bullet +@item +The main operating mode of @command{tar} does not act on a file name +explicitly listed on the command line, if one of its file name +components is excluded. In the example above, if +you create an archive and exclude files that end with @samp{*.o}, but +explicitly name the file @samp{dir.o/foo} after all the options have been +listed, @samp{dir.o/foo} will be excluded from the archive. + +@item +You can sometimes confuse the meanings of @option{--exclude} and +@option{--exclude-from}. Be careful: use @option{--exclude} when files +to be excluded are given as a pattern on the command line. Use +@option{--exclude-from} to introduce the name of a file which contains +a list of patterns, one per line; each of these patterns can exclude +zero, one, or many files. + +@item +When you use @option{--exclude=@var{pattern}}, be sure to quote the +@var{pattern} parameter, so @GNUTAR{} sees wildcard characters +like @samp{*}. If you do not do this, the shell might expand the +@samp{*} itself using files at hand, so @command{tar} might receive a +list of files instead of one pattern, or none at all, making the +command somewhat illegal. This might not correspond to what you want. + +For example, write: + +@smallexample +$ @kbd{tar -c -f @var{archive.tar} --exclude '*.o' @var{directory}} +@end smallexample + +@noindent +rather than: + +@smallexample +# @emph{Wrong!} +$ @kbd{tar -c -f @var{archive.tar} --exclude *.o @var{directory}} +@end smallexample + +@item +You must use use shell syntax, or globbing, rather than @code{regexp} +syntax, when using exclude options in @command{tar}. If you try to use +@code{regexp} syntax to describe files to be excluded, your command +might fail. + +@item +@FIXME{The change in semantics must have occurred before 1.11, +so I doubt if it is worth mentioning at all. Anyway, should at +least specify in which version the semantics changed.} +In earlier versions of @command{tar}, what is now the +@option{--exclude-from} option was called @option{--exclude} instead. +Now, @option{--exclude} applies to patterns listed on the command +line and @option{--exclude-from} applies to patterns listed in a +file. + +@end itemize + +@node wildcards +@section Wildcards Patterns and Matching + +@dfn{Globbing} is the operation by which @dfn{wildcard} characters, +@samp{*} or @samp{?} for example, are replaced and expanded into all +existing files matching the given pattern. @GNUTAR{} can use wildcard +patterns for matching (or globbing) archive members when extracting +from or listing an archive. Wildcard patterns are also used for +verifying volume labels of @command{tar} archives. This section has the +purpose of explaining wildcard syntax for @command{tar}. + +@FIXME{the next few paragraphs need work.} + +A @var{pattern} should be written according to shell syntax, using wildcard +characters to effect globbing. Most characters in the pattern stand +for themselves in the matched string, and case is significant: @samp{a} +will match only @samp{a}, and not @samp{A}. The character @samp{?} in the +pattern matches any single character in the matched string. The character +@samp{*} in the pattern matches zero, one, or more single characters in +the matched string. The character @samp{\} says to take the following +character of the pattern @emph{literally}; it is useful when one needs to +match the @samp{?}, @samp{*}, @samp{[} or @samp{\} characters, themselves. + +The character @samp{[}, up to the matching @samp{]}, introduces a character +class. A @dfn{character class} is a list of acceptable characters +for the next single character of the matched string. For example, +@samp{[abcde]} would match any of the first five letters of the alphabet. +Note that within a character class, all of the ``special characters'' +listed above other than @samp{\} lose their special meaning; for example, +@samp{[-\\[*?]]} would match any of the characters, @samp{-}, @samp{\}, +@samp{[}, @samp{*}, @samp{?}, or @samp{]}. (Due to parsing constraints, +the characters @samp{-} and @samp{]} must either come @emph{first} or +@emph{last} in a character class.) + +@cindex Excluding characters from a character class +@cindex Character class, excluding characters from +If the first character of the class after the opening @samp{[} +is @samp{!} or @samp{^}, then the meaning of the class is reversed. +Rather than listing character to match, it lists those characters which +are @emph{forbidden} as the next single character of the matched string. + +Other characters of the class stand for themselves. The special +construction @samp{[@var{a}-@var{e}]}, using an hyphen between two +letters, is meant to represent all characters between @var{a} and +@var{e}, inclusive. + +@FIXME{need to add a sentence or so here to make this clear for those +who don't have dan around.} + +Periods (@samp{.}) or forward slashes (@samp{/}) are not considered +special for wildcard matches. However, if a pattern completely matches +a directory prefix of a matched string, then it matches the full matched +string: thus, excluding a directory also excludes all the files beneath it. + +@menu +* controlling pattern-matching:: +@end menu + +@node controlling pattern-matching +@unnumberedsubsec Controlling Pattern-Matching + +For the purposes of this section, we call @dfn{exclusion members} all +member names obtained while processing @option{--exclude} and +@option{--exclude-from} options, and @dfn{inclusion members} those +member names that were given in the command line or read from the file +specified with @option{--files-from} option. + +These two pairs of member lists are used in the following operations: +@option{--diff}, @option{--extract}, @option{--list}, +@option{--update}. + +There are no inclusion members in create mode (@option{--create} and +@option{--append}), since in this mode the names obtained from the +command line refer to @emph{files}, not archive members. + +By default, inclusion members are compared with archive members +literally @footnote{Notice that earlier @GNUTAR{} versions used +globbing for inclusion members, which contradicted to UNIX98 +specification and was not documented. @xref{Changes}, for more +information on this and other changes.} and exclusion members are +treated as globbing patterns. For example: + +@smallexample +@group +$ @kbd{tar tf foo.tar} +a.c +b.c +a.txt +[remarks] +# @i{Member names are used verbatim:} +$ @kbd{tar -xf foo.tar -v '[remarks]'} +[remarks] +# @i{Exclude member names are globbed:} +$ @kbd{tar -xf foo.tar -v --exclude '*.c'} +a.txt +[remarks] +@end group +@end smallexample + +This behavior can be altered by using the following options: + +@table @option +@opindex wildcards +@item --wildcards +Treat all member names as wildcards. + +@opindex no-wildcards +@item --no-wildcards +Treat all member names as literal strings. +@end table + +Thus, to extract files whose names end in @samp{.c}, you can use: + +@smallexample +$ @kbd{tar -xf foo.tar -v --wildcards '*.c'} +a.c +b.c +@end smallexample + +@noindent +Notice quoting of the pattern to prevent the shell from interpreting +it. + +The effect of @option{--wildcards} option is canceled by +@option{--no-wildcards}. This can be used to pass part of +the command line arguments verbatim and other part as globbing +patterns. For example, the following invocation: + +@smallexample +$ @kbd{tar -xf foo.tar --wildcards '*.txt' --no-wildcards '[remarks]'} +@end smallexample + +@noindent +instructs @command{tar} to extract from @file{foo.tar} all files whose +names end in @samp{.txt} and the file named @file{[remarks]}. + +Normally, a pattern matches a name if an initial subsequence of the +name's components matches the pattern, where @samp{*}, @samp{?}, and +@samp{[...]} are the usual shell wildcards, @samp{\} escapes wildcards, +and wildcards can match @samp{/}. + +Other than optionally stripping leading @samp{/} from names +(@pxref{absolute}), patterns and names are used as-is. For +example, trailing @samp{/} is not trimmed from a user-specified name +before deciding whether to exclude it. + +However, this matching procedure can be altered by the options listed +below. These options accumulate. For example: + +@smallexample +--ignore-case --exclude='makefile' --no-ignore-case ---exclude='readme' +@end smallexample + +@noindent +ignores case when excluding @samp{makefile}, but not when excluding +@samp{readme}. + +@table @option +@opindex anchored +@opindex no-anchored +@item --anchored +@itemx --no-anchored +If anchored, a pattern must match an initial subsequence +of the name's components. Otherwise, the pattern can match any +subsequence. Default is @option{--no-anchored} for exclusion members +and @option{--anchored} inclusion members. + +@opindex ignore-case +@opindex no-ignore-case +@item --ignore-case +@itemx --no-ignore-case +When ignoring case, upper-case patterns match lower-case names and vice versa. +When not ignoring case (the default), matching is case-sensitive. + +@opindex wildcards-match-slash +@opindex no-wildcards-match-slash +@item --wildcards-match-slash +@itemx --no-wildcards-match-slash +When wildcards match slash (the default for exclusion members), a +wildcard like @samp{*} in the pattern can match a @samp{/} in the +name. Otherwise, @samp{/} is matched only by @samp{/}. + +@end table + +The @option{--recursion} and @option{--no-recursion} options +(@pxref{recurse}) also affect how member patterns are interpreted. If +recursion is in effect, a pattern matches a name if it matches any of +the name's parent directories. + +The following table summarizes pattern-matching default values: + +@multitable @columnfractions .3 .7 +@headitem Members @tab Default settings +@item Inclusion @tab @option{--no-wildcards --anchored --no-wildcards-match-slash} +@item Exclusion @tab @option{--wildcards --no-anchored --wildcards-match-slash} +@end multitable + +@node quoting styles +@section Quoting Member Names + +When displaying member names, @command{tar} takes care to avoid +ambiguities caused by certain characters. This is called @dfn{name +quoting}. The characters in question are: + +@itemize @bullet +@item Non-printable control characters: +@anchor{escape sequences} +@multitable @columnfractions 0.20 0.10 0.60 +@headitem Character @tab @acronym{ASCII} @tab Character name +@item \a @tab 7 @tab Audible bell +@item \b @tab 8 @tab Backspace +@item \f @tab 12 @tab Form feed +@item \n @tab 10 @tab New line +@item \r @tab 13 @tab Carriage return +@item \t @tab 9 @tab Horizontal tabulation +@item \v @tab 11 @tab Vertical tabulation +@end multitable + +@item Space (@acronym{ASCII} 32) + +@item Single and double quotes (@samp{'} and @samp{"}) + +@item Backslash (@samp{\}) +@end itemize + +The exact way @command{tar} uses to quote these characters depends on +the @dfn{quoting style}. The default quoting style, called +@dfn{escape} (see below), uses backslash notation to represent control +characters, space and backslash. Using this quoting style, control +characters are represented as listed in column @samp{Character} in the +above table, a space is printed as @samp{\ } and a backslash as @samp{\\}. + +@GNUTAR{} offers seven distinct quoting styles, which can be selected +using @option{--quoting-style} option: + +@table @option +@item --quoting-style=@var{style} +@opindex quoting-style + +Sets quoting style. Valid values for @var{style} argument are: +literal, shell, shell-always, c, escape, locale, clocale. +@end table + +These styles are described in detail below. To illustrate their +effect, we will use an imaginary tar archive @file{arch.tar} +containing the following members: + +@smallexample +@group +# 1. Contains horizontal tabulation character. +a tab +# 2. Contains newline character +a +newline +# 3. Contains a space +a space +# 4. Contains double quotes +a"double"quote +# 5. Contains single quotes +a'single'quote +# 6. Contains a backslash character: +a\backslash +@end group +@end smallexample + +Here is how usual @command{ls} command would have listed them, if they +had existed in the current working directory: + +@smallexample +@group +$ @kbd{ls} +a\ttab +a\nnewline +a\ space +a"double"quote +a'single'quote +a\\backslash +@end group +@end smallexample + +Quoting styles: + +@table @samp +@item literal +No quoting, display each character as is: + +@smallexample +@group +$ @kbd{tar tf arch.tar --quoting-style=literal} +./ +./a space +./a'single'quote +./a"double"quote +./a\backslash +./a tab +./a +newline +@end group +@end smallexample + +@item shell +Display characters the same way Bourne shell does: +control characters, except @samp{\t} and @samp{\n}, are printed using +backslash escapes, @samp{\t} and @samp{\n} are printed as is, and a +single quote is printed as @samp{\'}. If a name contains any quoted +characters, it is enclosed in single quotes. In particular, if a name +contains single quotes, it is printed as several single-quoted strings: + +@smallexample +@group +$ @kbd{tar tf arch.tar --quoting-style=shell} +./ +'./a space' +'./a'\''single'\''quote' +'./a"double"quote' +'./a\backslash' +'./a tab' +'./a +newline' +@end group +@end smallexample + +@item shell-always +Same as @samp{shell}, but the names are always enclosed in single +quotes: + +@smallexample +@group +$ @kbd{tar tf arch.tar --quoting-style=shell-always} +'./' +'./a space' +'./a'\''single'\''quote' +'./a"double"quote' +'./a\backslash' +'./a tab' +'./a +newline' +@end group +@end smallexample + +@item c +Use the notation of the C programming language. All names are +enclosed in double quotes. Control characters are quoted using +backslash notations, double quotes are represented as @samp{\"}, +backslash characters are represented as @samp{\\}. Single quotes and +spaces are not quoted: + +@smallexample +@group +$ @kbd{tar tf arch.tar --quoting-style=c} +"./" +"./a space" +"./a'single'quote" +"./a\"double\"quote" +"./a\\backslash" +"./a\ttab" +"./a\nnewline" +@end group +@end smallexample + +@item escape +Control characters are printed using backslash notation, a space is +printed as @samp{\ } and a backslash as @samp{\\}. This is the +default quoting style, unless it was changed when configured the +package. + +@smallexample +@group +$ @kbd{tar tf arch.tar --quoting-style=escape} +./ +./a space +./a'single'quote +./a"double"quote +./a\\backslash +./a\ttab +./a\nnewline +@end group +@end smallexample + +@item locale +Control characters, single quote and backslash are printed using +backslash notation. All names are quoted using left and right +quotation marks, appropriate to the current locale. If it does not +define quotation marks, use @samp{`} as left and @samp{'} as right +quotation marks. Any occurrences of the right quotation mark in a +name are escaped with @samp{\}, for example: + +For example: + +@smallexample +@group +$ @kbd{tar tf arch.tar --quoting-style=locale} +`./' +`./a space' +`./a\'single\'quote' +`./a"double"quote' +`./a\\backslash' +`./a\ttab' +`./a\nnewline' +@end group +@end smallexample + +@item clocale +Same as @samp{locale}, but @samp{"} is used for both left and right +quotation marks, if not provided by the currently selected locale: + +@smallexample +@group +$ @kbd{tar tf arch.tar --quoting-style=clocale} +"./" +"./a space" +"./a'single'quote" +"./a\"double\"quote" +"./a\\backslash" +"./a\ttab" +"./a\nnewline" +@end group +@end smallexample +@end table + +You can specify which characters should be quoted in addition to those +implied by the current quoting style: + +@table @option +@item --quote-chars=@var{string} +Always quote characters from @var{string}, even if the selected +quoting style would not quote them. +@end table + +For example, using @samp{escape} quoting (compare with the usual +escape listing above): + +@smallexample +@group +$ @kbd{tar tf arch.tar --quoting-style=escape --quote-chars=' "'} +./ +./a\ space +./a'single'quote +./a\"double\"quote +./a\\backslash +./a\ttab +./a\nnewline +@end group +@end smallexample + +To disable quoting of such additional characters, use the following +option: + +@table @option +@item --no-quote-chars=@var{string} +Remove characters listed in @var{string} from the list of quoted +characters set by the previous @option{--quote-chars} option. +@end table + +This option is particularly useful if you have added +@option{--quote-chars} to your @env{TAR_OPTIONS} (@pxref{TAR_OPTIONS}) +and wish to disable it for the current invocation. + +Note, that @option{--no-quote-chars} does @emph{not} disable those +characters that are quoted by default in the selected quoting style. + +@node transform +@section Modifying File and Member Names + +@command{Tar} archives contain detailed information about files stored +in them and full file names are part of that information. When +storing a file to an archive, its file name is recorded in it, +along with the actual file contents. When restoring from an archive, +a file is created on disk with exactly the same name as that stored +in the archive. In the majority of cases this is the desired behavior +of a file archiver. However, there are some cases when it is not. + +First of all, it is often unsafe to extract archive members with +absolute file names or those that begin with a @file{../}. @GNUTAR{} +takes special precautions when extracting such names and provides a +special option for handling them, which is described in +@ref{absolute}. + +Secondly, you may wish to extract file names without some leading +directory components, or with otherwise modified names. In other +cases it is desirable to store files under differing names in the +archive. + +@GNUTAR{} provides several options for these needs. + +@table @option +@opindex strip-components +@item --strip-components=@var{number} +Strip given @var{number} of leading components from file names before +extraction. +@end table + +For example, suppose you have archived whole @file{/usr} hierarchy to +a tar archive named @file{usr.tar}. Among other files, this archive +contains @file{usr/include/stdlib.h}, which you wish to extract to +the current working directory. To do so, you type: + +@smallexample +$ @kbd{tar -xf usr.tar --strip=2 usr/include/stdlib.h} +@end smallexample + +The option @option{--strip=2} instructs @command{tar} to strip the +two leading components (@file{usr/} and @file{include/}) off the file +name. + +If you add the @option{--verbose} (@option{-v}) option to the invocation +above, you will note that the verbose listing still contains the +full file name, with the two removed components still in place. This +can be inconvenient, so @command{tar} provides a special option for +altering this behavior: + +@anchor{show-transformed-names} +@table @option +@opindex show-transformed-names +@item --show-transformed-names +Display file or member names with all requested transformations +applied. +@end table + +@noindent +For example: + +@smallexample +@group +$ @kbd{tar -xf usr.tar -v --strip=2 usr/include/stdlib.h} +usr/include/stdlib.h +$ @kbd{tar -xf usr.tar -v --strip=2 --show-transformed usr/include/stdlib.h} +stdlib.h +@end group +@end smallexample + +Notice that in both cases the file @file{stdlib.h} is extracted to the +current working directory, @option{--show-transformed-names} affects +only the way its name is displayed. + +This option is especially useful for verifying whether the invocation +will have the desired effect. Thus, before running + +@smallexample +$ @kbd{tar -x --strip=@var{n}} +@end smallexample + +@noindent +it is often advisable to run + +@smallexample +$ @kbd{tar -t -v --show-transformed --strip=@var{n}} +@end smallexample + +@noindent +to make sure the command will produce the intended results. + +In case you need to apply more complex modifications to the file name, +@GNUTAR{} provides a general-purpose transformation option: + +@table @option +@opindex transform +@opindex xform +@item --transform=@var{expression} +@itemx --xform=@var{expression} +Modify file names using supplied @var{expression}. +@end table + +@noindent +The @var{expression} is a @command{sed}-like replace expression of the +form: + +@smallexample +s/@var{regexp}/@var{replace}/[@var{flags}] +@end smallexample + +@noindent +where @var{regexp} is a @dfn{regular expression}, @var{replace} is a +replacement for each file name part that matches @var{regexp}. Both +@var{regexp} and @var{replace} are described in detail in +@ref{The "s" Command, The "s" Command, The `s' Command, sed, GNU sed}. + +Any delimiter can be used in lieu of @samp{/}, the only requirement being +that it be used consistently throughout the expression. For example, +the following two expressions are equivalent: + +@smallexample +@group +s/one/two/ +s,one,two, +@end group +@end smallexample + +Changing delimiters is often useful when the @var{regex} contains +slashes. For example, it is more convenient to write @code{s,/,-,} than +@code{s/\//-/}. + +As in @command{sed}, you can give several replace expressions, +separated by a semicolon. + +Supported @var{flags} are: + +@table @samp +@item g +Apply the replacement to @emph{all} matches to the @var{regexp}, not +just the first. + +@item i +Use case-insensitive matching. + +@item x +@var{regexp} is an @dfn{extended regular expression} (@pxref{Extended +regexps, Extended regular expressions, Extended regular expressions, +sed, GNU sed}). + +@item @var{number} +Only replace the @var{number}th match of the @var{regexp}. + +Note: the @acronym{POSIX} standard does not specify what should happen +when you mix the @samp{g} and @var{number} modifiers. @GNUTAR{} +follows the GNU @command{sed} implementation in this regard, so +the interaction is defined to be: ignore matches before the +@var{number}th, and then match and replace all matches from the +@var{number}th on. + +@end table + +In addition, several @dfn{transformation scope} flags are supported, +that control to what files transformations apply. These are: + +@table @samp +@item r +Apply transformation to regular archive members. + +@item R +Do not apply transformation to regular archive members. + +@item s +Apply transformation to symbolic link targets. + +@item S +Do not apply transformation to symbolic link targets. + +@item h +Apply transformation to hard link targets. + +@item H +Do not apply transformation to hard link targets. +@end table + +Default is @samp{rsh}, which means to apply tranformations to both archive +members and targets of symbolic and hard links. + +Default scope flags can also be changed using @samp{flags=} statement +in the transform expression. The flags set this way remain in force +until next @samp{flags=} statement or end of expression, whichever +occurs first. For example: + +@smallexample + --transform 'flags=S;s|^|/usr/local/|' +@end smallexample + +Here are several examples of @option{--transform} usage: + +@enumerate +@item Extract @file{usr/} hierarchy into @file{usr/local/}: + +@smallexample +$ @kbd{tar --transform='s,usr/,usr/local/,' -x -f arch.tar} +@end smallexample + +@item Strip two leading directory components (equivalent to +@option{--strip-components=2}): + +@smallexample +$ @kbd{tar --transform='s,/*[^/]*/[^/]*/,,' -x -f arch.tar} +@end smallexample + +@item Convert each file name to lower case: + +@smallexample +$ @kbd{tar --transform 's/.*/\L&/' -x -f arch.tar} +@end smallexample + +@item Prepend @file{/prefix/} to each file name: + +@smallexample +$ @kbd{tar --transform 's,^,/prefix/,' -x -f arch.tar} +@end smallexample + +@item Archive the @file{/lib} directory, prepending @samp{/usr/local} +to each archive member: + +@smallexample +$ @kbd{tar --transform 's,^,/usr/local/,S' -c -f arch.tar /lib} +@end smallexample +@end enumerate + +Notice the use of flags in the last example. The @file{/lib} +directory often contains many symbolic links to files within it. +It may look, for example, like this: + +@smallexample +$ @kbd{ls -l} +drwxr-xr-x root/root 0 2008-07-08 16:20 /lib/ +-rwxr-xr-x root/root 1250840 2008-05-25 07:44 /lib/libc-2.3.2.so +lrwxrwxrwx root/root 0 2008-06-24 17:12 /lib/libc.so.6 -> libc-2.3.2.so +... +@end smallexample + +Using the expression @samp{s,^,/usr/local/,} would mean adding +@samp{/usr/local} to both regular archive members and to link +targets. In this case, @file{/lib/libc.so.6} would become: + +@smallexample + /usr/local/lib/libc.so.6 -> /usr/local/libc-2.3.2.so +@end smallexample + +This is definitely not desired. To avoid this, the @samp{S} flag +is used, which excludes symbolic link targets from filename +transformations. The result is: + +@smallexample +$ @kbd{tar --transform 's,^,/usr/local/,S', -c -v -f arch.tar \ + --show-transformed /lib} +drwxr-xr-x root/root 0 2008-07-08 16:20 /usr/local/lib/ +-rwxr-xr-x root/root 1250840 2008-05-25 07:44 /usr/local/lib/libc-2.3.2.so +lrwxrwxrwx root/root 0 2008-06-24 17:12 /usr/local/lib/libc.so.6 \ + -> libc-2.3.2.so +@end smallexample + +Unlike @option{--strip-components}, @option{--transform} can be used +in any @GNUTAR{} operation mode. For example, the following command +adds files to the archive while replacing the leading @file{usr/} +component with @file{var/}: + +@smallexample +$ @kbd{tar -cf arch.tar --transform='s,^usr/,var/,' /} +@end smallexample + +To test @option{--transform} effect we suggest using +@option{--show-transformed-names} option: + +@smallexample +$ @kbd{tar -cf arch.tar --transform='s,^usr/,var/,' \ + --verbose --show-transformed-names /} +@end smallexample + +If both @option{--strip-components} and @option{--transform} are used +together, then @option{--transform} is applied first, and the required +number of components is then stripped from its result. + +You can use as many @option{--transform} options in a single command +line as you want. The specified expressions will then be applied in +order of their appearance. For example, the following two invocations +are equivalent: + +@smallexample +$ @kbd{tar -cf arch.tar --transform='s,/usr/var,/var/' \ + --transform='s,/usr/local,/usr/,'} +$ @kbd{tar -cf arch.tar \ + --transform='s,/usr/var,/var/;s,/usr/local,/usr/,'} +@end smallexample + +@node after +@section Operating Only on New Files + +@cindex Excluding file by age +@cindex Data Modification time, excluding files by +@cindex Modification time, excluding files by +@cindex Age, excluding files by +The @option{--after-date=@var{date}} (@option{--newer=@var{date}}, +@option{-N @var{date}}) option causes @command{tar} to only work on +files whose data modification or status change times are newer than +the @var{date} given. If @var{date} starts with @samp{/} or @samp{.}, +it is taken to be a file name; the data modification time of that file +is used as the date. If you use this option when creating or appending +to an archive, the archive will only include new files. If you use +@option{--after-date} when extracting an archive, @command{tar} will +only extract files newer than the @var{date} you specify. + +If you only want @command{tar} to make the date comparison based on +modification of the file's data (rather than status +changes), then use the @option{--newer-mtime=@var{date}} option. + +@cindex --after-date and --update compared +@cindex --newer-mtime and --update compared +You may use these options with any operation. Note that these options +differ from the @option{--update} (@option{-u}) operation in that they +allow you to specify a particular date against which @command{tar} can +compare when deciding whether or not to archive the files. + +@table @option +@opindex after-date +@opindex newer +@item --after-date=@var{date} +@itemx --newer=@var{date} +@itemx -N @var{date} +Only store files newer than @var{date}. + +Acts on files only if their data modification or status change times are +later than @var{date}. Use in conjunction with any operation. + +If @var{date} starts with @samp{/} or @samp{.}, it is taken to be a file +name; the data modification time of that file is used as the date. + +@opindex newer-mtime +@item --newer-mtime=@var{date} +Acts like @option{--after-date}, but only looks at data modification times. +@end table + +These options limit @command{tar} to operate only on files which have +been modified after the date specified. A file's status is considered to have +changed if its contents have been modified, or if its owner, +permissions, and so forth, have been changed. (For more information on +how to specify a date, see @ref{Date input formats}; remember that the +entire date argument must be quoted if it contains any spaces.) + +Gurus would say that @option{--after-date} tests both the data +modification time (@code{mtime}, the time the contents of the file +were last modified) and the status change time (@code{ctime}, the time +the file's status was last changed: owner, permissions, etc.@:) +fields, while @option{--newer-mtime} tests only the @code{mtime} +field. + +To be precise, @option{--after-date} checks @emph{both} @code{mtime} and +@code{ctime} and processes the file if either one is more recent than +@var{date}, while @option{--newer-mtime} only checks @code{mtime} and +disregards @code{ctime}. Neither does it use @code{atime} (the last time the +contents of the file were looked at). + +Date specifiers can have embedded spaces. Because of this, you may need +to quote date arguments to keep the shell from parsing them as separate +arguments. For example, the following command will add to the archive +all the files modified less than two days ago: + +@smallexample +$ @kbd{tar -cf foo.tar --newer-mtime '2 days ago'} +@end smallexample + +When any of these options is used with the option @option{--verbose} +(@pxref{verbose tutorial}) @GNUTAR{} will try to convert the specified +date back to its textual representation and compare that with the +one given with the option. If the two dates differ, @command{tar} will +print a warning saying what date it will use. This is to help user +ensure he is using the right date. For example: + +@smallexample +@group +$ @kbd{tar -c -f archive.tar --after-date='10 days ago' .} +tar: Option --after-date: Treating date `10 days ago' as 2006-06-11 +13:19:37.232434 +@end group +@end smallexample + +@quotation +@strong{Please Note:} @option{--after-date} and @option{--newer-mtime} +should not be used for incremental backups. @xref{Incremental Dumps}, +for proper way of creating incremental backups. +@end quotation + +@node recurse +@section Descending into Directories +@cindex Avoiding recursion in directories +@cindex Descending directories, avoiding +@cindex Directories, avoiding recursion +@cindex Recursion in directories, avoiding + +Usually, @command{tar} will recursively explore all directories (either +those given on the command line or through the @option{--files-from} +option) for the various files they contain. However, you may not always +want @command{tar} to act this way. + +@opindex no-recursion +@cindex @command{find}, using with @command{tar} +The @option{--no-recursion} option inhibits @command{tar}'s recursive descent +into specified directories. If you specify @option{--no-recursion}, you can +use the @command{find} (@pxref{Top,, find, find, GNU Find Manual}) +utility for hunting through levels of directories to +construct a list of file names which you could then pass to @command{tar}. +@command{find} allows you to be more selective when choosing which files to +archive; see @ref{files}, for more information on using @command{find} with +@command{tar}. + +@table @option +@item --no-recursion +Prevents @command{tar} from recursively descending directories. + +@opindex recursion +@item --recursion +Requires @command{tar} to recursively descend directories. +This is the default. +@end table + +When you use @option{--no-recursion}, @GNUTAR{} grabs +directory entries themselves, but does not descend on them +recursively. Many people use @command{find} for locating files they +want to back up, and since @command{tar} @emph{usually} recursively +descends on directories, they have to use the @samp{@w{-not -type d}} +test in their @command{find} invocation (@pxref{Type, Type, Type test, +find, Finding Files}), as they usually do not want all the files in a +directory. They then use the @option{--files-from} option to archive +the files located via @command{find}. + +The problem when restoring files archived in this manner is that the +directories themselves are not in the archive; so the +@option{--same-permissions} (@option{--preserve-permissions}, +@option{-p}) option does not affect them---while users might really +like it to. Specifying @option{--no-recursion} is a way to tell +@command{tar} to grab only the directory entries given to it, adding +no new files on its own. To summarize, if you use @command{find} to +create a list of files to be stored in an archive, use it as follows: + +@smallexample +@group +$ @kbd{find @var{dir} @var{tests} | \ + tar -cf @var{archive} -T - --no-recursion} +@end group +@end smallexample + +The @option{--no-recursion} option also applies when extracting: it +causes @command{tar} to extract only the matched directory entries, not +the files under those directories. + +The @option{--no-recursion} option also affects how globbing patterns +are interpreted (@pxref{controlling pattern-matching}). + +The @option{--no-recursion} and @option{--recursion} options apply to +later options and operands, and can be overridden by later occurrences +of @option{--no-recursion} and @option{--recursion}. For example: + +@smallexample +$ @kbd{tar -cf jams.tar --no-recursion grape --recursion grape/concord} +@end smallexample + +@noindent +creates an archive with one entry for @file{grape}, and the recursive +contents of @file{grape/concord}, but no entries under @file{grape} +other than @file{grape/concord}. + +@node one +@section Crossing File System Boundaries +@cindex File system boundaries, not crossing + +@command{tar} will normally automatically cross file system boundaries in +order to archive files which are part of a directory tree. You can +change this behavior by running @command{tar} and specifying +@option{--one-file-system}. This option only affects files that are +archived because they are in a directory that is being archived; +@command{tar} will still archive files explicitly named on the command line +or through @option{--files-from}, regardless of where they reside. + +@table @option +@opindex one-file-system +@item --one-file-system +Prevents @command{tar} from crossing file system boundaries when +archiving. Use in conjunction with any write operation. +@end table + +The @option{--one-file-system} option causes @command{tar} to modify its +normal behavior in archiving the contents of directories. If a file in +a directory is not on the same file system as the directory itself, then +@command{tar} will not archive that file. If the file is a directory +itself, @command{tar} will not archive anything beneath it; in other words, +@command{tar} will not cross mount points. + +This option is useful for making full or incremental archival backups of +a file system. If this option is used in conjunction with +@option{--verbose} (@option{-v}), files that are excluded are +mentioned by name on the standard error. + +@menu +* directory:: Changing Directory +* absolute:: Absolute File Names +@end menu + +@node directory +@subsection Changing the Working Directory + +@FIXME{need to read over this node now for continuity; i've switched +things around some.} + +@cindex Changing directory mid-stream +@cindex Directory, changing mid-stream +@cindex Working directory, specifying +To change the working directory in the middle of a list of file names, +either on the command line or in a file specified using +@option{--files-from} (@option{-T}), use @option{--directory} (@option{-C}). +This will change the working directory to the specified directory +after that point in the list. + +@table @option +@opindex directory +@item --directory=@var{directory} +@itemx -C @var{directory} +Changes the working directory in the middle of a command line. +@end table + +For example, + +@smallexample +$ @kbd{tar -c -f jams.tar grape prune -C food cherry} +@end smallexample + +@noindent +will place the files @file{grape} and @file{prune} from the current +directory into the archive @file{jams.tar}, followed by the file +@file{cherry} from the directory @file{food}. This option is especially +useful when you have several widely separated files that you want to +store in the same archive. + +Note that the file @file{cherry} is recorded in the archive under the +precise name @file{cherry}, @emph{not} @file{food/cherry}. Thus, the +archive will contain three files that all appear to have come from the +same directory; if the archive is extracted with plain @samp{tar +--extract}, all three files will be written in the current directory. + +Contrast this with the command, + +@smallexample +$ @kbd{tar -c -f jams.tar grape prune -C food red/cherry} +@end smallexample + +@noindent +which records the third file in the archive under the name +@file{red/cherry} so that, if the archive is extracted using +@samp{tar --extract}, the third file will be written in a subdirectory +named @file{red}. + +You can use the @option{--directory} option to make the archive +independent of the original name of the directory holding the files. +The following command places the files @file{/etc/passwd}, +@file{/etc/hosts}, and @file{/lib/libc.a} into the archive +@file{foo.tar}: + +@smallexample +$ @kbd{tar -c -f foo.tar -C /etc passwd hosts -C /lib libc.a} +@end smallexample + +@noindent +However, the names of the archive members will be exactly what they were +on the command line: @file{passwd}, @file{hosts}, and @file{libc.a}. +They will not appear to be related by file name to the original +directories where those files were located. + +Note that @option{--directory} options are interpreted consecutively. If +@option{--directory} specifies a relative file name, it is interpreted +relative to the then current directory, which might not be the same as +the original current working directory of @command{tar}, due to a previous +@option{--directory} option. + +When using @option{--files-from} (@pxref{files}), you can put various +@command{tar} options (including @option{-C}) in the file list. Notice, +however, that in this case the option and its argument may not be +separated by whitespace. If you use short option, its argument must +either follow the option letter immediately, without any intervening +whitespace, or occupy the next line. Otherwise, if you use long +option, separate its argument by an equal sign. + +For instance, the file list for the above example will be: + +@smallexample +@group +-C/etc +passwd +hosts +--directory=/lib +libc.a +@end group +@end smallexample + +@noindent +To use it, you would invoke @command{tar} as follows: + +@smallexample +$ @kbd{tar -c -f foo.tar --files-from list} +@end smallexample + +The interpretation of @option{--directory} is disabled by +@option{--null} option. + +@node absolute +@subsection Absolute File Names +@cindex absolute file names +@cindex file names, absolute + +By default, @GNUTAR{} drops a leading @samp{/} on +input or output, and complains about file names containing a @file{..} +component. There is an option that turns off this behavior: + +@table @option +@opindex absolute-names +@item --absolute-names +@itemx -P +Do not strip leading slashes from file names, and permit file names +containing a @file{..} file name component. +@end table + +When @command{tar} extracts archive members from an archive, it strips any +leading slashes (@samp{/}) from the member name. This causes absolute +member names in the archive to be treated as relative file names. This +allows you to have such members extracted wherever you want, instead of +being restricted to extracting the member in the exact directory named +in the archive. For example, if the archive member has the name +@file{/etc/passwd}, @command{tar} will extract it as if the name were +really @file{etc/passwd}. + +File names containing @file{..} can cause problems when extracting, so +@command{tar} normally warns you about such files when creating an +archive, and rejects attempts to extracts such files. + +Other @command{tar} programs do not do this. As a result, if you +create an archive whose member names start with a slash, they will be +difficult for other people with a non-@GNUTAR{} +program to use. Therefore, @GNUTAR{} also strips +leading slashes from member names when putting members into the +archive. For example, if you ask @command{tar} to add the file +@file{/bin/ls} to an archive, it will do so, but the member name will +be @file{bin/ls}@footnote{A side effect of this is that when +@option{--create} is used with @option{--verbose} the resulting output +is not, generally speaking, the same as the one you'd get running +@kbd{tar --list} command. This may be important if you use some +scripts for comparing both outputs. @xref{listing member and file names}, +for the information on how to handle this case.}. + +If you use the @option{--absolute-names} (@option{-P}) option, +@command{tar} will do none of these transformations. + +To archive or extract files relative to the root directory, specify +the @option{--absolute-names} (@option{-P}) option. + +Normally, @command{tar} acts on files relative to the working +directory---ignoring superior directory names when archiving, and +ignoring leading slashes when extracting. + +When you specify @option{--absolute-names} (@option{-P}), +@command{tar} stores file names including all superior directory +names, and preserves leading slashes. If you only invoked +@command{tar} from the root directory you would never need the +@option{--absolute-names} option, but using this option +may be more convenient than switching to root. + +@FIXME{Should be an example in the tutorial/wizardry section using this +to transfer files between systems.} + +@table @option +@item --absolute-names +Preserves full file names (including superior directory names) when +archiving files. Preserves leading slash when extracting files. + +@end table + +@command{tar} prints out a message about removing the @samp{/} from +file names. This message appears once per @GNUTAR{} +invocation. It represents something which ought to be told; ignoring +what it means can cause very serious surprises, later. + +Some people, nevertheless, do not want to see this message. Wanting to +play really dangerously, one may of course redirect @command{tar} standard +error to the sink. For example, under @command{sh}: + +@smallexample +$ @kbd{tar -c -f archive.tar /home 2> /dev/null} +@end smallexample + +@noindent +Another solution, both nicer and simpler, would be to change to +the @file{/} directory first, and then avoid absolute notation. +For example: + +@smallexample +$ @kbd{tar -c -f archive.tar -C / home} +@end smallexample + +@xref{Integrity}, for some of the security-related implications +of using this option. + +@include parse-datetime.texi + +@node Formats +@chapter Controlling the Archive Format + +@cindex Tar archive formats +Due to historical reasons, there are several formats of tar archives. +All of them are based on the same principles, but have some subtle +differences that often make them incompatible with each other. + +GNU tar is able to create and handle archives in a variety of formats. +The most frequently used formats are (in alphabetical order): + +@table @asis +@item gnu +Format used by @GNUTAR{} versions up to 1.13.25. This format derived +from an early @acronym{POSIX} standard, adding some improvements such as +sparse file handling and incremental archives. Unfortunately these +features were implemented in a way incompatible with other archive +formats. + +Archives in @samp{gnu} format are able to hold file names of unlimited +length. + +@item oldgnu +Format used by @GNUTAR{} of versions prior to 1.12. + +@item v7 +Archive format, compatible with the V7 implementation of tar. This +format imposes a number of limitations. The most important of them +are: + +@enumerate +@item The maximum length of a file name is limited to 99 characters. +@item The maximum length of a symbolic link is limited to 99 characters. +@item It is impossible to store special files (block and character +devices, fifos etc.) +@item Maximum value of user or group @acronym{ID} is limited to 2097151 (7777777 +octal) +@item V7 archives do not contain symbolic ownership information (user +and group name of the file owner). +@end enumerate + +This format has traditionally been used by Automake when producing +Makefiles. This practice will change in the future, in the meantime, +however this means that projects containing file names more than 99 +characters long will not be able to use @GNUTAR{} @value{VERSION} and +Automake prior to 1.9. + +@item ustar +Archive format defined by @acronym{POSIX.1-1988} specification. It stores +symbolic ownership information. It is also able to store +special files. However, it imposes several restrictions as well: + +@enumerate +@item The maximum length of a file name is limited to 256 characters, +provided that the file name can be split at a directory separator in +two parts, first of them being at most 155 bytes long. So, in most +cases the maximum file name length will be shorter than 256 +characters. +@item The maximum length of a symbolic link name is limited to +100 characters. +@item Maximum size of a file the archive is able to accommodate +is 8GB +@item Maximum value of UID/GID is 2097151. +@item Maximum number of bits in device major and minor numbers is 21. +@end enumerate + +@item star +Format used by J@"org Schilling @command{star} +implementation. @GNUTAR{} is able to read @samp{star} archives but +currently does not produce them. + +@item posix +Archive format defined by @acronym{POSIX.1-2001} specification. This is the +most flexible and feature-rich format. It does not impose any +restrictions on file sizes or file name lengths. This format is quite +recent, so not all tar implementations are able to handle it properly. +However, this format is designed in such a way that any tar +implementation able to read @samp{ustar} archives will be able to read +most @samp{posix} archives as well, with the only exception that any +additional information (such as long file names etc.) will in such +case be extracted as plain text files along with the files it refers to. + +This archive format will be the default format for future versions +of @GNUTAR{}. + +@end table + +The following table summarizes the limitations of each of these +formats: + +@multitable @columnfractions .10 .20 .20 .20 .20 +@headitem Format @tab UID @tab File Size @tab File Name @tab Devn +@item gnu @tab 1.8e19 @tab Unlimited @tab Unlimited @tab 63 +@item oldgnu @tab 1.8e19 @tab Unlimited @tab Unlimited @tab 63 +@item v7 @tab 2097151 @tab 8GB @tab 99 @tab n/a +@item ustar @tab 2097151 @tab 8GB @tab 256 @tab 21 +@item posix @tab Unlimited @tab Unlimited @tab Unlimited @tab Unlimited +@end multitable + +The default format for @GNUTAR{} is defined at compilation +time. You may check it by running @command{tar --help}, and examining +the last lines of its output. Usually, @GNUTAR{} is configured +to create archives in @samp{gnu} format, however, future version will +switch to @samp{posix}. + +@menu +* Compression:: Using Less Space through Compression +* Attributes:: Handling File Attributes +* Portability:: Making @command{tar} Archives More Portable +* cpio:: Comparison of @command{tar} and @command{cpio} +@end menu + +@node Compression +@section Using Less Space through Compression + +@menu +* gzip:: Creating and Reading Compressed Archives +* sparse:: Archiving Sparse Files +@end menu + +@node gzip +@subsection Creating and Reading Compressed Archives +@cindex Compressed archives +@cindex Storing archives in compressed format + +@cindex gzip +@cindex bzip2 +@cindex lzip +@cindex lzma +@cindex lzop +@cindex compress +@GNUTAR{} is able to create and read compressed archives. It supports +a wide variety of compression programs, namely: @command{gzip}, +@command{bzip2}, @command{lzip}, @command{lzma}, @command{lzop}, +@command{xz} and traditional @command{compress}. The latter is +supported mostly for backward compatibility, and we recommend +against using it, because it is by far less effective than the other +compression programs@footnote{It also had patent problems in the past.}. + +Creating a compressed archive is simple: you just specify a +@dfn{compression option} along with the usual archive creation +commands. The compression option is @option{-z} (@option{--gzip}) to +create a @command{gzip} compressed archive, @option{-j} +(@option{--bzip2}) to create a @command{bzip2} compressed archive, +@option{--lzip} to create an @asis{lzip} compressed archive, +@option{-J} (@option{--xz}) to create an @asis{XZ} archive, +@option{--lzma} to create an @asis{LZMA} compressed +archive, @option{--lzop} to create an @asis{LSOP} archive, and +@option{-Z} (@option{--compress}) to use @command{compress} program. +For example: + +@smallexample +$ @kbd{tar cfz archive.tar.gz .} +@end smallexample + +You can also let @GNUTAR{} select the compression program based on +the suffix of the archive file name. This is done using +@option{--auto-compress} (@option{-a}) command line option. For +example, the following invocation will use @command{bzip2} for +compression: + +@smallexample +$ @kbd{tar cfa archive.tar.bz2 .} +@end smallexample + +@noindent +whereas the following one will use @command{lzma}: + +@smallexample +$ @kbd{tar cfa archive.tar.lzma .} +@end smallexample + +For a complete list of file name suffixes recognized by @GNUTAR{}, +see @ref{auto-compress}. + +Reading compressed archive is even simpler: you don't need to specify +any additional options as @GNUTAR{} recognizes its format +automatically. Thus, the following commands will list and extract the +archive created in previous example: + +@smallexample +# List the compressed archive +$ @kbd{tar tf archive.tar.gz} +# Extract the compressed archive +$ @kbd{tar xf archive.tar.gz} +@end smallexample + +The format recognition algorithm is based on @dfn{signatures}, a +special byte sequences in the beginning of file, that are specific for +certain compression formats. If this approach fails, @command{tar} +falls back to using archive name suffix to determine its format +(@pxref{auto-compress}, for a list of recognized suffixes). + +@anchor{alternative decompression programs} +@cindex alternative decompression programs +Some compression programs are able to handle different compression +formats. @GNUTAR{} uses this, if the principal decompressor for the +given format is not available. For example, if @command{compress} is +not installed, @command{tar} will try to use @command{gzip}. As of +version @value{VERSION} the following alternatives are +tried@footnote{To verbosely trace the decompressor selection, use the +@option{--warning=decompress-program} option +(@pxref{warnings,decompress-program}).}: + +@multitable @columnfractions 0.3 0.3 0.3 +@headitem Format @tab Main decompressor @tab Alternatives +@item compress @tab compress @tab gzip +@item lzma @tab lzma @tab xz +@item bzip2 @tab bzip2 @tab lbzip2 +@end multitable + +The only case when you have to specify a decompression option while +reading the archive is when reading from a pipe or from a tape drive +that does not support random access. However, in this case @GNUTAR{} +will indicate which option you should use. For example: + +@smallexample +$ @kbd{cat archive.tar.gz | tar tf -} +tar: Archive is compressed. Use -z option +tar: Error is not recoverable: exiting now +@end smallexample + +If you see such diagnostics, just add the suggested option to the +invocation of @GNUTAR{}: + +@smallexample +$ @kbd{cat archive.tar.gz | tar tfz -} +@end smallexample + +Notice also, that there are several restrictions on operations on +compressed archives. First of all, compressed archives cannot be +modified, i.e., you cannot update (@option{--update}, alias @option{-u}) +them or delete (@option{--delete}) members from them or +add (@option{--append}, alias @option{-r}) members to them. Likewise, you +cannot append another @command{tar} archive to a compressed archive using +@option{--concatenate} (@option{-A}). Secondly, multi-volume +archives cannot be compressed. + +The following options allow to select a particular compressor program: + +@table @option +@opindex gzip +@opindex ungzip +@item -z +@itemx --gzip +@itemx --ungzip +Filter the archive through @command{gzip}. + +@opindex xz +@item -J +@itemx --xz +Filter the archive through @code{xz}. + +@item -j +@itemx --bzip2 +Filter the archive through @code{bzip2}. + +@opindex lzip +@item --lzip +Filter the archive through @command{lzip}. + +@opindex lzma +@item --lzma +Filter the archive through @command{lzma}. + +@opindex lzop +@item --lzop +Filter the archive through @command{lzop}. + +@opindex compress +@opindex uncompress +@item -Z +@itemx --compress +@itemx --uncompress +Filter the archive through @command{compress}. +@end table + +When any of these options is given, @GNUTAR{} searches the compressor +binary in the current path and invokes it. The name of the compressor +program is specified at compilation time using a corresponding +@option{--with-@var{compname}} option to @command{configure}, e.g. +@option{--with-bzip2} to select a specific @command{bzip2} binary. +@xref{lbzip2}, for a detailed discussion. + +The output produced by @command{tar --help} shows the actual +compressor names along with each of these options. + +You can use any of these options on physical devices (tape drives, +etc.) and remote files as well as on normal files; data to or from +such devices or remote files is reblocked by another copy of the +@command{tar} program to enforce the specified (or default) record +size. The default compression parameters are used. Most compression +programs allow to override these by setting a program-specific +environment variable. For example, when using @command{gzip} you can +use @env{GZIP} as in the example below: + +@smallexample +$ @kbd{GZIP=--best tar cfz archive.tar.gz subdir} +@end smallexample + +@noindent +Another way would be to use the @option{-I} option instead (see +below), e.g.: + +@smallexample +$ @kbd{tar -cf archive.tar.gz -I 'gzip --best' subdir} +@end smallexample + +@noindent +Finally, the third, traditional, way to achieve the same result is to +use pipe: + +@smallexample +$ @kbd{tar cf - subdir | gzip --best -c - > archive.tar.gz} +@end smallexample + +@cindex corrupted archives +About corrupted compressed archives: compressed files have no +redundancy, for maximum compression. The adaptive nature of the +compression scheme means that the compression tables are implicitly +spread all over the archive. If you lose a few blocks, the dynamic +construction of the compression tables becomes unsynchronized, and there +is little chance that you could recover later in the archive. + +Another compression options provide a better control over creating +compressed archives. These are: + +@table @option +@anchor{auto-compress} +@opindex auto-compress +@item --auto-compress +@itemx -a +Select a compression program to use by the archive file name +suffix. The following suffixes are recognized: + +@multitable @columnfractions 0.3 0.6 +@headitem Suffix @tab Compression program +@item @samp{.gz} @tab @command{gzip} +@item @samp{.tgz} @tab @command{gzip} +@item @samp{.taz} @tab @command{gzip} +@item @samp{.Z} @tab @command{compress} +@item @samp{.taZ} @tab @command{compress} +@item @samp{.bz2} @tab @command{bzip2} +@item @samp{.tz2} @tab @command{bzip2} +@item @samp{.tbz2} @tab @command{bzip2} +@item @samp{.tbz} @tab @command{bzip2} +@item @samp{.lz} @tab @command{lzip} +@item @samp{.lzma} @tab @command{lzma} +@item @samp{.tlz} @tab @command{lzma} +@item @samp{.lzo} @tab @command{lzop} +@item @samp{.xz} @tab @command{xz} +@end multitable + +@opindex use-compress-program +@item --use-compress-program=@var{prog} +@itemx -I=@var{prog} +Use external compression program @var{prog}. Use this option if you +are not happy with the compression program associated with the suffix +at compile time or if you have a compression program that @GNUTAR{} +does not support. There are two requirements to which @var{prog} +should comply: + +First, when called without options, it should read data from standard +input, compress it and output it on standard output. + +Secondly, if called with @option{-d} argument, it should do exactly +the opposite, i.e., read the compressed data from the standard input +and produce uncompressed data on the standard output. +@end table + +@cindex gpg, using with tar +@cindex gnupg, using with tar +@cindex Using encrypted archives +The @option{--use-compress-program} option, in particular, lets you +implement your own filters, not necessarily dealing with +compression/decompression. For example, suppose you wish to implement +PGP encryption on top of compression, using @command{gpg} (@pxref{Top, +gpg, gpg ---- encryption and signing tool, gpg, GNU Privacy Guard +Manual}). The following script does that: + +@smallexample +@group +#! /bin/sh +case $1 in +-d) gpg --decrypt - | gzip -d -c;; +'') gzip -c | gpg -s;; +*) echo "Unknown option $1">&2; exit 1;; +esac +@end group +@end smallexample + +Suppose you name it @file{gpgz} and save it somewhere in your +@env{PATH}. Then the following command will create a compressed +archive signed with your private key: + +@smallexample +$ @kbd{tar -cf foo.tar.gpgz -Igpgz .} +@end smallexample + +@noindent +Likewise, the command below will list its contents: + +@smallexample +$ @kbd{tar -tf foo.tar.gpgz -Igpgz .} +@end smallexample + +@ignore +The above is based on the following discussion: + + I have one question, or maybe it's a suggestion if there isn't a way + to do it now. I would like to use @option{--gzip}, but I'd also like + the output to be fed through a program like @acronym{GNU} + @command{ecc} (actually, right now that's @samp{exactly} what I'd like + to use :-)), basically adding ECC protection on top of compression. + It seems as if this should be quite easy to do, but I can't work out + exactly how to go about it. Of course, I can pipe the standard output + of @command{tar} through @command{ecc}, but then I lose (though I + haven't started using it yet, I confess) the ability to have + @command{tar} use @command{rmt} for it's I/O (I think). + + I think the most straightforward thing would be to let me specify a + general set of filters outboard of compression (preferably ordered, + so the order can be automatically reversed on input operations, and + with the options they require specifiable), but beggars shouldn't be + choosers and anything you decide on would be fine with me. + + By the way, I like @command{ecc} but if (as the comments say) it can't + deal with loss of block sync, I'm tempted to throw some time at adding + that capability. Supposing I were to actually do such a thing and + get it (apparently) working, do you accept contributed changes to + utilities like that? (Leigh Clayton @file{loc@@soliton.com}, May 1995). + + Isn't that exactly the role of the + @option{--use-compress-prog=@var{program}} option? + I never tried it myself, but I suspect you may want to write a + @var{prog} script or program able to filter stdin to stdout to + way you want. It should recognize the @option{-d} option, for when + extraction is needed rather than creation. + + It has been reported that if one writes compressed data (through the + @option{--gzip} or @option{--compress} options) to a DLT and tries to use + the DLT compression mode, the data will actually get bigger and one will + end up with less space on the tape. +@end ignore + +@menu +* lbzip2:: Using lbzip2 with @GNUTAR{}. +@end menu + +@node lbzip2 +@subsubsection Using lbzip2 with @GNUTAR{}. +@cindex lbzip2 +@cindex Laszlo Ersek + @command{Lbzip2} is a multithreaded utility for handling +@samp{bzip2} compression, written by Laszlo Ersek. It makes use of +multiple processors to speed up its operation and in general works +considerably faster than @command{bzip2}. For a detailed description +of @command{lbzip2} see @uref{http://freshmeat.net/@/projects/@/lbzip2} and +@uref{http://www.linuxinsight.com/@/lbzip2-parallel-bzip2-utility.html, +lbzip2: parallel bzip2 utility}. + + Recent versions of @command{lbzip2} are mostly command line compatible +with @command{bzip2}, which makes it possible to automatically invoke +it via the @option{--bzip2} @GNUTAR{} command line option. To do so, +@GNUTAR{} must be configured with the @option{--with-bzip2} command +line option, like this: + +@smallexample +$ @kbd{./configure --with-bzip2=lbzip2 [@var{other-options}]} +@end smallexample + + Once configured and compiled this way, @command{tar --help} will show the +following: + +@smallexample +@group +$ @kbd{tar --help | grep -- --bzip2} + -j, --bzip2 filter the archive through lbzip2 +@end group +@end smallexample + +@noindent +which means that running @command{tar --bzip2} will invoke @command{lbzip2}. + +@node sparse +@subsection Archiving Sparse Files +@cindex Sparse Files + +Files in the file system occasionally have @dfn{holes}. A @dfn{hole} +in a file is a section of the file's contents which was never written. +The contents of a hole reads as all zeros. On many operating systems, +actual disk storage is not allocated for holes, but they are counted +in the length of the file. If you archive such a file, @command{tar} +could create an archive longer than the original. To have @command{tar} +attempt to recognize the holes in a file, use @option{--sparse} +(@option{-S}). When you use this option, then, for any file using +less disk space than would be expected from its length, @command{tar} +searches the file for consecutive stretches of zeros. It then records +in the archive for the file where the consecutive stretches of zeros +are, and only archives the ``real contents'' of the file. On +extraction (using @option{--sparse} is not needed on extraction) any +such files have holes created wherever the continuous stretches of zeros +were found. Thus, if you use @option{--sparse}, @command{tar} archives +won't take more space than the original. + +@table @option +@opindex sparse +@item -S +@itemx --sparse +This option instructs @command{tar} to test each file for sparseness +before attempting to archive it. If the file is found to be sparse it +is treated specially, thus allowing to decrease the amount of space +used by its image in the archive. + +This option is meaningful only when creating or updating archives. It +has no effect on extraction. +@end table + +Consider using @option{--sparse} when performing file system backups, +to avoid archiving the expanded forms of files stored sparsely in the +system. + +Even if your system has no sparse files currently, some may be +created in the future. If you use @option{--sparse} while making file +system backups as a matter of course, you can be assured the archive +will never take more space on the media than the files take on disk +(otherwise, archiving a disk filled with sparse files might take +hundreds of tapes). @xref{Incremental Dumps}. + +However, be aware that @option{--sparse} option presents a serious +drawback. Namely, in order to determine if the file is sparse +@command{tar} has to read it before trying to archive it, so in total +the file is read @strong{twice}. So, always bear in mind that the +time needed to process all files with this option is roughly twice +the time needed to archive them without it. +@FIXME{A technical note: + +Programs like @command{dump} do not have to read the entire file; by +examining the file system directly, they can determine in advance +exactly where the holes are and thus avoid reading through them. The +only data it need read are the actual allocated data blocks. +@GNUTAR{} uses a more portable and straightforward +archiving approach, it would be fairly difficult that it does +otherwise. Elizabeth Zwicky writes to @file{comp.unix.internals}, on +1990-12-10: + +@quotation +What I did say is that you cannot tell the difference between a hole and an +equivalent number of nulls without reading raw blocks. @code{st_blocks} at +best tells you how many holes there are; it doesn't tell you @emph{where}. +Just as programs may, conceivably, care what @code{st_blocks} is (care +to name one that does?), they may also care where the holes are (I have +no examples of this one either, but it's equally imaginable). + +I conclude from this that good archivers are not portable. One can +arguably conclude that if you want a portable program, you can in good +conscience restore files with as many holes as possible, since you can't +get it right. +@end quotation +} + +@cindex sparse formats, defined +When using @samp{POSIX} archive format, @GNUTAR{} is able to store +sparse files using in three distinct ways, called @dfn{sparse +formats}. A sparse format is identified by its @dfn{number}, +consisting, as usual of two decimal numbers, delimited by a dot. By +default, format @samp{1.0} is used. If, for some reason, you wish to +use an earlier format, you can select it using +@option{--sparse-version} option. + +@table @option +@opindex sparse-version +@item --sparse-version=@var{version} + +Select the format to store sparse files in. Valid @var{version} values +are: @samp{0.0}, @samp{0.1} and @samp{1.0}. @xref{Sparse Formats}, +for a detailed description of each format. +@end table + +Using @option{--sparse-format} option implies @option{--sparse}. + +@node Attributes +@section Handling File Attributes +@cindex atrributes, files +@cindex file attributes + +When @command{tar} reads files, it updates their access times. To +avoid this, use the @option{--atime-preserve[=METHOD]} option, which can either +reset the access time retroactively or avoid changing it in the first +place. + +@table @option +@opindex atime-preserve +@item --atime-preserve +@itemx --atime-preserve=replace +@itemx --atime-preserve=system +Preserve the access times of files that are read. This works only for +files that you own, unless you have superuser privileges. + +@option{--atime-preserve=replace} works on most systems, but it also +restores the data modification time and updates the status change +time. Hence it doesn't interact with incremental dumps nicely +(@pxref{Incremental Dumps}), and it can set access or data modification times +incorrectly if other programs access the file while @command{tar} is +running. + +@option{--atime-preserve=system} avoids changing the access time in +the first place, if the operating system supports this. +Unfortunately, this may or may not work on any given operating system +or file system. If @command{tar} knows for sure it won't work, it +complains right away. + +Currently @option{--atime-preserve} with no operand defaults to +@option{--atime-preserve=replace}, but this is intended to change to +@option{--atime-preserve=system} when the latter is better-supported. + +@opindex touch +@item -m +@itemx --touch +Do not extract data modification time. + +When this option is used, @command{tar} leaves the data modification times +of the files it extracts as the times when the files were extracted, +instead of setting it to the times recorded in the archive. + +This option is meaningless with @option{--list} (@option{-t}). + +@opindex same-owner +@item --same-owner +Create extracted files with the same ownership they have in the +archive. + +This is the default behavior for the superuser, +so this option is meaningful only for non-root users, when @command{tar} +is executed on those systems able to give files away. This is +considered as a security flaw by many people, at least because it +makes quite difficult to correctly account users for the disk space +they occupy. Also, the @code{suid} or @code{sgid} attributes of +files are easily and silently lost when files are given away. + +When writing an archive, @command{tar} writes the user @acronym{ID} and user name +separately. If it can't find a user name (because the user @acronym{ID} is not +in @file{/etc/passwd}), then it does not write one. When restoring, +it tries to look the name (if one was written) up in +@file{/etc/passwd}. If it fails, then it uses the user @acronym{ID} stored in +the archive instead. + +@opindex no-same-owner +@item --no-same-owner +@itemx -o +Do not attempt to restore ownership when extracting. This is the +default behavior for ordinary users, so this option has an effect +only for the superuser. + +@opindex numeric-owner +@item --numeric-owner +The @option{--numeric-owner} option allows (ANSI) archives to be written +without user/group name information or such information to be ignored +when extracting. It effectively disables the generation and/or use +of user/group name information. This option forces extraction using +the numeric ids from the archive, ignoring the names. + +This is useful in certain circumstances, when restoring a backup from +an emergency floppy with different passwd/group files for example. +It is otherwise impossible to extract files with the right ownerships +if the password file in use during the extraction does not match the +one belonging to the file system(s) being extracted. This occurs, +for example, if you are restoring your files after a major crash and +had booted from an emergency floppy with no password file or put your +disk into another machine to do the restore. + +The numeric ids are @emph{always} saved into @command{tar} archives. +The identifying names are added at create time when provided by the +system, unless @option{--format=oldgnu} is used. Numeric ids could be +used when moving archives between a collection of machines using +a centralized management for attribution of numeric ids to users +and groups. This is often made through using the NIS capabilities. + +When making a @command{tar} file for distribution to other sites, it +is sometimes cleaner to use a single owner for all files in the +distribution, and nicer to specify the write permission bits of the +files as stored in the archive independently of their actual value on +the file system. The way to prepare a clean distribution is usually +to have some Makefile rule creating a directory, copying all needed +files in that directory, then setting ownership and permissions as +wanted (there are a lot of possible schemes), and only then making a +@command{tar} archive out of this directory, before cleaning +everything out. Of course, we could add a lot of options to +@GNUTAR{} for fine tuning permissions and ownership. +This is not the good way, I think. @GNUTAR{} is +already crowded with options and moreover, the approach just explained +gives you a great deal of control already. + +@xopindex{same-permissions, short description} +@xopindex{preserve-permissions, short description} +@item -p +@itemx --same-permissions +@itemx --preserve-permissions +Extract all protection information. + +This option causes @command{tar} to set the modes (access permissions) of +extracted files exactly as recorded in the archive. If this option +is not used, the current @code{umask} setting limits the permissions +on extracted files. This option is by default enabled when +@command{tar} is executed by a superuser. + + +This option is meaningless with @option{--list} (@option{-t}). + +@opindex preserve +@item --preserve +Same as both @option{--same-permissions} and @option{--same-order}. + +This option is deprecated, and will be removed in @GNUTAR{} version 1.23. + +@end table + +@node Portability +@section Making @command{tar} Archives More Portable + +Creating a @command{tar} archive on a particular system that is meant to be +useful later on many other machines and with other versions of @command{tar} +is more challenging than you might think. @command{tar} archive formats +have been evolving since the first versions of Unix. Many such formats +are around, and are not always compatible with each other. This section +discusses a few problems, and gives some advice about making @command{tar} +archives more portable. + +One golden rule is simplicity. For example, limit your @command{tar} +archives to contain only regular files and directories, avoiding +other kind of special files. Do not attempt to save sparse files or +contiguous files as such. Let's discuss a few more problems, in turn. + +@FIXME{Discuss GNU extensions (incremental backups, multi-volume +archives and archive labels) in GNU and PAX formats.} + +@menu +* Portable Names:: Portable Names +* dereference:: Symbolic Links +* hard links:: Hard Links +* old:: Old V7 Archives +* ustar:: Ustar Archives +* gnu:: GNU and old GNU format archives. +* posix:: @acronym{POSIX} archives +* Checksumming:: Checksumming Problems +* Large or Negative Values:: Large files, negative time stamps, etc. +* Other Tars:: How to Extract GNU-Specific Data Using + Other @command{tar} Implementations +@end menu + +@node Portable Names +@subsection Portable Names + +Use portable file and member names. A name is portable if it contains +only @acronym{ASCII} letters and digits, @samp{/}, @samp{.}, @samp{_}, and +@samp{-}; it cannot be empty, start with @samp{-} or @samp{//}, or +contain @samp{/-}. Avoid deep directory nesting. For portability to +old Unix hosts, limit your file name components to 14 characters or +less. + +If you intend to have your @command{tar} archives to be read under +MSDOS, you should not rely on case distinction for file names, and you +might use the @acronym{GNU} @command{doschk} program for helping you +further diagnosing illegal MSDOS names, which are even more limited +than System V's. + +@node dereference +@subsection Symbolic Links +@cindex File names, using symbolic links +@cindex Symbolic link as file name + +@opindex dereference +Normally, when @command{tar} archives a symbolic link, it writes a +block to the archive naming the target of the link. In that way, the +@command{tar} archive is a faithful record of the file system contents. +When @option{--dereference} (@option{-h}) is used with +@option{--create} (@option{-c}), @command{tar} archives the files +symbolic links point to, instead of +the links themselves. + +When creating portable archives, use @option{--dereference} +(@option{-h}): some systems do not support +symbolic links, and moreover, your distribution might be unusable if +it contains unresolved symbolic links. + +When reading from an archive, the @option{--dereference} (@option{-h}) +option causes @command{tar} to follow an already-existing symbolic +link when @command{tar} writes or reads a file named in the archive. +Ordinarily, @command{tar} does not follow such a link, though it may +remove the link before writing a new file. @xref{Dealing with Old +Files}. + +The @option{--dereference} option is unsafe if an untrusted user can +modify directories while @command{tar} is running. @xref{Security}. + +@node hard links +@subsection Hard Links +@cindex File names, using hard links +@cindex hard links, dereferencing +@cindex dereferencing hard links + +Normally, when @command{tar} archives a hard link, it writes a +block to the archive naming the target of the link (a @samp{1} type +block). In that way, the actual file contents is stored in file only +once. For example, consider the following two files: + +@smallexample +@group +$ ls -l +-rw-r--r-- 2 gray staff 4 2007-10-30 15:11 one +-rw-r--r-- 2 gray staff 4 2007-10-30 15:11 jeden +@end group +@end smallexample + +Here, @file{jeden} is a link to @file{one}. When archiving this +directory with a verbose level 2, you will get an output similar to +the following: + +@smallexample +$ tar cfvv ../archive.tar . +drwxr-xr-x gray/staff 0 2007-10-30 15:13 ./ +-rw-r--r-- gray/staff 4 2007-10-30 15:11 ./jeden +hrw-r--r-- gray/staff 0 2007-10-30 15:11 ./one link to ./jeden +@end smallexample + +The last line shows that, instead of storing two copies of the file, +@command{tar} stored it only once, under the name @file{jeden}, and +stored file @file{one} as a hard link to this file. + +It may be important to know that all hard links to the given file are +stored in the archive. For example, this may be necessary for exact +reproduction of the file system. The following option does that: + +@table @option +@xopindex{check-links, described} +@item --check-links +@itemx -l +Check the number of links dumped for each processed file. If this +number does not match the total number of hard links for the file, print +a warning message. +@end table + +For example, trying to archive only file @file{jeden} with this option +produces the following diagnostics: + +@smallexample +$ tar -c -f ../archive.tar -l jeden +tar: Missing links to `jeden'. +@end smallexample + +Although creating special records for hard links helps keep a faithful +record of the file system contents and makes archives more compact, it +may present some difficulties when extracting individual members from +the archive. For example, trying to extract file @file{one} from the +archive created in previous examples produces, in the absense of file +@file{jeden}: + +@smallexample +$ tar xf archive.tar ./one +tar: ./one: Cannot hard link to `./jeden': No such file or directory +tar: Error exit delayed from previous errors +@end smallexample + +The reason for this behavior is that @command{tar} cannot seek back in +the archive to the previous member (in this case, @file{one}), to +extract it@footnote{There are plans to fix this in future releases.}. +If you wish to avoid such problems at the cost of a bigger archive, +use the following option: + +@table @option +@xopindex{hard-dereference, described} +@item --hard-dereference +Dereference hard links and store the files they refer to. +@end table + +For example, trying this option on our two sample files, we get two +copies in the archive, each of which can then be extracted +independently of the other: + +@smallexample +@group +$ tar -c -vv -f ../archive.tar --hard-dereference . +drwxr-xr-x gray/staff 0 2007-10-30 15:13 ./ +-rw-r--r-- gray/staff 4 2007-10-30 15:11 ./jeden +-rw-r--r-- gray/staff 4 2007-10-30 15:11 ./one +@end group +@end smallexample + +@node old +@subsection Old V7 Archives +@cindex Format, old style +@cindex Old style format +@cindex Old style archives +@cindex v7 archive format + +Certain old versions of @command{tar} cannot handle additional +information recorded by newer @command{tar} programs. To create an +archive in V7 format (not ANSI), which can be read by these old +versions, specify the @option{--format=v7} option in +conjunction with the @option{--create} (@option{-c}) (@command{tar} also +accepts @option{--portability} or @option{--old-archive} for this +option). When you specify it, +@command{tar} leaves out information about directories, pipes, fifos, +contiguous files, and device files, and specifies file ownership by +group and user IDs instead of group and user names. + +When updating an archive, do not use @option{--format=v7} +unless the archive was created using this option. + +In most cases, a @emph{new} format archive can be read by an @emph{old} +@command{tar} program without serious trouble, so this option should +seldom be needed. On the other hand, most modern @command{tar}s are +able to read old format archives, so it might be safer for you to +always use @option{--format=v7} for your distributions. Notice, +however, that @samp{ustar} format is a better alternative, as it is +free from many of @samp{v7}'s drawbacks. + +@node ustar +@subsection Ustar Archive Format + +@cindex ustar archive format +Archive format defined by @acronym{POSIX}.1-1988 specification is called +@code{ustar}. Although it is more flexible than the V7 format, it +still has many restrictions (@pxref{Formats,ustar}, for the detailed +description of @code{ustar} format). Along with V7 format, +@code{ustar} format is a good choice for archives intended to be read +with other implementations of @command{tar}. + +To create archive in @code{ustar} format, use @option{--format=ustar} +option in conjunction with the @option{--create} (@option{-c}). + +@node gnu +@subsection @acronym{GNU} and old @GNUTAR{} format + +@cindex GNU archive format +@cindex Old GNU archive format +@GNUTAR{} was based on an early draft of the +@acronym{POSIX} 1003.1 @code{ustar} standard. @acronym{GNU} extensions to +@command{tar}, such as the support for file names longer than 100 +characters, use portions of the @command{tar} header record which were +specified in that @acronym{POSIX} draft as unused. Subsequent changes in +@acronym{POSIX} have allocated the same parts of the header record for +other purposes. As a result, @GNUTAR{} format is +incompatible with the current @acronym{POSIX} specification, and with +@command{tar} programs that follow it. + +In the majority of cases, @command{tar} will be configured to create +this format by default. This will change in future releases, since +we plan to make @samp{POSIX} format the default. + +To force creation a @GNUTAR{} archive, use option +@option{--format=gnu}. + +@node posix +@subsection @GNUTAR{} and @acronym{POSIX} @command{tar} + +@cindex POSIX archive format +@cindex PAX archive format +Starting from version 1.14 @GNUTAR{} features full support for +@acronym{POSIX.1-2001} archives. + +A @acronym{POSIX} conformant archive will be created if @command{tar} +was given @option{--format=posix} (@option{--format=pax}) option. No +special option is required to read and extract from a @acronym{POSIX} +archive. + +@menu +* PAX keywords:: Controlling Extended Header Keywords. +@end menu + +@node PAX keywords +@subsubsection Controlling Extended Header Keywords + +@table @option +@opindex pax-option +@item --pax-option=@var{keyword-list} +Handle keywords in @acronym{PAX} extended headers. This option is +equivalent to @option{-o} option of the @command{pax} utility. +@end table + +@var{Keyword-list} is a comma-separated +list of keyword options, each keyword option taking one of +the following forms: + +@table @code +@item delete=@var{pattern} +When used with one of archive-creation commands, +this option instructs @command{tar} to omit from extended header records +that it produces any keywords matching the string @var{pattern}. + +When used in extract or list mode, this option instructs tar +to ignore any keywords matching the given @var{pattern} in the extended +header records. In both cases, matching is performed using the pattern +matching notation described in @acronym{POSIX 1003.2}, 3.13 +(@pxref{wildcards}). For example: + +@smallexample +--pax-option delete=security.* +@end smallexample + +would suppress security-related information. + +@item exthdr.name=@var{string} + +This keyword allows user control over the name that is written into the +ustar header blocks for the extended headers. The name is obtained +from @var{string} after making the following substitutions: + +@multitable @columnfractions .25 .55 +@headitem Meta-character @tab Replaced By +@item %d @tab The directory name of the file, equivalent to the +result of the @command{dirname} utility on the translated file name. +@item %f @tab The name of the file with the directory information +stripped, equivalent to the result of the @command{basename} utility +on the translated file name. +@item %p @tab The process @acronym{ID} of the @command{tar} process. +@item %% @tab A @samp{%} character. +@end multitable + +Any other @samp{%} characters in @var{string} produce undefined +results. + +If no option @samp{exthdr.name=string} is specified, @command{tar} +will use the following default value: + +@smallexample +%d/PaxHeaders.%p/%f +@end smallexample + +@item exthdr.mtime=@var{value} + +This keyword defines the value of the @samp{mtime} field that +is written into the ustar header blocks for the extended headers. +By default, the @samp{mtime} field is set to the modification time +of the archive member described by that extended headers. + +@item globexthdr.name=@var{string} +This keyword allows user control over the name that is written into +the ustar header blocks for global extended header records. The name +is obtained from the contents of @var{string}, after making +the following substitutions: + +@multitable @columnfractions .25 .55 +@headitem Meta-character @tab Replaced By +@item %n @tab An integer that represents the +sequence number of the global extended header record in the archive, +starting at 1. +@item %p @tab The process @acronym{ID} of the @command{tar} process. +@item %% @tab A @samp{%} character. +@end multitable + +Any other @samp{%} characters in @var{string} produce undefined results. + +If no option @samp{globexthdr.name=string} is specified, @command{tar} +will use the following default value: + +@smallexample +$TMPDIR/GlobalHead.%p.%n +@end smallexample + +@noindent +where @samp{$TMPDIR} represents the value of the @var{TMPDIR} +environment variable. If @var{TMPDIR} is not set, @command{tar} +uses @samp{/tmp}. + +@item globexthdr.mtime=@var{value} + +This keyword defines the value of the @samp{mtime} field that +is written into the ustar header blocks for the global extended headers. +By default, the @samp{mtime} field is set to the time when +@command{tar} was invoked. + +@item @var{keyword}=@var{value} +When used with one of archive-creation commands, these keyword/value pairs +will be included at the beginning of the archive in a global extended +header record. When used with one of archive-reading commands, +@command{tar} will behave as if it has encountered these keyword/value +pairs at the beginning of the archive in a global extended header +record. + +@item @var{keyword}:=@var{value} +When used with one of archive-creation commands, these keyword/value pairs +will be included as records at the beginning of an extended header for +each file. This is effectively equivalent to @var{keyword}=@var{value} +form except that it creates no global extended header records. + +When used with one of archive-reading commands, @command{tar} will +behave as if these keyword/value pairs were included as records at the +end of each extended header; thus, they will override any global or +file-specific extended header record keywords of the same names. +For example, in the command: + +@smallexample +tar --format=posix --create \ + --file archive --pax-option gname:=user . +@end smallexample + +the group name will be forced to a new value for all files +stored in the archive. +@end table + +In any of the forms described above, the @var{value} may be +a string enclosed in curly braces. In that case, the string +between the braces is understood either as a textual time +representation, as described in @ref{Date input formats}, or a name of +the existing file, starting with @samp{/} or @samp{.}. In the latter +case, the modification time of that file is used. + +For example, to set all modification times to the current date, you +use the following option: + +@smallexample +--pax-option='mtime:=@{now@}' +@end smallexample + +Note quoting of the option's argument. + +@cindex archives, binary equivalent +@cindex binary equivalent archives, creating +As another example, here is the option that ensures that any two +archives created using it, will be binary equivalent if they have the +same contents: + +@smallexample +--pax-option=exthdr.name=%d/PaxHeaders/%f,atime:=0 +@end smallexample + +@node Checksumming +@subsection Checksumming Problems + +SunOS and HP-UX @command{tar} fail to accept archives created using +@GNUTAR{} and containing non-@acronym{ASCII} file names, that +is, file names having characters with the eight bit set, because they +use signed checksums, while @GNUTAR{} uses unsigned +checksums while creating archives, as per @acronym{POSIX} standards. On +reading, @GNUTAR{} computes both checksums and +accepts any. It is somewhat worrying that a lot of people may go +around doing backup of their files using faulty (or at least +non-standard) software, not learning about it until it's time to +restore their missing files with an incompatible file extractor, or +vice versa. + +@GNUTAR{} computes checksums both ways, and accept +any on read, so @acronym{GNU} tar can read Sun tapes even with their +wrong checksums. @GNUTAR{} produces the standard +checksum, however, raising incompatibilities with Sun. That is to +say, @GNUTAR{} has not been modified to +@emph{produce} incorrect archives to be read by buggy @command{tar}'s. +I've been told that more recent Sun @command{tar} now read standard +archives, so maybe Sun did a similar patch, after all? + +The story seems to be that when Sun first imported @command{tar} +sources on their system, they recompiled it without realizing that +the checksums were computed differently, because of a change in +the default signing of @code{char}'s in their compiler. So they +started computing checksums wrongly. When they later realized their +mistake, they merely decided to stay compatible with it, and with +themselves afterwards. Presumably, but I do not really know, HP-UX +has chosen that their @command{tar} archives to be compatible with Sun's. +The current standards do not favor Sun @command{tar} format. In any +case, it now falls on the shoulders of SunOS and HP-UX users to get +a @command{tar} able to read the good archives they receive. + +@node Large or Negative Values +@subsection Large or Negative Values +@cindex large values +@cindex future time stamps +@cindex negative time stamps +@UNREVISED + +The above sections suggest to use @samp{oldest possible} archive +format if in doubt. However, sometimes it is not possible. If you +attempt to archive a file whose metadata cannot be represented using +required format, @GNUTAR{} will print error message and ignore such a +file. You will than have to switch to a format that is able to +handle such values. The format summary table (@pxref{Formats}) will +help you to do so. + +In particular, when trying to archive files larger than 8GB or with +timestamps not in the range 1970-01-01 00:00:00 through 2242-03-16 +12:56:31 @sc{utc}, you will have to chose between @acronym{GNU} and +@acronym{POSIX} archive formats. When considering which format to +choose, bear in mind that the @acronym{GNU} format uses +two's-complement base-256 notation to store values that do not fit +into standard @acronym{ustar} range. Such archives can generally be +read only by a @GNUTAR{} implementation. Moreover, they sometimes +cannot be correctly restored on another hosts even by @GNUTAR{}. For +example, using two's complement representation for negative time +stamps that assumes a signed 32-bit @code{time_t} generates archives +that are not portable to hosts with differing @code{time_t} +representations. + +On the other hand, @acronym{POSIX} archives, generally speaking, can +be extracted by any tar implementation that understands older +@acronym{ustar} format. The only exception are files larger than 8GB. + +@FIXME{Describe how @acronym{POSIX} archives are extracted by non +POSIX-aware tars.} + +@node Other Tars +@subsection How to Extract GNU-Specific Data Using Other @command{tar} Implementations + +In previous sections you became acquainted with various quirks +necessary to make your archives portable. Sometimes you may need to +extract archives containing GNU-specific members using some +third-party @command{tar} implementation or an older version of +@GNUTAR{}. Of course your best bet is to have @GNUTAR{} installed, +but if it is for some reason impossible, this section will explain +how to cope without it. + +When we speak about @dfn{GNU-specific} members we mean two classes of +them: members split between the volumes of a multi-volume archive and +sparse members. You will be able to always recover such members if +the archive is in PAX format. In addition split members can be +recovered from archives in old GNU format. The following subsections +describe the required procedures in detail. + +@menu +* Split Recovery:: Members Split Between Volumes +* Sparse Recovery:: Sparse Members +@end menu + +@node Split Recovery +@subsubsection Extracting Members Split Between Volumes + +@cindex Mutli-volume archives, extracting using non-GNU tars +If a member is split between several volumes of an old GNU format archive +most third party @command{tar} implementation will fail to extract +it. To extract it, use @command{tarcat} program (@pxref{Tarcat}). +This program is available from +@uref{http://www.gnu.org/@/software/@/tar/@/utils/@/tarcat.html, @GNUTAR{} +home page}. It concatenates several archive volumes into a single +valid archive. For example, if you have three volumes named from +@file{vol-1.tar} to @file{vol-3.tar}, you can do the following to +extract them using a third-party @command{tar}: + +@smallexample +$ @kbd{tarcat vol-1.tar vol-2.tar vol-3.tar | tar xf -} +@end smallexample + +@cindex Mutli-volume archives in PAX format, extracting using non-GNU tars +You could use this approach for most (although not all) PAX +format archives as well. However, extracting split members from a PAX +archive is a much easier task, because PAX volumes are constructed in +such a way that each part of a split member is extracted to a +different file by @command{tar} implementations that are not aware of +GNU extensions. More specifically, the very first part retains its +original name, and all subsequent parts are named using the pattern: + +@smallexample +%d/GNUFileParts.%p/%f.%n +@end smallexample + +@noindent +where symbols preceeded by @samp{%} are @dfn{macro characters} that +have the following meaning: + +@multitable @columnfractions .25 .55 +@headitem Meta-character @tab Replaced By +@item %d @tab The directory name of the file, equivalent to the +result of the @command{dirname} utility on its full name. +@item %f @tab The file name of the file, equivalent to the result +of the @command{basename} utility on its full name. +@item %p @tab The process @acronym{ID} of the @command{tar} process that +created the archive. +@item %n @tab Ordinal number of this particular part. +@end multitable + +For example, if the file @file{var/longfile} was split during archive +creation between three volumes, and the creator @command{tar} process +had process @acronym{ID} @samp{27962}, then the member names will be: + +@smallexample +var/longfile +var/GNUFileParts.27962/longfile.1 +var/GNUFileParts.27962/longfile.2 +@end smallexample + +When you extract your archive using a third-party @command{tar}, these +files will be created on your disk, and the only thing you will need +to do to restore your file in its original form is concatenate them in +the proper order, for example: + +@smallexample +@group +$ @kbd{cd var} +$ @kbd{cat GNUFileParts.27962/longfile.1 \ + GNUFileParts.27962/longfile.2 >> longfile} +$ rm -f GNUFileParts.27962 +@end group +@end smallexample + +Notice, that if the @command{tar} implementation you use supports PAX +format archives, it will probably emit warnings about unknown keywords +during extraction. They will look like this: + +@smallexample +@group +Tar file too small +Unknown extended header keyword 'GNU.volume.filename' ignored. +Unknown extended header keyword 'GNU.volume.size' ignored. +Unknown extended header keyword 'GNU.volume.offset' ignored. +@end group +@end smallexample + +@noindent +You can safely ignore these warnings. + +If your @command{tar} implementation is not PAX-aware, you will get +more warnings and more files generated on your disk, e.g.: + +@smallexample +@group +$ @kbd{tar xf vol-1.tar} +var/PaxHeaders.27962/longfile: Unknown file type 'x', extracted as +normal file +Unexpected EOF in archive +$ @kbd{tar xf vol-2.tar} +tmp/GlobalHead.27962.1: Unknown file type 'g', extracted as normal file +GNUFileParts.27962/PaxHeaders.27962/sparsefile.1: Unknown file type +'x', extracted as normal file +@end group +@end smallexample + +Ignore these warnings. The @file{PaxHeaders.*} directories created +will contain files with @dfn{extended header keywords} describing the +extracted files. You can delete them, unless they describe sparse +members. Read further to learn more about them. + +@node Sparse Recovery +@subsubsection Extracting Sparse Members + +@cindex sparse files, extracting with non-GNU tars +Any @command{tar} implementation will be able to extract sparse members from a +PAX archive. However, the extracted files will be @dfn{condensed}, +i.e., any zero blocks will be removed from them. When we restore such +a condensed file to its original form, by adding zero blocks (or +@dfn{holes}) back to their original locations, we call this process +@dfn{expanding} a compressed sparse file. + +@pindex xsparse +To expand a file, you will need a simple auxiliary program called +@command{xsparse}. It is available in source form from +@uref{http://www.gnu.org/@/software/@/tar/@/utils/@/xsparse.html, @GNUTAR{} +home page}. + +@cindex sparse files v.1.0, extracting with non-GNU tars +Let's begin with archive members in @dfn{sparse format +version 1.0}@footnote{@xref{PAX 1}.}, which are the easiest to expand. +The condensed file will contain both file map and file data, so no +additional data will be needed to restore it. If the original file +name was @file{@var{dir}/@var{name}}, then the condensed file will be +named @file{@var{dir}/@/GNUSparseFile.@var{n}/@/@var{name}}, where +@var{n} is a decimal number@footnote{Technically speaking, @var{n} is a +@dfn{process @acronym{ID}} of the @command{tar} process which created the +archive (@pxref{PAX keywords}).}. + +To expand a version 1.0 file, run @command{xsparse} as follows: + +@smallexample +$ @kbd{xsparse @file{cond-file}} +@end smallexample + +@noindent +where @file{cond-file} is the name of the condensed file. The utility +will deduce the name for the resulting expanded file using the +following algorithm: + +@enumerate 1 +@item If @file{cond-file} does not contain any directories, +@file{../cond-file} will be used; + +@item If @file{cond-file} has the form +@file{@var{dir}/@var{t}/@var{name}}, where both @var{t} and @var{name} +are simple names, with no @samp{/} characters in them, the output file +name will be @file{@var{dir}/@var{name}}. + +@item Otherwise, if @file{cond-file} has the form +@file{@var{dir}/@var{name}}, the output file name will be +@file{@var{name}}. +@end enumerate + +In the unlikely case when this algorithm does not suit your needs, +you can explicitly specify output file name as a second argument to +the command: + +@smallexample +$ @kbd{xsparse @file{cond-file} @file{out-file}} +@end smallexample + +It is often a good idea to run @command{xsparse} in @dfn{dry run} mode +first. In this mode, the command does not actually expand the file, +but verbosely lists all actions it would be taking to do so. The dry +run mode is enabled by @option{-n} command line argument: + +@smallexample +@group +$ @kbd{xsparse -n /home/gray/GNUSparseFile.6058/sparsefile} +Reading v.1.0 sparse map +Expanding file `/home/gray/GNUSparseFile.6058/sparsefile' to +`/home/gray/sparsefile' +Finished dry run +@end group +@end smallexample + +To actually expand the file, you would run: + +@smallexample +$ @kbd{xsparse /home/gray/GNUSparseFile.6058/sparsefile} +@end smallexample + +@noindent +The program behaves the same way all UNIX utilities do: it will keep +quiet unless it has simething important to tell you (e.g. an error +condition or something). If you wish it to produce verbose output, +similar to that from the dry run mode, use @option{-v} option: + +@smallexample +@group +$ @kbd{xsparse -v /home/gray/GNUSparseFile.6058/sparsefile} +Reading v.1.0 sparse map +Expanding file `/home/gray/GNUSparseFile.6058/sparsefile' to +`/home/gray/sparsefile' +Done +@end group +@end smallexample + +Additionally, if your @command{tar} implementation has extracted the +@dfn{extended headers} for this file, you can instruct @command{xstar} +to use them in order to verify the integrity of the expanded file. +The option @option{-x} sets the name of the extended header file to +use. Continuing our example: + +@smallexample +@group +$ @kbd{xsparse -v -x /home/gray/PaxHeaders.6058/sparsefile \ + /home/gray/GNUSparseFile.6058/sparsefile} +Reading extended header file +Found variable GNU.sparse.major = 1 +Found variable GNU.sparse.minor = 0 +Found variable GNU.sparse.name = sparsefile +Found variable GNU.sparse.realsize = 217481216 +Reading v.1.0 sparse map +Expanding file `/home/gray/GNUSparseFile.6058/sparsefile' to +`/home/gray/sparsefile' +Done +@end group +@end smallexample + +@anchor{extracting sparse v.0.x} +@cindex sparse files v.0.1, extracting with non-GNU tars +@cindex sparse files v.0.0, extracting with non-GNU tars +An @dfn{extended header} is a special @command{tar} archive header +that precedes an archive member and contains a set of +@dfn{variables}, describing the member properties that cannot be +stored in the standard @code{ustar} header. While optional for +expanding sparse version 1.0 members, the use of extended headers is +mandatory when expanding sparse members in older sparse formats: v.0.0 +and v.0.1 (The sparse formats are described in detail in @ref{Sparse +Formats}.) So, for these formats, the question is: how to obtain +extended headers from the archive? + +If you use a @command{tar} implementation that does not support PAX +format, extended headers for each member will be extracted as a +separate file. If we represent the member name as +@file{@var{dir}/@var{name}}, then the extended header file will be +named @file{@var{dir}/@/PaxHeaders.@var{n}/@/@var{name}}, where +@var{n} is an integer number. + +Things become more difficult if your @command{tar} implementation +does support PAX headers, because in this case you will have to +manually extract the headers. We recommend the following algorithm: + +@enumerate 1 +@item +Consult the documentation of your @command{tar} implementation for an +option that prints @dfn{block numbers} along with the archive +listing (analogous to @GNUTAR{}'s @option{-R} option). For example, +@command{star} has @option{-block-number}. + +@item +Obtain verbose listing using the @samp{block number} option, and +find block numbers of the sparse member in question and the member +immediately following it. For example, running @command{star} on our +archive we obtain: + +@smallexample +@group +$ @kbd{star -t -v -block-number -f arc.tar} +@dots{} +star: Unknown extended header keyword 'GNU.sparse.size' ignored. +star: Unknown extended header keyword 'GNU.sparse.numblocks' ignored. +star: Unknown extended header keyword 'GNU.sparse.name' ignored. +star: Unknown extended header keyword 'GNU.sparse.map' ignored. +block 56: 425984 -rw-r--r-- gray/users Jun 25 14:46 2006 GNUSparseFile.28124/sparsefile +block 897: 65391 -rw-r--r-- gray/users Jun 24 20:06 2006 README +@dots{} +@end group +@end smallexample + +@noindent +(as usual, ignore the warnings about unknown keywords.) + +@item +Let @var{size} be the size of the sparse member, @var{Bs} be its block number +and @var{Bn} be the block number of the next member. +Compute: + +@smallexample +@var{N} = @var{Bs} - @var{Bn} - @var{size}/512 - 2 +@end smallexample + +@noindent +This number gives the size of the extended header part in tar @dfn{blocks}. +In our example, this formula gives: @code{897 - 56 - 425984 / 512 - 2 += 7}. + +@item +Use @command{dd} to extract the headers: + +@smallexample +@kbd{dd if=@var{archive} of=@var{hname} bs=512 skip=@var{Bs} count=@var{N}} +@end smallexample + +@noindent +where @var{archive} is the archive name, @var{hname} is a name of the +file to store the extended header in, @var{Bs} and @var{N} are +computed in previous steps. + +In our example, this command will be + +@smallexample +$ @kbd{dd if=arc.tar of=xhdr bs=512 skip=56 count=7} +@end smallexample +@end enumerate + +Finally, you can expand the condensed file, using the obtained header: + +@smallexample +@group +$ @kbd{xsparse -v -x xhdr GNUSparseFile.6058/sparsefile} +Reading extended header file +Found variable GNU.sparse.size = 217481216 +Found variable GNU.sparse.numblocks = 208 +Found variable GNU.sparse.name = sparsefile +Found variable GNU.sparse.map = 0,2048,1050624,2048,@dots{} +Expanding file `GNUSparseFile.28124/sparsefile' to `sparsefile' +Done +@end group +@end smallexample + +@node cpio +@section Comparison of @command{tar} and @command{cpio} +@UNREVISED + +@FIXME{Reorganize the following material} + +The @command{cpio} archive formats, like @command{tar}, do have maximum +file name lengths. The binary and old @acronym{ASCII} formats have a maximum file +length of 256, and the new @acronym{ASCII} and @acronym{CRC ASCII} formats have a max +file length of 1024. @acronym{GNU} @command{cpio} can read and write archives +with arbitrary file name lengths, but other @command{cpio} implementations +may crash unexplainedly trying to read them. + +@command{tar} handles symbolic links in the form in which it comes in @acronym{BSD}; +@command{cpio} doesn't handle symbolic links in the form in which it comes +in System V prior to SVR4, and some vendors may have added symlinks +to their system without enhancing @command{cpio} to know about them. +Others may have enhanced it in a way other than the way I did it +at Sun, and which was adopted by AT&T (and which is, I think, also +present in the @command{cpio} that Berkeley picked up from AT&T and put +into a later @acronym{BSD} release---I think I gave them my changes). + +(SVR4 does some funny stuff with @command{tar}; basically, its @command{cpio} +can handle @command{tar} format input, and write it on output, and it +probably handles symbolic links. They may not have bothered doing +anything to enhance @command{tar} as a result.) + +@command{cpio} handles special files; traditional @command{tar} doesn't. + +@command{tar} comes with V7, System III, System V, and @acronym{BSD} source; +@command{cpio} comes only with System III, System V, and later @acronym{BSD} +(4.3-tahoe and later). + +@command{tar}'s way of handling multiple hard links to a file can handle +file systems that support 32-bit i-numbers (e.g., the @acronym{BSD} file system); +@command{cpio}s way requires you to play some games (in its ``binary'' +format, i-numbers are only 16 bits, and in its ``portable @acronym{ASCII}'' format, +they're 18 bits---it would have to play games with the "file system @acronym{ID}" +field of the header to make sure that the file system @acronym{ID}/i-number pairs +of different files were always different), and I don't know which +@command{cpio}s, if any, play those games. Those that don't might get +confused and think two files are the same file when they're not, and +make hard links between them. + +@command{tar}s way of handling multiple hard links to a file places only +one copy of the link on the tape, but the name attached to that copy +is the @emph{only} one you can use to retrieve the file; @command{cpio}s +way puts one copy for every link, but you can retrieve it using any +of the names. + +@quotation +What type of check sum (if any) is used, and how is this calculated. +@end quotation + +See the attached manual pages for @command{tar} and @command{cpio} format. +@command{tar} uses a checksum which is the sum of all the bytes in the +@command{tar} header for a file; @command{cpio} uses no checksum. + +@quotation +If anyone knows why @command{cpio} was made when @command{tar} was present +at the unix scene, +@end quotation + +It wasn't. @command{cpio} first showed up in PWB/UNIX 1.0; no +generally-available version of UNIX had @command{tar} at the time. I don't +know whether any version that was generally available @emph{within AT&T} +had @command{tar}, or, if so, whether the people within AT&T who did +@command{cpio} knew about it. + +On restore, if there is a corruption on a tape @command{tar} will stop at +that point, while @command{cpio} will skip over it and try to restore the +rest of the files. + +The main difference is just in the command syntax and header format. + +@command{tar} is a little more tape-oriented in that everything is blocked +to start on a record boundary. + +@quotation +Is there any differences between the ability to recover crashed +archives between the two of them. (Is there any chance of recovering +crashed archives at all.) +@end quotation + +Theoretically it should be easier under @command{tar} since the blocking +lets you find a header with some variation of @samp{dd skip=@var{nn}}. +However, modern @command{cpio}'s and variations have an option to just +search for the next file header after an error with a reasonable chance +of resyncing. However, lots of tape driver software won't allow you to +continue past a media error which should be the only reason for getting +out of sync unless a file changed sizes while you were writing the +archive. + +@quotation +If anyone knows why @command{cpio} was made when @command{tar} was present +at the unix scene, please tell me about this too. +@end quotation + +Probably because it is more media efficient (by not blocking everything +and using only the space needed for the headers where @command{tar} +always uses 512 bytes per file header) and it knows how to archive +special files. + +You might want to look at the freely available alternatives. The +major ones are @command{afio}, @GNUTAR{}, and +@command{pax}, each of which have their own extensions with some +backwards compatibility. + +Sparse files were @command{tar}red as sparse files (which you can +easily test, because the resulting archive gets smaller, and +@acronym{GNU} @command{cpio} can no longer read it). + +@node Media +@chapter Tapes and Other Archive Media +@UNREVISED + +A few special cases about tape handling warrant more detailed +description. These special cases are discussed below. + +Many complexities surround the use of @command{tar} on tape drives. Since +the creation and manipulation of archives located on magnetic tape was +the original purpose of @command{tar}, it contains many features making +such manipulation easier. + +Archives are usually written on dismountable media---tape cartridges, +mag tapes, or floppy disks. + +The amount of data a tape or disk holds depends not only on its size, +but also on how it is formatted. A 2400 foot long reel of mag tape +holds 40 megabytes of data when formatted at 1600 bits per inch. The +physically smaller EXABYTE tape cartridge holds 2.3 gigabytes. + +Magnetic media are re-usable---once the archive on a tape is no longer +needed, the archive can be erased and the tape or disk used over. +Media quality does deteriorate with use, however. Most tapes or disks +should be discarded when they begin to produce data errors. EXABYTE +tape cartridges should be discarded when they generate an @dfn{error +count} (number of non-usable bits) of more than 10k. + +Magnetic media are written and erased using magnetic fields, and +should be protected from such fields to avoid damage to stored data. +Sticking a floppy disk to a filing cabinet using a magnet is probably +not a good idea. + +@menu +* Device:: Device selection and switching +* Remote Tape Server:: +* Common Problems and Solutions:: +* Blocking:: Blocking +* Many:: Many archives on one tape +* Using Multiple Tapes:: Using Multiple Tapes +* label:: Including a Label in the Archive +* verify:: +* Write Protection:: +@end menu + +@node Device +@section Device Selection and Switching +@UNREVISED + +@table @option +@item -f [@var{hostname}:]@var{file} +@itemx --file=[@var{hostname}:]@var{file} +Use archive file or device @var{file} on @var{hostname}. +@end table + +This option is used to specify the file name of the archive @command{tar} +works on. + +If the file name is @samp{-}, @command{tar} reads the archive from standard +input (when listing or extracting), or writes it to standard output +(when creating). If the @samp{-} file name is given when updating an +archive, @command{tar} will read the original archive from its standard +input, and will write the entire new archive to its standard output. + +If the file name contains a @samp{:}, it is interpreted as +@samp{hostname:file name}. If the @var{hostname} contains an @dfn{at} +sign (@samp{@@}), it is treated as @samp{user@@hostname:file name}. In +either case, @command{tar} will invoke the command @command{rsh} (or +@command{remsh}) to start up an @command{/usr/libexec/rmt} on the remote +machine. If you give an alternate login name, it will be given to the +@command{rsh}. +Naturally, the remote machine must have an executable +@command{/usr/libexec/rmt}. This program is free software from the +University of California, and a copy of the source code can be found +with the sources for @command{tar}; it's compiled and installed by default. +The exact path to this utility is determined when configuring the package. +It is @file{@var{prefix}/libexec/rmt}, where @var{prefix} stands for +your installation prefix. This location may also be overridden at +runtime by using the @option{--rmt-command=@var{command}} option (@xref{Option Summary, +---rmt-command}, for detailed description of this option. @xref{Remote +Tape Server}, for the description of @command{rmt} command). + +If this option is not given, but the environment variable @env{TAPE} +is set, its value is used; otherwise, old versions of @command{tar} +used a default archive name (which was picked when @command{tar} was +compiled). The default is normally set up to be the @dfn{first} tape +drive or other transportable I/O medium on the system. + +Starting with version 1.11.5, @GNUTAR{} uses +standard input and standard output as the default device, and I will +not try anymore supporting automatic device detection at installation +time. This was failing really in too many cases, it was hopeless. +This is now completely left to the installer to override standard +input and standard output for default device, if this seems +preferable. Further, I think @emph{most} actual usages of +@command{tar} are done with pipes or disks, not really tapes, +cartridges or diskettes. + +Some users think that using standard input and output is running +after trouble. This could lead to a nasty surprise on your screen if +you forget to specify an output file name---especially if you are going +through a network or terminal server capable of buffering large amounts +of output. We had so many bug reports in that area of configuring +default tapes automatically, and so many contradicting requests, that +we finally consider the problem to be portably intractable. We could +of course use something like @samp{/dev/tape} as a default, but this +is @emph{also} running after various kind of trouble, going from hung +processes to accidental destruction of real tapes. After having seen +all this mess, using standard input and output as a default really +sounds like the only clean choice left, and a very useful one too. + +@GNUTAR{} reads and writes archive in records, I +suspect this is the main reason why block devices are preferred over +character devices. Most probably, block devices are more efficient +too. The installer could also check for @samp{DEFTAPE} in +@file{}. + +@table @option +@xopindex{force-local, short description} +@item --force-local +Archive file is local even if it contains a colon. + +@opindex rsh-command +@item --rsh-command=@var{command} +Use remote @var{command} instead of @command{rsh}. This option exists +so that people who use something other than the standard @command{rsh} +(e.g., a Kerberized @command{rsh}) can access a remote device. + +When this command is not used, the shell command found when +the @command{tar} program was installed is used instead. This is +the first found of @file{/usr/ucb/rsh}, @file{/usr/bin/remsh}, +@file{/usr/bin/rsh}, @file{/usr/bsd/rsh} or @file{/usr/bin/nsh}. +The installer may have overridden this by defining the environment +variable @env{RSH} @emph{at installation time}. + +@item -[0-7][lmh] +Specify drive and density. + +@xopindex{multi-volume, short description} +@item -M +@itemx --multi-volume +Create/list/extract multi-volume archive. + +This option causes @command{tar} to write a @dfn{multi-volume} archive---one +that may be larger than will fit on the medium used to hold it. +@xref{Multi-Volume Archives}. + +@xopindex{tape-length, short description} +@item -L @var{num} +@itemx --tape-length=@var{size}[@var{suf}] +Change tape after writing @var{size} units of data. Unless @var{suf} is +given, @var{size} is treated as kilobytes, i.e. @samp{@var{size} x +1024} bytes. The following suffixes alter this behavior: + +@float Table, size-suffixes +@caption{Size Suffixes} +@multitable @columnfractions 0.2 0.3 0.3 +@headitem Suffix @tab Units @tab Byte Equivalent +@item b @tab Blocks @tab @var{size} x 512 +@item B @tab Kilobytes @tab @var{size} x 1024 +@item c @tab Bytes @tab @var{size} +@item G @tab Gigabytes @tab @var{size} x 1024^3 +@item K @tab Kilobytes @tab @var{size} x 1024 +@item k @tab Kilobytes @tab @var{size} x 1024 +@item M @tab Megabytes @tab @var{size} x 1024^2 +@item P @tab Petabytes @tab @var{size} x 1024^5 +@item T @tab Terabytes @tab @var{size} x 1024^4 +@item w @tab Words @tab @var{size} x 2 +@end multitable +@end float + +This option might be useful when your tape drivers do not properly +detect end of physical tapes. By being slightly conservative on the +maximum tape length, you might avoid the problem entirely. + +@xopindex{info-script, short description} +@xopindex{new-volume-script, short description} +@item -F @var{file} +@itemx --info-script=@var{file} +@itemx --new-volume-script=@var{file} +Execute @file{file} at end of each tape. This implies +@option{--multi-volume} (@option{-M}). @xref{info-script}, for a detailed +description of this option. +@end table + +@node Remote Tape Server +@section Remote Tape Server + +@cindex remote tape drive +@pindex rmt +In order to access the tape drive on a remote machine, @command{tar} +uses the remote tape server written at the University of California at +Berkeley. The remote tape server must be installed as +@file{@var{prefix}/libexec/rmt} on any machine whose tape drive you +want to use. @command{tar} calls @command{rmt} by running an +@command{rsh} or @command{remsh} to the remote machine, optionally +using a different login name if one is supplied. + +A copy of the source for the remote tape server is provided. It is +Copyright @copyright{} 1983 by the Regents of the University of +California, but can be freely distributed. It is compiled and +installed by default. + +@cindex absolute file names +Unless you use the @option{--absolute-names} (@option{-P}) option, +@GNUTAR{} will not allow you to create an archive that contains +absolute file names (a file name beginning with @samp{/}.) If you try, +@command{tar} will automatically remove the leading @samp{/} from the +file names it stores in the archive. It will also type a warning +message telling you what it is doing. + +When reading an archive that was created with a different +@command{tar} program, @GNUTAR{} automatically +extracts entries in the archive which have absolute file names as if +the file names were not absolute. This is an important feature. A +visitor here once gave a @command{tar} tape to an operator to restore; +the operator used Sun @command{tar} instead of @GNUTAR{}, +and the result was that it replaced large portions of +our @file{/bin} and friends with versions from the tape; needless to +say, we were unhappy about having to recover the file system from +backup tapes. + +For example, if the archive contained a file @file{/usr/bin/computoy}, +@GNUTAR{} would extract the file to @file{usr/bin/computoy}, +relative to the current directory. If you want to extract the files in +an archive to the same absolute names that they had when the archive +was created, you should do a @samp{cd /} before extracting the files +from the archive, or you should either use the @option{--absolute-names} +option, or use the command @samp{tar -C / @dots{}}. + +@cindex Ultrix 3.1 and write failure +Some versions of Unix (Ultrix 3.1 is known to have this problem), +can claim that a short write near the end of a tape succeeded, +when it actually failed. This will result in the -M option not +working correctly. The best workaround at the moment is to use a +significantly larger blocking factor than the default 20. + +In order to update an archive, @command{tar} must be able to backspace the +archive in order to reread or rewrite a record that was just read (or +written). This is currently possible only on two kinds of files: normal +disk files (or any other file that can be backspaced with @samp{lseek}), +and industry-standard 9-track magnetic tape (or any other kind of tape +that can be backspaced with the @code{MTIOCTOP} @code{ioctl}). + +This means that the @option{--append}, @option{--concatenate}, and +@option{--delete} commands will not work on any other kind of file. +Some media simply cannot be backspaced, which means these commands and +options will never be able to work on them. These non-backspacing +media include pipes and cartridge tape drives. + +Some other media can be backspaced, and @command{tar} will work on them +once @command{tar} is modified to do so. + +Archives created with the @option{--multi-volume}, @option{--label}, and +@option{--incremental} (@option{-G}) options may not be readable by other version +of @command{tar}. In particular, restoring a file that was split over +a volume boundary will require some careful work with @command{dd}, if +it can be done at all. Other versions of @command{tar} may also create +an empty file whose name is that of the volume header. Some versions +of @command{tar} may create normal files instead of directories archived +with the @option{--incremental} (@option{-G}) option. + +@node Common Problems and Solutions +@section Some Common Problems and their Solutions + +@ifclear PUBLISH + +@format +errors from system: +permission denied +no such file or directory +not owner + +errors from @command{tar}: +directory checksum error +header format error + +errors from media/system: +i/o error +device busy +@end format + +@end ifclear + +@node Blocking +@section Blocking +@cindex block +@cindex record + +@dfn{Block} and @dfn{record} terminology is rather confused, and it +is also confusing to the expert reader. On the other hand, readers +who are new to the field have a fresh mind, and they may safely skip +the next two paragraphs, as the remainder of this manual uses those +two terms in a quite consistent way. + +John Gilmore, the writer of the public domain @command{tar} from which +@GNUTAR{} was originally derived, wrote (June 1995): + +@quotation +The nomenclature of tape drives comes from IBM, where I believe +they were invented for the IBM 650 or so. On IBM mainframes, what +is recorded on tape are tape blocks. The logical organization of +data is into records. There are various ways of putting records into +blocks, including @code{F} (fixed sized records), @code{V} (variable +sized records), @code{FB} (fixed blocked: fixed size records, @var{n} +to a block), @code{VB} (variable size records, @var{n} to a block), +@code{VSB} (variable spanned blocked: variable sized records that can +occupy more than one block), etc. The @code{JCL} @samp{DD RECFORM=} +parameter specified this to the operating system. + +The Unix man page on @command{tar} was totally confused about this. +When I wrote @code{PD TAR}, I used the historically correct terminology +(@command{tar} writes data records, which are grouped into blocks). +It appears that the bogus terminology made it into @acronym{POSIX} (no surprise +here), and now Fran@,{c}ois has migrated that terminology back +into the source code too. +@end quotation + +The term @dfn{physical block} means the basic transfer chunk from or +to a device, after which reading or writing may stop without anything +being lost. In this manual, the term @dfn{block} usually refers to +a disk physical block, @emph{assuming} that each disk block is 512 +bytes in length. It is true that some disk devices have different +physical blocks, but @command{tar} ignore these differences in its own +format, which is meant to be portable, so a @command{tar} block is always +512 bytes in length, and @dfn{block} always mean a @command{tar} block. +The term @dfn{logical block} often represents the basic chunk of +allocation of many disk blocks as a single entity, which the operating +system treats somewhat atomically; this concept is only barely used +in @GNUTAR{}. + +The term @dfn{physical record} is another way to speak of a physical +block, those two terms are somewhat interchangeable. In this manual, +the term @dfn{record} usually refers to a tape physical block, +@emph{assuming} that the @command{tar} archive is kept on magnetic tape. +It is true that archives may be put on disk or used with pipes, +but nevertheless, @command{tar} tries to read and write the archive one +@dfn{record} at a time, whatever the medium in use. One record is made +up of an integral number of blocks, and this operation of putting many +disk blocks into a single tape block is called @dfn{reblocking}, or +more simply, @dfn{blocking}. The term @dfn{logical record} refers to +the logical organization of many characters into something meaningful +to the application. The term @dfn{unit record} describes a small set +of characters which are transmitted whole to or by the application, +and often refers to a line of text. Those two last terms are unrelated +to what we call a @dfn{record} in @GNUTAR{}. + +When writing to tapes, @command{tar} writes the contents of the archive +in chunks known as @dfn{records}. To change the default blocking +factor, use the @option{--blocking-factor=@var{512-size}} (@option{-b +@var{512-size}}) option. Each record will then be composed of +@var{512-size} blocks. (Each @command{tar} block is 512 bytes. +@xref{Standard}.) Each file written to the archive uses at least one +full record. As a result, using a larger record size can result in +more wasted space for small files. On the other hand, a larger record +size can often be read and written much more efficiently. + +Further complicating the problem is that some tape drives ignore the +blocking entirely. For these, a larger record size can still improve +performance (because the software layers above the tape drive still +honor the blocking), but not as dramatically as on tape drives that +honor blocking. + +When reading an archive, @command{tar} can usually figure out the +record size on itself. When this is the case, and a non-standard +record size was used when the archive was created, @command{tar} will +print a message about a non-standard blocking factor, and then operate +normally. On some tape devices, however, @command{tar} cannot figure +out the record size itself. On most of those, you can specify a +blocking factor (with @option{--blocking-factor}) larger than the +actual blocking factor, and then use the @option{--read-full-records} +(@option{-B}) option. (If you specify a blocking factor with +@option{--blocking-factor} and don't use the +@option{--read-full-records} option, then @command{tar} will not +attempt to figure out the recording size itself.) On some devices, +you must always specify the record size exactly with +@option{--blocking-factor} when reading, because @command{tar} cannot +figure it out. In any case, use @option{--list} (@option{-t}) before +doing any extractions to see whether @command{tar} is reading the archive +correctly. + +@command{tar} blocks are all fixed size (512 bytes), and its scheme for +putting them into records is to put a whole number of them (one or +more) into each record. @command{tar} records are all the same size; +at the end of the file there's a block containing all zeros, which +is how you tell that the remainder of the last record(s) are garbage. + +In a standard @command{tar} file (no options), the block size is 512 +and the record size is 10240, for a blocking factor of 20. What the +@option{--blocking-factor} option does is sets the blocking factor, +changing the record size while leaving the block size at 512 bytes. +20 was fine for ancient 800 or 1600 bpi reel-to-reel tape drives; +most tape drives these days prefer much bigger records in order to +stream and not waste tape. When writing tapes for myself, some tend +to use a factor of the order of 2048, say, giving a record size of +around one megabyte. + +If you use a blocking factor larger than 20, older @command{tar} +programs might not be able to read the archive, so we recommend this +as a limit to use in practice. @GNUTAR{}, however, +will support arbitrarily large record sizes, limited only by the +amount of virtual memory or the physical characteristics of the tape +device. + +@menu +* Format Variations:: Format Variations +* Blocking Factor:: The Blocking Factor of an Archive +@end menu + +@node Format Variations +@subsection Format Variations +@cindex Format Parameters +@cindex Format Options +@cindex Options, archive format specifying +@cindex Options, format specifying +@UNREVISED + +Format parameters specify how an archive is written on the archive +media. The best choice of format parameters will vary depending on +the type and number of files being archived, and on the media used to +store the archive. + +To specify format parameters when accessing or creating an archive, +you can use the options described in the following sections. +If you do not specify any format parameters, @command{tar} uses +default parameters. You cannot modify a compressed archive. +If you create an archive with the @option{--blocking-factor} option +specified (@pxref{Blocking Factor}), you must specify that +blocking-factor when operating on the archive. @xref{Formats}, for other +examples of format parameter considerations. + +@node Blocking Factor +@subsection The Blocking Factor of an Archive +@cindex Blocking Factor +@cindex Record Size +@cindex Number of blocks per record +@cindex Number of bytes per record +@cindex Bytes per record +@cindex Blocks per record +@UNREVISED + +@opindex blocking-factor +The data in an archive is grouped into blocks, which are 512 bytes. +Blocks are read and written in whole number multiples called +@dfn{records}. The number of blocks in a record (i.e., the size of a +record in units of 512 bytes) is called the @dfn{blocking factor}. +The @option{--blocking-factor=@var{512-size}} (@option{-b +@var{512-size}}) option specifies the blocking factor of an archive. +The default blocking factor is typically 20 (i.e., 10240 bytes), but +can be specified at installation. To find out the blocking factor of +an existing archive, use @samp{tar --list --file=@var{archive-name}}. +This may not work on some devices. + +Records are separated by gaps, which waste space on the archive media. +If you are archiving on magnetic tape, using a larger blocking factor +(and therefore larger records) provides faster throughput and allows you +to fit more data on a tape (because there are fewer gaps). If you are +archiving on cartridge, a very large blocking factor (say 126 or more) +greatly increases performance. A smaller blocking factor, on the other +hand, may be useful when archiving small files, to avoid archiving lots +of nulls as @command{tar} fills out the archive to the end of the record. +In general, the ideal record size depends on the size of the +inter-record gaps on the tape you are using, and the average size of the +files you are archiving. @xref{create}, for information on +writing archives. + +@FIXME{Need example of using a cartridge with blocking factor=126 or more.} + +Archives with blocking factors larger than 20 cannot be read +by very old versions of @command{tar}, or by some newer versions +of @command{tar} running on old machines with small address spaces. +With @GNUTAR{}, the blocking factor of an archive is limited +only by the maximum record size of the device containing the archive, +or by the amount of available virtual memory. + +Also, on some systems, not using adequate blocking factors, as sometimes +imposed by the device drivers, may yield unexpected diagnostics. For +example, this has been reported: + +@smallexample +Cannot write to /dev/dlt: Invalid argument +@end smallexample + +@noindent +In such cases, it sometimes happen that the @command{tar} bundled by +the system is aware of block size idiosyncrasies, while @GNUTAR{} +requires an explicit specification for the block size, +which it cannot guess. This yields some people to consider +@GNUTAR{} is misbehaving, because by comparison, +@cite{the bundle @command{tar} works OK}. Adding @w{@kbd{-b 256}}, +for example, might resolve the problem. + +If you use a non-default blocking factor when you create an archive, you +must specify the same blocking factor when you modify that archive. Some +archive devices will also require you to specify the blocking factor when +reading that archive, however this is not typically the case. Usually, you +can use @option{--list} (@option{-t}) without specifying a blocking factor---@command{tar} +reports a non-default record size and then lists the archive members as +it would normally. To extract files from an archive with a non-standard +blocking factor (particularly if you're not sure what the blocking factor +is), you can usually use the @option{--read-full-records} (@option{-B}) option while +specifying a blocking factor larger then the blocking factor of the archive +(i.e., @samp{tar --extract --read-full-records --blocking-factor=300}). +@xref{list}, for more information on the @option{--list} (@option{-t}) +operation. @xref{Reading}, for a more detailed explanation of that option. + +@table @option +@item --blocking-factor=@var{number} +@itemx -b @var{number} +Specifies the blocking factor of an archive. Can be used with any +operation, but is usually not necessary with @option{--list} (@option{-t}). +@end table + +Device blocking + +@table @option +@item -b @var{blocks} +@itemx --blocking-factor=@var{blocks} +Set record size to @math{@var{blocks}*512} bytes. + +This option is used to specify a @dfn{blocking factor} for the archive. +When reading or writing the archive, @command{tar}, will do reads and writes +of the archive in records of @math{@var{block}*512} bytes. This is true +even when the archive is compressed. Some devices requires that all +write operations be a multiple of a certain size, and so, @command{tar} +pads the archive out to the next record boundary. + +The default blocking factor is set when @command{tar} is compiled, and is +typically 20. Blocking factors larger than 20 cannot be read by very +old versions of @command{tar}, or by some newer versions of @command{tar} +running on old machines with small address spaces. + +With a magnetic tape, larger records give faster throughput and fit +more data on a tape (because there are fewer inter-record gaps). +If the archive is in a disk file or a pipe, you may want to specify +a smaller blocking factor, since a large one will result in a large +number of null bytes at the end of the archive. + +When writing cartridge or other streaming tapes, a much larger +blocking factor (say 126 or more) will greatly increase performance. +However, you must specify the same blocking factor when reading or +updating the archive. + +Apparently, Exabyte drives have a physical block size of 8K bytes. +If we choose our blocksize as a multiple of 8k bytes, then the problem +seems to disappear. Id est, we are using block size of 112 right +now, and we haven't had the problem since we switched@dots{} + +With @GNUTAR{} the blocking factor is limited only +by the maximum record size of the device containing the archive, or by +the amount of available virtual memory. + +However, deblocking or reblocking is virtually avoided in a special +case which often occurs in practice, but which requires all the +following conditions to be simultaneously true: +@itemize @bullet +@item +the archive is subject to a compression option, +@item +the archive is not handled through standard input or output, nor +redirected nor piped, +@item +the archive is directly handled to a local disk, instead of any special +device, +@item +@option{--blocking-factor} is not explicitly specified on the @command{tar} +invocation. +@end itemize + +If the output goes directly to a local disk, and not through +stdout, then the last write is not extended to a full record size. +Otherwise, reblocking occurs. Here are a few other remarks on this +topic: + +@itemize @bullet + +@item +@command{gzip} will complain about trailing garbage if asked to +uncompress a compressed archive on tape, there is an option to turn +the message off, but it breaks the regularity of simply having to use +@samp{@var{prog} -d} for decompression. It would be nice if gzip was +silently ignoring any number of trailing zeros. I'll ask Jean-loup +Gailly, by sending a copy of this message to him. + +@item +@command{compress} does not show this problem, but as Jean-loup pointed +out to Michael, @samp{compress -d} silently adds garbage after +the result of decompression, which tar ignores because it already +recognized its end-of-file indicator. So this bug may be safely +ignored. + +@item +@samp{gzip -d -q} will be silent about the trailing zeros indeed, +but will still return an exit status of 2 which tar reports in turn. +@command{tar} might ignore the exit status returned, but I hate doing +that, as it weakens the protection @command{tar} offers users against +other possible problems at decompression time. If @command{gzip} was +silently skipping trailing zeros @emph{and} also avoiding setting the +exit status in this innocuous case, that would solve this situation. + +@item +@command{tar} should become more solid at not stopping to read a pipe at +the first null block encountered. This inelegantly breaks the pipe. +@command{tar} should rather drain the pipe out before exiting itself. +@end itemize + +@xopindex{ignore-zeros, short description} +@item -i +@itemx --ignore-zeros +Ignore blocks of zeros in archive (means EOF). + +The @option{--ignore-zeros} (@option{-i}) option causes @command{tar} to ignore blocks +of zeros in the archive. Normally a block of zeros indicates the +end of the archive, but when reading a damaged archive, or one which +was created by concatenating several archives together, this option +allows @command{tar} to read the entire archive. This option is not on +by default because many versions of @command{tar} write garbage after +the zeroed blocks. + +Note that this option causes @command{tar} to read to the end of the +archive file, which may sometimes avoid problems when multiple files +are stored on a single physical tape. + +@xopindex{read-full-records, short description} +@item -B +@itemx --read-full-records +Reblock as we read (for reading 4.2@acronym{BSD} pipes). + +If @option{--read-full-records} is used, @command{tar} +will not panic if an attempt to read a record from the archive does +not return a full record. Instead, @command{tar} will keep reading +until it has obtained a full +record. + +This option is turned on by default when @command{tar} is reading +an archive from standard input, or from a remote machine. This is +because on @acronym{BSD} Unix systems, a read of a pipe will return however +much happens to be in the pipe, even if it is less than @command{tar} +requested. If this option was not used, @command{tar} would fail as +soon as it read an incomplete record from the pipe. + +This option is also useful with the commands for updating an archive. + +@end table + +Tape blocking + +@FIXME{Appropriate options should be moved here from elsewhere.} + +@cindex blocking factor +@cindex tape blocking + +When handling various tapes or cartridges, you have to take care of +selecting a proper blocking, that is, the number of disk blocks you +put together as a single tape block on the tape, without intervening +tape gaps. A @dfn{tape gap} is a small landing area on the tape +with no information on it, used for decelerating the tape to a +full stop, and for later regaining the reading or writing speed. +When the tape driver starts reading a record, the record has to +be read whole without stopping, as a tape gap is needed to stop the +tape motion without losing information. + +@cindex Exabyte blocking +@cindex DAT blocking +Using higher blocking (putting more disk blocks per tape block) will use +the tape more efficiently as there will be less tape gaps. But reading +such tapes may be more difficult for the system, as more memory will be +required to receive at once the whole record. Further, if there is a +reading error on a huge record, this is less likely that the system will +succeed in recovering the information. So, blocking should not be too +low, nor it should be too high. @command{tar} uses by default a blocking of +20 for historical reasons, and it does not really matter when reading or +writing to disk. Current tape technology would easily accommodate higher +blockings. Sun recommends a blocking of 126 for Exabytes and 96 for DATs. +We were told that for some DLT drives, the blocking should be a multiple +of 4Kb, preferably 64Kb (@w{@kbd{-b 128}}) or 256 for decent performance. +Other manufacturers may use different recommendations for the same tapes. +This might also depends of the buffering techniques used inside modern +tape controllers. Some imposes a minimum blocking, or a maximum blocking. +Others request blocking to be some exponent of two. + +So, there is no fixed rule for blocking. But blocking at read time +should ideally be the same as blocking used at write time. At one place +I know, with a wide variety of equipment, they found it best to use a +blocking of 32 to guarantee that their tapes are fully interchangeable. + +I was also told that, for recycled tapes, prior erasure (by the same +drive unit that will be used to create the archives) sometimes lowers +the error rates observed at rewriting time. + +I might also use @option{--number-blocks} instead of +@option{--block-number}, so @option{--block} will then expand to +@option{--blocking-factor} unambiguously. + +@node Many +@section Many Archives on One Tape + +@FIXME{Appropriate options should be moved here from elsewhere.} + +@findex ntape @r{device} +Most tape devices have two entries in the @file{/dev} directory, or +entries that come in pairs, which differ only in the minor number for +this device. Let's take for example @file{/dev/tape}, which often +points to the only or usual tape device of a given system. There might +be a corresponding @file{/dev/nrtape} or @file{/dev/ntape}. The simpler +name is the @emph{rewinding} version of the device, while the name +having @samp{nr} in it is the @emph{no rewinding} version of the same +device. + +A rewinding tape device will bring back the tape to its beginning point +automatically when this device is opened or closed. Since @command{tar} +opens the archive file before using it and closes it afterwards, this +means that a simple: + +@smallexample +$ @kbd{tar cf /dev/tape @var{directory}} +@end smallexample + +@noindent +will reposition the tape to its beginning both prior and after saving +@var{directory} contents to it, thus erasing prior tape contents and +making it so that any subsequent write operation will destroy what has +just been saved. + +@cindex tape positioning +So, a rewinding device is normally meant to hold one and only one file. +If you want to put more than one @command{tar} archive on a given tape, you +will need to avoid using the rewinding version of the tape device. You +will also have to pay special attention to tape positioning. Errors in +positioning may overwrite the valuable data already on your tape. Many +people, burnt by past experiences, will only use rewinding devices and +limit themselves to one file per tape, precisely to avoid the risk of +such errors. Be fully aware that writing at the wrong position on a +tape loses all information past this point and most probably until the +end of the tape, and this destroyed information @emph{cannot} be +recovered. + +To save @var{directory-1} as a first archive at the beginning of a +tape, and leave that tape ready for a second archive, you should use: + +@smallexample +$ @kbd{mt -f /dev/nrtape rewind} +$ @kbd{tar cf /dev/nrtape @var{directory-1}} +@end smallexample + +@cindex tape marks +@dfn{Tape marks} are special magnetic patterns written on the tape +media, which are later recognizable by the reading hardware. These +marks are used after each file, when there are many on a single tape. +An empty file (that is to say, two tape marks in a row) signal the +logical end of the tape, after which no file exist. Usually, +non-rewinding tape device drivers will react to the close request issued +by @command{tar} by first writing two tape marks after your archive, and by +backspacing over one of these. So, if you remove the tape at that time +from the tape drive, it is properly terminated. But if you write +another file at the current position, the second tape mark will be +erased by the new information, leaving only one tape mark between files. + +So, you may now save @var{directory-2} as a second archive after the +first on the same tape by issuing the command: + +@smallexample +$ @kbd{tar cf /dev/nrtape @var{directory-2}} +@end smallexample + +@noindent +and so on for all the archives you want to put on the same tape. + +Another usual case is that you do not write all the archives the same +day, and you need to remove and store the tape between two archive +sessions. In general, you must remember how many files are already +saved on your tape. Suppose your tape already has 16 files on it, and +that you are ready to write the 17th. You have to take care of skipping +the first 16 tape marks before saving @var{directory-17}, say, by using +these commands: + +@smallexample +$ @kbd{mt -f /dev/nrtape rewind} +$ @kbd{mt -f /dev/nrtape fsf 16} +$ @kbd{tar cf /dev/nrtape @var{directory-17}} +@end smallexample + +In all the previous examples, we put aside blocking considerations, but +you should do the proper things for that as well. @xref{Blocking}. + +@menu +* Tape Positioning:: Tape Positions and Tape Marks +* mt:: The @command{mt} Utility +@end menu + +@node Tape Positioning +@subsection Tape Positions and Tape Marks +@UNREVISED + +Just as archives can store more than one file from the file system, +tapes can store more than one archive file. To keep track of where +archive files (or any other type of file stored on tape) begin and +end, tape archive devices write magnetic @dfn{tape marks} on the +archive media. Tape drives write one tape mark between files, +two at the end of all the file entries. + +If you think of data as a series of records "rrrr"'s, and tape marks as +"*"'s, a tape might look like the following: + +@smallexample +rrrr*rrrrrr*rrrrr*rr*rrrrr**------------------------- +@end smallexample + +Tape devices read and write tapes using a read/write @dfn{tape +head}---a physical part of the device which can only access one +point on the tape at a time. When you use @command{tar} to read or +write archive data from a tape device, the device will begin reading +or writing from wherever on the tape the tape head happens to be, +regardless of which archive or what part of the archive the tape +head is on. Before writing an archive, you should make sure that no +data on the tape will be overwritten (unless it is no longer needed). +Before reading an archive, you should make sure the tape head is at +the beginning of the archive you want to read. You can do it manually +via @code{mt} utility (@pxref{mt}). The @code{restore} script does +that automatically (@pxref{Scripted Restoration}). + +If you want to add new archive file entries to a tape, you should +advance the tape to the end of the existing file entries, backspace +over the last tape mark, and write the new archive file. If you were +to add two archives to the example above, the tape might look like the +following: + +@smallexample +rrrr*rrrrrr*rrrrr*rr*rrrrr*rrr*rrrr**---------------- +@end smallexample + +@node mt +@subsection The @command{mt} Utility +@UNREVISED + +@FIXME{Is it true that this only works on non-block devices? +should explain the difference, (fixed or variable).} +@xref{Blocking Factor}. + +You can use the @command{mt} utility to advance or rewind a tape past a +specified number of archive files on the tape. This will allow you +to move to the beginning of an archive before extracting or reading +it, or to the end of all the archives before writing a new one. +@FIXME{Why isn't there an "advance 'til you find two tape marks +together"?} + +The syntax of the @command{mt} command is: + +@smallexample +@kbd{mt [-f @var{tapename}] @var{operation} [@var{number}]} +@end smallexample + +where @var{tapename} is the name of the tape device, @var{number} is +the number of times an operation is performed (with a default of one), +and @var{operation} is one of the following: + +@FIXME{is there any use for record operations?} + +@table @option +@item eof +@itemx weof +Writes @var{number} tape marks at the current position on the tape. + +@item fsf +Moves tape position forward @var{number} files. + +@item bsf +Moves tape position back @var{number} files. + +@item rewind +Rewinds the tape. (Ignores @var{number}.) + +@item offline +@itemx rewoff1 +Rewinds the tape and takes the tape device off-line. (Ignores @var{number}.) + +@item status +Prints status information about the tape unit. + +@end table + +If you don't specify a @var{tapename}, @command{mt} uses the environment +variable @env{TAPE}; if @env{TAPE} is not set, @command{mt} will use +the default device specified in your @file{sys/mtio.h} file +(@code{DEFTAPE} variable). If this is not defined, the program will +display a descriptive error message and exit with code 1. + +@command{mt} returns a 0 exit status when the operation(s) were +successful, 1 if the command was unrecognized, and 2 if an operation +failed. + +@node Using Multiple Tapes +@section Using Multiple Tapes + +Often you might want to write a large archive, one larger than will fit +on the actual tape you are using. In such a case, you can run multiple +@command{tar} commands, but this can be inconvenient, particularly if you +are using options like @option{--exclude=@var{pattern}} or dumping entire file systems. +Therefore, @command{tar} provides a special mode for creating +multi-volume archives. + +@dfn{Multi-volume} archive is a single @command{tar} archive, stored +on several media volumes of fixed size. Although in this section we will +often call @samp{volume} a @dfn{tape}, there is absolutely no +requirement for multi-volume archives to be stored on tapes. Instead, +they can use whatever media type the user finds convenient, they can +even be located on files. + +When creating a multi-volume archive, @GNUTAR{} continues to fill +current volume until it runs out of space, then it switches to +next volume (usually the operator is queried to replace the tape on +this point), and continues working on the new volume. This operation +continues until all requested files are dumped. If @GNUTAR{} detects +end of media while dumping a file, such a file is archived in split +form. Some very big files can even be split across several volumes. + +Each volume is itself a valid @GNUTAR{} archive, so it can be read +without any special options. Consequently any file member residing +entirely on one volume can be extracted or otherwise operated upon +without needing the other volume. Sure enough, to extract a split +member you would need all volumes its parts reside on. + +Multi-volume archives suffer from several limitations. In particular, +they cannot be compressed. + +@GNUTAR{} is able to create multi-volume archives of two formats +(@pxref{Formats}): @samp{GNU} and @samp{POSIX}. + +@menu +* Multi-Volume Archives:: Archives Longer than One Tape or Disk +* Tape Files:: Tape Files +* Tarcat:: Concatenate Volumes into a Single Archive + +@end menu + +@node Multi-Volume Archives +@subsection Archives Longer than One Tape or Disk +@cindex Multi-volume archives + +@opindex multi-volume +To create an archive that is larger than will fit on a single unit of +the media, use the @option{--multi-volume} (@option{-M}) option in conjunction with +the @option{--create} option (@pxref{create}). A @dfn{multi-volume} +archive can be manipulated like any other archive (provided the +@option{--multi-volume} option is specified), but is stored on more +than one tape or file. + +When you specify @option{--multi-volume}, @command{tar} does not report an +error when it comes to the end of an archive volume (when reading), or +the end of the media (when writing). Instead, it prompts you to load +a new storage volume. If the archive is on a magnetic tape, you +should change tapes when you see the prompt; if the archive is on a +floppy disk, you should change disks; etc. + +@table @option +@item --multi-volume +@itemx -M +Creates a multi-volume archive, when used in conjunction with +@option{--create} (@option{-c}). To perform any other operation on a multi-volume +archive, specify @option{--multi-volume} in conjunction with that +operation. +For example: + +@smallexample +$ @kbd{tar --create --multi-volume --file=/dev/tape @var{files}} +@end smallexample +@end table + +The method @command{tar} uses to detect end of tape is not perfect, and +fails on some operating systems or on some devices. If @command{tar} +cannot detect the end of the tape itself, you can use +@option{--tape-length} option to inform it about the capacity of the +tape: + +@anchor{tape-length} +@table @option +@opindex tape-length +@item --tape-length=@var{size}[@var{suf}] +@itemx -L @var{size}[@var{suf}] +Set maximum length of a volume. The @var{suf}, if given, specifies +units in which @var{size} is expressed, e.g. @samp{2M} mean 2 +megabytes (@pxref{size-suffixes}, for a list of allowed size +suffixes). Without @var{suf}, units of 1024 bytes (kilobyte) are +assumed. + +This option selects @option{--multi-volume} automatically. For example: + +@smallexample +$ @kbd{tar --create --tape-length=41943040 --file=/dev/tape @var{files}} +@end smallexample + +@noindent +or, which is equivalent: + +@smallexample +$ @kbd{tar --create --tape-length=4G --file=/dev/tape @var{files}} +@end smallexample +@end table + +@anchor{change volume prompt} +When @GNUTAR{} comes to the end of a storage media, it asks you to +change the volume. The built-in prompt for POSIX locale +is@footnote{If you run @GNUTAR{} under a different locale, the +translation to the locale's language will be used.}: + +@smallexample +Prepare volume #@var{n} for `@var{archive}' and hit return: +@end smallexample + +@noindent +where @var{n} is the ordinal number of the volume to be created and +@var{archive} is archive file or device name. + +When prompting for a new tape, @command{tar} accepts any of the following +responses: + +@table @kbd +@item ? +Request @command{tar} to explain possible responses. +@item q +Request @command{tar} to exit immediately. +@item n @var{file-name} +Request @command{tar} to write the next volume on the file @var{file-name}. +@item ! +Request @command{tar} to run a subshell. This option can be disabled +by giving @option{--restrict} command line option to +@command{tar}@footnote{@xref{--restrict}, for more information about +this option.}. +@item y +Request @command{tar} to begin writing the next volume. +@end table + +(You should only type @samp{y} after you have changed the tape; +otherwise @command{tar} will write over the volume it just finished.) + +@cindex Volume number file +@cindex volno file +@anchor{volno-file} +@opindex volno-file +The volume number used by @command{tar} in its tape-changing prompt +can be changed; if you give the +@option{--volno-file=@var{file-of-number}} option, then +@var{file-of-number} should be an non-existing file to be created, or +else, a file already containing a decimal number. That number will be +used as the volume number of the first volume written. When +@command{tar} is finished, it will rewrite the file with the +now-current volume number. (This does not change the volume number +written on a tape label, as per @ref{label}, it @emph{only} affects +the number used in the prompt.) + +@cindex End-of-archive info script +@cindex Info script +@anchor{info-script} +@opindex info-script +@opindex new-volume-script +If you want more elaborate behavior than this, you can write a special +@dfn{new volume script}, that will be responsible for changing the +volume, and instruct @command{tar} to use it instead of its normal +prompting procedure: + +@table @option +@item --info-script=@var{script-name} +@itemx --new-volume-script=@var{script-name} +@itemx -F @var{script-name} +Specify the full name of the volume script to use. The script can be +used to eject cassettes, or to broadcast messages such as +@samp{Someone please come change my tape} when performing unattended +backups. +@end table + +The @var{script-name} is executed without any command line +arguments. It inherits @command{tar}'s shell environment. +Additional data is passed to it via the following +environment variables: + +@table @env +@vrindex TAR_VERSION, info script environment variable +@item TAR_VERSION +@GNUTAR{} version number. + +@vrindex TAR_ARCHIVE, info script environment variable +@item TAR_ARCHIVE +The name of the archive @command{tar} is processing. + +@vrindex TAR_BLOCKING_FACTOR, info script environment variable +@item TAR_BLOCKING_FACTOR +Current blocking factor (@pxref{Blocking}). + +@vrindex TAR_VOLUME, info script environment variable +@item TAR_VOLUME +Ordinal number of the volume @command{tar} is about to start. + +@vrindex TAR_SUBCOMMAND, info script environment variable +@item TAR_SUBCOMMAND +A short option describing the operation @command{tar} is executing. +@xref{Operations}, for a complete list of subcommand options. + +@vrindex TAR_FORMAT, info script environment variable +@item TAR_FORMAT +Format of the archive being processed. @xref{Formats}, for a complete +list of archive format names. + +@vrindex TAR_FD, info script environment variable +@item TAR_FD +File descriptor which can be used to communicate the new volume +name to @command{tar}. +@end table + +The volume script can instruct @command{tar} to use new archive name, +by writing in to file descriptor @env{$TAR_FD} (see below for an example). + +If the info script fails, @command{tar} exits; otherwise, it begins +writing the next volume. + +If you want @command{tar} to cycle through a series of files or tape +drives, there are three approaches to choose from. First of all, you +can give @command{tar} multiple @option{--file} options. In this case +the specified files will be used, in sequence, as the successive +volumes of the archive. Only when the first one in the sequence needs +to be used again will @command{tar} prompt for a tape change (or run +the info script). For example, suppose someone has two tape drives on +a system named @file{/dev/tape0} and @file{/dev/tape1}. For having +@GNUTAR{} to switch to the second drive when it needs to write the +second tape, and then back to the first tape, etc., just do either of: + +@smallexample +$ @kbd{tar --create --multi-volume --file=/dev/tape0 --file=/dev/tape1 @var{files}} +$ @kbd{tar cMff /dev/tape0 /dev/tape1 @var{files}} +@end smallexample + +The second method is to use the @samp{n} response to the tape-change +prompt. + +Finally, the most flexible approach is to use a volume script, that +writes new archive name to the file descriptor @env{$TAR_FD}. For example, the +following volume script will create a series of archive files, named +@file{@var{archive}-@var{vol}}, where @var{archive} is the name of the +archive being created (as given by @option{--file} option) and +@var{vol} is the ordinal number of the archive being created: + +@smallexample +@group +#! /bin/sh +echo Preparing volume $TAR_VOLUME of $TAR_ARCHIVE. + +name=`expr $TAR_ARCHIVE : '\(.*\)-.*'` +case $TAR_SUBCOMMAND in +-c) ;; +-d|-x|-t) test -r $@{name:-$TAR_ARCHIVE@}-$TAR_VOLUME || exit 1 + ;; +*) exit 1 +esac + +echo $@{name:-$TAR_ARCHIVE@}-$TAR_VOLUME >&$TAR_FD +@end group +@end smallexample + +The same script can be used while listing, comparing or extracting +from the created archive. For example: + +@smallexample +@group +# @r{Create a multi-volume archive:} +$ @kbd{tar -c -L1024 -f archive.tar -F new-volume .} +# @r{Extract from the created archive:} +$ @kbd{tar -x -f archive.tar -F new-volume .} +@end group +@end smallexample + +@noindent +Notice, that the first command had to use @option{-L} option, since +otherwise @GNUTAR{} will end up writing everything to file +@file{archive.tar}. + +You can read each individual volume of a multi-volume archive as if it +were an archive by itself. For example, to list the contents of one +volume, use @option{--list}, without @option{--multi-volume} specified. +To extract an archive member from one volume (assuming it is described +that volume), use @option{--extract}, again without +@option{--multi-volume}. + +If an archive member is split across volumes (i.e., its entry begins on +one volume of the media and ends on another), you need to specify +@option{--multi-volume} to extract it successfully. In this case, you +should load the volume where the archive member starts, and use +@samp{tar --extract --multi-volume}---@command{tar} will prompt for later +volumes as it needs them. @xref{extracting archives}, for more +information about extracting archives. + +Multi-volume archives can be modified like any other archive. To add +files to a multi-volume archive, you need to only mount the last +volume of the archive media (and new volumes, if needed). For all +other operations, you need to use the entire archive. + +If a multi-volume archive was labeled using +@option{--label=@var{archive-label}} (@pxref{label}) when it was +created, @command{tar} will not automatically label volumes which are +added later. To label subsequent volumes, specify +@option{--label=@var{archive-label}} again in conjunction with the +@option{--append}, @option{--update} or @option{--concatenate} operation. + +Notice that multi-volume support is a GNU extension and the archives +created in this mode should be read only using @GNUTAR{}. If you +absolutely have to process such archives using a third-party @command{tar} +implementation, read @ref{Split Recovery}. + +@node Tape Files +@subsection Tape Files +@cindex labeling archives +@opindex label +@UNREVISED + +To give the archive a name which will be recorded in it, use the +@option{--label=@var{volume-label}} (@option{-V @var{volume-label}}) +option. This will write a special block identifying +@var{volume-label} as the name of the archive to the front of the +archive which will be displayed when the archive is listed with +@option{--list}. If you are creating a multi-volume archive with +@option{--multi-volume} (@pxref{Using Multiple Tapes}), then the +volume label will have @samp{Volume @var{nnn}} appended to the name +you give, where @var{nnn} is the number of the volume of the archive. +If you use the @option{--label=@var{volume-label}} option when +reading an archive, it checks to make sure the label on the tape +matches the one you gave. @xref{label}. + +When @command{tar} writes an archive to tape, it creates a single +tape file. If multiple archives are written to the same tape, one +after the other, they each get written as separate tape files. When +extracting, it is necessary to position the tape at the right place +before running @command{tar}. To do this, use the @command{mt} command. +For more information on the @command{mt} command and on the organization +of tapes into a sequence of tape files, see @ref{mt}. + +People seem to often do: + +@smallexample +@kbd{--label="@var{some-prefix} `date +@var{some-format}`"} +@end smallexample + +or such, for pushing a common date in all volumes or an archive set. + +@node Tarcat +@subsection Concatenate Volumes into a Single Archive + +@pindex tarcat + Sometimes it is necessary to convert existing @GNUTAR{} multi-volume +archive to a single @command{tar} archive. Simply concatenating all +volumes into one will not work, since each volume carries an additional +information at the beginning. @GNUTAR{} is shipped with the shell +script @command{tarcat} designed for this purpose. + + The script takes a list of files comprising a multi-volume archive +and creates the resulting archive at the standard output. For example: + +@smallexample +@kbd{tarcat vol.1 vol.2 vol.3 | tar tf -} +@end smallexample + + The script implements a simple heuristics to determine the format of +the first volume file and to decide how to process the rest of the +files. However, it makes no attempt to verify whether the files are +given in order or even if they are valid @command{tar} archives. +It uses @command{dd} and does not filter its standard error, so you +will usually see lots of spurious messages. + +@FIXME{The script is not installed. Should we install it?} + +@node label +@section Including a Label in the Archive +@cindex Labeling an archive +@cindex Labels on the archive media +@cindex Labeling multi-volume archives + +@opindex label + To avoid problems caused by misplaced paper labels on the archive +media, you can include a @dfn{label} entry --- an archive member which +contains the name of the archive --- in the archive itself. Use the +@option{--label=@var{archive-label}} (@option{-V @var{archive-label}}) +option@footnote{Until version 1.10, that option was called +@option{--volume}, but is not available under that name anymore.} in +conjunction with the @option{--create} operation to include a label +entry in the archive as it is being created. + +@table @option +@item --label=@var{archive-label} +@itemx -V @var{archive-label} +Includes an @dfn{archive-label} at the beginning of the archive when +the archive is being created, when used in conjunction with the +@option{--create} operation. Checks to make sure the archive label +matches the one specified (when used in conjunction with any other +operation). +@end table + + If you create an archive using both +@option{--label=@var{archive-label}} (@option{-V @var{archive-label}}) +and @option{--multi-volume} (@option{-M}), each volume of the archive +will have an archive label of the form @samp{@var{archive-label} +Volume @var{n}}, where @var{n} is 1 for the first volume, 2 for the +next, and so on. @xref{Using Multiple Tapes}, for information on +creating multiple volume archives. + +@cindex Volume label, listing +@cindex Listing volume label + The volume label will be displayed by @option{--list} along with +the file contents. If verbose display is requested, it will also be +explicitly marked as in the example below: + +@smallexample +@group +$ @kbd{tar --verbose --list --file=iamanarchive} +V--------- 0/0 0 1992-03-07 12:01 iamalabel--Volume Header-- +-rw-r--r-- ringo/user 40 1990-05-21 13:30 iamafilename +@end group +@end smallexample + +@opindex test-label +@anchor{--test-label option} + However, @option{--list} option will cause listing entire +contents of the archive, which may be undesirable (for example, if the +archive is stored on a tape). You can request checking only the volume +label by specifying @option{--test-label} option. This option reads only the +first block of an archive, so it can be used with slow storage +devices. For example: + +@smallexample +@group +$ @kbd{tar --test-label --file=iamanarchive} +iamalabel +@end group +@end smallexample + + If @option{--test-label} is used with one or more command line +arguments, @command{tar} compares the volume label with each +argument. It exits with code 0 if a match is found, and with code 1 +otherwise@footnote{Note that @GNUTAR{} versions up to 1.23 indicated +mismatch with an exit code 2 and printed a spurious diagnostics on +stderr.}. No output is displayed, unless you also used the +@option{--verbose} option. For example: + +@smallexample +@group +$ @kbd{tar --test-label --file=iamanarchive 'iamalabel'} +@result{} 0 +$ @kbd{tar --test-label --file=iamanarchive 'alabel'} +@result{} 1 +@end group +@end smallexample + + When used with the @option{--verbose} option, @command{tar} +prints the actual volume label (if any), and a verbose diagnostics in +case of a mismatch: + +@smallexample +@group +$ @kbd{tar --test-label --verbose --file=iamanarchive 'iamalabel'} +iamalabel +@result{} 0 +$ @kbd{tar --test-label --verbose --file=iamanarchive 'alabel'} +iamalabel +tar: Archive label mismatch +@result{} 1 +@end group +@end smallexample + + If you request any operation, other than @option{--create}, along +with using @option{--label} option, @command{tar} will first check if +the archive label matches the one specified and will refuse to proceed +if it does not. Use this as a safety precaution to avoid accidentally +overwriting existing archives. For example, if you wish to add files +to @file{archive}, presumably labeled with string @samp{My volume}, +you will get: + +@smallexample +@group +$ @kbd{tar -rf archive --label 'My volume' .} +tar: Archive not labeled to match `My volume' +@end group +@end smallexample + +@noindent +in case its label does not match. This will work even if +@file{archive} is not labeled at all. + + Similarly, @command{tar} will refuse to list or extract the +archive if its label doesn't match the @var{archive-label} +specified. In those cases, @var{archive-label} argument is interpreted +as a globbing-style pattern which must match the actual magnetic +volume label. @xref{exclude}, for a precise description of how match +is attempted@footnote{Previous versions of @command{tar} used full +regular expression matching, or before that, only exact string +matching, instead of wildcard matchers. We decided for the sake of +simplicity to use a uniform matching device through +@command{tar}.}. If the switch @option{--multi-volume} (@option{-M}) is being used, +the volume label matcher will also suffix @var{archive-label} by +@w{@samp{ Volume [1-9]*}} if the initial match fails, before giving +up. Since the volume numbering is automatically added in labels at +creation time, it sounded logical to equally help the user taking care +of it when the archive is being read. + + You can also use @option{--label} to get a common information on +all tapes of a series. For having this information different in each +series created through a single script used on a regular basis, just +manage to get some date string as part of the label. For example: + +@smallexample +@group +$ @kbd{tar cfMV /dev/tape "Daily backup for `date +%Y-%m-%d`"} +$ @kbd{tar --create --file=/dev/tape --multi-volume \ + --label="Daily backup for `date +%Y-%m-%d`"} +@end group +@end smallexample + + Some more notes about volume labels: + +@itemize @bullet +@item Each label has its own date and time, which corresponds +to the time when @GNUTAR{} initially attempted to write it, +often soon after the operator launches @command{tar} or types the +carriage return telling that the next tape is ready. + +@item Comparing date labels to get an idea of tape throughput is +unreliable. It gives correct results only if the delays for rewinding +tapes and the operator switching them were negligible, which is +usually not the case. +@end itemize + +@node verify +@section Verifying Data as It is Stored +@cindex Verifying a write operation +@cindex Double-checking a write operation + +@table @option +@item -W +@itemx --verify +@opindex verify, short description +Attempt to verify the archive after writing. +@end table + +This option causes @command{tar} to verify the archive after writing it. +Each volume is checked after it is written, and any discrepancies +are recorded on the standard error output. + +Verification requires that the archive be on a back-space-able medium. +This means pipes, some cartridge tape drives, and some other devices +cannot be verified. + +You can insure the accuracy of an archive by comparing files in the +system with archive members. @command{tar} can compare an archive to the +file system as the archive is being written, to verify a write +operation, or can compare a previously written archive, to insure that +it is up to date. + +@xopindex{verify, using with @option{--create}} +@xopindex{create, using with @option{--verify}} +To check for discrepancies in an archive immediately after it is +written, use the @option{--verify} (@option{-W}) option in conjunction with +the @option{--create} operation. When this option is +specified, @command{tar} checks archive members against their counterparts +in the file system, and reports discrepancies on the standard error. + +To verify an archive, you must be able to read it from before the end +of the last written entry. This option is useful for detecting data +errors on some tapes. Archives written to pipes, some cartridge tape +drives, and some other devices cannot be verified. + +One can explicitly compare an already made archive with the file +system by using the @option{--compare} (@option{--diff}, @option{-d}) +option, instead of using the more automatic @option{--verify} option. +@xref{compare}. + +Note that these two options have a slightly different intent. The +@option{--compare} option checks how identical are the logical contents of some +archive with what is on your disks, while the @option{--verify} option is +really for checking if the physical contents agree and if the recording +media itself is of dependable quality. So, for the @option{--verify} +operation, @command{tar} tries to defeat all in-memory cache pertaining to +the archive, while it lets the speed optimization undisturbed for the +@option{--compare} option. If you nevertheless use @option{--compare} for +media verification, you may have to defeat the in-memory cache yourself, +maybe by opening and reclosing the door latch of your recording unit, +forcing some doubt in your operating system about the fact this is really +the same volume as the one just written or read. + +The @option{--verify} option would not be necessary if drivers were indeed +able to detect dependably all write failures. This sometimes require many +magnetic heads, some able to read after the writes occurred. One would +not say that drivers unable to detect all cases are necessarily flawed, +as long as programming is concerned. + +The @option{--verify} (@option{-W}) option will not work in +conjunction with the @option{--multi-volume} (@option{-M}) option or +the @option{--append} (@option{-r}), @option{--update} (@option{-u}) +and @option{--delete} operations. @xref{Operations}, for more +information on these operations. + +Also, since @command{tar} normally strips leading @samp{/} from file +names (@pxref{absolute}), a command like @samp{tar --verify -cf +/tmp/foo.tar /etc} will work as desired only if the working directory is +@file{/}, as @command{tar} uses the archive's relative member names +(e.g., @file{etc/motd}) when verifying the archive. + +@node Write Protection +@section Write Protection + +Almost all tapes and diskettes, and in a few rare cases, even disks can +be @dfn{write protected}, to protect data on them from being changed. +Once an archive is written, you should write protect the media to prevent +the archive from being accidentally overwritten or deleted. (This will +protect the archive from being changed with a tape or floppy drive---it +will not protect it from magnet fields or other physical hazards.) + +The write protection device itself is usually an integral part of the +physical media, and can be a two position (write enabled/write +disabled) switch, a notch which can be popped out or covered, a ring +which can be removed from the center of a tape reel, or some other +changeable feature. + +@node Reliability and security +@chapter Reliability and Security + +The @command{tar} command reads and writes files as any other +application does, and is subject to the usual caveats about +reliability and security. This section contains some commonsense +advice on the topic. + +@menu +* Reliability:: +* Security:: +@end menu + +@node Reliability +@section Reliability + +Ideally, when @command{tar} is creating an archive, it reads from a +file system that is not being modified, and encounters no errors or +inconsistencies while reading and writing. If this is the case, the +archive should faithfully reflect what was read. Similarly, when +extracting from an archive, ideally @command{tar} ideally encounters +no errors and the extracted files faithfully reflect what was in the +archive. + +However, when reading or writing real-world file systems, several +things can go wrong; these include permissions problems, corruption of +data, and race conditions. + +@menu +* Permissions problems:: +* Data corruption and repair:: +* Race conditions:: +@end menu + +@node Permissions problems +@subsection Permissions Problems + +If @command{tar} encounters errors while reading or writing files, it +normally reports an error and exits with nonzero status. The work it +does may therefore be incomplete. For example, when creating an +archive, if @command{tar} cannot read a file then it cannot copy the +file into the archive. + +@node Data corruption and repair +@subsection Data Corruption and Repair + +If an archive becomes corrupted by an I/O error, this may corrupt the +data in an extracted file. Worse, it may corrupt the file's metadata, +which may cause later parts of the archive to become misinterpreted. +An tar-format archive contains a checksum that most likely will detect +errors in the metadata, but it will not detect errors in the data. + +If data corruption is a concern, you can compute and check your own +checksums of an archive by using other programs, such as +@command{cksum}. + +When attempting to recover from a read error or data corruption in an +archive, you may need to skip past the questionable data and read the +rest of the archive. This requires some expertise in the archive +format and in other software tools. + +@node Race conditions +@subsection Race conditions + +If some other process is modifying the file system while @command{tar} +is reading or writing files, the result may well be inconsistent due +to race conditions. For example, if another process creates some +files in a directory while @command{tar} is creating an archive +containing the directory's files, @command{tar} may see some of the +files but not others, or it may see a file that is in the process of +being created. The resulting archive may not be a snapshot of the +file system at any point in time. If an application such as a +database system depends on an accurate snapshot, restoring from the +@command{tar} archive of a live file system may therefore break that +consistency and may break the application. The simplest way to avoid +the consistency issues is to avoid making other changes to the file +system while tar is reading it or writing it. + +When creating an archive, several options are available to avoid race +conditions. Some hosts have a way of snapshotting a file system, or +of temporarily suspending all changes to a file system, by (say) +suspending the only virtual machine that can modify a file system; if +you use these facilities and have @command{tar -c} read from a +snapshot when creating an archive, you can avoid inconsistency +problems. More drastically, before starting @command{tar} you could +suspend or shut down all processes other than @command{tar} that have +access to the file system, or you could unmount the file system and +then mount it read-only. + +When extracting from an archive, one approach to avoid race conditions +is to create a directory that no other process can write to, and +extract into that. + +@node Security +@section Security + +In some cases @command{tar} may be used in an adversarial situation, +where an untrusted user is attempting to gain information about or +modify otherwise-inaccessible files. Dealing with untrusted data +(that is, data generated by an untrusted user) typically requires +extra care, because even the smallest mistake in the use of +@command{tar} is more likely to be exploited by an adversary than by a +race condition. + +@menu +* Privacy:: +* Integrity:: +* Live untrusted data:: +* Security rules of thumb:: +@end menu + +@node Privacy +@subsection Privacy + +Standard privacy concerns apply when using @command{tar}. For +example, suppose you are archiving your home directory into a file +@file{/archive/myhome.tar}. Any secret information in your home +directory, such as your SSH secret keys, are copied faithfully into +the archive. Therefore, if your home directory contains any file that +should not be read by some other user, the archive itself should be +not be readable by that user. And even if the archive's data are +inaccessible to untrusted users, its metadata (such as size or +last-modified date) may reveal some information about your home +directory; if the metadata are intended to be private, the archive's +parent directory should also be inaccessible to untrusted users. + +One precaution is to create @file{/archive} so that it is not +accessible to any user, unless that user also has permission to access +all the files in your home directory. + +Similarly, when extracting from an archive, take care that the +permissions of the extracted files are not more generous than what you +want. Even if the archive itself is readable only to you, files +extracted from it have their own permissions that may differ. + +@node Integrity +@subsection Integrity + +When creating archives, take care that they are not writable by a +untrusted user; otherwise, that user could modify the archive, and +when you later extract from the archive you will get incorrect data. + +When @command{tar} extracts from an archive, by default it writes into +files relative to the working directory. If the archive was generated +by an untrusted user, that user therefore can write into any file +under the working directory. If the working directory contains a +symbolic link to another directory, the untrusted user can also write +into any file under the referenced directory. When extracting from an +untrusted archive, it is therefore good practice to create an empty +directory and run @command{tar} in that directory. + +When extracting from two or more untrusted archives, each one should +be extracted independently, into different empty directories. +Otherwise, the first archive could create a symbolic link into an area +outside the working directory, and the second one could follow the +link and overwrite data that is not under the working directory. For +example, when restoring from a series of incremental dumps, the +archives should have been created by a trusted process, as otherwise +the incremental restores might alter data outside the working +directory. + +If you use the @option{--absolute-names} (@option{-P}) option when +extracting, @command{tar} respects any file names in the archive, even +file names that begin with @file{/} or contain @file{..}. As this +lets the archive overwrite any file in your system that you can write, +the @option{--absolute-names} (@option{-P}) option should be used only +for trusted archives. + +Conversely, with the @option{--keep-old-files} (@option{-k}) option, +@command{tar} refuses to replace existing files when extracting; and +with the @option{--no-overwrite-dir} option, @command{tar} refuses to +replace the permissions or ownership of already-existing directories. +These options may help when extracting from untrusted archives. + +@node Live untrusted data +@subsection Dealing with Live Untrusted Data + +Extra care is required when creating from or extracting into a file +system that is accessible to untrusted users. For example, superusers +who invoke @command{tar} must be wary about its actions being hijacked +by an adversary who is reading or writing the file system at the same +time that @command{tar} is operating. + +When creating an archive from a live file system, @command{tar} is +vulnerable to denial-of-service attacks. For example, an adversarial +user could create the illusion of an indefinitely-deep directory +hierarchy @file{d/e/f/g/...} by creating directories one step ahead of +@command{tar}, or the illusion of an indefinitely-long file by +creating a sparse file but arranging for blocks to be allocated just +before @command{tar} reads them. There is no easy way for +@command{tar} to distinguish these scenarios from legitimate uses, so +you may need to monitor @command{tar}, just as you'd need to monitor +any other system service, to detect such attacks. + +While a superuser is extracting from an archive into a live file +system, an untrusted user might replace a directory with a symbolic +link, in hopes that @command{tar} will follow the symbolic link and +extract data into files that the untrusted user does not have access +to. Even if the archive was generated by the superuser, it may +contain a file such as @file{d/etc/passwd} that the untrusted user +earlier created in order to break in; if the untrusted user replaces +the directory @file{d/etc} with a symbolic link to @file{/etc} while +@command{tar} is running, @command{tar} will overwrite +@file{/etc/passwd}. This attack can be prevented by extracting into a +directory that is inaccessible to untrusted users. + +Similar attacks via symbolic links are also possible when creating an +archive, if the untrusted user can modify an ancestor of a top-level +argument of @command{tar}. For example, an untrusted user that can +modify @file{/home/eve} can hijack a running instance of @samp{tar -cf +- /home/eve/Documents/yesterday} by replacing +@file{/home/eve/Documents} with a symbolic link to some other +location. Attacks like these can be prevented by making sure that +untrusted users cannot modify any files that are top-level arguments +to @command{tar}, or any ancestor directories of these files. + +@node Security rules of thumb +@subsection Security Rules of Thumb + +This section briefly summarizes rules of thumb for avoiding security +pitfalls. + +@itemize @bullet + +@item +Protect archives at least as much as you protect any of the files +being archived. + +@item +Extract from an untrusted archive only into an otherwise-empty +directory. This directory and its parent should be accessible only to +trusted users. For example: + +@example +@group +$ @kbd{chmod go-rwx .} +$ @kbd{mkdir -m go-rwx dir} +$ @kbd{cd dir} +$ @kbd{tar -xvf /archives/got-it-off-the-net.tar.gz} +@end group +@end example + +As a corollary, do not do an incremental restore from an untrusted archive. + +@item +Do not let untrusted users access files extracted from untrusted +archives without checking first for problems such as setuid programs. + +@item +Do not let untrusted users modify directories that are ancestors of +top-level arguments of @command{tar}. For example, while you are +executing @samp{tar -cf /archive/u-home.tar /u/home}, do not let an +untrusted user modify @file{/}, @file{/archive}, or @file{/u}. + +@item +Pay attention to the diagnostics and exit status of @command{tar}. + +@item +When archiving live file systems, monitor running instances of +@command{tar} to detect denial-of-service attacks. + +@item +Avoid unusual options such as @option{--absolute-names} (@option{-P}), +@option{--dereference} (@option{-h}), @option{--overwrite}, +@option{--recursive-unlink}, and @option{--remove-files} unless you +understand their security implications. + +@end itemize + +@node Changes +@appendix Changes + +This appendix lists some important user-visible changes between +version @GNUTAR{} @value{VERSION} and previous versions. An up-to-date +version of this document is available at +@uref{http://www.gnu.org/@/software/@/tar/manual/changes.html,the +@GNUTAR{} documentation page}. + +@table @asis +@item Use of globbing patterns when listing and extracting. + +Previous versions of GNU tar assumed shell-style globbing when +extracting from or listing an archive. For example: + +@smallexample +$ @kbd{tar xf foo.tar '*.c'} +@end smallexample + +would extract all files whose names end in @samp{.c}. This behavior +was not documented and was incompatible with traditional tar +implementations. Therefore, starting from version 1.15.91, GNU tar +no longer uses globbing by default. For example, the above invocation +is now interpreted as a request to extract from the archive the file +named @file{*.c}. + +To facilitate transition to the new behavior for those users who got +used to the previous incorrect one, @command{tar} will print a warning +if it finds out that a requested member was not found in the archive +and its name looks like a globbing pattern. For example: + +@smallexample +$ @kbd{tar xf foo.tar '*.c'} +tar: Pattern matching characters used in file names. Please, +tar: use --wildcards to enable pattern matching, or --no-wildcards to +tar: suppress this warning. +tar: *.c: Not found in archive +tar: Error exit delayed from previous errors +@end smallexample + +To treat member names as globbing patterns, use the @option{--wildcards} option. +If you want to tar to mimic the behavior of versions prior to 1.15.91, +add this option to your @env{TAR_OPTIONS} variable. + +@xref{wildcards}, for the detailed discussion of the use of globbing +patterns by @GNUTAR{}. + +@item Use of short option @option{-o}. + +Earlier versions of @GNUTAR{} understood @option{-o} command line +option as a synonym for @option{--old-archive}. + +@GNUTAR{} starting from version 1.13.90 understands this option as +a synonym for @option{--no-same-owner}. This is compatible with +UNIX98 @command{tar} implementations. + +However, to facilitate transition, @option{-o} option retains its +old semantics when it is used with one of archive-creation commands. +Users are encouraged to use @option{--format=oldgnu} instead. + +It is especially important, since versions of @acronym{GNU} Automake +up to and including 1.8.4 invoke tar with this option to produce +distribution tarballs. @xref{Formats,v7}, for the detailed discussion +of this issue and its implications. + +@xref{Options, tar-formats, Changing Automake's Behavior, +automake, GNU Automake}, for a description on how to use various +archive formats with @command{automake}. + +Future versions of @GNUTAR{} will understand @option{-o} only as a +synonym for @option{--no-same-owner}. + +@item Use of short option @option{-l} + +Earlier versions of @GNUTAR{} understood @option{-l} option as a +synonym for @option{--one-file-system}. Since such usage contradicted +to UNIX98 specification and harmed compatibility with other +implementations, it was declared deprecated in version 1.14. However, +to facilitate transition to its new semantics, it was supported by +versions 1.15 and 1.15.90. The present use of @option{-l} as a short +variant of @option{--check-links} was introduced in version 1.15.91. + +@item Use of options @option{--portability} and @option{--old-archive} + +These options are deprecated. Please use @option{--format=v7} instead. + +@item Use of option @option{--posix} + +This option is deprecated. Please use @option{--format=posix} instead. +@end table + +@node Configuring Help Summary +@appendix Configuring Help Summary + +Running @kbd{tar --help} displays the short @command{tar} option +summary (@pxref{help}). This summary is organized by @dfn{groups} of +semantically close options. The options within each group are printed +in the following order: a short option, eventually followed by a list +of corresponding long option names, followed by a short description of +the option. For example, here is an excerpt from the actual @kbd{tar +--help} output: + +@verbatim + Main operation mode: + + -A, --catenate, --concatenate append tar files to an archive + -c, --create create a new archive + -d, --diff, --compare find differences between archive and + file system + --delete delete from the archive +@end verbatim + +@vrindex ARGP_HELP_FMT, environment variable +The exact visual representation of the help output is configurable via +@env{ARGP_HELP_FMT} environment variable. The value of this variable +is a comma-separated list of @dfn{format variable} assignments. There +are two kinds of format variables. An @dfn{offset variable} keeps the +offset of some part of help output text from the leftmost column on +the screen. A @dfn{boolean} variable is a flag that toggles some +output feature on or off. Depending on the type of the corresponding +variable, there are two kinds of assignments: + +@table @asis +@item Offset assignment + +The assignment to an offset variable has the following syntax: + +@smallexample +@var{variable}=@var{value} +@end smallexample + +@noindent +where @var{variable} is the variable name, and @var{value} is a +numeric value to be assigned to the variable. + +@item Boolean assignment + +To assign @code{true} value to a variable, simply put this variable name. To +assign @code{false} value, prefix the variable name with @samp{no-}. For +example: + +@smallexample +@group +# Assign @code{true} value: +dup-args +# Assign @code{false} value: +no-dup-args +@end group +@end smallexample +@end table + +Following variables are declared: + +@deftypevr {Help Output} boolean dup-args +If true, arguments for an option are shown with both short and long +options, even when a given option has both forms, for example: + +@smallexample + -f ARCHIVE, --file=ARCHIVE use archive file or device ARCHIVE +@end smallexample + +If false, then if an option has both short and long forms, the +argument is only shown with the long one, for example: + +@smallexample + -f, --file=ARCHIVE use archive file or device ARCHIVE +@end smallexample + +@noindent +and a message indicating that the argument is applicable to both +forms is printed below the options. This message can be disabled +using @code{dup-args-note} (see below). + +The default is false. +@end deftypevr + +@deftypevr {Help Output} boolean dup-args-note +If this variable is true, which is the default, the following notice +is displayed at the end of the help output: + +@quotation +Mandatory or optional arguments to long options are also mandatory or +optional for any corresponding short options. +@end quotation + +Setting @code{no-dup-args-note} inhibits this message. Normally, only one of +variables @code{dup-args} or @code{dup-args-note} should be set. +@end deftypevr + +@deftypevr {Help Output} offset short-opt-col +Column in which short options start. Default is 2. + +@smallexample +@group +$ @kbd{tar --help|grep ARCHIVE} + -f, --file=ARCHIVE use archive file or device ARCHIVE +$ @kbd{ARGP_HELP_FMT=short-opt-col=6 tar --help|grep ARCHIVE} + -f, --file=ARCHIVE use archive file or device ARCHIVE +@end group +@end smallexample +@end deftypevr + +@deftypevr {Help Output} offset long-opt-col +Column in which long options start. Default is 6. For example: + +@smallexample +@group +$ @kbd{tar --help|grep ARCHIVE} + -f, --file=ARCHIVE use archive file or device ARCHIVE +$ @kbd{ARGP_HELP_FMT=long-opt-col=16 tar --help|grep ARCHIVE} + -f, --file=ARCHIVE use archive file or device ARCHIVE +@end group +@end smallexample +@end deftypevr + +@deftypevr {Help Output} offset doc-opt-col +Column in which @dfn{doc options} start. A doc option isn't actually +an option, but rather an arbitrary piece of documentation that is +displayed in much the same manner as the options. For example, in +the description of @option{--format} option: + +@smallexample +@group + -H, --format=FORMAT create archive of the given format. + + FORMAT is one of the following: + + gnu GNU tar 1.13.x format + oldgnu GNU format as per tar <= 1.12 + pax POSIX 1003.1-2001 (pax) format + posix same as pax + ustar POSIX 1003.1-1988 (ustar) format + v7 old V7 tar format +@end group +@end smallexample + +@noindent +the format names are doc options. Thus, if you set +@kbd{ARGP_HELP_FMT=doc-opt-col=6} the above part of the help output +will look as follows: + +@smallexample +@group + -H, --format=FORMAT create archive of the given format. + + FORMAT is one of the following: + + gnu GNU tar 1.13.x format + oldgnu GNU format as per tar <= 1.12 + pax POSIX 1003.1-2001 (pax) format + posix same as pax + ustar POSIX 1003.1-1988 (ustar) format + v7 old V7 tar format +@end group +@end smallexample +@end deftypevr + +@deftypevr {Help Output} offset opt-doc-col +Column in which option description starts. Default is 29. + +@smallexample +@group +$ @kbd{tar --help|grep ARCHIVE} + -f, --file=ARCHIVE use archive file or device ARCHIVE +$ @kbd{ARGP_HELP_FMT=opt-doc-col=19 tar --help|grep ARCHIVE} + -f, --file=ARCHIVE use archive file or device ARCHIVE +$ @kbd{ARGP_HELP_FMT=opt-doc-col=9 tar --help|grep ARCHIVE} + -f, --file=ARCHIVE + use archive file or device ARCHIVE +@end group +@end smallexample + +@noindent +Notice, that the description starts on a separate line if +@code{opt-doc-col} value is too small. +@end deftypevr + +@deftypevr {Help Output} offset header-col +Column in which @dfn{group headers} are printed. A group header is a +descriptive text preceding an option group. For example, in the +following text: + +@verbatim + Main operation mode: + + -A, --catenate, --concatenate append tar files to + an archive + -c, --create create a new archive +@end verbatim +@noindent +@samp{Main operation mode:} is the group header. + +The default value is 1. +@end deftypevr + +@deftypevr {Help Output} offset usage-indent +Indentation of wrapped usage lines. Affects @option{--usage} +output. Default is 12. +@end deftypevr + +@deftypevr {Help Output} offset rmargin +Right margin of the text output. Used for wrapping. +@end deftypevr + +@node Fixing Snapshot Files +@appendix Fixing Snapshot Files +@include tar-snapshot-edit.texi + +@node Tar Internals +@appendix Tar Internals +@include intern.texi + +@node Genfile +@appendix Genfile +@include genfile.texi + +@node Free Software Needs Free Documentation +@appendix Free Software Needs Free Documentation +@include freemanuals.texi + +@node GNU Free Documentation License +@appendix GNU Free Documentation License + +@include fdl.texi + +@node Index of Command Line Options +@appendix Index of Command Line Options + +This appendix contains an index of all @GNUTAR{} long command line +options. The options are listed without the preceding double-dash. +For a cross-reference of short command line options, see +@ref{Short Option Summary}. + +@printindex op + +@node Index +@appendix Index + +@printindex cp + +@summarycontents +@contents +@bye + +@c Local variables: +@c texinfo-column-for-description: 32 +@c End: diff -Naur tar-1.26.orig//src/common.h tar-1.26/src/common.h --- tar-1.26.orig//src/common.h 2011-02-11 06:56:13.000000000 -0500 +++ tar-1.26/src/common.h 2011-09-06 13:22:06.000000000 -0400 @@ -253,6 +253,15 @@ /* If positive, preserve permissions when extracting. */ GLOBAL int same_permissions_option; +/* If positive, save the SELinux context. */ +GLOBAL int selinux_context_option; + +/* If positive, save the ACLs. */ +GLOBAL int acls_option; + +/* If positive, save the user and root xattrs. */ +GLOBAL int xattrs_option; + /* When set, strip the given number of file name components from the file name before extracting */ GLOBAL size_t strip_name_components; @@ -707,6 +716,9 @@ void update_archive (void); +/* Module attrs.c. */ +#include "xattrs.h" + /* Module xheader.c. */ void xheader_decode (struct tar_stat_info *stat); @@ -727,6 +739,12 @@ bool xheader_keyword_deleted_p (const char *kw); char *xheader_format_name (struct tar_stat_info *st, const char *fmt, size_t n); +void xheader_xattr_init(struct tar_stat_info *st); +void xheader_xattr_free(struct xattr_array *vals, size_t sz); +void xheader_xattr_copy(const struct tar_stat_info *st, + struct xattr_array **vals, size_t *sz); +void xheader_xattr_add(struct tar_stat_info *st, + const char *key, const char *val, size_t len); /* Module system.c */ diff -Naur tar-1.26.orig//src/common.h.orig tar-1.26/src/common.h.orig --- tar-1.26.orig//src/common.h.orig 1969-12-31 19:00:00.000000000 -0500 +++ tar-1.26/src/common.h.orig 2011-02-11 06:56:13.000000000 -0500 @@ -0,0 +1,834 @@ +/* Common declarations for the tar program. + + Copyright (C) 1988, 1992, 1993, 1994, 1996, 1997, 1999, 2000, 2001, + 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, + Inc. + + This program is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by the + Free Software Foundation; either version 3, or (at your option) any later + version. + + This program is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General + Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ + +/* Declare the GNU tar archive format. */ +#include "tar.h" + +/* The checksum field is filled with this while the checksum is computed. */ +#define CHKBLANKS " " /* 8 blanks, no null */ + +/* Some constants from POSIX are given names. */ +#define NAME_FIELD_SIZE 100 +#define PREFIX_FIELD_SIZE 155 +#define UNAME_FIELD_SIZE 32 +#define GNAME_FIELD_SIZE 32 + + + +/* Some various global definitions. */ + +/* Name of file to use for interacting with user. */ + +/* GLOBAL is defined to empty in tar.c only, and left alone in other *.c + modules. Here, we merely set it to "extern" if it is not already set. + GNU tar does depend on the system loader to preset all GLOBAL variables to + neutral (or zero) values, explicit initialization is usually not done. */ +#ifndef GLOBAL +# define GLOBAL extern +#endif + +#define TAREXIT_SUCCESS PAXEXIT_SUCCESS +#define TAREXIT_DIFFERS PAXEXIT_DIFFERS +#define TAREXIT_FAILURE PAXEXIT_FAILURE + + +#include "arith.h" +#include +#include +#include +#include +#include +#include +#include +#include +#define obstack_chunk_alloc xmalloc +#define obstack_chunk_free free +#include +#include +#include + +#include + +/* Log base 2 of common values. */ +#define LG_8 3 +#define LG_64 6 +#define LG_256 8 + +/* Information gleaned from the command line. */ + +/* Main command option. */ + +enum subcommand +{ + UNKNOWN_SUBCOMMAND, /* none of the following */ + APPEND_SUBCOMMAND, /* -r */ + CAT_SUBCOMMAND, /* -A */ + CREATE_SUBCOMMAND, /* -c */ + DELETE_SUBCOMMAND, /* -D */ + DIFF_SUBCOMMAND, /* -d */ + EXTRACT_SUBCOMMAND, /* -x */ + LIST_SUBCOMMAND, /* -t */ + UPDATE_SUBCOMMAND, /* -u */ + TEST_LABEL_SUBCOMMAND, /* --test-label */ +}; + +GLOBAL enum subcommand subcommand_option; + +/* Selected format for output archive. */ +GLOBAL enum archive_format archive_format; + +/* Size of each record, once in blocks, once in bytes. Those two variables + are always related, the second being BLOCKSIZE times the first. They do + not have _option in their name, even if their values is derived from + option decoding, as these are especially important in tar. */ +GLOBAL int blocking_factor; +GLOBAL size_t record_size; + +GLOBAL bool absolute_names_option; + +/* Display file times in UTC */ +GLOBAL bool utc_option; +/* Output file timestamps to the full resolution */ +GLOBAL bool full_time_option; + +/* This variable tells how to interpret newer_mtime_option, below. If zero, + files get archived if their mtime is not less than newer_mtime_option. + If nonzero, files get archived if *either* their ctime or mtime is not less + than newer_mtime_option. */ +GLOBAL int after_date_option; + +enum atime_preserve +{ + no_atime_preserve, + replace_atime_preserve, + system_atime_preserve +}; +GLOBAL enum atime_preserve atime_preserve_option; + +GLOBAL bool backup_option; + +/* Type of backups being made. */ +GLOBAL enum backup_type backup_type; + +GLOBAL bool block_number_option; + +GLOBAL unsigned checkpoint_option; +#define DEFAULT_CHECKPOINT 10 + +/* Specified name of compression program, or "gzip" as implied by -z. */ +GLOBAL const char *use_compress_program_option; + +GLOBAL bool dereference_option; +GLOBAL bool hard_dereference_option; + +/* Print a message if not all links are dumped */ +GLOBAL int check_links_option; + +/* Patterns that match file names to be excluded. */ +GLOBAL struct exclude *excluded; + +enum exclusion_tag_type + { + exclusion_tag_none, + /* Exclude the directory contents, but preserve the directory + itself and the exclusion tag file */ + exclusion_tag_contents, + /* Exclude everything below the directory, preserving the directory + itself */ + exclusion_tag_under, + /* Exclude entire directory */ + exclusion_tag_all, + }; + +/* Specified value to be put into tar file in place of stat () results, or + just -1 if such an override should not take place. */ +GLOBAL gid_t group_option; + +GLOBAL bool ignore_failed_read_option; + +GLOBAL bool ignore_zeros_option; + +GLOBAL bool incremental_option; + +/* Specified name of script to run at end of each tape change. */ +GLOBAL const char *info_script_option; + +GLOBAL bool interactive_option; + +/* If nonzero, extract only Nth occurrence of each named file */ +GLOBAL uintmax_t occurrence_option; + +enum old_files +{ + DEFAULT_OLD_FILES, /* default */ + NO_OVERWRITE_DIR_OLD_FILES, /* --no-overwrite-dir */ + OVERWRITE_OLD_FILES, /* --overwrite */ + UNLINK_FIRST_OLD_FILES, /* --unlink-first */ + KEEP_OLD_FILES, /* --keep-old-files */ + KEEP_NEWER_FILES /* --keep-newer-files */ +}; +GLOBAL enum old_files old_files_option; + +/* Specified file name for incremental list. */ +GLOBAL const char *listed_incremental_option; +/* Incremental dump level */ +GLOBAL int incremental_level; +/* Check device numbers when doing incremental dumps. */ +GLOBAL bool check_device_option; + +/* Specified mode change string. */ +GLOBAL struct mode_change *mode_option; + +/* Initial umask, if needed for mode change string. */ +GLOBAL mode_t initial_umask; + +GLOBAL bool multi_volume_option; + +/* Specified threshold date and time. Files having an older time stamp + do not get archived (also see after_date_option above). */ +GLOBAL struct timespec newer_mtime_option; + +/* If true, override actual mtime (see below) */ +GLOBAL bool set_mtime_option; +/* Value to be put in mtime header field instead of the actual mtime */ +GLOBAL struct timespec mtime_option; + +/* Return true if newer_mtime_option is initialized. */ +#define NEWER_OPTION_INITIALIZED(opt) (0 <= (opt).tv_nsec) + +/* Return true if the struct stat ST's M time is less than + newer_mtime_option. */ +#define OLDER_STAT_TIME(st, m) \ + (timespec_cmp (get_stat_##m##time (&(st)), newer_mtime_option) < 0) + +/* Likewise, for struct tar_stat_info ST. */ +#define OLDER_TAR_STAT_TIME(st, m) \ + (timespec_cmp ((st).m##time, newer_mtime_option) < 0) + +/* Zero if there is no recursion, otherwise FNM_LEADING_DIR. */ +GLOBAL int recursion_option; + +GLOBAL bool numeric_owner_option; + +GLOBAL bool one_file_system_option; + +/* Specified value to be put into tar file in place of stat () results, or + just -1 if such an override should not take place. */ +GLOBAL uid_t owner_option; + +GLOBAL bool recursive_unlink_option; + +GLOBAL bool read_full_records_option; + +GLOBAL bool remove_files_option; + +/* Specified rmt command. */ +GLOBAL const char *rmt_command_option; + +/* Specified remote shell command. */ +GLOBAL const char *rsh_command_option; + +GLOBAL bool same_order_option; + +/* If positive, preserve ownership when extracting. */ +GLOBAL int same_owner_option; + +/* If positive, preserve permissions when extracting. */ +GLOBAL int same_permissions_option; + +/* When set, strip the given number of file name components from the file name + before extracting */ +GLOBAL size_t strip_name_components; + +GLOBAL bool show_omitted_dirs_option; + +GLOBAL bool sparse_option; +GLOBAL unsigned tar_sparse_major; +GLOBAL unsigned tar_sparse_minor; + +GLOBAL bool starting_file_option; + +/* Specified maximum byte length of each tape volume (multiple of 1024). */ +GLOBAL tarlong tape_length_option; + +GLOBAL bool to_stdout_option; + +GLOBAL bool totals_option; + +GLOBAL bool touch_option; + +GLOBAL char *to_command_option; +GLOBAL bool ignore_command_error_option; + +/* Restrict some potentially harmful tar options */ +GLOBAL bool restrict_option; + +/* Return true if the extracted files are not being written to disk */ +#define EXTRACT_OVER_PIPE (to_stdout_option || to_command_option) + +/* Count how many times the option has been set, multiple setting yields + more verbose behavior. Value 0 means no verbosity, 1 means file name + only, 2 means file name and all attributes. More than 2 is just like 2. */ +GLOBAL int verbose_option; + +GLOBAL bool verify_option; + +/* Specified name of file containing the volume number. */ +GLOBAL const char *volno_file_option; + +/* Specified value or pattern. */ +GLOBAL const char *volume_label_option; + +/* Other global variables. */ + +/* File descriptor for archive file. */ +GLOBAL int archive; + +/* Nonzero when outputting to /dev/null. */ +GLOBAL bool dev_null_output; + +/* Timestamps: */ +GLOBAL struct timespec start_time; /* when we started execution */ +GLOBAL struct timespec volume_start_time; /* when the current volume was + opened*/ +GLOBAL struct timespec last_stat_time; /* when the statistics was last + computed */ + +GLOBAL struct tar_stat_info current_stat_info; + +/* List of tape drive names, number of such tape drives, allocated number, + and current cursor in list. */ +GLOBAL const char **archive_name_array; +GLOBAL size_t archive_names; +GLOBAL size_t allocated_archive_names; +GLOBAL const char **archive_name_cursor; + +/* Output index file name. */ +GLOBAL char const *index_file_name; + +/* Opaque structure for keeping directory meta-data */ +struct directory; + +/* Structure for keeping track of filenames and lists thereof. */ +struct name + { + struct name *next; /* Link to the next element */ + struct name *prev; /* Link to the previous element */ + + char *name; /* File name or globbing pattern */ + size_t length; /* cached strlen (name) */ + int matching_flags; /* wildcard flags if name is a pattern */ + bool cmdline; /* true if this name was given in the + command line */ + + int change_dir; /* Number of the directory to change to. + Set with the -C option. */ + uintmax_t found_count; /* number of times a matching file has + been found */ + + /* The following members are used for incremental dumps only, + if this struct name represents a directory; + see incremen.c */ + struct directory *directory;/* directory meta-data and contents */ + struct name *parent; /* pointer to the parent hierarchy */ + struct name *child; /* pointer to the first child */ + struct name *sibling; /* pointer to the next sibling */ + char *caname; /* canonical name */ + }; + +/* Obnoxious test to see if dimwit is trying to dump the archive. */ +GLOBAL dev_t ar_dev; +GLOBAL ino_t ar_ino; + +/* Flags for reading, searching, and fstatatting files. */ +GLOBAL int open_read_flags; +GLOBAL int open_searchdir_flags; +GLOBAL int fstatat_flags; + +GLOBAL int seek_option; +GLOBAL bool seekable_archive; + +GLOBAL dev_t root_device; + +/* Unquote filenames */ +GLOBAL bool unquote_option; + +/* Show file or archive names after transformation. + In particular, when creating archive in verbose mode, list member names + as stored in the archive */ +GLOBAL bool show_transformed_names_option; + +/* Delay setting modification times and permissions of extracted directories + until the end of extraction. This variable helps correctly restore directory + timestamps from archives with an unusual member order. It is automatically + set for incremental archives. */ +GLOBAL bool delay_directory_restore_option; + +/* Warn about implicit use of the wildcards in command line arguments. + (Default for tar prior to 1.15.91, but changed afterwards */ +GLOBAL bool warn_regex_usage; + +/* Declarations for each module. */ + +/* FIXME: compare.c should not directly handle the following variable, + instead, this should be done in buffer.c only. */ + +enum access_mode +{ + ACCESS_READ, + ACCESS_WRITE, + ACCESS_UPDATE +}; +extern enum access_mode access_mode; + +/* Module buffer.c. */ + +extern FILE *stdlis; +extern bool write_archive_to_stdout; +extern char *volume_label; +extern size_t volume_label_count; +extern char *continued_file_name; +extern uintmax_t continued_file_size; +extern uintmax_t continued_file_offset; +extern off_t records_written; + +char *drop_volume_label_suffix (const char *label); + +size_t available_space_after (union block *pointer); +off_t current_block_ordinal (void); +void close_archive (void); +void closeout_volume_number (void); +void compute_duration (void); +union block *find_next_block (void); +void flush_read (void); +void flush_write (void); +void flush_archive (void); +void init_volume_number (void); +void open_archive (enum access_mode mode); +void print_total_stats (void); +void reset_eof (void); +void set_next_block_after (union block *block); +void clear_read_error_count (void); +void xclose (int fd); +void archive_write_error (ssize_t status) __attribute__ ((noreturn)); +void archive_read_error (void); +off_t seek_archive (off_t size); +void set_start_time (void); + +void mv_begin_write (const char *file_name, off_t totsize, off_t sizeleft); + +void mv_begin_read (struct tar_stat_info *st); +void mv_end (void); +void mv_size_left (off_t size); + +void buffer_write_global_xheader (void); + +const char *first_decompress_program (int *pstate); +const char *next_decompress_program (int *pstate); + +/* Module create.c. */ + +enum dump_status + { + dump_status_ok, + dump_status_short, + dump_status_fail, + dump_status_not_implemented + }; + +void add_exclusion_tag (const char *name, enum exclusion_tag_type type, + bool (*predicate) (int)); +bool cachedir_file_p (int fd); +char *get_directory_entries (struct tar_stat_info *st); + +void create_archive (void); +void pad_archive (off_t size_left); +void dump_file (struct tar_stat_info *parent, char const *name, + char const *fullname); +union block *start_header (struct tar_stat_info *st); +void finish_header (struct tar_stat_info *st, union block *header, + off_t block_ordinal); +void simple_finish_header (union block *header); +union block * write_extended (bool global, struct tar_stat_info *st, + union block *old_header); +union block *start_private_header (const char *name, size_t size, time_t t); +void write_eot (void); +void check_links (void); +int subfile_open (struct tar_stat_info const *dir, char const *file, int flags); +void restore_parent_fd (struct tar_stat_info const *st); +void exclusion_tag_warning (const char *dirname, const char *tagname, + const char *message); +enum exclusion_tag_type check_exclusion_tags (struct tar_stat_info const *st, + const char **tag_file_name); + +#define OFF_TO_CHARS(val, where) off_to_chars (val, where, sizeof (where)) +#define TIME_TO_CHARS(val, where) time_to_chars (val, where, sizeof (where)) + +bool off_to_chars (off_t off, char *buf, size_t size); +bool time_to_chars (time_t t, char *buf, size_t size); + +/* Module diffarch.c. */ + +extern bool now_verifying; + +void diff_archive (void); +void diff_init (void); +void verify_volume (void); + +/* Module extract.c. */ + +void extr_init (void); +void extract_archive (void); +void extract_finish (void); +bool rename_directory (char *src, char *dst); + +/* Module delete.c. */ + +void delete_archive_members (void); + +/* Module incremen.c. */ + +struct directory *scan_directory (struct tar_stat_info *st); +const char *directory_contents (struct directory *dir); +const char *safe_directory_contents (struct directory *dir); + +void rebase_directory (struct directory *dir, + const char *samp, size_t slen, + const char *repl, size_t rlen); + +void append_incremental_renames (struct directory *dir); +void read_directory_file (void); +void write_directory_file (void); +void purge_directory (char const *directory_name); +void list_dumpdir (char *buffer, size_t size); +void update_parent_directory (struct tar_stat_info *st); + +size_t dumpdir_size (const char *p); +bool is_dumpdir (struct tar_stat_info *stat_info); + +/* Module list.c. */ + +enum read_header +{ + HEADER_STILL_UNREAD, /* for when read_header has not been called */ + HEADER_SUCCESS, /* header successfully read and checksummed */ + HEADER_SUCCESS_EXTENDED, /* likewise, but we got an extended header */ + HEADER_ZERO_BLOCK, /* zero block where header expected */ + HEADER_END_OF_FILE, /* true end of file while header expected */ + HEADER_FAILURE /* ill-formed header, or bad checksum */ +}; + +/* Operation mode for read_header: */ + +enum read_header_mode +{ + read_header_auto, /* process extended headers automatically */ + read_header_x_raw, /* return raw extended headers (return + HEADER_SUCCESS_EXTENDED) */ + read_header_x_global /* when POSIX global extended header is read, + decode it and return + HEADER_SUCCESS_EXTENDED */ +}; +extern union block *current_header; +extern enum archive_format current_format; +extern size_t recent_long_name_blocks; +extern size_t recent_long_link_blocks; + +void decode_header (union block *header, struct tar_stat_info *stat_info, + enum archive_format *format_pointer, int do_user_group); +void transform_stat_info (int typeflag, struct tar_stat_info *stat_info); +char const *tartime (struct timespec t, bool full_time); + +#define OFF_FROM_HEADER(where) off_from_header (where, sizeof (where)) +#define UINTMAX_FROM_HEADER(where) uintmax_from_header (where, sizeof (where)) + +off_t off_from_header (const char *buf, size_t size); +uintmax_t uintmax_from_header (const char *buf, size_t size); + +void list_archive (void); +void test_archive_label (void); +void print_for_mkdir (char *dirname, int length, mode_t mode); +void print_header (struct tar_stat_info *st, union block *blk, + off_t block_ordinal); +void read_and (void (*do_something) (void)); +enum read_header read_header (union block **return_block, + struct tar_stat_info *info, + enum read_header_mode m); +enum read_header tar_checksum (union block *header, bool silent); +void skip_file (off_t size); +void skip_member (void); + +/* Module misc.c. */ + +void assign_string (char **dest, const char *src); +int unquote_string (char *str); +char *zap_slashes (char *name); +char *normalize_filename (const char *name); +void replace_prefix (char **pname, const char *samp, size_t slen, + const char *repl, size_t rlen); + +typedef struct namebuf *namebuf_t; +namebuf_t namebuf_create (const char *dir); +void namebuf_free (namebuf_t buf); +char *namebuf_name (namebuf_t buf, const char *name); + +void code_ns_fraction (int ns, char *p); +char const *code_timespec (struct timespec ts, char *sbuf); +enum { BILLION = 1000000000, LOG10_BILLION = 9 }; +enum { TIMESPEC_STRSIZE_BOUND = + UINTMAX_STRSIZE_BOUND + LOG10_BILLION + sizeof "-." - 1 }; + +bool must_be_dot_or_slash (char const *); + +enum remove_option +{ + ORDINARY_REMOVE_OPTION, + RECURSIVE_REMOVE_OPTION, + + /* FIXME: The following value is never used. It seems to be intended + as a placeholder for a hypothetical option that should instruct tar + to recursively remove subdirectories in purge_directory(), + as opposed to the functionality of --recursive-unlink + (RECURSIVE_REMOVE_OPTION value), which removes them in + prepare_to_extract() phase. However, with the addition of more + meta-info to the incremental dumps, this should become unnecessary */ + WANT_DIRECTORY_REMOVE_OPTION +}; +int remove_any_file (const char *file_name, enum remove_option option); +bool maybe_backup_file (const char *file_name, bool this_is_the_archive); +void undo_last_backup (void); + +int deref_stat (char const *name, struct stat *buf); + +extern int chdir_current; +extern int chdir_fd; +int chdir_arg (char const *dir); +void chdir_do (int dir); +int chdir_count (void); + +void close_diag (char const *name); +void open_diag (char const *name); +void read_diag_details (char const *name, off_t offset, size_t size); +void readlink_diag (char const *name); +void savedir_diag (char const *name); +void seek_diag_details (char const *name, off_t offset); +void stat_diag (char const *name); +void file_removed_diag (const char *name, bool top_level, + void (*diagfn) (char const *name)); +void write_error_details (char const *name, size_t status, size_t size); +void write_fatal (char const *name) __attribute__ ((noreturn)); +void write_fatal_details (char const *name, ssize_t status, size_t size) + __attribute__ ((noreturn)); + +pid_t xfork (void); +void xpipe (int fd[2]); + +void *page_aligned_alloc (void **ptr, size_t size); +int set_file_atime (int fd, int parentfd, char const *file, + struct timespec atime); + +/* Module names.c. */ + +extern size_t name_count; +extern struct name *gnu_list_name; + +void gid_to_gname (gid_t gid, char **gname); +int gname_to_gid (char const *gname, gid_t *pgid); +void uid_to_uname (uid_t uid, char **uname); +int uname_to_uid (char const *uname, uid_t *puid); + +void name_init (void); +void name_add_name (const char *name, int matching_flags); +void name_add_dir (const char *name); +void name_term (void); +const char *name_next (int change_dirs); +void name_gather (void); +struct name *addname (char const *string, int change_dir, + bool cmdline, struct name *parent); +void remname (struct name *name); +bool name_match (const char *name); +void names_notfound (void); +void label_notfound (void); +void collect_and_sort_names (void); +struct name *name_scan (const char *name); +struct name const *name_from_list (void); +void blank_name_list (void); +char *new_name (const char *dir_name, const char *name); +size_t stripped_prefix_len (char const *file_name, size_t num); +bool all_names_found (struct tar_stat_info *st); + +bool excluded_name (char const *name); + +void add_avoided_name (char const *name); +bool is_avoided_name (char const *name); + +bool contains_dot_dot (char const *name); + +#define ISFOUND(c) ((occurrence_option == 0) ? (c)->found_count : \ + (c)->found_count == occurrence_option) +#define WASFOUND(c) ((occurrence_option == 0) ? (c)->found_count : \ + (c)->found_count >= occurrence_option) + +/* Module tar.c. */ + +void usage (int); + +int confirm (const char *message_action, const char *name); + +void tar_stat_init (struct tar_stat_info *st); +bool tar_stat_close (struct tar_stat_info *st); +void tar_stat_destroy (struct tar_stat_info *st); +void usage (int) __attribute__ ((noreturn)); +int tar_timespec_cmp (struct timespec a, struct timespec b); +const char *archive_format_string (enum archive_format fmt); +const char *subcommand_string (enum subcommand c); +void set_exit_status (int val); + +/* Module update.c. */ + +extern char *output_start; + +void update_archive (void); + +/* Module xheader.c. */ + +void xheader_decode (struct tar_stat_info *stat); +void xheader_decode_global (struct xheader *xhdr); +void xheader_store (char const *keyword, struct tar_stat_info *st, + void const *data); +void xheader_read (struct xheader *xhdr, union block *header, size_t size); +void xheader_write (char type, char *name, time_t t, struct xheader *xhdr); +void xheader_write_global (struct xheader *xhdr); +void xheader_finish (struct xheader *hdr); +void xheader_destroy (struct xheader *hdr); +char *xheader_xhdr_name (struct tar_stat_info *st); +char *xheader_ghdr_name (void); +void xheader_set_option (char *string); +void xheader_string_begin (struct xheader *xhdr); +void xheader_string_add (struct xheader *xhdr, char const *s); +bool xheader_string_end (struct xheader *xhdr, char const *keyword); +bool xheader_keyword_deleted_p (const char *kw); +char *xheader_format_name (struct tar_stat_info *st, const char *fmt, + size_t n); + +/* Module system.c */ + +void sys_detect_dev_null_output (void); +void sys_save_archive_dev_ino (void); +void sys_wait_for_child (pid_t, bool); +void sys_spawn_shell (void); +bool sys_compare_uid (struct stat *a, struct stat *b); +bool sys_compare_gid (struct stat *a, struct stat *b); +bool sys_file_is_archive (struct tar_stat_info *p); +bool sys_compare_links (struct stat *link_data, struct stat *stat_data); +int sys_truncate (int fd); +pid_t sys_child_open_for_compress (void); +pid_t sys_child_open_for_uncompress (void); +size_t sys_write_archive_buffer (void); +bool sys_get_archive_stat (void); +int sys_exec_command (char *file_name, int typechar, struct tar_stat_info *st); +void sys_wait_command (void); +int sys_exec_info_script (const char **archive_name, int volume_number); +void sys_exec_checkpoint_script (const char *script_name, + const char *archive_name, + int checkpoint_number); + +/* Module compare.c */ +void report_difference (struct tar_stat_info *st, const char *message, ...); + +/* Module sparse.c */ +bool sparse_member_p (struct tar_stat_info *st); +bool sparse_fixup_header (struct tar_stat_info *st); +enum dump_status sparse_dump_file (int, struct tar_stat_info *st); +enum dump_status sparse_extract_file (int fd, struct tar_stat_info *st, + off_t *size); +enum dump_status sparse_skip_file (struct tar_stat_info *st); +bool sparse_diff_file (int, struct tar_stat_info *st); + +/* Module utf8.c */ +bool string_ascii_p (const char *str); +bool utf8_convert (bool to_utf, char const *input, char **output); + +/* Module transform.c */ +#define XFORM_REGFILE 0x01 +#define XFORM_LINK 0x02 +#define XFORM_SYMLINK 0x04 +#define XFORM_ALL (XFORM_REGFILE|XFORM_LINK|XFORM_SYMLINK) + +void set_transform_expr (const char *expr); +bool transform_name (char **pinput, int type); +bool transform_name_fp (char **pinput, int type, + char *(*fun)(char *, void *), void *); +bool transform_program_p (void); + +/* Module suffix.c */ +void set_compression_program_by_suffix (const char *name, const char *defprog); + +/* Module checkpoint.c */ +void checkpoint_compile_action (const char *str); +void checkpoint_finish_compile (void); +void checkpoint_run (bool do_write); + +/* Module warning.c */ +#define WARN_ALONE_ZERO_BLOCK 0x00000001 +#define WARN_BAD_DUMPDIR 0x00000002 +#define WARN_CACHEDIR 0x00000004 +#define WARN_CONTIGUOUS_CAST 0x00000008 +#define WARN_FILE_CHANGED 0x00000010 +#define WARN_FILE_IGNORED 0x00000020 +#define WARN_FILE_REMOVED 0x00000040 +#define WARN_FILE_SHRANK 0x00000080 +#define WARN_FILE_UNCHANGED 0x00000100 +#define WARN_FILENAME_WITH_NULS 0x00000200 +#define WARN_IGNORE_ARCHIVE 0x00000400 +#define WARN_IGNORE_NEWER 0x00000800 +#define WARN_NEW_DIRECTORY 0x00001000 +#define WARN_RENAME_DIRECTORY 0x00002000 +#define WARN_SYMLINK_CAST 0x00004000 +#define WARN_TIMESTAMP 0x00008000 +#define WARN_UNKNOWN_CAST 0x00010000 +#define WARN_UNKNOWN_KEYWORD 0x00020000 +#define WARN_XDEV 0x00040000 +#define WARN_DECOMPRESS_PROGRAM 0x00080000 + +/* The warnings composing WARN_VERBOSE_WARNINGS are enabled by default + in verbose mode */ +#define WARN_VERBOSE_WARNINGS (WARN_RENAME_DIRECTORY|WARN_NEW_DIRECTORY|\ + WARN_DECOMPRESS_PROGRAM) +#define WARN_ALL (~WARN_VERBOSE_WARNINGS) + +void set_warning_option (const char *arg); + +extern int warning_option; + +#define WARNOPT(opt,args) \ + do \ + { \ + if (warning_option & opt) WARN (args); \ + } \ + while (0) + +/* Module unlink.c */ + +void queue_deferred_unlink (const char *name, bool is_dir); +void finish_deferred_unlinks (void); + +/* Module exit.c */ +extern void (*fatal_exit_hook) (void); diff -Naur tar-1.26.orig//src/create.c tar-1.26/src/create.c --- tar-1.26.orig//src/create.c 2011-03-12 04:09:33.000000000 -0500 +++ tar-1.26/src/create.c 2011-09-06 13:22:06.000000000 -0400 @@ -24,6 +24,7 @@ #include #include "common.h" + #include /* Error number to use when an impostor is discovered. @@ -936,6 +937,30 @@ GNAME_TO_CHARS (st->gname, header->header.gname); } + if (archive_format == POSIX_FORMAT) + { + if (acls_option > 0) + { + if (st->acls_a_ptr) + xheader_store ("SCHILY.acl.access", st, NULL); + if (st->acls_d_ptr) + xheader_store ("SCHILY.acl.default", st, NULL); + } + if ((selinux_context_option > 0) && st->cntx_name) + xheader_store ("RHT.security.selinux", st, NULL); + if (xattrs_option > 0) + { + size_t scan_xattr = 0; + struct xattr_array *xattr_map = st->xattr_map; + + while (scan_xattr < st->xattr_map_size) + { + xheader_store (xattr_map[scan_xattr].xkey, st, &scan_xattr); + ++scan_xattr; + } + } + } + return header; } @@ -1711,6 +1736,10 @@ bool ok; struct stat final_stat; + xattrs_acls_get(st, p, fd, !is_dir); + xattrs_selinux_get(st, p, fd); + xattrs_xattrs_get(st, p, fd); + if (is_dir) { const char *tag_file_name; @@ -1829,6 +1858,9 @@ if (NAME_FIELD_SIZE - (archive_format == OLDGNU_FORMAT) < size) write_long_link (st); + xattrs_selinux_get(st, p, -1); + xattrs_xattrs_get(st, p, -1); + block_ordinal = current_block_ordinal (); st->stat.st_size = 0; /* force 0 size on symlink */ header = start_header (st); @@ -1847,11 +1879,23 @@ } #endif else if (S_ISCHR (st->stat.st_mode)) - type = CHRTYPE; + { + type = CHRTYPE; + xattrs_selinux_get(st, p, -1); + xattrs_xattrs_get(st, p, -1); + } else if (S_ISBLK (st->stat.st_mode)) - type = BLKTYPE; + { + type = BLKTYPE; + xattrs_selinux_get(st, p, -1); + xattrs_xattrs_get(st, p, -1); + } else if (S_ISFIFO (st->stat.st_mode)) - type = FIFOTYPE; + { + type = FIFOTYPE; + xattrs_selinux_get(st, p, -1); + xattrs_xattrs_get(st, p, -1); + } else if (S_ISSOCK (st->stat.st_mode)) { WARNOPT (WARN_FILE_IGNORED, diff -Naur tar-1.26.orig//src/create.c.orig tar-1.26/src/create.c.orig --- tar-1.26.orig//src/create.c.orig 1969-12-31 19:00:00.000000000 -0500 +++ tar-1.26/src/create.c.orig 2011-03-12 04:09:33.000000000 -0500 @@ -0,0 +1,1915 @@ +/* Create a tar archive. + + Copyright (C) 1985, 1992, 1993, 1994, 1996, 1997, 1999, 2000, 2001, + 2003, 2004, 2005, 2006, 2007, 2009, 2010 Free Software Foundation, Inc. + + Written by John Gilmore, on 1985-08-25. + + This program is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by the + Free Software Foundation; either version 3, or (at your option) any later + version. + + This program is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General + Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ + +#include + +#include + +#include "common.h" +#include + +/* Error number to use when an impostor is discovered. + Pretend the impostor isn't there. */ +enum { IMPOSTOR_ERRNO = ENOENT }; + +struct link + { + dev_t dev; + ino_t ino; + nlink_t nlink; + char name[1]; + }; + +struct exclusion_tag +{ + const char *name; + size_t length; + enum exclusion_tag_type type; + bool (*predicate) (int fd); + struct exclusion_tag *next; +}; + +static struct exclusion_tag *exclusion_tags; + +void +add_exclusion_tag (const char *name, enum exclusion_tag_type type, + bool (*predicate) (int fd)) +{ + struct exclusion_tag *tag = xmalloc (sizeof tag[0]); + tag->next = exclusion_tags; + tag->name = name; + tag->type = type; + tag->predicate = predicate; + tag->length = strlen (name); + exclusion_tags = tag; +} + +void +exclusion_tag_warning (const char *dirname, const char *tagname, + const char *message) +{ + if (verbose_option) + WARNOPT (WARN_CACHEDIR, + (0, 0, + _("%s: contains a cache directory tag %s; %s"), + quotearg_colon (dirname), + quotearg_n (1, tagname), + message)); +} + +enum exclusion_tag_type +check_exclusion_tags (struct tar_stat_info const *st, char const **tag_file_name) +{ + struct exclusion_tag *tag; + + for (tag = exclusion_tags; tag; tag = tag->next) + { + int tagfd = subfile_open (st, tag->name, open_read_flags); + if (0 <= tagfd) + { + bool satisfied = !tag->predicate || tag->predicate (tagfd); + close (tagfd); + if (satisfied) + { + if (tag_file_name) + *tag_file_name = tag->name; + return tag->type; + } + } + } + + return exclusion_tag_none; +} + +/* Exclusion predicate to test if the named file (usually "CACHEDIR.TAG") + contains a valid header, as described at: + http://www.brynosaurus.com/cachedir + Applications can write this file into directories they create + for use as caches containing purely regenerable, non-precious data, + allowing us to avoid archiving them if --exclude-caches is specified. */ + +#define CACHEDIR_SIGNATURE "Signature: 8a477f597d28d172789f06886806bc55" +#define CACHEDIR_SIGNATURE_SIZE (sizeof CACHEDIR_SIGNATURE - 1) + +bool +cachedir_file_p (int fd) +{ + char tagbuf[CACHEDIR_SIGNATURE_SIZE]; + + return + (read (fd, tagbuf, CACHEDIR_SIGNATURE_SIZE) == CACHEDIR_SIGNATURE_SIZE + && memcmp (tagbuf, CACHEDIR_SIGNATURE, CACHEDIR_SIGNATURE_SIZE) == 0); +} + + +/* The maximum uintmax_t value that can be represented with DIGITS digits, + assuming that each digit is BITS_PER_DIGIT wide. */ +#define MAX_VAL_WITH_DIGITS(digits, bits_per_digit) \ + ((digits) * (bits_per_digit) < sizeof (uintmax_t) * CHAR_BIT \ + ? ((uintmax_t) 1 << ((digits) * (bits_per_digit))) - 1 \ + : (uintmax_t) -1) + +/* The maximum uintmax_t value that can be represented with octal + digits and a trailing NUL in BUFFER. */ +#define MAX_OCTAL_VAL(buffer) MAX_VAL_WITH_DIGITS (sizeof (buffer) - 1, LG_8) + +/* Convert VALUE to an octal representation suitable for tar headers. + Output to buffer WHERE with size SIZE. + The result is undefined if SIZE is 0 or if VALUE is too large to fit. */ + +static void +to_octal (uintmax_t value, char *where, size_t size) +{ + uintmax_t v = value; + size_t i = size; + + do + { + where[--i] = '0' + (v & ((1 << LG_8) - 1)); + v >>= LG_8; + } + while (i); +} + +/* Copy at most LEN bytes from the string SRC to DST. Terminate with + NUL unless SRC is LEN or more bytes long. */ + +static void +tar_copy_str (char *dst, const char *src, size_t len) +{ + size_t i; + for (i = 0; i < len; i++) + if (! (dst[i] = src[i])) + break; +} + +/* Same as tar_copy_str, but always terminate with NUL if using + is OLDGNU format */ + +static void +tar_name_copy_str (char *dst, const char *src, size_t len) +{ + tar_copy_str (dst, src, len); + if (archive_format == OLDGNU_FORMAT) + dst[len-1] = 0; +} + +/* Convert NEGATIVE VALUE to a base-256 representation suitable for + tar headers. NEGATIVE is 1 if VALUE was negative before being cast + to uintmax_t, 0 otherwise. Output to buffer WHERE with size SIZE. + The result is undefined if SIZE is 0 or if VALUE is too large to + fit. */ + +static void +to_base256 (int negative, uintmax_t value, char *where, size_t size) +{ + uintmax_t v = value; + uintmax_t propagated_sign_bits = + ((uintmax_t) - negative << (CHAR_BIT * sizeof v - LG_256)); + size_t i = size; + + do + { + where[--i] = v & ((1 << LG_256) - 1); + v = propagated_sign_bits | (v >> LG_256); + } + while (i); +} + +#define GID_TO_CHARS(val, where) gid_to_chars (val, where, sizeof (where)) +#define MAJOR_TO_CHARS(val, where) major_to_chars (val, where, sizeof (where)) +#define MINOR_TO_CHARS(val, where) minor_to_chars (val, where, sizeof (where)) +#define MODE_TO_CHARS(val, where) mode_to_chars (val, where, sizeof (where)) +#define UID_TO_CHARS(val, where) uid_to_chars (val, where, sizeof (where)) + +#define UNAME_TO_CHARS(name,buf) string_to_chars (name, buf, sizeof(buf)) +#define GNAME_TO_CHARS(name,buf) string_to_chars (name, buf, sizeof(buf)) + +static bool +to_chars (int negative, uintmax_t value, size_t valsize, + uintmax_t (*substitute) (int *), + char *where, size_t size, const char *type); + +static bool +to_chars_subst (int negative, int gnu_format, uintmax_t value, size_t valsize, + uintmax_t (*substitute) (int *), + char *where, size_t size, const char *type) +{ + uintmax_t maxval = (gnu_format + ? MAX_VAL_WITH_DIGITS (size - 1, LG_256) + : MAX_VAL_WITH_DIGITS (size - 1, LG_8)); + char valbuf[UINTMAX_STRSIZE_BOUND + 1]; + char maxbuf[UINTMAX_STRSIZE_BOUND]; + char minbuf[UINTMAX_STRSIZE_BOUND + 1]; + char const *minval_string; + char const *maxval_string = STRINGIFY_BIGINT (maxval, maxbuf); + char const *value_string; + + if (gnu_format) + { + uintmax_t m = maxval + 1 ? maxval + 1 : maxval / 2 + 1; + char *p = STRINGIFY_BIGINT (m, minbuf + 1); + *--p = '-'; + minval_string = p; + } + else + minval_string = "0"; + + if (negative) + { + char *p = STRINGIFY_BIGINT (- value, valbuf + 1); + *--p = '-'; + value_string = p; + } + else + value_string = STRINGIFY_BIGINT (value, valbuf); + + if (substitute) + { + int negsub; + uintmax_t sub = substitute (&negsub) & maxval; + /* NOTE: This is one of the few places where GNU_FORMAT differs from + OLDGNU_FORMAT. The actual differences are: + + 1. In OLDGNU_FORMAT all strings in a tar header end in \0 + 2. Incremental archives use oldgnu_header. + + Apart from this they are completely identical. */ + uintmax_t s = (negsub &= archive_format == GNU_FORMAT) ? - sub : sub; + char subbuf[UINTMAX_STRSIZE_BOUND + 1]; + char *sub_string = STRINGIFY_BIGINT (s, subbuf + 1); + if (negsub) + *--sub_string = '-'; + WARN ((0, 0, _("value %s out of %s range %s..%s; substituting %s"), + value_string, type, minval_string, maxval_string, + sub_string)); + return to_chars (negsub, s, valsize, 0, where, size, type); + } + else + ERROR ((0, 0, _("value %s out of %s range %s..%s"), + value_string, type, minval_string, maxval_string)); + return false; +} + +/* Convert NEGATIVE VALUE (which was originally of size VALSIZE) to + external form, using SUBSTITUTE (...) if VALUE won't fit. Output + to buffer WHERE with size SIZE. NEGATIVE is 1 iff VALUE was + negative before being cast to uintmax_t; its original bitpattern + can be deduced from VALSIZE, its original size before casting. + TYPE is the kind of value being output (useful for diagnostics). + Prefer the POSIX format of SIZE - 1 octal digits (with leading zero + digits), followed by '\0'. If this won't work, and if GNU or + OLDGNU format is allowed, use '\200' followed by base-256, or (if + NEGATIVE is nonzero) '\377' followed by two's complement base-256. + If neither format works, use SUBSTITUTE (...) instead. Pass to + SUBSTITUTE the address of an 0-or-1 flag recording whether the + substitute value is negative. */ + +static bool +to_chars (int negative, uintmax_t value, size_t valsize, + uintmax_t (*substitute) (int *), + char *where, size_t size, const char *type) +{ + int gnu_format = (archive_format == GNU_FORMAT + || archive_format == OLDGNU_FORMAT); + + /* Generate the POSIX octal representation if the number fits. */ + if (! negative && value <= MAX_VAL_WITH_DIGITS (size - 1, LG_8)) + { + where[size - 1] = '\0'; + to_octal (value, where, size - 1); + return true; + } + else if (gnu_format) + { + /* Try to cope with the number by using traditional GNU format + methods */ + + /* Generate the base-256 representation if the number fits. */ + if (((negative ? -1 - value : value) + <= MAX_VAL_WITH_DIGITS (size - 1, LG_256))) + { + where[0] = negative ? -1 : 1 << (LG_256 - 1); + to_base256 (negative, value, where + 1, size - 1); + return true; + } + + /* Otherwise, if the number is negative, and if it would not cause + ambiguity on this host by confusing positive with negative + values, then generate the POSIX octal representation of the value + modulo 2**(field bits). The resulting tar file is + machine-dependent, since it depends on the host word size. Yuck! + But this is the traditional behavior. */ + else if (negative && valsize * CHAR_BIT <= (size - 1) * LG_8) + { + static int warned_once; + if (! warned_once) + { + warned_once = 1; + WARN ((0, 0, _("Generating negative octal headers"))); + } + where[size - 1] = '\0'; + to_octal (value & MAX_VAL_WITH_DIGITS (valsize * CHAR_BIT, 1), + where, size - 1); + return true; + } + /* Otherwise fall back to substitution, if possible: */ + } + else + substitute = NULL; /* No substitution for formats, other than GNU */ + + return to_chars_subst (negative, gnu_format, value, valsize, substitute, + where, size, type); +} + +static uintmax_t +gid_substitute (int *negative) +{ + gid_t r; +#ifdef GID_NOBODY + r = GID_NOBODY; +#else + static gid_t gid_nobody; + if (!gid_nobody && !gname_to_gid ("nobody", &gid_nobody)) + gid_nobody = -2; + r = gid_nobody; +#endif + *negative = r < 0; + return r; +} + +static bool +gid_to_chars (gid_t v, char *p, size_t s) +{ + return to_chars (v < 0, (uintmax_t) v, sizeof v, gid_substitute, p, s, "gid_t"); +} + +static bool +major_to_chars (major_t v, char *p, size_t s) +{ + return to_chars (v < 0, (uintmax_t) v, sizeof v, 0, p, s, "major_t"); +} + +static bool +minor_to_chars (minor_t v, char *p, size_t s) +{ + return to_chars (v < 0, (uintmax_t) v, sizeof v, 0, p, s, "minor_t"); +} + +static bool +mode_to_chars (mode_t v, char *p, size_t s) +{ + /* In the common case where the internal and external mode bits are the same, + and we are not using POSIX or GNU format, + propagate all unknown bits to the external mode. + This matches historical practice. + Otherwise, just copy the bits we know about. */ + int negative; + uintmax_t u; + if (S_ISUID == TSUID && S_ISGID == TSGID && S_ISVTX == TSVTX + && S_IRUSR == TUREAD && S_IWUSR == TUWRITE && S_IXUSR == TUEXEC + && S_IRGRP == TGREAD && S_IWGRP == TGWRITE && S_IXGRP == TGEXEC + && S_IROTH == TOREAD && S_IWOTH == TOWRITE && S_IXOTH == TOEXEC + && archive_format != POSIX_FORMAT + && archive_format != USTAR_FORMAT + && archive_format != GNU_FORMAT) + { + negative = v < 0; + u = v; + } + else + { + negative = 0; + u = ((v & S_ISUID ? TSUID : 0) + | (v & S_ISGID ? TSGID : 0) + | (v & S_ISVTX ? TSVTX : 0) + | (v & S_IRUSR ? TUREAD : 0) + | (v & S_IWUSR ? TUWRITE : 0) + | (v & S_IXUSR ? TUEXEC : 0) + | (v & S_IRGRP ? TGREAD : 0) + | (v & S_IWGRP ? TGWRITE : 0) + | (v & S_IXGRP ? TGEXEC : 0) + | (v & S_IROTH ? TOREAD : 0) + | (v & S_IWOTH ? TOWRITE : 0) + | (v & S_IXOTH ? TOEXEC : 0)); + } + return to_chars (negative, u, sizeof v, 0, p, s, "mode_t"); +} + +bool +off_to_chars (off_t v, char *p, size_t s) +{ + return to_chars (v < 0, (uintmax_t) v, sizeof v, 0, p, s, "off_t"); +} + +bool +time_to_chars (time_t v, char *p, size_t s) +{ + return to_chars (v < 0, (uintmax_t) v, sizeof v, 0, p, s, "time_t"); +} + +static uintmax_t +uid_substitute (int *negative) +{ + uid_t r; +#ifdef UID_NOBODY + r = UID_NOBODY; +#else + static uid_t uid_nobody; + if (!uid_nobody && !uname_to_uid ("nobody", &uid_nobody)) + uid_nobody = -2; + r = uid_nobody; +#endif + *negative = r < 0; + return r; +} + +static bool +uid_to_chars (uid_t v, char *p, size_t s) +{ + return to_chars (v < 0, (uintmax_t) v, sizeof v, uid_substitute, p, s, "uid_t"); +} + +static bool +uintmax_to_chars (uintmax_t v, char *p, size_t s) +{ + return to_chars (0, v, sizeof v, 0, p, s, "uintmax_t"); +} + +static void +string_to_chars (char const *str, char *p, size_t s) +{ + tar_copy_str (p, str, s); + p[s - 1] = '\0'; +} + + +/* A directory is always considered dumpable. + Otherwise, only regular and contiguous files are considered dumpable. + Such a file is dumpable if it is sparse and both --sparse and --totals + are specified. + Otherwise, it is dumpable unless any of the following conditions occur: + + a) it is empty *and* world-readable, or + b) current archive is /dev/null */ + +static bool +file_dumpable_p (struct stat const *st) +{ + if (S_ISDIR (st->st_mode)) + return true; + if (! (S_ISREG (st->st_mode) || S_ISCTG (st->st_mode))) + return false; + if (dev_null_output) + return totals_option && sparse_option && ST_IS_SPARSE (*st); + return ! (st->st_size == 0 && (st->st_mode & MODE_R) == MODE_R); +} + + +/* Writing routines. */ + +/* Write the EOT block(s). Zero at least two blocks, through the end + of the record. Old tar, as previous versions of GNU tar, writes + garbage after two zeroed blocks. */ +void +write_eot (void) +{ + union block *pointer = find_next_block (); + memset (pointer->buffer, 0, BLOCKSIZE); + set_next_block_after (pointer); + pointer = find_next_block (); + memset (pointer->buffer, 0, available_space_after (pointer)); + set_next_block_after (pointer); +} + +/* Write a "private" header */ +union block * +start_private_header (const char *name, size_t size, time_t t) +{ + union block *header = find_next_block (); + + memset (header->buffer, 0, sizeof (union block)); + + tar_name_copy_str (header->header.name, name, NAME_FIELD_SIZE); + OFF_TO_CHARS (size, header->header.size); + + TIME_TO_CHARS (t, header->header.mtime); + MODE_TO_CHARS (S_IFREG|S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH, header->header.mode); + UID_TO_CHARS (getuid (), header->header.uid); + GID_TO_CHARS (getgid (), header->header.gid); + MAJOR_TO_CHARS (0, header->header.devmajor); + MINOR_TO_CHARS (0, header->header.devminor); + strncpy (header->header.magic, TMAGIC, TMAGLEN); + strncpy (header->header.version, TVERSION, TVERSLEN); + return header; +} + +/* Create a new header and store there at most NAME_FIELD_SIZE bytes of + the file name */ + +static union block * +write_short_name (struct tar_stat_info *st) +{ + union block *header = find_next_block (); + memset (header->buffer, 0, sizeof (union block)); + tar_name_copy_str (header->header.name, st->file_name, NAME_FIELD_SIZE); + return header; +} + +#define FILL(field,byte) do { \ + memset(field, byte, sizeof(field)-1); \ + (field)[sizeof(field)-1] = 0; \ +} while (0) + +/* Write a GNUTYPE_LONGLINK or GNUTYPE_LONGNAME block. */ +static void +write_gnu_long_link (struct tar_stat_info *st, const char *p, char type) +{ + size_t size = strlen (p) + 1; + size_t bufsize; + union block *header; + char *tmpname; + + header = start_private_header ("././@LongLink", size, time (NULL)); + FILL (header->header.mtime, '0'); + FILL (header->header.mode, '0'); + FILL (header->header.uid, '0'); + FILL (header->header.gid, '0'); + FILL (header->header.devmajor, 0); + FILL (header->header.devminor, 0); + uid_to_uname (0, &tmpname); + UNAME_TO_CHARS (tmpname, header->header.uname); + free (tmpname); + gid_to_gname (0, &tmpname); + GNAME_TO_CHARS (tmpname, header->header.gname); + free (tmpname); + + strcpy (header->buffer + offsetof (struct posix_header, magic), + OLDGNU_MAGIC); + header->header.typeflag = type; + finish_header (st, header, -1); + + header = find_next_block (); + + bufsize = available_space_after (header); + + while (bufsize < size) + { + memcpy (header->buffer, p, bufsize); + p += bufsize; + size -= bufsize; + set_next_block_after (header + (bufsize - 1) / BLOCKSIZE); + header = find_next_block (); + bufsize = available_space_after (header); + } + memcpy (header->buffer, p, size); + memset (header->buffer + size, 0, bufsize - size); + set_next_block_after (header + (size - 1) / BLOCKSIZE); +} + +static size_t +split_long_name (const char *name, size_t length) +{ + size_t i; + + if (length > PREFIX_FIELD_SIZE + 1) + length = PREFIX_FIELD_SIZE + 1; + else if (ISSLASH (name[length - 1])) + length--; + for (i = length - 1; i > 0; i--) + if (ISSLASH (name[i])) + break; + return i; +} + +static union block * +write_ustar_long_name (const char *name) +{ + size_t length = strlen (name); + size_t i, nlen; + union block *header; + + if (length > PREFIX_FIELD_SIZE + NAME_FIELD_SIZE + 1) + { + ERROR ((0, 0, _("%s: file name is too long (max %d); not dumped"), + quotearg_colon (name), + PREFIX_FIELD_SIZE + NAME_FIELD_SIZE + 1)); + return NULL; + } + + i = split_long_name (name, length); + if (i == 0 || (nlen = length - i - 1) > NAME_FIELD_SIZE || nlen == 0) + { + ERROR ((0, 0, + _("%s: file name is too long (cannot be split); not dumped"), + quotearg_colon (name))); + return NULL; + } + + header = find_next_block (); + memset (header->buffer, 0, sizeof (header->buffer)); + memcpy (header->header.prefix, name, i); + memcpy (header->header.name, name + i + 1, length - i - 1); + + return header; +} + +/* Write a long link name, depending on the current archive format */ +static void +write_long_link (struct tar_stat_info *st) +{ + switch (archive_format) + { + case POSIX_FORMAT: + xheader_store ("linkpath", st, NULL); + break; + + case V7_FORMAT: /* old V7 tar format */ + case USTAR_FORMAT: + case STAR_FORMAT: + ERROR ((0, 0, + _("%s: link name is too long; not dumped"), + quotearg_colon (st->link_name))); + break; + + case OLDGNU_FORMAT: + case GNU_FORMAT: + write_gnu_long_link (st, st->link_name, GNUTYPE_LONGLINK); + break; + + default: + abort(); /*FIXME*/ + } +} + +static union block * +write_long_name (struct tar_stat_info *st) +{ + switch (archive_format) + { + case POSIX_FORMAT: + xheader_store ("path", st, NULL); + break; + + case V7_FORMAT: + if (strlen (st->file_name) > NAME_FIELD_SIZE-1) + { + ERROR ((0, 0, _("%s: file name is too long (max %d); not dumped"), + quotearg_colon (st->file_name), + NAME_FIELD_SIZE - 1)); + return NULL; + } + break; + + case USTAR_FORMAT: + case STAR_FORMAT: + return write_ustar_long_name (st->file_name); + + case OLDGNU_FORMAT: + case GNU_FORMAT: + write_gnu_long_link (st, st->file_name, GNUTYPE_LONGNAME); + break; + + default: + abort(); /*FIXME*/ + } + return write_short_name (st); +} + +union block * +write_extended (bool global, struct tar_stat_info *st, union block *old_header) +{ + union block *header, hp; + char *p; + int type; + time_t t; + + if (st->xhdr.buffer || st->xhdr.stk == NULL) + return old_header; + + xheader_finish (&st->xhdr); + memcpy (hp.buffer, old_header, sizeof (hp)); + if (global) + { + type = XGLTYPE; + p = xheader_ghdr_name (); + time (&t); + } + else + { + type = XHDTYPE; + p = xheader_xhdr_name (st); + t = st->stat.st_mtime; + } + xheader_write (type, p, t, &st->xhdr); + free (p); + header = find_next_block (); + memcpy (header, &hp.buffer, sizeof (hp.buffer)); + return header; +} + +static union block * +write_header_name (struct tar_stat_info *st) +{ + if (archive_format == POSIX_FORMAT && !string_ascii_p (st->file_name)) + { + xheader_store ("path", st, NULL); + return write_short_name (st); + } + else if (NAME_FIELD_SIZE - (archive_format == OLDGNU_FORMAT) + < strlen (st->file_name)) + return write_long_name (st); + else + return write_short_name (st); +} + + +/* Header handling. */ + +/* Make a header block for the file whose stat info is st, + and return its address. */ + +union block * +start_header (struct tar_stat_info *st) +{ + union block *header; + + header = write_header_name (st); + if (!header) + return NULL; + + /* Override some stat fields, if requested to do so. */ + + if (owner_option != (uid_t) -1) + st->stat.st_uid = owner_option; + if (group_option != (gid_t) -1) + st->stat.st_gid = group_option; + if (mode_option) + st->stat.st_mode = + ((st->stat.st_mode & ~MODE_ALL) + | mode_adjust (st->stat.st_mode, S_ISDIR (st->stat.st_mode) != 0, + initial_umask, mode_option, NULL)); + + /* Paul Eggert tried the trivial test ($WRITER cf a b; $READER tvf a) + for a few tars and came up with the following interoperability + matrix: + + WRITER + 1 2 3 4 5 6 7 8 9 READER + . . . . . . . . . 1 = SunOS 4.2 tar + # . . # # . . # # 2 = NEC SVR4.0.2 tar + . . . # # . . # . 3 = Solaris 2.1 tar + . . . . . . . . . 4 = GNU tar 1.11.1 + . . . . . . . . . 5 = HP-UX 8.07 tar + . . . . . . . . . 6 = Ultrix 4.1 + . . . . . . . . . 7 = AIX 3.2 + . . . . . . . . . 8 = Hitachi HI-UX 1.03 + . . . . . . . . . 9 = Omron UNIOS-B 4.3BSD 1.60Beta + + . = works + # = ``impossible file type'' + + The following mask for old archive removes the `#'s in column 4 + above, thus making GNU tar both a universal donor and a universal + acceptor for Paul's test. */ + + if (archive_format == V7_FORMAT || archive_format == USTAR_FORMAT) + MODE_TO_CHARS (st->stat.st_mode & MODE_ALL, header->header.mode); + else + MODE_TO_CHARS (st->stat.st_mode, header->header.mode); + + { + uid_t uid = st->stat.st_uid; + if (archive_format == POSIX_FORMAT + && MAX_OCTAL_VAL (header->header.uid) < uid) + { + xheader_store ("uid", st, NULL); + uid = 0; + } + if (!UID_TO_CHARS (uid, header->header.uid)) + return NULL; + } + + { + gid_t gid = st->stat.st_gid; + if (archive_format == POSIX_FORMAT + && MAX_OCTAL_VAL (header->header.gid) < gid) + { + xheader_store ("gid", st, NULL); + gid = 0; + } + if (!GID_TO_CHARS (gid, header->header.gid)) + return NULL; + } + + { + off_t size = st->stat.st_size; + if (archive_format == POSIX_FORMAT + && MAX_OCTAL_VAL (header->header.size) < size) + { + xheader_store ("size", st, NULL); + size = 0; + } + if (!OFF_TO_CHARS (size, header->header.size)) + return NULL; + } + + { + struct timespec mtime = set_mtime_option ? mtime_option : st->mtime; + if (archive_format == POSIX_FORMAT) + { + if (MAX_OCTAL_VAL (header->header.mtime) < mtime.tv_sec + || mtime.tv_nsec != 0) + xheader_store ("mtime", st, &mtime); + if (MAX_OCTAL_VAL (header->header.mtime) < mtime.tv_sec) + mtime.tv_sec = 0; + } + if (!TIME_TO_CHARS (mtime.tv_sec, header->header.mtime)) + return NULL; + } + + /* FIXME */ + if (S_ISCHR (st->stat.st_mode) + || S_ISBLK (st->stat.st_mode)) + { + major_t devmajor = major (st->stat.st_rdev); + minor_t devminor = minor (st->stat.st_rdev); + + if (archive_format == POSIX_FORMAT + && MAX_OCTAL_VAL (header->header.devmajor) < devmajor) + { + xheader_store ("devmajor", st, NULL); + devmajor = 0; + } + if (!MAJOR_TO_CHARS (devmajor, header->header.devmajor)) + return NULL; + + if (archive_format == POSIX_FORMAT + && MAX_OCTAL_VAL (header->header.devminor) < devminor) + { + xheader_store ("devminor", st, NULL); + devminor = 0; + } + if (!MINOR_TO_CHARS (devminor, header->header.devminor)) + return NULL; + } + else if (archive_format != GNU_FORMAT && archive_format != OLDGNU_FORMAT) + { + if (!(MAJOR_TO_CHARS (0, header->header.devmajor) + && MINOR_TO_CHARS (0, header->header.devminor))) + return NULL; + } + + if (archive_format == POSIX_FORMAT) + { + xheader_store ("atime", st, NULL); + xheader_store ("ctime", st, NULL); + } + else if (incremental_option) + if (archive_format == OLDGNU_FORMAT || archive_format == GNU_FORMAT) + { + TIME_TO_CHARS (st->atime.tv_sec, header->oldgnu_header.atime); + TIME_TO_CHARS (st->ctime.tv_sec, header->oldgnu_header.ctime); + } + + header->header.typeflag = archive_format == V7_FORMAT ? AREGTYPE : REGTYPE; + + switch (archive_format) + { + case V7_FORMAT: + break; + + case OLDGNU_FORMAT: + case GNU_FORMAT: /*FIXME?*/ + /* Overwrite header->header.magic and header.version in one blow. */ + strcpy (header->buffer + offsetof (struct posix_header, magic), + OLDGNU_MAGIC); + break; + + case POSIX_FORMAT: + case USTAR_FORMAT: + strncpy (header->header.magic, TMAGIC, TMAGLEN); + strncpy (header->header.version, TVERSION, TVERSLEN); + break; + + default: + abort (); + } + + if (archive_format == V7_FORMAT || numeric_owner_option) + { + /* header->header.[ug]name are left as the empty string. */ + } + else + { + uid_to_uname (st->stat.st_uid, &st->uname); + gid_to_gname (st->stat.st_gid, &st->gname); + + if (archive_format == POSIX_FORMAT + && (strlen (st->uname) > UNAME_FIELD_SIZE + || !string_ascii_p (st->uname))) + xheader_store ("uname", st, NULL); + UNAME_TO_CHARS (st->uname, header->header.uname); + + if (archive_format == POSIX_FORMAT + && (strlen (st->gname) > GNAME_FIELD_SIZE + || !string_ascii_p (st->gname))) + xheader_store ("gname", st, NULL); + GNAME_TO_CHARS (st->gname, header->header.gname); + } + + return header; +} + +void +simple_finish_header (union block *header) +{ + size_t i; + int sum; + char *p; + + memcpy (header->header.chksum, CHKBLANKS, sizeof header->header.chksum); + + sum = 0; + p = header->buffer; + for (i = sizeof *header; i-- != 0; ) + /* We can't use unsigned char here because of old compilers, e.g. V7. */ + sum += 0xFF & *p++; + + /* Fill in the checksum field. It's formatted differently from the + other fields: it has [6] digits, a null, then a space -- rather than + digits, then a null. We use to_chars. + The final space is already there, from + checksumming, and to_chars doesn't modify it. + + This is a fast way to do: + + sprintf(header->header.chksum, "%6o", sum); */ + + uintmax_to_chars ((uintmax_t) sum, header->header.chksum, 7); + + set_next_block_after (header); +} + +/* Finish off a filled-in header block and write it out. We also + print the file name and/or full info if verbose is on. If BLOCK_ORDINAL + is not negative, is the block ordinal of the first record for this + file, which may be a preceding long name or long link record. */ +void +finish_header (struct tar_stat_info *st, + union block *header, off_t block_ordinal) +{ + /* Note: It is important to do this before the call to write_extended(), + so that the actual ustar header is printed */ + if (verbose_option + && header->header.typeflag != GNUTYPE_LONGLINK + && header->header.typeflag != GNUTYPE_LONGNAME + && header->header.typeflag != XHDTYPE + && header->header.typeflag != XGLTYPE) + { + /* FIXME: This global is used in print_header, sigh. */ + current_format = archive_format; + print_header (st, header, block_ordinal); + } + + header = write_extended (false, st, header); + simple_finish_header (header); +} + + +void +pad_archive (off_t size_left) +{ + union block *blk; + while (size_left > 0) + { + blk = find_next_block (); + memset (blk->buffer, 0, BLOCKSIZE); + set_next_block_after (blk); + size_left -= BLOCKSIZE; + } +} + +static enum dump_status +dump_regular_file (int fd, struct tar_stat_info *st) +{ + off_t size_left = st->stat.st_size; + off_t block_ordinal; + union block *blk; + + block_ordinal = current_block_ordinal (); + blk = start_header (st); + if (!blk) + return dump_status_fail; + + /* Mark contiguous files, if we support them. */ + if (archive_format != V7_FORMAT && S_ISCTG (st->stat.st_mode)) + blk->header.typeflag = CONTTYPE; + + finish_header (st, blk, block_ordinal); + + mv_begin_write (st->file_name, st->stat.st_size, st->stat.st_size); + while (size_left > 0) + { + size_t bufsize, count; + + blk = find_next_block (); + + bufsize = available_space_after (blk); + + if (size_left < bufsize) + { + /* Last read -- zero out area beyond. */ + bufsize = size_left; + count = bufsize % BLOCKSIZE; + if (count) + memset (blk->buffer + size_left, 0, BLOCKSIZE - count); + } + + count = (fd <= 0) ? bufsize : safe_read (fd, blk->buffer, bufsize); + if (count == SAFE_READ_ERROR) + { + read_diag_details (st->orig_file_name, + st->stat.st_size - size_left, bufsize); + pad_archive (size_left); + return dump_status_short; + } + size_left -= count; + set_next_block_after (blk + (bufsize - 1) / BLOCKSIZE); + + if (count != bufsize) + { + char buf[UINTMAX_STRSIZE_BOUND]; + memset (blk->buffer + count, 0, bufsize - count); + WARNOPT (WARN_FILE_SHRANK, + (0, 0, + ngettext ("%s: File shrank by %s byte; padding with zeros", + "%s: File shrank by %s bytes; padding with zeros", + size_left), + quotearg_colon (st->orig_file_name), + STRINGIFY_BIGINT (size_left, buf))); + if (! ignore_failed_read_option) + set_exit_status (TAREXIT_DIFFERS); + pad_archive (size_left - (bufsize - count)); + return dump_status_short; + } + } + return dump_status_ok; +} + + +/* Copy info from the directory identified by ST into the archive. + DIRECTORY contains the directory's entries. */ + +static void +dump_dir0 (struct tar_stat_info *st, char const *directory) +{ + bool top_level = ! st->parent; + const char *tag_file_name; + union block *blk = NULL; + off_t block_ordinal = current_block_ordinal (); + + st->stat.st_size = 0; /* force 0 size on dir */ + + blk = start_header (st); + if (!blk) + return; + + if (incremental_option && archive_format != POSIX_FORMAT) + blk->header.typeflag = GNUTYPE_DUMPDIR; + else /* if (standard_option) */ + blk->header.typeflag = DIRTYPE; + + /* If we're gnudumping, we aren't done yet so don't close it. */ + + if (!incremental_option) + finish_header (st, blk, block_ordinal); + else if (gnu_list_name->directory) + { + if (archive_format == POSIX_FORMAT) + { + xheader_store ("GNU.dumpdir", st, + safe_directory_contents (gnu_list_name->directory)); + finish_header (st, blk, block_ordinal); + } + else + { + off_t size_left; + off_t totsize; + size_t bufsize; + ssize_t count; + const char *buffer, *p_buffer; + + block_ordinal = current_block_ordinal (); + buffer = safe_directory_contents (gnu_list_name->directory); + totsize = dumpdir_size (buffer); + OFF_TO_CHARS (totsize, blk->header.size); + finish_header (st, blk, block_ordinal); + p_buffer = buffer; + size_left = totsize; + + mv_begin_write (st->file_name, totsize, totsize); + while (size_left > 0) + { + blk = find_next_block (); + bufsize = available_space_after (blk); + if (size_left < bufsize) + { + bufsize = size_left; + count = bufsize % BLOCKSIZE; + if (count) + memset (blk->buffer + size_left, 0, BLOCKSIZE - count); + } + memcpy (blk->buffer, p_buffer, bufsize); + size_left -= bufsize; + p_buffer += bufsize; + set_next_block_after (blk + (bufsize - 1) / BLOCKSIZE); + } + } + return; + } + + if (!recursion_option) + return; + + if (one_file_system_option + && !top_level + && st->parent->stat.st_dev != st->stat.st_dev) + { + if (verbose_option) + WARNOPT (WARN_XDEV, + (0, 0, + _("%s: file is on a different filesystem; not dumped"), + quotearg_colon (st->orig_file_name))); + } + else + { + char *name_buf; + size_t name_size; + + switch (check_exclusion_tags (st, &tag_file_name)) + { + case exclusion_tag_all: + /* Handled in dump_file0 */ + break; + + case exclusion_tag_none: + { + char const *entry; + size_t entry_len; + size_t name_len; + + name_buf = xstrdup (st->orig_file_name); + name_size = name_len = strlen (name_buf); + + /* Now output all the files in the directory. */ + for (entry = directory; (entry_len = strlen (entry)) != 0; + entry += entry_len + 1) + { + if (name_size < name_len + entry_len) + { + name_size = name_len + entry_len; + name_buf = xrealloc (name_buf, name_size + 1); + } + strcpy (name_buf + name_len, entry); + if (!excluded_name (name_buf)) + dump_file (st, entry, name_buf); + } + + free (name_buf); + } + break; + + case exclusion_tag_contents: + exclusion_tag_warning (st->orig_file_name, tag_file_name, + _("contents not dumped")); + name_size = strlen (st->orig_file_name) + strlen (tag_file_name) + 1; + name_buf = xmalloc (name_size); + strcpy (name_buf, st->orig_file_name); + strcat (name_buf, tag_file_name); + dump_file (st, tag_file_name, name_buf); + free (name_buf); + break; + + case exclusion_tag_under: + exclusion_tag_warning (st->orig_file_name, tag_file_name, + _("contents not dumped")); + break; + } + } +} + +/* Ensure exactly one trailing slash. */ +static void +ensure_slash (char **pstr) +{ + size_t len = strlen (*pstr); + while (len >= 1 && ISSLASH ((*pstr)[len - 1])) + len--; + if (!ISSLASH ((*pstr)[len])) + *pstr = xrealloc (*pstr, len + 2); + (*pstr)[len++] = '/'; + (*pstr)[len] = '\0'; +} + +/* If we just ran out of file descriptors, release a file descriptor + in the directory chain somewhere leading from DIR->parent->parent + up through the root. Return true if successful, false (preserving + errno == EMFILE) otherwise. + + Do not release DIR's file descriptor, or DIR's parent, as other + code assumes that they work. On some operating systems, another + process can claim file descriptor resources as we release them, and + some calls or their emulations require multiple file descriptors, + so callers should not give up if a single release doesn't work. */ + +static bool +open_failure_recover (struct tar_stat_info const *dir) +{ + if (errno == EMFILE && dir && dir->parent) + { + struct tar_stat_info *p; + for (p = dir->parent->parent; p; p = p->parent) + if (0 < p->fd && (! p->parent || p->parent->fd <= 0)) + { + tar_stat_close (p); + return true; + } + errno = EMFILE; + } + + return false; +} + +/* Return the directory entries of ST, in a dynamically allocated buffer, + each entry followed by '\0' and the last followed by an extra '\0'. + Return null on failure, setting errno. */ +char * +get_directory_entries (struct tar_stat_info *st) +{ + while (! (st->dirstream = fdopendir (st->fd))) + if (! open_failure_recover (st)) + return 0; + return streamsavedir (st->dirstream); +} + +/* Dump the directory ST. Return true if successful, false (emitting + diagnostics) otherwise. Get ST's entries, recurse through its + subdirectories, and clean up file descriptors afterwards. */ +static bool +dump_dir (struct tar_stat_info *st) +{ + char *directory = get_directory_entries (st); + if (! directory) + { + savedir_diag (st->orig_file_name); + return false; + } + + dump_dir0 (st, directory); + + restore_parent_fd (st); + free (directory); + return true; +} + + +/* Number of links a file can have without having to be entered into + the link table. Typically this is 1, but in trickier circumstances + it is 0. */ +static nlink_t trivial_link_count; + + +/* Main functions of this module. */ + +void +create_archive (void) +{ + struct name const *p; + + trivial_link_count = name_count <= 1 && ! dereference_option; + + open_archive (ACCESS_WRITE); + buffer_write_global_xheader (); + + if (incremental_option) + { + size_t buffer_size = 1000; + char *buffer = xmalloc (buffer_size); + const char *q; + + collect_and_sort_names (); + + while ((p = name_from_list ()) != NULL) + if (!excluded_name (p->name)) + dump_file (0, p->name, p->name); + + blank_name_list (); + while ((p = name_from_list ()) != NULL) + if (!excluded_name (p->name)) + { + struct tar_stat_info st; + size_t plen = strlen (p->name); + if (buffer_size <= plen) + { + while ((buffer_size *= 2) <= plen) + continue; + buffer = xrealloc (buffer, buffer_size); + } + memcpy (buffer, p->name, plen); + if (! ISSLASH (buffer[plen - 1])) + buffer[plen++] = DIRECTORY_SEPARATOR; + tar_stat_init (&st); + q = directory_contents (gnu_list_name->directory); + if (q) + while (*q) + { + size_t qlen = strlen (q); + if (*q == 'Y') + { + if (! st.orig_file_name) + { + int fd = openat (chdir_fd, p->name, + open_searchdir_flags); + if (fd < 0) + { + open_diag (p->name); + break; + } + st.fd = fd; + if (fstat (fd, &st.stat) != 0) + { + stat_diag (p->name); + break; + } + st.orig_file_name = xstrdup (p->name); + } + if (buffer_size < plen + qlen) + { + while ((buffer_size *=2 ) < plen + qlen) + continue; + buffer = xrealloc (buffer, buffer_size); + } + strcpy (buffer + plen, q + 1); + dump_file (&st, q + 1, buffer); + } + q += qlen + 1; + } + tar_stat_destroy (&st); + } + free (buffer); + } + else + { + const char *name; + while ((name = name_next (1)) != NULL) + if (!excluded_name (name)) + dump_file (0, name, name); + } + + write_eot (); + close_archive (); + finish_deferred_unlinks (); + if (listed_incremental_option) + write_directory_file (); +} + + +/* Calculate the hash of a link. */ +static size_t +hash_link (void const *entry, size_t n_buckets) +{ + struct link const *l = entry; + uintmax_t num = l->dev ^ l->ino; + return num % n_buckets; +} + +/* Compare two links for equality. */ +static bool +compare_links (void const *entry1, void const *entry2) +{ + struct link const *link1 = entry1; + struct link const *link2 = entry2; + return ((link1->dev ^ link2->dev) | (link1->ino ^ link2->ino)) == 0; +} + +static void +unknown_file_error (char const *p) +{ + WARNOPT (WARN_FILE_IGNORED, + (0, 0, _("%s: Unknown file type; file ignored"), + quotearg_colon (p))); + if (!ignore_failed_read_option) + set_exit_status (TAREXIT_FAILURE); +} + + +/* Handling of hard links */ + +/* Table of all non-directories that we've written so far. Any time + we see another, we check the table and avoid dumping the data + again if we've done it once already. */ +static Hash_table *link_table; + +/* Try to dump stat as a hard link to another file in the archive. + Return true if successful. */ +static bool +dump_hard_link (struct tar_stat_info *st) +{ + if (link_table + && (trivial_link_count < st->stat.st_nlink || remove_files_option)) + { + struct link lp; + struct link *duplicate; + off_t block_ordinal; + union block *blk; + + lp.ino = st->stat.st_ino; + lp.dev = st->stat.st_dev; + + if ((duplicate = hash_lookup (link_table, &lp))) + { + /* We found a link. */ + char const *link_name = safer_name_suffix (duplicate->name, true, + absolute_names_option); + + duplicate->nlink--; + + block_ordinal = current_block_ordinal (); + assign_string (&st->link_name, link_name); + if (NAME_FIELD_SIZE - (archive_format == OLDGNU_FORMAT) + < strlen (link_name)) + write_long_link (st); + + st->stat.st_size = 0; + blk = start_header (st); + if (!blk) + return false; + tar_copy_str (blk->header.linkname, link_name, NAME_FIELD_SIZE); + + blk->header.typeflag = LNKTYPE; + finish_header (st, blk, block_ordinal); + + if (remove_files_option) + queue_deferred_unlink (st->orig_file_name, false); + + return true; + } + } + return false; +} + +static void +file_count_links (struct tar_stat_info *st) +{ + if (hard_dereference_option) + return; + if (trivial_link_count < st->stat.st_nlink) + { + struct link *duplicate; + char *linkname = NULL; + struct link *lp; + + assign_string (&linkname, st->orig_file_name); + transform_name (&linkname, XFORM_LINK); + + lp = xmalloc (offsetof (struct link, name) + + strlen (linkname) + 1); + lp->ino = st->stat.st_ino; + lp->dev = st->stat.st_dev; + lp->nlink = st->stat.st_nlink; + strcpy (lp->name, linkname); + free (linkname); + + if (! ((link_table + || (link_table = hash_initialize (0, 0, hash_link, + compare_links, 0))) + && (duplicate = hash_insert (link_table, lp)))) + xalloc_die (); + + if (duplicate != lp) + abort (); + lp->nlink--; + } +} + +/* For each dumped file, check if all its links were dumped. Emit + warnings if it is not so. */ +void +check_links (void) +{ + struct link *lp; + + if (!link_table) + return; + + for (lp = hash_get_first (link_table); lp; + lp = hash_get_next (link_table, lp)) + { + if (lp->nlink) + { + WARN ((0, 0, _("Missing links to %s."), quote (lp->name))); + } + } +} + +/* Assuming DIR is the working directory, open FILE, using FLAGS to + control the open. A null DIR means to use ".". If we are low on + file descriptors, try to release one or more from DIR's parents to + reuse it. */ +int +subfile_open (struct tar_stat_info const *dir, char const *file, int flags) +{ + int fd; + + static bool initialized; + if (! initialized) + { + /* Initialize any tables that might be needed when file + descriptors are exhausted, and whose initialization might + require a file descriptor. This includes the system message + catalog and tar's message catalog. */ + initialized = true; + strerror (ENOENT); + gettext (""); + } + + while ((fd = openat (dir ? dir->fd : chdir_fd, file, flags)) < 0 + && open_failure_recover (dir)) + continue; + return fd; +} + +/* Restore the file descriptor for ST->parent, if it was temporarily + closed to conserve file descriptors. On failure, set the file + descriptor to the negative of the corresponding errno value. Call + this every time a subdirectory is ascended from. */ +void +restore_parent_fd (struct tar_stat_info const *st) +{ + struct tar_stat_info *parent = st->parent; + if (parent && ! parent->fd) + { + int parentfd = openat (st->fd, "..", open_searchdir_flags); + struct stat parentstat; + + if (parentfd < 0) + parentfd = - errno; + else if (! (fstat (parentfd, &parentstat) == 0 + && parent->stat.st_ino == parentstat.st_ino + && parent->stat.st_dev == parentstat.st_dev)) + { + close (parentfd); + parentfd = IMPOSTOR_ERRNO; + } + + if (parentfd < 0) + { + int origfd = openat (chdir_fd, parent->orig_file_name, + open_searchdir_flags); + if (0 <= origfd) + { + if (fstat (parentfd, &parentstat) == 0 + && parent->stat.st_ino == parentstat.st_ino + && parent->stat.st_dev == parentstat.st_dev) + parentfd = origfd; + else + close (origfd); + } + } + + parent->fd = parentfd; + } +} + +/* Dump a single file, recursing on directories. ST is the file's + status info, NAME its name relative to the parent directory, and P + its full name (which may be relative to the working directory). */ + +/* FIXME: One should make sure that for *every* path leading to setting + exit_status to failure, a clear diagnostic has been issued. */ + +static void +dump_file0 (struct tar_stat_info *st, char const *name, char const *p) +{ + union block *header; + char type; + off_t original_size; + struct timespec original_ctime; + off_t block_ordinal = -1; + int fd = 0; + bool is_dir; + struct tar_stat_info const *parent = st->parent; + bool top_level = ! parent; + int parentfd = top_level ? chdir_fd : parent->fd; + void (*diag) (char const *) = 0; + + if (interactive_option && !confirm ("add", p)) + return; + + assign_string (&st->orig_file_name, p); + assign_string (&st->file_name, + safer_name_suffix (p, false, absolute_names_option)); + + transform_name (&st->file_name, XFORM_REGFILE); + + if (parentfd < 0 && ! top_level) + { + errno = - parentfd; + diag = open_diag; + } + else if (fstatat (parentfd, name, &st->stat, fstatat_flags) != 0) + diag = stat_diag; + else if (file_dumpable_p (&st->stat)) + { + fd = subfile_open (parent, name, open_read_flags); + if (fd < 0) + diag = open_diag; + else + { + st->fd = fd; + if (fstat (fd, &st->stat) != 0) + diag = stat_diag; + } + } + if (diag) + { + file_removed_diag (p, top_level, diag); + return; + } + + st->archive_file_size = original_size = st->stat.st_size; + st->atime = get_stat_atime (&st->stat); + st->mtime = get_stat_mtime (&st->stat); + st->ctime = original_ctime = get_stat_ctime (&st->stat); + +#ifdef S_ISHIDDEN + if (S_ISHIDDEN (st->stat.st_mode)) + { + char *new = (char *) alloca (strlen (p) + 2); + if (new) + { + strcpy (new, p); + strcat (new, "@"); + p = new; + } + } +#endif + + /* See if we want only new files, and check if this one is too old to + put in the archive. + + This check is omitted if incremental_option is set *and* the + requested file is not explicitly listed in the command line. */ + + if (! (incremental_option && ! top_level) + && !S_ISDIR (st->stat.st_mode) + && OLDER_TAR_STAT_TIME (*st, m) + && (!after_date_option || OLDER_TAR_STAT_TIME (*st, c))) + { + if (!incremental_option && verbose_option) + WARNOPT (WARN_FILE_UNCHANGED, + (0, 0, _("%s: file is unchanged; not dumped"), + quotearg_colon (p))); + return; + } + + /* See if we are trying to dump the archive. */ + if (sys_file_is_archive (st)) + { + WARNOPT (WARN_IGNORE_ARCHIVE, + (0, 0, _("%s: file is the archive; not dumped"), + quotearg_colon (p))); + return; + } + + is_dir = S_ISDIR (st->stat.st_mode) != 0; + + if (!is_dir && dump_hard_link (st)) + return; + + if (is_dir || S_ISREG (st->stat.st_mode) || S_ISCTG (st->stat.st_mode)) + { + bool ok; + struct stat final_stat; + + if (is_dir) + { + const char *tag_file_name; + ensure_slash (&st->orig_file_name); + ensure_slash (&st->file_name); + + if (check_exclusion_tags (st, &tag_file_name) == exclusion_tag_all) + { + exclusion_tag_warning (st->orig_file_name, tag_file_name, + _("directory not dumped")); + return; + } + + ok = dump_dir (st); + + fd = st->fd; + parentfd = top_level ? chdir_fd : parent->fd; + } + else + { + enum dump_status status; + + if (fd && sparse_option && ST_IS_SPARSE (st->stat)) + { + status = sparse_dump_file (fd, st); + if (status == dump_status_not_implemented) + status = dump_regular_file (fd, st); + } + else + status = dump_regular_file (fd, st); + + switch (status) + { + case dump_status_ok: + case dump_status_short: + file_count_links (st); + break; + + case dump_status_fail: + break; + + case dump_status_not_implemented: + abort (); + } + + ok = status == dump_status_ok; + } + + if (ok) + { + if (fd < 0) + { + errno = - fd; + ok = false; + } + else if (fd == 0) + { + if (parentfd < 0 && ! top_level) + { + errno = - parentfd; + ok = false; + } + else + ok = fstatat (parentfd, name, &final_stat, fstatat_flags) == 0; + } + else + ok = fstat (fd, &final_stat) == 0; + + if (! ok) + file_removed_diag (p, top_level, stat_diag); + } + + if (ok) + { + if ((timespec_cmp (get_stat_ctime (&final_stat), original_ctime) != 0 + /* Original ctime will change if the file is a directory and + --remove-files is given */ + && !(remove_files_option && is_dir)) + || original_size < final_stat.st_size) + { + WARNOPT (WARN_FILE_CHANGED, + (0, 0, _("%s: file changed as we read it"), + quotearg_colon (p))); + set_exit_status (TAREXIT_DIFFERS); + } + else if (atime_preserve_option == replace_atime_preserve + && fd && (is_dir || original_size != 0) + && set_file_atime (fd, parentfd, name, st->atime) != 0) + utime_error (p); + } + + ok &= tar_stat_close (st); + if (ok && remove_files_option) + queue_deferred_unlink (p, is_dir); + + return; + } +#ifdef HAVE_READLINK + else if (S_ISLNK (st->stat.st_mode)) + { + char *buffer; + int size; + size_t linklen = st->stat.st_size; + if (linklen != st->stat.st_size || linklen + 1 == 0) + xalloc_die (); + buffer = (char *) alloca (linklen + 1); + size = readlinkat (parentfd, name, buffer, linklen + 1); + if (size < 0) + { + file_removed_diag (p, top_level, readlink_diag); + return; + } + buffer[size] = '\0'; + assign_string (&st->link_name, buffer); + transform_name (&st->link_name, XFORM_SYMLINK); + if (NAME_FIELD_SIZE - (archive_format == OLDGNU_FORMAT) < size) + write_long_link (st); + + block_ordinal = current_block_ordinal (); + st->stat.st_size = 0; /* force 0 size on symlink */ + header = start_header (st); + if (!header) + return; + tar_copy_str (header->header.linkname, st->link_name, NAME_FIELD_SIZE); + header->header.typeflag = SYMTYPE; + finish_header (st, header, block_ordinal); + /* nothing more to do to it */ + + if (remove_files_option) + queue_deferred_unlink (p, false); + + file_count_links (st); + return; + } +#endif + else if (S_ISCHR (st->stat.st_mode)) + type = CHRTYPE; + else if (S_ISBLK (st->stat.st_mode)) + type = BLKTYPE; + else if (S_ISFIFO (st->stat.st_mode)) + type = FIFOTYPE; + else if (S_ISSOCK (st->stat.st_mode)) + { + WARNOPT (WARN_FILE_IGNORED, + (0, 0, _("%s: socket ignored"), quotearg_colon (p))); + return; + } + else if (S_ISDOOR (st->stat.st_mode)) + { + WARNOPT (WARN_FILE_IGNORED, + (0, 0, _("%s: door ignored"), quotearg_colon (p))); + return; + } + else + { + unknown_file_error (p); + return; + } + + if (archive_format == V7_FORMAT) + { + unknown_file_error (p); + return; + } + + block_ordinal = current_block_ordinal (); + st->stat.st_size = 0; /* force 0 size */ + header = start_header (st); + if (!header) + return; + header->header.typeflag = type; + + if (type != FIFOTYPE) + { + MAJOR_TO_CHARS (major (st->stat.st_rdev), + header->header.devmajor); + MINOR_TO_CHARS (minor (st->stat.st_rdev), + header->header.devminor); + } + + finish_header (st, header, block_ordinal); + if (remove_files_option) + queue_deferred_unlink (p, false); +} + +/* Dump a file, recursively. PARENT describes the file's parent + directory, NAME is the file's name relative to PARENT, and FULLNAME + its full name, possibly relative to the working directory. NAME + may contain slashes at the top level of invocation. */ + +void +dump_file (struct tar_stat_info *parent, char const *name, + char const *fullname) +{ + struct tar_stat_info st; + tar_stat_init (&st); + st.parent = parent; + dump_file0 (&st, name, fullname); + if (parent && listed_incremental_option) + update_parent_directory (parent); + tar_stat_destroy (&st); +} diff -Naur tar-1.26.orig//src/extract.c tar-1.26/src/extract.c --- tar-1.26.orig//src/extract.c 2010-11-27 05:33:22.000000000 -0500 +++ tar-1.26/src/extract.c 2011-09-06 13:22:06.000000000 -0400 @@ -97,6 +97,14 @@ /* Directory that the name is relative to. */ int change_dir; + /* extended attributes*/ + char *cntx_name; + char *acls_a_ptr; + size_t acls_a_len; + char *acls_d_ptr; + size_t acls_d_len; + size_t xattr_map_size; /* Size of the xattr map */ + struct xattr_array *xattr_map; /* Length and contents of name. */ size_t file_name_len; char file_name[1]; @@ -134,6 +142,18 @@ hard-linked together. */ struct string_list *sources; + /* SELinux context */ + char *cntx_name; + + /* ACLs */ + char *acls_a_ptr; + size_t acls_a_len; + char *acls_d_ptr; + size_t acls_d_len; + + size_t xattr_map_size; /* Size of the xattr map */ + struct xattr_array *xattr_map; + /* The desired target of the desired link. */ char target[1]; }; @@ -335,6 +355,10 @@ utime_error (file_name); } + xattrs_acls_set(st, file_name, typeflag); + xattrs_selinux_set(st, file_name, typeflag); + xattrs_xattrs_set(st, file_name, typeflag); + if (0 < same_owner_option && ! interdir) { /* Some systems allow non-root users to give files away. Once this @@ -431,6 +455,36 @@ data->atflag = atflag; data->after_links = 0; data->change_dir = chdir_current; + data->cntx_name = NULL; + if (st) + assign_string (&data->cntx_name, st->cntx_name); + if (st && st->acls_a_ptr) + { + data->acls_a_ptr = xmemdup(st->acls_a_ptr, st->acls_a_len + 1); + data->acls_a_len = st->acls_a_len; + } + else + { + data->acls_a_ptr = NULL; + data->acls_a_len = 0; + } + if (st && st->acls_d_ptr) + { + data->acls_d_ptr = xmemdup(st->acls_d_ptr, st->acls_d_len + 1); + data->acls_d_len = st->acls_d_len; + } + else + { + data->acls_d_ptr = NULL; + data->acls_d_len = 0; + } + if (st) + xheader_xattr_copy (st, &data->xattr_map, &data->xattr_map_size); + else + { + data->xattr_map = NULL; + data->xattr_map_size = 0; + } strcpy (data->file_name, file_name); delayed_set_stat_head = data; if (must_be_dot_or_slash (file_name)) @@ -673,6 +727,31 @@ return RECOVER_NO; } +/* Restore stat extended attributes (xattr) for FILE_NAME, using information + given in *ST. Restore before extraction because they may affect layout. + If not restoring permissions, invert the + INVERT_PERMISSIONS bits from the file's current permissions. + TYPEFLAG specifies the type of the file. + FILE_CREATED indicates set_xattr has created the file */ +static int +set_xattr (char const *file_name, struct tar_stat_info const *st, + mode_t invert_permissions, char typeflag, int *file_created) +{ + int status = 0; + bool interdir_made = false; + + if ((xattrs_option >= 0) && st->xattr_map_size) { + mode_t mode = current_stat_info.stat.st_mode & MODE_RWX & ~ current_umask; + + do + status = mknod (file_name, mode ^ invert_permissions, 0); + while (status && maybe_recoverable ((char *)file_name, false, &interdir_made)); + xattrs_xattrs_set(st, file_name, typeflag); + *file_created = 1; + } + return(status); +} + /* Fix the statuses of all directories whose statuses need fixing, and which are not ancestors of FILE_NAME. If AFTER_LINKS is nonzero, do this for all such directories; otherwise, stop at the @@ -733,12 +812,23 @@ sb.stat.st_gid = data->gid; sb.atime = data->atime; sb.mtime = data->mtime; + sb.cntx_name = data->cntx_name; + sb.acls_a_ptr = data->acls_a_ptr; + sb.acls_a_len = data->acls_a_len; + sb.acls_d_ptr = data->acls_d_ptr; + sb.acls_d_len = data->acls_d_len; + sb.xattr_map = data->xattr_map; + sb.xattr_map_size = data->xattr_map_size; set_stat (data->file_name, &sb, -1, current_mode, current_mode_mask, DIRTYPE, data->interdir, data->atflag); } delayed_set_stat_head = data->next; + xheader_xattr_free (data->xattr_map, data->xattr_map_size); + free (data->cntx_name); + free (data->acls_a_ptr); + free (data->acls_d_ptr); free (data); } } @@ -854,6 +944,7 @@ static int open_output_file (char const *file_name, int typeflag, mode_t mode, + int file_created, mode_t *current_mode, mode_t *current_mode_mask) { int fd; @@ -864,6 +955,10 @@ ? O_TRUNC | (dereference_option ? 0 : O_NOFOLLOW) : O_EXCL)); + /* File might be created in set_xattr. So clear O_EXCL to avoid open() failure */ + if (file_created) + openflag = openflag & ~O_EXCL; + if (typeflag == CONTTYPE) { static int conttype_diagnosed; @@ -934,6 +1029,7 @@ bool interdir_made = false; mode_t mode = (current_stat_info.stat.st_mode & MODE_RWX & ~ (0 < same_owner_option ? S_IRWXG | S_IRWXO : 0)); + mode_t invert_permissions = 0 < same_owner_option ? mode & (S_IRWXG | S_IRWXO) : 0; mode_t current_mode = 0; mode_t current_mode_mask = 0; @@ -950,7 +1046,17 @@ } else { + int file_created = 0; + if (set_xattr (file_name, ¤t_stat_info, invert_permissions, + typeflag, &file_created)) + { + skip_member (); + open_error (file_name); + return 1; + } + while ((fd = open_output_file (file_name, typeflag, mode, + file_created, ¤t_mode, ¤t_mode_mask)) < 0) { @@ -1091,6 +1197,13 @@ + strlen (file_name) + 1); p->sources->next = 0; strcpy (p->sources->string, file_name); + p->cntx_name = NULL; + assign_string (&p->cntx_name, current_stat_info.cntx_name); + p->acls_a_ptr = NULL; + p->acls_a_len = 0; + p->acls_d_ptr = NULL; + p->acls_d_len = 0; + xheader_xattr_copy (¤t_stat_info, &p->xattr_map, &p->xattr_map_size); strcpy (p->target, current_stat_info.link_name); h = delayed_set_stat_head; @@ -1525,6 +1638,13 @@ st1.stat.st_gid = ds->gid; st1.atime = ds->atime; st1.mtime = ds->mtime; + st1.cntx_name = ds->cntx_name; + st1.acls_a_ptr = ds->acls_a_ptr; + st1.acls_a_len = ds->acls_a_len; + st1.acls_d_ptr = ds->acls_d_ptr; + st1.acls_d_len = ds->acls_d_len; + st1.xattr_map = ds->xattr_map; + st1.xattr_map_size = ds->xattr_map_size; set_stat (source, &st1, -1, 0, 0, SYMTYPE, false, AT_SYMLINK_NOFOLLOW); valid_source = source; @@ -1539,6 +1659,9 @@ sources = next; } + xheader_xattr_free (ds->xattr_map, ds->xattr_map_size); + free (ds->cntx_name); + { struct delayed_link *next = ds->next; free (ds); diff -Naur tar-1.26.orig//src/extract.c.orig tar-1.26/src/extract.c.orig --- tar-1.26.orig//src/extract.c.orig 1969-12-31 19:00:00.000000000 -0500 +++ tar-1.26/src/extract.c.orig 2010-11-27 05:33:22.000000000 -0500 @@ -0,0 +1,1600 @@ +/* Extract files from a tar archive. + + Copyright (C) 1988, 1992, 1993, 1994, 1996, 1997, 1998, 1999, 2000, + 2001, 2003, 2004, 2005, 2006, 2007, 2010 Free Software Foundation, Inc. + + Written by John Gilmore, on 1985-11-19. + + This program is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by the + Free Software Foundation; either version 3, or (at your option) any later + version. + + This program is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General + Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ + +#include +#include +#include +#include +#include + +#include "common.h" + +static bool we_are_root; /* true if our effective uid == 0 */ +static mode_t newdir_umask; /* umask when creating new directories */ +static mode_t current_umask; /* current umask (which is set to 0 if -p) */ + +#define ALL_MODE_BITS ((mode_t) ~ (mode_t) 0) + +#if ! HAVE_FCHMOD && ! defined fchmod +# define fchmod(fd, mode) (errno = ENOSYS, -1) +#endif +#if ! HAVE_FCHOWN && ! defined fchown +# define fchown(fd, uid, gid) (errno = ENOSYS, -1) +#endif + +/* Return true if an error number ERR means the system call is + supported in this case. */ +static bool +implemented (int err) +{ + return ! (err == ENOSYS + || err == ENOTSUP + || (EOPNOTSUPP != ENOTSUP && err == EOPNOTSUPP)); +} + +/* List of directories whose statuses we need to extract after we've + finished extracting their subsidiary files. If you consider each + contiguous subsequence of elements of the form [D]?[^D]*, where [D] + represents an element where AFTER_LINKS is nonzero and [^D] + represents an element where AFTER_LINKS is zero, then the head + of the subsequence has the longest name, and each non-head element + in the prefix is an ancestor (in the directory hierarchy) of the + preceding element. */ + +struct delayed_set_stat + { + /* Next directory in list. */ + struct delayed_set_stat *next; + + /* Metadata for this directory. */ + dev_t dev; + ino_t ino; + mode_t mode; /* The desired mode is MODE & ~ current_umask. */ + uid_t uid; + gid_t gid; + struct timespec atime; + struct timespec mtime; + + /* An estimate of the directory's current mode, along with a mask + specifying which bits of this estimate are known to be correct. + If CURRENT_MODE_MASK is zero, CURRENT_MODE's value doesn't + matter. */ + mode_t current_mode; + mode_t current_mode_mask; + + /* This directory is an intermediate directory that was created + as an ancestor of some other directory; it was not mentioned + in the archive, so do not set its uid, gid, atime, or mtime, + and don't alter its mode outside of MODE_RWX. */ + bool interdir; + + /* Whether symbolic links should be followed when accessing the + directory. */ + int atflag; + + /* Do not set the status of this directory until after delayed + links are created. */ + bool after_links; + + /* Directory that the name is relative to. */ + int change_dir; + + /* Length and contents of name. */ + size_t file_name_len; + char file_name[1]; + }; + +static struct delayed_set_stat *delayed_set_stat_head; + +/* List of links whose creation we have delayed. */ +struct delayed_link + { + /* The next delayed link in the list. */ + struct delayed_link *next; + + /* The device, inode number and ctime of the placeholder. Use + ctime, not mtime, to make false matches less likely if some + other process removes the placeholder. */ + dev_t dev; + ino_t ino; + struct timespec ctime; + + /* True if the link is symbolic. */ + bool is_symlink; + + /* The desired metadata, valid only the link is symbolic. */ + mode_t mode; + uid_t uid; + gid_t gid; + struct timespec atime; + struct timespec mtime; + + /* The directory that the sources and target are relative to. */ + int change_dir; + + /* A list of sources for this link. The sources are all to be + hard-linked together. */ + struct string_list *sources; + + /* The desired target of the desired link. */ + char target[1]; + }; + +static struct delayed_link *delayed_link_head; + +struct string_list + { + struct string_list *next; + char string[1]; + }; + +/* Set up to extract files. */ +void +extr_init (void) +{ + we_are_root = geteuid () == 0; + same_permissions_option += we_are_root; + same_owner_option += we_are_root; + + /* Option -p clears the kernel umask, so it does not affect proper + restoration of file permissions. New intermediate directories will + comply with umask at start of program. */ + + newdir_umask = umask (0); + if (0 < same_permissions_option) + current_umask = 0; + else + { + umask (newdir_umask); /* restore the kernel umask */ + current_umask = newdir_umask; + } +} + +/* Use fchmod if possible, fchmodat otherwise. */ +static int +fd_chmod (int fd, char const *file, mode_t mode, int atflag) +{ + if (0 <= fd) + { + int result = fchmod (fd, mode); + if (result == 0 || implemented (errno)) + return result; + } + return fchmodat (chdir_fd, file, mode, atflag); +} + +/* Use fchown if possible, fchownat otherwise. */ +static int +fd_chown (int fd, char const *file, uid_t uid, gid_t gid, int atflag) +{ + if (0 <= fd) + { + int result = fchown (fd, uid, gid); + if (result == 0 || implemented (errno)) + return result; + } + return fchownat (chdir_fd, file, uid, gid, atflag); +} + +/* Use fstat if possible, fstatat otherwise. */ +static int +fd_stat (int fd, char const *file, struct stat *st, int atflag) +{ + return (0 <= fd + ? fstat (fd, st) + : fstatat (chdir_fd, file, st, atflag)); +} + +/* Set the mode for FILE_NAME to MODE. + MODE_MASK specifies the bits of MODE that we care about; + thus if MODE_MASK is zero, do nothing. + If FD is nonnegative, it is a file descriptor for the file. + CURRENT_MODE and CURRENT_MODE_MASK specify information known about + the file's current mode, using the style of struct delayed_set_stat. + TYPEFLAG specifies the type of the file. + ATFLAG specifies the flag to use when statting the file. */ +static void +set_mode (char const *file_name, + mode_t mode, mode_t mode_mask, int fd, + mode_t current_mode, mode_t current_mode_mask, + char typeflag, int atflag) +{ + if (((current_mode ^ mode) | ~ current_mode_mask) & mode_mask) + { + if (MODE_ALL & ~ mode_mask & ~ current_mode_mask) + { + struct stat st; + if (fd_stat (fd, file_name, &st, atflag) != 0) + { + stat_error (file_name); + return; + } + current_mode = st.st_mode; + } + + current_mode &= MODE_ALL; + mode = (current_mode & ~ mode_mask) | (mode & mode_mask); + + if (current_mode != mode) + { + int chmod_errno = + fd_chmod (fd, file_name, mode, atflag) == 0 ? 0 : errno; + + /* On Solaris, chmod may fail if we don't have PRIV_ALL, because + setuid-root files would otherwise be a backdoor. See + http://opensolaris.org/jive/thread.jspa?threadID=95826 + (2009-09-03). */ + if (chmod_errno == EPERM && (mode & S_ISUID) + && priv_set_restore_linkdir () == 0) + { + chmod_errno = + fd_chmod (fd, file_name, mode, atflag) == 0 ? 0 : errno; + priv_set_remove_linkdir (); + } + + /* Linux fchmodat does not support AT_SYMLINK_NOFOLLOW, and + returns ENOTSUP even when operating on non-symlinks, try + again with the flag disabled if it does not appear to be + supported and if the file is not a symlink. This + introduces a race, alas. */ + if (atflag && typeflag != SYMTYPE && ! implemented (chmod_errno)) + chmod_errno = fd_chmod (fd, file_name, mode, 0) == 0 ? 0 : errno; + + if (chmod_errno + && (typeflag != SYMTYPE || implemented (chmod_errno))) + { + errno = chmod_errno; + chmod_error_details (file_name, mode); + } + } + } +} + +/* Check time after successfully setting FILE_NAME's time stamp to T. */ +static void +check_time (char const *file_name, struct timespec t) +{ + if (t.tv_sec <= 0) + WARNOPT (WARN_TIMESTAMP, + (0, 0, _("%s: implausibly old time stamp %s"), + file_name, tartime (t, true))); + else if (timespec_cmp (volume_start_time, t) < 0) + { + struct timespec now; + gettime (&now); + if (timespec_cmp (now, t) < 0) + { + char buf[TIMESPEC_STRSIZE_BOUND]; + struct timespec diff; + diff.tv_sec = t.tv_sec - now.tv_sec; + diff.tv_nsec = t.tv_nsec - now.tv_nsec; + if (diff.tv_nsec < 0) + { + diff.tv_nsec += BILLION; + diff.tv_sec--; + } + WARNOPT (WARN_TIMESTAMP, + (0, 0, _("%s: time stamp %s is %s s in the future"), + file_name, tartime (t, true), code_timespec (diff, buf))); + } + } +} + +/* Restore stat attributes (owner, group, mode and times) for + FILE_NAME, using information given in *ST. + If FD is nonnegative, it is a file descriptor for the file. + CURRENT_MODE and CURRENT_MODE_MASK specify information known about + the file's current mode, using the style of struct delayed_set_stat. + TYPEFLAG specifies the type of the file. + If INTERDIR, this is an intermediate directory. + ATFLAG specifies the flag to use when statting the file. */ + +static void +set_stat (char const *file_name, + struct tar_stat_info const *st, + int fd, mode_t current_mode, mode_t current_mode_mask, + char typeflag, bool interdir, int atflag) +{ + /* Do the utime before the chmod because some versions of utime are + broken and trash the modes of the file. */ + + if (! touch_option && ! interdir) + { + struct timespec ts[2]; + if (incremental_option) + ts[0] = st->atime; + else + ts[0].tv_nsec = UTIME_OMIT; + ts[1] = st->mtime; + + if (fdutimensat (fd, chdir_fd, file_name, ts, atflag) == 0) + { + if (incremental_option) + check_time (file_name, ts[0]); + check_time (file_name, ts[1]); + } + else if (typeflag != SYMTYPE || implemented (errno)) + utime_error (file_name); + } + + if (0 < same_owner_option && ! interdir) + { + /* Some systems allow non-root users to give files away. Once this + done, it is not possible anymore to change file permissions. + However, setting file permissions now would be incorrect, since + they would apply to the wrong user, and there would be a race + condition. So, don't use systems that allow non-root users to + give files away. */ + uid_t uid = st->stat.st_uid; + gid_t gid = st->stat.st_gid; + + if (fd_chown (fd, file_name, uid, gid, atflag) == 0) + { + /* Changing the owner can clear st_mode bits in some cases. */ + if ((current_mode | ~ current_mode_mask) & S_IXUGO) + current_mode_mask &= ~ (current_mode & (S_ISUID | S_ISGID)); + } + else if (typeflag != SYMTYPE || implemented (errno)) + chown_error_details (file_name, uid, gid); + } + + set_mode (file_name, + st->stat.st_mode & ~ current_umask, + 0 < same_permissions_option && ! interdir ? MODE_ALL : MODE_RWX, + fd, current_mode, current_mode_mask, typeflag, atflag); +} + +/* For each entry H in the leading prefix of entries in HEAD that do + not have after_links marked, mark H and fill in its dev and ino + members. Assume HEAD && ! HEAD->after_links. */ +static void +mark_after_links (struct delayed_set_stat *head) +{ + struct delayed_set_stat *h = head; + + do + { + struct stat st; + h->after_links = 1; + + if (deref_stat (h->file_name, &st) != 0) + stat_error (h->file_name); + else + { + h->dev = st.st_dev; + h->ino = st.st_ino; + } + } + while ((h = h->next) && ! h->after_links); +} + +/* Remember to restore stat attributes (owner, group, mode and times) + for the directory FILE_NAME, using information given in *ST, + once we stop extracting files into that directory. + + If ST is null, merely create a placeholder node for an intermediate + directory that was created by make_directories. + + NOTICE: this works only if the archive has usual member order, i.e. + directory, then the files in that directory. Incremental archive have + somewhat reversed order: first go subdirectories, then all other + members. To help cope with this case the variable + delay_directory_restore_option is set by prepare_to_extract. + + If an archive was explicitely created so that its member order is + reversed, some directory timestamps can be restored incorrectly, + e.g.: + tar --no-recursion -cf archive dir dir/file1 foo dir/file2 +*/ +static void +delay_set_stat (char const *file_name, struct tar_stat_info const *st, + mode_t current_mode, mode_t current_mode_mask, + mode_t mode, int atflag) +{ + size_t file_name_len = strlen (file_name); + struct delayed_set_stat *data = + xmalloc (offsetof (struct delayed_set_stat, file_name) + + file_name_len + 1); + data->next = delayed_set_stat_head; + data->mode = mode; + if (st) + { + data->dev = st->stat.st_dev; + data->ino = st->stat.st_ino; + data->uid = st->stat.st_uid; + data->gid = st->stat.st_gid; + data->atime = st->atime; + data->mtime = st->mtime; + } + data->file_name_len = file_name_len; + data->current_mode = current_mode; + data->current_mode_mask = current_mode_mask; + data->interdir = ! st; + data->atflag = atflag; + data->after_links = 0; + data->change_dir = chdir_current; + strcpy (data->file_name, file_name); + delayed_set_stat_head = data; + if (must_be_dot_or_slash (file_name)) + mark_after_links (data); +} + +/* Update the delayed_set_stat info for an intermediate directory + created within the file name of DIR. The intermediate directory turned + out to be the same as this directory, e.g. due to ".." or symbolic + links. *DIR_STAT_INFO is the status of the directory. */ +static void +repair_delayed_set_stat (char const *dir, + struct stat const *dir_stat_info) +{ + struct delayed_set_stat *data; + for (data = delayed_set_stat_head; data; data = data->next) + { + struct stat st; + if (fstatat (chdir_fd, data->file_name, &st, data->atflag) != 0) + { + stat_error (data->file_name); + return; + } + + if (st.st_dev == dir_stat_info->st_dev + && st.st_ino == dir_stat_info->st_ino) + { + data->dev = current_stat_info.stat.st_dev; + data->ino = current_stat_info.stat.st_ino; + data->mode = current_stat_info.stat.st_mode; + data->uid = current_stat_info.stat.st_uid; + data->gid = current_stat_info.stat.st_gid; + data->atime = current_stat_info.atime; + data->mtime = current_stat_info.mtime; + data->current_mode = st.st_mode; + data->current_mode_mask = ALL_MODE_BITS; + data->interdir = false; + return; + } + } + + ERROR ((0, 0, _("%s: Unexpected inconsistency when making directory"), + quotearg_colon (dir))); +} + +/* After a file/link/directory creation has failed, see if + it's because some required directory was not present, and if so, + create all required directories. Return zero if all the required + directories were created, nonzero (issuing a diagnostic) otherwise. + Set *INTERDIR_MADE if at least one directory was created. */ +static int +make_directories (char *file_name, bool *interdir_made) +{ + char *cursor0 = file_name + FILE_SYSTEM_PREFIX_LEN (file_name); + char *cursor; /* points into the file name */ + + for (cursor = cursor0; *cursor; cursor++) + { + mode_t mode; + mode_t desired_mode; + int status; + + if (! ISSLASH (*cursor)) + continue; + + /* Avoid mkdir of empty string, if leading or double '/'. */ + + if (cursor == cursor0 || ISSLASH (cursor[-1])) + continue; + + /* Avoid mkdir where last part of file name is "." or "..". */ + + if (cursor[-1] == '.' + && (cursor == cursor0 + 1 || ISSLASH (cursor[-2]) + || (cursor[-2] == '.' + && (cursor == cursor0 + 2 || ISSLASH (cursor[-3]))))) + continue; + + *cursor = '\0'; /* truncate the name there */ + desired_mode = MODE_RWX & ~ newdir_umask; + mode = desired_mode | (we_are_root ? 0 : MODE_WXUSR); + status = mkdirat (chdir_fd, file_name, mode); + + if (status == 0) + { + /* Create a struct delayed_set_stat even if + mode == desired_mode, because + repair_delayed_set_stat may need to update the struct. */ + delay_set_stat (file_name, + 0, mode & ~ current_umask, MODE_RWX, + desired_mode, AT_SYMLINK_NOFOLLOW); + + print_for_mkdir (file_name, cursor - file_name, desired_mode); + *interdir_made = true; + } + else if (errno == EEXIST) + status = 0; + else + { + /* Check whether the desired file exists. Even when the + file exists, mkdir can fail with some errno value E other + than EEXIST, so long as E describes an error condition + that also applies. */ + int e = errno; + struct stat st; + status = fstatat (chdir_fd, file_name, &st, 0); + if (status) + { + errno = e; + mkdir_error (file_name); + } + } + + *cursor = '/'; + if (status) + return status; + } + + return 0; +} + +/* Return true if FILE_NAME (with status *STP, if STP) is not a + directory, and has a time stamp newer than (or equal to) that of + TAR_STAT. */ +static bool +file_newer_p (const char *file_name, struct stat const *stp, + struct tar_stat_info *tar_stat) +{ + struct stat st; + + if (!stp) + { + if (deref_stat (file_name, &st) != 0) + { + if (errno != ENOENT) + { + stat_warn (file_name); + /* Be safer: if the file exists, assume it is newer. */ + return true; + } + return false; + } + stp = &st; + } + + return (! S_ISDIR (stp->st_mode) + && tar_timespec_cmp (tar_stat->mtime, get_stat_mtime (stp)) <= 0); +} + +#define RECOVER_NO 0 +#define RECOVER_OK 1 +#define RECOVER_SKIP 2 + +/* Attempt repairing what went wrong with the extraction. Delete an + already existing file or create missing intermediate directories. + Return RECOVER_OK if we somewhat increased our chances at a successful + extraction, RECOVER_NO if there are no chances, and RECOVER_SKIP if the + caller should skip extraction of that member. The value of errno is + properly restored on returning RECOVER_NO. + + If REGULAR, the caller was trying to extract onto a regular file. + + Set *INTERDIR_MADE if an intermediate directory is made as part of + the recovery process. */ + +static int +maybe_recoverable (char *file_name, bool regular, bool *interdir_made) +{ + int e = errno; + struct stat st; + struct stat const *stp = 0; + + if (*interdir_made) + return RECOVER_NO; + + switch (e) + { + case ELOOP: + + /* With open ("symlink", O_NOFOLLOW|...), POSIX says errno == ELOOP, + but some operating systems do not conform to the standard. */ +#ifdef EFTYPE + /* NetBSD uses errno == EFTYPE; see . */ + case EFTYPE: +#endif + /* FreeBSD 8.1 uses errno == EMLINK. */ + case EMLINK: + /* Tru64 5.1B uses errno == ENOTSUP. */ + case ENOTSUP: + + if (! regular + || old_files_option != OVERWRITE_OLD_FILES || dereference_option) + break; + if (strchr (file_name, '/')) + { + if (deref_stat (file_name, &st) != 0) + break; + stp = &st; + } + + /* The caller tried to open a symbolic link with O_NOFOLLOW. + Fall through, treating it as an already-existing file. */ + + case EEXIST: + /* Remove an old file, if the options allow this. */ + + switch (old_files_option) + { + case KEEP_OLD_FILES: + return RECOVER_SKIP; + + case KEEP_NEWER_FILES: + if (file_newer_p (file_name, stp, ¤t_stat_info)) + break; + /* FALL THROUGH */ + + case DEFAULT_OLD_FILES: + case NO_OVERWRITE_DIR_OLD_FILES: + case OVERWRITE_OLD_FILES: + if (0 < remove_any_file (file_name, ORDINARY_REMOVE_OPTION)) + return RECOVER_OK; + break; + + case UNLINK_FIRST_OLD_FILES: + break; + } + + case ENOENT: + /* Attempt creating missing intermediate directories. */ + if (make_directories (file_name, interdir_made) == 0 && *interdir_made) + return RECOVER_OK; + break; + + default: + /* Just say we can't do anything about it... */ + break; + } + + errno = e; + return RECOVER_NO; +} + +/* Fix the statuses of all directories whose statuses need fixing, and + which are not ancestors of FILE_NAME. If AFTER_LINKS is + nonzero, do this for all such directories; otherwise, stop at the + first directory that is marked to be fixed up only after delayed + links are applied. */ +static void +apply_nonancestor_delayed_set_stat (char const *file_name, bool after_links) +{ + size_t file_name_len = strlen (file_name); + bool check_for_renamed_directories = 0; + + while (delayed_set_stat_head) + { + struct delayed_set_stat *data = delayed_set_stat_head; + bool skip_this_one = 0; + struct stat st; + mode_t current_mode = data->current_mode; + mode_t current_mode_mask = data->current_mode_mask; + + check_for_renamed_directories |= data->after_links; + + if (after_links < data->after_links + || (data->file_name_len < file_name_len + && file_name[data->file_name_len] + && (ISSLASH (file_name[data->file_name_len]) + || ISSLASH (file_name[data->file_name_len - 1])) + && memcmp (file_name, data->file_name, data->file_name_len) == 0)) + break; + + chdir_do (data->change_dir); + + if (check_for_renamed_directories) + { + if (fstatat (chdir_fd, data->file_name, &st, data->atflag) != 0) + { + stat_error (data->file_name); + skip_this_one = 1; + } + else + { + current_mode = st.st_mode; + current_mode_mask = ALL_MODE_BITS; + if (! (st.st_dev == data->dev && st.st_ino == data->ino)) + { + ERROR ((0, 0, + _("%s: Directory renamed before its status could be extracted"), + quotearg_colon (data->file_name))); + skip_this_one = 1; + } + } + } + + if (! skip_this_one) + { + struct tar_stat_info sb; + sb.stat.st_mode = data->mode; + sb.stat.st_uid = data->uid; + sb.stat.st_gid = data->gid; + sb.atime = data->atime; + sb.mtime = data->mtime; + set_stat (data->file_name, &sb, + -1, current_mode, current_mode_mask, + DIRTYPE, data->interdir, data->atflag); + } + + delayed_set_stat_head = data->next; + free (data); + } +} + + + +/* Extractor functions for various member types */ + +static int +extract_dir (char *file_name, int typeflag) +{ + int status; + mode_t mode; + mode_t current_mode = 0; + mode_t current_mode_mask = 0; + int atflag = 0; + bool interdir_made = false; + + /* Save 'root device' to avoid purging mount points. */ + if (one_file_system_option && root_device == 0) + { + struct stat st; + + if (fstatat (chdir_fd, ".", &st, 0) != 0) + stat_diag ("."); + else + root_device = st.st_dev; + } + + if (incremental_option) + /* Read the entry and delete files that aren't listed in the archive. */ + purge_directory (file_name); + else if (typeflag == GNUTYPE_DUMPDIR) + skip_member (); + + /* If ownership or permissions will be restored later, create the + directory with restrictive permissions at first, so that in the + meantime processes owned by other users do not inadvertently + create files under this directory that inherit the wrong owner, + group, or permissions from the directory. If not root, though, + make the directory writeable and searchable at first, so that + files can be created under it. */ + mode = ((current_stat_info.stat.st_mode + & (0 < same_owner_option || 0 < same_permissions_option + ? S_IRWXU + : MODE_RWX)) + | (we_are_root ? 0 : MODE_WXUSR)); + + for (;;) + { + status = mkdirat (chdir_fd, file_name, mode); + if (status == 0) + { + current_mode = mode & ~ current_umask; + current_mode_mask = MODE_RWX; + atflag = AT_SYMLINK_NOFOLLOW; + break; + } + + if (errno == EEXIST + && (interdir_made + || old_files_option == DEFAULT_OLD_FILES + || old_files_option == OVERWRITE_OLD_FILES)) + { + struct stat st; + if (deref_stat (file_name, &st) == 0) + { + current_mode = st.st_mode; + current_mode_mask = ALL_MODE_BITS; + + if (S_ISDIR (current_mode)) + { + if (interdir_made) + { + repair_delayed_set_stat (file_name, &st); + return 0; + } + break; + } + } + errno = EEXIST; + } + + switch (maybe_recoverable (file_name, false, &interdir_made)) + { + case RECOVER_OK: + continue; + + case RECOVER_SKIP: + break; + + case RECOVER_NO: + if (errno != EEXIST) + { + mkdir_error (file_name); + return 1; + } + break; + } + break; + } + + if (status == 0 + || old_files_option == DEFAULT_OLD_FILES + || old_files_option == OVERWRITE_OLD_FILES) + delay_set_stat (file_name, ¤t_stat_info, + current_mode, current_mode_mask, + current_stat_info.stat.st_mode, atflag); + return status; +} + + + +static int +open_output_file (char const *file_name, int typeflag, mode_t mode, + mode_t *current_mode, mode_t *current_mode_mask) +{ + int fd; + bool overwriting_old_files = old_files_option == OVERWRITE_OLD_FILES; + int openflag = (O_WRONLY | O_BINARY | O_CLOEXEC | O_NOCTTY | O_NONBLOCK + | O_CREAT + | (overwriting_old_files + ? O_TRUNC | (dereference_option ? 0 : O_NOFOLLOW) + : O_EXCL)); + + if (typeflag == CONTTYPE) + { + static int conttype_diagnosed; + + if (!conttype_diagnosed) + { + conttype_diagnosed = 1; + WARNOPT (WARN_CONTIGUOUS_CAST, + (0, 0, _("Extracting contiguous files as regular files"))); + } + } + + /* If O_NOFOLLOW is needed but does not work, check for a symlink + separately. There's a race condition, but that cannot be avoided + on hosts lacking O_NOFOLLOW. */ + if (! O_NOFOLLOW && overwriting_old_files && ! dereference_option) + { + struct stat st; + if (fstatat (chdir_fd, file_name, &st, AT_SYMLINK_NOFOLLOW) == 0 + && S_ISLNK (st.st_mode)) + { + errno = ELOOP; + return -1; + } + } + + fd = openat (chdir_fd, file_name, openflag, mode); + if (0 <= fd) + { + if (overwriting_old_files) + { + struct stat st; + if (fstat (fd, &st) != 0) + { + int e = errno; + close (fd); + errno = e; + return -1; + } + if (! S_ISREG (st.st_mode)) + { + close (fd); + errno = EEXIST; + return -1; + } + *current_mode = st.st_mode; + *current_mode_mask = ALL_MODE_BITS; + } + else + { + *current_mode = mode & ~ current_umask; + *current_mode_mask = MODE_RWX; + } + } + + return fd; +} + +static int +extract_file (char *file_name, int typeflag) +{ + int fd; + off_t size; + union block *data_block; + int status; + size_t count; + size_t written; + bool interdir_made = false; + mode_t mode = (current_stat_info.stat.st_mode & MODE_RWX + & ~ (0 < same_owner_option ? S_IRWXG | S_IRWXO : 0)); + mode_t current_mode = 0; + mode_t current_mode_mask = 0; + + if (to_stdout_option) + fd = STDOUT_FILENO; + else if (to_command_option) + { + fd = sys_exec_command (file_name, 'f', ¤t_stat_info); + if (fd < 0) + { + skip_member (); + return 0; + } + } + else + { + while ((fd = open_output_file (file_name, typeflag, mode, + ¤t_mode, ¤t_mode_mask)) + < 0) + { + int recover = maybe_recoverable (file_name, true, &interdir_made); + if (recover != RECOVER_OK) + { + skip_member (); + if (recover == RECOVER_SKIP) + return 0; + open_error (file_name); + return 1; + } + } + } + + mv_begin_read (¤t_stat_info); + if (current_stat_info.is_sparse) + sparse_extract_file (fd, ¤t_stat_info, &size); + else + for (size = current_stat_info.stat.st_size; size > 0; ) + { + mv_size_left (size); + + /* Locate data, determine max length writeable, write it, + block that we have used the data, then check if the write + worked. */ + + data_block = find_next_block (); + if (! data_block) + { + ERROR ((0, 0, _("Unexpected EOF in archive"))); + break; /* FIXME: What happens, then? */ + } + + written = available_space_after (data_block); + + if (written > size) + written = size; + errno = 0; + count = full_write (fd, data_block->buffer, written); + size -= written; + + set_next_block_after ((union block *) + (data_block->buffer + written - 1)); + if (count != written) + { + if (!to_command_option) + write_error_details (file_name, count, written); + /* FIXME: shouldn't we restore from backup? */ + break; + } + } + + skip_file (size); + + mv_end (); + + /* If writing to stdout, don't try to do anything to the filename; + it doesn't exist, or we don't want to touch it anyway. */ + + if (to_stdout_option) + return 0; + + if (! to_command_option) + set_stat (file_name, ¤t_stat_info, fd, + current_mode, current_mode_mask, typeflag, false, + (old_files_option == OVERWRITE_OLD_FILES + ? 0 : AT_SYMLINK_NOFOLLOW)); + + status = close (fd); + if (status < 0) + close_error (file_name); + + if (to_command_option) + sys_wait_command (); + + return status; +} + +/* Create a placeholder file with name FILE_NAME, which will be + replaced after other extraction is done by a symbolic link if + IS_SYMLINK is true, and by a hard link otherwise. Set + *INTERDIR_MADE if an intermediate directory is made in the + process. */ + +static int +create_placeholder_file (char *file_name, bool is_symlink, bool *interdir_made) +{ + int fd; + struct stat st; + + while ((fd = openat (chdir_fd, file_name, O_WRONLY | O_CREAT | O_EXCL, 0)) < 0) + { + switch (maybe_recoverable (file_name, false, interdir_made)) + { + case RECOVER_OK: + continue; + + case RECOVER_SKIP: + return 0; + + case RECOVER_NO: + open_error (file_name); + return -1; + } + } + + if (fstat (fd, &st) != 0) + { + stat_error (file_name); + close (fd); + } + else if (close (fd) != 0) + close_error (file_name); + else + { + struct delayed_set_stat *h; + struct delayed_link *p = + xmalloc (offsetof (struct delayed_link, target) + + strlen (current_stat_info.link_name) + + 1); + p->next = delayed_link_head; + delayed_link_head = p; + p->dev = st.st_dev; + p->ino = st.st_ino; + p->ctime = get_stat_ctime (&st); + p->is_symlink = is_symlink; + if (is_symlink) + { + p->mode = current_stat_info.stat.st_mode; + p->uid = current_stat_info.stat.st_uid; + p->gid = current_stat_info.stat.st_gid; + p->atime = current_stat_info.atime; + p->mtime = current_stat_info.mtime; + } + p->change_dir = chdir_current; + p->sources = xmalloc (offsetof (struct string_list, string) + + strlen (file_name) + 1); + p->sources->next = 0; + strcpy (p->sources->string, file_name); + strcpy (p->target, current_stat_info.link_name); + + h = delayed_set_stat_head; + if (h && ! h->after_links + && strncmp (file_name, h->file_name, h->file_name_len) == 0 + && ISSLASH (file_name[h->file_name_len]) + && (last_component (file_name) == file_name + h->file_name_len + 1)) + mark_after_links (h); + + return 0; + } + + return -1; +} + +static int +extract_link (char *file_name, int typeflag) +{ + bool interdir_made = false; + char const *link_name; + int rc; + + link_name = current_stat_info.link_name; + + if (! absolute_names_option && contains_dot_dot (link_name)) + return create_placeholder_file (file_name, false, &interdir_made); + + do + { + struct stat st1, st2; + int e; + int status = linkat (chdir_fd, link_name, chdir_fd, file_name, 0); + e = errno; + + if (status == 0) + { + struct delayed_link *ds = delayed_link_head; + if (ds + && fstatat (chdir_fd, link_name, &st1, AT_SYMLINK_NOFOLLOW) == 0) + for (; ds; ds = ds->next) + if (ds->change_dir == chdir_current + && ds->dev == st1.st_dev + && ds->ino == st1.st_ino + && timespec_cmp (ds->ctime, get_stat_ctime (&st1)) == 0) + { + struct string_list *p = xmalloc (offsetof (struct string_list, string) + + strlen (file_name) + 1); + strcpy (p->string, file_name); + p->next = ds->sources; + ds->sources = p; + break; + } + return 0; + } + else if ((e == EEXIST && strcmp (link_name, file_name) == 0) + || ((fstatat (chdir_fd, link_name, &st1, AT_SYMLINK_NOFOLLOW) + == 0) + && (fstatat (chdir_fd, file_name, &st2, AT_SYMLINK_NOFOLLOW) + == 0) + && st1.st_dev == st2.st_dev + && st1.st_ino == st2.st_ino)) + return 0; + + errno = e; + } + while ((rc = maybe_recoverable (file_name, false, &interdir_made)) + == RECOVER_OK); + + if (rc == RECOVER_SKIP) + return 0; + if (!(incremental_option && errno == EEXIST)) + { + link_error (link_name, file_name); + return 1; + } + return 0; +} + +static int +extract_symlink (char *file_name, int typeflag) +{ +#ifdef HAVE_SYMLINK + bool interdir_made = false; + + if (! absolute_names_option + && (IS_ABSOLUTE_FILE_NAME (current_stat_info.link_name) + || contains_dot_dot (current_stat_info.link_name))) + return create_placeholder_file (file_name, true, &interdir_made); + + while (symlinkat (current_stat_info.link_name, chdir_fd, file_name) != 0) + switch (maybe_recoverable (file_name, false, &interdir_made)) + { + case RECOVER_OK: + continue; + + case RECOVER_SKIP: + return 0; + + case RECOVER_NO: + symlink_error (current_stat_info.link_name, file_name); + return -1; + } + + set_stat (file_name, ¤t_stat_info, -1, 0, 0, + SYMTYPE, false, AT_SYMLINK_NOFOLLOW); + return 0; + +#else + static int warned_once; + + if (!warned_once) + { + warned_once = 1; + WARNOPT (WARN_SYMBOLIC_CAST, + (0, 0, + _("Attempting extraction of symbolic links as hard links"))); + } + return extract_link (file_name, typeflag); +#endif +} + +#if S_IFCHR || S_IFBLK +static int +extract_node (char *file_name, int typeflag) +{ + bool interdir_made = false; + mode_t mode = (current_stat_info.stat.st_mode & (MODE_RWX | S_IFBLK | S_IFCHR) + & ~ (0 < same_owner_option ? S_IRWXG | S_IRWXO : 0)); + + while (mknodat (chdir_fd, file_name, mode, current_stat_info.stat.st_rdev) + != 0) + switch (maybe_recoverable (file_name, false, &interdir_made)) + { + case RECOVER_OK: + continue; + + case RECOVER_SKIP: + return 0; + + case RECOVER_NO: + mknod_error (file_name); + return -1; + } + + set_stat (file_name, ¤t_stat_info, -1, + mode & ~ current_umask, MODE_RWX, + typeflag, false, AT_SYMLINK_NOFOLLOW); + return 0; +} +#endif + +#if HAVE_MKFIFO || defined mkfifo +static int +extract_fifo (char *file_name, int typeflag) +{ + bool interdir_made = false; + mode_t mode = (current_stat_info.stat.st_mode & MODE_RWX + & ~ (0 < same_owner_option ? S_IRWXG | S_IRWXO : 0)); + + while (mkfifoat (chdir_fd, file_name, mode) != 0) + switch (maybe_recoverable (file_name, false, &interdir_made)) + { + case RECOVER_OK: + continue; + + case RECOVER_SKIP: + return 0; + + case RECOVER_NO: + mkfifo_error (file_name); + return -1; + } + + set_stat (file_name, ¤t_stat_info, -1, + mode & ~ current_umask, MODE_RWX, + typeflag, false, AT_SYMLINK_NOFOLLOW); + return 0; +} +#endif + +static int +extract_volhdr (char *file_name, int typeflag) +{ + skip_member (); + return 0; +} + +static int +extract_failure (char *file_name, int typeflag) +{ + return 1; +} + +typedef int (*tar_extractor_t) (char *file_name, int typeflag); + + + +/* Prepare to extract a file. Find extractor function. + Return zero if extraction should not proceed. */ + +static int +prepare_to_extract (char const *file_name, int typeflag, tar_extractor_t *fun) +{ + int rc = 1; + + if (EXTRACT_OVER_PIPE) + rc = 0; + + /* Select the extractor */ + switch (typeflag) + { + case GNUTYPE_SPARSE: + *fun = extract_file; + rc = 1; + break; + + case AREGTYPE: + case REGTYPE: + case CONTTYPE: + /* Appears to be a file. But BSD tar uses the convention that a slash + suffix means a directory. */ + if (current_stat_info.had_trailing_slash) + *fun = extract_dir; + else + { + *fun = extract_file; + rc = 1; + } + break; + + case SYMTYPE: + *fun = extract_symlink; + break; + + case LNKTYPE: + *fun = extract_link; + break; + +#if S_IFCHR + case CHRTYPE: + current_stat_info.stat.st_mode |= S_IFCHR; + *fun = extract_node; + break; +#endif + +#if S_IFBLK + case BLKTYPE: + current_stat_info.stat.st_mode |= S_IFBLK; + *fun = extract_node; + break; +#endif + +#if HAVE_MKFIFO || defined mkfifo + case FIFOTYPE: + *fun = extract_fifo; + break; +#endif + + case DIRTYPE: + case GNUTYPE_DUMPDIR: + *fun = extract_dir; + if (current_stat_info.is_dumpdir) + delay_directory_restore_option = true; + break; + + case GNUTYPE_VOLHDR: + *fun = extract_volhdr; + break; + + case GNUTYPE_MULTIVOL: + ERROR ((0, 0, + _("%s: Cannot extract -- file is continued from another volume"), + quotearg_colon (current_stat_info.file_name))); + *fun = extract_failure; + break; + + case GNUTYPE_LONGNAME: + case GNUTYPE_LONGLINK: + ERROR ((0, 0, _("Unexpected long name header"))); + *fun = extract_failure; + break; + + default: + WARNOPT (WARN_UNKNOWN_CAST, + (0, 0, + _("%s: Unknown file type `%c', extracted as normal file"), + quotearg_colon (file_name), typeflag)); + *fun = extract_file; + } + + /* Determine whether the extraction should proceed */ + if (rc == 0) + return 0; + + switch (old_files_option) + { + case UNLINK_FIRST_OLD_FILES: + if (!remove_any_file (file_name, + recursive_unlink_option ? RECURSIVE_REMOVE_OPTION + : ORDINARY_REMOVE_OPTION) + && errno && errno != ENOENT) + { + unlink_error (file_name); + return 0; + } + break; + + case KEEP_NEWER_FILES: + if (file_newer_p (file_name, 0, ¤t_stat_info)) + { + WARNOPT (WARN_IGNORE_NEWER, + (0, 0, _("Current %s is newer or same age"), + quote (file_name))); + return 0; + } + break; + + default: + break; + } + + return 1; +} + +/* Extract a file from the archive. */ +void +extract_archive (void) +{ + char typeflag; + tar_extractor_t fun; + + fatal_exit_hook = extract_finish; + + set_next_block_after (current_header); + + if (!current_stat_info.file_name[0] + || (interactive_option + && !confirm ("extract", current_stat_info.file_name))) + { + skip_member (); + return; + } + + /* Print the block from current_header and current_stat. */ + if (verbose_option) + print_header (¤t_stat_info, current_header, -1); + + /* Restore stats for all non-ancestor directories, unless + it is an incremental archive. + (see NOTICE in the comment to delay_set_stat above) */ + if (!delay_directory_restore_option) + { + int dir = chdir_current; + apply_nonancestor_delayed_set_stat (current_stat_info.file_name, 0); + chdir_do (dir); + } + + /* Take a safety backup of a previously existing file. */ + + if (backup_option) + if (!maybe_backup_file (current_stat_info.file_name, 0)) + { + int e = errno; + ERROR ((0, e, _("%s: Was unable to backup this file"), + quotearg_colon (current_stat_info.file_name))); + skip_member (); + return; + } + + /* Extract the archive entry according to its type. */ + /* KLUDGE */ + typeflag = sparse_member_p (¤t_stat_info) ? + GNUTYPE_SPARSE : current_header->header.typeflag; + + if (prepare_to_extract (current_stat_info.file_name, typeflag, &fun)) + { + if (fun && (*fun) (current_stat_info.file_name, typeflag) + && backup_option) + undo_last_backup (); + } + else + skip_member (); + +} + +/* Extract the links whose final extraction were delayed. */ +static void +apply_delayed_links (void) +{ + struct delayed_link *ds; + + for (ds = delayed_link_head; ds; ) + { + struct string_list *sources = ds->sources; + char const *valid_source = 0; + + chdir_do (ds->change_dir); + + for (sources = ds->sources; sources; sources = sources->next) + { + char const *source = sources->string; + struct stat st; + + /* Make sure the placeholder file is still there. If not, + don't create a link, as the placeholder was probably + removed by a later extraction. */ + if (fstatat (chdir_fd, source, &st, AT_SYMLINK_NOFOLLOW) == 0 + && st.st_dev == ds->dev + && st.st_ino == ds->ino + && timespec_cmp (get_stat_ctime (&st), ds->ctime) == 0) + { + /* Unlink the placeholder, then create a hard link if possible, + a symbolic link otherwise. */ + if (unlinkat (chdir_fd, source, 0) != 0) + unlink_error (source); + else if (valid_source + && (linkat (chdir_fd, valid_source, chdir_fd, source, 0) + == 0)) + ; + else if (!ds->is_symlink) + { + if (linkat (chdir_fd, ds->target, chdir_fd, source, 0) != 0) + link_error (ds->target, source); + } + else if (symlinkat (ds->target, chdir_fd, source) != 0) + symlink_error (ds->target, source); + else + { + struct tar_stat_info st1; + st1.stat.st_mode = ds->mode; + st1.stat.st_uid = ds->uid; + st1.stat.st_gid = ds->gid; + st1.atime = ds->atime; + st1.mtime = ds->mtime; + set_stat (source, &st1, -1, 0, 0, SYMTYPE, + false, AT_SYMLINK_NOFOLLOW); + valid_source = source; + } + } + } + + for (sources = ds->sources; sources; ) + { + struct string_list *next = sources->next; + free (sources); + sources = next; + } + + { + struct delayed_link *next = ds->next; + free (ds); + ds = next; + } + } + + delayed_link_head = 0; +} + +/* Finish the extraction of an archive. */ +void +extract_finish (void) +{ + /* First, fix the status of ordinary directories that need fixing. */ + apply_nonancestor_delayed_set_stat ("", 0); + + /* Then, apply delayed links, so that they don't affect delayed + directory status-setting for ordinary directories. */ + apply_delayed_links (); + + /* Finally, fix the status of directories that are ancestors + of delayed links. */ + apply_nonancestor_delayed_set_stat ("", 1); +} + +bool +rename_directory (char *src, char *dst) +{ + if (renameat (chdir_fd, src, chdir_fd, dst) != 0) + { + int e = errno; + bool interdir_made; + + switch (e) + { + case ENOENT: + if (make_directories (dst, &interdir_made) == 0) + { + if (renameat (chdir_fd, src, chdir_fd, dst) == 0) + return true; + e = errno; + } + break; + + case EXDEV: + /* FIXME: Fall back to recursive copying */ + + default: + break; + } + + ERROR ((0, e, _("Cannot rename %s to %s"), + quote_n (0, src), + quote_n (1, dst))); + return false; + } + return true; +} diff -Naur tar-1.26.orig//src/list.c tar-1.26/src/list.c --- tar-1.26.orig//src/list.c 2011-02-16 17:12:16.000000000 -0500 +++ tar-1.26/src/list.c 2011-09-06 13:22:06.000000000 -0400 @@ -604,6 +604,13 @@ assign_string (&stat_info->gname, header->header.gname[0] ? header->header.gname : NULL); + stat_info->acls_a_ptr = NULL; + stat_info->acls_a_len = 0; + stat_info->acls_d_ptr = NULL; + stat_info->acls_d_len = 0; + stat_info->cntx_name = NULL; + xheader_xattr_init(stat_info); + if (format == OLDGNU_FORMAT && incremental_option) { stat_info->atime.tv_sec = TIME_FROM_HEADER (header->oldgnu_header.atime); diff -Naur tar-1.26.orig//src/list.c.orig tar-1.26/src/list.c.orig --- tar-1.26.orig//src/list.c.orig 1969-12-31 19:00:00.000000000 -0500 +++ tar-1.26/src/list.c.orig 2011-02-16 17:12:16.000000000 -0500 @@ -0,0 +1,1446 @@ +/* List a tar archive, with support routines for reading a tar archive. + + Copyright (C) 1988, 1992, 1993, 1994, 1996, 1997, 1998, 1999, 2000, + 2001, 2003, 2004, 2005, 2006, 2007, 2010 Free Software Foundation, Inc. + + Written by John Gilmore, on 1985-08-26. + + This program is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by the + Free Software Foundation; either version 3, or (at your option) any later + version. + + This program is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General + Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ + +#include +#include +#include + +#include "common.h" + +#define max(a, b) ((a) < (b) ? (b) : (a)) + +union block *current_header; /* points to current archive header */ +enum archive_format current_format; /* recognized format */ +union block *recent_long_name; /* recent long name header and contents */ +union block *recent_long_link; /* likewise, for long link */ +size_t recent_long_name_blocks; /* number of blocks in recent_long_name */ +size_t recent_long_link_blocks; /* likewise, for long link */ +union block *recent_global_header; /* Recent global header block */ + +#define GID_FROM_HEADER(where) gid_from_header (where, sizeof (where)) +#define MAJOR_FROM_HEADER(where) major_from_header (where, sizeof (where)) +#define MINOR_FROM_HEADER(where) minor_from_header (where, sizeof (where)) +#define MODE_FROM_HEADER(where, hbits) \ + mode_from_header (where, sizeof (where), hbits) +#define TIME_FROM_HEADER(where) time_from_header (where, sizeof (where)) +#define UID_FROM_HEADER(where) uid_from_header (where, sizeof (where)) + +static gid_t gid_from_header (const char *buf, size_t size); +static major_t major_from_header (const char *buf, size_t size); +static minor_t minor_from_header (const char *buf, size_t size); +static mode_t mode_from_header (const char *buf, size_t size, unsigned *hbits); +static time_t time_from_header (const char *buf, size_t size); +static uid_t uid_from_header (const char *buf, size_t size); +static uintmax_t from_header (const char *, size_t, const char *, + uintmax_t, uintmax_t, bool, bool); + +/* Base 64 digits; see Internet RFC 2045 Table 1. */ +static char const base_64_digits[64] = +{ + 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', + 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', + 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', + 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', + '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' +}; + +/* Table of base-64 digit values indexed by unsigned chars. + The value is 64 for unsigned chars that are not base-64 digits. */ +static char base64_map[UCHAR_MAX + 1]; + +static void +base64_init (void) +{ + int i; + memset (base64_map, 64, sizeof base64_map); + for (i = 0; i < 64; i++) + base64_map[(int) base_64_digits[i]] = i; +} + +static char * +decode_xform (char *file_name, void *data) +{ + int type = *(int*)data; + + switch (type) + { + case XFORM_SYMLINK: + /* FIXME: It is not quite clear how and to which extent are the symbolic + links subject to filename transformation. In the absence of another + solution, symbolic links are exempt from component stripping and + name suffix normalization, but subject to filename transformation + proper. */ + return file_name; + + case XFORM_LINK: + file_name = safer_name_suffix (file_name, true, absolute_names_option); + break; + + case XFORM_REGFILE: + file_name = safer_name_suffix (file_name, false, absolute_names_option); + break; + } + + if (strip_name_components) + { + size_t prefix_len = stripped_prefix_len (file_name, + strip_name_components); + if (prefix_len == (size_t) -1) + prefix_len = strlen (file_name); + file_name += prefix_len; + } + return file_name; +} + +static bool +transform_member_name (char **pinput, int type) +{ + return transform_name_fp (pinput, type, decode_xform, &type); +} + +void +transform_stat_info (int typeflag, struct tar_stat_info *stat_info) +{ + if (typeflag == GNUTYPE_VOLHDR) + /* Name transformations don't apply to volume headers. */ + return; + + transform_member_name (&stat_info->file_name, XFORM_REGFILE); + switch (typeflag) + { + case SYMTYPE: + transform_member_name (&stat_info->link_name, XFORM_SYMLINK); + break; + + case LNKTYPE: + transform_member_name (&stat_info->link_name, XFORM_LINK); + } +} + +/* Main loop for reading an archive. */ +void +read_and (void (*do_something) (void)) +{ + enum read_header status = HEADER_STILL_UNREAD; + enum read_header prev_status; + struct timespec mtime; + + base64_init (); + name_gather (); + + open_archive (ACCESS_READ); + do + { + prev_status = status; + tar_stat_destroy (¤t_stat_info); + + status = read_header (¤t_header, ¤t_stat_info, + read_header_auto); + switch (status) + { + case HEADER_STILL_UNREAD: + case HEADER_SUCCESS_EXTENDED: + abort (); + + case HEADER_SUCCESS: + + /* Valid header. We should decode next field (mode) first. + Ensure incoming names are null terminated. */ + decode_header (current_header, ¤t_stat_info, + ¤t_format, 1); + if (! name_match (current_stat_info.file_name) + || (NEWER_OPTION_INITIALIZED (newer_mtime_option) + /* FIXME: We get mtime now, and again later; this causes + duplicate diagnostics if header.mtime is bogus. */ + && ((mtime.tv_sec + = TIME_FROM_HEADER (current_header->header.mtime)), + /* FIXME: Grab fractional time stamps from + extended header. */ + mtime.tv_nsec = 0, + current_stat_info.mtime = mtime, + OLDER_TAR_STAT_TIME (current_stat_info, m))) + || excluded_name (current_stat_info.file_name)) + { + switch (current_header->header.typeflag) + { + case GNUTYPE_VOLHDR: + case GNUTYPE_MULTIVOL: + break; + + case DIRTYPE: + if (show_omitted_dirs_option) + WARN ((0, 0, _("%s: Omitting"), + quotearg_colon (current_stat_info.file_name))); + /* Fall through. */ + default: + skip_member (); + continue; + } + } + transform_stat_info (current_header->header.typeflag, + ¤t_stat_info); + (*do_something) (); + continue; + + case HEADER_ZERO_BLOCK: + if (block_number_option) + { + char buf[UINTMAX_STRSIZE_BOUND]; + fprintf (stdlis, _("block %s: ** Block of NULs **\n"), + STRINGIFY_BIGINT (current_block_ordinal (), buf)); + } + + set_next_block_after (current_header); + + if (!ignore_zeros_option) + { + char buf[UINTMAX_STRSIZE_BOUND]; + + status = read_header (¤t_header, ¤t_stat_info, + read_header_auto); + if (status == HEADER_ZERO_BLOCK) + break; + WARNOPT (WARN_ALONE_ZERO_BLOCK, + (0, 0, _("A lone zero block at %s"), + STRINGIFY_BIGINT (current_block_ordinal (), buf))); + break; + } + status = prev_status; + continue; + + case HEADER_END_OF_FILE: + if (block_number_option) + { + char buf[UINTMAX_STRSIZE_BOUND]; + fprintf (stdlis, _("block %s: ** End of File **\n"), + STRINGIFY_BIGINT (current_block_ordinal (), buf)); + } + break; + + case HEADER_FAILURE: + /* If the previous header was good, tell them that we are + skipping bad ones. */ + set_next_block_after (current_header); + switch (prev_status) + { + case HEADER_STILL_UNREAD: + ERROR ((0, 0, _("This does not look like a tar archive"))); + /* Fall through. */ + + case HEADER_ZERO_BLOCK: + case HEADER_SUCCESS: + if (block_number_option) + { + char buf[UINTMAX_STRSIZE_BOUND]; + off_t block_ordinal = current_block_ordinal (); + block_ordinal -= recent_long_name_blocks; + block_ordinal -= recent_long_link_blocks; + fprintf (stdlis, _("block %s: "), + STRINGIFY_BIGINT (block_ordinal, buf)); + } + ERROR ((0, 0, _("Skipping to next header"))); + break; + + case HEADER_END_OF_FILE: + case HEADER_FAILURE: + /* We are in the middle of a cascade of errors. */ + break; + + case HEADER_SUCCESS_EXTENDED: + abort (); + } + continue; + } + break; + } + while (!all_names_found (¤t_stat_info)); + + close_archive (); + names_notfound (); /* print names not found */ +} + +/* Print a header block, based on tar options. */ +void +list_archive (void) +{ + off_t block_ordinal = current_block_ordinal (); + + /* Print the header block. */ + if (verbose_option) + print_header (¤t_stat_info, current_header, block_ordinal); + + if (incremental_option) + { + if (verbose_option > 2) + { + if (is_dumpdir (¤t_stat_info)) + list_dumpdir (current_stat_info.dumpdir, + dumpdir_size (current_stat_info.dumpdir)); + } + } + + skip_member (); +} + +/* Check header checksum */ +/* The standard BSD tar sources create the checksum by adding up the + bytes in the header as type char. I think the type char was unsigned + on the PDP-11, but it's signed on the Next and Sun. It looks like the + sources to BSD tar were never changed to compute the checksum + correctly, so both the Sun and Next add the bytes of the header as + signed chars. This doesn't cause a problem until you get a file with + a name containing characters with the high bit set. So tar_checksum + computes two checksums -- signed and unsigned. */ + +enum read_header +tar_checksum (union block *header, bool silent) +{ + size_t i; + int unsigned_sum = 0; /* the POSIX one :-) */ + int signed_sum = 0; /* the Sun one :-( */ + int recorded_sum; + uintmax_t parsed_sum; + char *p; + + p = header->buffer; + for (i = sizeof *header; i-- != 0;) + { + unsigned_sum += (unsigned char) *p; + signed_sum += (signed char) (*p++); + } + + if (unsigned_sum == 0) + return HEADER_ZERO_BLOCK; + + /* Adjust checksum to count the "chksum" field as blanks. */ + + for (i = sizeof header->header.chksum; i-- != 0;) + { + unsigned_sum -= (unsigned char) header->header.chksum[i]; + signed_sum -= (signed char) (header->header.chksum[i]); + } + unsigned_sum += ' ' * sizeof header->header.chksum; + signed_sum += ' ' * sizeof header->header.chksum; + + parsed_sum = from_header (header->header.chksum, + sizeof header->header.chksum, 0, + (uintmax_t) 0, + (uintmax_t) TYPE_MAXIMUM (int), true, silent); + if (parsed_sum == (uintmax_t) -1) + return HEADER_FAILURE; + + recorded_sum = parsed_sum; + + if (unsigned_sum != recorded_sum && signed_sum != recorded_sum) + return HEADER_FAILURE; + + return HEADER_SUCCESS; +} + +/* Read a block that's supposed to be a header block. Return its + address in *RETURN_BLOCK, and if it is good, the file's size + and names (file name, link name) in *INFO. + + Return one of enum read_header describing the status of the + operation. + + The MODE parameter instructs read_header what to do with special + header blocks, i.e.: extended POSIX, GNU long name or long link, + etc.: + + read_header_auto process them automatically, + read_header_x_raw when a special header is read, return + HEADER_SUCCESS_EXTENDED without actually + processing the header, + read_header_x_global when a POSIX global header is read, + decode it and return HEADER_SUCCESS_EXTENDED. + + You must always set_next_block_after(*return_block) to skip past + the header which this routine reads. */ + +enum read_header +read_header (union block **return_block, struct tar_stat_info *info, + enum read_header_mode mode) +{ + union block *header; + union block *header_copy; + char *bp; + union block *data_block; + size_t size, written; + union block *next_long_name = 0; + union block *next_long_link = 0; + size_t next_long_name_blocks = 0; + size_t next_long_link_blocks = 0; + + while (1) + { + enum read_header status; + + header = find_next_block (); + *return_block = header; + if (!header) + return HEADER_END_OF_FILE; + + if ((status = tar_checksum (header, false)) != HEADER_SUCCESS) + return status; + + /* Good block. Decode file size and return. */ + + if (header->header.typeflag == LNKTYPE) + info->stat.st_size = 0; /* links 0 size on tape */ + else + info->stat.st_size = OFF_FROM_HEADER (header->header.size); + + if (header->header.typeflag == GNUTYPE_LONGNAME + || header->header.typeflag == GNUTYPE_LONGLINK + || header->header.typeflag == XHDTYPE + || header->header.typeflag == XGLTYPE + || header->header.typeflag == SOLARIS_XHDTYPE) + { + if (mode == read_header_x_raw) + return HEADER_SUCCESS_EXTENDED; + else if (header->header.typeflag == GNUTYPE_LONGNAME + || header->header.typeflag == GNUTYPE_LONGLINK) + { + size_t name_size = info->stat.st_size; + size_t n = name_size % BLOCKSIZE; + size = name_size + BLOCKSIZE; + if (n) + size += BLOCKSIZE - n; + + if (name_size != info->stat.st_size || size < name_size) + xalloc_die (); + + header_copy = xmalloc (size + 1); + + if (header->header.typeflag == GNUTYPE_LONGNAME) + { + free (next_long_name); + next_long_name = header_copy; + next_long_name_blocks = size / BLOCKSIZE; + } + else + { + free (next_long_link); + next_long_link = header_copy; + next_long_link_blocks = size / BLOCKSIZE; + } + + set_next_block_after (header); + *header_copy = *header; + bp = header_copy->buffer + BLOCKSIZE; + + for (size -= BLOCKSIZE; size > 0; size -= written) + { + data_block = find_next_block (); + if (! data_block) + { + ERROR ((0, 0, _("Unexpected EOF in archive"))); + break; + } + written = available_space_after (data_block); + if (written > size) + written = size; + + memcpy (bp, data_block->buffer, written); + bp += written; + set_next_block_after ((union block *) + (data_block->buffer + written - 1)); + } + + *bp = '\0'; + } + else if (header->header.typeflag == XHDTYPE + || header->header.typeflag == SOLARIS_XHDTYPE) + xheader_read (&info->xhdr, header, + OFF_FROM_HEADER (header->header.size)); + else if (header->header.typeflag == XGLTYPE) + { + struct xheader xhdr; + + if (!recent_global_header) + recent_global_header = xmalloc (sizeof *recent_global_header); + memcpy (recent_global_header, header, + sizeof *recent_global_header); + memset (&xhdr, 0, sizeof xhdr); + xheader_read (&xhdr, header, + OFF_FROM_HEADER (header->header.size)); + xheader_decode_global (&xhdr); + xheader_destroy (&xhdr); + if (mode == read_header_x_global) + return HEADER_SUCCESS_EXTENDED; + } + + /* Loop! */ + + } + else + { + char const *name; + struct posix_header const *h = &header->header; + char namebuf[sizeof h->prefix + 1 + NAME_FIELD_SIZE + 1]; + + free (recent_long_name); + + if (next_long_name) + { + name = next_long_name->buffer + BLOCKSIZE; + recent_long_name = next_long_name; + recent_long_name_blocks = next_long_name_blocks; + } + else + { + /* Accept file names as specified by POSIX.1-1996 + section 10.1.1. */ + char *np = namebuf; + + if (h->prefix[0] && strcmp (h->magic, TMAGIC) == 0) + { + memcpy (np, h->prefix, sizeof h->prefix); + np[sizeof h->prefix] = '\0'; + np += strlen (np); + *np++ = '/'; + } + memcpy (np, h->name, sizeof h->name); + np[sizeof h->name] = '\0'; + name = namebuf; + recent_long_name = 0; + recent_long_name_blocks = 0; + } + assign_string (&info->orig_file_name, name); + assign_string (&info->file_name, name); + info->had_trailing_slash = strip_trailing_slashes (info->file_name); + + free (recent_long_link); + + if (next_long_link) + { + name = next_long_link->buffer + BLOCKSIZE; + recent_long_link = next_long_link; + recent_long_link_blocks = next_long_link_blocks; + } + else + { + memcpy (namebuf, h->linkname, sizeof h->linkname); + namebuf[sizeof h->linkname] = '\0'; + name = namebuf; + recent_long_link = 0; + recent_long_link_blocks = 0; + } + assign_string (&info->link_name, name); + + return HEADER_SUCCESS; + } + } +} + +#define ISOCTAL(c) ((c)>='0'&&(c)<='7') + +/* Decode things from a file HEADER block into STAT_INFO, also setting + *FORMAT_POINTER depending on the header block format. If + DO_USER_GROUP, decode the user/group information (this is useful + for extraction, but waste time when merely listing). + + read_header() has already decoded the checksum and length, so we don't. + + This routine should *not* be called twice for the same block, since + the two calls might use different DO_USER_GROUP values and thus + might end up with different uid/gid for the two calls. If anybody + wants the uid/gid they should decode it first, and other callers + should decode it without uid/gid before calling a routine, + e.g. print_header, that assumes decoded data. */ +void +decode_header (union block *header, struct tar_stat_info *stat_info, + enum archive_format *format_pointer, int do_user_group) +{ + enum archive_format format; + unsigned hbits; /* high bits of the file mode. */ + mode_t mode = MODE_FROM_HEADER (header->header.mode, &hbits); + + if (strcmp (header->header.magic, TMAGIC) == 0) + { + if (header->star_header.prefix[130] == 0 + && ISOCTAL (header->star_header.atime[0]) + && header->star_header.atime[11] == ' ' + && ISOCTAL (header->star_header.ctime[0]) + && header->star_header.ctime[11] == ' ') + format = STAR_FORMAT; + else if (stat_info->xhdr.size) + format = POSIX_FORMAT; + else + format = USTAR_FORMAT; + } + else if (strcmp (header->buffer + offsetof (struct posix_header, magic), + OLDGNU_MAGIC) + == 0) + format = hbits ? OLDGNU_FORMAT : GNU_FORMAT; + else + format = V7_FORMAT; + *format_pointer = format; + + stat_info->stat.st_mode = mode; + stat_info->mtime.tv_sec = TIME_FROM_HEADER (header->header.mtime); + stat_info->mtime.tv_nsec = 0; + assign_string (&stat_info->uname, + header->header.uname[0] ? header->header.uname : NULL); + assign_string (&stat_info->gname, + header->header.gname[0] ? header->header.gname : NULL); + + if (format == OLDGNU_FORMAT && incremental_option) + { + stat_info->atime.tv_sec = TIME_FROM_HEADER (header->oldgnu_header.atime); + stat_info->ctime.tv_sec = TIME_FROM_HEADER (header->oldgnu_header.ctime); + stat_info->atime.tv_nsec = stat_info->ctime.tv_nsec = 0; + } + else if (format == STAR_FORMAT) + { + stat_info->atime.tv_sec = TIME_FROM_HEADER (header->star_header.atime); + stat_info->ctime.tv_sec = TIME_FROM_HEADER (header->star_header.ctime); + stat_info->atime.tv_nsec = stat_info->ctime.tv_nsec = 0; + } + else + stat_info->atime = stat_info->ctime = start_time; + + if (format == V7_FORMAT) + { + stat_info->stat.st_uid = UID_FROM_HEADER (header->header.uid); + stat_info->stat.st_gid = GID_FROM_HEADER (header->header.gid); + stat_info->stat.st_rdev = 0; + } + else + { + if (do_user_group) + { + /* FIXME: Decide if this should somewhat depend on -p. */ + + if (numeric_owner_option + || !*header->header.uname + || !uname_to_uid (header->header.uname, &stat_info->stat.st_uid)) + stat_info->stat.st_uid = UID_FROM_HEADER (header->header.uid); + + if (numeric_owner_option + || !*header->header.gname + || !gname_to_gid (header->header.gname, &stat_info->stat.st_gid)) + stat_info->stat.st_gid = GID_FROM_HEADER (header->header.gid); + } + + switch (header->header.typeflag) + { + case BLKTYPE: + case CHRTYPE: + stat_info->stat.st_rdev = + makedev (MAJOR_FROM_HEADER (header->header.devmajor), + MINOR_FROM_HEADER (header->header.devminor)); + break; + + default: + stat_info->stat.st_rdev = 0; + } + } + + stat_info->archive_file_size = stat_info->stat.st_size; + xheader_decode (stat_info); + + if (sparse_member_p (stat_info)) + { + sparse_fixup_header (stat_info); + stat_info->is_sparse = true; + } + else + { + stat_info->is_sparse = false; + if (((current_format == GNU_FORMAT + || current_format == OLDGNU_FORMAT) + && current_header->header.typeflag == GNUTYPE_DUMPDIR) + || stat_info->dumpdir) + stat_info->is_dumpdir = true; + } +} + + +/* Convert buffer at WHERE0 of size DIGS from external format to + uintmax_t. DIGS must be positive. If TYPE is nonnull, the data + are of type TYPE. The buffer must represent a value in the range + -MINUS_MINVAL through MAXVAL. If OCTAL_ONLY, allow only octal + numbers instead of the other GNU extensions. Return -1 on error, + diagnosing the error if TYPE is nonnull and if !SILENT. */ +static uintmax_t +from_header (char const *where0, size_t digs, char const *type, + uintmax_t minus_minval, uintmax_t maxval, + bool octal_only, bool silent) +{ + uintmax_t value; + char const *where = where0; + char const *lim = where + digs; + int negative = 0; + + /* Accommodate buggy tar of unknown vintage, which outputs leading + NUL if the previous field overflows. */ + where += !*where; + + /* Accommodate older tars, which output leading spaces. */ + for (;;) + { + if (where == lim) + { + if (type && !silent) + ERROR ((0, 0, + /* TRANSLATORS: %s is type of the value (gid_t, uid_t, + etc.) */ + _("Blanks in header where numeric %s value expected"), + type)); + return -1; + } + if (!ISSPACE ((unsigned char) *where)) + break; + where++; + } + + value = 0; + if (ISODIGIT (*where)) + { + char const *where1 = where; + uintmax_t overflow = 0; + + for (;;) + { + value += *where++ - '0'; + if (where == lim || ! ISODIGIT (*where)) + break; + overflow |= value ^ (value << LG_8 >> LG_8); + value <<= LG_8; + } + + /* Parse the output of older, unportable tars, which generate + negative values in two's complement octal. If the leading + nonzero digit is 1, we can't recover the original value + reliably; so do this only if the digit is 2 or more. This + catches the common case of 32-bit negative time stamps. */ + if ((overflow || maxval < value) && '2' <= *where1 && type) + { + /* Compute the negative of the input value, assuming two's + complement. */ + int digit = (*where1 - '0') | 4; + overflow = 0; + value = 0; + where = where1; + for (;;) + { + value += 7 - digit; + where++; + if (where == lim || ! ISODIGIT (*where)) + break; + digit = *where - '0'; + overflow |= value ^ (value << LG_8 >> LG_8); + value <<= LG_8; + } + value++; + overflow |= !value; + + if (!overflow && value <= minus_minval) + { + if (!silent) + WARN ((0, 0, + /* TRANSLATORS: Second %s is a type name (gid_t,uid_t,etc.) */ + _("Archive octal value %.*s is out of %s range; assuming two's complement"), + (int) (where - where1), where1, type)); + negative = 1; + } + } + + if (overflow) + { + if (type && !silent) + ERROR ((0, 0, + /* TRANSLATORS: Second %s is a type name (gid_t,uid_t,etc.) */ + _("Archive octal value %.*s is out of %s range"), + (int) (where - where1), where1, type)); + return -1; + } + } + else if (octal_only) + { + /* Suppress the following extensions. */ + } + else if (*where == '-' || *where == '+') + { + /* Parse base-64 output produced only by tar test versions + 1.13.6 (1999-08-11) through 1.13.11 (1999-08-23). + Support for this will be withdrawn in future releases. */ + int dig; + if (!silent) + { + static bool warned_once; + if (! warned_once) + { + warned_once = true; + WARN ((0, 0, _("Archive contains obsolescent base-64 headers"))); + } + } + negative = *where++ == '-'; + while (where != lim + && (dig = base64_map[(unsigned char) *where]) < 64) + { + if (value << LG_64 >> LG_64 != value) + { + char *string = alloca (digs + 1); + memcpy (string, where0, digs); + string[digs] = '\0'; + if (type && !silent) + ERROR ((0, 0, + _("Archive signed base-64 string %s is out of %s range"), + quote (string), type)); + return -1; + } + value = (value << LG_64) | dig; + where++; + } + } + else if (*where == '\200' /* positive base-256 */ + || *where == '\377' /* negative base-256 */) + { + /* Parse base-256 output. A nonnegative number N is + represented as (256**DIGS)/2 + N; a negative number -N is + represented as (256**DIGS) - N, i.e. as two's complement. + The representation guarantees that the leading bit is + always on, so that we don't confuse this format with the + others (assuming ASCII bytes of 8 bits or more). */ + int signbit = *where & (1 << (LG_256 - 2)); + uintmax_t topbits = (((uintmax_t) - signbit) + << (CHAR_BIT * sizeof (uintmax_t) + - LG_256 - (LG_256 - 2))); + value = (*where++ & ((1 << (LG_256 - 2)) - 1)) - signbit; + for (;;) + { + value = (value << LG_256) + (unsigned char) *where++; + if (where == lim) + break; + if (((value << LG_256 >> LG_256) | topbits) != value) + { + if (type && !silent) + ERROR ((0, 0, + _("Archive base-256 value is out of %s range"), + type)); + return -1; + } + } + negative = signbit; + if (negative) + value = -value; + } + + if (where != lim && *where && !ISSPACE ((unsigned char) *where)) + { + if (type) + { + char buf[1000]; /* Big enough to represent any header. */ + static struct quoting_options *o; + + if (!o) + { + o = clone_quoting_options (0); + set_quoting_style (o, locale_quoting_style); + } + + while (where0 != lim && ! lim[-1]) + lim--; + quotearg_buffer (buf, sizeof buf, where0, lim - where, o); + if (!silent) + ERROR ((0, 0, + /* TRANSLATORS: Second %s is a type name (gid_t,uid_t,etc.) */ + _("Archive contains %.*s where numeric %s value expected"), + (int) sizeof buf, buf, type)); + } + + return -1; + } + + if (value <= (negative ? minus_minval : maxval)) + return negative ? -value : value; + + if (type && !silent) + { + char minval_buf[UINTMAX_STRSIZE_BOUND + 1]; + char maxval_buf[UINTMAX_STRSIZE_BOUND]; + char value_buf[UINTMAX_STRSIZE_BOUND + 1]; + char *minval_string = STRINGIFY_BIGINT (minus_minval, minval_buf + 1); + char *value_string = STRINGIFY_BIGINT (value, value_buf + 1); + if (negative) + *--value_string = '-'; + if (minus_minval) + *--minval_string = '-'; + /* TRANSLATORS: Second %s is type name (gid_t,uid_t,etc.) */ + ERROR ((0, 0, _("Archive value %s is out of %s range %s..%s"), + value_string, type, + minval_string, STRINGIFY_BIGINT (maxval, maxval_buf))); + } + + return -1; +} + +static gid_t +gid_from_header (const char *p, size_t s) +{ + return from_header (p, s, "gid_t", + - (uintmax_t) TYPE_MINIMUM (gid_t), + (uintmax_t) TYPE_MAXIMUM (gid_t), + false, false); +} + +static major_t +major_from_header (const char *p, size_t s) +{ + return from_header (p, s, "major_t", + - (uintmax_t) TYPE_MINIMUM (major_t), + (uintmax_t) TYPE_MAXIMUM (major_t), false, false); +} + +static minor_t +minor_from_header (const char *p, size_t s) +{ + return from_header (p, s, "minor_t", + - (uintmax_t) TYPE_MINIMUM (minor_t), + (uintmax_t) TYPE_MAXIMUM (minor_t), false, false); +} + +/* Convert P to the file mode, as understood by tar. + Store unrecognized mode bits (from 10th up) in HBITS. */ +static mode_t +mode_from_header (const char *p, size_t s, unsigned *hbits) +{ + unsigned u = from_header (p, s, "mode_t", + - (uintmax_t) TYPE_MINIMUM (mode_t), + TYPE_MAXIMUM (uintmax_t), false, false); + mode_t mode = ((u & TSUID ? S_ISUID : 0) + | (u & TSGID ? S_ISGID : 0) + | (u & TSVTX ? S_ISVTX : 0) + | (u & TUREAD ? S_IRUSR : 0) + | (u & TUWRITE ? S_IWUSR : 0) + | (u & TUEXEC ? S_IXUSR : 0) + | (u & TGREAD ? S_IRGRP : 0) + | (u & TGWRITE ? S_IWGRP : 0) + | (u & TGEXEC ? S_IXGRP : 0) + | (u & TOREAD ? S_IROTH : 0) + | (u & TOWRITE ? S_IWOTH : 0) + | (u & TOEXEC ? S_IXOTH : 0)); + *hbits = mode ^ u; + return mode; +} + +off_t +off_from_header (const char *p, size_t s) +{ + /* Negative offsets are not allowed in tar files, so invoke + from_header with minimum value 0, not TYPE_MINIMUM (off_t). */ + return from_header (p, s, "off_t", (uintmax_t) 0, + (uintmax_t) TYPE_MAXIMUM (off_t), false, false); +} + +static time_t +time_from_header (const char *p, size_t s) +{ + return from_header (p, s, "time_t", + - (uintmax_t) TYPE_MINIMUM (time_t), + (uintmax_t) TYPE_MAXIMUM (time_t), false, false); +} + +static uid_t +uid_from_header (const char *p, size_t s) +{ + return from_header (p, s, "uid_t", + - (uintmax_t) TYPE_MINIMUM (uid_t), + (uintmax_t) TYPE_MAXIMUM (uid_t), false, false); +} + +uintmax_t +uintmax_from_header (const char *p, size_t s) +{ + return from_header (p, s, "uintmax_t", (uintmax_t) 0, + TYPE_MAXIMUM (uintmax_t), false, false); +} + + +/* Return a printable representation of T. The result points to + static storage that can be reused in the next call to this + function, to ctime, or to asctime. If FULL_TIME, then output the + time stamp to its full resolution; otherwise, just output it to + 1-minute resolution. */ +char const * +tartime (struct timespec t, bool full_time) +{ + enum { fraclen = sizeof ".FFFFFFFFF" - 1 }; + static char buffer[max (UINTMAX_STRSIZE_BOUND + 1, + INT_STRLEN_BOUND (int) + 16) + + fraclen]; + struct tm *tm; + time_t s = t.tv_sec; + int ns = t.tv_nsec; + bool negative = s < 0; + char *p; + + if (negative && ns != 0) + { + s++; + ns = 1000000000 - ns; + } + + tm = utc_option ? gmtime (&s) : localtime (&s); + if (tm) + { + if (full_time) + { + sprintf (buffer, "%04ld-%02d-%02d %02d:%02d:%02d", + tm->tm_year + 1900L, tm->tm_mon + 1, tm->tm_mday, + tm->tm_hour, tm->tm_min, tm->tm_sec); + code_ns_fraction (ns, buffer + strlen (buffer)); + } + else + sprintf (buffer, "%04ld-%02d-%02d %02d:%02d", + tm->tm_year + 1900L, tm->tm_mon + 1, tm->tm_mday, + tm->tm_hour, tm->tm_min); + return buffer; + } + + /* The time stamp cannot be broken down, most likely because it + is out of range. Convert it as an integer, + right-adjusted in a field with the same width as the usual + 4-year ISO time format. */ + p = umaxtostr (negative ? - (uintmax_t) s : s, + buffer + sizeof buffer - UINTMAX_STRSIZE_BOUND - fraclen); + if (negative) + *--p = '-'; + while ((buffer + sizeof buffer - sizeof "YYYY-MM-DD HH:MM" + + (full_time ? sizeof ":SS.FFFFFFFFF" - 1 : 0)) + < p) + *--p = ' '; + if (full_time) + code_ns_fraction (ns, buffer + sizeof buffer - 1 - fraclen); + return p; +} + +/* Actually print it. + + Plain and fancy file header block logging. Non-verbose just prints + the name, e.g. for "tar t" or "tar x". This should just contain + file names, so it can be fed back into tar with xargs or the "-T" + option. The verbose option can give a bunch of info, one line per + file. I doubt anybody tries to parse its format, or if they do, + they shouldn't. Unix tar is pretty random here anyway. */ + + +/* Width of "user/group size", with initial value chosen + heuristically. This grows as needed, though this may cause some + stairstepping in the output. Make it too small and the output will + almost always look ragged. Make it too large and the output will + be spaced out too far. */ +static int ugswidth = 19; + +/* Width of printed time stamps. It grows if longer time stamps are + found (typically, those with nanosecond resolution). Like + USGWIDTH, some stairstepping may occur. */ +static int datewidth = sizeof "YYYY-MM-DD HH:MM" - 1; + +static bool volume_label_printed = false; + +static void +simple_print_header (struct tar_stat_info *st, union block *blk, + off_t block_ordinal) +{ + char modes[11]; + char const *time_stamp; + int time_stamp_len; + char *temp_name; + + /* These hold formatted ints. */ + char uform[UINTMAX_STRSIZE_BOUND], gform[UINTMAX_STRSIZE_BOUND]; + char *user, *group; + char size[2 * UINTMAX_STRSIZE_BOUND]; + /* holds formatted size or major,minor */ + char uintbuf[UINTMAX_STRSIZE_BOUND]; + int pad; + int sizelen; + + if (show_transformed_names_option) + temp_name = st->file_name ? st->file_name : st->orig_file_name; + else + temp_name = st->orig_file_name ? st->orig_file_name : st->file_name; + + if (block_number_option) + { + char buf[UINTMAX_STRSIZE_BOUND]; + if (block_ordinal < 0) + block_ordinal = current_block_ordinal (); + block_ordinal -= recent_long_name_blocks; + block_ordinal -= recent_long_link_blocks; + fprintf (stdlis, _("block %s: "), + STRINGIFY_BIGINT (block_ordinal, buf)); + } + + if (verbose_option <= 1) + { + /* Just the fax, mam. */ + fprintf (stdlis, "%s\n", quotearg (temp_name)); + } + else + { + /* File type and modes. */ + + modes[0] = '?'; + switch (blk->header.typeflag) + { + case GNUTYPE_VOLHDR: + volume_label_printed = true; + modes[0] = 'V'; + break; + + case GNUTYPE_MULTIVOL: + modes[0] = 'M'; + break; + + case GNUTYPE_LONGNAME: + case GNUTYPE_LONGLINK: + modes[0] = 'L'; + ERROR ((0, 0, _("Unexpected long name header"))); + break; + + case GNUTYPE_SPARSE: + case REGTYPE: + case AREGTYPE: + modes[0] = '-'; + if (temp_name[strlen (temp_name) - 1] == '/') + modes[0] = 'd'; + break; + case LNKTYPE: + modes[0] = 'h'; + break; + case GNUTYPE_DUMPDIR: + modes[0] = 'd'; + break; + case DIRTYPE: + modes[0] = 'd'; + break; + case SYMTYPE: + modes[0] = 'l'; + break; + case BLKTYPE: + modes[0] = 'b'; + break; + case CHRTYPE: + modes[0] = 'c'; + break; + case FIFOTYPE: + modes[0] = 'p'; + break; + case CONTTYPE: + modes[0] = 'C'; + break; + } + + pax_decode_mode (st->stat.st_mode, modes + 1); + + /* Time stamp. */ + + time_stamp = tartime (st->mtime, full_time_option); + time_stamp_len = strlen (time_stamp); + if (datewidth < time_stamp_len) + datewidth = time_stamp_len; + + /* User and group names. */ + + if (st->uname + && st->uname[0] + && current_format != V7_FORMAT + && !numeric_owner_option) + user = st->uname; + else + { + /* Try parsing it as an unsigned integer first, and as a + uid_t if that fails. This method can list positive user + ids that are too large to fit in a uid_t. */ + uintmax_t u = from_header (blk->header.uid, + sizeof blk->header.uid, 0, + (uintmax_t) 0, + (uintmax_t) TYPE_MAXIMUM (uintmax_t), + false, false); + if (u != -1) + user = STRINGIFY_BIGINT (u, uform); + else + { + sprintf (uform, "%ld", + (long) UID_FROM_HEADER (blk->header.uid)); + user = uform; + } + } + + if (st->gname + && st->gname[0] + && current_format != V7_FORMAT + && !numeric_owner_option) + group = st->gname; + else + { + /* Try parsing it as an unsigned integer first, and as a + gid_t if that fails. This method can list positive group + ids that are too large to fit in a gid_t. */ + uintmax_t g = from_header (blk->header.gid, + sizeof blk->header.gid, 0, + (uintmax_t) 0, + (uintmax_t) TYPE_MAXIMUM (uintmax_t), + false, false); + if (g != -1) + group = STRINGIFY_BIGINT (g, gform); + else + { + sprintf (gform, "%ld", + (long) GID_FROM_HEADER (blk->header.gid)); + group = gform; + } + } + + /* Format the file size or major/minor device numbers. */ + + switch (blk->header.typeflag) + { + case CHRTYPE: + case BLKTYPE: + strcpy (size, + STRINGIFY_BIGINT (major (st->stat.st_rdev), uintbuf)); + strcat (size, ","); + strcat (size, + STRINGIFY_BIGINT (minor (st->stat.st_rdev), uintbuf)); + break; + + default: + /* st->stat.st_size keeps stored file size */ + strcpy (size, STRINGIFY_BIGINT (st->stat.st_size, uintbuf)); + break; + } + + /* Figure out padding and print the whole line. */ + + sizelen = strlen (size); + pad = strlen (user) + 1 + strlen (group) + 1 + sizelen; + if (pad > ugswidth) + ugswidth = pad; + + fprintf (stdlis, "%s %s/%s %*s %-*s", + modes, user, group, ugswidth - pad + sizelen, size, + datewidth, time_stamp); + + fprintf (stdlis, " %s", quotearg (temp_name)); + + switch (blk->header.typeflag) + { + case SYMTYPE: + fprintf (stdlis, " -> %s\n", quotearg (st->link_name)); + break; + + case LNKTYPE: + fprintf (stdlis, _(" link to %s\n"), quotearg (st->link_name)); + break; + + default: + { + char type_string[2]; + type_string[0] = blk->header.typeflag; + type_string[1] = '\0'; + fprintf (stdlis, _(" unknown file type %s\n"), + quote (type_string)); + } + break; + + case AREGTYPE: + case REGTYPE: + case GNUTYPE_SPARSE: + case CHRTYPE: + case BLKTYPE: + case DIRTYPE: + case FIFOTYPE: + case CONTTYPE: + case GNUTYPE_DUMPDIR: + putc ('\n', stdlis); + break; + + case GNUTYPE_LONGLINK: + fprintf (stdlis, _("--Long Link--\n")); + break; + + case GNUTYPE_LONGNAME: + fprintf (stdlis, _("--Long Name--\n")); + break; + + case GNUTYPE_VOLHDR: + fprintf (stdlis, _("--Volume Header--\n")); + break; + + case GNUTYPE_MULTIVOL: + strcpy (size, + STRINGIFY_BIGINT + (UINTMAX_FROM_HEADER (blk->oldgnu_header.offset), + uintbuf)); + fprintf (stdlis, _("--Continued at byte %s--\n"), size); + break; + } + } + fflush (stdlis); +} + + +static void +print_volume_label (void) +{ + struct tar_stat_info vstat; + union block vblk; + enum archive_format dummy; + + memset (&vblk, 0, sizeof (vblk)); + vblk.header.typeflag = GNUTYPE_VOLHDR; + if (recent_global_header) + memcpy (vblk.header.mtime, recent_global_header->header.mtime, + sizeof vblk.header.mtime); + tar_stat_init (&vstat); + assign_string (&vstat.file_name, "."); + decode_header (&vblk, &vstat, &dummy, 0); + assign_string (&vstat.file_name, volume_label); + simple_print_header (&vstat, &vblk, 0); + tar_stat_destroy (&vstat); +} + +void +print_header (struct tar_stat_info *st, union block *blk, + off_t block_ordinal) +{ + if (current_format == POSIX_FORMAT && !volume_label_printed && volume_label) + { + print_volume_label (); + volume_label_printed = true; + } + + simple_print_header (st, blk, block_ordinal); +} + +/* Print a similar line when we make a directory automatically. */ +void +print_for_mkdir (char *dirname, int length, mode_t mode) +{ + char modes[11]; + + if (verbose_option > 1) + { + /* File type and modes. */ + + modes[0] = 'd'; + pax_decode_mode (mode, modes + 1); + + if (block_number_option) + { + char buf[UINTMAX_STRSIZE_BOUND]; + fprintf (stdlis, _("block %s: "), + STRINGIFY_BIGINT (current_block_ordinal (), buf)); + } + + fprintf (stdlis, "%s %*s %.*s\n", modes, ugswidth + 1 + datewidth, + _("Creating directory:"), length, quotearg (dirname)); + } +} + +/* Skip over SIZE bytes of data in blocks in the archive. */ +void +skip_file (off_t size) +{ + union block *x; + + /* FIXME: Make sure mv_begin_read is always called before it */ + + if (seekable_archive) + { + off_t nblk = seek_archive (size); + if (nblk >= 0) + size -= nblk * BLOCKSIZE; + else + seekable_archive = false; + } + + mv_size_left (size); + + while (size > 0) + { + x = find_next_block (); + if (! x) + FATAL_ERROR ((0, 0, _("Unexpected EOF in archive"))); + + set_next_block_after (x); + size -= BLOCKSIZE; + mv_size_left (size); + } +} + +/* Skip the current member in the archive. + NOTE: Current header must be decoded before calling this function. */ +void +skip_member (void) +{ + if (!current_stat_info.skipped) + { + char save_typeflag = current_header->header.typeflag; + set_next_block_after (current_header); + + mv_begin_read (¤t_stat_info); + + if (current_stat_info.is_sparse) + sparse_skip_file (¤t_stat_info); + else if (save_typeflag != DIRTYPE) + skip_file (current_stat_info.stat.st_size); + + mv_end (); + } +} + +void +test_archive_label () +{ + base64_init (); + name_gather (); + + open_archive (ACCESS_READ); + if (read_header (¤t_header, ¤t_stat_info, read_header_auto) + == HEADER_SUCCESS) + { + decode_header (current_header, + ¤t_stat_info, ¤t_format, 0); + if (current_header->header.typeflag == GNUTYPE_VOLHDR) + assign_string (&volume_label, current_header->header.name); + + if (volume_label) + { + if (verbose_option) + print_volume_label (); + if (!name_match (volume_label) && multi_volume_option) + { + char *s = drop_volume_label_suffix (volume_label); + name_match (s); + free (s); + } + } + } + close_archive (); + label_notfound (); +} diff -Naur tar-1.26.orig//src/Makefile.am tar-1.26/src/Makefile.am --- tar-1.26.orig//src/Makefile.am 2010-10-24 14:07:54.000000000 -0400 +++ tar-1.26/src/Makefile.am 2011-09-06 13:22:06.000000000 -0400 @@ -20,7 +20,7 @@ bin_PROGRAMS = tar -noinst_HEADERS = arith.h common.h tar.h +noinst_HEADERS = arith.h common.h tar.h xattrs.h tar_SOURCES = \ buffer.c\ checkpoint.c\ @@ -42,10 +42,11 @@ unlink.c\ update.c\ utf8.c\ - warning.c + warning.c\ + xattrs.c INCLUDES = -I$(top_srcdir)/gnu -I../ -I../gnu -I$(top_srcdir)/lib -I../lib LDADD = ../lib/libtar.a ../gnu/libgnu.a $(LIBINTL) $(LIBICONV) -tar_LDADD = $(LDADD) $(LIB_CLOCK_GETTIME) $(LIB_EACCESS) +tar_LDADD = $(LIBS) $(LDADD) $(LIB_CLOCK_GETTIME) $(LIB_EACCESS) diff -Naur tar-1.26.orig//src/tar.c tar-1.26/src/tar.c --- tar-1.26.orig//src/tar.c 2010-10-24 14:07:55.000000000 -0400 +++ tar-1.26/src/tar.c 2011-09-06 13:22:06.000000000 -0400 @@ -255,7 +255,8 @@ enum { - ANCHORED_OPTION = CHAR_MAX + 1, + ACLS_OPTION = CHAR_MAX + 1, + ANCHORED_OPTION, ATIME_PRESERVE_OPTION, BACKUP_OPTION, CHECK_DEVICE_OPTION, @@ -288,6 +289,7 @@ MODE_OPTION, MTIME_OPTION, NEWER_MTIME_OPTION, + NO_ACLS_OPTION, NO_ANCHORED_OPTION, NO_AUTO_COMPRESS_OPTION, NO_CHECK_DEVICE_OPTION, @@ -301,9 +303,11 @@ NO_SAME_OWNER_OPTION, NO_SAME_PERMISSIONS_OPTION, NO_SEEK_OPTION, + NO_SELINUX_CONTEXT_OPTION, NO_UNQUOTE_OPTION, NO_WILDCARDS_MATCH_SLASH_OPTION, NO_WILDCARDS_OPTION, + NO_XATTR_OPTION, NULL_OPTION, NUMERIC_OWNER_OPTION, OCCURRENCE_OPTION, @@ -325,6 +329,7 @@ RMT_COMMAND_OPTION, RSH_COMMAND_OPTION, SAME_OWNER_OPTION, + SELINUX_CONTEXT_OPTION, SHOW_DEFAULTS_OPTION, SHOW_OMITTED_DIRS_OPTION, SHOW_TRANSFORMED_NAMES_OPTION, @@ -340,7 +345,8 @@ VOLNO_FILE_OPTION, WARNING_OPTION, WILDCARDS_MATCH_SLASH_OPTION, - WILDCARDS_OPTION + WILDCARDS_OPTION, + XATTR_OPTION }; const char *argp_program_version = "tar (" PACKAGE_NAME ") " VERSION; @@ -486,6 +492,10 @@ {NULL, 0, NULL, 0, N_("Handling of file attributes:"), GRID }, + {"acls", ACLS_OPTION, 0, 0, + N_("Save the ACLs to the archive"), GRID+1 }, + {"no-acls", NO_ACLS_OPTION, 0, 0, + N_("Don't extract the ACLs from the archive"), GRID+1 }, {"owner", OWNER_OPTION, N_("NAME"), 0, N_("force NAME as owner for added files"), GRID+1 }, {"group", GROUP_OPTION, N_("NAME"), 0, @@ -516,6 +526,14 @@ {"preserve-order", 's', 0, 0, N_("sort names to extract to match archive"), GRID+1 }, {"same-order", 0, 0, OPTION_ALIAS, NULL, GRID+1 }, + {"selinux", SELINUX_CONTEXT_OPTION, 0, 0, + N_("Save the SELinux context to the archive"), GRID+1 }, + {"no-selinux", NO_SELINUX_CONTEXT_OPTION, 0, 0, + N_("Don't extract the SELinux context from the archive"), GRID+1 }, + {"xattrs", XATTR_OPTION, 0, 0, + N_("Save the user/root xattrs to the archive"), GRID+1 }, + {"no-xattrs", NO_XATTR_OPTION, 0, 0, + N_("Don't extract the user/root xattrs from the archive"), GRID+1 }, {"preserve", PRESERVE_OPTION, 0, 0, N_("same as both -p and -s"), GRID+1 }, {"delay-directory-restore", DELAY_DIRECTORY_RESTORE_OPTION, 0, 0, @@ -2079,6 +2097,37 @@ same_permissions_option = -1; break; + case ACLS_OPTION: + set_archive_format ("posix"); + acls_option = 1; + break; + + case NO_ACLS_OPTION: + acls_option = -1; + break; + + case SELINUX_CONTEXT_OPTION: + set_archive_format ("posix"); + selinux_context_option = 1; + break; + + case NO_SELINUX_CONTEXT_OPTION: + selinux_context_option = -1; + break; + + case XATTR_OPTION: + set_archive_format ("posix"); + if (!acls_option) acls_option = 1; + if (!selinux_context_option) selinux_context_option = 1; + xattrs_option = 1; + break; + + case NO_XATTR_OPTION: + if (!acls_option) acls_option = -1; + if (!selinux_context_option) selinux_context_option = -1; + xattrs_option = -1; + break; + case RECURSION_OPTION: recursion_option = FNM_LEADING_DIR; break; @@ -2461,6 +2510,29 @@ || subcommand_option != LIST_SUBCOMMAND)) USAGE_ERROR ((0, 0, _("--pax-option can be used only on POSIX archives"))); + /* star create's non-POSIX typed archives with xattr support, so allow the + extra headers */ + if ((acls_option > 0) + && archive_format != POSIX_FORMAT + && (subcommand_option != EXTRACT_SUBCOMMAND + || subcommand_option != DIFF_SUBCOMMAND + || subcommand_option != LIST_SUBCOMMAND)) + USAGE_ERROR ((0, 0, _("--acls can be used only on POSIX archives"))); + + if ((selinux_context_option > 0) + && archive_format != POSIX_FORMAT + && (subcommand_option != EXTRACT_SUBCOMMAND + || subcommand_option != DIFF_SUBCOMMAND + || subcommand_option != LIST_SUBCOMMAND)) + USAGE_ERROR ((0, 0, _("--selinux can be used only on POSIX archives"))); + + if ((xattrs_option > 0) + && archive_format != POSIX_FORMAT + && (subcommand_option != EXTRACT_SUBCOMMAND + || subcommand_option != DIFF_SUBCOMMAND + || subcommand_option != LIST_SUBCOMMAND)) + USAGE_ERROR ((0, 0, _("--xattrs can be used only on POSIX archives"))); + /* If ready to unlink hierarchies, so we are for simpler files. */ if (recursive_unlink_option) old_files_option = UNLINK_FIRST_OLD_FILES; @@ -2713,11 +2785,15 @@ tar_stat_destroy (struct tar_stat_info *st) { tar_stat_close (st); + xheader_xattr_free (st->xattr_map, st->xattr_map_size); free (st->orig_file_name); free (st->file_name); free (st->link_name); free (st->uname); free (st->gname); + free (st->cntx_name); + free (st->acls_a_ptr); + free (st->acls_d_ptr); free (st->sparse_map); free (st->dumpdir); xheader_destroy (&st->xhdr); diff -Naur tar-1.26.orig//src/tar.h tar-1.26/src/tar.h --- tar-1.26.orig//src/tar.h 2010-10-24 14:07:46.000000000 -0400 +++ tar-1.26/src/tar.h 2011-09-06 13:22:06.000000000 -0400 @@ -276,6 +276,14 @@ uintmax_t string_length; }; +/* Information about xattrs for a file. */ +struct xattr_array + { + char *xkey; + char *xval_ptr; + size_t xval_len; + }; + struct tar_stat_info { char *orig_file_name; /* name of file read from the archive header */ @@ -287,6 +295,15 @@ char *uname; /* user name of owner */ char *gname; /* group name of owner */ + + char *cntx_name; /* SELinux context for the current archive entry. */ + + char *acls_a_ptr; /* Access ACLs for the current archive entry. */ + size_t acls_a_len; /* Access ACLs for the current archive entry. */ + + char *acls_d_ptr; /* Default ACLs for the current archive entry. */ + size_t acls_d_len; /* Default ACLs for the current archive entry. */ + struct stat stat; /* regular filesystem stat */ /* STAT doesn't always have access, data modification, and status @@ -309,6 +326,9 @@ size_t sparse_map_size; /* Size of the sparse map */ struct sp_array *sparse_map; + size_t xattr_map_size; /* Size of the xattr map */ + struct xattr_array *xattr_map; + /* Extended headers */ struct xheader xhdr; diff -Naur tar-1.26.orig//src/xattrs.c tar-1.26/src/xattrs.c --- tar-1.26.orig//src/xattrs.c 1969-12-31 19:00:00.000000000 -0500 +++ tar-1.26/src/xattrs.c 2011-09-06 13:22:06.000000000 -0400 @@ -0,0 +1,489 @@ +/* Create a tar archive. + + Copyright (C) 2006 Free Software Foundation, Inc. + + Written by James Antill, on 2006-07-27. + + This program is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by the + Free Software Foundation; either version 2, or (at your option) any later + version. + + This program is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General + Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ + +#include + +#include + +#include "common.h" + + +#ifndef HAVE_SELINUX_SELINUX_H +# undef HAVE_LIBSELINUX +#endif + +#ifndef HAVE_ATTR_XATTR_H +# undef HAVE_XATTRS +#endif + +#ifndef HAVE_SYS_ACL_H +# undef HAVE_LIBACL +#endif + +#ifdef HAVE_SELINUX_SELINUX_H +# include +#endif + +#ifdef HAVE_ATTR_XATTR_H +# include +#endif + +#ifdef HAVE_SYS_ACL_H +# include +#endif + + +#if 0 /* unused by xattr's atm. */ +static void xattrs__fd_get(struct tar_stat_info *st, + char const *file_name, int fd, const char *attr, + char **ret_ptr, size_t *ret_len) +{ +#ifdef HAVE_XATTRS + static ssize_t asz = 1024; + ssize_t ret = 0; + static char *val = NULL; + + if (!val) val = xmalloc (asz); + + while (((ret = fgetxattr (fd, attr, val, asz)) == -1) && + (errno == ERANGE)) + { + asz <<= 1; + val = xrealloc (val, asz); + } + + if (ret != -1) + { + *ret_ptr = xmemdup (val, ret + 1); + *ret_len = ret; + } + else if (errno != ENOATTR) + call_arg_warn ("fgetxattr", file_name); +#endif +} +#endif + +static void xattrs__acls_get_a(struct tar_stat_info *st, + char const *file_name, int fd, + char **ret_ptr, size_t *ret_len) +{ /* "system.posix_acl_access" */ +#ifdef HAVE_LIBACL + char *val = NULL; + ssize_t len; + acl_t acl; + + if (fd != -1) + { + if ((acl = acl_get_fd (fd)) == (acl_t)NULL) + { + if (errno != ENOTSUP) + call_arg_warn ("acl_get_fd", file_name); + return; + } + } + else if ((acl = acl_get_file (file_name, ACL_TYPE_ACCESS)) == (acl_t)NULL) + { + if (errno != ENOTSUP) + call_arg_warn ("acl_get_file", file_name); + return; + } + + + val = acl_to_text(acl, &len); + acl_free (acl); + + if (val == NULL) + { + call_arg_warn ("acl_to_text", file_name); + return; + } + + *ret_ptr = xstrdup (val); + *ret_len = len; + + acl_free (val); +#endif +} + +static void xattrs__acls_get_d(struct tar_stat_info *st, + char const *file_name, + char **ret_ptr, size_t *ret_len) +{ /* "system.posix_acl_default" */ +#ifdef HAVE_LIBACL + char *val = NULL; + ssize_t len; + acl_t acl; + + if ((acl = acl_get_file (file_name, ACL_TYPE_DEFAULT)) == (acl_t)NULL) + { + if (errno != ENOTSUP) + call_arg_warn ("acl_get_file", file_name); + return; + } + + val = acl_to_text(acl, &len); + acl_free (acl); + + if (val == NULL) + { + call_arg_warn ("acl_to_text", file_name); + return; + } + + *ret_ptr = xstrdup (val); + *ret_len = len; + + acl_free (val); +#endif +} + +void xattrs_acls_get(struct tar_stat_info *st, char const *file_name, int fd, + int xisfile) +{ + if (acls_option > 0) + { +#ifndef HAVE_LIBACL + static int done = 0; + if (!done) + WARN ((0, 0, _("ACL support requested, but not available"))); + done = 1; +#endif + xattrs__acls_get_a (st, file_name, fd, + &st->acls_a_ptr, &st->acls_a_len); + if (!xisfile) + xattrs__acls_get_d (st, file_name, + &st->acls_d_ptr, &st->acls_d_len); + } +} + +void xattrs_selinux_get(struct tar_stat_info *st, char const *file_name, int fd) +{ + if (selinux_context_option > 0) + { +#ifndef HAVE_LIBSELINUX + static int done = 0; + if (!done) + WARN ((0, 0, _("SELinux support requested, but not available"))); + done = 1; +#else + if (fd == -1) + { + if ((lgetfilecon (file_name, &st->cntx_name) == -1) && (errno != ENOTSUP) && (errno != ENODATA)) + call_arg_warn ("lgetfilecon", file_name); + } + else if ((fgetfilecon (fd, &st->cntx_name) == -1) && (errno != ENOTSUP) && (errno != ENODATA)) + call_arg_warn ("fgetfilecon", file_name); +#endif + } +} + +void xattrs_xattrs_get(struct tar_stat_info *st, char const *file_name, int fd) +{ + if (xattrs_option > 0) + { /* get all xattrs ... this include security.* and system.* if + available. We filter them here, but we have to filter them + in xattrs_xattrs_set() anyway. + */ + static ssize_t xsz = 1024; + static char *xatrs = NULL; + ssize_t xret = -1; + +#ifndef HAVE_XATTRS + static int done = 0; + if ((xattrs_option > 0) && !done) + WARN ((0, 0, _("Xattr support requested, but not available"))); + done = 1; +#else + + if (!xatrs) xatrs = xmalloc (xsz); + + while (((fd == -1) ? + ((xret = llistxattr (file_name, xatrs, xsz)) == -1) : + ((xret = flistxattr (fd, xatrs, xsz)) == -1)) && + (errno == ERANGE)) + { + xsz <<= 1; + xatrs = xrealloc (xatrs, xsz); + } + + if (xret == -1) + call_arg_warn ((fd == -1) ? "llistxattrs" : "flistxattrs", file_name); + else + { + const char *attr = xatrs; + static ssize_t asz = 1024; + static char *val = NULL; + + if (!val) val = xmalloc (asz); + + while (xret > 0) + { + size_t len = strlen (attr); + ssize_t aret = 0; + + /* Archive all xattrs during creation, decide at extraction time + * which ones are of interest/use for the target filesystem. */ + while (((fd == -1) ? + ((aret = lgetxattr (file_name, attr, val, asz)) == -1) : + ((aret = fgetxattr (fd, attr, val, asz)) == -1)) && + (errno == ERANGE)) + { + asz <<= 1; + val = xrealloc (val, asz); + } + + if (aret != -1) + xheader_xattr_add (st, attr, val, aret); + else if (errno != ENOATTR) + call_arg_warn ((fd==-1) ? "lgetxattr" : "fgetxattr", file_name); + + attr += len + 1; + xret -= len + 1; + } + } +#endif + } +} + +static void xattrs__fd_set(struct tar_stat_info const *st, + char const *file_name, char typeflag, + const char *attr, + const char *ptr, size_t len) +{ +#ifdef HAVE_XATTRS + if (ptr) + { + const char *sysname = "setxattr"; + int ret = -1; + + if (typeflag != SYMTYPE) + ret = setxattr (file_name, attr, ptr, len, 0); + else + { + sysname = "lsetxattr"; + ret = lsetxattr (file_name, attr, ptr, len, 0); + } + + /* do not print warnings when SELinux is disabled */ + if ((ret == -1) && (errno != EPERM) && (errno != ENOTSUP)) + call_arg_error(sysname, file_name); + } +#endif +} + +/* convert unix permissions into an ACL ... needed due to "default" ACLs */ +#ifdef HAVE_LIBACL +static acl_t perms2acl(int perms) +{ + char val[] = "user::---,group::---,other::---"; + /* 0123456789 123456789 123456789 123456789 */ + + /* user */ + if (perms & 0400) val[ 6] = 'r'; + if (perms & 0200) val[ 7] = 'w'; + if (perms & 0100) val[ 8] = 'x'; + + /* group */ + if (perms & 0040) val[17] = 'r'; + if (perms & 0020) val[18] = 'w'; + if (perms & 0010) val[19] = 'x'; + + /* other */ + if (perms & 0004) val[28] = 'r'; + if (perms & 0002) val[29] = 'w'; + if (perms & 0001) val[30] = 'x'; + + return (acl_from_text (val)); +} +#endif + +static char *skip_to_ext_fields(char *ptr) +{ + ptr += strcspn(ptr, ":,\n"); /* skip tag name. Ie. user/group/default/mask */ + + if (*ptr != ':') + return (ptr); /* error? no user/group field */ + ++ptr; + + ptr += strcspn(ptr, ":,\n"); /* skip user/group name */ + + if (*ptr != ':') + return (ptr); /* error? no perms field */ + ++ptr; + + ptr += strcspn(ptr, ":,\n"); /* skip perms */ + + if (*ptr != ':') + return (ptr); /* no extra fields */ + + return (ptr); +} + +/* The POSIX draft allows extra fields after the three main ones. Star + uses this to add a fourth field for user/group which is the numeric ID. + We just skip all extra fields atm. */ +static const char *fixup_extra_acl_fields(const char *ptr) +{ + char *src = (char *)ptr; + char *dst = (char *)ptr; + + while (*src) + { + const char *old = src; + size_t len = 0; + + src = skip_to_ext_fields(src); + len = src - old; + if (old != dst) memmove(dst, old, len); + dst += len; + + if (*src == ':') /* We have extra fields, skip them all */ + src += strcspn(src, "\n,"); + + if ((*src == '\n') || (*src == ',')) + *dst++ = *src++; /* also done when dst == src, but that's ok */ + } + if (src != dst) + *dst = 0; + + return ptr; +} + +static void xattrs__acls_set(struct tar_stat_info const *st, + char const *file_name, int type, + const char *ptr, size_t len) +{ /* "system.posix_acl_access" */ +#ifdef HAVE_LIBACL + acl_t acl; + + if (ptr) + { + /* assert (strlen (ptr) == len); */ + ptr = fixup_extra_acl_fields(ptr); + + acl = acl_from_text (ptr); + acls_option = 1; + } + else if (acls_option > 0) + acl = perms2acl (st->stat.st_mode); + else + return; /* don't call acl functions unless we first hit an ACL, or + --acls was passed explicitly */ + + if (acl == (acl_t)NULL) + { + call_arg_warn ("acl_from_text", file_name); + return; + } + + if (acl_set_file (file_name, type, acl) == -1) + { + if (errno != ENOTSUP) + call_arg_warn ("acl_set_file", file_name); + } + acl_free (acl); +#endif +} + +void xattrs_acls_set(struct tar_stat_info const *st, + char const *file_name, char typeflag) +{ + if ((acls_option >= 0) && (typeflag != SYMTYPE)) + { +#ifndef HAVE_LIBACL + static int done = 0; + if (!done) + WARN ((0, 0, _("ACL support requested, but not available"))); + done = 1; +#else + xattrs__acls_set (st, file_name, ACL_TYPE_ACCESS, + st->acls_a_ptr, st->acls_a_len); + if ((typeflag == DIRTYPE) || (typeflag == GNUTYPE_DUMPDIR)) + xattrs__acls_set (st, file_name, ACL_TYPE_DEFAULT, + st->acls_d_ptr, st->acls_d_len); +#endif + } +} + +void xattrs_selinux_set(struct tar_stat_info const *st, + char const *file_name, char typeflag) +{ + if ((selinux_context_option >= 0) && st->cntx_name) + { + const char *sysname = "setfilecon"; + int ret = -1; + +#ifndef HAVE_LIBSELINUX + static int done = 0; + if (!done) + WARN ((0, 0, _("SELinux support requested, but not available"))); + done = 1; +#else + if (typeflag != SYMTYPE) + ret = setfilecon (file_name, st->cntx_name); + else + { + sysname = "lsetfilecon"; + ret = lsetfilecon (file_name, st->cntx_name); + } + + if ((ret == -1) && (errno == EPERM)) + call_arg_warn(sysname, file_name); + else if ((ret == -1) && (errno != EOPNOTSUPP)) + call_arg_error(sysname, file_name); +#endif + } +} + +void xattrs_xattrs_set(struct tar_stat_info const *st, + char const *file_name, char typeflag) +{ + if ((xattrs_option >= 0) && st->xattr_map_size) + { + size_t scan = 0; + +#ifndef HAVE_XATTRS + static int done = 0; + if (!done) + WARN ((0, 0, _("Xattr support requested, but not available"))); + done = 1; +#else + while (scan < st->xattr_map_size) + { + char *keyword = st->xattr_map[scan].xkey; + + /* assert (!memcpy (keyword, "SCHILY.xattr.", strlen("SCHILY.xattr."))); */ + keyword += strlen("SCHILY.xattr."); + + if (strncmp (keyword, "user.", strlen("user.")) && + strncmp (keyword, "lustre.", strlen("lustre.")) && + strncmp (keyword, "trusted.", strlen("trusted.")) && + strncmp (keyword, "security.NTACL", strlen("security.NTACL"))) + continue; /* don't try and set anything but normal xattrs */ + + xattrs__fd_set (st, file_name, typeflag, keyword, + st->xattr_map[scan].xval_ptr, + st->xattr_map[scan].xval_len); + + ++scan; + } +#endif + } +} diff -Naur tar-1.26.orig//src/xattrs.h tar-1.26/src/xattrs.h --- tar-1.26.orig//src/xattrs.h 1969-12-31 19:00:00.000000000 -0500 +++ tar-1.26/src/xattrs.h 2011-09-06 13:22:06.000000000 -0400 @@ -0,0 +1,14 @@ + +extern void xattrs_acls_get(struct tar_stat_info *st, + char const *file_name, int fd, int xisfile); +extern void xattrs_selinux_get(struct tar_stat_info *st, + char const *file_name, int fd); +extern void xattrs_xattrs_get(struct tar_stat_info *st, + char const *file_name, int fd); + +extern void xattrs_acls_set(struct tar_stat_info const *st, + char const *file_name, char typeflag); +extern void xattrs_selinux_set(struct tar_stat_info const *st, + char const *file_name, char typeflag); +extern void xattrs_xattrs_set(struct tar_stat_info const *st, + char const *file_name, char typeflag); diff -Naur tar-1.26.orig//src/xheader.c tar-1.26/src/xheader.c --- tar-1.26.orig//src/xheader.c 2010-11-27 06:18:49.000000000 -0500 +++ tar-1.26/src/xheader.c 2011-09-06 13:22:06.000000000 -0400 @@ -460,6 +460,74 @@ } } +void xheader_xattr_init(struct tar_stat_info *st) +{ + st->xattr_map = NULL; + st->xattr_map_size = 0; +} + +void xheader_xattr_free(struct xattr_array *xattr_map, size_t xattr_map_size) +{ + size_t scan = 0; + + while (scan < xattr_map_size) + { + free (xattr_map[scan].xkey); + free (xattr_map[scan].xval_ptr); + + ++scan; + } + free (xattr_map); +} + +static void xheader_xattr__add(struct xattr_array **xattr_map, + size_t *xattr_map_size, + const char *key, const char *val, size_t len) +{ + size_t pos = (*xattr_map_size)++; + + *xattr_map = xrealloc (*xattr_map, + *xattr_map_size * sizeof(struct xattr_array)); + (*xattr_map)[pos].xkey = xstrdup (key); + (*xattr_map)[pos].xval_ptr = xmemdup (val, len + 1); + (*xattr_map)[pos].xval_len = len; +} + +void xheader_xattr_add(struct tar_stat_info *st, + const char *key, const char *val, size_t len) +{ + size_t klen = strlen (key); + char *xkey = xmalloc (strlen("SCHILY.xattr.") + klen + 1); + char *tmp = xkey; + + tmp = stpcpy (tmp, "SCHILY.xattr."); + tmp = stpcpy (tmp, key); + + xheader_xattr__add (&st->xattr_map, &st->xattr_map_size, xkey, val, len); + + free (xkey); +} + +void xheader_xattr_copy(const struct tar_stat_info *st, + struct xattr_array **xattr_map, size_t *xattr_map_size) +{ + size_t scan = 0; + + *xattr_map = NULL; + *xattr_map_size = 0; + + while (scan < st->xattr_map_size) + { + char *key = st->xattr_map[scan].xkey; + char *val = st->xattr_map[scan].xval_ptr; + size_t len = st->xattr_map[scan].xval_len; + + xheader_xattr__add(xattr_map, xattr_map_size, key, val, len); + + ++scan; + } +} + /* General Interface */ @@ -473,6 +541,7 @@ struct xheader *, void const *data); void (*decoder) (struct tar_stat_info *, char const *, char const *, size_t); int flags; + bool prefix; }; /* This declaration must be extern, because ISO C99 section 6.9.2 @@ -489,8 +558,17 @@ struct xhdr_tab const *p; for (p = xhdr_tab; p->keyword; p++) - if (strcmp (p->keyword, keyword) == 0) - return p; + if (p->prefix) + { + if (strncmp (p->keyword, keyword, strlen(p->keyword)) == 0) + return p; + } + else + { + if (strcmp (p->keyword, keyword) == 0) + return p; + } + return NULL; } @@ -500,7 +578,7 @@ struct xhdr_tab const *p; for (p = xhdr_tab; p->keyword; p++) - if ((p->flags & XHDR_PROTECTED) && fnmatch (pattern, p->keyword, 0) == 0) + if (!p->prefix && (p->flags & XHDR_PROTECTED) && fnmatch (pattern, p->keyword, 0) == 0) return true; return false; } @@ -511,7 +589,7 @@ struct xhdr_tab const *p; for (p = xhdr_tab; p->keyword; p++) - if ((p->flags & XHDR_PROTECTED) && strcmp (p->keyword, keyword) == 0) + if (!p->prefix && (p->flags & XHDR_PROTECTED) && strcmp (p->keyword, keyword) == 0) return true; return false; } @@ -1470,6 +1548,71 @@ } static void +xattr_selinux_coder (struct tar_stat_info const *st, char const *keyword, + struct xheader *xhdr, void const *data) +{ + code_string (st->cntx_name, keyword, xhdr); +} + +static void +xattr_selinux_decoder (struct tar_stat_info *st, + char const *keyword, char const *arg, size_t size) +{ + decode_string (&st->cntx_name, arg); +} + +static void +xattr_acls_a_coder (struct tar_stat_info const *st , char const *keyword, + struct xheader *xhdr, void const *data) +{ + xheader_print_n (xhdr, keyword, st->acls_a_ptr, st->acls_a_len); +} + +static void +xattr_acls_a_decoder (struct tar_stat_info *st, + char const *keyword, char const *arg, size_t size) +{ + st->acls_a_ptr = xmemdup (arg, size + 1); + st->acls_a_len = size; +} + +static void +xattr_acls_d_coder (struct tar_stat_info const *st , char const *keyword, + struct xheader *xhdr, void const *data) +{ + xheader_print_n (xhdr, keyword, st->acls_d_ptr, st->acls_d_len); +} + +static void +xattr_acls_d_decoder (struct tar_stat_info *st, + char const *keyword, char const *arg, size_t size) +{ + st->acls_d_ptr = xmemdup (arg, size + 1); + st->acls_d_len = size; +} + +static void +xattr_coder (struct tar_stat_info const *st , char const *keyword, + struct xheader *xhdr, void const *data) +{ + struct xattr_array *xattr_map = st->xattr_map; + const size_t *off = data; + xheader_print_n (xhdr, keyword, + xattr_map[*off].xval_ptr, xattr_map[*off].xval_len); +} + +static void +xattr_decoder (struct tar_stat_info *st, + char const *keyword, char const *arg, size_t size) +{ + char *xstr = NULL; + + xstr = xmemdup(arg, size + 1); + xheader_xattr_add(st, keyword + strlen("SCHILY.xattr."), xstr, size); + free(xstr); +} + +static void sparse_major_coder (struct tar_stat_info const *st, char const *keyword, struct xheader *xhdr, void const *data) { @@ -1506,53 +1649,53 @@ } struct xhdr_tab const xhdr_tab[] = { - { "atime", atime_coder, atime_decoder, 0 }, - { "comment", dummy_coder, dummy_decoder, 0 }, - { "charset", dummy_coder, dummy_decoder, 0 }, - { "ctime", ctime_coder, ctime_decoder, 0 }, - { "gid", gid_coder, gid_decoder, 0 }, - { "gname", gname_coder, gname_decoder, 0 }, - { "linkpath", linkpath_coder, linkpath_decoder, 0 }, - { "mtime", mtime_coder, mtime_decoder, 0 }, - { "path", path_coder, path_decoder, 0 }, - { "size", size_coder, size_decoder, 0 }, - { "uid", uid_coder, uid_decoder, 0 }, - { "uname", uname_coder, uname_decoder, 0 }, + { "atime", atime_coder, atime_decoder, 0, false }, + { "comment", dummy_coder, dummy_decoder, 0, false }, + { "charset", dummy_coder, dummy_decoder, 0, false }, + { "ctime", ctime_coder, ctime_decoder, 0, false }, + { "gid", gid_coder, gid_decoder, 0, false }, + { "gname", gname_coder, gname_decoder, 0, false }, + { "linkpath", linkpath_coder, linkpath_decoder, 0, false }, + { "mtime", mtime_coder, mtime_decoder, 0, false }, + { "path", path_coder, path_decoder, 0, false }, + { "size", size_coder, size_decoder, 0, false }, + { "uid", uid_coder, uid_decoder, 0, false }, + { "uname", uname_coder, uname_decoder, 0, false }, /* Sparse file handling */ { "GNU.sparse.name", path_coder, path_decoder, - XHDR_PROTECTED }, + XHDR_PROTECTED, false }, { "GNU.sparse.major", sparse_major_coder, sparse_major_decoder, - XHDR_PROTECTED }, + XHDR_PROTECTED, false }, { "GNU.sparse.minor", sparse_minor_coder, sparse_minor_decoder, - XHDR_PROTECTED }, + XHDR_PROTECTED, false }, { "GNU.sparse.realsize", sparse_size_coder, sparse_size_decoder, - XHDR_PROTECTED }, + XHDR_PROTECTED, false }, { "GNU.sparse.numblocks", sparse_numblocks_coder, sparse_numblocks_decoder, - XHDR_PROTECTED }, + XHDR_PROTECTED, false }, /* tar 1.14 - 1.15.90 keywords. */ { "GNU.sparse.size", sparse_size_coder, sparse_size_decoder, - XHDR_PROTECTED }, + XHDR_PROTECTED, false }, /* tar 1.14 - 1.15.1 keywords. Multiple instances of these appeared in 'x' headers, and each of them was meaningful. It confilcted with POSIX specs, which requires that "when extended header records conflict, the last one given in the header shall take precedence." */ { "GNU.sparse.offset", sparse_offset_coder, sparse_offset_decoder, - XHDR_PROTECTED }, + XHDR_PROTECTED, false }, { "GNU.sparse.numbytes", sparse_numbytes_coder, sparse_numbytes_decoder, - XHDR_PROTECTED }, + XHDR_PROTECTED, false }, /* tar 1.15.90 keyword, introduced to remove the above-mentioned conflict. */ { "GNU.sparse.map", NULL /* Unused, see pax_dump_header() */, - sparse_map_decoder, 0 }, + sparse_map_decoder, 0, false }, { "GNU.dumpdir", dumpdir_coder, dumpdir_decoder, - XHDR_PROTECTED }, + XHDR_PROTECTED, false }, /* Keeps the tape/volume label. May be present only in the global headers. Equivalent to GNUTYPE_VOLHDR. */ { "GNU.volume.label", volume_label_coder, volume_label_decoder, - XHDR_PROTECTED | XHDR_GLOBAL }, + XHDR_PROTECTED | XHDR_GLOBAL, false }, /* These may be present in a first global header of the archive. They provide the same functionality as GNUTYPE_MULTIVOL header. @@ -1561,11 +1704,41 @@ GNU.volume.offset keeps the offset of the start of this volume, otherwise kept in oldgnu_header.offset. */ { "GNU.volume.filename", volume_label_coder, volume_filename_decoder, - XHDR_PROTECTED | XHDR_GLOBAL }, + XHDR_PROTECTED | XHDR_GLOBAL, false }, { "GNU.volume.size", volume_size_coder, volume_size_decoder, - XHDR_PROTECTED | XHDR_GLOBAL }, + XHDR_PROTECTED | XHDR_GLOBAL, false }, { "GNU.volume.offset", volume_offset_coder, volume_offset_decoder, - XHDR_PROTECTED | XHDR_GLOBAL }, + XHDR_PROTECTED | XHDR_GLOBAL, false }, + + /* We get the SELinux value from filecon, so add a namespace for SELinux + instead of storing it in SCHILY.xattr.* (which would be RAW). */ + { "RHT.security.selinux", + xattr_selinux_coder, xattr_selinux_decoder, 0, false }, + + /* ACLs, use the star format... */ + { "SCHILY.acl.access", + xattr_acls_a_coder, xattr_acls_a_decoder, 0, false }, + + { "SCHILY.acl.default", + xattr_acls_d_coder, xattr_acls_d_decoder, 0, false }, + + /* FIXME: These are compat. for FC-6 ... we shipped a tar using the generic + header names by accident. */ + { "SCHILY.xattr.security.selinux", + xattr_selinux_coder, xattr_selinux_decoder, 0, false }, + { "SCHILY.xattr.system.posix_acl_access", + xattr_acls_a_coder, xattr_acls_a_decoder, 0, false }, + { "SCHILY.xattr.system.posix_acl_default", + xattr_acls_d_coder, xattr_acls_d_decoder, 0, false }, + + /* xattrs use the star format. note we only save some variants... */ + { "SCHILY.xattr.user", xattr_coder, xattr_decoder, 0, true }, + { "SCHILY.xattr.trusted", xattr_coder, xattr_decoder, 0, true }, + { "SCHILY.xattr.lustre", xattr_coder, xattr_decoder, 0, true }, + { "SCHILY.xattr.security.NTACL", xattr_coder, xattr_decoder, 0, true }, + + /* ignore everything else in the xattr namespaces... */ + { "SCHILY.xattr", dummy_coder, dummy_decoder, 0, true }, - { NULL, NULL, NULL, 0 } + { NULL, NULL, NULL, 0, false } };