FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
libfodbreposysconf.c
Go to the documentation of this file.
1 /* **************************************************************
2  Copyright (C) 2011 Hewlett-Packard Development Company, L.P.
3  Copyright (C) 2015, 2018 Siemens AG
4 
5  This program is free software; you can redistribute it and/or
6  modify it under the terms of the GNU General Public License
7  version 2 as published by the Free Software Foundation.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License along
15  with this program; if not, write to the Free Software Foundation, Inc.,
16  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 ************************************************************** */
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <stdarg.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <unistd.h>
31 
32 #include <libfossscheduler.h>
33 #include <libfossdb.h>
34 #include "libfodbreposysconf.h"
35 
36 #ifndef TESTDBDIR
37 // this is only to make IDEs happy
38 #define TESTDBDIR "../../../testing/db/"
39 #error
40 #endif
41 
42 static char* Sysconf = NULL;
43 static char DBName[ARRAY_LENGTH];
44 static char DBConf[ARRAY_LENGTH];
45 static char RepoDir[ARRAY_LENGTH];
46 static char confFile[ARRAY_LENGTH];
47 
48 fo_dbManager* createTestEnvironment(const char* srcDirs, const char* doConnectAsAgent, int initDbTables) {
49  GString* gString = g_string_new(TESTDBDIR "/createTestEnvironment.php");
50  if (srcDirs) {
51  g_string_append_printf(gString, " -d '%s'", srcDirs);
52  }
53  if (initDbTables) {
54  g_string_append_printf(gString, " -f");
55  }
56  gchar* cmd = g_string_free(gString, FALSE);
57 
58  FILE* pipe = popen(cmd, "r");
59 
60  if (!pipe) {
61  printf("cannot run create test environment script: %s\n", cmd);
62  goto createError;
63  }
64 
65  Sysconf = calloc(1, ARRAY_LENGTH + 1);
66  size_t count = fread(Sysconf, 1, ARRAY_LENGTH, pipe);
67 
68  int rv = fclose(pipe);
69 
70  if (rv != 0 || count == 0) {
71  printf("command %s failed with output:\n%s\n", cmd, Sysconf);
72  goto createError;
73  }
74 
75  g_free(cmd);
76 
77  fo_dbManager* result = NULL;
78  if (doConnectAsAgent) {
79  char* argv[] = {(char*) doConnectAsAgent, "-c", Sysconf};
80  int argc = 3;
81 
82  fo_scheduler_connect_dbMan(&argc, argv, &result);
83  } else {
84  char buffer[ARRAY_LENGTH + 1];
85  snprintf(buffer, ARRAY_LENGTH, "%s/Db.conf", Sysconf);
86  char* errorMsg = NULL;
87  PGconn* conn = fo_dbconnect(buffer, &errorMsg);
88 
89  if (!errorMsg) {
90  result = fo_dbManager_new_withConf(conn, buffer);
91  } else {
92  printf("error connecting: %s\n", errorMsg);
93  }
94  }
95  return result;
96 
97 createError:
98  if (cmd) {
99  g_free(cmd);
100  }
101  return NULL;
102 }
103 
104 void dropTestEnvironment(fo_dbManager* dbManager, const char* srcDir, const char* doConnectAsAgent) {
105  if (dbManager) {
106  fo_dbManager_finish(dbManager);
107  }
108  if (doConnectAsAgent) {
110  }
111 
112  if (Sysconf) {
113  char buffer[ARRAY_LENGTH];
114  snprintf(buffer, ARRAY_LENGTH, TESTDBDIR "/purgeTestEnvironment.php -d '%s' -c '%s'", srcDir, Sysconf);
115  FILE* pipe = popen(buffer, "r");
116 
117  if (!pipe) {
118  printf("cannot run purge test environment script: %s\n", buffer);
119  return;
120  }
121 
122  fclose(pipe);
123  free(Sysconf);
124  }
125 }
126 
132 static void command_output(char* command) {
133  FILE* stream;
134  char tmp[ARRAY_LENGTH];
135  int i = 0;
136  int status = 0;
137 
138  stream = popen(command, "r");
139  if (!stream) status = 1;
140  memset(tmp, '\0', sizeof(tmp));
141  if (fgets(tmp, ARRAY_LENGTH, stream) != NULL) {
142  while ((tmp[i] != '\n') && (tmp[i] != ' ') && (tmp[i] != EOF))
143  i++;
144  Sysconf = malloc(i);
145  memcpy(Sysconf, tmp, i);
146  Sysconf[i] = '\0';
147  }
148  int rc = pclose(stream);
149  if (rc != 0) status = 1;
150  if (status == 1) {
151  printf("Failed to run %s, exit code is:%d .\n", command, rc >> 8);
152  exit(1);
153  }
154  return;
155 }
156 
165 int create_db_repo_sysconf(int type, char* agent_name, char* sysconfdir) {
166 #if 0
167  char *sysconfdir;
169  sysconfdir = getenv ("SYSCONFDIR");
170  if (sysconfdir == NULL)
171  {
172  printf ("The SYSCONFDIR enviroment variable is not existed.\n");
173  return 1;
174  }
175 #endif
176 
177  const char INIT_CMD[] = "../../../testing/db/createTestDB.php";
178  char *CMD, *tmp;
179 
180  CMD = (char *)malloc(strlen(INIT_CMD) + 1);
181  if (!CMD)
182  {
183  return -1;
184  }
185  sprintf(CMD, "%s", INIT_CMD);
186 
187  if (sysconfdir)
188  {
189  tmp = (char *)malloc(strlen(CMD) + 4 + strlen(sysconfdir) + 1);
190  if (!tmp)
191  {
192  free(CMD);
193  return -1;
194  }
195  sprintf(tmp, "%s -c %s", CMD, sysconfdir);
196  free(CMD);
197  CMD = tmp;
198  }
199 
200  switch(type)
201  {
202  case 0:
203  tmp = (char *)malloc(strlen(CMD) + 4);
204  if (!tmp)
205  {
206  free(CMD);
207  return -1;
208  }
209  sprintf(tmp, "%s -e", CMD);
210  free(CMD);
211  CMD = tmp;
212  case 1:
213  command_output(CMD);
214  break;
215  default:
216  break;
217  }
218 
219  free(CMD);
220 
221  int argc = 3;
222  char* argv[] = {agent_name, "-c", Sysconf};
223 
224  PGconn* unused;
225  fo_scheduler_connect(&argc, argv, &unused);
226 
227 #ifdef TEST
228  printf("create_db_repo_sysconf sucessfully\n");
229 #endif
230  return 0;
231 }
232 
238 void drop_db_repo_sysconf(char* DBName) {
239  char CMD[ARRAY_LENGTH];
240  memset(CMD, '\0', sizeof(CMD));
241  sprintf(CMD, "../../../testing/db/createTestDB.php -d %s", DBName);
242  command_output(CMD);
243 #ifdef TEST
244  printf("remove DBName is:%s\n", DBName);
245 #endif
247  free(Sysconf);
248  Sysconf = NULL;
249 #ifdef TEST
250  printf("drop_db_repo_sysconf sucessfully\n");
251 #endif
252 }
253 
259 char* get_test_name() {
260  char* TestName = strstr(Sysconf, "Conf") + 4;
261 #ifdef TEST
262  printf("TestName is:%s\n", TestName);
263 #endif
264  return TestName;
265 }
266 
272 char* get_db_name() {
273  memset(DBName, '\0', sizeof(DBName));
274  char* TestName = get_test_name();
275  sprintf(DBName, "fosstest%s", TestName);
276 #ifdef TEST
277  printf("DBName is:%s\n", DBName);
278 #endif
279  return DBName;
280 }
281 
287 char* get_sysconfdir() {
288 #ifdef TEST
289  printf("Sysconf is:%s\n", Sysconf);
290 #endif
291  return Sysconf;
292 }
293 
299 char* get_dbconf() {
300  memset(DBConf, '\0', sizeof(DBConf));
301  sprintf(DBConf, "%s/Db.conf", Sysconf);
302  return DBConf;
303 }
304 
305 char* get_confFile() {
306  memset(confFile, '\0', sizeof(confFile));
307  sprintf(confFile, "%s/fossology.conf", Sysconf);
308  return confFile;
309 }
315 char* get_repodir() {
316  strncpy(RepoDir, Sysconf, ARRAY_LENGTH);
317  RepoDir[ARRAY_LENGTH-1] = '\0';
318 
319  char* test_name_tmp = strstr(RepoDir, "testDbConf");
320  if (test_name_tmp)
321  {
322  *test_name_tmp = '\0';
323  }
324 
325  char *tmp = malloc(strlen(RepoDir) + 1);
326  if (!tmp) {
327  return NULL;
328  }
329 
330  sprintf(tmp, "%s", RepoDir);
331  sprintf(RepoDir, "%stestDbRepo%s", tmp, get_test_name());
332 
333  free(tmp);
334 
335 #ifdef TEST
336  printf("RepoDir is:%s\n", RepoDir);
337 #endif
338  return RepoDir;
339 }
340 
349 char *createTestConfDir(char* cwd, char* agentName)
350 {
351  struct stat st = {0};
352  int rc;
353  char CMD[2048];
354  FILE *testConfFile;
355 
356  char *confDir = malloc((strlen(cwd) + 10) * sizeof(char));
357  char confFile[1024];
358  char agentDir[1024];
359 
360  if(cwd == NULL || agentName == NULL || cwd[0] == '\0' || agentName[0] == '\0')
361  {
362  return NULL;
363  }
364 
365  sprintf(confDir, "%s/testconf", cwd);
366  sprintf(confFile, "%s/fossology.conf", confDir);
367  sprintf(agentDir, "%s/../..", cwd);
368 
369  if (stat(confDir, &st) == -1)
370  {
371  mkdir(confDir, 0775);
372  }
373 
374  memset(CMD, '\0', sizeof(CMD));
375  sprintf(CMD, "%s/mods-enabled/%s", confDir, agentName);
376  if (stat(CMD, &st) == -1)
377  {
378  mkdir(CMD, 0775);
379  }
380 
381  testConfFile = fopen(confFile,"w");
382  fprintf(testConfFile, ";fossology.conf for testing\n");
383  fprintf(testConfFile, "[FOSSOLOGY]\nport = 24693\n");
384  fprintf(testConfFile, "address = localhost\n");
385  fprintf(testConfFile, "depth = 0\n");
386  fprintf(testConfFile, "path = %s\n", confDir);
387  fprintf(testConfFile, "[HOSTS]\n");
388  fprintf(testConfFile, "localhost = localhost AGENT_DIR 10\n");
389  fprintf(testConfFile, "[REPOSITORY]\n");
390  fprintf(testConfFile, "localhost = * 00 ff\n");
391  fprintf(testConfFile, "[DIRECTORIES]\n");
392  fprintf(testConfFile, "PROJECTUSER=fossy\n");
393  fprintf(testConfFile, "PROJECTGROUP=fossy\n");
394  fprintf(testConfFile, "MODDIR=%s/../../../..\n", cwd);
395  fprintf(testConfFile, "LOGDIR=%s\n", confDir);
396  fclose(testConfFile);
397 
398  memset(CMD, '\0', sizeof(CMD));
399  sprintf(CMD, "install -D %s/../../../../VERSION %s/VERSION", cwd, confDir);
400  rc = system(CMD);
401 
402  memset(CMD, '\0', sizeof(CMD));
403  sprintf(CMD, "install -D %s/../../../../install/defconf/Db.conf %s/Db.conf", cwd, confDir);
404  rc = system(CMD);
405 
406  memset(CMD, '\0', sizeof(CMD));
407  sprintf(CMD, "install -D %s/VERSION %s/mods-enabled/%s/VERSION", agentDir, confDir, agentName);
408  rc = system(CMD);
409 
410  memset(CMD, '\0', sizeof(CMD));
411  sprintf(CMD, "ln -fs %s/agent %s/mods-enabled/%s", agentDir, confDir, agentName);
412  rc = system(CMD);
413 
414  if (rc != -1)
415  return confDir;
416  else
417  return NULL;
418 }
419 
420 #if 0
421 int main()
422 {
424  get_test_name();
425  get_sysconfdir();
426  get_db_name();
427  drop_db_repo_sysconf(DBName);
428 }
429 #endif
static void command_output(char *command)
get command output
int create_db_repo_sysconf(int type, char *agent_name, char *sysconfdir)
char * get_test_name()
get the test name just created by create_db_repo_sysconf()
char * createTestConfDir(char *cwd, char *agentName)
create a dummy sysConfDir for a given agent
void fo_scheduler_disconnect(int retcode)
Disconnect the scheduler connection.
void fo_scheduler_connect(int *argc, char **argv, PGconn **db_conn)
Establish a connection between an agent and the scheduler.
char * get_dbconf()
get Db.conf path just created by create_db_repo_sysconf()
void drop_db_repo_sysconf(char *DBName)
drop db, sysconfig dir and repo
cmdlist CMD[]
Global command table.
char buffer[2048]
The last thing received from the scheduler.
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:28
char * get_db_name()
get the DB name just created by create_db_repo_sysconf()
void fo_scheduler_connect_dbMan(int *argc, char **argv, fo_dbManager **dbManager)
Make a connection from an agent to the scheduler and create a DB manager as well. ...
char * get_sysconfdir()
get sysconfig dir path just created by create_db_repo_sysconf()
PGconn * fo_dbconnect(char *DBConfFile, char **ErrorBuf)
Connect to a database. The default is Db.conf.
Definition: libfossdb.c:40
char * get_repodir()
get repo path just created by create_db_repo_sysconf()