9front - general discussion about 9front
 help / color / mirror / Atom feed
* Re: [9front] tar: write directory metadata after file extraction
@ 2019-11-03 15:35 rgl
  0 siblings, 0 replies; only message in thread
From: rgl @ 2019-11-03 15:35 UTC (permalink / raw)
  To: 9front

[-- Attachment #1: Type: text/plain, Size: 77 bytes --]

ok, here's the corrected version to keep the original directory mode intact.

[-- Attachment #2: Type: text/plain, Size: 2334 bytes --]

diff -r fe7fc19b1e64 sys/src/cmd/tar.c
--- a/sys/src/cmd/tar.c	Sun Nov 03 15:20:57 2019 +0100
+++ b/sys/src/cmd/tar.c	Sun Nov 03 16:31:09 2019 +0100
@@ -129,6 +129,13 @@
 	int	open;
 } Pushstate;
 
+typedef struct {
+	Hdr	*hp;
+	int	fd;
+	long	mtime;
+	ulong	mode;
+} Leftover;
+
 #define OTHER(rdwr) ((rdwr) == Rd? Wr: Rd)
 
 static int fixednblock;
@@ -153,6 +160,9 @@
 static Hdr *tpblk, *endblk;
 static Hdr *curblk;
 
+static Leftover *scraps;
+static int nscrap;
+
 static void
 usage(void)
 {
@@ -238,6 +248,40 @@
 	return docompress? comps: nil;
 }
 
+/* leftover directories management */
+
+static void wrmeta(int, Hdr*, long, int);
+
+static void
+pushleftover(Hdr *hp, int fd, long mtime, ulong mode)
+{
+	Leftover *l;
+
+	/* looking for a better alloc strategy */
+	scraps = realloc(scraps, ++nscrap*sizeof(Leftover));
+	if(scraps == nil)
+		sysfatal("realloc: %r");
+	l = scraps+(nscrap-1);
+	l->hp = hp;
+	l->fd = fd;
+	l->mtime = mtime;
+	l->mode = mode;
+}
+
+static void
+flushleftovers(void)
+{
+	Leftover *l;
+
+	while(nscrap-- > 0){
+		l = scraps+nscrap;
+		wrmeta(l->fd, l->hp, l->mtime, l->mode);
+		close(l->fd);
+	}
+	free(scraps);
+	scraps = nil;
+}
+
 /*
  * push a filter, cmd, onto fd.  if input, it's an input descriptor.
  * returns a descriptor to replace fd, or -1 on error.
@@ -1145,11 +1189,12 @@
 {
 	int fd = -1, dir = 0;
 	long mtime = strtol(hp->mtime, nil, 8);
-	ulong mode = strtoul(hp->mode, nil, 8) & 0777;
+	ulong mode0 = strtoul(hp->mode, nil, 8) & 0777, mode;
 	Off bytes = hdrsize(hp);		/* for printing */
 	ulong blksleft = BYTES2TBLKS(arsize(hp));
 
 	/* fiddle name, figure out mode and blocks */
+	mode = mode0;
 	if (isdir(hp)) {
 		mode |= DMDIR|0700;
 		dir = 1;
@@ -1182,14 +1227,14 @@
 
 	copyfromar(ar, fd, fname, blksleft, bytes);
 
-	/* touch up meta data and close */
+	/* touch up meta data and close, dirs are saved for later */
 	if (fd >= 0) {
-		/*
-		 * directories should be wstated *after* we're done
-		 * creating files in them, but we don't do that.
-		 */
 		if (settime)
-			wrmeta(fd, hp, mtime, mode);
+			if (dir) {
+				pushleftover(hp, fd, mtime, mode0);
+				return;
+			} else
+				wrmeta(fd, hp, mtime, mode0);
 		close(fd);
 	}
 }
@@ -1275,6 +1320,7 @@
 		else
 			skip(ar, hp, longname);
 	}
+	flushleftovers();
 
 	if (comp)
 		return pushclose(&ps);

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2019-11-03 15:35 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-11-03 15:35 [9front] tar: write directory metadata after file extraction rgl

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).