Dmitry Ivankov <[email protected]> ha escrit: > appears as environment variable > "TAR_HEADER_ORG.project.property" which is incorrect name. I believe > there are also other forbidden symbols (except '.') to use in a header > keyword name.
Yes, you are right. Replacing dots with some other character does not seem a feasible solution, so it would be better to revamp the implementation as in the attached patch (don't forget to revert the previous one before applying it). Regards, Sergey
>From 27d8eee06203d37da741d85c4c42ad5cb78384d8 Mon Sep 17 00:00:00 2001 From: Sergey Poznyakoff <[email protected]> Date: Tue, 25 Feb 2014 21:13:38 +0200 Subject: [PATCH] Pass extended global headers to the environment of --to-command and --checkpoint * NEWS: Update. * doc/tar.1: Document TAR_HEADER_ envars. * doc/tar.texi: Likewise. * src/common.h (xheader_iterate_global): New prototype. * src/system.c (stat_to_env): Call xheader_iterate_global to pass global headers to the environment. * src/xheader.c (xheader_iterate_global): New function. --- NEWS | 13 +++++++++++-- doc/tar.1 | 15 +++++++++++++-- doc/tar.texi | 38 ++++++++++++++++++++++++++++++++++++++ src/common.h | 2 ++ src/system.c | 19 +++++++++++++++++++ src/xheader.c | 11 +++++++++++ 6 files changed, 94 insertions(+), 4 deletions(-) diff --git a/NEWS b/NEWS index fa24142..67d53a3 100644 --- a/NEWS +++ b/NEWS @@ -1,4 +1,4 @@ -GNU tar NEWS - User visible changes. 2014-02-21 +GNU tar NEWS - User visible changes. 2014-02-25 Please send GNU tar bug reports to <[email protected]> @@ -67,7 +67,16 @@ speed up archivation. This release includes official tar(1) and rmt(8) manpages. Distribution maintainers are kindly asked to use these instead of the home-made pages they provided so far. - + +* Environment variables for --to-command and --checkpoint options + +If the archive being processed is a POSIX.1-2001 archive with global +extended headers, the number of these headers is passed in the +environment variable TAR_HEADER_COUNT. The name and value of each +header are passed in variables TAR_HEADER_NAME_<n> and +TAR_HEADER_VALUE_<n>, correspondingly, where <n> is the ordinal +number of that header (starting at 0). + version 1.27.1 - Sergey Poznyakoff, 2013-11-17 diff --git a/doc/tar.1 b/doc/tar.1 index cd133d9..ef0315c 100644 --- a/doc/tar.1 +++ b/doc/tar.1 @@ -13,7 +13,7 @@ .\" .\" You should have received a copy of the GNU General Public License .\" along with this program. If not, see <http://www.gnu.org/licenses/>. -.TH TAR 1 "February 22, 2014" "TAR" "GNU TAR Manual" +.TH TAR 1 "February 25, 2014" "TAR" "GNU TAR Manual" .SH NAME tar \- an archiving utility .SH SYNOPSIS @@ -448,9 +448,20 @@ Format of the archive being processed. One of: .BR posix , .BR ustar , .BR v7 . +.TP .B TAR_SUBCOMMAND A short option (with a leading dash) describing the operation \fBtar\fR is -executing. +executing. +.TP +\fBTAR_HEADER_COUNT +Number of global extended headers in the archive (only for +POSIX.1-2001 archives). +.TP +\fBTAR_HEADER_NAME_\fIN\fR +Name of the \fIN\fRth extended header (\fIN\fR starts at 0). +.TP +\fBTAR_HEADER_VALUE_\fIN\fR +Value of the \fIN\fRth extended header. .RE .SS Handling of file attributes .TP diff --git a/doc/tar.texi b/doc/tar.texi index e3df0c9..c8d521b 100644 --- a/doc/tar.texi +++ b/doc/tar.texi @@ -4196,6 +4196,25 @@ Format of the archive being processed. @xref{Formats}, for a complete list of archive format names. @end table +@vrindex TAR_HEADER_COUNT, checkpoint script environment +If the archive being processed is a POSIX.1-2001 archive +(@pxref{posix}) with global extended headers, these headers +are passed to the environment in the following way. The +variable @env{TAR_HEADER_COUNT} is set to the total +number of headers. The headers are assigned ordinal numbers +starting from 0. Then, two environment variables are +created for each header (@var{n} stands for the header ordinal +number): + +@table @env +@vrindex TAR_HEADER_NAME_@var{n}, checkpoint script environment +@item TAR_HEADER_NAME_@var{n} +The name of the @var{n}th variable. +@vrindex TAR_HEADER_VALUE_@var{n}, checkpoint script environment +@item TAR_HEADER_VALUE_@var{n} +The value of the @var{n}th variable. +@end table + These environment variables can also be passed as arguments to the command, provided that they are properly escaped, for example: @@ -5778,6 +5797,25 @@ Format of the archive being processed. @xref{Formats}, for a complete list of archive format names. @end table +@vrindex TAR_HEADER_COUNT, checkpoint script environment +If the archive being processed is a POSIX.1-2001 archive +(@pxref{posix}) with global extended headers, these headers +are passed to the environment in the following way. The +variable @env{TAR_HEADER_COUNT} is set to the total +number of headers. The headers are assigned ordinal numbers +starting from 0. Then, two environment variables are +created for each header (@var{n} stands for the header ordinal +number): + +@table @env +@vrindex TAR_HEADER_NAME_@var{n}, to-command environment +@item TAR_HEADER_NAME_@var{n} +The name of the @var{n}th variable. +@vrindex TAR_HEADER_VALUE_@var{n}, to-command environment +@item TAR_HEADER_VALUE_@var{n} +The value of the @var{n}th variable. +@end table + These variables are defined prior to executing the command, so you can pass them as arguments, if you prefer. For example, if the command @var{proc} takes the member name and size as its arguments, then you diff --git a/src/common.h b/src/common.h index edf787c..c11db69 100644 --- a/src/common.h +++ b/src/common.h @@ -784,6 +784,8 @@ void update_archive (void); void xheader_decode (struct tar_stat_info *stat); void xheader_decode_global (struct xheader *xhdr); +void xheader_iterate_global (int (*fn) (const char *, const char *, void *), + void *); void xheader_store (char const *keyword, struct tar_stat_info *st, void const *data); void xheader_read (struct xheader *xhdr, union block *header, off_t size); diff --git a/src/system.c b/src/system.c index 9414233..d21aa47 100644 --- a/src/system.c +++ b/src/system.c @@ -672,9 +672,25 @@ chr_to_env (char const *envar, char c) xalloc_die (); } +static int +hdr_to_env (const char *name, const char *value, void *data) +{ + unsigned long *n = data; + char *var = xasprintf ("TAR_HEADER_NAME_%lu", *n); + str_to_env (var, name); + free (var); + var = xasprintf ("TAR_HEADER_VALUE_%lu", *n); + str_to_env (var, value); + free (var); + ++*n; + return 0; +} + static void stat_to_env (char *name, char type, struct tar_stat_info *st) { + unsigned long hc = 0; + str_to_env ("TAR_VERSION", PACKAGE_VERSION); str_to_env ("TAR_ARCHIVE", *archive_name_cursor); dec_to_env ("TAR_VOLUME", archive_name_cursor - archive_name_array + 1); @@ -695,6 +711,9 @@ stat_to_env (char *name, char type, struct tar_stat_info *st) dec_to_env ("TAR_UID", st->stat.st_uid); dec_to_env ("TAR_GID", st->stat.st_gid); + xheader_iterate_global (hdr_to_env, &hc); + dec_to_env ("TAR_HEADER_COUNT", hc); + switch (type) { case 'b': diff --git a/src/xheader.c b/src/xheader.c index c94c6d3..3858126 100644 --- a/src/xheader.c +++ b/src/xheader.c @@ -782,6 +782,17 @@ xheader_decode_global (struct xheader *xhdr) } } +void +xheader_iterate_global (int (*fn) (const char *, const char *, void *), + void *data) +{ + struct keyword_list *kp; + for (kp = global_header_override_list; kp; kp = kp->next) + if (fn (kp->pattern, kp->value, data)) + break; +} + + static void xheader_init (struct xheader *xhdr) { -- 1.7.12.1
