30 #include <sys/types.h>
34 extern void logme(
char *msg);
47 const char *folder,
const char *outputFile,
48 const char *apiurl,
const char *key)
57 LOG_ERROR(
"Snippet scan: fork() failed: %s", strerror(errno));
65 if (setenv(
"PYTHONPATH", pythonPath, 1) != 0)
75 args[argc++] = (
char *)scanossPath;
76 args[argc++] =
"scan";
77 args[argc++] = (
char *)folder;
78 args[argc++] =
"--format=csv";
80 args[argc++] = (
char *)outputFile;
82 if (apiurl != NULL && apiurl[0] !=
'\0')
84 args[argc++] =
"--apiurl";
85 args[argc++] = (
char *)apiurl;
88 if (key != NULL && key[0] !=
'\0')
90 args[argc++] =
"--key";
91 args[argc++] = (
char *)key;
96 execv(scanossPath, args);
103 if (waitpid(pid, &status, 0) < 0)
105 LOG_ERROR(
"Snippet scan: waitpid() failed: %s", strerror(errno));
109 if (WIFEXITED(status))
111 int exit_code = WEXITSTATUS(status);
114 LOG_ERROR(
"Snippet scan: scanoss-py exited with code %d", exit_code);
118 else if (WIFSIGNALED(status))
120 LOG_ERROR(
"Snippet scan: scanoss-py killed by signal %d", WTERMSIG(status));
127 extern char *baseTMP;
131 extern char ApiUrl[200];
132 extern char accToken[100];
136 int splitLine(
char *lineToSplit,
char *separator,
char **fields)
142 char *strSplit = lineToSplit;
143 while ((token = strtok_r(strSplit, separator, &strSplit)))
144 sprintf(fields[i++],
"%s", token);
148 void extract_csv(
char *out,
char *in,
int n,
long limit,
char sep)
152 sprintf(line,
"%s", in);
153 sprintf(seps,
"%c", sep);
154 char *token = strtok(line, seps);
157 while (token != NULL)
162 sprintf(out,
"%s", token);
165 token = strtok(NULL, seps);
178 sprintf(sqlbuf,
"SELECT pfile_sha1 || '.' || pfile_md5 || '.' || pfile_size AS pfile_path FROM pfile WHERE pfile_pk = %ld;", pFileKey);
179 result = PQexec(
db_conn, sqlbuf);
186 sprintf(path,
"%s", PQgetvalue(result, 0, 0));
197 sprintf(sqlbuf,
"select lr.rf_pk from license_ref lr where lr.rf_shortname like '%s';", name);
198 result = PQexec(
db_conn, sqlbuf);
205 int num_rows = PQntuples(result);
206 int return_value = -1;
209 const char *value = PQgetvalue(result, 0, 0);
212 return_value = atoi(value);
236 void dumpToFile(
const char *path,
unsigned char *content,
long size)
239 fptr = fopen(path,
"w");
243 LOG_ERROR(
"Snippet scan: Could not create temp file")
247 fwrite(content, size, 1, fptr);
252 void RestoreTempFile(
char *uploadFolder,
long key,
long realParent,
char *realName)
256 sprintf(dstName,
"%s/%ld_%ld_%s", uploadFolder, realParent, key, realName);
259 fseek(f, 0, SEEK_END);
261 unsigned char *contents = calloc(1, size);
262 memset(contents,
'\0', size);
264 size_t readSize = fread(contents, size, 1, f);
295 char auxSql[MAXCMD * 4];
297 char detectedUsage[MAXCMD];
298 char detPurls[MAXCMD];
299 char detLicenses[MAXCMD];
301 char detMatch[MAXCMD];
302 char detLines[MAXCMD];
303 char detPath[MAXCMD];
306 sprintf(Cmd,
"%s/results.csv", folder);
307 FILE *file = fopen(Cmd,
"r");
309 char line[MAXCMD * 10];
313 LOG_ERROR(
"Error while opening the file");
316 while (fgets(line,
sizeof(line), file))
323 if (line[0] != 0 && line[0] !=
' ')
327 char srcName[MAXCMD];
328 extract_csv(path, line, 2, MAXCMD,
',');
332 sscanf(path,
"%ld_%ld_%s", &parent, &key, srcName);
338 int lenLine = strlen(line);
339 extract_csv(detectedUsage, line, 3, lenLine,
',');
340 extract_csv(detPurls, line, 8, lenLine,
',');
341 extract_csv(detLicenses, line, 5, lenLine,
',');
342 extract_csv(detUrl, line, 9, lenLine,
',');
344 extract_csv(detMatch, line, 10, lenLine,
',');
345 extract_csv(detLines, line, 12, lenLine,
',');
346 extract_csv(detPath, line, 13, lenLine,
',');
350 for (
int i = 1; i < 5; i++)
353 if (strlen(detLicenses) > 1)
355 extract_csv(aux, detLicenses, i, strlen(detLicenses),
';');
365 sprintf(auxSql,
"INSERT INTO license_file(rf_fk, agent_fk, rf_timestamp, pfile_fk) VALUES(%d,%d, now(), %ld);", detLic,
Agent_pk, key);
366 result = PQexec(
db_conn, auxSql);
377 if (strcmp((
char *)detectedUsage,
"none") && (!(strcmp((
char *)detectedUsage,
"file")) || !(strcmp((
char *)detectedUsage,
"snippet"))))
380 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);
381 result = PQexec(
db_conn, auxSQL);
383 if (PQntuples(result) == 0)
405 char pythonPath[512];
406 char scanossPath[512];
407 char outputFile[512];
408 char *apiurlPtr = NULL;
415 LOG_ERROR(
"Snippet scan: PROJECTUSER not configured");
420 snprintf(pythonPath,
sizeof(pythonPath),
"/home/%s/pythondeps/", user);
421 snprintf(scanossPath,
sizeof(scanossPath),
"/home/%s/pythondeps/bin/scanoss-py", user);
422 snprintf(outputFile,
sizeof(outputFile),
"%s/results.csv", folder);
425 if (ApiUrl[0] !=
'\0')
431 if (accToken[0] !=
'\0' && accToken[0] !=
' ')
438 snprintf(logMsg,
sizeof(logMsg),
"Running scanoss-py scan on %s", folder);
448 int RebuildUpload(
long upload_pk,
char *tempFolder)
459 LOG_ERROR(
"Snippet scan: Missing upload key");
467 snprintf(sqlbuf,
sizeof(sqlbuf),
468 "SELECT * from uploadtree_a, upload where upload_fk = upload_pk and upload_pk = '%ld' ",
upload_pk);
469 result = PQexec(
db_conn, sqlbuf);
472 LOG_ERROR(
"Snippet scan: Error retrieving jobs");
476 numrows = PQntuples(result);
483 for (i = 0; i < numrows; i++)
487 parent = atoi(PQgetvalue(result, i, 1));
488 realParent = atoi(PQgetvalue(result, i, 2));
489 fileMode = atol(PQgetvalue(result, i, 5));
490 asprintf(&realName,
"%s", PQgetvalue(result, i, 8));
491 for (
int j = 0; j < strlen(realName); j++)
493 if ((realName[j] >=
'A' && realName[j] <=
'Z') ||
494 (realName[j] >=
'a' && realName[j] <=
'z') ||
495 (realName[j] >=
'0' && realName[j] <=
'9') || realName[j] ==
'.')
501 if (parent != realParent && (fileMode == ((1 << 28) | (1 << 13) | (1 << 9))))
508 if (fileMode != ((1 << 28) | (1 << 13) | (1 << 9)))
510 pFileFK = atoi(PQgetvalue(result, i, 4));
513 RestoreTempFile(tempFolder, pFileFK, parent, realName);
535 printf(
"Usage: %s [file|folder]\n", Name);
536 printf(
" -i :: initialize the database, then exit.\n");
537 printf(
" -v :: verbose (-vv = more verbose)\n");
538 printf(
" -c :: Specify the directory for the system configuration.\n");
539 printf(
" -C <file path/folder path> :: run from command line.\n");
540 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 fo_scheduler_heart(int i)
This function must be called by agents to let the scheduler know they are alive and how many items th...
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.
static int run_scanoss_command(const char *pythonPath, const char *scanossPath, const char *folder, const char *outputFile, const char *apiurl, const char *key)
Safely run scanoss-py command using fork+exec instead of popen.
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.