31 extern void logme(
char *msg);
37 extern char ApiUrl[200];
38 extern char accToken[100];
42 int splitLine(
char *lineToSplit,
char *separator,
char **fields)
48 char *strSplit = lineToSplit;
49 while ((token = strtok_r(strSplit, separator, &strSplit)))
50 sprintf(fields[i++],
"%s", token);
54 void extract_csv(
char *out,
char *in,
int n,
long limit,
char sep)
58 sprintf(line,
"%s", in);
59 sprintf(seps,
"%c", sep);
60 char *token = strtok(line, seps);
68 sprintf(out,
"%s", token);
71 token = strtok(NULL, seps);
84 sprintf(sqlbuf,
"SELECT pfile_sha1 || '.' || pfile_md5 || '.' || pfile_size AS pfile_path FROM pfile WHERE pfile_pk = %ld;", pFileKey);
85 result = PQexec(
db_conn, sqlbuf);
92 sprintf(path,
"%s", PQgetvalue(result, 0, 0));
103 sprintf(sqlbuf,
"select lr.rf_pk from license_ref lr where lr.rf_shortname like '%s';", name);
104 result = PQexec(
db_conn, sqlbuf);
111 int num_rows = PQntuples(result);
112 int return_value = -1;
115 const char *value = PQgetvalue(result, 0, 0);
118 return_value = atoi(value);
142 void dumpToFile(
const char *path,
unsigned char *content,
long size)
145 fptr = fopen(path,
"w");
149 LOG_ERROR(
"Snippet scan: Could not create temp file")
153 fwrite(content, size, 1, fptr);
158 void RestoreTempFile(
char *uploadFolder,
long key,
long realParent,
char *realName)
162 sprintf(dstName,
"%s/%ld_%ld_%s", uploadFolder, realParent, key, realName);
165 fseek(f, 0, SEEK_END);
167 unsigned char *contents = calloc(1, size);
168 memset(contents,
'\0', size);
170 size_t readSize = fread(contents, size, 1, f);
201 char auxSql[MAXCMD * 4];
203 char detectedUsage[MAXCMD];
204 char detPurls[MAXCMD];
205 char detLicenses[MAXCMD];
207 char detMatch[MAXCMD];
208 char detLines[MAXCMD];
209 char detPath[MAXCMD];
212 sprintf(Cmd,
"%s/results.csv", folder);
213 FILE *file = fopen(Cmd,
"r");
215 char line[MAXCMD * 10];
219 LOG_ERROR(
"Error while opening the file");
222 while (fgets(line,
sizeof(line), file))
229 if (line[0] != 0 && line[0] !=
' ')
233 char srcName[MAXCMD];
234 extract_csv(path, line, 2, MAXCMD,
',');
238 sscanf(path,
"%ld_%ld_%s", &parent, &key, srcName);
244 int lenLine = strlen(line);
245 extract_csv(detectedUsage, line, 3, lenLine,
',');
246 extract_csv(detPurls, line, 8, lenLine,
',');
247 extract_csv(detLicenses, line, 5, lenLine,
',');
248 extract_csv(detUrl, line, 9, lenLine,
',');
250 extract_csv(detMatch, line, 10, lenLine,
',');
251 extract_csv(detLines, line, 12, lenLine,
',');
252 extract_csv(detPath, line, 13, lenLine,
',');
256 for (
int i = 1; i < 5; i++)
259 if (strlen(detLicenses) > 1)
261 extract_csv(aux, detLicenses, i, strlen(detLicenses),
';');
271 sprintf(auxSql,
"INSERT INTO license_file(rf_fk, agent_fk, rf_timestamp, pfile_fk) VALUES(%d,%d, now(), %ld);", detLic,
Agent_pk, key);
272 result = PQexec(
db_conn, auxSql);
283 if (strcmp((
char *)detectedUsage,
"none") && (!(strcmp((
char *)detectedUsage,
"file")) || !(strcmp((
char *)detectedUsage,
"snippet"))))
286 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);
287 result = PQexec(
db_conn, auxSQL);
289 if (PQntuples(result) == 0)
313 memset(Cmd,
'\0', MAXCMD);
315 unsigned char apiurl[400];
316 unsigned char key[110];
318 if (ApiUrl[0] !=
'\0')
320 sprintf((
char *)apiurl,
"--apiurl %s", ApiUrl);
323 memset(apiurl, 0,
sizeof(apiurl));
325 if (accToken[0] !=
'\0' && accToken[0] !=
' ')
327 sprintf((
char *)key,
"--key %s", accToken);
330 memset(key, 0,
sizeof(key));
335 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);
338 Fin = popen(Cmd,
"r");
341 LOG_ERROR(
"Snippet scan: failed to start scan %s", strerror(errno));
352 int RebuildUpload(
long upload_pk,
char *tempFolder)
363 LOG_ERROR(
"Snippet scan: Missing upload key");
371 snprintf(sqlbuf,
sizeof(sqlbuf),
372 "SELECT * from uploadtree_a, upload where upload_fk = upload_pk and upload_pk = '%ld' ",
upload_pk);
373 result = PQexec(
db_conn, sqlbuf);
376 LOG_ERROR(
"Snippet scan: Error retrieving jobs");
380 numrows = PQntuples(result);
387 for (i = 0; i < numrows; i++)
391 parent = atoi(PQgetvalue(result, i, 1));
392 realParent = atoi(PQgetvalue(result, i, 2));
393 fileMode = atol(PQgetvalue(result, i, 5));
394 asprintf(&realName,
"%s", PQgetvalue(result, i, 8));
395 for (
int j = 0; j < strlen(realName); j++)
397 if ((realName[j] >=
'A' && realName[j] <=
'Z') ||
398 (realName[j] >=
'a' && realName[j] <=
'z') ||
399 (realName[j] >=
'0' && realName[j] <=
'9') || realName[j] ==
'.')
405 if (parent != realParent && (fileMode == ((1 << 28) | (1 << 13) | (1 << 9))))
412 if (fileMode != ((1 << 28) | (1 << 13) | (1 << 9)))
414 pFileFK = atoi(PQgetvalue(result, i, 4));
417 RestoreTempFile(tempFolder, pFileFK, parent, realName);
439 printf(
"Usage: %s [file|folder]\n", Name);
440 printf(
" -i :: initialize the database, then exit.\n");
441 printf(
" -v :: verbose (-vv = more verbose)\n");
442 printf(
" -c :: Specify the directory for the system configuration.\n");
443 printf(
" -C <file path/folder path> :: run from command line.\n");
444 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.
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.