Skip to content

Commit

Permalink
Merge ea523de into 879ff60
Browse files Browse the repository at this point in the history
  • Loading branch information
garlick committed Jan 16, 2018
2 parents 879ff60 + ea523de commit 7b1c5f9
Show file tree
Hide file tree
Showing 11 changed files with 152 additions and 43 deletions.
4 changes: 2 additions & 2 deletions src/cmd/builtin/content.c
Expand Up @@ -26,7 +26,7 @@
#include <unistd.h>

#include "src/common/libutil/blobref.h"
#include "src/common/libutil/readall.h"
#include "src/common/libutil/read_all.h"

static int internal_content_load (optparse_t *p, int ac, char *av[])
{
Expand Down Expand Up @@ -76,7 +76,7 @@ static int internal_content_store (optparse_t *p, int ac, char *av[])
flags |= CONTENT_FLAG_CACHE_BYPASS;
if (!(h = builtin_get_flux_handle (p)))
log_err_exit ("flux_open");
if ((size = read_all (STDIN_FILENO, &data)) < 0)
if ((size = read_all (STDIN_FILENO, (void **)&data)) < 0)
log_err_exit ("read");
if (!(f = flux_content_store (h, data, size, flags)))
log_err_exit ("flux_content_store");
Expand Down
4 changes: 2 additions & 2 deletions src/cmd/flux-kvs.c
Expand Up @@ -34,7 +34,7 @@

#include "src/common/libutil/xzmalloc.h"
#include "src/common/libutil/log.h"
#include "src/common/libutil/readall.h"
#include "src/common/libutil/read_all.h"
#include "src/common/libkvs/treeobj.h"

int cmd_namespace_create (optparse_t *p, int argc, char **argv);
Expand Down Expand Up @@ -602,7 +602,7 @@ int cmd_put (optparse_t *p, int argc, char **argv)
put_flags |= FLUX_KVS_APPEND;

if (!strcmp (val, "-")) { // special handling for "--raw key=-"
if ((len = read_all (STDIN_FILENO, &buf)) < 0)
if ((len = read_all (STDIN_FILENO, (void **)&buf)) < 0)
log_err_exit ("stdin");
val = (char *)buf;
} else
Expand Down
4 changes: 2 additions & 2 deletions src/cmd/flux-logger.c
Expand Up @@ -34,7 +34,7 @@
#include "src/common/libutil/xzmalloc.h"
#include "src/common/libutil/log.h"
#include "src/common/libutil/stdlog.h"
#include "src/common/libutil/readall.h"
#include "src/common/libutil/read_all.h"


#define OPTIONS "hs:n:"
Expand Down Expand Up @@ -82,7 +82,7 @@ int main (int argc, char *argv[])
}
}
if (optind == argc) {
len = read_all (STDIN_FILENO, (uint8_t **)&message);
len = read_all (STDIN_FILENO, (void **)&message);
} else {
if ((e = argz_create (argv + optind, &message, &len)) != 0)
log_errn_exit (e, "argz_create");
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/flux-module.c
Expand Up @@ -37,7 +37,7 @@
#include "src/common/libutil/xzmalloc.h"
#include "src/common/libutil/log.h"
#include "src/common/libutil/oom.h"
#include "src/common/libutil/readall.h"
#include "src/common/libutil/read_all.h"
#include "src/common/libutil/nodeset.h"
#include "src/common/libutil/iterators.h"

Expand Down
11 changes: 8 additions & 3 deletions src/common/libutil/Makefile.am
Expand Up @@ -31,8 +31,8 @@ libutil_la_SOURCES = \
nodeset.c \
nodeset.h \
shortjson.h \
readall.c \
readall.h \
read_all.c \
read_all.h \
ev_zmq.c \
ev_zmq.h \
ev_zlist.c \
Expand Down Expand Up @@ -94,7 +94,8 @@ TESTS = test_nodeset.t \
test_unlink.t \
test_cleanup.t \
test_blobref.t \
test_dirwalk.t
test_dirwalk.t \
test_read_all.t

test_ldadd = \
$(top_builddir)/src/common/libutil/libutil.la \
Expand Down Expand Up @@ -181,3 +182,7 @@ test_cleanup_t_LDADD = $(test_ldadd) $(JANSSON_LIBS)
test_dirwalk_t_SOURCES = test/dirwalk.c
test_dirwalk_t_CPPFLAGS = $(test_cppflags)
test_dirwalk_t_LDADD = $(test_ldadd)

test_read_all_t_SOURCES = test/read_all.c
test_read_all_t_CPPFLAGS = $(test_cppflags)
test_read_all_t_LDADD = $(test_ldadd)
51 changes: 30 additions & 21 deletions src/common/libutil/readall.c → src/common/libutil/read_all.c
Expand Up @@ -27,52 +27,61 @@
#endif
#include <stdlib.h>
#include <unistd.h>
#include <stdint.h>
#include <errno.h>

#include "readall.h"
#include "read_all.h"

int write_all (int fd, const uint8_t *buf, int len)
ssize_t write_all (int fd, const void *buf, size_t len)
{
int n;
int count = 0;
ssize_t n;
ssize_t count = 0;

if (fd < 0 || (buf == NULL && len != 0)) {
errno = EINVAL;
return -1;
}
while (count < len) {
if ((n = write (fd, buf + count, len - count)) < 0)
return n;
return -1;
count += n;
}
return count;
}

int read_all (int fd, uint8_t **bufp)
ssize_t read_all (int fd, void **bufp)
{
const int chunksize = 4096;
int len = 0;
uint8_t *buf = NULL;
int n;
int count = 0;
const size_t chunksize = 4096;
size_t len = 0;
void *buf = NULL;
ssize_t n;
ssize_t count = 0;
void *new;
int saved_errno;

if (fd < 0 || !bufp) {
errno = EINVAL;
return -1;
}
do {
if (len - count == 0) {
len += chunksize;
if (!(buf = buf ? realloc (buf, len) : malloc (len)))
goto nomem;
}
if ((n = read (fd, buf + count, len - count)) < 0) {
free (buf);
return n;
if (!(new = realloc (buf, len)))
goto error;
buf = new;
}
if ((n = read (fd, buf + count, len - count)) < 0)
goto error;
count += n;
} while (n != 0);
*bufp = buf;
return count;
nomem:
errno = ENOMEM;
error:
saved_errno = errno;
free (buf);
errno = saved_errno;
return -1;
}


/*
* vi:tabstop=4 shiftwidth=4 expandtab
*/
9 changes: 9 additions & 0 deletions src/common/libutil/read_all.h
@@ -0,0 +1,9 @@
#ifndef _UTIL_READ_ALL_H
#define _UTIL_READ_ALL_H

#include <sys/types.h>

ssize_t write_all (int fd, const void *buf, size_t len);
ssize_t read_all (int fd, void **bufp);

#endif /* !_UTIL_READ_ALL_H */
9 changes: 0 additions & 9 deletions src/common/libutil/readall.h

This file was deleted.

3 changes: 2 additions & 1 deletion src/common/libutil/test/popen2.c
@@ -1,9 +1,10 @@
#include <string.h>
#include <errno.h>
#include <stdint.h>

#include "src/common/libtap/tap.h"
#include "src/common/libutil/popen2.h"
#include "src/common/libutil/readall.h"
#include "src/common/libutil/read_all.h"

int main(int argc, char** argv)
{
Expand Down
94 changes: 94 additions & 0 deletions src/common/libutil/test/read_all.c
@@ -0,0 +1,94 @@
#if HAVE_CONFIG_H
#include "config.h"
#endif
#include <sys/param.h>
#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>

#include "src/common/libtap/tap.h"
#include "read_all.h"

/* Test for bad args.
*/
void test_badargs (void)
{
char *buf;

errno = 0;
ok (write_all (-1, "", 1) < 0 && errno == EINVAL,
"write_all fd=-1 fails with EINVAL");
errno = 0;
ok (write_all (STDOUT_FILENO, NULL, 1) < 0 && errno == EINVAL,
"write_all buf=NULL fails with EINVAL");

errno = 0;
ok (read_all (-1, (void **)&buf) < 0 && errno == EINVAL,
"read_all fd=-1 fails with EINVAL");
errno = 0;
ok (read_all (STDIN_FILENO, NULL) < 0 && errno == EINVAL,
"read_all buf=NULL fails with EINVAL");
}

/* Write out 'n' bytes to tmpfile.
* Read back 'n' bytes from tmpfile.
* Verify bytes.
*/
void test_readback (size_t sz)
{
void *buf;
void *buf2;
char *t = getenv ("TMPDIR");
char tmpfile[PATH_MAX + 1];
int fd;
ssize_t sz2;

/* Create tmpfile
*/
if (snprintf (tmpfile, sizeof (tmpfile), "%s/read_all.XXXXXX",
t ? t : "/tmp") >= sizeof (tmpfile))
BAIL_OUT ("tmpfile overflow");
if ((fd = mkstemp (tmpfile)) < 0)
BAIL_OUT ("mkstemp: %s", strerror (errno));

/* Create buffer to write of specified size
*/
if (!(buf = malloc (sz)))
BAIL_OUT ("malloc failed");
memset (buf, 'a', sz);

/* Write, read, verify by fd
*/
ok (write_all (fd, buf, sz) == sz,
"write_all wrote %lu bytes", sz);
if (lseek (fd, 0, SEEK_SET) < 0)
BAIL_OUT ("lseek: %s", strerror (errno));
ok ((sz2 = read_all (fd, &buf2)) == sz,
"read_all read %lu bytes", sz);
ok (memcmp (buf2, buf, sz2) == 0,
"and data matches what was written");

if (close (fd) < 0)
BAIL_OUT ("close: %s", strerror (errno));
free (buf);
free (buf2);
(void)unlink (tmpfile);
}

int main (int argc, char *argv[])
{

plan (NO_PLAN);

test_readback (33);
test_readback (8192); // more than internal chunk size

test_badargs ();

done_testing ();
}

/*
* vi: ts=4 sw=4 expandtab
*/
4 changes: 2 additions & 2 deletions t/kvs/blobref.c
Expand Up @@ -11,7 +11,7 @@
#include "src/common/libutil/xzmalloc.h"
#include "src/common/libutil/log.h"
#include "src/common/libutil/blobref.h"
#include "src/common/libutil/readall.h"
#include "src/common/libutil/read_all.h"

int main (int argc, char *argv[])
{
Expand All @@ -26,7 +26,7 @@ int main (int argc, char *argv[])
}
hashtype = argv[1];

if ((size = read_all (STDIN_FILENO, &data)) < 0)
if ((size = read_all (STDIN_FILENO, (void **)&data)) < 0)
log_err_exit ("read");

if (blobref_hash (hashtype, data, size, blobref) < 0)
Expand Down

0 comments on commit 7b1c5f9

Please sign in to comment.