mirror of
https://github.com/servalproject/serval-dna.git
synced 2024-12-23 15:02:27 +00:00
Improve read_whole_file()
Move into os.c, declare and document in os.h
This commit is contained in:
parent
39b2f3a6f5
commit
21328e2888
21
os.c
21
os.c
@ -162,3 +162,24 @@ ssize_t read_symlink(const char *path, char *buf, size_t len)
|
||||
buf[nr] = '\0';
|
||||
return nr;
|
||||
}
|
||||
|
||||
ssize_t read_whole_file(const char *path, unsigned char *buffer, size_t buffer_size)
|
||||
{
|
||||
int fd = open(path, O_RDONLY);
|
||||
if (fd == -1)
|
||||
return WHYF_perror("open(%d,%s,O_RDONLY)", fd, alloca_str_toprint(path));
|
||||
ssize_t ret;
|
||||
struct stat stat;
|
||||
if (fstat(fd, &stat) == -1)
|
||||
ret = WHYF_perror("fstat(%d)", fd);
|
||||
else if ((size_t)stat.st_size > buffer_size)
|
||||
ret = WHYF("file %s (size %zu) is larger than available buffer (%zu)", alloca_str_toprint(path), (size_t)stat.st_size, buffer_size);
|
||||
else {
|
||||
ret = read(fd, buffer, buffer_size);
|
||||
if (ret == -1)
|
||||
ret = WHYF_perror("read(%d,%s,%zu)", fd, alloca_str_toprint(path), buffer_size);
|
||||
}
|
||||
if (close(fd) == -1)
|
||||
ret = WHYF_perror("close(%d)", fd);
|
||||
return ret;
|
||||
}
|
||||
|
8
os.h
8
os.h
@ -125,4 +125,12 @@ int urandombytes(unsigned char *buf, size_t len);
|
||||
*/
|
||||
ssize_t read_symlink(const char *path, char *buf, size_t len);
|
||||
|
||||
/* Read the whole file into the given buffer. If the file will not fit into
|
||||
* the buffer or if there is an error opening or reading the file, logs an
|
||||
* error and returns -1. Otherwise, returns the number of bytes read.
|
||||
*
|
||||
* @author Andrew Bettison <andrew@servalproject.com>
|
||||
*/
|
||||
ssize_t read_whole_file(const char *path, unsigned char *buffer, size_t buffer_size);
|
||||
|
||||
#endif //__SERVAL_DNA__OS_H
|
||||
|
@ -399,19 +399,6 @@ int rhizome_manifest_verify(rhizome_manifest *m)
|
||||
return 1;
|
||||
}
|
||||
|
||||
ssize_t read_whole_file(const char *path, unsigned char *buffer, size_t buffer_size)
|
||||
{
|
||||
int fd = open(path, O_RDONLY);
|
||||
if (fd == -1)
|
||||
return WHYF_perror("open(%s,O_RDONLY)", alloca_str_toprint(path));
|
||||
ssize_t ret = read(fd, buffer, buffer_size);
|
||||
if (ret == -1)
|
||||
ret = WHYF_perror("read(%s,%zu)", alloca_str_toprint(path), buffer_size);
|
||||
if (close(fd) == -1)
|
||||
ret = WHY_perror("close");
|
||||
return ret;
|
||||
}
|
||||
|
||||
static void rhizome_manifest_clear(rhizome_manifest *m)
|
||||
{
|
||||
while (m->var_count) {
|
||||
|
Loading…
Reference in New Issue
Block a user