chunk: Externalize error reporting in chunk_write()
This avoids passing that arbitrary label just for error messages, and gives greater flexibility in handling errors.
This commit is contained in:
@@ -789,7 +789,7 @@ START_TEST(test_chunk_map)
|
||||
chunk_t *map, contents = chunk_from_chars(0x01,0x02,0x03,0x04,0x05);
|
||||
char *path = "/tmp/strongswan-chunk-map-test";
|
||||
|
||||
ck_assert(chunk_write(contents, path, "chunk_map", 022, TRUE));
|
||||
ck_assert(chunk_write(contents, path, 022, TRUE));
|
||||
|
||||
/* read */
|
||||
map = chunk_map(path, FALSE);
|
||||
@@ -827,7 +827,7 @@ START_TEST(test_chunk_from_fd_file)
|
||||
char *path = "/tmp/strongswan-chunk-fd-test";
|
||||
int fd;
|
||||
|
||||
ck_assert(chunk_write(contents, path, "chunk_fd", 022, TRUE));
|
||||
ck_assert(chunk_write(contents, path, 022, TRUE));
|
||||
|
||||
fd = open(path, O_RDONLY);
|
||||
ck_assert(fd != -1);
|
||||
|
||||
@@ -28,7 +28,6 @@
|
||||
#include <ctype.h>
|
||||
|
||||
#include "chunk.h"
|
||||
#include "debug.h"
|
||||
|
||||
/**
|
||||
* Empty chunk.
|
||||
@@ -209,15 +208,16 @@ void chunk_split(chunk_t chunk, const char *mode, ...)
|
||||
/**
|
||||
* Described in header.
|
||||
*/
|
||||
bool chunk_write(chunk_t chunk, char *path, char *label, mode_t mask, bool force)
|
||||
bool chunk_write(chunk_t chunk, char *path, mode_t mask, bool force)
|
||||
{
|
||||
mode_t oldmask;
|
||||
FILE *fd;
|
||||
bool good = FALSE;
|
||||
int tmp = 0;
|
||||
|
||||
if (!force && access(path, F_OK) == 0)
|
||||
{
|
||||
DBG1(DBG_LIB, " %s file '%s' already exists", label, path);
|
||||
errno = EEXIST;
|
||||
return FALSE;
|
||||
}
|
||||
oldmask = umask(mask);
|
||||
@@ -226,23 +226,20 @@ bool chunk_write(chunk_t chunk, char *path, char *label, mode_t mask, bool force
|
||||
{
|
||||
if (fwrite(chunk.ptr, sizeof(u_char), chunk.len, fd) == chunk.len)
|
||||
{
|
||||
DBG1(DBG_LIB, " written %s file '%s' (%d bytes)",
|
||||
label, path, chunk.len);
|
||||
good = TRUE;
|
||||
}
|
||||
else
|
||||
{
|
||||
DBG1(DBG_LIB, " writing %s file '%s' failed: %s",
|
||||
label, path, strerror(errno));
|
||||
tmp = errno;
|
||||
}
|
||||
fclose(fd);
|
||||
}
|
||||
else
|
||||
{
|
||||
DBG1(DBG_LIB, " could not open %s file '%s': %s", label, path,
|
||||
strerror(errno));
|
||||
tmp = errno;
|
||||
}
|
||||
umask(oldmask);
|
||||
errno = tmp;
|
||||
return good;
|
||||
}
|
||||
|
||||
|
||||
@@ -90,14 +90,15 @@ void chunk_split(chunk_t chunk, const char *mode, ...);
|
||||
/**
|
||||
* Write the binary contents of a chunk_t to a file
|
||||
*
|
||||
* If the write fails, errno is set appropriately.
|
||||
*
|
||||
* @param chunk contents to write to file
|
||||
* @param path path where file is written to
|
||||
* @param label label specifying file type
|
||||
* @param mask file mode creation mask
|
||||
* @param force overwrite existing file by force
|
||||
* @return TRUE if write operation was successful
|
||||
*/
|
||||
bool chunk_write(chunk_t chunk, char *path, char *label, mode_t mask, bool force);
|
||||
bool chunk_write(chunk_t chunk, char *path, mode_t mask, bool force);
|
||||
|
||||
/**
|
||||
* Store data read from FD into a chunk
|
||||
|
||||
Reference in New Issue
Block a user