From 902b120ee62a4e360ec8ff28a5a604a9b2df0935 Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Thu, 29 Sep 2022 00:24:53 -0500 Subject: [PATCH] Change tar/sed protocol to include file type. (Doesn't use it yet.) --- toys/posix/sed.c | 6 +++++- toys/posix/tar.c | 12 ++++++------ 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/toys/posix/sed.c b/toys/posix/sed.c index 24dd3499..c01fe8ad 100644 --- a/toys/posix/sed.c +++ b/toys/posix/sed.c @@ -220,7 +220,7 @@ static void sed_line(char **pline, long plen) char *line; long len; struct sedcmd *command; - int eol = 0, tea = 0; + int eol = 0, tea = 0, xftype = 0; if (FLAG(tarxform)) { if (!pline) return; @@ -250,6 +250,10 @@ static void sed_line(char **pline, long plen) if (!line || !len) return; if (line[len-1] == TT.delim) line[--len] = eol++; + if (FLAG(tarxform) && len) { + xftype = line[--len]; + line[len] = 0; + } TT.count++; // The restart-1 is because we added one to make sure it wasn't NULL, diff --git a/toys/posix/tar.c b/toys/posix/tar.c index 34182387..6930df8e 100644 --- a/toys/posix/tar.c +++ b/toys/posix/tar.c @@ -192,7 +192,7 @@ static void alloread(void *buf, int len) (*b)[len] = 0; } -static char *xform(char **name) +static char *xform(char **name, char type) { char buf[9], *end; off_t len; @@ -200,7 +200,7 @@ static char *xform(char **name) if (!TT.xform) return 0; buf[8] = 0; - if (dprintf(TT.xfpipe[0], "%s%c", *name, 0) != strlen(*name)+1 + if (dprintf(TT.xfpipe[0], "%s%c%c", *name, type, 0) != strlen(*name)+2 || readall(TT.xfpipe[1], buf, 8) != 8 || !(len = estrtol(buf, &end, 16)) || errno ||*end) error_exit("bad xform"); xreadall(TT.xfpipe[1], *name = xmalloc(len+1), len); @@ -259,14 +259,11 @@ static int add_to_tar(struct dirtree *node) TT.warn = 0; } - xfname = xform(&hname); if (TT.owner) st->st_uid = TT.ouid; if (TT.group) st->st_gid = TT.ggid; if (TT.mode) st->st_mode = string_to_mode(TT.mode, st->st_mode); if (TT.mtime) st->st_mtime = TT.mtt; - memset(&hdr, 0, sizeof(hdr)); - strncpy(hdr.name, hname, sizeof(hdr.name)); ITOO(hdr.mode, st->st_mode &07777); ITOO(hdr.uid, st->st_uid); ITOO(hdr.gid, st->st_gid); @@ -274,6 +271,9 @@ static int add_to_tar(struct dirtree *node) ITOO(hdr.mtime, st->st_mtime); strcpy(hdr.magic, "ustar "); + xfname = xform(&hname, 'r'); + strncpy(hdr.name, hname, sizeof(hdr.name)); + // Hard link or symlink? i=0 neither, i=1 hardlink, i=2 symlink // Are there hardlinks to a non-directory entry? @@ -795,7 +795,7 @@ static void unpack_tar(char *first) // We accept --show-transformed but always do, so it's a NOP. name = TT.hdr.name; - if (xform(&name)) { + if (xform(&name, 'r')) { free(TT.hdr.name); TT.hdr.name = name; } -- 2.39.2