untar.c: Coverity ID 26151 and reformat

The Coverity issue was an ignored return value from a read()
in a loop which should have been a seek() since the data
read was ignored.

The file itself needed reformatting to conform to RTEMS style.
This commit is contained in:
Joel Sherrill
2014-12-14 17:30:23 -06:00
parent 1ae7baf2d8
commit 09220c8129

View File

@@ -1,19 +1,19 @@
/** /**
* @file * @file
*
* @brief Untar an Image * @brief Untar an Image
* @ingroup libmisc_untar_img Untar Image * @ingroup libmisc_untar_img Untar Image
*
* FIXME: * FIXME:
* 1. Symbolic links are not created. * 1. Symbolic links are not created.
* 2. Untar_FromMemory uses FILE *fp. * 2. Untar_FromMemory uses FILE *fp.
* 3. How to determine end of archive? * 3. How to determine end of archive?
*
*/ */
/* /*
* Written by: Jake Janovetz <janovetz@tempest.ece.uiuc.edu> * Written by: Jake Janovetz <janovetz@tempest.ece.uiuc.edu>
*
* The license and distribution terms for this file may be * The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at * found in the file LICENSE in this distribution or at
* http://www.rtems.org/license/LICENSE. * http://www.rtems.org/license/LICENSE.
@@ -34,9 +34,9 @@
#include <rtems/bspIo.h> #include <rtems/bspIo.h>
/************************************************************************** /*
* TAR file format: * TAR file format:
*
* Offset Length Contents * Offset Length Contents
* 0 100 bytes File name ('\0' terminated, 99 maxmum length) * 0 100 bytes File name ('\0' terminated, 99 maxmum length)
* 100 8 bytes File mode (in octal ascii) * 100 8 bytes File mode (in octal ascii)
@@ -63,14 +63,14 @@
* sum = 0; * sum = 0;
* for(i = 0; i < 512; i++) * for(i = 0; i < 512; i++)
* sum += 0xFF & header[i]; * sum += 0xFF & header[i];
*************************************************************************/ */
#define MAX_NAME_FIELD_SIZE 99 #define MAX_NAME_FIELD_SIZE 99
/************************************************************************** /*
* This converts octal ASCII number representations into an * This converts octal ASCII number representations into an
* unsigned long. Only support 32-bit numbers for now. * unsigned long. Only support 32-bit numbers for now.
*************************************************************************/ */
unsigned long unsigned long
_rtems_octal2ulong( _rtems_octal2ulong(
const char *octascii, const char *octascii,
@@ -81,10 +81,8 @@ _rtems_octal2ulong(
unsigned long num; unsigned long num;
num = 0; num = 0;
for (i=0; i < len; i++) for (i=0; i < len; i++) {
{ if ((octascii[i] < '0') || (octascii[i] > '9')) {
if ((octascii[i] < '0') || (octascii[i] > '9'))
{
continue; continue;
} }
num = num * 8 + ((unsigned long)(octascii[i] - '0')); num = num * 8 + ((unsigned long)(octascii[i] - '0'));
@@ -92,29 +90,28 @@ _rtems_octal2ulong(
return(num); return(num);
} }
/*
/************************************************************************** * Function: Untar_FromMemory
* Function: Untar_FromMemory * *
************************************************************************** * Description:
* Description: * *
* * * This is a simple subroutine used to rip links, directories, and
* This is a simple subroutine used to rip links, directories, and * * files out of a block of memory.
* files out of a block of memory. * *
* * *
* * * Inputs:
* Inputs: * *
* * * void * tar_buf - Pointer to TAR buffer.
* void * tar_buf - Pointer to TAR buffer. * * size_t size - Length of TAR buffer.
* size_t size - Length of TAR buffer. * *
* * *
* * * Output:
* Output: * *
* * * int - UNTAR_SUCCESSFUL (0) on successful completion.
* int - UNTAR_SUCCESSFUL (0) on successful completion. * * UNTAR_INVALID_CHECKSUM for an invalid header checksum.
* UNTAR_INVALID_CHECKSUM for an invalid header checksum. * * UNTAR_INVALID_HEADER for an invalid header.
* UNTAR_INVALID_HEADER for an invalid header. * *
* * */
**************************************************************************/
int int
Untar_FromMemory( Untar_FromMemory(
void *tar_buf, void *tar_buf,
@@ -136,12 +133,9 @@ Untar_FromMemory(
unsigned long file_size; unsigned long file_size;
unsigned char linkflag; unsigned char linkflag;
ptr = 0; ptr = 0;
while (1) while (1) {
{ if (ptr + 512 > size) {
if (ptr + 512 > size)
{
retval = UNTAR_SUCCESSFUL; retval = UNTAR_SUCCESSFUL;
break; break;
} }
@@ -149,8 +143,7 @@ Untar_FromMemory(
/* Read the header */ /* Read the header */
bufr = &tar_ptr[ptr]; bufr = &tar_ptr[ptr];
ptr += 512; ptr += 512;
if (strncmp(&bufr[257], "ustar", 5)) if (strncmp(&bufr[257], "ustar", 5)) {
{
retval = UNTAR_SUCCESSFUL; retval = UNTAR_SUCCESSFUL;
break; break;
} }
@@ -161,54 +154,44 @@ Untar_FromMemory(
linkflag = bufr[156]; linkflag = bufr[156];
file_size = _rtems_octal2ulong(&bufr[124], 12); file_size = _rtems_octal2ulong(&bufr[124], 12);
/****************************************************************** /*
* Compute the TAR checksum and check with the value in * Compute the TAR checksum and check with the value in
* the archive. The checksum is computed over the entire * the archive. The checksum is computed over the entire
* header, but the checksum field is substituted with blanks. * header, but the checksum field is substituted with blanks.
******************************************************************/ */
hdr_chksum = _rtems_octal2ulong(&bufr[148], 8); hdr_chksum = _rtems_octal2ulong(&bufr[148], 8);
sum = _rtems_tar_header_checksum(bufr); sum = _rtems_tar_header_checksum(bufr);
if (sum != hdr_chksum) if (sum != hdr_chksum) {
{
retval = UNTAR_INVALID_CHECKSUM; retval = UNTAR_INVALID_CHECKSUM;
break; break;
} }
/*
/******************************************************************
* We've decoded the header, now figure out what it contains and * We've decoded the header, now figure out what it contains and
* do something with it. * do something with it.
*****************************************************************/ */
if (linkflag == SYMTYPE) if (linkflag == SYMTYPE) {
{
strncpy(linkname, &bufr[157], MAX_NAME_FIELD_SIZE); strncpy(linkname, &bufr[157], MAX_NAME_FIELD_SIZE);
linkname[MAX_NAME_FIELD_SIZE] = '\0'; linkname[MAX_NAME_FIELD_SIZE] = '\0';
symlink(linkname, fname); symlink(linkname, fname);
} } else if (linkflag == REGTYPE) {
else if (linkflag == REGTYPE)
{
nblocks = (((file_size) + 511) & ~511) / 512; nblocks = (((file_size) + 511) & ~511) / 512;
if ((fp = fopen(fname, "w")) == NULL) if ((fp = fopen(fname, "w")) == NULL) {
{
printk("Untar: failed to create file %s\n", fname); printk("Untar: failed to create file %s\n", fname);
ptr += 512 * nblocks; ptr += 512 * nblocks;
} } else {
else
{
unsigned long sizeToGo = file_size; unsigned long sizeToGo = file_size;
size_t len; size_t len;
/*************************************************************** /*
* Read out the data. There are nblocks of data where nblocks * Read out the data. There are nblocks of data where nblocks
* is the file_size rounded to the nearest 512-byte boundary. * is the file_size rounded to the nearest 512-byte boundary.
**************************************************************/ */
for (i=0; i<nblocks; i++) for (i=0; i<nblocks; i++) {
{
len = ((sizeToGo < 512L)?(sizeToGo):(512L)); len = ((sizeToGo < 512L)?(sizeToGo):(512L));
n = fwrite(&tar_ptr[ptr], 1, len, fp); n = fwrite(&tar_ptr[ptr], 1, len, fp);
if (n != len) if (n != len) {
{
printk("untar: Error during write\n"); printk("untar: Error during write\n");
retval = UNTAR_FAIL; retval = UNTAR_FAIL;
break; break;
@@ -218,9 +201,7 @@ Untar_FromMemory(
} }
fclose(fp); fclose(fp);
} }
} } else if (linkflag == DIRTYPE) {
else if (linkflag == DIRTYPE)
{
if ( mkdir(fname, S_IRWXU | S_IRWXG | S_IRWXO) != 0 ) { if ( mkdir(fname, S_IRWXU | S_IRWXG | S_IRWXO) != 0 ) {
printk("Untar: failed to create directory %s\n", fname); printk("Untar: failed to create directory %s\n", fname);
retval = UNTAR_FAIL; retval = UNTAR_FAIL;
@@ -232,31 +213,24 @@ Untar_FromMemory(
return(retval); return(retval);
} }
/*
/************************************************************************** * Function: Untar_FromFile
* Function: Untar_FromFile * *
************************************************************************** * Description:
* Description: * *
* * * This is a simple subroutine used to rip links, directories, and
* This is a simple subroutine used to rip links, directories, and * * files out of a TAR file.
* files out of a TAR file. * *
* * * Inputs:
* * *
* Inputs: * * const char *tar_name - TAR filename.
* * *
* const char *tar_name - TAR filename. * * Output:
* * *
* * * int - UNTAR_SUCCESSFUL (0) on successful completion.
* Output: * * UNTAR_INVALID_CHECKSUM for an invalid header checksum.
* * * UNTAR_INVALID_HEADER for an invalid header.
* int - UNTAR_SUCCESSFUL (0) on successful completion. * */
* UNTAR_INVALID_CHECKSUM for an invalid header checksum. *
* UNTAR_INVALID_HEADER for an invalid header. *
* *
**************************************************************************
* Change History: *
* 12/30/1998 - Creation (JWJ) *
*************************************************************************/
int int
Untar_FromFile( Untar_FromFile(
const char *tar_name const char *tar_name
@@ -287,18 +261,14 @@ Untar_FromFile(
return(UNTAR_FAIL); return(UNTAR_FAIL);
} }
while (1) while (1) {
{
/* Read the header */ /* Read the header */
/* If the header read fails, we just consider it the end /* If the header read fails, we just consider it the end of the tarfile. */
of the tarfile. */ if ((n = read(fd, bufr, 512)) != 512) {
if ((n = read(fd, bufr, 512)) != 512)
{
break; break;
} }
if (strncmp(&bufr[257], "ustar", 5)) if (strncmp(&bufr[257], "ustar", 5)) {
{
break; break;
} }
@@ -308,61 +278,48 @@ Untar_FromFile(
linkflag = bufr[156]; linkflag = bufr[156];
size = _rtems_octal2ulong(&bufr[124], 12); size = _rtems_octal2ulong(&bufr[124], 12);
/****************************************************************** /*
* Compute the TAR checksum and check with the value in * Compute the TAR checksum and check with the value in
* the archive. The checksum is computed over the entire * the archive. The checksum is computed over the entire
* header, but the checksum field is substituted with blanks. * header, but the checksum field is substituted with blanks.
******************************************************************/ */
hdr_chksum = _rtems_octal2ulong(&bufr[148], 8); hdr_chksum = _rtems_octal2ulong(&bufr[148], 8);
sum = _rtems_tar_header_checksum(bufr); sum = _rtems_tar_header_checksum(bufr);
if (sum != hdr_chksum) if (sum != hdr_chksum) {
{
retval = UNTAR_INVALID_CHECKSUM; retval = UNTAR_INVALID_CHECKSUM;
break; break;
} }
/****************************************************************** /*
* We've decoded the header, now figure out what it contains and * We've decoded the header, now figure out what it contains and
* do something with it. * do something with it.
*****************************************************************/ */
if (linkflag == SYMTYPE) if (linkflag == SYMTYPE) {
{
strncpy(linkname, &bufr[157], MAX_NAME_FIELD_SIZE); strncpy(linkname, &bufr[157], MAX_NAME_FIELD_SIZE);
linkname[MAX_NAME_FIELD_SIZE] = '\0'; linkname[MAX_NAME_FIELD_SIZE] = '\0';
symlink(linkname,fname); symlink(linkname,fname);
} } else if (linkflag == REGTYPE) {
else if (linkflag == REGTYPE)
{
int out_fd; int out_fd;
/****************************************************************** /*
* Read out the data. There are nblocks of data where nblocks * Read out the data. There are nblocks of data where nblocks
* is the size rounded to the nearest 512-byte boundary. * is the size rounded to the nearest 512-byte boundary.
*****************************************************************/ */
nblocks = (((size) + 511) & ~511) / 512; nblocks = (((size) + 511) & ~511) / 512;
if ((out_fd = creat(fname, 0644)) == -1) if ((out_fd = creat(fname, 0644)) == -1) {
{ (void) lseek(fd, SEEK_CUR, 512 * nblocks);
for (i=0; i<nblocks; i++) } else {
{ for (i=0; i<nblocks; i++) {
n = read(fd, bufr, 512);
}
}
else
{
for (i=0; i<nblocks; i++)
{
n = read(fd, bufr, 512); n = read(fd, bufr, 512);
n = MIN(n, size - i*512); n = MIN(n, size - i*512);
write(out_fd, bufr, n); (void) write(out_fd, bufr, n);
} }
close(out_fd); close(out_fd);
} }
} } else if (linkflag == DIRTYPE) {
else if (linkflag == DIRTYPE) (void) mkdir(fname, S_IRWXU | S_IRWXG | S_IRWXO);
{
mkdir(fname, S_IRWXU | S_IRWXG | S_IRWXO);
} }
} }
free(bufr); free(bufr);
@@ -371,11 +328,11 @@ Untar_FromFile(
return(retval); return(retval);
} }
/************************************************************************ /*
* Compute the TAR checksum and check with the value in * Compute the TAR checksum and check with the value in
* the archive. The checksum is computed over the entire * the archive. The checksum is computed over the entire
* header, but the checksum field is substituted with blanks. * header, but the checksum field is substituted with blanks.
************************************************************************/ */
int int
_rtems_tar_header_checksum( _rtems_tar_header_checksum(
const char *bufr const char *bufr
@@ -384,8 +341,7 @@ _rtems_tar_header_checksum(
int i, sum; int i, sum;
sum = 0; sum = 0;
for (i=0; i<512; i++) for (i=0; i<512; i++) {
{
if ((i >= 148) && (i < 156)) if ((i >= 148) && (i < 156))
sum += 0xff & ' '; sum += 0xff & ' ';
else else