cd $AMBERHOME patch -p0 -N < ptraj_2010-07-16-fixFilesize.patch Author: Daniel R. Roe Date: 2010-07-16 Description: Fixes reading of hex values on big endian machines which prevented correct identification of files and/or reading incorrect number of frames in gzipped trajectory files. Use unsigned char instead of uint8_t for compatibility with obsolete compilers. Also fix calculation of # frames in Amber Trajectories by switching to long long int (to be consistent with the stat struct) and clean up the bitwise arithmetic in gzipFileSize(). ----------------------------------------------------------------------------- --- AmberTools/src/ptraj/ptraj.c 2010-07-16 20:27:16.000000000 -0400 +++ AmberTools/src/ptraj/ptraj.c 2010-07-16 20:56:55.000000000 -0400 @@ -2058,7 +2058,8 @@ int lines_per_set; int start = 1; int stop = 1; - int frame_lines, frame_size, title_size, seekable, file_size; + int frame_lines, title_size, seekable; + long long int file_size, frame_size; long int endoffset; float *binposScratch; FILE *fp; @@ -2417,20 +2418,29 @@ // Determine Uncompressed File Size for Nframes calculation file_size=0; - if (trajInfo->compressType==1) // Gzip + if (trajInfo->compressType==1) // Gzip file_size=gzipFileSize(filename); else if (trajInfo->compressType==2) // Bzip2 file_size=bzip2FileSize(filename); else if (trajInfo->compressType==3) // Zip file_size=zipFileSize(filename); - if (file_size<0) return NULL; + if (file_size<0) { + fprintf(stdout, + "ERROR in %s: Could not calculate uncompressed file size for %s\n", + ROUTINE, filename); + return NULL; + } if (file_size==0) file_size=frame_stat.st_size; if (prnlev>0) - fprintf(stdout,"[%i] Title offset %u Frame Size %u File size %lu\n", + fprintf(stdout,"[%i] Title offset %u Frame Size %u File size %lli\n", worldrank,trajInfo->titleSize,trajInfo->frameSize,file_size); - trajInfo->Nframes = (int) ((file_size - trajInfo->titleSize) / trajInfo->frameSize); - if (((file_size - trajInfo->titleSize) % (trajInfo->frameSize)) == 0) { + frame_size = (long long int) trajInfo->titleSize; + file_size = file_size - frame_size; // Subtract title size from file total size. + frame_size = (long long int) trajInfo->frameSize; + trajInfo->Nframes = (int) (file_size / frame_size); + if (prnlev>0) fprintf(stdout," File has %i frames.\n",trajInfo->Nframes); + if ( (file_size % frame_size) == 0 ) { seekable = 1; stop = trajInfo->Nframes; } else { --- AmberTools/src/ptraj/io.c 2010-07-01 00:41:40.000000000 -0400 +++ AmberTools/src/ptraj/io.c 2010-07-16 21:51:04.000000000 -0400 @@ -673,14 +673,15 @@ } } -/* DAN ROE: - * gzipFileSize() - * Return the uncompressed size in bytes of gzipped file by peeking at the - * last 4 bytes. +/* gzipFileSize() + * DRR: Return the uncompressed size in bytes of gzipped file by peeking + * at the last 4 bytes. + * NOTE: long long int should be equivalent to off_t. */ -int gzipFileSize(char *filename) { +long long int gzipFileSize(char *filename) { FILE *infile; - int b1,b2,b3,b4,val; + unsigned char b1,b2,b3,b4; + long long int val,temp; if (filename==NULL) return -1; if ( (infile = fopen(filename,"rb"))==NULL ) { @@ -697,21 +698,35 @@ fread(&b2,1,1,infile); fread(&b1,1,1,infile); - val = (b1 << 24) | (b2 << 16) + (b3 << 8) + b4; + val = 0; + temp = (long long int) b1; + temp <<= 24; + val = val | temp; + temp = (long long int) b2; + temp <<= 16; + val = val | temp; + temp = (long long int) b3; + temp <<= 8; + val = val | temp; + temp = (long long int) b4; + val = val | temp; + + //val = (b1 << 24) | (b2 << 16) + (b3 << 8) + b4; fclose(infile); - if (prnlev>0) fprintf(stdout,"gzipFileSize: Uncompressed size of %s: %i\n",filename,val); + if (prnlev>0) fprintf(stdout,"gzipFileSize: Uncompressed size of %s: %lli\n",filename,val); return val; } -/* DAN ROE: + +/* * bzip2FileSize() - * Return the uncompressed size of bzip2 file in bytes by counting all - * characters using bzcat and wc. + * DRR: Return the uncompressed size of bzip2 file in bytes by counting + * all characters using bzcat and wc. */ -int bzip2FileSize(char *filename) { - int val; +long long int bzip2FileSize(char *filename) { + long long int val; char *command; FILE *pipe; @@ -724,20 +739,21 @@ fprintf(stdout," Check that bzcat and wc are present on your system.\n"); return -1; } - fscanf(pipe,"%i",&val); + fscanf(pipe,"%lli",&val); pclose(pipe); - if (prnlev>0) fprintf(stdout,"bzip2FileSize: Uncompressed size of %s: %i\n",filename,val); + if (prnlev>0) fprintf(stdout,"bzip2FileSize: Uncompressed size of %s: %lli\n",filename,val); return val; } -/* DAN ROE: + +/* * zipFileSize() - * Return the uncompressed size of zip file in bytes by counting all - * characters using unzip and wc. + * DRR: Return the uncompressed size of zip file in bytes by counting + * all characters using unzip and wc. */ -int zipFileSize(char *filename) { - int val; +long long int zipFileSize(char *filename) { + long long int val; char *command; FILE *pipe; @@ -750,10 +766,10 @@ fprintf(stdout," Check that unzip and wc are present on your system.\n"); return -1; } - fscanf(pipe,"%i",&val); + fscanf(pipe,"%lli",&val); pclose(pipe); - if (prnlev>0) fprintf(stdout,"zipFileSize: Uncompressed size of %s: %i\n",filename,val); + if (prnlev>0) fprintf(stdout,"zipFileSize: Uncompressed size of %s: %lli\n",filename,val); return val; } @@ -774,7 +790,7 @@ * -2: Internal error */ int id_Filesig(char *filename, FILE *infile) { - int *h; + unsigned char *h; int i, type; /* Check that either filename or infile is specified, but not both */ @@ -797,7 +813,7 @@ rewind(infile); /* Read first 3 bytes from file */ - h=(int*) calloc(3,sizeof(int)); + h=(unsigned char*) calloc(3,sizeof(unsigned char)); fread(h,1,1,infile); fread(h+1,1,1,infile); fread(h+2,1,1,infile); --- AmberTools/src/ptraj/io.h 2010-07-16 20:26:17.000000000 -0400 +++ AmberTools/src/ptraj/io.h 2010-07-16 20:26:56.000000000 -0400 @@ -76,9 +76,9 @@ extern void doSystem(char *); extern char * promptToOpenFile( FILE **, char *, char *, char *); -extern int gzipFileSize(char *); -extern int bzip2FileSize(char *); -extern int zipFileSize(char *); +extern long long int gzipFileSize(char *); +extern long long int bzip2FileSize(char *); +extern long long int zipFileSize(char *); extern int id_Filesig(char *,FILE *); extern int openFile( FILE **, char *, char *); extern int promptUserResponse(FILE *, FILE *, char *, char *, int); @@ -97,9 +97,9 @@ extern void doSystem(); extern char * promptToOpenFile(); -extern int gzipFileSize(); -extern int bzip2FileSize(); -extern int zipFileSize(); +extern long long int gzipFileSize(); +extern long long int bzip2FileSize(); +extern long long int zipFileSize(); extern int id_Filesig(); extern int openFile(); extern int promptUserResponse();