28 const char*
SCM_REGEX =
"/\\.git|\\.hg|\\.bzr|CVS/ROOT|\\.svn/";
44 char FileNameParent[PATH_MAX];
45 struct stat st, stParent;
46 memcpy(FileNameParent, FileName,
sizeof(FileNameParent));
47 FileNameParent[PATH_MAX-1] = 0;
48 char *lastSlashPos = strrchr(FileNameParent,
'/');
49 if (NULL != lastSlashPos)
54 FileNameParent[lastSlashPos - FileNameParent] =
'\0';
55 if (!strcmp(FileNameParent + strlen(FileNameParent) - 4,
".dir"))
60 FileNameParent[strlen(FileNameParent) - 4] =
'\0';
63 if (stat(FileNameParent, &stParent) != 0 || stat(FileName, &st) != 0)
69 if(S_ISREG(stParent.st_mode) && stParent.st_size > 0 &&
70 (st.st_size/stParent.st_size > InflateSize))
100 if (NULL == Name)
return;
102 char *LastDot = strrchr(Name,
'.');
103 if (LastDot == NULL)
return;
105 if ((LastDot[1]>=
'0')&&(LastDot[1]<=
'9'))
return;
106 if (LastDot) *LastDot = 0;
122 for(i=0;
CMD[i].Magic != NULL; i++)
130 for(i=0;
CMD[i].Magic != NULL; i++)
132 if (
CMD[i].Magic[0] ==
'\0')
continue;
134 memset(
SQL,
'\0',MAXSQL);
135 snprintf(
SQL,MAXSQL,
"SELECT mimetype_pk FROM mimetype WHERE mimetype_name = '%s';",
CMD[i].Magic);
138 else if (PQntuples(result) > 0)
140 CMD[i].
DBindex = atol(PQgetvalue(result,0,0));
146 memset(
SQL,
'\0',MAXSQL);
147 snprintf(
SQL,MAXSQL,
"INSERT INTO mimetype (mimetype_name) VALUES ('%s');",
CMD[i].Magic);
174 char *
Src,
int ProtectQuotes,
char *Replace)
177 char Temp[FILENAME_MAX];
179 memset(Dest,
'\0',DestLen);
182 while((
Src[i] !=
'\0') && (d < DestLen))
185 if (ProtectQuotes && (
Src[i]==
'\''))
187 if (d+4 >= DestLen)
return(1);
188 strcpy(Dest+d,
"'\\''");
192 else if (!ProtectQuotes && strchr(
"\\",
Src[i]))
194 if (d+2 >= DestLen)
return(1);
196 Dest[d] =
Src[i]; d++;
199 else if (Replace && (
Src[i]==
'%') && (
Src[i+1]==
's'))
202 if (d+strlen(Temp) >= DestLen)
return(1);
225 int Prune (
char *Fname,
struct stat Stat)
227 if (!Fname || (Fname[0]==
'\0'))
return(1);
229 if (S_ISLNK(Stat.st_mode) || S_ISCHR(Stat.st_mode) ||
230 S_ISBLK(Stat.st_mode) || S_ISFIFO(Stat.st_mode) ||
231 S_ISSOCK(Stat.st_mode))
237 if (S_ISREG(Stat.st_mode) && (Stat.st_nlink > 1))
243 if (S_ISREG(Stat.st_mode) && (Stat.st_size == 0))
258 char Dir[FILENAME_MAX+1];
263 memset(Dir,
'\0',
sizeof(Dir));
265 for(i=1; Dir[i] !=
'\0'; i++)
271 if (stat(Dir,&Status) == 0)
273 if (!S_ISDIR(Status.st_mode))
275 LOG_FATAL(
"'%s' is not a directory.",Dir);
282 if (rc && (errno == EEXIST)) rc=0;
285 LOG_FATAL(
"mkdir %s' failed, error: %s",Dir,strerror(errno));
293 rc = mkdir(Dir,0770);
294 if (rc && (errno == EEXIST)) rc=0;
297 LOG_FATAL(
"mkdir %s' failed, error: %s",Dir,strerror(errno));
313 if (mkdir(Fname,0770))
315 if (errno == EEXIST)
return(0);
331 if (!Fname || (Fname[0]==
'\0'))
return(0);
332 rc = lstat(Fname,&Stat);
333 if (rc != 0)
return(0);
334 return(S_ISDIR(Stat.st_mode));
347 if (!Fname || (Fname[0]==
'\0'))
return(0);
348 if (Link) rc = stat(Fname,&Stat);
349 else rc = lstat(Fname,&Stat);
350 if (rc != 0)
return(0);
351 return(S_ISREG(Stat.st_mode));
369 if (!Fin)
return(-1);
370 if (feof(Fin))
return(-1);
371 memset(Line,
'\0',MaxLine);
375 while(!feof(Fin) && (C>=0) && (i<MaxLine))
379 if (i > 0)
return(i);
405 char TestCmd[FILENAME_MAX];
407 Path = getenv(
"PATH");
408 if (!Path)
return(0);
410 memset(TestCmd,
'\0',
sizeof(TestCmd));
412 for(i=0; (j<FILENAME_MAX-1) && (Path[i] !=
'\0'); i++)
416 if ((j>0) && (TestCmd[j-1] !=
'/')) strcat(TestCmd,
"/");
418 if (
IsFile(TestCmd,1))
return(1);
420 memset(TestCmd,
'\0',
sizeof(TestCmd));
433 if (TestCmd[j-1] !=
'/') strcat(TestCmd,
"/");
435 if (
IsFile(TestCmd,1))
return(1);
437 if (!
Quiet) LOG_WARNING(
"%s not found in $PATH",Exe);
451 unsigned char * Mmap;
452 int LenIn, LenOut, Wrote;
457 if (lstat(
Src,&Stat) == -1)
return(1);
458 LenIn = Stat.st_size;
459 if (!S_ISREG(Stat.st_mode))
return(1);
461 Fin = open(
Src,O_RDONLY);
464 LOG_FATAL(
"Unable to open source '%s'",
Src);
469 Slash = strrchr(
Dst,
'/');
470 if (Slash && (Slash !=
Dst))
477 Fout = open(
Dst,O_WRONLY|O_CREAT|O_TRUNC,Stat.st_mode);
480 LOG_FATAL(
"Unable to open target '%s'",
Dst);
486 Mmap = mmap(0,LenIn,PROT_READ,MAP_PRIVATE,Fin,0);
487 if (Mmap == MAP_FAILED)
489 LOG_FATAL(
"pfile %s Unable to process file.",
Pfile_Pk);
490 LOG_WARNING(
"pfile %s Mmap failed during copy.",
Pfile_Pk);
498 while((LenOut < LenIn) && (Wrote >= 0))
500 Wrote = write(Fout,Mmap+LenOut,LenIn-LenOut);
524 if (Pid <= 0)
return(-1);
527 for(i=0; (i<MAXCHILD) && (
Queue[i].ChildPid != Pid); i++) ;
528 if (
Queue[i].ChildPid != Pid)
535 if (!WIFEXITED(Status))
539 LOG_FATAL(
"Child had unnatural death");
545 else Status = WEXITSTATUS(Status);
550 LOG_FATAL(
"Child had non-zero status: %d",Status);
551 LOG_FATAL(
"Child was to recurse on %s",
Queue[i].ChildRecurse);
559 Queue[i].ChildPid = 0;
581 for(i=0;
CMD[i].
Cmd != NULL; i++)
583 if (
CMD[i].Cmd[0] ==
'\0')
continue;
603 for(i=0;
CMD[i].
Cmd != NULL; i++)
610 for(i=0;
CMD[i].
Cmd != NULL; i++)
630 char InfoCmd[FILENAME_MAX + 64];
633 if (!
IsExe(
"pdfinfo",1))
639 snprintf(InfoCmd,
sizeof(InfoCmd),
"pdfinfo %s >/dev/null 2>&1",QuotedPath);
640 rc = system(InfoCmd);
641 if (WIFEXITED(rc)) rc = WEXITSTATUS(rc);
646 LOG_ERROR(
"Skipping pdftotext, PDF is encrypted/corrupt (pdfinfo rc=%d): %s",
668 char *Out,
char *Where)
670 char Cmd1[FILENAME_MAX * 5];
671 char ResolvedPath[FILENAME_MAX * 2];
672 char CWD[FILENAME_MAX];
674 char TempPre[FILENAME_MAX];
675 char TempFile[FILENAME_MAX];
676 char TempCwd[FILENAME_MAX];
677 char TempPost[FILENAME_MAX];
685 LOG_DEBUG(
"Extracting %s: %s > %s",Cmd,
File,Out);
691 LOG_DEBUG(
"Extracting %s in %s: %s\n",Cmd,Where,
File);
695 LOG_DEBUG(
"Testing %s: %s\n",Cmd,
File);
700 if (getcwd(CWD,
sizeof(CWD)) == NULL)
702 LOG_FATAL(
"directory name longer than %d characters",(
int)
sizeof(CWD));
705 if (
Verbose > 1){ LOG_DEBUG(
"CWD: %s\n",CWD);}
706 if ((Where != NULL) && (Where[0] !=
'\0'))
708 if (chdir(Where) != 0)
711 if (chdir(Where) != 0)
713 LOG_FATAL(
"Unable to access directory '%s'",Where);
717 if (
Verbose > 1) LOG_DEBUG(
"CWD: %s",Where);
722 memset(Cmd1,
'\0',
sizeof(Cmd1));
723 if (
TaintString(TempPre,FILENAME_MAX,CmdPre,0,Out) ||
732 snprintf(Cmd1,
sizeof(Cmd1),
"%s %s '%s/%s' %s",
733 Cmd,TempPre,TempCwd,TempFile,TempPost);
734 snprintf(ResolvedPath,
sizeof(ResolvedPath),
"'%s/%s'",TempCwd,TempFile);
738 snprintf(Cmd1,
sizeof(Cmd1),
"%s %s '%s' %s",
739 Cmd,TempPre,TempFile,TempPost);
740 snprintf(ResolvedPath,
sizeof(ResolvedPath),
"'%s'",TempFile);
743 if (strcmp(Cmd,
"pdftotext")==0)
745 char TimedCmd[FILENAME_MAX * 5 + 96];
747 char *CmdMemLimitEnv;
748 int CmdTimeout = DEFAULT_PDF_CMD_TIMEOUT;
749 char *CmdTimeoutEnv = getenv(
"FOSSOLOGY_CMD_TIMEOUT");
750 if (CmdTimeoutEnv && atoi(CmdTimeoutEnv) > 0) CmdTimeout = atoi(CmdTimeoutEnv);
756 if (Out && Out[0]) unlink(Out);
758 LOG_ERROR(
"Unable to change directory to %s", CWD);
762 CmdMemLimitEnv = getenv(
"FOSSOLOGY_CMD_MEM_LIMIT_KB");
763 if (CmdMemLimitEnv && atol(CmdMemLimitEnv) > 0)
764 CmdMemLimitKB = atol(CmdMemLimitEnv);
766 CmdMemLimitKB = DEFAULT_CMD_MEM_LIMIT_KB;
768 if (CmdMemLimitKB > 0)
769 snprintf(TimedCmd,
sizeof(TimedCmd),
"ulimit -v %ld 2>/dev/null; timeout -k 10 %ds %s",
770 CmdMemLimitKB,CmdTimeout,Cmd1);
772 snprintf(TimedCmd,
sizeof(TimedCmd),
"timeout -k 10 %ds %s",CmdTimeout,Cmd1);
775 rc = system(TimedCmd);
778 LOG_ERROR(
"Process killed by signal (%d), skipping file: %s",WTERMSIG(rc),Cmd1);
784 if (WIFEXITED(rc)) rc = WEXITSTATUS(rc);
786 if (rc == 124 || rc == 137)
788 LOG_ERROR(
"Command killed (timeout %ds or memory limit), skipping file: %s",CmdTimeout,Cmd1);
794 if (Killed && Out && Out[0]) unlink(Out);
801 LOG_ERROR(
"Process killed by signal (%d): %s",WTERMSIG(rc),Cmd1);
804 if (WIFEXITED(rc)) rc = WEXITSTATUS(rc);
807 if (
Verbose) LOG_DEBUG(
"in %s -- %s ; rc=%d",Where,Cmd1,rc);
810 LOG_ERROR(
"Unable to change directory to %s", CWD);
811 if (
Verbose > 1) LOG_DEBUG(
"CWD: %s",CWD);
826 LOG_FATAL(
"Failed to initialize magic cookie");
851 if (strcmp(pExt,
".dsc")==0)
856 if ((fp = fopen(
Filename,
"r")) == NULL)
return 0;
858 while ((c = fgetc(fp)) != EOF && j < 500 ){
863 if ((strstr(line,
"-----BEGIN PGP SIGNED MESSAGE-----") && strstr(line,
"Source:")) ||
864 (strstr(line,
"Format:") && strstr(line,
"Source:") && strstr(line,
"Version:")))
884 unsigned char hdr[6];
888 if ((fp = fopen(
Filename,
"rb")) == NULL)
return 0;
889 n = fread(hdr, 1,
sizeof(hdr), fp);
893 if ((hdr[0] == 0xC7 && hdr[1] == 0x71) || (hdr[0] == 0x71 && hdr[1] == 0xC7))
897 return (memcmp(hdr,
"070707", 6) == 0 || memcmp(hdr,
"070701", 6) == 0 ||
898 memcmp(hdr,
"070702", 6) == 0);
918 if (strstr(Type,
"Debian binary package"))
920 strcpy(TypeBuf,
"application/x-debian-package");
924 if (strstr(Type,
"ISO 9660"))
926 strcpy(TypeBuf,
"application/x-iso9660-image");
935 rc3 =
RunCommand(
"7z",
"t -y -pjunk",
Filename,
" 2>&1 | grep 'Wrong password' >/dev/null",NULL,NULL);
938 LOG_ERROR(
"'%s' cannot be unpacked, password required.",
Filename);
944 strcpy(TypeBuf,
"application/x-7z-w-compressed");
948 if (strstr(Type,
" ext2 "))
950 strcpy(TypeBuf,
"application/x-ext2");
954 if (strstr(Type,
" ext3 "))
956 strcpy(TypeBuf,
"application/x-ext3");
960 if (strstr(Type,
"x86 boot sector, mkdosfs"))
962 strcpy(TypeBuf,
"application/x-fat");
966 if (strstr(Type,
"NTFS"))
968 strcpy(TypeBuf,
"application/x-ntfs");
972 if (strstr(Type,
"x86 boot"))
974 strcpy(TypeBuf,
"application/x-x86_boot");
996 if (Type == NULL)
return(-1);
1004 if ((strcmp(basename(
Filename),
".bss") == 0) && (strstr(Type,
"octet")))
1006 Type = strdup(
"text/plain");
1012 strncpy(TypeBuf, Type,
sizeof(TypeBuf));
1018 strcpy(TypeBuf,
"application/x-cpio");
1021 if (strstr(Type,
"octet" ))
1028 if (strstr(Type,
"msword") || strstr(Type,
"vnd.ms"))
1029 strcpy(TypeBuf,
"application/x-7z-w-compressed");
1032 if (strstr(Type,
"application/x-exe") ||
1033 strstr(Type,
"application/x-shellscript"))
1036 if ((rc==0) || (rc==1) || (rc==2) || (rc==51))
1038 strcpy(TypeBuf,
"application/x-zip");
1041 else if (strstr(Type,
"application/x-tar"))
1051 for(i=0; (
CMD[i].
Cmd != NULL) && (
Match == -1); i++)
1053 if (
CMD[i].Status == 0)
continue;
1054 if (
CMD[i].Type == CMD_DEFAULT)
1059 if (!strstr(TypeBuf,
CMD[i].Magic))
1071 LOG_DEBUG(
"MISS: Type=%s %s",TypeBuf,
Filename);
1100 if (d->Name) free(d->Name);
1112 dirlist *dlist=NULL, *dhead=NULL;
1114 struct dirent *Entry;
1118 Dir = opendir(Fullname);
1119 if (Dir == NULL)
return(NULL);
1121 Entry = readdir(Dir);
1122 while(Entry != NULL)
1124 if (!strcmp(Entry->d_name,
"."))
goto skip;
1125 if (!strcmp(Entry->d_name,
".."))
goto skip;
1129 LOG_FATAL(
"Failed to allocate dirlist memory");
1132 dhead->Name = (
char *)malloc(strlen(Entry->d_name)+1);
1135 LOG_FATAL(
"Failed to allocate dirlist.Name memory");
1138 memset(dhead->Name,
'\0',strlen(Entry->d_name)+1);
1139 strcpy(dhead->Name,Entry->d_name);
1141 dhead->Next = dlist;
1149 while(dhead->Next && (strcmp(dhead->Name,dhead->Next->Name) > 0))
1153 dhead->Name = dhead->Next->Name;
1154 dhead->Next->Name = Name;
1155 dhead = dhead->Next;
1161 Entry = readdir(Dir);
1167 printf(
"Directory: %s\n",Fullname);
1168 for(dhead=dlist; dhead; dhead=dhead->
Next)
1170 printf(
" %s\n",dhead->Name);
1187 void SetDir (
char *Dest,
int DestLen,
char *Smain,
char *Sfile)
1191 memset(Dest,
'\0',DestLen);
1196 if (Sfile && (Sfile[0]==
'/')) Sfile++;
1204 if (!memcmp(Sfile,
"../",3)) { Sfile+=3; i=1; }
1205 else if (!memcmp(Sfile,
"./",2)) { Sfile+=2; i=1; }
1207 while(Sfile && !memcmp(Sfile,
"../",3)) Sfile+=3;
1210 if ((strlen(Dest) > 0) && (Last(Smain) !=
'/') && (Sfile[0] !=
'/'))
1212 if (Sfile) strcat(Dest,Sfile);
1214 for(i=strlen(Dest)-1; (i>=0) && (Dest[i] !=
'/'); i--)
1227 LOG_DEBUG(
"Container:");
1228 printf(
" Source: %s\n",CI->Source);
1229 printf(
" Partdir: %s\n",CI->
Partdir);
1230 printf(
" Partname: %s\n",CI->
Partname);
1233 printf(
" HasChild: %d\n",CI->
HasChild);
1234 printf(
" Pruned: %d\n",CI->
Pruned);
1235 printf(
" Corrupt: %d\n",CI->
Corrupt);
1236 printf(
" Artifact: %d\n",CI->
Artifact);
1237 printf(
" IsDir: %d\n",CI->
IsDir);
1240 printf(
" pfile_pk: %ld\n",CI->
pfile_pk);
1242 printf(
" Parent Cmd: %d\n",CI->
PI.Cmd);
1262 if (!Fuid || (Fuid[0] ==
'\0'))
return(1);
1265 memset(
SQL,
'\0',MAXSQL);
1266 snprintf(
SQL,MAXSQL,
"SELECT pfile_pk,pfile_mimetypefk,pfile_sha256 FROM pfile "
1267 "WHERE pfile_sha1 = '%.40s' AND pfile_md5 = '%.32s' AND pfile_size = '%s';",
1268 Fuid,Fuid+41,Fuid+140);
1273 if (PQntuples(result) == 0)
1279 memset(
SQL,
'\0',MAXSQL);
1282 snprintf(
SQL,MAXSQL,
"INSERT INTO pfile (pfile_sha1,pfile_md5,pfile_sha256,pfile_size,pfile_mimetypefk) "
1283 "VALUES ('%.40s','%.32s','%.64s','%s','%ld');",
1284 Fuid,Fuid+41,Fuid+74,Fuid+140,
CMD[CI->
PI.Cmd].
DBindex);
1288 snprintf(
SQL,MAXSQL,
"INSERT INTO pfile (pfile_sha1,pfile_md5,pfile_sha256,pfile_size) VALUES ('%.40s','%.32s','%.64s','%s');",
1289 Fuid,Fuid+41,Fuid+74,Fuid+140);
1293 if ((result==0) || ((PQresultStatus(result) != PGRES_COMMAND_OK) &&
1294 (strncmp(
"23505", PQresultErrorField(result, PG_DIAG_SQLSTATE),5))))
1296 LOG_ERROR(
"Error inserting pfile, %s.",
SQL);
1303 memset(
SQL,
'\0',MAXSQL);
1304 snprintf(
SQL,MAXSQL,
"SELECT pfile_pk,pfile_mimetypefk,pfile_sha256 FROM pfile "
1305 "WHERE pfile_sha1 = '%.40s' AND pfile_md5 = '%.32s' AND pfile_sha256 = '%.64s' AND pfile_size = '%s';",
1306 Fuid,Fuid+41,Fuid+74,Fuid+140);
1312 Val = PQgetvalue(result,0,0);
1317 tempMimeType = atol(PQgetvalue(result,0,1));
1318 tempSha256 = PQgetvalue(result,0,2);
1324 memset(
SQL,
'\0',MAXSQL);
1325 snprintf(
SQL,MAXSQL,
"UPDATE pfile SET pfile_mimetypefk = '%ld' WHERE pfile_pk = '%ld';",
1331 if (strncasecmp(tempSha256, Fuid+74, 64) != 0)
1334 memset(
SQL,
'\0',MAXSQL);
1335 snprintf(
SQL,MAXSQL,
"UPDATE pfile SET pfile_sha256 = '%.64s' WHERE pfile_pk = '%ld';",
1370 err = regcomp (&preg,
SCM_REGEX, REG_NOSUB | REG_EXTENDED);
1375 match = regexec (&preg, sourcefilename, 0, NULL, 0);
1380 if (
Verbose) LOG_DEBUG(
"match found %s",sourcefilename);
1382 else if(
match == REG_NOMATCH)
1385 if (
Verbose) LOG_DEBUG(
"match not found %s",sourcefilename);
1391 size = regerror (err, &preg, NULL, 0);
1392 text = malloc (
sizeof (*text) * size);
1395 regerror (err, &preg, text, size);
1396 LOG_ERROR(
"Error regexc '%s' '%s' return %d, error %s",
SCM_REGEX,sourcefilename,
match,text);
1400 LOG_ERROR(
"Not enough memory (%lu)",
sizeof (*text) * size);
1408 LOG_ERROR(
"Error regcomp(%d)",err);
1428 char UfileName[1024];
1443 memset(UfileName,
'\0',
sizeof(UfileName));
1447 snprintf(UfileName,
sizeof(UfileName),
"SELECT upload_filename FROM upload WHERE upload_pk = %s;",
Upload_Pk);
1448 result = PQexec(
pgConn, UfileName);
1450 memset(UfileName,
'\0',
sizeof(UfileName));
1451 ufile_name = PQgetvalue(result,0,0);
1453 if (strchr(ufile_name,
'/')) ufile_name = strrchr(ufile_name,
'/')+1;
1461 if ((Len > 4) && !strcmp(CI->
Partname+Len-4,
".dir"))
1462 strcpy(UfileName,
"artifact.dir");
1463 else if ((Len > 9) && !strcmp(CI->
Partname+Len-9,
".unpacked"))
1464 strcpy(UfileName,
"artifact.unpacked");
1465 else if ((Len > 5) && !strcmp(CI->
Partname+Len-5,
".meta"))
1466 strcpy(UfileName,
"artifact.meta");
1468 strcpy(UfileName,
"artifact");
1475 LOG_WARNING(
"Error escaping filename with multibyte character set (%s).", CI->
Partname);
1479 strncpy(UfileName, EscBuf,
sizeof(UfileName));
1493 for (cp=UfileName; *cp; cp++)
if (!isprint(*cp) || (*cp==
'/') || (*cp==
'\\')) *cp =
'~';
1497 memset(
SQL,
'\0',MAXSQL);
1501 snprintf(
SQL,MAXSQL,
"INSERT INTO %s (parent,pfile_fk,ufile_mode,ufile_name,upload_fk) VALUES (%ld,%ld,%ld,E'%s',%s);",
1513 snprintf(
SQL,MAXSQL,
"INSERT INTO %s (upload_fk,pfile_fk,ufile_mode,ufile_name) VALUES (%s,%ld,%ld,E'%s');",
1520 memset(
SQL,
'\0',MAXSQL);
1521 snprintf(
SQL,MAXSQL,
"SELECT currval('uploadtree_uploadtree_pk_seq');");
1528 fo_scheduler_heart(1);
1553 memset(FuidNew,
'\0',
sizeof(FuidNew));
1555 strncpy(FuidNew, Fuid, 74);
1557 strcat(FuidNew,Fuid+140);
1564 LOG_ERROR(
"Failed to import '%s' as '%s' into the repository",CI->Source,FuidNew);
1568 if (
Verbose) LOG_DEBUG(
"Repository[%s]: insert '%s' as '%s'",
1603 if (CI->Source[0] ==
'\0')
return(0);
1604 memset(Fuid,0,
sizeof(Fuid));
1611 for(i=0; CI->Source[i] !=
'\0'; i++)
1613 if (isalnum(CI->Source[i]) ||
1614 strchr(
" `~!@#$%^*()-=_+[]{}\\|;:',./?",CI->Source[i]))
1616 else fprintf(
ListOutFile,
"&#x%02x;",(
int)(CI->Source[i])&0xff);
1625 for(i=0; CI->
Partname[i] !=
'\0'; i++)
1628 strchr(
" `~!@#$%^*()-=_+[]{}\\|;:',./?",CI->
Partname[i]))
1636 if ((CI->
PI.Cmd >= 0) && (
CMD[CI->
PI.Cmd].
Type != CMD_DEFAULT))
1641 else if (S_ISDIR(CI->
Stat.st_mode))
1670 if (S_ISDIR(CI->
Stat.st_mode))
1674 else if (S_ISREG(CI->
Stat.st_mode))
1692 if (CI->
Stat.st_mtime)
1699 if (CI->
Stat.st_ctime)
1709 if (S_ISREG(CI->
Stat.st_mode) && !CI->
Pruned)
1715 memset(SHA256,
'\0',
sizeof(SHA256));
1718 if(calc_sha256sum(CI->Source, SHA256))
1720 LOG_FATAL(
"Unable to calculate SHA256 of %s\n", CI->Source);
1731 for(i=0; i<20; i++) { sprintf(Fuid+0+i*2,
"%02X",Sum->
SHA1digest[i]); }
1733 for(i=0; i<16; i++) { sprintf(Fuid+41+i*2,
"%02X",Sum->
MD5digest[i]); }
1735 for(i=0; i<64; i++) { sprintf(Fuid+74+i,
"%c",SHA256[i]); }
1737 snprintf(Fuid+140,
sizeof(Fuid)-140,
"%Lu",(
long long unsigned int)Sum->
DataLen);
1745 Fin = fopen(CI->Source,
"rb");
1751 for(i=0; i<20; i++) { sprintf(Fuid+0+i*2,
"%02X",Sum->
SHA1digest[i]); }
1753 for(i=0; i<16; i++) { sprintf(Fuid+41+i*2,
"%02X",Sum->
MD5digest[i]); }
1755 for(i=0; i<64; i++) { sprintf(Fuid+74+i,
"%c",SHA256[i]); }
1757 snprintf(Fuid+140,
sizeof(Fuid)-140,
"%Lu",(
long long unsigned int)Sum->
DataLen);
1783 char RMcmd[FILENAME_MAX];
1785 memset(RMcmd,
'\0',
sizeof(RMcmd));
1786 snprintf(RMcmd, FILENAME_MAX -1,
"rm -rf '%s' ", dirpath);
1807 char HostName[2048];
1808 struct timeval time_st;
1810 NewPath = strdup(DirPath);
1812 if ((subs = strstr(NewPath,
"%U")) )
1815 if (gettimeofday(&time_st, 0))
1818 LOG_WARNING(
"gettimeofday() failure.");
1819 time_st.tv_usec = 999999;
1823 snprintf(TmpPath,
sizeof(TmpPath),
"%s%ul", NewPath, (
unsigned)time_st.tv_usec);
1825 NewPath = strdup(TmpPath);
1828 if ((subs = strstr(NewPath,
"%H")) )
1831 gethostname(HostName,
sizeof(HostName));
1834 snprintf(TmpPath,
sizeof(TmpPath),
"%s%s%s", NewPath, HostName, subs+2);
1836 NewPath = strdup(TmpPath);
1839 if ((subs = strstr(NewPath,
"%R")) )
1844 snprintf(TmpPath,
sizeof(TmpPath),
"%s%s%s", NewPath,
fo_config_get(
sysconfig,
"FOSSOLOGY",
"path", NULL), subs+2);
1846 NewPath = strdup(TmpPath);
1852 void deleteTmpFiles(
char *dir)
1865 fprintf(stderr,
"Universal Unpacker, %s, compiled %s %s\n",
1866 Version,__DATE__,__TIME__);
1867 fprintf(stderr,
"Usage: %s [options] file [file [file...]]\n",Name);
1868 fprintf(stderr,
" Extracts each file.\n");
1869 fprintf(stderr,
" If filename specifies a directory, then extracts everything in it.\n");
1870 fprintf(stderr,
" Unpack Options:\n");
1871 fprintf(stderr,
" -h :: help (print this message), then exit.\n");
1872 fprintf(stderr,
" -C :: force continue when unpack tool fails.\n");
1873 fprintf(stderr,
" -d dir :: specify alternate extraction directory. %%U substitutes a unique ID.\n");
1874 fprintf(stderr,
" Default is the same directory as file (usually not a good idea).\n");
1875 fprintf(stderr,
" -m # :: number of CPUs to use (default: 1).\n");
1876 fprintf(stderr,
" -P :: prune files: remove links, >1 hard links, zero files, etc.\n");
1877 fprintf(stderr,
" -R :: recursively unpack (same as '-r -1')\n");
1878 fprintf(stderr,
" -r # :: recurse to a specified depth (0=none/default, -1=infinite)\n");
1879 fprintf(stderr,
" -X :: remove recursive sources after unpacking.\n");
1880 fprintf(stderr,
" -x :: remove ALL unpacked files when done (clean up).\n");
1881 fprintf(stderr,
" I/O Options:\n");
1882 fprintf(stderr,
" -L out :: Generate a log of files extracted (in XML) to out.\n");
1883 fprintf(stderr,
" -F :: Using files from the repository.\n");
1884 fprintf(stderr,
" -i :: Initialize the database queue system, then exit.\n");
1885 fprintf(stderr,
" -I :: Ignore SCM Data.\n");
1886 fprintf(stderr,
" -Q :: Using scheduler queue system. (Includes -F)\n");
1887 fprintf(stderr,
" If -L is used, unpacked files are placed in 'files'.\n");
1888 fprintf(stderr,
" -T rep :: Set gold repository name to 'rep' (for testing)\n");
1889 fprintf(stderr,
" -t rep :: Set files repository name to 'rep' (for testing)\n");
1890 fprintf(stderr,
" -A :: do not set the initial DB container as an artifact.\n");
1891 fprintf(stderr,
" -f :: force processing files that already exist in the DB.\n");
1892 fprintf(stderr,
" -q :: quiet (generate no output).\n");
1893 fprintf(stderr,
" -U upload_pk :: upload to unpack (implies -RQ). Writes to db.\n");
1894 fprintf(stderr,
" -v :: verbose (-vv = more verbose).\n");
1895 fprintf(stderr,
" -V :: print the version info, then exit.\n");
1896 fprintf(stderr,
"Currently identifies and processes:\n");
1897 fprintf(stderr,
" Compressed files: .Z .gz .bz .bz2 upx\n");
1898 fprintf(stderr,
" Archives files: tar cpio zip jar ar rar cab\n");
1899 fprintf(stderr,
" Data files: pdf\n");
1900 fprintf(stderr,
" Installer files: rpm deb\n");
1901 fprintf(stderr,
" File images: iso9660(plain/Joliet/Rock Ridge) FAT(12/16/32) ext2/ext3 NTFS\n");
1902 fprintf(stderr,
" Boot partitions: x86, vmlinuz\n");
1929 if (!ExcludePatterns || !
Filename)
return 0;
1931 char *patternsCopy = strdup(ExcludePatterns);
1932 if (!patternsCopy)
return 0;
1934 char *pattern = strtok(patternsCopy,
",");
1935 while (pattern != NULL) {
1937 if (
Verbose) LOG_DEBUG(
"Excluding: %s (matched substring: %s)",
Filename, pattern);
1941 pattern = strtok(NULL,
",");
char SQL[256]
SQL query to execute.
char * uploadtree_tablename
upload.uploadtree_tablename
Cksum * SumComputeBuff(CksumFile *CF)
Compute the checksum, allocate and return a Cksum containing the sum value.
Cksum * SumComputeFile(FILE *Fin)
Compute the checksum, allocate and return a string containing the sum value.
CksumFile * SumOpenFile(char *Fname)
Open and mmap a file.
void SumCloseFile(CksumFile *CF)
Close a file that was opened with SumOpenFile()
int Verbose
Verbose level.
PGconn * pgConn
Database connection.
Stores all extern variables used by the agent.
char * Pfile_Pk
Pfile pk in DB.
int ReunpackSwitch
Set if the uploadtree records are missing from db.
int UseRepository
Using files from the repository?
int TotalDirectories
Number of directories.
unpackqueue Queue[MAXCHILD+1]
Manage children.
int Quiet
Run in quiet mode?
int TotalFiles
Number of regular files.
int TotalArtifacts
Number of artifacts.
cmdlist CMD[]
Global command table.
char * Upload_Pk
Upload pk in DB.
char REP_FILES[16]
Files repository name.
FILE * ListOutFile
File to store unpack list.
long TotalItems
Number of records inserted.
int IgnoreSCMData
1: Ignore SCM data, 0: dont ignore it.
int TotalCompressedFiles
Number of compressed files.
int ForceDuplicate
When using db, should it process duplicates?
int ForceContinue
Force continue when unpack tool fails?
magic_t MagicCookie
for Magic
char * fo_config_get(fo_conf *conf, const char *group, const char *key, GError **error)
Gets an element based on its group name and key name. If the group or key is not found,...
int fo_checkPQresult(PGconn *pgConn, PGresult *result, char *sql, char *FileID, int LineNumb)
Check the result status of a postgres SELECT.
int fo_checkPQcommand(PGconn *pgConn, PGresult *result, char *sql, char *FileID, int LineNumb)
Check the result status of a postgres commands (not select) If an error occured, write the error to s...
int fo_RepImport(char *Source, char *Type, char *Filename, int Link)
Import a file into the repository.
int fo_RepExist(char *Type, char *Filename)
Determine if a file exists.
void fo_scheduler_disconnect(int retcode)
Disconnect the scheduler connection.
Store file handler and mmap of a file.
Store check sum of a file.
uint8_t SHA1digest[20]
SHA1 digest of the file.
uint64_t DataLen
Size of the file.
uint8_t MD5digest[16]
MD5 digest of the file.
Structure for storing information about a particular file.
char Partname[FILENAME_MAX]
char PartnameNew[FILENAME_MAX]
char Partdir[FILENAME_MAX]
Store the results of a regex match.
static char * Dst
Destination location.
static char * Src
Souce location.
int RunCommand(char *Cmd, char *CmdPre, char *File, char *CmdPost, char *Out, char *Where)
Try a command and return command code.
int IsFile(char *Fname, int Link)
Given a filename, is it a file?
int DisplayContainerInfo(ContainerInfo *CI, int Cmd)
Print what can be printed in XML.
void DebugContainerInfo(ContainerInfo *CI)
Print a ContainerInfo structure.
int RemoveDir(char *dirpath)
Remove all files under dirpath (rm -rf)
int MkDirs(char *Fname)
Same as command-line "mkdir -p".
int Prune(char *Fname, struct stat Stat)
Given a filename and its stat, prune it.
int IsCpioFile(char *Filename)
Check the leading magic bytes for a cpio archive.
int TestSCMData(char *sourcefilename)
Search for SCM data in the filename.
int InitMagic()
Open and load Magic file Initializes global MagicCookie.
void CheckCommands(int Show)
Make sure all commands are usable.
int IsDebianSourceFile(char *Filename)
Read file to see if it is a Debian source file.
int MkDir(char *Fname)
Smart mkdir.
int TaintString(char *Dest, int DestLen, char *Src, int ProtectQuotes, char *Replace)
Protect strings intelligently.
void SetDir(char *Dest, int DestLen, char *Smain, char *Sfile)
Set a destination directory name.
int ParentWait()
Wait for a child. Sets child status.
void RemovePostfix(char *Name)
get rid of the postfix
void OctetType(char *Filename, char *TypeBuf)
Figure out the real type of "octet" files in case we can unarchive them.
int AddToRepository(ContainerInfo *CI, char *Fuid, int Mask)
Add a ContainerInfo record to the repository AND to the database.
void FreeDirList(dirlist *DL)
Free a list of files in a directory list.
int DBInsertPfile(ContainerInfo *CI, char *Fuid)
Insert a Pfile record. Sets the pfile_pk in CI.
int DBInsertUploadTree(ContainerInfo *CI, int Mask)
Insert an UploadTree record.
int IsPdfProcessable(const char *QuotedPath)
Structurally validate a PDF before handing it to pdftotext.
void Usage(char *Name, char *Version)
Display program usage.
void InitCmd()
Initialize the metahandler CMD table.
int IsExe(char *Exe, int Quiet)
Check if the executable exists.
void SafeExit(int rc)
Close scheduler and database connections, then exit.
int FindCmd(char *Filename)
Given a file name, determine the type of extraction command. This uses Magic.
int ShouldExclude(char *Filename, const char *ExcludePatterns)
Determines if a file or folder should be excluded.
int CopyFile(char *Src, char *Dst)
Copy a file. For speed: mmap and save.
int IsInflatedFile(char *FileName, int InflateSize)
Test if the file is a compression bomb.
char * PathCheck(char *DirPath)
Check if path contains a "%U" or "%H". If so, substitute a unique ID for U.
void SQLNoticeProcessor(void *arg, const char *message)
Dummy postgresql notice processor. This prevents Notices from being written to stderr.
dirlist * MakeDirList(char *Fullname)
Create a list of files in a directory.
int ReadLine(FILE *Fin, char *Line, int MaxLine)
Read a command from a stream.
int IsDir(char *Fname)
Given a filename, is it a directory?