[vlc-devel] [PATCH v2 7/13] core: add vlc_MakeTmpFile() helper
Steve Lhomme
robux4 at ycbcr.xyz
Tue Oct 6 09:14:20 CEST 2020
On 2020-10-06 7:59, Lyndon Brown wrote:
> From: Lyndon Brown <jnqnfe at gmail.com>
> Date: Tue, 6 Oct 2020 01:08:32 +0100
> Subject: core: add vlc_MakeTmpFile() helper
>
> to avoid messy and duplicated code creating temporary files.
>
> diff --git a/include/vlc_fs.h b/include/vlc_fs.h
> index f3f50ea431..50ec52c299 100644
> --- a/include/vlc_fs.h
> +++ b/include/vlc_fs.h
> @@ -99,6 +99,21 @@ VLC_API int vlc_openat(int fd, const char *filename, int flags, ...) VLC_USED;
>
> VLC_API int vlc_mkstemp( char * );
>
> +/**
> + * Temporary file creation helper
> + *
> + * Creates a unique temporary file, using config_GetTempPath() to determine the
> + * directory in which to create it, and using vlc_mkstemp() for the creation.
> + *
> + * Note, the returned `path` pointer must be free'd with free(), except when
> + * the returned file descriptor is -1 indicating error, where is it undefined.
> + *
> + * @param path [OUT] full path of the created file.
> + * @param filetemplate filename template, which must end with a sequence of 'XXXXXX'.
It seems vlc_mkstemp expects exactly "XXXXXX" so a define could be
useful. String concatenation can be used with the string the user wants.
> + * @return file descriptor, or -1 on error.
> + */
> +VLC_API int vlc_MakeTmpFile( char **path, const char *filetemplate ) VLC_USED;
> +
> /**
> * Duplicates a file descriptor. The new file descriptor has the close-on-exec
> * descriptor flag preset.
> diff --git a/src/libvlccore.sym b/src/libvlccore.sym
> index 028891b3c9..d783c3546c 100644
> --- a/src/libvlccore.sym
> +++ b/src/libvlccore.sym
> @@ -432,6 +432,7 @@ vlc_loaddir
> vlc_lstat
> vlc_mkdir
> vlc_mkstemp
> +vlc_MakeTmpFile
> vlc_open
> vlc_openat
> vlc_memfd
> diff --git a/src/text/filesystem.c b/src/text/filesystem.c
> index 2f3b6470c8..fa518f6b6e 100644
> --- a/src/text/filesystem.c
> +++ b/src/text/filesystem.c
> @@ -30,6 +30,7 @@
>
> #include <vlc_common.h>
> #include <vlc_fs.h>
> +#include <vlc_configuration.h>
> #include <vlc_sort.h>
>
> #include <assert.h>
> @@ -242,3 +243,21 @@ VLC_WEAK int vlc_mkstemp(char *template)
> errno = EEXIST;
> return -1;
> }
> +
> +int vlc_MakeTmpFile( char **path, const char *filetemplate )
> +{
> + char *tmpdir = config_GetTempPath();
> + if (tmpdir == NULL)
> + return -1;
> +
> + if (asprintf(path, "%s"DIR_SEP"%s", tmpdir, filetemplate) < 0) {
Altenatively you can add the "XXXXXX" here and not impose it on the
caller at all.
> + free(tmpdir);
> + return -1;
> + }
> + free(tmpdir);
> +
> + int fd = vlc_mkstemp(*path);
> + if (fd == -1)
> + free(*path);
> + return fd;
> +}
>
> _______________________________________________
> vlc-devel mailing list
> To unsubscribe or modify your subscription options:
> https://mailman.videolan.org/listinfo/vlc-devel
>
More information about the vlc-devel
mailing list