chunk: Use dynamically allocated buffer in chunk_from_fd()

When acting on files, we can use fstat() to estimate the buffer size. On
non-file FDs, we dynamically increase an allocated buffer.

Additionally we slightly change the function signature to properly handle
zero-length files and add appropriate unit tests.
This commit is contained in:
Martin Willi
2014-01-23 15:55:32 +01:00
parent 595b6d9a82
commit 1c4a3459f7
10 changed files with 183 additions and 25 deletions
+41 -12
View File
@@ -247,33 +247,62 @@ bool chunk_write(chunk_t chunk, char *path, char *label, mode_t mask, bool force
/**
* Described in header.
*/
chunk_t chunk_from_fd(int fd)
bool chunk_from_fd(int fd, chunk_t *out)
{
char buf[8096];
char *pos = buf;
ssize_t len, total = 0;
struct stat sb;
char *buf, *tmp;
ssize_t len, total = 0, bufsize;
if (fstat(fd, &sb) == 0 && S_ISREG(sb.st_mode))
{
bufsize = sb.st_size;
}
else
{
bufsize = 256;
}
buf = malloc(bufsize);
if (!buf)
{ /* for huge files */
return FALSE;
}
while (TRUE)
{
len = read(fd, pos, buf + sizeof(buf) - pos);
len = read(fd, buf + total, bufsize - total);
if (len < 0)
{
DBG1(DBG_LIB, "reading from file descriptor failed: %s",
strerror(errno));
return chunk_empty;
free(buf);
return FALSE;
}
if (len == 0)
{
break;
}
total += len;
if (total == sizeof(buf))
if (total == bufsize)
{
DBG1(DBG_LIB, "buffer too small to read from file descriptor");
return chunk_empty;
bufsize *= 2;
tmp = realloc(buf, bufsize);
if (!tmp)
{
free(buf);
return FALSE;
}
buf = tmp;
}
}
return chunk_clone(chunk_create(buf, total));
if (total == 0)
{
free(buf);
buf = NULL;
}
else if (total < bufsize)
{
buf = realloc(buf, total);
}
*out = chunk_create(buf, total);
return TRUE;
}
/**
+5 -2
View File
@@ -102,10 +102,13 @@ bool chunk_write(chunk_t chunk, char *path, char *label, mode_t mask, bool force
/**
* Store data read from FD into a chunk
*
* On error, errno is set appropriately.
*
* @param fd file descriptor to read from
* @return chunk or chunk_empty on failure
* @param chunk chunk receiving allocated buffer
* @return TRUE if successful, FALSE on failure
*/
chunk_t chunk_from_fd(int fd);
bool chunk_from_fd(int fd, chunk_t *chunk);
/**
* mmap() a file to a chunk