36 char *fcnName =
"getBucketpool";
42 sprintf(sqlbuf,
"select bucketpool_pk from bucketpool where (bucketpool_name='%s') and (active='Y') order by version desc",
44 result = PQexec(pgConn, sqlbuf);
46 if (PQntuples(result) > 0) bucketpool_pk = atoi(PQgetvalue(result, 0, 0));
65 char *fcnName =
"initBuckets";
76 if ((!pgConn) || (!bucketpool_pk))
78 printf(
"ERROR: %s.%s.%d Invalid input pgConn: %lx, bucketpool_pk: %d.\n",
79 __FILE__, fcnName, __LINE__, (
unsigned long)pgConn, bucketpool_pk);
84 sprintf(sqlbuf,
"select bucket_pk, bucket_type, bucket_regex, bucket_filename, stopon, bucket_name, applies_to from bucket_def where bucketpool_fk=%d order by bucket_evalorder asc", bucketpool_pk);
85 result = PQexec(pgConn, sqlbuf);
87 numRows = PQntuples(result);
90 printf(
"ERROR: %s.%s.%d No bucket defs for pool %d.\n",
91 __FILE__, fcnName, __LINE__, bucketpool_pk);
96 bucketDefList = calloc(numRows+1,
sizeof(
bucketdef_t));
97 if (bucketDefList == 0)
99 printf(
"ERROR: %s.%s.%d No memory to allocate %d bucket defs.\n",
100 __FILE__, fcnName, __LINE__, numRows);
105 for (rowNum=0; rowNum<numRows; rowNum++)
107 bucketDefList[rowNum].
bucket_pk = atoi(PQgetvalue(result, rowNum, 0));
108 bucketDefList[rowNum].
bucket_type = atoi(PQgetvalue(result, rowNum, 1));
109 bucketDefList[rowNum].bucketpool_pk = bucketpool_pk;
112 if (bucketDefList[rowNum].bucket_type == 3)
114 rv = regcomp(&bucketDefList[rowNum].compRegex, PQgetvalue(result, rowNum, 2),
115 REG_NOSUB | REG_ICASE | REG_EXTENDED);
118 printf(
"ERROR: %s.%s.%d Invalid regular expression for bucketpool_pk: %d, bucket: %s\n",
119 __FILE__, fcnName, __LINE__, bucketpool_pk, PQgetvalue(result, rowNum, 5));
122 bucketDefList[rowNum].
regex = strdup(PQgetvalue(result, rowNum, 2));
125 bucketDefList[rowNum].
dataFilename = strdup(PQgetvalue(result, rowNum, 3));
128 if (strlen(bucketDefList[rowNum].dataFilename) > 0)
130 snprintf(filepath,
sizeof(filepath),
"%s/bucketpools/%d/%s",
131 PROJECTSTATEDIR, bucketpool_pk, bucketDefList[rowNum].dataFilename);
132 if (stat(filepath, &statbuf) == -1)
135 gethostname(hostname,
sizeof(hostname));
136 printf(
"ERROR: %s.%s.%d File: %s is missing on host: %s. bucketpool_pk: %d, bucket: %s\n",
137 __FILE__, fcnName, __LINE__, filepath, hostname, bucketpool_pk, PQgetvalue(result, rowNum, 5));
143 if (bucketDefList[rowNum].bucket_type == 1)
144 bucketDefList[rowNum].
match_every =
getMatchEvery(pgConn, bucketpool_pk, bucketDefList[rowNum].dataFilename, pcroot);
147 if (bucketDefList[rowNum].bucket_type == 2)
149 bucketDefList[rowNum].
match_only =
getMatchOnly(pgConn, bucketpool_pk, bucketDefList[rowNum].dataFilename, pcroot);
153 if (bucketDefList[rowNum].bucket_type == 5)
155 bucketDefList[rowNum].
regex_row =
getRegexFile(pgConn, bucketpool_pk, bucketDefList[rowNum].dataFilename, pcroot);
158 bucketDefList[rowNum].
stopon = *PQgetvalue(result, rowNum, 4);
159 bucketDefList[rowNum].
bucket_name = strdup(PQgetvalue(result, rowNum, 5));
160 bucketDefList[rowNum].
applies_to = *PQgetvalue(result, rowNum, 6);
163 if (numErrors)
return 0;
167 for (rowNum=0; rowNum<numRows; rowNum++)
169 printf(
"\nbucket_pk[%d] = %d\n", rowNum, bucketDefList[rowNum].bucket_pk);
170 printf(
"bucket_name[%d] = %s\n", rowNum, bucketDefList[rowNum].bucket_name);
171 printf(
"bucket_type[%d] = %d\n", rowNum, bucketDefList[rowNum].bucket_type);
172 printf(
"dataFilename[%d] = %s\n", rowNum, bucketDefList[rowNum].dataFilename);
173 printf(
"stopon[%d] = %c\n", rowNum, bucketDefList[rowNum].stopon);
174 printf(
"applies_to[%d] = %c\n", rowNum, bucketDefList[rowNum].applies_to);
175 printf(
"nomos_agent_pk[%d] = %d\n", rowNum, bucketDefList[rowNum].nomos_agent_pk);
176 printf(
"bucket_agent_pk[%d] = %d\n", rowNum, bucketDefList[rowNum].bucket_agent_pk);
177 printf(
"regex[%d] = %s\n", rowNum, bucketDefList[rowNum].regex);
181 return bucketDefList;
199 char *fcnName =
"getMatchOnly";
200 char *delims =
",\t\n\r";
211 snprintf(filepath,
sizeof(filepath),
"%s/bucketpools/%d/%s",
212 PROJECTSTATEDIR, bucketpool_pk, filename);
215 fin = fopen(filepath,
"r");
218 printf(
"FATAL: %s.%s.%d Failure to open bucket file %s (pool=%d).\nError: %s\n",
219 __FILE__, fcnName, __LINE__, filepath, bucketpool_pk, strerror(errno));
224 while (fgets(inbuf,
sizeof(inbuf), fin)) line_count++;
228 match_only = calloc(line_count+1,
sizeof(
int));
231 printf(
"FATAL: %s.%s.%d Unable to allocate %d int array.\n",
232 __FILE__, fcnName, __LINE__, line_count+1);
242 while (fgets(inbuf,
sizeof(inbuf), fin))
245 sp = strtok(inbuf, delims);
248 if ((sp == 0) || (*sp ==
'#'))
continue;
255 match_only[matchNumb++] = lr_pk;
283 char *fcnName =
"getMatchEvery";
286 int **match_every = 0;
287 int **match_every_head = 0;
294 snprintf(filepath,
sizeof(filepath),
"%s/bucketpools/%d/%s",
295 PROJECTSTATEDIR, bucketpool_pk, filename);
298 fin = fopen(filepath,
"r");
301 printf(
"FATAL: %s.%s.%d Failure to initialize bucket %s (pool=%d).\nError: %s\n",
302 __FILE__, fcnName, __LINE__, filepath, bucketpool_pk, strerror(errno));
307 while (fgets(inbuf,
sizeof(inbuf), fin)) line_count++;
311 match_every = calloc(line_count+1,
sizeof(
int *));
314 printf(
"FATAL: %s.%s.%d Unable to allocate %d int array.\n",
315 __FILE__, fcnName, __LINE__, line_count+1);
319 match_every_head = match_every;
326 while (fgets(inbuf,
sizeof(inbuf), fin))
329 if (inbuf[0] ==
'#')
continue;
334 match_every[matchNumb++] = lr_pkArray;
340 free(match_every_head);
341 match_every_head = 0;
344 return match_every_head;
374 char *fcnName =
"getRegexFile";
382 char *Delims =
" \t\n\r";
388 snprintf(filepath,
sizeof(filepath),
"%s/bucketpools/%d/%s",
389 PROJECTSTATEDIR, bucketpool_pk, filename);
392 fin = fopen(filepath,
"r");
395 printf(
"FATAL: %s.%s.%d Failure to initialize bucket %s (pool=%d).\nError: %s\n",
396 __FILE__, fcnName, __LINE__, filepath, bucketpool_pk, strerror(errno));
397 printf(
"In v1.3, files were in %s. To be LSB compliate, v1.4 now requires them to be in %s\n",
398 DATADIR, PROJECTSTATEDIR);
403 while (fgets(inbuf,
sizeof(inbuf), fin)) line_count++;
407 regex_row_head = calloc(line_count+1,
sizeof(
regex_file_t));
410 printf(
"FATAL: %s.%s.%d Unable to allocate %d regex_file_t array.\n",
411 __FILE__, fcnName, __LINE__, line_count+1);
421 while (fgets(inbuf,
sizeof(inbuf), fin))
424 if (inbuf[0] ==
'#')
continue;
427 token = strtok_r(inbuf, Delims, &saveptr);
430 if (token[0] == 0)
continue;
433 if (regex_row_head[rowNumb].ftype1 == 0)
break;
436 token = strtok_r(NULL, Delims, &saveptr);
437 regex_row_head[rowNumb].
regex1 = strdup(token);
438 rv = regcomp(®ex_row_head[rowNumb].compRegex1, token, REG_NOSUB | REG_ICASE);
441 printf(
"ERROR: %s.%s.%d Invalid regular expression for file: %s, [%s], row: %d\n",
442 __FILE__, fcnName, __LINE__, filepath, token, rowNumb+1);
448 token = strtok_r(NULL, Delims, &saveptr);
456 if (strcasecmp(token,
"and") == 0) regex_row_head[rowNumb].
op = 1;
458 if (strcasecmp(token,
"or") == 0) regex_row_head[rowNumb].
op = 2;
460 if (strcasecmp(token,
"not") == 0) regex_row_head[rowNumb].
op = 3;
463 printf(
"ERROR: %s.%s.%d Invalid operator in file: %s, [%s], row: %d\n",
464 __FILE__, fcnName, __LINE__, filepath, token, rowNumb+1);
471 token = strtok_r(NULL, Delims, &saveptr);
473 if (regex_row_head[rowNumb].ftype2 == 0)
break;
476 token = strtok_r(NULL, Delims, &saveptr);
477 regex_row_head[rowNumb].
regex2 = strdup(token);
478 rv = regcomp(®ex_row_head[rowNumb].compRegex2, token, REG_NOSUB | REG_ICASE);
481 printf(
"ERROR: %s.%s.%d Invalid regular expression for file: %s, [%s], row: %d\n",
482 __FILE__, fcnName, __LINE__, filepath, token, rowNumb+1);
493 if (errorCount) exit(-1);
497 free(regex_row_head);
501 return regex_row_head;
517 if (strcasecmp(token,
"filename") == 0)
return(1);
519 if (strcasecmp(token,
"license") == 0)
return(2);
520 printf(
"FATAL: Invalid bucket file (%s), unknown filetype (%s)\n",
543 char *fcnName =
"getLicsInStr";
544 char *delims =
"|\n\r ";
547 int *pkArrayHead = 0;
552 if (!nameStr)
return 0;
557 while (*sp)
if (*sp++ == *delims) lic_count++;
561 pkArray = calloc(lic_count+1,
sizeof(
int));
564 printf(
"FATAL: %s.%s.%d Unable to allocate %d int array.\n",
565 __FILE__, fcnName, __LINE__, lic_count+1);
568 pkArrayHead = pkArray;
573 while ((sp = strtok(nameStr, delims)) != 0)
580 pkArray[matchNumb++] = lr_pk;
616 char *fcnName =
"LatestNomosAgent";
619 int nomos_agent_pk = 0;
623 snprintf(sql,
sizeof(sql),
624 "select agent_fk from nomos_ars, agent \ 625 WHERE agent_pk=agent_fk and ars_success=true and upload_fk='%d' \ 626 and agent_enabled=true order by agent_ts desc limit 1",
628 result = PQexec(pgConn, sql);
630 if (PQntuples(result) == 0)
return 0;
631 nomos_agent_pk = atoi(PQgetvalue(result,0,0));
633 return nomos_agent_pk;
652 char *fcnName =
"childParent";
655 int childParent_pk = 0;
659 snprintf(sql,
sizeof(sql),
660 "select uploadtree_pk,ufile_mode from uploadtree where parent=%d limit 1",
662 result = PQexec(pgConn, sql);
664 if (PQntuples(result) == 0)
break;
667 if ((atoi(PQgetvalue(result, 0, 1)) & 1<<28) == 0)
669 childParent_pk = uploadtree_pk;
672 uploadtree_pk = atoi(PQgetvalue(result, 0, 0));
674 }
while (childParent_pk == 0);
677 return childParent_pk;
int fo_checkPQresult(PGconn *pgConn, PGresult *result, char *sql, char *FileID, int LineNumb)
Check the result status of a postgres SELECT.
FUNCTION long lrcache_lookup(cacheroot_t *pcroot, char *rf_shortname)
Lookup rf_pk in the license_ref cache rf_shortname is the key.
PGconn * pgConn
Database connection.
FUNCTION int childParent(PGconn *pgConn, int uploadtree_pk)
Given an uploadtree_pk of a container, find the uploadtree_pk of it's children (i.e. scan down through artifacts to get the children's parent.
FUNCTION int LatestNomosAgent(PGconn *pgConn, int upload_pk)
Get the latest nomos agent_pk that has data for this this uploadtree.
FUNCTION int * getMatchOnly(PGconn *pgConn, int bucketpool_pk, char *filename, cacheroot_t *pcroot)
Read the match only file (bucket type 2)
FUNCTION int getBucketpool_pk(PGconn *pgConn, char *bucketpool_name)
Get a bucketpool_pk based on the bucketpool_name.
FUNCTION int ** getMatchEvery(PGconn *pgConn, int bucketpool_pk, char *filename, cacheroot_t *pcroot)
Read the match every file filename, for bucket type 1.
FUNCTION int getRegexFiletype(char *token, char *filepath)
Given a filetype token from REGEX-FILE return the token int representation.
FUNCTION regex_file_t * getRegexFile(PGconn *pgConn, int bucketpool_pk, char *filename, cacheroot_t *pcroot)
Parse filename, for bucket type 5 REGEX-FILE Lines are in format:
FUNCTION int * getLicsInStr(PGconn *pgConn, char *nameStr, cacheroot_t *pcroot)
Given a string with | separated license names return an integer array of rf_pk's. ...
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.