History

stream: add STREAM_ALLOW_PARTIAL_READ flag

This allows stream_read_complete() to return partial result instead of error if the file size is larger than the limit.

nanahi committed 2 years ago

Commit c2e71ce1 parent fad946d2 files · +23 −12Browse files

Unified · Split

2 files
  1. stream/stream.c +11 −2
  2. stream/stream.h +12 −10
stream/stream.c+11−2
@@ -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 allocation804// 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 treated805// 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 error809// 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 and810// 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 else831 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) {
stream/stream.h+12−10
@@ -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->mode40// stream->mode
41#define STREAM_READ 041#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 loadfile47#define STREAM_ORIGIN_DIRECT (1 << 2) // passed from cmdline or loadfile
48#define STREAM_ORIGIN_FS (2 << 2) // referenced from playlist on unix FS48#define STREAM_ORIGIN_FS (2 << 2) // referenced from playlist on unix FS
49#define STREAM_ORIGIN_NET (3 << 2) // referenced from playlist on network49#define STREAM_ORIGIN_NET (3 << 2) // referenced from playlist on network
50#define STREAM_ORIGIN_UNSAFE (4 << 2) // from a grotesque source50#define STREAM_ORIGIN_UNSAFE (4 << 2) // from a grotesque source
51 51
52#define STREAM_ORIGIN_MASK (7 << 2) // for extracting origin value from flags52#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 URLs54#define STREAM_LOCAL_FS_ONLY (1 << 5) // stream_file only, no URLs
55#define STREAM_LESS_NOISE (1 << 6) // try to log errors only55#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 filesystem156 bool is_local_fs : 1; // from the filesystem
156 bool is_directory : 1; // directory on the filesystem157 bool is_directory : 1; // directory on the filesystem
157 bool access_references : 1; // open other streams158 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

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t