81 char BuildVersion[]=
"adj2nest build version: " VERSION_S
" r(" COMMIT_HASH_S
").\n";
127 for(i=0; i<Depth; i++) printf(
" ");
134 if (Tree[Index].
Child > -1)
140 snprintf(
SQL,
sizeof(
SQL),
"UPDATE %s SET lft='%ld', rgt='%ld' WHERE uploadtree_pk='%ld'",
145 fo_scheduler_heart(1);
147 if (Tree[Index].
Sibling < 0)
break;
163 static long LastParentId=-1;
164 static long LastParentIndex=-1;
176 if (Parent == LastParentId)
186 LastParentId = Parent;
196 LastParentId = Parent;
204 if (Tree[P].
Child < 0)
227 long RootRows, NonRootRows;
228 PGresult* pgNonRootResult;
229 PGresult* pgRootResult;
237 if (LastChar >=
'0' && LastChar <=
'9')
246 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);
250 NonRootRows = PQntuples(pgNonRootResult);
251 TreeSize = NonRootRows;
252 LOG_VERBOSE(
"# Upload %ld: %ld items\n",UploadPk,TreeSize);
254 snprintf(
SQL,
sizeof(
SQL),
"SELECT uploadtree_pk,parent FROM %s WHERE upload_fk = %ld AND parent IS NULL",
uploadtree_tablename,UploadPk);
258 RootRows = PQntuples(pgRootResult);
259 TreeSize += RootRows;
262 if (Tree) { free(Tree); }
263 if (TreeSize <= 0) { Tree=NULL;
return; }
265 for(i=0; i<TreeSize+1; i++)
276 for(i=0; i<RootRows; i++)
278 Child = atol(PQgetvalue(pgRootResult, i, 0));
283 if ((i % 100000) == 0) fo_scheduler_heart(0);
287 for(i=0; i<NonRootRows; i++)
289 Child = atol(PQgetvalue(pgNonRootResult,i,0));
290 Parent = atol(PQgetvalue(pgNonRootResult,i,1));
294 if ((i % 100000) == 0) fo_scheduler_heart(0);
298 PQclear(pgNonRootResult);
299 PQclear(pgRootResult);
314 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 )");
318 MaxRow = PQntuples(pgResult);
319 for(Row=0; Row < MaxRow; Row++)
321 UploadPk = atol(PQgetvalue(pgResult,Row,0));
325 printf(
"Processing %ld :: %s",UploadPk,PQgetvalue(pgResult,Row,2));
326 S = PQgetvalue(pgResult,Row,1);
327 if (S && S[0]) printf(
" (%s)",S);
331 if (Tree) free(Tree);
348 printf(
"# Uploads\n");
349 snprintf(
SQL,
sizeof(
SQL),
"SELECT upload_pk,upload_desc,upload_filename FROM upload ORDER BY upload_pk");
354 MaxRow = PQntuples(pgResult);
355 for(Row=0; Row < MaxRow; Row++)
357 NewPid = atol(PQgetvalue(pgResult,Row,0));
361 printf(
"%ld :: %s",NewPid,PQgetvalue(pgResult,Row,2));
362 S = PQgetvalue(pgResult,Row,1);
363 if (S && S[0]) printf(
" (%s)",S);
383 int MatchField (
char *Field,
char *S)
386 if (!S || (S[0]==
'\0'))
return(-1);
387 while(isspace(S[0])) S++;
389 if (!strncmp(Field,S,Len))
392 while (isspace(S[Len])) Len++;
393 if (S[Len] ==
'=')
return (1);
406 char * SkipFieldValue (
char *S)
410 if (!S || (S[0]==
'\0'))
return(NULL);
413 while((S[0] !=
'\0') && (S[0]!=
'=')) S++;
414 if (S[0]==
'\0')
return(NULL);
416 while(isspace(S[0])) S++;
417 if (S[0]==
'\0')
return(NULL);
422 case '\"':
case '\'':
430 while((S[0]!=
'\0') && (S[0]!=Quote))
437 if (S[0]==Quote) S++;
438 while(isspace(S[0])) S++;
451 char * UntaintValue (
char *Sin,
char *Sout)
456 while((Sin[0] !=
'\0') && (Sin[0]!=
'=')) Sin++;
457 if (Sin[0]==
'\0')
return(NULL);
459 while(isspace(Sin[0])) Sin++;
460 if (Sin[0]==
'\0') { Sout[0]=
'\0';
return(NULL); }
465 case '\"':
case '\'':
475 while((Sin[0]!=
'\0') && (Sin[0]!=Quote))
477 if (Sin[0] ==
'\\') {
482 else if (Sin[0] ==
'r') {
485 else if (Sin[0] ==
'a') {
512 int SetParm (
char *ParmName,
char *Parm)
517 if (!ParmName || (ParmName[0]==
'\0'))
return(1);
518 if (!Parm || (Parm[0]==
'\0'))
return(1);
521 while(!(rc=MatchField(ParmName,Parm)))
523 Parm = SkipFieldValue(Parm);
525 if (rc != 1)
return(0);
528 UntaintValue(Parm,OldParm);
545 snprintf(
SQL,
sizeof(
SQL),
"UPDATE upload SET upload_mode = upload_mode | (1<<6) WHERE upload_pk='%ld'",
567 printf(
"Usage: %s [options] [id [id ...]]\n",Name);
568 printf(
" -h :: help (print this message), then exit.\n");
569 printf(
" -i :: initialize the database, then exit.\n");
570 printf(
" -a :: run on ALL uploads that have no nested set records.\n");
571 printf(
" -c SYSCONFDIR :: Specify the directory for the system configuration.\n");
572 printf(
" -v :: verbose (-vv = more verbose).\n");
573 printf(
" -u :: list all upload ids, then exit.\n");
574 printf(
" no file :: process upload ids from the scheduler.\n");
575 printf(
" id :: process upload ids from the command-line.\n");
576 printf(
" -V :: print the version info, then exit.\n");
580 int main (
int argc,
char *argv[])
584 long *uploads_to_scan;
585 int upload_count = 0;
587 char *agent_desc =
"Adj2nest Agent";
590 char agent_rev[myBUFSIZ];
597 sprintf(agent_rev,
"%s.%s", VERSION, COMMIT_HASH);
602 uploads_to_scan = calloc(argc,
sizeof(
long));
605 while((c = getopt(argc,argv,
"aciuvVh")) != -1)
636 for (i = optind; i < argc; i++)
638 uploads_to_scan[upload_count] = atol(argv[i]);
642 if (upload_count == 0)
652 LOG_ERROR(
"You have no update permissions on upload %ld", UploadPk);
658 if (Tree) free(Tree);
663 if (rv == -1) LOG_ERROR(
"Unable to update mode on upload %ld", UploadPk);
668 for (i = 0; i < upload_count; i++)
670 UploadPk = uploads_to_scan[i];
673 if (Tree) free(Tree);
678 if (rv == -1) LOG_ERROR(
"Unable to update mode on upload %ld", UploadPk);
680 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.
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.