[vlc-devel] [PATCH v2 3/13] platform: avoid hard coded temp dir path in vlc_memfd()
Rémi Denis-Courmont
remi at remlab.net
Tue Oct 6 16:49:05 CEST 2020
Le tiistaina 6. lokakuuta 2020, 8.57.32 EEST Lyndon Brown a écrit :
> From: Lyndon Brown <jnqnfe at gmail.com>
> Date: Mon, 5 Oct 2020 21:07:14 +0100
> Subject: platform: avoid hard coded temp dir path in vlc_memfd()
>
> on posix/linux the $TMPDIR path should be preferred, using "/tmp" as a
> fallback. here we use our new config_GetTempPath() helper.
>
> diff --git a/src/linux/filesystem.c b/src/linux/filesystem.c
> index e94a60ec45..efdff12580 100644
> --- a/src/linux/filesystem.c
> +++ b/src/linux/filesystem.c
> @@ -33,6 +33,7 @@
>
> #include <vlc_common.h>
> #include <vlc_fs.h>
> +#include <vlc_configuration.h>
>
> int vlc_memfd(void)
> {
> @@ -43,16 +44,30 @@ int vlc_memfd(void)
> return fd;
> #endif
>
> + char *tempdir = config_GetTempPath();
> + if (tempdir == NULL)
> + return -1;
> +
> /* Fallback to open with O_TMPFILE, */
> - fd = open("/tmp", O_RDWR | O_CLOEXEC | O_TMPFILE, S_IRUSR | S_IWUSR);
> - if (fd != -1 || (errno != EISDIR && errno != EOPNOTSUPP))
> + fd = open(tempdir, O_RDWR | O_CLOEXEC | O_TMPFILE, S_IRUSR | S_IWUSR);
> + if (fd != -1 || (errno != EISDIR && errno != EOPNOTSUPP)) {
> + free(tempdir);
> return fd;
> + }
>
> /* Fallback to POSIX implementation if O_TMPFILE is not supported
> (errno is * EISDIR, or EOPNOTSUPP, cf. man open(2). */
> - char bufpath[] = "/tmp/"PACKAGE_NAME"XXXXXX";
> + const char *filetemplate = PACKAGE_NAME"XXXXXX";
> + char *bufpath;
> + if (asprintf(&bufpath, "%s/%s", tempdir, filetemplate) == -1) {
> + free(tempdir);
> + return -1;
> + }
> + free(tempdir);
> +
> fd = vlc_mkstemp(bufpath);
> if (fd != -1)
> unlink(bufpath);
> + free(bufpath);
> return fd;
> }
> diff --git a/src/posix/filesystem.c b/src/posix/filesystem.c
> index 60a997d9e0..1cf5df224d 100644
> --- a/src/posix/filesystem.c
> +++ b/src/posix/filesystem.c
> @@ -44,6 +44,7 @@
>
> #include <vlc_common.h>
> #include <vlc_fs.h>
> +#include <vlc_configuration.h>
>
> #if !defined(HAVE_ACCEPT4)
> static inline void vlc_cloexec(int fd)
> @@ -90,12 +91,22 @@ int vlc_mkstemp (char *template)
>
> VLC_WEAK int vlc_memfd(void)
> {
> - char bufpath[] = "/tmp/"PACKAGE_NAME"XXXXXX";
> - int fd;
> + char *tempdir = config_GetTempPath();
> + if (tempdir == NULL)
> + return -1;
You're adding an error case for no good reasons here.
You could just as well call getenv() directly or follow the config_GetSysPath()
pattern. Either way, it saves one strdup().
> +
> + const char *filetemplate = PACKAGE_NAME"XXXXXX";
> + char *bufpath;
> + if (asprintf(&bufpath, "%s/%s", tempdir, filetemplate) == -1) {
Use string concatenation.
> + free(tempdir);
> + return -1;
> + }
> + free(tempdir);
>
> - fd = vlc_mkstemp (bufpath);
> + int fd = vlc_mkstemp(bufpath);
> if (fd != -1)
> unlink (bufpath);
> + free(bufpath);
> return fd;
> }
>
>
> _______________________________________________
> vlc-devel mailing list
> To unsubscribe or modify your subscription options:
> https://mailman.videolan.org/listinfo/vlc-devel
--
Реми Дёни-Курмон
http://www.remlab.net/
More information about the vlc-devel
mailing list