9front - general discussion about 9front
 help / color / mirror / Atom feed
* bug: tar doesnt understand pax extended headers
@ 2018-09-15  2:56 Nick Owens
  0 siblings, 0 replies; 2+ messages in thread
From: Nick Owens @ 2018-09-15  2:56 UTC (permalink / raw)
  To: 9front

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

tar(1) doesn't understand pax extended headers. attached is a patch
which will simply ignore pax headers.

see http://pubs.opengroup.org/onlinepubs/9699919799/utilities/pax.html
for a description of the headers and their format.

in the above documentation the type is referred to by 'typeflag', and
in tar.c, linkflag. pax extended headers have two types, 'x' and 'g'.

the main thing that these seem to be useful for are file names which
are longer than whats normally representable.

right now, tar(1) will treat these pax extended headers as normal
"files" and extract the headers to disk, cluttering the fs.

for now we could simply ignore them (easy), but it would be ideal to
read them to find the long path names. the only trouble with this is
that one needs to parse all of the pax 'size key=value\n' attributes
in the pax extended header, finding the 'path' key and using its value
as the new path. there's already some provision for this in tar.c's
"getname" function. there's also no fixed size for these pax
attributes, so filenames can (in theory) be of unlimited length.

if anyone wants to pick this up, the "archive/tar" package in go seems
like a decent reference.

[-- Attachment #2: tar.c.patch --]
[-- Type: text/x-patch, Size: 573 bytes --]

diff --git a/sys/src/cmd/tar.c b/sys/src/cmd/tar.c
--- a/sys/src/cmd/tar.c
+++ b/sys/src/cmd/tar.c
@@ -78,6 +78,8 @@ enum {
 
 	LF_LONGNAME =	'L',		/* GNU extenstion */
 	LF_LONGLINK = 	'K',
+	LF_PAXX		=	'x',
+	LF_PAXG		=	'g',
 };
 
 #define islink(lf)	(isreallink(lf) || issymlink(lf))
@@ -1061,6 +1063,9 @@ openfname(Hdr *hp, char *fname, int dir,
 	case LF_FIFO:
 		fprint(2, "%s: can't make fifo %s\n", argv0, fname);
 		break;
+	case LF_PAXX:
+	case LF_PAXG:
+		break;
 	default:
 		if (!keepexisting || access(fname, AEXIST) < 0) {
 			int rw = (dir? OREAD: OWRITE);

^ permalink raw reply	[flat|nested] 2+ messages in thread

* bug: tar doesnt understand pax extended headers
@ 2018-09-25  3:27 Jordan Niethe
  0 siblings, 0 replies; 2+ messages in thread
From: Jordan Niethe @ 2018-09-25  3:27 UTC (permalink / raw)
  To: 9front

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

Hello everyone,
Here is a patch for the problem talked about here:
9fs 9front; cat /n/lists/9front/1536980231.00

I'd appreciate any feedback.
-jpn

[-- Attachment #2: utar.patch --]
[-- Type: text/x-patch, Size: 3151 bytes --]

--- oldtar.c	Tue Sep 25 01:08:56 2018
+++ tar.c	Tue Sep 25 01:45:13 2018
@@ -73,6 +73,7 @@
 	LF_DIR =	'5',
 	LF_FIFO =	'6',
 	LF_CONTIG =	'7',
+	LF_XHEADER = 'x',
 
 	/* 'A' - 'Z' are reserved for custom implementations */
 
@@ -1211,36 +1212,109 @@
 	}
 }
 
+static void
+getpaxpath(char *s, char *path, char *fname) {
+	long n, len;
+	char *p, *k, *v;
+	
+	/* in case there is no path record */
+	*path = '\0';
+
+	while (*s) {
+		len = strlen(s);
+
+		/* first space marks length number */
+		p = strchr(s, ' ');
+		if (p == nil) {
+			fprint(2, "%s: pax record missing length: %s\n",
+			argv0, fname);
+			return;
+		}
+		*p++ = '\0';
+
+		n = strtoul(s, nil,10);
+		if (n < 5 || len < n) {
+			fprint(2, "%s: pax record has invalid length: %s\n",
+			argv0, fname);
+			return;
+		}
+
+		/* new line seperates each record */
+		if (s[n - 1] != '\n') {
+			fprint(2, "%s: pax record missing \\n: %s\n",
+			argv0, fname);
+			return;
+		}
+		s[n-1] = '\0';
+
+		/* equal sign seperates key and value pair */
+		v = strchr(p, '='); 
+		if (v == nil) {
+			fprint(2, "%s: pax record missing =: %s\n",
+			argv0, fname);
+			return;
+		}
+		*v++ = '\0';
+		k = p;
+		
+		/* only care about path */
+		if (strcmp(k, "path") == 0) {
+			path[Maxlongname] = '\0';
+			strncpy(path, v, Maxlongname);
+			break;
+		}
+		s += n;
+	}
+}
+
+static void
+artobuf(int ar, Hdr *hp, char *fname, char *buf, int max)
+{
+	ulong blksleft, blksread;
+	char *p;
+	int n;
+
+	p = buf;
+	for (blksleft = BYTES2TBLKS(arsize(hp)); blksleft > 0;
+		 blksleft -= blksread) {
+		hp = getblkrd(ar, Alldata);
+		if (hp == nil)
+			sysfatal("unexpected EOF on archive reading %s from %s",
+				fname, arname);
+		blksread = gothowmany(blksleft);
+		n = &buf[max] - p;
+		if(Tblock*blksread < n)
+			n = Tblock*blksread;
+		memmove(p, hp->data, n);
+		p += n;
+		putreadblks(ar, blksread);
+	}
+	*p = '\0';
+}
+
 static char*
 getname(int ar, Hdr *hp)
 {
 	static char namebuf[Maxlongname+1], *nextname = nil;
-	ulong blksleft, blksread;
-	char *fname, *p;
-	int n;
+	char *fname, *xrecords;
+	Off bytes;
 
-	if(nextname != nil && nextname[0] != '\0'){
+	if (nextname != nil && nextname[0] != '\0') {
 		fname = nextname, nextname = nil;
 		return fname;
 	}
 	fname = name(hp);
-	if(hp->linkflag == LF_LONGNAME){
-		p = namebuf;
-		for (blksleft = BYTES2TBLKS(arsize(hp)); blksleft > 0;
-		     blksleft -= blksread) {
-			hp = getblkrd(ar, Alldata);
-			if (hp == nil)
-				sysfatal("unexpected EOF on archive reading %s from %s",
-					fname, arname);
-			blksread = gothowmany(blksleft);
-			n = &namebuf[Maxlongname] - p;
-			if(Tblock*blksread < n)
-				n = Tblock*blksread;
-			memmove(p, hp->data, n);
-			p += n;
-			putreadblks(ar, blksread);
-		}
-		*p = '\0';
+	if (hp->linkflag == LF_LONGNAME) {
+		artobuf(ar, hp, fname, namebuf, Maxlongname);
+		fname = nil;
+		nextname = namebuf;
+	} else if (hp->linkflag == LF_XHEADER) {
+		bytes = arsize(hp);
+		xrecords = malloc(bytes+1);
+		assert(xrecords != nil);
+		artobuf(ar, hp, fname, xrecords, bytes);
+		getpaxpath(xrecords, namebuf, fname);
+		free(xrecords);
 		fname = nil;
 		nextname = namebuf;
 	} else {

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2018-09-25  3:27 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-09-15  2:56 bug: tar doesnt understand pax extended headers Nick Owens
2018-09-25  3:27 Jordan Niethe

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).