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