summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorAliaksei Karaliou <akaraliou@panasas.com>2018-12-27 04:25:47 -0500
committerKarolin Seeger <kseeger@samba.org>2019-06-13 10:22:03 +0000
commitecd28164948f1b217cb2135b548738752a97153e (patch)
tree27a01c1fe3b5491632d5de4d9ef7bfbe37a2668f /lib
parentfefd249619b8a95cf5caa522a3d6330001cc637b (diff)
downloadsamba-ecd28164948f1b217cb2135b548738752a97153e.tar.gz
s3:util: Move static file_pload() function to lib/util
file_pload() is static private function in Samba3 library, however it does not have any special dependencies and might be widely used as common function, so moving it into common samba-util library. Extra fix needed to enable easy back-port of code for: BUG: https://bugzilla.samba.org/show_bug.cgi?id=13964 Signed-off-by: Aliaksei Karaliou <akaraliou@panasas.com> Reviewed-by: Garming Sam <garming@catalyst.net.nz> Reviewed-by: Andrew Bartlett <abartlet@samba.org> (cherry picked from commit d21fc7d8b86b0cddc619ffe528d9cd93eeedbb0b)
Diffstat (limited to 'lib')
-rw-r--r--lib/util/samba_util.h5
-rw-r--r--lib/util/util_file.c48
2 files changed, 53 insertions, 0 deletions
diff --git a/lib/util/samba_util.h b/lib/util/samba_util.h
index 7b96a595d43..fff5478cd6d 100644
--- a/lib/util/samba_util.h
+++ b/lib/util/samba_util.h
@@ -394,6 +394,11 @@ _PUBLIC_ int fdprintf(int fd, const char *format, ...) PRINTF_ATTRIBUTE(2,3);
*/
bool file_compare(const char *path1, const char *path2);
+/*
+ load from a pipe into memory.
+ */
+char *file_pload(const char *syscmd, size_t *size);
+
/* The following definitions come from lib/util/util.c */
diff --git a/lib/util/util_file.c b/lib/util/util_file.c
index 926eda240f6..90d39f7cdd3 100644
--- a/lib/util/util_file.c
+++ b/lib/util/util_file.c
@@ -24,6 +24,8 @@
#include "system/filesys.h"
#include <talloc.h>
#include "lib/util/samba_util.h"
+#include "lib/util/sys_popen.h"
+#include "lib/util/sys_rw.h"
#include "lib/util/debug.h"
/**
@@ -362,3 +364,49 @@ bool file_compare(const char *path1, const char *path2)
talloc_free(mem_ctx);
return true;
}
+
+
+/**
+ Load from a pipe into memory.
+**/
+char *file_pload(const char *syscmd, size_t *size)
+{
+ int fd, n;
+ char *p;
+ char buf[1024];
+ size_t total;
+
+ fd = sys_popen(syscmd);
+ if (fd == -1) {
+ return NULL;
+ }
+
+ p = NULL;
+ total = 0;
+
+ while ((n = sys_read(fd, buf, sizeof(buf))) > 0) {
+ p = talloc_realloc(NULL, p, char, total + n + 1);
+ if (!p) {
+ DEBUG(0,("file_pload: failed to expand buffer!\n"));
+ close(fd);
+ return NULL;
+ }
+ memcpy(p+total, buf, n);
+ total += n;
+ }
+
+ if (p) {
+ p[total] = 0;
+ }
+
+ /* FIXME: Perhaps ought to check that the command completed
+ * successfully (returned 0); if not the data may be
+ * truncated. */
+ sys_pclose(fd);
+
+ if (size) {
+ *size = total;
+ }
+
+ return p;
+}