[PATCH] Retain timestamp with gunzip

Ian Wienand ianw at vmware.com
Fri Oct 31 22:36:59 UTC 2008


Thanks for the review

On Thu, Oct 30, 2008 at 05:41:40PM -0700, Denys Vlasenko wrote:
> On Thursday 30 October 2008 00:43, Ian Wienand wrote:
> Please do make it a struct (one-member for now) so that
> we don't need to re-shuffle this stuff *again*.
> Let's call it "struct unpack_misc_info"?

I shortened that to unpack_info

Other changes

 - I don't think bbunpack() belongs as a generic in libbb.h (also, it
   means libbb.h has to drag in a declaration for unpack_info).

 - I kept it so the stream functions remained the same, and gzip has
   an extra unpack_gz_stream_with_info() fn

 - it's a little strange bzip and gzip use bbunpck() to *pack*.  they
   get an unpack_info argument despite not unpacking.
 
> > -USE_DESKTOP(long long) int unpack_bunzip2(void)
> > +USE_DESKTOP(long long) int unpack_bunzip2(time_t *mtime)
> >  {
> > +     *mtime = 0; // not handled yet
> 
> Don't do it. Assume that it is suitably initialized
> (say, memset to 0) and only fill in values if they differ
> from that.

Ok

> >               magic2 = xread_char(STDIN_FILENO);
> >               if (ENABLE_FEATURE_SEAMLESS_Z && magic2 == 0x9d) {
> > +                     mtime = NULL;

> Why do you set mtime = NULL?

Old code, gone.

> > -static int check_header_gzip(STATE_PARAM_ONLY)
> > +static int check_header_gzip(STATE_PARAM time_t *mtime)
> >  {
> >       union {
> >               unsigned char raw[8];
> >               struct {
> >                       uint8_t gz_method;
> >                       uint8_t flags;
> > -                     //uint32_t mtime; - unused fields
> > -                     //uint8_t xtra_flags;
> > -                     //uint8_t os_flags;
> > -             } formatted; /* packed */
> > +                     uint32_t mtime;
> > +                     uint8_t xtra_flags;
> > +                     uint8_t os_flags;
> > +             } __attribute__((packed)) formatted;
> >       } header;
> 
> Why did you uncomment the other two?

It's grabbing 8 bytes packed either way, just looked a bit neater

-i

Index: archival/gzip.c
===================================================================
--- archival/gzip.c	(revision 23853)
+++ archival/gzip.c	(working copy)
@@ -40,6 +40,7 @@
 */
 
 #include "libbb.h"
+#include "unarchive.h"
 
 
 /* ===========================================================================
@@ -2014,7 +2015,7 @@
 }
 
 static
-USE_DESKTOP(long long) int pack_gzip(void)
+USE_DESKTOP(long long) int pack_gzip(unpack_info_t *info UNUSED_PARAM)
 {
 	struct stat s;
 
Index: archival/bbunzip.c
===================================================================
--- archival/bbunzip.c	(revision 23853)
+++ archival/bbunzip.c	(working copy)
@@ -30,13 +30,14 @@
 
 int FAST_FUNC bbunpack(char **argv,
 	char* (*make_new_name)(char *filename),
-	USE_DESKTOP(long long) int (*unpacker)(void)
+	USE_DESKTOP(long long) int (*unpacker)(unpack_info_t *info)
 )
 {
 	struct stat stat_buf;
 	USE_DESKTOP(long long) int status;
 	char *filename, *new_name;
 	smallint exitcode = 0;
+	unpack_info_t info = { .mtime = 0 };
 
 	do {
 		/* NB: new_name is *maybe* malloc'ed! */
@@ -92,14 +93,25 @@
 					"use -f to force it");
 		}
 
