add rk_undumpdata

git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@23391 ec53bebd-3082-4978-b11e-865c3cabbd6b
This commit is contained in:
Love Hörnquist Åstrand
2008-07-26 18:24:54 +00:00
parent a41b11dae7
commit 13ebabfab7

View File

@@ -55,3 +55,44 @@ rk_dumpdata (const char *filename, const void *buf, size_t size)
net_write(fd, buf, size); net_write(fd, buf, size);
close(fd); close(fd);
} }
/*
* Read all data from a filename, care about errors.
*/
int ROKEN_LIB_FUNCTION
rk_undumpdata(const char *filename, void **buf, size_t *size)
{
struct stat sb;
int fd, ret;
ssize_t sret;
*buf = NULL;
fd = open(filename, O_RDONLY, 0);
if (fd < 0)
return errno;
if (fstat(fd, &sb) != 0){
ret = errno;
goto out;
}
*buf = malloc(sb.st_size);
if (*buf == NULL) {
ret = ENOMEM;
goto out;
}
*size = sb.st_size;
sret = net_read(fd, *buf, *size);
if (sret < 0)
ret = errno;
else if (sret != *size) {
ret = EINVAL;
free(*buf);
*buf = NULL;
}
out:
close(fd);
return ret;
}