FOSSology  4.4.0
Open Source License Compliance by Open Source Software
process.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 
24 FUNCTION int ProcessFile(char *FilePath, pFileResult_t FileResult)
25 {
26  int rv; // generic renurn value
27  FILE *fin;
28 
29  LOG_VERBOSE("ProcessFile: %s",FilePath);
30 
31  /* Open the file */
32  fin = fopen(FilePath, "r");
33  if (!fin)
34  {
35  LOG_ERROR("FATAL: %s.%s.%d Failure to open file %s.\nError: %s\n",
36  __FILE__, "ProcessFile()", __LINE__, FilePath, strerror(errno));
37  return -1;
38  }
39 
40  /* Read the first buffer's worth from the file, ignoring errors to simplify the demo */
41  rv = fread(FileResult->Buf, sizeof(char), sizeof(FileResult->Buf), fin);
42 
43  /* Convert to a hex string and save in the FileResult */
44  Char2Hex(FileResult->Buf, DataSize, FileResult->HexStr);
45 
46  /* Close the file */
47  fclose(fin);
48 
49  /* Return Success */
50  return 0;
51 }
52 
53 
61 FUNCTION int ProcessUpload(int upload_pk, int agent_fk)
62 {
63  PGresult* result; // the result of the database access
64  int i;
65  int rv; // generic return value
66  int numrows; // generic return value
67  int pfile_pk;
68  char *FilePath; // complete path to file in the repository
70  char LastChar;
71  char sqlbuf[1024];
72  char FileName[128];
73  char DataBuf[128];
74  char *RepoArea = "files";
75 // char *ufile_mode = "30000000"; // This mode is for artifacts and containers (which will be excluded)
76  char *ufile_mode = "10000000"; // This mode is for artifacts only (which will be excluded)
77  FileResult_t FileResult;
78 
79  /* Select each upload filename (repository filename) that hasn't been processed by this agent yet */
80  char* SelectFilename_sql = "\
81  SELECT pfile_pk, pfile_sha1 || '.' || pfile_md5 || '.' || pfile_size AS pfilename \
82  FROM ( SELECT distinct(pfile_fk) AS PF FROM uploadtree \
83  WHERE upload_fk = %d and (ufile_mode&x'%s'::int)=0 \
84  ) AS SS \
85  left outer join demomod on (PF = pfile_fk ) \
86  inner join pfile on (PF = pfile_pk) \
87  WHERE demomod_pk IS null or agent_fk <> %d";
88  char* SelectFilename2_sql = "\
89  SELECT pfile_pk, pfile_sha1 || '.' || pfile_md5 || '.' || pfile_size AS pfilename \
90  FROM ( SELECT distinct(pfile_fk) AS PF FROM %s \
91  WHERE (ufile_mode&x'%s'::int)=0 \
92  ) AS SS \
93  left outer join demomod on (PF = pfile_fk ) \
94  inner join pfile on (PF = pfile_pk) \
95  WHERE demomod_pk IS null or agent_fk <> %d";
96 
97  /* Find the correct uploadtree table name */
100  {
101  LOG_FATAL("demomod passed invalid upload, upload_pk = %d", upload_pk);
102  return(-110);
103  }
104 
105  /* If the last character of the uploadtree_tablename is a digit, then we don't need upload_fk
106  * in the query (because the table only has that uplaod).
107  */
108  LastChar = uploadtree_tablename[strlen(uploadtree_tablename)-1];
109  if (LastChar >= '0' && LastChar <= '9')
110  {
111  snprintf(sqlbuf, sizeof(sqlbuf), SelectFilename2_sql, uploadtree_tablename, ufile_mode, agent_fk);
112  }
113  else
114  {
115  snprintf(sqlbuf, sizeof(sqlbuf), SelectFilename_sql, upload_pk, ufile_mode, agent_fk);
116  }
117  free(uploadtree_tablename);
118 
119  /* retrieve the records to process */
120  result = PQexec(pgConn, sqlbuf);
121  if (fo_checkPQresult(pgConn, result, sqlbuf, __FILE__, __LINE__)) ExitNow(-100);
122  numrows = PQntuples(result);
123 
124  /* process all files in this upload */
125  for (i=0; i<numrows; i++)
126  {
127  strcpy(FileName, PQgetvalue(result, i, 1));
128  pfile_pk = atoi(PQgetvalue(result, i, 0));
129  FilePath = fo_RepMkPath(RepoArea, FileName);
130  if (!FilePath)
131  {
132  LOG_FATAL("demomod was unable to derive a file path for pfile %d. Check your HOSTS configuration.", pfile_pk);
133  return(-111);
134  }
135 
136  rv = ProcessFile(FilePath, &FileResult);
137  if (rv == 0)
138  {
139  fo_scheduler_heart(1); // Tell the scheduler that we are alive and update item count
140 
141  /* Update the database (through fo_sqlCopyAdd buffered copy, this is much faster than single inserts) */
142  snprintf(DataBuf, sizeof(DataBuf), "%d\t%d\t%s\n", pfile_pk, agent_fk, FileResult.HexStr);
143  fo_sqlCopyAdd(psqlcpy, DataBuf);
144  }
145  }
146  PQclear(result);
147  return(0); // success
148 }
char * uploadtree_tablename
upload.uploadtree_tablename
Definition: adj2nest.c:100
FUNCTION int ProcessUpload(int upload_pk, int agent_fk)
Process a single upload - read the first 32 bytes in each file.
Definition: process.c:61
PGconn * pgConn
Database connection.
Definition: adj2nest.c:86
FUNCTION int ProcessFile(char *FilePath, pFileResult_t FileResult)
Process a single file - read the first 32 bytes.
Definition: process.c:24
psqlCopy_t psqlcpy
fo_sqlCopy struct used for fast data insertion
Definition: demomod.c:47
FUNCTION void Char2Hex(char *InBuf, int NumBytes, char *OutBuf)
Convert a character buffer to a hex string.
Definition: utils.c:91
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
FUNCTION char * GetUploadtreeTableName(PGconn *pgConn, int upload_pk)
Get the uploadtree table name for this upload_pk If upload_pk does not exist, return "uploadtree".
Definition: libfossagent.c:414
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_heart(int i)
This function must be called by agents to let the scheduler know they are alive and how many items th...
int fo_sqlCopyAdd(psqlCopy_t pCopy, char *DataRow)
Add a data row to an sqlCopy Use '\N' to pass in a null.
Definition: sqlCopy.c:146
const char * upload_pk
Definition: sqlstatements.h:82
char HexStr[(DataSize *2)+1]
Hexadecimal string.
Definition: demomod.h:30
char Buf[DataSize]
Buffer.
Definition: demomod.h:29