FOSSology  4.4.0
Open Source License Compliance by Open Source Software
container.c
Go to the documentation of this file.
1 /*
2  SPDX-FileCopyrightText: © 2010-2011 Hewlett-Packard Development Company, L.P.
3 
4  SPDX-License-Identifier: GPL-2.0-only
5 */
6 
11 #include "buckets.h"
12 
13 extern int debug;
14 
15 
36 FUNCTION int *getContainerBuckets(PGconn *pgConn, pbucketdef_t bucketDefArray,
37  int uploadtree_pk)
38 {
39  char *fcnName = "getContainerBuckets";
40  char sql[1024];
41  int *bucket_pk_list = 0;
42  int numBucketDefs = 0;
43  int numLics;
44  int upload_pk, lft, rgt;
45  int bucketNumb;
46  PGresult *result;
47  pbucketdef_t pbucketDefArray;
48 
49  if (debug) printf("%s: for uploadtree_pk %d\n",fcnName,uploadtree_pk);
50 
51  /*** Create the return array ***/
52  /* count how many elements are in in_bucketDefArray.
53  This won't be needed after implementing pbucketpool_t
54  */
55  for (pbucketDefArray = bucketDefArray; pbucketDefArray->bucket_pk; pbucketDefArray++)
56  numBucketDefs++;
57 
58  /* Create a null terminated int array, to hold the bucket_pk list */
59  bucket_pk_list = calloc(numBucketDefs+1, sizeof(int));
60  if (bucket_pk_list == 0)
61  {
62  printf("FATAL: %s(%d) out of memory allocating int array of %d ints\n",
63  fcnName, __LINE__, numBucketDefs+1);
64  return 0;
65  }
66  /*** END: Create the return array ***/
67 
68  /*** Find lft and rgt bounds for uploadtree_pk ***/
69  snprintf(sql, sizeof(sql),
70  "SELECT lft,rgt,upload_fk FROM uploadtree WHERE uploadtree_pk ='%d'",
71  uploadtree_pk);
72  result = PQexec(pgConn, sql);
73  if (fo_checkPQresult(pgConn, result, sql, fcnName, __LINE__))
74  {
75  free(bucket_pk_list);
76  return 0;
77  }
78  numLics = PQntuples(result);
79  if (numLics == 0)
80  {
81  if (debug) printf("%s(%d): uploadtree_pk %d %s returned no recs.\n",__FILE__, __LINE__,uploadtree_pk, sql);
82  PQclear(result);
83  return bucket_pk_list;
84  }
85  lft = atoi(PQgetvalue(result, 0, 0));
86  rgt = atoi(PQgetvalue(result, 0, 1));
87  upload_pk = atoi(PQgetvalue(result, 0, 2));
88  PQclear(result);
89  /*** END: Find lft and rgt bounds for uploadtree_pk ***/
90 
91 
92  /*** Select all the unique buckets in this tree ***/
93  snprintf(sql, sizeof(sql),
94  "SELECT distinct(bucket_fk) as bucket_pk\
95  from bucket_file, bucket_def,\
96  (SELECT distinct(pfile_fk) as PF from uploadtree \
97  where upload_fk=%d\
98  and ((ufile_mode & (1<<28))=0)\
99  and uploadtree.lft BETWEEN %d and %d) as SS\
100  where PF=pfile_fk and agent_fk=%d\
101  and bucket_file.nomosagent_fk=%d\
102  and bucket_pk=bucket_fk\
103  and bucketpool_fk=%d",
104  upload_pk, lft, rgt, bucketDefArray->bucket_agent_pk,
105  bucketDefArray->nomos_agent_pk, bucketDefArray->bucketpool_pk);
106  if (debug) printf("%s(%d): Find buckets in container for uploadtree_pk %d\n%s\n",__FILE__, __LINE__,uploadtree_pk, sql);
107  result = PQexec(pgConn, sql);
108  if (fo_checkPQresult(pgConn, result, sql, fcnName, __LINE__))
109  {
110  free(bucket_pk_list);
111  return 0;
112  }
113  numLics = PQntuples(result);
114  if (numLics == 0)
115  {
116  PQclear(result);
117  return bucket_pk_list;
118  }
119  /*** END: Select all the unique buckets in this tree ***/
120 
121  /*** Populate the return array with the bucket_pk's ***/
122  for (bucketNumb=0; bucketNumb < numLics; bucketNumb++)
123  {
124  bucket_pk_list[bucketNumb] = atoi(PQgetvalue(result, bucketNumb, 0));
125  }
126  PQclear(result);
127 
128  if (debug)
129  {
130  printf("getContainerBuckets returning: ");
131  for (bucketNumb=0; bucketNumb < numLics; bucketNumb++)
132  {
133  printf("%d " ,bucket_pk_list[bucketNumb]);
134  }
135  printf("\n");
136  }
137 
138  return bucket_pk_list;
139 }
PGconn * pgConn
Database connection.
Definition: adj2nest.c:86
FUNCTION int * getContainerBuckets(PGconn *pgConn, pbucketdef_t bucketDefArray, int uploadtree_pk)
Given a container uploadtree_pk and bucketdef, determine what buckets the container is in.
Definition: container.c:36
int debug
Definition: buckets.c:57
int fo_checkPQresult(PGconn *pgConn, PGresult *result, char *sql, char *FileID, int LineNumb)
Check the result status of a postgres SELECT.
Definition: libfossdb.c:170
const char * upload_pk
Definition: sqlstatements.h:82
int nomos_agent_pk
Definition: buckets.h:70
int bucket_agent_pk
Definition: buckets.h:71