90 int regexScan(regex_t *regex,
char *regexStr, FILE *scanFilePtr,
char *fileName)
104 while (fgets(textBuff, 1024, scanFilePtr) != NULL)
107 retCode = regexec(regex, textBuff, 1, rm, 0);
110 sprintf(msgBuff,
"%s: regex found at line %d at position %d. -> %.*s \n",
111 fileName, lineCount, rm[0].rm_so+1, rm[0].rm_eo-rm[0].rm_so, textBuff + rm[0].rm_so);
118 else if (retCode == REG_NOMATCH)
124 regerror(retCode, regex, msgBuff,
sizeof(msgBuff));
125 fprintf(stderr,
"Out of memory? - regex match failure: %s\n", msgBuff);
134 sprintf(msgBuff,
"%s: %s not found\n", fileName, regexStr);
158 sprintf(sqlSelect,
"SELECT pfile_sha1, pfile_md5, pfile_size, ufile_name FROM" 159 " pfile, uploadtree WHERE pfile_fk = pfile_pk and pfile_pk = '%s'", pfileNum);
160 result = PQexec(
pgConn, sqlSelect);
165 if (PQntuples(result) == 0)
170 fprintf(stderr,
"Database does not contain pfile_pk: %s\n", pfileNum);
173 else if (PQntuples(result) != 1)
178 fprintf(stderr,
"Database contains multiple pfile_pk: %s\n", pfileNum);
183 sprintf(pfileRepoName,
"%s.%s.%s", PQgetvalue(result, 0, 0), PQgetvalue(result, 0, 1), PQgetvalue(result, 0, 2));
185 sprintf(pfileRealName,
"%s", PQgetvalue(result, 0, 3));
204 PGresult *result, *pfileResult;
206 int fileCount, i, retCode;
208 char fileRealName[1000];
209 char fileRepoName[1000];
216 sprintf(sqlSelect,
"SELECT upload_pk, upload_mode, upload_filename from upload where upload_pk = '%s'", uploadNum);
217 result = PQexec(
pgConn, sqlSelect);
222 if (PQntuples(result) == 0)
224 fprintf(stderr,
"No uploads appear to be available here!\n");
231 if ((atoi(PQgetvalue(result, 0, 1)) & 96) != 96)
233 fprintf(stderr,
"Upload %s was not successfully processed after upload!\n", uploadNum);
239 sprintf(sqlSelect,
"SELECT uploadtree.pfile_fk, ufile_name from uploadtree, upload" 240 " where upload_fk = upload_pk and uploadtree.pfile_fk <> 0 and ufile_mode = 32768 and upload_pk = '%s'", uploadNum);
241 result = PQexec(
pgConn, sqlSelect);
245 fileCount = PQntuples(result);
249 retCode = regcomp(®ex, regexStr, REG_ICASE+REG_EXTENDED);
252 fprintf(stderr,
"regex %s failed to compile\n", regexStr);
257 for (i=0; i<fileCount; i++)
260 sprintf(sqlSelect,
"SELECT pfile_sha1, pfile_md5, pfile_size, ufile_name" 261 " FROM pfile, uploadtree WHERE pfile_fk = pfile_pk and pfile_pk = '%s'", PQgetvalue(result, i, 0));
262 pfileResult = PQexec(
pgConn, sqlSelect);
267 if (PQntuples(pfileResult) == 1)
272 if (
pfileNumToNames(PQgetvalue(result, i, 0), fileRepoName, fileRealName) != 0)
274 fprintf(stderr,
"ERROR: Unable to locate pfile_pk '%s'\n", PQgetvalue(result, i, 0));
282 fprintf(stderr,
"ERROR: Unable to open '%s/%s'\n",
"files", fileRepoName);
287 regexScan(®ex, regexStr, scanFilePtr, fileRealName);
291 fprintf(stderr,
"WARNING: File: %s - Located %d instances of pfile_pk %s ! Size = %s bytes!\n",
292 PQgetvalue(result, i, 1), PQntuples(pfileResult), PQgetvalue(result, i, 0), PQgetvalue(pfileResult, i, 2));
307 printf(
"Usage: %s [options] [id [id ...]]\n",Name);
308 printf(
" -i :: initialize the database, then exit.\n");
309 printf(
" -c SYSCONFDIR :: FOSSology configuration directory.\n");
310 printf(
" -h :: show available command line options.\n");
311 printf(
" -v :: increase agent logging verbosity.\n");
312 printf(
" -r :: regex expression to load from command line.\n");
313 printf(
" filename :: filename to process with regex.\n");
317 int main (
int argc,
char *argv[])
325 bool regexSet =
false;
332 int scannedCount = 0;
339 char agent_rev[myBUFSIZ];
349 sprintf(agent_rev,
"%s.%s", VERSION, COMMIT_HASH);
351 fprintf(stdout,
"regexscan reports version info as '%s.%s'.\n", VERSION, COMMIT_HASH);
355 while((c = getopt(argc,argv,
"chir:v")) != -1)
365 sprintf(regexStr,
"%s", optarg);
383 fprintf (stderr,
"No regex value has been requested!\n");
390 nonoptargs = argc - optind;
401 printf(
"UploadPK is: %ld\n", UploadPK);
402 sprintf(uploadNum,
"%ld", UploadPK);
404 if (scannedCount == 0)
406 fprintf(stderr,
"Failed to successfully scan: upload - %s!\n", uploadNum);
413 sprintf(fileName,
"%s", argv[optind]);
415 scanFilePtr = fopen(fileName,
"r");
418 fprintf(stderr,
"ERROR: Unable to open '%s'\n", fileName);
424 retCode = regcomp(®ex, regexStr, REG_ICASE+REG_EXTENDED);
427 fprintf(stderr,
"regex %s failed to compile\n", regexStr);
433 retCode =
regexScan(®ex, (
char *)regexStr, scanFilePtr, (
char *)fileName);
437 fprintf(stderr,
"Failed to successfully scan: %s!\n", fileName);
int fo_checkPQresult(PGconn *pgConn, PGresult *result, char *sql, char *FileID, int LineNumb)
Check the result status of a postgres SELECT.
Store the results of a regex match.
int regexScanUpload(char *uploadNum, char *regexStr)
Scan an Upload for a regex - regular expression. gets a list of files in an upload and calls regexS...
char * fo_scheduler_current()
Get the last read string from the scheduler.
void fo_scheduler_disconnect(int retcode)
Disconnect the scheduler connection.
void fo_scheduler_connect(int *argc, char **argv, PGconn **db_conn)
Establish a connection between an agent and the scheduler.
int regexScan(regex_t *regex, char *regexStr, FILE *scanFilePtr, char *fileName)
Scan a file for a regex - regular expression. the regex is compiled in this function for performanc...
FILE * fo_RepFread(char *Type, char *Filename)
Perform an fopen for reading only.
int agent_verbose
Common verbose flags for the agents, this is used so that the scheduler can change the verbose level ...
int pfileNumToNames(char *pfileNum, char *pfileRepoName, char *pfileRealName)
Creates filenames from pfile_pk value.
PGconn * pgConn
Database connection.
int fo_scheduler_userID()
Gets the id of the user that created the job that the agent is running.
The main FOSSology C library.
char * fo_scheduler_next()
Get the next data to process from the scheduler.
void Usage(char *Name)
Usage description for this regexscan agent.
char * fo_sysconfig(const char *sectionname, const char *variablename)
gets a system configuration variable from the configuration data.