-		status = unpacker();
+		status = unpacker(&info);
 		if (status < 0)
 			exitcode = 1;
 
 		if (filename) {
 			char *del = new_name;
 			if (status >= 0) {
-				/* TODO: restore user/group/times here? */
+				/* TODO: restore other things? */
+				if (info.mtime > 0) {
+					struct utimbuf times;
+					/* Done with this.  On some
+					systems calling utime then
+					closing resets the mtime. */
+					close(STDOUT_FILENO);
+					times.actime = info.mtime;
+					times.modtime = info.mtime;
+					utime(new_name, &times); // don't worry about errors
+				}
+
 				/* Delete _compressed_ file */
 				del = filename;
 				/* restore extension (unless tgz -> tar case) */
@@ -159,7 +171,7 @@
 }
 
 static
-USE_DESKTOP(long long) int unpack_bunzip2(void)
+USE_DESKTOP(long long) int unpack_bunzip2(unpack_info_t *info UNUSED_PARAM)
 {
 	return unpack_bz2_stream_prime(STDIN_FILENO, STDOUT_FILENO);
 }
@@ -235,7 +247,7 @@
 }
 
 static
-USE_DESKTOP(long long) int unpack_gunzip(void)
+USE_DESKTOP(long long) int unpack_gunzip(unpack_info_t *info)
 {
 	USE_DESKTOP(long long) int status = -1;
 
@@ -247,7 +259,7 @@
 		if (ENABLE_FEATURE_SEAMLESS_Z && magic2 == 0x9d) {
 			status = unpack_Z_stream(STDIN_FILENO, STDOUT_FILENO);
 		} else if (magic2 == 0x8b) {
-			status = unpack_gz_stream(STDIN_FILENO, STDOUT_FILENO);
+			status = unpack_gz_stream_with_info(STDIN_FILENO, STDOUT_FILENO, info);
 		} else {
 			goto bad_magic;
 		}
@@ -309,7 +321,7 @@
 }
 
 static
-USE_DESKTOP(long long) int unpack_unlzma(void)
+USE_DESKTOP(long long) int unpack_unlzma(unpack_info_t *info UNUSED_PARAM)
 {
 	return unpack_lzma_stream(STDIN_FILENO, STDOUT_FILENO);
 }
@@ -344,7 +356,7 @@
 }
 
 static
-USE_DESKTOP(long long) int unpack_uncompress(void)
+USE_DESKTOP(long long) int unpack_uncompress(unpack_info_t *info UNUSED_PARAM)
 {
 	USE_DESKTOP(long long) int status = -1;
 
Index: archival/bzip2.c
===================================================================
--- archival/bzip2.c	(revision 23853)
+++ archival/bzip2.c	(working copy)
@@ -8,6 +8,7 @@
  */
 
 #include "libbb.h"
+#include "unarchive.h"
 
 #define CONFIG_BZIP2_FEATURE_SPEED 1
 
@@ -101,7 +102,7 @@
 }
 
 static
-USE_DESKTOP(long long) int compressStream(void)
+USE_DESKTOP(long long) int compressStream(unpack_info_t *info UNUSED_PARAM)
 {
 	USE_DESKTOP(long long) int total;
 	ssize_t count;
Index: archival/libunarchive/decompress_unzip.c
===================================================================
--- archival/libunarchive/decompress_unzip.c	(revision 23853)
+++ archival/libunarchive/decompress_unzip.c	(working copy)
@@ -1108,17 +1108,17 @@
 	return res;
 }
 
