From: Trond Norbye Date: Sat, 2 May 2009 06:54:30 +0000 (-0700) Subject: Refactor: memcached_io_read should return memcached_return error codes X-Git-Tag: 0.29~12 X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=c97c8525686219ecae254d6e6a4b8ccab0777269;p=m6w6%2Flibmemcached Refactor: memcached_io_read should return memcached_return error codes --- diff --git a/libmemcached/memcached_io.c b/libmemcached/memcached_io.c index 3f184e06..95774865 100644 --- a/libmemcached/memcached_io.c +++ b/libmemcached/memcached_io.c @@ -90,8 +90,8 @@ void memcached_io_preread(memcached_st *ptr) } #endif -ssize_t memcached_io_read(memcached_server_st *ptr, - void *buffer, size_t length) +memcached_return memcached_io_read(memcached_server_st *ptr, + void *buffer, size_t length, ssize_t *nread) { char *buffer_ptr; @@ -105,26 +105,26 @@ ssize_t memcached_io_read(memcached_server_st *ptr, while (1) { - data_read= read(ptr->fd, - ptr->read_buffer, - MEMCACHED_MAX_BUFFER); + data_read= read(ptr->fd, ptr->read_buffer, MEMCACHED_MAX_BUFFER); if (data_read > 0) break; else if (data_read == -1) { ptr->cached_errno= errno; + memcached_return rc= MEMCACHED_UNKNOWN_READ_FAILURE; switch (errno) { case EAGAIN: case EINTR: - if (io_wait(ptr, MEM_READ) == MEMCACHED_SUCCESS) + if ((rc= io_wait(ptr, MEM_READ)) == MEMCACHED_SUCCESS) continue; /* fall through */ default: { memcached_quit_server(ptr, 1); - return -1; + *nread= -1; + return rc; } } } @@ -140,7 +140,8 @@ ssize_t memcached_io_read(memcached_server_st *ptr, it will return EGAIN if data is not immediatly available. */ memcached_quit_server(ptr, 1); - return -1; + *nread= -1; + return MEMCACHED_UNKNOWN_READ_FAILURE; } } @@ -173,7 +174,8 @@ ssize_t memcached_io_read(memcached_server_st *ptr, } ptr->server_failure_counter= 0; - return (size_t)(buffer_ptr - (char*)buffer); + *nread = (size_t)(buffer_ptr - (char*)buffer); + return MEMCACHED_SUCCESS; } ssize_t memcached_io_write(memcached_server_st *ptr, @@ -443,12 +445,12 @@ memcached_return memcached_safe_read(memcached_server_st *ptr, while (offset < size) { - ssize_t nread= memcached_io_read(ptr, data + offset, size - offset); - if (nread <= 0) - { - memcached_io_reset(ptr); - return MEMCACHED_UNKNOWN_READ_FAILURE; - } + ssize_t nread; + memcached_return rc= memcached_io_read(ptr, data + offset, size - offset, + &nread); + if (rc != MEMCACHED_SUCCESS) + return rc; + offset+= nread; } @@ -471,8 +473,10 @@ memcached_return memcached_io_readline(memcached_server_st *ptr, * buffer. Call the standard read function to avoid duplicating * the logic. */ - if (memcached_io_read(ptr, buffer_ptr, 1) != 1) - return MEMCACHED_UNKNOWN_READ_FAILURE; + ssize_t nread; + memcached_return rc= memcached_io_read(ptr, buffer_ptr, 1, &nread); + if (rc != MEMCACHED_SUCCESS) + return rc; if (*buffer_ptr == '\n') line_complete= true; diff --git a/libmemcached/memcached_io.h b/libmemcached/memcached_io.h index 54422774..372357a9 100644 --- a/libmemcached/memcached_io.h +++ b/libmemcached/memcached_io.h @@ -26,8 +26,8 @@ struct udp_datagram_header_st { ssize_t memcached_io_write(memcached_server_st *ptr, const void *buffer, size_t length, char with_flush); void memcached_io_reset(memcached_server_st *ptr); -ssize_t memcached_io_read(memcached_server_st *ptr, - void *buffer, size_t length); +memcached_return memcached_io_read(memcached_server_st *ptr, + void *buffer, size_t length, ssize_t *nread); /* Read a line (terminated by '\n') into the buffer */ memcached_return memcached_io_readline(memcached_server_st *ptr, char *buffer_ptr, diff --git a/libmemcached/memcached_quit.c b/libmemcached/memcached_quit.c index c93cb188..c202d433 100644 --- a/libmemcached/memcached_quit.c +++ b/libmemcached/memcached_quit.c @@ -16,7 +16,6 @@ void memcached_quit_server(memcached_server_st *ptr, uint8_t io_death) if (io_death == 0 && ptr->type != MEMCACHED_CONNECTION_UDP) { memcached_return rc; - ssize_t read_length; char buffer[MEMCACHED_MAX_BUFFER]; if (ptr->root->flags & MEM_BINARY_PROTOCOL) @@ -37,9 +36,9 @@ void memcached_quit_server(memcached_server_st *ptr, uint8_t io_death) * results in server throwing away all data which is * not read */ - while ((read_length= - memcached_io_read(ptr, buffer, sizeof(buffer)/sizeof(*buffer))) - > 0); + ssize_t nread; + while (memcached_io_read(ptr, buffer, sizeof(buffer)/sizeof(*buffer), + &nread) == MEMCACHED_SUCCESS); } memcached_io_close(ptr); diff --git a/libmemcached/memcached_response.c b/libmemcached/memcached_response.c index 55416930..845052db 100644 --- a/libmemcached/memcached_response.c +++ b/libmemcached/memcached_response.c @@ -80,7 +80,6 @@ static memcached_return textual_value_fetch(memcached_server_st *ptr, char *end_ptr; char *next_ptr; size_t value_length; - size_t read_length; size_t to_read; char *value_ptr; @@ -167,7 +166,6 @@ static memcached_return textual_value_fetch(memcached_server_st *ptr, } value_ptr= memcached_string_value(&result->value); - read_length= 0; /* We read the \r\n into the string since not doing so is more cycles then the waster of memory to do so. @@ -176,7 +174,11 @@ static memcached_return textual_value_fetch(memcached_server_st *ptr, some people lazy about using the return length. */ to_read= (value_length) + 2; - read_length= memcached_io_read(ptr, value_ptr, to_read); + ssize_t read_length= 0; + memcached_return rrc= memcached_io_read(ptr, value_ptr, to_read, &read_length); + if (rrc != MEMCACHED_SUCCESS) + return rrc; + if (read_length != (size_t)(value_length + 2)) { goto read_error; diff --git a/tests/function.c b/tests/function.c index 182aa8c3..485a95cc 100644 --- a/tests/function.c +++ b/tests/function.c @@ -2020,9 +2020,10 @@ static test_return user_supplied_bug10(memcached_st *memc) { rc= memcached_set(mclone, key, key_len,value, value_length, 0, 0); - assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_WRITE_FAILURE || rc == MEMCACHED_BUFFERED); + assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_WRITE_FAILURE || + rc == MEMCACHED_BUFFERED || rc == MEMCACHED_TIMEOUT); - if (rc == MEMCACHED_WRITE_FAILURE) + if (rc == MEMCACHED_WRITE_FAILURE || rc == MEMCACHED_TIMEOUT) x--; }