From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from re-prd-fep-046.btinternet.com (mailomta14-re.btinternet.com [213.120.69.107]) by sourceware.org (Postfix) with ESMTPS id 9EE14385E02A for ; Fri, 8 Mar 2024 18:36:35 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 9EE14385E02A Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=dronecode.org.uk Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=dronecode.org.uk ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 9EE14385E02A Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=213.120.69.107 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1709922997; cv=none; b=Ltis2jLoA/LbmQrbu+eROA5CTV1QTDMrHuaAfT9C48UiORmCeUrlyK/rbyyIoYgQYuvfVhmjz7s9nhitzksTw+YbEJuVaXkxFvbpjfzj6FHOLOqKdhmMWRSPRGCbdeJjADqFjlrrP3P8E+qgGcQV1KyQ8rYvOuWuK8oWCvER9gM= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1709922997; c=relaxed/simple; bh=D847N5ZxYhma5NtDiTi83Ha0wBiXCXwfQN9hYGRnlBg=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=D+Q+MCw7oZmSglSxCqx/HHK61X2q6Y4neAgsp+1Q4DLuTpI06p2mV1Amz0MtTeD41Tx3GPHRpPfGdOLdTosBN+XZFk/WvHeru34VzM76uomJrqlTNNP7eDUf2t5bveKPK8cwkiDxolNlClOBU0upwpGixOdablEAQjb6IkwL8MU= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from re-prd-rgout-004.btmx-prd.synchronoss.net ([10.2.54.7]) by re-prd-fep-046.btinternet.com with ESMTP id <20240308183634.BHFQ19051.re-prd-fep-046.btinternet.com@re-prd-rgout-004.btmx-prd.synchronoss.net>; Fri, 8 Mar 2024 18:36:34 +0000 Authentication-Results: btinternet.com; none X-SNCR-Rigid: 6577B87C0AB06B99 X-Originating-IP: [81.153.98.185] X-OWM-Source-IP: 81.153.98.185 X-OWM-Env-Sender: jon.turney@dronecode.org.uk X-VadeSecure-score: verdict=clean score=0/300, class=clean X-RazorGate-Vade: gggruggvucftvghtrhhoucdtuddrgedvledrieehgdduudegucetufdoteggodetrfdotffvucfrrhhofhhilhgvmecuueftkffvkffujffvgffngfevqffopdfqfgfvnecuuegrihhlohhuthemuceftddunecunecujfgurhephffvvefufffkofgjfhgggfestdekredtredttdenucfhrhhomheplfhonhcuvfhurhhnvgihuceojhhonhdrthhurhhnvgihsegurhhonhgvtghouggvrdhorhhgrdhukheqnecuggftrfgrthhtvghrnhepleeitdejhfdtveekheeugeffgeevfedtjeejveefhfeiffefkedtvdetheehieejnecukfhppeekuddrudehfedrleekrddukeehnecuvehluhhsthgvrhfuihiivgepieenucfrrghrrghmpehhvghloheplhhotggrlhhhohhsthdrlhhotggrlhguohhmrghinhdpihhnvghtpeekuddrudehfedrleekrddukeehpdhmrghilhhfrhhomhepjhhonhdrthhurhhnvgihsegurhhonhgvtghouggvrdhorhhgrdhukhdpnhgspghrtghpthhtohepvddprhgtphhtthhopegthihgfihinhdqrghpphhssegthihgfihinhdrtghomhdprhgtphhtthhopehjohhnrdhtuhhrnhgvhiesughrohhnvggtohguvgdrohhrghdruhhkpdhrvghvkffrpehhohhsthekuddqudehfedqleekqddukeehrdhrrghnghgvkeduqdduheefrdgsthgtvghnthhrrghlphhluhhsrdgtohhmpdgruhhthhgpuhhsvghrpehjohhnthhurhhnvgihsegsthhinhhtvghrnhgvthdrtghomhdpghgvohfk rfepifeupdfovfetjfhoshhtpehrvgdqphhrugdqrhhgohhuthdqtddtge X-RazorGate-Vade-Verdict: clean 0 X-RazorGate-Vade-Classification: clean Received: from localhost.localdomain (81.153.98.185) by re-prd-rgout-004.btmx-prd.synchronoss.net (authenticated as jonturney@btinternet.com) id 6577B87C0AB06B99; Fri, 8 Mar 2024 18:36:34 +0000 From: Jon Turney To: cygwin-apps@cygwin.com Cc: Jon Turney Subject: [PATCH setup 14/16] Push check_for_cached into package_source Date: Fri, 8 Mar 2024 18:34:33 +0000 Message-ID: <20240308183440.4263-15-jon.turney@dronecode.org.uk> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240308183440.4263-1-jon.turney@dronecode.org.uk> References: <20240308183440.4263-1-jon.turney@dronecode.org.uk> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-8.8 required=5.0 tests=BAYES_00,GIT_PATCH_0,JMQ_SPF_NEUTRAL,KAM_DMARC_STATUS,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_PASS,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: This is kind of half-right. It helps make the package database code self-contained (since that needs to use check_for_cached as part of ScanDownloadedFiles), but also pulls apart the 'cache checking' and 'download file and put it in the cache'. There's probably some scope for an package_source interface for "where is the package cache download location for this package from that site." --- download.cc | 104 ++-------------------------------------------- download.h | 6 --- package_meta.cc | 3 +- package_source.cc | 95 ++++++++++++++++++++++++++++++++++++++++++ package_source.h | 4 ++ 5 files changed, 103 insertions(+), 109 deletions(-) diff --git a/download.cc b/download.cc index fbe36e5..4aba83e 100644 --- a/download.cc +++ b/download.cc @@ -19,7 +19,7 @@ #include "csu_util/rfc1738.h" #include "download.h" - + #include "win32.h" #include @@ -28,7 +28,6 @@ #include #include "resource.h" -#include "msg.h" #include "dialog.h" #include "geturl.h" #include "state.h" @@ -48,110 +47,13 @@ extern ThreeBarProgressPage Progress; -// Return true if selected checks pass, false if they don't and the -// user chooses to delete the file; otherwise throw an exception. -static bool -validateCachedPackage (const std::string& fullname, packagesource & pkgsource, - Feedback &feedback, bool check_hash, bool check_size) -{ - try - { - if (check_size) - pkgsource.check_size_and_cache (fullname); - if (check_hash) - pkgsource.check_hash (feedback); - return true; - } - catch (Exception *e) - { - pkgsource.set_cached (""); - const char *filename = fullname.c_str (); - if (strncmp (filename, "file://", 7) == 0) - filename += 7; - if (e->errNo() == APPERR_CORRUPT_PACKAGE - && yesno (feedback.owner(), IDS_QUERY_CORRUPT, filename) == IDYES) - remove (filename); - else - throw e; - } - return false; -} - -/* 0 if not cached; may throw exception if validation fails. - */ -int -check_for_cached (packagesource & pkgsource, Feedback &feedback, - bool mirror_mode, bool check_hash) -{ - /* If the packagesource doesn't have a filename, it can't possibly be in the - cache */ - if (!pkgsource.Canonical()) - { - return 0; - } - - /* Note that the cache dir is represented by a mirror site of file://local_dir */ - std::string prefix = "file://" + local_dir + "/"; - std::string fullname = prefix + pkgsource.Canonical(); - - if (mirror_mode) - { - /* Just assume correctness of mirror. */ - if (!pkgsource.Cached()) - pkgsource.set_cached (fullname); - return 1; - } - - // Already found one, which we can assume to have the right size. - if (pkgsource.Cached()) - { - if (validateCachedPackage (pkgsource.Cached(), pkgsource, feedback, - check_hash, false)) - return 1; - // If we get here, pkgsource.Cached() was corrupt and deleted. - pkgsource.set_cached (""); - } - - /* - 1) is there a legacy version in the cache dir available. - */ - if (io_stream::exists (fullname)) - { - if (validateCachedPackage (fullname, pkgsource, feedback, check_hash, true)) - return 1; - // If we get here, fullname was corrupt and deleted, but it - // might have been cached. - pkgsource.set_cached (""); - } - - /* - 2) is there a version from one of the selected mirror sites available ? - */ - for (packagesource::sitestype::const_iterator n = pkgsource.sites.begin(); - n != pkgsource.sites.end(); ++n) - { - std::string fullname = prefix + rfc1738_escape_part (n->key) + "/" + - pkgsource.Canonical (); - if (io_stream::exists(fullname)) - { - if (validateCachedPackage (fullname, pkgsource, feedback, check_hash, - true)) - return 1; - // If we get here, fullname was corrupt and deleted, but it - // might have been cached. - pkgsource.set_cached (""); - } - } - return 0; -} - /* download a file from a mirror site to the local cache. */ static int download_one (packagesource & pkgsource, Feedback &feedback) { try { - if (check_for_cached (pkgsource, feedback)) + if (pkgsource.check_for_cached(feedback)) return 0; } catch (Exception * e) @@ -295,7 +197,7 @@ do_download_thread (HINSTANCE h, HWND owner) try { - if (!check_for_cached (*version.source(), feedback)) + if (!(version.source()->check_for_cached(feedback))) total_download_bytes += version.source()->size; } catch (Exception * e) diff --git a/download.h b/download.h index 3f65153..e887c92 100644 --- a/download.h +++ b/download.h @@ -16,10 +16,4 @@ #ifndef SETUP_DOWNLOAD_H #define SETUP_DOWNLOAD_H -#include "Feedback.h" - -class packagesource; -int check_for_cached (packagesource & pkgsource, Feedback &feedback, - bool mirror_mode = false, bool check_hash = true); - #endif /* SETUP_DOWNLOAD_H */ diff --git a/package_meta.cc b/package_meta.cc index 0c5e2f5..4baea34 100644 --- a/package_meta.cc +++ b/package_meta.cc @@ -41,7 +41,6 @@ #include #include "Generic.h" -#include "download.h" #include "Exception.h" #include "resource.h" @@ -841,7 +840,7 @@ packagemeta::scan (const packageversion &pkg, bool mirror_mode, Feedback &feedba try { - if (!check_for_cached (*(pkg.source ()), feedback, mirror_mode, false) + if (!pkg.source()->check_for_cached(feedback, mirror_mode, false) && ::source == IDC_SOURCE_LOCALDIR) return false; } diff --git a/package_source.cc b/package_source.cc index a652a3b..bc90158 100644 --- a/package_source.cc +++ b/package_source.cc @@ -25,6 +25,9 @@ #include "filemanip.h" #include "io_stream.h" #include "Feedback.h" +#include "csu_util/rfc1738.h" +#include "resource.h" +#include "state.h" site::site (const std::string& newkey) : key(newkey) { @@ -185,3 +188,95 @@ packagesource::check_md5 (const std::string fullname, Feedback &feedback) const Log (LOG_BABBLE) << "MD5 verified OK: " << fullname << " " << md5.str() << endLog; } + +// Return true if selected checks pass, false if they don't and the +// user chooses to delete the file; otherwise throw an exception. +static bool +validateCachedPackage (const std::string& fullname, packagesource &pkgsource, + Feedback &feedback, bool check_hash, bool check_size) +{ + try + { + if (check_size) + pkgsource.check_size_and_cache (fullname); + if (check_hash) + pkgsource.check_hash (feedback); + return true; + } + catch (Exception *e) + { + pkgsource.set_cached (""); + const char *filename = fullname.c_str (); + if (strncmp (filename, "file://", 7) == 0) + filename += 7; + if (e->errNo() == APPERR_CORRUPT_PACKAGE + && yesno (feedback.owner(), IDS_QUERY_CORRUPT, filename) == IDYES) + remove (filename); + else + throw e; + } + return false; +} + +int +packagesource::check_for_cached (Feedback &feedback, bool mirror_mode, + bool check_hash) +{ + /* If the packagesource doesn't have a filename, it can't possibly be in the + cache */ + if (!Canonical()) + { + return 0; + } + + /* Note that the cache dir is represented by a mirror site of file://local_dir */ + std::string prefix = "file://" + local_dir + "/"; + std::string fullname = prefix + Canonical(); + + if (mirror_mode) + { + /* Just assume correctness of mirror. */ + if (!Cached()) + set_cached (fullname); + return 1; + } + + // Already found one, which we can assume to have the right size. + if (Cached()) + { + if (validateCachedPackage (Cached(), *this, feedback, check_hash, false)) + return 1; + // If we get here, this.Cached() was corrupt and deleted. + set_cached (""); + } + + /* + 1) is there a legacy version in the cache dir available. + */ + if (io_stream::exists (fullname)) + { + if (validateCachedPackage (fullname, *this, feedback, check_hash, true)) + return 1; + // If we get here, fullname was corrupt and deleted, but it + // might have been cached. + set_cached (""); + } + + /* + 2) is there a version from one of the selected mirror sites available ? + */ + for (packagesource::sitestype::const_iterator n = sites.begin(); + n != sites.end(); ++n) + { + std::string fullname = prefix + rfc1738_escape_part (n->key) + "/" + Canonical (); + if (io_stream::exists(fullname)) + { + if (validateCachedPackage (fullname, *this, feedback, check_hash, true)) + return 1; + // If we get here, fullname was corrupt and deleted, but it + // might have been cached. + set_cached (""); + } + } + return 0; +} diff --git a/package_source.h b/package_source.h index 663a603..52efc4b 100644 --- a/package_source.h +++ b/package_source.h @@ -85,6 +85,10 @@ public: typedef std::vector sitestype; sitestype sites; + /* returns 0 if not cached; may throw exception if validation fails. */ + int check_for_cached (Feedback &feedback, bool mirror_mode = false, + bool check_hash = true); + private: std::string canonical; /* For progress reporting. */ -- 2.43.0