cosmetics to chunk_write()
This commit is contained in:
+18
-19
@@ -18,6 +18,8 @@
|
|||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <errno.h>
|
||||||
|
|
||||||
#include "chunk.h"
|
#include "chunk.h"
|
||||||
|
|
||||||
@@ -205,41 +207,38 @@ void chunk_split(chunk_t chunk, const char *mode, ...)
|
|||||||
/**
|
/**
|
||||||
* Described in header.
|
* 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;
|
mode_t oldmask;
|
||||||
FILE *fd;
|
FILE *fd;
|
||||||
|
bool good = FALSE;
|
||||||
|
|
||||||
if (!force)
|
if (!force && access(path, F_OK) == 0)
|
||||||
{
|
{
|
||||||
fd = fopen(path, "r");
|
DBG1(" file '%s' already exists", path);
|
||||||
if (fd)
|
return FALSE;
|
||||||
{
|
|
||||||
fclose(fd);
|
|
||||||
DBG1(" %s file '%s' already exists", label, path);
|
|
||||||
return FALSE;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* set umask */
|
|
||||||
oldmask = umask(mask);
|
oldmask = umask(mask);
|
||||||
|
|
||||||
fd = fopen(path, "w");
|
fd = fopen(path, "w");
|
||||||
|
|
||||||
if (fd)
|
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);
|
fclose(fd);
|
||||||
DBG1(" written %s file '%s' (%u bytes)", label, path, chunk.len);
|
|
||||||
umask(oldmask);
|
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
DBG1(" could not open %s file '%s' for writing", label, path);
|
DBG1(" could not open file '%s': %s", path, strerror(errno));
|
||||||
umask(oldmask);
|
|
||||||
return FALSE;
|
|
||||||
}
|
}
|
||||||
|
umask(oldmask);
|
||||||
|
return good;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** hex conversion digits */
|
/** hex conversion digits */
|
||||||
|
|||||||
@@ -82,7 +82,7 @@ void chunk_split(chunk_t chunk, const char *mode, ...);
|
|||||||
/**
|
/**
|
||||||
* Write the binary contents of a chunk_t to a file
|
* 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
|
* convert a chunk to an allocated hex string
|
||||||
|
|||||||
@@ -544,6 +544,7 @@ int main(int argc, char **argv)
|
|||||||
attr_chunk = attr_cert->get_encoding(attr_cert);
|
attr_chunk = attr_cert->get_encoding(attr_cert);
|
||||||
if (chunk_write(attr_chunk, outfile, "attribute cert", 0022, TRUE))
|
if (chunk_write(attr_chunk, outfile, "attribute cert", 0022, TRUE))
|
||||||
{
|
{
|
||||||
|
DBG1(" wrote attribute cert file '%s' (%u bytes)", outfile, chunk.len);
|
||||||
write_serial(serial);
|
write_serial(serial);
|
||||||
status = 0;
|
status = 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user