48 static char String[4096];
51 memset(String,
'\0',
sizeof(String));
52 if (!S)
return(String);
53 for(i=0; (S[0]!=
'\0') && (i <
sizeof(String)-1); S++)
55 if (S[0]==
'\n') { String[i++]=
'\\'; String[i++]=
'n'; }
56 else if (S[0]==
'\r') { String[i++]=
'\\'; String[i++]=
'r'; }
57 else if (S[0]==
'\a') { String[i++]=
'\\'; String[i++]=
'a'; }
58 else if (S[0]==
'\'') { String[i++]=
'\\'; String[i++]=
'\''; }
59 else if (S[0]==
'\"') { String[i++]=
'\\'; String[i++]=
'"'; }
60 else if (S[0]==
'\\') { String[i++]=
'\\'; String[i++]=
'\\'; }
61 else String[i++]=S[0];
72 memset(
SQL, 0, MAXCMD);
73 snprintf(
SQL, MAXCMD-1,
"SELECT mimetype_pk,mimetype_name FROM mimetype ORDER BY mimetype_pk ASC;");
97 if (!Mimetype || (Mimetype[0]==
'\0'))
return(-1);
101 if (!strcmp(Mimetype,PQgetvalue(
DBMime,i,1)))
103 return(atoi(PQgetvalue(
DBMime,i,0)));
108 memset(
SQL,
'\0',
sizeof(
SQL));
109 snprintf(
SQL,
sizeof(
SQL)-1,
"INSERT INTO mimetype (mimetype_name) VALUES ('%s');",
TaintString(Mimetype));
114 if ((result==0) || ((PQresultStatus(result) != PGRES_COMMAND_OK) &&
115 (strncmp(
"23505", PQresultErrorField(result, PG_DIAG_SQLSTATE),5))))
145 if (!Ext || (Ext[0] ==
'\0'))
return(-1);
146 ExtLen = strlen(Ext);
148 LOG_VERBOSE0(
"Looking for mimetype based on extension: '%s'",Ext);
152 if (Line[0] ==
'#')
continue;
154 for(i=0; (Line[i] !=
'\0') && !isspace(Line[i]); i++);
155 if (Line[i] ==
'\0')
continue;
161 printf(
"CheckMimeTypes(%s) in '%s' from '%s\n",Ext,Line+i,Line);
163 for( ; Line[i] !=
'\0'; i++)
167 if ((Line[i-1] !=
'\0') && !isspace(Line[i-1]))
172 if ((Line[i] == Ext[0]) && !strncasecmp(Line+i,Ext,ExtLen) &&
173 ( (Line[i+ExtLen] ==
'\0') || isspace(Line[i+ExtLen]) )
177 LOG_VERBOSE0(
"Found mimetype by extension: '%s' = '%s'",Ext,Line);
184 if (!strcasecmp(Ext,
"spec"))
return(
DBFindMime(
"application/x-rpm-spec"));
207 memset(
SQL,
'\0',
sizeof(
SQL));
208 snprintf(
SQL,
sizeof(
SQL)-1,
"SELECT distinct(ufile_name) FROM uploadtree WHERE pfile_fk = %d",Akey);
216 Maxu = PQntuples(result);
217 for(u=0; u<Maxu; u++)
219 Ext = strrchr(PQgetvalue(result,u,0),
'.');
224 if (rc >= 0)
return(rc);
232 Ext = strrchr(
A,
'.');
237 if (rc >= 0)
return(rc);
268 Fin = fopen(Filename,
"rb");
269 if (!Fin)
return(-1);
273 while(!feof(Fin) && isprint(C) && (i < 100))
280 if (i==0)
return(
DBFindMime(
"application/x-empty"));
281 if ((C >= 0) && !isprint(C))
return(
DBFindMime(
"application/octet-stream"));
294 char MimeType[MAXCMD];
302 memset(
SQL,
'\0',
sizeof(
SQL));
303 snprintf(
SQL,
sizeof(
SQL)-1,
"SELECT pfile_mimetypefk FROM pfile WHERE pfile_pk = %d AND pfile_mimetypefk is not null;",Akey);
311 if (PQntuples(result) > 0)
321 MagicType = (
char *)magic_file(
MagicCookie,Filename);
322 memset(MimeType,
'\0',MAXCMD);
325 LOG_VERBOSE0(
"Found mimetype by magic: '%s'",MagicType);
328 (i<MAXCMD) && (MagicType[i] !=
'\0') &&
329 !isspace(MagicType[i]) && !strchr(
",;",MagicType[i]);
332 MimeType[i] = MagicType[i];
334 if (!strchr(MimeType,
'/')) { memset(MimeType,
'\0',MAXCMD); }
339 if (!strcmp(MimeType,
"text/plain") || !strcmp(MimeType,
"application/octet-stream") || (MimeType[0]==
'\0'))
344 if (MimeTypeID < 0) MimeTypeID =
GetDefaultMime(MimeType,Filename);
356 MimeTypeID =
DBFindMime(
"application/octet-stream");
362 result = PQexec(
pgConn,
"BEGIN;");
369 memset(
SQL,
'\0',
sizeof(
SQL));
370 snprintf(
SQL,
sizeof(
SQL)-1,
"SELECT * FROM pfile WHERE pfile_pk = %d FOR UPDATE;",Akey);
379 memset(
SQL,
'\0',
sizeof(
SQL));
380 snprintf(
SQL,
sizeof(
SQL)-1,
"UPDATE pfile SET pfile_mimetypefk = %d WHERE pfile_pk = %d;",MimeTypeID,Akey);
390 result = PQexec(
pgConn,
"COMMIT;");
404 if (MimeTypeID == atoi(PQgetvalue(
DBMime,i,0)))
406 printf(
"%s : mimetype_pk=%d : ",PQgetvalue(
DBMime,i,1),MimeTypeID);
409 printf(
"%s\n",Filename);
426 char *Value,
int ValueMax)
431 memset(Field,0,FieldMax);
432 memset(Value,0,ValueMax);
434 while(isspace(Sin[0])) Sin++;
435 if (Sin[0]==
'\0')
return(NULL);
438 for(s=0; (Sin[
s] !=
'\0') && !isspace(Sin[s]) && (Sin[
s] !=
'='); s++)
442 while(isspace(Sin[s])) s++;
447 if (Sin[s]==
'\0')
return(NULL);
449 while(isspace(Sin[s])) s++;
450 if (Sin[s]==
'\0')
return(NULL);
453 if ((Sin[s]==
'\'') || (Sin[
s]==
'"'))
457 if (Sin[s]==
'\0')
return(NULL);
461 for( ; (Sin[
s] !=
'\0') && (Sin[s] != GotQuote); s++)
463 if (Sin[s]==
'\\') Value[v++]=Sin[++
s];
464 else Value[v++]=Sin[
s];
470 for( ; (Sin[
s] !=
'\0') && !isspace(Sin[s]); s++)
472 if (Sin[s]==
'\\') Value[v++]=Sin[++
s];
473 else Value[v++]=Sin[
s];
476 while(isspace(Sin[s])) s++;
494 memset(Line,
'\0',MaxLine);
495 if (feof(Fin))
return(-1);
499 while(!feof(Fin) && (C>=0) && (i<MaxLine))
503 if (i > 0)
return(i);
523 printf(
"Usage: %s [options] [file [file [...]]\n",Name);
524 printf(
" -h :: help (print this message), then exit.\n");
525 printf(
" -i :: initialize the database, then exit.\n");
526 printf(
" -v :: verbose (-vv = more verbose)\n");
527 printf(
" -c :: Specify the directory for the system configuration.\n");
528 printf(
" -C :: run from command line.\n");
529 printf(
" -V :: print the version info, then exit.\n");
530 printf(
" file :: if files are listed, display their mimetype.\n");
531 printf(
" no file :: process data from the scheduler.\n");
int fo_checkPQresult(PGconn *pgConn, PGresult *result, char *sql, char *FileID, int LineNumb)
Check the result status of a postgres SELECT.
PGresult * DBMime
contents of mimetype table
int DBCheckFileExtention()
Given a pfile, identify any filenames and see if any of them have a known extension based on /etc/mim...
void DBLoadMime()
Populate the DBMime table.
int s
The socket that the CLI will use to communicate.
int DBFindMime(char *Mimetype)
Find a mime type in the DBMime table.
int GetDefaultMime(char *MimeType, char *Filename)
Get the ID for the default mimetype.
char * GetFieldValue(char *Sin, char *Field, int FieldMax, char *Value, int ValueMax)
Given a string that contains field='value' pairs, save the items.
char A[MAXCMD]
input for this system
int fo_checkPQcommand(PGconn *pgConn, PGresult *result, char *sql, char *FileID, int LineNumb)
Check the result status of a postgres commands (not select) If an error occured, write the error to s...
void DBCheckMime(char *Filename)
Given a file, check if it has a mime type in the DB.
int Agent_pk
agent identifier
int MaxDBMime
how many rows in DBMime
FILE * FMimetype
for /etc/mime.types
magic_t MagicCookie
for Magic
char * TaintString(char *S)
Create a string with taint quoting.
int ReadLine(FILE *Fin, char *Line, int MaxLine)
Read a line each time from one file.
PGconn * pgConn
Database connection.
int CheckMimeTypes(char *Ext)
Given an extension, see if extension exists in the /etc/mime.types.
void Usage(char *Name)
Here are some suggested options.