tar2qfile: disable debug messages

This commit is contained in:
Marek Marczykowski-Górecki 2013-11-24 04:42:12 +01:00
parent dba3571883
commit 03923ae548

View File

@ -48,6 +48,8 @@
#include <string.h> #include <string.h>
#include <qfile-utils.h> #include <qfile-utils.h>
// #define DEBUG
/*************************************************** /***************************************************
* Most routines extracted from the PAX project (tar.c...) * * Most routines extracted from the PAX project (tar.c...) *
***************************************************/ ***************************************************/
@ -430,12 +432,18 @@ ustar_rd (int fd, struct file_header * untrusted_hdr, char *buf, struct stat * s
/* /*
* we only get proper sized buffers * we only get proper sized buffers
*/ */
#ifdef DEBUG
fprintf(stderr,"Checking if valid header\n"); fprintf(stderr,"Checking if valid header\n");
#endif
if (ustar_id (buf, BLKMULT) < 0) { if (ustar_id (buf, BLKMULT) < 0) {
fprintf (stderr,"Invalid header\n"); #ifdef DEBUG
fprintf (stderr, "Invalid header\n");
#endif
return INVALID_HEADER; return INVALID_HEADER;
} }
#ifdef DEBUG
fprintf(stderr,"Valid header!\n"); fprintf(stderr,"Valid header!\n");
#endif
/* DISABLED: Internal to PAX /* DISABLED: Internal to PAX
arcn->org_name = arcn->name; arcn->org_name = arcn->name;
arcn->sb.st_nlink = 1; arcn->sb.st_nlink = 1;
@ -472,8 +480,10 @@ ustar_rd (int fd, struct file_header * untrusted_hdr, char *buf, struct stat * s
// qfile count the \0 in the namelen // qfile count the \0 in the namelen
untrusted_hdr->namelen += 1; untrusted_hdr->namelen += 1;
#ifdef DEBUG
fprintf(stderr,"Retrieved name len: %d\n",untrusted_hdr->namelen); fprintf(stderr,"Retrieved name len: %d\n",untrusted_hdr->namelen);
fprintf(stderr,"Retrieved name: %s\n",untrusted_namebuf); fprintf(stderr,"Retrieved name: %s\n",untrusted_namebuf);
#endif
/* /*
* follow the spec to the letter. we should only have mode bits, strip * follow the spec to the letter. we should only have mode bits, strip
@ -523,7 +533,7 @@ ustar_rd (int fd, struct file_header * untrusted_hdr, char *buf, struct stat * s
arcn->skip = 0; arcn->skip = 0;
arcn->sb.st_rdev = (dev_t) 0; arcn->sb.st_rdev = (dev_t) 0;
*/ */
/* /*
* set the mode and PAX type according to the typeflag in the header * set the mode and PAX type according to the typeflag in the header
@ -531,14 +541,18 @@ ustar_rd (int fd, struct file_header * untrusted_hdr, char *buf, struct stat * s
switch (hd->typeflag) switch (hd->typeflag)
{ {
case FIFOTYPE: case FIFOTYPE:
#ifdef DEBUG
fprintf(stderr,"File is FIFOTYPE\n"); fprintf(stderr,"File is FIFOTYPE\n");
#endif
/* DISABLED: unused /* DISABLED: unused
arcn->type = PAX_FIF; arcn->type = PAX_FIF;
arcn->sb.st_mode |= S_IFIFO; arcn->sb.st_mode |= S_IFIFO;
*/ */
break; break;
case DIRTYPE: case DIRTYPE:
#ifdef DEBUG
fprintf(stderr,"File is DIRTYPE\n"); fprintf(stderr,"File is DIRTYPE\n");
#endif
/* DISABLED: unused /* DISABLED: unused
arcn->type = PAX_DIR; arcn->type = PAX_DIR;
arcn->sb.st_mode |= S_IFDIR; arcn->sb.st_mode |= S_IFDIR;
@ -555,10 +569,14 @@ ustar_rd (int fd, struct file_header * untrusted_hdr, char *buf, struct stat * s
*/ */
break; break;
case BLKTYPE: case BLKTYPE:
#ifdef DEBUG
fprintf(stderr,"File is BLKTYPE\n"); fprintf(stderr,"File is BLKTYPE\n");
#endif
break; break;
case CHRTYPE: case CHRTYPE:
#ifdef DEBUG
fprintf(stderr,"File is CHRTYPE\n"); fprintf(stderr,"File is CHRTYPE\n");
#endif
/* /*
* this type requires the rdev field to be set. * this type requires the rdev field to be set.
*/ */
@ -583,10 +601,14 @@ ustar_rd (int fd, struct file_header * untrusted_hdr, char *buf, struct stat * s
// arcn->sb.st_rdev = TODEV (devmajor, devminor); // arcn->sb.st_rdev = TODEV (devmajor, devminor);
break; break;
case SYMTYPE: case SYMTYPE:
#ifdef DEBUG
fprintf(stderr,"File is SYMTYPE\n"); fprintf(stderr,"File is SYMTYPE\n");
#endif
break; break;
case LNKTYPE: case LNKTYPE:
#ifdef DEBUG
fprintf(stderr,"File is LNKTYPE\n"); fprintf(stderr,"File is LNKTYPE\n");
#endif
if (hd->typeflag == SYMTYPE) if (hd->typeflag == SYMTYPE)
{ {
// arcn->type = PAX_SLK; // arcn->type = PAX_SLK;
@ -608,10 +630,14 @@ ustar_rd (int fd, struct file_header * untrusted_hdr, char *buf, struct stat * s
// MIN(TNMSZ+1,sizeof (arcn->ln_name))); // MIN(TNMSZ+1,sizeof (arcn->ln_name)));
break; break;
case LONGLINKTYPE: case LONGLINKTYPE:
#ifdef DEBUG
fprintf(stderr,"File is LONGLINKTYPE\n"); fprintf(stderr,"File is LONGLINKTYPE\n");
#endif
break; break;
case LONGNAMETYPE: case LONGNAMETYPE:
#ifdef DEBUG
fprintf(stderr,"File is LONGNAMETYPE\n"); fprintf(stderr,"File is LONGNAMETYPE\n");
#endif
/* /*
* GNU long link/file; we tag these here and let the * GNU long link/file; we tag these here and let the
* pax internals deal with it -- too ugly otherwise. * pax internals deal with it -- too ugly otherwise.
@ -624,21 +650,31 @@ ustar_rd (int fd, struct file_header * untrusted_hdr, char *buf, struct stat * s
// arcn->ln_nlen = 0; // arcn->ln_nlen = 0;
break; break;
case CONTTYPE: case CONTTYPE:
#ifdef DEBUG
fprintf(stderr,"File is CONTTYPE\n"); fprintf(stderr,"File is CONTTYPE\n");
#endif
break; break;
case AREGTYPE: case AREGTYPE:
#ifdef DEBUG
fprintf(stderr,"File is AREGTYPE\n"); fprintf(stderr,"File is AREGTYPE\n");
#endif
break; break;
case REGTYPE: case REGTYPE:
#ifdef DEBUG
fprintf(stderr,"File is REGTYPE of size %ld\n",sb->st_size); fprintf(stderr,"File is REGTYPE of size %ld\n",sb->st_size);
#endif
// Check if user want to extract this file // Check if user want to extract this file
should_extract = 1; should_extract = 1;
for (i=0; i < filters->filters_count; i++) { for (i=0; i < filters->filters_count; i++) {
should_extract = 0; should_extract = 0;
#ifdef DEBUG
fprintf(stderr, "Comparing with filter %s\n", filters->filters[i]); fprintf(stderr, "Comparing with filter %s\n", filters->filters[i]);
#endif
if (strncmp(untrusted_namebuf, filters->filters[i], strlen(filters->filters[i])) == 0) { if (strncmp(untrusted_namebuf, filters->filters[i], strlen(filters->filters[i])) == 0) {
#ifdef DEBUG
fprintf(stderr, "Match (%d)\n", filters->filters_matches[i]); fprintf(stderr, "Match (%d)\n", filters->filters_matches[i]);
#endif
should_extract = 1; should_extract = 1;
filters->filters_matches[i]++; filters->filters_matches[i]++;
if (filters->filters_matches[i] == 1) { if (filters->filters_matches[i] == 1) {
@ -649,7 +685,9 @@ ustar_rd (int fd, struct file_header * untrusted_hdr, char *buf, struct stat * s
} }
} }
if (should_extract != 1) { if (should_extract != 1) {
#ifdef DEBUG
fprintf(stderr, "File should be filtered.. Skipping\n"); fprintf(stderr, "File should be filtered.. Skipping\n");
#endif
return NEED_SKIP_FILE; return NEED_SKIP_FILE;
} }
@ -671,7 +709,9 @@ ustar_rd (int fd, struct file_header * untrusted_hdr, char *buf, struct stat * s
char * token = strtok(NULL, "/"); char * token = strtok(NULL, "/");
while (token != NULL) { while (token != NULL) {
#ifdef DEBUG
fprintf(stderr,"Found directory %s (last:%s)\n",token,last_token); fprintf(stderr,"Found directory %s (last:%s)\n",token,last_token);
#endif
// Recompose the path based on last discovered directory // Recompose the path based on last discovered directory
if (path == NULL) { if (path == NULL) {
@ -690,21 +730,31 @@ ustar_rd (int fd, struct file_header * untrusted_hdr, char *buf, struct stat * s
strncpy(path+pathsize+1, last_token, strlen(last_token)); strncpy(path+pathsize+1, last_token, strlen(last_token));
path[pathsize+strlen(last_token)+1] = '\0'; path[pathsize+strlen(last_token)+1] = '\0';
} }
#ifdef DEBUG
fprintf(stderr,"Path is %s\n",path); fprintf(stderr,"Path is %s\n",path);
#endif
#ifdef DEBUG
fprintf(stderr,"Checking from i=0 i<%d\n",n_dirs); fprintf(stderr,"Checking from i=0 i<%d\n",n_dirs);
#endif
// Verify if qfile headers for the current path have already been sent based on the dirs_headers_sent table // Verify if qfile headers for the current path have already been sent based on the dirs_headers_sent table
dir_found = 0; dir_found = 0;
for (i = 0; i < n_dirs; ++i) { for (i = 0; i < n_dirs; ++i) {
#ifdef DEBUG
fprintf(stderr,"Comparing with %d %d %s %s\n",i,n_dirs,dirs_headers_sent[i],path); fprintf(stderr,"Comparing with %d %d %s %s\n",i,n_dirs,dirs_headers_sent[i],path);
#endif
if (strcmp(dirs_headers_sent[i],path)==0) { if (strcmp(dirs_headers_sent[i],path)==0) {
#ifdef DEBUG
fprintf(stderr,"Directory headers already sent\n"); fprintf(stderr,"Directory headers already sent\n");
#endif
dir_found=1; dir_found=1;
} }
} }
if (dir_found == 0) { if (dir_found == 0) {
// Register the current path as being sent in the dirs_headers_sent table // Register the current path as being sent in the dirs_headers_sent table
#ifdef DEBUG
fprintf(stderr,"Inserting %s into register\n",path); fprintf(stderr,"Inserting %s into register\n",path);
#endif
dirs_headers_sent = realloc(dirs_headers_sent, sizeof (char*) * n_dirs++); dirs_headers_sent = realloc(dirs_headers_sent, sizeof (char*) * n_dirs++);
if (dirs_headers_sent == NULL) if (dirs_headers_sent == NULL)
return MEMORY_ALLOC_FAILED; return MEMORY_ALLOC_FAILED;
@ -723,7 +773,9 @@ ustar_rd (int fd, struct file_header * untrusted_hdr, char *buf, struct stat * s
dir_header.mode = untrusted_hdr->mode | S_IFDIR; dir_header.mode = untrusted_hdr->mode | S_IFDIR;
dir_header.filelen = 0; dir_header.filelen = 0;
#ifdef DEBUG
fprintf(stderr,"Sending directory headers for %s\n",path); fprintf(stderr,"Sending directory headers for %s\n",path);
#endif
// Send the qfile headers for the current directory path // Send the qfile headers for the current directory path
write_headers(&dir_header, path); write_headers(&dir_header, path);
} }
@ -733,17 +785,25 @@ ustar_rd (int fd, struct file_header * untrusted_hdr, char *buf, struct stat * s
free(path); free(path);
free(dirbuf); free(dirbuf);
#ifdef DEBUG
fprintf(stderr,"End of directory checks\n"); fprintf(stderr,"End of directory checks\n");
#endif
// Restore POSIX stat file mode (because PAX format use its own file type) // Restore POSIX stat file mode (because PAX format use its own file type)
untrusted_hdr->mode |= S_IFREG; untrusted_hdr->mode |= S_IFREG;
#ifdef DEBUG
fprintf(stderr,"Writing file header\n"); fprintf(stderr,"Writing file header\n");
#endif
// Send header and file content // Send header and file content
write_headers(untrusted_hdr, untrusted_namebuf); write_headers(untrusted_hdr, untrusted_namebuf);
#ifdef DEBUG
fprintf(stderr,"Writing file content\n"); fprintf(stderr,"Writing file content\n");
#endif
ret = copy_file(1, fd, untrusted_hdr->filelen, &crc32_sum); ret = copy_file(1, fd, untrusted_hdr->filelen, &crc32_sum);
#ifdef DEBUG
fprintf(stderr,"Copyfile returned with error %d\n",ret); fprintf(stderr,"Copyfile returned with error %d\n",ret);
#endif
if (ret != COPY_FILE_OK) { if (ret != COPY_FILE_OK) {
if (ret != COPY_FILE_WRITE_ERROR) if (ret != COPY_FILE_WRITE_ERROR)
gui_fatal("Copying file %s: %s", untrusted_namebuf, gui_fatal("Copying file %s: %s", untrusted_namebuf,
@ -756,22 +816,30 @@ ustar_rd (int fd, struct file_header * untrusted_hdr, char *buf, struct stat * s
} }
} }
// Extract extra padding // Extract extra padding
#ifdef DEBUG
fprintf(stderr,"Need to remove pad:%lld %lld\n",untrusted_hdr->filelen,BLKMULT-(untrusted_hdr->filelen%BLKMULT)); fprintf(stderr,"Need to remove pad:%lld %lld\n",untrusted_hdr->filelen,BLKMULT-(untrusted_hdr->filelen%BLKMULT));
#endif
if (untrusted_hdr->filelen%BLKMULT > 0) if (untrusted_hdr->filelen%BLKMULT > 0)
ret = read(fd, buf, BLKMULT-(untrusted_hdr->filelen%BLKMULT)); ret = read(fd, buf, BLKMULT-(untrusted_hdr->filelen%BLKMULT));
#ifdef DEBUG
fprintf(stderr,"Removed %d bytes of padding\n",ret); fprintf(stderr,"Removed %d bytes of padding\n",ret);
#endif
// Resync trailing headers in order to find next file chunck in the tar file // Resync trailing headers in order to find next file chunck in the tar file
return NEED_SYNC_TRAIL; return NEED_SYNC_TRAIL;
break; break;
case EXTHEADERTYPE: case EXTHEADERTYPE:
#ifdef DEBUG
fprintf(stderr,"Extended HEADER encountered\n"); fprintf(stderr,"Extended HEADER encountered\n");
#endif
return NEED_SKIP; return NEED_SKIP;
break; break;
default: default:
#ifdef DEBUG
fprintf(stderr,"Default type detected:%c\n",hd->typeflag); fprintf(stderr,"Default type detected:%c\n",hd->typeflag);
#endif
return NEED_SKIP; return NEED_SKIP;
/* /*
* these types have file data that follows. Set the skip and * these types have file data that follows. Set the skip and
@ -806,18 +874,24 @@ void tar_file_processor(int fd, struct filters *filters)
int sync_count = 0; int sync_count = 0;
while ((size = read(fd, &buf, BLKMULT))) { while ((size = read(fd, &buf, BLKMULT))) {
if (size != -1) { if (size != -1) {
#ifdef DEBUG
fprintf(stderr,"Read %ld bytes\n",size); fprintf(stderr,"Read %ld bytes\n",size);
#endif
ret = 0; ret = 0;
if (current==NEED_SYNC_TRAIL) { if (current==NEED_SYNC_TRAIL) {
ret = tar_trail (buf, 1, &sync_count); ret = tar_trail (buf, 1, &sync_count);
#ifdef DEBUG
fprintf(stderr,"Synchronizing trail: %d %d\n",ret,sync_count); fprintf(stderr,"Synchronizing trail: %d %d\n",ret,sync_count);
#endif
if (ret != 1) { if (ret != 1) {
current = NEED_READ; current = NEED_READ;
} }
} }
if (current==NEED_READ) { if (current==NEED_READ) {
current = ustar_rd(fd, &hdr, buf, &sb, filters); current = ustar_rd(fd, &hdr, buf, &sb, filters);
#ifdef DEBUG
fprintf(stderr,"Return %d\n",ret); fprintf(stderr,"Return %d\n",ret);
#endif
} }
if (current==NEED_SKIP || current==NEED_SKIP_FILE) { if (current==NEED_SKIP || current==NEED_SKIP_FILE) {
if (current==NEED_SKIP_FILE && if (current==NEED_SKIP_FILE &&
@ -844,13 +918,17 @@ void tar_file_processor(int fd, struct filters *filters)
} }
// Extract extra padding // Extract extra padding
#ifdef DEBUG
fprintf(stderr,"Need to remove pad:%ld %lld %lld\n",to_skip,hdr.filelen,BLKMULT-(hdr.filelen%BLKMULT)); fprintf(stderr,"Need to remove pad:%ld %lld %lld\n",to_skip,hdr.filelen,BLKMULT-(hdr.filelen%BLKMULT));
#endif
if (hdr.filelen%BLKMULT > 0) { if (hdr.filelen%BLKMULT > 0) {
ret = read(fd, &buf, BLKMULT-(hdr.filelen%BLKMULT)); ret = read(fd, &buf, BLKMULT-(hdr.filelen%BLKMULT));
#ifdef DEBUG
fprintf(stderr,"Removed %d bytes of padding\n",ret); fprintf(stderr,"Removed %d bytes of padding\n",ret);
#endif
} }
current = NEED_SYNC_TRAIL; current = NEED_SYNC_TRAIL;
} }
i++; i++;
} }
//if (i >= 10) //if (i >= 10)
@ -890,7 +968,9 @@ int main(int argc, char **argv)
// Parse tar file // Parse tar file
use_stdin = 0; use_stdin = 0;
entry = argv[i]; entry = argv[i];
#ifdef DEBUG
fprintf(stderr,"Parsing file %s\n",entry); fprintf(stderr,"Parsing file %s\n",entry);
#endif
fd = open(entry, O_RDONLY); fd = open(entry, O_RDONLY);
if (fd < 0) { if (fd < 0) {
@ -911,8 +991,9 @@ int main(int argc, char **argv)
filters.matched_filters = 0; filters.matched_filters = 0;
if (use_stdin == 1) { if (use_stdin == 1) {
// No argument specified. Use STDIN #ifdef DEBUG
fprintf(stderr,"Using STDIN\n"); fprintf(stderr,"Using STDIN\n");
#endif
set_block(0); set_block(0);
fd = 0; fd = 0;
} }