fx: stream: only return an error from read/write if no bytes were moved
This commit is contained in:
+18
-25
@@ -642,9 +642,8 @@ static fx_stream *init_stdio_stream(FILE *fp, fx_stream_mode mode)
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct fx_stdio_stream_p *p = fx_object_get_private(
|
struct fx_stdio_stream_p *p
|
||||||
stream,
|
= fx_object_get_private(stream, FX_TYPE_STDIO_STREAM);
|
||||||
FX_TYPE_STDIO_STREAM);
|
|
||||||
fx_stream_cfg *cfg = fx_object_get_protected(stream, FX_TYPE_STREAM);
|
fx_stream_cfg *cfg = fx_object_get_protected(stream, FX_TYPE_STREAM);
|
||||||
p->s_fp = fp;
|
p->s_fp = fp;
|
||||||
cfg->s_mode = mode;
|
cfg->s_mode = mode;
|
||||||
@@ -681,8 +680,8 @@ static enum fx_status stream_push_indent(struct stream_data *stream, int indent)
|
|||||||
stream->s_private->s_istack_size += 4;
|
stream->s_private->s_istack_size += 4;
|
||||||
}
|
}
|
||||||
|
|
||||||
int cur_indent = stream->s_private
|
int cur_indent
|
||||||
->s_istack[stream->s_private->s_istack_ptr];
|
= stream->s_private->s_istack[stream->s_private->s_istack_ptr];
|
||||||
stream->s_private->s_istack[++stream->s_private->s_istack_ptr]
|
stream->s_private->s_istack[++stream->s_private->s_istack_ptr]
|
||||||
= cur_indent + indent;
|
= cur_indent + indent;
|
||||||
|
|
||||||
@@ -750,9 +749,8 @@ fx_stream_buffer *fx_stream_buffer_create_dynamic(size_t buffer_size)
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct fx_stream_buffer_p *p = fx_object_get_private(
|
struct fx_stream_buffer_p *p
|
||||||
buffer,
|
= fx_object_get_private(buffer, FX_TYPE_STREAM_BUFFER);
|
||||||
FX_TYPE_STREAM_BUFFER);
|
|
||||||
|
|
||||||
p->p_buf = malloc(buffer_size);
|
p->p_buf = malloc(buffer_size);
|
||||||
if (!p->p_buf) {
|
if (!p->p_buf) {
|
||||||
@@ -773,9 +771,8 @@ fx_stream_buffer *fx_stream_buffer_create(void *buf, size_t len)
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct fx_stream_buffer_p *p = fx_object_get_private(
|
struct fx_stream_buffer_p *p
|
||||||
buffer,
|
= fx_object_get_private(buffer, FX_TYPE_STREAM_BUFFER);
|
||||||
FX_TYPE_STREAM_BUFFER);
|
|
||||||
|
|
||||||
p->p_buf = buf;
|
p->p_buf = buf;
|
||||||
p->p_buf_len = len;
|
p->p_buf_len = len;
|
||||||
@@ -1130,15 +1127,14 @@ static enum fx_status stdio_read(
|
|||||||
size_t max,
|
size_t max,
|
||||||
size_t *nr_read)
|
size_t *nr_read)
|
||||||
{
|
{
|
||||||
struct fx_stdio_stream_p *p = fx_object_get_private(
|
struct fx_stdio_stream_p *p
|
||||||
stream,
|
= fx_object_get_private(stream, FX_TYPE_STDIO_STREAM);
|
||||||
FX_TYPE_STDIO_STREAM);
|
|
||||||
|
|
||||||
enum fx_status status = FX_SUCCESS;
|
enum fx_status status = FX_SUCCESS;
|
||||||
|
|
||||||
size_t count = fread(out, 1, max, p->s_fp);
|
size_t count = fread(out, 1, max, p->s_fp);
|
||||||
|
|
||||||
if (ferror(p->s_fp)) {
|
if (count == 0 && ferror(p->s_fp)) {
|
||||||
status = FX_ERR_IO_FAILURE;
|
status = FX_ERR_IO_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1152,14 +1148,13 @@ static enum fx_status stdio_write(
|
|||||||
size_t count,
|
size_t count,
|
||||||
size_t *nr_written)
|
size_t *nr_written)
|
||||||
{
|
{
|
||||||
struct fx_stdio_stream_p *p = fx_object_get_private(
|
struct fx_stdio_stream_p *p
|
||||||
stream,
|
= fx_object_get_private(stream, FX_TYPE_STDIO_STREAM);
|
||||||
FX_TYPE_STDIO_STREAM);
|
|
||||||
|
|
||||||
enum fx_status status = FX_SUCCESS;
|
enum fx_status status = FX_SUCCESS;
|
||||||
size_t w = fwrite(data, 1, count, p->s_fp);
|
size_t w = fwrite(data, 1, count, p->s_fp);
|
||||||
|
|
||||||
if (ferror(p->s_fp)) {
|
if (count == 0 && ferror(p->s_fp)) {
|
||||||
status = FX_ERR_IO_FAILURE;
|
status = FX_ERR_IO_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1172,9 +1167,8 @@ static enum fx_status stdio_seek(
|
|||||||
long long offset,
|
long long offset,
|
||||||
fx_stream_seek_origin origin)
|
fx_stream_seek_origin origin)
|
||||||
{
|
{
|
||||||
struct fx_stdio_stream_p *p = fx_object_get_private(
|
struct fx_stdio_stream_p *p
|
||||||
stream,
|
= fx_object_get_private(stream, FX_TYPE_STDIO_STREAM);
|
||||||
FX_TYPE_STDIO_STREAM);
|
|
||||||
|
|
||||||
int whence = 0;
|
int whence = 0;
|
||||||
switch (origin) {
|
switch (origin) {
|
||||||
@@ -1201,9 +1195,8 @@ static enum fx_status stdio_seek(
|
|||||||
|
|
||||||
static enum fx_status stdio_tell(const fx_stream *stream, size_t *cursor)
|
static enum fx_status stdio_tell(const fx_stream *stream, size_t *cursor)
|
||||||
{
|
{
|
||||||
struct fx_stdio_stream_p *p = fx_object_get_private(
|
struct fx_stdio_stream_p *p
|
||||||
stream,
|
= fx_object_get_private(stream, FX_TYPE_STDIO_STREAM);
|
||||||
FX_TYPE_STDIO_STREAM);
|
|
||||||
|
|
||||||
long pos = ftell(p->s_fp);
|
long pos = ftell(p->s_fp);
|
||||||
if (pos == -1L) {
|
if (pos == -1L) {
|
||||||
|
|||||||
Reference in New Issue
Block a user