int main (int argc, char *argv[]) { int i; char *Result=NULL; Cksum *Sum; if (argc==1) { /* no args? read from stdin */ Sum = SumComputeFile(stdin); if (Sum) { Result=SumToString(Sum); free(Sum); } if (Result) { printf("%s\n",Result); free(Result); } } for(i=1; i<argc; i++) { if (!strcmp(argv[i],"-")) { /* read from stdin */ Sum = SumComputeFile(stdin); if (Sum) { Result=SumToString(Sum); free(Sum); } if (Result != NULL) { printf("%s %s\n",Result,argv[i]); free(Result); Result=NULL; } } else { /* read from a file */ RecurseFiles(argv[i]); } } return(0); } /* main() */
/** * \brief test function SumToString * \test * -# Get a result from SumComputeFile() * -# Call SumToString() on the result * -# Check if the function translated the structure to a string */ void testSumToString() { Cksum *SumTest; FILE *Fin; Filename = "../testdata/test.zip"; char *Fuid = NULL; Fin = fopen(Filename,"rb"); if (Fin) { SumTest = SumComputeFile(Fin); if (SumTest) { Fuid = SumToString(SumTest); FO_ASSERT_STRING_EQUAL(Fuid, "5CBBD4E0487601E9160A5C887E5C0C1E6541B3DE.5234FC4D5F9786A51B2206B9DEEACA68.825"); free(SumTest); } fclose(Fin); } }
/********************************************************* DBLoadGold(): Insert a file into the database and repository. (This mimicks the old webgoldimport.) *********************************************************/ void DBLoadGold () { Cksum *Sum; char *Unique=NULL; char *SHA1, *MD5, *Len; char SQL[MAXCMD]; long PfileKey; char *Path; FILE *Fin; int rc; if (Debug) printf("Processing %s\n",GlobalTempFile); Fin = fopen(GlobalTempFile,"rb"); if (!Fin) { printf("ERROR upload %ld Unable to open temp file.\n",GlobalUploadKey); printf("LOG upload %ld Unable to open temp file %s from %s\n", GlobalUploadKey,GlobalTempFile,GlobalURL); fflush(stdout); DBclose(DB); exit(1); } Sum = SumComputeFile(Fin); fclose(Fin); if (ForceGroup > 0) { chown(GlobalTempFile,-1,ForceGroup); } if (!Sum) { printf("ERROR upload %ld Unable to compute checksum.\n",GlobalUploadKey); printf("LOG upload %ld Unable to compute checksum for %s from %s\n", GlobalUploadKey,GlobalTempFile,GlobalURL); fflush(stdout); DBclose(DB); exit(2); } if (Sum->DataLen <= 0) { printf("ERROR upload %ld No bytes downloaded from %s.\n",GlobalUploadKey,GlobalURL); printf("LOG upload %ld No bytes downloaded from %s to %s.\n", GlobalUploadKey,GlobalURL,GlobalTempFile); fflush(stdout); DBclose(DB); exit(3); } Unique = SumToString(Sum); if (Debug) printf("Unique %s\n",Unique); if (GlobalImportGold) { if (Debug) printf("Import Gold %s\n",Unique); rc = RepImport(GlobalTempFile,"gold",Unique,1); if (rc != 0) { printf("ERROR upload %ld Failed to import file into the repository (RepImport=%d).\n",GlobalUploadKey,rc); printf("LOG upload %ld Failed to import %s from %s into gold %s\n", GlobalUploadKey,GlobalTempFile,GlobalURL,Unique); fflush(stdout); DBclose(DB); exit(4); } /* Put the file in the "files" repository too */ Path = RepMkPath("gold",Unique); if (ForceGroup >= 0) { chown(Path,-1,ForceGroup); } } /* if GlobalImportGold */ else /* if !GlobalImportGold */ { Path = GlobalTempFile; } /* else if !GlobalImportGold */ if (Debug) printf("Path is %s\n",Path); if (!Path) { printf("ERROR upload %ld Failed to determine repository location.\n",GlobalUploadKey); printf("LOG upload %ld Failed to determine repository location for %s in gold\n", GlobalUploadKey,Unique); fflush(stdout); DBclose(DB); exit(5); } if (Debug) printf("Import files %s\n",Path); if (RepImport(Path,"files",Unique,1) != 0) { printf("ERROR upload %ld Failed to import file into the repository.\n",GlobalUploadKey); printf("LOG upload %ld Failed to import %s from %s into files\n", GlobalUploadKey,Unique,Path); fflush(stdout); DBclose(DB); exit(6); } if (ForceGroup >= 0) { chown(Path,-1,ForceGroup); } if (Path != GlobalTempFile) free(Path); /* Now update the DB */ /** Break out the sha1, md5, len components **/ SHA1 = Unique; MD5 = Unique+41; /* 40 for sha1 + 1 for '.' */ Len = Unique+41+33; /* 32 for md5 + 1 for '.' */ /** Set the pfile **/ memset(SQL,'\0',MAXCMD); snprintf(SQL,MAXCMD-1,"SELECT pfile_pk FROM pfile WHERE pfile_sha1 = '%.40s' AND pfile_md5 = '%.32s' AND pfile_size = %s;", SHA1,MD5,Len); if (DBaccess(DB,SQL) < 0) { printf("ERROR upload %ld Unable to select from the database\n",GlobalUploadKey); printf("LOG upload %ld Unable to select from the database: %s\n",GlobalUploadKey,SQL); fflush(stdout); DBclose(DB); exit(7); } /* See if pfile needs to be added */ if (DBdatasize(DB) <= 0) { /* Insert it */ memset(SQL,'\0',MAXCMD); snprintf(SQL,MAXCMD-1,"INSERT INTO pfile (pfile_sha1, pfile_md5, pfile_size) VALUES ('%.40s','%.32s',%s);", SHA1,MD5,Len); if (DBaccess(DB,SQL) < 0) { printf("ERROR upload %ld Unable to select from the database\n",GlobalUploadKey); printf("LOG upload %ld Unable to select from the database: %s\n",GlobalUploadKey,SQL); fflush(stdout); DBclose(DB); exit(8); } DBaccess(DB,"SELECT currval('pfile_pfile_pk_seq');"); } PfileKey = atol(DBgetvalue(DB,0,0)); if (Debug) printf("pfile_pk = %ld\n",PfileKey); /* Upload the DB so the pfile is linked to the upload record */ DBaccess(DB,"BEGIN;"); memset(SQL,'\0',MAXCMD); snprintf(SQL,MAXCMD-1,"SELECT * FROM upload WHERE upload_pk=%ld FOR UPDATE;",GlobalUploadKey); DBaccess(DB,SQL); memset(SQL,'\0',MAXCMD); snprintf(SQL,MAXCMD-1,"UPDATE upload SET pfile_fk=%ld WHERE upload_pk=%ld;", PfileKey,GlobalUploadKey); if (Debug) printf("SQL=%s\n",SQL); if (DBaccess(DB,SQL) < 0) { printf("ERROR upload %ld Unable to update the database\n",GlobalUploadKey); printf("LOG upload %ld Unable to update the database: %s\n",GlobalUploadKey,SQL); fflush(stdout); DBclose(DB); exit(9); } DBaccess(DB,"COMMIT;"); /* Clean up */ free(Sum); } /* DBLoadGold() */
/********************************************** RecurseFiles(): Process all files in all directories. **********************************************/ void RecurseFiles (char *S) { char NewS[FILENAME_MAX+1]; DIR *Dir; struct dirent *Entry; struct stat64 Stat; CksumFile *CF; char *Result=NULL; Cksum *Sum; Dir = opendir(S); if (Dir == NULL) { Result=NULL; /* it's a single file -- compute checksum */ CF = SumOpenFile(S); if (CF == NULL) { FILE *Fin; Fin = fopen64(S,"rb"); if (!Fin) { perror("Huh?"); fprintf(stderr,"ERROR: cannot open file \"%s\".\n",S); } else { Sum = SumComputeFile(Fin); if (Sum) { Result=SumToString(Sum); free(Sum); } fclose(Fin); } } else { Sum = SumComputeBuff(CF); if (Sum) { Result=SumToString(Sum); free(Sum); } SumCloseFile(CF); } if (Result != NULL) { printf("%s %s\n",Result,S); free(Result); Result=NULL; } return; } Entry = readdir(Dir); while(Entry != NULL) { if (!strcmp(Entry->d_name,".")) goto skip; if (!strcmp(Entry->d_name,"..")) goto skip; memset(NewS,'\0',sizeof(NewS)); strcpy(NewS,S); strcat(NewS,"/"); strcat(NewS,Entry->d_name); lstat64(NewS,&Stat); Result=NULL; if (S_ISDIR(Stat.st_mode)) RecurseFiles(NewS); else { /* compute checksum */ CF = SumOpenFile(NewS); if (CF == NULL) { FILE *Fin; Fin = fopen64(NewS,"rb"); if (!Fin) fprintf(stderr,"ERROR: Cannot open file \"%s\".\n",NewS); else { Sum = SumComputeFile(Fin); if (Sum) { Result=SumToString(Sum); free(Sum); } fclose(Fin); } } else { Sum = SumComputeBuff(CF); if (Sum) { Result=SumToString(Sum); free(Sum); } SumCloseFile(CF); } if (Result != NULL) { printf("%s %s\n",Result,NewS); free(Result); Result=NULL; } } skip: Entry = readdir(Dir); } closedir(Dir); } /* RecurseFiles() */