This allows stream_read_complete() to return partial result instead of error if the file size is larger than the limit.
Showing the changes to stream (2 of 2 files). Show all
Unified · Split
| @@ -354,6 +354,7 @@ static int stream_create_instance(const stream_info_t *sinfo, · 353 unmodified lines | ||
| 354 | 354 | s->path = talloc_strdup(s, path); |
| 355 | 355 | s->mode = flags & (STREAM_READ | STREAM_WRITE); |
| 356 | 356 | s->requested_buffer_size = opts->buffer_size; |
| 357 | + s->allow_partial_read = flags & STREAM_ALLOW_PARTIAL_READ; | |
| 357 | 358 | |
| 358 | 359 | if (flags & STREAM_LESS_NOISE) |
| 359 | 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 | 803 | // Read the rest of the stream into memory (current pos to EOF), and return it. |
| 803 | 804 | // talloc_ctx: used as talloc parent for the returned allocation |
| 804 | 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 | 809 | // returns: stream contents, or .start/.len set to NULL on error |
| 807 | 810 | // If the file was empty, but no error happened, .start will be non-NULL and |
| 808 | 811 | // .len will be 0. |
| @@ -821,16 +824,22 @@ struct bstr stream_read_complete(struct stream *s, void *talloc_ctx, · 12 unmodified lines | ||
| 821 | 824 | int padding = 1; |
| 822 | 825 | char *buf = NULL; |
| 823 | 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 | 828 | return (struct bstr){NULL, 0}; |
| 826 | 829 | if (size > 0) |
| 827 | 830 | bufsize = size + padding; |
| 828 | 831 | else |
| 829 | 832 | bufsize = 1000; |
| 833 | + if (s->allow_partial_read) | |
| 834 | + bufsize = MPMIN(bufsize, max_size + padding); | |
| 830 | 835 | while (1) { |
| 831 | 836 | buf = talloc_realloc_size(talloc_ctx, buf, bufsize); |
| 832 | 837 | int readsize = stream_read(s, buf + total_read, bufsize - total_read); |
| 833 | 838 | total_read += readsize; |
| 839 | + if (total_read >= max_size && s->allow_partial_read) { | |
| 840 | + total_read = max_size; | |
| 841 | + break; | |
| 842 | + } | |
| 834 | 843 | if (total_read < bufsize) |
| 835 | 844 | break; |
| 836 | 845 | if (bufsize > max_size) { |
| @@ -38,21 +38,22 @@ · 37 unmodified lines | ||
| 38 | 38 | // flags for stream_open_ext (this includes STREAM_READ and STREAM_WRITE) |
| 39 | 39 | |
| 40 | 40 | // stream->mode |
| 41 | −#define STREAM_READ 0 | |
| 42 | −#define STREAM_WRITE (1 << 0) | |
| 41 | +#define STREAM_READ 0 | |
| 42 | +#define STREAM_WRITE (1 << 0) | |
| 43 | 43 | |
| 44 | −#define STREAM_SILENT (1 << 1) | |
| 44 | +#define STREAM_SILENT (1 << 1) | |
| 45 | 45 | |
| 46 | 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 | |
| 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 | |
| 50 | −#define STREAM_ORIGIN_UNSAFE (4 << 2) // from a grotesque source | |
| 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 | |
| 49 | +#define STREAM_ORIGIN_NET (3 << 2) // referenced from playlist on network | |
| 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 | |
| 55 | −#define STREAM_LESS_NOISE (1 << 6) // try to log errors only | |
| 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 | |
| 56 | +#define STREAM_ALLOW_PARTIAL_READ (1 << 7) // allows partial read with stream_read_file() | |
| 56 | 57 | |
| 57 | 58 | // end flags for stream_open_ext (the naming convention sucks) |
| 58 | 59 | |
| @@ -155,6 +156,7 @@ typedef struct stream { · 96 unmodified lines | ||
| 155 | 156 | bool is_local_fs : 1; // from the filesystem |
| 156 | 157 | bool is_directory : 1; // directory on the filesystem |
| 157 | 158 | bool access_references : 1; // open other streams |
| 159 | + bool allow_partial_read : 1; // allows partial read with stream_read_file() | |
| 158 | 160 | struct mp_log *log; |
| 159 | 161 | struct mpv_global *global; |
| 160 | 162 | |