44 long GlobalPfileFk=-1;
63 if (((mode & 1<<18) + (mode & 0040000) == 0) && ((mode & 1<<28) == 0) && ((mode & 1<<29) != 0))
return 1;
84 long Bytes,Words,Lines;
92 LOG_FATAL(
"pfile %ld Word count unable to open file.\n",GlobalPfileFk);
93 printf(
"LOG pfile %ld Word count unable to open file: pfile_fk=%ld pfile=%s\n",GlobalPfileFk,GlobalPfileFk,
GlobalPfile);
99 memset(Cmd,
'\0',MAXCMD);
100 snprintf(Cmd,MAXCMD,
"/usr/bin/wc '%s' 2>/dev/null",RepFile);
103 Fin = popen(Cmd,
"r");
106 LOG_FATAL(
"pfile %ld Word count unable to count words.\n",GlobalPfileFk);
107 printf(
"LOG pfile %ld Word count unable to run command: %s\n",GlobalPfileFk,Cmd);
113 fscanf(Fin,
"%ld %ld %ld",&Lines,&Words,&Bytes);
118 printf(
"%s: Bytes=%ld Words=%ld Lines=%ld\n",
Pfile,Bytes,Words,Lines);
123 memset(Cmd,
'\0',MAXCMD);
124 snprintf(Cmd,MAXCMD,
"INSERT INTO agent_wc (pfile_fk,wc_words,wc_lines) VALUES (%ld,%ld,%ld);",
125 PfileFk,Words,Lines);
129 LOG_FATAL(
"pfile %ld Database insert failed.\n",GlobalPfileFk);
130 printf(
"LOG pfile %ld Database insert failed: %s\n",GlobalPfileFk,
SQL);
156 printf(
"Usage: %s [options]\n",Name);
157 printf(
" -i :: Initialize the DB connection then exit (nothing downloaded)\n");
158 printf(
" -c :: Specify the directory for the system configuration.\n");
159 printf(
" -C :: Run from command line.\n");
166 int main(
int argc,
char *argv[])
172 char *agent_desc =
"File character, line, word count.";
178 char agent_rev[MAXCMD];
181 char *AgentARSName =
"wc_agent_ars";
193 while((c = getopt(argc,argv,
"iCc:")) != -1)
214 LOG_FATAL(
"Unable to connect to database\n");
220 sprintf(agent_rev,
"%s.%s", VERSION, COMMIT_HASH);
232 if (0 == CmdlineFlag)
254 memset(sqlbuf, 0,
sizeof(sqlbuf));
255 snprintf(sqlbuf,
sizeof(sqlbuf),
256 "select ars_pk from wc_agent_ars,agent \
257 where agent_pk=agent_fk and ars_success=true \
258 and upload_fk='%d' and agent_fk='%d'",
260 result = PQexec(
pgConn, sqlbuf);
266 if (PQntuples(result) > 0)
269 LOG_WARNING(
"Ignoring requested wc_agent analysis of upload %d - Results are already in database.\n",
upload_pk);
277 memset(sqlbuf, 0,
sizeof(sqlbuf));
278 snprintf(sqlbuf,
sizeof(sqlbuf),
"SELECT DISTINCT(pfile_pk) as pfile_id,"
279 " pfile_sha1 || '.' || pfile_md5 || '.' || pfile_size AS pfile_path,"
280 " ufile_mode FROM uploadtree, pfile"
281 " WHERE uploadtree.pfile_fk = pfile.pfile_pk"
282 " AND pfile.pfile_pk not in(SELECT pfile_fk from agent_wc)"
283 " AND upload_fk = '%d' LIMIT 5000;",
upload_pk);
284 result = PQexec(
pgConn, sqlbuf);
290 pfile_count = PQntuples(result);
293 for(i=0; i < pfile_count; i++)
295 ufile_mode = atoi(PQgetvalue(result, i, 2));
298 GlobalPfileFk = atoi(PQgetvalue(result, i, 0));
302 LOG_FATAL(
"pfile %ld Word count failed.\n",GlobalPfileFk);
303 printf(
"LOG pfile %ld Word count failed: pfile_fk=%ld pfile=%s\n",GlobalPfileFk,GlobalPfileFk,
GlobalPfile);
char SQL[256]
SQL query to execute.
char * Pfile
Pfile name (SHA1.MD5.Size)
int Agent_pk
agent identifier
FUNCTION int fo_CreateARSTable(PGconn *pgConn, const char *tableName)
Create ars table if it doesn't already exist.
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.
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.
PGconn * fo_dbconnect(char *DBConfFile, char **ErrorBuf)
Connect to a database. The default is Db.conf.
int fo_checkPQresult(PGconn *pgConn, PGresult *result, char *sql, char *FileID, int LineNumb)
Check the result status of a postgres SELECT.
int fo_tableExists(PGconn *pgConn, const char *tableName)
Check if table exists. Note, this assumes the database name is 'fossology'.
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...
The main FOSSology C library.
char * fo_RepMkPath(const char *Type, char *Filename)
Given a filename, construct the full path to the file.
void fo_scheduler_disconnect(int retcode)
Disconnect the scheduler connection.
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.
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.
char * DBConfFile
DB conf file location.
void * pgConn
Database connection.
int main(int argc, char *argv[])
main function
void Usage(char *Name)
Say how to run this program.
int IsFile(long mode)
Check if the pfile_id is a file.
int ProcessData(long PfileFk, char *Pfile)
This function does the work.