-
Notifications
You must be signed in to change notification settings - Fork 442
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add in-memory I/O using hFILE fixed buffers #590
Changes from 4 commits
80bb84f
58fb979
29a6789
5cd9e07
3972015
3d11377
3656732
fde4dcf
5fa67e9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -404,7 +404,7 @@ off_t hseek(hFILE *fp, off_t offset, int whence) | |
{ | ||
off_t curpos, pos; | ||
|
||
if (writebuffer_is_nonempty(fp)) { | ||
if (writebuffer_is_nonempty(fp) && fp->mobile) { | ||
int ret = flush_buffer(fp); | ||
if (ret < 0) return ret; | ||
} | ||
|
@@ -615,6 +615,39 @@ static hFILE *hopen_fd(const char *filename, const char *mode) | |
return NULL; | ||
} | ||
|
||
static hFILE *hpreload_fd(const char *filename, const char *mode) | ||
{ | ||
if(!strchr(mode, 'r')) | ||
{ | ||
return NULL; | ||
} | ||
|
||
hFILE_fd *fp = NULL; | ||
FILE *file = fopen(filename, mode); | ||
if (!file) goto error; | ||
|
||
if(fseek(file, 0, SEEK_END) != 0) goto error; | ||
int len = ftell(file); | ||
fseek(file, 0, SEEK_SET); | ||
|
||
char* buffer = malloc(len); | ||
if(buffer == NULL) goto error; | ||
if(fread(buffer, 1, len, file) != len) goto error; | ||
|
||
fp = (hFILE_fd *) hfile_init_fixed(sizeof (hFILE_fd), mode, buffer, len, len); | ||
if (fp == NULL) goto error; | ||
|
||
fp->fd = fileno(file); | ||
fp->is_socket = 0; | ||
fp->base.backend = &fd_backend; | ||
return &fp->base; | ||
|
||
error: | ||
if (file) { int save = errno; (void) fclose(file); errno = save; } | ||
hfile_destroy((hFILE *) fp); | ||
return NULL; | ||
} | ||
|
||
hFILE *hdopen(int fd, const char *mode) | ||
{ | ||
hFILE_fd *fp = (hFILE_fd*) hfile_init(sizeof (hFILE_fd), mode, blksize(fd)); | ||
|
@@ -711,6 +744,15 @@ static int cmp_prefix(const char *key, const char *s) | |
return 0; | ||
} | ||
|
||
static hFILE *create_hfile_mem(char* buffer, const char* mode, size_t buf_filled, size_t buf_size) | ||
{ | ||
hFILE_mem *fp = (hFILE_mem *) hfile_init_fixed(sizeof(hFILE_mem), mode, buffer, buf_filled, buf_size); | ||
if (fp == NULL) { free(buffer); return NULL; } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should let the caller function free |
||
|
||
fp->base.backend = &mem_backend; | ||
return &fp->base; | ||
} | ||
|
||
static hFILE *hopen_mem(const char *url, const char *mode) | ||
{ | ||
size_t length, size; | ||
|
@@ -735,6 +777,8 @@ static hFILE *hopen_mem(const char *url, const char *mode) | |
hts_decode_percent(buffer, &length, data); | ||
} | ||
|
||
return create_hfile_mem(buffer, mode, length, size); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You still need to free |
||
|
||
hFILE_mem *fp = (hFILE_mem *) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This code is unreachable. |
||
hfile_init_fixed(sizeof (hFILE_mem), mode, buffer, length, size); | ||
if (fp == NULL) { free(buffer); return NULL; } | ||
|
@@ -743,6 +787,37 @@ static hFILE *hopen_mem(const char *url, const char *mode) | |
return &fp->base; | ||
} | ||
|
||
hFILE *hopenv_mem(const char *filename, const char *mode, va_list args) | ||
{ | ||
char* buffer = va_arg(args, char*); | ||
size_t sz = va_arg(args, size_t); | ||
va_end(args); | ||
|
||
return create_hfile_mem(buffer, mode, sz, sz); | ||
} | ||
|
||
int hfile_mem_get_buffer(hFILE *file, char **buffer, size_t *length){ | ||
if(file->backend != &mem_backend) { | ||
errno = EINVAL; | ||
return -1; | ||
} | ||
|
||
*buffer = file->buffer; | ||
*length = file->buffer - file->limit; | ||
|
||
return 0; | ||
} | ||
|
||
int hfile_plugin_init_mem(struct hFILE_plugin *self) | ||
{ | ||
// mem files are declared remote so they work with a tabix index | ||
static const struct hFILE_scheme_handler handler = | ||
{NULL, hfile_always_remote, "mem", 2000 + 50, hopenv_mem}; | ||
self->name = "mem"; | ||
hfile_add_scheme_handler("mem", &handler); | ||
return 0; | ||
} | ||
|
||
|
||
/***************************************** | ||
* Plugin and hopen() backend dispatcher * | ||
|
@@ -833,6 +908,7 @@ static void load_hfile_plugins() | |
hfile_add_scheme_handler("data", &data); | ||
hfile_add_scheme_handler("file", &file); | ||
init_add_plugin(NULL, hfile_plugin_init_net, "knetfile"); | ||
init_add_plugin(NULL, hfile_plugin_init_mem, "mem"); | ||
|
||
#ifdef ENABLE_PLUGINS | ||
struct hts_path_itr path; | ||
|
@@ -879,11 +955,25 @@ static hFILE *hopen_unknown_scheme(const char *fname, const char *mode) | |
return fp; | ||
} | ||
|
||
static hFILE *hopenv_unknown_scheme(const char *fname, const char *mode, va_list args) | ||
{ | ||
char* method_type = va_arg(args, char*); | ||
va_end(args); | ||
if(!strcmp(method_type, "preload")){ | ||
errno = EPROTONOSUPPORT; | ||
return NULL; | ||
} | ||
|
||
hFILE *fp = hpreload_fd(fname, mode); | ||
if (fp == NULL && errno == ENOENT) errno = EPROTONOSUPPORT; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm unsure why this line exists. hpreload_fd can return NULL/ENOENT for valid reasons, and ENOENT is suitable then I think. |
||
return fp; | ||
} | ||
|
||
/* Returns the appropriate handler, or NULL if the string isn't an URL. */ | ||
static const struct hFILE_scheme_handler *find_scheme_handler(const char *s) | ||
{ | ||
static const struct hFILE_scheme_handler unknown_scheme = | ||
{ hopen_unknown_scheme, hfile_always_local, "built-in", 0 }; | ||
{ hopen_unknown_scheme, hfile_always_local, "built-in", 2000 + 50, hopenv_unknown_scheme }; | ||
|
||
char scheme[12]; | ||
int i; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should check
mode
value. IfNULL
,strchr
will seg fault.