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

Message ID 20200629173251.2093-1-anatol.pomozov@gmail.com
State Accepted, archived
Headers show
Series [pacman-dev] Add a utility function to check whether a file exists in the cache | expand

Commit Message

Anatol Pomozov June 29, 2020, 5:32 p.m. UTC
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@gmail.com>
---
 lib/libalpm/util.c | 14 ++++++++++++++
 lib/libalpm/util.h |  2 ++
 2 files changed, 16 insertions(+)

Patch

diff --git a/lib/libalpm/util.c b/lib/libalpm/util.c
index ead03004..76728eb4 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 != NULL);
+	FREE(fpath);
+	return res;
+}
+
 /** Check the alpm cachedirs for existence and find a writable one.
  * If no valid cache directory can be found, use /tmp.
  * @param handle the context handle
diff --git a/lib/libalpm/util.h b/lib/libalpm/util.h
index 3306a022..4fc6e718 100644
--- a/lib/libalpm/util.h
+++ b/lib/libalpm/util.h
@@ -136,6 +136,8 @@  int _alpm_run_chroot(alpm_handle_t *handle, const char *cmd, char *const argv[],
 int _alpm_ldconfig(alpm_handle_t *handle);
 int _alpm_str_cmp(const void *s1, const void *s2);
 char *_alpm_filecache_find(alpm_handle_t *handle, const char *filename);
+/* Checks whether a file exists in cache */
+int _alpm_filecache_exists(alpm_handle_t *handle, const char *filename);
 const char *_alpm_filecache_setup(alpm_handle_t *handle);
 /* Unlike many uses of alpm_pkgvalidation_t, _alpm_test_checksum expects
  * an enum value rather than a bitfield. */