From ab003cdeba70e28470a885c4aa87b786ae8b48d0 Mon Sep 17 00:00:00 2001 From: Lam Wei Lun Date: Mon, 16 Nov 2026 19:26:31 +0800 Subject: [PATCH] Make app updates opt-in: notify when verified, install only from the dialog The updater installed a prefetched build when the application quit, so an update happened with no click and no visible moment. Now the startup check only prefetches, a persistent notification opens the dialog once the helper has verified the artifact, and the dialog's install button is the only path to the helper. Install-at-quit and the helper's --install-only mode are gone. Readiness is the helper's verified marker bound to the artifact digest, not the file name: the helper renames before verifying, and a nightly reuses one asset name. Skip deletes the cached artifact under a per-download lock. --- .gitignore | 1 + docs/HLSD/updater.md | 97 +++-- src/libslic3r/UpdateChannel.cpp | 17 +- src/libslic3r/UpdateChannel.hpp | 16 +- src/libslic3r/UpdateHandoff.cpp | 61 ++- src/libslic3r/UpdateHandoff.hpp | 28 +- src/slic3r/GUI/GUI_App.cpp | 366 ++++++++++-------- src/slic3r/GUI/GUI_App.hpp | 29 +- src/slic3r/GUI/NotificationManager.hpp | 4 + src/slic3r/UpdateHelper/InstallApply.hpp | 5 +- src/slic3r/UpdateHelper/InstallPlatform.hpp | 2 +- src/slic3r/UpdateHelper/UpdateHelperFrame.cpp | 59 ++- src/slic3r/UpdateHelper/UpdateHelperFrame.hpp | 12 +- src/slic3r/UpdateHelper/main.cpp | 11 +- tests/libslic3r/update_channel_tests.cpp | 40 +- tests/libslic3r/update_handoff_tests.cpp | 140 ++++++- 16 files changed, 569 insertions(+), 319 deletions(-) diff --git a/.gitignore b/.gitignore index 5224ecad6b..5a1d54d0d7 100644 --- a/.gitignore +++ b/.gitignore @@ -55,3 +55,4 @@ __pycache__/ /.test/ docs/superpowers/ ctest_results.xml +.slim/deepwork/ diff --git a/docs/HLSD/updater.md b/docs/HLSD/updater.md index 7d8e11a461..b4d774225f 100644 --- a/docs/HLSD/updater.md +++ b/docs/HLSD/updater.md @@ -50,8 +50,8 @@ outside the application carries no channel, so the channel is read from its vers instead: `-dev` or an unparseable version is nightly, `-rc`, `-beta` and `-alpha` are pre-release, the rest stable. That inferred channel may only switch to a candidate that does not sort lower, and a version that cannot be compared takes no switch at all: -otherwise a hand-installed `2.6.0-rc1` would be replaced by an older stable at quit, -with nothing shown. +otherwise a hand-installed `2.6.0-rc1` would be offered an older stable on a switch, +and a click would replace a newer build with an older one. `resolve_update()` performs the whole decision and returns a candidate plus a reason string for the log. It is pure — no network, no wx, no globals — so every rule above @@ -76,41 +76,50 @@ the GUI decides a button label once and the helper is only ever started for a fl that can use it. An executable inside a CMake build tree resolves to `other` on both Windows and macOS: a development build must never replace the tree it runs from. -## The two flows +## The flows -A check the user starts from Help → Check for Updates keeps its dialog, shows the -channel, and installs on request. The automatic check — startup, or the re-check after -Preferences closes — never opens a dialog: +Every check resolves the same way. When something newer exists, the prefetch helper +downloads it, verifies it, records a verified marker beside the artifact and exits. The +application keeps running and shows a notification: ``` -startup check → resolve_update → prefetch helper (download + sha256) - ↓ - application keeps running - ↓ - user quits - ↓ - install helper (verify, lock, swap, record) +check → resolve_update → prefetch (download, verify, mark) → notification + ↓ + user opens the dialog + ↓ + install helper (lock, verify, swap, record, relaunch) ``` -The prefetch helper exits once the file is verified. The install helper runs when the -application is closing, takes the install lock only after the parent process is gone, -verifies the file again, swaps it in, records the digest and — for a quit-time install -— does not start the application again. +Nothing installs without that click. There is no install at quit: quitting in any state +leaves the verified artifact in the cache, and the next start notifies again. -`update_action()` owns the choice between these flows, and `update_ready_to_install()` -owns the question of whether a quit may install: a dirty project or a running job -defers it to a later quit. Both are pure functions, so the policy is tested rather -than observed. +`update_action()` owns the choice between doing nothing, prefetching and notifying, and +takes readiness as an input on top of the skip and manual-check rules. It is pure, so the +policy is tested rather than observed. Readiness is a filesystem fact: +`download_is_verified()` requires the artifact at its expected size *and* a marker naming +the candidate's digest, because the helper renames the file before verifying it, and a +nightly reuses one asset name across builds. -A quit-time install is also refused for a candidate the user skipped, for one from a -channel they have since left, and when this run already handed the install to a helper. -macOS reaches the quit through `wxEVT_END_SESSION` as well, where `OnExit()` does not run, -so the same decision is made there. In stealth mode the startup check does not run, -because it starts a background download; the re-check after a channel change still does. +The notification carries one action, which opens the same dialog as Help → Check for +Updates: release notes, channel, "Install and restart", "Skip this Version", "Cancel". A +manual check with a verified artifact opens it immediately; one that finds nothing answers +"This is the newest version."; one that runs while a download is in flight says so. -An automatic update that fails leaves a message in `update_failed.txt`, which the next -start shows as a notification. Without a dialog this is the only way a failure reaches -the user; the detailed trail is in `updater.log`. +Neither the notification nor the candidate is persisted. The next check recomputes both +from the release list and the cache, which is why an artifact that finished while the +application was closed still produces a notification at the next start. + +`Skip this Version` stops offering that build and deletes the cached artifact. The deletion +takes a per-download lock, because a prefetch helper started before a restart may still be +writing the `.part`; when the lock is held, the deletion is deferred to the next skipped +check. + +In stealth mode the startup check does not run, because it starts a background download; +the re-check after a channel change and Help → Check for Updates still do. + +A failed download leaves a message in `update_failed.txt`, which the next start shows as a +notification; the detailed trail is in `updater.log`. A failed install is visible in the +helper window instead, because every install is started by the user. ## What the two processes exchange @@ -132,13 +141,18 @@ handed to its helper. | `installed.json` | helper | app, next start | what was installed, and the tree it belongs to | | `pending.json` | helper, before the swap | app, next start | the swap in flight; on macOS the tree moved aside, for rollback and cleanup | | `install.lock` | app, then helper | both | an OS file lock held for the life of the holder | -| `update_failed.txt` | helper | app, next start | why an automatic update failed | +| `download.lock` | prefetch helper | app, skip path | held while a prefetch writes; a skip defers its cleanup while it is held | +| `.verified` | prefetch helper, after verification | app | the digest the artifact verified for | +| `update_failed.txt` | helper | app, next start | why a background download failed | | `downloads//`, `helper//`, `updater.log` | both | both | artifact, extracted helper, log | The digest of the build a `.part` belongs to is recorded beside it, so a resumed download can never be appended to a file for a different build. A `.part` that is already the size of the release is verified instead of fetched again, and a verified -artifact is kept until it is installed rather than downloaded once per start. +artifact is kept until it is installed rather than downloaded once per start. The +artifact is reported ready only when a `.verified` marker beside it names the candidate's +digest: the helper renames the file before verifying it, and a nightly keeps one asset +name across builds, so the name and its size are not identity. A download whose size or digest does not match the listing is dropped with its digest marker, so the next check fetches it again rather than trusting its size. A file that @@ -147,7 +161,9 @@ check is repeated on the next start rather than the file being thrown away. The lock is an OS-level lock on an open handle, not a pid file: the OS releases it when the holder dies, and a held lock cannot be read through another handle, so it -carries no state. It prevents two installs, and the cleanup respects it. +carries no state. It prevents two installs, and the cleanup respects it. A second lock in +the download directory is held by the prefetch while it writes, so a skip's cleanup and an +install that follows a click both wait or refuse rather than race it. ## Installing @@ -210,10 +226,10 @@ recorded only in the other registry view — a CPack-era leftover beside a newer install — is refused rather than written by the wrong scope rule. Telling the first apart needs to know whether the caller named the tree, which `$INSTDIR` alone does not say. -The helper turns a failure into the message the next start shows. It adds that the previous -version was removed only when the executable is no longer in the tree after the installer -stopped, which with stage and swap means the installer reached the swap and could not -finish; every other failure leaves a version that still runs. +The helper reports a failure in its window, and records it for the next start only when it +has no window. It adds that the previous version was removed only when the executable is no +longer in the tree after the installer stopped, which with stage and swap means the installer +reached the swap and could not finish; every other failure leaves a version that still runs. **Uninstalling.** The uninstaller removes only the record of the tree it sits in: the machine entry when a machine record names that tree, the per-user entry when the user hive @@ -258,7 +274,8 @@ on the user reading a dialog: An empty value disables it with a warning, which is what a local or unsigned PR build wants and what a release build must not have. -Checks 3 and 4 together are what replaces the user's judgement once nothing prompts. +Checks 3 and 4 together are what let a single click install the file without the user +judging what it is. A release build carries the pin of its platform and cannot be configured without it: `ORCA_RELEASE_BUILD=ON` makes the configure fail when Windows has no publisher or macOS @@ -286,6 +303,12 @@ packages under, and refuses to ship a helper that would accept anything else. the next start, which that build may never reach. - The Windows check for another running instance cannot see processes it has no permission to open. +- Nothing installs while the application closes. Every update ends in a restart the user + started, so a nightly notifies at every launch until it is installed or skipped. +- A launch without a network cannot install a cached artifact: the asset URL and digest + arrive with the check. +- Skip deletes the cached artifact only when no prefetch holds the download lock, so a + skipped build's file can survive one more check. ## Where the code lives diff --git a/src/libslic3r/UpdateChannel.cpp b/src/libslic3r/UpdateChannel.cpp index a3fa414236..9e475c12b9 100644 --- a/src/libslic3r/UpdateChannel.cpp +++ b/src/libslic3r/UpdateChannel.cpp @@ -378,10 +378,10 @@ UpdateCandidate resolve_update(const std::string& releases_json, const std::string source_channel = inferred_channel ? update_channel_key(channel_of_version(running_version)) : installed_channel; bool channel_switch = channel != UpdateChannel::PreRelease && source_channel != update_channel_key(channel); - // An inferred switch must not downgrade: the automatic check would otherwise replace a - // hand-installed 2.6.0-rc1 with the newest stable at quit, with no dialog. A version that - // cannot be compared takes no switch either: offering a channel's current build without an - // ordering test is the same silent downgrade. + // An inferred switch must not downgrade: it would otherwise offer a hand-installed + // 2.6.0-rc1 the newest stable, and a click would replace a newer build with an older one. + // A version that cannot be compared takes no switch either: offering a channel's current + // build without an ordering test is the same downgrade. if (channel_switch && inferred_channel) channel_switch = Semver::parse(running_version) && is_newer_version(candidate.version, running_version); @@ -464,15 +464,10 @@ UpdateAction update_action(const UpdateRequest& request) if (request.skipped && !request.user_initiated) return UpdateAction::None; - if (!request.automatic) - return UpdateAction::InstallNow; + if (request.download_ready) + return UpdateAction::NotifyReady; return UpdateAction::Prefetch; } -bool update_ready_to_install(bool download_ready, bool project_dirty, bool job_running) -{ - return download_ready && !project_dirty && !job_running; -} - } // namespace Slic3r diff --git a/src/libslic3r/UpdateChannel.hpp b/src/libslic3r/UpdateChannel.hpp index 5b381d2438..c736d449c7 100644 --- a/src/libslic3r/UpdateChannel.hpp +++ b/src/libslic3r/UpdateChannel.hpp @@ -101,12 +101,12 @@ void update_skip_values(const UpdateCandidate& candidate, std::string& skip_version, std::string& skip_artifact); -// What the caller does with a candidate. A check the user started keeps its dialog; -// the automatic one downloads in the background and installs when the application quits. +// What the caller does with a candidate. A check may download on its own, but nothing +// installs until the user asks for it. enum class UpdateAction { - None, // nothing to do - Prefetch, // fetch and verify now, install when the application quits - InstallNow, // fetch, verify and install, closing the application first + None, // nothing to do + Prefetch, // fetch and verify now, notify when the artifact is ready + NotifyReady, // the verified artifact is in the cache: notify, or open the dialog }; struct UpdateRequest @@ -114,15 +114,11 @@ struct UpdateRequest bool available = false; // resolve_update() found a candidate bool skipped = false; // update_is_skipped() answered true for this check bool user_initiated = false; // Help > Check for Updates - bool automatic = true; // this check runs without a dialog + bool download_ready = false; // download_is_verified(): the artifact is in the cache }; UpdateAction update_action(const UpdateRequest& request); -// True when the verified download may be installed as the application exits: a job in -// progress or an unsaved project means the install waits for a later quit. -bool update_ready_to_install(bool download_ready, bool project_dirty, bool job_running); - } // namespace Slic3r #endif // slic3r_UpdateChannel_hpp_ diff --git a/src/libslic3r/UpdateHandoff.cpp b/src/libslic3r/UpdateHandoff.cpp index f731e7a11f..5f868cb68f 100644 --- a/src/libslic3r/UpdateHandoff.cpp +++ b/src/libslic3r/UpdateHandoff.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -159,6 +160,13 @@ bool is_plain_path_segment(const std::string& segment) }); } +// Markers are compared without the line ending an editor or a text writer may add. +void trim_trailing_space(std::string& text) +{ + while (!text.empty() && (text.back() == '\n' || text.back() == '\r' || text.back() == ' ' || text.back() == '\t')) + text.pop_back(); +} + #ifdef _WIN32 using LockHandle = HANDLE; #else @@ -306,6 +314,11 @@ std::string updater_lock_path() return (fs::path(cache_root()) / "install.lock").string(); } +std::string updater_download_lock_path() +{ + return (fs::path(updater_download_dir()) / "download.lock").string(); +} + std::string updater_log_path() { return (fs::path(cache_root()) / "updater.log").string(); @@ -369,9 +382,47 @@ DownloadPaths download_paths(const std::string& dir, const std::string& asset_na paths.part = (fs::path(dir) / (keep + ".part")).string(); paths.marker = (fs::path(dir) / (keep + ".part.digest")).string(); paths.artifact = (fs::path(dir) / keep).string(); + paths.verified = (fs::path(dir) / (keep + ".part.verified")).string(); return paths; } +bool download_is_verified(const std::string& dir, + const std::string& asset_name, + const std::string& digest, + int64_t expected_size) +{ + if (expected_size <= 0) + return false; + + const DownloadPaths paths = download_paths(dir, asset_name); + boost::system::error_code ec; + if (!fs::is_regular_file(paths.artifact, ec)) + return false; + const auto size = fs::file_size(paths.artifact, ec); + if (ec || static_cast(size) != expected_size) + return false; + + std::string recorded; + if (!read_file(paths.verified, recorded)) + return false; + trim_trailing_space(recorded); + return recorded == digest; +} + +bool mark_download_verified(const std::string& dir, const std::string& asset_name, const std::string& digest) +{ + return write_file_atomic(download_paths(dir, asset_name).verified, digest); +} + +void remove_download(const std::string& dir, const std::string& asset_name) +{ + const DownloadPaths paths = download_paths(dir, asset_name); + remove_file(paths.artifact); + remove_file(paths.verified); + remove_file(paths.marker); + remove_file(paths.part); +} + DownloadPrep prepare_download_dir(const std::string& dir, const std::string& asset_name, const std::string& digest, @@ -383,6 +434,7 @@ DownloadPrep prepare_download_dir(const std::string& dir, const fs::path part(paths.part); const fs::path marker(paths.marker); const fs::path artifact(paths.artifact); + const fs::path verified(paths.verified); boost::system::error_code ec; if (!fs::is_directory(directory, ec)) @@ -392,8 +444,7 @@ DownloadPrep prepare_download_dir(const std::string& dir, // smaller than that build: anything else is a leftover. std::string recorded; const bool have_marker = read_file(marker.string(), recorded); - while (!recorded.empty() && (recorded.back() == '\n' || recorded.back() == '\r' || recorded.back() == ' ')) - recorded.pop_back(); + trim_trailing_space(recorded); if (!digest.empty() && fs::is_regular_file(part, ec) && have_marker) { prep.resumable = recorded == digest; if (prep.resumable && expected_size > 0) { @@ -421,9 +472,12 @@ DownloadPrep prepare_download_dir(const std::string& dir, // Whatever else sits here is another candidate's, and stays: with a per-install directory a // second helper (a channel switch, a new nightly) may be writing it beside this download. + // The verified marker belongs to the artifact, so the two are swept together. std::vector own_names; - if (!prep.complete) + if (!prep.complete) { own_names.push_back(artifact.filename().string()); + own_names.push_back(verified.filename().string()); + } if (asset_name.empty()) { // The names a download took before it was named after its asset. own_names.emplace_back("update.part"); @@ -454,6 +508,7 @@ void discard_download(const std::string& dir, const std::string& asset_name) { const DownloadPaths paths = download_paths(dir, asset_name); remove_file(paths.artifact); + remove_file(paths.verified); remove_file(paths.marker); } diff --git a/src/libslic3r/UpdateHandoff.hpp b/src/libslic3r/UpdateHandoff.hpp index 55a536d901..9825c4f5e6 100644 --- a/src/libslic3r/UpdateHandoff.hpp +++ b/src/libslic3r/UpdateHandoff.hpp @@ -84,6 +84,9 @@ std::string updater_handoff_path(); std::string updater_install_record_path(); std::string updater_pending_path(); std::string updater_lock_path(); +// /download.lock: held by the prefetch helper while it writes, so a skip can +// tell a live download from a leftover file. +std::string updater_download_lock_path(); std::string updater_log_path(); std::string updater_failure_path(); @@ -102,18 +105,36 @@ std::string install_identity_hex(const std::string& install_root); std::string install_identity_key(const std::string& install_root); // The names a download takes inside its directory: written to `part`, renamed to `artifact` -// once verified, with `marker` recording the digest the partial file belongs to. +// once verified, with `marker` recording the digest the partial file belongs to and +// `verified` recording the digest the helper verified the artifact against. struct DownloadPaths { std::string part; std::string marker; std::string artifact; + std::string verified; }; // `asset_name` is a release asset name; anything path-like in it is dropped, and an empty // name falls back to "update.part". DownloadPaths download_paths(const std::string& dir, const std::string& asset_name); +// True when the artifact exists at the expected size and the helper recorded a successful +// verification of this exact digest. A final name alone means only that the download +// finished: fetch_artifact() renames before verifying. +bool download_is_verified(const std::string& dir, + const std::string& asset_name, + const std::string& digest, + int64_t expected_size); + +// Records a successful verification; read by download_is_verified(). Content is the digest. +bool mark_download_verified(const std::string& dir, + const std::string& asset_name, + const std::string& digest); + +// Removes the artifact, its verified marker, the digest marker and any .part. Takes no lock. +void remove_download(const std::string& dir, const std::string& asset_name); + // The state of a partial download in `dir` for `asset_name`, after the directory has been // reduced to this download's own files. Another candidate's files are left alone, because a // second helper may be writing them. The artifact's digest is recorded beside the .part, so a @@ -134,8 +155,9 @@ DownloadPrep prepare_download_dir(const std::string& dir, const std::string& digest, int64_t expected_size); -// Drops a download the verifier rejected: the artifact and its digest marker, so the next -// run fetches it again. The .part is left alone: a silent prefetch may still be writing it. +// Drops a download the verifier rejected: the artifact with its verified marker and its +// digest marker, so the next run fetches it again. The .part is left alone: a silent +// prefetch may still be writing it. void discard_download(const std::string& dir, const std::string& asset_name); // Only https downloads of `asset_name` from the OrcaSlicer release page, at diff --git a/src/slic3r/GUI/GUI_App.cpp b/src/slic3r/GUI/GUI_App.cpp index 7727bae5cc..f75ae37e49 100644 --- a/src/slic3r/GUI/GUI_App.cpp +++ b/src/slic3r/GUI/GUI_App.cpp @@ -1129,10 +1129,6 @@ void GUI_App::post_init() BOOST_LOG_TRIVIAL(info) << __FUNCTION__ << " sync_user_preset: false"; } - // OnExit() runs after the windows are gone, so the install on exit decides from what the - // close saw. Bound here after the GL retry, and again for a frame recreate_GUI() builds. - bind_mainframe_close_handler(); - // Restore a backup and consume installed.json before anything else: the G-code viewer // never runs the block below, so it would otherwise never finish an interrupted install. CallAfter([this] { @@ -2926,7 +2922,7 @@ void GUI_App::on_start_subscribe_again(std::string dev_id) obj->subscribe_counter--; BOOST_LOG_TRIVIAL(info) << __FUNCTION__ << ": dev_id=" << obj->get_dev_id(); } - }); + }, start_subscribe_timer->GetId()); start_subscribe_timer->Start(5000, wxTIMER_ONE_SHOT); } @@ -2968,8 +2964,6 @@ bool GUI_App::OnInit() int GUI_App::OnExit() { - install_update_on_exit(); - stop_http_server(); stop_sync_user_preset(); if (m_startup_cloud_plugin_fetch_thread.joinable()) @@ -3318,10 +3312,6 @@ bool GUI_App::on_init_inner() // Stop the Orca Cloud health check as OnExit() does, so no request is in flight while exit() tears down. wxGetApp().Bind(wxEVT_END_SESSION, [this](wxCloseEvent &e) { BOOST_LOG_TRIVIAL(info) << __FUNCTION__ << "received wxEVT_END_SESSION"; - // OnExit() never runs on this path, so a verified update is installed here instead of - // being dropped; the helper only installs after this process has gone. A launch that - // already happened is not repeated. - install_update_on_exit(); stop_sync_user_preset(); stop_orca_health_check(m_agent); // Before the plugin manager and network module go away. @@ -3536,29 +3526,54 @@ bool GUI_App::on_init_inner() if (this->plater_ == nullptr || !m_update_candidate.available) return; + // Readiness needs the helper's verified marker, not just the artifact's name: a + // nightly reuses one name, and fetch_artifact() renames before it verifies. + const bool in_app = can_install_in_app(m_update_candidate, current_install_flavour()); + const bool ready = in_app && download_is_verified(updater_download_dir(), m_update_candidate.asset.name, + m_update_candidate.asset.digest, + m_update_candidate.asset.size); + UpdateRequest request; request.available = m_update_candidate.available; request.skipped = update_is_skipped(m_update_candidate, app_config->get("skip_version"), app_config->skip_artifact()); request.user_initiated = evt.GetInt() != 0; - request.automatic = !request.user_initiated; + request.download_ready = ready; switch (update_action(request)) { case UpdateAction::None: - // A user-initiated check shows the dialog even for a skipped version; the - // startup check stays quiet then. The candidate is dropped: the install at - // quit must not take it, and an artifact prefetched before the skip may - // still be in the cache. + // Skip means "do not tell me about this build", so a cached artifact of a + // skipped candidate goes too. A manual check of a skipped version still reaches + // the dialog through Prefetch or NotifyReady. BOOST_LOG_TRIVIAL(info) << "Update " << m_update_candidate.version_label << ": nothing to do"; + if (request.skipped) + drop_cached_update(m_update_candidate.asset.name); m_update_candidate = UpdateCandidate(); + if (m_update_ready_timer) + m_update_ready_timer->Stop(); + m_update_notified_identity.clear(); + if (NotificationManager* notifications = notification_manager()) + notifications->close_notification_of_type(NotificationType::AppUpdateReady); + return; + case UpdateAction::NotifyReady: + // A click deserves an immediate answer; the automatic check posts the + // persistent notification instead. + if (request.user_initiated) + show_update_dialog(); + else + notify_update_ready(); return; case UpdateAction::Prefetch: - // The automatic check never shows a dialog: it downloads quietly and installs - // when the application quits. + // The automatic check never shows a dialog: it downloads quietly and the + // notification follows once the artifact is verified. BOOST_LOG_TRIVIAL(info) << "Update " << m_update_candidate.version_label << ": downloading in the background"; - // prefetch_update() only logs for a flavour that cannot install in place, so the - // user would never hear of the release. Point them at it instead. - if (!can_install_in_app(m_update_candidate, current_install_flavour())) { + if (!in_app) { + // prefetch_update() only logs for a flavour that cannot install in place, so + // the user would never hear of the release. Point them at it instead. + if (request.user_initiated) { + show_update_dialog(); + return; + } if (NotificationManager* notifications = notification_manager()) { const UpdateCandidate candidate = m_update_candidate; notifications->push_notification( @@ -3571,50 +3586,18 @@ bool GUI_App::on_init_inner() return true; }); } + m_update_candidate = UpdateCandidate(); return; } prefetch_update(); - return; - case UpdateAction::InstallNow: - break; - } - - // Destroyed before the action runs: the in-app update closes the main frame, which - // would otherwise delete the dialog as a child and then again at scope exit. - int answer = wxID_CANCEL; - { - UpdateVersionDialog dialog(this->mainframe); - wxString channel_label; - switch (m_update_candidate.channel) { - case UpdateChannel::PreRelease: channel_label = _L("Pre-Release channel"); break; - case UpdateChannel::Nightly: channel_label = _L("Nightly channel"); break; - case UpdateChannel::Stable: channel_label = _L("Stable channel"); break; + start_update_ready_timer(); + // Also answers a check made while this session's prefetch is already running. + if (request.user_initiated) { + MessageDialog dlg(this->mainframe, _L("The update is downloading in the background."), + _L("Update"), wxOK | wxICON_INFORMATION); + dlg.ShowModal(); } - dialog.set_channel_label(channel_label); - dialog.set_release_url(wxString::FromUTF8(m_update_candidate.release_url)); - dialog.set_install_in_app(can_install_in_app(m_update_candidate, current_install_flavour())); - dialog.update_version_info(wxString::FromUTF8(m_update_candidate.release_notes), - wxString::FromUTF8(m_update_candidate.version_label)); - answer = dialog.ShowModal(); - } - switch (answer) { - case wxID_YES: { - // Store builds get updates from the Microsoft Store and Flatpak builds from - // Flathub. Download-only installs open the asset itself. - if (can_install_in_app(m_update_candidate, current_install_flavour())) - start_in_app_update(); - else - open_update_page(m_update_candidate); - // The page replaces the install: nothing may remain armed for the quit path. - m_update_candidate = UpdateCandidate(); - break; - } - // A declined or dismissed dialog disarms the candidate: install_update_on_exit() - // would otherwise install what the user just refused. - case wxID_NO: - default: - m_update_candidate = UpdateCandidate(); - break; + return; } }); @@ -5118,8 +5101,6 @@ void GUI_App::recreate_GUI(const wxString &msg_name) switch_window_pools(); mainframe = new MainFrame(); - // The rebuilt frame needs the close handler too, or the snapshot below stays stale. - bind_mainframe_close_handler(); if (is_editor()) // hide settings tabs after first Layout mainframe->select_tab(TAB_ID_PREPARE); @@ -6643,10 +6624,19 @@ void GUI_App::check_new_version_sf(bool show_tips, int by_user) error); }) .on_complete([this, by_user, channel, installed_artifact, installed_channel, flavour](std::string body, unsigned http_status) { - // Every return below offers no update. The candidate is cleared on the UI thread so - // that a withdrawn or unreachable release is not installed by the quit path. + // Every return below offers no update. The candidate is cleared on the UI thread, and + // the notification and the poll with it: the release is no longer on offer. auto drop_candidate = [this] { - GUI::wxGetApp().CallAfter([this] { m_update_candidate = UpdateCandidate(); }); + GUI::wxGetApp().CallAfter([this] { + m_update_candidate = UpdateCandidate(); + // The notification is gone, so the same candidate must be able to notify + // again once a later check resolves it. + m_update_notified_identity.clear(); + if (m_update_ready_timer) + m_update_ready_timer->Stop(); + if (NotificationManager* notifications = notification_manager()) + notifications->close_notification_of_type(NotificationType::AppUpdateReady); + }); }; if (http_status != 200) { drop_candidate(); @@ -6861,8 +6851,8 @@ std::vector updater_helper_candidates(const fs::path& exe_dir) return candidates; } -// The copy extract_updater_helper() left in the cache, if any. Preferred at exit, because a -// prefetch helper may still run from it and Windows cannot replace a running binary. +// The copy extract_updater_helper() left in the cache, if any. Preferred, because a prefetch +// helper may still run from it and Windows cannot replace a running binary. std::string extracted_updater_helper() { for (const fs::path& candidate : updater_helper_candidates(fs::path(updater_helper_dir()))) { @@ -7005,19 +6995,6 @@ bool launch_updater_helper(const std::string& helper, std::initializer_list 0; } -// True when the helper's verified download of this candidate is still in the cache. -bool update_download_ready(const UpdateCandidate& candidate) -{ - if (candidate.asset.size <= 0) - return false; - const fs::path artifact(download_paths(updater_download_dir(), candidate.asset.name).artifact); - boost::system::error_code ec; - if (!fs::is_regular_file(artifact, ec)) - return false; - const auto size = fs::file_size(artifact, ec); - return !ec && static_cast(size) == candidate.asset.size; -} - // True when this process runs from `path`. Compared on disk, so a differently spelled path still matches. bool running_from(const std::string& path) { @@ -7044,6 +7021,17 @@ void clear_install_leftovers() void GUI_App::set_skip_version(bool skip) { BOOST_LOG_TRIVIAL(info) << "set_skip_version, skip = " << skip << ", version = " << m_update_candidate.version_label; + // The cleanup below needs the asset name after the candidate is gone. + const std::string asset_name = m_update_candidate.asset.name; + if (skip) { + if (NotificationManager* notifications = notification_manager()) + notifications->close_notification_of_type(NotificationType::AppUpdateReady); + if (m_update_ready_timer) + m_update_ready_timer->Stop(); + m_update_notified_identity.clear(); + m_prefetching_artifact.clear(); + } + std::string skip_version = app_config->get("skip_version"); std::string skip_artifact = app_config->skip_artifact(); if (skip) { @@ -7059,23 +7047,11 @@ void GUI_App::set_skip_version(bool skip) app_config->set_skip_artifact(skip_artifact); // Read the skip values before this: for a nightly the skip names the candidate's digest. - // Nothing may install on a quit what the user just asked not to be offered. - if (skip) + if (skip) { m_update_candidate = UpdateCandidate(); -} - -void GUI_App::bind_mainframe_close_handler() -{ - if (mainframe == nullptr) - return; - mainframe->Bind(wxEVT_CLOSE_WINDOW, [this](wxCloseEvent& evt) { - if (this->plater_ != nullptr) { - m_closing_project_dirty = this->plater_->is_project_dirty(); - m_closing_job_running = this->plater_->is_background_process_slicing() || - this->plater_->is_export_gcode_scheduled(); - } - evt.Skip(); - }); + // Skip means "do not tell me about this build", so the cached artifact goes too. + drop_cached_update(asset_name); + } } void GUI_App::start_in_app_update() @@ -7090,27 +7066,6 @@ void GUI_App::start_in_app_update() set_updater_cache_dir(update_cache_dir()); set_updater_install_root(app_install_root()); - // The background prefetch may still have the .part open for append: a second helper would - // resume it, fail the digest and report the update as damaged. On Windows - // extract_updater_helper() would also delete the folder that prefetch runs from. - // A .part whose size already matches the asset is complete, not still downloading: the - // helper's next step renames it into place, and a manual install that waits for it would - // only ever be told to try again. A .part no prefetch of this run wrote is a leftover: the - // helper resumes it, so it is no reason to refuse. - if (!update_download_ready(m_update_candidate) && m_prefetching_artifact == m_update_candidate.identity()) { - const fs::path part(download_paths(updater_download_dir(), m_update_candidate.asset.name).part); - boost::system::error_code part_ec; - const auto part_size = fs::file_size(part, part_ec); - const int64_t expected = m_update_candidate.asset.size; - const bool still_downloading = !part_ec && (expected <= 0 || static_cast(part_size) < expected); - if (still_downloading) { - MessageDialog dlg(this->mainframe, _L("The update is still downloading. Try again in a moment."), - _L("Update"), wxOK | wxICON_INFORMATION); - dlg.ShowModal(); - return; - } - } - // Refuse while this app has a local job: an install closes the app and would lose it. if (this->plater_ != nullptr && (this->plater_->is_background_process_slicing() || this->plater_->is_export_gcode_scheduled())) { @@ -7129,11 +7084,24 @@ void GUI_App::start_in_app_update() return; } + // A prefetch helper may still be writing this build's .part: a second helper would resume + // it and the digest would fail. Its lock says so, and this one is held until the process + // exits, so no prefetch starts again during the close. + const std::string download_lock = updater_download_lock_path(); + if (!acquire_install_lock(download_lock)) { + release_install_lock(lock_path); + MessageDialog dlg(this->mainframe, _L("The update is still downloading. Try again in a moment."), + _L("Update"), wxOK | wxICON_INFORMATION); + dlg.ShowModal(); + return; + } + const UpdateHandoff handoff = make_update_handoff(m_update_candidate); const std::string helper = extract_updater_helper(); if (helper.empty()) { release_install_lock(lock_path); + release_install_lock(download_lock); MessageDialog dlg(this->mainframe, _L("The updater is missing from this installation. Opening the release page instead."), _L("Update"), wxOK | wxICON_WARNING); @@ -7144,6 +7112,7 @@ void GUI_App::start_in_app_update() if (!write_handoff(handoff)) { release_install_lock(lock_path); + release_install_lock(download_lock); MessageDialog dlg(this->mainframe, _L("Could not prepare the update."), _L("Update"), wxOK | wxICON_ERROR); dlg.ShowModal(); return; @@ -7151,13 +7120,9 @@ void GUI_App::start_in_app_update() // Close the app before the helper starts: the running binary cannot be replaced. // Close(false) keeps the normal prompts (save project, empty the print queue). - // OnExit() runs inside Close() and would otherwise start a second, silent helper on the - // same install, so this is set first and cleared if the close is vetoed: the app then - // keeps running, and a later quit must still be able to install. - m_update_install_started = true; if (!mainframe || !mainframe->Close(false)) { - m_update_install_started = false; release_install_lock(lock_path); + release_install_lock(download_lock); remove_handoff(); return; } @@ -7165,6 +7130,7 @@ void GUI_App::start_in_app_update() // The helper outlives this process and must not keep the log folder as its working directory. if (!launch_updater_helper(helper, {})) { release_install_lock(lock_path); + release_install_lock(download_lock); remove_handoff(); MessageDialog dlg(nullptr, _L("Could not start the updater."), _L("Update"), wxOK | wxICON_ERROR); dlg.ShowModal(); @@ -7210,68 +7176,121 @@ void GUI_App::prefetch_update() BOOST_LOG_TRIVIAL(info) << "Started the background download of " << m_update_candidate.asset.name; } -void GUI_App::install_update_on_exit() +void GUI_App::show_update_dialog() { - // A helper already handed this install; starting another one races it for the lock. - if (m_update_install_started) - return; - if (!m_update_candidate.available) return; - // A skipped candidate, or one from a channel the user has left, is never installed on a - // quit: the check stores the candidate before the decision, and an artifact prefetched - // before the skip can still be in the cache. - if (update_is_skipped(m_update_candidate, app_config->get("skip_version"), app_config->skip_artifact())) - return; - if (update_channel_from_key(app_config->update_channel()) != m_update_candidate.channel) - return; + // The notification has done its job: the dialog answers for the same candidate. + if (NotificationManager* notifications = notification_manager()) + notifications->close_notification_of_type(NotificationType::AppUpdateReady); - if (!can_install_in_app(m_update_candidate, current_install_flavour())) { - BOOST_LOG_TRIVIAL(info) << "Not installing the update on exit: no asset for this installation"; - return; + // Destroyed before the action runs: the in-app update closes the main frame, which + // would otherwise delete the dialog as a child and then again at scope exit. + int answer = wxID_CANCEL; + { + UpdateVersionDialog dialog(this->mainframe); + wxString channel_label; + switch (m_update_candidate.channel) { + case UpdateChannel::PreRelease: channel_label = _L("Pre-Release channel"); break; + case UpdateChannel::Nightly: channel_label = _L("Nightly channel"); break; + case UpdateChannel::Stable: channel_label = _L("Stable channel"); break; + } + dialog.set_channel_label(channel_label); + dialog.set_release_url(wxString::FromUTF8(m_update_candidate.release_url)); + dialog.set_install_in_app(can_install_in_app(m_update_candidate, current_install_flavour())); + dialog.update_version_info(wxString::FromUTF8(m_update_candidate.release_notes), + wxString::FromUTF8(m_update_candidate.version_label)); + answer = dialog.ShowModal(); + } + switch (answer) { + case wxID_YES: + // Store builds get updates from the Microsoft Store and Flatpak builds from + // Flathub. Download-only installs open the asset itself. + if (can_install_in_app(m_update_candidate, current_install_flavour())) + start_in_app_update(); + else + open_update_page(m_update_candidate); + // The page replaces the install: nothing may remain armed for this candidate. + m_update_candidate = UpdateCandidate(); + break; + case wxID_NO: + default: + m_update_candidate = UpdateCandidate(); + break; } + // The dialog answered for this candidate: the poll has nothing left to watch. The + // notified identity is kept, so dismissing the dialog stays quiet for this session. + if (m_update_ready_timer) + m_update_ready_timer->Stop(); +} + +void GUI_App::notify_update_ready() +{ + if (m_update_notified_identity == m_update_candidate.identity()) + return; + NotificationManager* notifications = notification_manager(); + if (notifications == nullptr) + return; + + // Warning is the only level that does not fade: Important disappears after 20 s. + notifications->push_notification(NotificationType::AppUpdateReady, + NotificationManager::NotificationLevel::WarningNotificationLevel, + format(_u8L("OrcaSlicer %1% is ready to install."), m_update_candidate.version_label), + _u8L("Install update"), + [](wxEvtHandler*) { + wxGetApp().CallAfter([] { wxGetApp().show_update_dialog(); }); + return true; + }); + m_update_notified_identity = m_update_candidate.identity(); +} + +void GUI_App::drop_cached_update(const std::string& asset_name) +{ set_updater_cache_dir(update_cache_dir()); set_updater_install_root(app_install_root()); - const bool download_ready = update_download_ready(m_update_candidate); - if (!download_ready) { - BOOST_LOG_TRIVIAL(info) << "Not installing the update on exit: no verified download of " << m_update_candidate.asset.name; + const std::string dir = updater_download_dir(); + if (dir.empty()) + return; + boost::system::error_code ec; + if (!fs::is_directory(fs::path(dir), ec)) return; - } - // The close event recorded whether a project or a job was at risk; the window is gone here. - if (!update_ready_to_install(download_ready, m_closing_project_dirty, m_closing_job_running)) { - BOOST_LOG_TRIVIAL(info) << "Update deferred to a later quit: project dirty " << m_closing_project_dirty << ", job running " - << m_closing_job_running; + // A prefetch helper started before a restart may still be writing the artifact, and + // deleting its .part would fail the write and leave a confusing update_failed.txt. Its + // lock says so; the next skipped check retries. + const std::string lock_path = updater_download_lock_path(); + if (!acquire_install_lock(lock_path)) return; - } + remove_download(dir, asset_name); + release_install_lock(lock_path); +} - // The extracted copy first: a prefetch helper may still run from it, and Windows cannot - // replace a running binary. - std::string helper = extracted_updater_helper(); - if (helper.empty()) - helper = extract_updater_helper(); - if (helper.empty()) { - BOOST_LOG_TRIVIAL(warning) << "Not installing the update on exit: the updater helper is missing"; - return; +void GUI_App::start_update_ready_timer() +{ + if (m_update_ready_timer == nullptr) { + m_update_ready_timer = new wxTimer(this, wxID_ANY); + // Bound on its own id: a wildcard handler would also take every other app timer. + Bind(wxEVT_TIMER, &GUI_App::on_update_ready_timer, this, m_update_ready_timer->GetId()); } + m_update_ready_timer->Start(15000); +} - // The helper deletes the handoff it read at prefetch time, so this writes a fresh one. - if (!write_handoff(make_update_handoff(m_update_candidate))) { - BOOST_LOG_TRIVIAL(warning) << "Not installing the update on exit: could not write the handoff"; +void GUI_App::on_update_ready_timer(wxTimerEvent&) +{ + if (!m_update_candidate.available) { + m_update_ready_timer->Stop(); return; } - - if (!launch_updater_helper(helper, {L"--install-only", L"--silent"})) { - m_update_install_started = false; // nothing started, a later quit may try again - remove_handoff(); - BOOST_LOG_TRIVIAL(warning) << "Could not start the updater on exit"; + // One stat per tick: the helper's verified marker only appears once it is done. + if (!download_is_verified(updater_download_dir(), m_update_candidate.asset.name, m_update_candidate.asset.digest, + m_update_candidate.asset.size)) return; - } - m_update_install_started = true; - BOOST_LOG_TRIVIAL(info) << "Started the updater on exit for " << m_update_candidate.version_label; + m_prefetching_artifact.clear(); + m_update_ready_timer->Stop(); + notify_update_ready(); } void GUI_App::finish_pending_update() @@ -9655,8 +9674,21 @@ void GUI_App::open_preferences(PreferencesTab tab, const std::string& highlight_ dlg.ShowModal(); // Checked once the dialog is closed: an update started while it is open would // hand its result to a dialog that is still modal. - if (app_config->update_channel() != channel_before) + if (app_config->update_channel() != channel_before) { + // The candidate, its notification and its artifact belong to the channel the user + // has left. The deletion takes the download lock, so a prefetch still writing the + // old build is left alone and the file is swept on a later check. + const std::string old_asset = m_update_candidate.asset.name; + if (NotificationManager* notifications = notification_manager()) + notifications->close_notification_of_type(NotificationType::AppUpdateReady); + if (m_update_ready_timer) + m_update_ready_timer->Stop(); + m_update_notified_identity.clear(); + m_update_candidate = UpdateCandidate(); + if (!old_asset.empty()) + drop_cached_update(old_asset); check_new_version_sf(true, 0); + } need_recreate_gui = dlg.recreate_GUI(); pending_language = dlg.pending_language(); if (!need_recreate_gui) { diff --git a/src/slic3r/GUI/GUI_App.hpp b/src/slic3r/GUI/GUI_App.hpp index da21a48cee..e70372ab2d 100644 --- a/src/slic3r/GUI/GUI_App.hpp +++ b/src/slic3r/GUI/GUI_App.hpp @@ -58,6 +58,8 @@ class wxMenuItem; class wxMenuBar; class wxTopLevelWindow; class wxDataViewCtrl; +class wxTimer; +class wxTimerEvent; class wxBookCtrlBase; class wxGLCanvas; class wxGLContext; @@ -357,11 +359,11 @@ private: Slic3r::UpdateCandidate m_update_candidate; // Identity of the artifact whose background download was started, so it starts once. std::string m_prefetching_artifact; - // What the close event saw, because OnExit() runs after the Plater is gone. - bool m_closing_project_dirty { false }; - bool m_closing_job_running { false }; - // A helper already owns this run's install; a second one must not be started at exit. - bool m_update_install_started { false }; + // Identity of the candidate the ready notification was already pushed for, so one + // candidate notifies once per session. + std::string m_update_notified_identity; + // Polls the cache while a prefetch runs, so the notification follows without a restart. + wxTimer* m_update_ready_timer { nullptr }; static std::string version_display; HMSQuery *hms_query { nullptr }; FilamentColorCodeQuery* m_filament_color_code_query{ nullptr }; @@ -621,16 +623,21 @@ public: // Hands the resolved update to the helper: writes the handoff, extracts and spawns the // helper, and closes the app. Falls back to the release page when that is not possible. void start_in_app_update(); + // Offers the candidate: install in place or open the release page. Called by a manual + // check and by the ready notification's action, never by the automatic check. + void show_update_dialog(); + // Pushes the persistent "ready to install" notification for the current candidate. + void notify_update_ready(); // Starts the download the automatic check hands to the helper, without a dialog or a - // lock; the update installs from install_update_on_exit(). + // lock; the notification follows once the artifact is verified. void prefetch_update(); - // Installs a verified download as the app quits, when no project or job is at risk. - void install_update_on_exit(); + // Deletes a cached artifact the user no longer wants, unless a prefetch still writes it. + void drop_cached_update(const std::string& asset_name); + // Polls download_is_verified() while a prefetch runs and notifies when it is done. + void start_update_ready_timer(); + void on_update_ready_timer(wxTimerEvent& event); // Cleans up after an install that happened while this build was being replaced. void finish_pending_update(); - // Records what the close event saw, for install_update_on_exit(). Bound on every - // mainframe, including a frame rebuilt by recreate_GUI(). - void bind_mainframe_close_handler(); // Offers to migrate user filament presets made against an older release that lost printers their parent // listed then. At startup it asks on every start that finds such presets, unless the user chose not to be // reminded; while the user presets sync with the cloud, it waits until they have been pulled. by_user also diff --git a/src/slic3r/GUI/NotificationManager.hpp b/src/slic3r/GUI/NotificationManager.hpp index 713a8a599c..471af51d7c 100644 --- a/src/slic3r/GUI/NotificationManager.hpp +++ b/src/slic3r/GUI/NotificationManager.hpp @@ -201,6 +201,10 @@ enum class NotificationType // One aggregate notice that installed OrcaCloud plugins have updates. "More" opens the // Plugins dialog, where the updates can be installed. OrcaCloudPluginUpdateAvailable, + // A verified application update is in the cache. Persistent, with an "Install update" + // action that opens the update dialog. Not registered in basic_notifications: it is + // pushed with the explicit level, text, hypertext and callback overload. + AppUpdateReady, NotificationTypeCount }; diff --git a/src/slic3r/UpdateHelper/InstallApply.hpp b/src/slic3r/UpdateHelper/InstallApply.hpp index 7173424e60..c5089aafdb 100644 --- a/src/slic3r/UpdateHelper/InstallApply.hpp +++ b/src/slic3r/UpdateHelper/InstallApply.hpp @@ -42,9 +42,8 @@ struct ProgressSink // Verified artifact in hand: swap it into the install described by `handoff`, // record it, and launch the new build. Returns false with `error` filled when -// nothing was changed, or when a change was rolled back. `launch_new_build` is -// false for the install that runs after the user quit, where starting the -// application again would contradict the quit. +// nothing was changed, or when a change was rolled back. `launch_new_build` starts +// the new build once it is in place; the frame always passes true. bool apply_update(const UpdateHandoff& handoff, const std::string& artifact_path, ProgressSink& sink, diff --git a/src/slic3r/UpdateHelper/InstallPlatform.hpp b/src/slic3r/UpdateHelper/InstallPlatform.hpp index dbd241af1e..51b0505efb 100644 --- a/src/slic3r/UpdateHelper/InstallPlatform.hpp +++ b/src/slic3r/UpdateHelper/InstallPlatform.hpp @@ -14,7 +14,7 @@ namespace Updater { // Each returns false with `error` filled when the install did not happen, or // when a change already made was rolled back. `launch` starts the new build once -// it is in place; the install after a quit passes false. +// it is in place. bool apply_windows_install(const UpdateHandoff& handoff, const std::string& artifact_path, ProgressSink& sink, std::string& error, bool launch = true); bool apply_macos_install(const UpdateHandoff& handoff, const std::string& artifact_path, ProgressSink& sink, std::string& error, bool launch = true); diff --git a/src/slic3r/UpdateHelper/UpdateHelperFrame.cpp b/src/slic3r/UpdateHelper/UpdateHelperFrame.cpp index 0c57752d9e..374c96a497 100644 --- a/src/slic3r/UpdateHelper/UpdateHelperFrame.cpp +++ b/src/slic3r/UpdateHelper/UpdateHelperFrame.cpp @@ -290,9 +290,7 @@ UpdateHelperFrame::UpdateHelperFrame(const UpdateHandoff& handoff, Mode mode) Bind(wxEVT_BUTTON, &UpdateHelperFrame::on_cancel, this, wxID_CANCEL); Bind(wxEVT_CLOSE_WINDOW, &UpdateHelperFrame::on_close, this); - const char* mode_name = m_mode == Mode::Prefetch ? "prefetch" - : m_mode == Mode::InstallOnly ? "install-only" - : "interactive"; + const char* mode_name = m_mode == Mode::Prefetch ? "prefetch" : "interactive"; m_log.write(std::string("update helper started in ") + mode_name + " mode for version " + handoff.version + " on the " + handoff.channel + " channel"); } @@ -367,7 +365,7 @@ void UpdateHelperFrame::finish(int code, const wxString& message, bool alert) m_worker_done.store(true); // The application closed itself for this update: if the update does not go // through, start it again rather than leave the user without one. A silent run - // must not: the user quit deliberately and the next start reports the failure. + // has no window to say that in, so it leaves the message for the next start. const bool relaunch = code != 0 && m_parent_gone && !silent(); wxTheApp->CallAfter([this, message, alert, relaunch, code]() { if (relaunch && !relaunch_app(m_handoff)) @@ -624,21 +622,7 @@ bool UpdateHelperFrame::check_artifact(const fs::path& artifact) m_log.write("the artifact disappeared while it was being verified"); } - if (m_mode == Mode::InstallOnly) { - if (content_failure) { - fail(1, error); - return false; - } - // Nothing complete to install, and nothing failed: the next start fetches it again. - // The application already closed for this update, so it is started again rather - // than left missing. - m_log.write("no complete download to install; starting the application again"); - if (!relaunch_app(m_handoff)) - m_log.write("could not start the application again"); - finish(0, "", false); - } else { - fail(1, error); - } + fail(1, error); return false; } @@ -652,14 +636,28 @@ void UpdateHelperFrame::run() const fs::path part(paths.part); const fs::path artifact(paths.artifact); - // Prefetch only puts the artifact on disk: the install is a separate - // --install-only run, started when the application quits. + // Prefetch downloads and verifies the artifact, then records the verification the + // application reads. The install itself waits for the user to accept it. if (m_mode == Mode::Prefetch) { + // Held while writing, so a skip can tell a live download from a leftover file. + const std::string download_lock = Slic3r::updater_download_lock_path(); + if (!Slic3r::acquire_install_lock(download_lock)) { + m_log.write("another download of this install is running"); + finish(0, "", false); + return; + } if (fetch_artifact(download_dir, part, artifact) && check_artifact(artifact)) { + // The marker is what the application reads as "ready": a run that cannot + // record it has to report a failure, or the user is told nothing at all. + if (!Slic3r::mark_download_verified(download_dir.string(), m_handoff.asset_name, m_handoff.digest)) { + fail(1, "The downloaded file could not be recorded."); + return; + } // A failure left by an earlier run no longer applies: the download this // start needed is in place. Slic3r::remove_update_failure(); - m_log.write("prefetch done, the install is left to the next run"); + m_log.write("prefetch done, the install waits for the user"); + Slic3r::release_install_lock(download_lock); finish(0, "", false); } return; @@ -672,13 +670,6 @@ void UpdateHelperFrame::run() return; } if (waited == WaitResult::Timeout) { - if (silent()) { - // The application did not exit in time, and nothing failed: the - // verified file stays for the next start. - m_log.write("leaving the update for the next start: the parent process is still running"); - finish(0, "", false); - return; - } m_log.write("giving up: the parent process is still running"); fail(1, "OrcaSlicer is still running."); return; @@ -702,13 +693,7 @@ void UpdateHelperFrame::run() } // A manual update downloads here, once the lock has ruled out a second helper. - if (m_mode == Mode::Interactive && !fetch_artifact(download_dir, part, artifact)) - return; - - // Install-only has nothing to fetch: the file a previous run left is what it - // installs, if it is still there. Checked after the parent is gone, so a run with - // nothing to install starts the application again only once it has exited. - if (m_mode == Mode::InstallOnly && !check_artifact(artifact)) + if (!fetch_artifact(download_dir, part, artifact)) return; // Held from here until the install returns: what is verified is what runs. @@ -756,7 +741,7 @@ void UpdateHelperFrame::run() sink.cancelled = [this]() { return m_cancel_requested.load(); }; std::string install_error; - if (!apply_update(m_handoff, artifact.string(), sink, install_error, m_mode != Mode::InstallOnly)) { + if (!apply_update(m_handoff, artifact.string(), sink, install_error)) { if (!m_unfinished_install.empty()) { m_log.write("leaving the running installer to finish the update"); finish(0, wxString::FromUTF8(m_unfinished_install), !silent()); diff --git a/src/slic3r/UpdateHelper/UpdateHelperFrame.hpp b/src/slic3r/UpdateHelper/UpdateHelperFrame.hpp index 519e673fa6..01a03079e1 100644 --- a/src/slic3r/UpdateHelper/UpdateHelperFrame.hpp +++ b/src/slic3r/UpdateHelper/UpdateHelperFrame.hpp @@ -47,12 +47,11 @@ private: class UpdateHelperFrame : public wxFrame { public: - // How this run was started. The silent modes never show the window: they only - // write the log, and leave any failure for the next start to report. + // How this run was started. The silent mode never shows the window: it only + // writes the log, and leaves any failure for the next start to report. enum class Mode { Interactive, // started from the application: wait, download, install - Prefetch, // --silent: fetch the artifact only; a later run installs it - InstallOnly, // --install-only: the artifact is complete; install after the application quits + Prefetch, // --silent: fetch and verify the artifact only; the install waits for the user }; explicit UpdateHelperFrame(const UpdateHandoff& handoff, Mode mode = Mode::Interactive); @@ -74,14 +73,13 @@ private: void on_cancel(wxCommandEvent& event); void on_close(wxCloseEvent& event); - bool silent() const { return m_mode != Mode::Interactive; } + bool silent() const { return m_mode == Mode::Prefetch; } void run(); // worker thread // Fetches the artifact into `artifact` unless an earlier run already did. Does not verify it. bool fetch_artifact(const boost::filesystem::path& download_dir, const boost::filesystem::path& part, const boost::filesystem::path& artifact); - // Verifies the file this run or an earlier one fetched. Nothing to install is not a - // failure at quit: the next start fetches it again. + // Verifies the file this run or an earlier one fetched. Any failure is this run's failure. bool check_artifact(const boost::filesystem::path& artifact); // Ends a failed run: logged, and recorded for the next start when there is no window. void fail(int code, const std::string& message); diff --git a/src/slic3r/UpdateHelper/main.cpp b/src/slic3r/UpdateHelper/main.cpp index ffc86bb220..f0d7e1a0de 100644 --- a/src/slic3r/UpdateHelper/main.cpp +++ b/src/slic3r/UpdateHelper/main.cpp @@ -39,7 +39,7 @@ bool parse_handoff_path(int argc, wxChar** argv, wxString& out) return false; } -// Accepts "--silent" and "--install-only", in any order and anywhere on the line. +// Accepts "--silent", anywhere on the line. bool has_flag(int argc, wxChar** argv, const wxString& flag) { for (int i = 1; i < argc; ++i) { @@ -94,16 +94,13 @@ bool UpdateHelperApp::OnInit() // Not wxApp::OnInit(): its command-line parser rejects --handoff and returns false. SetAppName("OrcaSlicerUpdater"); - // An automatic update starts the helper without a window: "--silent" fetches the - // file while the application still runs, "--install-only" installs one fetched - // by an earlier run. + // An automatic update starts the helper without a window: "--silent" fetches and + // verifies the file while the application still runs. using Mode = Slic3r::Updater::UpdateHelperFrame::Mode; Mode mode = Mode::Interactive; if (has_flag(argc, argv, "--silent")) mode = Mode::Prefetch; - if (has_flag(argc, argv, "--install-only")) - mode = Mode::InstallOnly; - // The silent modes never open a window, not even for an error. + // The silent mode never opens a window, not even for an error. const bool silent_run = mode != Mode::Interactive; wxString handoff_arg; diff --git a/tests/libslic3r/update_channel_tests.cpp b/tests/libslic3r/update_channel_tests.cpp index 907519cf81..f245b04931 100644 --- a/tests/libslic3r/update_channel_tests.cpp +++ b/tests/libslic3r/update_channel_tests.cpp @@ -939,7 +939,7 @@ TEST_CASE("A check with nothing to offer has nothing to do", "[UpdateChannel]") // Unavailable wins over every other field, a manual check included. request.user_initiated = true; - request.automatic = false; + request.download_ready = true; CHECK(update_action(request) == UpdateAction::None); } @@ -948,40 +948,38 @@ TEST_CASE("An automatic check ignores a skipped update", "[UpdateChannel]") UpdateRequest request; request.available = true; request.skipped = true; - request.automatic = true; CHECK(update_action(request) == UpdateAction::None); + + // A skipped build is not reported either, even when its artifact is already verified. + request.download_ready = true; + CHECK(update_action(request) == UpdateAction::None); } -TEST_CASE("A manual check installs a skipped update", "[UpdateChannel]") +TEST_CASE("A manual check still offers a skipped update", "[UpdateChannel]") { UpdateRequest request; request.available = true; request.skipped = true; request.user_initiated = true; - request.automatic = false; - CHECK(update_action(request) == UpdateAction::InstallNow); + CHECK(update_action(request) == UpdateAction::Prefetch); + + // The verified artifact opens the dialog at once rather than being fetched again. + request.download_ready = true; + CHECK(update_action(request) == UpdateAction::NotifyReady); } -TEST_CASE("An automatic check prefetches the update", "[UpdateChannel]") +TEST_CASE("A verified download is reported ready instead of prefetched", "[UpdateChannel]") { const UpdateRequest request{true, false, false, true}; + CHECK(update_action(request) == UpdateAction::NotifyReady); +} + +TEST_CASE("An available update without a verified download is prefetched", "[UpdateChannel]") +{ + const UpdateRequest request{true, false, false, false}; + CHECK(update_action(request) == UpdateAction::Prefetch); } - -TEST_CASE("A non-automatic check installs the update now", "[UpdateChannel]") -{ - const UpdateRequest request{true, false, true, false}; - - CHECK(update_action(request) == UpdateAction::InstallNow); -} - -TEST_CASE("An update installs on quit only when it is downloaded and nothing is in the way", "[UpdateChannel]") -{ - CHECK(update_ready_to_install(true, false, false)); - CHECK_FALSE(update_ready_to_install(false, false, false)); - CHECK_FALSE(update_ready_to_install(true, true, false)); - CHECK_FALSE(update_ready_to_install(true, false, true)); -} diff --git a/tests/libslic3r/update_handoff_tests.cpp b/tests/libslic3r/update_handoff_tests.cpp index edaf80b8bd..a3f8fc8da0 100644 --- a/tests/libslic3r/update_handoff_tests.cpp +++ b/tests/libslic3r/update_handoff_tests.cpp @@ -77,6 +77,16 @@ fs::path marker_path(const fs::path& dir) return dir / (std::string(kAsset) + ".part.digest"); } +fs::path artifact_path(const fs::path& dir) +{ + return dir / kAsset; +} + +fs::path verified_path(const fs::path& dir) +{ + return dir / (std::string(kAsset) + ".part.verified"); +} + // A download directory holding a partial download of the artifact, recorded for `digest`. void write_partial_download(const fs::path& dir, const std::string& digest, std::size_t bytes) { @@ -140,6 +150,11 @@ TEST_CASE("Each install downloads into its own directory", "[UpdateHandoff]") CHECK(parent.parent_path() == shared); CHECK(parent.filename() == install_identity_hex("/opt/OrcaSlicer")); + // The download lock lives inside the install's own download directory, not beside the + // install lock: two copies must be able to prefetch at the same time. + CHECK(fs::path(updater_download_lock_path()) == parent / "download.lock"); + CHECK(fs::path(updater_download_lock_path()) != fs::path(updater_lock_path())); + // The job handed to the helper is keyed too: two copies share one cache, and one must // not overwrite the job the other one handed over. const std::string keyed_handoff = updater_handoff_path(); @@ -182,6 +197,11 @@ TEST_CASE("A download is named after the asset, inside its own directory", "[Upd CHECK(fs::path(paths.part) == fs::path("cache") / "OrcaSlicer_V2.5.0.AppImage.part"); CHECK(fs::path(paths.marker) == fs::path("cache") / "OrcaSlicer_V2.5.0.AppImage.part.digest"); CHECK(fs::path(paths.artifact) == fs::path("cache") / "OrcaSlicer_V2.5.0.AppImage"); + // The verified marker is its own file, beside the artifact it describes. + CHECK(fs::path(paths.verified) == fs::path("cache") / "OrcaSlicer_V2.5.0.AppImage.part.verified"); + CHECK(fs::path(paths.verified) != fs::path(paths.part)); + CHECK(fs::path(paths.verified) != fs::path(paths.marker)); + CHECK(fs::path(paths.verified) != fs::path(paths.artifact)); // No asset name to go by: the artifact still lands in the directory. CHECK(fs::path(download_paths("cache", "").artifact) == fs::path("cache") / "update.part"); @@ -312,9 +332,11 @@ TEST_CASE("A verified artifact recorded for another build is dropped", "[UpdateH TEST_CASE("A download the verifier rejected is fetched again", "[UpdateHandoff]") { ScopedTemporaryDir dir; - // The helper renames a verified download to its asset name and keeps the digest marker. + // The helper renames a verified download to its asset name and keeps the digest marker, + // then records the digest it verified. write_text(dir.path() / kAsset, std::string(kSize, 'x')); write_text(marker_path(dir.path()), kDigest); + CHECK(mark_download_verified(dir.string(), kAsset, kDigest)); CHECK(prepare_download_dir(dir.string(), kAsset, kDigest, kSize).complete); // A prefetch of another build is still writing its .part. @@ -324,12 +346,128 @@ TEST_CASE("A download the verifier rejected is fetched again", "[UpdateHandoff]" discard_download(dir.string(), kAsset); CHECK_FALSE(fs::exists(dir.path() / kAsset)); CHECK_FALSE(fs::exists(marker_path(dir.path()))); + CHECK_FALSE(fs::exists(verified_path(dir.path()))); CHECK(fs::exists(other)); // Nothing is left for the next run to take as complete, so it downloads the file again. CHECK_FALSE(prepare_download_dir(dir.string(), kAsset, kDigest, kSize).complete); } +TEST_CASE("A download needs the artifact at its expected size before it can be verified", "[UpdateHandoff]") +{ + ScopedTemporaryDir dir; + + // Nothing downloaded yet. + CHECK_FALSE(download_is_verified(dir.string(), kAsset, kDigest, kSize)); + + // The helper has recorded a verification, but the artifact is not there. + CHECK(mark_download_verified(dir.string(), kAsset, kDigest)); + CHECK_FALSE(download_is_verified(dir.string(), kAsset, kDigest, kSize)); + + // The final name is there, at the wrong size: the download was interrupted. + write_text(artifact_path(dir.path()), std::string(kSize - 1, 'x')); + CHECK_FALSE(download_is_verified(dir.string(), kAsset, kDigest, kSize)); + + // A release without a size can never be reported ready. + write_text(artifact_path(dir.path()), std::string(kSize, 'x')); + CHECK_FALSE(download_is_verified(dir.string(), kAsset, kDigest, 0)); +} + +TEST_CASE("A download at the right size is verified only by a marker naming its digest", "[UpdateHandoff]") +{ + ScopedTemporaryDir dir; + write_text(artifact_path(dir.path()), std::string(kSize, 'x')); + + // fetch_artifact() renames before verifying, so the artifact alone proves nothing. + CHECK_FALSE(download_is_verified(dir.string(), kAsset, kDigest, kSize)); + + CHECK(mark_download_verified(dir.string(), kAsset, kDigest)); + CHECK(read_text(verified_path(dir.path())) == kDigest); + CHECK(download_is_verified(dir.string(), kAsset, kDigest, kSize)); + + // A nightly reuses one asset name, so another build's digest leaves this download unverified. + write_text(verified_path(dir.path()), "sha256:2222"); + CHECK_FALSE(download_is_verified(dir.string(), kAsset, kDigest, kSize)); +} + +TEST_CASE("A recorded digest with a trailing line ending still verifies", "[UpdateHandoff]") +{ + ScopedTemporaryDir dir; + write_text(artifact_path(dir.path()), std::string(kSize, 'x')); + write_text(verified_path(dir.path()), std::string(kDigest) + "\r\n"); + + CHECK(download_is_verified(dir.string(), kAsset, kDigest, kSize)); +} + +TEST_CASE("A release without a digest verifies against an empty marker only", "[UpdateHandoff]") +{ + ScopedTemporaryDir dir; + write_text(artifact_path(dir.path()), std::string(kSize, 'x')); + + // No marker at all: nothing recorded this download as verified. + CHECK_FALSE(download_is_verified(dir.string(), kAsset, "", kSize)); + + write_text(verified_path(dir.path()), ""); + CHECK(download_is_verified(dir.string(), kAsset, "", kSize)); + // An empty marker does not stand in for a digest. + CHECK_FALSE(download_is_verified(dir.string(), kAsset, kDigest, kSize)); + + write_text(verified_path(dir.path()), kDigest); + CHECK_FALSE(download_is_verified(dir.string(), kAsset, "", kSize)); +} + +TEST_CASE("Removing a download takes the artifact and every marker beside it", "[UpdateHandoff]") +{ + ScopedTemporaryDir dir; + write_text(part_path(dir.path()), std::string(50, 'y')); + write_text(marker_path(dir.path()), kDigest); + write_text(artifact_path(dir.path()), std::string(kSize, 'x')); + CHECK(mark_download_verified(dir.string(), kAsset, kDigest)); + + remove_download(dir.string(), kAsset); + + CHECK_FALSE(fs::exists(part_path(dir.path()))); + CHECK_FALSE(fs::exists(marker_path(dir.path()))); + CHECK_FALSE(fs::exists(artifact_path(dir.path()))); + CHECK_FALSE(fs::exists(verified_path(dir.path()))); +} + +TEST_CASE("Removing a download from a directory that is not there does nothing", "[UpdateHandoff]") +{ + ScopedTemporaryDir dir; + const fs::path absent = dir.path() / "absent"; + + remove_download(absent.string(), kAsset); + CHECK_FALSE(fs::exists(absent)); +} + +TEST_CASE("The verified marker is swept with the artifact of another build", "[UpdateHandoff]") +{ + ScopedTemporaryDir dir; + write_text(artifact_path(dir.path()), std::string(kSize, 'x')); + write_text(marker_path(dir.path()), "sha256:2222"); + write_text(verified_path(dir.path()), "sha256:2222"); + + const DownloadPrep prep = prepare_download_dir(dir.string(), kAsset, kDigest, kSize); + CHECK_FALSE(prep.complete); + CHECK(contains(prep.dropped, artifact_path(dir.path()))); + CHECK_FALSE(fs::exists(artifact_path(dir.path()))); + CHECK_FALSE(fs::exists(verified_path(dir.path()))); +} + +TEST_CASE("The verified marker of this build's artifact is kept for the next launch", "[UpdateHandoff]") +{ + ScopedTemporaryDir dir; + write_text(artifact_path(dir.path()), std::string(kSize, 'x')); + write_text(marker_path(dir.path()), kDigest); + CHECK(mark_download_verified(dir.string(), kAsset, kDigest)); + + const DownloadPrep prep = prepare_download_dir(dir.string(), kAsset, kDigest, kSize); + CHECK(prep.complete); + CHECK(fs::exists(verified_path(dir.path()))); + CHECK(download_is_verified(dir.string(), kAsset, kDigest, kSize)); +} + TEST_CASE("The build a partial download belongs to is recorded next to it", "[UpdateHandoff]") { ScopedTemporaryDir dir;