Skip to content

Commit

Permalink
file_utils: handle libcs without fmemopen()
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
  • Loading branch information
Christian Brauner committed Mar 10, 2020
1 parent 77c3e9a commit 7fa9063
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
4 changes: 4 additions & 0 deletions configure.ac
Expand Up @@ -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>]])
Expand Down
23 changes: 22 additions & 1 deletion src/lxc/file_utils.c
Expand Up @@ -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;
Expand All @@ -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)
Expand All @@ -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;
}

0 comments on commit 7fa9063

Please sign in to comment.