diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index efa31fe3..fd66f248 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -278,6 +278,10 @@ jobs: # dylint-link binaries need glibc 2.38 or later, and 22.04 ships 2.35. dylint: runs-on: ubuntu-24.04 + env: + # The Tauri system packages, named once for the two steps that hand + # them to apt-get. + TAURI_APT_PACKAGES: libwebkit2gtk-4.1-dev libssl-dev librsvg2-dev steps: - uses: actions/checkout@v4 @@ -290,13 +294,175 @@ jobs: - name: Cache cargo uses: Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2 - # A mirror that does not answer makes apt-get wait for a long time, so - # the step fails after 20 minutes. It normally takes under one. - - name: Install Tauri system packages - timeout-minutes: 20 + # On this image apt can get the archives of the Tauri packages at + # about 60 kB/s, which is 20 minutes for 70 MB. It stays on that + # mirror, because its timeout fires only when no data arrives for 15 + # seconds: https://github.com/actions/runner-images/issues/14823 + # So this step and the three after it keep the archives in an Actions + # cache, and apt downloads only the ones the cache lacks. This step + # asks apt which archives it would download, and the cache key is a + # hash of their names, sizes, and hashes. The key leaves the addresses + # out, because the mirror differs from runner to runner. A mirror that + # does not answer makes apt-get wait for a long time, so the step + # fails after 10 minutes. + - name: List the archives of the Tauri system packages + id: apt-list + timeout-minutes: 10 + shell: bash run: | + set -euo pipefail + read -r -a packages <<< "$TAURI_APT_PACKAGES" + list="$RUNNER_TEMP/dylint-apt-archives.txt" sudo apt-get update - sudo apt-get install -y --no-install-recommends libwebkit2gtk-4.1-dev libssl-dev librsvg2-dev + # --print-uris prints one line for each archive apt would download: + # 'address' name size KIND:hash. ForceHash makes the hash the + # SHA-256 of the package index, where apt prints the MD5 sum by + # default. An archive whose line has no SHA-256 or SHA-512 is + # listed with `-` and is never cached. + name_form='^[A-Za-z0-9][A-Za-z0-9+._~%-]*\.deb$' + hash_form='^(SHA256:[0-9a-f]{64}|SHA512:[0-9a-f]{128})$' + : > "$list.unsorted" + unread=0 + if uris="$(sudo apt-get install --print-uris -qq -y --no-install-recommends \ + -o Acquire::ForceHash=sha256 "${packages[@]}")"; then + while read -r address name size hash _; do + if [[ "$name" =~ $name_form && "$size" =~ ^[0-9]+$ ]]; then + [[ "$hash" =~ $hash_form ]] || hash=- + printf '%s %s %s\n' "$name" "$size" "$hash" >> "$list.unsorted" + elif [ -n "$address" ]; then + unread=$((unread + 1)) + fi + done <<< "$uris" + else + echo "::warning::apt-get could not list the archives of the Tauri packages, so this run uses no cache of them." + fi + # One order on every runner, so one list gives one key. + LC_ALL=C sort -u "$list.unsorted" > "$list" || : > "$list" + if [ "$unread" -ne 0 ]; then + echo "::warning::$unread lines of apt-get --print-uris do not have the form 'address' name size hash, so their archives are not cached." + fi + echo "apt would download $(wc -l < "$list") archives, and $(grep -c -v ' -$' "$list" || true) of them have a hash this job checks." + digest="$(sha256sum < "$list")" || digest=unlisted + { + echo "list=$list" + echo "dir=$RUNNER_TEMP/dylint-apt-archives" + echo "hash=${digest%% *}" + } >> "$GITHUB_OUTPUT" + + # A run that finds no cache of its own list restores the newest cache + # of another list, and the next step takes from it the archives that + # are in both lists. A restore that fails, or that takes more than 10 + # minutes, is a run with no cache. + - name: Restore the archives of the Tauri system packages + id: apt-cache + continue-on-error: true + timeout-minutes: 10 + uses: actions/cache/restore@v6 + with: + path: ${{ steps.apt-list.outputs.dir }} + key: dylint-apt-archives-${{ steps.apt-list.outputs.hash }} + restore-keys: dylint-apt-archives- + + # apt takes an archive that is already in its directory when the size + # is right, and it checks no hash of it. So a restored file goes into + # that directory only when it has the hash that apt listed, and apt + # downloads every other archive itself. After the download the cache + # directory gets the listed archives and nothing else, each checked + # the same way, and that comes before the install in case the install + # removes them. A run with no cache downloads every archive, which + # takes about 20 minutes from a slow mirror, so the step fails after + # 30. With every archive in the cache it downloads nothing. + - name: Install Tauri system packages + id: apt-install + timeout-minutes: 30 + shell: bash + env: + APT_LIST: ${{ steps.apt-list.outputs.list }} + APT_CACHE: ${{ steps.apt-list.outputs.dir }} + run: | + set -euo pipefail + read -r -a packages <<< "$TAURI_APT_PACKAGES" + list="${APT_LIST:-}" + cache="${APT_CACHE:-}" + # The cache directory is removed below, so it must be this job's. + case "$cache" in "$RUNNER_TEMP"/?*) ;; *) cache= ;; esac + archives=/var/cache/apt/archives/ + eval "$(apt-config shell archives Dir::Cache::Archives/d || true)" + archives="${archives%/}" + [ -d "$archives" ] || archives=/var/cache/apt/archives + + # Succeeds when the file $2 has the hash $1, which is SHA256:hex + # or SHA512:hex. A hash of any other kind is not checked, so the + # file fails. + has_hash() { + local actual + case "$1" in + SHA256:*) actual="$(sha256sum < "$2")" || return 1 ;; + SHA512:*) actual="$(sha512sum < "$2")" || return 1 ;; + *) return 1 ;; + esac + [ "${1%%:*}:${actual%% *}" = "$1" ] + } + + staged=0 + refused=0 + if [ -n "$cache" ] && [ -d "$cache" ] && [ -f "$list" ]; then + while read -r name _ expected <&3; do + case "$expected" in SHA256:* | SHA512:*) ;; *) continue ;; esac + [ -f "$cache/$name" ] || continue + if ! has_hash "$expected" "$cache/$name"; then + refused=$((refused + 1)) + echo "The restored $name does not have the hash $expected." + elif sudo cp -- "$cache/$name" "$archives/$name"; then + staged=$((staged + 1)) + fi + done 3< "$list" + fi + echo "$staged archives come from the cache." + if [ "$refused" -ne 0 ]; then + echo "::warning::$refused restored archives do not have the hash that apt lists, so apt downloads them again. This job saves only archives that have that hash, so the restored cache is damaged or something else saved it. Delete that cache." + fi + + sudo apt-get install --download-only -y --no-install-recommends "${packages[@]}" + + kept=0 + wanted=0 + if [ -n "$cache" ] && [ -f "$list" ] && rm -rf -- "$cache" && mkdir -p -- "$cache"; then + while read -r name _ expected <&3; do + case "$expected" in SHA256:* | SHA512:*) ;; *) continue ;; esac + wanted=$((wanted + 1)) + if [ -f "$archives/$name" ] && sudo cat -- "$archives/$name" > "$cache/$name" && has_hash "$expected" "$cache/$name"; then + kept=$((kept + 1)) + else + rm -f -- "$cache/$name" || true + fi + done 3< "$list" + fi + complete=false + if [ "$wanted" -gt 0 ] && [ "$kept" -eq "$wanted" ]; then + complete=true + elif [ "$wanted" -gt 0 ]; then + echo "::warning::The cache directory holds $kept of the $wanted archives to cache, so this run saves no cache." + fi + echo "The cache directory holds $kept of the $wanted archives to cache." + echo "cache-complete=$complete" >> "$GITHUB_OUTPUT" + + sudo apt-get install -y --no-install-recommends "${packages[@]}" + + # A step of its own, before the lint, so that a run whose lint fails + # still saves the archives. A cache is never replaced, so a run that + # restored the cache of its own list saves nothing. A run whose cache + # directory lacks an archive of the list saves nothing either, because + # a later run would restore that cache and never fill it. A save that + # fails leaves the job as it is. + - name: Save the archives of the Tauri system packages + if: steps.apt-cache.outputs.cache-hit != 'true' && steps.apt-install.outputs.cache-complete == 'true' + continue-on-error: true + timeout-minutes: 10 + uses: actions/cache/save@v6 + with: + path: ${{ steps.apt-list.outputs.dir }} + key: dylint-apt-archives-${{ steps.apt-list.outputs.hash }} - uses: actions/setup-node@v4 with: diff --git a/Cargo.lock b/Cargo.lock index 12b665fc..e0ce945f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2415,12 +2415,6 @@ version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9" -[[package]] -name = "humantime" -version = "2.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "15cdd26707701c53297e2fa6afb323d55fbc1d0810c3aec078ae3ef0424c3c15" - [[package]] name = "hybrid-array" version = "0.4.14" @@ -8182,7 +8176,6 @@ version = "0.0.0" dependencies = [ "axum", "dunce", - "humantime", "percent-encoding", "serde", "serde_json", diff --git a/Cargo.toml b/Cargo.toml index 6252e917..5cac133d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -110,7 +110,6 @@ arc-swap = "1" # into the binary in release builds. interpolate-folder-path resolves the # $OUT_DIR prefix in the `folder` attribute. rust-embed = { version = "8", features = ["interpolate-folder-path"] } -humantime = "2" tempfile = "3" # Exact pin: turso is pre-1.0 and backs user-visible workspace files, so # upgrades are deliberate, reviewed changes rather than resolver drift. diff --git a/crates/workshop/workspace/Cargo.toml b/crates/workshop/workspace/Cargo.toml index f321db38..078f81b5 100644 --- a/crates/workshop/workspace/Cargo.toml +++ b/crates/workshop/workspace/Cargo.toml @@ -17,7 +17,6 @@ test-fixtures = [] [dependencies] axum.workspace = true dunce.workspace = true -humantime.workspace = true serde.workspace = true serde_json.workspace = true thiserror.workspace = true diff --git a/crates/workshop/workspace/src/lib.rs b/crates/workshop/workspace/src/lib.rs index 243a486d..6cda5483 100644 --- a/crates/workshop/workspace/src/lib.rs +++ b/crates/workshop/workspace/src/lib.rs @@ -31,6 +31,7 @@ mod blocking; mod error; mod handlers; mod handles; +mod rfc3339; #[cfg(test)] mod test_support; mod workspace; diff --git a/crates/workshop/workspace/src/rfc3339-tests.rs b/crates/workshop/workspace/src/rfc3339-tests.rs new file mode 100644 index 00000000..05358017 --- /dev/null +++ b/crates/workshop/workspace/src/rfc3339-tests.rs @@ -0,0 +1,90 @@ +//! Tests of the time text: fixed counts of seconds on each side of a +//! leap day, of a century year that has none, and of the end of the +//! year 9999, then the two rules for a clock outside those years. + +use std::time::Duration; + +use super::*; + +/// The last second that RFC 3339 has a text for, `9999-12-31T23:59:59Z`. +const LAST_SECOND_OF_9999: u64 = 253_402_300_799; + +#[test] +fn a_count_of_seconds_formats_as_its_utc_date_and_time() { + let cases = [ + (0, "1970-01-01T00:00:00Z"), + (951_782_400, "2000-02-29T00:00:00Z"), + (951_868_799, "2000-02-29T23:59:59Z"), + (1_000_000_000, "2001-09-09T01:46:40Z"), + (1_709_164_800, "2024-02-29T00:00:00Z"), + (LAST_SECOND_OF_9999, "9999-12-31T23:59:59Z"), + ]; + for (seconds, text) in cases { + assert_eq!(rfc3339_of_seconds(seconds), text, "{seconds} seconds"); + } +} + +#[test] +fn a_century_year_that_400_does_not_divide_has_no_leap_day() { + assert_eq!(rfc3339_of_seconds(4_107_542_399), "2100-02-28T23:59:59Z"); + assert_eq!( + rfc3339_of_seconds(4_107_542_400), + "2100-03-01T00:00:00Z", + "the second after February 28 of 2100 is in March" + ); +} + +#[test] +fn a_time_drops_its_part_of_a_second() { + let time = UNIX_EPOCH + Duration::new(1_000_000_000, 999_999_900); + assert_eq!(rfc3339_of(time), "2001-09-09T01:46:40Z"); +} + +#[test] +fn a_time_before_the_epoch_reads_as_the_epoch() { + let before = [ + Duration::from_nanos(100), + Duration::from_secs(1), + Duration::from_hours(24 * 365), + ]; + for earlier in before { + let time = UNIX_EPOCH + .checked_sub(earlier) + .expect("the system clock has a time this far before the epoch"); + assert_eq!( + rfc3339_of(time), + "1970-01-01T00:00:00Z", + "{earlier:?} before the epoch" + ); + } +} + +#[test] +fn a_year_past_9999_prints_every_digit_it_has() { + assert_eq!( + rfc3339_of_seconds(LAST_SECOND_OF_9999 + 1), + "10000-01-01T00:00:00Z", + "the second after the last one RFC 3339 has a text for" + ); + assert_eq!( + rfc3339_of_seconds(u64::MAX), + "584554051223-11-09T07:00:15Z", + "the largest count of seconds formats too" + ); +} + +#[test] +fn the_current_time_has_the_form_of_the_text() { + let now = now_rfc3339(); + let form: String = now + .chars() + .map(|character| { + if character.is_ascii_digit() { + '0' + } else { + character + } + }) + .collect(); + assert_eq!(form, "0000-00-00T00:00:00Z", "{now}"); +} diff --git a/crates/workshop/workspace/src/rfc3339.rs b/crates/workshop/workspace/src/rfc3339.rs new file mode 100644 index 00000000..97f4744c --- /dev/null +++ b/crates/workshop/workspace/src/rfc3339.rs @@ -0,0 +1,83 @@ +//! The time text the workspace stores: RFC 3339 in UTC to whole seconds, +//! `YYYY-MM-DDTHH:MM:SSZ`. +//! +//! [`now_rfc3339`] reads the wall clock, and [`rfc3339_of_seconds`] +//! formats a count of seconds with no clock, so a test pins exact texts. +//! Both are total: no clock reading and no count of seconds makes one +//! fail or panic. A time before the Unix epoch reads as the epoch, and +//! a year past 9999, for which RFC 3339 has no form, prints with every +//! digit it has. + +use std::time::{SystemTime, UNIX_EPOCH}; + +/// The seconds in one day of Unix time, which counts no leap second. +const SECONDS_PER_DAY: u64 = 86_400; + +/// The current time as RFC 3339 in UTC, whole seconds. It never fails: +/// a clock set before the Unix epoch reads as the epoch +/// ([`rfc3339_of`]), and a clock past the year 9999 prints its year with +/// every digit it has ([`rfc3339_of_seconds`]). +pub(crate) fn now_rfc3339() -> String { + rfc3339_of(SystemTime::now()) +} + +/// `time` as RFC 3339 in UTC, with the part of a second dropped. A time +/// before the Unix epoch reads as the epoch, `1970-01-01T00:00:00Z`: the +/// text records when something happened, and a clock set that far back +/// is wrong whatever the text says. +fn rfc3339_of(time: SystemTime) -> String { + // `duration_since` fails only for a time before the epoch. + let since_epoch = time.duration_since(UNIX_EPOCH).unwrap_or_default(); + rfc3339_of_seconds(since_epoch.as_secs()) +} + +/// The time `seconds` whole seconds after the Unix epoch, as +/// `YYYY-MM-DDTHH:MM:SSZ`. The year has four digits through +/// `9999-12-31T23:59:59Z`, which is 253,402,300,799 seconds. A later +/// time prints its year with every digit it has, as in +/// `10000-01-01T00:00:00Z`, which is no RFC 3339 text, and the largest +/// count there is still formats. +fn rfc3339_of_seconds(seconds: u64) -> String { + let (year, month, day) = civil_from_days(seconds / SECONDS_PER_DAY); + let second_of_day = seconds % SECONDS_PER_DAY; + format!( + "{year:04}-{month:02}-{day:02}T{:02}:{:02}:{:02}Z", + second_of_day / 3_600, + second_of_day / 60 % 60, + second_of_day % 60 + ) +} + +/// The year, the month, and the day of the month of the day `days` days +/// after 1970-01-01, in the Gregorian calendar. `days` is a count of +/// seconds divided by [`SECONDS_PER_DAY`], and no such number is large +/// enough to overflow a sum here. +fn civil_from_days(days: u64) -> (u64, u64, u64) { + // Howard Hinnant's `civil_from_days`: + // https://howardhinnant.github.io/date_algorithms.html#civil_from_days + // It counts days from 0000-03-01, so a year starts in March and a + // leap day is the last day of its year, and it works in eras of 400 + // years, which are 146,097 days each. A day at or after 1970-01-01 + // keeps every value here at or above zero, so the arithmetic is + // unsigned. + let shifted = days + 719_468; + let era = shifted / 146_097; + let day_of_era = shifted % 146_097; + let year_of_era = + (day_of_era - day_of_era / 1_460 + day_of_era / 36_524 - day_of_era / 146_096) / 365; + let day_of_year = day_of_era - (365 * year_of_era + year_of_era / 4 - year_of_era / 100); + let month_from_march = (5 * day_of_year + 2) / 153; + let day = day_of_year - (153 * month_from_march + 2) / 5 + 1; + let month = if month_from_march < 10 { + month_from_march + 3 + } else { + month_from_march - 9 + }; + // January and February belong to the year that started in March. + let year = year_of_era + era * 400 + u64::from(month <= 2); + (year, month, day) +} + +#[cfg(test)] +#[path = "rfc3339-tests.rs"] +mod tests; diff --git a/crates/workshop/workspace/src/workspace.rs b/crates/workshop/workspace/src/workspace.rs index f7cee8d4..86c876df 100644 --- a/crates/workshop/workspace/src/workspace.rs +++ b/crates/workshop/workspace/src/workspace.rs @@ -25,7 +25,8 @@ use serde::Serialize; use tokio::sync::watch; use crate::error::WorkspaceError; -use crate::workspace_file::{WindowState, now_rfc3339}; +use crate::rfc3339::now_rfc3339; +use crate::workspace_file::WindowState; mod backing; mod confine; diff --git a/crates/workshop/workspace/src/workspace_file.rs b/crates/workshop/workspace/src/workspace_file.rs index 0d57cc69..9aab0616 100644 --- a/crates/workshop/workspace/src/workspace_file.rs +++ b/crates/workshop/workspace/src/workspace_file.rs @@ -23,7 +23,6 @@ mod actor; mod siblings; pub(crate) mod ui_state_kv; -pub(crate) use actor::now_rfc3339; use actor::{COMMAND_QUEUE_DEPTH, Command, SCHEMA_V1}; use siblings::{already_taken, copy_siblings_or_clean_up, plan_siblings}; pub(crate) use ui_state_kv::{UI_STATE_KEYS, empty_ui_state}; diff --git a/crates/workshop/workspace/src/workspace_file/actor.rs b/crates/workshop/workspace/src/workspace_file/actor.rs index 9c7166f2..df651eb2 100644 --- a/crates/workshop/workspace/src/workspace_file/actor.rs +++ b/crates/workshop/workspace/src/workspace_file/actor.rs @@ -3,7 +3,6 @@ //! so the file mirrors memory without locks. use std::path::{Path, PathBuf}; -use std::time::SystemTime; use tokio::sync::{mpsc, oneshot}; @@ -13,6 +12,7 @@ use super::{ SUPPORTED_VERSION, WindowState, WorkspaceContents, WorkspaceFileError, io_failure, }; use crate::blocking::blocking; +use crate::rfc3339::now_rfc3339; /// A reply slot for a command that answers with success or failure. type Ack = oneshot::Sender>; @@ -328,11 +328,6 @@ fn window_json(state: &WindowState) -> String { serde_json::to_string(state).unwrap_or_default() } -/// The current time as RFC 3339 in UTC, whole seconds. -pub(crate) fn now_rfc3339() -> String { - humantime::format_rfc3339_seconds(SystemTime::now()).to_string() -} - /// Removes the `-wal` sidecar beside `path` when it holds no frames. fn remove_empty_wal_sidecar(path: &Path) { let sidecar = wal_sidecar_of(path); diff --git a/deny.toml b/deny.toml index eaa32d99..4ac7e957 100644 --- a/deny.toml +++ b/deny.toml @@ -14,13 +14,7 @@ version = 2 unmaintained = "workspace" yanked = "deny" # Reviewed advisories consciously accepted go here, each with a rationale. -ignore = [ - # humantime is marked unmaintained; the advisory reports no vulnerability - # and no safe upgrade. workshop-workspace uses one function of it, - # `format_rfc3339_seconds`, to stamp a time. The entry goes when the - # crate is replaced. - { id = "RUSTSEC-2026-0542", reason = "humantime is unmaintained with no vulnerability reported and no safe upgrade; remove this entry when the crate is replaced" }, -] +ignore = [] [licenses] version = 2