<div dir="ltr">Looks good. Was there a test case (before or after)?<div><br></div><div>--joel</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Sun, Jul 31, 2016 at 8:03 PM, Chris Johns <span dir="ltr"><<a href="mailto:chrisj@rtems.org" target="_blank">chrisj@rtems.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">This patch parses the mode field in the tar header and sets the<br>
directory or file to the mode value in the header.<br>
<br>
Closes #2768.<br>
---<br>
cpukit/libmisc/untar/untar.c | 47 ++++++++++++++++++++++++++------------------<br>
cpukit/libmisc/untar/untar.h | 7 ++++++-<br>
2 files changed, 34 insertions(+), 20 deletions(-)<br>
<br>
diff --git a/cpukit/libmisc/untar/untar.c b/cpukit/libmisc/untar/untar.c<br>
index 9a14f67..ecf1877 100644<br>
--- a/cpukit/libmisc/untar/untar.c<br>
+++ b/cpukit/libmisc/untar/untar.c<br>
@@ -250,6 +250,7 @@ static int<br>
Untar_ProcessHeader(<br>
const char *bufr,<br>
char *fname,<br>
+ unsigned long *mode,<br>
unsigned long *file_size,<br>
unsigned long *nblocks,<br>
unsigned char *linkflag,<br>
@@ -286,6 +287,8 @@ Untar_ProcessHeader(<br>
strncpy(fname, bufr, MAX_NAME_FIELD_SIZE);<br>
fname[MAX_NAME_FIELD_SIZE] = '\0';<br>
<br>
+ *mode = strtoul(&bufr[100], NULL, 8);<br>
+<br>
*linkflag = bufr[156];<br>
*file_size = _rtems_octal2ulong(&bufr[124], 12);<br>
<br>
@@ -299,7 +302,8 @@ Untar_ProcessHeader(<br>
rtems_printf(printer, "untar: symlink: %s -> %s\n", linkname, fname);<br>
symlink(linkname, fname);<br>
} else if (*linkflag == REGTYPE) {<br>
- rtems_printf(printer, "untar: file: %s (%i)\n", fname, (int) *file_size);<br>
+ rtems_printf(printer, "untar: file: %s (s:%i,m:%04o)\n",<br>
+ fname, (int) *file_size, (int) *mode);<br>
*nblocks = (((*file_size) + 511) & ~511) / 512;<br>
if (Make_Path(printer, fname, false) < 0) {<br>
retval = UNTAR_FAIL;<br>
@@ -318,7 +322,7 @@ Untar_ProcessHeader(<br>
if (!S_ISDIR(stat_buf.st_mode)) {<br>
r = unlink(fname);<br>
if (r == 0) {<br>
- r = mkdir(fname, S_IRWXU | S_IRWXG | S_IRWXO);<br>
+ r = mkdir(fname, *mode);<br>
}<br>
}<br>
}<br>
@@ -362,15 +366,16 @@ Untar_FromMemory_Print(<br>
const rtems_printer *printer<br>
)<br>
{<br>
- FILE *fp;<br>
+ int fd;<br>
const char *tar_ptr = (const char *)tar_buf;<br>
const char *bufr;<br>
char fname[100];<br>
int retval = UNTAR_SUCCESSFUL;<br>
unsigned long ptr;<br>
- unsigned long nblocks;<br>
- unsigned long file_size;<br>
- unsigned char linkflag;<br>
+ unsigned long nblocks = 0;<br>
+ unsigned long file_size = 0;<br>
+ unsigned long mode = 0;<br>
+ unsigned char linkflag = 0;<br>
<br>
rtems_printf(printer, "untar: memory at %p (%zu)\n", tar_buf, size);<br>
<br>
@@ -385,20 +390,21 @@ Untar_FromMemory_Print(<br>
bufr = &tar_ptr[ptr];<br>
ptr += 512;<br>
<br>
- retval = Untar_ProcessHeader(bufr, fname, &file_size, &nblocks, &linkflag, printer);<br>
+ retval = Untar_ProcessHeader(bufr, fname, &mode, &file_size,<br>
+ &nblocks, &linkflag, printer);<br>
<br>
if (retval != UNTAR_SUCCESSFUL)<br>
break;<br>
<br>
if (linkflag == REGTYPE) {<br>
- if ((fp = fopen(fname, "w")) == NULL) {<br>
+ if ((fd = open(fname, O_TRUNC | O_CREAT | O_WRONLY, mode)) == -1) {<br>
Print_Error(printer, "open", fname);<br>
ptr += 512 * nblocks;<br>
} else {<br>
unsigned long sizeToGo = file_size;<br>
- size_t len;<br>
- size_t i;<br>
- size_t n;<br>
+ ssize_t len;<br>
+ ssize_t i;<br>
+ ssize_t n;<br>
<br>
/*<br>
* Read out the data. There are nblocks of data where nblocks is the<br>
@@ -406,7 +412,7 @@ Untar_FromMemory_Print(<br>
*/<br>
for (i = 0; i < nblocks; i++) {<br>
len = ((sizeToGo < 512L) ? (sizeToGo) : (512L));<br>
- n = fwrite(&tar_ptr[ptr], 1, len, fp);<br>
+ n = write(fd, &tar_ptr[ptr], len);<br>
if (n != len) {<br>
Print_Error(printer, "write", fname);<br>
retval = UNTAR_FAIL;<br>
@@ -415,7 +421,7 @@ Untar_FromMemory_Print(<br>
ptr += 512;<br>
sizeToGo -= n;<br>
}<br>
- fclose(fp);<br>
+ close(fd);<br>
}<br>
<br>
}<br>
@@ -485,9 +491,10 @@ Untar_FromFile_Print(<br>
char fname[100];<br>
int retval;<br>
unsigned long i;<br>
- unsigned long nblocks;<br>
- unsigned long file_size;<br>
- unsigned char linkflag;<br>
+ unsigned long nblocks = 0;<br>
+ unsigned long file_size = 0;<br>
+ unsigned long mode = 0;<br>
+ unsigned char linkflag = 0;<br>
<br>
retval = UNTAR_SUCCESSFUL;<br>
<br>
@@ -508,7 +515,8 @@ Untar_FromFile_Print(<br>
break;<br>
}<br>
<br>
- retval = Untar_ProcessHeader(bufr, fname, &file_size, &nblocks, &linkflag, printer);<br>
+ retval = Untar_ProcessHeader(bufr, fname, &mode, &file_size,<br>
+ &nblocks, &linkflag, printer);<br>
<br>
if (retval != UNTAR_SUCCESSFUL)<br>
break;<br>
@@ -521,7 +529,7 @@ Untar_FromFile_Print(<br>
* is the size rounded to the nearest 512-byte boundary.<br>
*/<br>
<br>
- if ((out_fd = creat(fname, 0644)) == -1) {<br>
+ if ((out_fd = creat(fname, mode)) == -1) {<br>
(void) lseek(fd, SEEK_CUR, 512UL * nblocks);<br>
} else {<br>
for (i = 0; i < nblocks; i++) {<br>
@@ -579,6 +587,7 @@ int Untar_FromChunk_Print(<br>
retval = Untar_ProcessHeader(<br>
&context->header[0],<br>
&context->fname[0],<br>
+ &context->mode,<br>
&context->todo_bytes,<br>
&context->todo_blocks,<br>
&linkflag,<br>
@@ -591,7 +600,7 @@ int Untar_FromChunk_Print(<br>
}<br>
<br>
if (linkflag == REGTYPE) {<br>
- context->out_fd = creat(&context->fname[0], 0644);<br>
+ context->out_fd = creat(&context->fname[0], context->mode);<br>
<br>
if (context->out_fd >= 0) {<br>
context->state = UNTAR_CHUNK_WRITE;<br>
diff --git a/cpukit/libmisc/untar/untar.h b/cpukit/libmisc/untar/untar.h<br>
index 4d00d36..3c8bb74 100644<br>
--- a/cpukit/libmisc/untar/untar.h<br>
+++ b/cpukit/libmisc/untar/untar.h<br>
@@ -74,9 +74,14 @@ typedef struct {<br>
size_t done_bytes;<br>
<br>
/**<br>
+ * @brief Mode of the file.<br>
+ */<br>
+ unsigned long mode;<br>
+<br>
+ /**<br>
* @brief Overall amount of bytes to be processed.<br>
*/<br>
- long unsigned todo_bytes;<br>
+ unsigned long todo_bytes;<br>
<br>
/**<br>
* @brief Overall amount of blocks to be processed.<br>
<span class="HOEnZb"><font color="#888888">--<br>
2.4.6<br>
<br>
_______________________________________________<br>
devel mailing list<br>
<a href="mailto:devel@rtems.org">devel@rtems.org</a><br>
<a href="http://lists.rtems.org/mailman/listinfo/devel" rel="noreferrer" target="_blank">http://lists.rtems.org/mailman/listinfo/devel</a><br>
</font></span></blockquote></div><br></div>