-
Notifications
You must be signed in to change notification settings - Fork 465
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
Closed
Closed
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
80bb84f
Add in-memory I/O
ThomasHickman 58fb979
Add better error handling in hpreload_fd
ThomasHickman 29a6789
Add create_hfile_mem
ThomasHickman 5cd9e07
Move mem backend code to the correct section
ThomasHickman 3972015
Fix issues with mem hfile
ThomasHickman 3d11377
Free hFILE buffer when erroring
ThomasHickman 3656732
Stop freeing hfile mem buffers in hclose
ThomasHickman fde4dcf
Fixes to PR#590.
jkbonfield 5fa67e9
Create preload scheme
ThomasHickman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(mode == NULL || !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,16 @@ 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) | ||
| return NULL; | ||
|
|
||
| fp->base.backend = &mem_backend; | ||
| return &fp->base; | ||
| } | ||
|
|
||
| static hFILE *hopen_mem(const char *url, const char *mode) | ||
| { | ||
| size_t length, size; | ||
|
|
@@ -735,12 +778,38 @@ static hFILE *hopen_mem(const char *url, const char *mode) | |
| hts_decode_percent(buffer, &length, data); | ||
| } | ||
|
|
||
| hFILE_mem *fp = (hFILE_mem *) | ||
| hfile_init_fixed(sizeof (hFILE_mem), mode, buffer, length, size); | ||
| if (fp == NULL) { free(buffer); return NULL; } | ||
| return create_hfile_mem(buffer, mode, length, size); | ||
| } | ||
|
|
||
| fp->base.backend = &mem_backend; | ||
| 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; | ||
| } | ||
|
|
||
|
|
||
|
|
@@ -833,6 +902,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 +949,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; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 still need to free
buffer. I would check the value returned bycreate_hfile_memand, ifNULL,free(buffer).