diff --git a/src/libstrongswan/chunk.c b/src/libstrongswan/chunk.c index b2c9d600f..14524e77b 100644 --- a/src/libstrongswan/chunk.c +++ b/src/libstrongswan/chunk.c @@ -18,6 +18,8 @@ #include #include +#include +#include #include "chunk.h" @@ -205,41 +207,38 @@ void chunk_split(chunk_t chunk, const char *mode, ...) /** * Described in header. */ -bool chunk_write(chunk_t chunk, const char *path, const 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; - if (!force) + if (!force && access(path, F_OK) == 0) { - fd = fopen(path, "r"); - if (fd) - { - fclose(fd); - DBG1(" %s file '%s' already exists", label, path); - return FALSE; - } + DBG1(" file '%s' already exists", path); + return FALSE; } - - /* set umask */ oldmask = umask(mask); - fd = fopen(path, "w"); - if (fd) { - fwrite(chunk.ptr, sizeof(u_char), chunk.len, fd); + if (fwrite(chunk.ptr, sizeof(u_char), chunk.len, fd) == chunk.len) + { + good = TRUE; + } + else + { + DBG1(" writing to file '%s' failed: %s", path, strerror(errno)); + } fclose(fd); - DBG1(" written %s file '%s' (%u bytes)", label, path, chunk.len); - umask(oldmask); return TRUE; } else { - DBG1(" could not open %s file '%s' for writing", label, path); - umask(oldmask); - return FALSE; + DBG1(" could not open file '%s': %s", path, strerror(errno)); } + umask(oldmask); + return good; } /** hex conversion digits */ diff --git a/src/libstrongswan/chunk.h b/src/libstrongswan/chunk.h index b4fd2f080..af0e9c4f3 100644 --- a/src/libstrongswan/chunk.h +++ b/src/libstrongswan/chunk.h @@ -82,7 +82,7 @@ void chunk_split(chunk_t chunk, const char *mode, ...); /** * Write the binary contents of a chunk_t to a file */ -bool chunk_write(chunk_t chunk, const char *path, const char *label, mode_t mask, bool force); +bool chunk_write(chunk_t chunk, char *path, mode_t mask, bool force); /** * convert a chunk to an allocated hex string diff --git a/src/openac/openac.c b/src/openac/openac.c index b16b53b0b..61c3b05da 100755 --- a/src/openac/openac.c +++ b/src/openac/openac.c @@ -544,6 +544,7 @@ int main(int argc, char **argv) attr_chunk = attr_cert->get_encoding(attr_cert); if (chunk_write(attr_chunk, outfile, "attribute cert", 0022, TRUE)) { + DBG1(" wrote attribute cert file '%s' (%u bytes)", outfile, chunk.len); write_serial(serial); status = 0; }