FOSSology  4.4.0
Open Source License Compliance by Open Source Software
write.c
Go to the documentation of this file.
1 /*
2  SPDX-FileCopyrightText: © 2010-2014 Hewlett-Packard Development Company, L.P.
3 
4  SPDX-License-Identifier: GPL-2.0-only
5 */
6 
12 #include "buckets.h"
13 
14 extern int debug;
15 
29 FUNCTION int writeBuckets(PGconn *pgConn, int pfile_pk, int uploadtree_pk,
30  int *bucketList, int agent_pk, int nomosagent_pk, int bucketpool_pk)
31 {
32  char *fcnName = "writeBuckets";
33  char sql[1024];
34  PGresult *result = 0;
35  int rv = 0;
36  //if (debug) printf("debug: %s:%s() pfile: %d, uploadtree_pk: %d\n", __FILE__, fcnName, pfile_pk, uploadtree_pk);
37  if (debug) printf("debug: %s:%s() pfile: %d, uploadtree_pk: %d\n", __FILE__, fcnName, pfile_pk, uploadtree_pk);
38 
39 
40  if (bucketList)
41  {
42  while(*bucketList)
43  {
45  if (pfile_pk)
46  {
47  if (processed(pgConn, agent_pk, nomosagent_pk, pfile_pk, uploadtree_pk, bucketpool_pk, *bucketList))
48  {
49  snprintf(sql, sizeof(sql),
50  "UPDATE bucket_file set bucket_fk = %d from bucket_def where pfile_fk = %d and \
51  bucket_fk= bucket_pk and bucket_def.bucketpool_fk = %d;",
52  *bucketList, pfile_pk, bucketpool_pk);
53  }
54  else
55  {
56  snprintf(sql, sizeof(sql),
57  "insert into bucket_file (bucket_fk, pfile_fk, agent_fk, nomosagent_fk) values(%d,%d,%d,%d)",
58  *bucketList, pfile_pk, agent_pk, nomosagent_pk);
59  }
60  if (debug)
61  printf("%s(%d): %s\n", __FILE__, __LINE__, sql);
62  result = PQexec(pgConn, sql);
63  // ignore duplicate constraint failure (23505), report others
64  if ((result==0) || ((PQresultStatus(result) != PGRES_COMMAND_OK) &&
65  (strncmp("23505", PQresultErrorField(result, PG_DIAG_SQLSTATE),5))))
66  {
67  printf("ERROR: %s.%s().%d: Failed to add bucket to bucket_file.\n",
68  __FILE__,fcnName, __LINE__);
69  fo_checkPQresult(pgConn, result, sql, __FILE__, __LINE__);
70  PQclear(result);
71  rv = -1;
72  break;
73  }
74  }
75  else
76  {
77  snprintf(sql, sizeof(sql),
78  "insert into bucket_container (bucket_fk, uploadtree_fk, agent_fk, nomosagent_fk) \
79  values(%d,%d,%d,%d)", *bucketList, uploadtree_pk, agent_pk, nomosagent_pk);
80  if (debug)
81  printf("%s(%d): %s\n", __FILE__, __LINE__, sql);
82 
83  result = PQexec(pgConn, sql);
84  if ((PQresultStatus(result) != PGRES_COMMAND_OK) &&
85  (strncmp("23505", PQresultErrorField(result, PG_DIAG_SQLSTATE),5)))
86  {
87  // ignore duplicate constraint failure (23505)
88  printf("ERROR: %s.%s().%d: Failed to add bucket to bucket_file. %s\n: %s\n",
89  __FILE__,fcnName, __LINE__,
90  PQresultErrorMessage(result), sql);
91  PQclear(result);
92  rv = -1;
93  break;
94  }
95  }
96  if (result) PQclear(result);
97  bucketList++;
98  }
99  }
100 
101  if (debug) printf("%s:%s() returning rv=%d\n", __FILE__, fcnName, rv);
102  return rv;
103 }
PGconn * pgConn
Database connection.
Definition: adj2nest.c:86
int agent_pk
Definition: agent.h:74
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
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:139
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:29
int debug
Definition: buckets.c:57