Bug fix w/ libmagic & vfile

This commit is contained in:
simon987 2020-04-20 10:15:06 -04:00
parent a9cd6a45ac
commit 2bc91f01b4
4 changed files with 17 additions and 12 deletions

View File

@ -24,6 +24,7 @@ parse_job_t *create_fs_parse_job(const char *filepath, const struct stat *info,
job->vfile.filepath = job->filepath;
job->vfile.read = fs_read;
job->vfile.reset = fs_reset;
job->vfile.close = fs_close;
job->vfile.fd = -1;
job->vfile.is_fs_file = TRUE;

View File

@ -7,7 +7,6 @@
#include <magic.h>
__thread magic_t Magic = NULL;
#define MIN_VIDEO_SIZE 1024 * 64
#define MIN_IMAGE_SIZE 1024 * 2
@ -33,6 +32,12 @@ void fs_close(struct vfile *f) {
}
}
void fs_reset(struct vfile *f) {
if (f->fd != -1) {
lseek(f->fd, 0, SEEK_SET);
}
}
void parse(void *arg) {
parse_job_t *job = arg;
@ -44,10 +49,6 @@ void parse(void *arg) {
return;
}
if (Magic == NULL) {
Magic = magic_open(MAGIC_MIME_TYPE);
magic_load(Magic, NULL);
}
doc.filepath = job->filepath;
doc.ext = (short) job->ext;
@ -78,6 +79,9 @@ void parse(void *arg) {
if (doc.mime == 0 && !ScanCtx.fast) {
// Get mime type with libmagic
magic_t magic = magic_open(MAGIC_MIME_TYPE);
magic_load(magic, NULL);
bytes_read = job->vfile.read(&job->vfile, buf, PARSE_BUF_SIZE);
if (bytes_read < 0) {
@ -91,7 +95,7 @@ void parse(void *arg) {
return;
}
const char *magic_mime_str = magic_buffer(Magic, buf, bytes_read);
const char *magic_mime_str = magic_buffer(magic, buf, bytes_read);
if (magic_mime_str != NULL) {
doc.mime = mime_get_mime_by_string(ScanCtx.mime_table, magic_mime_str);
@ -102,8 +106,9 @@ void parse(void *arg) {
}
}
magic_close(Magic);
Magic = NULL;
job->vfile.reset(&job->vfile);
magic_close(magic);
}
int mmime = MAJOR_MIME(doc.mime);
@ -154,7 +159,5 @@ void parse(void *arg) {
}
void cleanup_parse() {
if (Magic != NULL) {
magic_close(Magic);
}
// noop
}

View File

@ -7,6 +7,7 @@
int fs_read(struct vfile *f, void *buf, size_t size);
void fs_close(struct vfile *f);
void fs_reset(struct vfile *f);
void parse(void *arg);

2
third-party/libscan vendored

@ -1 +1 @@
Subproject commit e1ad26d0c6a3bd46bec40f4a693a76c0013b2c0a
Subproject commit faa2d8a68c3377d12bbaf1560cba9cb909007d6f