From patchwork Sun Jan 9 17:04:17 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: morganamilo X-Patchwork-Id: 2013 Return-Path: Delivered-To: patchwork@archlinux.org Received: from mail.archlinux.org [95.216.189.61] by patchwork.archlinux.org with IMAP (fetchmail-6.4.26) for (single-drop); Sun, 09 Jan 2022 17:04:24 +0000 (UTC) Received: from mail.archlinux.org by mail.archlinux.org with LMTP id iEMUJZcV22EmEgAAK+/4rw (envelope-from ) for ; Sun, 09 Jan 2022 17:04:23 +0000 Received: from lists.archlinux.org (lists.archlinux.org [IPv6:2a01:4f9:c010:9eb4::1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mail.archlinux.org (Postfix) with ESMTPS id 5AC84C0AA5C; Sun, 9 Jan 2022 17:04:22 +0000 (UTC) Received: from lists.archlinux.org (localhost [IPv6:::1]) by lists.archlinux.org (Postfix) with ESMTP id 28037B92168; Sun, 9 Jan 2022 17:04:22 +0000 (UTC) X-Original-To: pacman-dev@lists.archlinux.org Delivered-To: pacman-dev@lists.archlinux.org Received: from mail.archlinux.org (mail.archlinux.org [95.216.189.61]) by lists.archlinux.org (Postfix) with ESMTPS id A1673B92160 for ; Sun, 9 Jan 2022 17:04:20 +0000 (UTC) From: morganamilo DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=archlinux.org; s=dkim-ed25519; t=1641747860; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=Vzb7eciWp5xHXh4DxD/C7nHZ3d6+mLo03Q8ijj9r+c0=; b=Kg86H2SrzLRYdw1zrtG2LHOO6+hJfv8jRoxkTAb0m188M3ATx1jPdHdxs0BAWU3dalpUfG JXJPoInfMayslnBQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=archlinux.org; s=dkim-rsa; t=1641747860; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=Vzb7eciWp5xHXh4DxD/C7nHZ3d6+mLo03Q8ijj9r+c0=; b=GebyOMrkmLQak+kgtvInAAXeuojqbaJFmBvjxnq1mKYl2Uc4693EW2syngmmk4MD41ljOH +5y5xunm4dA64mLVBuH+0PIVlVogRKLOHZw8clvC/PjkiiXU5oywWQ8H8WQ66LecoX8uTc BWFVmq7fRcMEgvBjwvw/iHRAVtJ3KRn/7+gheqiT/jBXBXjIILutqr6M5M5S/5W5GSOFKL 3JTH3nH4Kx8YLueAetDSS0iAmbNhXOsq+ncnCxOwrdJn5Akajnn7ju72lsUOhr8gHCqV97 kTWc11BTHgabuicp3k8A/e1oz3EQf+x+l3hT0aiGZNU/1uNr9AJOIqfO8CRZdzZOwbXniV nLvpwVWqXp1rHoy/NiBJL9tSohAxA1chg484De5b+fRJQ9HNXZk9E/vGO2SBlmk79zDhTw ltMM2Pkc8wpe3odd0IU4KsJUqHWBMMJtILRqRfHYF3qGlAttQFy0Mx7rFa+W/BsYQeIRIn J5+PxuAAuzxLM4Ot9+i4R0yBjmMlXsRPzVGwuJm3+8XpYkVzelD9ID4m6rlHRw1vSOhTDj 5fATqripkPgFgqP18xkdQP6z6ETUdECyzH4OhL1lrh+/YOo3F7F3ziJ6yf96o+IdWsqYu7 NBPCxJY3dSXVKnGGpflTJD7N++CFTIR9NQ1gnX+VFIzpyWrR+ALE0= To: pacman-dev@lists.archlinux.org Cc: morganamilo Subject: [PATCH] libalpm: don't download files from local servers Date: Sun, 9 Jan 2022 17:04:17 +0000 Message-Id: <20220109170417.405098-1-morganamilo@archlinux.org> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-BeenThere: pacman-dev@lists.archlinux.org X-Mailman-Version: 2.1.39 Precedence: list List-Id: Discussion list for pacman development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: pacman-dev-bounces@lists.archlinux.org Sender: "pacman-dev" Authentication-Results: mail.archlinux.org; dkim=pass header.d=archlinux.org header.s=dkim-ed25519 header.b=Kg86H2Sr; dkim=pass header.d=archlinux.org header.s=dkim-rsa header.b=GebyOMrk; dmarc=pass (policy=none) header.from=archlinux.org; spf=pass (mail.archlinux.org: domain of pacman-dev-bounces@lists.archlinux.org designates 2a01:4f9:c010:9eb4::1 as permitted sender) smtp.mailfrom=pacman-dev-bounces@lists.archlinux.org X-Rspamd-Queue-Id: 5AC84C0AA5C X-Spamd-Result: default: False [-2.61 / 15.00]; DWL_DNSWL_MED(-2.00)[archlinux.org:dkim]; MID_CONTAINS_FROM(1.00)[]; R_MISSING_CHARSET(0.50)[]; RCVD_DKIM_ARC_DNSWL_MED(-0.50)[]; DMARC_POLICY_ALLOW(-0.50)[archlinux.org,none]; RCVD_IN_DNSWL_MED(-0.40)[2a01:4f9:c010:9eb4::1:from,95.216.189.61:received]; MAILLIST(-0.20)[mailman]; R_SPF_ALLOW(-0.20)[+ip6:2a01:4f9:c010:9eb4::1:c]; R_DKIM_ALLOW(-0.20)[archlinux.org:s=dkim-ed25519,archlinux.org:s=dkim-rsa]; MIME_GOOD(-0.10)[text/plain]; HAS_LIST_UNSUB(-0.01)[]; MIME_TRACE(0.00)[0:+]; ASN(0.00)[asn:24940, ipnet:2a01:4f9::/32, country:DE]; RCVD_TLS_LAST(0.00)[]; ARC_NA(0.00)[]; RCVD_COUNT_THREE(0.00)[3]; DKIM_TRACE(0.00)[archlinux.org:+]; FROM_HAS_DN(0.00)[]; NEURAL_HAM(-0.00)[-0.999]; RCPT_COUNT_TWO(0.00)[2]; TO_DN_SOME(0.00)[]; FROM_NEQ_ENVFROM(0.00)[morganamilo@archlinux.org,pacman-dev-bounces@lists.archlinux.org]; PREVIOUSLY_DELIVERED(0.00)[pacman-dev@lists.archlinux.org]; FORGED_SENDER_MAILLIST(0.00)[] X-Rspamd-Server: mail.archlinux.org This causes file:// servers to be treated as if they were cache dirs when checking if a package needs to be downloaded/read. This stops packages being duplicated any time pacman "downloads" a package from a local repository. --- lib/libalpm/package.c | 4 ++-- lib/libalpm/sync.c | 20 ++++++-------------- lib/libalpm/util.c | 35 +++++++++++++++++++++++++++++++++-- lib/libalpm/util.h | 3 ++- 4 files changed, 43 insertions(+), 19 deletions(-) diff --git a/lib/libalpm/package.c b/lib/libalpm/package.c index f837f84a..f805edfa 100644 --- a/lib/libalpm/package.c +++ b/lib/libalpm/package.c @@ -57,7 +57,7 @@ int SYMEXPORT alpm_pkg_checkmd5sum(alpm_pkg_t *pkg) ASSERT(pkg->origin == ALPM_PKG_FROM_SYNCDB, RET_ERR(pkg->handle, ALPM_ERR_WRONG_ARGS, -1)); - fpath = _alpm_filecache_find(pkg->handle, pkg->filename); + fpath = _alpm_cache_find_pkg(pkg, 0); retval = _alpm_test_checksum(fpath, pkg->md5sum, ALPM_PKG_VALIDATION_MD5SUM); @@ -283,7 +283,7 @@ int SYMEXPORT alpm_pkg_get_sig(alpm_pkg_t *pkg, unsigned char **sig, size_t *sig alpm_errno_t err; int ret = -1; - pkgpath = _alpm_filecache_find(pkg->handle, pkg->filename); + pkgpath = _alpm_cache_find_pkg(pkg, 0); if(!pkgpath) { GOTO_ERR(pkg->handle, ALPM_ERR_PKG_NOT_FOUND, cleanup); } diff --git a/lib/libalpm/sync.c b/lib/libalpm/sync.c index 36ad6242..f89d5c0d 100644 --- a/lib/libalpm/sync.c +++ b/lib/libalpm/sync.c @@ -323,7 +323,7 @@ static int compute_download_size(alpm_pkg_t *newpkg) ASSERT(newpkg->filename != NULL, RET_ERR(handle, ALPM_ERR_PKG_INVALID_NAME, -1)); fname = newpkg->filename; - fpath = _alpm_filecache_find(handle, fname); + fpath = _alpm_cache_find_pkg(newpkg, 0); /* downloaded file exists, so there's nothing to grab */ if(fpath) { @@ -333,7 +333,7 @@ static int compute_download_size(alpm_pkg_t *newpkg) CALLOC(fnamepart, strlen(fname) + 6, sizeof(char), return -1); sprintf(fnamepart, "%s.part", fname); - fpath = _alpm_filecache_find(handle, fnamepart); + fpath = _alpm_cache_find_pkg(newpkg, 1); if(fpath) { struct stat st; if(stat(fpath, &st) == 0) { @@ -737,21 +737,13 @@ static int find_dl_candidates(alpm_handle_t *handle, alpm_list_t **files) ASSERT(spkg->filename != NULL, RET_ERR(handle, ALPM_ERR_PKG_INVALID_NAME, -1)); - need_download = spkg->download_size != 0 || !_alpm_filecache_exists(handle, spkg->filename); + need_download = spkg->download_size != 0 || !_alpm_cache_pkg_exists(spkg, 0); /* even if the package file in the cache we need to check for * accompanion *.sig file as well. * If *.sig is not cached then force download the package + its signature file. */ if(!need_download && (siglevel & ALPM_SIG_PACKAGE)) { - char *sig_filename = NULL; - int len = strlen(spkg->filename) + 5; - - MALLOC(sig_filename, len, RET_ERR(handle, ALPM_ERR_MEMORY, -1)); - snprintf(sig_filename, len, "%s.sig", spkg->filename); - - need_download = !_alpm_filecache_exists(handle, sig_filename); - - FREE(sig_filename); + need_download = !_alpm_cache_pkg_exists(spkg, 1); } if(need_download) { @@ -990,7 +982,7 @@ static int check_validity(alpm_handle_t *handle, } current_bytes += v.pkg->size; - v.path = _alpm_filecache_find(handle, v.pkg->filename); + v.path = _alpm_cache_find_pkg(v.pkg, 0); v.siglevel = alpm_db_get_siglevel(alpm_pkg_get_db(v.pkg)); if(_alpm_pkg_validate_internal(handle, v.path, v.pkg, @@ -1080,7 +1072,7 @@ static int load_packages(alpm_handle_t *handle, alpm_list_t **data, } current_bytes += spkg->size; - filepath = _alpm_filecache_find(handle, spkg->filename); + filepath = _alpm_cache_find_pkg(spkg, 0); /* load the package file and replace pkgcache entry with it in the target list */ /* TODO: alpm_pkg_get_db() will not work on this target anymore */ diff --git a/lib/libalpm/util.c b/lib/libalpm/util.c index 299d287e..f871dce0 100644 --- a/lib/libalpm/util.c +++ b/lib/libalpm/util.c @@ -815,6 +815,37 @@ int _alpm_str_cmp(const void *s1, const void *s2) return strcmp(s1, s2); } +char *_alpm_cache_find_pkg(alpm_pkg_t *pkg, int sig) { + alpm_handle_t *handle = pkg->handle; + struct stat buf; + alpm_list_t *servers = pkg->origin_data.db->servers; + char *retpath; + char filepath[PATH_MAX]; + + for(alpm_list_t *j = servers; j; j = j->next) { + char *server = j->data; + + if(strncmp("file://", server, strlen("file://")) == 0) { + int len = strlen(server) - strlen("file://") + 1 + strlen(pkg->filename) + 1; + + if(sig) { + len += strlen(".sig"); + snprintf(filepath, len, "%s/%s", server + strlen("file://"), pkg->filename); + } else { + snprintf(filepath, len, "%s/%s.sig", server + strlen("file://"), pkg->filename); + } + + if(stat(filepath, &buf) == 0 && S_ISREG(buf.st_mode)) { + STRDUP(retpath, filepath, RET_ERR(handle, ALPM_ERR_MEMORY, NULL)); + _alpm_log(handle, ALPM_LOG_DEBUG, "found pkg in repo cache: %s\n", retpath); + return retpath; + } + } + } + + return _alpm_filecache_find(handle, pkg->filename); +} + /** Find a filename in a registered alpm cachedir. * @param handle the context handle * @param filename name of file to find @@ -846,10 +877,10 @@ char *_alpm_filecache_find(alpm_handle_t *handle, const char *filename) * @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 _alpm_cache_pkg_exists(alpm_pkg_t *pkg, int sig) { int res; - char *fpath = _alpm_filecache_find(handle, filename); + char *fpath = _alpm_cache_find_pkg(pkg, sig); res = (fpath != NULL); FREE(fpath); return res; diff --git a/lib/libalpm/util.h b/lib/libalpm/util.h index b7297f81..5c1febef 100644 --- a/lib/libalpm/util.h +++ b/lib/libalpm/util.h @@ -133,9 +133,10 @@ int _alpm_run_chroot(alpm_handle_t *handle, const char *cmd, char *const argv[], _alpm_cb_io in_cb, void *in_ctx); int _alpm_ldconfig(alpm_handle_t *handle); int _alpm_str_cmp(const void *s1, const void *s2); +char *_alpm_cache_find_pkg(alpm_pkg_t *pkg, int sig); 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); +int _alpm_cache_pkg_exists(alpm_pkg_t *pkg, int sig); 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. */