From: Christian Brauner Date: Tue, 10 Mar 2020 16:41:50 +0000 (+0100) Subject: file_utils: handle libcs without fmemopen() X-Git-Tag: lxc-4.0.0~39^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7fa906308918b17b42b4273f1b781d46be12aa1c;p=thirdparty%2Flxc.git file_utils: handle libcs without fmemopen() Signed-off-by: Christian Brauner --- diff --git a/configure.ac b/configure.ac index 31bf90b15..3b4cd5730 100644 --- a/configure.ac +++ b/configure.ac @@ -701,6 +701,10 @@ AC_CHECK_FUNCS([strlcat], AM_CONDITIONAL(HAVE_STRLCAT, true) AC_DEFINE(HAVE_STRLCAT,1,[Have strlcat]), AM_CONDITIONAL(HAVE_STRLCAT, false)) +AC_CHECK_FUNCS([fmemopen], + AM_CONDITIONAL(HAVE_FMEMOPEN, true) + AC_DEFINE(HAVE_FMEMOPEN,1,[Have fmemopen]), + AM_CONDITIONAL(HAVE_FMEMOPEN, false)) # HAVE_STRUCT_RTNL_LINK_STATS64={0,1} AC_CHECK_TYPES([struct rtnl_link_stats64], [], [], [[#include ]]) diff --git a/src/lxc/file_utils.c b/src/lxc/file_utils.c index 56c3552b9..ad9705772 100644 --- a/src/lxc/file_utils.c +++ b/src/lxc/file_utils.c @@ -470,6 +470,7 @@ char *file_to_buf(const char *path, size_t *length) FILE *fopen_cached(const char *path, const char *mode, void **caller_freed_buffer) { +#ifdef HAVE_FMEMOPEN __do_free char *buf = NULL; size_t len = 0; FILE *f; @@ -483,13 +484,17 @@ FILE *fopen_cached(const char *path, const char *mode, void **caller_freed_buffe return NULL; *caller_freed_buffer = move_ptr(buf); return f; +#else + return fopen(path, mode); +#endif } FILE *fdopen_cached(int fd, const char *mode, void **caller_freed_buffer) { + FILE *f; +#ifdef HAVE_FMEMOPEN __do_free char *buf = NULL; size_t len = 0; - FILE *f; buf = fd_to_buf(fd, &len); if (!buf) @@ -500,5 +505,21 @@ FILE *fdopen_cached(int fd, const char *mode, void **caller_freed_buffer) return NULL; *caller_freed_buffer = move_ptr(buf); + +#else + + __do_close_prot_errno int dupfd = -EBADF; + + dupfd = dup(fd); + if (dupfd < 0) + return NULL; + + f = fdopen(dupfd, "re"); + if (!f) + return NULL; + + /* Transfer ownership of fd. */ + move_fd(dupfd); +#endif return f; }