-static int check_header_gzip(STATE_PARAM_ONLY)
+static int check_header_gzip(STATE_PARAM unpack_info_t *info)
 {
 	union {
 		unsigned char raw[8];
 		struct {
 			uint8_t gz_method;
 			uint8_t flags;
-			//uint32_t mtime; - unused fields
-			//uint8_t xtra_flags;
-			//uint8_t os_flags;
-		} formatted; /* packed */
+			uint32_t mtime;
+			uint8_t xtra_flags;
+			uint8_t os_flags;
+		} __attribute__((packed)) formatted;
 	} header;
 
 	/*
@@ -1167,6 +1167,15 @@
 		}
 	}
 
+	if (info) {
+#if BB_LITTLE_ENDIAN
+		/* gzip data always in le */
+		header.formatted.mtime =
+			SWAP_LE32(header.formatted.mtime);
+#endif
+		info->mtime = header.formatted.mtime;
+	}
+
 	/* Read the header checksum */
 	if (header.formatted.flags & 0x02) {
 		if (!top_up(PASS_STATE 2))
@@ -1177,7 +1186,7 @@
 }
 
 USE_DESKTOP(long long) int FAST_FUNC
-unpack_gz_stream(int in, int out)
+unpack_gz_stream_with_info(int in, int out, unpack_info_t *info)
 {
 	uint32_t v32;
 	USE_DESKTOP(long long) int n;
@@ -1192,7 +1201,7 @@
 	gunzip_src_fd = in;
 
  again:
-	if (!check_header_gzip(PASS_STATE_ONLY)) {
+	if (!check_header_gzip(PASS_STATE info)) {
 		bb_error_msg("corrupted data");
 		n = -1;
 		goto ret;
@@ -1239,3 +1248,9 @@
 	DEALLOC_STATE;
 	return n;
 }
+
+USE_DESKTOP(long long) int FAST_FUNC
+unpack_gz_stream(int in, int out)
+{
+	return unpack_gz_stream_with_info(in, out, NULL);
+}
Index: include/libbb.h
===================================================================
--- include/libbb.h	(revision 23853)
+++ include/libbb.h	(working copy)
@@ -912,10 +912,7 @@
 /* Don't need USE_xxx() guard for these */
 int gunzip_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 int bunzip2_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
-int bbunpack(char **argv,
-	char* (*make_new_name)(char *filename),
-	USE_DESKTOP(long long) int (*unpacker)(void)
-) FAST_FUNC;
+
 #if ENABLE_ROUTE
 void bb_displayroutes(int noresolve, int netstatfmt) FAST_FUNC;
 #endif
Index: include/unarchive.h
===================================================================
--- include/unarchive.h	(revision 23853)
+++ include/unarchive.h	(working copy)
@@ -77,6 +77,12 @@
 } archive_handle_t;
 
 
+/* Info struct unpackers can fill out to inform users of thing like
+ * timestamps of unpacked files */
+typedef struct unpack_info_t {
+	time_t mtime;
+} unpack_info_t;
+
 extern archive_handle_t *init_handle(void) FAST_FUNC;
 
 extern char filter_accept_all(archive_handle_t *archive_handle) FAST_FUNC;
@@ -126,10 +132,15 @@
 /* the rest wants 2 first bytes already skipped by the caller */
 USE_DESKTOP(long long) int unpack_bz2_stream(int src_fd, int dst_fd) FAST_FUNC;
 USE_DESKTOP(long long) int unpack_gz_stream(int src_fd, int dst_fd) FAST_FUNC;
+USE_DESKTOP(long long) int unpack_gz_stream_with_info(int src_fd, int dst_fd, unpack_info_t *info) FAST_FUNC;
 USE_DESKTOP(long long) int unpack_Z_stream(int fd_in, int fd_out) FAST_FUNC;
 /* wrapper which checks first two bytes to be "BZ" */
 USE_DESKTOP(long long) int unpack_bz2_stream_prime(int src_fd, int dst_fd) FAST_FUNC;
 
+int bbunpack(char **argv,
+	     char* (*make_new_name)(char *filename),
+	     USE_DESKTOP(long long) int (*unpacker)(unpack_info_t *info)) FAST_FUNC;
+
 #if BB_MMU
 void open_transformer(int fd,
 	USE_DESKTOP(long long) int FAST_FUNC (*transformer)(int src_fd, int dst_fd)) FAST_FUNC;



More information about the busybox mailing list