chunk: Add helper function to create a chunk from data read from a file descriptor
This commit is contained in:
@@ -243,6 +243,38 @@ bool chunk_write(chunk_t chunk, char *path, char *label, mode_t mask, bool force
|
|||||||
return good;
|
return good;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Described in header.
|
||||||
|
*/
|
||||||
|
chunk_t chunk_from_fd(int fd)
|
||||||
|
{
|
||||||
|
char buf[8096];
|
||||||
|
char *pos = buf;
|
||||||
|
ssize_t len, total = 0;
|
||||||
|
|
||||||
|
while (TRUE)
|
||||||
|
{
|
||||||
|
len = read(fd, pos, buf + sizeof(buf) - pos);
|
||||||
|
if (len < 0)
|
||||||
|
{
|
||||||
|
DBG1(DBG_LIB, "reading from file descriptor failed: %s",
|
||||||
|
strerror(errno));
|
||||||
|
return chunk_empty;
|
||||||
|
}
|
||||||
|
if (len == 0)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
total += len;
|
||||||
|
if (total == sizeof(buf))
|
||||||
|
{
|
||||||
|
DBG1(DBG_LIB, "buffer too small to read from file descriptor");
|
||||||
|
return chunk_empty;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return chunk_clone(chunk_create(buf, total));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/** hex conversion digits */
|
/** hex conversion digits */
|
||||||
static char hexdig_upper[] = "0123456789ABCDEF";
|
static char hexdig_upper[] = "0123456789ABCDEF";
|
||||||
|
|||||||
@@ -99,6 +99,14 @@ void chunk_split(chunk_t chunk, const char *mode, ...);
|
|||||||
*/
|
*/
|
||||||
bool chunk_write(chunk_t chunk, char *path, char *label, mode_t mask, bool force);
|
bool chunk_write(chunk_t chunk, char *path, char *label, mode_t mask, bool force);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Store data read from FD into a chunk
|
||||||
|
*
|
||||||
|
* @param fd file descriptor to read from
|
||||||
|
* @return chunk or chunk_empty on failure
|
||||||
|
*/
|
||||||
|
chunk_t chunk_from_fd(int fd);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Convert a chunk of data to hex encoding.
|
* Convert a chunk of data to hex encoding.
|
||||||
*
|
*
|
||||||
|
|||||||
Reference in New Issue
Block a user