This allows stream_read_complete() to return partial result instead of error if the file size is larger than the limit.
Unified · Split
| @@ -354,6 +354,7 @@ static int stream_create_instance(const stream_info_t *sinfo, · 353 unmodified lines | |||
| 354 | s->path = talloc_strdup(s, path); | 354 | s->path = talloc_strdup(s, path); |
| 355 | s->mode = flags & (STREAM_READ | STREAM_WRITE); | 355 | s->mode = flags & (STREAM_READ | STREAM_WRITE); |
| 356 | s->requested_buffer_size = opts->buffer_size; | 356 | s->requested_buffer_size = opts->buffer_size; |
| 357 | s->allow_partial_read = flags & STREAM_ALLOW_PARTIAL_READ; | ||
| 357 | 358 | ||
| 358 | if (flags & STREAM_LESS_NOISE) | 359 | if (flags & STREAM_LESS_NOISE) |
| 359 | mp_msg_set_max_level(s->log, MSGL_WARN); | 360 | mp_msg_set_max_level(s->log, MSGL_WARN); |
| @@ -802,7 +803,9 @@ int stream_skip_bom(struct stream *s) · 442 unmodified lines | |||
| 802 | // Read the rest of the stream into memory (current pos to EOF), and return it. | 803 | // Read the rest of the stream into memory (current pos to EOF), and return it. |
| 803 | // talloc_ctx: used as talloc parent for the returned allocation | 804 | // talloc_ctx: used as talloc parent for the returned allocation |
| 804 | // max_size: must be set to >0. If the file is larger than that, it is treated | 805 | // max_size: must be set to >0. If the file is larger than that, it is treated |
| 805 | // as error. This is a minor robustness measure. | 806 | // as error. This is a minor robustness measure. If the stream is |
| 807 | // created with STREAM_ALLOW_PARTIAL_READ flag, partial result up to | ||
| 808 | // max_size is returned instead. | ||
| 806 | // returns: stream contents, or .start/.len set to NULL on error | 809 | // returns: stream contents, or .start/.len set to NULL on error |
| 807 | // If the file was empty, but no error happened, .start will be non-NULL and | 810 | // If the file was empty, but no error happened, .start will be non-NULL and |
| 808 | // .len will be 0. | 811 | // .len will be 0. |
| @@ -821,16 +824,22 @@ struct bstr stream_read_complete(struct stream *s, void *talloc_ctx, · 12 unmodified lines | |||
| 821 | int padding = 1; | 824 | int padding = 1; |
| 822 | char *buf = NULL; | 825 | char *buf = NULL; |
| 823 | int64_t size = stream_get_size(s) - stream_tell(s); | 826 | int64_t size = stream_get_size(s) - stream_tell(s); |
| 824 | if (size > max_size) | 827 | if (size > max_size && !s->allow_partial_read) |
| 825 | return (struct bstr){NULL, 0}; | 828 | return (struct bstr){NULL, 0}; |
| 826 | if (size > 0) | 829 | if (size > 0) |
| 827 | bufsize = size + padding; | 830 | bufsize = size + padding; |
| 828 | else | 831 | else |
| 829 | bufsize = 1000; | 832 | bufsize = 1000; |
| 833 | if (s->allow_partial_read) | ||
| 834 | bufsize = MPMIN(bufsize, max_size + padding); | ||
| 830 | while (1) { | 835 | while (1) { |
| 831 | buf = talloc_realloc_size(talloc_ctx, buf, bufsize); | 836 | buf = talloc_realloc_size(talloc_ctx, buf, bufsize); |
| 832 | int readsize = stream_read(s, buf + total_read, bufsize - total_read); | 837 | int readsize = stream_read(s, buf + total_read, bufsize - total_read); |
| 833 | total_read += readsize; | 838 | total_read += readsize; |
| 839 | if (total_read >= max_size && s->allow_partial_read) { | ||
| 840 | total_read = max_size; | ||
| 841 | break; | ||
| 842 | } | ||
| 834 | if (total_read < bufsize) | 843 | if (total_read < bufsize) |
| 835 | break; | 844 | break; |
| 836 | if (bufsize > max_size) { | 845 | if (bufsize > max_size) { |
| @@ -38,21 +38,22 @@ · 37 unmodified lines | |||
| 38 | // flags for stream_open_ext (this includes STREAM_READ and STREAM_WRITE) | 38 | // flags for stream_open_ext (this includes STREAM_READ and STREAM_WRITE) |
| 39 | 39 | ||
| 40 | // stream->mode | 40 | // stream->mode |
| 41 | #define STREAM_READ 0 | 41 | #define STREAM_READ 0 |
| 42 | #define STREAM_WRITE (1 << 0) | 42 | #define STREAM_WRITE (1 << 0) |
| 43 | 43 | ||
| 44 | #define STREAM_SILENT (1 << 1) | 44 | #define STREAM_SILENT (1 << 1) |
| 45 | 45 | ||
| 46 | // Origin value for "security". This is an integer within the flags bit-field. | 46 | // Origin value for "security". This is an integer within the flags bit-field. |
| 47 | #define STREAM_ORIGIN_DIRECT (1 << 2) // passed from cmdline or loadfile | 47 | #define STREAM_ORIGIN_DIRECT (1 << 2) // passed from cmdline or loadfile |
| 48 | #define STREAM_ORIGIN_FS (2 << 2) // referenced from playlist on unix FS | 48 | #define STREAM_ORIGIN_FS (2 << 2) // referenced from playlist on unix FS |
| 49 | #define STREAM_ORIGIN_NET (3 << 2) // referenced from playlist on network | 49 | #define STREAM_ORIGIN_NET (3 << 2) // referenced from playlist on network |
| 50 | #define STREAM_ORIGIN_UNSAFE (4 << 2) // from a grotesque source | 50 | #define STREAM_ORIGIN_UNSAFE (4 << 2) // from a grotesque source |
| 51 | 51 | ||
| 52 | #define STREAM_ORIGIN_MASK (7 << 2) // for extracting origin value from flags | 52 | #define STREAM_ORIGIN_MASK (7 << 2) // for extracting origin value from flags |
| 53 | 53 | ||
| 54 | #define STREAM_LOCAL_FS_ONLY (1 << 5) // stream_file only, no URLs | 54 | #define STREAM_LOCAL_FS_ONLY (1 << 5) // stream_file only, no URLs |
| 55 | #define STREAM_LESS_NOISE (1 << 6) // try to log errors only | 55 | #define STREAM_LESS_NOISE (1 << 6) // try to log errors only |
| 56 | #define STREAM_ALLOW_PARTIAL_READ (1 << 7) // allows partial read with stream_read_file() | ||
| 56 | 57 | ||
| 57 | // end flags for stream_open_ext (the naming convention sucks) | 58 | // end flags for stream_open_ext (the naming convention sucks) |
| 58 | 59 | ||
| @@ -155,6 +156,7 @@ typedef struct stream { · 96 unmodified lines | |||
| 155 | bool is_local_fs : 1; // from the filesystem | 156 | bool is_local_fs : 1; // from the filesystem |
| 156 | bool is_directory : 1; // directory on the filesystem | 157 | bool is_directory : 1; // directory on the filesystem |
| 157 | bool access_references : 1; // open other streams | 158 | bool access_references : 1; // open other streams |
| 159 | bool allow_partial_read : 1; // allows partial read with stream_read_file() | ||
| 158 | struct mp_log *log; | 160 | struct mp_log *log; |
| 159 | struct mpv_global *global; | 161 | struct mpv_global *global; |
| 160 | 162 | ||