FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
utils.c
Go to the documentation of this file.
1 /***************************************************************
2  Copyright (C) 2013 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 "demomod.h"
24 
25 /********** Globals *************/
26 extern psqlCopy_t psqlcpy; // fo_sqlCopy struct used for fast data insertion
27 extern PGconn *pgConn; // database connection
28 
39 FUNCTION void CheckTable(char *AgentARSName)
40 {
41  PGresult* result; // the result of the database access
42  int rv; // generic return value
43  char *TableName = "demomod";
44  char *CreateTableSQL =
45  "CREATE TABLE demomod ( \
46  demomod_pk serial NOT NULL PRIMARY KEY, \
47  pfile_fk integer, \
48  agent_fk integer, \
49  firstbytes character(65), \
50  FOREIGN KEY (pfile_fk) REFERENCES pfile(pfile_pk) ON DELETE CASCADE, \
51  FOREIGN KEY (agent_fk) REFERENCES agent(agent_pk) ON DELETE CASCADE \
52  ); \
53  COMMENT ON TABLE demomod IS 'table for demo module'; \
54  COMMENT ON COLUMN demomod.firstbytes IS 'Hex string of first 32 bytes of the pfile'; \
55  ";
56 
57 
58  /* Check if the demomod_ars table exists. If not, create it.
59  * The _ars tables serve as an audit trail. They tell you when an agent
60  * has run and what the parameters were. They also provide a database service.
61  * Without the _ars tables, the agent would have to run in a single transaction,
62  * potentially inserting millions of records in that single transaction. This
63  * can result in deadlocks, large memory consumption, and slower performance.
64  * Because of the _ars tables, we can run in multiple transactions.
65  */
66  rv = fo_tableExists(pgConn, AgentARSName);
67  if (!rv)
68  {
69  rv = fo_CreateARSTable(pgConn, AgentARSName);
70  if (!rv)
71  {
72  LOG_FATAL("%s table could not be created", AgentARSName);
73  ExitNow(-10);
74  }
75  }
76 
77  /* Check if TableName exists. If not, create it
78  * An easy way to get all the table creation sql is to create the table with all your
79  * constraints manually or with phppgadmin, then export the table definition.
80  */
81  rv = fo_tableExists(pgConn, TableName);
82  if (!rv)
83  {
84  result = PQexec(pgConn, CreateTableSQL);
85  if (fo_checkPQresult(pgConn, result, CreateTableSQL, __FILE__, __LINE__))
86  {
87  LOG_ERROR("Failed to create %s table.", TableName);
88  ExitNow(-11);
89  }
90  }
91 
92 }
93 
94 
103 FUNCTION void Char2Hex(char *InBuf, int NumBytes, char *OutBuf)
104 {
105  int i;
106  char* pbuf = OutBuf;
107  for (i=0; i<NumBytes; i++) pbuf += sprintf(pbuf, "%02X", (unsigned char)InBuf[i]);
108 
109  *(pbuf) = '\0';
110 } /* Char2Hex() */
111 
112 
120 FUNCTION void ExitNow(int ExitVal)
121 {
122  if (pgConn) PQfinish(pgConn);
123 
124  if (ExitVal) LOG_ERROR("Exiting with status %d", ExitVal);
125 
126  fo_scheduler_disconnect(ExitVal);
127  exit(ExitVal);
128 } /* ExitNow() */
PGconn * pgConn
Database connection.
Definition: adj2nest.c:98
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
FUNCTION void Char2Hex(char *InBuf, int NumBytes, char *OutBuf)
Convert a character buffer to a hex string.
Definition: utils.c:103
void fo_scheduler_disconnect(int retcode)
Disconnect the scheduler connection.
FUNCTION void CheckTable(char *AgentARSName)
Check to make sure the demomod and demomod_ars tables exists.
Definition: utils.c:39
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:120
FUNCTION int fo_CreateARSTable(PGconn *pgConn, const char *tableName)
Create ars table if it doesn&#39;t already exist.
Definition: libfossagent.c:284
int fo_tableExists(PGconn *pgConn, const char *tableName)
Check if table exists. Note, this assumes the database name is &#39;fossology&#39;.
Definition: libfossdb.c:243
psqlCopy_t psqlcpy
fo_sqlCopy struct used for fast data insertion
Definition: demomod.c:58