[pacman-dev] [PATCH] Add a utility function to check whether a file exists in the cache

Allan McRae allan at archlinux.org
Mon Apr 27 01:07:20 UTC 2020


On 24/4/20 2:41 pm, Anatol Pomozov wrote:
> It is similar to _alpm_filecache_find() but does not return a
> dynamically allocated memory to user. Thus the user does not need to
> free this resource.
> 
> Signed-off-by: Anatol Pomozov <anatol.pomozov at gmail.com>
> ---
>  lib/libalpm/util.c | 14 ++++++++++++++
>  lib/libalpm/util.h |  2 ++
>  2 files changed, 16 insertions(+)
> 
> diff --git a/lib/libalpm/util.c b/lib/libalpm/util.c
> index cb838e43..6307dc14 100644
> --- a/lib/libalpm/util.c
> +++ b/lib/libalpm/util.c
> @@ -834,6 +834,20 @@ char *_alpm_filecache_find(alpm_handle_t *handle, const char *filename)
>  	return NULL;
>  }
>  
> +/** Check whether a filename exists in a registered alpm cachedir.
> + * @param handle the context handle
> + * @param filename name of file to find
> + * @return 0 if the filename was not found, 1 otherwise
> + */
> +int _alpm_filecache_exists(alpm_handle_t *handle, const char *filename)
> +{
> +	int res;
> +	char *fpath = _alpm_filecache_find(handle, filename);
> +	res = !!fpath;

Please adjust:

res = (fpath != NULL)

This follows from our style guideline (although not explicitly stated)

6.  When using strcmp() (or any function that returns 0 on success) in a
    conditional statement, use != 0 or == 0 and not the negation (!)
    operator.


I'm also assuming that this function will not be used in such a way that
_alpm_filecache_find will be called again if it exists...

Allan


More information about the pacman-dev mailing list