/
mime.c
113 lines (89 loc) · 2.86 KB
/
mime.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
Weborf
Copyright (C) 2010 Salvo "LtWorf" Tomaselli
Weborf is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
@author Salvo "LtWorf" Tomaselli <tiposchi@tiscali.it>
*/
#include "options.h"
#include <string.h>
#include <unistd.h>
#include "mime.h"
#ifdef SEND_MIMETYPES
#include <magic.h>
#endif
/**
Returns a token for the libmagic.
buffer must NOT be shared amongst threads.
The size of buffer is assumed to be at least MIMETYPELEN
*/
int mime_init(magic_t *token) {
#ifdef SEND_MIMETYPES
*token=magic_open(MAGIC_SYMLINK | MAGIC_MIME_TYPE);
if (*token==NULL) return 1;
return magic_load(*token,NULL); //Load the database
#else
*token=NULL;
return 0;
#endif
}
/**
Releases the token for libmagic
If the token is null, it will do nothing.
*/
void mime_release(magic_t token) {
if (token==NULL) return;
#ifdef SEND_MIMETYPES
magic_close(token);
#endif
}
/**
returns mimetype of an opened file descriptor
the cursor can be located at any position
fd is the descriptor to an open file
sb is the stat to the same file
*/
const char* mime_get_fd (magic_t token,int fd,struct stat *sb) {
#ifdef SEND_MIMETYPES
if (token==NULL) return NULL;
/*If fd is a directory, send the mimetype without attempting to read it*/
if (sb->st_mode & S_IFREG) {
/*
* Seek file to 0 and read its header to know its mime type
* then seek again to the previous position
*/
char buf[64];
//Get the current cursor position
off_t prev_pos=lseek(fd,0,SEEK_CUR);
//Set the cursor to the beginning of the file
lseek(fd,0,SEEK_SET);
//Reads 64 bytes to determine the type
int r=read(fd,&buf,64);
//Reset the position
lseek(fd,prev_pos,SEEK_SET);
const char* mime=magic_buffer(token,&buf,r);
return mime;
} else if (sb->st_mode & S_IFDIR)
return "application/x-directory";
else if (sb->st_mode & S_IFSOCK)
return "application/x-socket";
else if (sb->st_mode & S_IFLNK)
return "application/x-symlink";
else if (sb->st_mode & S_IFBLK)
return "application/x-block-device";
else if (sb->st_mode & S_IFCHR)
return "application/x-character-device";
//else if (sb->st_mode & S_IFIFO)
return "application/x-fifo";
#else
return NULL;
#endif
}