From ebad1996149d72cf4a1fe15fe8326f2b4996f585 Mon Sep 17 00:00:00 2001 From: Aaron Griffin Date: Fri, 18 May 2007 01:24:59 -0500 Subject: Allow $repo expansion in 'Server' config lines Small change (addition of a 'strreplace' function) which replaces any $repo tokens found in a server line with the name of the repo or section being processed. While this is more simplistic than suggestions on flyspray, it works and I think it is cleaner. Merits can be discussed further. Ref: FS#6389 Signed-off-by: Aaron Griffin --- lib/libalpm/alpm.c | 26 ++++++++++++--------- lib/libalpm/util.c | 67 +++++++++++++++++++++++++++++++++++++++++++++--------- lib/libalpm/util.h | 1 + 3 files changed, 72 insertions(+), 22 deletions(-) (limited to 'lib') diff --git a/lib/libalpm/alpm.c b/lib/libalpm/alpm.c index 4239430e..cc7ef32d 100644 --- a/lib/libalpm/alpm.c +++ b/lib/libalpm/alpm.c @@ -224,7 +224,7 @@ int alpm_db_setserver(pmdb_t *db, const char *url) } db->servers = alpm_list_add(db->servers, server); _alpm_log(PM_LOG_DEBUG, _("adding new server to database '%s': protocol '%s', server '%s', path '%s'"), - db->treename, server->s_url->scheme, server->s_url->host, server->s_url->doc); + db->treename, server->s_url->scheme, server->s_url->host, server->s_url->doc); } else { FREELIST(db->servers); _alpm_log(PM_LOG_DEBUG, _("serverlist flushed for '%s'"), db->treename); @@ -467,8 +467,8 @@ int alpm_pkg_checksha1sum(pmpkg_t *pkg) ASSERT(pkg->data != handle->db_local, RET_ERR(PM_ERR_PKG_INVALID, -1)); snprintf(path, PATH_MAX, "%s%s/%s-%s" PM_EXT_PKG, - handle->root, handle->cachedir, - alpm_pkg_get_name(pkg), alpm_pkg_get_version(pkg)); + handle->root, handle->cachedir, + alpm_pkg_get_name(pkg), alpm_pkg_get_version(pkg)); sha1sum = _alpm_SHAFile(path); if(sha1sum == NULL) { @@ -511,8 +511,8 @@ int alpm_pkg_checkmd5sum(pmpkg_t *pkg) ASSERT(pkg->data != handle->db_local, RET_ERR(PM_ERR_PKG_INVALID, -1)); snprintf(path, PATH_MAX, "%s%s/%s-%s" PM_EXT_PKG, - handle->root, handle->cachedir, - alpm_pkg_get_name(pkg), alpm_pkg_get_version(pkg)); + handle->root, handle->cachedir, + alpm_pkg_get_name(pkg), alpm_pkg_get_version(pkg)); md5sum = _alpm_MDFile(path); if(md5sum == NULL) { @@ -545,7 +545,7 @@ int alpm_pkg_checkmd5sum(pmpkg_t *pkg) */ int SYMEXPORT alpm_pkg_vercmp(const char *ver1, const char *ver2) { - ALPM_LOG_FUNC; + ALPM_LOG_FUNC; return(_alpm_versioncmp(ver1, ver2)); } @@ -636,8 +636,8 @@ alpm_list_t SYMEXPORT *alpm_db_search(pmdb_t *db, alpm_list_t* needles) * @return 0 on success, -1 on error (pm_errno is set accordingly) */ int SYMEXPORT alpm_trans_init(pmtranstype_t type, pmtransflag_t flags, - alpm_trans_cb_event event, alpm_trans_cb_conv conv, - alpm_trans_cb_progress progress) + alpm_trans_cb_event event, alpm_trans_cb_conv conv, + alpm_trans_cb_progress progress) { char path[PATH_MAX]; @@ -1059,11 +1059,15 @@ int SYMEXPORT alpm_parse_config(char *file, alpm_cb_db_register callback, const } } else { if(strcmp(origkey, "Server") == 0 || strcmp(key, "SERVER") == 0) { - /* add to the list */ - if(alpm_db_setserver(db, ptr) != 0) { + /* let's attempt a replacement for the current repo */ + char *server = _alpm_strreplace(ptr, "$repo", section); + + if(alpm_db_setserver(db, server) != 0) { /* pm_errno is set by alpm_db_setserver */ return(-1); } + + free(server); } else { RET_ERR(PM_ERR_CONF_BAD_SYNTAX, -1); } @@ -1100,7 +1104,7 @@ alpm_list_t SYMEXPORT *alpm_get_upgrades() for(m = _alpm_db_get_pkgcache(handle->db_local); m; m = m->next) { pmpkg_t *lpkg = m->data; - + if(strcmp(k->data, alpm_pkg_get_name(lpkg)) == 0) { _alpm_log(PM_LOG_DEBUG, _("checking replacement '%s' for package '%s'"), k->data, alpm_pkg_get_name(spkg)); diff --git a/lib/libalpm/util.c b/lib/libalpm/util.c index 15e69b79..daefa391 100644 --- a/lib/libalpm/util.c +++ b/lib/libalpm/util.c @@ -161,7 +161,7 @@ int _alpm_copyfile(const char *src, const char *dest) } /* Convert a string to uppercase - */ +*/ char *_alpm_strtoupper(char *str) { char *ptr = str; @@ -174,7 +174,7 @@ char *_alpm_strtoupper(char *str) } /* Trim whitespace and newlines from a string - */ +*/ char *_alpm_strtrim(char *str) { char *pch = str; @@ -205,8 +205,53 @@ char *_alpm_strtrim(char *str) return(str); } +/* Helper function for _alpm_strreplace */ +static void _strnadd(char **str, const char *append, unsigned int count) +{ + if(*str) { + *str = realloc(*str, strlen(*str) + count + 1); + } else { + *str = calloc(sizeof(char), count + 1); + } + + strncat(*str, append, count); +} + +/* Replace all occurances of 'needle' with 'replace' in 'str', returning + * a new string (must be free'd) */ +char *_alpm_strreplace(const char *str, const char *needle, const char *replace) +{ + const char *p, *q; + p = q = str; + + char *newstr = NULL; + unsigned int needlesz = strlen(needle), + replacesz = strlen(replace); + + while (1) { + q = strstr(p, needle); + if(!q) { /* not found */ + if(*p) { + /* add the rest of 'p' */ + _strnadd(&newstr, p, strlen(p)); + } + break; + } else { /* found match */ + if(q > p){ + /* add chars between this occurance and last occurance, if any */ + _strnadd(&newstr, p, q - p); + } + _strnadd(&newstr, replace, replacesz); + p = q + needlesz; + } + } + + return newstr; +} + + /* Create a lock file - */ +*/ int _alpm_lckmk(const char *file) { int fd, count = 0; @@ -234,7 +279,7 @@ int _alpm_lckmk(const char *file) } /* Remove a lock file - */ +*/ int _alpm_lckrm(const char *file) { if(unlink(file) == -1 && errno != ENOENT) { @@ -244,7 +289,7 @@ int _alpm_lckrm(const char *file) } /* Compression functions - */ +*/ int _alpm_unpack(const char *archive, const char *prefix, const char *fn) { @@ -252,8 +297,8 @@ int _alpm_unpack(const char *archive, const char *prefix, const char *fn) struct archive_entry *entry; char expath[PATH_MAX]; const int archive_flags = ARCHIVE_EXTRACT_OWNER | - ARCHIVE_EXTRACT_PERM | - ARCHIVE_EXTRACT_TIME; + ARCHIVE_EXTRACT_PERM | + ARCHIVE_EXTRACT_TIME; ALPM_LOG_FUNC; @@ -278,14 +323,14 @@ int _alpm_unpack(const char *archive, const char *prefix, const char *fn) archive_entry_set_pathname(entry, expath); if(archive_read_extract(_archive, entry, archive_flags) != ARCHIVE_OK) { _alpm_log(PM_LOG_ERROR, _("could not extract %s: %s\n"), archive_entry_pathname(entry), archive_error_string(_archive)); - return(1); + return(1); } if(fn) { break; } } - + archive_read_finish(_archive); return(0); } @@ -349,8 +394,8 @@ int _alpm_logaction(unsigned short usesyslog, FILE *f, const char *str) /* Use ISO-8601 date format */ fprintf(f, "[%04d-%02d-%02d %02d:%02d] %s\n", - tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday, - tm->tm_hour, tm->tm_min, str); + tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday, + tm->tm_hour, tm->tm_min, str); fflush(f); } diff --git a/lib/libalpm/util.h b/lib/libalpm/util.h index b98d8a7a..a1551273 100644 --- a/lib/libalpm/util.h +++ b/lib/libalpm/util.h @@ -48,6 +48,7 @@ int _alpm_makepath(const char *path); int _alpm_copyfile(const char *src, const char *dest); char *_alpm_strtoupper(char *str); char *_alpm_strtrim(char *str); +char *_alpm_strreplace(const char *str, const char *needle, const char *replace); int _alpm_lckmk(const char *file); int _alpm_lckrm(const char *file); int _alpm_unpack(const char *archive, const char *prefix, const char *fn); -- cgit v1.2.3-24-g4f1b