66 char BuildVersion[]=
"buckets build version: " VERSION_S
" r(" COMMIT_HASH_S
").\n";
72 int main(
int argc,
char **argv)
74 char *agentDesc =
"Bucket agent";
77 int ReadFromStdin = 1;
78 int head_uploadtree_pk = 0;
83 char *Delims =
",= \t\n\r";
84 char *token, *saveptr;
86 int nomos_agent_pk = 0;
87 int bucketpool_pk = 0;
93 char *bucketpool_name;
97 char agent_rev[myBUFSIZ];
113 while ((cmdopt = getopt(argc, argv,
"rin:p:t:u:vc:hV")) != -1)
122 bucketpool_name = optarg;
128 printf(
"%s is not an active bucketpool name.\n", bucketpool_name);
133 bucketpool_pk = atoi(optarg);
135 sprintf(sqlbuf,
"select bucketpool_pk from bucketpool where bucketpool_pk=%d and active='Y'", bucketpool_pk);
138 printf(
"%d is not an active bucketpool_pk.\n", atoi(optarg));
143 head_uploadtree_pk = atoi(optarg);
145 sprintf(sqlbuf,
"select uploadtree_pk from uploadtree where uploadtree_pk=%d", head_uploadtree_pk);
147 if (!head_uploadtree_pk)
148 printf(
"%d is not an active uploadtree_pk.\n", atoi(optarg));
152 if (!head_uploadtree_pk)
156 sprintf(sqlbuf,
"select upload_pk from upload where upload_pk=%d",
uploadtree.upload_fk);
159 printf(
"%d is not an valid upload_pk.\n", atoi(optarg));
162 sprintf(sqlbuf,
"select uploadtree_pk from uploadtree where upload_fk=%d and parent is null",
uploadtree.upload_fk);
187 if (!bucketpool_pk && !ReadFromStdin)
189 printf(
"FATAL: You must specify an active bucketpool.\n");
193 if (!head_uploadtree_pk && !ReadFromStdin)
195 printf(
"FATAL: You must specify a valid uploadtree_pk or upload_pk.\n");
205 sprintf(agent_rev,
"%s.%s", VERSION, COMMIT_HASH);
216 printf(
"FATAL: Bucket agent could not allocate license_ref table cache.\n");
235 while (token && (!
uploadtree.upload_fk || !bucketpool_pk))
237 if (strcmp(token,
"bppk") == 0)
239 bucketpool_pk = atoi(strtok_r(NULL, Delims, &saveptr));
242 if (strcmp(token,
"upk") == 0)
244 uploadtree.upload_fk = atoi(strtok_r(NULL, Delims, &saveptr));
246 token = strtok_r(NULL, Delims, &saveptr);
252 LOG_ERROR(
"You have no update permissions on upload %d",
uploadtree.upload_fk);
257 sprintf(sqlbuf,
"select uploadtree_pk, pfile_fk, ufile_name, ufile_mode,lft,rgt from uploadtree \
258 where upload_fk='%d' and parent is null limit 1",
uploadtree.upload_fk);
259 topresult = PQexec(
pgConn, sqlbuf);
261 if (PQntuples(topresult) == 0)
263 printf(
"ERROR: %s.%s missing upload_pk %d.\nsql: %s",
264 __FILE__, agentDesc,
uploadtree.upload_fk, sqlbuf);
268 head_uploadtree_pk = atol(PQgetvalue(topresult, 0, 0));
269 uploadtree.uploadtree_pk = head_uploadtree_pk;
271 uploadtree.pfile_fk = atol(PQgetvalue(topresult, 0, 1));
272 uploadtree.ufile_name = strdup(PQgetvalue(topresult, 0, 2));
273 uploadtree.ufile_mode = atoi(PQgetvalue(topresult, 0, 3));
274 uploadtree.lft = atoi(PQgetvalue(topresult, 0, 4));
275 uploadtree.rgt = atoi(PQgetvalue(topresult, 0, 5));
281 if (readnum > 1)
break;
286 sprintf(sqlbuf,
"select pfile_fk, ufile_name, ufile_mode,lft,rgt, upload_fk from uploadtree where uploadtree_pk=%d", head_uploadtree_pk);
287 topresult = PQexec(
pgConn, sqlbuf);
293 if (PQntuples(topresult) == 0)
295 printf(
"FATAL: %s.%s missing root uploadtree_pk %d\n",
296 __FILE__, agentDesc, head_uploadtree_pk);
300 uploadtree.uploadtree_pk = head_uploadtree_pk;
301 uploadtree.pfile_fk = atol(PQgetvalue(topresult, 0, 0));
302 uploadtree.ufile_name = strdup(PQgetvalue(topresult, 0, 1));
303 uploadtree.ufile_mode = atoi(PQgetvalue(topresult, 0, 2));
304 uploadtree.lft = atoi(PQgetvalue(topresult, 0, 3));
305 uploadtree.rgt = atoi(PQgetvalue(topresult, 0, 4));
306 uploadtree.upload_fk = atoi(PQgetvalue(topresult, 0, 5));
314 if (nomos_agent_pk == 0)
316 printf(
"WARNING: Bucket agent called on treeitem (%d), but the latest nomos agent hasn't created any license data for this tree.\n",
332 if (1 == rerun)
break;
333 printf(
"LOG: Duplicate request for bucket agent to process upload_pk: %d, uploadtree_pk: %d, bucketpool_pk: %d, bucket agent_pk: %d, nomos agent_pk: %d, pfile_pk: %d ignored.\n",
344 if (bucketDefArray == 0)
346 printf(
"FATAL: %s.%d Bucket definition for pool %d could not be initialized.\n",
347 __FILE__, __LINE__, bucketpool_pk);
357 LOG_FATAL(
"buckets passed invalid upload, upload_pk = %d",
uploadtree.upload_fk);
362 for (tmpbucketDefArray = bucketDefArray; tmpbucketDefArray->
bucket_pk; tmpbucketDefArray++)
369 for (tmpbucketDefArray = bucketDefArray; tmpbucketDefArray->
bucket_pk; tmpbucketDefArray++)
379 sprintf(sqlbuf,
"select mimetype_pk from mimetype where mimetype_name='application/x-debian-package'");
380 result = PQexec(
pgConn, sqlbuf);
382 if (PQntuples(result) == 0)
384 printf(
"FATAL: (%s.%d) Missing application/x-debian-package mimetype.\n",__FILE__,__LINE__);
390 sprintf(sqlbuf,
"select mimetype_pk from mimetype where mimetype_name='application/x-debian-source'");
391 result = PQexec(
pgConn, sqlbuf);
393 if (PQntuples(result) == 0)
395 printf(
"FATAL: (%s.%d) Missing application/x-debian-source mimetype.\n",__FILE__,__LINE__);
404 snprintf(sqlbuf,
sizeof(sqlbuf),
405 "insert into bucket_ars (agent_fk, upload_fk, ars_success, nomosagent_fk, bucketpool_fk) values(%d,%d,'%s',%d,%d)",
408 printf(
"%s(%d): %s\n", __FILE__, __LINE__, sqlbuf);
410 result = PQexec(
pgConn, sqlbuf);
415 sprintf(sqlbuf,
"select ars_pk from bucket_ars where agent_fk='%d' and upload_fk='%d' and ars_success='%s' and nomosagent_fk='%d' \
416 and bucketpool_fk='%d' and ars_endtime is null \
417 order by ars_starttime desc limit 1",
419 result = PQexec(
pgConn, sqlbuf);
421 if (PQntuples(result) == 0)
423 printf(
"FATAL: (%s.%d) Missing bucket_ars record.\n%s\n",__FILE__,__LINE__,sqlbuf);
426 ars_pk = atol(PQgetvalue(result, 0, 0));
431 if (
debug) printf(
"%s sql: %s\n",__FILE__, sqlbuf);
442 if ((!rv) && (IsContainer(
uploadtree.ufile_mode)))
448 if (0 == rerun && ars_pk)
451 snprintf(sqlbuf,
sizeof(sqlbuf),
452 "update bucket_ars set ars_endtime=now(), ars_success=false where ars_pk='%d'",
455 snprintf(sqlbuf,
sizeof(sqlbuf),
456 "update bucket_ars set ars_endtime=now(), ars_success=true where ars_pk='%d'",
460 printf(
"%s(%d): %s\n", __FILE__, __LINE__, sqlbuf);
462 result = PQexec(
pgConn, sqlbuf);
469 free(bucketDefArray);
PGconn * pgConn
Database connection.
char * uploadtree_tablename
upload.uploadtree_tablename
int main(int argc, char **argv)
int verbose
The verbose flag for the cli.
Usage()
Print Usage statement.
FUNCTION int LatestNomosAgent(PGconn *pgConn, int upload_pk)
Get the latest nomos agent_pk that has data for this this uploadtree.
FUNCTION int getBucketpool_pk(PGconn *pgConn, char *bucketpool_name)
Get a bucketpool_pk based on the bucketpool_name.
FUNCTION pbucketdef_t initBuckets(PGconn *pgConn, int bucketpool_pk, cacheroot_t *pcroot)
Initialize the bucket definition list. If an error occured, write the error to stdout.
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...
#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 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.
FUNCTION void lrcache_free(cacheroot_t *pcroot)
Free the hash table.
FUNCTION int lrcache_init(PGconn *pgConn, cacheroot_t *pcroot)
Build a cache the license ref db table.
int maxnodes
No. of nodes in the list.
cachenode_t * nodes
Array of nodes.
Contains information required by uploadtree elements.
FUNCTION int UploadProcessed(PGconn *pgConn, int bucketagent_pk, int nomosagent_pk, int pfile_pk, int uploadtree_pk, int upload_pk, int bucketpool_pk)
Has this upload already been bucket processed? This function checks buckets_ars to see if the upload ...
FUNCTION int validate_pk(PGconn *pgConn, char *sql)
Verify a primary key exists.
FUNCTION int walkTree(PGconn *pgConn, pbucketdef_t bucketDefArray, int agent_pk, int uploadtree_pk, int skipProcessedCheck, int hasPrules)
This function does a recursive depth first walk through a file tree (uploadtree).
FUNCTION int processFile(PGconn *pgConn, pbucketdef_t bucketDefArray, puploadtree_t puploadtree, int agent_pk, int hasPrules)
Process a file.