-
Notifications
You must be signed in to change notification settings - Fork 464
Add in-memory I/O using hFILE fixed buffers #590
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
Changes from 1 commit
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,47 @@ 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; | ||
|
|
||
| fseek(file, 0, SEEK_END); | ||
|
Contributor
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 need to check the return value of fseek, incase someone has given a non-seekable stream to this function. (Eg /dev/stdin) |
||
| int len = ftell(file); | ||
| fseek(file, 0, SEEK_SET); | ||
|
|
||
| char* buffer = malloc(len); | ||
| if(buffer == NULL) | ||
| { | ||
| errno = ENOMEM; | ||
|
Contributor
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. Manually setting errno isn't recommended. Malloc will do this itself, as would the fread below. Indeed we're obscuring the fread return as it may have other more meaningful errno values. |
||
| goto error; | ||
| } | ||
| if(fread(buffer, 1, len, file) != len) | ||
| { | ||
| errno = EIO; | ||
| 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)); | ||
|
|
@@ -821,6 +862,41 @@ static int init_add_plugin(void *obj, int (*init)(struct hFILE_plugin *), | |
| return 0; | ||
| } | ||
|
|
||
| hFILE *hopenv_mem(const char *filename, const char *mode, va_list args) | ||
|
Contributor
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 wondering if the existing hopen_mem, which is strictly URL based rather than generic memory based, should be reimplemented in terms of the generic hopenv_mem.
Contributor
Author
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. Looks like a good idea, though in |
||
| { | ||
| char* buffer = va_arg(args, char*); | ||
| size_t sz = va_arg(args, size_t); | ||
| va_end(args); | ||
|
|
||
| hFILE_mem *fp = (hFILE_mem *) hfile_init_fixed(sizeof(hFILE_mem), mode, buffer, sz, sz); | ||
|
|
||
| fp->base.backend = &mem_backend; | ||
|
|
||
| return &fp->base; | ||
| } | ||
|
|
||
| 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; | ||
| } | ||
|
|
||
| static void load_hfile_plugins() | ||
| { | ||
| static const struct hFILE_scheme_handler | ||
|
|
@@ -833,6 +909,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 +956,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; | ||
|
Contributor
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
modevalue. IfNULL,strchrwill seg fault.