93 char BuildVersion[]=
"adj2nest build version: " VERSION_S
" r(" COMMIT_HASH_S
").\n";
134 for(i=0; i<Depth; i++) printf(
" ");
141 if (Tree[Index].
Child > -1)
147 snprintf(
SQL,
sizeof(
SQL),
"UPDATE %s SET lft='%ld', rgt='%ld' WHERE uploadtree_pk='%ld'",
171 static long LastParentId=-1;
172 static long LastParentIndex=-1;
184 if (Parent == LastParentId)
194 LastParentId = Parent;
204 LastParentId = Parent;
212 if (Tree[P].Child < 0)
235 long RootRows, NonRootRows;
236 PGresult* pgNonRootResult;
237 PGresult* pgRootResult;
245 if (LastChar >=
'0' && LastChar <=
'9')
254 snprintf(
SQL,
sizeof(
SQL),
"SELECT uploadtree_pk,parent FROM %s WHERE upload_fk = %ld AND parent IS NOT NULL ORDER BY parent, ufile_mode&(1<<29) DESC, ufile_name",
uploadtree_tablename,UploadPk);
258 NonRootRows = PQntuples(pgNonRootResult);
259 TreeSize = NonRootRows;
260 LOG_VERBOSE(
"# Upload %ld: %ld items\n",UploadPk,TreeSize);
262 snprintf(
SQL,
sizeof(
SQL),
"SELECT uploadtree_pk,parent FROM %s WHERE upload_fk = %ld AND parent IS NULL",
uploadtree_tablename,UploadPk);
266 RootRows = PQntuples(pgRootResult);
267 TreeSize += RootRows;
270 if (Tree) { free(Tree); }
271 if (TreeSize <= 0) { Tree=NULL;
return; }
273 for(i=0; i<TreeSize+1; i++)
284 for(i=0; i<RootRows; i++)
286 Child = atol(PQgetvalue(pgRootResult, i, 0));
295 for(i=0; i<NonRootRows; i++)
297 Child = atol(PQgetvalue(pgNonRootResult,i,0));
298 Parent = atol(PQgetvalue(pgNonRootResult,i,1));
306 PQclear(pgNonRootResult);
307 PQclear(pgRootResult);
322 snprintf(
SQL,
sizeof(
SQL),
"SELECT DISTINCT upload_pk,upload_desc,upload_filename FROM upload WHERE upload_pk IN ( SELECT DISTINCT upload_fk FROM uploadtree WHERE lft IS NULL )");
326 MaxRow = PQntuples(pgResult);
327 for(Row=0; Row < MaxRow; Row++)
329 UploadPk = atol(PQgetvalue(pgResult,Row,0));
333 printf(
"Processing %ld :: %s",UploadPk,PQgetvalue(pgResult,Row,2));
334 S = PQgetvalue(pgResult,Row,1);
335 if (S && S[0]) printf(
" (%s)",S);
339 if (Tree) free(Tree);
356 printf(
"# Uploads\n");
357 snprintf(
SQL,
sizeof(
SQL),
"SELECT upload_pk,upload_desc,upload_filename FROM upload ORDER BY upload_pk");
362 MaxRow = PQntuples(pgResult);
363 for(Row=0; Row < MaxRow; Row++)
365 NewPid = atol(PQgetvalue(pgResult,Row,0));
369 printf(
"%ld :: %s",NewPid,PQgetvalue(pgResult,Row,2));
370 S = PQgetvalue(pgResult,Row,1);
371 if (S && S[0]) printf(
" (%s)",S);
391 int MatchField (
char *Field,
char *S)
394 if (!S || (S[0]==
'\0'))
return(-1);
395 while(isspace(S[0])) S++;
397 if (!strncmp(Field,S,Len))
400 while (isspace(S[Len])) Len++;
401 if (S[Len] ==
'=')
return (1);
414 char * SkipFieldValue (
char *S)
418 if (!S || (S[0]==
'\0'))
return(NULL);
421 while((S[0] !=
'\0') && (S[0]!=
'=')) S++;
422 if (S[0]==
'\0')
return(NULL);
424 while(isspace(S[0])) S++;
425 if (S[0]==
'\0')
return(NULL);
430 case '\"':
case '\'':
438 while((S[0]!=
'\0') && (S[0]!=Quote))
445 if (S[0]==Quote) S++;
446 while(isspace(S[0])) S++;
459 char * UntaintValue (
char *Sin,
char *Sout)
464 while((Sin[0] !=
'\0') && (Sin[0]!=
'=')) Sin++;
465 if (Sin[0]==
'\0')
return(NULL);
467 while(isspace(Sin[0])) Sin++;
468 if (Sin[0]==
'\0') { Sout[0]=
'\0';
return(NULL); }
473 case '\"':
case '\'':
483 while((Sin[0]!=
'\0') && (Sin[0]!=Quote))
485 if (Sin[0] ==
'\\') {
490 else if (Sin[0] ==
'r') {
493 else if (Sin[0] ==
'a') {
520 int SetParm (
char *ParmName,
char *Parm)
525 if (!ParmName || (ParmName[0]==
'\0'))
return(1);
526 if (!Parm || (Parm[0]==
'\0'))
return(1);
529 while(!(rc=MatchField(ParmName,Parm)))
531 Parm = SkipFieldValue(Parm);
533 if (rc != 1)
return(0);
536 UntaintValue(Parm,OldParm);
553 snprintf(
SQL,
sizeof(
SQL),
"UPDATE upload SET upload_mode = upload_mode | (1<<6) WHERE upload_pk='%ld'",
575 printf(
"Usage: %s [options] [id [id ...]]\n",Name);
576 printf(
" -h :: help (print this message), then exit.\n");
577 printf(
" -i :: initialize the database, then exit.\n");
578 printf(
" -a :: run on ALL uploads that have no nested set records.\n");
579 printf(
" -c SYSCONFDIR :: Specify the directory for the system configuration.\n");
580 printf(
" -v :: verbose (-vv = more verbose).\n");
581 printf(
" -u :: list all upload ids, then exit.\n");
582 printf(
" no file :: process upload ids from the scheduler.\n");
583 printf(
" id :: process upload ids from the command-line.\n");
584 printf(
" -V :: print the version info, then exit.\n");
588 int main (
int argc,
char *argv[])
592 long *uploads_to_scan;
593 int upload_count = 0;
595 char *agent_desc =
"Adj2nest Agent";
598 char agent_rev[myBUFSIZ];
605 sprintf(agent_rev,
"%s.%s", VERSION, COMMIT_HASH);
610 uploads_to_scan = calloc(argc,
sizeof(
long));
613 while((c = getopt(argc,argv,
"aciuvVh")) != -1)
644 for (i = optind; i < argc; i++)
646 uploads_to_scan[upload_count] = atol(argv[i]);
650 if (upload_count == 0)
660 LOG_ERROR(
"You have no update permissions on upload %ld", UploadPk);
666 if (Tree) free(Tree);
671 if (rv == -1) LOG_ERROR(
"Unable to update mode on upload %ld", UploadPk);
676 for (i = 0; i < upload_count; i++)
678 UploadPk = uploads_to_scan[i];
681 if (Tree) free(Tree);
686 if (rv == -1) LOG_ERROR(
"Unable to update mode on upload %ld", UploadPk);
688 free(uploads_to_scan);
FUNCTION char * GetUploadtreeTableName(PGconn *pgConn, int upload_pk)
Get the uploadtree table name for this upload_pk If upload_pk does not exist, return "uploadtree"...
int fo_checkPQresult(PGconn *pgConn, PGresult *result, char *sql, char *FileID, int LineNumb)
Check the result status of a postgres SELECT.
PGconn * pgConn
Database connection.
char * fo_scheduler_current()
Get the last read string from the scheduler.
int UpdateUpload(long UploadPk)
Finish updating the upload record and permissions data.
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.
void SetParent(long Parent, long Child)
int agent_verbose
Common verbose flags for the agents, this is used so that the scheduler can change the verbose level ...
void WalkTree(long Index, long Depth)
void LoadAdj(long UploadPk)
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 fo_scheduler_userID()
Gets the id of the user that created the job that the agent is running.
Usage()
Print Usage statement.
Contains information required by uploadtree elements.
The main FOSSology C library.
char * uploadtree_tablename
upload.uploadtree_tablename
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.
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...
FUNCTION int GetUploadPerm(PGconn *pgConn, long UploadPk, int user_pk)
Get users permission to this upload.
char * fo_sysconfig(const char *sectionname, const char *variablename)
gets a system configuration variable from the configuration data.
#define PERM_WRITE
Read-Write permission.