diff options
author | Mattias Andrée <maandree@operamail.com> | 2014-08-23 16:22:02 +0200 |
---|---|---|
committer | Mattias Andrée <maandree@operamail.com> | 2014-08-23 16:22:02 +0200 |
commit | e5bf09312ff335b3e5ad7c9b2980a8e5afe62bae (patch) | |
tree | b46cc93d3930fdc3c2d87418f13c883c3dfbcc07 /src | |
parent | m (diff) | |
download | mds-e5bf09312ff335b3e5ad7c9b2980a8e5afe62bae.tar.gz mds-e5bf09312ff335b3e5ad7c9b2980a8e5afe62bae.tar.bz2 mds-e5bf09312ff335b3e5ad7c9b2980a8e5afe62bae.tar.xz |
full_read returns length
Signed-off-by: Mattias Andrée <maandree@operamail.com>
Diffstat (limited to '')
-rw-r--r-- | src/libmdsserver/util.c | 33 | ||||
-rw-r--r-- | src/libmdsserver/util.h | 7 | ||||
-rw-r--r-- | src/mds-base.c | 2 |
3 files changed, 24 insertions, 18 deletions
diff --git a/src/libmdsserver/util.c b/src/libmdsserver/util.c index 66edf16..ed3664b 100644 --- a/src/libmdsserver/util.c +++ b/src/libmdsserver/util.c @@ -263,28 +263,31 @@ int full_write(int fd, const char* buffer, size_t length) /** * Read a file completly and ignore interruptions * - * @param fd The file descriptor - * @return The content of the file, you will need to free it. `NULL` on error. + * @param fd The file descriptor + * @param length Output parameter for the length of the file, may be `NULL` + * @return The content of the file, you will need to free it. `NULL` on error. */ -char* full_read(int fd) +char* full_read(int fd, size_t* length) { - size_t state_buf_size = 8 << 10; - size_t state_buf_ptr = 0; - char* state_buf; + size_t buffer_size = 8 << 10; + size_t buffer_ptr = 0; + char* buffer; ssize_t got; + if (length != NULL) + *length = 0; /* Allocate buffer for data. */ - if (xmalloc(state_buf, state_buf_size, char)) + if (xmalloc(buffer, buffer_size, char)) return NULL; /* Read the file. */ for (;;) { /* Grow buffer if it is too small. */ - if (state_buf_size == state_buf_ptr) + if (buffer_size == buffer_ptr) { - char* old_buf = state_buf; - if (xrealloc(state_buf, state_buf_size <<= 1, char)) + char* old_buf = buffer; + if (xrealloc(buffer, buffer_size <<= 1, char)) { free(old_buf); return NULL; @@ -292,18 +295,20 @@ char* full_read(int fd) } /* Read from the file into the buffer. */ - got = read(fd, state_buf + state_buf_ptr, state_buf_size - state_buf_ptr); + got = read(fd, buffer + buffer_ptr, buffer_size - buffer_ptr); if ((got < 0) && (errno != EINTR)) { - free(state_buf); + free(buffer); return NULL; } if (got == 0) break; - state_buf_ptr += (size_t)got; + buffer_ptr += (size_t)got; } - return state_buf; + if (length != NULL) + *length = buffer_ptr; + return buffer; } diff --git a/src/libmdsserver/util.h b/src/libmdsserver/util.h index 30b2139..77fdf4b 100644 --- a/src/libmdsserver/util.h +++ b/src/libmdsserver/util.h @@ -121,10 +121,11 @@ int full_write(int fd, const char* buffer, size_t length); /** * Read a file completly and ignore interruptions * - * @param fd The file descriptor - * @return The content of the file, you will need to free it. `NULL` on error. + * @param fd The file descriptor + * @param length Output parameter for the length of the file, may be `NULL` + * @return The content of the file, you will need to free it. `NULL` on error. */ -char* full_read(int fd); +char* full_read(int fd, size_t* length); /** * Check whether a string begins with a specific string, diff --git a/src/mds-base.c b/src/mds-base.c index dd72081..672a1d5 100644 --- a/src/mds-base.c +++ b/src/mds-base.c @@ -363,7 +363,7 @@ static int base_unmarshal(void) fail_if (reexec_fd < 0); /* Critical. */ /* Read the state file. */ - fail_if ((state_buf = state_buf_ = full_read(reexec_fd)) == NULL); + fail_if ((state_buf = state_buf_ = full_read(reexec_fd, NULL)) == NULL); /* Release resources. */ close(reexec_fd); |