37 static char String[4096];
40 memset(String,
'\0',
sizeof(String));
41 if (!S)
return(String);
42 for(i=0; (S[0]!=
'\0') && (i <
sizeof(String)-1); S++)
44 if (S[0]==
'\n') { String[i++]=
'\\'; String[i++]=
'n'; }
45 else if (S[0]==
'\r') { String[i++]=
'\\'; String[i++]=
'r'; }
46 else if (S[0]==
'\a') { String[i++]=
'\\'; String[i++]=
'a'; }
47 else if (S[0]==
'\'') { String[i++]=
'\\'; String[i++]=
'\''; }
48 else if (S[0]==
'\"') { String[i++]=
'\\'; String[i++]=
'"'; }
49 else if (S[0]==
'\\') { String[i++]=
'\\'; String[i++]=
'\\'; }
50 else String[i++]=S[0];
61 memset(
SQL, 0, MAXCMD);
62 snprintf(
SQL, MAXCMD-1,
"SELECT mimetype_pk,mimetype_name FROM mimetype ORDER BY mimetype_pk ASC;");
86 if (!Mimetype || (Mimetype[0]==
'\0'))
return(-1);
90 if (!strcmp(Mimetype,PQgetvalue(
DBMime,i,1)))
92 return(atoi(PQgetvalue(
DBMime,i,0)));
97 memset(
SQL,
'\0',
sizeof(
SQL));
98 snprintf(
SQL,
sizeof(
SQL)-1,
"INSERT INTO mimetype (mimetype_name) VALUES ('%s');",
TaintString(Mimetype));
103 if ((result==0) || ((PQresultStatus(result) != PGRES_COMMAND_OK) &&
104 (strncmp(
"23505", PQresultErrorField(result, PG_DIAG_SQLSTATE),5))))
134 if (!Ext || (Ext[0] ==
'\0'))
return(-1);
135 ExtLen = strlen(Ext);
137 LOG_VERBOSE0(
"Looking for mimetype based on extension: '%s'",Ext);
141 if (Line[0] ==
'#')
continue;
143 for(i=0; (Line[i] !=
'\0') && !isspace(Line[i]); i++);
144 if (Line[i] ==
'\0')
continue;
150 printf(
"CheckMimeTypes(%s) in '%s' from '%s\n",Ext,Line+i,Line);
152 for( ; Line[i] !=
'\0'; i++)
156 if ((Line[i-1] !=
'\0') && !isspace(Line[i-1]))
161 if ((Line[i] == Ext[0]) && !strncasecmp(Line+i,Ext,ExtLen) &&
162 ( (Line[i+ExtLen] ==
'\0') || isspace(Line[i+ExtLen]) )
166 LOG_VERBOSE0(
"Found mimetype by extension: '%s' = '%s'",Ext,Line);
173 if (!strcasecmp(Ext,
"spec"))
return(
DBFindMime(
"application/x-rpm-spec"));
196 memset(
SQL,
'\0',
sizeof(
SQL));
197 snprintf(
SQL,
sizeof(
SQL)-1,
"SELECT distinct(ufile_name) FROM uploadtree WHERE pfile_fk = %d",Akey);
205 Maxu = PQntuples(result);
206 for(u=0; u<Maxu; u++)
208 Ext = strrchr(PQgetvalue(result,u,0),
'.');
213 if (rc >= 0)
return(rc);
221 Ext = strrchr(
A,
'.');
226 if (rc >= 0)
return(rc);
258 if (!Fin)
return(-1);
262 while(!feof(Fin) && isprint(C) && (i < 100))
269 if (i==0)
return(
DBFindMime(
"application/x-empty"));
270 if ((C >= 0) && !isprint(C))
return(
DBFindMime(
"application/octet-stream"));
283 char MimeType[MAXCMD];
291 memset(
SQL,
'\0',
sizeof(
SQL));
292 snprintf(
SQL,
sizeof(
SQL)-1,
"SELECT pfile_mimetypefk FROM pfile WHERE pfile_pk = %d AND pfile_mimetypefk is not null;",Akey);
300 if (PQntuples(result) > 0)
311 memset(MimeType,
'\0',MAXCMD);
314 LOG_VERBOSE0(
"Found mimetype by magic: '%s'",MagicType);
317 (i<MAXCMD) && (MagicType[i] !=
'\0') &&
318 !isspace(MagicType[i]) && !strchr(
",;",MagicType[i]);
321 MimeType[i] = MagicType[i];
323 if (!strchr(MimeType,
'/')) { memset(MimeType,
'\0',MAXCMD); }
328 if (!strcmp(MimeType,
"text/plain") || !strcmp(MimeType,
"application/octet-stream") || (MimeType[0]==
'\0'))
345 MimeTypeID =
DBFindMime(
"application/octet-stream");
351 result = PQexec(
pgConn,
"BEGIN;");
358 memset(
SQL,
'\0',
sizeof(
SQL));
359 snprintf(
SQL,
sizeof(
SQL)-1,
"SELECT * FROM pfile WHERE pfile_pk = %d FOR UPDATE;",Akey);
368 memset(
SQL,
'\0',
sizeof(
SQL));
369 snprintf(
SQL,
sizeof(
SQL)-1,
"UPDATE pfile SET pfile_mimetypefk = %d WHERE pfile_pk = %d;",MimeTypeID,Akey);
379 result = PQexec(
pgConn,
"COMMIT;");
393 if (MimeTypeID == atoi(PQgetvalue(
DBMime,i,0)))
395 printf(
"%s : mimetype_pk=%d : ",PQgetvalue(
DBMime,i,1),MimeTypeID);
415 char *Value,
int ValueMax)
420 memset(Field,0,FieldMax);
421 memset(Value,0,ValueMax);
423 while(isspace(Sin[0])) Sin++;
424 if (Sin[0]==
'\0')
return(NULL);
427 for(
s=0; (Sin[
s] !=
'\0') && !isspace(Sin[
s]) && (Sin[
s] !=
'=');
s++)
431 while(isspace(Sin[
s]))
s++;
436 if (Sin[
s]==
'\0')
return(NULL);
438 while(isspace(Sin[
s]))
s++;
439 if (Sin[
s]==
'\0')
return(NULL);
442 if ((Sin[
s]==
'\'') || (Sin[
s]==
'"'))
446 if (Sin[
s]==
'\0')
return(NULL);
450 for( ; (Sin[
s] !=
'\0') && (Sin[
s] != GotQuote);
s++)
452 if (Sin[
s]==
'\\') Value[v++]=Sin[++
s];
453 else Value[v++]=Sin[
s];
459 for( ; (Sin[
s] !=
'\0') && !isspace(Sin[
s]);
s++)
461 if (Sin[
s]==
'\\') Value[v++]=Sin[++
s];
462 else Value[v++]=Sin[
s];
465 while(isspace(Sin[
s]))
s++;
483 memset(Line,
'\0',MaxLine);
484 if (feof(Fin))
return(-1);
488 while(!feof(Fin) && (C>=0) && (i<MaxLine))
492 if (i > 0)
return(i);
512 printf(
"Usage: %s [options] [file [file [...]]\n",Name);
513 printf(
" -h :: help (print this message), then exit.\n");
514 printf(
" -i :: initialize the database, then exit.\n");
515 printf(
" -v :: verbose (-vv = more verbose)\n");
516 printf(
" -c :: Specify the directory for the system configuration.\n");
517 printf(
" -C :: run from command line.\n");
518 printf(
" -V :: print the version info, then exit.\n");
519 printf(
" file :: if files are listed, display their mimetype.\n");
520 printf(
" no file :: process data from the scheduler.\n");
void DBLoadMime()
Populate the DBMime table.
FILE * FMimetype
for /etc/mime.types
char * GetFieldValue(char *Sin, char *Field, int FieldMax, char *Value, int ValueMax)
Given a string that contains field='value' pairs, save the items.
void Usage(char *Name)
Here are some suggested options.
int CheckMimeTypes(char *Ext)
Given an extension, see if extension exists in the /etc/mime.types.
char * TaintString(char *S)
Create a string with taint quoting.
int GetDefaultMime(char *MimeType, char *Filename)
Get the ID for the default mimetype.
int DBFindMime(char *Mimetype)
Find a mime type in the DBMime table.
char A[MAXCMD]
input for this system
PGconn * pgConn
Database connection.
magic_t MagicCookie
for Magic
int MaxDBMime
how many rows in DBMime
void DBCheckMime(char *Filename)
Given a file, check if it has a mime type in the DB.
int DBCheckFileExtention()
Given a pfile, identify any filenames and see if any of them have a known extension based on /etc/mim...
int Agent_pk
agent identifier
PGresult * DBMime
contents of mimetype table
int ReadLine(FILE *Fin, char *Line, int MaxLine)
Read a line each time from one file.
int s
The socket that the CLI will use to communicate.
int fo_checkPQresult(PGconn *pgConn, PGresult *result, char *sql, char *FileID, int LineNumb)
Check the result status of a postgres SELECT.
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...