FOSSology  4.4.0
Open Source License Compliance by Open Source Software
mimetype.c
Go to the documentation of this file.
1 /*
2  SPDX-FileCopyrightText: © 2007-2013 Hewlett-Packard Development Company, L.P.
3 
4  SPDX-License-Identifier: GPL-2.0-only
5 */
53 #include "finder.h"
54 
55 #ifdef COMMIT_HASH_S
56 char BuildVersion[]="mimetype build version: " VERSION_S " r(" COMMIT_HASH_S ").\n";
57 #else
58 char BuildVersion[]="mimetype build version: NULL.\n";
59 #endif
60 
67 int main(int argc, char *argv[])
68 {
69  int arg;
70  char *Parm = NULL;
71  char *Path = NULL;
72  int c;
73  char *agent_desc = "Determines mimetype for each file";
74  int pfile_count = 0;
75  int Agent_pk;
76  int ars_pk = 0;
77 
78  int upload_pk = 0; // the upload primary key
79  int user_pk = 0;
80  char *AgentARSName = "mimetype_ars";
81  int rv;
82  PGresult *result;
83  char sqlbuf[1024];
84  int CmdlineFlag = 0;
85  char *COMMIT_HASH;
86  char *VERSION;
87  char agent_rev[MAXCMD];
88 
89  /* initialize the scheduler connection */
90  fo_scheduler_connect(&argc, argv, &pgConn);
91 
92  /* Process command-line */
93  while((c = getopt(argc,argv,"iCc:hvV")) != -1)
94  {
95  switch(c)
96  {
97  case 'i':
98  PQfinish(pgConn);
99  return(0);
100  case 'c':
101  /* do nothing with this option */
102  break;
103  case 'C':
104  CmdlineFlag = 1;
105  break;
106  case 'v':
107  agent_verbose++;
108  break;
109  case 'V':
110  printf("%s", BuildVersion);
111  PQfinish(pgConn);
112  return(0);
113  default:
114  Usage(argv[0]);
115  PQfinish(pgConn);
116  exit(-1);
117  }
118  }
119 
120  COMMIT_HASH = fo_sysconfig("mimetype", "COMMIT_HASH");
121  VERSION = fo_sysconfig("mimetype", "VERSION");
122  sprintf(agent_rev, "%s.%s", VERSION, COMMIT_HASH);
123  /* Get the Agent Key from the DB */
124  Agent_pk = fo_GetAgentKey(pgConn, basename(argv[0]), 0, agent_rev, agent_desc);
125 
126  FMimetype = fopen("/etc/mime.types","rb");
127  if (!FMimetype)
128  {
129  LOG_WARNING("Unable to open /etc/mime.types\n");
130  }
131 
132  MagicCookie = magic_open(MAGIC_PRESERVE_ATIME|MAGIC_MIME);
133  if (MagicCookie == NULL)
134  {
135  LOG_FATAL("Failed to initialize magic cookie\n");
136  PQfinish(pgConn);
137  exit(-1);
138  }
139  if (magic_load(MagicCookie,NULL) != 0)
140  {
141  LOG_FATAL("Failed to load magic file: UnMagic\n");
142  PQfinish(pgConn);
143  exit(-1);
144  }
145 
146  /* Run from the command-line (for testing) */
147  for(arg=optind; arg < argc; arg++)
148  {
149  Akey = -1;
150  strncpy(A,argv[arg],sizeof(A)-1);
151  A[sizeof(A)-1] = '\0';
152  DBCheckMime(A);
153  }
154 
155  /* Run from scheduler! */
156  if (0 == CmdlineFlag)
157  {
158  user_pk = fo_scheduler_userID(); /* get user_pk for user who queued the agent */
159 
160  while(fo_scheduler_next())
161  {
162  /* get piece of information, including upload_pk, others */
163  Parm = fo_scheduler_current();
164  if (Parm && Parm[0])
165  {
166  upload_pk = atoi(Parm);
167 
168  /* Check Permissions */
169  if (GetUploadPerm(pgConn, upload_pk, user_pk) < PERM_WRITE)
170  {
171  LOG_ERROR("You have no update permissions on upload %d", upload_pk);
172  continue;
173  }
174 
175  /* does ars table exist?
176  * If not, create it.
177  */
178  rv = fo_tableExists(pgConn, AgentARSName);
179  if (!rv)
180  {
181  rv = fo_CreateARSTable(pgConn, AgentARSName);
182  if (!rv) return(0);
183  }
184 
185  /* check ars table if this is duplicate request*/
186  memset(sqlbuf, 0, sizeof(sqlbuf));
187  snprintf(sqlbuf, sizeof(sqlbuf),
188  "select ars_pk from mimetype_ars,agent \
189  where agent_pk=agent_fk and ars_success=true \
190  and upload_fk='%d' and agent_fk='%d'",
192  result = PQexec(pgConn, sqlbuf);
193  if (fo_checkPQresult(pgConn, result, sqlbuf, __FILE__, __LINE__)) exit(-1);
194  if (PQntuples(result) > 0)
195  {
196  PQclear(result);
197  LOG_WARNING("Ignoring requested mimetype analysis of upload %d - Results are already in database.\n",upload_pk);
198  continue;
199  }
200  PQclear(result);
201 
202  /* Record analysis start in mimetype_ars, the mimetype audit trail. */
203  ars_pk = fo_WriteARS(pgConn, ars_pk, upload_pk, Agent_pk, AgentARSName, 0, 0);
204 
205  /* get all pfile ids on a upload record */
206  memset(sqlbuf, 0, sizeof(sqlbuf));
207  snprintf(sqlbuf, sizeof(sqlbuf), "SELECT DISTINCT(pfile_pk) as Akey, pfile_sha1 || '.' || pfile_md5 || '.' || pfile_size AS A FROM uploadtree, pfile WHERE uploadtree.pfile_fk = pfile.pfile_pk AND pfile_mimetypefk is NULL AND upload_fk = '%d';", upload_pk);
208  result = PQexec(pgConn, sqlbuf);
209  if (fo_checkPQresult(pgConn, result, sqlbuf, __FILE__, __LINE__)) exit(-1);
210  pfile_count = PQntuples(result);
211  int i;
212  for(i=0; i < pfile_count; i++)
213  {
214  Akey = atoi(PQgetvalue(result, i, 0));
215  strncpy(A, PQgetvalue(result, i, 1), sizeof(A)-1);
216  A[sizeof(A)-1] = '\0';
217  if (Akey <= 0 || A[0]=='\0')
218  {
219  printf("ERROR: Data is in an unknown format.\n");
220  PQfinish(pgConn);
221  exit(-1);
222  }
223 
224  /* Process the repository file */
225  /* Find the path */
226  Path = fo_RepMkPath("files",A);
227  if (Path && fo_RepExist("files",A))
228  {
229  /* Get the mimetype! */
230  DBCheckMime(Path);
231  }
232  else
233  {
234  printf("ERROR pfile %d Unable to process.\n",Akey);
235  printf("LOG pfile %d File '%s' not found.\n",Akey,A);
236  PQfinish(pgConn);
237  exit(-1);
238  }
239  /* Clean up Path memory */
240  if(Path)
241  {
242  free(Path);
243  Path = NULL;
244  }
246  }
247  PQclear(result);
248 
249  /* Record analysis success in mimetype_ars. */
250  if (ars_pk) fo_WriteARS(pgConn, ars_pk, upload_pk, Agent_pk, AgentARSName, 0, 1);
251  }
252  }
253  } /* if run from scheduler */
254 
255  /* Clean up */
256  if (FMimetype) fclose(FMimetype);
257  magic_close(MagicCookie);
258  if (DBMime) PQclear(DBMime);
259  if (pgConn) PQfinish(pgConn);
260  /* after cleaning up agent, disconnect from the scheduler, this doesn't return */
262  return(0);
263 } /* main() */
264 
PGconn * pgConn
Database connection.
Definition: adj2nest.c:86
char BuildVersion[]
Definition: buckets.c:68
FILE * FMimetype
for /etc/mime.types
Definition: finder.c:21
char A[MAXCMD]
input for this system
Definition: finder.c:26
magic_t MagicCookie
for Magic
Definition: finder.c:23
void DBCheckMime(char *Filename)
Given a file, check if it has a mime type in the DB.
Definition: finder.c:281
int Agent_pk
agent identifier
Definition: finder.c:19
PGresult * DBMime
contents of mimetype table
Definition: finder.c:16
Usage()
Print Usage statement.
Definition: fo_dbcheck.php:63
FUNCTION int GetUploadPerm(PGconn *pgConn, long UploadPk, int user_pk)
Get users permission to this upload.
Definition: libfossagent.c:378
FUNCTION int fo_CreateARSTable(PGconn *pgConn, const char *tableName)
Create ars table if it doesn't already exist.
Definition: libfossagent.c:270
FUNCTION int fo_WriteARS(PGconn *pgConn, int ars_pk, int upload_pk, int agent_pk, const char *tableName, const char *ars_status, int ars_success)
Write ars record.
Definition: libfossagent.c:214
FUNCTION int fo_GetAgentKey(PGconn *pgConn, const char *agent_name, long Upload_pk, const char *rev, const char *agent_desc)
Get the latest enabled agent key (agent_pk) from the database.
Definition: libfossagent.c:158
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
int fo_tableExists(PGconn *pgConn, const char *tableName)
Check if table exists. Note, this assumes the database name is 'fossology'.
Definition: libfossdb.c:232
#define PERM_WRITE
Read-Write permission.
Definition: libfossology.h:33
char * fo_RepMkPath(const char *Type, char *Filename)
Given a filename, construct the full path to the file.
Definition: libfossrepo.c:352
int fo_RepExist(char *Type, char *Filename)
Determine if a file exists.
Definition: libfossrepo.c:486
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...
char * fo_sysconfig(const char *sectionname, const char *variablename)
gets a system configuration variable from the configuration data.
int agent_verbose
Common verbose flags for the agents, this is used so that the scheduler can change the verbose level ...
int fo_scheduler_userID()
Gets the id of the user that created the job that the agent is running.
char * fo_scheduler_current()
Get the last read string from the scheduler.
char * fo_scheduler_next()
Get the next data to process from the scheduler.
void fo_scheduler_connect(int *argc, char **argv, PGconn **db_conn)
Establish a connection between an agent and the scheduler.
int main(int argc, char *argv[])
Get the mimetype for a package.
Definition: mimetype.c:67
const char * upload_pk
Definition: sqlstatements.h:82