Commit b684c98d authored by Jonathan Jenkins's avatar Jonathan Jenkins

add a file dump utility function

parent 33e78fd2
Pipeline #19 skipped
...@@ -66,6 +66,13 @@ const char * ssg_get_addr_str(const ssg_t s, int rank); ...@@ -66,6 +66,13 @@ const char * ssg_get_addr_str(const ssg_t s, int rank);
// group serialization mechanism // group serialization mechanism
hg_return_t hg_proc_ssg_t(hg_proc_t proc, ssg_t *s); hg_return_t hg_proc_ssg_t(hg_proc_t proc, ssg_t *s);
/// utility functions
// dump address list to the given file
// returns -1 on error, corresponding to the return code of open/write/close,
// and sets errno
int ssg_dump(const ssg_t s, const char *fname);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <fcntl.h> #include <fcntl.h>
#include <unistd.h> #include <unistd.h>
#include <errno.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
...@@ -397,6 +398,47 @@ end: ...@@ -397,6 +398,47 @@ end:
return hret; return hret;
} }
int ssg_dump(const ssg_t s, const char *fname)
{
// file to write to
int fd = -1;
ssize_t written;
// string to xform and dump
char * addrs_dup = NULL;
char * tok = NULL;
char * addrs_dup_end = NULL;
// return code
int ret = 0;
// copy the backing buffer, replacing the intermediate null chars with
// newlines (can't use strdup)
addrs_dup = malloc(s->buf_size);
if (addrs_dup == NULL) { errno = ENOMEM; ret = -1; goto end; }
memcpy(addrs_dup, s->backing_buf, s->buf_size);
tok = addrs_dup;
addrs_dup_end = addrs_dup + s->buf_size;
for (int i = 0; i < s->num_addrs-1; i++) {
tok = memchr(tok, '\0', addrs_dup_end - tok);
if (tok == NULL) { errno = EINVAL; ret = -1; goto end; }
*tok = '\n';
}
// open the file and dump in a single call
fd = open(fname, O_WRONLY | O_CREAT | O_EXCL);
if (fd == -1) { ret = -1; goto end; }
// don't include the null char at the end
written = write(fd, addrs_dup, s->buf_size-1);
if (written != s->buf_size-1) ret = -1;
end:
free(addrs_dup);
if (fd != -1) close(fd);
return ret;
}
typedef struct serv_addr_out typedef struct serv_addr_out
{ {
hg_addr_t addr; hg_addr_t addr;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment