Skip to content

Commit a8d4fc3

Browse files
committed
lib: add xbps_pkg_{path,url,path_or_url} functions
1 parent 96db706 commit a8d4fc3

File tree

2 files changed

+122
-1
lines changed

2 files changed

+122
-1
lines changed

include/xbps.h.in

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2037,7 +2037,7 @@ bool xbps_remote_binpkg_exists(struct xbps_handle *xhp, xbps_dictionary_t pkgd);
20372037
*/
20382038
bool xbps_repository_is_remote(const char *uri);
20392039

2040-
/*
2040+
/**
20412041
* Returns an allocated string with the full path to the binary package
20422042
* matching \a pkgd.
20432043
*
@@ -2054,6 +2054,59 @@ bool xbps_repository_is_remote(const char *uri);
20542054
*/
20552055
char *xbps_repository_pkg_path(struct xbps_handle *xhp, xbps_dictionary_t pkgd);
20562056

2057+
/**
2058+
* Put the path to the binary package \a pkgd into \a dst.
2059+
*
2060+
* The \a pkgd dictionary must contain the following objects:
2061+
* - architecture (string)
2062+
* - pkgver (string)
2063+
* - repository (string)
2064+
*
2065+
* @param[in] xhp Pointer to an xbps_handle struct.
2066+
* @param[in] dst Destination buffer.
2067+
* @param[in] dstsz Destination buffer size.
2068+
* @param[in] pkgd Package dictionary.
2069+
*
2070+
* @return The length of the path or a negative errno on error.
2071+
* @retval -EINVAL Missing required dictionary entry.
2072+
* @retval -ENOBUFS The path would exceed the supplied \a dst buffer.
2073+
*/
2074+
ssize_t xbps_pkg_path(struct xbps_handle *xhp, char *dst, size_t dstsz, xbps_dictionary_t pkgd);
2075+
2076+
/**
2077+
* Put the url to the binary package \a pkgd into \a dst.
2078+
*
2079+
* The \a pkgd dictionary must contain the following objects:
2080+
* - architecture (string)
2081+
* - pkgver (string)
2082+
* - repository (string)
2083+
*
2084+
* @param[in] xhp The pointer to an xbps_handle struct.
2085+
* @param[in] pkgd The package dictionary to match.
2086+
*
2087+
* @return The length of the path or a negative errno on error.
2088+
* @retval -EINVAL Missing required dictionary entry.
2089+
* @retval -ENOBUFS The path would exceed the supplied \a dst buffer.
2090+
*/
2091+
ssize_t xbps_pkg_url(struct xbps_handle *xhp, char *dst, size_t dstsz, xbps_dictionary_t pkgd);
2092+
2093+
/**
2094+
* Put the url or path (if cached) to the binary package \a pkgd into \a dst.
2095+
*
2096+
* The \a pkgd dictionary must contain the following objects:
2097+
* - architecture (string)
2098+
* - pkgver (string)
2099+
* - repository (string)
2100+
*
2101+
* @param[in] xhp The pointer to an xbps_handle struct.
2102+
* @param[in] pkgd The package dictionary to match.
2103+
*
2104+
* @return The length of the path or a negative errno on error.
2105+
* @retval -EINVAL Missing required dictionary entry.
2106+
* @retval -ENOBUFS The path would exceed the supplied \a dst buffer.
2107+
*/
2108+
ssize_t xbps_pkg_path_or_url(struct xbps_handle *xhp, char *dst, size_t dstsz, xbps_dictionary_t pkgd);
2109+
20572110
/**
20582111
* Gets the name of a package string. Package strings are composed
20592112
* by a @<pkgname@>/@<version@> pair and separated by the *minus*

lib/util.c

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,74 @@ xbps_pkgpattern_version(const char *pkg)
327327
return strpbrk(pkg, "><*?[]");
328328
}
329329

330+
ssize_t
331+
xbps_pkg_path(struct xbps_handle *xhp, char *dst, size_t dstsz, xbps_dictionary_t pkgd)
332+
{
333+
const char *pkgver = NULL, *arch = NULL, *repoloc = NULL;
334+
int l;
335+
336+
if (!xbps_dictionary_get_cstring_nocopy(pkgd, "pkgver", &pkgver) ||
337+
!xbps_dictionary_get_cstring_nocopy(pkgd, "architecture", &arch) ||
338+
!xbps_dictionary_get_cstring_nocopy(pkgd, "repository", &repoloc))
339+
return -EINVAL;
340+
341+
if (xbps_repository_is_remote(repoloc))
342+
repoloc = xhp->cachedir;
343+
344+
l = snprintf(dst, dstsz, "%s/%s.%s.xbps", repoloc, pkgver, arch);
345+
if (l < 0 || (size_t)l >= dstsz)
346+
return -ENOBUFS;
347+
348+
return l;
349+
}
350+
351+
ssize_t
352+
xbps_pkg_url(struct xbps_handle *xhp UNUSED, char *dst, size_t dstsz, xbps_dictionary_t pkgd)
353+
{
354+
const char *pkgver = NULL, *arch = NULL, *repoloc = NULL;
355+
int l;
356+
357+
if (!xbps_dictionary_get_cstring_nocopy(pkgd, "pkgver", &pkgver) ||
358+
!xbps_dictionary_get_cstring_nocopy(pkgd, "architecture", &arch) ||
359+
!xbps_dictionary_get_cstring_nocopy(pkgd, "repository", &repoloc))
360+
return -EINVAL;
361+
362+
l = snprintf(dst, dstsz, "%s/%s.%s.xbps", repoloc, pkgver, arch);
363+
if (l < 0 || (size_t)l >= dstsz)
364+
return -ENOBUFS;
365+
366+
return l;
367+
}
368+
369+
ssize_t
370+
xbps_pkg_path_or_url(struct xbps_handle *xhp UNUSED, char *dst, size_t dstsz, xbps_dictionary_t pkgd)
371+
{
372+
const char *pkgver = NULL, *arch = NULL, *repoloc = NULL;
373+
int l;
374+
375+
if (!xbps_dictionary_get_cstring_nocopy(pkgd, "pkgver", &pkgver) ||
376+
!xbps_dictionary_get_cstring_nocopy(pkgd, "architecture", &arch) ||
377+
!xbps_dictionary_get_cstring_nocopy(pkgd, "repository", &repoloc))
378+
return -EINVAL;
379+
380+
if (xbps_repository_is_remote(repoloc)) {
381+
l = snprintf(dst, dstsz, "%s/%s.%s.xbps", xhp->cachedir,
382+
pkgver, arch);
383+
if (l < 0 || (size_t)l >= dstsz)
384+
return -ENOBUFS;
385+
if (access(dst, R_OK) == 0)
386+
return l;
387+
if (errno != ENOENT)
388+
return -errno;
389+
}
390+
391+
l = snprintf(dst, dstsz, "%s/%s.%s.xbps", repoloc, pkgver, arch);
392+
if (l < 0 || (size_t)l >= dstsz)
393+
return -ENOBUFS;
394+
395+
return l;
396+
}
397+
330398
char *
331399
xbps_repository_pkg_path(struct xbps_handle *xhp, xbps_dictionary_t pkg_repod)
332400
{

0 commit comments

Comments
 (0)