FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
write.c
Go to the documentation of this file.
1 /***************************************************************
2  Copyright (C) 2010-2014 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  ***************************************************************/
23 #include "buckets.h"
24 
25 extern int debug;
26 
40 FUNCTION int writeBuckets(PGconn *pgConn, int pfile_pk, int uploadtree_pk,
41  int *bucketList, int agent_pk, int nomosagent_pk, int bucketpool_pk)
42 {
43  char *fcnName = "writeBuckets";
44  char sql[1024];
45  PGresult *result = 0;
46  int rv = 0;
47  //if (debug) printf("debug: %s:%s() pfile: %d, uploadtree_pk: %d\n", __FILE__, fcnName, pfile_pk, uploadtree_pk);
48  if (debug) printf("debug: %s:%s() pfile: %d, uploadtree_pk: %d\n", __FILE__, fcnName, pfile_pk, uploadtree_pk);
49 
50 
51  if (bucketList)
52  {
53  while(*bucketList)
54  {
56  if (pfile_pk)
57  {
58  if (processed(pgConn, agent_pk, nomosagent_pk, pfile_pk, uploadtree_pk, bucketpool_pk, *bucketList))
59  {
60  snprintf(sql, sizeof(sql),
61  "UPDATE bucket_file set bucket_fk = %d from bucket_def where pfile_fk = %d and \
62  bucket_fk= bucket_pk and bucket_def.bucketpool_fk = %d;",
63  *bucketList, pfile_pk, bucketpool_pk);
64  }
65  else
66  {
67  snprintf(sql, sizeof(sql),
68  "insert into bucket_file (bucket_fk, pfile_fk, agent_fk, nomosagent_fk) values(%d,%d,%d,%d)",
69  *bucketList, pfile_pk, agent_pk, nomosagent_pk);
70  }
71  if (debug)
72  printf("%s(%d): %s\n", __FILE__, __LINE__, sql);
73  result = PQexec(pgConn, sql);
74  // ignore duplicate constraint failure (23505), report others
75  if ((result==0) || ((PQresultStatus(result) != PGRES_COMMAND_OK) &&
76  (strncmp("23505", PQresultErrorField(result, PG_DIAG_SQLSTATE),5))))
77  {
78  printf("ERROR: %s.%s().%d: Failed to add bucket to bucket_file.\n",
79  __FILE__,fcnName, __LINE__);
80  fo_checkPQresult(pgConn, result, sql, __FILE__, __LINE__);
81  PQclear(result);
82  rv = -1;
83  break;
84  }
85  }
86  else
87  {
88  snprintf(sql, sizeof(sql),
89  "insert into bucket_container (bucket_fk, uploadtree_fk, agent_fk, nomosagent_fk) \
90  values(%d,%d,%d,%d)", *bucketList, uploadtree_pk, agent_pk, nomosagent_pk);
91  if (debug)
92  printf("%s(%d): %s\n", __FILE__, __LINE__, sql);
93 
94  result = PQexec(pgConn, sql);
95  if ((PQresultStatus(result) != PGRES_COMMAND_OK) &&
96  (strncmp("23505", PQresultErrorField(result, PG_DIAG_SQLSTATE),5)))
97  {
98  // ignore duplicate constraint failure (23505)
99  printf("ERROR: %s.%s().%d: Failed to add bucket to bucket_file. %s\n: %s\n",
100  __FILE__,fcnName, __LINE__,
101  PQresultErrorMessage(result), sql);
102  PQclear(result);
103  rv = -1;
104  break;
105  }
106  }
107  if (result) PQclear(result);
108  bucketList++;
109  }
110  }
111 
112  if (debug) printf("%s:%s() returning rv=%d\n", __FILE__, fcnName, rv);
113  return rv;
114 }
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
PGconn * pgConn
Database connection.
Definition: adj2nest.c:98
int debug
Definition: buckets.c:68
FUNCTION int writeBuckets(PGconn *pgConn, int pfile_pk, int uploadtree_pk, int *bucketList, int agent_pk, int nomosagent_pk, int bucketpool_pk)
Write bucket results to either db (bucket_file, bucket_container) or stdout.
Definition: write.c:40
int agent_pk
Definition: agent.h:85
void fo_scheduler_heart(int i)
This function must be called by agents to let the scheduler know they are alive and how many items th...
FUNCTION int processed(PGconn *pgConn, int agent_pk, int nomos_agent_pk, int pfile_pk, int uploadtree_pk, int bucketpool_pk, int bucket_pk)
Has this pfile or uploadtree_pk already been bucket processed? This only works if the bucket has been...
Definition: validate.c:150