56 long GlobalPfileFk=-1;
75 if (((mode & 1<<18) + (mode & 0040000) == 0) && ((mode & 1<<28) == 0) && ((mode & 1<<29) != 0))
return 1;
96 long Bytes,Words,Lines;
104 LOG_FATAL(
"pfile %ld Word count unable to open file.\n",GlobalPfileFk);
105 printf(
"LOG pfile %ld Word count unable to open file: pfile_fk=%ld pfile=%s\n",GlobalPfileFk,GlobalPfileFk,
GlobalPfile);
111 memset(Cmd,
'\0',MAXCMD);
112 snprintf(Cmd,MAXCMD,
"/usr/bin/wc '%s' 2>/dev/null",RepFile);
115 Fin = popen(Cmd,
"r");
118 LOG_FATAL(
"pfile %ld Word count unable to count words.\n",GlobalPfileFk);
119 printf(
"LOG pfile %ld Word count unable to run command: %s\n",GlobalPfileFk,Cmd);
125 fscanf(Fin,
"%ld %ld %ld",&Lines,&Words,&Bytes);
130 printf(
"%s: Bytes=%ld Words=%ld Lines=%ld\n",Pfile,Bytes,Words,Lines);
135 memset(Cmd,
'\0',MAXCMD);
136 snprintf(Cmd,MAXCMD,
"INSERT INTO agent_wc (pfile_fk,wc_words,wc_lines) VALUES (%ld,%ld,%ld);",
137 PfileFk,Words,Lines);
138 result = PQexec(
pgConn, SQL);
141 LOG_FATAL(
"pfile %ld Database insert failed.\n",GlobalPfileFk);
142 printf(
"LOG pfile %ld Database insert failed: %s\n",GlobalPfileFk,SQL);
168 printf(
"Usage: %s [options]\n",Name);
169 printf(
" -i :: Initialize the DB connection then exit (nothing downloaded)\n");
170 printf(
" -c :: Specify the directory for the system configuration.\n");
171 printf(
" -C :: Run from command line.\n");
178 int main(
int argc,
char *argv[])
184 char *agent_desc =
"File character, line, word count.";
190 char agent_rev[MAXCMD];
193 char *AgentARSName =
"wc_agent_ars";
205 while((c = getopt(argc,argv,
"iCc:")) != -1)
226 LOG_FATAL(
"Unable to connect to database\n");
232 sprintf(agent_rev,
"%s.%s", VERSION, COMMIT_HASH);
244 if (0 == CmdlineFlag)
254 upload_pk = atoi(Parm);
266 memset(sqlbuf, 0,
sizeof(sqlbuf));
267 snprintf(sqlbuf,
sizeof(sqlbuf),
268 "select ars_pk from wc_agent_ars,agent \ 269 where agent_pk=agent_fk and ars_success=true \ 270 and upload_fk='%d' and agent_fk='%d'",
271 upload_pk, Agent_pk);
272 result = PQexec(
pgConn, sqlbuf);
278 if (PQntuples(result) > 0)
281 LOG_WARNING(
"Ignoring requested wc_agent analysis of upload %d - Results are already in database.\n",upload_pk);
289 memset(sqlbuf, 0,
sizeof(sqlbuf));
290 snprintf(sqlbuf,
sizeof(sqlbuf),
"SELECT DISTINCT(pfile_pk) as pfile_id," 291 " pfile_sha1 || '.' || pfile_md5 || '.' || pfile_size AS pfile_path," 292 " ufile_mode FROM uploadtree, pfile" 293 " WHERE uploadtree.pfile_fk = pfile.pfile_pk" 294 " AND pfile.pfile_pk not in(SELECT pfile_fk from agent_wc)" 295 " AND upload_fk = '%d' LIMIT 5000;", upload_pk);
296 result = PQexec(
pgConn, sqlbuf);
302 pfile_count = PQntuples(result);
305 for(i=0; i < pfile_count; i++)
307 ufile_mode = atoi(PQgetvalue(result, i, 2));
310 GlobalPfileFk = atoi(PQgetvalue(result, i, 0));
314 LOG_FATAL(
"pfile %ld Word count failed.\n",GlobalPfileFk);
315 printf(
"LOG pfile %ld Word count failed: pfile_fk=%ld pfile=%s\n",GlobalPfileFk,GlobalPfileFk,
GlobalPfile);
char * DBConfFile
DB conf file location.
int fo_checkPQresult(PGconn *pgConn, PGresult *result, char *sql, char *FileID, int LineNumb)
Check the result status of a postgres SELECT.
int ProcessData(long PfileFk, char *Pfile)
This function does the work.
int IsFile(long mode)
Check if the pfile_id is a file.
char * Pfile
Pfile name (SHA1.MD5.Size)
void * pgConn
Database connection.
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 fo_checkPQcommand(PGconn *pgConn, PGresult *result, char *sql, char *FileID, int LineNumb)
Check the result status of a postgres commands (not select) If an error occured, write the error to s...
char SQL[256]
SQL query to execute.
int Agent_pk
agent identifier
The main FOSSology C library.
int main(int argc, char *argv[])
main function
char * fo_RepMkPath(const char *Type, char *Filename)
Given a filename, construct the full path to the file.
FUNCTION int fo_CreateARSTable(PGconn *pgConn, const char *tableName)
Create ars table if it doesn't already exist.
int fo_tableExists(PGconn *pgConn, const char *tableName)
Check if table exists. Note, this assumes the database name is 'fossology'.
FUNCTION int fo_GetAgentKey(PGconn *pgConn, const char *agent_name, long Upload_pk, const char *rev, const char *agent_desc)
Get the latest enabled agent key (agent_pk) from the database.
char * fo_scheduler_next()
Get the next data to process from the scheduler.
FUNCTION int fo_WriteARS(PGconn *pgConn, int ars_pk, int upload_pk, int agent_pk, const char *tableName, const char *ars_status, int ars_success)
Write ars record.
void Usage(char *Name)
Say how to run this program.
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...
char * fo_sysconfig(const char *sectionname, const char *variablename)
gets a system configuration variable from the configuration data.
PGconn * fo_dbconnect(char *DBConfFile, char **ErrorBuf)
Connect to a database. The default is Db.conf.