31 extern void logme(
char *msg);
37 extern char ApiUrl[200];
38 extern char accToken[100];
44 int splitLine(
char *lineToSplit,
char *separator,
char **fields){
50 char* strSplit = lineToSplit;
51 while ((token = strtok_r(strSplit , separator, &strSplit)))
52 sprintf(fields[i++],
"%s",token);
56 void extract_csv(
char *out,
char *in,
int n,
long limit,
char sep)
60 sprintf(line,
"%s",in);
61 sprintf(seps,
"%c",sep);
62 char * token = strtok(line, seps);
65 while( token != NULL ) {
68 sprintf(out,
"%s",token);
72 token = strtok(NULL, seps);
86 sprintf(sqlbuf,
"SELECT pfile_sha1 || '.' || pfile_md5 || '.' || pfile_size AS pfile_path FROM pfile WHERE pfile_pk = %ld;", pFileKey);
87 result = PQexec(
db_conn, sqlbuf);
94 sprintf(path,
"%s", PQgetvalue(result, 0, 0));
106 sprintf(sqlbuf,
"select lr.rf_pk from license_ref lr where lr.rf_shortname like '%s';", name);
107 result = PQexec(
db_conn, sqlbuf);
114 if (PQgetvalue(result, 0, 0) != NULL)
116 return atoi(PQgetvalue(result, 0, 0));
129 void dumpToFile(
const char *path,
unsigned char *content,
long size)
132 fptr = fopen(path,
"w");
136 LOG_ERROR(
"Snippet scan: Could not create temp file")
140 fwrite(content, size, 1, fptr);
145 void RestoreTempFile(
char * uploadFolder,
long key,
long realParent,
char *realName)
149 sprintf(dstName,
"%s/%ld_%ld_%s", uploadFolder,realParent,key,realName);
152 fseek(f, 0, SEEK_END);
154 unsigned char *contents = calloc(1, size);
155 memset(contents,
'\0', size);
157 size_t readSize = fread(contents, size, 1, f);
188 char auxSql[MAXCMD*4];
190 char detectedUsage[MAXCMD];
191 char detPurls[MAXCMD];
192 char detLicenses[MAXCMD];
194 char detMatch[MAXCMD];
195 char detLines[MAXCMD];
196 char detPath[MAXCMD];
199 sprintf(Cmd,
"%s/results.csv", folder);
200 FILE* file = fopen(Cmd,
"r");
202 char line[MAXCMD*10];
205 LOG_ERROR(
"Error while opening the file");
208 while (fgets(line,
sizeof(line), file)) {
209 if (resCount==0) { resCount++;
continue;}
210 if (line[0]!=0 && line[0]!=
' '){
213 char srcName[MAXCMD];
214 extract_csv(path, line, 2,MAXCMD,
',');
216 sscanf(path,
"%ld_%ld_%s",&parent,&key,srcName);
219 int lenLine=strlen(line);
220 extract_csv(detectedUsage, line, 3,lenLine,
',');
221 extract_csv(detPurls, line, 8,lenLine,
',');
222 extract_csv(detLicenses, line, 5,lenLine,
',');
223 extract_csv(detUrl, line, 9,lenLine,
',');
225 extract_csv(detMatch, line, 10,lenLine,
',');
226 extract_csv(detLines, line, 12,lenLine,
',');
227 extract_csv(detPath, line, 13,lenLine,
',');
231 for (
int i=1;i<5;i++){
233 extract_csv(aux, detLicenses, i,strlen(detLicenses),
';');
234 if (aux==NULL)
break;
238 sprintf(auxSql,
"INSERT INTO license_file(rf_fk, agent_fk, rf_timestamp, pfile_fk) VALUES(%d,%d, now(), %ld);",detLic,
Agent_pk,key);
239 result = PQexec(
db_conn,auxSql);
248 if(strcmp((
char *)detectedUsage,
"none") && (!(strcmp((
char *)detectedUsage,
"file"))||!(strcmp((
char *)detectedUsage,
"snippet")) )){
250 asprintf(&auxSQL,
"INSERT INTO scanoss_fileinfo (pfile_fk, matchtype, lineranges, purl,filepath,url) VALUES(%ld, '%s', '%s', '%s','%s','%s');", key,detectedUsage,detLines,detPurls,detPath,detUrl);
251 result = PQexec(
db_conn,auxSQL);
253 if (PQntuples(result) == 0)
274 memset(Cmd,
'\0', MAXCMD);
276 unsigned char apiurl[400];
277 unsigned char key[110];
279 if(ApiUrl[0]!=
'\0') {
280 sprintf((
char *) apiurl,
"--apiurl %s",ApiUrl);
283 memset(apiurl,0,
sizeof(apiurl));
285 if(accToken[0]!=
'\0' && accToken[0]!=
' ') {
286 sprintf((
char *)key,
"--key %s",accToken);
289 memset(key,0,
sizeof(key));
294 sprintf(Cmd,
"PYTHONPATH='/home/%s/pythondeps/' /home/%s/pythondeps/bin/scanoss-py scan %s --format=csv -o %s/results.csv %s %s", user,user,folder, folder,apiurl,key);
297 Fin = popen(Cmd,
"r");
299 LOG_ERROR(
"Snippet scan: failed to start scan %s",strerror(errno));
309 int RebuildUpload(
long upload_pk,
char *tempFolder)
320 LOG_ERROR(
"Snippet scan: Missing upload key");
328 snprintf(sqlbuf,
sizeof(sqlbuf),
329 "SELECT * from uploadtree_a, upload where upload_fk = upload_pk and upload_pk = '%ld' ",
upload_pk);
330 result = PQexec(
db_conn, sqlbuf);
333 LOG_ERROR(
"Snippet scan: Error retrieving jobs");
337 numrows = PQntuples(result);
344 for (i = 0; i < numrows; i++)
347 parent = atoi(PQgetvalue(result, i, 1));
348 realParent = atoi(PQgetvalue(result, i, 2));
349 fileMode = atol(PQgetvalue(result, i, 5));
350 asprintf(&realName,
"%s",PQgetvalue(result, i, 8));
351 for(
int j=0;j<strlen(realName);j++){
352 if(( realName[j]>=
'A' && realName[j]<=
'Z' ) ||
353 ( realName[j]>=
'a' && realName[j]<=
'z' ) ||
354 ( realName[j]>=
'0' && realName[j]<=
'9' ) || realName[j]==
'.');
355 else realName[j]=
'_';
359 if (parent != realParent && (fileMode == ((1<<28)|(1<<13)|(1<<9)))){
364 if (fileMode != ((1<<28)|(1<<13)|(1<<9))) {
365 pFileFK = atoi(PQgetvalue(result, i, 4));
367 RestoreTempFile(tempFolder, pFileFK,parent,realName);
397 printf(
"Usage: %s [file|folder]\n", Name);
398 printf(
" -i :: initialize the database, then exit.\n");
399 printf(
" -v :: verbose (-vv = more verbose)\n");
400 printf(
" -c :: Specify the directory for the system configuration.\n");
401 printf(
" -C <file path/folder path> :: run from command line.\n");
402 printf(
" -V :: print the version info, then exit.\n");
char * uploadtree_tablename
upload.uploadtree_tablename
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,...
FUNCTION char * GetUploadtreeTableName(PGconn *pgConn, int upload_pk)
Get the uploadtree table name for this upload_pk If upload_pk does not exist, return "uploadtree".
int fo_checkPQresult(PGconn *pgConn, PGresult *result, char *sql, char *FileID, int LineNumb)
Check the result status of a postgres SELECT.
int fo_RepFclose(FILE *F)
Perform an fclose.
FILE * fo_RepFread(char *Type, char *Filename)
Perform an fopen for reading only.
void Usage(char *Name)
Say how to run this program.
void dumpToFile(const char *path, unsigned char *content, long size)
Dumps the content of a file in the repository to a temporary file.
PGconn * db_conn
The connection to Database.
void ParseResults(char *folder)
Parse results from a temporary file and store results on database.
int ScanFolder(char *folder)
Scans a Temporary folder.
int Verbose
Verbose level.
FILE * openFileByKey(long pFileKey)
Open a file of the repository given its primary key.
int Agent_pk
agent identifier
int getLicenseId(unsigned char *name)
Retrieves the license id (license_ref.rf_pk) given its short name.