FOSSology  4.4.0
Open Source License Compliance by Open Source Software
utils.c
Go to the documentation of this file.
1 /*
2  SPDX-FileCopyrightText: © 2013 Hewlett-Packard Development Company, L.P.
3 
4  SPDX-License-Identifier: GPL-2.0-only
5 */
11 #include "demomod.h"
12 
13 /********** Globals *************/
14 extern psqlCopy_t psqlcpy; // fo_sqlCopy struct used for fast data insertion
15 extern PGconn *pgConn; // database connection
16 
27 FUNCTION void CheckTable(char *AgentARSName)
28 {
29  PGresult* result; // the result of the database access
30  int rv; // generic return value
31  char *TableName = "demomod";
32  char *CreateTableSQL =
33  "CREATE TABLE demomod ( \
34  demomod_pk serial NOT NULL PRIMARY KEY, \
35  pfile_fk integer, \
36  agent_fk integer, \
37  firstbytes character(65), \
38  FOREIGN KEY (pfile_fk) REFERENCES pfile(pfile_pk) ON DELETE CASCADE, \
39  FOREIGN KEY (agent_fk) REFERENCES agent(agent_pk) ON DELETE CASCADE \
40  ); \
41  COMMENT ON TABLE demomod IS 'table for demo module'; \
42  COMMENT ON COLUMN demomod.firstbytes IS 'Hex string of first 32 bytes of the pfile'; \
43  ";
44 
45 
46  /* Check if the demomod_ars table exists. If not, create it.
47  * The _ars tables serve as an audit trail. They tell you when an agent
48  * has run and what the parameters were. They also provide a database service.
49  * Without the _ars tables, the agent would have to run in a single transaction,
50  * potentially inserting millions of records in that single transaction. This
51  * can result in deadlocks, large memory consumption, and slower performance.
52  * Because of the _ars tables, we can run in multiple transactions.
53  */
54  rv = fo_tableExists(pgConn, AgentARSName);
55  if (!rv)
56  {
57  rv = fo_CreateARSTable(pgConn, AgentARSName);
58  if (!rv)
59  {
60  LOG_FATAL("%s table could not be created", AgentARSName);
61  ExitNow(-10);
62  }
63  }
64 
65  /* Check if TableName exists. If not, create it
66  * An easy way to get all the table creation sql is to create the table with all your
67  * constraints manually or with phppgadmin, then export the table definition.
68  */
69  rv = fo_tableExists(pgConn, TableName);
70  if (!rv)
71  {
72  result = PQexec(pgConn, CreateTableSQL);
73  if (fo_checkPQresult(pgConn, result, CreateTableSQL, __FILE__, __LINE__))
74  {
75  LOG_ERROR("Failed to create %s table.", TableName);
76  ExitNow(-11);
77  }
78  }
79 
80 }
81 
82 
91 FUNCTION void Char2Hex(char *InBuf, int NumBytes, char *OutBuf)
92 {
93  int i;
94  char* pbuf = OutBuf;
95  for (i=0; i<NumBytes; i++) pbuf += sprintf(pbuf, "%02X", (unsigned char)InBuf[i]);
96 
97  *(pbuf) = '\0';
98 } /* Char2Hex() */
99 
100 
108 FUNCTION void ExitNow(int ExitVal)
109 {
110  if (pgConn) PQfinish(pgConn);
111 
112  if (ExitVal) LOG_ERROR("Exiting with status %d", ExitVal);
113 
114  fo_scheduler_disconnect(ExitVal);
115  exit(ExitVal);
116 } /* ExitNow() */
FUNCTION void Char2Hex(char *InBuf, int NumBytes, char *OutBuf)
Convert a character buffer to a hex string.
Definition: utils.c:91
PGconn * pgConn
Database connection.
Definition: adj2nest.c:86
FUNCTION void ExitNow(int ExitVal)
Exit function. This does all cleanup and should be used instead of calling exit() or main() return.
Definition: utils.c:108
psqlCopy_t psqlcpy
fo_sqlCopy struct used for fast data insertion
Definition: demomod.c:47
FUNCTION void CheckTable(char *AgentARSName)
Check to make sure the demomod and demomod_ars tables exists.
Definition: utils.c:27
FUNCTION int fo_CreateARSTable(PGconn *pgConn, const char *tableName)
Create ars table if it doesn't already exist.
Definition: libfossagent.c:270
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
int fo_tableExists(PGconn *pgConn, const char *tableName)
Check if table exists. Note, this assumes the database name is 'fossology'.
Definition: libfossdb.c:232
void fo_scheduler_disconnect(int retcode)
Disconnect the scheduler connection.