FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
libfossdb.c
Go to the documentation of this file.
1 /**************************************************************
2 Copyright (C) 2011 Hewlett-Packard Development Company, L.P.
3 
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License version 2.1 as published by the Free Software Foundation.
7 
8 This library 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 GNU
11 Lesser General Public License for more details.
12 
13 You should have received a copy of the GNU Lesser General Public License
14 along with this library; if not, write to the Free Software Foundation, Inc.0
15 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 **************************************************************/
17 
23 #define ERRBUFSIZE 11264
24 
25 #include "libfossdb.h"
26 
27 #include <string.h>
28 
40 PGconn* fo_dbconnect(char* DBConfFile, char** ErrorBuf)
41 {
42  FILE* Fconf;
43  PGconn* pgConn;
44  char Line[1024];
45  char CMD[10240];
46  int i, CMDlen;
47  int C;
48  int PosEqual; /* index of "=" in Line */
49  int PosSemi; /* index of ";" in Line */
50  int BufLen;
51 
52  if (DBConfFile)
53  Fconf = fopen(DBConfFile, "r");
54  else
55  Fconf = fopen(FOSSDB_CONF, "r");
56  if (!Fconf)
57  {
58  *ErrorBuf = malloc(ERRBUFSIZE);
59  if (*ErrorBuf)
60  {
61  snprintf(*ErrorBuf, ERRBUFSIZE, "Database conf file: %s, ",
62  (DBConfFile ? DBConfFile : "FOSSDB_CONF"));
63  BufLen = strlen(*ErrorBuf);
64  strerror_r(errno, *ErrorBuf + BufLen, ERRBUFSIZE - BufLen);
65  }
66  return (NULL);
67  }
68 
69  /* read the configuration file */
70  memset(CMD, '\0', sizeof(CMD));
71  CMDlen = 0;
72  while (!feof(Fconf))
73  {
74  C = '@';
75  PosEqual = 0;
76  PosSemi = 0;
77  memset(Line, '\0', sizeof(Line));
78  /* read a line of data */
79  /* All lines are in the format: "field=value;" */
80  /* Lines beginning with "#" are ignored. */
81  for (i = 0; (i < sizeof(Line)) && (C != '\n') && (C > 0); i++)
82  {
83  C = fgetc(Fconf);
84  if ((C > 0) && (C != '\n')) Line[i] = C;
85  if ((C == '=') && !PosEqual) PosEqual = i;
86  else if ((C == ';') && !PosSemi) PosSemi = i;
87  }
88  /* check for a valid line */
89  if (PosSemi < PosEqual) PosEqual = 0;
90  if ((Line[0] != '#') && PosEqual && PosSemi)
91  {
92  /* looks good to me! */
93  if (CMD[0] != '\0')
94  {
95  CMD[CMDlen++] = ' ';
96  if (CMDlen >= sizeof(CMD))
97  {
98  fclose(Fconf);
99  goto BadConf;
100  }
101  }
102  Line[PosSemi] = '\0';
103  for (i = 0; i < PosEqual; i++)
104  {
105  if (!isspace(Line[i])) CMD[CMDlen++] = Line[i];
106  if (CMDlen >= sizeof(CMD))
107  {
108  fclose(Fconf);
109  goto BadConf;
110  }
111  }
112  CMD[CMDlen++] = '=';
113  if (CMDlen >= sizeof(CMD))
114  {
115  fclose(Fconf);
116  goto BadConf;
117  }
118  for (i = PosEqual + 1; Line[i] != '\0'; i++)
119  {
120  if (!isspace(Line[i])) CMD[CMDlen++] = Line[i];
121  if (CMDlen >= sizeof(CMD))
122  {
123  fclose(Fconf);
124  goto BadConf;
125  }
126  }
127  }
128  }
129 
130  /* done reading file */
131  fclose(Fconf);
132  if (CMD[0] == '\0') goto BadConf;
133 
134  /* Perform the connection */
135  pgConn = PQconnectdb(CMD);
136  if (PQstatus(pgConn) != CONNECTION_OK)
137  {
138  *ErrorBuf = malloc(ERRBUFSIZE);
139  if (*ErrorBuf)
140  {
141  int i = 0;
142  const char pass[10]= "password=";
143  for(i = strstr(CMD,pass) - CMD + strlen(pass); i < strlen(CMD); i++){
144  if(CMD[i] == ' '){
145  break;
146  }
147  CMD[i] ='*';
148  }
149  snprintf(*ErrorBuf, ERRBUFSIZE,
150  "ERROR: Unable to connect to the database\n Connection string: '%s'\n Connection status: '%d'\n Check: /usr/local/etc/fossology/Db.conf\n", CMD, PQstatus(pgConn));
151  }
152  return (NULL);
153  }
154 
155  return (pgConn);
156 
157  BadConf:
158  *ErrorBuf = malloc(ERRBUFSIZE);
159  snprintf(*ErrorBuf, ERRBUFSIZE, "Invalid Database conf file: %s, ",
160  (DBConfFile ? DBConfFile : "FOSSDB_CONF"));
161  return (NULL);
162 } /* fo_dbconnect() */
163 
164 
181 int fo_checkPQresult(PGconn* pgConn, PGresult* result, char* sql, char* FileID, int LineNumb)
182 {
183  if (!result)
184  {
185  printf("FATAL: %s:%d, %s\nOn: %s\n",
186  FileID, LineNumb, PQerrorMessage(pgConn), sql);
187  return -1;
188  }
189 
190  /* If no error, return */
191  if (PQresultStatus(result) == PGRES_TUPLES_OK) return 0;
192 
193  printf("ERROR: %s:%d, %s\nOn: %s\n",
194  FileID, LineNumb, PQresultErrorMessage(result), sql);
195  PQclear(result);
196  return (-1);
197 } /* fo_checkPQresult */
198 
199 
215 int fo_checkPQcommand(PGconn* pgConn, PGresult* result, char* sql, char* FileID, int LineNumb)
216 {
217  if (!result)
218  {
219  printf("FATAL: %s:%d, %sOn: %s\n",
220  FileID, LineNumb, PQerrorMessage(pgConn), sql);
221  return -1;
222  }
223 
224  /* If no error, return */
225  if (PQresultStatus(result) == PGRES_COMMAND_OK) return 0;
226 
227  printf("ERROR: %s:%d, %sOn: %s\n",
228  FileID, LineNumb, PQresultErrorMessage(result), sql);
229  PQclear(result);
230  return (-1);
231 } /* fo_checkPQcommand */
232 
233 
243 int fo_tableExists(PGconn* pgConn, const char* tableName)
244 {
245  char sql[256];
246  PGresult* result;
247  int TabCount;
248 
249  snprintf(sql, sizeof(sql),
250  "select count(*) from information_schema.tables where table_catalog='%s' and table_name='%s'",
251  PQdb(pgConn), tableName);
252  result = PQexec(pgConn, sql);
253  if (fo_checkPQresult(pgConn, result, sql, __FILE__, __LINE__)) return 0;
254 
255  TabCount = atol(PQgetvalue(result, 0, 0));
256 
257  PQclear(result);
258  return (TabCount);
259 } /* fo_tableExists() */
260 
char * DBConfFile
DB conf file location.
Definition: testRun.c:33
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
cmdlist CMD[]
Global command table.
int fo_checkPQcommand(PGconn *pgConn, PGresult *result, char *sql, char *FileID, int LineNumb)
Check the result status of a postgres commands (not select) If an error occured, write the error to s...
Definition: libfossdb.c:215
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
PGconn * fo_dbconnect(char *DBConfFile, char **ErrorBuf)
Connect to a database. The default is Db.conf.
Definition: libfossdb.c:40