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

Showing the changes to stream (2 of 2 files). Show all

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
354354 s->path = talloc_strdup(s, path);
355355 s->mode = flags & (STREAM_READ | STREAM_WRITE);
356356 s->requested_buffer_size = opts->buffer_size;
357+ s->allow_partial_read = flags & STREAM_ALLOW_PARTIAL_READ;
357358
358359 if (flags & STREAM_LESS_NOISE)
359360 mp_msg_set_max_level(s->log, MSGL_WARN);
@@ -802,7 +803,9 @@ int stream_skip_bom(struct stream *s) · 442 unmodified lines
802803 // Read the rest of the stream into memory (current pos to EOF), and return it.
803804 // talloc_ctx: used as talloc parent for the returned allocation
804805 // 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.
806809 // returns: stream contents, or .start/.len set to NULL on error
807810 // If the file was empty, but no error happened, .start will be non-NULL and
808811 // .len will be 0.
@@ -821,16 +824,22 @@ struct bstr stream_read_complete(struct stream *s, void *talloc_ctx, · 12 unmodified lines
821824 int padding = 1;
822825 char *buf = NULL;
823826 int64_t size = stream_get_size(s) - stream_tell(s);
824− if (size > max_size)
827+ if (size > max_size && !s->allow_partial_read)
825828 return (struct bstr){NULL, 0};
826829 if (size > 0)
827830 bufsize = size + padding;
828831 else
829832 bufsize = 1000;
833+ if (s->allow_partial_read)
834+ bufsize = MPMIN(bufsize, max_size + padding);
830835 while (1) {
831836 buf = talloc_realloc_size(talloc_ctx, buf, bufsize);
832837 int readsize = stream_read(s, buf + total_read, bufsize - total_read);
833838 total_read += readsize;
839+ if (total_read >= max_size && s->allow_partial_read) {
840+ total_read = max_size;
841+ break;
842+ }
834843 if (total_read < bufsize)
835844 break;
836845 if (bufsize > max_size) {
stream/stream.h+12−10
@@ -38,21 +38,22 @@ · 37 unmodified lines
3838 // flags for stream_open_ext (this includes STREAM_READ and STREAM_WRITE)
3939
4040 // stream->mode
41−#define STREAM_READ 0
42−#define STREAM_WRITE (1 << 0)
41+#define STREAM_READ 0
42+#define STREAM_WRITE (1 << 0)
4343
44−#define STREAM_SILENT (1 << 1)
44+#define STREAM_SILENT (1 << 1)
4545
4646 // 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
5151
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
5353
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()
5657
5758 // end flags for stream_open_ext (the naming convention sucks)
5859
@@ -155,6 +156,7 @@ typedef struct stream { · 96 unmodified lines
155156 bool is_local_fs : 1; // from the filesystem
156157 bool is_directory : 1; // directory on the filesystem
157158 bool access_references : 1; // open other streams
159+ bool allow_partial_read : 1; // allows partial read with stream_read_file()
158160 struct mp_log *log;
159161 struct mpv_global *global;
160162

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