]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
file_utils: handle libcs without fmemopen()
authorChristian Brauner <christian.brauner@ubuntu.com>
Tue, 10 Mar 2020 16:41:50 +0000 (17:41 +0100)
committerChristian Brauner <christian.brauner@ubuntu.com>
Tue, 10 Mar 2020 16:44:56 +0000 (17:44 +0100)
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
configure.ac
src/lxc/file_utils.c

index 31bf90b15c63190d1924a709ceea23958228faac..3b4cd5730e128184368ae3493bc266b31393373f 100644 (file)
@@ -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 <linux/if_link.h>]])
index 56c3552b9b75682a1b70feff9e57f334cb8a0f67..ad970577294981ff652320da7bf6b362840fef26 100644 (file)
@@ -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;
 }