79 int regexScan(regex_t *regex,
char *regexStr, FILE *scanFilePtr,
char *fileName)
93 while (fgets(textBuff, 1024, scanFilePtr) != NULL)
96 retCode = regexec(regex, textBuff, 1, rm, 0);
99 sprintf(msgBuff,
"%s: regex found at line %d at position %d. -> %.*s \n",
100 fileName, lineCount, rm[0].rm_so+1, rm[0].rm_eo-rm[0].rm_so, textBuff + rm[0].rm_so);
107 else if (retCode == REG_NOMATCH)
113 regerror(retCode, regex, msgBuff,
sizeof(msgBuff));
114 fprintf(stderr,
"Out of memory? - regex match failure: %s\n", msgBuff);
123 sprintf(msgBuff,
"%s: %s not found\n", fileName, regexStr);
147 sprintf(sqlSelect,
"SELECT pfile_sha1, pfile_md5, pfile_size, ufile_name FROM"
148 " pfile, uploadtree WHERE pfile_fk = pfile_pk and pfile_pk = '%s'", pfileNum);
149 result = PQexec(
pgConn, sqlSelect);
154 if (PQntuples(result) == 0)
159 fprintf(stderr,
"Database does not contain pfile_pk: %s\n", pfileNum);
162 else if (PQntuples(result) != 1)
167 fprintf(stderr,
"Database contains multiple pfile_pk: %s\n", pfileNum);
172 sprintf(pfileRepoName,
"%s.%s.%s", PQgetvalue(result, 0, 0), PQgetvalue(result, 0, 1), PQgetvalue(result, 0, 2));
174 sprintf(pfileRealName,
"%s", PQgetvalue(result, 0, 3));
193 PGresult *result, *pfileResult;
195 int fileCount, i, retCode;
197 char fileRealName[1000];
198 char fileRepoName[1000];
205 sprintf(sqlSelect,
"SELECT upload_pk, upload_mode, upload_filename from upload where upload_pk = '%s'", uploadNum);
206 result = PQexec(
pgConn, sqlSelect);
211 if (PQntuples(result) == 0)
213 fprintf(stderr,
"No uploads appear to be available here!\n");
220 if ((atoi(PQgetvalue(result, 0, 1)) & 96) != 96)
222 fprintf(stderr,
"Upload %s was not successfully processed after upload!\n", uploadNum);
228 sprintf(sqlSelect,
"SELECT uploadtree.pfile_fk, ufile_name from uploadtree, upload"
229 " where upload_fk = upload_pk and uploadtree.pfile_fk <> 0 and ufile_mode = 32768 and upload_pk = '%s'", uploadNum);
230 result = PQexec(
pgConn, sqlSelect);
234 fileCount = PQntuples(result);
238 retCode = regcomp(®ex, regexStr, REG_ICASE+REG_EXTENDED);
241 fprintf(stderr,
"regex %s failed to compile\n", regexStr);
246 for (i=0; i<fileCount; i++)
249 sprintf(sqlSelect,
"SELECT pfile_sha1, pfile_md5, pfile_size, ufile_name"
250 " FROM pfile, uploadtree WHERE pfile_fk = pfile_pk and pfile_pk = '%s'", PQgetvalue(result, i, 0));
251 pfileResult = PQexec(
pgConn, sqlSelect);
256 if (PQntuples(pfileResult) == 1)
261 if (
pfileNumToNames(PQgetvalue(result, i, 0), fileRepoName, fileRealName) != 0)
263 fprintf(stderr,
"ERROR: Unable to locate pfile_pk '%s'\n", PQgetvalue(result, i, 0));
271 fprintf(stderr,
"ERROR: Unable to open '%s/%s'\n",
"files", fileRepoName);
276 regexScan(®ex, regexStr, scanFilePtr, fileRealName);
280 fprintf(stderr,
"WARNING: File: %s - Located %d instances of pfile_pk %s ! Size = %s bytes!\n",
281 PQgetvalue(result, i, 1), PQntuples(pfileResult), PQgetvalue(result, i, 0), PQgetvalue(pfileResult, i, 2));
296 printf(
"Usage: %s [options] [id [id ...]]\n",Name);
297 printf(
" -i :: initialize the database, then exit.\n");
298 printf(
" -c SYSCONFDIR :: FOSSology configuration directory.\n");
299 printf(
" -h :: show available command line options.\n");
300 printf(
" -v :: increase agent logging verbosity.\n");
301 printf(
" -r :: regex expression to load from command line.\n");
302 printf(
" filename :: filename to process with regex.\n");
306 int main (
int argc,
char *argv[])
314 bool regexSet =
false;
321 int scannedCount = 0;
328 char agent_rev[myBUFSIZ];
338 sprintf(agent_rev,
"%s.%s", VERSION, COMMIT_HASH);
340 fprintf(stdout,
"regexscan reports version info as '%s.%s'.\n", VERSION, COMMIT_HASH);
344 while((c = getopt(argc,argv,
"chir:v")) != -1)
354 sprintf(regexStr,
"%s", optarg);
372 fprintf (stderr,
"No regex value has been requested!\n");
379 nonoptargs = argc - optind;
390 printf(
"UploadPK is: %ld\n", UploadPK);
391 sprintf(uploadNum,
"%ld", UploadPK);
393 if (scannedCount == 0)
395 fprintf(stderr,
"Failed to successfully scan: upload - %s!\n", uploadNum);
402 sprintf(fileName,
"%s", argv[optind]);
404 scanFilePtr = fopen(fileName,
"r");
407 fprintf(stderr,
"ERROR: Unable to open '%s'\n", fileName);
413 retCode = regcomp(®ex, regexStr, REG_ICASE+REG_EXTENDED);
416 fprintf(stderr,
"regex %s failed to compile\n", regexStr);
422 retCode =
regexScan(®ex, (
char *)regexStr, scanFilePtr, (
char *)fileName);
426 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.
The main FOSSology C library.
FILE * fo_RepFread(char *Type, char *Filename)
Perform an fopen for reading only.
void fo_scheduler_disconnect(int retcode)
Disconnect the scheduler connection.
char * fo_sysconfig(const char *sectionname, const char *variablename)
gets a system configuration variable from the configuration data.
int agent_verbose
Common verbose flags for the agents, this is used so that the scheduler can change the verbose level ...
int fo_scheduler_userID()
Gets the id of the user that created the job that the agent is running.
char * fo_scheduler_current()
Get the last read string from the scheduler.
char * fo_scheduler_next()
Get the next data to process from the scheduler.
void fo_scheduler_connect(int *argc, char **argv, PGconn **db_conn)
Establish a connection between an agent and the scheduler.
void Usage(char *Name)
Usage description for this regexscan agent.
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...
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...
PGconn * pgConn
Database connection.
int pfileNumToNames(char *pfileNum, char *pfileRepoName, char *pfileRealName)
Creates filenames from pfile_pk value.
Store the results of a regex match.