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  SPDX-FileCopyrightText: © 2019 Siemens AG
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
12 #include "maintagent.h"
13 
21 FUNCTION void exitNow(int exitVal)
22 {
23  if (pgConn) PQfinish(pgConn);
25 
26  if (exitVal) LOG_ERROR("Exiting with status %d", exitVal);
27 
28  fo_scheduler_disconnect(exitVal);
29  exit(exitVal);
30 } /* ExitNow() */
31 
39 FUNCTION char* strtoupper(char* s)
40 {
41  char* p = s;
42  while ((*p = toupper(*p)))
43  p++;
44  return s;
45 }
46 
67 FUNCTION void recurseDir(const char* type, char* path, int level)
68 {
69  DIR* dir;
70  struct dirent* entry;
71  dir = opendir(path);
72  if (dir == NULL)
73  {
74  LOG_ERROR("Unable to open dir: '%s'", path);
75  LOG_ERROR("Error: %s", strerror(errno));
76  return;
77  }
78  while ((entry = readdir(dir)) != NULL)
79  {
80  if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0)
81  {
82  char nextpath[myBUFSIZ];
83  if (level > 0)
84  {
85  // Have not reached to file yet
86  strncpy(nextpath, path, myBUFSIZ - 1);
87  strncat(nextpath, "/", myBUFSIZ - 1);
88  strncat(nextpath, entry->d_name, myBUFSIZ - 1);
89 
90  recurseDir(type, nextpath, level - 1);
91  }
92  else
93  {
94  // entry is a file
95  char delim[] = ".";
96  char sha1[41];
97  char md5[33];
98  long fsize;
99  char* ptr;
100 
101  memset(sha1, '\0', 41);
102  memset(md5, '\0', 33);
103  strncpy(nextpath, entry->d_name, myBUFSIZ - 1);
104  ptr = strtok(nextpath, delim);
105  if (ptr == NULL)
106  {
107  LOG_FATAL("Unable to split path '%s' for pfile.", nextpath);
108  exitNow(-105);
109  }
110  strncpy(sha1, ptr, 40);
111  ptr = strtok(NULL, delim);
112  strncpy(md5, ptr, 32);
113  ptr = strtok(NULL, delim);
114  fsize = atol(ptr);
115  checkPFileExists(sha1, md5, fsize, type);
116  }
117  }
118  }
119  closedir(dir);
120 }
121 
130 FUNCTION void checkPFileExists(char* sha1, char* md5, long fsize,
131  const char* type)
132 {
133  PGresult* result;
134  int countTuples;
135  fo_dbManager_PreparedStatement* existsStatement;
136  char sql[] =
137  "WITH pf AS ("
138  "SELECT pfile_pk FROM pfile "
139  "WHERE pfile_md5 = $1 AND pfile_sha1 = $2 AND pfile_size = $3) "
140  "SELECT 1 AS exists FROM uploadtree INNER JOIN pf "
141  "ON pf.pfile_pk = pfile_fk "
142  "UNION ALL "
143  "SELECT 1 AS exists FROM upload INNER JOIN pf "
144  "ON pf.pfile_pk = pfile_fk;";
145 
146  existsStatement = fo_dbManager_PrepareStamement(dbManager, "checkPfileExists",
147  sql, char*, char*, long);
148  result = fo_dbManager_ExecPrepared(existsStatement, strtoupper(md5),
149  strtoupper(sha1), fsize);
150  if (fo_checkPQresult(pgConn, result, sql, __FILE__, __LINE__))
151  {
152  exitNow(-140);
153  }
154 
155  countTuples = PQntuples(result);
156  if (countTuples < 1)
157  {
158  deleteRepoFile(sha1, md5, fsize, type);
160  }
161  else
162  {
164  }
165  PQclear(result);
166 }
167 
177 FUNCTION void deleteRepoFile(char* sha1, char* md5, long fsize,
178  const char* type)
179 {
180  char filename[myBUFSIZ];
181  char* goldFilePath;
182 
183  snprintf(filename, myBUFSIZ, "%s.%s.%ld", sha1, md5, fsize);
184  goldFilePath = fo_RepMkPath(type, filename);
185  unlink(goldFilePath);
186  free(goldFilePath);
187 }
PGconn * pgConn
Database connection.
Definition: adj2nest.c:86
int s
The socket that the CLI will use to communicate.
Definition: fo_cli.c:37
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
char * fo_RepMkPath(const char *Type, char *Filename)
Given a filename, construct the full path to the file.
Definition: libfossrepo.c:352
void fo_scheduler_disconnect(int retcode)
Disconnect the scheduler connection.
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...
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16
FUNCTION char * strtoupper(char *s)
Helper function to upper case a string.
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:21
FUNCTION void checkPFileExists(char *sha1, char *md5, long fsize, const char *type)
Check if given checksums exists in DB, if not call deleteRepoFile()
Definition: utils.c:130
FUNCTION void deleteRepoFile(char *sha1, char *md5, long fsize, const char *type)
Take a file checksum, generate repo path and call unlink()
Definition: utils.c:177
FUNCTION void recurseDir(const char *type, char *path, int level)
Recursively read directory till level is 0.
Definition: utils.c:67
PGresult * fo_dbManager_ExecPrepared(fo_dbManager_PreparedStatement *preparedStatement,...)
Execute a prepared statement.
Definition: standalone.c:36
void fo_dbManager_free(fo_dbManager *dbManager)
Un-allocate the memory from a DB manager.
Definition: standalone.c:34