Skip to content

armgit / uzu

Public Imported from GitHub
0

A high-performance inference engine for AI models

update downloader

eugeneed1d6938306c
diff --git a/Cargo.lock b/Cargo.lock
index a035b3098..ce578e6f8 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -14,7 +14,7 @@ version = "0.8.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a15f179cd60c4584b8a8c596927aadc462e27f2ca70c04e0071964a73ba7a75"
dependencies = [
- "cfg-if 1.0.4",
+ "cfg-if",
"const-random",
"getrandom 0.3.4",
"once_cell",
@@ -73,7 +73,7 @@ checksum = "812947049edcd670a82cd5c73c3661d2e58468577ba8489de58e1a73c04cbd5d"
dependencies = [
"alsa-sys",
"bitflags 2.11.1",
- "cfg-if 1.0.4",
+ "cfg-if",
"libc",
]
@@ -285,33 +285,6 @@ dependencies = [
"tokio",
]
-[[package]]
-name = "async-fetcher"
-version = "0.12.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "9dff46cd7c152ba8bf85fc372af427c9d8d7b793f5d2809395f4a3c80013ca23"
-dependencies = [
- "async-shutdown",
- "derive-new",
- "derive_setters",
- "digest",
- "filetime",
- "futures",
- "hex",
- "http",
- "httpdate",
- "ifaces",
- "log",
- "md-5",
- "numtoa",
- "poolshark",
- "reqwest 0.12.28",
- "serde",
- "sha2",
- "thiserror 2.0.18",
- "tokio",
-]
-
[[package]]
name = "async-openai"
version = "0.35.0"
@@ -352,12 +325,6 @@ dependencies = [
"syn 2.0.117",
]
-[[package]]
-name = "async-shutdown"
-version = "0.2.2"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "f13fa00d3d9ba01f4c6ee77a73d784281cfa3340a9e1d43948e439921d24e934"
-
[[package]]
name = "async-trait"
version = "0.1.89"
@@ -787,12 +754,6 @@ version = "0.10.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e4b40c7323adcfc0a41c4b88143ed58346ff65a288fc144329c5c45e05d70c6"
-[[package]]
-name = "bitflags"
-version = "0.7.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "aad18937a628ec6abcd26d1489012cc0e18c21798210f491af69ded9b881106d"
-
[[package]]
name = "bitflags"
version = "1.3.2"
@@ -823,7 +784,7 @@ dependencies = [
"arrayref",
"arrayvec",
"cc",
- "cfg-if 1.0.4",
+ "cfg-if",
"constant_time_eq",
"cpufeatures 0.3.0",
]
@@ -1018,12 +979,6 @@ dependencies = [
"nom 7.1.3",
]
-[[package]]
-name = "cfg-if"
-version = "0.1.10"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
-
[[package]]
name = "cfg-if"
version = "1.0.4"
@@ -1042,7 +997,7 @@ version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6f8d983286843e49675a4b7a2d174efe136dc93a18d69130dd18198a6c167601"
dependencies = [
- "cfg-if 1.0.4",
+ "cfg-if",
"cpufeatures 0.3.0",
"rand_core 0.10.1",
]
@@ -1167,8 +1122,10 @@ dependencies = [
name = "cli-storage"
version = "0.4.6"
dependencies = [
+ "clap",
"crossterm",
"dotenvy",
+ "download-manager",
"futures-util",
"ratatui",
"shoji",
@@ -1266,7 +1223,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3fdb1325a1cece981e8a296ab8f0f9b63ae357bd0784a9faaf548cc7b480707a"
dependencies = [
"castaway",
- "cfg-if 1.0.4",
+ "cfg-if",
"itoa",
"rustversion",
"ryu",
@@ -1475,7 +1432,7 @@ version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9481c1c90cbf2ac953f07c8d4a58aa3945c425b7185c9154d67a65e4230da511"
dependencies = [
- "cfg-if 1.0.4",
+ "cfg-if",
]
[[package]]
@@ -1551,15 +1508,6 @@ dependencies = [
"crossbeam-utils",
]
-[[package]]
-name = "crossbeam-queue"
-version = "0.3.12"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "0f58bbc28f91df819d0aa2a2c00cd19754769c2fad90579b3592b1c9ba7a3115"
-dependencies = [
- "crossbeam-utils",
-]
-
[[package]]
name = "crossbeam-utils"
version = "0.8.21"
@@ -1711,16 +1659,6 @@ dependencies = [
"darling_macro 0.20.11",
]
-[[package]]
-name = "darling"
-version = "0.21.3"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "9cdf337090841a411e2a7f3deb9187445851f91b309c0c0a29e05f74a00a48c0"
-dependencies = [
- "darling_core 0.21.3",
- "darling_macro 0.21.3",
-]
-
[[package]]
name = "darling"
version = "0.23.0"
@@ -1745,20 +1683,6 @@ dependencies = [
"syn 2.0.117",
]
-[[package]]
-name = "darling_core"
-version = "0.21.3"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "1247195ecd7e3c85f83c8d2a366e4210d588e802133e1e355180a9870b517ea4"
-dependencies = [
- "fnv",
- "ident_case",
- "proc-macro2",
- "quote",
- "strsim",
- "syn 2.0.117",
-]
-
[[package]]
name = "darling_core"
version = "0.23.0"
@@ -1783,17 +1707,6 @@ dependencies = [
"syn 2.0.117",
]
-[[package]]
-name = "darling_macro"
-version = "0.21.3"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "d38308df82d1080de0afee5d069fa14b0326a88c14f15c5ccda35b4a6c414c81"
-dependencies = [
- "darling_core 0.21.3",
- "quote",
- "syn 2.0.117",
-]
-
[[package]]
name = "darling_macro"
version = "0.23.0"
@@ -1879,17 +1792,6 @@ dependencies = [
"serde_core",
]
-[[package]]
-name = "derive-new"
-version = "0.7.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "2cdc8d50f426189eef89dac62fabfa0abb27d5cc008f25bf4156a0203325becc"
-dependencies = [
- "proc-macro2",
- "quote",
- "syn 2.0.117",
-]
-
[[package]]
name = "derive_builder"
version = "0.20.2"
@@ -1943,18 +1845,6 @@ dependencies = [
"syn 2.0.117",
]
-[[package]]
-name = "derive_setters"
-version = "0.1.9"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "b7e6f6fa1f03c14ae082120b84b3c7fbd7b8588d924cf2d7c3daf9afd49df8b9"
-dependencies = [
- "darling 0.21.3",
- "proc-macro2",
- "quote",
- "syn 2.0.117",
-]
-
[[package]]
name = "digest"
version = "0.10.7"
@@ -2028,16 +1918,12 @@ checksum = "1aaf95b3e5c8f23aa320147307562d361db0ae0d51242340f558153b4eb2439b"
name = "download-manager"
version = "0.4.6"
dependencies = [
- "anyhow",
- "async-fetcher",
- "async-shutdown",
"async-trait",
"base64 0.22.1",
"block2",
"chrono",
"crc32c",
"futures-util",
- "indicatif",
"libc",
"mock-registry",
"objc-sys",
@@ -2051,10 +1937,7 @@ dependencies = [
"thiserror 2.0.18",
"tokio",
"tokio-stream",
- "tokio-util",
"tracing",
- "tracing-appender",
- "tracing-subscriber",
"uuid",
"wiremock",
]
@@ -2107,7 +1990,7 @@ version = "0.8.35"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "75030f3c4f45dafd7586dd6780965a8c7e8e285a5ecb86713e63a79c5b2766f3"
dependencies = [
- "cfg-if 1.0.4",
+ "cfg-if",
]
[[package]]
@@ -2174,7 +2057,7 @@ version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "26c7b13d0780cb82722fd59f6f57f925e143427e4a75313a6c77243bf5326ae6"
dependencies = [
- "cfg-if 1.0.4",
+ "cfg-if",
"home",
"windows-sys 0.59.0",
]
@@ -2306,17 +2189,6 @@ dependencies = [
"winapi",
]
-[[package]]
-name = "filetime"
-version = "0.2.27"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "f98844151eee8917efc50bd9e8318cb963ae8b297431495d3f758616ea5c57db"
-dependencies = [
- "cfg-if 1.0.4",
- "libc",
- "libredox",
-]
-
[[package]]
name = "find-msvc-tools"
version = "0.1.9"
@@ -2517,15 +2389,6 @@ dependencies = [
"slab",
]
-[[package]]
-name = "fxhash"
-version = "0.2.1"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "c31b6d751ae2c7f11320402d34e41349dd1016f8d5d45e48c4312bc8625af50c"
-dependencies = [
- "byteorder",
-]
-
[[package]]
name = "generational-box"
version = "0.5.6"
@@ -2560,7 +2423,7 @@ version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ff2abc00be7fca6ebc474524697ae276ad847ad0a6b3faa4bcb027e9a4614ad0"
dependencies = [
- "cfg-if 1.0.4",
+ "cfg-if",
"js-sys",
"libc",
"wasi",
@@ -2573,7 +2436,7 @@ version = "0.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "899def5c37c4fd7b2664648c28120ecec138e4d395b459e5ca34f9cce2dd77fd"
dependencies = [
- "cfg-if 1.0.4",
+ "cfg-if",
"js-sys",
"libc",
"r-efi 5.3.0",
@@ -2587,7 +2450,7 @@ version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0de51e6874e94e7bf76d726fc5d13ba782deca734ff60d5bb2fb2607c7406555"
dependencies = [
- "cfg-if 1.0.4",
+ "cfg-if",
"libc",
"r-efi 6.0.0",
"rand_core 0.10.1",
@@ -2656,9 +2519,9 @@ checksum = "be136d9dacc2a13cc70bb6c8f902b414fb2641f8db1314637c6b7933411a8f82"
[[package]]
name = "h2"
-version = "0.4.13"
+version = "0.4.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "2f44da3a8150a6703ed5d34e164b875fd14c2cdab9af1252a9a1020bde2bdc54"
+checksum = "171fefbc92fe4a4de27e0698d6a5b392d6a0e333506bc49133760b3bcf948733"
dependencies = [
"atomic-waker",
"bytes",
@@ -2680,7 +2543,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6ea2d84b969582b4b1864a92dc5d27cd2b77b622a8d79306834f1be5ba20d84b"
dependencies = [
"bytemuck",
- "cfg-if 1.0.4",
+ "cfg-if",
"crunchy",
"num-traits",
"zerocopy",
@@ -3074,16 +2937,6 @@ dependencies = [
"icu_properties",
]
-[[package]]
-name = "ifaces"
-version = "0.1.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "fa5cce6b4cae6792378aefb34152be19600949dde0041e7910430022f94b81f1"
-dependencies = [
- "libc",
- "nix 0.8.1",
-]
-
[[package]]
name = "ignore"
version = "0.4.25"
@@ -3210,7 +3063,7 @@ version = "0.1.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e0242819d153cba4b4b05a5a8f2a7e9bbf97b6055b2a002b395c96b5ff3c0222"
dependencies = [
- "cfg-if 1.0.4",
+ "cfg-if",
]
[[package]]
@@ -3266,16 +3119,6 @@ version = "2.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d98f6fed1fde3f8c21bc40a1abb88dd75e67924f9cffc3ef95607bad8017f8e2"
-[[package]]
-name = "iri-string"
-version = "0.7.12"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "25e659a4bb38e810ebc252e53b5814ff908a8c58c2a9ce2fae1bbec24cbf4e20"
-dependencies = [
- "memchr",
- "serde",
-]
-
[[package]]
name = "is-macro"
version = "0.3.7"
@@ -3361,7 +3204,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1a87aa2bb7d2af34197c04845522473242e1aa17c12f4935d5856491a7fb8c97"
dependencies = [
"cesu8",
- "cfg-if 1.0.4",
+ "cfg-if",
"combine",
"jni-sys 0.3.1",
"log",
@@ -3376,7 +3219,7 @@ version = "0.22.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5efd9a482cf3a427f00d6b35f14332adc7902ce91efb778580e180ff90fa3498"
dependencies = [
- "cfg-if 1.0.4",
+ "cfg-if",
"combine",
"jni-macros",
"jni-sys 0.4.1",
@@ -3444,7 +3287,7 @@ version = "0.3.97"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1840c94c045fbcf8ba2812c95db44499f7c64910a912551aaaa541decebcacf"
dependencies = [
- "cfg-if 1.0.4",
+ "cfg-if",
"futures-util",
"once_cell",
"wasm-bindgen",
@@ -3468,9 +3311,9 @@ dependencies = [
[[package]]
name = "jsonschema"
-version = "0.46.3"
+version = "0.46.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "cbe92a2f8b00686061eab5cdcfd6f382c27f2084456e7be90ae9f0fe4a30552a"
+checksum = "fc59d2432e047d6090ba1d83c782d0128bd6203857978218f5614dbd3287281f"
dependencies = [
"ahash",
"bytecount",
@@ -3567,7 +3410,7 @@ version = "0.8.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d7c4b02199fee7c5d21a5ae7d8cfa79a6ef5bb2fc834d6e9058e89c825efdc55"
dependencies = [
- "cfg-if 1.0.4",
+ "cfg-if",
"windows-link",
]
@@ -3577,7 +3420,7 @@ version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "754ca22de805bb5744484a5b151a9e1a8e837d5dc232c2d7d8c2e3492edc8b60"
dependencies = [
- "cfg-if 1.0.4",
+ "cfg-if",
"windows-link",
]
@@ -3593,10 +3436,7 @@ version = "0.1.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e02f3bb43d335493c96bf3fd3a321600bf6bd07ed34bc64118e9293bdffea46c"
dependencies = [
- "bitflags 2.11.1",
"libc",
- "plain",
- "redox_syscall 0.7.4",
]
[[package]]
@@ -3725,7 +3565,7 @@ version = "1.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c0aeb26bf5e836cc1c341c8106051b573f1766dfa05aa87f0b98be5e51b02303"
dependencies = [
- "nix 0.29.0",
+ "nix",
"winapi",
]
@@ -3791,20 +3631,10 @@ version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ea1f30cedd69f0a2954655f7188c6a834246d2bcf1e315e2ac40c4b24dc9519"
dependencies = [
- "cfg-if 1.0.4",
+ "cfg-if",
"rayon",
]
-[[package]]
-name = "md-5"
-version = "0.10.6"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "d89e7ee0cfbedfc4da3340218492196241d89eefb6dab27de5df917a6d2e78cf"
-dependencies = [
- "cfg-if 1.0.4",
- "digest",
-]
-
[[package]]
name = "memchr"
version = "2.8.0"
@@ -3998,9 +3828,9 @@ dependencies = [
[[package]]
name = "mtl-rs"
-version = "0.1.9"
+version = "0.1.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "e851d258c741f92ea3e92824afcfa888bbe814ac23d2825a9352baa5ce4f8ce0"
+checksum = "88e5c70772dfcf1f3f89d873fbd7e039d9d291b192b84df2b5f462e3d3597fba"
dependencies = [
"bitflags 2.11.1",
"block2",
@@ -4169,18 +3999,6 @@ version = "1.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "650eef8c711430f1a879fdd01d4745a7deea475becfb90269c06775983bbf086"
-[[package]]
-name = "nix"
-version = "0.8.1"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "47e49f6982987135c5e9620ab317623e723bd06738fd85377e8d55f57c8b6487"
-dependencies = [
- "bitflags 0.7.0",
- "cfg-if 0.1.10",
- "libc",
- "void",
-]
-
[[package]]
name = "nix"
version = "0.29.0"
@@ -4188,7 +4006,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "71e2746dc3a24dd78b3cfcb7be93368c6de9963d30f43a6a73998a9cf4b17b46"
dependencies = [
"bitflags 2.11.1",
- "cfg-if 1.0.4",
+ "cfg-if",
"cfg_aliases",
"libc",
"memoffset",
@@ -4196,9 +4014,9 @@ dependencies = [
[[package]]
name = "no_std_io2"
-version = "0.9.3"
+version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "b51ed7824b6e07d354605f4abb3d9d300350701299da96642ee084f5ce631550"
+checksum = "418abd1b6d34fbf6cae440dc874771b0525a604428704c76e48b29a5e67b8003"
dependencies = [
"memchr",
]
@@ -4406,12 +4224,6 @@ dependencies = [
"rustc-hash 2.1.2",
]
-[[package]]
-name = "numtoa"
-version = "0.3.1"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "9a6da8d50bd4f4b2e9788c44714a3fa4e465d33fd6a6ad70991db6eb30807dca"
-
[[package]]
name = "objc-sys"
version = "0.3.5"
@@ -4612,15 +4424,14 @@ dependencies = [
[[package]]
name = "openssl"
-version = "0.10.78"
+version = "0.10.79"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "f38c4372413cdaaf3cc79dd92d29d7d9f5ab09b51b10dded508fb90bb70b9222"
+checksum = "bf0b434746ee2832f4f0baf10137e1cabb18cbe6912c69e2e33263c45250f542"
dependencies = [
"bitflags 2.11.1",
- "cfg-if 1.0.4",
+ "cfg-if",
"foreign-types",
"libc",
- "once_cell",
"openssl-macros",
"openssl-sys",
]
@@ -4644,9 +4455,9 @@ checksum = "7c87def4c32ab89d880effc9e097653c8da5d6ef28e6b539d313baaacfbafcbe"
[[package]]
name = "openssl-sys"
-version = "0.9.114"
+version = "0.9.115"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "13ce1245cd07fcc4cfdb438f7507b0c7e4f3849a69fd84d52374c66d83741bb6"
+checksum = "158fe5b292746440aa6e7a7e690e55aeb72d41505e2804c23c6973ad0e9c9781"
dependencies = [
"cc",
"libc",
@@ -4710,9 +4521,9 @@ version = "0.9.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2621685985a2ebf1c516881c026032ac7deafcda1a2c9b7850dc81e3dfcb64c1"
dependencies = [
- "cfg-if 1.0.4",
+ "cfg-if",
"libc",
- "redox_syscall 0.5.18",
+ "redox_syscall",
"smallvec",
"windows-link",
]
@@ -4904,31 +4715,6 @@ dependencies = [
"miniz_oxide",
]
-[[package]]
-name = "poolshark"
-version = "0.2.7"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "b456f253445cabecd2b64634e59865b1f531c3a6375878d4e94bc0c513cc89e8"
-dependencies = [
- "crossbeam-queue",
- "fxhash",
- "indexmap 2.14.0",
- "poolshark_derive",
- "serde",
- "serde_derive",
- "triomphe",
-]
-
-[[package]]
-name = "poolshark_derive"
-version = "0.1.2"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "2e4b0a2610b718322d012c67426a17a7c21734cec3693ebc8e68f3e28119dba3"
-dependencies = [
- "quote",
- "syn 2.0.117",
-]
-
[[package]]
name = "portable-atomic"
version = "1.13.1"
@@ -5022,18 +4808,18 @@ dependencies = [
[[package]]
name = "profiling"
-version = "1.0.17"
+version = "1.0.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "3eb8486b569e12e2c32ad3e204dbaba5e4b5b216e9367044f25f1dba42341773"
+checksum = "3d595e54a326bc53c1c197b32d295e14b169e3cfeaa8dc82b529f947fba6bcf5"
dependencies = [
"profiling-procmacros",
]
[[package]]
name = "profiling-procmacros"
-version = "1.0.17"
+version = "1.0.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "52717f9a02b6965224f95ca2a81e2e0c5c43baacd28ca057577988930b6c3d5b"
+checksum = "4488a4a36b9a4ba6b9334a32a39971f77c1436ec82c38707bce707699cc3bbcb"
dependencies = [
"quote",
"syn 2.0.117",
@@ -5416,7 +5202,7 @@ version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "577c9b9f652b4c121fb25c6a391dd06406d3b092ba68827e6d2f09550edc54b3"
dependencies = [
- "cfg-if 1.0.4",
+ "cfg-if",
"crossterm",
"instability",
"ratatui-core",
@@ -5475,7 +5261,7 @@ dependencies = [
"av1-grain",
"bitstream-io",
"built",
- "cfg-if 1.0.4",
+ "cfg-if",
"interpolate_name",
"itertools 0.14.0",
"libc",
@@ -5557,15 +5343,6 @@ dependencies = [
"bitflags 2.11.1",
]
-[[package]]
-name = "redox_syscall"
-version = "0.7.4"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "f450ad9c3b1da563fb6948a8e0fb0fb9269711c9c73d9ea1de5058c79c8d643a"
-dependencies = [
- "bitflags 2.11.1",
-]
-
[[package]]
name = "redox_users"
version = "0.5.2"
@@ -5599,9 +5376,9 @@ dependencies = [
[[package]]
name = "referencing"
-version = "0.46.3"
+version = "0.46.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "e125f10bdcd507598c702daada18c47fe5bfba4d7a9545b015b5d432f7168ca3"
+checksum = "cb674900ca31acd75c4aaf63f48e43e719631c0539ea5a9e64163d1296bcb730"
dependencies = [
"ahash",
"fluent-uri",
@@ -5841,7 +5618,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a4689e6c2294d81e88dc6261c768b63bc4fcdb852be6d1352498b114f61383b7"
dependencies = [
"cc",
- "cfg-if 1.0.4",
+ "cfg-if",
"getrandom 0.2.17",
"libc",
"untrusted",
@@ -5878,7 +5655,7 @@ version = "0.26.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c845311f0ff7951c5506121a9ad75aec44d083c31583b2ea5a30bcb0b0abba0"
dependencies = [
- "cfg-if 1.0.4",
+ "cfg-if",
"glob",
"proc-macro-crate",
"proc-macro2",
@@ -6312,9 +6089,9 @@ dependencies = [
[[package]]
name = "serde_with"
-version = "3.18.0"
+version = "3.19.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "dd5414fad8e6907dbdd5bc441a50ae8d6e26151a03b1de04d89a5576de61d01f"
+checksum = "f05839ce67618e14a09b286535c0d9c94e85ef25469b0e13cb4f844e5593eb19"
dependencies = [
"base64 0.22.1",
"chrono",
@@ -6331,9 +6108,9 @@ dependencies = [
[[package]]
name = "serde_with_macros"
-version = "3.18.0"
+version = "3.19.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "d3db8978e608f1fe7357e211969fd9abdcae80bac1ba7a3369bb7eb6b404eb65"
+checksum = "cf2ebbe86054f9b45bc3881e865683ccfaccce97b9b4cb53f3039d67f355a334"
dependencies = [
"darling 0.23.0",
"proc-macro2",
@@ -6347,7 +6124,7 @@ version = "0.10.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e3bf829a2d51ab4a5ddf1352d8470c140cadc8301b2ae1789db023f01cedd6ba"
dependencies = [
- "cfg-if 1.0.4",
+ "cfg-if",
"cpufeatures 0.2.17",
"digest",
]
@@ -6364,7 +6141,7 @@ version = "0.10.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a7507d819769d01a365ab707794a4084392c824f54a7a6a7862f8c3d0892b283"
dependencies = [
- "cfg-if 1.0.4",
+ "cfg-if",
"cpufeatures 0.2.17",
"digest",
]
@@ -6476,9 +6253,9 @@ checksum = "e3a9fe34e3e7a50316060351f37187a3f546bce95496156754b601a5fa71b76e"
[[package]]
name = "siphasher"
-version = "1.0.2"
+version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "b2aa850e253778c88a04c3d7323b043aeda9d3e30d5971937c1855769763678e"
+checksum = "8ee5873ec9cce0195efcb7a4e9507a04cd49aec9c83d0389df45b1ef7ba2e649"
[[package]]
name = "slab"
@@ -6831,7 +6608,7 @@ dependencies = [
"libc",
"log",
"memmem",
- "nix 0.29.0",
+ "nix",
"num-derive",
"num-traits",
"ordered-float 4.6.0",
@@ -6927,7 +6704,7 @@ version = "1.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f60246a4944f24f6e018aa17cdeffb7818b76356965d03b07d6a9886e8962185"
dependencies = [
- "cfg-if 1.0.4",
+ "cfg-if",
]
[[package]]
@@ -7074,9 +6851,9 @@ dependencies = [
[[package]]
name = "tokio"
-version = "1.52.1"
+version = "1.52.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "b67dee974fe86fd92cc45b7a95fdd2f99a36a6d7b0d431a231178d3d670bbcc6"
+checksum = "110a78583f19d5cdb2c5ccf321d1290344e71313c6c37d43520d386027d18386"
dependencies = [
"bytes",
"libc",
@@ -7239,20 +7016,20 @@ dependencies = [
[[package]]
name = "tower-http"
-version = "0.6.8"
+version = "0.6.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "d4e6559d53cc268e5031cd8429d05415bc4cb4aefc4aa5d6cc35fbf5b924a1f8"
+checksum = "68d6fdd9f81c2819c9a8b0e0cd91660e7746a8e6ea2ba7c6b2b057985f6bcb51"
dependencies = [
"bitflags 2.11.1",
"bytes",
"futures-util",
"http",
"http-body",
- "iri-string",
"pin-project-lite",
"tower",
"tower-layer",
"tower-service",
+ "url",
]
[[package]]
@@ -7353,16 +7130,6 @@ dependencies = [
"tracing-subscriber",
]
-[[package]]
-name = "triomphe"
-version = "0.1.15"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "dd69c5aa8f924c7519d6372789a74eac5b94fb0f8fcf0d4a97eb0bfc3e785f39"
-dependencies = [
- "serde",
- "stable_deref_trait",
-]
-
[[package]]
name = "try-lock"
version = "0.2.5"
@@ -7762,6 +7529,7 @@ dependencies = [
"atomic",
"getrandom 0.4.2",
"js-sys",
+ "serde_core",
"sha1_smol",
"wasm-bindgen",
]
@@ -7815,6 +7583,7 @@ dependencies = [
"pyo3-stub-gen",
"reqwest 0.13.3",
"rodio",
+ "rstest",
"schemars 1.2.1",
"serde",
"serde-wasm-bindgen",
@@ -7866,12 +7635,6 @@ version = "0.9.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a"
-[[package]]
-name = "void"
-version = "1.0.2"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d"
-
[[package]]
name = "vsimd"
version = "0.8.0"
@@ -7945,7 +7708,7 @@ version = "0.2.120"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "df52b6d9b87e0c74c9edfa1eb2d9bf85e5d63515474513aa50fa181b3c4f5db1"
dependencies = [
- "cfg-if 1.0.4",
+ "cfg-if",
"once_cell",
"rustversion",
"serde",
diff --git a/Cargo.toml b/Cargo.toml
index 9810d8593..653e6811c 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -112,7 +112,7 @@ indicatif = "0.18"
inquire = "0.9"
console = "0.16"
rocket = { version = "0.5", features = ["json"] }
-uuid = { version = "1.20", features = ["v4", "v5"] }
+uuid = { version = "1.20", features = ["serde", "v4", "v5"] }
comfy-table = { version = "7.2", default-features = false }
autocxx = "0.30"
cxx = "1.0"
@@ -136,7 +136,6 @@ futures-util = "0.3.31"
http = "1.4.0"
httparse = "1.10.1"
stream_throttle = "0.5.1"
-async-fetcher = "0.12"
reqwest = { version = "0.13.2", features = [
"json",
"stream",
diff --git a/apps/playground/Tuist.swift b/apps/playground/Tuist.swift
new file mode 100644
index 000000000..ff8e64fa9
--- /dev/null
+++ b/apps/playground/Tuist.swift
@@ -0,0 +1,3 @@
+import ProjectDescription
+
+let config = Config()
diff --git a/crates/cli-storage/Cargo.toml b/crates/cli-storage/Cargo.toml
index c802e2b61..088cab137 100644
--- a/crates/cli-storage/Cargo.toml
+++ b/crates/cli-storage/Cargo.toml
@@ -23,3 +23,5 @@ tracing.workspace = true
tracing-subscriber.workspace = true
tracing-appender.workspace = true
dotenvy.workspace = true
+clap.workspace = true
+download-manager.workspace = true
diff --git a/crates/cli-storage/src/main.rs b/crates/cli-storage/src/main.rs
index 30d022a25..47a48bb61 100644
--- a/crates/cli-storage/src/main.rs
+++ b/crates/cli-storage/src/main.rs
@@ -8,20 +8,60 @@ mod ui;
use std::{io, sync::Arc};
+use clap::{Parser, ValueEnum};
use crossterm::{
execute,
terminal::{EnterAlternateScreen, LeaveAlternateScreen, disable_raw_mode, enable_raw_mode},
};
+use download_manager::FileDownloadManagerType;
use ratatui::{Terminal, backend::CrosstermBackend};
use uzu::engine::{Engine, EngineConfig};
use crate::{app::App, events::EventHandler};
+#[derive(Debug, Clone, Parser)]
+struct Cli {
+ #[arg(long, value_enum, default_value_t = DownloadManagerCliType::default())]
+ download_manager: DownloadManagerCliType,
+}
+
+#[derive(Debug, Clone, Copy, ValueEnum)]
+enum DownloadManagerCliType {
+ Apple,
+ Universal,
+}
+
+impl Default for DownloadManagerCliType {
+ fn default() -> Self {
+ FileDownloadManagerType::default().into()
+ }
+}
+
+impl From<FileDownloadManagerType> for DownloadManagerCliType {
+ fn from(download_manager_type: FileDownloadManagerType) -> Self {
+ match download_manager_type {
+ FileDownloadManagerType::Apple => Self::Apple,
+ FileDownloadManagerType::Universal => Self::Universal,
+ }
+ }
+}
+
+impl From<DownloadManagerCliType> for FileDownloadManagerType {
+ fn from(download_manager_type: DownloadManagerCliType) -> Self {
+ match download_manager_type {
+ DownloadManagerCliType::Apple => Self::Apple,
+ DownloadManagerCliType::Universal => Self::Universal,
+ }
+ }
+}
+
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
+ let cli = Cli::parse();
dotenvy::dotenv().ok();
let runtime = tokio::runtime::Handle::current();
- let engine = Arc::new(Engine::new(EngineConfig::default()).await?);
+ let engine =
+ Arc::new(Engine::new_with_download_manager_type(EngineConfig::default(), cli.download_manager.into()).await?);
// Setup terminal
enable_raw_mode()?;
diff --git a/crates/download-manager/Cargo.toml b/crates/download-manager/Cargo.toml
index 06b4a4552..28bd0a99d 100644
--- a/crates/download-manager/Cargo.toml
+++ b/crates/download-manager/Cargo.toml
@@ -12,21 +12,20 @@ license.workspace = true
tokio.workspace = true
tokio-stream.workspace = true
futures-util.workspace = true
-tokio-util.workspace = true
serde.workspace = true
serde_json.workspace = true
-anyhow.workspace = true
async-trait.workspace = true
thiserror.workspace = true
uuid.workspace = true
-async-shutdown.workspace = true
crc32c.workspace = true
base64.workspace = true
tracing.workspace = true
chrono.workspace = true
-async-fetcher.workspace = true
reqwest.workspace = true
+[target.'cfg(unix)'.dependencies]
+libc.workspace = true
+
[target.'cfg(target_vendor = "apple")'.dependencies]
objc2.workspace = true
objc2-foundation.workspace = true
@@ -35,10 +34,6 @@ objc-sys.workspace = true
[dev-dependencies]
tempfile.workspace = true
-libc.workspace = true
-indicatif.workspace = true
-tracing-subscriber.workspace = true
-tracing-appender.workspace = true
rstest.workspace = true
mock-registry.workspace = true
wiremock.workspace = true
diff --git a/crates/download-manager/examples/cross_platform_download.rs b/crates/download-manager/examples/cross_platform_download.rs
index 982746e98..0a22f6b6b 100644
--- a/crates/download-manager/examples/cross_platform_download.rs
+++ b/crates/download-manager/examples/cross_platform_download.rs
@@ -3,14 +3,14 @@ use std::path::PathBuf;
/// Cross-platform download example
/// This example shows how to use FileDownloadManager trait
/// which works on both Apple and non-Apple platforms
-use download_manager::{FileCheck, FileDownloadManagerType, create_download_manager};
+use download_manager::{FileCheck, FileDownloadManager};
use tokio::runtime::Handle;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let tokio_handle = Handle::current();
- let manager = create_download_manager(FileDownloadManagerType::default(), tokio_handle).await?;
+ let manager = <dyn FileDownloadManager>::system_default(tokio_handle).await?;
let url = "https://huggingface.co/Qwen/Qwen3.5-0.8B/resolve/main/tokenizer.json".to_string();
diff --git a/crates/download-manager/src/backends/apple/active_task.rs b/crates/download-manager/src/backends/apple/active_task.rs
new file mode 100644
index 000000000..764147991
--- /dev/null
+++ b/crates/download-manager/src/backends/apple/active_task.rs
@@ -0,0 +1,100 @@
+use std::{
+ path::{Path, PathBuf},
+ sync::{Arc, Mutex},
+};
+
+use objc2::rc::Retained;
+use objc2_foundation::NSURLSessionDownloadTask;
+use tokio::sync::oneshot::channel as tokio_oneshot_channel;
+
+use crate::{
+ DownloadId,
+ backends::apple::{
+ AppleBackend, AppleBackendError, AppleEventRegistry, AppleSinkKey, resume_data_handler::ResumeDataHandler,
+ task_ext::AppleDownloadTaskExt,
+ },
+ traits::{ActiveTask, CancelOutcome, DownloadBackend},
+};
+
+#[derive(Debug)]
+pub struct AppleActiveTask {
+ task: Retained<NSURLSessionDownloadTask>,
+ event_registry: AppleEventRegistry,
+ sink_key: AppleSinkKey,
+}
+
+impl AppleActiveTask {
+ pub fn new(
+ task: Retained<NSURLSessionDownloadTask>,
+ event_registry: AppleEventRegistry,
+ download_id: DownloadId,
+ ) -> Self {
+ let sink_key = (download_id, task.task_identifier());
+ Self {
+ task,
+ event_registry,
+ sink_key,
+ }
+ }
+
+ fn unregister_event_sink(&self) {
+ if let Ok(mut event_registry) = self.event_registry.lock() {
+ event_registry.remove(&self.sink_key);
+ }
+ }
+}
+
+impl Drop for AppleActiveTask {
+ fn drop(&mut self) {
+ self.unregister_event_sink();
+ self.task.cancel();
+ }
+}
+
+#[async_trait::async_trait]
+impl ActiveTask for AppleActiveTask {
+ type Backend = AppleBackend;
+
+ async fn pause(
+ self,
+ destination: &Path,
+ ) -> Result<PathBuf, <Self::Backend as DownloadBackend>::Error> {
+ let resume_artifact_path = destination.with_extension("resume_data");
+ let (resume_data_sender, resume_data_receiver) = tokio_oneshot_channel::<Box<[u8]>>();
+ let pending_resume_data_sender = Arc::new(Mutex::new(Some(resume_data_sender)));
+ self.unregister_event_sink();
+ {
+ let pending_resume_data_sender = Arc::clone(&pending_resume_data_sender);
+ let handler = ResumeDataHandler::new_bytes(move |resume_data_bytes| {
+ let resume_data_sender = match pending_resume_data_sender.lock() {
+ Ok(mut resume_data_sender) => resume_data_sender.take(),
+ Err(poisoned_sender) => {
+ let mut resume_data_sender = poisoned_sender.into_inner();
+ resume_data_sender.take()
+ },
+ };
+ if let Some(resume_data_sender) = resume_data_sender {
+ let _ = resume_data_sender.send(resume_data_bytes);
+ }
+ });
+ unsafe {
+ self.task.cancelByProducingResumeData(&handler);
+ }
+ }
+ let resume_data_bytes =
+ resume_data_receiver.await.map_err(|error| AppleBackendError::ResumeData(error.to_string()))?;
+ tokio::fs::write(&resume_artifact_path, resume_data_bytes)
+ .await
+ .map_err(|error| AppleBackendError::Io(error.to_string()))?;
+ Ok(resume_artifact_path)
+ }
+
+ async fn cancel(
+ self,
+ _destination: &Path,
+ ) -> CancelOutcome {
+ self.unregister_event_sink();
+ self.task.cancel();
+ CancelOutcome::BestEffort
+ }
+}
diff --git a/crates/download-manager/src/backends/apple/backend.rs b/crates/download-manager/src/backends/apple/backend.rs
new file mode 100644
index 000000000..a52d1cbc1
--- /dev/null
+++ b/crates/download-manager/src/backends/apple/backend.rs
@@ -0,0 +1,86 @@
+use std::{path::Path, sync::Arc};
+
+use objc2_foundation::NSURLSessionTaskState;
+
+use crate::{
+ DownloadError,
+ backends::{
+ apple::{
+ AppleActiveTask, AppleBackendContext, AppleBackendError, resume_data_parser, task_ext::AppleDownloadTaskExt,
+ },
+ common::{self, InitialTaskAttachment},
+ },
+ lock_manager::DestinationLockLease,
+ traits::{ActiveDownloadGeneration, BackendEventSender, DownloadBackend, DownloadConfig},
+};
+
+#[derive(Clone, Debug, Default)]
+pub struct AppleBackend;
+
+impl DownloadBackend for AppleBackend {
+ type Context = AppleBackendContext;
+ type ActiveTask = AppleActiveTask;
+ type Error = AppleBackendError;
+
+ fn read_resume_progress(part_path: &Path) -> Option<u64> {
+ resume_data_parser::read_resume_progress(part_path)
+ }
+}
+
+#[async_trait::async_trait]
+impl common::Backend for AppleBackend {
+ const RESUME_ARTIFACT_EXTENSION: &'static str = "resume_data";
+ const SUPPORTS_INITIAL_TASK_ATTACHMENT: bool = true;
+
+ fn manager_suffix() -> &'static str {
+ "apple"
+ }
+
+ fn create_context(tokio_handle: tokio::runtime::Handle) -> Result<Self::Context, DownloadError> {
+ Ok(AppleBackendContext::new(tokio_handle))
+ }
+
+ async fn initial_task_attachment(
+ context: &Self::Context,
+ config: Arc<DownloadConfig>,
+ generation: ActiveDownloadGeneration,
+ backend_event_sender: BackendEventSender,
+ _destination_lease: &DestinationLockLease,
+ ) -> Result<InitialTaskAttachment<Self>, DownloadError> {
+ let Some(task) = context
+ .claim_matching_download_task(&config)
+ .await
+ .map_err(|error| DownloadError::Backend(error.to_string()))?
+ else {
+ return Ok(InitialTaskAttachment::None);
+ };
+
+ match task.state() {
+ NSURLSessionTaskState::Running | NSURLSessionTaskState::Suspended => {
+ let initial_downloaded_bytes = task.count_of_bytes_received();
+ let total_bytes = match task.count_of_bytes_expected_to_receive() {
+ 0 => config.expected_bytes,
+ total_bytes => Some(total_bytes),
+ };
+ context.attach_existing_task(&task, Arc::clone(&config), generation, backend_event_sender);
+ if matches!(task.state(), NSURLSessionTaskState::Suspended) {
+ task.resume();
+ }
+ Ok(InitialTaskAttachment::Downloading {
+ active_task: AppleActiveTask::new(task, context.event_registry(), config.download_id),
+ initial_downloaded_bytes,
+ total_bytes,
+ })
+ },
+ NSURLSessionTaskState::Completed | NSURLSessionTaskState::Canceling => Ok(InitialTaskAttachment::None),
+ _ => Ok(InitialTaskAttachment::None),
+ }
+ }
+
+ async fn has_initial_task_to_claim(
+ context: &Self::Context,
+ config: &DownloadConfig,
+ ) -> Result<bool, DownloadError> {
+ context.has_download_task_to_claim(config).await.map_err(|error| DownloadError::Backend(error.to_string()))
+ }
+}
diff --git a/crates/download-manager/src/backends/apple/context.rs b/crates/download-manager/src/backends/apple/context.rs
new file mode 100644
index 000000000..cd1a02e59
--- /dev/null
+++ b/crates/download-manager/src/backends/apple/context.rs
@@ -0,0 +1,281 @@
+use std::{
+ collections::HashMap,
+ path::Path,
+ sync::{Arc, Mutex},
+};
+
+use objc2::{rc::Retained, runtime::ProtocolObject};
+use objc2_foundation::{
+ NSBundle, NSData, NSString, NSURL, NSURLSession, NSURLSessionConfiguration, NSURLSessionDelegate,
+ NSURLSessionDownloadTask, NSURLSessionTaskState,
+};
+use tokio::{runtime::Handle as TokioHandle, sync::oneshot::channel as tokio_oneshot_channel};
+
+use crate::{
+ DownloadInfo, FileCheck,
+ backends::apple::{
+ AppleActiveTask, AppleBackend, AppleBackendError, AppleEventRegistry, AppleEventSink, AppleGetTasksHandler,
+ AppleSessionDelegate, task_ext::AppleDownloadTaskExt,
+ },
+ lock_manager::DestinationLockLease,
+ traits::{ActiveDownloadGeneration, BackendContext, BackendEventSender, DownloadConfig},
+};
+
+pub struct AppleBackendContext {
+ session: Retained<NSURLSession>,
+ _delegate: Retained<AppleSessionDelegate>,
+ _delegate_protocol_object: Retained<ProtocolObject<dyn NSURLSessionDelegate>>,
+ event_registry: AppleEventRegistry,
+ tokio_handle: TokioHandle,
+}
+
+impl std::fmt::Debug for AppleBackendContext {
+ fn fmt(
+ &self,
+ formatter: &mut std::fmt::Formatter<'_>,
+ ) -> std::fmt::Result {
+ formatter.debug_struct("AppleBackendContext").finish_non_exhaustive()
+ }
+}
+
+impl AppleBackendContext {
+ pub fn new(tokio_handle: TokioHandle) -> Self {
+ let event_registry = Arc::new(Mutex::new(HashMap::new()));
+ let delegate = AppleSessionDelegate::new(Arc::clone(&event_registry));
+ let delegate_protocol_object = AppleSessionDelegate::protocol_object(delegate.clone());
+ let session = unsafe {
+ NSURLSession::sessionWithConfiguration_delegate_delegateQueue(
+ &automatic_session_configuration(),
+ Some(&delegate_protocol_object),
+ None,
+ )
+ };
+
+ Self {
+ session,
+ _delegate: delegate,
+ _delegate_protocol_object: delegate_protocol_object,
+ event_registry,
+ tokio_handle,
+ }
+ }
+
+ pub(crate) async fn claim_matching_download_task(
+ &self,
+ config: &DownloadConfig,
+ ) -> Result<Option<Retained<NSURLSessionDownloadTask>>, AppleBackendError> {
+ self.find_download_task(config).await
+ }
+
+ pub(crate) async fn has_download_task_to_claim(
+ &self,
+ config: &DownloadConfig,
+ ) -> Result<bool, AppleBackendError> {
+ let download_tasks = self.download_tasks().await?;
+ Ok(download_tasks
+ .iter()
+ .any(|task| task.download_id() == Some(config.download_id) && is_live_task_state(task.state())))
+ }
+
+ #[cfg(test)]
+ #[allow(dead_code)]
+ pub(crate) fn event_sink_count_for_download(
+ &self,
+ download_id: crate::DownloadId,
+ ) -> usize {
+ self.event_registry
+ .lock()
+ .map(|registry| registry.keys().filter(|(id, _)| *id == download_id).count())
+ .unwrap_or(0)
+ }
+
+ #[cfg(test)]
+ #[allow(dead_code)]
+ pub(crate) fn event_sink_task_identifiers_for_download(
+ &self,
+ download_id: crate::DownloadId,
+ ) -> Vec<u64> {
+ self.event_registry
+ .lock()
+ .map(|registry| {
+ registry
+ .keys()
+ .filter_map(|(id, task_identifier)| (*id == download_id).then_some(*task_identifier))
+ .collect()
+ })
+ .unwrap_or_default()
+ }
+
+ async fn find_download_task(
+ &self,
+ config: &DownloadConfig,
+ ) -> Result<Option<Retained<NSURLSessionDownloadTask>>, AppleBackendError> {
+ let download_tasks = self.download_tasks().await?;
+ Ok(select_matching_download_task(download_tasks, config))
+ }
+
+ async fn download_tasks(&self) -> Result<Box<[Retained<NSURLSessionDownloadTask>]>, AppleBackendError> {
+ let (download_tasks_sender, download_tasks_receiver) = tokio_oneshot_channel();
+ let pending_download_tasks_sender = Arc::new(Mutex::new(Some(download_tasks_sender)));
+ let handler = AppleGetTasksHandler::new({
+ let pending_download_tasks_sender = Arc::clone(&pending_download_tasks_sender);
+ move |_data_tasks, _upload_tasks, download_tasks| {
+ let download_tasks_sender = match pending_download_tasks_sender.lock() {
+ Ok(mut download_tasks_sender) => download_tasks_sender.take(),
+ Err(poisoned_sender) => {
+ let mut download_tasks_sender = poisoned_sender.into_inner();
+ download_tasks_sender.take()
+ },
+ };
+ if let Some(download_tasks_sender) = download_tasks_sender {
+ let _ = download_tasks_sender.send(download_tasks);
+ }
+ }
+ });
+ unsafe {
+ self.session.getTasksWithCompletionHandler(&handler);
+ }
+ download_tasks_receiver.await.map_err(|error| {
+ AppleBackendError::TaskEnumeration(format!("URLSession task enumeration callback dropped: {error}"))
+ })
+ }
+
+ pub fn attach_existing_task(
+ &self,
+ task: &NSURLSessionDownloadTask,
+ config: Arc<DownloadConfig>,
+ generation: ActiveDownloadGeneration,
+ backend_event_sender: BackendEventSender,
+ ) {
+ self.prepare_task(task, config, generation, backend_event_sender);
+ }
+
+ pub(crate) fn event_registry(&self) -> AppleEventRegistry {
+ Arc::clone(&self.event_registry)
+ }
+}
+
+fn select_matching_download_task(
+ download_tasks: Box<[Retained<NSURLSessionDownloadTask>]>,
+ config: &DownloadConfig,
+) -> Option<Retained<NSURLSessionDownloadTask>> {
+ let mut live_match = None;
+ for task in download_tasks {
+ if task.download_id() != Some(config.download_id) {
+ continue;
+ }
+ let task_state = task.state();
+ if !is_live_task_state(task_state) {
+ continue;
+ }
+ let info = task.download_info();
+ let source_url_matches = info.as_ref().map(|info| info.source_url == config.source_url).unwrap_or(false);
+ let crc_matches = info.as_ref().map(|info| info.crc32c == config.file_check.expected_crc()).unwrap_or(false);
+ if !(source_url_matches && crc_matches) {
+ task.cancel();
+ continue;
+ }
+ if live_match.is_none() {
+ live_match = Some(task);
+ } else {
+ task.cancel();
+ }
+ }
+ live_match
+}
+
+fn is_live_task_state(state: NSURLSessionTaskState) -> bool {
+ matches!(state, NSURLSessionTaskState::Running | NSURLSessionTaskState::Suspended)
+}
+
+fn automatic_session_configuration() -> Retained<NSURLSessionConfiguration> {
+ let bundle_id = NSBundle::mainBundle().bundleIdentifier().unwrap_or_default().to_string();
+ if bundle_id.is_empty() {
+ NSURLSessionConfiguration::ephemeralSessionConfiguration()
+ } else {
+ let session_id = NSString::from_str(&format!("{bundle_id}.trymirai.download-manager"));
+ let configuration = NSURLSessionConfiguration::backgroundSessionConfigurationWithIdentifier(&session_id);
+ configuration.setSessionSendsLaunchEvents(true);
+ configuration.setDiscretionary(false);
+ configuration.setWaitsForConnectivity(true);
+ configuration
+ }
+}
+
+unsafe impl Send for AppleBackendContext {}
+unsafe impl Sync for AppleBackendContext {}
+
+#[async_trait::async_trait]
+impl BackendContext for AppleBackendContext {
+ type Backend = AppleBackend;
+
+ async fn download(
+ &self,
+ config: Arc<DownloadConfig>,
+ generation: ActiveDownloadGeneration,
+ backend_event_sender: BackendEventSender,
+ _destination_lease: &DestinationLockLease,
+ ) -> Result<AppleActiveTask, AppleBackendError> {
+ let ns_url = NSURL::URLWithString(&NSString::from_str(&config.source_url)).ok_or(AppleBackendError::BadUrl)?;
+ let task = self.session.downloadTaskWithURL(&ns_url);
+ self.prepare_task(&task, Arc::clone(&config), generation, backend_event_sender);
+ task.resume();
+ Ok(AppleActiveTask::new(task, Arc::clone(&self.event_registry), config.download_id))
+ }
+
+ async fn resume(
+ &self,
+ config: Arc<DownloadConfig>,
+ generation: ActiveDownloadGeneration,
+ resume_artifact_path: &Path,
+ backend_event_sender: BackendEventSender,
+ _destination_lease: &DestinationLockLease,
+ ) -> Result<AppleActiveTask, AppleBackendError> {
+ let resume_data =
+ tokio::fs::read(resume_artifact_path).await.map_err(|error| AppleBackendError::Io(error.to_string()))?;
+ let task = if resume_data.is_empty() {
+ let ns_url =
+ NSURL::URLWithString(&NSString::from_str(&config.source_url)).ok_or(AppleBackendError::BadUrl)?;
+ self.session.downloadTaskWithURL(&ns_url)
+ } else {
+ let ns_data = NSData::with_bytes(&resume_data);
+ self.session.downloadTaskWithResumeData(&ns_data)
+ };
+ self.prepare_task(&task, Arc::clone(&config), generation, backend_event_sender);
+ task.resume();
+ Ok(AppleActiveTask::new(task, Arc::clone(&self.event_registry), config.download_id))
+ }
+}
+
+impl AppleBackendContext {
+ fn prepare_task(
+ &self,
+ task: &NSURLSessionDownloadTask,
+ config: Arc<DownloadConfig>,
+ generation: ActiveDownloadGeneration,
+ backend_event_sender: BackendEventSender,
+ ) {
+ let download_info = match &config.file_check {
+ FileCheck::CRC(crc) => DownloadInfo::with_crc(
+ config.source_url.clone(),
+ config.destination.to_string_lossy().to_string(),
+ crc.clone(),
+ ),
+ FileCheck::None => {
+ DownloadInfo::new(config.source_url.clone(), config.destination.to_string_lossy().to_string())
+ },
+ };
+ task.set_download_info(&download_info);
+ if let Ok(mut registry) = self.event_registry.lock() {
+ registry.insert(
+ (config.download_id, task.task_identifier()),
+ AppleEventSink {
+ generation,
+ destination: config.destination.clone(),
+ backend_event_sender,
+ tokio_handle: self.tokio_handle.clone(),
+ },
+ );
+ }
+ }
+}
diff --git a/crates/download-manager/src/backends/apple/delegate.rs b/crates/download-manager/src/backends/apple/delegate.rs
new file mode 100644
index 000000000..d6b26120c
--- /dev/null
+++ b/crates/download-manager/src/backends/apple/delegate.rs
@@ -0,0 +1,171 @@
+use std::{
+ collections::HashMap,
+ path::PathBuf,
+ sync::{Arc, Mutex},
+};
+
+use objc2::{
+ ClassType, DefinedClass, define_class, msg_send,
+ rc::{Allocated, Retained},
+ runtime::ProtocolObject,
+};
+use objc2_foundation::{
+ NSError, NSObject, NSObjectProtocol, NSURL, NSURLSession, NSURLSessionDelegate, NSURLSessionDownloadDelegate,
+ NSURLSessionDownloadTask, NSURLSessionTask, NSURLSessionTaskDelegate,
+};
+use tokio::runtime::Handle as TokioHandle;
+
+use crate::{
+ DownloadId,
+ backends::apple::task_ext::AppleDownloadTaskExt,
+ file_download_task_actor::BackendEvent,
+ traits::{ActiveDownloadGeneration, BackendEventSender},
+};
+
+#[derive(Clone, Debug)]
+pub struct AppleEventSink {
+ pub generation: ActiveDownloadGeneration,
+ pub destination: PathBuf,
+ pub backend_event_sender: BackendEventSender,
+ pub tokio_handle: TokioHandle,
+}
+
+pub type AppleSinkKey = (DownloadId, u64);
+pub type AppleEventRegistry = Arc<Mutex<HashMap<AppleSinkKey, AppleEventSink>>>;
+
+#[derive(Debug, Clone)]
+pub struct AppleSessionDelegateIvars {
+ pub event_registry: AppleEventRegistry,
+}
+
+define_class!(
+ #[unsafe(super(NSObject))]
+ #[derive(Debug)]
+ #[ivars = AppleSessionDelegateIvars]
+ pub struct AppleSessionDelegate;
+
+ unsafe impl NSObjectProtocol for AppleSessionDelegate {}
+
+ unsafe impl NSURLSessionDelegate for AppleSessionDelegate {
+ #[unsafe(method(URLSession:didBecomeInvalidWithError:))]
+ fn did_become_invalid_with_error(
+ &self,
+ _session: &NSURLSession,
+ _error: Option<&NSError>,
+ ) {
+ }
+ }
+
+ unsafe impl NSURLSessionTaskDelegate for AppleSessionDelegate {
+ #[unsafe(method(URLSession:task:didCompleteWithError:))]
+ fn did_complete_with_error(
+ &self,
+ _session: &NSURLSession,
+ task: &NSURLSessionTask,
+ error: Option<&NSError>,
+ ) {
+ let Some(download_task) = task.downcast_ref::<NSURLSessionDownloadTask>() else {
+ return;
+ };
+ let Some(error) = error else {
+ return;
+ };
+ let Some(download_id) = download_task.download_id() else {
+ return;
+ };
+ let key: AppleSinkKey = (download_id, download_task.task_identifier());
+ let Some(sink) =
+ Self::ivars(self).event_registry.lock().ok().and_then(|mut registry| registry.remove(&key))
+ else {
+ return;
+ };
+ let message = error.localizedDescription().to_string();
+ sink.tokio_handle.clone().spawn(async move {
+ let _ = sink.backend_event_sender.send_terminal(BackendEvent::error(sink.generation, message)).await;
+ });
+ }
+ }
+
+ unsafe impl NSURLSessionDownloadDelegate for AppleSessionDelegate {
+ #[unsafe(method(URLSession:downloadTask:didFinishDownloadingToURL:))]
+ fn did_finish_downloading_to_url(
+ &self,
+ _session: &NSURLSession,
+ download_task: &NSURLSessionDownloadTask,
+ location: &NSURL,
+ ) {
+ let Some(download_id) = download_task.download_id() else {
+ return;
+ };
+ let key: AppleSinkKey = (download_id, download_task.task_identifier());
+ let Some(sink) =
+ Self::ivars(self).event_registry.lock().ok().and_then(|mut registry| registry.remove(&key))
+ else {
+ return;
+ };
+ let Some(temporary_path) = location.path().map(|path| PathBuf::from(path.to_string())) else {
+ return;
+ };
+
+ // Move synchronously: NSURLSession deletes the temp file as soon as this method returns.
+ if let Some(parent) = sink.destination.parent() {
+ let _ = std::fs::create_dir_all(parent);
+ }
+ let move_result = std::fs::rename(&temporary_path, &sink.destination).or_else(|_| {
+ std::fs::copy(&temporary_path, &sink.destination)?;
+ let _ = std::fs::remove_file(&temporary_path);
+ Ok::<(), std::io::Error>(())
+ });
+
+ let terminal_event = match move_result {
+ Ok(()) => BackendEvent::completed(sink.generation),
+ Err(error) => BackendEvent::error(sink.generation, format!("move into destination failed: {error}")),
+ };
+ sink.tokio_handle.clone().spawn(async move {
+ let _ = sink.backend_event_sender.send_terminal(terminal_event).await;
+ });
+ }
+
+ #[unsafe(method(URLSession:downloadTask:didWriteData:totalBytesWritten:totalBytesExpectedToWrite:))]
+ fn did_write_data(
+ &self,
+ _session: &NSURLSession,
+ download_task: &NSURLSessionDownloadTask,
+ _bytes_written_since_last_callback: i64,
+ cumulative_bytes_written: i64,
+ total_expected_bytes_to_write: i64,
+ ) {
+ let Some(download_id) = download_task.download_id() else {
+ return;
+ };
+ let key: AppleSinkKey = (download_id, download_task.task_identifier());
+ let Some(sink) =
+ Self::ivars(self).event_registry.lock().ok().and_then(|registry| registry.get(&key).cloned())
+ else {
+ return;
+ };
+ let downloaded_bytes = cumulative_bytes_written.max(0) as u64;
+ let total_bytes = (total_expected_bytes_to_write > 0).then_some(total_expected_bytes_to_write as u64);
+
+ sink.tokio_handle.clone().spawn(async move {
+ sink.backend_event_sender.send_progress(sink.generation, downloaded_bytes, total_bytes).await;
+ });
+ }
+ }
+);
+
+impl AppleSessionDelegate {
+ pub fn new(event_registry: AppleEventRegistry) -> Retained<Self> {
+ unsafe {
+ let allocated: Allocated<Self> = msg_send![Self::class(), alloc];
+ let allocated_with_ivars = allocated.set_ivars(AppleSessionDelegateIvars {
+ event_registry,
+ });
+ msg_send![super(allocated_with_ivars), init]
+ }
+ }
+
+ pub fn protocol_object(delegate: Retained<Self>) -> Retained<ProtocolObject<dyn NSURLSessionDelegate>> {
+ ProtocolObject::<dyn NSURLSessionDelegate>::from_retained(delegate)
+ }
+}
diff --git a/crates/download-manager/src/backends/apple/error.rs b/crates/download-manager/src/backends/apple/error.rs
new file mode 100644
index 000000000..3efbc188f
--- /dev/null
+++ b/crates/download-manager/src/backends/apple/error.rs
@@ -0,0 +1,11 @@
+#[derive(thiserror::Error, Clone, Debug, PartialEq, Eq)]
+pub enum AppleBackendError {
+ #[error("bad url")]
+ BadUrl,
+ #[error("resume data error: {0}")]
+ ResumeData(String),
+ #[error("io error: {0}")]
+ Io(String),
+ #[error("task enumeration error: {0}")]
+ TaskEnumeration(String),
+}
diff --git a/crates/download-manager/src/backends/apple/get_tasks_handler.rs b/crates/download-manager/src/backends/apple/get_tasks_handler.rs
new file mode 100644
index 000000000..b96d4b10d
--- /dev/null
+++ b/crates/download-manager/src/backends/apple/get_tasks_handler.rs
@@ -0,0 +1,54 @@
+use std::{ops::Deref, ptr::NonNull};
+
+use block2::{Block, RcBlock};
+use objc2::rc::Retained;
+use objc2_foundation::{NSArray, NSURLSessionDataTask, NSURLSessionDownloadTask, NSURLSessionUploadTask};
+
+type AppleGetTasksBlock = dyn Fn(
+ NonNull<NSArray<NSURLSessionDataTask>>,
+ NonNull<NSArray<NSURLSessionUploadTask>>,
+ NonNull<NSArray<NSURLSessionDownloadTask>>,
+);
+
+pub struct AppleGetTasksHandler(RcBlock<AppleGetTasksBlock>);
+
+impl AppleGetTasksHandler {
+ pub fn new(
+ handler: impl Fn(
+ Box<[Retained<NSURLSessionDataTask>]>,
+ Box<[Retained<NSURLSessionUploadTask>]>,
+ Box<[Retained<NSURLSessionDownloadTask>]>,
+ ) + Send
+ + Sync
+ + 'static
+ ) -> Self {
+ Self(RcBlock::new(
+ move |data_tasks_pointer: NonNull<NSArray<NSURLSessionDataTask>>,
+ upload_tasks_pointer: NonNull<NSArray<NSURLSessionUploadTask>>,
+ download_tasks_pointer: NonNull<NSArray<NSURLSessionDownloadTask>>| {
+ let data_tasks = unsafe { data_tasks_pointer.as_ref() };
+ let upload_tasks = unsafe { upload_tasks_pointer.as_ref() };
+ let download_tasks = unsafe { download_tasks_pointer.as_ref() };
+
+ handler(
+ data_tasks.to_vec().into_boxed_slice(),
+ upload_tasks.to_vec().into_boxed_slice(),
+ download_tasks.to_vec().into_boxed_slice(),
+ );
+ },
+ ))
+ }
+}
+
+// SAFETY: ctor requires `Send + Sync + 'static` captures, and the block copies
+// Objective-C arrays into owned retained values before invoking the Rust closure.
+unsafe impl Send for AppleGetTasksHandler {}
+unsafe impl Sync for AppleGetTasksHandler {}
+
+impl Deref for AppleGetTasksHandler {
+ type Target = Block<AppleGetTasksBlock>;
+
+ fn deref(&self) -> &Self::Target {
+ &self.0
+ }
+}
diff --git a/crates/download-manager/src/backends/apple/manager.rs b/crates/download-manager/src/backends/apple/manager.rs
new file mode 100644
index 000000000..a34befaef
--- /dev/null
+++ b/crates/download-manager/src/backends/apple/manager.rs
@@ -0,0 +1,3 @@
+use crate::backends::{apple::AppleBackend, common::DownloadManager};
+
+pub type AppleDownloadManager = DownloadManager<AppleBackend>;
diff --git a/crates/download-manager/src/backends/apple/mod.rs b/crates/download-manager/src/backends/apple/mod.rs
new file mode 100644
index 000000000..351e3b951
--- /dev/null
+++ b/crates/download-manager/src/backends/apple/mod.rs
@@ -0,0 +1,18 @@
+mod active_task;
+mod backend;
+mod context;
+mod delegate;
+mod error;
+mod get_tasks_handler;
+mod manager;
+mod resume_data_handler;
+mod resume_data_parser;
+mod task_ext;
+
+pub use active_task::AppleActiveTask;
+pub use backend::AppleBackend;
+pub use context::AppleBackendContext;
+pub(crate) use delegate::{AppleEventRegistry, AppleEventSink, AppleSessionDelegate, AppleSinkKey};
+pub use error::AppleBackendError;
+pub(crate) use get_tasks_handler::AppleGetTasksHandler;
+pub use manager::AppleDownloadManager;
diff --git a/crates/download-manager/src/backends/apple/resume_data_handler.rs b/crates/download-manager/src/backends/apple/resume_data_handler.rs
new file mode 100644
index 000000000..db5410520
--- /dev/null
+++ b/crates/download-manager/src/backends/apple/resume_data_handler.rs
@@ -0,0 +1,32 @@
+use std::ops::Deref;
+
+use block2::{Block, RcBlock};
+use objc2_foundation::NSData;
+
+pub struct ResumeDataHandler(RcBlock<dyn Fn(*mut NSData)>);
+
+impl ResumeDataHandler {
+ pub fn new_bytes(handler: impl Fn(Box<[u8]>) + Send + Sync + 'static) -> Self {
+ Self(RcBlock::new(move |data_pointer: *mut NSData| {
+ let resume_data_bytes = if let Some(data) = unsafe { data_pointer.as_ref() } {
+ data.to_vec().into_boxed_slice()
+ } else {
+ Box::new([])
+ };
+ handler(resume_data_bytes);
+ }))
+ }
+}
+
+// SAFETY: ctor requires `Send + Sync + 'static` captures, and the block copies
+// `NSData` into owned bytes before invoking the Rust closure.
+unsafe impl Send for ResumeDataHandler {}
+unsafe impl Sync for ResumeDataHandler {}
+
+impl Deref for ResumeDataHandler {
+ type Target = Block<dyn Fn(*mut NSData)>;
+
+ fn deref(&self) -> &Self::Target {
+ &self.0
+ }
+}
diff --git a/crates/download-manager/src/backends/apple/resume_data_parser.rs b/crates/download-manager/src/backends/apple/resume_data_parser.rs
new file mode 100644
index 000000000..c1364b04a
--- /dev/null
+++ b/crates/download-manager/src/backends/apple/resume_data_parser.rs
@@ -0,0 +1,83 @@
+use std::path::Path;
+
+use objc2::{AnyThread, class, msg_send, rc::Retained, runtime::AnyObject};
+use objc2_foundation::{
+ NSData, NSDictionary, NSKeyedUnarchiver, NSNumber, NSObject, NSObjectProtocol, NSPropertyListFormat,
+ NSPropertyListMutabilityOptions, NSPropertyListSerialization, NSString,
+};
+
+const RESUME_BYTES_RECEIVED_KEY: &str = "NSURLSessionResumeBytesReceived";
+
+pub(crate) fn read_resume_progress(part_path: &Path) -> Option<u64> {
+ let bytes = std::fs::read(part_path).ok()?;
+ let nsdata = NSData::with_bytes(&bytes);
+ let dict = parse_resume_dict(&nsdata)?;
+ read_unsigned(&dict, RESUME_BYTES_RECEIVED_KEY)
+}
+
+fn parse_resume_dict(data: &NSData) -> Option<Retained<NSDictionary<NSString, NSObject>>> {
+ // Newer OS releases use NSKeyedArchive; older ones use a plain plist.
+ if let Some(dict) = parse_via_keyed_unarchiver(data) {
+ return Some(dict);
+ }
+ parse_via_property_list(data)
+}
+
+fn parse_via_keyed_unarchiver(data: &NSData) -> Option<Retained<NSDictionary<NSString, NSObject>>> {
+ unsafe {
+ let allocated = NSKeyedUnarchiver::alloc();
+ let unarchiver = NSKeyedUnarchiver::initForReadingFromData_error(allocated, data).ok()?;
+ unarchiver.setRequiresSecureCoding(false);
+
+ let key = NSString::from_str("NSKeyedArchiveRootObjectKey");
+ let object = unarchiver.decodeObjectForKey(&key);
+ unarchiver.finishDecoding();
+
+ let object = object?;
+ let dict_class = class!(NSDictionary);
+ let raw: *mut AnyObject = Retained::into_raw(object).cast();
+ let is_dict: bool = msg_send![raw, isKindOfClass: dict_class];
+ if !is_dict {
+ // Balance the `into_raw` by reclaiming ownership so the object drops.
+ let _ = Retained::<AnyObject>::from_raw(raw);
+ return None;
+ }
+ let typed_ptr = raw.cast::<NSDictionary<NSString, NSObject>>();
+ Retained::from_raw(typed_ptr)
+ }
+}
+
+fn parse_via_property_list(data: &NSData) -> Option<Retained<NSDictionary<NSString, NSObject>>> {
+ unsafe {
+ let object = NSPropertyListSerialization::propertyListWithData_options_format_error(
+ data,
+ NSPropertyListMutabilityOptions::MutableContainersAndLeaves,
+ core::ptr::null_mut::<NSPropertyListFormat>(),
+ )
+ .ok()?;
+ let dict_class = class!(NSDictionary);
+ let raw: *mut AnyObject = Retained::into_raw(object).cast();
+ let is_dict: bool = msg_send![raw, isKindOfClass: dict_class];
+ if !is_dict {
+ // Balance the `into_raw` by reclaiming ownership so the object drops.
+ let _ = Retained::<AnyObject>::from_raw(raw);
+ return None;
+ }
+ let typed_ptr = raw.cast::<NSDictionary<NSString, NSObject>>();
+ Retained::from_raw(typed_ptr)
+ }
+}
+
+fn read_unsigned(
+ dict: &NSDictionary<NSString, NSObject>,
+ key: &str,
+) -> Option<u64> {
+ let key_ns = NSString::from_str(key);
+ let object = dict.objectForKey(&key_ns)?;
+ let number_class = class!(NSNumber);
+ if !object.isKindOfClass(number_class) {
+ return None;
+ }
+ let number = unsafe { Retained::cast_unchecked::<NSNumber>(object) };
+ Some(number.unsignedLongLongValue())
+}
diff --git a/crates/download-manager/src/backends/apple/task_ext.rs b/crates/download-manager/src/backends/apple/task_ext.rs
new file mode 100644
index 000000000..2b50fca55
--- /dev/null
+++ b/crates/download-manager/src/backends/apple/task_ext.rs
@@ -0,0 +1,59 @@
+use std::path::PathBuf;
+
+use objc2::msg_send;
+use objc2_foundation::{NSString, NSURLSessionDownloadTask, NSURLSessionTaskState};
+
+use crate::{DownloadId, DownloadInfo, compute_download_id};
+
+pub trait AppleDownloadTaskExt {
+ fn set_download_info(
+ &self,
+ info: &DownloadInfo,
+ );
+ fn download_info(&self) -> Option<DownloadInfo>;
+ fn download_id(&self) -> Option<DownloadId>;
+ fn state(&self) -> NSURLSessionTaskState;
+ fn count_of_bytes_expected_to_receive(&self) -> u64;
+ fn count_of_bytes_received(&self) -> u64;
+ fn task_identifier(&self) -> u64;
+}
+
+impl AppleDownloadTaskExt for NSURLSessionDownloadTask {
+ fn set_download_info(
+ &self,
+ info: &DownloadInfo,
+ ) {
+ if let Ok(json) = info.to_json() {
+ let ns_string = NSString::from_str(&json);
+ self.setTaskDescription(Some(&ns_string));
+ }
+ }
+
+ fn download_info(&self) -> Option<DownloadInfo> {
+ self.taskDescription()
+ .map(|description| description.to_string())
+ .and_then(|description| DownloadInfo::from_json(&description).ok())
+ }
+
+ fn download_id(&self) -> Option<DownloadId> {
+ self.download_info().map(|info| compute_download_id(PathBuf::from(info.destination_path).as_path()))
+ }
+
+ fn state(&self) -> NSURLSessionTaskState {
+ unsafe { msg_send![self, state] }
+ }
+
+ fn count_of_bytes_expected_to_receive(&self) -> u64 {
+ let bytes_expected: i64 = unsafe { msg_send![self, countOfBytesExpectedToReceive] };
+ bytes_expected.max(0) as u64
+ }
+
+ fn count_of_bytes_received(&self) -> u64 {
+ let bytes_received: i64 = unsafe { msg_send![self, countOfBytesReceived] };
+ bytes_received.max(0) as u64
+ }
+
+ fn task_identifier(&self) -> u64 {
+ unsafe { msg_send![self, taskIdentifier] }
+ }
+}
diff --git a/crates/download-manager/src/backends/common/action_executor.rs b/crates/download-manager/src/backends/common/action_executor.rs
new file mode 100644
index 000000000..fb98fef56
--- /dev/null
+++ b/crates/download-manager/src/backends/common/action_executor.rs
@@ -0,0 +1,45 @@
+use std::path::Path;
+
+use crate::{
+ DownloadError,
+ crc_utils::save_crc_file,
+ lock_manager::DestinationLockLease,
+ reducer::{Action, ActionPlan},
+};
+
+pub async fn apply_actions(
+ action_plan: &ActionPlan,
+ _destination_lease: &DestinationLockLease,
+) -> Result<(), DownloadError> {
+ for action in action_plan.as_slice() {
+ match action {
+ Action::DeleteFile {
+ path,
+ }
+ | Action::DeleteCrcCache {
+ path,
+ }
+ | Action::DeleteResumeArtifact {
+ path,
+ } => {
+ remove_file_if_present(path).await?;
+ },
+ Action::SaveCrcCache {
+ destination,
+ crc,
+ } => {
+ save_crc_file(destination, crc)?;
+ },
+ }
+ }
+
+ Ok(())
+}
+
+async fn remove_file_if_present(path: &Path) -> Result<(), DownloadError> {
+ match tokio::fs::remove_file(path).await {
+ Ok(()) => Ok(()),
+ Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(()),
+ Err(error) => Err(DownloadError::from(error)),
+ }
+}
diff --git a/crates/download-manager/src/backends/common/backend.rs b/crates/download-manager/src/backends/common/backend.rs
new file mode 100644
index 000000000..4cce5b6dc
--- /dev/null
+++ b/crates/download-manager/src/backends/common/backend.rs
@@ -0,0 +1,45 @@
+use std::{fmt::Debug, sync::Arc};
+
+use tokio::runtime::Handle as TokioHandle;
+
+use crate::{
+ DownloadError,
+ lock_manager::DestinationLockLease,
+ traits::{ActiveDownloadGeneration, BackendEventSender, DownloadBackend, DownloadConfig},
+};
+
+pub enum InitialTaskAttachment<B: Backend> {
+ None,
+ Downloading {
+ active_task: B::ActiveTask,
+ initial_downloaded_bytes: u64,
+ total_bytes: Option<u64>,
+ },
+}
+
+#[async_trait::async_trait]
+pub trait Backend: DownloadBackend + Debug + Clone + Send + Sync + Sized + 'static {
+ const RESUME_ARTIFACT_EXTENSION: &'static str;
+ const SUPPORTS_INITIAL_TASK_ATTACHMENT: bool = false;
+
+ fn manager_suffix() -> &'static str;
+
+ fn create_context(tokio_handle: TokioHandle) -> Result<Self::Context, DownloadError>;
+
+ async fn initial_task_attachment(
+ _context: &Self::Context,
+ _config: Arc<DownloadConfig>,
+ _generation: ActiveDownloadGeneration,
+ _backend_event_sender: BackendEventSender,
+ _destination_lease: &DestinationLockLease,
+ ) -> Result<InitialTaskAttachment<Self>, DownloadError> {
+ Ok(InitialTaskAttachment::None)
+ }
+
+ async fn has_initial_task_to_claim(
+ _context: &Self::Context,
+ _config: &DownloadConfig,
+ ) -> Result<bool, DownloadError> {
+ Ok(false)
+ }
+}
diff --git a/crates/download-manager/src/backends/common/manager.rs b/crates/download-manager/src/backends/common/manager.rs
new file mode 100644
index 000000000..f5010fc7c
--- /dev/null
+++ b/crates/download-manager/src/backends/common/manager.rs
@@ -0,0 +1,316 @@
+use std::sync::Arc;
+
+use tokio::runtime::Handle as TokioHandle;
+use tokio_stream::wrappers::BroadcastStream as TokioBroadcastStream;
+
+use crate::{
+ DownloadError, DownloadEvent, FileCheck, FileDownloadManager, FileDownloadTask, LockFileState,
+ backends::common::{Backend, DownloadManagerState, Startup},
+ check_lock_file, compute_download_id,
+ download_log_event::{DownloadLogEvent, log},
+ file_download_task::CachedFileDownloadTask,
+ file_download_task_actor::GenericFileDownloadTask,
+ lock_manager::{DestinationLockLease, lock_path_for_destination},
+ reducer::InitialLifecycleState,
+};
+
+#[derive(Clone, Debug)]
+pub struct DownloadManager<B: Backend> {
+ pub(crate) state: DownloadManagerState,
+ pub(crate) context: Arc<B::Context>,
+}
+
+impl<B: Backend> DownloadManager<B> {
+ pub fn from_tokio_handle(tokio_handle: TokioHandle) -> Result<Self, DownloadError> {
+ let context = B::create_context(tokio_handle.clone())?;
+ let state = DownloadManagerState::new(B::manager_suffix());
+ log(DownloadLogEvent::ManagerCreated {
+ manager_id: state.manager_id.clone(),
+ });
+ Ok(Self {
+ state,
+ context: Arc::new(context),
+ })
+ }
+}
+
+#[async_trait::async_trait]
+impl<B: Backend> FileDownloadManager for DownloadManager<B> {
+ fn manager_id(&self) -> &str {
+ &self.state.manager_id
+ }
+
+ fn subscribe_to_all_downloads(&self) -> TokioBroadcastStream<DownloadEvent> {
+ self.state.subscribe_to_all_downloads()
+ }
+
+ fn global_broadcast_sender(&self) -> crate::SharedDownloadEventSender {
+ self.state.global_broadcast_sender()
+ }
+
+ async fn get_all_file_tasks(&self) -> Result<Vec<Arc<dyn FileDownloadTask>>, DownloadError> {
+ self.state.get_all_file_tasks().await
+ }
+
+ async fn remove_file_task(
+ &self,
+ download_id: crate::DownloadId,
+ ) -> Result<(), DownloadError> {
+ let construction_lock = self.state.construction_lock(download_id).await;
+ let _construction_guard = construction_lock.lock().await;
+ let shutdown_result = match self.state.take_task(download_id).await {
+ Some(task) => task.managed().shutdown_for_removal().await,
+ None => Ok(()),
+ };
+ self.state.remove_construction_lock_if_unshared(download_id, &construction_lock).await;
+ match shutdown_result {
+ Ok(()) | Err(DownloadError::TaskStopped | DownloadError::ChannelClosed) => Ok(()),
+ Err(error) => Err(error),
+ }
+ }
+
+ async fn file_download_task(
+ &self,
+ source_url: &str,
+ destination_path: &std::path::Path,
+ file_check: FileCheck,
+ expected_bytes: Option<u64>,
+ ) -> Result<Arc<dyn FileDownloadTask>, DownloadError> {
+ let download_id = compute_download_id(destination_path);
+ if let Some(cached_task) = self.state.get_task(download_id).await
+ && !cached_task.is_stopped()
+ {
+ let task = cached_task.public();
+ ensure_cached_task_matches(&task, source_url, &file_check, expected_bytes)?;
+ return Ok(task);
+ }
+
+ let construction_lock = self.state.construction_lock(download_id).await;
+ let _construction_guard = construction_lock.lock().await;
+ let result = async {
+ let cached_task_result = match self.state.get_task(download_id).await {
+ Some(cached_task) if !cached_task.is_stopped() => {
+ let task = cached_task.public();
+ ensure_cached_task_matches(&task, source_url, &file_check, expected_bytes)?;
+ Some(Ok(task))
+ },
+ Some(_) => {
+ let _ = self.state.take_task(download_id).await;
+ None
+ },
+ None => None,
+ };
+ if let Some(cached_task_result) = cached_task_result {
+ cached_task_result
+ } else {
+ let startup = observe_startup::<B>(
+ download_id,
+ source_url,
+ destination_path,
+ file_check.clone(),
+ expected_bytes,
+ &self.state.manager_id,
+ self.state.instance_id,
+ )
+ .await?;
+ let (startup, startup_lease) = prepare_startup::<B>(
+ startup,
+ self.context.as_ref(),
+ download_id,
+ source_url,
+ destination_path,
+ file_check,
+ expected_bytes,
+ &self.state.manager_id,
+ self.state.instance_id,
+ )
+ .await?;
+
+ log(DownloadLogEvent::StartupReconciled {
+ download_id,
+ initial_lifecycle_state: startup.initial_lifecycle_state.name(),
+ action_count: startup.action_plan.as_slice().len(),
+ });
+
+ let task = Arc::new(
+ GenericFileDownloadTask::<B>::spawn_with_initial_attachment(
+ startup.config,
+ Arc::clone(&self.context),
+ startup.initial_lifecycle_state,
+ startup.initial_projection,
+ startup.initial_progress,
+ startup_lease,
+ )
+ .await?,
+ );
+ log(DownloadLogEvent::TaskSpawned {
+ download_id,
+ });
+ let public_task: Arc<dyn FileDownloadTask> = task.clone();
+ let managed_task = task;
+ self.state
+ .insert_task(download_id, CachedFileDownloadTask::new(Arc::clone(&public_task), managed_task))
+ .await;
+ Ok(public_task)
+ }
+ }
+ .await;
+ if result.is_err() {
+ self.state.remove_construction_lock_if_unshared(download_id, &construction_lock).await;
+ }
+ result
+ }
+
+ async fn destination_foreign_lock(
+ &self,
+ destination_path: &std::path::Path,
+ ) -> Option<String> {
+ let lock_path = lock_path_for_destination(destination_path);
+ match check_lock_file(&lock_path, &self.state.manager_id, self.state.instance_id, std::process::id()).await {
+ LockFileState::OwnedByOtherApp(info) => Some(info.manager_id),
+ _ => None,
+ }
+ }
+}
+
+#[allow(clippy::too_many_arguments)]
+pub(crate) async fn observe_startup<B: Backend>(
+ download_id: crate::DownloadId,
+ source_url: &str,
+ destination_path: &std::path::Path,
+ file_check: FileCheck,
+ expected_bytes: Option<u64>,
+ manager_id: &str,
+ manager_instance_id: uuid::Uuid,
+) -> Result<Startup, DownloadError> {
+ Startup::observe::<B>(
+ download_id,
+ source_url,
+ destination_path,
+ file_check,
+ expected_bytes,
+ manager_id,
+ manager_instance_id,
+ )
+ .await
+}
+
+#[allow(clippy::too_many_arguments)]
+pub(crate) async fn prepare_startup<B: Backend>(
+ startup: Startup,
+ context: &B::Context,
+ download_id: crate::DownloadId,
+ source_url: &str,
+ destination_path: &std::path::Path,
+ file_check: FileCheck,
+ expected_bytes: Option<u64>,
+ manager_id: &str,
+ manager_instance_id: uuid::Uuid,
+) -> Result<(Startup, Option<DestinationLockLease>), DownloadError> {
+ if !startup_requires_destination_lease::<B>(&startup, context).await? {
+ return Ok((startup, None));
+ }
+
+ let lease =
+ match DestinationLockLease::acquire_for_destination(destination_path, manager_id, manager_instance_id).await {
+ Ok(lease) => lease,
+ Err(error) if error.kind() == std::io::ErrorKind::AlreadyExists => {
+ let startup = observe_startup::<B>(
+ download_id,
+ source_url,
+ destination_path,
+ file_check,
+ expected_bytes,
+ manager_id,
+ manager_instance_id,
+ )
+ .await?;
+ if startup.lock_state.is_conflict() {
+ return Ok((startup, None));
+ }
+ return Err(DownloadError::from(error));
+ },
+ Err(error) => return Err(DownloadError::from(error)),
+ };
+ let startup = match observe_startup::<B>(
+ download_id,
+ source_url,
+ destination_path,
+ file_check,
+ expected_bytes,
+ manager_id,
+ manager_instance_id,
+ )
+ .await
+ {
+ Ok(startup) => startup,
+ Err(error) => {
+ let _ = lease.release().await;
+ return Err(error);
+ },
+ };
+
+ if let Err(error) = startup.apply_actions(&lease).await {
+ let _ = lease.release().await;
+ return Err(error);
+ }
+
+ if startup_can_attach_initial_task::<B>(&startup) {
+ Ok((startup, Some(lease)))
+ } else {
+ lease.release().await?;
+ Ok((startup, None))
+ }
+}
+
+async fn startup_requires_destination_lease<B: Backend>(
+ startup: &Startup,
+ context: &B::Context,
+) -> Result<bool, DownloadError> {
+ if startup.lock_state.is_conflict() || !startup.action_plan.is_empty() {
+ return Ok(!startup.lock_state.is_conflict());
+ }
+
+ if !startup_can_attach_initial_task::<B>(startup) {
+ return Ok(false);
+ }
+
+ B::has_initial_task_to_claim(context, startup.config.as_ref()).await
+}
+
+fn startup_can_attach_initial_task<B: Backend>(startup: &Startup) -> bool {
+ B::SUPPORTS_INITIAL_TASK_ATTACHMENT
+ && !startup.lock_state.is_conflict()
+ && !matches!(startup.initial_lifecycle_state, InitialLifecycleState::Downloaded)
+}
+
+fn ensure_cached_task_matches(
+ cached: &Arc<dyn FileDownloadTask>,
+ source_url: &str,
+ file_check: &FileCheck,
+ expected_bytes: Option<u64>,
+) -> Result<(), DownloadError> {
+ if cached.source_url() != source_url {
+ return Err(DownloadError::ConflictingConfig(format!(
+ "{} already requested with source_url {:?}; cannot reuse for {:?}",
+ cached.destination().display(),
+ cached.source_url(),
+ source_url,
+ )));
+ }
+ if cached.file_check() != file_check {
+ return Err(DownloadError::ConflictingConfig(format!(
+ "{} already requested with a different file_check",
+ cached.destination().display(),
+ )));
+ }
+ if cached.expected_bytes() != expected_bytes {
+ return Err(DownloadError::ConflictingConfig(format!(
+ "{} already requested with expected_bytes {:?}; got {:?}",
+ cached.destination().display(),
+ cached.expected_bytes(),
+ expected_bytes,
+ )));
+ }
+ Ok(())
+}
diff --git a/crates/download-manager/src/backends/common/manager_state.rs b/crates/download-manager/src/backends/common/manager_state.rs
new file mode 100644
index 000000000..1d6a4211e
--- /dev/null
+++ b/crates/download-manager/src/backends/common/manager_state.rs
@@ -0,0 +1,124 @@
+use std::{collections::HashMap, sync::Arc};
+
+use tokio::sync::{Mutex as TokioMutex, broadcast::channel as tokio_broadcast_channel};
+use tokio_stream::wrappers::BroadcastStream as TokioBroadcastStream;
+use uuid::Uuid;
+
+use crate::{
+ DownloadError, DownloadEvent, DownloadId, FileDownloadTask, SharedDownloadEventSender,
+ file_download_task::CachedFileDownloadTask,
+};
+
+type TaskCache = Arc<TokioMutex<HashMap<DownloadId, CachedFileDownloadTask>>>;
+type ConstructionLocks = Arc<TokioMutex<HashMap<DownloadId, Arc<TokioMutex<()>>>>>;
+
+#[derive(Clone)]
+pub struct DownloadManagerState {
+ pub manager_id: String,
+ pub instance_id: Uuid,
+ pub global_broadcast_sender: SharedDownloadEventSender,
+ task_cache: TaskCache,
+ construction_locks: ConstructionLocks,
+}
+
+impl std::fmt::Debug for DownloadManagerState {
+ fn fmt(
+ &self,
+ formatter: &mut std::fmt::Formatter<'_>,
+ ) -> std::fmt::Result {
+ formatter
+ .debug_struct("DownloadManagerState")
+ .field("manager_id", &self.manager_id)
+ .field("instance_id", &self.instance_id)
+ .finish()
+ }
+}
+
+impl DownloadManagerState {
+ pub fn new(suffix: &str) -> Self {
+ let (global_broadcast_sender, _) = tokio_broadcast_channel::<DownloadEvent>(256);
+ Self {
+ manager_id: generate_manager_id(suffix),
+ instance_id: Uuid::new_v4(),
+ global_broadcast_sender: Arc::new(global_broadcast_sender),
+ task_cache: Arc::new(TokioMutex::new(HashMap::new())),
+ construction_locks: Arc::new(TokioMutex::new(HashMap::new())),
+ }
+ }
+
+ pub fn subscribe_to_all_downloads(&self) -> TokioBroadcastStream<DownloadEvent> {
+ TokioBroadcastStream::new(self.global_broadcast_sender.subscribe())
+ }
+
+ pub fn global_broadcast_sender(&self) -> SharedDownloadEventSender {
+ Arc::clone(&self.global_broadcast_sender)
+ }
+
+ pub async fn get_all_file_tasks(&self) -> Result<Vec<Arc<dyn FileDownloadTask>>, DownloadError> {
+ Ok(self.task_cache.lock().await.values().map(CachedFileDownloadTask::public).collect())
+ }
+
+ pub(crate) async fn get_task(
+ &self,
+ download_id: DownloadId,
+ ) -> Option<CachedFileDownloadTask> {
+ self.task_cache.lock().await.get(&download_id).cloned()
+ }
+
+ pub(crate) async fn insert_task(
+ &self,
+ download_id: DownloadId,
+ task: CachedFileDownloadTask,
+ ) {
+ self.task_cache.lock().await.insert(download_id, task);
+ }
+
+ pub(crate) async fn take_task(
+ &self,
+ download_id: DownloadId,
+ ) -> Option<CachedFileDownloadTask> {
+ self.task_cache.lock().await.remove(&download_id)
+ }
+
+ pub async fn remove_construction_lock_if_unshared(
+ &self,
+ download_id: DownloadId,
+ construction_lock: &Arc<TokioMutex<()>>,
+ ) {
+ let mut construction_locks = self.construction_locks.lock().await;
+ let Some(cached_lock) = construction_locks.get(&download_id) else {
+ return;
+ };
+ // Only remove if no other caller is racing with us — otherwise they'd serialize on a different mutex.
+ if Arc::ptr_eq(cached_lock, construction_lock) && Arc::strong_count(construction_lock) == 2 {
+ construction_locks.remove(&download_id);
+ }
+ }
+
+ pub async fn construction_lock(
+ &self,
+ download_id: DownloadId,
+ ) -> Arc<TokioMutex<()>> {
+ let mut construction_locks = self.construction_locks.lock().await;
+ construction_locks.entry(download_id).or_insert_with(|| Arc::new(TokioMutex::new(()))).clone()
+ }
+}
+
+fn generate_manager_id(suffix: &str) -> String {
+ #[cfg(target_vendor = "apple")]
+ {
+ use objc2_foundation::NSBundle;
+
+ let bundle_id = NSBundle::mainBundle().bundleIdentifier().unwrap_or_default().to_string();
+ if bundle_id.is_empty() {
+ format!("mirai.{suffix}")
+ } else {
+ format!("{bundle_id}.mirai.{suffix}")
+ }
+ }
+
+ #[cfg(not(target_vendor = "apple"))]
+ {
+ format!("mirai.{suffix}")
+ }
+}
diff --git a/crates/download-manager/src/backends/common/mod.rs b/crates/download-manager/src/backends/common/mod.rs
new file mode 100644
index 000000000..a3c10609a
--- /dev/null
+++ b/crates/download-manager/src/backends/common/mod.rs
@@ -0,0 +1,10 @@
+mod action_executor;
+mod backend;
+pub(crate) mod manager;
+mod manager_state;
+mod startup;
+
+pub use backend::{Backend, InitialTaskAttachment};
+pub use manager::DownloadManager;
+pub use manager_state::DownloadManagerState;
+pub use startup::Startup;
diff --git a/crates/download-manager/src/backends/common/startup.rs b/crates/download-manager/src/backends/common/startup.rs
new file mode 100644
index 000000000..34b2983ad
--- /dev/null
+++ b/crates/download-manager/src/backends/common/startup.rs
@@ -0,0 +1,107 @@
+use std::{path::Path, sync::Arc};
+
+use crate::{
+ DownloadError, DownloadId, FileCheck, FileState, LockFileState,
+ backends::common::{Backend, action_executor::apply_actions},
+ check_lock_file,
+ crc_utils::crc_path_for_file,
+ file_download_task_actor::{ProgressCounters, PublicProjection},
+ lock_manager::{DestinationLockLease, lock_path_for_destination},
+ reducer::{ActionPlan, DiskObservation, InitialLifecycleState, LockObservation, decide, validate},
+ traits::DownloadConfig,
+};
+
+#[derive(Clone, Debug)]
+pub struct Startup {
+ pub config: Arc<DownloadConfig>,
+ pub initial_lifecycle_state: InitialLifecycleState,
+ pub initial_projection: PublicProjection,
+ pub initial_progress: ProgressCounters,
+ pub action_plan: ActionPlan,
+ pub lock_state: LockFileState,
+}
+
+impl Startup {
+ pub async fn observe<B: Backend>(
+ download_id: DownloadId,
+ source_url: &str,
+ destination_path: &Path,
+ file_check: FileCheck,
+ expected_bytes: Option<u64>,
+ manager_id: &str,
+ manager_instance_id: uuid::Uuid,
+ ) -> Result<Self, DownloadError> {
+ let resume_artifact_path = destination_path.with_extension(B::RESUME_ARTIFACT_EXTENSION);
+ let expected_crc = match &file_check {
+ FileCheck::CRC(crc) => Some(crc.clone()),
+ FileCheck::None => None,
+ };
+ let crc_path = crc_path_for_file(destination_path);
+ let resume_state = file_state(&resume_artifact_path);
+ let resume_size = match resume_state {
+ FileState::Exists => B::read_resume_progress(&resume_artifact_path),
+ FileState::Missing => None,
+ };
+ let observation = DiskObservation {
+ destination_state: file_state(destination_path),
+ crc_state: file_state(&crc_path),
+ resume_state,
+ destination_size: file_size(destination_path),
+ resume_size,
+ expected_crc,
+ expected_bytes,
+ destination_path: destination_path.to_path_buf(),
+ crc_path: Some(crc_path),
+ resume_artifact_path: Some(resume_artifact_path),
+ };
+ let lock_state = check_lock_file(
+ &lock_path_for_destination(destination_path),
+ manager_id,
+ manager_instance_id,
+ std::process::id(),
+ )
+ .await;
+ let lock_observation = LockObservation {
+ state: lock_state.clone(),
+ };
+ let validation = validate(&observation).await;
+ let decision = decide(&observation, &lock_observation, &validation);
+ let config = Arc::new(DownloadConfig {
+ download_id,
+ source_url: source_url.to_string(),
+ destination: destination_path.to_path_buf(),
+ file_check,
+ expected_bytes,
+ manager_id: manager_id.to_string(),
+ manager_instance_id,
+ });
+
+ Ok(Self {
+ config,
+ initial_lifecycle_state: decision.initial_lifecycle_state,
+ initial_projection: decision.initial_projection,
+ initial_progress: decision.initial_progress,
+ action_plan: decision.action_plan,
+ lock_state,
+ })
+ }
+
+ pub(crate) async fn apply_actions(
+ &self,
+ destination_lease: &DestinationLockLease,
+ ) -> Result<(), DownloadError> {
+ apply_actions(&self.action_plan, destination_lease).await
+ }
+}
+
+fn file_state(path: &Path) -> FileState {
+ if path.exists() && path.is_file() {
+ FileState::Exists
+ } else {
+ FileState::Missing
+ }
+}
+
+fn file_size(path: &Path) -> Option<u64> {
+ path.metadata().ok().map(|metadata| metadata.len())
+}
diff --git a/crates/download-manager/src/managers/mod.rs b/crates/download-manager/src/backends/mod.rs
similarity index 51%
rename from crates/download-manager/src/managers/mod.rs
rename to crates/download-manager/src/backends/mod.rs
index b2cb87575..6d2e8c9b0 100644
--- a/crates/download-manager/src/managers/mod.rs
+++ b/crates/download-manager/src/backends/mod.rs
@@ -1,3 +1,3 @@
-#[cfg(target_vendor = "apple")]
pub mod apple;
+pub mod common;
pub mod universal;
diff --git a/crates/download-manager/src/backends/universal/active_task.rs b/crates/download-manager/src/backends/universal/active_task.rs
new file mode 100644
index 000000000..a0e544352
--- /dev/null
+++ b/crates/download-manager/src/backends/universal/active_task.rs
@@ -0,0 +1,72 @@
+use std::path::{Path, PathBuf};
+
+use tokio::task::JoinHandle as TokioJoinHandle;
+
+use crate::{
+ backends::universal::UniversalBackend,
+ traits::{ActiveTask, CancelOutcome, DownloadBackend},
+};
+
+pub struct UniversalActiveTask {
+ task_handles: Box<[TokioJoinHandle<()>]>,
+ resume_artifact_path: PathBuf,
+}
+
+impl std::fmt::Debug for UniversalActiveTask {
+ fn fmt(
+ &self,
+ formatter: &mut std::fmt::Formatter<'_>,
+ ) -> std::fmt::Result {
+ formatter
+ .debug_struct("UniversalActiveTask")
+ .field("resume_artifact_path", &self.resume_artifact_path)
+ .finish_non_exhaustive()
+ }
+}
+
+impl UniversalActiveTask {
+ pub fn new(
+ task_handles: Box<[TokioJoinHandle<()>]>,
+ resume_artifact_path: PathBuf,
+ ) -> Self {
+ Self {
+ task_handles,
+ resume_artifact_path,
+ }
+ }
+}
+
+impl Drop for UniversalActiveTask {
+ fn drop(&mut self) {
+ for handle in self.task_handles.iter() {
+ handle.abort();
+ }
+ }
+}
+
+#[async_trait::async_trait]
+impl ActiveTask for UniversalActiveTask {
+ type Backend = UniversalBackend;
+
+ async fn pause(
+ mut self,
+ _destination: &Path,
+ ) -> Result<PathBuf, <Self::Backend as DownloadBackend>::Error> {
+ for task_handle in std::mem::take(&mut self.task_handles) {
+ task_handle.abort();
+ let _ = task_handle.await;
+ }
+ Ok(std::mem::take(&mut self.resume_artifact_path))
+ }
+
+ async fn cancel(
+ mut self,
+ _destination: &Path,
+ ) -> CancelOutcome {
+ for task_handle in std::mem::take(&mut self.task_handles) {
+ task_handle.abort();
+ let _ = task_handle.await;
+ }
+ CancelOutcome::BestEffort
+ }
+}
diff --git a/crates/download-manager/src/backends/universal/backend.rs b/crates/download-manager/src/backends/universal/backend.rs
new file mode 100644
index 000000000..ca2d771c1
--- /dev/null
+++ b/crates/download-manager/src/backends/universal/backend.rs
@@ -0,0 +1,29 @@
+use crate::{
+ DownloadError,
+ backends::{
+ common,
+ universal::{UniversalActiveTask, UniversalBackendContext, UniversalBackendError},
+ },
+ traits::DownloadBackend,
+};
+
+#[derive(Clone, Debug, Default)]
+pub struct UniversalBackend;
+
+impl DownloadBackend for UniversalBackend {
+ type Context = UniversalBackendContext;
+ type ActiveTask = UniversalActiveTask;
+ type Error = UniversalBackendError;
+}
+
+impl common::Backend for UniversalBackend {
+ const RESUME_ARTIFACT_EXTENSION: &'static str = "part";
+
+ fn manager_suffix() -> &'static str {
+ "universal"
+ }
+
+ fn create_context(tokio_handle: tokio::runtime::Handle) -> Result<Self::Context, DownloadError> {
+ Ok(UniversalBackendContext::new(tokio_handle))
+ }
+}
diff --git a/crates/download-manager/src/backends/universal/context.rs b/crates/download-manager/src/backends/universal/context.rs
new file mode 100644
index 000000000..1c810f97b
--- /dev/null
+++ b/crates/download-manager/src/backends/universal/context.rs
@@ -0,0 +1,240 @@
+use std::{
+ path::{Path, PathBuf},
+ sync::Arc,
+ time::Duration,
+};
+
+use futures_util::StreamExt;
+use reqwest::{
+ StatusCode,
+ header::{CONTENT_LENGTH, CONTENT_RANGE, RANGE},
+};
+use tokio::{
+ fs::{File as TokioFile, OpenOptions as TokioOpenOptions},
+ io::AsyncWriteExt,
+ runtime::Handle as TokioHandle,
+};
+
+use crate::{
+ backends::universal::{UniversalActiveTask, UniversalBackend, UniversalBackendError},
+ file_download_task_actor::BackendEvent,
+ lock_manager::DestinationLockLease,
+ traits::{ActiveDownloadGeneration, BackendContext, BackendEventSender, DownloadConfig},
+};
+
+#[derive(Clone, Debug)]
+pub struct UniversalBackendContext {
+ tokio_handle: TokioHandle,
+ pub retries: u16,
+ pub progress_interval_ms: u64,
+}
+
+impl UniversalBackendContext {
+ pub fn new(tokio_handle: TokioHandle) -> Self {
+ Self {
+ tokio_handle,
+ retries: 3,
+ progress_interval_ms: 500,
+ }
+ }
+}
+
+#[async_trait::async_trait]
+impl BackendContext for UniversalBackendContext {
+ type Backend = UniversalBackend;
+
+ async fn download(
+ &self,
+ config: Arc<DownloadConfig>,
+ generation: ActiveDownloadGeneration,
+ backend_event_sender: BackendEventSender,
+ _destination_lease: &DestinationLockLease,
+ ) -> Result<UniversalActiveTask, UniversalBackendError> {
+ let resume_artifact_path = config.destination.with_extension("part");
+ self.start(config, generation, resume_artifact_path, backend_event_sender).await
+ }
+
+ async fn resume(
+ &self,
+ config: Arc<DownloadConfig>,
+ generation: ActiveDownloadGeneration,
+ resume_artifact_path: &Path,
+ backend_event_sender: BackendEventSender,
+ _destination_lease: &DestinationLockLease,
+ ) -> Result<UniversalActiveTask, UniversalBackendError> {
+ self.start(config, generation, resume_artifact_path.to_path_buf(), backend_event_sender).await
+ }
+}
+
+impl UniversalBackendContext {
+ async fn start(
+ &self,
+ config: Arc<DownloadConfig>,
+ generation: ActiveDownloadGeneration,
+ resume_artifact_path: PathBuf,
+ backend_event_sender: BackendEventSender,
+ ) -> Result<UniversalActiveTask, UniversalBackendError> {
+ if let Some(parent) = config.destination.parent() {
+ tokio::fs::create_dir_all(parent).await.map_err(|error| UniversalBackendError::Io(error.to_string()))?;
+ }
+
+ let retry_count = self.retries;
+ let progress_interval = Duration::from_millis(self.progress_interval_ms);
+ let task_handle = self.tokio_handle.spawn(download_streaming(
+ config,
+ generation,
+ resume_artifact_path.clone(),
+ backend_event_sender,
+ retry_count,
+ progress_interval,
+ ));
+
+ Ok(UniversalActiveTask::new(Box::from([task_handle]), resume_artifact_path))
+ }
+}
+
+async fn download_streaming(
+ config: Arc<DownloadConfig>,
+ generation: ActiveDownloadGeneration,
+ resume_artifact_path: PathBuf,
+ backend_event_sender: BackendEventSender,
+ retry_count: u16,
+ progress_interval: Duration,
+) {
+ let result = download_streaming_with_retries(
+ Arc::clone(&config),
+ generation,
+ &resume_artifact_path,
+ &backend_event_sender,
+ retry_count,
+ progress_interval,
+ )
+ .await;
+
+ let terminal_event = match result {
+ Ok(()) => BackendEvent::completed(generation),
+ Err(error) => BackendEvent::error(generation, error),
+ };
+ let _ = backend_event_sender.send_terminal(terminal_event).await;
+}
+
+async fn download_streaming_with_retries(
+ config: Arc<DownloadConfig>,
+ generation: ActiveDownloadGeneration,
+ resume_artifact_path: &Path,
+ backend_event_sender: &BackendEventSender,
+ retry_count: u16,
+ progress_interval: Duration,
+) -> Result<(), String> {
+ let mut attempt = 0_u16;
+ loop {
+ match download_once(
+ Arc::clone(&config),
+ generation,
+ resume_artifact_path,
+ backend_event_sender,
+ progress_interval,
+ )
+ .await
+ {
+ Ok(()) => return Ok(()),
+ Err(error) if attempt < retry_count => {
+ attempt = attempt.saturating_add(1);
+ tokio::time::sleep(Duration::from_millis(250)).await;
+ tracing::debug!("retrying universal download after error: {error}");
+ },
+ Err(error) => return Err(error),
+ }
+ }
+}
+
+async fn download_once(
+ config: Arc<DownloadConfig>,
+ generation: ActiveDownloadGeneration,
+ resume_artifact_path: &Path,
+ backend_event_sender: &BackendEventSender,
+ progress_interval: Duration,
+) -> Result<(), String> {
+ let client = reqwest::Client::new();
+ let resume_from_bytes =
+ tokio::fs::metadata(resume_artifact_path).await.ok().map(|metadata| metadata.len()).unwrap_or(0);
+ let mut request = client.get(&config.source_url);
+ if resume_from_bytes > 0 {
+ request = request.header(RANGE, format!("bytes={resume_from_bytes}-"));
+ }
+
+ let response = request.send().await.map_err(|error| error.to_string())?;
+ let status = response.status();
+ let content_range = response.headers().get(CONTENT_RANGE).cloned();
+ let resume_from_bytes = if resume_from_bytes > 0 {
+ match status {
+ StatusCode::PARTIAL_CONTENT => {
+ let header = content_range
+ .as_ref()
+ .ok_or_else(|| "server returned 206 without Content-Range header".to_string())?;
+ let header_value =
+ header.to_str().map_err(|error| format!("non-utf8 Content-Range header: {error}"))?;
+ let advertised_start = parse_content_range_start(header_value)
+ .ok_or_else(|| format!("server returned malformed Content-Range header: {header_value}"))?;
+ if advertised_start != resume_from_bytes {
+ return Err(format!(
+ "server returned bytes starting at {advertised_start} but client requested {resume_from_bytes}"
+ ));
+ }
+ resume_from_bytes
+ },
+ StatusCode::OK => 0,
+ _ => return Err(format!("server did not honor range request: status {status}")),
+ }
+ } else {
+ resume_from_bytes
+ };
+ let response = response.error_for_status().map_err(|error| error.to_string())?;
+ let remaining_bytes = response
+ .headers()
+ .get(CONTENT_LENGTH)
+ .and_then(|value| value.to_str().ok())
+ .and_then(|value| value.parse::<u64>().ok());
+ let total_bytes = remaining_bytes
+ .map(|remaining_bytes| remaining_bytes.saturating_add(resume_from_bytes))
+ .or(config.expected_bytes);
+
+ let mut file = open_part_file(resume_artifact_path, resume_from_bytes).await.map_err(|error| error.to_string())?;
+ let mut downloaded_bytes = resume_from_bytes;
+ let mut last_progress_emit =
+ std::time::Instant::now().checked_sub(progress_interval).unwrap_or_else(std::time::Instant::now);
+ let mut stream = response.bytes_stream();
+
+ while let Some(chunk) = stream.next().await {
+ let chunk = chunk.map_err(|error| error.to_string())?;
+ file.write_all(&chunk).await.map_err(|error| error.to_string())?;
+ downloaded_bytes = downloaded_bytes.saturating_add(chunk.len() as u64);
+
+ if last_progress_emit.elapsed() >= progress_interval {
+ backend_event_sender.send_progress(generation, downloaded_bytes, total_bytes).await;
+ last_progress_emit = std::time::Instant::now();
+ }
+ }
+
+ file.flush().await.map_err(|error| error.to_string())?;
+ backend_event_sender.send_progress(generation, downloaded_bytes, total_bytes.or(Some(downloaded_bytes))).await;
+ tokio::fs::rename(resume_artifact_path, &config.destination).await.map_err(|error| error.to_string())
+}
+
+async fn open_part_file(
+ resume_artifact_path: &Path,
+ resume_from_bytes: u64,
+) -> Result<TokioFile, std::io::Error> {
+ if resume_from_bytes > 0 {
+ TokioOpenOptions::new().create(true).append(true).open(resume_artifact_path).await
+ } else {
+ TokioOpenOptions::new().create(true).write(true).truncate(true).open(resume_artifact_path).await
+ }
+}
+
+fn parse_content_range_start(header_value: &str) -> Option<u64> {
+ let value = header_value.strip_prefix("bytes ")?.trim_start();
+ let (range, _) = value.split_once('/')?;
+ let (start, _) = range.split_once('-')?;
+ start.parse::<u64>().ok()
+}
diff --git a/crates/download-manager/src/backends/universal/error.rs b/crates/download-manager/src/backends/universal/error.rs
new file mode 100644
index 000000000..693d24a51
--- /dev/null
+++ b/crates/download-manager/src/backends/universal/error.rs
@@ -0,0 +1,5 @@
+#[derive(thiserror::Error, Clone, Debug, PartialEq, Eq)]
+pub enum UniversalBackendError {
+ #[error("io error: {0}")]
+ Io(String),
+}
diff --git a/crates/download-manager/src/backends/universal/manager.rs b/crates/download-manager/src/backends/universal/manager.rs
new file mode 100644
index 000000000..864434dda
--- /dev/null
+++ b/crates/download-manager/src/backends/universal/manager.rs
@@ -0,0 +1,3 @@
+use crate::backends::{common::DownloadManager, universal::UniversalBackend};
+
+pub type UniversalDownloadManager = DownloadManager<UniversalBackend>;
diff --git a/crates/download-manager/src/backends/universal/mod.rs b/crates/download-manager/src/backends/universal/mod.rs
new file mode 100644
index 000000000..3dda3aac1
--- /dev/null
+++ b/crates/download-manager/src/backends/universal/mod.rs
@@ -0,0 +1,11 @@
+mod active_task;
+mod backend;
+mod context;
+mod error;
+mod manager;
+
+pub use active_task::UniversalActiveTask;
+pub use backend::UniversalBackend;
+pub use context::UniversalBackendContext;
+pub use error::UniversalBackendError;
+pub use manager::UniversalDownloadManager;
diff --git a/crates/download-manager/src/checked_file_state.rs b/crates/download-manager/src/checked_file_state.rs
index 9aae646c2..8b1104c9e 100644
--- a/crates/download-manager/src/checked_file_state.rs
+++ b/crates/download-manager/src/checked_file_state.rs
@@ -1,4 +1,6 @@
-#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+use serde::{Deserialize, Serialize};
+
+#[derive(Serialize, Deserialize, Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum CheckedFileState {
Valid,
Invalid,
diff --git a/crates/download-manager/src/crc_utils.rs b/crates/download-manager/src/crc_utils.rs
index 40b8cad9c..911dba474 100644
--- a/crates/download-manager/src/crc_utils.rs
+++ b/crates/download-manager/src/crc_utils.rs
@@ -1,31 +1,36 @@
use std::{
- fs::File,
+ fs::{self, File},
io::{Error as IoError, Read},
- path::Path,
+ path::{Path, PathBuf},
+ time::UNIX_EPOCH,
};
use base64::Engine;
+use serde::{Deserialize, Serialize};
const CHUNK_SIZE: usize = 8 * 1024 * 1024;
+const CRC_CACHE_VERSION: u8 = 1;
+
+#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, Hash)]
+struct CrcCacheReceipt {
+ version: u8,
+ crc: String,
+ file_size: u64,
+ modified_unix_seconds: u64,
+ modified_nanos: u32,
+}
pub fn calculate_and_verify_crc(
file_path: &Path,
expected_crc32c_base64: &str,
) -> Result<bool, IoError> {
- let expected_bytes = match base64::engine::general_purpose::STANDARD.decode(expected_crc32c_base64) {
- Ok(bytes) => bytes,
- Err(_) => return Ok(false),
- };
-
- if expected_bytes.len() != 4 {
+ let Some(expected_crc) = decode_crc32c(expected_crc32c_base64) else {
return Ok(false);
- }
-
- let expected_crc = u32::from_be_bytes([expected_bytes[0], expected_bytes[1], expected_bytes[2], expected_bytes[3]]);
+ };
let mut file = File::open(file_path)?;
- let mut buffer = vec![0u8; CHUNK_SIZE];
- let mut crc: u32 = 0;
+ let mut buffer = vec![0_u8; CHUNK_SIZE];
+ let mut crc = 0_u32;
loop {
let bytes_read = file.read(&mut buffer)?;
@@ -38,12 +43,69 @@ pub fn calculate_and_verify_crc(
Ok(crc == expected_crc)
}
-#[allow(dead_code)]
+pub fn crc_cache_matches(
+ file_path: &Path,
+ expected_crc32c_base64: &str,
+) -> bool {
+ if decode_crc32c(expected_crc32c_base64).is_none() {
+ return false;
+ }
+
+ let Ok(cache_contents) = fs::read_to_string(crc_path_for_file(file_path)) else {
+ return false;
+ };
+ let Ok(cached_receipt) = serde_json::from_str::<CrcCacheReceipt>(&cache_contents) else {
+ return false;
+ };
+ let Some(current_receipt) = CrcCacheReceipt::from_file(file_path, expected_crc32c_base64) else {
+ return false;
+ };
+
+ cached_receipt == current_receipt
+}
+
pub fn save_crc_file(
file_path: &Path,
crc_value: &str,
) -> Result<(), IoError> {
- let crc_path_str = format!("{}.crc", file_path.display());
- std::fs::write(&crc_path_str, crc_value)?;
- Ok(())
+ let Some(receipt) = CrcCacheReceipt::from_file(file_path, crc_value) else {
+ return Ok(());
+ };
+ let receipt_json = serde_json::to_vec(&receipt).map_err(IoError::other)?;
+ std::fs::write(crc_path_for_file(file_path), receipt_json)
+}
+
+pub fn crc_path_for_file(file_path: &Path) -> PathBuf {
+ PathBuf::from(format!("{}.crc", file_path.display()))
+}
+
+impl CrcCacheReceipt {
+ fn from_file(
+ file_path: &Path,
+ crc: &str,
+ ) -> Option<Self> {
+ let metadata = fs::metadata(file_path).ok()?;
+ if !metadata.is_file() {
+ return None;
+ }
+ let modified = metadata.modified().ok()?.duration_since(UNIX_EPOCH).ok()?;
+
+ Some(Self {
+ version: CRC_CACHE_VERSION,
+ crc: crc.to_string(),
+ file_size: metadata.len(),
+ modified_unix_seconds: modified.as_secs(),
+ modified_nanos: modified.subsec_nanos(),
+ })
+ }
+}
+
+fn decode_crc32c(expected_crc32c_base64: &str) -> Option<u32> {
+ let expected_bytes = base64::engine::general_purpose::STANDARD.decode(expected_crc32c_base64).ok()?;
+
+ if expected_bytes.len() != 4 {
+ return None;
+ }
+
+ Some(u32::from_be_bytes([expected_bytes[0], expected_bytes[1], expected_bytes[2], expected_bytes[3]]))
}
diff --git a/crates/download-manager/src/download_error.rs b/crates/download-manager/src/download_error.rs
index ec686438d..fd5d8b640 100644
--- a/crates/download-manager/src/download_error.rs
+++ b/crates/download-manager/src/download_error.rs
@@ -1,23 +1,17 @@
-#[derive(thiserror::Error, Debug)]
+#[derive(thiserror::Error, Clone, Debug, PartialEq, Eq)]
pub enum DownloadError {
#[error("io error: {0}")]
- Io(#[from] std::io::Error),
+ Io(String),
#[error("json error: {0}")]
- SerdeJson(#[from] serde_json::Error),
- #[error("http client error: {0}")]
- Reqwest(#[from] reqwest::Error),
+ SerdeJson(String),
#[error("http error: status {0}")]
HttpStatus(u16),
#[error("canceled")]
Canceled,
#[error("resume unsupported")]
ResumeUnsupported,
- #[error("io error: {0}")]
- IOError(String),
#[error("unsupported file download manager type")]
UnsupportedType,
-
- // Concrete cases instead of stringly-typed variants
#[error("bad url")]
BadUrl,
#[error("missing download info for task")]
@@ -40,4 +34,24 @@ pub enum DownloadError {
InvalidStateTransition,
#[error("file locked by another manager: {0}")]
LockedByOther(String),
+ #[error("conflicting download config for destination: {0}")]
+ ConflictingConfig(String),
+ #[error("task stopped")]
+ TaskStopped,
+ #[error("channel closed")]
+ ChannelClosed,
+ #[error("backend error: {0}")]
+ Backend(String),
+}
+
+impl From<std::io::Error> for DownloadError {
+ fn from(error: std::io::Error) -> Self {
+ Self::Io(error.to_string())
+ }
+}
+
+impl From<serde_json::Error> for DownloadError {
+ fn from(error: serde_json::Error) -> Self {
+ Self::SerdeJson(error.to_string())
+ }
}
diff --git a/crates/download-manager/src/download_info.rs b/crates/download-manager/src/download_info.rs
index 737c0b3c5..6dfa71d44 100644
--- a/crates/download-manager/src/download_info.rs
+++ b/crates/download-manager/src/download_info.rs
@@ -1,20 +1,14 @@
use serde::{Deserialize, Serialize};
-/// DownloadInfo is a small, serializable struct that describes a download.
-/// It is designed to be encoded/decoded to JSON and stored in NSURLSessionTask.taskDescription.
-#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
+#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, Hash)]
pub struct DownloadInfo {
- /// The original source URL for the download.
pub source_url: String,
- /// The absolute or relative destination path where the file should be persisted.
pub destination_path: String,
- /// Optional CRC32C checksum in base64 format for file validation.
#[serde(default)]
pub crc32c: Option<String>,
}
impl DownloadInfo {
- /// Construct a new DownloadInfo.
pub fn new<U: Into<String>, P: Into<String>>(
source_url: U,
destination_path: P,
@@ -38,12 +32,10 @@ impl DownloadInfo {
}
}
- /// Serialize this DownloadInfo to a compact JSON string.
pub fn to_json(&self) -> Result<String, serde_json::Error> {
serde_json::to_string(self)
}
- /// Deserialize a DownloadInfo from a JSON string.
pub fn from_json(json: &str) -> Result<Self, serde_json::Error> {
serde_json::from_str(json)
}
diff --git a/crates/download-manager/src/download_log_event.rs b/crates/download-manager/src/download_log_event.rs
new file mode 100644
index 000000000..13c587275
--- /dev/null
+++ b/crates/download-manager/src/download_log_event.rs
@@ -0,0 +1,50 @@
+use serde::{Deserialize, Serialize};
+
+use crate::{DownloadId, FileDownloadEvent, file_download_task_actor::BackendEvent, traits::ActiveDownloadGeneration};
+
+#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, Hash)]
+pub(crate) enum DownloadLogEvent {
+ ManagerCreated {
+ manager_id: String,
+ },
+ StartupReconciled {
+ download_id: DownloadId,
+ initial_lifecycle_state: &'static str,
+ action_count: usize,
+ },
+ TaskSpawned {
+ download_id: DownloadId,
+ },
+ StateTransition {
+ download_id: DownloadId,
+ from: &'static str,
+ to: &'static str,
+ },
+ BackendProgress {
+ download_id: DownloadId,
+ generation: ActiveDownloadGeneration,
+ downloaded_bytes: u64,
+ total_bytes: Option<u64>,
+ },
+ BackendTerminal {
+ download_id: DownloadId,
+ event: BackendEvent,
+ },
+ PublicEventEmitted {
+ download_id: DownloadId,
+ event: FileDownloadEvent,
+ },
+}
+
+pub(crate) fn log(event: DownloadLogEvent) {
+ match &event {
+ DownloadLogEvent::BackendProgress {
+ ..
+ } => {
+ tracing::trace!(download_log_event = ?event, "download manager event");
+ },
+ _ => {
+ tracing::debug!(download_log_event = ?event, "download manager event");
+ },
+ }
+}
diff --git a/crates/download-manager/src/download_manager_state.rs b/crates/download-manager/src/download_manager_state.rs
deleted file mode 100644
index 954f9c2fa..000000000
--- a/crates/download-manager/src/download_manager_state.rs
+++ /dev/null
@@ -1,71 +0,0 @@
-use std::{collections::HashMap, sync::Arc};
-
-use tokio::sync::broadcast::channel as tokio_broadcast_channel;
-use tokio_stream::wrappers::BroadcastStream as TokioBroadcastStream;
-
-use crate::{
- DownloadError, DownloadEvent, DownloadId, FileDownloadTask, SharedDownloadEventSender, TokioHandle, TokioMutex,
-};
-
-pub(crate) type TaskCache = Arc<TokioMutex<HashMap<DownloadId, Arc<dyn FileDownloadTask>>>>;
-
-#[derive(Clone)]
-pub(crate) struct DownloadManagerState {
- pub(crate) manager_id: String,
- pub(crate) global_broadcast_sender: SharedDownloadEventSender,
- pub(crate) tokio_handle: TokioHandle,
- pub(crate) task_cache: TaskCache,
-}
-
-impl std::fmt::Debug for DownloadManagerState {
- fn fmt(
- &self,
- formatter: &mut std::fmt::Formatter<'_>,
- ) -> std::fmt::Result {
- formatter.debug_struct("DownloadManagerState").field("manager_id", &self.manager_id).finish()
- }
-}
-
-impl DownloadManagerState {
- pub(crate) fn new(
- suffix: &str,
- tokio_handle: TokioHandle,
- ) -> Self {
- let manager_id = generate_manager_id(suffix);
- let (global_broadcast_sender, _global_broadcast_receiver) = tokio_broadcast_channel::<DownloadEvent>(256);
- Self {
- manager_id,
- global_broadcast_sender: Arc::new(global_broadcast_sender),
- tokio_handle,
- task_cache: Arc::new(TokioMutex::new(HashMap::new())),
- }
- }
-
- pub(crate) fn subscribe_to_all_downloads(&self) -> TokioBroadcastStream<DownloadEvent> {
- TokioBroadcastStream::new(self.global_broadcast_sender.subscribe())
- }
-
- pub(crate) async fn get_all_file_tasks(&self) -> Result<Vec<Arc<dyn FileDownloadTask>>, DownloadError> {
- let task_cache_guard = self.task_cache.lock().await;
- Ok(task_cache_guard.values().cloned().collect())
- }
-}
-
-fn generate_manager_id(suffix: &str) -> String {
- #[cfg(target_vendor = "apple")]
- {
- use crate::NSBundle;
- let bundle_id = NSBundle::mainBundle().bundleIdentifier().unwrap_or_default().to_string();
-
- if bundle_id.is_empty() {
- format!("mirai.{suffix}")
- } else {
- format!("{bundle_id}.mirai.{suffix}")
- }
- }
-
- #[cfg(not(target_vendor = "apple"))]
- {
- format!("mirai.{suffix}")
- }
-}
diff --git a/crates/download-manager/src/download_state.rs b/crates/download-manager/src/download_state.rs
index 67ced174d..86983b1a7 100644
--- a/crates/download-manager/src/download_state.rs
+++ b/crates/download-manager/src/download_state.rs
@@ -1,4 +1,6 @@
-#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+use serde::{Deserialize, Serialize};
+
+#[derive(Serialize, Deserialize, Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum DownloadState {
Downloading,
Paused,
diff --git a/crates/download-manager/src/file_check.rs b/crates/download-manager/src/file_check.rs
index 1fd5ac5b4..21857f77a 100644
--- a/crates/download-manager/src/file_check.rs
+++ b/crates/download-manager/src/file_check.rs
@@ -1,5 +1,16 @@
-#[derive(Debug, Clone)]
+use serde::{Deserialize, Serialize};
+
+#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, Hash)]
pub enum FileCheck {
CRC(String),
None,
}
+
+impl FileCheck {
+ pub fn expected_crc(&self) -> Option<String> {
+ match self {
+ Self::CRC(crc) => Some(crc.clone()),
+ Self::None => None,
+ }
+ }
+}
diff --git a/crates/download-manager/src/file_download_event.rs b/crates/download-manager/src/file_download_event.rs
index c171fdadc..3f88f84c8 100644
--- a/crates/download-manager/src/file_download_event.rs
+++ b/crates/download-manager/src/file_download_event.rs
@@ -1,6 +1,8 @@
-use crate::PathBuf;
+use std::path::PathBuf;
-#[derive(Debug, Clone)]
+use serde::{Deserialize, Serialize};
+
+#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, Hash)]
pub enum FileDownloadEvent {
DownloadCompleted {
tmp_path: PathBuf,
diff --git a/crates/download-manager/src/file_download_manager.rs b/crates/download-manager/src/file_download_manager.rs
index a5a1bd495..4417526ca 100644
--- a/crates/download-manager/src/file_download_manager.rs
+++ b/crates/download-manager/src/file_download_manager.rs
@@ -3,75 +3,88 @@ use std::{path::Path, sync::Arc};
use tokio::{runtime::Handle as TokioHandle, sync::broadcast::Sender as TokioBroadcastSender};
use tokio_stream::wrappers::BroadcastStream as TokioBroadcastStream;
-use crate::{DownloadError, DownloadId, FileCheck, FileDownloadEvent, FileDownloadTask};
+use crate::{
+ DownloadError, DownloadId, FileCheck, FileDownloadEvent, FileDownloadTask,
+ backends::{apple::AppleDownloadManager, universal::UniversalDownloadManager},
+};
pub type DownloadEvent = (DownloadId, FileDownloadEvent);
pub type DownloadEventSender = TokioBroadcastSender<DownloadEvent>;
pub type SharedDownloadEventSender = Arc<DownloadEventSender>;
+#[derive(serde::Serialize, serde::Deserialize, Clone, Copy, Debug, PartialEq, Eq, Hash)]
+#[serde(rename_all = "snake_case")]
+pub enum FileDownloadManagerType {
+ Universal,
+ Apple,
+}
+
+impl Default for FileDownloadManagerType {
+ fn default() -> Self {
+ if cfg!(target_vendor = "apple") {
+ Self::Apple
+ } else {
+ Self::Universal
+ }
+ }
+}
+
#[async_trait::async_trait]
pub trait FileDownloadManager: Send + Sync + 'static {
fn manager_id(&self) -> &str;
-
fn subscribe_to_all_downloads(&self) -> TokioBroadcastStream<DownloadEvent>;
-
fn global_broadcast_sender(&self) -> SharedDownloadEventSender;
async fn get_all_file_tasks(&self) -> Result<Vec<Arc<dyn FileDownloadTask>>, DownloadError>;
+ async fn remove_file_task(
+ &self,
+ download_id: DownloadId,
+ ) -> Result<(), DownloadError>;
+
async fn file_download_task(
&self,
- source_url: &String,
+ source_url: &str,
destination_path: &Path,
file_check: FileCheck,
expected_bytes: Option<u64>,
) -> Result<Arc<dyn FileDownloadTask>, DownloadError>;
-}
-#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
-pub enum FileDownloadManagerType {
- Universal,
- Apple,
+ async fn destination_foreign_lock(
+ &self,
+ _destination_path: &Path,
+ ) -> Option<String> {
+ None
+ }
}
-impl Default for FileDownloadManagerType {
- fn default() -> Self {
- if cfg!(target_vendor = "apple") {
- FileDownloadManagerType::Apple
- } else {
- FileDownloadManagerType::Universal
+impl dyn FileDownloadManager {
+ pub async fn new(
+ file_download_manager_type: FileDownloadManagerType,
+ tokio_handle: TokioHandle,
+ ) -> Result<Box<dyn FileDownloadManager>, DownloadError> {
+ match file_download_manager_type {
+ FileDownloadManagerType::Universal => {
+ let manager: Box<dyn FileDownloadManager> =
+ Box::new(UniversalDownloadManager::from_tokio_handle(tokio_handle)?);
+ Ok(manager)
+ },
+ FileDownloadManagerType::Apple => {
+ #[cfg(target_vendor = "apple")]
+ {
+ let manager: Box<dyn FileDownloadManager> =
+ Box::new(AppleDownloadManager::from_tokio_handle(tokio_handle)?);
+ Ok(manager)
+ }
+ #[cfg(not(target_vendor = "apple"))]
+ {
+ Err(DownloadError::UnsupportedType)
+ }
+ },
}
}
-}
-#[allow(unreachable_code)]
-pub async fn create_download_manager(
- r#type: FileDownloadManagerType,
- tokio_handle: TokioHandle,
-) -> Result<Box<dyn FileDownloadManager>, DownloadError> {
- match r#type {
- FileDownloadManagerType::Universal => {
- use crate::managers::universal::{AsyncFetcherConfig, AsyncFetcherDownloadManager};
- let config = AsyncFetcherConfig::default()
- .with_connections_per_file(4)
- .with_retries(3)
- .with_progress_interval_ms(500);
- let manager = AsyncFetcherDownloadManager::new(config, tokio_handle).await?;
- return Ok(Box::new(manager) as Box<dyn FileDownloadManager>);
- },
- FileDownloadManagerType::Apple => {
- #[cfg(target_vendor = "apple")]
- {
- use crate::managers::apple::{SessionConfig, URLSessionDownloadManager, URLSessionDropPolicy};
- let manager = URLSessionDownloadManager::new(
- SessionConfig::default(),
- URLSessionDropPolicy::FinishTasksAndInvalidate,
- tokio_handle,
- )
- .await?;
- return Ok(Box::new(manager) as Box<dyn FileDownloadManager>);
- }
- return Err(DownloadError::UnsupportedType);
- },
- };
+ pub async fn system_default(tokio_handle: TokioHandle) -> Result<Box<dyn FileDownloadManager>, DownloadError> {
+ Self::new(FileDownloadManagerType::default(), tokio_handle).await
+ }
}
diff --git a/crates/download-manager/src/file_download_phase.rs b/crates/download-manager/src/file_download_phase.rs
index 6436ed3ec..e5165fc8c 100644
--- a/crates/download-manager/src/file_download_phase.rs
+++ b/crates/download-manager/src/file_download_phase.rs
@@ -1,4 +1,6 @@
-#[derive(Debug, Clone, PartialEq, Eq)]
+use serde::{Deserialize, Serialize};
+
+#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, Hash)]
pub enum FileDownloadPhase {
NotDownloaded,
Downloading,
diff --git a/crates/download-manager/src/file_download_state.rs b/crates/download-manager/src/file_download_state.rs
index 4a9f9f2d5..9e439c58f 100644
--- a/crates/download-manager/src/file_download_state.rs
+++ b/crates/download-manager/src/file_download_state.rs
@@ -1,12 +1,11 @@
+use serde::{Deserialize, Serialize};
+
use crate::FileDownloadPhase;
-#[derive(Debug, Clone, PartialEq, Eq)]
+#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, Hash)]
pub struct FileDownloadState {
- /// Total size of the file in bytes.
pub total_bytes: u64,
- /// Bytes already downloaded (meaningful in Downloading / Paused).
pub downloaded_bytes: u64,
- /// Current phase of the download.
pub phase: FileDownloadPhase,
}
@@ -49,11 +48,19 @@ impl FileDownloadState {
}
}
- pub fn error(err: String) -> Self {
+ pub fn locked_by_other(manager_id: String) -> Self {
+ Self {
+ total_bytes: 0,
+ downloaded_bytes: 0,
+ phase: FileDownloadPhase::LockedByOther(manager_id),
+ }
+ }
+
+ pub fn error(message: String) -> Self {
Self {
total_bytes: 0,
downloaded_bytes: 0,
- phase: FileDownloadPhase::Error(err),
+ phase: FileDownloadPhase::Error(message),
}
}
}
diff --git a/crates/download-manager/src/file_download_task.rs b/crates/download-manager/src/file_download_task.rs
index 71af57270..dff86b675 100644
--- a/crates/download-manager/src/file_download_task.rs
+++ b/crates/download-manager/src/file_download_task.rs
@@ -1,16 +1,17 @@
-use std::path::Path;
+use std::{fmt::Debug, path::Path, sync::Arc};
-use crate::{
- DownloadError, DownloadEventSender, DownloadId, FileCheck, FileDownloadState,
- prelude::{TokioBroadcastSender, TokioBroadcastStream},
-};
+use tokio::sync::broadcast::Sender as TokioBroadcastSender;
+use tokio_stream::wrappers::BroadcastStream as TokioBroadcastStream;
+
+use crate::{DownloadError, DownloadEventSender, DownloadId, FileCheck, FileDownloadState};
#[async_trait::async_trait]
-pub trait FileDownloadTask: Send + Sync + std::fmt::Debug {
+pub trait FileDownloadTask: Send + Sync + Debug {
fn download_id(&self) -> DownloadId;
fn source_url(&self) -> &str;
fn destination(&self) -> &Path;
fn file_check(&self) -> &FileCheck;
+ fn expected_bytes(&self) -> Option<u64>;
async fn download(&self) -> Result<(), DownloadError>;
async fn pause(&self) -> Result<(), DownloadError>;
@@ -23,9 +24,43 @@ pub trait FileDownloadTask: Send + Sync + std::fmt::Debug {
global_broadcast: DownloadEventSender,
);
async fn stop_listening(&self);
-
- /// Wait for the task to fully complete (download finished, lock released, state updated).
async fn wait(&self);
fn broadcast_sender(&self) -> TokioBroadcastSender<FileDownloadState>;
}
+
+#[async_trait::async_trait]
+pub(crate) trait ManagedFileDownloadTask: FileDownloadTask {
+ async fn shutdown_for_removal(&self) -> Result<(), DownloadError>;
+ fn is_stopped(&self) -> bool;
+}
+
+#[derive(Clone)]
+pub(crate) struct CachedFileDownloadTask {
+ public: Arc<dyn FileDownloadTask>,
+ managed: Arc<dyn ManagedFileDownloadTask>,
+}
+
+impl CachedFileDownloadTask {
+ pub(crate) fn new(
+ public: Arc<dyn FileDownloadTask>,
+ managed: Arc<dyn ManagedFileDownloadTask>,
+ ) -> Self {
+ Self {
+ public,
+ managed,
+ }
+ }
+
+ pub(crate) fn public(&self) -> Arc<dyn FileDownloadTask> {
+ Arc::clone(&self.public)
+ }
+
+ pub(crate) fn managed(&self) -> Arc<dyn ManagedFileDownloadTask> {
+ Arc::clone(&self.managed)
+ }
+
+ pub(crate) fn is_stopped(&self) -> bool {
+ self.managed.is_stopped()
+ }
+}
diff --git a/crates/download-manager/src/file_download_task_actor/actor_state.rs b/crates/download-manager/src/file_download_task_actor/actor_state.rs
new file mode 100644
index 000000000..f685eba71
--- /dev/null
+++ b/crates/download-manager/src/file_download_task_actor/actor_state.rs
@@ -0,0 +1,68 @@
+use std::{fmt, path::PathBuf};
+
+use crate::{
+ lock_manager::DestinationLockLease,
+ reducer::InitialLifecycleState,
+ traits::{ActiveDownloadGeneration, DownloadBackend},
+};
+
+pub(crate) enum DownloadActorState<B: DownloadBackend> {
+ NotDownloaded,
+ Paused {
+ part_path: PathBuf,
+ },
+ Downloading {
+ active_task: B::ActiveTask,
+ generation: ActiveDownloadGeneration,
+ destination_lease: DestinationLockLease,
+ },
+ Downloaded,
+}
+
+impl<B: DownloadBackend> DownloadActorState<B> {
+ pub(crate) fn name(&self) -> &'static str {
+ match self {
+ Self::NotDownloaded => "NotDownloaded",
+ Self::Paused {
+ ..
+ } => "Paused",
+ Self::Downloading {
+ ..
+ } => "Downloading",
+ Self::Downloaded => "Downloaded",
+ }
+ }
+}
+
+impl<B: DownloadBackend> fmt::Debug for DownloadActorState<B> {
+ fn fmt(
+ &self,
+ formatter: &mut fmt::Formatter<'_>,
+ ) -> fmt::Result {
+ match self {
+ Self::NotDownloaded => formatter.debug_struct("NotDownloaded").finish(),
+ Self::Paused {
+ part_path,
+ } => formatter.debug_struct("Paused").field("part_path", part_path).finish(),
+ Self::Downloading {
+ generation,
+ ..
+ } => formatter.debug_struct("Downloading").field("generation", generation).finish_non_exhaustive(),
+ Self::Downloaded => formatter.debug_struct("Downloaded").finish(),
+ }
+ }
+}
+
+impl<B: DownloadBackend> From<InitialLifecycleState> for DownloadActorState<B> {
+ fn from(initial_lifecycle_state: InitialLifecycleState) -> Self {
+ match initial_lifecycle_state {
+ InitialLifecycleState::NotDownloaded => Self::NotDownloaded,
+ InitialLifecycleState::Paused {
+ part_path,
+ } => Self::Paused {
+ part_path,
+ },
+ InitialLifecycleState::Downloaded => Self::Downloaded,
+ }
+ }
+}
diff --git a/crates/download-manager/src/file_download_task_actor/backend_event.rs b/crates/download-manager/src/file_download_task_actor/backend_event.rs
new file mode 100644
index 000000000..e05154479
--- /dev/null
+++ b/crates/download-manager/src/file_download_task_actor/backend_event.rs
@@ -0,0 +1,30 @@
+use crate::traits::ActiveDownloadGeneration;
+
+#[derive(serde::Serialize, serde::Deserialize, Clone, Debug, PartialEq, Eq, Hash)]
+pub enum BackendEvent {
+ Completed {
+ generation: ActiveDownloadGeneration,
+ },
+ Error {
+ generation: ActiveDownloadGeneration,
+ message: String,
+ },
+}
+
+impl BackendEvent {
+ pub fn completed(generation: ActiveDownloadGeneration) -> Self {
+ Self::Completed {
+ generation,
+ }
+ }
+
+ pub fn error(
+ generation: ActiveDownloadGeneration,
+ message: String,
+ ) -> Self {
+ Self::Error {
+ generation,
+ message,
+ }
+ }
+}
diff --git a/crates/download-manager/src/file_download_task_actor/backend_progress.rs b/crates/download-manager/src/file_download_task_actor/backend_progress.rs
new file mode 100644
index 000000000..a875ba66a
--- /dev/null
+++ b/crates/download-manager/src/file_download_task_actor/backend_progress.rs
@@ -0,0 +1,8 @@
+use crate::traits::ActiveDownloadGeneration;
+
+#[derive(Clone, Debug, PartialEq, Eq, Hash)]
+pub struct BackendProgress {
+ pub generation: ActiveDownloadGeneration,
+ pub downloaded_bytes: u64,
+ pub total_bytes: Option<u64>,
+}
diff --git a/crates/download-manager/src/file_download_task_actor/download_task_actor.rs b/crates/download-manager/src/file_download_task_actor/download_task_actor.rs
new file mode 100644
index 000000000..fa7fcdf68
--- /dev/null
+++ b/crates/download-manager/src/file_download_task_actor/download_task_actor.rs
@@ -0,0 +1,643 @@
+use std::{
+ path::{Path, PathBuf},
+ sync::Arc,
+};
+
+use tokio::sync::{
+ Mutex as TokioMutex,
+ broadcast::Sender as TokioBroadcastSender,
+ mpsc::Receiver as TokioMpscReceiver,
+ oneshot::Sender as TokioOneshotSender,
+ watch::{Receiver as TokioWatchReceiver, Sender as TokioWatchSender},
+};
+
+use crate::{
+ DownloadError, FileCheck, FileDownloadState, LockFileState, check_lock_file,
+ crc_utils::{calculate_and_verify_crc, crc_path_for_file, save_crc_file},
+ download_log_event::{DownloadLogEvent, log},
+ file_download_task_actor::{
+ BackendEvent, DownloadActorState, PendingProgressSlot, ProgressCounters, PublicProjection, TaskCommand,
+ TerminalOutcome, project_runtime_public_state,
+ },
+ lock_manager::{DestinationLockLease, lock_path_for_destination},
+ release_lock_if_owned,
+ traits::{
+ ActiveDownloadGeneration, ActiveDownloadGenerationCounter, ActiveTask, BackendContext, BackendEventSender,
+ DownloadBackend, DownloadConfig,
+ },
+};
+
+enum ActorLoopExit {
+ AlreadyStopped,
+ PreserveArtifacts,
+}
+
+pub struct DownloadTaskActor<B: DownloadBackend> {
+ config: Arc<DownloadConfig>,
+ context: Arc<B::Context>,
+ backend_event_sender: BackendEventSender,
+ generation_counter: ActiveDownloadGenerationCounter,
+ state: DownloadActorState<B>,
+ projection: PublicProjection,
+ progress_counters: ProgressCounters,
+ command_receiver: TokioMpscReceiver<TaskCommand>,
+ backend_event_receiver: TokioMpscReceiver<BackendEvent>,
+ pending_progress: Arc<TokioMutex<PendingProgressSlot>>,
+ progress_waker_receiver: TokioWatchReceiver<()>,
+ public_state_sender: TokioWatchSender<FileDownloadState>,
+ progress_sender: TokioBroadcastSender<FileDownloadState>,
+ terminal_sender: TokioWatchSender<TerminalOutcome>,
+ pending_terminal_outcome: Option<TerminalOutcome>,
+}
+
+impl<B: DownloadBackend> DownloadTaskActor<B> {
+ #[allow(clippy::too_many_arguments)]
+ pub fn new(
+ config: Arc<DownloadConfig>,
+ context: Arc<B::Context>,
+ backend_event_sender: BackendEventSender,
+ generation_counter: ActiveDownloadGenerationCounter,
+ state: DownloadActorState<B>,
+ projection: PublicProjection,
+ progress_counters: ProgressCounters,
+ command_receiver: TokioMpscReceiver<TaskCommand>,
+ backend_event_receiver: TokioMpscReceiver<BackendEvent>,
+ pending_progress: Arc<TokioMutex<PendingProgressSlot>>,
+ progress_waker_receiver: TokioWatchReceiver<()>,
+ public_state_sender: TokioWatchSender<FileDownloadState>,
+ progress_sender: TokioBroadcastSender<FileDownloadState>,
+ terminal_sender: TokioWatchSender<TerminalOutcome>,
+ ) -> Self {
+ Self {
+ config,
+ context,
+ backend_event_sender,
+ generation_counter,
+ state,
+ projection,
+ progress_counters,
+ command_receiver,
+ backend_event_receiver,
+ pending_progress,
+ progress_waker_receiver,
+ public_state_sender,
+ progress_sender,
+ terminal_sender,
+ pending_terminal_outcome: None,
+ }
+ }
+
+ pub async fn run(mut self) {
+ self.publish_current_state();
+
+ let mut loop_exit = ActorLoopExit::PreserveArtifacts;
+ loop {
+ tokio::select! {
+ command = self.command_receiver.recv() => {
+ let Some(command) = command else {
+ break;
+ };
+ if !self.handle_command(command).await {
+ loop_exit = ActorLoopExit::AlreadyStopped;
+ break;
+ }
+ }
+ backend_event = self.backend_event_receiver.recv() => {
+ let Some(backend_event) = backend_event else {
+ break;
+ };
+ self.handle_backend_event(backend_event).await;
+ self.publish_current_state();
+ self.flush_terminal_outcome();
+ }
+ progress_wake_result = self.progress_waker_receiver.changed() => {
+ if progress_wake_result.is_err() {
+ break;
+ }
+ self.handle_pending_progress().await;
+ self.publish_current_state();
+ self.flush_terminal_outcome();
+ }
+ }
+ }
+
+ if matches!(loop_exit, ActorLoopExit::PreserveArtifacts) {
+ self.stop_preserving_artifacts().await;
+ self.publish_current_state();
+ self.flush_terminal_outcome();
+ }
+ let _ = self.terminal_sender.send(TerminalOutcome::ActorStopped);
+ }
+
+ async fn stop_preserving_artifacts(&mut self) {
+ let current_state = std::mem::replace(&mut self.state, DownloadActorState::NotDownloaded);
+ let from_state = current_state.name();
+ match current_state {
+ DownloadActorState::Downloading {
+ active_task,
+ destination_lease,
+ ..
+ } => match active_task.pause(&self.config.destination).await {
+ Ok(part_path) => {
+ let downloaded_bytes = B::read_resume_progress(&part_path).unwrap_or(0);
+ self.progress_counters = ProgressCounters {
+ downloaded_bytes,
+ total_bytes: self.config.expected_bytes.unwrap_or(downloaded_bytes),
+ };
+ release_destination_lease(destination_lease).await;
+ self.finish_transition(
+ from_state,
+ DownloadActorState::Paused {
+ part_path,
+ },
+ );
+ },
+ Err(error) => {
+ tracing::debug!("failed to preserve resume data while stopping download actor: {error}");
+ self.progress_counters = ProgressCounters::default();
+ release_destination_lease(destination_lease).await;
+ self.finish_transition(from_state, DownloadActorState::NotDownloaded);
+ },
+ },
+ state => {
+ self.state = state;
+ },
+ }
+
+ let lock_path = lock_path_for_destination(&self.config.destination);
+ let _ = release_lock_if_owned(&lock_path, &self.config.manager_id, self.config.manager_instance_id).await;
+ }
+
+ async fn handle_command(
+ &mut self,
+ command: TaskCommand,
+ ) -> bool {
+ match command {
+ TaskCommand::Download {
+ reply_sender,
+ } => {
+ let result = self.handle_download().await;
+ self.publish_current_state();
+ self.flush_terminal_outcome();
+ send_reply(reply_sender, result);
+ true
+ },
+ TaskCommand::Pause {
+ reply_sender,
+ } => {
+ let result = self.handle_pause().await;
+ self.publish_current_state();
+ self.flush_terminal_outcome();
+ send_reply(reply_sender, result);
+ true
+ },
+ TaskCommand::Cancel {
+ reply_sender,
+ } => {
+ let result = self.handle_cancel_or_remove().await;
+ self.publish_current_state();
+ self.flush_terminal_outcome();
+ send_reply(reply_sender, result);
+ true
+ },
+ TaskCommand::Remove {
+ reply_sender,
+ } => {
+ let result = self.handle_cancel_or_remove().await;
+ self.publish_current_state();
+ self.flush_terminal_outcome();
+ send_reply(reply_sender, result);
+ false
+ },
+ }
+ }
+
+ async fn handle_download(&mut self) -> Result<(), DownloadError> {
+ match &self.state {
+ DownloadActorState::NotDownloaded => self.start_fresh_download().await,
+ DownloadActorState::Paused {
+ part_path,
+ } => self.resume_download(part_path.clone()).await,
+ DownloadActorState::Downloading {
+ ..
+ }
+ | DownloadActorState::Downloaded => Ok(()),
+ }
+ }
+
+ async fn handle_pause(&mut self) -> Result<(), DownloadError> {
+ let current_state = std::mem::replace(&mut self.state, DownloadActorState::NotDownloaded);
+ let from_state = current_state.name();
+ match current_state {
+ DownloadActorState::Downloading {
+ active_task,
+ destination_lease,
+ ..
+ } => {
+ let pause_result = active_task.pause(&self.config.destination).await;
+ release_destination_lease(destination_lease).await;
+ match pause_result {
+ Ok(part_path) => {
+ let downloaded_bytes = B::read_resume_progress(&part_path).unwrap_or(0);
+ self.progress_counters = ProgressCounters {
+ downloaded_bytes,
+ total_bytes: self.config.expected_bytes.unwrap_or(downloaded_bytes),
+ };
+ self.finish_transition(
+ from_state,
+ DownloadActorState::Paused {
+ part_path,
+ },
+ );
+ Ok(())
+ },
+ Err(error) => {
+ let message = error.to_string();
+ self.projection = PublicProjection::StickyError(message.clone());
+ self.progress_counters = ProgressCounters::default();
+ self.pending_terminal_outcome = Some(TerminalOutcome::Error(message.clone()));
+ self.finish_transition(from_state, DownloadActorState::NotDownloaded);
+ Err(DownloadError::Backend(message))
+ },
+ }
+ },
+ DownloadActorState::Paused {
+ ..
+ } => {
+ self.state = current_state;
+ Ok(())
+ },
+ state => {
+ self.state = state;
+ Err(DownloadError::InvalidStateTransition)
+ },
+ }
+ }
+
+ async fn handle_cancel_or_remove(&mut self) -> Result<(), DownloadError> {
+ let current_state = std::mem::replace(&mut self.state, DownloadActorState::NotDownloaded);
+ let from_state = current_state.name();
+ match current_state {
+ DownloadActorState::Downloading {
+ active_task,
+ destination_lease,
+ ..
+ } => {
+ let _ = active_task.cancel(&self.config.destination).await;
+ remove_resume_artifact(&self.config.destination);
+ self.progress_counters = ProgressCounters::default();
+ self.projection = PublicProjection::None;
+ release_destination_lease(destination_lease).await;
+ self.finish_transition(from_state, DownloadActorState::NotDownloaded);
+ Ok(())
+ },
+ DownloadActorState::Paused {
+ part_path,
+ } => {
+ remove_file(&part_path);
+ self.progress_counters = ProgressCounters::default();
+ self.projection = PublicProjection::None;
+ self.finish_transition(from_state, DownloadActorState::NotDownloaded);
+ Ok(())
+ },
+ state => {
+ self.state = state;
+ if matches!(self.projection, PublicProjection::StickyError(_)) {
+ self.projection = PublicProjection::None;
+ self.progress_counters = ProgressCounters::default();
+ }
+ Ok(())
+ },
+ }
+ }
+
+ async fn handle_backend_event(
+ &mut self,
+ backend_event: BackendEvent,
+ ) {
+ match backend_event {
+ BackendEvent::Completed {
+ generation,
+ } => self.handle_backend_completed(generation).await,
+ BackendEvent::Error {
+ generation,
+ message,
+ } => self.handle_backend_error(generation, message).await,
+ }
+ }
+
+ async fn handle_backend_completed(
+ &mut self,
+ completed_generation: ActiveDownloadGeneration,
+ ) {
+ let should_handle = matches!(
+ &self.state,
+ DownloadActorState::Downloading {
+ generation,
+ ..
+ } if *generation == completed_generation
+ );
+ if !should_handle {
+ return;
+ }
+
+ let current_state = std::mem::replace(&mut self.state, DownloadActorState::NotDownloaded);
+ let from_state = current_state.name();
+ if let DownloadActorState::Downloading {
+ destination_lease,
+ ..
+ } = current_state
+ {
+ match validate_completed_file(&self.config).await {
+ Ok(total_bytes) => {
+ remove_resume_artifact(&self.config.destination);
+ self.progress_counters = ProgressCounters {
+ downloaded_bytes: total_bytes,
+ total_bytes,
+ };
+ self.projection = PublicProjection::None;
+ release_destination_lease(destination_lease).await;
+ self.finish_transition(from_state, DownloadActorState::Downloaded);
+ self.pending_terminal_outcome = Some(TerminalOutcome::Downloaded);
+ },
+ Err(message) => {
+ remove_file(&self.config.destination);
+ remove_resume_artifact(&self.config.destination);
+ remove_file(&crc_path_for_file(&self.config.destination));
+ self.progress_counters = ProgressCounters::default();
+ self.projection = PublicProjection::StickyError(message.clone());
+ release_destination_lease(destination_lease).await;
+ self.finish_transition(from_state, DownloadActorState::NotDownloaded);
+ self.pending_terminal_outcome = Some(TerminalOutcome::Error(message));
+ },
+ }
+ }
+ }
+
+ async fn handle_backend_error(
+ &mut self,
+ error_generation: ActiveDownloadGeneration,
+ message: String,
+ ) {
+ let should_handle = matches!(
+ &self.state,
+ DownloadActorState::Downloading {
+ generation,
+ ..
+ } if *generation == error_generation
+ );
+ if !should_handle {
+ return;
+ }
+
+ let current_state = std::mem::replace(&mut self.state, DownloadActorState::NotDownloaded);
+ let from_state = current_state.name();
+ if let DownloadActorState::Downloading {
+ active_task,
+ destination_lease,
+ ..
+ } = current_state
+ {
+ let _ = active_task.cancel(&self.config.destination).await;
+ remove_resume_artifact(&self.config.destination);
+ self.projection = PublicProjection::StickyError(message.clone());
+ self.progress_counters = ProgressCounters::default();
+ release_destination_lease(destination_lease).await;
+ self.finish_transition(from_state, DownloadActorState::NotDownloaded);
+ self.pending_terminal_outcome = Some(TerminalOutcome::Error(message));
+ }
+ }
+
+ async fn handle_pending_progress(&mut self) {
+ let progress = self.pending_progress.lock().await.take();
+ let Some(progress) = progress else {
+ return;
+ };
+
+ if let DownloadActorState::Downloading {
+ generation,
+ ..
+ } = &self.state
+ && *generation == progress.generation
+ {
+ self.progress_counters = ProgressCounters {
+ downloaded_bytes: progress.downloaded_bytes,
+ total_bytes: progress.total_bytes.or(self.config.expected_bytes).unwrap_or(progress.downloaded_bytes),
+ };
+ }
+ }
+
+ async fn start_fresh_download(&mut self) -> Result<(), DownloadError> {
+ let lease = self.acquire_destination_lease().await?;
+ let generation = self.generation_counter.allocate_next();
+ let active_task = match self
+ .context
+ .download(Arc::clone(&self.config), generation, self.backend_event_sender.clone(), &lease)
+ .await
+ {
+ Ok(active_task) => active_task,
+ Err(error) => {
+ release_destination_lease(lease).await;
+ return Err(DownloadError::Backend(error.to_string()));
+ },
+ };
+
+ self.projection = PublicProjection::None;
+ self.progress_counters = ProgressCounters {
+ downloaded_bytes: 0,
+ total_bytes: self.config.expected_bytes.unwrap_or(0),
+ };
+ self.transition_to(DownloadActorState::Downloading {
+ active_task,
+ generation,
+ destination_lease: lease,
+ });
+ Ok(())
+ }
+
+ async fn resume_download(
+ &mut self,
+ part_path: PathBuf,
+ ) -> Result<(), DownloadError> {
+ if !part_path.exists() {
+ remove_file(&part_path);
+ return self.start_fresh_download().await;
+ }
+
+ let lease = self.acquire_destination_lease().await?;
+ let generation = self.generation_counter.allocate_next();
+ let resume_bytes = B::read_resume_progress(&part_path).unwrap_or(0);
+ let active_task = match self
+ .context
+ .resume(Arc::clone(&self.config), generation, &part_path, self.backend_event_sender.clone(), &lease)
+ .await
+ {
+ Ok(active_task) => active_task,
+ Err(error) => {
+ release_destination_lease(lease).await;
+ remove_file(&part_path);
+ let message = error.to_string();
+ self.projection = PublicProjection::StickyError(message.clone());
+ self.progress_counters = ProgressCounters::default();
+ self.transition_to(DownloadActorState::NotDownloaded);
+ self.pending_terminal_outcome = Some(TerminalOutcome::Error(message.clone()));
+ return Err(DownloadError::Backend(message));
+ },
+ };
+
+ self.projection = PublicProjection::None;
+ self.progress_counters = ProgressCounters {
+ downloaded_bytes: resume_bytes,
+ total_bytes: self.config.expected_bytes.unwrap_or(resume_bytes),
+ };
+ self.transition_to(DownloadActorState::Downloading {
+ active_task,
+ generation,
+ destination_lease: lease,
+ });
+ Ok(())
+ }
+
+ async fn acquire_destination_lease(&mut self) -> Result<DestinationLockLease, DownloadError> {
+ let lock_path = lock_path_for_destination(&self.config.destination);
+ match check_lock_file(&lock_path, &self.config.manager_id, self.config.manager_instance_id, std::process::id())
+ .await
+ {
+ LockFileState::OwnedByOtherApp(lock_file_info) => {
+ self.projection = PublicProjection::LockedByOther(lock_file_info.manager_id.clone());
+ Err(DownloadError::LockedByOther(lock_file_info.manager_id))
+ },
+ LockFileState::Missing
+ | LockFileState::OwnedByUs(_)
+ | LockFileState::OwnedBySameAppOldProcess(_)
+ | LockFileState::Stale(_)
+ | LockFileState::StaleUnparseable(_) => {
+ match DestinationLockLease::acquire(
+ &lock_path,
+ &self.config.manager_id,
+ self.config.manager_instance_id,
+ )
+ .await
+ {
+ Ok(lease) => Ok(lease),
+ Err(error) if error.kind() == std::io::ErrorKind::AlreadyExists => {
+ match check_lock_file(
+ &lock_path,
+ &self.config.manager_id,
+ self.config.manager_instance_id,
+ std::process::id(),
+ )
+ .await
+ {
+ LockFileState::OwnedByOtherApp(lock_file_info) => {
+ self.projection = PublicProjection::LockedByOther(lock_file_info.manager_id.clone());
+ Err(DownloadError::LockedByOther(lock_file_info.manager_id))
+ },
+ _ => Err(DownloadError::from(error)),
+ }
+ },
+ Err(error) => Err(DownloadError::from(error)),
+ }
+ },
+ }
+ }
+
+ fn publish_current_state(&self) {
+ let public_state =
+ project_runtime_public_state(&self.state, &self.projection, self.progress_counters, &self.config);
+ let _ = self.public_state_sender.send(public_state.clone());
+ let _ = self.progress_sender.send(public_state);
+ }
+
+ fn flush_terminal_outcome(&mut self) {
+ if let Some(terminal_outcome) = self.pending_terminal_outcome.take() {
+ let _ = self.terminal_sender.send(terminal_outcome);
+ }
+ }
+
+ fn transition_to(
+ &mut self,
+ next_state: DownloadActorState<B>,
+ ) {
+ let from_state = self.state.name();
+ self.finish_transition(from_state, next_state);
+ }
+
+ fn finish_transition(
+ &mut self,
+ from_state: &'static str,
+ next_state: DownloadActorState<B>,
+ ) {
+ let to_state = next_state.name();
+ self.state = next_state;
+ if from_state != to_state {
+ log(DownloadLogEvent::StateTransition {
+ download_id: self.config.download_id,
+ from: from_state,
+ to: to_state,
+ });
+ }
+ }
+}
+
+fn send_reply(
+ reply_sender: TokioOneshotSender<Result<(), DownloadError>>,
+ result: Result<(), DownloadError>,
+) {
+ let _ = reply_sender.send(result);
+}
+
+async fn release_destination_lease(destination_lease: DestinationLockLease) {
+ let _ = destination_lease.release().await;
+}
+
+fn remove_file(path: &Path) {
+ let _ = std::fs::remove_file(path);
+}
+
+fn remove_resume_artifact(destination: &Path) {
+ remove_file(&destination.with_extension("part"));
+ remove_file(&destination.with_extension("resume_data"));
+}
+
+async fn validate_completed_file(config: &DownloadConfig) -> Result<u64, String> {
+ let mut metadata = config.destination.metadata().ok();
+ for _attempt in 0..10 {
+ if metadata.is_some() {
+ break;
+ }
+ tokio::time::sleep(std::time::Duration::from_millis(50)).await;
+ metadata = config.destination.metadata().ok();
+ }
+ let metadata = metadata.ok_or_else(|| "download completed but destination is missing".to_string())?;
+ let actual_bytes = metadata.len();
+
+ if let Some(expected_bytes) = config.expected_bytes
+ && expected_bytes != actual_bytes
+ {
+ return Err(format!("downloaded file is {actual_bytes} bytes but registry declared {expected_bytes}"));
+ }
+
+ let total_bytes = config.expected_bytes.unwrap_or(actual_bytes);
+
+ match &config.file_check {
+ FileCheck::None => Ok(total_bytes),
+ FileCheck::CRC(expected_crc) => {
+ let destination = config.destination.clone();
+ let expected = expected_crc.clone();
+ let crc_result = tokio::task::spawn_blocking(move || calculate_and_verify_crc(&destination, &expected))
+ .await
+ .map_err(|error| format!("CRC verification error: {error}"))?;
+ match crc_result {
+ Ok(true) => {
+ let destination = config.destination.clone();
+ let expected = expected_crc.clone();
+ let _ = tokio::task::spawn_blocking(move || save_crc_file(&destination, &expected)).await;
+ Ok(total_bytes)
+ },
+ Ok(false) => Err("CRC verification failed".to_string()),
+ Err(error) => Err(format!("CRC verification error: {error}")),
+ }
+ },
+ }
+}
diff --git a/crates/download-manager/src/file_download_task_actor/generic_file_download_task.rs b/crates/download-manager/src/file_download_task_actor/generic_file_download_task.rs
new file mode 100644
index 000000000..a84c0bbbd
--- /dev/null
+++ b/crates/download-manager/src/file_download_task_actor/generic_file_download_task.rs
@@ -0,0 +1,384 @@
+use std::{
+ fmt,
+ marker::PhantomData,
+ path::Path,
+ sync::{
+ Arc,
+ atomic::{AtomicBool, Ordering},
+ },
+};
+
+use tokio::{
+ sync::{
+ Mutex as TokioMutex,
+ broadcast::{Sender as TokioBroadcastSender, channel as tokio_broadcast_channel},
+ mpsc::{Sender as TokioMpscSender, channel as tokio_mpsc_channel},
+ oneshot::channel as tokio_oneshot_channel,
+ watch::{Receiver as TokioWatchReceiver, channel as tokio_watch_channel},
+ },
+ task::JoinHandle as TokioJoinHandle,
+};
+use tokio_stream::{StreamExt as TokioStreamExt, wrappers::BroadcastStream as TokioBroadcastStream};
+
+use crate::{
+ DownloadError, DownloadEventSender, DownloadId, FileCheck, FileDownloadEvent, FileDownloadPhase, FileDownloadState,
+ backends::common::{Backend, InitialTaskAttachment},
+ download_log_event::{DownloadLogEvent, log},
+ file_download_task::{FileDownloadTask, ManagedFileDownloadTask},
+ file_download_task_actor::{
+ DownloadActorState, DownloadTaskActor, PendingProgressSlot, ProgressCounters, PublicProjection, TaskCommand,
+ TerminalOutcome, project_runtime_public_state,
+ },
+ lock_manager::DestinationLockLease,
+ reducer::InitialLifecycleState,
+ traits::{ActiveDownloadGenerationCounter, BackendEventSender, DownloadBackend, DownloadConfig},
+};
+
+pub struct GenericFileDownloadTask<B: DownloadBackend> {
+ config: Arc<DownloadConfig>,
+ command_sender: TokioMpscSender<TaskCommand>,
+ public_state_receiver: TokioWatchReceiver<FileDownloadState>,
+ progress_sender: TokioBroadcastSender<FileDownloadState>,
+ terminal_receiver: TokioWatchReceiver<TerminalOutcome>,
+ listener_task: Arc<TokioMutex<Option<TokioJoinHandle<()>>>>,
+ is_stopped: AtomicBool,
+ backend: PhantomData<B>,
+}
+
+impl<B: DownloadBackend> GenericFileDownloadTask<B> {
+ pub(crate) async fn spawn_with_initial_attachment(
+ config: Arc<DownloadConfig>,
+ context: Arc<B::Context>,
+ initial_lifecycle_state: InitialLifecycleState,
+ initial_projection: PublicProjection,
+ initial_progress: ProgressCounters,
+ mut startup_lease: Option<DestinationLockLease>,
+ ) -> Result<Self, DownloadError>
+ where
+ B: Backend,
+ {
+ let (command_sender, command_receiver) = tokio_mpsc_channel(64);
+ let (backend_event_sender, backend_event_receiver) = tokio_mpsc_channel(64);
+ let pending_progress = Arc::new(TokioMutex::new(PendingProgressSlot::default()));
+ let (progress_waker_sender, progress_waker_receiver) = tokio_watch_channel(());
+ let backend_event_sender = BackendEventSender::new(
+ config.download_id,
+ backend_event_sender,
+ Arc::clone(&pending_progress),
+ progress_waker_sender,
+ );
+ let (progress_sender, _) = tokio_broadcast_channel(64);
+ let (terminal_sender, terminal_receiver) = tokio_watch_channel(TerminalOutcome::Pending);
+
+ let mut generation_counter = ActiveDownloadGenerationCounter::default();
+ let attachment_generation = generation_counter.allocate_next();
+ let attachment = if let Some(destination_lease) = startup_lease.as_ref() {
+ match B::initial_task_attachment(
+ context.as_ref(),
+ Arc::clone(&config),
+ attachment_generation,
+ backend_event_sender.clone(),
+ destination_lease,
+ )
+ .await
+ {
+ Ok(attachment) => attachment,
+ Err(error) => {
+ if let Some(lease) = startup_lease.take() {
+ let _ = lease.release().await;
+ }
+ return Err(error);
+ },
+ }
+ } else {
+ InitialTaskAttachment::None
+ };
+ let (lifecycle_state, progress_counters) = match (attachment, &initial_lifecycle_state) {
+ (InitialTaskAttachment::None, _) => (initial_lifecycle_state.into(), initial_progress),
+ (
+ InitialTaskAttachment::Downloading {
+ ..
+ },
+ InitialLifecycleState::Downloaded,
+ ) => (initial_lifecycle_state.into(), initial_progress),
+ (
+ InitialTaskAttachment::Downloading {
+ active_task,
+ initial_downloaded_bytes,
+ total_bytes,
+ },
+ _,
+ ) => {
+ let Some(destination_lease) = startup_lease.take() else {
+ return Err(DownloadError::Backend(
+ "backend returned an attached task without a startup lease".to_string(),
+ ));
+ };
+ (
+ DownloadActorState::Downloading {
+ active_task,
+ generation: attachment_generation,
+ destination_lease,
+ },
+ ProgressCounters {
+ downloaded_bytes: initial_downloaded_bytes,
+ total_bytes: total_bytes.or(config.expected_bytes).unwrap_or(initial_downloaded_bytes),
+ },
+ )
+ },
+ };
+
+ if let Some(lease) = startup_lease.take() {
+ lease.release().await?;
+ }
+
+ let initial_public_state =
+ project_runtime_public_state(&lifecycle_state, &initial_projection, progress_counters, &config);
+ let (public_state_sender, public_state_receiver) = tokio_watch_channel(initial_public_state);
+
+ let actor = DownloadTaskActor::<B>::new(
+ Arc::clone(&config),
+ Arc::clone(&context),
+ backend_event_sender,
+ generation_counter,
+ lifecycle_state,
+ initial_projection,
+ progress_counters,
+ command_receiver,
+ backend_event_receiver,
+ pending_progress,
+ progress_waker_receiver,
+ public_state_sender,
+ progress_sender.clone(),
+ terminal_sender,
+ );
+ tokio::spawn(actor.run());
+
+ Ok(Self {
+ config,
+ command_sender,
+ public_state_receiver,
+ progress_sender,
+ terminal_receiver,
+ listener_task: Arc::new(TokioMutex::new(None)),
+ is_stopped: AtomicBool::new(false),
+ backend: PhantomData,
+ })
+ }
+
+ async fn send_command(
+ &self,
+ command_builder: impl FnOnce(tokio::sync::oneshot::Sender<Result<(), DownloadError>>) -> TaskCommand,
+ ) -> Result<(), DownloadError> {
+ if self.is_stopped.load(Ordering::SeqCst) {
+ return Err(DownloadError::TaskStopped);
+ }
+ let (reply_sender, reply_receiver) = tokio_oneshot_channel();
+ self.command_sender.send(command_builder(reply_sender)).await.map_err(|_| DownloadError::ChannelClosed)?;
+ reply_receiver.await.unwrap_or(Err(DownloadError::TaskStopped))
+ }
+
+ async fn wait_for_actor_stopped(&self) {
+ let mut terminal_receiver = self.terminal_receiver.clone();
+ loop {
+ if matches!(terminal_receiver.borrow().clone(), TerminalOutcome::ActorStopped) {
+ break;
+ }
+
+ if terminal_receiver.changed().await.is_err() {
+ break;
+ }
+ }
+ }
+}
+
+impl<B: DownloadBackend> fmt::Debug for GenericFileDownloadTask<B> {
+ fn fmt(
+ &self,
+ formatter: &mut fmt::Formatter<'_>,
+ ) -> fmt::Result {
+ formatter
+ .debug_struct("GenericFileDownloadTask")
+ .field("download_id", &self.config.download_id)
+ .field("source_url", &self.config.source_url)
+ .field("destination", &self.config.destination)
+ .finish()
+ }
+}
+
+#[async_trait::async_trait]
+impl<B: DownloadBackend> crate::FileDownloadTask for GenericFileDownloadTask<B> {
+ fn download_id(&self) -> DownloadId {
+ self.config.download_id
+ }
+
+ fn source_url(&self) -> &str {
+ &self.config.source_url
+ }
+
+ fn destination(&self) -> &Path {
+ &self.config.destination
+ }
+
+ fn file_check(&self) -> &FileCheck {
+ &self.config.file_check
+ }
+
+ fn expected_bytes(&self) -> Option<u64> {
+ self.config.expected_bytes
+ }
+
+ async fn download(&self) -> Result<(), DownloadError> {
+ self.send_command(|reply_sender| TaskCommand::Download {
+ reply_sender,
+ })
+ .await
+ }
+
+ async fn pause(&self) -> Result<(), DownloadError> {
+ self.send_command(|reply_sender| TaskCommand::Pause {
+ reply_sender,
+ })
+ .await
+ }
+
+ async fn cancel(&self) -> Result<(), DownloadError> {
+ self.send_command(|reply_sender| TaskCommand::Cancel {
+ reply_sender,
+ })
+ .await
+ }
+
+ async fn state(&self) -> FileDownloadState {
+ self.public_state_receiver.borrow().clone()
+ }
+
+ async fn progress(&self) -> Result<TokioBroadcastStream<FileDownloadState>, DownloadError> {
+ Ok(TokioBroadcastStream::new(self.progress_sender.subscribe()))
+ }
+
+ async fn start_listening(
+ &self,
+ global_broadcast: DownloadEventSender,
+ ) {
+ let mut listener_task = self.listener_task.lock().await;
+ if listener_task.is_some() {
+ return;
+ }
+
+ let download_id = self.config.download_id;
+ let destination = self.config.destination.clone();
+ let mut local_stream = TokioBroadcastStream::new(self.progress_sender.subscribe());
+ *listener_task = Some(tokio::spawn(async move {
+ let mut last_downloaded_bytes = 0u64;
+
+ while let Some(result) = local_stream.next().await {
+ let Ok(state) = result else {
+ continue;
+ };
+
+ match state.phase {
+ FileDownloadPhase::Downloading => {
+ let bytes_written = state.downloaded_bytes.saturating_sub(last_downloaded_bytes);
+ last_downloaded_bytes = state.downloaded_bytes;
+ let event = FileDownloadEvent::ProgressUpdate {
+ bytes_written,
+ total_bytes_written: state.downloaded_bytes,
+ total_bytes_expected: state.total_bytes,
+ };
+ log(DownloadLogEvent::PublicEventEmitted {
+ download_id,
+ event: event.clone(),
+ });
+ let _ = global_broadcast.send((download_id, event));
+ },
+ FileDownloadPhase::Downloaded => {
+ let event = FileDownloadEvent::DownloadCompleted {
+ tmp_path: destination.clone(),
+ final_destination: destination.clone(),
+ };
+ log(DownloadLogEvent::PublicEventEmitted {
+ download_id,
+ event: event.clone(),
+ });
+ let _ = global_broadcast.send((download_id, event));
+ break;
+ },
+ FileDownloadPhase::Error(message) => {
+ let event = FileDownloadEvent::Error {
+ message,
+ };
+ log(DownloadLogEvent::PublicEventEmitted {
+ download_id,
+ event: event.clone(),
+ });
+ let _ = global_broadcast.send((download_id, event));
+ break;
+ },
+ FileDownloadPhase::NotDownloaded
+ | FileDownloadPhase::Paused
+ | FileDownloadPhase::LockedByOther(_) => {},
+ }
+ }
+ }));
+ }
+
+ async fn stop_listening(&self) {
+ if let Some(listener_task) = self.listener_task.lock().await.take() {
+ listener_task.abort();
+ let _ = listener_task.await;
+ }
+ }
+
+ async fn wait(&self) {
+ let mut terminal_receiver = self.terminal_receiver.clone();
+ loop {
+ match terminal_receiver.borrow().clone() {
+ TerminalOutcome::Pending => {},
+ TerminalOutcome::Downloaded | TerminalOutcome::Error(_) | TerminalOutcome::ActorStopped => break,
+ }
+
+ if terminal_receiver.changed().await.is_err() {
+ break;
+ }
+ }
+ }
+
+ fn broadcast_sender(&self) -> TokioBroadcastSender<FileDownloadState> {
+ self.progress_sender.clone()
+ }
+}
+
+#[async_trait::async_trait]
+impl<B: DownloadBackend> ManagedFileDownloadTask for GenericFileDownloadTask<B> {
+ async fn shutdown_for_removal(&self) -> Result<(), DownloadError> {
+ if self.is_stopped.swap(true, Ordering::SeqCst) {
+ self.wait_for_actor_stopped().await;
+ self.stop_listening().await;
+ return Ok(());
+ }
+
+ let (reply_sender, reply_receiver) = tokio_oneshot_channel();
+ if self
+ .command_sender
+ .send(TaskCommand::Remove {
+ reply_sender,
+ })
+ .await
+ .is_err()
+ {
+ self.wait_for_actor_stopped().await;
+ self.stop_listening().await;
+ return Ok(());
+ }
+
+ let result = reply_receiver.await.unwrap_or(Err(DownloadError::TaskStopped));
+ self.wait_for_actor_stopped().await;
+ self.stop_listening().await;
+ result
+ }
+
+ fn is_stopped(&self) -> bool {
+ self.is_stopped.load(Ordering::SeqCst)
+ }
+}
diff --git a/crates/download-manager/src/file_download_task_actor/mod.rs b/crates/download-manager/src/file_download_task_actor/mod.rs
new file mode 100644
index 000000000..534df13e2
--- /dev/null
+++ b/crates/download-manager/src/file_download_task_actor/mod.rs
@@ -0,0 +1,23 @@
+mod actor_state;
+mod backend_event;
+mod backend_progress;
+mod download_task_actor;
+mod generic_file_download_task;
+mod pending_progress_slot;
+mod progress_counters;
+mod public_projection;
+mod public_state_projector;
+mod task_command;
+mod terminal_outcome;
+
+pub(crate) use actor_state::DownloadActorState;
+pub use backend_event::BackendEvent;
+pub use backend_progress::BackendProgress;
+pub use download_task_actor::DownloadTaskActor;
+pub use generic_file_download_task::GenericFileDownloadTask;
+pub use pending_progress_slot::PendingProgressSlot;
+pub use progress_counters::ProgressCounters;
+pub use public_projection::PublicProjection;
+pub use public_state_projector::project_runtime_public_state;
+pub use task_command::TaskCommand;
+pub use terminal_outcome::TerminalOutcome;
diff --git a/crates/download-manager/src/file_download_task_actor/pending_progress_slot.rs b/crates/download-manager/src/file_download_task_actor/pending_progress_slot.rs
new file mode 100644
index 000000000..2267d7c16
--- /dev/null
+++ b/crates/download-manager/src/file_download_task_actor/pending_progress_slot.rs
@@ -0,0 +1,12 @@
+use crate::file_download_task_actor::BackendProgress;
+
+#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
+pub struct PendingProgressSlot {
+ pub progress: Option<BackendProgress>,
+}
+
+impl PendingProgressSlot {
+ pub fn take(&mut self) -> Option<BackendProgress> {
+ self.progress.take()
+ }
+}
diff --git a/crates/download-manager/src/file_download_task_actor/progress_counters.rs b/crates/download-manager/src/file_download_task_actor/progress_counters.rs
new file mode 100644
index 000000000..f762abfac
--- /dev/null
+++ b/crates/download-manager/src/file_download_task_actor/progress_counters.rs
@@ -0,0 +1,7 @@
+use serde::{Deserialize, Serialize};
+
+#[derive(Serialize, Deserialize, Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
+pub struct ProgressCounters {
+ pub downloaded_bytes: u64,
+ pub total_bytes: u64,
+}
diff --git a/crates/download-manager/src/file_download_task_actor/public_projection.rs b/crates/download-manager/src/file_download_task_actor/public_projection.rs
new file mode 100644
index 000000000..464095e79
--- /dev/null
+++ b/crates/download-manager/src/file_download_task_actor/public_projection.rs
@@ -0,0 +1,9 @@
+use serde::{Deserialize, Serialize};
+
+#[derive(Serialize, Deserialize, Clone, Debug, Default, PartialEq, Eq, Hash)]
+pub enum PublicProjection {
+ #[default]
+ None,
+ LockedByOther(String),
+ StickyError(String),
+}
diff --git a/crates/download-manager/src/file_download_task_actor/public_state_projector.rs b/crates/download-manager/src/file_download_task_actor/public_state_projector.rs
new file mode 100644
index 000000000..6c4247e29
--- /dev/null
+++ b/crates/download-manager/src/file_download_task_actor/public_state_projector.rs
@@ -0,0 +1,47 @@
+use crate::{
+ FileDownloadState,
+ file_download_task_actor::{DownloadActorState, ProgressCounters, PublicProjection},
+ traits::{DownloadBackend, DownloadConfig},
+};
+
+pub fn project_runtime_public_state<B: DownloadBackend>(
+ lifecycle_state: &DownloadActorState<B>,
+ projection: &PublicProjection,
+ progress_counters: ProgressCounters,
+ config: &DownloadConfig,
+) -> FileDownloadState {
+ match projection {
+ PublicProjection::StickyError(message) => FileDownloadState::error(message.clone()),
+ PublicProjection::LockedByOther(manager_id) => FileDownloadState::locked_by_other(manager_id.clone()),
+ PublicProjection::None => match lifecycle_state {
+ DownloadActorState::NotDownloaded => FileDownloadState::not_downloaded(config.expected_bytes.unwrap_or(0)),
+ DownloadActorState::Paused {
+ ..
+ } => FileDownloadState::paused(
+ progress_counters.downloaded_bytes,
+ fallback_total_bytes(progress_counters, config.expected_bytes),
+ ),
+ DownloadActorState::Downloaded => {
+ let total_bytes = config.expected_bytes.unwrap_or(progress_counters.total_bytes);
+ FileDownloadState::downloaded(total_bytes)
+ },
+ DownloadActorState::Downloading {
+ ..
+ } => FileDownloadState::downloading(
+ progress_counters.downloaded_bytes,
+ fallback_total_bytes(progress_counters, config.expected_bytes),
+ ),
+ },
+ }
+}
+
+fn fallback_total_bytes(
+ progress_counters: ProgressCounters,
+ expected_bytes: Option<u64>,
+) -> u64 {
+ if progress_counters.total_bytes == 0 {
+ expected_bytes.unwrap_or(0)
+ } else {
+ progress_counters.total_bytes
+ }
+}
diff --git a/crates/download-manager/src/file_download_task_actor/task_command.rs b/crates/download-manager/src/file_download_task_actor/task_command.rs
new file mode 100644
index 000000000..14e232ac1
--- /dev/null
+++ b/crates/download-manager/src/file_download_task_actor/task_command.rs
@@ -0,0 +1,19 @@
+use tokio::sync::oneshot::Sender as TokioOneshotSender;
+
+use crate::DownloadError;
+
+#[derive(Debug)]
+pub enum TaskCommand {
+ Download {
+ reply_sender: TokioOneshotSender<Result<(), DownloadError>>,
+ },
+ Pause {
+ reply_sender: TokioOneshotSender<Result<(), DownloadError>>,
+ },
+ Cancel {
+ reply_sender: TokioOneshotSender<Result<(), DownloadError>>,
+ },
+ Remove {
+ reply_sender: TokioOneshotSender<Result<(), DownloadError>>,
+ },
+}
diff --git a/crates/download-manager/src/file_download_task_actor/terminal_outcome.rs b/crates/download-manager/src/file_download_task_actor/terminal_outcome.rs
new file mode 100644
index 000000000..f1b176f3e
--- /dev/null
+++ b/crates/download-manager/src/file_download_task_actor/terminal_outcome.rs
@@ -0,0 +1,9 @@
+use serde::{Deserialize, Serialize};
+
+#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, Hash)]
+pub enum TerminalOutcome {
+ Pending,
+ Downloaded,
+ Error(String),
+ ActorStopped,
+}
diff --git a/crates/download-manager/src/file_state.rs b/crates/download-manager/src/file_state.rs
index 5f0cd99ff..ac7a2916a 100644
--- a/crates/download-manager/src/file_state.rs
+++ b/crates/download-manager/src/file_state.rs
@@ -1,9 +1,7 @@
-#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+use serde::{Deserialize, Serialize};
+
+#[derive(Serialize, Deserialize, Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum FileState {
Exists,
Missing,
}
-
-pub type DownloadedFileState = FileState;
-pub type CRCFileState = FileState;
-pub type ResumeDataFileState = FileState;
diff --git a/crates/download-manager/src/internal_download_state.rs b/crates/download-manager/src/internal_download_state.rs
deleted file mode 100644
index 98c2e9fed..000000000
--- a/crates/download-manager/src/internal_download_state.rs
+++ /dev/null
@@ -1,76 +0,0 @@
-use crate::{DownloadError, PathBuf};
-
-#[derive(Debug, Clone, Copy, PartialEq, Eq)]
-pub enum StateTransitionAction {
- Download,
- Pause,
- Cancel,
-}
-
-#[derive(Debug)]
-pub enum InternalDownloadState<Task> {
- Downloaded {
- file_path: PathBuf,
- crc_path: Option<PathBuf>,
- },
- Downloading {
- task: Task,
- },
- Paused {
- part_path: PathBuf,
- },
- NotDownloaded,
-}
-
-impl<Task> InternalDownloadState<Task> {
- pub fn can_transition(
- &self,
- action: StateTransitionAction,
- ) -> Result<(), DownloadError> {
- match (self, action) {
- (InternalDownloadState::NotDownloaded, StateTransitionAction::Download) => Ok(()),
- (
- InternalDownloadState::Paused {
- ..
- },
- StateTransitionAction::Download,
- ) => Ok(()),
- (
- InternalDownloadState::Downloading {
- ..
- },
- StateTransitionAction::Download,
- ) => Ok(()),
- (
- InternalDownloadState::Downloaded {
- ..
- },
- StateTransitionAction::Download,
- ) => Ok(()),
-
- (
- InternalDownloadState::Downloading {
- ..
- },
- StateTransitionAction::Pause,
- ) => Ok(()),
- (
- InternalDownloadState::Paused {
- ..
- },
- StateTransitionAction::Pause,
- ) => Ok(()),
- (
- InternalDownloadState::Downloaded {
- ..
- },
- StateTransitionAction::Pause,
- ) => Ok(()),
- (InternalDownloadState::NotDownloaded, StateTransitionAction::Pause) => {
- Err(DownloadError::InvalidStateTransition)
- },
-
- (_, StateTransitionAction::Cancel) => Ok(()),
- }
- }
-}
diff --git a/crates/download-manager/src/lib.rs b/crates/download-manager/src/lib.rs
index 9d8af0e2b..844189b23 100644
--- a/crates/download-manager/src/lib.rs
+++ b/crates/download-manager/src/lib.rs
@@ -2,7 +2,7 @@ mod checked_file_state;
mod crc_utils;
mod download_error;
mod download_info;
-mod download_manager_state;
+mod download_log_event;
mod download_state;
mod file_check;
mod file_download_event;
@@ -11,13 +11,14 @@ mod file_download_phase;
mod file_download_state;
mod file_download_task;
mod file_state;
-mod internal_download_state;
mod lock_file_info;
mod lock_file_state;
mod lock_manager;
-pub mod managers;
-mod prelude;
-mod utils;
+
+pub(crate) mod backends;
+pub(crate) mod file_download_task_actor;
+pub(crate) mod reducer;
+pub(crate) mod traits;
pub use checked_file_state::CheckedFileState;
pub use download_error::DownloadError;
@@ -27,44 +28,16 @@ pub use file_check::FileCheck;
pub use file_download_event::FileDownloadEvent;
pub use file_download_manager::{
DownloadEvent, DownloadEventSender, FileDownloadManager, FileDownloadManagerType, SharedDownloadEventSender,
- create_download_manager,
};
pub use file_download_phase::FileDownloadPhase;
pub use file_download_state::FileDownloadState;
pub use file_download_task::FileDownloadTask;
-pub use file_state::{CRCFileState, DownloadedFileState, FileState, ResumeDataFileState};
-pub use internal_download_state::{InternalDownloadState, StateTransitionAction};
+pub use file_state::FileState;
pub use lock_file_info::LockFileInfo;
pub use lock_file_state::LockFileState;
-pub use lock_manager::{acquire_lock, check_lock_file, release_lock, try_acquire_lock};
-pub use utils::compute_download_id;
+pub use lock_manager::{acquire_lock, check_lock_file, release_lock_if_owned, try_acquire_lock};
-pub type TaskID = usize;
pub type DownloadId = uuid::Uuid;
-
-use std::{
- path::{Path, PathBuf},
- sync::Arc,
-};
-
-// Re-export for internal use (used by lock modules)
-#[allow(unused_imports)]
-pub(crate) use chrono;
-use crc_utils::calculate_and_verify_crc;
-// Import NSBundle on all Apple platforms (needed for manager ID generation)
-#[cfg(target_vendor = "apple")]
-use objc2_foundation::NSBundle;
-#[allow(unused_imports)]
-pub(crate) use serde;
-#[allow(unused_imports)]
-pub(crate) use serde_json;
-use tokio::{
- fs,
- runtime::Handle as TokioHandle,
- sync::{
- Mutex as TokioMutex,
- broadcast::{Sender as TokioBroadcastSender, channel as tokio_broadcast_channel},
- },
- task::JoinHandle as TokioJoinHandle,
-};
-use uuid::Uuid;
+pub fn compute_download_id(destination_path: &std::path::Path) -> DownloadId {
+ uuid::Uuid::new_v5(&uuid::Uuid::NAMESPACE_URL, destination_path.display().to_string().as_bytes())
+}
diff --git a/crates/download-manager/src/lock_file_info.rs b/crates/download-manager/src/lock_file_info.rs
index b09e908df..9b4654085 100644
--- a/crates/download-manager/src/lock_file_info.rs
+++ b/crates/download-manager/src/lock_file_info.rs
@@ -1,8 +1,11 @@
-use crate::chrono;
+use serde::{Deserialize, Serialize};
+use uuid::Uuid;
-#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, PartialEq, Eq)]
+#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, Hash)]
pub struct LockFileInfo {
pub manager_id: String,
+ #[serde(default)]
+ pub instance_id: Uuid,
pub acquired_at: chrono::DateTime<chrono::Utc>,
pub process_id: u32,
}
@@ -10,10 +13,12 @@ pub struct LockFileInfo {
impl LockFileInfo {
pub fn new(
manager_id: String,
+ instance_id: Uuid,
process_id: u32,
) -> Self {
Self {
manager_id,
+ instance_id,
acquired_at: chrono::Utc::now(),
process_id,
}
diff --git a/crates/download-manager/src/lock_file_state.rs b/crates/download-manager/src/lock_file_state.rs
index 969446dff..b3eb6909e 100644
--- a/crates/download-manager/src/lock_file_state.rs
+++ b/crates/download-manager/src/lock_file_state.rs
@@ -1,37 +1,30 @@
+use serde::{Deserialize, Serialize};
+
use crate::LockFileInfo;
-#[derive(Debug, Clone, PartialEq)]
+#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, Hash)]
pub enum LockFileState {
- /// No lock file exists - safe to acquire
Missing,
-
- /// Lock owned by this manager instance (same manager_id + same PID)
OwnedByUs(LockFileInfo),
-
- /// Lock owned by same app but old process (same manager_id + different PID, process dead)
OwnedBySameAppOldProcess(LockFileInfo),
-
- /// Lock owned by another active app (different manager_id, active)
OwnedByOtherApp(LockFileInfo),
-
- /// Lock exists but is stale (different manager_id, timeout expired)
Stale(LockFileInfo),
+ StaleUnparseable(Vec<u8>),
}
impl LockFileState {
- /// Check if we can safely acquire or use this lock
pub fn can_proceed(&self) -> bool {
matches!(
self,
- LockFileState::Missing
- | LockFileState::OwnedByUs(_)
- | LockFileState::OwnedBySameAppOldProcess(_)
- | LockFileState::Stale(_)
+ Self::Missing
+ | Self::OwnedByUs(_)
+ | Self::OwnedBySameAppOldProcess(_)
+ | Self::Stale(_)
+ | Self::StaleUnparseable(_)
)
}
- /// Check if this is a blocking conflict
pub fn is_conflict(&self) -> bool {
- matches!(self, LockFileState::OwnedByOtherApp(_))
+ matches!(self, Self::OwnedByOtherApp(_))
}
}
diff --git a/crates/download-manager/src/lock_manager.rs b/crates/download-manager/src/lock_manager.rs
index fb4b2d9d1..88b4de036 100644
--- a/crates/download-manager/src/lock_manager.rs
+++ b/crates/download-manager/src/lock_manager.rs
@@ -1,171 +1,384 @@
-use std::fs;
+use std::path::{Path, PathBuf};
-use crate::{LockFileInfo, LockFileState, Path, TokioHandle, chrono};
+use tokio::io::AsyncWriteExt;
+use uuid::Uuid;
+
+use crate::{LockFileInfo, LockFileState};
const LOCK_TIMEOUT_MINUTES: i64 = 30;
-/// Check if lock file exists and determine its state
-pub fn check_lock_file(
+#[derive(Debug)]
+pub(crate) struct DestinationLockLease {
+ lock_path: PathBuf,
+ manager_id: String,
+ instance_id: Uuid,
+}
+
+impl DestinationLockLease {
+ pub(crate) async fn acquire_for_destination(
+ destination_path: &Path,
+ manager_id: &str,
+ instance_id: Uuid,
+ ) -> Result<Self, std::io::Error> {
+ Self::acquire(&lock_path_for_destination(destination_path), manager_id, instance_id).await
+ }
+
+ pub(crate) async fn acquire(
+ lock_path: &Path,
+ manager_id: &str,
+ instance_id: Uuid,
+ ) -> Result<Self, std::io::Error> {
+ acquire_lock(lock_path, manager_id, instance_id).await?;
+ Ok(Self {
+ lock_path: lock_path.to_path_buf(),
+ manager_id: manager_id.to_string(),
+ instance_id,
+ })
+ }
+
+ pub(crate) async fn release(self) -> Result<bool, std::io::Error> {
+ release_lock_if_owned(&self.lock_path, &self.manager_id, self.instance_id).await
+ }
+}
+
+pub(crate) fn lock_path_for_destination(destination: &Path) -> PathBuf {
+ PathBuf::from(format!("{}.lock", destination.display()))
+}
+
+pub async fn check_lock_file(
lock_path: &Path,
our_manager_id: &str,
+ our_instance_id: Uuid,
our_process_id: u32,
) -> LockFileState {
- if !lock_path.exists() {
- return LockFileState::Missing;
- }
+ let bytes = match std::fs::read(lock_path) {
+ Ok(bytes) => bytes,
+ Err(error) if error.kind() == std::io::ErrorKind::NotFound => return LockFileState::Missing,
+ Err(_) => return classify_unparseable_lock(lock_path, Vec::new()),
+ };
- let lock_info = match read_lock_file(lock_path) {
- Ok(info) => info,
- Err(e) => {
- tracing::warn!("[LOCK] Failed to read lock file {}: {}. Treating as stale.", lock_path.display(), e);
- // Corrupted lock file - treat as stale with dummy info
- return LockFileState::Stale(LockFileInfo {
- manager_id: "unknown".to_string(),
- acquired_at: chrono::Utc::now(),
- process_id: 0,
- });
- },
+ let lock_info = match serde_json::from_slice::<LockFileInfo>(&bytes) {
+ Ok(lock_info) => lock_info,
+ Err(_) => return classify_unparseable_lock(lock_path, bytes),
};
- // Check if we own this lock (same manager_id + same PID)
if lock_info.manager_id == our_manager_id {
if lock_info.process_id == our_process_id {
- return LockFileState::OwnedByUs(lock_info);
- } else {
- // Same app, different process - check if old process is dead
- if is_process_alive(lock_info.process_id) {
- // Old process still alive - this shouldn't happen in normal operation
- tracing::warn!(
- "[LOCK] Same manager_id but different PID and process is alive: {}",
- lock_info.process_id
- );
- return LockFileState::OwnedByOtherApp(lock_info);
- } else {
- return LockFileState::OwnedBySameAppOldProcess(lock_info);
+ if lock_info.instance_id == our_instance_id {
+ return LockFileState::OwnedByUs(lock_info);
}
+ return LockFileState::OwnedByOtherApp(lock_info);
}
- }
- // Different manager_id - check if stale
- if is_lock_stale(&lock_info) {
- return LockFileState::Stale(lock_info);
- }
+ if is_process_alive(lock_info.process_id).await {
+ return LockFileState::OwnedByOtherApp(lock_info);
+ }
- LockFileState::OwnedByOtherApp(lock_info)
-}
+ return LockFileState::OwnedBySameAppOldProcess(lock_info);
+ }
-/// Read and parse lock file
-fn read_lock_file(lock_path: &Path) -> Result<LockFileInfo, Box<dyn std::error::Error>> {
- let contents = std::fs::read_to_string(lock_path)?;
- let info: LockFileInfo = serde_json::from_str(&contents)?;
- Ok(info)
-}
+ if is_process_alive(lock_info.process_id).await {
+ return LockFileState::OwnedByOtherApp(lock_info);
+ }
-/// Check if a lock is stale based on timeout
-fn is_lock_stale(lock_info: &LockFileInfo) -> bool {
- let elapsed = chrono::Utc::now() - lock_info.acquired_at;
- elapsed > chrono::Duration::minutes(LOCK_TIMEOUT_MINUTES)
+ if is_lock_stale(&lock_info) {
+ LockFileState::Stale(lock_info)
+ } else {
+ LockFileState::OwnedByOtherApp(lock_info)
+ }
}
-/// Check if a process is still alive (Unix-specific)
-#[cfg(unix)]
-fn is_process_alive(pid: u32) -> bool {
- use std::process::{Command, Stdio};
- Command::new("kill")
- .args(["-0", &pid.to_string()])
- .stdin(Stdio::null())
- .stdout(Stdio::null())
- .stderr(Stdio::null())
- .status()
- .map(|s| s.success())
- .unwrap_or(false)
-}
-
-/// Check if a process is still alive (Windows-specific)
-#[cfg(windows)]
-fn is_process_alive(pid: u32) -> bool {
- use std::process::{Command, Stdio};
- Command::new("tasklist")
- .args(["/FI", &format!("PID eq {}", pid)])
- .stdin(Stdio::null())
- .stdout(Stdio::piped())
- .stderr(Stdio::null())
- .output()
- .map(|output| String::from_utf8_lossy(&output.stdout).contains(&pid.to_string()))
- .unwrap_or(false)
-}
-
-/// Acquire lock atomically
pub async fn acquire_lock(
- tokio_handle: &TokioHandle,
lock_path: &Path,
manager_id: &str,
+ instance_id: Uuid,
) -> Result<(), std::io::Error> {
- let lock_info = LockFileInfo::new(manager_id.to_string(), std::process::id());
-
- let json =
- serde_json::to_string_pretty(&lock_info).map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))?;
-
- let lock_path_buf = lock_path.to_path_buf();
- let lock_path_clone = lock_path_buf.clone();
- tokio_handle
- .spawn_blocking(move || {
- // Create parent directory if needed
- if let Some(parent) = lock_path_buf.parent() {
- fs::create_dir_all(parent)?;
- }
+ let lock_info = LockFileInfo::new(manager_id.to_string(), instance_id, std::process::id());
+ let lock_contents = serde_json::to_string_pretty(&lock_info).map_err(std::io::Error::other)?;
- // Atomic write using temp file + rename
- let temp_path = lock_path_buf.with_extension("lock.tmp");
- fs::write(&temp_path, json)?;
- fs::rename(temp_path, &lock_path_buf)?;
- Ok::<(), std::io::Error>(())
- })
- .await
- .map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))??;
+ if let Some(parent) = lock_path.parent() {
+ tokio::fs::create_dir_all(parent).await?;
+ }
- tracing::info!("[LOCK] Acquired lock for {} (manager_id: {})", lock_path_clone.display(), manager_id);
+ match write_new_lock_file(lock_path, lock_contents.as_bytes()).await {
+ Ok(()) => Ok(()),
+ Err(error) if error.kind() == std::io::ErrorKind::AlreadyExists => {
+ match check_lock_file(lock_path, manager_id, instance_id, std::process::id()).await {
+ LockFileState::OwnedByUs(_) => Ok(()),
+ LockFileState::OwnedBySameAppOldProcess(observed) | LockFileState::Stale(observed) => {
+ match reclaim_stale_lock(lock_path, ReclaimExpectation::Matching(observed)).await? {
+ ReclaimOutcome::Reclaimed | ReclaimOutcome::Missing => {
+ Box::pin(acquire_lock(lock_path, manager_id, instance_id)).await
+ },
+ ReclaimOutcome::Changed => Err(std::io::Error::new(
+ std::io::ErrorKind::AlreadyExists,
+ "destination lock changed while reclaiming stale lock",
+ )),
+ }
+ },
+ LockFileState::StaleUnparseable(snapshot) => {
+ match reclaim_stale_lock(lock_path, ReclaimExpectation::UnparseableSnapshot(snapshot)).await? {
+ ReclaimOutcome::Reclaimed | ReclaimOutcome::Missing => {
+ Box::pin(acquire_lock(lock_path, manager_id, instance_id)).await
+ },
+ ReclaimOutcome::Changed => Err(std::io::Error::new(
+ std::io::ErrorKind::AlreadyExists,
+ "destination lock changed while reclaiming stale lock",
+ )),
+ }
+ },
+ LockFileState::OwnedByOtherApp(info) => Err(std::io::Error::new(
+ std::io::ErrorKind::AlreadyExists,
+ format!("destination locked by {}", info.manager_id),
+ )),
+ LockFileState::Missing => Box::pin(acquire_lock(lock_path, manager_id, instance_id)).await,
+ }
+ },
+ Err(error) => Err(error),
+ }
+}
- Ok(())
+async fn write_new_lock_file(
+ lock_path: &Path,
+ lock_contents: &[u8],
+) -> Result<(), std::io::Error> {
+ let temporary_lock_path = lock_path.with_extension(format!("lock-tmp-{}", Uuid::new_v4()));
+ let temporary_result = async {
+ let mut file = tokio::fs::OpenOptions::new().write(true).create_new(true).open(&temporary_lock_path).await?;
+ file.write_all(lock_contents).await?;
+ file.sync_all().await
+ }
+ .await;
+ if let Err(error) = temporary_result {
+ let _ = tokio::fs::remove_file(&temporary_lock_path).await;
+ return Err(error);
+ }
+
+ match tokio::fs::hard_link(&temporary_lock_path, lock_path).await {
+ Ok(()) => {
+ let _ = tokio::fs::remove_file(&temporary_lock_path).await;
+ Ok(())
+ },
+ Err(error) if error.kind() == std::io::ErrorKind::AlreadyExists => {
+ let _ = tokio::fs::remove_file(&temporary_lock_path).await;
+ Err(error)
+ },
+ Err(_) => {
+ let result = write_new_lock_file_direct(lock_path, lock_contents).await;
+ let _ = tokio::fs::remove_file(&temporary_lock_path).await;
+ result
+ },
+ }
}
-/// Release the lock by deleting the lock file
-pub async fn release_lock(
- tokio_handle: &TokioHandle,
+async fn write_new_lock_file_direct(
lock_path: &Path,
+ lock_contents: &[u8],
) -> Result<(), std::io::Error> {
- let lock_path = lock_path.to_path_buf();
- tokio_handle
- .spawn_blocking(move || {
- if lock_path.exists() {
- fs::remove_file(&lock_path)?;
- tracing::info!("[LOCK] Released lock: {}", lock_path.display());
- }
- Ok::<(), std::io::Error>(())
- })
- .await
- .map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))??;
+ let mut file = tokio::fs::OpenOptions::new().write(true).create_new(true).open(lock_path).await?;
+ if let Err(error) = file.write_all(lock_contents).await {
+ let _ = tokio::fs::remove_file(lock_path).await;
+ return Err(error);
+ }
+ if let Err(error) = file.sync_all().await {
+ let _ = tokio::fs::remove_file(lock_path).await;
+ return Err(error);
+ }
Ok(())
}
-/// Try to acquire a lock (non-blocking check first)
-pub async fn try_acquire_lock(
- tokio_handle: &TokioHandle,
+#[derive(Clone, Debug)]
+pub(crate) enum ReclaimExpectation {
+ Matching(LockFileInfo),
+ UnparseableSnapshot(Vec<u8>),
+}
+
+#[derive(Clone, Copy, Debug, PartialEq, Eq)]
+pub(crate) enum ReclaimOutcome {
+ Reclaimed,
+ Missing,
+ Changed,
+}
+
+#[derive(Clone, Copy, Debug, PartialEq, Eq)]
+pub(crate) enum RestoreOutcome {
+ Restored,
+ DestinationAlreadyExists,
+}
+
+pub(crate) async fn reclaim_stale_lock(
+ lock_path: &Path,
+ expectation: ReclaimExpectation,
+) -> Result<ReclaimOutcome, std::io::Error> {
+ let quarantine_path = lock_path.with_extension(format!("reclaim-{}", Uuid::new_v4()));
+ match tokio::fs::rename(lock_path, &quarantine_path).await {
+ Ok(()) => {},
+ Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(ReclaimOutcome::Missing),
+ Err(error) => return Err(error),
+ }
+ let confirmed = match &expectation {
+ ReclaimExpectation::Matching(observed) => {
+ matches!(read_lock_file(&quarantine_path).ok(), Some(actual) if &actual == observed)
+ },
+ ReclaimExpectation::UnparseableSnapshot(snapshot) => {
+ matches!(tokio::fs::read(&quarantine_path).await.ok(), Some(bytes) if &bytes == snapshot)
+ },
+ };
+ if confirmed {
+ let _ = tokio::fs::remove_file(&quarantine_path).await;
+ return Ok(ReclaimOutcome::Reclaimed);
+ }
+ let _ = try_restore_quarantine(&quarantine_path, lock_path).await?;
+ Ok(ReclaimOutcome::Changed)
+}
+
+pub async fn release_lock_if_owned(
lock_path: &Path,
manager_id: &str,
+ instance_id: Uuid,
) -> Result<bool, std::io::Error> {
- let lock_path_buf = lock_path.to_path_buf();
- let manager_id_str = manager_id.to_string();
- let process_id = std::process::id();
+ let observed = match check_lock_file(lock_path, manager_id, instance_id, std::process::id()).await {
+ LockFileState::OwnedByUs(info) => info,
+ _ => return Ok(false),
+ };
+ let quarantine_path = lock_path.with_extension(format!("release-{}", Uuid::new_v4()));
+ match tokio::fs::rename(lock_path, &quarantine_path).await {
+ Ok(()) => {},
+ Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(false),
+ Err(error) => return Err(error),
+ }
+ let parsed = read_lock_file(&quarantine_path).ok();
+ let confirmed = matches!(parsed.as_ref(), Some(actual) if actual == &observed);
+ if confirmed {
+ let _ = tokio::fs::remove_file(&quarantine_path).await;
+ return Ok(true);
+ }
+ let _ = try_restore_quarantine(&quarantine_path, lock_path).await?;
+ Ok(false)
+}
- let lock_state = tokio_handle
- .spawn_blocking(move || check_lock_file(&lock_path_buf, &manager_id_str, process_id))
- .await
- .map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))?;
+pub(crate) async fn try_restore_quarantine(
+ quarantine_path: &Path,
+ lock_path: &Path,
+) -> Result<RestoreOutcome, std::io::Error> {
+ // Use atomic create-or-fail (hard_link, then create_new fallback) so a fresh lock
+ // installed by another owner during our quarantine isn't clobbered by `rename`.
+ match tokio::fs::hard_link(quarantine_path, lock_path).await {
+ Ok(()) => {
+ let _ = tokio::fs::remove_file(quarantine_path).await;
+ return Ok(RestoreOutcome::Restored);
+ },
+ Err(error) if error.kind() == std::io::ErrorKind::AlreadyExists => {
+ let _ = tokio::fs::remove_file(quarantine_path).await;
+ return Ok(RestoreOutcome::DestinationAlreadyExists);
+ },
+ Err(_) => {},
+ }
+
+ let bytes = match tokio::fs::read(quarantine_path).await {
+ Ok(bytes) => bytes,
+ Err(error) => {
+ let _ = tokio::fs::remove_file(quarantine_path).await;
+ return Err(error);
+ },
+ };
+ let mut file = match tokio::fs::OpenOptions::new().write(true).create_new(true).open(lock_path).await {
+ Ok(file) => file,
+ Err(error) if error.kind() == std::io::ErrorKind::AlreadyExists => {
+ let _ = tokio::fs::remove_file(quarantine_path).await;
+ return Ok(RestoreOutcome::DestinationAlreadyExists);
+ },
+ Err(error) => {
+ let _ = tokio::fs::remove_file(quarantine_path).await;
+ return Err(error);
+ },
+ };
+ if let Err(error) = file.write_all(&bytes).await {
+ let _ = tokio::fs::remove_file(lock_path).await;
+ let _ = tokio::fs::remove_file(quarantine_path).await;
+ return Err(error);
+ }
+ if let Err(error) = file.sync_all().await {
+ let _ = tokio::fs::remove_file(lock_path).await;
+ let _ = tokio::fs::remove_file(quarantine_path).await;
+ return Err(error);
+ }
+ let _ = tokio::fs::remove_file(quarantine_path).await;
+ Ok(RestoreOutcome::Restored)
+}
- if lock_state.is_conflict() {
+pub async fn try_acquire_lock(
+ lock_path: &Path,
+ manager_id: &str,
+ instance_id: Uuid,
+) -> Result<bool, std::io::Error> {
+ if check_lock_file(lock_path, manager_id, instance_id, std::process::id()).await.is_conflict() {
return Ok(false);
}
- acquire_lock(tokio_handle, lock_path, manager_id).await?;
+
+ acquire_lock(lock_path, manager_id, instance_id).await?;
Ok(true)
}
+
+fn read_lock_file(lock_path: &Path) -> Result<LockFileInfo, Box<dyn std::error::Error>> {
+ Ok(serde_json::from_str(&std::fs::read_to_string(lock_path)?)?)
+}
+
+fn classify_unparseable_lock(
+ lock_path: &Path,
+ snapshot: Vec<u8>,
+) -> LockFileState {
+ let stale_duration = std::time::Duration::from_secs((LOCK_TIMEOUT_MINUTES * 60) as u64);
+ let mtime = match std::fs::metadata(lock_path).and_then(|metadata| metadata.modified()) {
+ Ok(mtime) => mtime,
+ Err(error) if error.kind() == std::io::ErrorKind::NotFound => return LockFileState::Missing,
+ Err(_) => {
+ return LockFileState::OwnedByOtherApp(LockFileInfo {
+ manager_id: "unknown".to_string(),
+ instance_id: Uuid::nil(),
+ acquired_at: chrono::Utc::now(),
+ process_id: 0,
+ });
+ },
+ };
+ let age = std::time::SystemTime::now().duration_since(mtime).ok();
+ if matches!(age, Some(age) if age >= stale_duration) {
+ LockFileState::StaleUnparseable(snapshot)
+ } else {
+ LockFileState::OwnedByOtherApp(LockFileInfo {
+ manager_id: "unknown".to_string(),
+ instance_id: Uuid::nil(),
+ acquired_at: chrono::Utc::now(),
+ process_id: 0,
+ })
+ }
+}
+
+fn is_lock_stale(lock_info: &LockFileInfo) -> bool {
+ chrono::Utc::now() - lock_info.acquired_at > chrono::Duration::minutes(LOCK_TIMEOUT_MINUTES)
+}
+
+#[cfg(unix)]
+async fn is_process_alive(process_id: u32) -> bool {
+ // SAFETY: `kill(pid, 0)` is a probe with no signal delivery. Direct syscall is required
+ // because sandboxed macOS apps can't exec `/bin/kill`.
+ let result = unsafe { libc::kill(process_id as libc::pid_t, 0) };
+ if result == 0 {
+ return true;
+ }
+ // EPERM = exists but we can't signal it; ESRCH = gone.
+ std::io::Error::last_os_error().raw_os_error() == Some(libc::EPERM)
+}
+
+#[cfg(windows)]
+async fn is_process_alive(process_id: u32) -> bool {
+ tokio::task::spawn_blocking(move || {
+ std::process::Command::new("tasklist")
+ .args(["/FI", &format!("PID eq {process_id}")])
+ .output()
+ .map(|output| String::from_utf8_lossy(&output.stdout).contains(&process_id.to_string()))
+ .unwrap_or(false)
+ })
+ .await
+ .unwrap_or(false)
+}
diff --git a/crates/download-manager/src/managers/apple/file_download_task.rs b/crates/download-manager/src/managers/apple/file_download_task.rs
deleted file mode 100644
index d39c7976e..000000000
--- a/crates/download-manager/src/managers/apple/file_download_task.rs
+++ /dev/null
@@ -1,838 +0,0 @@
-use std::path::PathBuf;
-
-use tokio::{
- sync::{
- Mutex as TokioMutex,
- broadcast::Sender as TokioBroadcastSender,
- watch::{Receiver as TokioWatchReceiver, Sender as TokioWatchSender, channel as tokio_watch_channel},
- },
- time::{Duration as TokioDuration, sleep as tokio_sleep},
-};
-use uuid::Uuid;
-
-use crate::{
- DownloadError, DownloadEventSender, DownloadId, DownloadInfo, FileCheck, FileDownloadEvent, FileDownloadPhase,
- FileDownloadState, FileDownloadTask as FileDownloadTaskTrait, InternalDownloadState, LockFileState,
- StateTransitionAction, check_lock_file,
- lock_manager::{acquire_lock, release_lock},
- managers::apple::{
- URLSessionDownloadTaskResumeData, URLSessionExt, UrlSessionDownloadTaskExt,
- url_session_state_reducer::{
- check_crc_file_exists, check_file_exists, check_resume_file_exists, reconcile_to_internal_state,
- reduce_to_checked_file_state, reduce_to_file_download_state,
- },
- },
- prelude::*,
-};
-
-pub struct FileDownloadTask {
- pub download_id: Uuid,
- pub source_url: String,
- pub destination: PathBuf,
- pub file_check: FileCheck,
- pub manager_id: String,
- expected_bytes: Option<u64>,
- state: Arc<TokioMutex<FileDownloadState>>,
- broadcast_sender: TokioBroadcastSender<FileDownloadState>,
- pub internal_state: TokioMutex<InternalDownloadState<Retained<NSURLSessionDownloadTask>>>,
- session: Option<Retained<NSURLSession>>,
- listener_task: Arc<TokioMutex<Option<TokioJoinHandle<()>>>>,
- tokio_handle: TokioHandle,
- completed_tx: TokioWatchSender<bool>,
- _completed_rx: TokioWatchReceiver<bool>,
-}
-
-impl std::fmt::Debug for FileDownloadTask {
- fn fmt(
- &self,
- f: &mut std::fmt::Formatter<'_>,
- ) -> std::fmt::Result {
- f.debug_struct("FileDownloadTask")
- .field("download_id", &self.download_id)
- .field("source_url", &self.source_url)
- .field("destination", &self.destination)
- .field("file_check", &self.file_check)
- .finish()
- }
-}
-
-impl FileDownloadTask {
- pub fn new(
- download_id: Uuid,
- source_url: String,
- destination: PathBuf,
- file_check: FileCheck,
- manager_id: String,
- expected_bytes: Option<u64>,
- internal_state: InternalDownloadState<Retained<NSURLSessionDownloadTask>>,
- initial_state: FileDownloadState,
- session: Option<Retained<NSURLSession>>,
- tokio_handle: TokioHandle,
- ) -> Self {
- let (broadcast_sender, _receiver) = tokio_broadcast_channel::<FileDownloadState>(64);
- let (completed_tx, completed_rx) = tokio_watch_channel(false);
- Self {
- download_id,
- source_url,
- destination,
- file_check,
- manager_id,
- expected_bytes,
- state: Arc::new(TokioMutex::new(initial_state)),
- broadcast_sender,
- internal_state: TokioMutex::new(internal_state),
- session,
- listener_task: Arc::new(TokioMutex::new(None)),
- tokio_handle,
- completed_tx,
- _completed_rx: completed_rx,
- }
- }
-
- pub async fn wait(&self) {
- let mut rx = self.completed_tx.subscribe();
- if *rx.borrow() {
- return;
- }
- let _ = rx.changed().await;
- }
-
- fn lock_path(&self) -> PathBuf {
- PathBuf::from(format!("{}.lock", self.destination.display()))
- }
-
- fn resume_data_path(&self) -> PathBuf {
- PathBuf::from(format!("{}.resume_data", self.destination.display()))
- }
-
- /// Reconcile lock state based on current internal state
- async fn reconcile_lock_state(&self) {
- let lock_path = self.lock_path();
- let lock_state = check_lock_file(&lock_path, &self.manager_id, std::process::id());
-
- let is_downloading = {
- let guard = self.internal_state.lock().await;
- matches!(*guard, InternalDownloadState::Downloading { .. })
- };
-
- if is_downloading {
- // Ensure we own the lock when downloading
- match lock_state {
- LockFileState::OwnedByUs(_) => {
- // Already owned
- },
- LockFileState::Missing | LockFileState::OwnedBySameAppOldProcess(_) | LockFileState::Stale(_) => {
- if let Err(e) = acquire_lock(&self.tokio_handle, &lock_path, &self.manager_id).await {
- tracing::warn!(
- "[FILE_TASK] Failed to acquire lock during reconciliation {}: {}",
- lock_path.display(),
- e
- );
- } else {
- tracing::info!("[FILE_TASK] Acquired lock during reconciliation: {}", lock_path.display());
- }
- },
- LockFileState::OwnedByOtherApp(info) => {
- tracing::warn!("[FILE_TASK] Another manager owns the lock ({}).", info.manager_id);
- },
- }
- } else {
- // Not downloading → release our lock if we own it or it's safe to clean up
- match lock_state {
- LockFileState::OwnedByUs(_) | LockFileState::OwnedBySameAppOldProcess(_) | LockFileState::Stale(_) => {
- if let Err(e) = release_lock(&self.tokio_handle, &lock_path).await {
- tracing::warn!(
- "[FILE_TASK] Failed to release lock during reconciliation {}: {}",
- lock_path.display(),
- e
- );
- } else {
- tracing::info!("[FILE_TASK] Released lock during reconciliation: {}", lock_path.display());
- }
- },
- LockFileState::Missing | LockFileState::OwnedByOtherApp(_) => {},
- }
- }
- }
-
- pub fn broadcast_sender(&self) -> TokioBroadcastSender<FileDownloadState> {
- self.broadcast_sender.clone()
- }
-
- pub async fn update_state_and_broadcast(
- &self,
- new_state: FileDownloadState,
- ) {
- let mut state_guard = self.state.lock().await;
- let current_bytes = state_guard.downloaded_bytes;
- let new_bytes = new_state.downloaded_bytes;
-
- tracing::debug!(
- "[FILE_TASK] update_state_and_broadcast: id={}, current={:?}({} bytes), new={:?}({} bytes)",
- self.download_id,
- state_guard.phase,
- current_bytes,
- new_state.phase,
- new_bytes
- );
-
- // Guard against backwards progress (stale NSURLSession events)
- // Only accept updates that maintain or increase progress
- if new_bytes < current_bytes
- && !matches!(
- new_state.phase,
- FileDownloadPhase::Downloaded | FileDownloadPhase::Error(_) | FileDownloadPhase::NotDownloaded
- )
- {
- tracing::debug!("[FILE_TASK] ⚠️ Rejected backwards progress: {} < {}", new_bytes, current_bytes);
- return;
- }
-
- // Allow state transitions to proceed normally
- // The guard against backwards progress above is sufficient
-
- *state_guard = new_state.clone();
- drop(state_guard);
-
- let receiver_count = self.broadcast_sender.receiver_count();
-
- if receiver_count == 0 {
- tracing::debug!("[FILE_TASK] No receivers active, skipping broadcast (likely shutdown)");
- return;
- }
-
- tracing::debug!("[FILE_TASK] ✓ State updated and broadcasting to {} receiver(s)", receiver_count);
-
- let _ = self.broadcast_sender.send(new_state.clone());
- }
-
- pub async fn download(&self) -> Result<(), DownloadError> {
- let mut internal_state_guard = self.internal_state.lock().await;
- internal_state_guard.can_transition(StateTransitionAction::Download)?;
-
- tracing::debug!(
- "[FILE_TASK] download() called: id={}, internal_state={:?}",
- self.download_id,
- match &*internal_state_guard {
- InternalDownloadState::NotDownloaded => "NotDownloaded",
- InternalDownloadState::Downloading {
- ..
- } => "Downloading",
- InternalDownloadState::Paused {
- ..
- } => "Paused",
- InternalDownloadState::Downloaded {
- ..
- } => "Downloaded",
- }
- );
-
- match &*internal_state_guard {
- InternalDownloadState::NotDownloaded => {
- tracing::debug!("[FILE_TASK] Creating new download task for: {}", self.source_url);
- let session = self.session.as_ref().ok_or(DownloadError::TaskNotFoundAfterCreation)?;
- let download_task = session.download_task_with_url(&self.source_url)?;
-
- let download_info = match &self.file_check {
- FileCheck::CRC(expected_crc) => DownloadInfo::with_crc(
- self.source_url.clone(),
- self.destination.display().to_string(),
- expected_crc.clone(),
- ),
- FileCheck::None => {
- DownloadInfo::new(self.source_url.clone(), self.destination.display().to_string())
- },
- };
- download_task.set_download_info(&download_info);
- download_task.resume();
-
- *internal_state_guard = InternalDownloadState::Downloading {
- task: download_task.clone(),
- };
- drop(internal_state_guard);
-
- // Ensure invariant: when downloading, no leftover resume_data exists
- let resume_data_path = self.resume_data_path();
- if resume_data_path.exists() {
- let _ = fs::remove_file(&resume_data_path).await;
- tracing::debug!("[FILE_TASK] Removed stale resume_data on start: {}", resume_data_path.display());
- }
-
- let downloading_state = FileDownloadState::downloading(0, 0);
- self.update_state_and_broadcast(downloading_state).await;
- // Ensure lock acquired now that we're downloading
- self.reconcile_lock_state().await;
- Ok(())
- },
- InternalDownloadState::Downloading {
- task: existing_download_task,
- } => {
- tracing::debug!("[FILE_TASK] Already downloading, calling resume on existing task");
- existing_download_task.resume();
- Ok(())
- },
- InternalDownloadState::Paused {
- part_path: saved_resume_path,
- } => {
- tracing::debug!("[FILE_TASK] Resuming from paused state, resume_path={:?}", saved_resume_path);
-
- let resume_path = saved_resume_path.clone();
- let total_bytes_fallback = self.expected_bytes.unwrap_or(0);
-
- async fn reset_to_not_downloaded_and_retry(
- task: &FileDownloadTask,
- resume_path: &PathBuf,
- total_bytes: u64,
- reason: &str,
- ) -> Result<(), DownloadError> {
- tracing::warn!(
- "[FILE_TASK] Resume data invalid ({}). Falling back to fresh download: {}",
- reason,
- resume_path.display()
- );
-
- // Best-effort cleanup: remove the unusable resume file so next launch doesn't
- // keep presenting a Paused state that cannot actually be resumed.
- let _ = fs::remove_file(resume_path).await;
-
- // Reset user-facing state so progress can restart from 0 without being rejected
- // by the backwards-progress guard.
- task.update_state_and_broadcast(FileDownloadState::not_downloaded(total_bytes)).await;
-
- // Ensure any lock is released before starting a fresh task.
- task.reconcile_lock_state().await;
-
- Box::pin(task.download()).await
- }
-
- // Check if resume file actually exists
- if !resume_path.exists() {
- *internal_state_guard = InternalDownloadState::NotDownloaded;
- drop(internal_state_guard);
- return reset_to_not_downloaded_and_retry(
- self,
- &resume_path,
- total_bytes_fallback,
- "resume file missing",
- )
- .await;
- }
-
- // Log resume file size and destination state prior to resuming
- if let Ok(meta) = std::fs::metadata(&resume_path) {
- tracing::debug!("[FILE_TASK] Resume data present: {} bytes", meta.len());
- }
- if let Ok(meta) = std::fs::metadata(&self.destination) {
- tracing::debug!("[FILE_TASK] Destination prior to resume: exists with {} bytes", meta.len());
- } else {
- tracing::debug!("[FILE_TASK] Destination prior to resume: missing");
- }
-
- let session = self.session.as_ref().ok_or(DownloadError::TaskNotFoundAfterCreation)?;
- let resume_data = match URLSessionDownloadTaskResumeData::from_file(&resume_path) {
- Ok(data) => data,
- Err(_e) => {
- *internal_state_guard = InternalDownloadState::NotDownloaded;
- drop(internal_state_guard);
- return reset_to_not_downloaded_and_retry(
- self,
- &resume_path,
- total_bytes_fallback,
- "failed to parse resume file",
- )
- .await;
- },
- };
-
- // If the resume blob points at a CFNetwork temp file that no longer exists
- // (very common after reboot), URLSession resume will fail or restart from 0.
- // Detect that early and restart cleanly to avoid a "stuck" Paused state.
- if let Some(temp_path) = resume_data.temp_file_path() {
- if !temp_path.exists() {
- tracing::warn!("[FILE_TASK] Resume temp file missing: {}", temp_path.display());
- *internal_state_guard = InternalDownloadState::NotDownloaded;
- drop(internal_state_guard);
- return reset_to_not_downloaded_and_retry(
- self,
- &resume_path,
- total_bytes_fallback,
- "CFNetwork temp file missing",
- )
- .await;
- }
- }
-
- let download_task = match session.download_task_with_resume_data(&resume_data) {
- Ok(task) => task,
- Err(_e) => {
- *internal_state_guard = InternalDownloadState::NotDownloaded;
- drop(internal_state_guard);
- return reset_to_not_downloaded_and_retry(
- self,
- &resume_path,
- total_bytes_fallback,
- "failed to create URLSession task from resume data",
- )
- .await;
- },
- };
-
- let download_info = match &self.file_check {
- FileCheck::CRC(expected_crc) => DownloadInfo::with_crc(
- self.source_url.clone(),
- self.destination.display().to_string(),
- expected_crc.clone(),
- ),
- FileCheck::None => {
- DownloadInfo::new(self.source_url.clone(), self.destination.display().to_string())
- },
- };
- download_task.set_download_info(&download_info);
- download_task.resume();
-
- let downloaded_bytes = resume_data.bytes_received.unwrap_or(0);
- let total_bytes = resume_data.bytes_expected_to_receive.unwrap_or(0);
-
- tracing::debug!(
- "[FILE_TASK] Resuming with bytes_received={} of expected={}",
- downloaded_bytes,
- total_bytes
- );
-
- *internal_state_guard = InternalDownloadState::Downloading {
- task: download_task.clone(),
- };
- drop(internal_state_guard);
-
- // Ensure invariant: when downloading, resume_data file is removed
- let resume_data_path = self.resume_data_path();
- if resume_data_path.exists() {
- let _ = fs::remove_file(&resume_data_path).await;
- tracing::debug!("[FILE_TASK] Removed resume_data after resume: {}", resume_data_path.display());
- }
-
- let downloading_state = FileDownloadState::downloading(downloaded_bytes, total_bytes);
- self.update_state_and_broadcast(downloading_state).await;
- // Ensure lock acquired now that we're downloading
- self.reconcile_lock_state().await;
- Ok(())
- },
- InternalDownloadState::Downloaded {
- ..
- } => {
- tracing::debug!("[FILE_TASK] Already downloaded, broadcasting current state");
- let current_state = self.state.lock().await.clone();
- self.update_state_and_broadcast(current_state).await;
- let _ = self.completed_tx.send(true);
- Ok(())
- },
- }
- }
-
- pub async fn pause(&self) -> Result<(), DownloadError> {
- let mut internal_state_guard = self.internal_state.lock().await;
- internal_state_guard.can_transition(StateTransitionAction::Pause)?;
-
- tracing::debug!(
- "[FILE_TASK] pause() called: id={}, internal_state={:?}",
- self.download_id,
- match &*internal_state_guard {
- InternalDownloadState::NotDownloaded => "NotDownloaded",
- InternalDownloadState::Downloading {
- ..
- } => "Downloading",
- InternalDownloadState::Paused {
- ..
- } => "Paused",
- InternalDownloadState::Downloaded {
- ..
- } => "Downloaded",
- }
- );
-
- match &*internal_state_guard {
- InternalDownloadState::Downloading {
- task: download_task,
- } => {
- tracing::debug!("[FILE_TASK] Pausing download task, producing resume data...");
-
- let resume_data = download_task.cancel_by_producing_resume_data().await?;
- let resume_data_path = format!("{}.resume_data", self.destination.display());
- resume_data.save_to_file(&resume_data_path).map_err(|_| DownloadError::ResumeDataError)?;
-
- let downloaded_bytes = resume_data.bytes_received.unwrap_or(0);
- let total_bytes = resume_data.bytes_expected_to_receive.unwrap_or(0);
-
- tracing::debug!("[FILE_TASK] Resume data saved: {} bytes, transitioning to Paused", downloaded_bytes);
-
- *internal_state_guard = InternalDownloadState::Paused {
- part_path: resume_data_path.into(),
- };
- drop(internal_state_guard);
-
- let paused_state = FileDownloadState::paused(downloaded_bytes, total_bytes);
- self.update_state_and_broadcast(paused_state).await;
- // Ensure lock released once paused
- self.reconcile_lock_state().await;
-
- Ok(())
- },
- InternalDownloadState::Downloaded {
- ..
- } => Ok(()),
- _ => Ok(()),
- }
- }
-
- pub async fn cancel(&self) -> Result<(), DownloadError> {
- let mut internal_state_guard = self.internal_state.lock().await;
- internal_state_guard.can_transition(StateTransitionAction::Cancel)?;
-
- tracing::debug!(
- "[FILE_TASK] cancel() called: id={}, internal_state before={:?}",
- self.download_id,
- match &*internal_state_guard {
- InternalDownloadState::NotDownloaded => "NotDownloaded",
- InternalDownloadState::Downloading {
- ..
- } => "Downloading",
- InternalDownloadState::Paused {
- ..
- } => "Paused",
- InternalDownloadState::Downloaded {
- ..
- } => "Downloaded",
- }
- );
-
- let resume_path_to_delete: Option<PathBuf> = match &*internal_state_guard {
- InternalDownloadState::Downloading {
- task: download_task,
- } => {
- download_task.cancel();
- Some(PathBuf::from(format!("{}.resume_data", self.destination.display())))
- },
- InternalDownloadState::Paused {
- part_path: saved_resume_path,
- } => Some(saved_resume_path.clone()),
- _ => None,
- };
- *internal_state_guard = InternalDownloadState::NotDownloaded;
- drop(internal_state_guard);
-
- if let Some(resume_path) = resume_path_to_delete {
- let _ = fs::remove_file(&resume_path).await;
- }
-
- tracing::debug!("[FILE_TASK] cancel() completed: id={}, internal_state now=NotDownloaded", self.download_id);
-
- let not_downloaded_state = FileDownloadState::not_downloaded(0);
- self.update_state_and_broadcast(not_downloaded_state).await;
- // Ensure lock released once canceled
- self.reconcile_lock_state().await;
- Ok(())
- }
-
- pub async fn state(&self) -> FileDownloadState {
- let state = self.state.lock().await.clone();
- state
- }
-
- pub async fn progress(&self) -> Result<TokioBroadcastStream<FileDownloadState>, DownloadError> {
- Ok(TokioBroadcastStream::new(self.broadcast_sender.subscribe()))
- }
-
- /// Handle URLSession download completion event
- /// This encapsulates the reduction and reconciliation logic
- pub async fn handle_download_completion(&self) {
- tracing::debug!(
- "[FILE_TASK] handle_download_completion: id={}, dest={}",
- self.download_id,
- self.destination.display()
- );
-
- // Small delay to ensure filesystem operations complete
- // macOS file system might have slight latency
- self.tokio_handle
- .spawn(async {
- tokio_sleep(TokioDuration::from_millis(10)).await;
- })
- .await
- .ok();
-
- let crc_file_path = PathBuf::from(format!("{}.crc", self.destination.display()));
- let resume_data_path = PathBuf::from(format!("{}.resume_data", self.destination.display()));
-
- // Check file states
- let downloaded_file_state = check_file_exists(&self.destination);
- let crc_file_state = check_crc_file_exists(&crc_file_path);
- let resume_data_state = check_resume_file_exists(&resume_data_path);
-
- tracing::debug!(
- "[FILE_TASK] File states: downloaded={:?}, crc={:?}, resume={:?}",
- downloaded_file_state,
- crc_file_state,
- resume_data_state
- );
- tracing::debug!("[FILE_TASK] Destination file exists (explicit check): {}", self.destination.exists());
- if let Ok(metadata) = std::fs::metadata(&self.destination) {
- tracing::debug!("[FILE_TASK] Destination file size: {} bytes", metadata.len());
- }
-
- let expected_crc = match &self.file_check {
- FileCheck::CRC(crc_hash) => Some(crc_hash.as_str()),
- FileCheck::None => None,
- };
-
- // Reduce to checked file state (validates CRC if needed)
- let checked_file_state =
- reduce_to_checked_file_state(downloaded_file_state, crc_file_state, &self.destination, expected_crc);
-
- tracing::debug!("[FILE_TASK] Checked file state: {:?}", checked_file_state);
-
- // Reconcile to internal state (cleanup, etc.)
- let internal_state = reconcile_to_internal_state(
- checked_file_state,
- resume_data_state,
- None,
- &self.destination,
- &crc_file_path,
- &resume_data_path,
- self.expected_bytes,
- )
- .await;
-
- // Reduce to file download state for display
- let file_download_state = reduce_to_file_download_state(
- checked_file_state,
- resume_data_state,
- None,
- None,
- &self.destination,
- self.expected_bytes,
- &self.manager_id,
- );
-
- tracing::debug!("[FILE_TASK] Final state after completion: {:?}", file_download_state.phase);
-
- // Update internal state
- let mut internal_state_guard = self.internal_state.lock().await;
- *internal_state_guard = internal_state;
- drop(internal_state_guard);
-
- // Broadcast the new state
- self.update_state_and_broadcast(file_download_state).await;
-
- // Reconcile lock after completion (should release)
- self.reconcile_lock_state().await;
- }
-
- /// Handle URLSession progress update event
- pub async fn handle_progress_update(
- &self,
- _bytes_written: u64,
- total_bytes_written: u64,
- total_bytes_expected: u64,
- ) {
- tracing::debug!(
- "[FILE_TASK] handle_progress_update: id={}, progress={}/{} bytes",
- self.download_id,
- total_bytes_written,
- total_bytes_expected
- );
-
- let resume_data_path = self.resume_data_path();
- if resume_data_path.exists() {
- if let Err(e) = fs::remove_file(&resume_data_path).await {
- tracing::warn!(
- "[FILE_TASK] Failed to remove stale resume_data on progress {}: {}",
- resume_data_path.display(),
- e
- );
- } else {
- tracing::debug!("[FILE_TASK] Removed stale resume_data on progress: {}", resume_data_path.display());
- }
- }
-
- let downloading_state = FileDownloadState::downloading(total_bytes_written, total_bytes_expected);
- self.update_state_and_broadcast(downloading_state).await;
- }
-
- /// Handle URLSession error event
- pub async fn handle_error(
- &self,
- error_message: String,
- ) {
- let error_state = FileDownloadState::error(error_message);
- self.update_state_and_broadcast(error_state).await;
- // On errors, ensure lock is released
- self.reconcile_lock_state().await;
- }
-
- /// Start listening to global download events
- pub async fn start_listening(
- &self,
- global_broadcast: DownloadEventSender,
- ) {
- // Check if already listening
- let mut listener_guard = self.listener_task.lock().await;
- if listener_guard.is_some() {
- tracing::debug!("[FILE_TASK] start_listening: Already listening for id={}", self.download_id);
- return;
- }
-
- tracing::debug!("[FILE_TASK] start_listening: Starting listener for id={}", self.download_id);
-
- let download_id = self.download_id;
- let task = Arc::new(self.clone_arc_fields());
- let completed = self.completed_tx.clone();
-
- tracing::debug!("[FILE_TASK] Spawning listener task for download_id={}", download_id);
-
- let handle = self.tokio_handle.spawn(async move {
- tracing::debug!("[FILE_TASK] Listener task started for id={}", download_id);
-
- let mut stream = TokioBroadcastStream::new(global_broadcast.subscribe());
-
- while let Some(result) = stream.next().await {
- if let Ok((id, event)) = result {
- if id == download_id {
- tracing::debug!("[FILE_TASK] Received event for download_id={}: {:?}", download_id, event);
-
- match event {
- FileDownloadEvent::DownloadCompleted {
- final_destination,
- ..
- } => {
- tracing::debug!(
- "[FILE_TASK] Processing DownloadCompleted: id={}, reported_dest={}",
- download_id,
- final_destination.display()
- );
-
- tracing::debug!("[FILE_TASK] Task's destination path: {}", task.destination.display());
-
- tracing::debug!("[FILE_TASK] Paths match: {}", final_destination == task.destination);
-
- // Add a small delay to ensure file system operations are complete
- // The delegate moves files synchronously, but filesystem flush might be async
- let _ = task
- .tokio_handle
- .spawn(async {
- tokio_sleep(TokioDuration::from_millis(100)).await;
- })
- .await;
-
- task.handle_download_completion().await;
- let _ = completed.send(true);
- },
- FileDownloadEvent::ProgressUpdate {
- bytes_written,
- total_bytes_written,
- total_bytes_expected,
- } => {
- task.handle_progress_update(bytes_written, total_bytes_written, total_bytes_expected)
- .await;
- },
- FileDownloadEvent::Error {
- message,
- } => {
- task.handle_error(message).await;
- let _ = completed.send(true);
- },
- }
- }
- }
- }
- });
-
- *listener_guard = Some(handle);
- }
-
- /// Stop listening to global download events
- pub async fn stop_listening(&self) {
- let mut listener_guard = self.listener_task.lock().await;
- if let Some(handle) = listener_guard.take() {
- handle.abort();
- let _ = handle.await;
- }
- }
-
- /// Helper to clone Arc fields for listener task
- fn clone_arc_fields(&self) -> Self {
- Self {
- download_id: self.download_id,
- source_url: self.source_url.clone(),
- destination: self.destination.clone(),
- file_check: self.file_check.clone(),
- manager_id: self.manager_id.clone(),
- expected_bytes: self.expected_bytes,
- state: self.state.clone(),
- broadcast_sender: self.broadcast_sender.clone(),
- internal_state: TokioMutex::new(InternalDownloadState::NotDownloaded),
- session: self.session.clone(),
- listener_task: Arc::new(TokioMutex::new(None)),
- tokio_handle: self.tokio_handle.clone(),
- completed_tx: self.completed_tx.clone(),
- _completed_rx: self.completed_tx.subscribe(),
- }
- }
-}
-
-#[async_trait::async_trait]
-impl FileDownloadTaskTrait for FileDownloadTask {
- fn download_id(&self) -> DownloadId {
- self.download_id
- }
-
- fn source_url(&self) -> &str {
- &self.source_url
- }
-
- fn destination(&self) -> &std::path::Path {
- &self.destination
- }
-
- fn file_check(&self) -> &FileCheck {
- &self.file_check
- }
-
- fn broadcast_sender(&self) -> TokioBroadcastSender<FileDownloadState> {
- self.broadcast_sender()
- }
-
- async fn download(&self) -> Result<(), DownloadError> {
- self.download().await
- }
-
- async fn pause(&self) -> Result<(), DownloadError> {
- self.pause().await
- }
-
- async fn cancel(&self) -> Result<(), DownloadError> {
- self.cancel().await
- }
-
- async fn state(&self) -> FileDownloadState {
- self.state().await
- }
-
- async fn progress(&self) -> Result<TokioBroadcastStream<FileDownloadState>, DownloadError> {
- self.progress().await
- }
-
- async fn start_listening(
- &self,
- global_broadcast: DownloadEventSender,
- ) {
- self.start_listening(global_broadcast).await
- }
-
- async fn stop_listening(&self) {
- self.stop_listening().await
- }
-
- async fn wait(&self) {
- self.wait().await
- }
-}
diff --git a/crates/download-manager/src/managers/apple/mod.rs b/crates/download-manager/src/managers/apple/mod.rs
deleted file mode 100644
index 59b46facb..000000000
--- a/crates/download-manager/src/managers/apple/mod.rs
+++ /dev/null
@@ -1,28 +0,0 @@
-mod file_download_task;
-mod session_config;
-mod url_session_delegate;
-mod url_session_download_manager;
-mod url_session_download_task_ext;
-mod url_session_download_task_resume_data;
-mod url_session_error;
-mod url_session_ext;
-mod url_session_get_tasks_handler;
-mod url_session_resume_data_handler;
-mod url_session_state_reducer;
-mod url_session_task_ext;
-
-pub use file_download_task::FileDownloadTask;
-pub use session_config::SessionConfig;
-pub use url_session_delegate::URLSessionDelegate;
-pub use url_session_download_manager::{URLSessionDownloadManager, URLSessionDropPolicy};
-pub use url_session_download_task_resume_data::URLSessionDownloadTaskResumeData;
-pub use url_session_error::URLSessionError;
-pub use url_session_get_tasks_handler::URLSessionGetTasksCompletionHandler;
-pub use url_session_resume_data_handler::URLSessionResumeDataHandler;
-
-use crate::prelude::*;
-
-pub type URLSessionDownloadTaskState = Option<NSURLSessionTaskState>;
-
-pub use url_session_download_task_ext::UrlSessionDownloadTaskExt;
-pub use url_session_ext::URLSessionExt;
diff --git a/crates/download-manager/src/managers/apple/session_config.rs b/crates/download-manager/src/managers/apple/session_config.rs
deleted file mode 100644
index 0adb2f640..000000000
--- a/crates/download-manager/src/managers/apple/session_config.rs
+++ /dev/null
@@ -1,62 +0,0 @@
-use crate::prelude::*;
-
-#[allow(unused)]
-#[derive(Debug, Clone)]
-pub enum BackgroundSessionID {
- Default,
- Custom(String),
-}
-
-#[allow(unused)]
-#[derive(Debug, Clone)]
-pub enum SessionConfig {
- Foreground,
- Background(BackgroundSessionID),
- /// Background in apps, Foreground in CLIs
- Automatic,
-}
-
-impl Default for SessionConfig {
- fn default() -> Self {
- Self::Automatic
- }
-}
-
-impl SessionConfig {
- pub fn ns_url_session_configuration(&self) -> Retained<NSURLSessionConfiguration> {
- let bundle_id = NSBundle::mainBundle().bundleIdentifier().unwrap_or_default().to_string();
-
- let create_ephemeral_config = || NSURLSessionConfiguration::ephemeralSessionConfiguration();
-
- let create_background_config = |background_session_id: &BackgroundSessionID| {
- let session_id = NSString::from_str(&match background_session_id {
- BackgroundSessionID::Default => {
- if bundle_id.is_empty() {
- "trymirai.download-manager".to_string()
- } else {
- format!("{}.trymirai.download-manager", bundle_id)
- }
- },
- BackgroundSessionID::Custom(id) => id.clone(),
- });
-
- let config = NSURLSessionConfiguration::backgroundSessionConfigurationWithIdentifier(&session_id);
- config.setSessionSendsLaunchEvents(true);
- config.setDiscretionary(false);
- config.setWaitsForConnectivity(true);
- config
- };
-
- match self {
- SessionConfig::Foreground => create_ephemeral_config(),
- SessionConfig::Background(background_session_id) => create_background_config(background_session_id),
- SessionConfig::Automatic => {
- if bundle_id.is_empty() {
- create_ephemeral_config()
- } else {
- create_background_config(&BackgroundSessionID::Default)
- }
- },
- }
- }
-}
diff --git a/crates/download-manager/src/managers/apple/url_session_delegate.rs b/crates/download-manager/src/managers/apple/url_session_delegate.rs
deleted file mode 100644
index 2b1e12244..000000000
--- a/crates/download-manager/src/managers/apple/url_session_delegate.rs
+++ /dev/null
@@ -1,199 +0,0 @@
-use std::path::PathBuf;
-
-use crate::{
- FileDownloadEvent, SharedDownloadEventSender,
- managers::apple::{URLSessionError, UrlSessionDownloadTaskExt},
- prelude::*,
-};
-
-#[derive(Debug, Clone)]
-pub struct URLSessionDelegateIvars {
- pub global_broadcast_sender: SharedDownloadEventSender,
-}
-
-define_class!(
- #[unsafe(super(NSObject))]
- #[derive(Debug)]
- #[ivars = URLSessionDelegateIvars]
- pub struct URLSessionDelegate;
-
- unsafe impl NSObjectProtocol for URLSessionDelegate {}
-
- unsafe impl NSURLSessionDelegate for URLSessionDelegate {
- #[unsafe(method(URLSession:didBecomeInvalidWithError:))]
- fn did_become_invalid_with_error(
- &self,
- _session: &NSURLSession,
- _error: Option<&NSError>,
- ) {
- }
-
- #[unsafe(method(URLSessionDidFinishEventsForBackgroundURLSession:))]
- fn did_finish_events_for_background_url_session(
- &self,
- _session: &NSURLSession,
- ) {
- }
- }
-
- unsafe impl NSURLSessionTaskDelegate for URLSessionDelegate {
- #[unsafe(method(URLSession:task:didCompleteWithError:))]
- fn did_complete_with_error(
- &self,
- _session: &NSURLSession,
- task: &NSURLSessionTask,
- error: Option<&NSError>,
- ) {
- if let Some(download_task) = task.downcast_ref::<NSURLSessionDownloadTask>() {
- if let Some(err) = error {
- let parsed_error = URLSessionError::from_nserror(err);
-
- if parsed_error.should_ignore() {
- return;
- }
-
- if let Some(download_id) = download_task.download_id() {
- let error_message = parsed_error.user_message();
- let event = FileDownloadEvent::Error {
- message: error_message,
- };
- let _ = Self::ivars(self).global_broadcast_sender.send((download_id, event));
- }
- }
- }
- }
- }
-
- unsafe impl NSURLSessionDownloadDelegate for URLSessionDelegate {
- #[unsafe(method(URLSession:downloadTask:didFinishDownloadingToURL:))]
- fn did_finish_downloading_to_url(
- &self,
- _session: &NSURLSession,
- download_task: &NSURLSessionDownloadTask,
- location: &NSURL,
- ) {
- tracing::debug!("[DELEGATE] didFinishDownloadingToURL called");
-
- let tmp_path = PathBuf::from(location.path().unwrap().to_string());
- if let Some(download_info) = download_task.download_info() {
- let final_destination = PathBuf::from(&download_info.destination_path);
- if let Some(parent_dir) = final_destination.parent() {
- let _ = fs::create_dir_all(parent_dir);
-
- tracing::debug!("[DELEGATE] Moving file from temp to final destination...");
-
- let move_successful = match std::fs::rename(&tmp_path, &final_destination) {
- Ok(_) => true,
- Err(_) => match std::fs::copy(&tmp_path, &final_destination) {
- Ok(_) => {
- let _ = fs::remove_file(&tmp_path);
- true
- },
- Err(_) => false,
- },
- };
-
- if !move_successful {
- tracing::debug!("[DELEGATE] ✗ File move failed");
-
- if let Some(download_id) = download_task.download_id() {
- let event = FileDownloadEvent::Error {
- message: "move into destination failed".to_string(),
- };
- let _ = Self::ivars(self).global_broadcast_sender.send((download_id, event));
- }
- return;
- }
-
- tracing::debug!("[DELEGATE] ✓ File moved successfully");
-
- if let Some(download_id) = download_task.download_id() {
- let event = FileDownloadEvent::DownloadCompleted {
- tmp_path: tmp_path.clone(),
- final_destination: final_destination.clone(),
- };
-
- tracing::debug!("[DELEGATE] Broadcasting download completion event");
-
- let _ = Self::ivars(self).global_broadcast_sender.send((download_id, event));
-
- tracing::debug!("[DELEGATE] ✓ didFinishDownloadingToURL complete");
- }
- }
- }
- }
-
- #[unsafe(method(URLSession:downloadTask:didWriteData:totalBytesWritten:totalBytesExpectedToWrite:))]
- fn did_write_data(
- &self,
- _session: &NSURLSession,
- download_task: &NSURLSessionDownloadTask,
- bytes_written_since_last_callback: i64,
- cumulative_bytes_written: i64,
- total_expected_bytes_to_write: i64,
- ) {
- // Clean up resume_data file on first progress event
- // This confirms NSURLSession has processed the resume_data
- if let Some(info) = download_task.download_info() {
- let dest = PathBuf::from(&info.destination_path);
- let resume_path = format!("{}.resume_data", dest.display());
- if PathBuf::from(&resume_path).exists() {
- let _ = fs::remove_file(&resume_path);
- }
- }
-
- if let Some(download_id) = download_task.download_id() {
- let bytes_written = if bytes_written_since_last_callback > 0 {
- bytes_written_since_last_callback as u64
- } else {
- 0
- };
- let total_bytes_written = if cumulative_bytes_written > 0 {
- cumulative_bytes_written as u64
- } else {
- 0
- };
- let total_bytes_expected = if total_expected_bytes_to_write > 0 {
- total_expected_bytes_to_write as u64
- } else {
- 0
- };
-
- let event = FileDownloadEvent::ProgressUpdate {
- bytes_written,
- total_bytes_written,
- total_bytes_expected,
- };
-
- tracing::debug!(
- "[DELEGATE] Progress: download_id={}, written={}, total={}/{} bytes",
- download_id,
- bytes_written,
- total_bytes_written,
- total_bytes_expected
- );
-
- let send_result = Self::ivars(self).global_broadcast_sender.send((download_id, event));
-
- if let Err(e) = send_result {
- tracing::debug!("[DELEGATE] ⚠️ Failed to send progress event: {:?}", e);
- }
- }
- }
- }
-);
-
-impl URLSessionDelegate {
- pub fn new(global_broadcast_sender: SharedDownloadEventSender) -> Retained<Self> {
- unsafe {
- let class = Self::class();
- let allocated: Allocated<Self> = msg_send![class, alloc];
- let allocated_with_ivars = allocated.set_ivars(URLSessionDelegateIvars {
- global_broadcast_sender,
- });
- let initialized: Retained<Self> = msg_send![super(allocated_with_ivars), init];
-
- initialized
- }
- }
-}
diff --git a/crates/download-manager/src/managers/apple/url_session_download_manager.rs b/crates/download-manager/src/managers/apple/url_session_download_manager.rs
deleted file mode 100644
index 62e257dea..000000000
--- a/crates/download-manager/src/managers/apple/url_session_download_manager.rs
+++ /dev/null
@@ -1,303 +0,0 @@
-use std::path::{Path, PathBuf};
-
-use crate::{
- DownloadError, DownloadEvent, FileCheck, FileDownloadManager, FileDownloadTask as FileDownloadTaskTrait,
- SharedDownloadEventSender, compute_download_id,
- download_manager_state::DownloadManagerState,
- managers::apple::{
- FileDownloadTask, SessionConfig, URLSessionDelegate, URLSessionExt, UrlSessionDownloadTaskExt,
- url_session_state_reducer::{
- check_crc_file_exists, check_file_exists, check_resume_file_exists, reconcile_to_internal_state,
- reduce_to_checked_file_state, reduce_to_file_download_state,
- },
- },
- prelude::*,
-};
-
-#[allow(unused)]
-#[derive(Debug, Clone)]
-pub enum URLSessionDropPolicy {
- FinishTasksAndInvalidate,
- InvalidateAndCancel,
-}
-
-#[derive(Debug, Clone)]
-#[allow(dead_code)]
-pub struct URLSessionDownloadManager {
- state: DownloadManagerState,
- session: Retained<NSURLSession>,
- delegate: Retained<URLSessionDelegate>,
- delegate_protocol_object: Retained<ProtocolObject<dyn NSURLSessionDelegate>>,
- drop_policy: URLSessionDropPolicy,
-}
-
-unsafe impl Send for URLSessionDownloadManager {}
-unsafe impl Sync for URLSessionDownloadManager {}
-
-impl URLSessionDownloadManager {
- #[allow(unused)]
- pub fn manager_id(&self) -> &str {
- &self.state.manager_id
- }
-
- #[allow(unused)]
- pub async fn new(
- session_config: SessionConfig,
- drop_policy: URLSessionDropPolicy,
- tokio_handle: TokioHandle,
- ) -> Result<Self, DownloadError> {
- let state = DownloadManagerState::new("apple", tokio_handle);
-
- let backend = autoreleasepool(|_| unsafe {
- let delegate = URLSessionDelegate::new(state.global_broadcast_sender.clone());
-
- let delegate_protocol_object = ProtocolObject::<dyn NSURLSessionDelegate>::from_retained(delegate.clone());
-
- let session = NSURLSession::sessionWithConfiguration_delegate_delegateQueue(
- &session_config.ns_url_session_configuration(),
- Some(&delegate_protocol_object),
- None,
- );
-
- Self {
- state,
- session,
- delegate: delegate.clone(),
- delegate_protocol_object,
- drop_policy,
- }
- });
-
- tracing::debug!("[DOWNLOAD_MANAGER] URLSessionDownloadManager created with tokio handle");
-
- backend.initialize_task_cache().await?;
-
- tracing::debug!("[DOWNLOAD_MANAGER] Task cache initialized, ready for downloads");
-
- Ok(backend)
- }
-
- /// Invalidate the underlying NSURLSession and cancel all tasks.
- /// Safe to call multiple times; used by tests to ensure cleanup on exit.
- fn invalidate_and_cancel(&self) {
- autoreleasepool(|_| {
- self.session.invalidateAndCancel();
- });
- }
-
- /// Finish all outstanding tasks and invalidate the session.
- /// For background sessions, tasks may continue in the background.
- fn finish_tasks_and_invalidate(&self) {
- autoreleasepool(|_| {
- self.session.finishTasksAndInvalidate();
- });
- }
-
- async fn initialize_task_cache(&self) -> Result<(), DownloadError> {
- let tasks = self.session.get_tasks().await;
-
- for download_task in tasks.download_tasks {
- if let Some(download_info) = download_task.download_info() {
- let destination = PathBuf::from(&download_info.destination_path);
- let download_id = compute_download_id(&download_info.source_url, &destination);
-
- let downloaded_file_state = check_file_exists(&destination);
- let crc_file_path = PathBuf::from(format!("{}.crc", destination.display()));
- let crc_file_state = check_crc_file_exists(&crc_file_path);
- let resume_data_path = PathBuf::from(format!("{}.resume_data", destination.display()));
- let resume_data_state = check_resume_file_exists(&resume_data_path);
- let url_session_task_state = Some(download_task.state());
-
- let checked_file_state = reduce_to_checked_file_state(
- downloaded_file_state,
- crc_file_state,
- &destination,
- download_info.crc32c.as_deref(),
- );
-
- // Extract expected bytes from download task
- let expected_bytes = {
- let count = download_task.count_of_bytes_expected_to_receive();
- if count > 0 {
- Some(count)
- } else {
- None
- }
- };
-
- let internal_state = reconcile_to_internal_state(
- checked_file_state,
- resume_data_state,
- Some(&download_task),
- &destination,
- &crc_file_path,
- &resume_data_path,
- expected_bytes,
- )
- .await;
-
- let file_download_state = reduce_to_file_download_state(
- checked_file_state,
- resume_data_state,
- url_session_task_state,
- Some(&download_task),
- &destination,
- expected_bytes,
- &self.state.manager_id,
- );
-
- let file_check = download_info.crc32c.map_or(FileCheck::None, |crc_hash| FileCheck::CRC(crc_hash));
-
- let file_task = Arc::new(FileDownloadTask::new(
- download_id,
- download_info.source_url.clone(),
- destination.clone(),
- file_check,
- self.state.manager_id.clone(),
- expected_bytes,
- internal_state,
- file_download_state,
- Some(self.session.clone()),
- self.state.tokio_handle.clone(),
- ));
-
- file_task.start_listening((*self.state.global_broadcast_sender).clone()).await;
-
- // Special case: If task is Completed and file exists with valid CRC,
- // the delegate callback was missed (app was closed during completion).
- // Manually trigger completion handling to validate and cache CRC.
- if matches!(download_task.state(), NSURLSessionTaskState::Completed)
- && matches!(checked_file_state, crate::CheckedFileState::Valid)
- {
- tracing::info!(
- "[DOWNLOAD_MANAGER] Completed task with valid file detected during init, triggering completion handling for: {}",
- destination.display()
- );
- file_task.handle_download_completion().await;
- }
-
- self.state.task_cache.lock().await.insert(download_id, file_task);
- } else {
- download_task.cancel();
- }
- }
-
- Ok(())
- }
-}
-
-impl Drop for URLSessionDownloadManager {
- fn drop(&mut self) {
- match self.drop_policy {
- URLSessionDropPolicy::FinishTasksAndInvalidate => {
- self.finish_tasks_and_invalidate();
- },
- URLSessionDropPolicy::InvalidateAndCancel => {
- self.invalidate_and_cancel();
- },
- }
- }
-}
-
-#[async_trait::async_trait]
-impl FileDownloadManager for URLSessionDownloadManager {
- fn manager_id(&self) -> &str {
- &self.state.manager_id
- }
-
- fn subscribe_to_all_downloads(&self) -> TokioBroadcastStream<DownloadEvent> {
- self.state.subscribe_to_all_downloads()
- }
-
- fn global_broadcast_sender(&self) -> SharedDownloadEventSender {
- self.state.global_broadcast_sender.clone()
- }
-
- async fn get_all_file_tasks(&self) -> Result<Vec<Arc<dyn FileDownloadTaskTrait>>, DownloadError> {
- self.state.get_all_file_tasks().await
- }
-
- async fn file_download_task(
- &self,
- source_url: &String,
- destination_path: &Path,
- file_check: FileCheck,
- expected_bytes: Option<u64>,
- ) -> Result<Arc<dyn FileDownloadTaskTrait>, DownloadError> {
- use crate::check_lock_file;
-
- let download_id = compute_download_id(source_url, destination_path);
-
- {
- let task_cache_guard = self.state.task_cache.lock().await;
- if let Some(cached_task) = task_cache_guard.get(&download_id) {
- return Ok(cached_task.clone());
- }
- }
-
- // Check lock file before proceeding (result will be used by reduction only)
- let lock_path = PathBuf::from(format!("{}.lock", destination_path.display()));
- let _ = check_lock_file(&lock_path, &self.state.manager_id, std::process::id());
-
- // If file is locked by another manager, do not error here.
- // Reduction will expose LockedByOther in the FileDownloadState.
-
- let downloaded_file_state = check_file_exists(destination_path);
- let crc_file_path = PathBuf::from(format!("{}.crc", destination_path.display()));
- let crc_file_state = check_crc_file_exists(&crc_file_path);
- let resume_data_path = PathBuf::from(format!("{}.resume_data", destination_path.display()));
- let resume_data_state = check_resume_file_exists(&resume_data_path);
-
- let expected_crc = match &file_check {
- FileCheck::CRC(crc_hash) => Some(crc_hash.as_str()),
- FileCheck::None => None,
- };
-
- let checked_file_state =
- reduce_to_checked_file_state(downloaded_file_state, crc_file_state, destination_path, expected_crc);
-
- let internal_state = reconcile_to_internal_state(
- checked_file_state,
- resume_data_state,
- None,
- destination_path,
- &crc_file_path,
- &resume_data_path,
- expected_bytes,
- )
- .await;
-
- let file_download_state = reduce_to_file_download_state(
- checked_file_state,
- resume_data_state,
- None,
- None,
- destination_path,
- expected_bytes,
- &self.state.manager_id,
- );
-
- // Do not acquire lock here. Lock acquisition/release is handled during
- // reconciliation when entering/leaving the Downloading state.
-
- let file_task = Arc::new(FileDownloadTask::new(
- download_id,
- source_url.clone(),
- destination_path.to_path_buf(),
- file_check,
- self.state.manager_id.clone(),
- expected_bytes,
- internal_state,
- file_download_state,
- Some(self.session.clone()),
- self.state.tokio_handle.clone(),
- ));
-
- // Start listening to global broadcast events
- tracing::debug!("[MANAGER] Starting listener for download_id={}", download_id);
- file_task.start_listening((*self.state.global_broadcast_sender).clone()).await;
-
- self.state.task_cache.lock().await.insert(download_id, file_task.clone());
- Ok(file_task)
- }
-}
diff --git a/crates/download-manager/src/managers/apple/url_session_download_task_ext.rs b/crates/download-manager/src/managers/apple/url_session_download_task_ext.rs
deleted file mode 100644
index 1630bb74f..000000000
--- a/crates/download-manager/src/managers/apple/url_session_download_task_ext.rs
+++ /dev/null
@@ -1,115 +0,0 @@
-use std::path::PathBuf;
-
-use async_trait::async_trait;
-
-use crate::{
- DownloadId, DownloadInfo, TaskID, compute_download_id,
- managers::apple::{URLSessionDownloadTaskResumeData, URLSessionResumeDataHandler},
- prelude::*,
-};
-
-#[allow(unused)]
-#[async_trait]
-pub trait UrlSessionDownloadTaskExt {
- fn task_identifier(&self) -> TaskID;
- fn task_description(&self) -> Option<String>;
- fn set_task_description(
- &self,
- s: &str,
- );
- fn download_info(&self) -> Option<DownloadInfo>;
- fn set_download_info(
- &self,
- info: &DownloadInfo,
- );
- fn download_id(&self) -> Option<DownloadId>;
- fn state(&self) -> NSURLSessionTaskState;
- fn count_of_bytes_expected_to_receive(&self) -> u64;
- fn count_of_bytes_received(&self) -> u64;
- async fn cancel_by_producing_resume_data(&self) -> Result<URLSessionDownloadTaskResumeData, crate::DownloadError>;
-}
-
-#[async_trait]
-impl UrlSessionDownloadTaskExt for NSURLSessionDownloadTask {
- fn task_identifier(&self) -> TaskID {
- unsafe { msg_send![self, taskIdentifier] }
- }
-
- fn task_description(&self) -> Option<String> {
- let desc_opt = self.taskDescription();
- desc_opt.map(|d| d.to_string())
- }
-
- fn set_task_description(
- &self,
- s: &str,
- ) {
- let ns = NSString::from_str(s);
- self.setTaskDescription(Some(&ns));
- }
-
- fn download_info(&self) -> Option<DownloadInfo> {
- self.task_description().and_then(|desc| serde_json::from_str::<crate::DownloadInfo>(&desc).ok())
- }
-
- fn set_download_info(
- &self,
- info: &DownloadInfo,
- ) {
- let ns = NSString::from_str(&serde_json::to_string(info).unwrap());
- self.setTaskDescription(Some(&ns));
- }
-
- fn download_id(&self) -> Option<DownloadId> {
- self.download_info().and_then(|info| {
- Some(compute_download_id(&info.source_url, PathBuf::from(info.destination_path).as_path()))
- })
- }
-
- fn state(&self) -> NSURLSessionTaskState {
- unsafe { msg_send![self, state] }
- }
-
- fn count_of_bytes_expected_to_receive(&self) -> u64 {
- let bytes_expected: i64 = unsafe { msg_send![self, countOfBytesExpectedToReceive] };
- if bytes_expected > 0 {
- bytes_expected as u64
- } else {
- 0
- }
- }
-
- fn count_of_bytes_received(&self) -> u64 {
- let bytes_received: i64 = unsafe { msg_send![self, countOfBytesReceived] };
- if bytes_received > 0 {
- bytes_received as u64
- } else {
- 0
- }
- }
-
- async fn cancel_by_producing_resume_data(&self) -> Result<URLSessionDownloadTaskResumeData, crate::DownloadError> {
- let (sender, receiver) = tokio_oneshot_channel::<Box<[u8]>>();
- let sender_cell: Arc<Mutex<Option<TokioOneshotSender<Box<[u8]>>>>> = Arc::new(Mutex::new(Some(sender)));
- {
- let sender_cell = Arc::clone(&sender_cell);
- let handler = URLSessionResumeDataHandler::new_bytes(move |bytes| {
- if let Some(s) = sender_cell.lock().unwrap().take() {
- let _ = s.send(bytes);
- }
- });
- unsafe {
- self.cancelByProducingResumeData(&handler);
- }
- }
- let resume_data_bytes = receiver.await.map_err(|_| crate::DownloadError::ResumeDataError)?;
-
- let mut resume_data = URLSessionDownloadTaskResumeData::from_bytes(&resume_data_bytes)
- .map_err(|_| crate::DownloadError::ResumeDataError)?;
-
- resume_data.bytes_received = Some(self.count_of_bytes_received());
- resume_data.bytes_expected_to_receive = Some(self.count_of_bytes_expected_to_receive());
-
- Ok(resume_data)
- }
-}
diff --git a/crates/download-manager/src/managers/apple/url_session_download_task_resume_data.rs b/crates/download-manager/src/managers/apple/url_session_download_task_resume_data.rs
deleted file mode 100644
index eed6effbf..000000000
--- a/crates/download-manager/src/managers/apple/url_session_download_task_resume_data.rs
+++ /dev/null
@@ -1,310 +0,0 @@
-use std::path::{Path, PathBuf};
-
-use thiserror::Error;
-
-use crate::prelude::*;
-
-#[derive(Debug, Clone, Error)]
-pub enum ResumeDataError {
- #[error("I/O error: {0}")]
- IoError(String),
-
- #[error("Failed to parse resume data: {0}")]
- ParseError(String),
-
- #[error("Decoded object is not a dictionary")]
- NotADictionary,
-
- #[error("Failed to serialize resume data")]
- SerializationFailed,
-}
-
-impl From<Retained<NSError>> for ResumeDataError {
- fn from(error: Retained<NSError>) -> Self {
- ResumeDataError::ParseError(error.localizedDescription().to_string())
- }
-}
-
-/// Parsed resume data from NSURLSessionDownloadTask
-///
-/// This struct allows reading resume data to extract download progress information.
-#[derive(Debug, Clone)]
-pub struct URLSessionDownloadTaskResumeData {
- /// Number of bytes received so far
- pub bytes_received: Option<u64>,
- /// Expected total bytes to receive
- pub bytes_expected_to_receive: Option<u64>,
- /// Local path where resume data was originally stored (iOS < 10.2)
- pub local_path: Option<PathBuf>,
- /// Temporary file name in the system temp directory
- pub temp_file_name: Option<String>,
- /// Byte range for resume (as raw object, needs further parsing if needed)
- pub byte_range: Option<String>,
- /// Original request data (NSKeyedArchive encoded NSURLRequest)
- pub original_request: Option<Vec<u8>>,
- /// Current request data (NSKeyedArchive encoded NSURLRequest)
- pub current_request: Option<Vec<u8>>,
- /// Resume data version
- pub version: Option<u64>,
-}
-
-impl URLSessionDownloadTaskResumeData {
- /// Parse resume data from a file path
- pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Self, ResumeDataError> {
- let data = std::fs::read(path.as_ref()).map_err(|e| ResumeDataError::IoError(e.to_string()))?;
- Self::from_bytes(&data)
- }
-
- /// Parse resume data from bytes
- pub fn from_bytes(bytes: &[u8]) -> Result<Self, ResumeDataError> {
- let nsdata = NSData::with_bytes(bytes);
- let dict = Self::parse_resume_dict(&nsdata)?;
-
- Ok(Self {
- bytes_received: Self::get_number_value(&dict, "NSURLSessionResumeBytesReceived"),
- bytes_expected_to_receive: Self::get_number_value(&dict, "NSURLSessionResumeBytesExpectedToReceive"),
- local_path: Self::get_string_value(&dict, "NSURLSessionResumeInfoLocalPath").map(PathBuf::from),
- temp_file_name: Self::get_string_value(&dict, "NSURLSessionResumeInfoTempFileName"),
- byte_range: Self::get_string_value(&dict, "NSURLSessionResumeByteRange"),
- original_request: Self::get_data_value(&dict, "NSURLSessionResumeOriginalRequest"),
- current_request: Self::get_data_value(&dict, "NSURLSessionResumeCurrentRequest"),
- version: Self::get_number_value(&dict, "NSURLSessionResumeInfoVersion"),
- })
- }
-
- /// Serialize resume data back to bytes (standalone)
- ///
- /// Builds a minimal dictionary from the fields present on this struct
- /// and serializes it in binary PropertyList format. This does not depend
- /// on original resume data bytes.
- pub fn to_bytes(&self) -> Result<Box<[u8]>, ResumeDataError> {
- unsafe {
- // Create an empty NSDictionary<NSString, NSObject> to seed correct generics
- let empty: Retained<NSDictionary<NSString, NSObject>> = NSDictionary::new();
- let mutable_dict = NSMutableDictionary::dictionaryWithDictionary(&empty);
-
- if let Some(bytes_received) = self.bytes_received {
- mutable_dict.setObject_forKey(
- &NSNumber::numberWithUnsignedLongLong(bytes_received),
- &ProtocolObject::from_retained(NSString::from_str("NSURLSessionResumeBytesReceived")),
- );
- }
-
- if let Some(bytes_expected) = self.bytes_expected_to_receive {
- mutable_dict.setObject_forKey(
- &NSNumber::numberWithUnsignedLongLong(bytes_expected),
- &ProtocolObject::from_retained(NSString::from_str("NSURLSessionResumeBytesExpectedToReceive")),
- );
- }
-
- if let Some(ref local_path) = self.local_path {
- mutable_dict.setObject_forKey(
- &NSString::from_str(&local_path.to_string_lossy()),
- &ProtocolObject::from_retained(NSString::from_str("NSURLSessionResumeInfoLocalPath")),
- );
- }
-
- if let Some(ref temp_name) = self.temp_file_name {
- mutable_dict.setObject_forKey(
- &NSString::from_str(temp_name),
- &ProtocolObject::from_retained(NSString::from_str("NSURLSessionResumeInfoTempFileName")),
- );
- }
-
- if let Some(ref byte_range) = self.byte_range {
- mutable_dict.setObject_forKey(
- &NSString::from_str(byte_range),
- &ProtocolObject::from_retained(NSString::from_str("NSURLSessionResumeByteRange")),
- );
- }
-
- if let Some(ref original_req) = self.original_request {
- let nsdata = NSData::with_bytes(original_req);
- mutable_dict.setObject_forKey(
- &nsdata,
- &ProtocolObject::from_retained(NSString::from_str("NSURLSessionResumeOriginalRequest")),
- );
- }
-
- if let Some(ref current_req) = self.current_request {
- let nsdata = NSData::with_bytes(current_req);
- mutable_dict.setObject_forKey(
- &nsdata,
- &ProtocolObject::from_retained(NSString::from_str("NSURLSessionResumeCurrentRequest")),
- );
- }
-
- if let Some(ver) = self.version {
- mutable_dict.setObject_forKey(
- &NSNumber::numberWithUnsignedLongLong(ver),
- &ProtocolObject::from_retained(NSString::from_str("NSURLSessionResumeInfoVersion")),
- );
- }
-
- // Serialize to binary PropertyList format
- // NSURLSession accepts PropertyList format for resume data
- let data = NSPropertyListSerialization::dataWithPropertyList_format_options_error(
- &mutable_dict,
- NSPropertyListFormat::BinaryFormat_v1_0,
- 0,
- )
- .ok()
- .ok_or_else(|| ResumeDataError::SerializationFailed)?;
-
- Ok(data.to_vec().into_boxed_slice())
- }
- }
-
- /// Save modified resume data to a file
- pub fn save_to_file<P: AsRef<Path>>(
- &self,
- path: P,
- ) -> Result<(), ResumeDataError> {
- let bytes = self.to_bytes()?;
- std::fs::write(path.as_ref(), bytes).map_err(|e| ResumeDataError::IoError(e.to_string()))?;
- Ok(())
- }
-
- /// Get the temporary file path based on version
- pub fn temp_file_path(&self) -> Option<PathBuf> {
- if let Some(version) = self.version {
- if version > 1 {
- // iOS 10.2+ uses temp file name
- self.temp_file_name.as_ref().map(|name| std::env::temp_dir().join(name))
- } else {
- // iOS < 10.2 uses local path
- self.local_path.as_ref().map(|path| {
- if let Some(name) = path.file_name() {
- std::env::temp_dir().join(name)
- } else {
- path.clone()
- }
- })
- }
- } else {
- // If version is missing, try temp_file_name first
- self.temp_file_name.as_ref().map(|name| std::env::temp_dir().join(name)).or_else(|| {
- self.local_path.as_ref().map(|path| {
- if let Some(name) = path.file_name() {
- std::env::temp_dir().join(name)
- } else {
- path.clone()
- }
- })
- })
- }
- }
-
- /// Parse resume data dictionary using NSKeyedUnarchiver
- ///
- /// In beta versions and newer iOS, resumeData is NSKeyedArchive encoded
- /// instead of plain plist. Falls back to PropertyListSerialization for
- /// older formats.
- fn parse_resume_dict(data: &NSData) -> Result<Retained<NSDictionary<NSString, NSObject>>, ResumeDataError> {
- unsafe {
- let dict_cls = objc2::class!(NSDictionary);
-
- // Try NSKeyedUnarchiver first (NSKeyedArchive format) using modern API
- let allocated: Allocated<NSKeyedUnarchiver> = msg_send![NSKeyedUnarchiver::class(), alloc];
-
- match NSKeyedUnarchiver::initForReadingFromData_error(allocated, data) {
- Ok(unarchiver) => {
- // Disable secure coding for NSURLSession resume data compatibility
- unarchiver.setRequiresSecureCoding(false);
-
- // Decode using the standard root object key
- let key = NSString::from_str("NSKeyedArchiveRootObjectKey");
-
- if let Some(obj) = unarchiver.decodeObjectForKey(&key) {
- unarchiver.finishDecoding();
- let is_dict: bool = msg_send![&obj, isKindOfClass: dict_cls];
- if is_dict {
- let dict = Retained::cast_unchecked::<NSDictionary<NSString, NSObject>>(obj);
- return Ok(dict);
- }
- }
-
- unarchiver.finishDecoding();
- },
- Err(_) => {},
- }
-
- // Fallback to PropertyListSerialization for legacy formats
- let obj = NSPropertyListSerialization::propertyListWithData_options_format_error(
- data,
- NSPropertyListMutabilityOptions::MutableContainersAndLeaves,
- core::ptr::null_mut::<NSPropertyListFormat>(),
- )
- .ok()
- .ok_or_else(|| {
- ResumeDataError::ParseError("Both NSKeyedUnarchiver and PropertyListSerialization failed".to_string())
- })?;
-
- // Check if it's a dictionary
- let is_dict: bool = msg_send![&obj, isKindOfClass: dict_cls];
-
- if is_dict {
- Ok(Retained::cast_unchecked(obj))
- } else {
- Err(ResumeDataError::NotADictionary)
- }
- }
- }
-
- /// Get a u64 value from the dictionary
- fn get_number_value(
- dict: &NSDictionary<NSString, NSObject>,
- key: &str,
- ) -> Option<u64> {
- let key_ns = NSString::from_str(key);
- dict.objectForKey(&key_ns).and_then(|obj| {
- let number_class = objc2::class!(NSNumber);
- let is_number = obj.isKindOfClass(number_class);
-
- if is_number {
- let num = unsafe { Retained::cast_unchecked::<NSNumber>(obj) };
- Some(num.unsignedLongLongValue())
- } else {
- None
- }
- })
- }
-
- /// Get a string value from the dictionary
- fn get_string_value(
- dict: &NSDictionary<NSString, NSObject>,
- key: &str,
- ) -> Option<String> {
- let key_ns = NSString::from_str(key);
- dict.objectForKey(&key_ns).and_then(|obj| {
- let string_class = objc2::class!(NSString);
- let is_string = obj.isKindOfClass(string_class);
-
- if is_string {
- let s = unsafe { Retained::cast_unchecked::<NSString>(obj) };
- Some(s.to_string())
- } else {
- None
- }
- })
- }
-
- /// Get data bytes from the dictionary
- fn get_data_value(
- dict: &NSDictionary<NSString, NSObject>,
- key: &str,
- ) -> Option<Vec<u8>> {
- let key_ns = NSString::from_str(key);
- dict.objectForKey(&key_ns).and_then(|obj| {
- let data_class = objc2::class!(NSData);
- let is_data = obj.isKindOfClass(data_class);
-
- if is_data {
- let data = unsafe { Retained::cast_unchecked::<NSData>(obj) };
- Some(data.to_vec())
- } else {
- None
- }
- })
- }
-}
diff --git a/crates/download-manager/src/managers/apple/url_session_error.rs b/crates/download-manager/src/managers/apple/url_session_error.rs
deleted file mode 100644
index daf302031..000000000
--- a/crates/download-manager/src/managers/apple/url_session_error.rs
+++ /dev/null
@@ -1,260 +0,0 @@
-use crate::prelude::*;
-
-/// Common NSURLError codes from the NSURLErrorDomain
-#[derive(Debug, Clone, Copy, PartialEq, Eq)]
-pub enum URLSessionErrorCode {
- /// -999: Request was cancelled (expected during pause operations)
- Cancelled,
- /// -1000: Malformed URL
- BadURL,
- /// -1001: Connection timeout
- TimedOut,
- /// -1002: Unsupported URL scheme
- UnsupportedURL,
- /// -1003: DNS resolution failed
- CannotFindHost,
- /// -1004: Connection refused by server
- CannotConnectToHost,
- /// -1005: Connection dropped during transfer
- NetworkConnectionLost,
- /// -1006: DNS lookup failed
- DNSLookupFailed,
- /// -1007: Too many redirects
- HTTPTooManyRedirects,
- /// -1008: Resource unavailable
- ResourceUnavailable,
- /// -1009: No internet connection
- NotConnectedToInternet,
- /// -1010: Redirect to non-existent location
- RedirectToNonExistentLocation,
- /// -1011: Bad server response
- BadServerResponse,
- /// -1012: User cancelled authentication
- UserCancelledAuthentication,
- /// -1013: Authentication required
- UserAuthenticationRequired,
- /// -1200: Secure connection failed
- SecureConnectionFailed,
- /// -1201: Server certificate invalid
- ServerCertificateHasBadDate,
- /// -1202: Server certificate untrusted
- ServerCertificateUntrusted,
- /// -1203: Server certificate has unknown root
- ServerCertificateHasUnknownRoot,
- /// -1204: Server certificate not yet valid
- ServerCertificateNotYetValid,
- /// -1205: Client certificate rejected
- ClientCertificateRejected,
- /// -1206: Client certificate required
- ClientCertificateRequired,
- /// -2000: Cannot load from network
- CannotLoadFromNetwork,
- /// Other error code not in the known list
- Other(i64),
-}
-
-impl URLSessionErrorCode {
- /// Parse an error code from NSError
- pub fn from_error_code(code: i64) -> Self {
- match code {
- -999 => Self::Cancelled,
- -1000 => Self::BadURL,
- -1001 => Self::TimedOut,
- -1002 => Self::UnsupportedURL,
- -1003 => Self::CannotFindHost,
- -1004 => Self::CannotConnectToHost,
- -1005 => Self::NetworkConnectionLost,
- -1006 => Self::DNSLookupFailed,
- -1007 => Self::HTTPTooManyRedirects,
- -1008 => Self::ResourceUnavailable,
- -1009 => Self::NotConnectedToInternet,
- -1010 => Self::RedirectToNonExistentLocation,
- -1011 => Self::BadServerResponse,
- -1012 => Self::UserCancelledAuthentication,
- -1013 => Self::UserAuthenticationRequired,
- -1200 => Self::SecureConnectionFailed,
- -1201 => Self::ServerCertificateHasBadDate,
- -1202 => Self::ServerCertificateUntrusted,
- -1203 => Self::ServerCertificateHasUnknownRoot,
- -1204 => Self::ServerCertificateNotYetValid,
- -1205 => Self::ClientCertificateRejected,
- -1206 => Self::ClientCertificateRequired,
- -2000 => Self::CannotLoadFromNetwork,
- other => Self::Other(other),
- }
- }
-
- /// Get the raw error code value
- pub fn code(&self) -> i64 {
- match self {
- Self::Cancelled => -999,
- Self::BadURL => -1000,
- Self::TimedOut => -1001,
- Self::UnsupportedURL => -1002,
- Self::CannotFindHost => -1003,
- Self::CannotConnectToHost => -1004,
- Self::NetworkConnectionLost => -1005,
- Self::DNSLookupFailed => -1006,
- Self::HTTPTooManyRedirects => -1007,
- Self::ResourceUnavailable => -1008,
- Self::NotConnectedToInternet => -1009,
- Self::RedirectToNonExistentLocation => -1010,
- Self::BadServerResponse => -1011,
- Self::UserCancelledAuthentication => -1012,
- Self::UserAuthenticationRequired => -1013,
- Self::SecureConnectionFailed => -1200,
- Self::ServerCertificateHasBadDate => -1201,
- Self::ServerCertificateUntrusted => -1202,
- Self::ServerCertificateHasUnknownRoot => -1203,
- Self::ServerCertificateNotYetValid => -1204,
- Self::ClientCertificateRejected => -1205,
- Self::ClientCertificateRequired => -1206,
- Self::CannotLoadFromNetwork => -2000,
- Self::Other(code) => *code,
- }
- }
-
- /// Get a human-readable description of the error
- pub fn description(&self) -> &'static str {
- match self {
- Self::Cancelled => "Request was cancelled",
- Self::BadURL => "Malformed URL",
- Self::TimedOut => "Connection timeout",
- Self::UnsupportedURL => "Unsupported URL scheme",
- Self::CannotFindHost => "DNS resolution failed",
- Self::CannotConnectToHost => "Connection refused by server",
- Self::NetworkConnectionLost => "Connection dropped during transfer",
- Self::DNSLookupFailed => "DNS lookup failed",
- Self::HTTPTooManyRedirects => "Too many redirects",
- Self::ResourceUnavailable => "Resource unavailable",
- Self::NotConnectedToInternet => "No internet connection",
- Self::RedirectToNonExistentLocation => "Redirect to non-existent location",
- Self::BadServerResponse => "Bad server response",
- Self::UserCancelledAuthentication => "User cancelled authentication",
- Self::UserAuthenticationRequired => "Authentication required",
- Self::SecureConnectionFailed => "Secure connection failed",
- Self::ServerCertificateHasBadDate => "Server certificate has invalid date",
- Self::ServerCertificateUntrusted => "Server certificate is untrusted",
- Self::ServerCertificateHasUnknownRoot => "Server certificate has unknown root",
- Self::ServerCertificateNotYetValid => "Server certificate not yet valid",
- Self::ClientCertificateRejected => "Client certificate rejected",
- Self::ClientCertificateRequired => "Client certificate required",
- Self::CannotLoadFromNetwork => "Cannot load from network",
- Self::Other(_) => "Unknown error",
- }
- }
-
- /// Check if this error should be ignored (e.g., expected cancellations)
- pub fn should_ignore(&self) -> bool {
- matches!(self, Self::Cancelled)
- }
-
- /// Check if this is a network connectivity error
- pub fn is_network_error(&self) -> bool {
- matches!(
- self,
- Self::NotConnectedToInternet
- | Self::NetworkConnectionLost
- | Self::CannotConnectToHost
- | Self::TimedOut
- | Self::CannotLoadFromNetwork
- )
- }
-
- /// Check if this is a DNS/host resolution error
- pub fn is_dns_error(&self) -> bool {
- matches!(self, Self::CannotFindHost | Self::DNSLookupFailed)
- }
-
- /// Check if this is a certificate/security error
- pub fn is_certificate_error(&self) -> bool {
- matches!(
- self,
- Self::SecureConnectionFailed
- | Self::ServerCertificateHasBadDate
- | Self::ServerCertificateUntrusted
- | Self::ServerCertificateHasUnknownRoot
- | Self::ServerCertificateNotYetValid
- | Self::ClientCertificateRejected
- | Self::ClientCertificateRequired
- )
- }
-
- /// Check if this error is retryable (might succeed on retry)
- pub fn is_retryable(&self) -> bool {
- self.is_network_error() || matches!(self, Self::TimedOut | Self::ResourceUnavailable | Self::BadServerResponse)
- }
-}
-
-/// Parsed error information from NSError
-#[derive(Debug, Clone)]
-pub struct URLSessionError {
- /// Parsed error code
- pub code: URLSessionErrorCode,
- /// Error domain (e.g., "NSURLErrorDomain")
- pub domain: String,
- /// Localized error description from the system
- pub localized_description: String,
-}
-
-#[allow(unused)]
-impl URLSessionError {
- /// Parse an NSError into a URLSessionError
- pub fn from_nserror(error: &NSError) -> Self {
- let code = URLSessionErrorCode::from_error_code(error.code() as i64);
- let domain = error.domain().to_string();
- let localized_description = error.localizedDescription().to_string();
-
- Self {
- code,
- domain,
- localized_description,
- }
- }
-
- /// Get a formatted error message suitable for user display
- pub fn user_message(&self) -> String {
- // Use localized description if available and non-empty
- if !self.localized_description.is_empty() {
- format!("{} (code: {})", self.localized_description, self.code.code())
- } else {
- format!("{} (code: {})", self.code.description(), self.code.code())
- }
- }
-
- /// Get a detailed error message suitable for logging
- pub fn debug_message(&self) -> String {
- format!(
- "code={}, domain={}, desc='{}', type={}",
- self.code.code(),
- self.domain,
- self.localized_description,
- self.code.description()
- )
- }
-
- /// Check if this error should be ignored
- pub fn should_ignore(&self) -> bool {
- self.code.should_ignore()
- }
-
- /// Check if this is a network connectivity error
- pub fn is_network_error(&self) -> bool {
- self.code.is_network_error()
- }
-
- /// Check if this is a DNS/host resolution error
- pub fn is_dns_error(&self) -> bool {
- self.code.is_dns_error()
- }
-
- /// Check if this is a certificate/security error
- pub fn is_certificate_error(&self) -> bool {
- self.code.is_certificate_error()
- }
-
- /// Check if this error is retryable
- pub fn is_retryable(&self) -> bool {
- self.code.is_retryable()
- }
-}
diff --git a/crates/download-manager/src/managers/apple/url_session_ext.rs b/crates/download-manager/src/managers/apple/url_session_ext.rs
deleted file mode 100644
index 12ce8c00a..000000000
--- a/crates/download-manager/src/managers/apple/url_session_ext.rs
+++ /dev/null
@@ -1,121 +0,0 @@
-use crate::{
- DownloadError,
- managers::apple::{URLSessionDownloadTaskResumeData, URLSessionGetTasksCompletionHandler},
- prelude::*,
-};
-
-#[allow(unused)]
-pub struct URLSessionTasks {
- pub data_tasks: Box<[Retained<NSURLSessionDataTask>]>,
- pub upload_tasks: Box<[Retained<NSURLSessionUploadTask>]>,
- pub download_tasks: Box<[Retained<NSURLSessionDownloadTask>]>,
-}
-
-impl Default for URLSessionTasks {
- fn default() -> Self {
- Self {
- data_tasks: Box::default(),
- upload_tasks: Box::default(),
- download_tasks: Box::default(),
- }
- }
-}
-
-#[allow(unused)]
-#[async_trait::async_trait]
-pub trait URLSessionExt {
- async fn get_tasks(&self) -> URLSessionTasks;
- fn get_tasks_blocking(&self) -> URLSessionTasks;
- fn description(&self) -> Option<String>;
- fn set_description(
- &self,
- s: &str,
- );
- fn download_task_with_url(
- &self,
- source_url: &str,
- ) -> Result<Retained<NSURLSessionDownloadTask>, DownloadError>;
- fn download_task_with_resume_data(
- &self,
- resume_data: &URLSessionDownloadTaskResumeData,
- ) -> Result<Retained<NSURLSessionDownloadTask>, DownloadError>;
-}
-
-#[async_trait::async_trait]
-impl URLSessionExt for NSURLSession {
- async fn get_tasks(&self) -> URLSessionTasks {
- let receiver = {
- let (sender, receiver) = tokio_oneshot_channel();
- let sender = Arc::new(Mutex::new(Some(sender)));
- let handler = URLSessionGetTasksCompletionHandler::new({
- let sender = sender.clone();
- move |data_tasks, upload_tasks, download_tasks| {
- if let Some(s) = sender.lock().unwrap().take() {
- let _ = s.send(URLSessionTasks {
- data_tasks,
- upload_tasks,
- download_tasks,
- });
- }
- }
- });
- unsafe { self.getTasksWithCompletionHandler(&handler) };
- receiver
- };
- receiver.await.unwrap_or(URLSessionTasks::default())
- }
-
- fn get_tasks_blocking(&self) -> URLSessionTasks {
- use std::sync::{Arc, Mutex, mpsc::sync_channel};
- let (sender, receiver) = sync_channel(1);
- let sender = Arc::new(Mutex::new(Some(sender)));
- let handler = URLSessionGetTasksCompletionHandler::new({
- let sender = sender.clone();
- move |data_tasks, upload_tasks, download_tasks| {
- if let Some(s) = sender.lock().unwrap().take() {
- let _ = s.send(URLSessionTasks {
- data_tasks,
- upload_tasks,
- download_tasks,
- });
- }
- }
- });
- unsafe { self.getTasksWithCompletionHandler(&handler) };
- receiver.recv().unwrap_or_default()
- }
-
- fn description(&self) -> Option<String> {
- let desc_opt = self.sessionDescription();
- desc_opt.map(|d| d.to_string())
- }
-
- fn set_description(
- &self,
- s: &str,
- ) {
- let ns = NSString::from_str(s);
- self.setSessionDescription(Some(&ns));
- }
-
- fn download_task_with_url(
- &self,
- source_url: &str,
- ) -> Result<Retained<NSURLSessionDownloadTask>, DownloadError> {
- autoreleasepool(|_| {
- let nsurl = NSURL::URLWithString(&NSString::from_str(source_url)).ok_or(DownloadError::BadUrl)?;
- Ok::<_, DownloadError>(self.downloadTaskWithURL(&nsurl))
- })
- }
-
- fn download_task_with_resume_data(
- &self,
- resume_data: &URLSessionDownloadTaskResumeData,
- ) -> Result<Retained<NSURLSessionDownloadTask>, DownloadError> {
- let bytes = resume_data.to_bytes().map_err(|_| DownloadError::ResumeDataError)?;
- autoreleasepool(|_| {
- let nsdata = NSData::with_bytes(&bytes);
- Ok::<_, DownloadError>(self.downloadTaskWithResumeData(&nsdata))
- })
- }
-}
diff --git a/crates/download-manager/src/managers/apple/url_session_get_tasks_handler.rs b/crates/download-manager/src/managers/apple/url_session_get_tasks_handler.rs
deleted file mode 100644
index 5f45d34bd..000000000
--- a/crates/download-manager/src/managers/apple/url_session_get_tasks_handler.rs
+++ /dev/null
@@ -1,56 +0,0 @@
-use crate::prelude::*;
-
-/// A wrapper for NSURLSession getTasksWithCompletionHandler block.
-///
-/// Signature corresponds to Apple's API:
-/// (NSArray<NSURLSessionDataTask>*, NSArray<NSURLSessionUploadTask>*, NSArray<NSURLSessionDownloadTask>*) -> void
-pub struct URLSessionGetTasksCompletionHandler(
- RcBlock<
- dyn Fn(
- NonNull<NSArray<NSURLSessionDataTask>>,
- NonNull<NSArray<NSURLSessionUploadTask>>,
- NonNull<NSArray<NSURLSessionDownloadTask>>,
- ),
- >,
-);
-
-impl URLSessionGetTasksCompletionHandler {
- pub fn new<F>(handler: F) -> Self
- where
- F: Fn(
- Box<[Retained<NSURLSessionDataTask>]>,
- Box<[Retained<NSURLSessionUploadTask>]>,
- Box<[Retained<NSURLSessionDownloadTask>]>,
- ) + 'static,
- {
- Self(RcBlock::new(
- move |data_tasks_ptr: NonNull<NSArray<NSURLSessionDataTask>>,
- upload_tasks_ptr: NonNull<NSArray<NSURLSessionUploadTask>>,
- download_tasks_ptr: NonNull<NSArray<NSURLSessionDownloadTask>>| {
- let data_tasks = unsafe { data_tasks_ptr.as_ref() };
- let upload_tasks = unsafe { upload_tasks_ptr.as_ref() };
- let download_tasks = unsafe { download_tasks_ptr.as_ref() };
-
- let data_vec = data_tasks.to_vec();
- let upload_vec = upload_tasks.to_vec();
- let download_vec = download_tasks.to_vec();
-
- handler(data_vec.into_boxed_slice(), upload_vec.into_boxed_slice(), download_vec.into_boxed_slice());
- },
- ))
- }
-}
-
-impl Deref for URLSessionGetTasksCompletionHandler {
- type Target = Block<
- dyn Fn(
- NonNull<NSArray<NSURLSessionDataTask>>,
- NonNull<NSArray<NSURLSessionUploadTask>>,
- NonNull<NSArray<NSURLSessionDownloadTask>>,
- ),
- >;
-
- fn deref(&self) -> &Self::Target {
- &self.0
- }
-}
diff --git a/crates/download-manager/src/managers/apple/url_session_resume_data_handler.rs b/crates/download-manager/src/managers/apple/url_session_resume_data_handler.rs
deleted file mode 100644
index 17e32cb2d..000000000
--- a/crates/download-manager/src/managers/apple/url_session_resume_data_handler.rs
+++ /dev/null
@@ -1,36 +0,0 @@
-use crate::prelude::*;
-
-pub struct URLSessionResumeDataHandler(RcBlock<dyn Fn(*mut NSData)>);
-
-impl URLSessionResumeDataHandler {
- #[allow(unused)]
- pub fn new<F>(handler: F) -> Self
- where
- F: Fn(*mut NSData) + 'static,
- {
- Self(RcBlock::new(handler))
- }
-
- pub fn new_bytes<F>(handler: F) -> Self
- where
- F: Fn(Box<[u8]>) + 'static,
- {
- Self(RcBlock::new(move |ptr: *mut NSData| {
- let boxed: Box<[u8]> = if let Some(nsdata) = unsafe { ptr.as_ref() } {
- nsdata.to_vec().into_boxed_slice()
- } else {
- // Some systems may deliver nil resume data; treat as empty
- Box::new([])
- };
- handler(boxed);
- }))
- }
-}
-
-impl Deref for URLSessionResumeDataHandler {
- type Target = Block<dyn Fn(*mut NSData)>;
-
- fn deref(&self) -> &Self::Target {
- &self.0
- }
-}
diff --git a/crates/download-manager/src/managers/apple/url_session_state_reducer.rs b/crates/download-manager/src/managers/apple/url_session_state_reducer.rs
deleted file mode 100644
index 162a2881d..000000000
--- a/crates/download-manager/src/managers/apple/url_session_state_reducer.rs
+++ /dev/null
@@ -1,641 +0,0 @@
-//! State Reduction System
-//!
-//! This module implements a pure functional state reduction system for file downloads.
-//! It takes various inputs (file existence, CRC files, resume data, URLSession tasks)
-//! and produces deterministic outputs (FileDownloadState, InternalDownloadState).
-//!
-//! ## Three-Stage Pipeline
-//!
-//! 1. **Validation** (`reduce_to_checked_file_state`):
-//! - Checks file existence
-//! - Validates CRC integrity (with caching optimization)
-//! - Returns: Valid, Invalid, or Missing
-//!
-//! 2. **Display** (`reduce_to_file_download_state`):
-//! - Maps validated state to user-facing phase
-//! - Calculates progress percentages
-//! - Returns: FileDownloadState for UI
-//!
-//! 3. **Reconciliation** (`reconcile_to_internal_state`):
-//! - Performs cleanup actions (delete corrupted files, etc.)
-//! - Determines what we have after cleanup
-//! - Returns: InternalDownloadState for operations
-//!
-//! ## CRC Caching for Performance
-//!
-//! To avoid recalculating CRC on every app launch:
-//!
-//! **On Download Completion:**
-//! 1. File downloads successfully
-//! 2. Calculate CRC to validate
-//! 3. If valid → Save to `.crc` file (e.g., `model.safetensors.crc`)
-//!
-//! **On Next Launch:**
-//! 1. Check if `.crc` file exists
-//! 2. If exists → Read cached CRC and compare (instant, no file scan)
-//! 3. If missing → Calculate CRC → Save to cache
-//! 4. If mismatch → Recalculate to verify → Update cache
-//!
-//! This makes subsequent launches **100x faster** for large model files.
-
-use std::path::{Path, PathBuf};
-
-use crate::{
- CRCFileState, CheckedFileState, DownloadedFileState, FileDownloadState, InternalDownloadState, LockFileState,
- ResumeDataFileState, calculate_and_verify_crc, check_lock_file,
- managers::apple::{URLSessionDownloadTaskResumeData, URLSessionDownloadTaskState, UrlSessionDownloadTaskExt},
- prelude::*,
-};
-
-/// Check if the downloaded file exists on disk
-pub fn check_file_exists(path: &Path) -> DownloadedFileState {
- if path.exists() {
- DownloadedFileState::Exists
- } else {
- DownloadedFileState::Missing
- }
-}
-
-/// Check if the CRC validation file (.crc) exists on disk
-pub fn check_crc_file_exists(path: &Path) -> CRCFileState {
- if path.exists() {
- CRCFileState::Exists
- } else {
- CRCFileState::Missing
- }
-}
-
-/// Check if the resume data file (.resume_data) exists on disk
-pub fn check_resume_file_exists(path: &Path) -> ResumeDataFileState {
- if path.exists() {
- ResumeDataFileState::Exists
- } else {
- ResumeDataFileState::Missing
- }
-}
-fn validate_crc(
- file_path: &Path,
- expected_crc: &str,
-) -> bool {
- #[cfg(debug_assertions)]
- {
- tracing::debug!("[CRC] Starting CRC validation for file: {}", file_path.display());
- let start = std::time::Instant::now();
- let result = calculate_and_verify_crc(file_path, expected_crc).unwrap_or(false);
- let duration = start.elapsed();
- tracing::debug!(
- "[CRC] CRC validation {} in {:.2}s",
- if result {
- "✓ PASSED"
- } else {
- "✗ FAILED"
- },
- duration.as_secs_f64()
- );
- return result;
- }
-
- #[cfg(not(debug_assertions))]
- calculate_and_verify_crc(file_path, expected_crc).unwrap_or(false)
-}
-
-/// First Reduction: Validate the downloaded file
-///
-/// Checks if the downloaded file exists and validates its integrity using CRC if expected.
-/// Returns CheckedFileState indicating whether the file is Valid, Invalid, or Missing.
-///
-/// CRC Caching Mechanism:
-/// - On download completion: Calculate CRC → if valid, save to `.crc` file
-/// - On next app launch: Check `.crc` file exists → use cached value (fast path)
-/// - If cache missing: Recalculate CRC → save to `.crc` file for next time
-/// - If cache mismatch: Recalculate to verify → update cache if valid
-///
-/// Logic:
-/// - If file is missing → Missing (regardless of CRC file existence)
-/// - If file exists + CRC cached → Use cached value (fast)
-/// - If file exists + CRC not cached → Validate CRC → Cache result → Valid or Invalid
-/// - If file exists + no CRC expected → Valid (no validation needed)
-pub fn reduce_to_checked_file_state(
- file_state: DownloadedFileState,
- crc_file_state: CRCFileState,
- file_path: &Path,
- expected_crc: Option<&str>,
-) -> CheckedFileState {
- tracing::info!(
- "[REDUCE_CHECKED] file={:?}, crc_file={:?}, expected_crc={:?}, path={}",
- file_state,
- crc_file_state,
- expected_crc,
- file_path.display()
- );
-
- let result = match (file_state, crc_file_state, expected_crc) {
- // File missing - nothing to validate
- (DownloadedFileState::Missing, _, _) => CheckedFileState::Missing,
-
- // File exists with CRC file - use cached CRC (fast path)
- (DownloadedFileState::Exists, CRCFileState::Exists, Some(expected)) => {
- let crc_path = format!("{}.crc", file_path.display());
- if let Ok(saved_crc) = std::fs::read_to_string(&crc_path) {
- if saved_crc.trim() == expected {
- tracing::debug!("[REDUCE_CHECKED] ✓ Using cached CRC for {}", file_path.display());
- CheckedFileState::Valid
- } else {
- tracing::warn!("[REDUCE_CHECKED] CRC mismatch in cache, recalculating for {}", file_path.display());
- // CRC mismatch, recalculate to be sure
- if validate_crc(file_path, expected) {
- // Update cache with correct value
- let _ = crate::crc_utils::save_crc_file(file_path, expected);
- CheckedFileState::Valid
- } else {
- CheckedFileState::Invalid
- }
- }
- } else {
- tracing::warn!("[REDUCE_CHECKED] Failed to read cached CRC, recalculating for {}", file_path.display());
- // Fallback to recalculation if reading .crc fails
- if validate_crc(file_path, expected) {
- // Save the cache for next time
- let _ = crate::crc_utils::save_crc_file(file_path, expected);
- CheckedFileState::Valid
- } else {
- CheckedFileState::Invalid
- }
- }
- },
-
- // File exists without CRC file - recalculate and cache
- (DownloadedFileState::Exists, CRCFileState::Missing, Some(expected)) => {
- if validate_crc(file_path, expected) {
- // Cache the CRC for next time
- if let Err(e) = crate::crc_utils::save_crc_file(file_path, expected) {
- tracing::warn!("[REDUCE_CHECKED] Failed to cache CRC for {}: {}", file_path.display(), e);
- } else {
- tracing::debug!("[REDUCE_CHECKED] ✓ CRC cached for {}", file_path.display());
- }
- CheckedFileState::Valid
- } else {
- CheckedFileState::Invalid
- }
- },
-
- // File exists without CRC requirement - consider valid
- (DownloadedFileState::Exists, _, None) => CheckedFileState::Valid,
- };
-
- tracing::info!("[REDUCE_CHECKED] → Result: {:?}", result);
-
- result
-}
-
-/// Second Reduction: Determine FileDownloadState for display
-///
-/// Maps combinations of file validation result, resume data, and URLSession task state
-/// to FileDownloadState with appropriate byte counts for UI display.
-///
-/// This produces the user-facing state based on current reality.
-pub fn reduce_to_file_download_state(
- checked_state: CheckedFileState,
- resume_state: ResumeDataFileState,
- task_state: URLSessionDownloadTaskState,
- url_session_task: Option<&Retained<NSURLSessionDownloadTask>>,
- file_path: &Path,
- expected_bytes: Option<u64>,
- manager_id: &str,
-) -> FileDownloadState {
- tracing::info!(
- "[REDUCE_STATE] checked={:?}, resume={:?}, task_state={:?}, path={}",
- checked_state,
- resume_state,
- task_state.map(|s| format!("{:?}", s)),
- file_path.display()
- );
-
- // Lock awareness (decision in reduction): when not already downloaded and a lock
- // is held by another manager, surface LockedByOther immediately.
- if !matches!(checked_state, CheckedFileState::Valid) {
- let lock_path = PathBuf::from(format!("{}.lock", file_path.display()));
- let lock_state = check_lock_file(&lock_path, manager_id, std::process::id());
- if let LockFileState::OwnedByOtherApp(info) = lock_state {
- let bytes = url_session_task.map(|task| task.count_of_bytes_received()).unwrap_or(0);
- let total = expected_bytes
- .or_else(|| url_session_task.map(|t| t.count_of_bytes_expected_to_receive()))
- .unwrap_or(0);
- return FileDownloadState {
- phase: crate::FileDownloadPhase::LockedByOther(info.manager_id),
- downloaded_bytes: bytes,
- total_bytes: total,
- };
- }
- }
-
- let result = match (checked_state, resume_state, task_state) {
- (CheckedFileState::Valid, _, _) => {
- let bytes = std::fs::metadata(file_path).map(|metadata| metadata.len()).unwrap_or(0);
- FileDownloadState::downloaded(bytes)
- },
-
- (CheckedFileState::Invalid, ResumeDataFileState::Missing, None) => {
- let total = expected_bytes.unwrap_or(0);
- FileDownloadState::not_downloaded(total)
- },
- (CheckedFileState::Invalid, ResumeDataFileState::Exists, None) => {
- let resume_path = PathBuf::from(format!("{}.resume_data", file_path.display()));
- let resume_bytes = get_resume_bytes(&resume_path).unwrap_or(0);
- let total = expected_bytes.unwrap_or(0);
- FileDownloadState::paused(resume_bytes, total)
- },
- (CheckedFileState::Invalid, _, Some(NSURLSessionTaskState::Running)) => {
- let bytes = url_session_task.map(|task| task.count_of_bytes_received()).unwrap_or(0);
- let total_from_task = url_session_task.map(|task| task.count_of_bytes_expected_to_receive()).unwrap_or(0);
- let total = if total_from_task > 0 {
- total_from_task
- } else {
- expected_bytes.unwrap_or(0)
- };
- FileDownloadState::downloading(bytes, total)
- },
- (CheckedFileState::Invalid, _, Some(NSURLSessionTaskState::Suspended)) => {
- let bytes = url_session_task.map(|task| task.count_of_bytes_received()).unwrap_or(0);
- let total_from_task = url_session_task.map(|task| task.count_of_bytes_expected_to_receive()).unwrap_or(0);
- let total = if total_from_task > 0 {
- total_from_task
- } else {
- expected_bytes.unwrap_or(0)
- };
- FileDownloadState::paused(bytes, total)
- },
- (CheckedFileState::Invalid, _, Some(NSURLSessionTaskState::Completed | NSURLSessionTaskState::Canceling)) => {
- let total = expected_bytes.unwrap_or(0);
- FileDownloadState::not_downloaded(total)
- },
-
- (CheckedFileState::Missing, ResumeDataFileState::Missing, None) => {
- let total = expected_bytes.unwrap_or(0);
- FileDownloadState::not_downloaded(total)
- },
- (CheckedFileState::Missing, ResumeDataFileState::Exists, None) => {
- let resume_path = PathBuf::from(format!("{}.resume_data", file_path.display()));
- let bytes = get_resume_bytes(&resume_path).unwrap_or(0);
- let total = expected_bytes.unwrap_or(0);
- FileDownloadState::paused(bytes, total)
- },
- (CheckedFileState::Missing, _, Some(NSURLSessionTaskState::Running)) => {
- let bytes = url_session_task.map(|task| task.count_of_bytes_received()).unwrap_or(0);
- let total_from_task = url_session_task.map(|task| task.count_of_bytes_expected_to_receive()).unwrap_or(0);
- let total = if total_from_task > 0 {
- total_from_task
- } else {
- expected_bytes.unwrap_or(0)
- };
- FileDownloadState::downloading(bytes, total)
- },
- (CheckedFileState::Missing, _, Some(NSURLSessionTaskState::Suspended)) => {
- let bytes = url_session_task.map(|task| task.count_of_bytes_received()).unwrap_or(0);
- let total_from_task = url_session_task.map(|task| task.count_of_bytes_expected_to_receive()).unwrap_or(0);
- let total = if total_from_task > 0 {
- total_from_task
- } else {
- expected_bytes.unwrap_or(0)
- };
- FileDownloadState::paused(bytes, total)
- },
- (CheckedFileState::Missing, _, Some(NSURLSessionTaskState::Completed | NSURLSessionTaskState::Canceling)) => {
- let total = expected_bytes.unwrap_or(0);
- FileDownloadState::not_downloaded(total)
- },
-
- _ => {
- let total = expected_bytes.unwrap_or(0);
- FileDownloadState::not_downloaded(total)
- },
- };
-
- tracing::info!("[REDUCE_STATE] → Result: {:?}", result);
-
- // Safety net: If we calculated 0 total bytes (e.g. because URLSession hasn't reported size yet)
- // but we have an expected size from registry, use the expected size to prevent progress jumps.
- if result.total_bytes == 0 {
- if let Some(expected) = expected_bytes {
- if expected > 0 {
- return FileDownloadState {
- phase: result.phase,
- downloaded_bytes: result.downloaded_bytes,
- total_bytes: expected,
- };
- }
- }
- }
-
- result
-}
-
-/// Reconciliation: Clean up disk artifacts and determine final internal state
-///
-/// Performs necessary cleanup actions based on file validation and task state:
-/// - Deletes corrupted/stale files and invalidates their CRC caches
-/// - Keeps valid CRC cache files (`.crc`) for fast validation on next launch
-/// - Deletes or keeps resume data based on state
-/// - Cancels URLSession tasks when appropriate
-/// - Produces resume data from suspended tasks
-/// - Compares and keeps the better resume data when multiple sources exist
-///
-/// CRC Cache Management:
-/// - Valid files: Keep `.crc` file (already created by reduce_to_checked_file_state)
-/// - Invalid files: Delete both file and `.crc` cache
-/// - Downloaded state: Preserve `.crc` for instant validation on next init
-///
-/// Returns: InternalDownloadState representing what we have after cleanup
-///
-/// Invariants after reconciliation:
-/// - CheckedFileState::Valid → File + CRC remain on disk
-/// - CheckedFileState::Invalid → File + CRC deleted
-/// - CheckedFileState::Missing → CRC deleted if existed
-pub async fn reconcile_to_internal_state(
- checked_file: CheckedFileState,
- resume_file: ResumeDataFileState,
- url_session_task: Option<&Retained<NSURLSessionDownloadTask>>,
- file_path: &Path,
- crc_path: &Path,
- resume_path: &Path,
- expected_bytes: Option<u64>,
-) -> InternalDownloadState<Retained<NSURLSessionDownloadTask>> {
- tracing::info!(
- "[RECONCILE] checked={:?}, resume={:?}, task={:?}, path={}",
- checked_file,
- resume_file,
- url_session_task.map(|t| format!("{:?}", t.state())),
- file_path.display()
- );
-
- let crc_path_buf = crc_path.to_path_buf();
-
- let result = match (checked_file, resume_file, url_session_task) {
- // Cases 1-2: Valid file, no task
- (CheckedFileState::Valid, ResumeDataFileState::Missing, None) => {
- tracing::info!("[RECONCILE] Case 1: Valid file, no resume, no task → Downloaded");
-
- InternalDownloadState::Downloaded {
- file_path: file_path.to_path_buf(),
- crc_path: if crc_path_buf.exists() {
- Some(crc_path_buf)
- } else {
- None
- },
- }
- },
- (CheckedFileState::Valid, ResumeDataFileState::Exists, None) => {
- tracing::info!("[RECONCILE] Case 2: Valid file, has resume, no task → Downloaded (delete resume)");
-
- let _ = std::fs::remove_file(resume_path);
- InternalDownloadState::Downloaded {
- file_path: file_path.to_path_buf(),
- crc_path: if crc_path_buf.exists() {
- Some(crc_path_buf)
- } else {
- None
- },
- }
- },
-
- // Cases 3-10: Valid file with any task - cancel task, cleanup
- (CheckedFileState::Valid, _, Some(any_task)) => {
- tracing::info!("[RECONCILE] Cases 3-10: Valid file with task → Downloaded (cancel task, cleanup)");
-
- any_task.cancel();
- if resume_file == ResumeDataFileState::Exists {
- let _ = std::fs::remove_file(resume_path);
- }
- InternalDownloadState::Downloaded {
- file_path: file_path.to_path_buf(),
- crc_path: if crc_path_buf.exists() {
- Some(crc_path_buf)
- } else {
- None
- },
- }
- },
-
- // Case 11: Invalid file, no resume, no task
- (CheckedFileState::Invalid, ResumeDataFileState::Missing, None) => {
- // Check if this is a partial download (file size < expected size)
- // If so, preserve it - don't delete partial downloads
- let is_partial = if let (Ok(metadata), Some(expected)) = (std::fs::metadata(file_path), expected_bytes) {
- let actual_size = metadata.len();
- actual_size < expected
- } else {
- false
- };
-
- if is_partial {
- tracing::info!("[RECONCILE] Case 11: Invalid file is partial download, preserving (not deleting)");
- // Keep the partial file, but remove invalid CRC cache
- // Don't create resume data - we can't without URLSession context
- // File will be handled when user initiates download again
- let _ = std::fs::remove_file(&crc_path_buf);
- InternalDownloadState::NotDownloaded
- } else {
- tracing::info!(
- "[RECONCILE] Case 11: Invalid file at full size → NotDownloaded (deleting corrupted file)"
- );
- let _ = std::fs::remove_file(file_path);
- let _ = std::fs::remove_file(&crc_path_buf);
- InternalDownloadState::NotDownloaded
- }
- },
-
- // Case 12: Invalid file, has resume, no task
- (CheckedFileState::Invalid, ResumeDataFileState::Exists, None) => {
- tracing::info!("[RECONCILE] Case 12: Invalid file, has resume → Paused");
-
- let _ = std::fs::remove_file(file_path);
- let _ = std::fs::remove_file(&crc_path_buf);
- InternalDownloadState::Paused {
- part_path: resume_path.to_path_buf(),
- }
- },
-
- // Cases 13-14: Invalid file + Running task
- (CheckedFileState::Invalid, _, Some(running_task))
- if matches!(running_task.state(), NSURLSessionTaskState::Running) =>
- {
- tracing::info!("[RECONCILE] Cases 13-14: Invalid file, running task → Downloading");
-
- let _ = std::fs::remove_file(file_path);
- let _ = std::fs::remove_file(&crc_path_buf);
- if resume_file == ResumeDataFileState::Exists {
- let _ = std::fs::remove_file(resume_path);
- }
- InternalDownloadState::Downloading {
- task: Retained::clone(running_task),
- }
- },
-
- // Case 15: Invalid, no resume, suspended task → Paused (produce resume)
- (CheckedFileState::Invalid, ResumeDataFileState::Missing, Some(suspended_task))
- if matches!(suspended_task.state(), NSURLSessionTaskState::Suspended) =>
- {
- tracing::info!("[RECONCILE] Case 15: Invalid, no resume, suspended → Paused (produce resume)");
-
- let _ = std::fs::remove_file(file_path);
- let _ = std::fs::remove_file(&crc_path_buf);
-
- if let Ok(resume_data) = suspended_task.cancel_by_producing_resume_data().await {
- let _ = resume_data.save_to_file(resume_path);
- InternalDownloadState::Paused {
- part_path: resume_path.to_path_buf(),
- }
- } else {
- InternalDownloadState::NotDownloaded
- }
- },
-
- // Case 16: Invalid, has resume, suspended task → Paused (keep better)
- (CheckedFileState::Invalid, ResumeDataFileState::Exists, Some(suspended_task))
- if matches!(suspended_task.state(), NSURLSessionTaskState::Suspended) =>
- {
- tracing::info!("[RECONCILE] Case 16: Invalid, has resume, suspended → Paused (compare resume)");
-
- let _ = std::fs::remove_file(file_path);
- let _ = std::fs::remove_file(&crc_path_buf);
-
- if let Ok(new_resume) = suspended_task.cancel_by_producing_resume_data().await {
- let existing_bytes = get_resume_bytes(resume_path).unwrap_or(0);
- let new_bytes = new_resume.bytes_received.unwrap_or(0);
-
- tracing::info!("[RECONCILE] Resume bytes: existing={} vs new={}", existing_bytes, new_bytes);
-
- if new_bytes > existing_bytes {
- let _ = new_resume.save_to_file(resume_path);
- }
- }
- InternalDownloadState::Paused {
- part_path: resume_path.to_path_buf(),
- }
- },
-
- // Cases 17-20: Invalid file + Completed/Canceling → NotDownloaded
- (CheckedFileState::Invalid, _, Some(finished_task)) => {
- tracing::info!("[RECONCILE] Cases 17-20: Invalid, completed/canceling → NotDownloaded");
-
- let _ = std::fs::remove_file(file_path);
- let _ = std::fs::remove_file(&crc_path_buf);
- if resume_file == ResumeDataFileState::Exists {
- let _ = std::fs::remove_file(resume_path);
- }
- finished_task.cancel();
- InternalDownloadState::NotDownloaded
- },
-
- // Case 21: No file, no resume, no task
- (CheckedFileState::Missing, ResumeDataFileState::Missing, None) => {
- tracing::info!("[RECONCILE] Case 21: No file, no resume, no task → NotDownloaded");
-
- let _ = std::fs::remove_file(&crc_path_buf);
- InternalDownloadState::NotDownloaded
- },
-
- // Case 22: No file, has resume, no task
- (CheckedFileState::Missing, ResumeDataFileState::Exists, None) => {
- tracing::info!("[RECONCILE] Case 22: No file, has resume → Paused");
-
- let _ = std::fs::remove_file(&crc_path_buf);
- InternalDownloadState::Paused {
- part_path: resume_path.to_path_buf(),
- }
- },
-
- // Cases 23-24: No file + Running task → Downloading
- (CheckedFileState::Missing, _, Some(running_task))
- if matches!(running_task.state(), NSURLSessionTaskState::Running) =>
- {
- tracing::info!("[RECONCILE] Cases 23-24: No file, running task → Downloading");
-
- let _ = std::fs::remove_file(&crc_path_buf);
- if resume_file == ResumeDataFileState::Exists {
- let _ = std::fs::remove_file(resume_path);
- }
- InternalDownloadState::Downloading {
- task: Retained::clone(running_task),
- }
- },
-
- // Case 25: No file, no resume, suspended task → Paused (produce resume)
- (CheckedFileState::Missing, ResumeDataFileState::Missing, Some(suspended_task))
- if matches!(suspended_task.state(), NSURLSessionTaskState::Suspended) =>
- {
- tracing::info!("[RECONCILE] Case 25: No file, no resume, suspended → Paused (produce resume)");
-
- let _ = std::fs::remove_file(&crc_path_buf);
-
- if let Ok(resume_data) = suspended_task.cancel_by_producing_resume_data().await {
- let _ = resume_data.save_to_file(resume_path);
- InternalDownloadState::Paused {
- part_path: resume_path.to_path_buf(),
- }
- } else {
- InternalDownloadState::NotDownloaded
- }
- },
-
- // Case 26: No file, has resume, suspended task → Paused (keep better)
- (CheckedFileState::Missing, ResumeDataFileState::Exists, Some(suspended_task))
- if matches!(suspended_task.state(), NSURLSessionTaskState::Suspended) =>
- {
- tracing::info!("[RECONCILE] Case 26: No file, has resume, suspended → Paused (compare resume)");
-
- let _ = std::fs::remove_file(&crc_path_buf);
-
- if let Ok(new_resume) = suspended_task.cancel_by_producing_resume_data().await {
- let existing_bytes = get_resume_bytes(resume_path).unwrap_or(0);
- let new_bytes = new_resume.bytes_received.unwrap_or(0);
-
- tracing::info!("[RECONCILE] Resume bytes: existing={} vs new={}", existing_bytes, new_bytes);
-
- if new_bytes > existing_bytes {
- let _ = new_resume.save_to_file(resume_path);
- }
- }
- InternalDownloadState::Paused {
- part_path: resume_path.to_path_buf(),
- }
- },
-
- // Cases 27-28: No file + Completed task → Keep task alive for delegate callback
- (CheckedFileState::Missing, _, Some(completed_task))
- if matches!(completed_task.state(), NSURLSessionTaskState::Completed) =>
- {
- tracing::info!("[RECONCILE] Cases 27-28: No file, completed task → Downloading (awaiting delegate)");
-
- let _ = std::fs::remove_file(&crc_path_buf);
- if resume_file == ResumeDataFileState::Exists {
- let _ = std::fs::remove_file(resume_path);
- }
- // Don't cancel! Keep task alive so delegate can move the file
- InternalDownloadState::Downloading {
- task: Retained::clone(completed_task),
- }
- },
-
- // Cases 29-30: No file + Canceling task → NotDownloaded
- (CheckedFileState::Missing, _, Some(canceling_task)) => {
- tracing::info!("[RECONCILE] Cases 29-30: No file, canceling → NotDownloaded");
-
- let _ = std::fs::remove_file(&crc_path_buf);
- if resume_file == ResumeDataFileState::Exists {
- let _ = std::fs::remove_file(resume_path);
- }
- canceling_task.cancel();
- InternalDownloadState::NotDownloaded
- },
- };
-
- tracing::info!("[RECONCILE] → Final state: {:?}", result);
-
- result
-}
-
-/// Extract bytes_received from a resume data file
-fn get_resume_bytes(resume_path: &Path) -> Option<u64> {
- URLSessionDownloadTaskResumeData::from_file(resume_path).ok().and_then(|data| data.bytes_received)
-}
diff --git a/crates/download-manager/src/managers/apple/url_session_task_ext.rs b/crates/download-manager/src/managers/apple/url_session_task_ext.rs
deleted file mode 100644
index e7accaf94..000000000
--- a/crates/download-manager/src/managers/apple/url_session_task_ext.rs
+++ /dev/null
@@ -1,39 +0,0 @@
-use crate::{TaskID, prelude::*};
-
-#[allow(unused)]
-pub trait UrlSessionTaskExt {
- fn task_identifier(&self) -> TaskID;
- fn task_description(&self) -> Option<String>;
- fn set_task_description(
- &self,
- s: &str,
- );
- fn url_string(&self) -> Option<String>;
-}
-
-impl UrlSessionTaskExt for NSURLSessionTask {
- fn task_identifier(&self) -> TaskID {
- unsafe { msg_send![self, taskIdentifier] }
- }
-
- fn task_description(&self) -> Option<String> {
- let desc_opt = self.taskDescription();
- desc_opt.map(|d| d.to_string())
- }
-
- fn set_task_description(
- &self,
- s: &str,
- ) {
- let ns = NSString::from_str(s);
- self.setTaskDescription(Some(&ns));
- }
-
- fn url_string(&self) -> Option<String> {
- self.originalRequest()
- .or_else(|| self.currentRequest())
- .and_then(|req| req.URL())
- .and_then(|url| url.absoluteString())
- .map(|s| s.to_string())
- }
-}
diff --git a/crates/download-manager/src/managers/universal/async_fetcher_config.rs b/crates/download-manager/src/managers/universal/async_fetcher_config.rs
deleted file mode 100644
index 076763010..000000000
--- a/crates/download-manager/src/managers/universal/async_fetcher_config.rs
+++ /dev/null
@@ -1,55 +0,0 @@
-#[derive(Debug, Clone)]
-pub struct AsyncFetcherConfig {
- pub connections_per_file: u16,
- pub retries: u16,
- pub progress_interval_ms: u64,
-}
-
-impl Default for AsyncFetcherConfig {
- fn default() -> Self {
- Self {
- connections_per_file: 4,
- retries: 3,
- progress_interval_ms: 500,
- }
- }
-}
-
-impl AsyncFetcherConfig {
- #[allow(unused)]
- pub fn new(
- connections_per_file: u16,
- retries: u16,
- progress_interval_ms: u64,
- ) -> Self {
- Self {
- connections_per_file,
- retries,
- progress_interval_ms,
- }
- }
-
- pub fn with_connections_per_file(
- mut self,
- connections_per_file: u16,
- ) -> Self {
- self.connections_per_file = connections_per_file;
- self
- }
-
- pub fn with_retries(
- mut self,
- retries: u16,
- ) -> Self {
- self.retries = retries;
- self
- }
-
- pub fn with_progress_interval_ms(
- mut self,
- progress_interval_ms: u64,
- ) -> Self {
- self.progress_interval_ms = progress_interval_ms;
- self
- }
-}
diff --git a/crates/download-manager/src/managers/universal/async_fetcher_download_manager.rs b/crates/download-manager/src/managers/universal/async_fetcher_download_manager.rs
deleted file mode 100644
index e54190266..000000000
--- a/crates/download-manager/src/managers/universal/async_fetcher_download_manager.rs
+++ /dev/null
@@ -1,165 +0,0 @@
-use tokio_stream::wrappers::BroadcastStream as TokioBroadcastStream;
-
-use crate::{
- Arc, DownloadError, DownloadEvent, FileCheck, FileDownloadManager, FileDownloadTask as FileDownloadTaskTrait, Path,
- PathBuf, SharedDownloadEventSender, TokioHandle, compute_download_id,
- download_manager_state::DownloadManagerState,
- managers::universal::{AsyncFetcherConfig, FileDownloadTask},
-};
-
-#[derive(Debug, Clone)]
-pub struct AsyncFetcherDownloadManager {
- state: DownloadManagerState,
- pub config: AsyncFetcherConfig,
-}
-
-impl AsyncFetcherDownloadManager {
- #[allow(unused)]
- pub fn manager_id(&self) -> &str {
- &self.state.manager_id
- }
-
- #[allow(unused)]
- pub async fn new(
- config: AsyncFetcherConfig,
- tokio_handle: TokioHandle,
- ) -> Result<Self, DownloadError> {
- let state = DownloadManagerState::new("universal", tokio_handle);
-
- let manager = Self {
- state,
- config,
- };
-
- tracing::debug!(
- "[DOWNLOAD_MANAGER] AsyncFetcherDownloadManager created with tokio handle, manager_id={}",
- manager.manager_id()
- );
-
- Ok(manager)
- }
-}
-
-#[async_trait::async_trait]
-impl FileDownloadManager for AsyncFetcherDownloadManager {
- #[allow(unused)]
- fn manager_id(&self) -> &str {
- &self.state.manager_id
- }
-
- fn subscribe_to_all_downloads(&self) -> TokioBroadcastStream<DownloadEvent> {
- self.state.subscribe_to_all_downloads()
- }
-
- fn global_broadcast_sender(&self) -> SharedDownloadEventSender {
- self.state.global_broadcast_sender.clone()
- }
-
- async fn get_all_file_tasks(&self) -> Result<Vec<Arc<dyn FileDownloadTaskTrait>>, DownloadError> {
- self.state.get_all_file_tasks().await
- }
-
- async fn file_download_task(
- &self,
- source_url: &String,
- destination_path: &Path,
- file_check: FileCheck,
- expected_bytes: Option<u64>,
- ) -> Result<Arc<dyn FileDownloadTaskTrait>, DownloadError> {
- use crate::managers::universal::async_fetcher_state_reducer::{
- check_crc_file_exists, check_file_exists, check_part_file_exists, reconcile_to_internal_state,
- reduce_to_checked_file_state, reduce_to_file_download_state,
- };
-
- let download_id = compute_download_id(source_url, destination_path);
-
- {
- let task_cache_guard = self.state.task_cache.lock().await;
- if let Some(cached_task) = task_cache_guard.get(&download_id) {
- return Ok(cached_task.clone());
- }
- }
-
- // Lock presence will be reflected by reduction, no error on create
-
- let downloaded_file_state = check_file_exists(destination_path);
- let crc_file_path = PathBuf::from(format!("{}.crc", destination_path.display()));
- let crc_file_state = check_crc_file_exists(&crc_file_path);
- let part_file_path = destination_path.with_extension("part");
- let part_file_state = check_part_file_exists(&part_file_path);
-
- let expected_crc = match &file_check {
- FileCheck::CRC(crc_hash) => Some(crc_hash.as_str()),
- FileCheck::None => None,
- };
-
- tracing::info!(
- "[MANAGER:AF] reduce_to_checked_file_state inputs: downloaded={:?}, crc={:?}, dest={}, expected_crc={:?}",
- downloaded_file_state,
- crc_file_state,
- destination_path.display(),
- expected_crc
- );
- let checked_file_state =
- reduce_to_checked_file_state(downloaded_file_state, crc_file_state, destination_path, expected_crc);
-
- tracing::info!(
- "[MANAGER:AF] reconcile_to_internal_state inputs: checked={:?}, part={:?}, dest={}, crc_path={}, part_path={}, expected_bytes={:?}",
- checked_file_state,
- part_file_state,
- destination_path.display(),
- crc_file_path.display(),
- part_file_path.display(),
- expected_bytes
- );
- let internal_state = reconcile_to_internal_state(
- checked_file_state,
- part_file_state,
- destination_path,
- &crc_file_path,
- &part_file_path,
- expected_bytes,
- expected_crc,
- )
- .await;
-
- tracing::info!(
- "[MANAGER:AF] reduce_to_file_download_state inputs: checked={:?}, part={:?}, dest={}, part_path={}, expected_bytes={:?}, manager_id={}",
- checked_file_state,
- part_file_state,
- destination_path.display(),
- part_file_path.display(),
- expected_bytes,
- self.state.manager_id
- );
- let file_download_state = reduce_to_file_download_state(
- checked_file_state,
- part_file_state,
- destination_path,
- &part_file_path,
- expected_bytes,
- &self.state.manager_id,
- );
-
- tracing::info!("[MANAGER:AF] initial states: internal={:?}, display={:?}", internal_state, file_download_state);
-
- let file_task: Arc<dyn FileDownloadTaskTrait> = Arc::new(FileDownloadTask::new(
- download_id,
- source_url.clone(),
- destination_path.to_path_buf(),
- file_check,
- self.state.manager_id.clone(),
- expected_bytes,
- internal_state,
- file_download_state,
- self.config.clone(),
- self.state.tokio_handle.clone(),
- ));
-
- tracing::debug!("[MANAGER] Starting listener for download_id={}", download_id);
- file_task.start_listening((*self.state.global_broadcast_sender).clone()).await;
-
- self.state.task_cache.lock().await.insert(download_id, file_task.clone());
- Ok(file_task)
- }
-}
diff --git a/crates/download-manager/src/managers/universal/async_fetcher_file_download_task.rs b/crates/download-manager/src/managers/universal/async_fetcher_file_download_task.rs
deleted file mode 100644
index ffedbbcf1..000000000
--- a/crates/download-manager/src/managers/universal/async_fetcher_file_download_task.rs
+++ /dev/null
@@ -1,738 +0,0 @@
-use async_fetcher::{FetchEvent, Fetcher, Source};
-use async_shutdown::ShutdownManager;
-use tokio::{
- sync::{
- mpsc,
- watch::{Receiver as TokioWatchReceiver, Sender as TokioWatchSender, channel as tokio_watch_channel},
- },
- time::{Duration as TokioDuration, sleep as tokio_sleep},
-};
-use tokio_stream::{
- StreamExt as TokioStreamExt,
- wrappers::{BroadcastStream as TokioBroadcastStream, UnboundedReceiverStream},
-};
-
-use crate::{
- Arc, DownloadError, DownloadEventSender, DownloadId, FileCheck, FileDownloadEvent, FileDownloadPhase,
- FileDownloadState, FileDownloadTask as FileDownloadTaskTrait, InternalDownloadState, LockFileState, PathBuf,
- StateTransitionAction, TokioBroadcastSender, TokioHandle, TokioJoinHandle, TokioMutex, Uuid, acquire_lock,
- calculate_and_verify_crc, check_lock_file, crc_utils, fs, managers::universal::AsyncFetcherConfig, release_lock,
- tokio_broadcast_channel,
-};
-
-pub struct FileDownloadTask {
- pub download_id: Uuid,
- pub source_url: String,
- pub destination: PathBuf,
- pub file_check: FileCheck,
- pub manager_id: String,
- expected_bytes: Option<u64>,
- state: Arc<TokioMutex<FileDownloadState>>,
- broadcast_sender: TokioBroadcastSender<FileDownloadState>,
- pub internal_state: TokioMutex<InternalDownloadState<()>>,
- config: AsyncFetcherConfig,
- shutdown_manager: Arc<TokioMutex<Option<ShutdownManager<()>>>>,
- fetcher_task: Arc<TokioMutex<Option<TokioJoinHandle<()>>>>,
- listener_task: Arc<TokioMutex<Option<TokioJoinHandle<()>>>>,
- tokio_handle: TokioHandle,
- completed_tx: TokioWatchSender<bool>,
- _completed_rx: TokioWatchReceiver<bool>,
-}
-
-impl std::fmt::Debug for FileDownloadTask {
- fn fmt(
- &self,
- f: &mut std::fmt::Formatter<'_>,
- ) -> std::fmt::Result {
- f.debug_struct("FileDownloadTask")
- .field("download_id", &self.download_id)
- .field("source_url", &self.source_url)
- .field("destination", &self.destination)
- .field("file_check", &self.file_check)
- .finish()
- }
-}
-
-impl FileDownloadTask {
- pub fn new(
- download_id: Uuid,
- source_url: String,
- destination: PathBuf,
- file_check: FileCheck,
- manager_id: String,
- expected_bytes: Option<u64>,
- internal_state: InternalDownloadState<()>,
- initial_state: FileDownloadState,
- config: AsyncFetcherConfig,
- tokio_handle: TokioHandle,
- ) -> Self {
- let (broadcast_sender, _receiver) = tokio_broadcast_channel::<FileDownloadState>(64);
- let (completed_tx, completed_rx) = tokio_watch_channel(false);
- Self {
- download_id,
- source_url,
- destination,
- file_check,
- manager_id,
- expected_bytes,
- state: Arc::new(TokioMutex::new(initial_state)),
- broadcast_sender,
- internal_state: TokioMutex::new(internal_state),
- config,
- shutdown_manager: Arc::new(TokioMutex::new(None)),
- fetcher_task: Arc::new(TokioMutex::new(None)),
- listener_task: Arc::new(TokioMutex::new(None)),
- tokio_handle,
- completed_tx,
- _completed_rx: completed_rx,
- }
- }
-
- fn lock_path(&self) -> PathBuf {
- PathBuf::from(format!("{}.lock", self.destination.display()))
- }
-
- async fn reconcile_lock_state(&self) {
- let lock_path = self.lock_path();
- let lock_state = check_lock_file(&lock_path, &self.manager_id, std::process::id());
-
- let is_downloading = {
- let guard = self.internal_state.lock().await;
- matches!(*guard, InternalDownloadState::Downloading { .. })
- };
-
- if is_downloading {
- match lock_state {
- LockFileState::OwnedByUs(_) => {},
- LockFileState::Missing | LockFileState::OwnedBySameAppOldProcess(_) | LockFileState::Stale(_) => {
- if let Err(e) = acquire_lock(&self.tokio_handle, &lock_path, &self.manager_id).await {
- tracing::warn!(
- "[FILE_TASK] Failed to acquire lock during reconciliation {}: {}",
- lock_path.display(),
- e
- );
- }
- },
- LockFileState::OwnedByOtherApp(info) => {
- tracing::warn!("[FILE_TASK] Another manager owns the lock ({}).", info.manager_id);
- },
- }
- } else {
- match lock_state {
- LockFileState::OwnedByUs(_) | LockFileState::OwnedBySameAppOldProcess(_) | LockFileState::Stale(_) => {
- let _ = release_lock(&self.tokio_handle, &lock_path).await;
- },
- _ => {},
- }
- }
- }
-
- pub fn broadcast_sender(&self) -> TokioBroadcastSender<FileDownloadState> {
- self.broadcast_sender.clone()
- }
-
- pub async fn update_state_and_broadcast(
- &self,
- new_state: FileDownloadState,
- ) {
- let mut state_guard = self.state.lock().await;
- let current_bytes = state_guard.downloaded_bytes;
- let new_bytes = new_state.downloaded_bytes;
-
- tracing::debug!(
- "[FILE_TASK] update_state_and_broadcast: id={}, current={:?}({} bytes), new={:?}({} bytes)",
- self.download_id,
- state_guard.phase,
- current_bytes,
- new_state.phase,
- new_bytes
- );
-
- if new_bytes < current_bytes
- && !matches!(
- new_state.phase,
- FileDownloadPhase::Downloaded | FileDownloadPhase::Error(_) | FileDownloadPhase::NotDownloaded
- )
- {
- tracing::debug!("[FILE_TASK] ⚠️ Rejected backwards progress: {} < {}", new_bytes, current_bytes);
- return;
- }
-
- *state_guard = new_state.clone();
- drop(state_guard);
-
- let receiver_count = self.broadcast_sender.receiver_count();
-
- if receiver_count == 0 {
- tracing::debug!("[FILE_TASK] No receivers active, skipping broadcast (likely shutdown)");
- return;
- }
-
- tracing::debug!("[FILE_TASK] ✓ State updated and broadcasting to {} receiver(s)", receiver_count);
-
- let _ = self.broadcast_sender.send(new_state.clone());
- }
-
- pub async fn download(&self) -> Result<(), DownloadError> {
- let mut internal_state_guard = self.internal_state.lock().await;
- internal_state_guard.can_transition(StateTransitionAction::Download)?;
-
- tracing::debug!(
- "[FILE_TASK] download() called: id={}, internal_state={:?}",
- self.download_id,
- match &*internal_state_guard {
- InternalDownloadState::NotDownloaded => "NotDownloaded",
- InternalDownloadState::Downloading {
- ..
- } => "Downloading",
- InternalDownloadState::Paused {
- ..
- } => "Paused",
- InternalDownloadState::Downloaded {
- ..
- } => "Downloaded",
- }
- );
-
- match &*internal_state_guard {
- InternalDownloadState::NotDownloaded
- | InternalDownloadState::Paused {
- ..
- } => {
- let part_path = self.destination.with_extension("part");
-
- if let Some(parent) = self.destination.parent() {
- tokio::fs::create_dir_all(parent).await.map_err(|e| DownloadError::IOError(e.to_string()))?;
- }
-
- let resume_from_bytes = if part_path.exists() {
- tokio::fs::metadata(&part_path).await.ok().map(|m| m.len()).unwrap_or(0)
- } else {
- 0
- };
-
- let shutdown = ShutdownManager::new();
- *self.shutdown_manager.lock().await = Some(shutdown.clone());
-
- let (events_tx, events_rx) = mpsc::unbounded_channel();
-
- let fetcher = Fetcher::<()>::default()
- .connections_per_file(self.config.connections_per_file)
- .retries(self.config.retries)
- .progress_interval(self.config.progress_interval_ms)
- .shutdown(shutdown.clone())
- .events(events_tx)
- .build();
-
- let source = Source::builder(Arc::from(self.destination.clone()), self.source_url.clone().into())
- .partial(Arc::from(part_path.clone()))
- .build();
-
- let download_id = self.download_id;
- let destination = self.destination.clone();
- let expected_crc = match &self.file_check {
- FileCheck::CRC(crc) => Some(crc.clone()),
- FileCheck::None => None,
- };
- let expected_bytes = self.expected_bytes;
- let broadcast_sender = self.broadcast_sender.clone();
-
- *internal_state_guard = InternalDownloadState::Downloading {
- task: (),
- };
- drop(internal_state_guard);
-
- let downloading_state = FileDownloadState::downloading(resume_from_bytes, 0);
- self.update_state_and_broadcast(downloading_state).await;
- // Ensure lock acquired once downloading
- self.reconcile_lock_state().await;
-
- let _task = self.tokio_handle.spawn(async move {
- tracing::info!("[FETCHER] Event processing task started for id={}", download_id);
- let mut total_size: Option<u64> = None;
- let mut downloaded_bytes = resume_from_bytes;
-
- let mut evt_rx = UnboundedReceiverStream::new(events_rx);
- while let Some((_path, _data, evt)) = evt_rx.next().await {
- tracing::debug!(
- "[FETCHER] Received event: id={}, event={:?}",
- download_id,
- std::mem::discriminant(&evt)
- );
- match evt {
- FetchEvent::Fetching => {
- tracing::debug!("[FETCHER] Started fetching: {}", download_id);
- },
- FetchEvent::ContentLength(len) => {
- // Content-Length is the remaining bytes to download
- // Add resume_from_bytes to get total file size
- total_size = Some(len + resume_from_bytes);
- tracing::debug!(
- "[FETCHER] Content length: {} bytes (remaining: {}, resumed: {})",
- len + resume_from_bytes,
- len,
- resume_from_bytes
- );
- },
- FetchEvent::Progress(bytes) => {
- // Progress reports incremental bytes since last update
- // We need to accumulate them to get total downloaded
- downloaded_bytes += bytes;
-
- let total = total_size.unwrap_or(expected_bytes.unwrap_or(downloaded_bytes));
- let progress_state = FileDownloadState::downloading(downloaded_bytes, total);
- let receiver_count = broadcast_sender.receiver_count();
- tracing::info!(
- "[FETCHER] Progress: id={}, bytes={}/{}, receivers={}",
- download_id,
- downloaded_bytes,
- total,
- receiver_count
- );
- let _ = broadcast_sender.send(progress_state);
- },
- FetchEvent::Fetched => {
- tracing::debug!("[FETCHER] Download completed: {}", download_id);
-
- // async-fetcher automatically moves the .part file to destination
- // We just need to wait a moment for the file system to catch up
- if !destination.exists() {
- tracing::warn!(
- "[FETCHER] Destination file not yet visible after Fetched event: {}",
- destination.display()
- );
- // Give a brief moment for filesystem operations to complete
- tokio_sleep(TokioDuration::from_millis(100)).await;
-
- if !destination.exists() {
- tracing::error!(
- "[FETCHER] Destination file still missing after wait: {}",
- destination.display()
- );
- let error_state = FileDownloadState {
- phase: FileDownloadPhase::Error(
- "Download completed but file not found at destination".to_string(),
- ),
- downloaded_bytes: 0,
- total_bytes: 0,
- };
- let _ = broadcast_sender.send(error_state);
- return;
- }
- }
-
- if let Some(ref crc) = expected_crc {
- tracing::debug!(
- "[FETCHER] Verifying CRC for {}, expected: {}",
- destination.display(),
- crc
- );
-
- if let Ok(metadata) = std::fs::metadata(&destination) {
- tracing::debug!("[FETCHER] File size: {} bytes", metadata.len());
- }
-
- match calculate_and_verify_crc(&destination, crc) {
- Ok(true) => {
- tracing::debug!("[FETCHER] CRC verification passed");
-
- // Save CRC file for future verification
- if let Err(e) = crc_utils::save_crc_file(&destination, crc) {
- tracing::warn!("[FETCHER] Failed to save CRC file: {}", e);
- }
-
- let final_bytes = downloaded_bytes;
- let total = total_size.unwrap_or(expected_bytes.unwrap_or(final_bytes));
- let completed_state = FileDownloadState {
- phase: FileDownloadPhase::Downloaded,
- downloaded_bytes: final_bytes,
- total_bytes: total,
- };
- let _ = broadcast_sender.send(completed_state);
- },
- Ok(false) => {
- tracing::error!("[FETCHER] CRC verification failed - checksum mismatch");
- let _ = fs::remove_file(&destination);
- let error_state = FileDownloadState {
- phase: FileDownloadPhase::Error("CRC verification failed".to_string()),
- downloaded_bytes: 0,
- total_bytes: 0,
- };
- let _ = broadcast_sender.send(error_state);
- },
- Err(e) => {
- tracing::error!("[FETCHER] CRC verification error: {}", e);
- let _ = fs::remove_file(&destination);
- let error_state = FileDownloadState {
- phase: FileDownloadPhase::Error(format!(
- "CRC verification error: {}",
- e
- )),
- downloaded_bytes: 0,
- total_bytes: 0,
- };
- let _ = broadcast_sender.send(error_state);
- },
- }
- } else {
- let final_bytes = downloaded_bytes;
- let total = total_size.unwrap_or(expected_bytes.unwrap_or(final_bytes));
- let completed_state = FileDownloadState {
- phase: FileDownloadPhase::Downloaded,
- downloaded_bytes: final_bytes,
- total_bytes: total,
- };
- let _ = broadcast_sender.send(completed_state);
- }
- },
- FetchEvent::Retrying => {
- tracing::debug!("[FETCHER] Retrying download: {}", download_id);
- },
- }
- }
- });
-
- let source_stream = futures_util::stream::once(async { (source, Arc::new(())) });
- let fetch_task = fetcher.stream_from(source_stream, 1);
- let broadcast_clone = self.broadcast_sender.clone();
- let download_id_clone = self.download_id;
-
- tracing::info!("[FETCHER] Starting fetch task for id={}", download_id);
-
- self.tokio_handle.spawn(async move {
- futures_util::pin_mut!(fetch_task);
- while let Some((_path, _data, result)) = fetch_task.next().await {
- if let Err(e) = result {
- tracing::error!("[FETCHER] Error downloading {}: {}", download_id_clone, e);
- let error_state = FileDownloadState {
- phase: FileDownloadPhase::Error(e.to_string()),
- downloaded_bytes: 0,
- total_bytes: 0,
- };
- let _ = broadcast_clone.send(error_state);
- }
- }
- });
-
- Ok(())
- },
- InternalDownloadState::Downloading {
- ..
- } => {
- tracing::debug!("[FILE_TASK] Already downloading");
- Ok(())
- },
- InternalDownloadState::Downloaded {
- ..
- } => {
- tracing::debug!("[FILE_TASK] Already downloaded, broadcasting current state");
- let current_state = self.state.lock().await.clone();
- self.update_state_and_broadcast(current_state).await;
- let _ = self.completed_tx.send(true);
- Ok(())
- },
- }
- }
-
- pub async fn pause(&self) -> Result<(), DownloadError> {
- let mut internal_state_guard = self.internal_state.lock().await;
- internal_state_guard.can_transition(StateTransitionAction::Pause)?;
-
- tracing::debug!(
- "[FILE_TASK] pause() called: id={}, internal_state={:?}",
- self.download_id,
- match &*internal_state_guard {
- InternalDownloadState::NotDownloaded => "NotDownloaded",
- InternalDownloadState::Downloading {
- ..
- } => "Downloading",
- InternalDownloadState::Paused {
- ..
- } => "Paused",
- InternalDownloadState::Downloaded {
- ..
- } => "Downloaded",
- }
- );
-
- match &*internal_state_guard {
- InternalDownloadState::Downloading {
- ..
- } => {
- tracing::debug!("[FILE_TASK] Triggering shutdown for pause...");
-
- if let Some(shutdown) = self.shutdown_manager.lock().await.as_ref() {
- let _ = shutdown.trigger_shutdown(());
- }
-
- let part_path = self.destination.with_extension("part");
- let downloaded_bytes = tokio::fs::metadata(&part_path).await.ok().map(|m| m.len()).unwrap_or(0);
- let total_bytes = self.expected_bytes.unwrap_or(downloaded_bytes);
-
- *internal_state_guard = InternalDownloadState::Paused {
- part_path: part_path.clone(),
- };
- drop(internal_state_guard);
-
- let paused_state = FileDownloadState::paused(downloaded_bytes, total_bytes);
- self.update_state_and_broadcast(paused_state).await;
-
- if let Some(task) = self.fetcher_task.lock().await.take() {
- task.abort();
- let _ = task.await;
- }
- // Reconcile lock (release)
- self.reconcile_lock_state().await;
-
- Ok(())
- },
- InternalDownloadState::Downloaded {
- ..
- } => Ok(()),
- _ => Ok(()),
- }
- }
-
- pub async fn cancel(&self) -> Result<(), DownloadError> {
- let mut internal_state_guard = self.internal_state.lock().await;
- internal_state_guard.can_transition(StateTransitionAction::Cancel)?;
-
- tracing::debug!(
- "[FILE_TASK] cancel() called: id={}, internal_state before={:?}",
- self.download_id,
- match &*internal_state_guard {
- InternalDownloadState::NotDownloaded => "NotDownloaded",
- InternalDownloadState::Downloading {
- ..
- } => "Downloading",
- InternalDownloadState::Paused {
- ..
- } => "Paused",
- InternalDownloadState::Downloaded {
- ..
- } => "Downloaded",
- }
- );
-
- match &*internal_state_guard {
- InternalDownloadState::Downloading {
- ..
- } => {
- if let Some(shutdown) = self.shutdown_manager.lock().await.as_ref() {
- let _ = shutdown.trigger_shutdown(());
- }
-
- if let Some(task) = self.fetcher_task.lock().await.take() {
- task.abort();
- let _ = task.await;
- }
- },
- InternalDownloadState::Paused {
- ..
- } => {},
- _ => {},
- }
-
- let part_path = self.destination.with_extension("part");
- let _ = fs::remove_file(&part_path);
-
- *internal_state_guard = InternalDownloadState::NotDownloaded;
- drop(internal_state_guard);
-
- tracing::debug!("[FILE_TASK] cancel() completed: id={}, internal_state now=NotDownloaded", self.download_id);
-
- let not_downloaded_state = FileDownloadState::not_downloaded(0);
- self.update_state_and_broadcast(not_downloaded_state).await;
- // Ensure lock released on cancel
- self.reconcile_lock_state().await;
- Ok(())
- }
-
- pub async fn state(&self) -> FileDownloadState {
- let state = self.state.lock().await.clone();
- state
- }
-
- pub async fn progress(&self) -> Result<TokioBroadcastStream<FileDownloadState>, DownloadError> {
- Ok(TokioBroadcastStream::new(self.broadcast_sender.subscribe()))
- }
-
- pub async fn wait(&self) {
- let mut rx = self.completed_tx.subscribe();
- if *rx.borrow() {
- return;
- }
- let _ = rx.changed().await;
- }
-
- pub async fn start_listening(
- &self,
- global_broadcast: DownloadEventSender,
- ) {
- let mut listener_guard = self.listener_task.lock().await;
- if listener_guard.is_some() {
- tracing::debug!("[FILE_TASK] start_listening: Already listening for id={}", self.download_id);
- return;
- }
-
- tracing::debug!("[FILE_TASK] start_listening: Starting listener for id={}", self.download_id);
-
- let download_id = self.download_id;
- let destination = self.destination.clone();
- let mut local_stream = TokioBroadcastStream::new(self.broadcast_sender.subscribe());
- let tokio_handle = self.tokio_handle.clone();
- let tokio_handle_for_closure = tokio_handle.clone();
-
- let manager_id = self.manager_id.clone();
- let completed = self.completed_tx.clone();
- let handle = tokio_handle.spawn(async move {
- tracing::debug!("[FILE_TASK] Listener task started for id={}", download_id);
-
- let mut last_downloaded_bytes = 0u64;
-
- while let Some(result) = tokio_stream::StreamExt::next(&mut local_stream).await {
- if let Ok(state) = result {
- tracing::debug!("[FILE_TASK] Forwarding event for download_id={}: {:?}", download_id, state.phase);
-
- match state.phase {
- FileDownloadPhase::Downloading => {
- let bytes_written = state.downloaded_bytes.saturating_sub(last_downloaded_bytes);
- last_downloaded_bytes = state.downloaded_bytes;
- let event = FileDownloadEvent::ProgressUpdate {
- bytes_written,
- total_bytes_written: state.downloaded_bytes,
- total_bytes_expected: state.total_bytes,
- };
- let _ = global_broadcast.send((download_id, event));
- },
- FileDownloadPhase::Downloaded => {
- // Reconcile lock after completion
- let lock_path = PathBuf::from(format!("{}.lock", destination.display()));
- let _ = tokio_handle_for_closure
- .clone()
- .spawn(async move {
- let lock_state = check_lock_file(&lock_path, &manager_id, std::process::id());
- match lock_state {
- LockFileState::OwnedByUs(_)
- | LockFileState::OwnedBySameAppOldProcess(_)
- | LockFileState::Stale(_) => {
- let _ = release_lock(&tokio_handle_for_closure, &lock_path).await;
- },
- _ => {},
- }
- })
- .await;
-
- let event = FileDownloadEvent::DownloadCompleted {
- tmp_path: destination.clone(),
- final_destination: destination.clone(),
- };
- let _ = global_broadcast.send((download_id, event));
- tracing::debug!("[FILE_TASK] Sent DownloadCompleted event for id={}", download_id);
-
- let _ = completed.send(true);
- break;
- },
- FileDownloadPhase::Error(msg) => {
- // Reconcile lock on error
- let lock_path = PathBuf::from(format!("{}.lock", destination.display()));
- let _ = tokio_handle_for_closure
- .clone()
- .spawn(async move {
- let lock_state = check_lock_file(&lock_path, &manager_id, std::process::id());
- match lock_state {
- LockFileState::OwnedByUs(_)
- | LockFileState::OwnedBySameAppOldProcess(_)
- | LockFileState::Stale(_) => {
- let _ = release_lock(&tokio_handle_for_closure, &lock_path).await;
- },
- _ => {},
- }
- })
- .await;
-
- let event = FileDownloadEvent::Error {
- message: msg,
- };
- let _ = global_broadcast.send((download_id, event));
-
- let _ = completed.send(true);
- break;
- },
- _ => {},
- }
- }
- }
-
- tracing::debug!("[FILE_TASK] Listener task ended for id={}", download_id);
- });
-
- *listener_guard = Some(handle);
- }
-
- pub async fn stop_listening(&self) {
- let mut listener_guard = self.listener_task.lock().await;
- if let Some(handle) = listener_guard.take() {
- handle.abort();
- let _ = handle.await;
- }
- }
-}
-
-#[async_trait::async_trait]
-impl FileDownloadTaskTrait for FileDownloadTask {
- fn download_id(&self) -> DownloadId {
- self.download_id
- }
-
- fn source_url(&self) -> &str {
- &self.source_url
- }
-
- fn destination(&self) -> &std::path::Path {
- &self.destination
- }
-
- fn file_check(&self) -> &FileCheck {
- &self.file_check
- }
-
- fn broadcast_sender(&self) -> TokioBroadcastSender<FileDownloadState> {
- self.broadcast_sender()
- }
-
- async fn download(&self) -> Result<(), DownloadError> {
- self.download().await
- }
-
- async fn pause(&self) -> Result<(), DownloadError> {
- self.pause().await
- }
-
- async fn cancel(&self) -> Result<(), DownloadError> {
- self.cancel().await
- }
-
- async fn state(&self) -> FileDownloadState {
- self.state().await
- }
-
- async fn progress(&self) -> Result<TokioBroadcastStream<FileDownloadState>, DownloadError> {
- self.progress().await
- }
-
- async fn start_listening(
- &self,
- global_broadcast: DownloadEventSender,
- ) {
- self.start_listening(global_broadcast).await
- }
-
- async fn stop_listening(&self) {
- self.stop_listening().await
- }
-
- async fn wait(&self) {
- self.wait().await
- }
-}
diff --git a/crates/download-manager/src/managers/universal/async_fetcher_state_reducer.rs b/crates/download-manager/src/managers/universal/async_fetcher_state_reducer.rs
deleted file mode 100644
index 291a8544f..000000000
--- a/crates/download-manager/src/managers/universal/async_fetcher_state_reducer.rs
+++ /dev/null
@@ -1,279 +0,0 @@
-use crate::{
- CheckedFileState, FileDownloadPhase, FileDownloadState, FileState, InternalDownloadState, LockFileState, Path,
- calculate_and_verify_crc, check_lock_file, crc_utils::save_crc_file, fs,
-};
-
-pub type PartFileState = FileState;
-
-pub fn check_file_exists(path: &Path) -> FileState {
- if path.exists() && path.is_file() {
- tracing::debug!("[AF REDUCE] check_file_exists: EXISTS path={}", path.display());
- FileState::Exists
- } else {
- tracing::debug!("[AF REDUCE] check_file_exists: MISSING path={}", path.display());
- FileState::Missing
- }
-}
-
-pub fn check_crc_file_exists(path: &Path) -> FileState {
- if path.exists() && path.is_file() {
- FileState::Exists
- } else {
- FileState::Missing
- }
-}
-
-pub fn check_part_file_exists(path: &Path) -> PartFileState {
- if path.exists() && path.is_file() {
- FileState::Exists
- } else {
- FileState::Missing
- }
-}
-
-pub fn reduce_to_checked_file_state(
- downloaded_file_state: FileState,
- crc_file_state: FileState,
- destination: &Path,
- expected_crc: Option<&str>,
-) -> CheckedFileState {
- tracing::info!(
- "[AF REDUCE_CHECKED] file={:?}, crc_file={:?}, expected_crc={:?}, path={}",
- downloaded_file_state,
- crc_file_state,
- expected_crc,
- destination.display()
- );
- match (downloaded_file_state, crc_file_state, expected_crc) {
- // File missing - nothing to validate
- (FileState::Missing, _, _) => CheckedFileState::Missing,
-
- // File exists with CRC file - use cached CRC (fast path)
- (FileState::Exists, FileState::Exists, Some(expected_crc_value)) => {
- let crc_path = format!("{}.crc", destination.display());
- if let Ok(saved_crc) = std::fs::read_to_string(&crc_path) {
- if saved_crc.trim() == expected_crc_value {
- tracing::debug!("[AF REDUCE_CHECKED] ✓ Using cached CRC for {}", destination.display());
- // Cached CRC matches - fast path!
- CheckedFileState::Valid
- } else {
- tracing::warn!(
- "[AF REDUCE_CHECKED] CRC mismatch in cache, recalculating for {}",
- destination.display()
- );
- // CRC mismatch in cache - recalculate to be sure
- if let Ok(is_valid) = calculate_and_verify_crc(destination, expected_crc_value) {
- if is_valid {
- tracing::debug!(
- "[AF REDUCE_CHECKED] ✓ CRC recalculation passed; updating cache for {}",
- destination.display()
- );
- // Update cache with correct value
- let _ = save_crc_file(destination, expected_crc_value);
- CheckedFileState::Valid
- } else {
- tracing::warn!(
- "[AF REDUCE_CHECKED] ✗ CRC recalculation failed for {}",
- destination.display()
- );
- CheckedFileState::Invalid
- }
- } else {
- tracing::warn!("[AF REDUCE_CHECKED] ✗ CRC recalculation errored for {}", destination.display());
- CheckedFileState::Invalid
- }
- }
- } else {
- tracing::warn!(
- "[AF REDUCE_CHECKED] Failed to read cached CRC, recalculating for {}",
- destination.display()
- );
- // Failed to read cached CRC - recalculate
- if let Ok(is_valid) = calculate_and_verify_crc(destination, expected_crc_value) {
- if is_valid {
- tracing::debug!(
- "[AF REDUCE_CHECKED] ✓ CRC recalculation passed; caching for {}",
- destination.display()
- );
- // Save the cache for next time
- let _ = save_crc_file(destination, expected_crc_value);
- CheckedFileState::Valid
- } else {
- tracing::warn!("[AF REDUCE_CHECKED] ✗ CRC recalculation failed for {}", destination.display());
- CheckedFileState::Invalid
- }
- } else {
- tracing::warn!("[AF REDUCE_CHECKED] ✗ CRC recalculation errored for {}", destination.display());
- CheckedFileState::Invalid
- }
- }
- },
-
- // File exists without CRC file - recalculate and cache
- (FileState::Exists, FileState::Missing, Some(expected_crc_value)) => {
- if let Ok(is_valid) = calculate_and_verify_crc(destination, expected_crc_value) {
- if is_valid {
- tracing::debug!("[AF REDUCE_CHECKED] ✓ CRC valid; caching for {}", destination.display());
- // Cache the CRC for next time
- let _ = save_crc_file(destination, expected_crc_value);
- CheckedFileState::Valid
- } else {
- tracing::warn!("[AF REDUCE_CHECKED] ✗ CRC invalid for {}", destination.display());
- CheckedFileState::Invalid
- }
- } else {
- tracing::warn!("[AF REDUCE_CHECKED] ✗ CRC calculation errored for {}", destination.display());
- CheckedFileState::Invalid
- }
- },
-
- // File exists without CRC requirement - consider valid
- (FileState::Exists, _, None) => CheckedFileState::Valid,
- }
-}
-
-pub async fn reconcile_to_internal_state(
- checked_file_state: CheckedFileState,
- part_file_state: PartFileState,
- destination: &Path,
- crc_file_path: &Path,
- part_file_path: &Path,
- _expected_bytes: Option<u64>,
- expected_crc: Option<&str>,
-) -> InternalDownloadState<()> {
- tracing::info!(
- "[AF RECONCILE] checked={:?}, part={:?}, path={}, expected_bytes={:?}",
- checked_file_state,
- part_file_state,
- destination.display(),
- _expected_bytes
- );
- match checked_file_state {
- CheckedFileState::Valid => {
- // CRC file should already exist (created during reduce_to_checked_file_state)
- // but save it if somehow missing
- if let Some(crc_value) = expected_crc {
- if !crc_file_path.exists() {
- let _ = save_crc_file(destination, crc_value);
- }
- }
- let _ = fs::remove_file(part_file_path);
- InternalDownloadState::Downloaded {
- file_path: destination.to_path_buf(),
- crc_path: if crc_file_path.exists() {
- Some(crc_file_path.to_path_buf())
- } else {
- None
- },
- }
- },
- CheckedFileState::Invalid => {
- let _ = fs::remove_file(destination);
- let _ = fs::remove_file(crc_file_path);
- match part_file_state {
- FileState::Exists => InternalDownloadState::Paused {
- part_path: part_file_path.to_path_buf(),
- },
- FileState::Missing => InternalDownloadState::NotDownloaded,
- }
- },
- CheckedFileState::Missing => match part_file_state {
- FileState::Exists => InternalDownloadState::Paused {
- part_path: part_file_path.to_path_buf(),
- },
- FileState::Missing => InternalDownloadState::NotDownloaded,
- },
- }
-}
-
-pub fn reduce_to_file_download_state(
- checked_file_state: CheckedFileState,
- part_file_state: PartFileState,
- destination: &Path,
- part_file_path: &Path,
- expected_bytes: Option<u64>,
- manager_id: &str,
-) -> FileDownloadState {
- tracing::info!(
- "[AF REDUCE_STATE] checked={:?}, part={:?}, path={}, expected_bytes={:?}",
- checked_file_state,
- part_file_state,
- destination.display(),
- expected_bytes
- );
-
- // Lock awareness: if the file is locked by another manager/process,
- // reflect this in the user-facing state. Lock decision belongs to reduction.
- let lock_path = std::path::PathBuf::from(format!("{}.lock", destination.display()));
- let lock_state = check_lock_file(&lock_path, manager_id, std::process::id());
- tracing::debug!(
- "[AF REDUCE_STATE] lock_path={}, lock_state={:?}, manager_id={}",
- lock_path.display(),
- lock_state,
- manager_id
- );
- if let LockFileState::OwnedByOtherApp(info) = lock_state {
- if !matches!(checked_file_state, CheckedFileState::Valid) {
- let total = expected_bytes.unwrap_or(0);
- let state = FileDownloadState {
- phase: FileDownloadPhase::LockedByOther(info.manager_id),
- downloaded_bytes: 0,
- total_bytes: total,
- };
- tracing::info!("[AF REDUCE_STATE] → Result: {:?}", state);
- return state;
- }
- }
-
- let state = match checked_file_state {
- CheckedFileState::Valid => {
- let file_size = std::fs::metadata(destination).ok().map(|m| m.len()).unwrap_or(0);
- FileDownloadState {
- phase: FileDownloadPhase::Downloaded,
- downloaded_bytes: file_size,
- total_bytes: file_size,
- }
- },
- CheckedFileState::Invalid => match part_file_state {
- FileState::Exists => {
- let part_size = std::fs::metadata(part_file_path).ok().map(|m| m.len()).unwrap_or(0);
- let total = expected_bytes.unwrap_or(part_size);
- FileDownloadState {
- phase: FileDownloadPhase::Paused,
- downloaded_bytes: part_size,
- total_bytes: total,
- }
- },
- FileState::Missing => {
- let total = expected_bytes.unwrap_or(0);
- FileDownloadState {
- phase: FileDownloadPhase::NotDownloaded,
- downloaded_bytes: 0,
- total_bytes: total,
- }
- },
- },
- CheckedFileState::Missing => match part_file_state {
- FileState::Exists => {
- let part_size = std::fs::metadata(part_file_path).ok().map(|m| m.len()).unwrap_or(0);
- let total = expected_bytes.unwrap_or(part_size);
- FileDownloadState {
- phase: FileDownloadPhase::Paused,
- downloaded_bytes: part_size,
- total_bytes: total,
- }
- },
- FileState::Missing => {
- let total = expected_bytes.unwrap_or(0);
- FileDownloadState {
- phase: FileDownloadPhase::NotDownloaded,
- downloaded_bytes: 0,
- total_bytes: total,
- }
- },
- },
- };
-
- tracing::info!("[AF REDUCE_STATE] → Result: {:?}", state);
- state
-}
diff --git a/crates/download-manager/src/managers/universal/mod.rs b/crates/download-manager/src/managers/universal/mod.rs
deleted file mode 100644
index a4ccad764..000000000
--- a/crates/download-manager/src/managers/universal/mod.rs
+++ /dev/null
@@ -1,8 +0,0 @@
-mod async_fetcher_config;
-mod async_fetcher_download_manager;
-mod async_fetcher_file_download_task;
-mod async_fetcher_state_reducer;
-
-pub use async_fetcher_config::AsyncFetcherConfig;
-pub use async_fetcher_download_manager::AsyncFetcherDownloadManager;
-pub use async_fetcher_file_download_task::FileDownloadTask;
diff --git a/crates/download-manager/src/prelude/apple.rs b/crates/download-manager/src/prelude/apple.rs
deleted file mode 100644
index 2e4ade499..000000000
--- a/crates/download-manager/src/prelude/apple.rs
+++ /dev/null
@@ -1,13 +0,0 @@
-pub use block2::{Block, RcBlock};
-pub use objc2::{
- ClassType, DefinedClass, define_class, msg_send,
- rc::{Allocated, Retained, autoreleasepool},
- runtime::ProtocolObject,
-};
-pub use objc2_foundation::{
- NSArray, NSBundle, NSData, NSDictionary, NSError, NSKeyedUnarchiver, NSMutableDictionary, NSNumber, NSObject,
- NSObjectProtocol, NSPropertyListFormat, NSPropertyListMutabilityOptions, NSPropertyListSerialization, NSString,
- NSURL, NSURLSession, NSURLSessionConfiguration, NSURLSessionDataTask, NSURLSessionDelegate,
- NSURLSessionDownloadDelegate, NSURLSessionDownloadTask, NSURLSessionTask, NSURLSessionTaskDelegate,
- NSURLSessionTaskState, NSURLSessionUploadTask,
-};
diff --git a/crates/download-manager/src/prelude/mod.rs b/crates/download-manager/src/prelude/mod.rs
deleted file mode 100644
index 8f65219b1..000000000
--- a/crates/download-manager/src/prelude/mod.rs
+++ /dev/null
@@ -1,7 +0,0 @@
-#[cfg(target_vendor = "apple")]
-mod apple;
-mod universal;
-
-#[cfg(target_vendor = "apple")]
-pub use crate::prelude::apple::*;
-pub use crate::prelude::universal::*;
diff --git a/crates/download-manager/src/prelude/universal.rs b/crates/download-manager/src/prelude/universal.rs
deleted file mode 100644
index 26b576ea3..000000000
--- a/crates/download-manager/src/prelude/universal.rs
+++ /dev/null
@@ -1,16 +0,0 @@
-pub use std::{
- ops::Deref,
- ptr::NonNull,
- sync::{Arc, Mutex},
-};
-
-pub use tokio::{
- fs,
- runtime::Handle as TokioHandle,
- sync::{
- broadcast::{Sender as TokioBroadcastSender, channel as tokio_broadcast_channel},
- oneshot::{Sender as TokioOneshotSender, channel as tokio_oneshot_channel},
- },
- task::JoinHandle as TokioJoinHandle,
-};
-pub use tokio_stream::{StreamExt as TokioStreamExt, wrappers::BroadcastStream as TokioBroadcastStream};
diff --git a/crates/download-manager/src/reducer/action.rs b/crates/download-manager/src/reducer/action.rs
new file mode 100644
index 000000000..e2421db93
--- /dev/null
+++ b/crates/download-manager/src/reducer/action.rs
@@ -0,0 +1,20 @@
+use std::path::PathBuf;
+
+use serde::{Deserialize, Serialize};
+
+#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, Hash)]
+pub enum Action {
+ DeleteFile {
+ path: PathBuf,
+ },
+ DeleteCrcCache {
+ path: PathBuf,
+ },
+ DeleteResumeArtifact {
+ path: PathBuf,
+ },
+ SaveCrcCache {
+ destination: PathBuf,
+ crc: String,
+ },
+}
diff --git a/crates/download-manager/src/reducer/action_plan.rs b/crates/download-manager/src/reducer/action_plan.rs
new file mode 100644
index 000000000..f4432ee74
--- /dev/null
+++ b/crates/download-manager/src/reducer/action_plan.rs
@@ -0,0 +1,40 @@
+use crate::reducer::Action;
+
+#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
+pub struct ActionPlan {
+ actions: Box<[Action]>,
+}
+
+impl ActionPlan {
+ pub fn empty() -> Self {
+ Self::default()
+ }
+
+ pub fn from_ordered_actions(actions: impl IntoIterator<Item = Action>) -> Self {
+ let mut ordered_actions = Vec::new();
+
+ for action in actions {
+ if !ordered_actions.contains(&action) {
+ ordered_actions.push(action);
+ }
+ }
+
+ Self {
+ actions: ordered_actions.into_boxed_slice(),
+ }
+ }
+
+ pub fn merge_in_order(action_plans: impl IntoIterator<Item = ActionPlan>) -> Self {
+ Self::from_ordered_actions(
+ action_plans.into_iter().flat_map(|action_plan| Vec::from(action_plan.actions).into_iter()),
+ )
+ }
+
+ pub fn as_slice(&self) -> &[Action] {
+ &self.actions
+ }
+
+ pub fn is_empty(&self) -> bool {
+ self.actions.is_empty()
+ }
+}
diff --git a/crates/download-manager/src/reducer/decision.rs b/crates/download-manager/src/reducer/decision.rs
new file mode 100644
index 000000000..a6acae665
--- /dev/null
+++ b/crates/download-manager/src/reducer/decision.rs
@@ -0,0 +1,109 @@
+use crate::{
+ CheckedFileState, FileState, LockFileState,
+ file_download_task_actor::{ProgressCounters, PublicProjection},
+ reducer::{Action, ActionPlan, DiskObservation, InitialLifecycleState, LockObservation, ValidationOutcome},
+};
+
+#[derive(Clone, Debug, PartialEq, Eq, Hash)]
+pub struct Decision {
+ pub initial_lifecycle_state: InitialLifecycleState,
+ pub initial_projection: PublicProjection,
+ pub initial_progress: ProgressCounters,
+ pub action_plan: ActionPlan,
+}
+
+pub fn decide(
+ observation: &DiskObservation,
+ lock_observation: &LockObservation,
+ validation: &ValidationOutcome,
+) -> Decision {
+ let action_plan = if lock_observation.state.is_conflict() {
+ ActionPlan::empty()
+ } else {
+ let decision_action_plan = decide_actions(observation, validation);
+ ActionPlan::merge_in_order([validation.action_plan.clone(), decision_action_plan])
+ };
+
+ let initial_lifecycle_state = match validation.checked {
+ CheckedFileState::Valid => InitialLifecycleState::Downloaded,
+ CheckedFileState::Invalid | CheckedFileState::Missing if observation.resume_state == FileState::Exists => {
+ InitialLifecycleState::Paused {
+ part_path: observation
+ .resume_artifact_path
+ .clone()
+ .unwrap_or_else(|| observation.destination_path.with_extension("part")),
+ }
+ },
+ CheckedFileState::Invalid | CheckedFileState::Missing => InitialLifecycleState::NotDownloaded,
+ };
+
+ // Don't surface `LockedByOther` when the file is already on disk; ownership-sensitive
+ // callers use `FileDownloadManager::destination_foreign_lock` directly.
+ let initial_projection = match (&lock_observation.state, &initial_lifecycle_state) {
+ (LockFileState::OwnedByOtherApp(lock_file_info), state)
+ if !matches!(state, InitialLifecycleState::Downloaded) =>
+ {
+ PublicProjection::LockedByOther(lock_file_info.manager_id.clone())
+ },
+ _ => PublicProjection::None,
+ };
+
+ let initial_progress = match &initial_lifecycle_state {
+ InitialLifecycleState::Paused {
+ ..
+ } => ProgressCounters {
+ downloaded_bytes: observation.resume_size.unwrap_or(0),
+ total_bytes: observation.expected_bytes.or(observation.resume_size).unwrap_or(0),
+ },
+ InitialLifecycleState::Downloaded => {
+ let total_bytes = observation.expected_bytes.or(observation.destination_size).unwrap_or(0);
+ ProgressCounters {
+ downloaded_bytes: total_bytes,
+ total_bytes,
+ }
+ },
+ InitialLifecycleState::NotDownloaded => ProgressCounters {
+ downloaded_bytes: 0,
+ total_bytes: observation.expected_bytes.unwrap_or(0),
+ },
+ };
+
+ Decision {
+ initial_lifecycle_state,
+ initial_projection,
+ initial_progress,
+ action_plan,
+ }
+}
+
+fn decide_actions(
+ observation: &DiskObservation,
+ validation: &ValidationOutcome,
+) -> ActionPlan {
+ let mut actions = Vec::new();
+
+ if validation.checked == CheckedFileState::Valid
+ && observation.resume_state == FileState::Exists
+ && let Some(path) = observation.resume_artifact_path.clone()
+ {
+ actions.push(Action::DeleteResumeArtifact {
+ path,
+ });
+ }
+
+ if validation.checked == CheckedFileState::Invalid {
+ actions.push(Action::DeleteFile {
+ path: observation.destination_path.clone(),
+ });
+
+ if observation.crc_state == FileState::Exists
+ && let Some(path) = observation.crc_path.clone()
+ {
+ actions.push(Action::DeleteCrcCache {
+ path,
+ });
+ }
+ }
+
+ ActionPlan::from_ordered_actions(actions)
+}
diff --git a/crates/download-manager/src/reducer/disk_observation.rs b/crates/download-manager/src/reducer/disk_observation.rs
new file mode 100644
index 000000000..ac75f6693
--- /dev/null
+++ b/crates/download-manager/src/reducer/disk_observation.rs
@@ -0,0 +1,19 @@
+use std::path::PathBuf;
+
+use serde::{Deserialize, Serialize};
+
+use crate::FileState;
+
+#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, Hash)]
+pub struct DiskObservation {
+ pub destination_state: FileState,
+ pub crc_state: FileState,
+ pub resume_state: FileState,
+ pub destination_size: Option<u64>,
+ pub resume_size: Option<u64>,
+ pub expected_crc: Option<String>,
+ pub expected_bytes: Option<u64>,
+ pub destination_path: PathBuf,
+ pub crc_path: Option<PathBuf>,
+ pub resume_artifact_path: Option<PathBuf>,
+}
diff --git a/crates/download-manager/src/reducer/initial_lifecycle_state.rs b/crates/download-manager/src/reducer/initial_lifecycle_state.rs
new file mode 100644
index 000000000..6970b7ead
--- /dev/null
+++ b/crates/download-manager/src/reducer/initial_lifecycle_state.rs
@@ -0,0 +1,24 @@
+use std::path::PathBuf;
+
+use serde::{Deserialize, Serialize};
+
+#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, Hash)]
+pub enum InitialLifecycleState {
+ NotDownloaded,
+ Paused {
+ part_path: PathBuf,
+ },
+ Downloaded,
+}
+
+impl InitialLifecycleState {
+ pub fn name(&self) -> &'static str {
+ match self {
+ Self::NotDownloaded => "NotDownloaded",
+ Self::Paused {
+ ..
+ } => "Paused",
+ Self::Downloaded => "Downloaded",
+ }
+ }
+}
diff --git a/crates/download-manager/src/reducer/lock_observation.rs b/crates/download-manager/src/reducer/lock_observation.rs
new file mode 100644
index 000000000..cab355a35
--- /dev/null
+++ b/crates/download-manager/src/reducer/lock_observation.rs
@@ -0,0 +1,8 @@
+use serde::{Deserialize, Serialize};
+
+use crate::LockFileState;
+
+#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, Hash)]
+pub struct LockObservation {
+ pub state: LockFileState,
+}
diff --git a/crates/download-manager/src/reducer/mod.rs b/crates/download-manager/src/reducer/mod.rs
new file mode 100644
index 000000000..ee0049927
--- /dev/null
+++ b/crates/download-manager/src/reducer/mod.rs
@@ -0,0 +1,15 @@
+mod action;
+mod action_plan;
+mod decision;
+mod disk_observation;
+mod initial_lifecycle_state;
+mod lock_observation;
+mod validation_outcome;
+
+pub use action::Action;
+pub use action_plan::ActionPlan;
+pub use decision::decide;
+pub use disk_observation::DiskObservation;
+pub use initial_lifecycle_state::InitialLifecycleState;
+pub use lock_observation::LockObservation;
+pub use validation_outcome::{ValidationOutcome, validate};
diff --git a/crates/download-manager/src/reducer/validation_outcome.rs b/crates/download-manager/src/reducer/validation_outcome.rs
new file mode 100644
index 000000000..6760dd45b
--- /dev/null
+++ b/crates/download-manager/src/reducer/validation_outcome.rs
@@ -0,0 +1,68 @@
+use crate::{
+ CheckedFileState, FileState,
+ crc_utils::{calculate_and_verify_crc, crc_cache_matches},
+ reducer::{Action, ActionPlan, DiskObservation},
+};
+
+#[derive(Clone, Debug, PartialEq, Eq, Hash)]
+pub struct ValidationOutcome {
+ pub checked: CheckedFileState,
+ pub action_plan: ActionPlan,
+}
+
+pub async fn validate(observation: &DiskObservation) -> ValidationOutcome {
+ let length_matches = match (observation.destination_size, observation.expected_bytes) {
+ (Some(actual), Some(expected)) => actual == expected,
+ _ => true,
+ };
+
+ let (checked, mut actions) = match (observation.destination_state, observation.expected_crc.as_deref()) {
+ (FileState::Missing, _) => (CheckedFileState::Missing, Vec::new()),
+ (FileState::Exists, _) if !length_matches => (CheckedFileState::Invalid, Vec::new()),
+ (FileState::Exists, None) => (CheckedFileState::Valid, Vec::new()),
+ (FileState::Exists, Some(expected_crc)) => validate_crc_with_cache(observation, expected_crc).await,
+ };
+
+ if checked == CheckedFileState::Missing
+ && observation.crc_state == FileState::Exists
+ && let Some(path) = observation.crc_path.clone()
+ {
+ actions.push(Action::DeleteCrcCache {
+ path,
+ });
+ }
+
+ let action_plan = ActionPlan::from_ordered_actions(actions);
+
+ ValidationOutcome {
+ checked,
+ action_plan,
+ }
+}
+
+async fn validate_crc_with_cache(
+ observation: &DiskObservation,
+ expected_crc: &str,
+) -> (CheckedFileState, Vec<Action>) {
+ if observation.crc_state == FileState::Exists && crc_cache_matches(&observation.destination_path, expected_crc) {
+ return (CheckedFileState::Valid, Vec::new());
+ }
+
+ let destination = observation.destination_path.clone();
+ let expected = expected_crc.to_string();
+ let crc_check = tokio::task::spawn_blocking(move || calculate_and_verify_crc(&destination, &expected))
+ .await
+ .ok()
+ .and_then(|inner| inner.ok());
+
+ match crc_check {
+ Some(true) => (
+ CheckedFileState::Valid,
+ vec![Action::SaveCrcCache {
+ destination: observation.destination_path.clone(),
+ crc: expected_crc.to_string(),
+ }],
+ ),
+ _ => (CheckedFileState::Invalid, Vec::new()),
+ }
+}
diff --git a/crates/download-manager/src/traits/active_download_generation.rs b/crates/download-manager/src/traits/active_download_generation.rs
new file mode 100644
index 000000000..12bdb7d32
--- /dev/null
+++ b/crates/download-manager/src/traits/active_download_generation.rs
@@ -0,0 +1,10 @@
+use serde::{Deserialize, Serialize};
+
+#[derive(Serialize, Deserialize, Clone, Copy, Debug, PartialEq, Eq, Hash)]
+pub struct ActiveDownloadGeneration(u64);
+
+impl ActiveDownloadGeneration {
+ pub fn new(value: u64) -> Self {
+ Self(value)
+ }
+}
diff --git a/crates/download-manager/src/traits/active_download_generation_counter.rs b/crates/download-manager/src/traits/active_download_generation_counter.rs
new file mode 100644
index 000000000..dd30ddf82
--- /dev/null
+++ b/crates/download-manager/src/traits/active_download_generation_counter.rs
@@ -0,0 +1,14 @@
+use crate::traits::ActiveDownloadGeneration;
+
+#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
+pub struct ActiveDownloadGenerationCounter {
+ next_value: u64,
+}
+
+impl ActiveDownloadGenerationCounter {
+ pub fn allocate_next(&mut self) -> ActiveDownloadGeneration {
+ let generation = ActiveDownloadGeneration::new(self.next_value);
+ self.next_value = self.next_value.saturating_add(1);
+ generation
+ }
+}
diff --git a/crates/download-manager/src/traits/active_task.rs b/crates/download-manager/src/traits/active_task.rs
new file mode 100644
index 000000000..9692eba8c
--- /dev/null
+++ b/crates/download-manager/src/traits/active_task.rs
@@ -0,0 +1,18 @@
+use std::path::{Path, PathBuf};
+
+use crate::traits::{CancelOutcome, DownloadBackend};
+
+#[async_trait::async_trait]
+pub trait ActiveTask: Send + Sync + Sized {
+ type Backend: DownloadBackend<ActiveTask = Self>;
+
+ async fn pause(
+ self,
+ destination: &Path,
+ ) -> Result<PathBuf, <Self::Backend as DownloadBackend>::Error>;
+
+ async fn cancel(
+ self,
+ destination: &Path,
+ ) -> CancelOutcome;
+}
diff --git a/crates/download-manager/src/traits/backend_context.rs b/crates/download-manager/src/traits/backend_context.rs
new file mode 100644
index 000000000..78e002e00
--- /dev/null
+++ b/crates/download-manager/src/traits/backend_context.rs
@@ -0,0 +1,28 @@
+use std::{path::Path, sync::Arc};
+
+use crate::{
+ lock_manager::DestinationLockLease,
+ traits::{ActiveDownloadGeneration, BackendEventSender, DownloadBackend, DownloadConfig},
+};
+
+#[async_trait::async_trait]
+pub trait BackendContext: Send + Sync + Sized {
+ type Backend: DownloadBackend<Context = Self>;
+
+ async fn download(
+ &self,
+ config: Arc<DownloadConfig>,
+ generation: ActiveDownloadGeneration,
+ backend_event_sender: BackendEventSender,
+ destination_lease: &DestinationLockLease,
+ ) -> Result<<Self::Backend as DownloadBackend>::ActiveTask, <Self::Backend as DownloadBackend>::Error>;
+
+ async fn resume(
+ &self,
+ config: Arc<DownloadConfig>,
+ generation: ActiveDownloadGeneration,
+ resume_artifact_path: &Path,
+ backend_event_sender: BackendEventSender,
+ destination_lease: &DestinationLockLease,
+ ) -> Result<<Self::Backend as DownloadBackend>::ActiveTask, <Self::Backend as DownloadBackend>::Error>;
+}
diff --git a/crates/download-manager/src/traits/backend_event_sender.rs b/crates/download-manager/src/traits/backend_event_sender.rs
new file mode 100644
index 000000000..1d44aee45
--- /dev/null
+++ b/crates/download-manager/src/traits/backend_event_sender.rs
@@ -0,0 +1,73 @@
+use std::sync::Arc;
+
+use tokio::sync::{Mutex as TokioMutex, mpsc::Sender as TokioMpscSender, watch::Sender as TokioWatchSender};
+
+use crate::{
+ DownloadId,
+ download_log_event::{DownloadLogEvent, log},
+ file_download_task_actor::{BackendEvent, BackendProgress, PendingProgressSlot},
+ traits::ActiveDownloadGeneration,
+};
+
+#[derive(Clone, Debug)]
+pub struct BackendEventSender {
+ download_id: DownloadId,
+ terminal_event_sender: TokioMpscSender<BackendEvent>,
+ progress_coalescer: SharedProgressCoalescer,
+}
+
+impl BackendEventSender {
+ pub fn new(
+ download_id: DownloadId,
+ terminal_event_sender: TokioMpscSender<BackendEvent>,
+ pending_progress: Arc<TokioMutex<PendingProgressSlot>>,
+ actor_waker: TokioWatchSender<()>,
+ ) -> Self {
+ Self {
+ download_id,
+ terminal_event_sender,
+ progress_coalescer: SharedProgressCoalescer {
+ pending_progress,
+ actor_waker,
+ },
+ }
+ }
+
+ pub async fn send_terminal(
+ &self,
+ event: BackendEvent,
+ ) -> Result<(), BackendEvent> {
+ log(DownloadLogEvent::BackendTerminal {
+ download_id: self.download_id,
+ event: event.clone(),
+ });
+ self.terminal_event_sender.send(event).await.map_err(|error| error.0)
+ }
+
+ pub async fn send_progress(
+ &self,
+ generation: ActiveDownloadGeneration,
+ downloaded_bytes: u64,
+ total_bytes: Option<u64>,
+ ) {
+ log(DownloadLogEvent::BackendProgress {
+ download_id: self.download_id,
+ generation,
+ downloaded_bytes,
+ total_bytes,
+ });
+ let mut pending_progress = self.progress_coalescer.pending_progress.lock().await;
+ pending_progress.progress = Some(BackendProgress {
+ generation,
+ downloaded_bytes,
+ total_bytes,
+ });
+ let _ = self.progress_coalescer.actor_waker.send(());
+ }
+}
+
+#[derive(Clone, Debug)]
+struct SharedProgressCoalescer {
+ pending_progress: Arc<TokioMutex<PendingProgressSlot>>,
+ actor_waker: TokioWatchSender<()>,
+}
diff --git a/crates/download-manager/src/traits/cancel_outcome.rs b/crates/download-manager/src/traits/cancel_outcome.rs
new file mode 100644
index 000000000..ab310fa3e
--- /dev/null
+++ b/crates/download-manager/src/traits/cancel_outcome.rs
@@ -0,0 +1,8 @@
+use serde::{Deserialize, Serialize};
+
+#[derive(Serialize, Deserialize, Clone, Copy, Debug, PartialEq, Eq, Hash)]
+pub enum CancelOutcome {
+ Confirmed,
+ TimedOut,
+ BestEffort,
+}
diff --git a/crates/download-manager/src/traits/download_backend.rs b/crates/download-manager/src/traits/download_backend.rs
new file mode 100644
index 000000000..b8adc9d0b
--- /dev/null
+++ b/crates/download-manager/src/traits/download_backend.rs
@@ -0,0 +1,15 @@
+use std::{fmt::Debug, path::Path};
+
+use crate::traits::{ActiveTask, BackendContext};
+
+pub trait DownloadBackend: Debug + Clone + Send + Sync + 'static {
+ type Context: BackendContext<Backend = Self>;
+ type ActiveTask: ActiveTask<Backend = Self>;
+ type Error: std::error::Error + Send + Sync + 'static;
+
+ // Default = file size (correct for `.part`-style artifacts). Apple must override:
+ // `.resume_data` is a small metadata blob, not the downloaded bytes.
+ fn read_resume_progress(part_path: &Path) -> Option<u64> {
+ std::fs::metadata(part_path).ok().map(|metadata| metadata.len())
+ }
+}
diff --git a/crates/download-manager/src/traits/download_config.rs b/crates/download-manager/src/traits/download_config.rs
new file mode 100644
index 000000000..041d50a7e
--- /dev/null
+++ b/crates/download-manager/src/traits/download_config.rs
@@ -0,0 +1,17 @@
+use std::path::PathBuf;
+
+use serde::{Deserialize, Serialize};
+use uuid::Uuid;
+
+use crate::{DownloadId, FileCheck};
+
+#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, Hash)]
+pub struct DownloadConfig {
+ pub download_id: DownloadId,
+ pub source_url: String,
+ pub destination: PathBuf,
+ pub file_check: FileCheck,
+ pub expected_bytes: Option<u64>,
+ pub manager_id: String,
+ pub manager_instance_id: Uuid,
+}
diff --git a/crates/download-manager/src/traits/mod.rs b/crates/download-manager/src/traits/mod.rs
new file mode 100644
index 000000000..007ea3a41
--- /dev/null
+++ b/crates/download-manager/src/traits/mod.rs
@@ -0,0 +1,17 @@
+mod active_download_generation;
+mod active_download_generation_counter;
+mod active_task;
+mod backend_context;
+mod backend_event_sender;
+mod cancel_outcome;
+mod download_backend;
+mod download_config;
+
+pub use active_download_generation::ActiveDownloadGeneration;
+pub use active_download_generation_counter::ActiveDownloadGenerationCounter;
+pub use active_task::ActiveTask;
+pub use backend_context::BackendContext;
+pub use backend_event_sender::BackendEventSender;
+pub use cancel_outcome::CancelOutcome;
+pub use download_backend::DownloadBackend;
+pub use download_config::DownloadConfig;
diff --git a/crates/download-manager/src/utils.rs b/crates/download-manager/src/utils.rs
deleted file mode 100644
index c0b92d882..000000000
--- a/crates/download-manager/src/utils.rs
+++ /dev/null
@@ -1,16 +0,0 @@
-use crate::{Path, Uuid};
-
-#[inline]
-pub fn compute_download_id(
- url: &str,
- dest: &Path,
-) -> Uuid {
- // Stable namespace for this crate; constant to ensure deterministic IDs.
- // This is a randomly generated namespace UUID v4, hardcoded.
- // Changing this will change the computed IDs across the app.
- const NAMESPACE: Uuid = Uuid::from_bytes([
- 0x7d, 0xa6, 0x81, 0xb9, 0x12, 0x5f, 0x4f, 0x6b, 0x8a, 0x1f, 0x85, 0x5c, 0x84, 0x3a, 0x3c, 0x5e,
- ]);
- let key = format!("{}\n{}", url, dest.display());
- Uuid::new_v5(&NAMESPACE, key.as_bytes())
-}
diff --git a/crates/download-manager/tests/common/mod.rs b/crates/download-manager/tests/common/mod.rs
index 835a12efe..132da97cc 100644
--- a/crates/download-manager/tests/common/mod.rs
+++ b/crates/download-manager/tests/common/mod.rs
@@ -1,7 +1,10 @@
-use std::sync::Arc;
+#![allow(dead_code, unused_imports)]
+
+use std::{sync::Arc, time::Duration};
use download_manager::{FileDownloadPhase, FileDownloadState, FileDownloadTask};
pub use mock_registry::{Behavior, MockRegistry};
+use tokio::time::timeout;
use tokio_stream::{StreamExt, wrappers::BroadcastStream};
pub async fn wait_for_phase(
@@ -9,19 +12,23 @@ pub async fn wait_for_phase(
progress_stream: &mut BroadcastStream<FileDownloadState>,
mut is_expected_phase: impl FnMut(&FileDownloadPhase) -> bool,
) -> FileDownloadState {
- let state = task.state().await;
- if is_expected_phase(&state.phase) {
- return state;
- }
-
- while let Some(result) = progress_stream.next().await {
- let state = result.expect("download progress stream must not lag");
+ timeout(Duration::from_secs(15), async {
+ let state = task.state().await;
if is_expected_phase(&state.phase) {
return state;
}
- }
- panic!("download progress stream ended before expected phase");
+ while let Some(result) = progress_stream.next().await {
+ let state = result.expect("download progress stream must not lag");
+ if is_expected_phase(&state.phase) {
+ return state;
+ }
+ }
+
+ panic!("download progress stream ended before expected phase");
+ })
+ .await
+ .expect("timed out waiting for file download phase")
}
pub fn error_message(state: FileDownloadState) -> String {
diff --git a/crates/download-manager/tests/unit/backends/apple/mod.rs b/crates/download-manager/tests/unit/backends/apple/mod.rs
new file mode 100644
index 000000000..4c9d08011
--- /dev/null
+++ b/crates/download-manager/tests/unit/backends/apple/mod.rs
@@ -0,0 +1 @@
+mod test_context;
diff --git a/crates/download-manager/tests/unit/backends/apple/test_context.rs b/crates/download-manager/tests/unit/backends/apple/test_context.rs
new file mode 100644
index 000000000..c4174956a
--- /dev/null
+++ b/crates/download-manager/tests/unit/backends/apple/test_context.rs
@@ -0,0 +1,253 @@
+use std::sync::Arc;
+
+use mock_registry::{Behavior, MockRegistry};
+use tokio::{
+ runtime::Handle as TokioHandle,
+ sync::{
+ Mutex as TokioMutex,
+ mpsc::{Receiver as TokioMpscReceiver, channel as tokio_mpsc_channel},
+ watch::channel as tokio_watch_channel,
+ },
+};
+use uuid::Uuid;
+
+use crate::{
+ FileCheck,
+ backends::apple::AppleBackendContext,
+ compute_download_id,
+ file_download_task_actor::{BackendEvent, PendingProgressSlot},
+ lock_manager::DestinationLockLease,
+ traits::{ActiveDownloadGeneration, BackendContext, BackendEventSender, DownloadConfig},
+};
+
+fn backend_event_sender() -> (BackendEventSender, TokioMpscReceiver<BackendEvent>) {
+ let (backend_event_sender, backend_event_receiver) = tokio_mpsc_channel(64);
+ let pending_progress = Arc::new(TokioMutex::new(PendingProgressSlot::default()));
+ let (progress_waker_sender, _progress_waker_receiver) = tokio_watch_channel(());
+ (
+ BackendEventSender::new(Uuid::new_v4(), backend_event_sender, pending_progress, progress_waker_sender),
+ backend_event_receiver,
+ )
+}
+
+#[tokio::test(flavor = "multi_thread")]
+async fn test_apple_resume_empty_resume_data_starts_fresh_download() -> Result<(), Box<dyn std::error::Error>> {
+ let registry = MockRegistry::start().await?;
+ let served_file = registry.file("config.json")?;
+ let temporary_directory = tempfile::tempdir()?;
+ let destination = temporary_directory.path().join(&served_file.file.name);
+ let resume_artifact_path = destination.with_extension("resume_data");
+ tokio::fs::write(&resume_artifact_path, b"").await?;
+
+ let context = AppleBackendContext::new(TokioHandle::current());
+ let config = Arc::new(DownloadConfig {
+ download_id: compute_download_id(&destination),
+ source_url: served_file.file.url.clone(),
+ destination: destination.clone(),
+ file_check: FileCheck::None,
+ expected_bytes: Some(served_file.file.size as u64),
+ manager_id: "test-manager".to_string(),
+ manager_instance_id: Uuid::new_v4(),
+ });
+ let destination_lease = DestinationLockLease::acquire_for_destination(
+ &config.destination,
+ &config.manager_id,
+ config.manager_instance_id,
+ )
+ .await?;
+ let generation = ActiveDownloadGeneration::new(0);
+ let (backend_event_sender, mut backend_event_receiver) = backend_event_sender();
+
+ let active_task = context
+ .resume(Arc::clone(&config), generation, &resume_artifact_path, backend_event_sender, &destination_lease)
+ .await?;
+ let event = tokio::time::timeout(std::time::Duration::from_secs(5), backend_event_receiver.recv())
+ .await?
+ .ok_or_else(|| {
+ std::io::Error::new(
+ std::io::ErrorKind::UnexpectedEof,
+ "backend event channel closed before empty resume-data fallback completed",
+ )
+ })?;
+
+ assert_eq!(event, BackendEvent::completed(generation));
+ assert_eq!(tokio::fs::read(&destination).await?, served_file.bytes.to_vec());
+
+ drop(active_task);
+ destination_lease.release().await?;
+ Ok(())
+}
+
+#[tokio::test(flavor = "multi_thread")]
+async fn dropping_apple_active_task_unregisters_event_sink() -> Result<(), Box<dyn std::error::Error>> {
+ let registry = MockRegistry::start_with(Behavior::THROTTLED).await?;
+ let served_file = registry.file("config.json")?;
+ let temporary_directory = tempfile::tempdir()?;
+ let destination = temporary_directory.path().join(&served_file.file.name);
+
+ let context = AppleBackendContext::new(TokioHandle::current());
+ let download_id = compute_download_id(&destination);
+ let config = Arc::new(DownloadConfig {
+ download_id,
+ source_url: served_file.file.url.clone(),
+ destination,
+ file_check: FileCheck::None,
+ expected_bytes: Some(served_file.file.size as u64),
+ manager_id: "test-manager".to_string(),
+ manager_instance_id: Uuid::new_v4(),
+ });
+ let destination_lease = DestinationLockLease::acquire_for_destination(
+ &config.destination,
+ &config.manager_id,
+ config.manager_instance_id,
+ )
+ .await?;
+
+ let (backend_event_sender, _backend_event_receiver) = backend_event_sender();
+ let active_task = context
+ .download(Arc::clone(&config), ActiveDownloadGeneration::new(0), backend_event_sender, &destination_lease)
+ .await?;
+ assert_eq!(
+ context.event_sink_count_for_download(download_id),
+ 1,
+ "precondition: exactly one event sink should be registered for download_id after starting download",
+ );
+
+ drop(active_task);
+ destination_lease.release().await?;
+
+ assert_eq!(
+ context.event_sink_count_for_download(download_id),
+ 0,
+ "dropping AppleActiveTask must unregister its event sink",
+ );
+ Ok(())
+}
+
+#[tokio::test(flavor = "multi_thread")]
+async fn registry_distinguishes_generations_for_same_download_id() -> Result<(), Box<dyn std::error::Error>> {
+ let registry = MockRegistry::start_with(Behavior::THROTTLED).await?;
+ let served_file = registry.file("config.json")?;
+ let temporary_directory = tempfile::tempdir()?;
+ let destination = temporary_directory.path().join(&served_file.file.name);
+ let download_id = compute_download_id(&destination);
+
+ let context = AppleBackendContext::new(TokioHandle::current());
+ let config = Arc::new(DownloadConfig {
+ download_id,
+ source_url: served_file.file.url.clone(),
+ destination,
+ file_check: FileCheck::None,
+ expected_bytes: Some(served_file.file.size as u64),
+ manager_id: "test-manager".to_string(),
+ manager_instance_id: Uuid::new_v4(),
+ });
+ let destination_lease = DestinationLockLease::acquire_for_destination(
+ &config.destination,
+ &config.manager_id,
+ config.manager_instance_id,
+ )
+ .await?;
+
+ let (backend_event_sender_first, _backend_event_receiver_first) = backend_event_sender();
+ let first_task = context
+ .download(
+ Arc::clone(&config),
+ ActiveDownloadGeneration::new(0),
+ backend_event_sender_first,
+ &destination_lease,
+ )
+ .await?;
+ assert_eq!(
+ context.event_sink_task_identifiers_for_download(download_id).len(),
+ 1,
+ "first generation must register a sink",
+ );
+
+ let (backend_event_sender_second, _backend_event_receiver_second) = backend_event_sender();
+ let second_task = context
+ .download(
+ Arc::clone(&config),
+ ActiveDownloadGeneration::new(1),
+ backend_event_sender_second,
+ &destination_lease,
+ )
+ .await?;
+
+ let both_keys = context.event_sink_task_identifiers_for_download(download_id);
+ assert_eq!(both_keys.len(), 2, "second generation must not overwrite the first sink; got keys: {both_keys:?}");
+ assert_ne!(both_keys[0], both_keys[1], "the two generations must have distinct task identifiers");
+
+ drop(first_task);
+ drop(second_task);
+ destination_lease.release().await?;
+ Ok(())
+}
+
+#[tokio::test(flavor = "multi_thread")]
+async fn claim_cancels_mismatched_task() -> Result<(), Box<dyn std::error::Error>> {
+ let registry = MockRegistry::start_with(Behavior::THROTTLED).await?;
+ let served_file = registry.file("tokenizer.json")?;
+ let temporary_directory = tempfile::tempdir()?;
+ let destination = temporary_directory.path().join(&served_file.file.name);
+ let download_id = compute_download_id(&destination);
+
+ let context = AppleBackendContext::new(TokioHandle::current());
+ let original_config = Arc::new(DownloadConfig {
+ download_id,
+ source_url: served_file.file.url.clone(),
+ destination: destination.clone(),
+ file_check: FileCheck::None,
+ expected_bytes: Some(served_file.file.size as u64),
+ manager_id: "test-manager".to_string(),
+ manager_instance_id: Uuid::new_v4(),
+ });
+ let destination_lease = DestinationLockLease::acquire_for_destination(
+ &original_config.destination,
+ &original_config.manager_id,
+ original_config.manager_instance_id,
+ )
+ .await?;
+
+ let (backend_event_sender, _backend_event_receiver) = backend_event_sender();
+ let original_task = context
+ .download(
+ Arc::clone(&original_config),
+ ActiveDownloadGeneration::new(0),
+ backend_event_sender,
+ &destination_lease,
+ )
+ .await?;
+
+ assert!(
+ context.claim_matching_download_task(&original_config).await?.is_some(),
+ "precondition: original task should be visible before the mismatched claim",
+ );
+
+ let mismatched_config = DownloadConfig {
+ source_url: "http://example.invalid/different-url".to_string(),
+ ..(*original_config).clone()
+ };
+ assert!(
+ context.has_download_task_to_claim(&mismatched_config).await?,
+ "manager startup must take the claim path for a live same-destination task with mismatched metadata",
+ );
+ assert!(context.claim_matching_download_task(&mismatched_config).await?.is_none());
+
+ let mut cancelled = false;
+ for _attempt in 0..50 {
+ if context.claim_matching_download_task(&original_config).await?.is_none() {
+ cancelled = true;
+ break;
+ }
+ tokio::time::sleep(std::time::Duration::from_millis(20)).await;
+ }
+
+ assert!(
+ cancelled,
+ "claiming a same-destination task with mismatched metadata must cancel the stale URLSession task",
+ );
+ drop(original_task);
+ destination_lease.release().await?;
+ Ok(())
+}
diff --git a/crates/download-manager/tests/unit/backends/common/mod.rs b/crates/download-manager/tests/unit/backends/common/mod.rs
new file mode 100644
index 000000000..6dfc229bb
--- /dev/null
+++ b/crates/download-manager/tests/unit/backends/common/mod.rs
@@ -0,0 +1,6 @@
+mod test_actor_task_drop;
+mod test_corrupt_body;
+mod test_lifecycle;
+mod test_manager_startup;
+mod test_state_and_lock_regression;
+mod test_truncated_body;
diff --git a/crates/download-manager/tests/unit/backends/common/test_actor_task_drop.rs b/crates/download-manager/tests/unit/backends/common/test_actor_task_drop.rs
new file mode 100644
index 000000000..b5334e23a
--- /dev/null
+++ b/crates/download-manager/tests/unit/backends/common/test_actor_task_drop.rs
@@ -0,0 +1,98 @@
+use std::{path::PathBuf, sync::Arc};
+
+use download_manager::{
+ DownloadError, FileCheck, FileDownloadManager, FileDownloadManagerType, FileDownloadPhase,
+};
+use rstest::rstest;
+use tokio::runtime::Handle as TokioHandle;
+
+use crate::common::{Behavior, MockRegistry, wait_for_phase};
+
+#[rstest]
+#[case::universal(FileDownloadManagerType::Universal, "part")]
+#[cfg_attr(target_vendor = "apple", case::apple(FileDownloadManagerType::Apple, "resume_data"))]
+#[tokio::test(flavor = "multi_thread")]
+async fn remove_paused_task_deletes_resume_artifact(
+ #[case] download_manager_type: FileDownloadManagerType,
+ #[case] resume_artifact_extension: &str,
+) -> Result<(), Box<dyn std::error::Error>> {
+ let temporary_directory = tempfile::tempdir()?;
+ let destination = temporary_directory.path().join("model.bin");
+ let resume_artifact = destination.with_extension(resume_artifact_extension);
+ tokio::fs::write(&resume_artifact, b"partial").await?;
+
+ let manager = <dyn FileDownloadManager>::new(download_manager_type, TokioHandle::current()).await?;
+ let task = manager
+ .file_download_task("http://example.invalid/model.bin", &destination, FileCheck::None, Some(100))
+ .await?;
+
+ assert!(matches!(task.state().await.phase, FileDownloadPhase::Paused));
+
+ manager.remove_file_task(task.download_id()).await?;
+
+ assert!(!resume_artifact.exists());
+ assert!(matches!(task.download().await, Err(DownloadError::TaskStopped)));
+ Ok(())
+}
+
+#[rstest]
+#[case::universal(FileDownloadManagerType::Universal, "part")]
+#[cfg_attr(target_vendor = "apple", case::apple(FileDownloadManagerType::Apple, "resume_data"))]
+#[tokio::test(flavor = "multi_thread")]
+async fn dropping_active_download_cancels_backend_before_releasing_lock(
+ #[case] download_manager_type: FileDownloadManagerType,
+ #[case] resume_artifact_extension: &str,
+) -> Result<(), Box<dyn std::error::Error>> {
+ let registry = MockRegistry::start_with(Behavior::THROTTLED).await?;
+ let tokenizer = registry.file("tokenizer.json")?;
+ let temp_dir = tempfile::tempdir()?;
+ let destination = temp_dir.path().join(&tokenizer.file.name);
+ let lock_path = PathBuf::from(format!("{}.lock", destination.display()));
+ let resume_artifact = destination.with_extension(resume_artifact_extension);
+ let manager = <dyn FileDownloadManager>::new(download_manager_type, TokioHandle::current()).await.unwrap();
+ let task = manager
+ .file_download_task(
+ &tokenizer.file.url,
+ &destination,
+ FileCheck::CRC(tokenizer.crc32c()?),
+ Some(tokenizer.file.size as u64),
+ )
+ .await
+ .unwrap();
+ let mut progress = task.progress().await.unwrap();
+ task.download().await.unwrap();
+ wait_for_phase(&task, &mut progress, |phase| matches!(phase, FileDownloadPhase::Downloading)).await;
+ assert!(lock_path.exists());
+
+ manager.remove_file_task(task.download_id()).await.unwrap();
+
+ assert!(matches!(task.download().await, Err(DownloadError::TaskStopped)));
+ let replacement_task = manager
+ .file_download_task(
+ &tokenizer.file.url,
+ &destination,
+ FileCheck::CRC(tokenizer.crc32c()?),
+ Some(tokenizer.file.size as u64),
+ )
+ .await
+ .unwrap();
+ assert!(!Arc::ptr_eq(&task, &replacement_task));
+ drop(replacement_task);
+
+ let mut lock_released = false;
+ for _ in 0..100 {
+ if !lock_path.exists() {
+ lock_released = true;
+ break;
+ }
+ tokio::time::sleep(std::time::Duration::from_millis(20)).await;
+ }
+ assert!(lock_released);
+ assert!(!resume_artifact.exists());
+
+ let destination_size_at_release = destination.metadata().map(|m| m.len()).unwrap_or(0);
+ tokio::time::sleep(std::time::Duration::from_millis(200)).await;
+ let destination_size_after = destination.metadata().map(|m| m.len()).unwrap_or(0);
+ assert_eq!(destination_size_at_release, destination_size_after);
+ Ok(())
+}
diff --git a/crates/download-manager/tests/unit/backends/common/test_corrupt_body.rs b/crates/download-manager/tests/unit/backends/common/test_corrupt_body.rs
new file mode 100644
index 000000000..1f08c9bbc
--- /dev/null
+++ b/crates/download-manager/tests/unit/backends/common/test_corrupt_body.rs
@@ -0,0 +1,68 @@
+use download_manager::{FileCheck, FileDownloadManager, FileDownloadManagerType, FileDownloadPhase};
+use rstest::rstest;
+use tokio::runtime::Handle as TokioHandle;
+
+use crate::common::{Behavior, MockRegistry, error_message, wait_for_phase};
+
+#[rstest]
+#[case::universal(FileDownloadManagerType::Universal)]
+#[cfg_attr(target_vendor = "apple", case::apple(FileDownloadManagerType::Apple))]
+#[tokio::test(flavor = "multi_thread")]
+async fn test_corrupt_body_fails_crc(
+ #[case] download_manager_type: FileDownloadManagerType
+) -> Result<(), Box<dyn std::error::Error>> {
+ let registry = MockRegistry::start_with(Behavior::CORRUPT_BODY).await?;
+ let tokenizer = registry.file("tokenizer.json")?;
+ let temp_dir = tempfile::tempdir().unwrap();
+ let destination = temp_dir.path().join(&tokenizer.file.name);
+ let manager = <dyn FileDownloadManager>::new(download_manager_type, TokioHandle::current()).await.unwrap();
+ let task = manager
+ .file_download_task(
+ &tokenizer.file.url,
+ &destination,
+ FileCheck::CRC(tokenizer.crc32c()?),
+ Some(tokenizer.file.size as u64),
+ )
+ .await
+ .unwrap();
+ let mut progress = task.progress().await.unwrap();
+
+ task.download().await.unwrap();
+ let state = wait_for_phase(&task, &mut progress, |phase| matches!(phase, FileDownloadPhase::Error(_))).await;
+ let message = error_message(state);
+ assert!(
+ message.contains("CRC") || message.contains("checksum"),
+ "unexpected error: {message}"
+ );
+ Ok(())
+}
+
+#[rstest]
+#[case::universal(FileDownloadManagerType::Universal)]
+#[cfg_attr(target_vendor = "apple", case::apple(FileDownloadManagerType::Apple))]
+#[tokio::test(flavor = "multi_thread")]
+async fn test_corrupt_body_cancel_resets_error_state(
+ #[case] download_manager_type: FileDownloadManagerType,
+) -> Result<(), Box<dyn std::error::Error>> {
+ let registry = MockRegistry::start_with(Behavior::CORRUPT_BODY).await?;
+ let tokenizer = registry.file("tokenizer.json")?;
+ let temporary_directory = tempfile::tempdir()?;
+ let destination = temporary_directory.path().join(&tokenizer.file.name);
+ let manager = <dyn FileDownloadManager>::new(download_manager_type, TokioHandle::current()).await?;
+ let task = manager
+ .file_download_task(
+ &tokenizer.file.url,
+ &destination,
+ FileCheck::CRC(tokenizer.crc32c()?),
+ Some(tokenizer.file.size as u64),
+ )
+ .await?;
+ let mut progress = task.progress().await?;
+
+ task.download().await?;
+ wait_for_phase(&task, &mut progress, |phase| matches!(phase, FileDownloadPhase::Error(_))).await;
+ task.cancel().await?;
+
+ assert!(matches!(task.state().await.phase, FileDownloadPhase::NotDownloaded));
+ Ok(())
+}
diff --git a/crates/download-manager/tests/unit/lifecycle_test.rs b/crates/download-manager/tests/unit/backends/common/test_lifecycle.rs
similarity index 87%
rename from crates/download-manager/tests/unit/lifecycle_test.rs
rename to crates/download-manager/tests/unit/backends/common/test_lifecycle.rs
index 3e8c6ff7b..758e2e35a 100644
--- a/crates/download-manager/tests/unit/lifecycle_test.rs
+++ b/crates/download-manager/tests/unit/backends/common/test_lifecycle.rs
@@ -1,4 +1,4 @@
-use download_manager::{FileCheck, FileDownloadManagerType, FileDownloadPhase, create_download_manager};
+use download_manager::{FileCheck, FileDownloadManager, FileDownloadManagerType, FileDownloadPhase};
use rstest::rstest;
use tokio::runtime::Handle as TokioHandle;
@@ -16,7 +16,7 @@ async fn test_download_fresh_completes(
let temp_dir = tempfile::tempdir().unwrap();
let destination = temp_dir.path().join(&tokenizer.file.name);
- let manager = create_download_manager(download_manager_type, TokioHandle::current()).await.unwrap();
+ let manager = <dyn FileDownloadManager>::new(download_manager_type, TokioHandle::current()).await.unwrap();
let task = manager
.file_download_task(
&tokenizer.file.url,
diff --git a/crates/download-manager/tests/unit/backends/common/test_manager_startup.rs b/crates/download-manager/tests/unit/backends/common/test_manager_startup.rs
new file mode 100644
index 000000000..f28b68502
--- /dev/null
+++ b/crates/download-manager/tests/unit/backends/common/test_manager_startup.rs
@@ -0,0 +1,148 @@
+use chrono::Utc;
+use download_manager::{FileCheck, FileDownloadManager, FileDownloadManagerType, FileDownloadPhase};
+use tempfile::tempdir;
+use tokio::runtime::Handle as TokioHandle;
+
+use crate::common::MockRegistry;
+
+#[tokio::test(flavor = "multi_thread")]
+async fn test_manager_startup_valid_existing_file_is_downloaded() -> Result<(), Box<dyn std::error::Error>> {
+ let registry = MockRegistry::start().await?;
+ let served_file = registry.file("config.json")?;
+ let expected_crc = served_file.crc32c()?;
+ let temporary_directory = tempdir()?;
+ let destination = temporary_directory.path().join(&served_file.file.name);
+ let crc_path = std::path::PathBuf::from(format!("{}.crc", destination.display()));
+ let resume_artifact = destination.with_extension("part");
+ tokio::fs::write(&destination, served_file.bytes.as_ref()).await?;
+ tokio::fs::write(&resume_artifact, b"partial").await?;
+
+ let task = manager_task(&served_file.file.url, &destination, FileCheck::CRC(expected_crc.clone()), Some(served_file.file.size as u64)).await?;
+
+ assert_eq!(task.state().await.phase, FileDownloadPhase::Downloaded);
+ assert!(!resume_artifact.exists());
+ let crc_receipt: serde_json::Value = serde_json::from_str(&tokio::fs::read_to_string(crc_path).await?)?;
+ assert_eq!(crc_receipt["version"].as_u64(), Some(1));
+ assert_eq!(crc_receipt["crc"].as_str(), Some(expected_crc.as_str()));
+ assert_eq!(crc_receipt["file_size"].as_u64(), Some(served_file.file.size as u64));
+ assert!(crc_receipt["modified_unix_seconds"].as_u64().is_some());
+ assert!(crc_receipt["modified_nanos"].as_u64().is_some());
+ Ok(())
+}
+
+#[tokio::test(flavor = "multi_thread")]
+async fn test_manager_startup_rejects_legacy_crc_cache_after_file_changes() -> Result<(), Box<dyn std::error::Error>> {
+ let registry = MockRegistry::start().await?;
+ let served_file = registry.file("config.json")?;
+ let expected_crc = served_file.crc32c()?;
+ let temporary_directory = tempdir()?;
+ let destination = temporary_directory.path().join(&served_file.file.name);
+ let crc_path = std::path::PathBuf::from(format!("{}.crc", destination.display()));
+ let mut stale_destination_bytes = served_file.bytes.to_vec();
+ stale_destination_bytes[0] = stale_destination_bytes[0].wrapping_add(1);
+ tokio::fs::write(&destination, stale_destination_bytes).await?;
+ tokio::fs::write(&crc_path, &expected_crc).await?;
+
+ let task = manager_task(
+ &served_file.file.url,
+ &destination,
+ FileCheck::CRC(expected_crc),
+ Some(served_file.file.size as u64),
+ )
+ .await?;
+
+ assert_eq!(task.state().await.phase, FileDownloadPhase::NotDownloaded);
+ assert!(!destination.exists());
+ Ok(())
+}
+
+#[tokio::test(flavor = "multi_thread")]
+async fn test_manager_startup_revalidates_stale_metadata_crc_cache() -> Result<(), Box<dyn std::error::Error>> {
+ let registry = MockRegistry::start().await?;
+ let served_file = registry.file("config.json")?;
+ let expected_crc = served_file.crc32c()?;
+ let temporary_directory = tempdir()?;
+ let destination = temporary_directory.path().join(&served_file.file.name);
+ let crc_path = std::path::PathBuf::from(format!("{}.crc", destination.display()));
+ let mut stale_destination_bytes = served_file.bytes.to_vec();
+ stale_destination_bytes[0] = stale_destination_bytes[0].wrapping_add(1);
+ tokio::fs::write(&destination, stale_destination_bytes).await?;
+ tokio::fs::write(
+ &crc_path,
+ serde_json::to_vec(&serde_json::json!({
+ "version": 1,
+ "crc": expected_crc.clone(),
+ "file_size": served_file.file.size,
+ "modified_unix_seconds": 0,
+ "modified_nanos": 0,
+ }))?,
+ )
+ .await?;
+
+ let task = manager_task(&served_file.file.url, &destination, FileCheck::CRC(expected_crc), Some(served_file.file.size as u64)).await?;
+
+ assert_eq!(task.state().await.phase, FileDownloadPhase::NotDownloaded);
+ assert!(!destination.exists());
+ Ok(())
+}
+
+#[tokio::test(flavor = "multi_thread")]
+async fn test_manager_startup_directory_destination_is_not_downloaded() -> Result<(), Box<dyn std::error::Error>> {
+ let registry = MockRegistry::start().await?;
+ let served_file = registry.file("config.json")?;
+ let temporary_directory = tempdir()?;
+ let destination = temporary_directory.path().join(&served_file.file.name);
+ tokio::fs::create_dir(&destination).await?;
+
+ let task = manager_task(&served_file.file.url, &destination, FileCheck::None, None).await?;
+
+ assert_eq!(task.state().await.phase, FileDownloadPhase::NotDownloaded);
+ Ok(())
+}
+
+#[tokio::test(flavor = "multi_thread")]
+async fn test_manager_startup_foreign_lock_preserves_files() -> Result<(), Box<dyn std::error::Error>> {
+ let registry = MockRegistry::start().await?;
+ let served_file = registry.file("config.json")?;
+ let temporary_directory = tempdir()?;
+ let destination = temporary_directory.path().join(&served_file.file.name);
+ let crc_path = std::path::PathBuf::from(format!("{}.crc", destination.display()));
+ let resume_artifact = destination.with_extension("part");
+ let lock_path = std::path::PathBuf::from(format!("{}.lock", destination.display()));
+ tokio::fs::write(&destination, b"corrupt").await?;
+ tokio::fs::write(&crc_path, served_file.crc32c()?).await?;
+ tokio::fs::write(&resume_artifact, b"partial").await?;
+ tokio::fs::write(
+ &lock_path,
+ serde_json::to_vec(&serde_json::json!({
+ "manager_id": "foreign-manager",
+ "acquired_at": Utc::now(),
+ "process_id": std::process::id(),
+ }))?,
+ )
+ .await?;
+
+ let task = manager_task(
+ &served_file.file.url,
+ &destination,
+ FileCheck::CRC(served_file.crc32c()?),
+ Some(served_file.file.size as u64),
+ )
+ .await?;
+
+ assert!(matches!(task.state().await.phase, FileDownloadPhase::LockedByOther(_)));
+ assert!(destination.exists());
+ assert!(crc_path.exists());
+ assert!(resume_artifact.exists());
+ Ok(())
+}
+
+async fn manager_task(
+ source_url: &str,
+ destination: &std::path::Path,
+ file_check: FileCheck,
+ expected_bytes: Option<u64>,
+) -> Result<std::sync::Arc<dyn download_manager::FileDownloadTask>, download_manager::DownloadError> {
+ let manager = <dyn FileDownloadManager>::new(FileDownloadManagerType::Universal, TokioHandle::current()).await?;
+ manager.file_download_task(source_url, destination, file_check, expected_bytes).await
+}
diff --git a/crates/download-manager/tests/unit/backends/common/test_state_and_lock_regression.rs b/crates/download-manager/tests/unit/backends/common/test_state_and_lock_regression.rs
new file mode 100644
index 000000000..1013676be
--- /dev/null
+++ b/crates/download-manager/tests/unit/backends/common/test_state_and_lock_regression.rs
@@ -0,0 +1,160 @@
+use std::sync::Arc;
+
+use download_manager::{DownloadError, FileCheck, FileDownloadManager, FileDownloadManagerType, FileDownloadPhase};
+use rstest::rstest;
+use tokio::runtime::Handle as TokioHandle;
+
+use crate::common::{Behavior, MockRegistry, wait_for_phase};
+
+#[rstest]
+#[case::universal(FileDownloadManagerType::Universal)]
+#[cfg_attr(target_vendor = "apple", case::apple(FileDownloadManagerType::Apple))]
+#[tokio::test(flavor = "multi_thread")]
+async fn test_concurrent_task_creation_returns_same_task(
+ #[case] download_manager_type: FileDownloadManagerType
+) -> Result<(), Box<dyn std::error::Error>> {
+ let registry = MockRegistry::start().await?;
+ let tokenizer = registry.file("tokenizer.json")?;
+ let temp_dir = tempfile::tempdir().unwrap();
+ let destination = temp_dir.path().join(&tokenizer.file.name);
+ let manager = <dyn FileDownloadManager>::new(download_manager_type, TokioHandle::current())
+ .await
+ .unwrap();
+
+ let (first, second) = tokio::join!(
+ manager.file_download_task(
+ &tokenizer.file.url,
+ &destination,
+ FileCheck::CRC(tokenizer.crc32c()?),
+ Some(tokenizer.file.size as u64),
+ ),
+ manager.file_download_task(
+ &tokenizer.file.url,
+ &destination,
+ FileCheck::CRC(tokenizer.crc32c()?),
+ Some(tokenizer.file.size as u64),
+ ),
+ );
+ let first = first.unwrap();
+ let second = second.unwrap();
+
+ assert!(Arc::ptr_eq(&first, &second));
+ Ok(())
+}
+
+#[rstest]
+#[case::universal(FileDownloadManagerType::Universal)]
+#[cfg_attr(target_vendor = "apple", case::apple(FileDownloadManagerType::Apple))]
+#[tokio::test(flavor = "multi_thread")]
+async fn different_urls_targeting_same_destination_are_rejected(
+ #[case] download_manager_type: FileDownloadManagerType,
+) -> Result<(), Box<dyn std::error::Error>> {
+ let registry = MockRegistry::start().await?;
+ let tokenizer = registry.file("tokenizer.json")?;
+ let temp_dir = tempfile::tempdir().unwrap();
+ let destination = temp_dir.path().join(&tokenizer.file.name);
+ let manager = <dyn FileDownloadManager>::new(download_manager_type, TokioHandle::current()).await.unwrap();
+
+ let _first = manager
+ .file_download_task(
+ &tokenizer.file.url,
+ &destination,
+ FileCheck::CRC(tokenizer.crc32c()?),
+ Some(tokenizer.file.size as u64),
+ )
+ .await
+ .unwrap();
+ let second = manager
+ .file_download_task(
+ "http://example.invalid/different-url",
+ &destination,
+ FileCheck::None,
+ Some(tokenizer.file.size as u64),
+ )
+ .await;
+
+ assert!(matches!(second, Err(DownloadError::ConflictingConfig(_))));
+ Ok(())
+}
+
+#[rstest]
+#[case::universal(FileDownloadManagerType::Universal)]
+#[cfg_attr(target_vendor = "apple", case::apple(FileDownloadManagerType::Apple))]
+#[tokio::test(flavor = "multi_thread")]
+async fn different_expected_bytes_targeting_same_destination_are_rejected(
+ #[case] download_manager_type: FileDownloadManagerType,
+) -> Result<(), Box<dyn std::error::Error>> {
+ let registry = MockRegistry::start().await?;
+ let tokenizer = registry.file("tokenizer.json")?;
+ let temp_dir = tempfile::tempdir().unwrap();
+ let destination = temp_dir.path().join(&tokenizer.file.name);
+ let manager = <dyn FileDownloadManager>::new(download_manager_type, TokioHandle::current()).await.unwrap();
+
+ let _first = manager
+ .file_download_task(
+ &tokenizer.file.url,
+ &destination,
+ FileCheck::CRC(tokenizer.crc32c()?),
+ Some(tokenizer.file.size as u64),
+ )
+ .await
+ .unwrap();
+ let second = manager
+ .file_download_task(
+ &tokenizer.file.url,
+ &destination,
+ FileCheck::CRC(tokenizer.crc32c()?),
+ Some(tokenizer.file.size as u64 + 1),
+ )
+ .await;
+
+ assert!(matches!(second, Err(DownloadError::ConflictingConfig(_))));
+ Ok(())
+}
+
+#[rstest]
+#[case::universal(FileDownloadManagerType::Universal)]
+#[cfg_attr(target_vendor = "apple", case::apple(FileDownloadManagerType::Apple))]
+#[tokio::test(flavor = "multi_thread")]
+async fn separate_managers_in_same_process_cannot_share_destination_lock(
+ #[case] download_manager_type: FileDownloadManagerType,
+) -> Result<(), Box<dyn std::error::Error>> {
+ let registry = MockRegistry::start_with(Behavior::THROTTLED).await?;
+ let tokenizer = registry.file("tokenizer.json")?;
+ let temp_dir = tempfile::tempdir().unwrap();
+ let destination = temp_dir.path().join(&tokenizer.file.name);
+
+ let manager_a = <dyn FileDownloadManager>::new(download_manager_type, TokioHandle::current()).await.unwrap();
+ let task_a = manager_a
+ .file_download_task(
+ &tokenizer.file.url,
+ &destination,
+ FileCheck::CRC(tokenizer.crc32c()?),
+ Some(tokenizer.file.size as u64),
+ )
+ .await
+ .unwrap();
+ let mut progress_a = task_a.progress().await.unwrap();
+ task_a.download().await.unwrap();
+ wait_for_phase(&task_a, &mut progress_a, |phase| matches!(phase, FileDownloadPhase::Downloading)).await;
+
+ let manager_b = <dyn FileDownloadManager>::new(download_manager_type, TokioHandle::current()).await.unwrap();
+ let task_b = manager_b
+ .file_download_task(
+ &tokenizer.file.url,
+ &destination,
+ FileCheck::CRC(tokenizer.crc32c()?),
+ Some(tokenizer.file.size as u64),
+ )
+ .await
+ .unwrap();
+
+ assert!(matches!(task_b.state().await.phase, FileDownloadPhase::LockedByOther(_)));
+
+ drop(task_b);
+ drop(manager_b);
+
+ let progress_a_after_b_dropped = task_a.state().await.phase;
+ assert!(!matches!(progress_a_after_b_dropped, FileDownloadPhase::Error(_)));
+ Ok(())
+}
diff --git a/crates/download-manager/tests/unit/backends/common/test_truncated_body.rs b/crates/download-manager/tests/unit/backends/common/test_truncated_body.rs
new file mode 100644
index 000000000..0c1f6abc2
--- /dev/null
+++ b/crates/download-manager/tests/unit/backends/common/test_truncated_body.rs
@@ -0,0 +1,43 @@
+use download_manager::{FileCheck, FileDownloadManager, FileDownloadManagerType, FileDownloadPhase};
+use rstest::rstest;
+use tokio::runtime::Handle as TokioHandle;
+
+use crate::common::{Behavior, MockRegistry, wait_for_phase};
+
+#[rstest]
+#[case::universal(FileDownloadManagerType::Universal)]
+#[cfg_attr(target_vendor = "apple", case::apple(FileDownloadManagerType::Apple))]
+#[tokio::test(flavor = "multi_thread")]
+async fn truncated_body_without_crc_fails_length_check(
+ #[case] download_manager_type: FileDownloadManagerType,
+) -> Result<(), Box<dyn std::error::Error>> {
+ let registry = MockRegistry::start_with(Behavior::TRUNCATE_BODY).await?;
+ let served_file = registry.file("config.json")?;
+ let temp_dir = tempfile::tempdir().unwrap();
+ let destination = temp_dir.path().join(&served_file.file.name);
+
+ let manager = <dyn FileDownloadManager>::new(download_manager_type, TokioHandle::current()).await.unwrap();
+ let task = manager
+ .file_download_task(
+ &served_file.file.url,
+ &destination,
+ FileCheck::None,
+ Some(served_file.file.size as u64),
+ )
+ .await
+ .unwrap();
+ let mut progress = task.progress().await.unwrap();
+
+ task.download().await.unwrap();
+ let state = wait_for_phase(&task, &mut progress, |phase| {
+ matches!(phase, FileDownloadPhase::Downloaded | FileDownloadPhase::Error(_))
+ })
+ .await;
+
+ assert!(
+ matches!(state.phase, FileDownloadPhase::Error(_)),
+ "truncated body must surface as Error, got {:?}",
+ state.phase,
+ );
+ Ok(())
+}
diff --git a/crates/download-manager/tests/unit/backends/mod.rs b/crates/download-manager/tests/unit/backends/mod.rs
new file mode 100644
index 000000000..ddd4277f2
--- /dev/null
+++ b/crates/download-manager/tests/unit/backends/mod.rs
@@ -0,0 +1,4 @@
+#[cfg(target_vendor = "apple")]
+mod apple;
+mod common;
+mod universal;
diff --git a/crates/download-manager/tests/unit/backends/universal/mod.rs b/crates/download-manager/tests/unit/backends/universal/mod.rs
new file mode 100644
index 000000000..522185cd3
--- /dev/null
+++ b/crates/download-manager/tests/unit/backends/universal/mod.rs
@@ -0,0 +1,2 @@
+mod test_resume;
+mod test_resume_content_range_validation;
diff --git a/crates/download-manager/tests/unit/backends/universal/test_resume.rs b/crates/download-manager/tests/unit/backends/universal/test_resume.rs
new file mode 100644
index 000000000..02a2a6033
--- /dev/null
+++ b/crates/download-manager/tests/unit/backends/universal/test_resume.rs
@@ -0,0 +1,39 @@
+use std::time::Duration;
+
+use download_manager::{FileCheck, FileDownloadManager, FileDownloadManagerType, FileDownloadPhase};
+use tempfile::tempdir;
+use tokio::time::timeout;
+use wiremock::{
+ Mock, MockServer, ResponseTemplate,
+ matchers::{method, path},
+};
+
+#[tokio::test(flavor = "multi_thread")]
+async fn test_universal_resume_restarts_when_server_ignores_range() -> Result<(), Box<dyn std::error::Error>> {
+ let bytes = b"complete file body";
+ let server = MockServer::start().await;
+ Mock::given(method("GET"))
+ .and(path("/model.bin"))
+ .respond_with(ResponseTemplate::new(200).set_body_bytes(bytes.as_slice()))
+ .mount(&server)
+ .await;
+
+ let temporary_directory = tempdir().unwrap();
+ let destination = temporary_directory.path().join("model.bin");
+ let part_path = destination.with_extension("part");
+ tokio::fs::write(&part_path, b"partial").await?;
+
+ let manager =
+ <dyn FileDownloadManager>::new(FileDownloadManagerType::Universal, tokio::runtime::Handle::current()).await?;
+ let task = manager
+ .file_download_task(&format!("{}/model.bin", server.uri()), &destination, FileCheck::None, Some(bytes.len() as u64))
+ .await?;
+
+ assert_eq!(task.state().await.phase, FileDownloadPhase::Paused);
+ task.download().await?;
+ timeout(Duration::from_secs(10), task.wait()).await?;
+
+ assert_eq!(task.state().await.phase, FileDownloadPhase::Downloaded);
+ assert_eq!(tokio::fs::read(&destination).await?, bytes);
+ Ok(())
+}
diff --git a/crates/download-manager/tests/unit/backends/universal/test_resume_content_range_validation.rs b/crates/download-manager/tests/unit/backends/universal/test_resume_content_range_validation.rs
new file mode 100644
index 000000000..76024c45e
--- /dev/null
+++ b/crates/download-manager/tests/unit/backends/universal/test_resume_content_range_validation.rs
@@ -0,0 +1,62 @@
+use std::time::Duration;
+
+use download_manager::{FileCheck, FileDownloadManager, FileDownloadManagerType, FileDownloadPhase};
+use tempfile::tempdir;
+use tokio::time::timeout;
+use wiremock::{
+ Mock, MockServer, ResponseTemplate,
+ matchers::{method, path},
+};
+
+#[tokio::test(flavor = "multi_thread")]
+async fn resume_rejects_206_when_content_range_offset_mismatches_request()
+-> Result<(), Box<dyn std::error::Error>> {
+ let full_bytes: &[u8] = b"abcdefghij";
+ let partial_bytes: &[u8] = b"abcde";
+ let total = full_bytes.len();
+
+ let server = MockServer::start().await;
+ Mock::given(method("GET"))
+ .and(path("/model.bin"))
+ .respond_with(
+ ResponseTemplate::new(206)
+ .set_body_bytes(partial_bytes)
+ .insert_header("Content-Range", format!("bytes 0-{}/{}", total - 1, total)),
+ )
+ .mount(&server)
+ .await;
+
+ let temporary_directory = tempdir().unwrap();
+ let destination = temporary_directory.path().join("model.bin");
+ let part_path = destination.with_extension("part");
+ tokio::fs::write(&part_path, partial_bytes).await?;
+
+ let manager =
+ <dyn FileDownloadManager>::new(FileDownloadManagerType::Universal, tokio::runtime::Handle::current()).await?;
+ let task = manager
+ .file_download_task(
+ &format!("{}/model.bin", server.uri()),
+ &destination,
+ FileCheck::None,
+ Some(total as u64),
+ )
+ .await?;
+
+ assert_eq!(task.state().await.phase, FileDownloadPhase::Paused);
+ task.download().await?;
+ timeout(Duration::from_secs(10), task.wait()).await?;
+
+ let final_phase = task.state().await.phase;
+ match &final_phase {
+ FileDownloadPhase::Error(_) => Ok(()),
+ FileDownloadPhase::Downloaded => {
+ assert_eq!(
+ tokio::fs::read(&destination).await?,
+ full_bytes,
+ "phase reached Downloaded but file content was corrupted by accepting a misaligned Content-Range",
+ );
+ Ok(())
+ },
+ other => panic!("unexpected final phase: {:?}", other),
+ }
+}
diff --git a/crates/download-manager/tests/unit/mod.rs b/crates/download-manager/tests/unit/mod.rs
index 1170c23f9..0637eea96 100644
--- a/crates/download-manager/tests/unit/mod.rs
+++ b/crates/download-manager/tests/unit/mod.rs
@@ -1,2 +1,2 @@
-mod lifecycle_test;
-mod universal;
+mod backends;
+mod test_lock_manager;
diff --git a/crates/download-manager/tests/unit/test_entry.rs b/crates/download-manager/tests/unit/test_entry.rs
index c0d36c57c..b2d060b3d 100644
--- a/crates/download-manager/tests/unit/test_entry.rs
+++ b/crates/download-manager/tests/unit/test_entry.rs
@@ -1,4 +1,10 @@
+extern crate self as download_manager;
+
+include!("../../src/lib.rs");
+
#[path = "../common/mod.rs"]
mod common;
-include!("mod.rs");
+mod unit_tests {
+ include!("mod.rs");
+}
diff --git a/crates/download-manager/tests/unit/test_lock_manager.rs b/crates/download-manager/tests/unit/test_lock_manager.rs
new file mode 100644
index 000000000..fd0c6516f
--- /dev/null
+++ b/crates/download-manager/tests/unit/test_lock_manager.rs
@@ -0,0 +1,228 @@
+use std::path::PathBuf;
+
+use chrono::{Duration as ChronoDuration, Utc};
+use download_manager::{LockFileInfo, acquire_lock, release_lock_if_owned};
+use uuid::Uuid;
+
+use crate::lock_manager::{
+ ReclaimExpectation, ReclaimOutcome, RestoreOutcome, reclaim_stale_lock, try_restore_quarantine,
+};
+
+fn lock_path(temp: &tempfile::TempDir) -> PathBuf {
+ temp.path().join("destination.lock")
+}
+
+#[tokio::test(flavor = "multi_thread")]
+async fn acquire_lock_does_not_overwrite_other_managers_lock() -> Result<(), Box<dyn std::error::Error>> {
+ let temp = tempfile::tempdir()?;
+ let path = lock_path(&temp);
+
+ acquire_lock(&path, "manager-a", Uuid::new_v4()).await?;
+ let acquired_by_b = acquire_lock(&path, "manager-b", Uuid::new_v4()).await;
+
+ assert!(acquired_by_b.is_err());
+
+ let info: LockFileInfo = serde_json::from_str(&std::fs::read_to_string(&path)?)?;
+ assert_eq!(info.manager_id, "manager-a");
+ Ok(())
+}
+
+#[tokio::test(flavor = "multi_thread")]
+async fn acquire_lock_reclaims_stale_locks() -> Result<(), Box<dyn std::error::Error>> {
+ let temp = tempfile::tempdir()?;
+ let path = lock_path(&temp);
+
+ let stale_info = LockFileInfo {
+ manager_id: "manager-a".to_string(),
+ instance_id: Uuid::new_v4(),
+ acquired_at: Utc::now() - ChronoDuration::hours(2),
+ process_id: 999_999,
+ };
+ std::fs::write(&path, serde_json::to_string(&stale_info)?)?;
+
+ acquire_lock(&path, "manager-b", Uuid::new_v4()).await?;
+
+ let info: LockFileInfo = serde_json::from_str(&std::fs::read_to_string(&path)?)?;
+ assert_eq!(info.manager_id, "manager-b");
+ Ok(())
+}
+
+#[tokio::test(flavor = "multi_thread")]
+async fn acquire_lock_is_idempotent_for_same_owner() -> Result<(), Box<dyn std::error::Error>> {
+ let temp = tempfile::tempdir()?;
+ let path = lock_path(&temp);
+ let instance_id = Uuid::new_v4();
+
+ acquire_lock(&path, "manager-a", instance_id).await?;
+ acquire_lock(&path, "manager-a", instance_id).await?;
+
+ let info: LockFileInfo = serde_json::from_str(&std::fs::read_to_string(&path)?)?;
+ assert_eq!(info.manager_id, "manager-a");
+ assert_eq!(info.process_id, std::process::id());
+ Ok(())
+}
+
+#[tokio::test(flavor = "multi_thread")]
+async fn acquire_lock_reclaims_old_unparseable_lock() -> Result<(), Box<dyn std::error::Error>> {
+ let temp = tempfile::tempdir()?;
+ let path = lock_path(&temp);
+
+ std::fs::write(&path, b"this is not valid json")?;
+ let two_hours_ago = std::time::SystemTime::now() - std::time::Duration::from_secs(2 * 60 * 60);
+ let times = std::fs::FileTimes::new().set_modified(two_hours_ago).set_accessed(two_hours_ago);
+ let lock_handle = std::fs::OpenOptions::new().write(true).open(&path)?;
+ lock_handle.set_times(times)?;
+ drop(lock_handle);
+
+ acquire_lock(&path, "manager-b", Uuid::new_v4()).await?;
+
+ let info: LockFileInfo = serde_json::from_str(&std::fs::read_to_string(&path)?)?;
+ assert_eq!(info.manager_id, "manager-b");
+ Ok(())
+}
+
+#[tokio::test(flavor = "multi_thread")]
+async fn acquire_lock_does_not_steal_a_recent_unparseable_lock() -> Result<(), Box<dyn std::error::Error>> {
+ let temp = tempfile::tempdir()?;
+ let path = lock_path(&temp);
+
+ std::fs::write(&path, b"")?;
+
+ let result = acquire_lock(&path, "manager-b", Uuid::new_v4()).await;
+
+ assert!(result.is_err());
+ Ok(())
+}
+
+#[tokio::test(flavor = "multi_thread")]
+async fn acquire_lock_blocks_same_manager_id_with_different_instance_id() -> Result<(), Box<dyn std::error::Error>> {
+ let temp = tempfile::tempdir()?;
+ let path = lock_path(&temp);
+
+ acquire_lock(&path, "manager-a", Uuid::new_v4()).await?;
+ let result = acquire_lock(&path, "manager-a", Uuid::new_v4()).await;
+
+ assert!(result.is_err());
+ Ok(())
+}
+
+#[tokio::test(flavor = "multi_thread")]
+async fn check_lock_file_treats_disappearing_lock_as_missing() -> Result<(), Box<dyn std::error::Error>> {
+ let temp = tempfile::tempdir()?;
+ let path = lock_path(&temp);
+
+ let result = acquire_lock(&path, "self-manager", Uuid::new_v4()).await;
+
+ assert!(result.is_ok());
+ Ok(())
+}
+
+#[tokio::test(flavor = "multi_thread")]
+async fn release_lock_if_owned_leaves_other_owners_lock_intact() -> Result<(), Box<dyn std::error::Error>> {
+ let temp = tempfile::tempdir()?;
+ let path = lock_path(&temp);
+
+ let other_owner = LockFileInfo {
+ manager_id: "other-manager".to_string(),
+ instance_id: Uuid::new_v4(),
+ acquired_at: Utc::now(),
+ process_id: std::process::id(),
+ };
+ std::fs::write(&path, serde_json::to_string(&other_owner)?)?;
+
+ let released = release_lock_if_owned(&path, "self-manager", Uuid::new_v4()).await?;
+
+ assert!(!released);
+ let info: LockFileInfo = serde_json::from_str(&std::fs::read_to_string(&path)?)?;
+ assert_eq!(info.manager_id, "other-manager");
+ Ok(())
+}
+
+#[tokio::test(flavor = "multi_thread")]
+async fn release_lock_if_owned_removes_our_own_lock() -> Result<(), Box<dyn std::error::Error>> {
+ let temp = tempfile::tempdir()?;
+ let path = lock_path(&temp);
+ let instance_id = Uuid::new_v4();
+
+ acquire_lock(&path, "self-manager", instance_id).await?;
+ let released = release_lock_if_owned(&path, "self-manager", instance_id).await?;
+
+ assert!(released);
+ assert!(!path.exists());
+ Ok(())
+}
+
+#[tokio::test(flavor = "multi_thread")]
+async fn acquire_lock_does_not_steal_live_lock_from_other_manager() -> Result<(), Box<dyn std::error::Error>> {
+ let temp = tempfile::tempdir()?;
+ let path = lock_path(&temp);
+
+ let live_owner = LockFileInfo {
+ manager_id: "other-manager".to_string(),
+ instance_id: Uuid::new_v4(),
+ acquired_at: Utc::now() - ChronoDuration::hours(2),
+ process_id: std::process::id(),
+ };
+ std::fs::write(&path, serde_json::to_string(&live_owner)?)?;
+
+ let result = acquire_lock(&path, "manager-b", Uuid::new_v4()).await;
+
+ assert!(result.is_err());
+ let info: LockFileInfo = serde_json::from_str(&std::fs::read_to_string(&path)?)?;
+ assert_eq!(info.manager_id, "other-manager");
+ Ok(())
+}
+
+#[tokio::test(flavor = "multi_thread")]
+async fn reclaim_unparseable_snapshot_refuses_when_bytes_changed() -> Result<(), Box<dyn std::error::Error>> {
+ let temp = tempfile::tempdir()?;
+ let path = lock_path(&temp);
+ std::fs::write(&path, b"different bytes than the original snapshot")?;
+
+ let stale_snapshot = b"old garbage that was observed earlier".to_vec();
+ let reclaimed = reclaim_stale_lock(&path, ReclaimExpectation::UnparseableSnapshot(stale_snapshot)).await?;
+
+ assert!(matches!(reclaimed, ReclaimOutcome::Changed));
+ let restored = std::fs::read(&path)?;
+ assert_eq!(restored, b"different bytes than the original snapshot");
+ Ok(())
+}
+
+#[tokio::test(flavor = "multi_thread")]
+async fn reclaim_unparseable_snapshot_succeeds_when_bytes_match() -> Result<(), Box<dyn std::error::Error>> {
+ let temp = tempfile::tempdir()?;
+ let path = lock_path(&temp);
+ let snapshot = b"identical garbage".to_vec();
+ std::fs::write(&path, &snapshot)?;
+
+ let reclaimed = reclaim_stale_lock(&path, ReclaimExpectation::UnparseableSnapshot(snapshot)).await?;
+
+ assert_eq!(reclaimed, ReclaimOutcome::Reclaimed);
+ assert!(!path.exists());
+ Ok(())
+}
+
+#[tokio::test(flavor = "multi_thread")]
+async fn try_restore_quarantine_does_not_overwrite_a_new_lock_at_destination()
+-> Result<(), Box<dyn std::error::Error>> {
+ let temp = tempfile::tempdir()?;
+ let lock_target = lock_path(&temp);
+ let quarantine = lock_target.with_extension("reclaim-test");
+ std::fs::write(&quarantine, b"quarantined-stale-content")?;
+
+ let new_owner = LockFileInfo {
+ manager_id: "new-owner".to_string(),
+ instance_id: Uuid::new_v4(),
+ acquired_at: Utc::now(),
+ process_id: std::process::id(),
+ };
+ std::fs::write(&lock_target, serde_json::to_string(&new_owner)?)?;
+
+ let restore_outcome = try_restore_quarantine(&quarantine, &lock_target).await?;
+
+ assert_eq!(restore_outcome, RestoreOutcome::DestinationAlreadyExists);
+ assert!(!quarantine.exists());
+ let intact: LockFileInfo = serde_json::from_str(&std::fs::read_to_string(&lock_target)?)?;
+ assert_eq!(intact.manager_id, "new-owner");
+ Ok(())
+}
diff --git a/crates/download-manager/tests/unit/universal/corrupt_body_test.rs b/crates/download-manager/tests/unit/universal/corrupt_body_test.rs
deleted file mode 100644
index ae480c730..000000000
--- a/crates/download-manager/tests/unit/universal/corrupt_body_test.rs
+++ /dev/null
@@ -1,32 +0,0 @@
-use download_manager::{FileCheck, FileDownloadManagerType, FileDownloadPhase, create_download_manager};
-use tokio::runtime::Handle as TokioHandle;
-
-use crate::common::{Behavior, MockRegistry, error_message, wait_for_phase};
-
-#[tokio::test(flavor = "multi_thread")]
-async fn test_universal_corrupt_body_fails_crc() -> Result<(), Box<dyn std::error::Error>> {
- let registry = MockRegistry::start_with(Behavior::CORRUPT_BODY).await?;
- let tokenizer = registry.file("tokenizer.json")?;
- let temp_dir = tempfile::tempdir().unwrap();
- let destination = temp_dir.path().join(&tokenizer.file.name);
- let manager = create_download_manager(FileDownloadManagerType::Universal, TokioHandle::current()).await.unwrap();
- let task = manager
- .file_download_task(
- &tokenizer.file.url,
- &destination,
- FileCheck::CRC(tokenizer.crc32c()?),
- Some(tokenizer.file.size as u64),
- )
- .await
- .unwrap();
- let mut progress = task.progress().await.unwrap();
-
- task.download().await.unwrap();
- let state = wait_for_phase(&task, &mut progress, |phase| matches!(phase, FileDownloadPhase::Error(_))).await;
- let message = error_message(state);
- assert!(
- message.contains("CRC") || message.contains("checksum"),
- "unexpected error: {message}"
- );
- Ok(())
-}
diff --git a/crates/download-manager/tests/unit/universal/mod.rs b/crates/download-manager/tests/unit/universal/mod.rs
deleted file mode 100644
index 0b67dbb8c..000000000
--- a/crates/download-manager/tests/unit/universal/mod.rs
+++ /dev/null
@@ -1,2 +0,0 @@
-mod corrupt_body_test;
-mod state_and_lock_regression_test;
diff --git a/crates/download-manager/tests/unit/universal/state_and_lock_regression_test.rs b/crates/download-manager/tests/unit/universal/state_and_lock_regression_test.rs
deleted file mode 100644
index 790245e10..000000000
--- a/crates/download-manager/tests/unit/universal/state_and_lock_regression_test.rs
+++ /dev/null
@@ -1,71 +0,0 @@
-use std::{path::PathBuf, sync::Arc};
-
-use chrono::Utc;
-use download_manager::{FileCheck, FileDownloadManagerType, FileDownloadPhase, create_download_manager};
-use tokio::{fs::write as tokio_write, runtime::Handle as TokioHandle};
-
-use crate::common::MockRegistry;
-
-#[tokio::test(flavor = "multi_thread")]
-async fn test_universal_foreign_lock_surfaces_locked_state() -> Result<(), Box<dyn std::error::Error>> {
- let registry = MockRegistry::start().await?;
- let model_weights = registry.file("model.safetensors")?;
- let temp_dir = tempfile::tempdir().unwrap();
- let destination = temp_dir.path().join(&model_weights.file.name);
- let lock_path = PathBuf::from(format!("{}.lock", destination.display()));
- let foreign_lock = serde_json::json!({
- "manager_id": "other-manager",
- "acquired_at": Utc::now(),
- "process_id": std::process::id(),
- });
- tokio_write(&lock_path, serde_json::to_vec(&foreign_lock).unwrap())
- .await
- .unwrap();
-
- let manager = create_download_manager(FileDownloadManagerType::Universal, TokioHandle::current()).await.unwrap();
- let task = manager
- .file_download_task(
- &model_weights.file.url,
- &destination,
- FileCheck::CRC(model_weights.crc32c()?),
- Some(model_weights.file.size as u64),
- )
- .await
- .unwrap();
-
- let state = task.state().await;
- assert!(matches!(state.phase, FileDownloadPhase::LockedByOther(_)));
- Ok(())
-}
-
-#[tokio::test(flavor = "multi_thread")]
-async fn test_universal_concurrent_task_creation_returns_same_task() -> Result<(), Box<dyn std::error::Error>> {
- let registry = MockRegistry::start().await?;
- let tokenizer = registry.file("tokenizer.json")?;
- let temp_dir = tempfile::tempdir().unwrap();
- let destination = temp_dir.path().join(&tokenizer.file.name);
- let manager = create_download_manager(FileDownloadManagerType::Universal, TokioHandle::current()).await.unwrap();
-
- let (first, second) = tokio::join!(
- manager.file_download_task(
- &tokenizer.file.url,
- &destination,
- FileCheck::CRC(tokenizer.crc32c()?),
- Some(tokenizer.file.size as u64),
- ),
- manager.file_download_task(
- &tokenizer.file.url,
- &destination,
- FileCheck::CRC(tokenizer.crc32c()?),
- Some(tokenizer.file.size as u64),
- ),
- );
- let first = first.unwrap();
- let second = second.unwrap();
-
- assert!(
- Arc::ptr_eq(&first, &second),
- "same URL and destination should return the same task object"
- );
- Ok(())
-}
diff --git a/crates/mock-registry/src/behavior.rs b/crates/mock-registry/src/behavior.rs
index f540438bc..2e88528f1 100644
--- a/crates/mock-registry/src/behavior.rs
+++ b/crates/mock-registry/src/behavior.rs
@@ -3,5 +3,6 @@ bitflags::bitflags! {
pub struct Behavior: u8 {
const CORRUPT_BODY = 1 << 0;
const THROTTLED = 1 << 1;
+ const TRUNCATE_BODY = 1 << 2;
}
}
diff --git a/crates/mock-registry/src/file_server.rs b/crates/mock-registry/src/file_server.rs
index 8af6c287a..bdaf43ac8 100644
--- a/crates/mock-registry/src/file_server.rs
+++ b/crates/mock-registry/src/file_server.rs
@@ -94,6 +94,11 @@ async fn handle_file_request(
} else {
served_file.bytes.clone()
};
+ let bytes = if behavior.contains(Behavior::TRUNCATE_BODY) {
+ truncate_bytes(&bytes)
+ } else {
+ bytes
+ };
let file_size = bytes.len() as u64;
let range_header = request.headers().get(RANGE).and_then(|header| header.to_str().ok());
let (status, body_start, body_end_exclusive) =
@@ -134,6 +139,13 @@ fn corrupt_bytes(bytes: &Arc<[u8]>) -> Arc<[u8]> {
corrupted_bytes.into()
}
+fn truncate_bytes(bytes: &Arc<[u8]>) -> Arc<[u8]> {
+ if bytes.is_empty() {
+ return bytes.clone();
+ }
+ bytes[..bytes.len() - 1].to_vec().into()
+}
+
fn file_server_base_url(listener: &TcpListener) -> Result<String> {
let address = listener.local_addr()?;
Ok(match address {
diff --git a/crates/uzu/Cargo.toml b/crates/uzu/Cargo.toml
index 7793692aa..b170efbcd 100644
--- a/crates/uzu/Cargo.toml
+++ b/crates/uzu/Cargo.toml
@@ -125,10 +125,11 @@ tracing-appender.workspace = true
chrono.workspace = true
tempfile.workspace = true
indicatif.workspace = true
+dotenvy.workspace = true
schemars.workspace = true
serde_json.workspace = true
mock-registry.workspace = true
-dotenvy.workspace = true
+rstest.workspace = true
[[test]]
name = "unit"
diff --git a/crates/uzu/src/engine/downloader.rs b/crates/uzu/src/engine/downloader.rs
index ce5c5b85d..7a20fa4a5 100644
--- a/crates/uzu/src/engine/downloader.rs
+++ b/crates/uzu/src/engine/downloader.rs
@@ -18,7 +18,7 @@ pub struct Downloader {
}
impl Downloader {
- pub(crate) fn new(
+ pub fn new(
identifier: String,
storage: SharedAccess<Storage>,
) -> Self {
@@ -27,6 +27,43 @@ impl Downloader {
storage,
}
}
+
+ async fn wait_for_resume_to_be_observable(
+ &self,
+ mut stream: BroadcastStream<(String, DownloadState)>,
+ ) -> Result<(), EngineError> {
+ if self.state().await.is_some_and(|state| Self::is_resume_observable_phase(&state.phase)) {
+ return Ok(());
+ }
+
+ while let Some(result) = stream.next().await {
+ match result {
+ Ok((identifier, state)) if identifier == self.identifier => {
+ if Self::is_resume_observable_phase(&state.phase) {
+ return Ok(());
+ }
+ },
+ Ok(_) => {},
+ Err(error) => {
+ tracing::warn!(
+ identifier = self.identifier,
+ ?error,
+ "downloader resume stream lagged; some updates were dropped"
+ );
+ },
+ }
+ }
+
+ Err(EngineError::UnableToGetDownloaderProgressStream {})
+ }
+
+ fn is_progress_streaming_phase(phase: &DownloadPhase) -> bool {
+ matches!(phase, DownloadPhase::Downloading {})
+ }
+
+ fn is_resume_observable_phase(phase: &DownloadPhase) -> bool {
+ matches!(phase, DownloadPhase::Downloading {} | DownloadPhase::Downloaded {} | DownloadPhase::Error { .. })
+ }
}
#[bindings::export(Implementation)]
@@ -44,14 +81,19 @@ impl Downloader {
let result = match state.phase {
DownloadPhase::Downloading {} | DownloadPhase::Downloaded {} => Ok(()),
DownloadPhase::NotDownloaded {} | DownloadPhase::Paused {} | DownloadPhase::Locked {} => {
- Ok(self.storage.lock().await.download(&self.identifier).await?)
+ let stream = self.storage.lock().await.subscribe();
+ self.storage.lock().await.download(&self.identifier).await?;
+ self.wait_for_resume_to_be_observable(stream).await
},
DownloadPhase::Error {
..
} => {
+ let stream = self.storage.lock().await.subscribe();
let storage = self.storage.lock().await;
storage.delete(&self.identifier).await?;
- Ok(storage.download(&self.identifier).await?)
+ storage.download(&self.identifier).await?;
+ drop(storage);
+ self.wait_for_resume_to_be_observable(stream).await
},
};
result
@@ -69,15 +111,14 @@ impl Downloader {
#[bindings::export(Method)]
pub async fn progress(&self) -> Result<DownloaderStream, EngineError> {
- let identifier = self.identifier.clone();
let Some(state) = self.state().await else {
return Err(EngineError::UnableToGetDownloaderProgressStream {});
};
- if matches!(state.phase, DownloadPhase::Downloaded {}) {
- return Err(EngineError::UnableToGetDownloaderProgressStream {});
+ if !Self::is_progress_streaming_phase(&state.phase) {
+ return Ok(DownloaderStream::empty(self.identifier.clone()));
}
let stream = self.storage.lock().await.subscribe();
- Ok(DownloaderStream::new(identifier, stream))
+ Ok(DownloaderStream::new(self.identifier.clone(), stream))
}
}
@@ -114,25 +155,26 @@ impl DownloaderStream {
let mut stream_guard = self.stream.lock().await;
let stream = stream_guard.as_mut()?;
while let Some(result) = stream.next().await {
- if let Ok((identifier, state)) = result {
- if identifier == self.identifier {
- let update = DownloaderStreamUpdate {
- bytes_total: state.total_bytes,
- bytes_downloaded: state.downloaded_bytes,
- };
- match state.phase {
- DownloadPhase::Downloading {} | DownloadPhase::Locked {} => {},
- DownloadPhase::NotDownloaded {}
- | DownloadPhase::Downloaded {}
- | DownloadPhase::Error {
- ..
- }
- | DownloadPhase::Paused {} => {
+ match result {
+ Ok((identifier, state)) => {
+ if identifier == self.identifier {
+ let update = DownloaderStreamUpdate {
+ bytes_total: state.total_bytes,
+ bytes_downloaded: state.downloaded_bytes,
+ };
+ if !Downloader::is_progress_streaming_phase(&state.phase) {
*stream_guard = None;
- },
+ }
+ return Some(update);
}
- return Some(update);
- }
+ },
+ Err(error) => {
+ tracing::warn!(
+ identifier = self.identifier,
+ ?error,
+ "downloader progress stream lagged; some updates were dropped"
+ );
+ },
}
}
None
diff --git a/crates/uzu/src/engine/mod.rs b/crates/uzu/src/engine/mod.rs
index ced544ad2..92b6d75d2 100644
--- a/crates/uzu/src/engine/mod.rs
+++ b/crates/uzu/src/engine/mod.rs
@@ -3,12 +3,13 @@ pub mod config;
mod downloader;
mod error;
-use std::{collections::HashMap, sync::Arc};
+use std::{collections::HashMap, path::PathBuf, sync::Arc};
use backend_remote::openai::Backend as OpenAIBackend;
use backend_uzu::inference::Backend as UzuBackend;
pub use callback::{EngineCallback, EngineCallbackType};
pub use config::EngineConfig;
+use download_manager::FileDownloadManagerType;
pub use downloader::{Downloader, DownloaderStream, DownloaderStreamUpdate};
pub use error::EngineError;
use indexmap::IndexSet;
@@ -52,35 +53,14 @@ pub struct Engine {
impl Engine {
pub async fn new(config: EngineConfig) -> Result<Self, EngineError> {
- let tokio_handle = Handle::try_current().map_err(|error| EngineError::TokioError {
- message: error.to_string(),
- })?;
-
- let settings = if let Some(application_identifier) = &config.application_identifier {
- Some(Settings::new(application_identifier.clone())?)
- } else {
- None
- };
- let mut config = config;
- if let Some(settings) = &settings {
- config.synchronize_with_settings(settings)?;
- }
-
- let device = Device::new()?;
- let registry = SharedAccess::new(MergedRegistry::new(vec![]));
- let storage_config = StorageConfig::new(device.clone(), None, "mirai".to_string());
- logs::start(storage_config.cache_path(), &storage_config.log_name(), false);
-
- let storage = SharedAccess::new(Storage::new(tokio_handle.clone(), storage_config).await?);
+ Self::new_with_download_manager_type(config, FileDownloadManagerType::default()).await
+ }
- let engine = Self {
- settings: SharedAccess::new(settings),
- storage,
- registry,
- backends: SharedAccess::new(HashMap::new()),
- callback: SharedAccess::new(None),
- };
- engine.spawn_storage_listener().await;
+ pub async fn new_with_download_manager_type(
+ config: EngineConfig,
+ download_manager_type: FileDownloadManagerType,
+ ) -> Result<Self, EngineError> {
+ let (engine, config, device) = Self::new_base(config, download_manager_type, None).await?;
{
let uzu_backend = UzuBackend::new();
@@ -151,6 +131,44 @@ impl Engine {
Ok(engine)
}
+
+ async fn new_base(
+ config: EngineConfig,
+ download_manager_type: FileDownloadManagerType,
+ cache_path: Option<PathBuf>,
+ ) -> Result<(Self, EngineConfig, Device), EngineError> {
+ let tokio_handle = Handle::try_current().map_err(|error| EngineError::TokioError {
+ message: error.to_string(),
+ })?;
+
+ let settings = if let Some(application_identifier) = &config.application_identifier {
+ Some(Settings::new(application_identifier.clone())?)
+ } else {
+ None
+ };
+ let mut config = config;
+ if let Some(settings) = &settings {
+ config.synchronize_with_settings(settings)?;
+ }
+
+ let device = Device::new()?;
+ let registry = SharedAccess::new(MergedRegistry::new(vec![]));
+ let storage_config = StorageConfig::new(device.clone(), cache_path, "mirai".to_string())
+ .with_download_manager_type(download_manager_type);
+ logs::start(storage_config.cache_path(), &storage_config.log_name(), false);
+
+ let storage = SharedAccess::new(Storage::new(tokio_handle, storage_config).await?);
+
+ let engine = Self {
+ settings: SharedAccess::new(settings),
+ storage,
+ registry,
+ backends: SharedAccess::new(HashMap::new()),
+ callback: SharedAccess::new(None),
+ };
+ engine.spawn_storage_listener().await;
+ Ok((engine, config, device))
+ }
}
#[bindings::export(Implementation)]
diff --git a/crates/uzu/src/logs/mod.rs b/crates/uzu/src/logs/mod.rs
index 6b8c5152b..06ba9da62 100644
--- a/crates/uzu/src/logs/mod.rs
+++ b/crates/uzu/src/logs/mod.rs
@@ -9,8 +9,8 @@ use tracing_subscriber::{EnvFilter, fmt, layer::SubscriberExt, util::SubscriberI
static INIT: Once = Once::new();
-const MAX_LOG_SIZE_BYTES: u64 = 10 * 1024 * 1024;
-const KEEP_LINES: usize = 5000;
+const MAX_LOG_SIZE_BYTES: u64 = 50 * 1024 * 1024;
+const KEEP_LINES: usize = 50000;
/// Initialize tracing for production use
/// Logs are written to a single file that gets trimmed when it exceeds MAX_LOG_SIZE_BYTES
@@ -47,9 +47,10 @@ fn init(
// Keep guard alive for the process lifetime
std::mem::forget(guard);
+ let default_filter = "info,download_manager=debug,uzu=debug";
let env_filter = EnvFilter::try_from_default_env()
- .or_else(|_| EnvFilter::try_new("info"))
- .unwrap_or_else(|_| EnvFilter::new("info"));
+ .or_else(|_| EnvFilter::try_new(default_filter))
+ .unwrap_or_else(|_| EnvFilter::new(default_filter));
let registry = tracing_subscriber::registry().with(env_filter).with(
fmt::layer()
diff --git a/crates/uzu/src/storage/config.rs b/crates/uzu/src/storage/config.rs
index 01ea647a5..951174b78 100644
--- a/crates/uzu/src/storage/config.rs
+++ b/crates/uzu/src/storage/config.rs
@@ -1,5 +1,6 @@
use std::path::PathBuf;
+use download_manager::FileDownloadManagerType;
use serde::{Deserialize, Serialize};
use shoji::types::model::Model;
@@ -11,6 +12,8 @@ pub struct Config {
pub device: Device,
pub base_path: Option<PathBuf>,
pub name: String,
+ #[serde(default)]
+ pub download_manager_type: FileDownloadManagerType,
}
impl Config {
@@ -23,6 +26,7 @@ impl Config {
device,
base_path,
name,
+ download_manager_type: FileDownloadManagerType::default(),
}
}
@@ -46,4 +50,14 @@ impl Config {
pub fn log_name(&self) -> String {
format!("{}.log", self.name)
}
+
+ pub fn with_download_manager_type(
+ &self,
+ download_manager_type: FileDownloadManagerType,
+ ) -> Self {
+ Self {
+ download_manager_type,
+ ..self.clone()
+ }
+ }
}
diff --git a/crates/uzu/src/storage/mod.rs b/crates/uzu/src/storage/mod.rs
index 5a5feb8c7..8db5aeaae 100644
--- a/crates/uzu/src/storage/mod.rs
+++ b/crates/uzu/src/storage/mod.rs
@@ -9,9 +9,7 @@ use std::{
};
pub use config::Config;
-use download_manager::{
- FileCheck, FileDownloadManager, FileDownloadManagerType, FileDownloadPhase, create_download_manager,
-};
+use download_manager::{FileCheck, FileDownloadManager, FileDownloadPhase};
pub use error::StorageError;
use futures_util::future::join_all;
use shoji::types::{
@@ -44,7 +42,7 @@ impl Storage {
})?;
let download_manager = SharedAccess::new(Arc::from(
- create_download_manager(FileDownloadManagerType::default(), tokio_handle.clone()).await.map_err(
+ <dyn FileDownloadManager>::new(config.download_manager_type, tokio_handle.clone()).await.map_err(
|error| StorageError::DownloadManager {
message: error.to_string(),
},
@@ -84,7 +82,9 @@ impl Storage {
for task in existing_file_tasks {
let task_state = task.state().await;
if matches!(task_state.phase, FileDownloadPhase::Error(_)) {
+ let download_id = task.download_id();
let _ = task.cancel().await;
+ let _ = download_manager.remove_file_task(download_id).await;
} else {
active_file_tasks.push(task);
}
@@ -98,6 +98,9 @@ impl Storage {
.collect();
for identifier in stale_model_identifiers {
if let Some(item) = items.remove(&identifier) {
+ if let Err(error) = item.detach_active_downloads().await {
+ tracing::warn!(?error, identifier, "failed to detach stale model file tasks");
+ }
item.stop_listening().await;
}
}
diff --git a/crates/uzu/src/storage/types/download_phase.rs b/crates/uzu/src/storage/types/download_phase.rs
index a02898c1e..1ce4fd709 100644
--- a/crates/uzu/src/storage/types/download_phase.rs
+++ b/crates/uzu/src/storage/types/download_phase.rs
@@ -15,11 +15,11 @@ pub enum DownloadPhase {
impl DownloadPhase {
pub fn is_in_progress(&self) -> bool {
- matches!(self, Self::Downloading {} | Self::Paused {})
+ matches!(self, Self::Downloading {})
}
pub fn can_pause(&self) -> bool {
- matches!(self, Self::Downloading {} | Self::Paused {})
+ matches!(self, Self::Downloading {})
}
pub fn can_delete(&self) -> bool {
diff --git a/crates/uzu/src/storage/types/download_state.rs b/crates/uzu/src/storage/types/download_state.rs
index 340cd2976..19f5874e1 100644
--- a/crates/uzu/src/storage/types/download_state.rs
+++ b/crates/uzu/src/storage/types/download_state.rs
@@ -132,7 +132,14 @@ pub fn reduce_file_download_states(file_states: &[FileDownloadState]) -> Downloa
return DownloadState::downloaded(total_bytes as i64);
}
- // Locked takes precedence over other in-progress states
+ if any_error {
+ if let Some(error_state) = file_states.iter().find(|f| matches!(f.phase, FileDownloadPhase::Error(_))) {
+ if let FileDownloadPhase::Error(err) = &error_state.phase {
+ return DownloadState::error(err.clone());
+ }
+ }
+ }
+
if any_locked {
return DownloadState::locked(downloaded_bytes as i64, total_bytes as i64);
}
@@ -145,14 +152,6 @@ pub fn reduce_file_download_states(file_states: &[FileDownloadState]) -> Downloa
return DownloadState::paused(downloaded_bytes as i64, total_bytes as i64);
}
- if any_error {
- if let Some(error_state) = file_states.iter().find(|f| matches!(f.phase, FileDownloadPhase::Error(_))) {
- if let FileDownloadPhase::Error(err) = &error_state.phase {
- return DownloadState::error(err.clone());
- }
- }
- }
-
// If some files are downloaded but not all, and nothing is actively
// downloading, treat as paused (partial progress, can resume)
if any_downloaded && downloaded_bytes > 0 {
diff --git a/crates/uzu/src/storage/types/item.rs b/crates/uzu/src/storage/types/item.rs
index 6b5597352..08ba0b331 100644
--- a/crates/uzu/src/storage/types/item.rs
+++ b/crates/uzu/src/storage/types/item.rs
@@ -1,10 +1,10 @@
use std::{
- fs::{create_dir_all, read_dir, remove_dir_all, remove_file},
- path::{Path, PathBuf},
+ fs::{create_dir_all, remove_dir_all},
+ path::PathBuf,
sync::Arc,
};
-use download_manager::{FileDownloadManager, FileDownloadState, FileDownloadTask};
+use download_manager::{DownloadError, FileDownloadManager, FileDownloadPhase, FileDownloadState, FileDownloadTask};
use futures_util::future::join_all;
use shoji::types::basic::File;
use tokio::{
@@ -21,7 +21,7 @@ use crate::{
helpers::SharedAccess,
storage::{
StorageError,
- types::{CrcSnapshot, DownloadPhase, DownloadState, StorageDownloadEventSender, reduce_file_download_states},
+ types::{DownloadPhase, DownloadState, StorageDownloadEventSender, reduce_file_download_states},
},
};
@@ -291,26 +291,14 @@ impl Item {
pub async fn download(&self) -> Result<(), StorageError> {
tracing::debug!("[MODEL] download() called for model: {}", self.identifier);
- // Ensure file tasks are initialized
let tasks_were_created = self.ensure_file_tasks().await?;
-
- // Only restart listener if file tasks were just created
if tasks_were_created {
tracing::debug!("[MODEL] Restarting listener for model: {} (file tasks created)", self.identifier);
self.stop_listening().await;
self.start_listening().await;
}
- // Reduce state after ensuring file tasks
let current_state = self.reduce_state().await;
- self.update_state_and_broadcast(current_state.clone()).await;
-
- tracing::debug!(
- "[MODEL] Current state: phase={:?}, can_transition={}",
- current_state.phase,
- Self::can_transition_to_downloading(&current_state.phase)
- );
-
if !Self::can_transition_to_downloading(&current_state.phase) {
return Err(StorageError::InvalidStateTransition {
from: current_state.phase.clone(),
@@ -319,31 +307,29 @@ impl Item {
}
tracing::debug!("[MODEL] Calling ensure_downloading");
+ self.ensure_downloading().await?;
- let result = self.ensure_downloading().await;
+ self.stop_listening().await;
+ self.start_listening().await;
- tracing::debug!("[MODEL] download() completed for model: {}", self.identifier);
+ let downloading_state = self.reduce_state().await;
+ self.update_state_and_broadcast(downloading_state).await;
- result
+ tracing::debug!("[MODEL] download() completed for model: {}", self.identifier);
+ Ok(())
}
pub async fn pause(&self) -> Result<(), StorageError> {
tracing::debug!("[MODEL] pause() called for model: {}", self.identifier);
- // Ensure file tasks are initialized
let tasks_were_created = self.ensure_file_tasks().await?;
-
- // Only restart listener if file tasks were just created
if tasks_were_created {
tracing::debug!("[MODEL] Restarting listener for model: {} (file tasks created)", self.identifier);
self.stop_listening().await;
self.start_listening().await;
}
- // Reduce state after ensuring file tasks
let current_state = self.reduce_state().await;
- self.update_state_and_broadcast(current_state.clone()).await;
-
if !current_state.can_pause() {
return Err(StorageError::InvalidStateTransition {
from: current_state.phase.clone(),
@@ -352,64 +338,56 @@ impl Item {
}
tracing::debug!("[MODEL] Calling ensure_paused");
+ self.ensure_paused().await?;
- let result = self.ensure_paused().await;
- if result.is_ok() {
- let file_tasks_guard = self.file_download_tasks.lock().await;
- let mut file_download_states = Vec::with_capacity(file_tasks_guard.len());
- for file_task in file_tasks_guard.iter() {
- file_download_states.push(file_task.state().await);
- }
- drop(file_tasks_guard);
-
- let mut cache_guard = self.file_download_states.lock().await;
- *cache_guard = file_download_states;
- drop(cache_guard);
-
- let paused_state = self.reduce_state().await;
- self.update_state_and_broadcast(paused_state).await;
+ let file_tasks_guard = self.file_download_tasks.lock().await;
+ let mut file_download_states = Vec::with_capacity(file_tasks_guard.len());
+ for file_task in file_tasks_guard.iter() {
+ file_download_states.push(file_task.state().await);
}
+ drop(file_tasks_guard);
- tracing::debug!("[MODEL] pause() completed for model: {}", self.identifier);
+ *self.file_download_states.lock().await = file_download_states;
+ let paused_state = self.reduce_state().await;
+ self.update_state_and_broadcast(paused_state).await;
- result
+ tracing::debug!("[MODEL] pause() completed for model: {}", self.identifier);
+ Ok(())
}
pub async fn cancel(&self) -> Result<(), StorageError> {
- // Ensure file tasks are initialized
let tasks_were_created = self.ensure_file_tasks().await?;
-
- // Only restart listener if file tasks were just created
if tasks_were_created {
tracing::debug!("[MODEL] Restarting listener for model: {} (file tasks created)", self.identifier);
self.stop_listening().await;
self.start_listening().await;
}
- // Reduce state after ensuring file tasks
- let current_state = self.reduce_state().await;
- self.update_state_and_broadcast(current_state.clone()).await;
-
- let file_tasks_guard = self.file_download_tasks.lock().await;
- for file_task in file_tasks_guard.iter() {
- let _ = file_task.cancel().await;
- }
- drop(file_tasks_guard);
-
for file_info in self.files.iter() {
- let file_path = self.cache_path.join(&file_info.name);
- if file_path.exists() {
- let _ = remove_file(&file_path);
- }
- let resume_data_path = format!("{}.resume_data", file_path.display());
- if Path::new(&resume_data_path).exists() {
- let _ = remove_file(&resume_data_path);
- }
- if let Some(filename) = file_path.file_name().and_then(|n| n.to_str()) {
- let _ = CrcSnapshot::remove_crc(&self.cache_path, filename);
+ let destination = self.cache_path.join(&file_info.name);
+ if let Some(owner) = self.file_download_manager.destination_foreign_lock(&destination).await {
+ tracing::info!(?destination, %owner, "refusing to cancel: destination is locked by another manager");
+ return Err(StorageError::InvalidStateTransition {
+ from: DownloadPhase::Locked {},
+ to: DownloadPhase::NotDownloaded {},
+ });
}
}
+ let current_state = self.reduce_state().await;
+ if matches!(current_state.phase, DownloadPhase::Locked {}) {
+ return Err(StorageError::InvalidStateTransition {
+ from: current_state.phase,
+ to: DownloadPhase::NotDownloaded {},
+ });
+ }
+
+ self.cancel_and_remove_active_file_tasks().await?;
+
+ self.stop_listening().await;
+ *self.file_download_tasks.lock().await = Vec::new();
+ *self.file_download_states.lock().await = Vec::new();
+
if self.cache_path.exists() {
let _ = remove_dir_all(&self.cache_path);
}
@@ -424,6 +402,35 @@ impl Item {
Ok(BroadcastStream::new(self.broadcast_sender.subscribe()))
}
+ pub async fn detach_active_downloads(&self) -> Result<(), StorageError> {
+ self.cancel_and_remove_active_file_tasks().await?;
+ *self.file_download_tasks.lock().await = Vec::new();
+ *self.file_download_states.lock().await = Vec::new();
+ Ok(())
+ }
+
+ async fn cancel_and_remove_active_file_tasks(&self) -> Result<(), StorageError> {
+ let file_tasks = self.file_download_tasks.lock().await.clone();
+ let cancel_futures = file_tasks.iter().map(|file_task| {
+ let file_task = file_task.clone();
+ let manager = self.file_download_manager.clone();
+ async move {
+ let download_id = file_task.download_id();
+ file_task.cancel().await?;
+ manager.remove_file_task(download_id).await?;
+ Ok::<(), download_manager::DownloadError>(())
+ }
+ });
+ let first_error = join_all(cancel_futures).await.into_iter().find_map(Result::err);
+
+ match first_error {
+ Some(error) => Err(StorageError::DownloadManager {
+ message: error.to_string(),
+ }),
+ None => Ok(()),
+ }
+ }
+
/// Handle file task state update
/// Called by ModelStorage listener when a file task broadcasts a state change
pub async fn handle_file_task_update(&self) {
@@ -452,11 +459,16 @@ impl Item {
let file_tasks_guard = self.file_download_tasks.lock().await;
let download_futures = file_tasks_guard.iter().map(|file_task| {
let file_task = file_task.clone();
- async move {
- let _ = file_task.download().await;
- }
+ async move { file_task.download().await }
});
- join_all(download_futures).await;
+ let first_error = join_all(download_futures).await.into_iter().find_map(Result::err);
+ drop(file_tasks_guard);
+
+ if let Some(error) = first_error {
+ return Err(StorageError::DownloadManager {
+ message: error.to_string(),
+ });
+ }
tracing::debug!("[MODEL] ensure_downloading: All file downloads initiated");
Ok(())
@@ -468,37 +480,31 @@ impl Item {
let pause_futures = file_tasks_guard.iter().map(|file_task| {
let file_task = file_task.clone();
async move {
- let _ = file_task.pause().await;
- }
- });
- join_all(pause_futures).await;
-
- tracing::debug!("[MODEL] ensure_paused: All file tasks paused");
- Ok(())
- }
-
- #[allow(dead_code)]
- async fn cleanup_empty_directory(&self) -> Result<(), StorageError> {
- if !self.cache_path.exists() {
- return Ok(());
- }
- let entries = read_dir(&self.cache_path).map_err(|error| StorageError::IO {
- message: error.to_string(),
- })?;
- let mut has_real_files = false;
- for entry in entries {
- if let Ok(entry) = entry {
- if let Some(name) = entry.file_name().to_str() {
- if !name.ends_with(".resume_data") && !name.ends_with(".crc") && !name.starts_with('.') {
- has_real_files = true;
- break;
+ if matches!(file_task.state().await.phase, FileDownloadPhase::Downloading) {
+ match file_task.pause().await {
+ Ok(()) => Ok(()),
+ Err(DownloadError::InvalidStateTransition)
+ if !matches!(file_task.state().await.phase, FileDownloadPhase::Downloading) =>
+ {
+ Ok(())
+ },
+ Err(error) => Err(error),
}
+ } else {
+ Ok(())
}
}
+ });
+ let first_error = join_all(pause_futures).await.into_iter().find_map(Result::err);
+ drop(file_tasks_guard);
+
+ if let Some(error) = first_error {
+ return Err(StorageError::DownloadManager {
+ message: error.to_string(),
+ });
}
- if !has_real_files {
- let _ = remove_dir_all(&self.cache_path);
- }
+
+ tracing::debug!("[MODEL] ensure_paused: All file tasks paused");
Ok(())
}
@@ -576,10 +582,11 @@ impl Item {
// Fan-in: per-stream forwarders into a single bounded channel
let num_streams = streams.len();
let (tx, mut rx) = tokio_mpsc_channel::<(usize, FileDownloadState)>(1024);
+ let mut forwarder_handles = Vec::with_capacity(num_streams);
for (idx, mut stream) in streams {
let tx = tx.clone();
- model.handle.spawn(async move {
+ let forwarder_handle = model.handle.spawn(async move {
while let Some(item) = stream.next().await {
match item {
Ok(state) => {
@@ -592,7 +599,11 @@ impl Item {
}
}
});
+ forwarder_handles.push(forwarder_handle);
}
+ let _forwarder_handles = ListenerForwarderHandles {
+ handles: forwarder_handles,
+ };
drop(tx); // close when all forwarders end
// Aggregator: coalesce bursts, keep only latest state per task
@@ -663,3 +674,15 @@ impl Item {
)
}
}
+
+struct ListenerForwarderHandles {
+ handles: Vec<TokioJoinHandle<()>>,
+}
+
+impl Drop for ListenerForwarderHandles {
+ fn drop(&mut self) {
+ for handle in &self.handles {
+ handle.abort();
+ }
+ }
+}
diff --git a/crates/uzu/tests/common/test_storage.rs b/crates/uzu/tests/common/test_storage.rs
index 2eb40d2a7..b8d148cb9 100644
--- a/crates/uzu/tests/common/test_storage.rs
+++ b/crates/uzu/tests/common/test_storage.rs
@@ -2,6 +2,7 @@
use std::path::PathBuf;
+use download_manager::FileDownloadManagerType;
use shoji::types::model::Model;
use tokio::runtime::Handle as TokioHandle;
use uzu::{
@@ -22,12 +23,21 @@ impl TestStorage {
pub async fn with_models(
tokio_handle: TokioHandle,
models: Vec<Model>,
+ ) -> Result<Self, Box<dyn std::error::Error>> {
+ Self::with_models_and_manager(tokio_handle, models, FileDownloadManagerType::default()).await
+ }
+
+ pub async fn with_models_and_manager(
+ tokio_handle: TokioHandle,
+ models: Vec<Model>,
+ download_manager_type: FileDownloadManagerType,
) -> Result<Self, Box<dyn std::error::Error>> {
let temp_dir_guard = tempfile::tempdir()?;
let base_path = temp_dir_guard.path().to_path_buf();
let registry = FixedRegistry::new("test_registry".to_string(), models.clone());
let device = Device::new()?;
- let config = Config::new(device, Some(base_path.clone()), "test_storage".to_string());
+ let config = Config::new(device, Some(base_path.clone()), "test_storage".to_string())
+ .with_download_manager_type(download_manager_type);
let storage = Storage::new(tokio_handle, config.clone()).await?;
storage.refresh(models).await?;
Ok(Self {
diff --git a/crates/uzu/tests/common/tracing_setup.rs b/crates/uzu/tests/common/tracing_setup.rs
index a6986d32c..8b73a0249 100644
--- a/crates/uzu/tests/common/tracing_setup.rs
+++ b/crates/uzu/tests/common/tracing_setup.rs
@@ -6,15 +6,10 @@ use tracing_subscriber::{EnvFilter, fmt, layer::SubscriberExt, util::SubscriberI
static INIT: Once = Once::new();
-/// Initialize tracing for tests with file output
-/// Logs are written to `test_logs/model_storage_<timestamp>.log`
pub fn init_test_tracing() {
INIT.call_once(|| {
let timestamp = chrono::Utc::now().format("%Y%m%d_%H%M%S");
-
- // Use workspace root for test_logs
- let log_dir =
- std::env::current_dir().unwrap_or_else(|_| std::path::PathBuf::from(".")).join("tests").join("logs");
+ let log_dir = std::env::temp_dir().join("uzu-storage-test-logs");
std::fs::create_dir_all(&log_dir).ok();
let file_appender = tracing_appender::rolling::never(&log_dir, format!("storage_{}.log", timestamp));
@@ -27,6 +22,7 @@ pub fn init_test_tracing() {
tracing_subscriber::registry()
.with(EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("debug")))
.with(fmt::layer().with_writer(non_blocking).with_ansi(false))
+ .with(fmt::layer().with_test_writer())
.init();
tracing::info!("Test logging initialized, writing to test_logs/");
diff --git a/crates/uzu/tests/test_engine.rs b/crates/uzu/tests/test_engine.rs
deleted file mode 100644
index 1f6013680..000000000
--- a/crates/uzu/tests/test_engine.rs
+++ /dev/null
@@ -1,131 +0,0 @@
-#![cfg(not(target_family = "wasm"))]
-
-use uzu::{
- engine::{Engine, EngineConfig},
- session::chat::ChatSessionStreamChunk,
- types::session::{
- chat::{ChatConfig, ChatMessage, ChatReplyConfig},
- classification::ClassificationMessage,
- },
-};
-
-#[ignore]
-#[tokio::test]
-async fn test_engine_chat() {
- dotenvy::dotenv().ok();
-
- let config = EngineConfig::default();
- let engine = Engine::new(config).await.unwrap();
-
- println!("-------------------------");
- let models = engine.models().await.unwrap();
- for model in models {
- let identifier = model.identifier.clone();
- let name = model.name();
- let repo_ids = model.repo_ids();
- let registry_identifier = model.registry.identifier.clone();
- let backend_identifiers = model.backends.iter().map(|backend| backend.identifier.clone()).collect::<Vec<_>>();
- let family_vendor_identifier = model.family.as_ref().map(|family| family.vendor.identifier.clone());
- let family_identifier = model.family.as_ref().map(|family| family.identifier.clone());
- let properties_identifier = model.properties.as_ref().map(|properties| properties.identifier.clone());
- let quantization_vendor_identifier =
- model.quantization.as_ref().map(|quantization| quantization.vendor.identifier.clone());
- let quantization_identifier = model.quantization.as_ref().map(|quantization| quantization.identifier.clone());
- let download_phase = engine.downloader(&model).state().await.map_or(None, |state| Some(state.phase));
- println!("identifier: {}", identifier);
- println!("name: {}", name);
- println!("repo_ids: {:?}", repo_ids);
- println!("registry_identifier: {}", registry_identifier);
- println!("backend_identifiers: {:?}", backend_identifiers);
- println!("family_vendor_identifier: {:?}", family_vendor_identifier);
- println!("family_identifier: {:?}", family_identifier);
- println!("properties_identifier: {:?}", properties_identifier);
- println!("quantization_vendor_identifier: {:?}", quantization_vendor_identifier);
- println!("quantization_identifier: {:?}", quantization_identifier);
- println!("download_phase: {:?}", download_phase);
- println!("specializations: {:?}", model.specializations);
- println!("-------------------------");
- }
-
- let model = engine.model("alibaba:qwen3:0.6b".to_string()).await.unwrap().unwrap();
- while let Some(update) = engine.download(&model).await.unwrap().next().await {
- println!("Downloading: {}", update.progress());
- }
-
- let session = engine.chat(model, ChatConfig::default()).await.unwrap();
- session.reset().await.unwrap();
-
- let messages = vec![
- ChatMessage::system().with_text("You are a helpful assistant.".to_string()),
- ChatMessage::user().with_text("My name is John Doe".to_string()),
- ];
- let stream = session.reply_with_stream(messages, ChatReplyConfig::default()).await;
- while let Some(progress) = stream.next().await {
- match progress {
- ChatSessionStreamChunk::Replies {
- replies,
- } => {
- let state = session.state().await;
- let messages = session.messages().await;
- let roles = messages.iter().map(|message| message.role.clone()).collect::<Vec<_>>();
- println!("State: {state:?}");
- println!("Roles: {roles:?}");
- for reply in replies {
- let duration = reply.stats.duration;
- let finish_reason = reply.finish_reason;
- let text_length = reply.message.text().unwrap_or_default().len();
- println!(
- "\tDuration: {duration}\n\tFinish reason: {finish_reason:?}\n\tText length: {text_length}"
- );
- }
- },
- ChatSessionStreamChunk::Error {
- error,
- } => eprintln!("Stream error: {error}"),
- }
- }
-
- let messages = vec![ChatMessage::user().with_text("What is my name?".to_string())];
- let _ = session.reply(messages, ChatReplyConfig::default()).await.unwrap();
-
- let messages = session.messages().await;
- for message in messages {
- println!("Message: {message:?}");
- }
-}
-
-#[ignore]
-#[tokio::test]
-async fn test_engine_classification() {
- dotenvy::dotenv().ok();
-
- let config = EngineConfig::default();
- let engine = Engine::new(config).await.unwrap();
- let model = engine.model("ModernBERT-Chat-Moderation".to_string()).await.unwrap().unwrap();
- while let Some(update) = engine.download(&model).await.unwrap().next().await {
- println!("Downloading: {}", update.progress());
- }
-
- let session = engine.classification(model).await.unwrap();
- let messages = vec![ClassificationMessage::user("Hi!".to_string())];
- let result = session.classify(messages).await.unwrap();
- println!("Output: {result:?}");
-}
-
-#[ignore]
-#[tokio::test]
-async fn test_engine_text_to_speech() {
- dotenvy::dotenv().ok();
-
- let config = EngineConfig::default();
- let engine = Engine::new(config).await.unwrap();
- let model = engine.model("s1-mini".to_string()).await.unwrap().unwrap();
- while let Some(update) = engine.download(&model).await.unwrap().next().await {
- println!("Downloading: {}", update.progress());
- }
-
- let session = engine.text_to_speech(model).await.unwrap();
- let result = session.synthesize("London is the capital of United Kingdom and one of the world’s most influential cities, known for its rich history, cultural diversity, and global significance in finance, politics, and the arts. Situated along the River Thames, the city blends historic landmarks like Tower of London and Buckingham Palace with modern architecture such as The Shard. London is also home to renowned institutions including the British Museum and vibrant areas like Covent Garden, offering a mix of history, entertainment, and innovation that attracts millions of visitors each year.".to_string()).await.unwrap();
- let path = dirs::home_dir().unwrap().join("Desktop").join("output.wav").to_string_lossy().to_string();
- result.pcm_batch.save_as_wav(path).unwrap();
-}
diff --git a/crates/uzu/tests/unit/storage/lifecycle_mock_registry_model_test.rs b/crates/uzu/tests/unit/storage/lifecycle_mock_registry_model_test.rs
index d61498195..0ff2320ff 100644
--- a/crates/uzu/tests/unit/storage/lifecycle_mock_registry_model_test.rs
+++ b/crates/uzu/tests/unit/storage/lifecycle_mock_registry_model_test.rs
@@ -1,17 +1,35 @@
use std::sync::Arc;
+use chrono::Utc;
+use download_manager::FileDownloadManagerType;
use mock_registry::{Behavior, MockRegistry};
+use rstest::rstest;
use tokio::runtime::Handle as TokioHandle;
+use tokio::time::{Duration, timeout};
use tokio_stream::{StreamExt, wrappers::BroadcastStream};
-use uzu::storage::types::{DownloadPhase, DownloadState, Item};
+use uzu::{
+ engine::Downloader,
+ helpers::SharedAccess,
+ storage::types::{DownloadPhase, DownloadState, Item},
+};
-use crate::common::test_storage::TestStorage;
+use crate::common::{test_storage::TestStorage, tracing_setup::init_test_tracing};
+#[rstest]
+#[case::universal(FileDownloadManagerType::Universal)]
+#[cfg_attr(target_vendor = "apple", case::apple(FileDownloadManagerType::Apple))]
#[tokio::test(flavor = "multi_thread")]
-async fn test_storage_mock_registry_model_download_lifecycle() -> Result<(), Box<dyn std::error::Error>> {
+async fn test_storage_mock_registry_model_download_lifecycle(
+ #[case] download_manager_type: FileDownloadManagerType,
+) -> Result<(), Box<dyn std::error::Error>> {
+ init_test_tracing();
+ tracing::info!("starting storage mock registry lifecycle test");
let registry = MockRegistry::start_with(Behavior::THROTTLED).await?;
let model = registry.models.first().ok_or_else(|| std::io::Error::other("mock registry must include a model"))?;
- let test_storage = TestStorage::with_models(TokioHandle::current(), vec![model.clone()]).await?;
+ tracing::info!(model_identifier = %model.identifier, "loaded mock registry model");
+ let test_storage =
+ TestStorage::with_models_and_manager(TokioHandle::current(), vec![model.clone()], download_manager_type)
+ .await?;
let model_identifier = model.identifier.clone();
let item = Arc::new(
test_storage
@@ -23,31 +41,52 @@ async fn test_storage_mock_registry_model_download_lifecycle() -> Result<(), Box
let mut progress = item.progress().await?;
let total_bytes = item.state().await.total_bytes;
+ tracing::info!(total_bytes, "starting first download");
item.download().await?;
- wait_for_item_state(&item, &mut progress, |state| {
- has_reached_fraction(state, 1, 4) || matches!(state.phase, DownloadPhase::Downloaded {})
+ let pre_pause_state = wait_for_item_state(&item, &mut progress, "first download reaches 25%", |state| {
+ has_reached_fraction(state, 1, 4)
})
.await;
+ assert!(
+ matches!(pre_pause_state.phase, DownloadPhase::Downloading {}),
+ "must pause while download is still active to exercise pause/resume; got {:?}",
+ pre_pause_state.phase
+ );
+ tracing::info!("pausing storage item");
item.pause().await?;
item.reconcile().await?;
let paused_state = item.state().await;
- assert!(matches!(paused_state.phase, DownloadPhase::Paused {} | DownloadPhase::Downloaded {}));
+ tracing::info!(?paused_state, "observed paused state");
+ assert!(matches!(paused_state.phase, DownloadPhase::Paused {}), "pause must transition to Paused; got {:?}", paused_state.phase);
+ tracing::info!("resuming storage item");
item.download().await?;
- wait_for_item_state(&item, &mut progress, |state| {
- has_reached_fraction(state, 1, 2) || matches!(state.phase, DownloadPhase::Downloaded {})
- })
- .await;
+ let post_resume_state =
+ wait_for_item_state(&item, &mut progress, "resumed download reaches 50%", |state| {
+ has_reached_fraction(state, 1, 2) || matches!(state.phase, DownloadPhase::Downloaded {})
+ })
+ .await;
+ assert!(
+ matches!(post_resume_state.phase, DownloadPhase::Downloading {} | DownloadPhase::Downloaded {}),
+ "resumed model must transition through Downloading and reach Downloaded; got {:?}",
+ post_resume_state.phase
+ );
+ tracing::info!("cancelling storage item");
item.cancel().await?;
let cancelled_state = item.state().await;
+ tracing::info!(?cancelled_state, "observed cancelled state");
assert!(matches!(cancelled_state.phase, DownloadPhase::NotDownloaded {}));
assert_eq!(cancelled_state.downloaded_bytes, 0);
+ tracing::info!("starting final download");
item.download().await?;
- let final_state =
- wait_for_item_state(&item, &mut progress, |state| matches!(state.phase, DownloadPhase::Downloaded {})).await;
+ let final_state = wait_for_item_state(&item, &mut progress, "final download completes", |state| {
+ matches!(state.phase, DownloadPhase::Downloaded {})
+ })
+ .await;
+ tracing::info!(?final_state, "observed final downloaded state");
assert_eq!(final_state.downloaded_bytes, total_bytes);
assert_eq!(final_state.total_bytes, total_bytes);
@@ -60,24 +99,227 @@ async fn test_storage_mock_registry_model_download_lifecycle() -> Result<(), Box
Ok(())
}
+#[rstest]
+#[case::universal(FileDownloadManagerType::Universal)]
+#[cfg_attr(target_vendor = "apple", case::apple(FileDownloadManagerType::Apple))]
+#[tokio::test(flavor = "multi_thread")]
+async fn test_storage_cancel_does_not_delete_locked_files(
+ #[case] download_manager_type: FileDownloadManagerType,
+) -> Result<(), Box<dyn std::error::Error>> {
+ init_test_tracing();
+ let registry = MockRegistry::start().await?;
+ let model = registry.models.first().ok_or_else(|| std::io::Error::other("mock registry must include a model"))?;
+ let test_storage =
+ TestStorage::with_models_and_manager(TokioHandle::current(), vec![model.clone()], download_manager_type)
+ .await?;
+ let item = test_storage
+ .storage
+ .get(&model.identifier)
+ .await
+ .ok_or_else(|| format!("model not found: {}", model.identifier))?;
+ let served_file = registry.files.first().ok_or_else(|| std::io::Error::other("mock registry must include files"))?;
+ let destination = item.cache_path.join(&served_file.file.name);
+ if let Some(parent) = destination.parent() {
+ tokio::fs::create_dir_all(parent).await?;
+ }
+ tokio::fs::write(&destination, served_file.bytes.as_ref()).await?;
+ tokio::fs::write(
+ std::path::PathBuf::from(format!("{}.lock", destination.display())),
+ serde_json::to_vec(&serde_json::json!({
+ "manager_id": "foreign-manager",
+ "acquired_at": Utc::now(),
+ "process_id": std::process::id(),
+ }))?,
+ )
+ .await?;
+
+ let result = item.cancel().await;
+
+ assert!(result.is_err());
+ assert!(destination.exists());
+ Ok(())
+}
+
+#[rstest]
+#[case::universal(FileDownloadManagerType::Universal)]
+#[cfg_attr(target_vendor = "apple", case::apple(FileDownloadManagerType::Apple))]
+#[tokio::test(flavor = "multi_thread")]
+async fn test_downloader_progress_stream_closes_after_pause(
+ #[case] download_manager_type: FileDownloadManagerType,
+) -> Result<(), Box<dyn std::error::Error>> {
+ init_test_tracing();
+ let registry = MockRegistry::start_with(Behavior::THROTTLED).await?;
+ let model = registry.models.first().ok_or_else(|| std::io::Error::other("mock registry must include a model"))?;
+ let test_storage =
+ TestStorage::with_models_and_manager(TokioHandle::current(), vec![model.clone()], download_manager_type)
+ .await?;
+ let downloader = Downloader::new(model.identifier.clone(), SharedAccess::new(test_storage.storage));
+
+ downloader.resume().await?;
+ let progress = downloader.progress().await?;
+ timeout(Duration::from_secs(30), async {
+ loop {
+ let state = downloader.state().await.expect("model state must exist");
+ if matches!(state.phase, DownloadPhase::Downloading {}) {
+ break;
+ }
+ tokio::time::sleep(Duration::from_millis(20)).await;
+ }
+ })
+ .await?;
+
+ downloader.pause().await?;
+ timeout(Duration::from_secs(10), async {
+ while progress.next().await.is_some() {}
+ })
+ .await?;
+
+ Ok(())
+}
+
+#[rstest]
+#[case::universal(FileDownloadManagerType::Universal)]
+#[cfg_attr(target_vendor = "apple", case::apple(FileDownloadManagerType::Apple))]
+#[tokio::test(flavor = "multi_thread")]
+async fn test_downloader_pause_updates_public_state(
+ #[case] download_manager_type: FileDownloadManagerType,
+) -> Result<(), Box<dyn std::error::Error>> {
+ init_test_tracing();
+ let registry = MockRegistry::start_with(Behavior::THROTTLED).await?;
+ let model = registry.models.first().ok_or_else(|| std::io::Error::other("mock registry must include a model"))?;
+ let test_storage =
+ TestStorage::with_models_and_manager(TokioHandle::current(), vec![model.clone()], download_manager_type)
+ .await?;
+ let downloader = Downloader::new(model.identifier.clone(), SharedAccess::new(test_storage.storage));
+
+ downloader.resume().await?;
+ wait_for_downloader_state(&downloader, "downloader reaches Downloading", |state| {
+ matches!(state.phase, DownloadPhase::Downloading {})
+ })
+ .await;
+
+ downloader.pause().await?;
+ let paused_state = wait_for_downloader_state(&downloader, "downloader reaches Paused", |state| {
+ matches!(state.phase, DownloadPhase::Paused {})
+ })
+ .await;
+
+ assert!(!paused_state.is_in_progress());
+ assert!(!paused_state.can_pause());
+ Ok(())
+}
+
+#[rstest]
+#[case::universal(FileDownloadManagerType::Universal)]
+#[cfg_attr(target_vendor = "apple", case::apple(FileDownloadManagerType::Apple))]
+#[tokio::test(flavor = "multi_thread")]
+async fn test_downloader_progress_after_resuming_paused_model_reaches_downloaded(
+ #[case] download_manager_type: FileDownloadManagerType,
+) -> Result<(), Box<dyn std::error::Error>> {
+ init_test_tracing();
+ let registry = MockRegistry::start_with(Behavior::THROTTLED).await?;
+ let model = registry.models.first().ok_or_else(|| std::io::Error::other("mock registry must include a model"))?;
+ let test_storage =
+ TestStorage::with_models_and_manager(TokioHandle::current(), vec![model.clone()], download_manager_type)
+ .await?;
+ let downloader = Downloader::new(model.identifier.clone(), SharedAccess::new(test_storage.storage));
+
+ downloader.resume().await?;
+ wait_for_downloader_state(&downloader, "downloader reaches Downloading before pause", |state| {
+ matches!(state.phase, DownloadPhase::Downloading {})
+ })
+ .await;
+ downloader.pause().await?;
+ wait_for_downloader_state(&downloader, "downloader reaches Paused before resume", |state| {
+ matches!(state.phase, DownloadPhase::Paused {})
+ })
+ .await;
+
+ downloader.resume().await?;
+ let progress = downloader.progress().await?;
+ timeout(Duration::from_secs(30), async {
+ loop {
+ let state = downloader.state().await.expect("model state must exist");
+ if matches!(state.phase, DownloadPhase::Downloaded {}) {
+ return;
+ }
+ assert!(progress.next().await.is_some(), "progress stream ended before the resumed model downloaded");
+ }
+ })
+ .await?;
+ Ok(())
+}
+
+#[rstest]
+#[case::universal(FileDownloadManagerType::Universal)]
+#[cfg_attr(target_vendor = "apple", case::apple(FileDownloadManagerType::Apple))]
+#[tokio::test(flavor = "multi_thread")]
+async fn test_downloader_progress_for_downloaded_model_is_empty(
+ #[case] download_manager_type: FileDownloadManagerType,
+) -> Result<(), Box<dyn std::error::Error>> {
+ init_test_tracing();
+ let registry = MockRegistry::start().await?;
+ let model = registry.models.first().ok_or_else(|| std::io::Error::other("mock registry must include a model"))?;
+ let test_storage =
+ TestStorage::with_models_and_manager(TokioHandle::current(), vec![model.clone()], download_manager_type)
+ .await?;
+ let downloader = Downloader::new(model.identifier.clone(), SharedAccess::new(test_storage.storage));
+
+ downloader.resume().await?;
+ wait_for_downloader_state(&downloader, "downloader reaches Downloaded", |state| {
+ matches!(state.phase, DownloadPhase::Downloaded {})
+ })
+ .await;
+
+ let downloaded_progress = downloader.progress().await?;
+ assert!(downloaded_progress.next().await.is_none());
+ Ok(())
+}
+
async fn wait_for_item_state(
item: &Arc<Item>,
progress: &mut BroadcastStream<DownloadState>,
+ label: &'static str,
mut is_expected_state: impl FnMut(&DownloadState) -> bool,
) -> DownloadState {
- let current_state = item.state().await;
- if is_expected_state(&current_state) {
- return current_state;
- }
+ timeout(Duration::from_secs(30), async {
+ tracing::info!(label, "waiting for storage download state");
+ let current_state = item.state().await;
+ tracing::debug!(label, ?current_state, "observed initial storage download state");
+ if is_expected_state(&current_state) {
+ return current_state;
+ }
- while let Some(result) = progress.next().await {
- let state = result.expect("storage progress stream must not lag");
- if is_expected_state(&state) {
- return state;
+ while let Some(result) = progress.next().await {
+ let state = result.expect("storage progress stream must not lag");
+ tracing::debug!(label, ?state, "observed storage progress state");
+ if is_expected_state(&state) {
+ return state;
+ }
}
- }
- panic!("storage progress stream ended before expected state");
+ panic!("storage progress stream ended before expected state");
+ })
+ .await
+ .unwrap_or_else(|_| panic!("timed out waiting for storage download state: {label}"))
+}
+
+async fn wait_for_downloader_state(
+ downloader: &Downloader,
+ label: &'static str,
+ mut is_expected_state: impl FnMut(&DownloadState) -> bool,
+) -> DownloadState {
+ timeout(Duration::from_secs(30), async {
+ loop {
+ let state = downloader.state().await.expect("model state must exist");
+ if is_expected_state(&state) {
+ return state;
+ }
+ tokio::time::sleep(Duration::from_millis(20)).await;
+ }
+ })
+ .await
+ .unwrap_or_else(|_| panic!("timed out waiting for downloader state: {label}"))
}
fn has_reached_fraction(