59 int regexScan(regex_t *regex,
char *regexStr, FILE *scanFilePtr,
char *fileName)
73 while (fgets(textBuff, 1024, scanFilePtr) != NULL)
76 retCode = regexec(regex, textBuff, 1, rm, 0);
79 sprintf(msgBuff,
"%s: regex found at line %d at position %d. -> %.*s \n",
80 fileName, lineCount, rm[0].rm_so+1, rm[0].rm_eo-rm[0].rm_so, textBuff + rm[0].rm_so);
87 else if (retCode == REG_NOMATCH)
93 regerror(retCode, regex, msgBuff,
sizeof(msgBuff));
94 fprintf(stderr,
"Out of memory? - regex match failure: %s\n", msgBuff);
103 sprintf(msgBuff,
"%s: %s not found\n", fileName, regexStr);
127 sprintf(sqlSelect,
"SELECT pfile_sha1, pfile_md5, pfile_size, ufile_name FROM pfile, uploadtree WHERE pfile_fk = pfile_pk and pfile_pk = '%s'", pfileNum);
128 result = PQexec(
pgConn, sqlSelect);
133 if (PQntuples(result) == 0)
138 fprintf(stderr,
"Database does not contain pfile_pk: %s\n", pfileNum);
141 else if (PQntuples(result) != 1)
146 fprintf(stderr,
"Database contains multiple pfile_pk: %s\n", pfileNum);
151 sprintf(pfileRepoName,
"%s.%s.%s", PQgetvalue(result, 0, 0), PQgetvalue(result, 0, 1), PQgetvalue(result, 0, 2));
153 sprintf(pfileRealName,
"%s", PQgetvalue(result, 0, 3));
172 PGresult *result, *pfileResult;
174 int fileCount, i, retCode;
176 char fileRealName[1000];
177 char fileRepoName[1000];
184 sprintf(sqlSelect,
"SELECT upload_pk, upload_mode, upload_filename from upload where upload_pk = '%s'", uploadNum);
185 result = PQexec(
pgConn, sqlSelect);
190 if (PQntuples(result) == 0)
192 fprintf(stderr,
"No uploads appear to be available here!\n");
199 if ((atoi(PQgetvalue(result, 0, 1)) & 96) != 96)
201 fprintf(stderr,
"Upload %s was not successfully processed after upload!\n", uploadNum);
207 sprintf(sqlSelect,
"SELECT uploadtree.pfile_fk, ufile_name from uploadtree, upload"
208 " where upload_fk = upload_pk and uploadtree.pfile_fk <> 0 and ufile_mode = 32768 and upload_pk = '%s'", uploadNum);
209 result = PQexec(
pgConn, sqlSelect);
213 fileCount = PQntuples(result);
217 retCode = regcomp(®ex, regexStr, REG_ICASE+REG_EXTENDED);
220 fprintf(stderr,
"regex %s failed to compile\n", regexStr);
225 for (i=0; i<fileCount; i++)
228 sprintf(sqlSelect,
"SELECT pfile_sha1, pfile_md5, pfile_size, ufile_name"
229 " FROM pfile, uploadtree WHERE pfile_fk = pfile_pk and pfile_pk = '%s'", PQgetvalue(result, i, 0));
230 pfileResult = PQexec(
pgConn, sqlSelect);
235 if (PQntuples(pfileResult) == 1)
240 if (
pfileNumToNames(PQgetvalue(result, i, 0), fileRepoName, fileRealName) != 0)
242 fprintf(stderr,
"ERROR: Unable to locate pfile_pk '%s'\n", PQgetvalue(result, i, 0));
250 fprintf(stderr,
"ERROR: Unable to open '%s/%s'\n",
"files", fileRepoName);
255 regexScan(®ex, regexStr, scanFilePtr, fileRealName);
259 fprintf(stderr,
"WARNING: File: %s - Located %d instances of pfile_pk %s ! Size = %s bytes!\n",
260 PQgetvalue(result, i, 1), PQntuples(pfileResult), PQgetvalue(result, i, 0), PQgetvalue(pfileResult, i, 2));
274 printf(
"Usage: %s [options] [id [id ...]]\n",Name);
275 printf(
" -i :: initialize the database, then exit.\n");
276 printf(
" -c SYSCONFDIR :: FOSSology configuration directory.\n");
277 printf(
" -h :: show available command line options.\n");
278 printf(
" -v :: increase agent logging verbosity.\n");
279 printf(
" -r :: regex expression to load from command line.\n");
280 printf(
" filename :: filename to process with regex.\n");
284 int main (
int argc,
char *argv[])
292 bool regexSet =
false;
299 int scannedCount = 0;
306 char agent_rev[myBUFSIZ];
316 sprintf(agent_rev,
"%s.%s", VERSION, COMMIT_HASH);
318 fprintf(stdout,
"regexscan reports version info as '%s.%s'.\n", VERSION, COMMIT_HASH);
322 while((c = getopt(argc,argv,
"chir:v")) != -1)
332 sprintf(regexStr,
"%s", optarg);
350 fprintf (stderr,
"No regex value has been requested!\n");
357 nonoptargs = argc - optind;
368 printf(
"UploadPK is: %ld\n", UploadPK);
369 sprintf(uploadNum,
"%ld", UploadPK);
371 if (scannedCount == 0)
373 fprintf(stderr,
"Failed to successfully scan: upload - %s!\n", uploadNum);
380 sprintf(fileName,
"%s", argv[optind]);
382 scanFilePtr = fopen(fileName,
"r");
385 fprintf(stderr,
"ERROR: Unable to open '%s'\n", fileName);
391 retCode = regcomp(®ex, regexStr, REG_ICASE+REG_EXTENDED);
394 fprintf(stderr,
"regex %s failed to compile\n", regexStr);
400 retCode =
regexScan(®ex, (
char *)regexStr, scanFilePtr, (
char *)fileName);
404 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.