81 char BuildVersion[]=
"adj2nest build version: " VERSION_S
" r(" COMMIT_HASH_S
").\n";
122 for(i=0; i<Depth; i++) printf(
" ");
129 if (Tree[Index].
Child > -1)
135 snprintf(
SQL,
sizeof(
SQL),
"UPDATE %s SET lft='%ld', rgt='%ld' WHERE uploadtree_pk='%ld'",
159 static long LastParentId=-1;
160 static long LastParentIndex=-1;
172 if (Parent == LastParentId)
182 LastParentId = Parent;
192 LastParentId = Parent;
200 if (Tree[P].
Child < 0)
223 long RootRows, NonRootRows;
224 PGresult* pgNonRootResult;
225 PGresult* pgRootResult;
233 if (LastChar >=
'0' && LastChar <=
'9')
242 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);
246 NonRootRows = PQntuples(pgNonRootResult);
247 TreeSize = NonRootRows;
248 LOG_VERBOSE(
"# Upload %ld: %ld items\n",UploadPk,TreeSize);
250 snprintf(
SQL,
sizeof(
SQL),
"SELECT uploadtree_pk,parent FROM %s WHERE upload_fk = %ld AND parent IS NULL",
uploadtree_tablename,UploadPk);
254 RootRows = PQntuples(pgRootResult);
255 TreeSize += RootRows;
258 if (Tree) { free(Tree); }
259 if (TreeSize <= 0) { Tree=NULL;
return; }
261 for(i=0; i<TreeSize+1; i++)
272 for(i=0; i<RootRows; i++)
274 Child = atol(PQgetvalue(pgRootResult, i, 0));
283 for(i=0; i<NonRootRows; i++)
285 Child = atol(PQgetvalue(pgNonRootResult,i,0));
286 Parent = atol(PQgetvalue(pgNonRootResult,i,1));
294 PQclear(pgNonRootResult);
295 PQclear(pgRootResult);
310 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 )");
314 MaxRow = PQntuples(pgResult);
315 for(Row=0; Row < MaxRow; Row++)
317 UploadPk = atol(PQgetvalue(pgResult,Row,0));
321 printf(
"Processing %ld :: %s",UploadPk,PQgetvalue(pgResult,Row,2));
322 S = PQgetvalue(pgResult,Row,1);
323 if (S && S[0]) printf(
" (%s)",S);
327 if (Tree) free(Tree);
344 printf(
"# Uploads\n");
345 snprintf(
SQL,
sizeof(
SQL),
"SELECT upload_pk,upload_desc,upload_filename FROM upload ORDER BY upload_pk");
350 MaxRow = PQntuples(pgResult);
351 for(Row=0; Row < MaxRow; Row++)
353 NewPid = atol(PQgetvalue(pgResult,Row,0));
357 printf(
"%ld :: %s",NewPid,PQgetvalue(pgResult,Row,2));
358 S = PQgetvalue(pgResult,Row,1);
359 if (S && S[0]) printf(
" (%s)",S);
379 int MatchField (
char *Field,
char *S)
382 if (!S || (S[0]==
'\0'))
return(-1);
383 while(isspace(S[0])) S++;
385 if (!strncmp(Field,S,Len))
388 while (isspace(S[Len])) Len++;
389 if (S[Len] ==
'=')
return (1);
402 char * SkipFieldValue (
char *S)
406 if (!S || (S[0]==
'\0'))
return(NULL);
409 while((S[0] !=
'\0') && (S[0]!=
'=')) S++;
410 if (S[0]==
'\0')
return(NULL);
412 while(isspace(S[0])) S++;
413 if (S[0]==
'\0')
return(NULL);
418 case '\"':
case '\'':
426 while((S[0]!=
'\0') && (S[0]!=Quote))
433 if (S[0]==Quote) S++;
434 while(isspace(S[0])) S++;
447 char * UntaintValue (
char *Sin,
char *Sout)
452 while((Sin[0] !=
'\0') && (Sin[0]!=
'=')) Sin++;
453 if (Sin[0]==
'\0')
return(NULL);
455 while(isspace(Sin[0])) Sin++;
456 if (Sin[0]==
'\0') { Sout[0]=
'\0';
return(NULL); }
461 case '\"':
case '\'':
471 while((Sin[0]!=
'\0') && (Sin[0]!=Quote))
473 if (Sin[0] ==
'\\') {
478 else if (Sin[0] ==
'r') {
481 else if (Sin[0] ==
'a') {
508 int SetParm (
char *ParmName,
char *Parm)
513 if (!ParmName || (ParmName[0]==
'\0'))
return(1);
514 if (!Parm || (Parm[0]==
'\0'))
return(1);
517 while(!(rc=MatchField(ParmName,Parm)))
519 Parm = SkipFieldValue(Parm);
521 if (rc != 1)
return(0);
524 UntaintValue(Parm,OldParm);
541 snprintf(
SQL,
sizeof(
SQL),
"UPDATE upload SET upload_mode = upload_mode | (1<<6) WHERE upload_pk='%ld'",
563 printf(
"Usage: %s [options] [id [id ...]]\n",Name);
564 printf(
" -h :: help (print this message), then exit.\n");
565 printf(
" -i :: initialize the database, then exit.\n");
566 printf(
" -a :: run on ALL uploads that have no nested set records.\n");
567 printf(
" -c SYSCONFDIR :: Specify the directory for the system configuration.\n");
568 printf(
" -v :: verbose (-vv = more verbose).\n");
569 printf(
" -u :: list all upload ids, then exit.\n");
570 printf(
" no file :: process upload ids from the scheduler.\n");
571 printf(
" id :: process upload ids from the command-line.\n");
572 printf(
" -V :: print the version info, then exit.\n");
576 int main (
int argc,
char *argv[])
580 long *uploads_to_scan;
581 int upload_count = 0;
583 char *agent_desc =
"Adj2nest Agent";
586 char agent_rev[myBUFSIZ];
593 sprintf(agent_rev,
"%s.%s", VERSION, COMMIT_HASH);
598 uploads_to_scan = calloc(argc,
sizeof(
long));
601 while((c = getopt(argc,argv,
"aciuvVh")) != -1)
632 for (i = optind; i < argc; i++)
634 uploads_to_scan[upload_count] = atol(argv[i]);
638 if (upload_count == 0)
648 LOG_ERROR(
"You have no update permissions on upload %ld", UploadPk);
654 if (Tree) free(Tree);
659 if (rv == -1) LOG_ERROR(
"Unable to update mode on upload %ld", UploadPk);
664 for (i = 0; i < upload_count; i++)
666 UploadPk = uploads_to_scan[i];
669 if (Tree) free(Tree);
674 if (rv == -1) LOG_ERROR(
"Unable to update mode on upload %ld", UploadPk);
676 free(uploads_to_scan);
void LoadAdj(long UploadPk)
void SetParent(long Parent, long Child)
int UpdateUpload(long UploadPk)
Finish updating the upload record and permissions data.
char SQL[256]
SQL query to execute.
PGconn * pgConn
Database connection.
void WalkTree(long Index, long Depth)
char * uploadtree_tablename
upload.uploadtree_tablename
Usage()
Print Usage statement.
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".
FUNCTION int GetUploadPerm(PGconn *pgConn, long UploadPk, int user_pk)
Get users permission to this upload.
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.
int fo_checkPQresult(PGconn *pgConn, PGresult *result, char *sql, char *FileID, int LineNumb)
Check the result status of a postgres SELECT.
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.
#define PERM_WRITE
Read-Write permission.
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.
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.
Contains information required by uploadtree elements.