FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
container.c
Go to the documentation of this file.
1 /***************************************************************
2  Copyright (C) 2010-2011 Hewlett-Packard Development Company, L.P.
3 
4  This program is free software; you can redistribute it and/or
5  modify it under the terms of the GNU General Public License
6  version 2 as published by the Free Software Foundation.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License along
14  with this program; if not, write to the Free Software Foundation, Inc.,
15  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 
17  ***************************************************************/
18 
23 #include "buckets.h"
24 
25 extern int debug;
26 
27 
48 FUNCTION int *getContainerBuckets(PGconn *pgConn, pbucketdef_t bucketDefArray,
49  int uploadtree_pk)
50 {
51  char *fcnName = "getContainerBuckets";
52  char sql[1024];
53  int *bucket_pk_list = 0;
54  int numBucketDefs = 0;
55  int numLics;
56  int upload_pk, lft, rgt;
57  int bucketNumb;
58  PGresult *result;
59  pbucketdef_t pbucketDefArray;
60 
61  if (debug) printf("%s: for uploadtree_pk %d\n",fcnName,uploadtree_pk);
62 
63  /*** Create the return array ***/
64  /* count how many elements are in in_bucketDefArray.
65  This won't be needed after implementing pbucketpool_t
66  */
67  for (pbucketDefArray = bucketDefArray; pbucketDefArray->bucket_pk; pbucketDefArray++)
68  numBucketDefs++;
69 
70  /* Create a null terminated int array, to hold the bucket_pk list */
71  bucket_pk_list = calloc(numBucketDefs+1, sizeof(int));
72  if (bucket_pk_list == 0)
73  {
74  printf("FATAL: %s(%d) out of memory allocating int array of %d ints\n",
75  fcnName, __LINE__, numBucketDefs+1);
76  return 0;
77  }
78  /*** END: Create the return array ***/
79 
80  /*** Find lft and rgt bounds for uploadtree_pk ***/
81  snprintf(sql, sizeof(sql),
82  "SELECT lft,rgt,upload_fk FROM uploadtree WHERE uploadtree_pk ='%d'",
83  uploadtree_pk);
84  result = PQexec(pgConn, sql);
85  if (fo_checkPQresult(pgConn, result, sql, fcnName, __LINE__))
86  {
87  free(bucket_pk_list);
88  return 0;
89  }
90  numLics = PQntuples(result);
91  if (numLics == 0)
92  {
93  if (debug) printf("%s(%d): uploadtree_pk %d %s returned no recs.\n",__FILE__, __LINE__,uploadtree_pk, sql);
94  PQclear(result);
95  return bucket_pk_list;
96  }
97  lft = atoi(PQgetvalue(result, 0, 0));
98  rgt = atoi(PQgetvalue(result, 0, 1));
99  upload_pk = atoi(PQgetvalue(result, 0, 2));
100  PQclear(result);
101  /*** END: Find lft and rgt bounds for uploadtree_pk ***/
102 
103 
104  /*** Select all the unique buckets in this tree ***/
105  snprintf(sql, sizeof(sql),
106  "SELECT distinct(bucket_fk) as bucket_pk\
107  from bucket_file, bucket_def,\
108  (SELECT distinct(pfile_fk) as PF from uploadtree \
109  where upload_fk=%d\
110  and ((ufile_mode & (1<<28))=0)\
111  and uploadtree.lft BETWEEN %d and %d) as SS\
112  where PF=pfile_fk and agent_fk=%d\
113  and bucket_file.nomosagent_fk=%d\
114  and bucket_pk=bucket_fk\
115  and bucketpool_fk=%d",
116  upload_pk, lft, rgt, bucketDefArray->bucket_agent_pk,
117  bucketDefArray->nomos_agent_pk, bucketDefArray->bucketpool_pk);
118  if (debug) printf("%s(%d): Find buckets in container for uploadtree_pk %d\n%s\n",__FILE__, __LINE__,uploadtree_pk, sql);
119  result = PQexec(pgConn, sql);
120  if (fo_checkPQresult(pgConn, result, sql, fcnName, __LINE__))
121  {
122  free(bucket_pk_list);
123  return 0;
124  }
125  numLics = PQntuples(result);
126  if (numLics == 0)
127  {
128  PQclear(result);
129  return bucket_pk_list;
130  }
131  /*** END: Select all the unique buckets in this tree ***/
132 
133  /*** Populate the return array with the bucket_pk's ***/
134  for (bucketNumb=0; bucketNumb < numLics; bucketNumb++)
135  {
136  bucket_pk_list[bucketNumb] = atoi(PQgetvalue(result, bucketNumb, 0));
137  }
138  PQclear(result);
139 
140  if (debug)
141  {
142  printf("getContainerBuckets returning: ");
143  for (bucketNumb=0; bucketNumb < numLics; bucketNumb++)
144  {
145  printf("%d " ,bucket_pk_list[bucketNumb]);
146  }
147  printf("\n");
148  }
149 
150  return bucket_pk_list;
151 }
int fo_checkPQresult(PGconn *pgConn, PGresult *result, char *sql, char *FileID, int LineNumb)
Check the result status of a postgres SELECT.
Definition: libfossdb.c:181
int bucket_agent_pk
Definition: buckets.h:82
PGconn * pgConn
Database connection.
Definition: adj2nest.c:98
int debug
Definition: buckets.c:68
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:48
int nomos_agent_pk
Definition: buckets.h:81
const char * upload_pk
Definition: sqlstatements.h:93