tauri-plugin-hdiff-update 0.3.0

Tauri v2 plugin for signed, transactional HDiffPatch directory updates.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
use std::{
    fs,
    io::Read,
    path::{Path, PathBuf},
    time::{SystemTime, UNIX_EPOCH},
};

use base64::Engine;
use fs2::FileExt;
use hdiff_update_core::{
    download_to_file, sha256_file, write_json_file_atomic, DownloadEvent, HttpHeader,
};
use minisign_verify::{PublicKey, Signature};
use semver::Version;
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use tauri::{ipc::Channel, AppHandle, Manager, ResourceId, Runtime, State, Webview};

use crate::file_update::{FileUpdateCommandError, FileUpdateHelperConfig};

#[cfg(windows)]
use std::{ffi::OsStr, os::windows::ffi::OsStrExt};
#[cfg(windows)]
use windows_sys::Win32::{
    Foundation::{CloseHandle, ERROR_CANCELLED, ERROR_ELEVATION_REQUIRED, WAIT_TIMEOUT},
    Storage::FileSystem::SYNCHRONIZE,
    System::Threading::{
        GetProcessId, OpenProcess, WaitForSingleObject, PROCESS_QUERY_LIMITED_INFORMATION,
    },
    UI::{
        Shell::{ShellExecuteExW, SEE_MASK_NOASYNC, SEE_MASK_NOCLOSEPROCESS, SHELLEXECUTEINFOW},
        WindowsAndMessaging::SW_SHOWNORMAL,
    },
};

const FULL_UPDATE_ROOT: &str = "full-updates";
const TRANSACTION_SCHEMA_VERSION: u32 = 1;
const MAX_INSTALLER_BYTES: u64 = 1024 * 1024 * 1024;
const MAX_AUTOMATIC_LAUNCH_ATTEMPTS: u32 = 2;
const INSTALLER_PROCESS_GRACE_MS: u64 = 15 * 60 * 1000;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum FullUpdateState {
    Preparing,
    Ready,
    Launching,
    Failed,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct FullUpdateTransaction {
    pub schema_version: u32,
    pub transaction_id: String,
    pub application_id: String,
    pub current_version: String,
    pub target_version: String,
    pub installer_path: PathBuf,
    pub installer_sha256: String,
    pub installer_size: u64,
    pub updater_signature: String,
    pub source_executable_sha256: String,
    pub state: FullUpdateState,
    pub launch_attempts: u32,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub launched_pid: Option<u32>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub launched_at_ms: Option<u64>,
    pub created_at_ms: u64,
    pub updated_at_ms: u64,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub failure_reason: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
struct FullPreparedPointer {
    transaction_path: PathBuf,
    target_version: String,
    installer_sha256: String,
}

#[derive(Debug, Clone, Copy, Serialize)]
#[serde(rename_all = "camelCase")]
pub enum PreparedFullUpdateKind {
    Prepared,
    AlreadyPrepared,
}

#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct PreparedFullUpdate {
    pub kind: PreparedFullUpdateKind,
    pub transaction: FullUpdateTransaction,
    pub transaction_path: PathBuf,
    pub installer_path: PathBuf,
    pub bytes_downloaded: u64,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct LaunchFullUpdateOptions {
    pub transaction_path: PathBuf,
}

#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct LaunchFullUpdateResult {
    pub pid: u32,
}

#[tauri::command]
pub async fn prepare_full_update<R: Runtime>(
    webview: Webview<R>,
    config: State<'_, FileUpdateHelperConfig>,
    update_rid: ResourceId,
    on_event: Channel<DownloadEvent>,
) -> Result<PreparedFullUpdate, FileUpdateCommandError> {
    if config.updater_public_key.trim().is_empty() {
        return Err(FileUpdateCommandError::from(
            "full updater public key is not compiled into the application",
        ));
    }
    let app = webview.app_handle();
    if app.config().identifier != config.application_id {
        return Err(FileUpdateCommandError::from(
            "full update plugin application id does not match the Tauri identifier",
        ));
    }
    let update = webview
        .resources_table()
        .get::<tauri_plugin_updater::Update>(update_rid)?;
    validate_version_upgrade(&update.current_version, &update.version)?;
    if !same_version(&update.current_version, config.current_version)
        || !same_version(
            &app.package_info().version.to_string(),
            config.current_version,
        )
    {
        return Err(FileUpdateCommandError::from(format!(
            "full update source version mismatch: updater={}, application={}, configured={}",
            update.current_version,
            app.package_info().version,
            config.current_version
        )));
    }
    if update.download_url.scheme() != "https" {
        return Err(FileUpdateCommandError::from(
            "full update installer must use HTTPS",
        ));
    }

    let cache_root = full_update_cache_root(app)?;
    fs::create_dir_all(&cache_root)?;
    let lock = open_lock(&cache_root)?;
    lock.lock_exclusive()?;

    if let Some(prepared) = load_reusable_update(&cache_root, &config, &update.version)? {
        return Ok(prepared);
    }
    reset_full_update_cache(&cache_root)?;

    let transaction_id = full_transaction_id(
        config.application_id,
        &update.current_version,
        &update.version,
        update.download_url.as_str(),
    );
    let transaction_root = cache_root.join("transactions").join(transaction_id.clone());
    fs::create_dir_all(&transaction_root)?;
    let transaction_path = transaction_root.join("transaction.json");
    let installer_path = transaction_root.join("installer.exe");
    let source_executable_sha256 = sha256_file(std::env::current_exe()?)?.sha256;
    let now = now_millis();
    let mut transaction = FullUpdateTransaction {
        schema_version: TRANSACTION_SCHEMA_VERSION,
        transaction_id,
        application_id: config.application_id.to_string(),
        current_version: update.current_version.clone(),
        target_version: update.version.clone(),
        installer_path: installer_path.clone(),
        installer_sha256: String::new(),
        installer_size: 0,
        updater_signature: update.signature.clone(),
        source_executable_sha256,
        state: FullUpdateState::Preparing,
        launch_attempts: 0,
        launched_pid: None,
        launched_at_ms: None,
        created_at_ms: now,
        updated_at_ms: now,
        failure_reason: None,
    };
    write_json_file_atomic(&transaction_path, &transaction)?;

    let headers = update
        .headers
        .iter()
        .map(|(name, value)| {
            Ok(HttpHeader {
                name: name.as_str().to_string(),
                value: value
                    .to_str()
                    .map_err(FileUpdateCommandError::from)?
                    .to_string(),
            })
        })
        .collect::<Result<Vec<_>, FileUpdateCommandError>>()?;
    let timeout_secs = update.timeout.map(|timeout| timeout.as_secs().max(1));
    let stats = download_to_file(
        update.download_url.as_str(),
        &installer_path,
        &headers,
        timeout_secs,
        Some(MAX_INSTALLER_BYTES),
        |event| {
            let _ = on_event.send(event);
        },
    )
    .await?;
    ensure_windows_installer(&installer_path)?;
    verify_updater_signature(
        &installer_path,
        config.updater_public_key,
        &update.signature,
    )?;
    let digest = sha256_file(&installer_path)?;

    transaction.installer_sha256 = digest.sha256.clone();
    transaction.installer_size = digest.size;
    transaction.state = FullUpdateState::Ready;
    transaction.updated_at_ms = now_millis();
    write_json_file_atomic(&transaction_path, &transaction)?;
    write_json_file_atomic(
        cache_root.join("prepared.json"),
        &FullPreparedPointer {
            transaction_path: transaction_path.clone(),
            target_version: transaction.target_version.clone(),
            installer_sha256: transaction.installer_sha256.clone(),
        },
    )?;

    Ok(PreparedFullUpdate {
        kind: PreparedFullUpdateKind::Prepared,
        transaction,
        transaction_path,
        installer_path,
        bytes_downloaded: stats.bytes_written,
    })
}

#[tauri::command]
pub async fn launch_full_update<R: Runtime>(
    app: AppHandle<R>,
    config: State<'_, FileUpdateHelperConfig>,
    options: LaunchFullUpdateOptions,
) -> Result<LaunchFullUpdateResult, FileUpdateCommandError> {
    let cache_root = full_update_cache_root(&app)?;
    let lock = open_lock(&cache_root)?;
    lock.lock_exclusive()?;
    let transaction_path = validate_transaction_path(&cache_root, &options.transaction_path)?;
    let pid = launch_full_update_inner(&config, &cache_root, &transaction_path, false)?;
    Ok(LaunchFullUpdateResult { pid })
}

pub(crate) fn maybe_apply_prepared_full_update(
    config: &FileUpdateHelperConfig,
) -> Result<bool, FileUpdateCommandError> {
    let cache_root = helper_cache_root(config)?;
    let pointer_path = cache_root.join("prepared.json");
    if !pointer_path.is_file() {
        return Ok(false);
    }
    let pointer = match read_json::<FullPreparedPointer>(&pointer_path) {
        Ok(pointer) => pointer,
        Err(_) => {
            let _ = fs::remove_file(pointer_path);
            return Ok(false);
        }
    };
    let transaction_path = match validate_transaction_path(&cache_root, &pointer.transaction_path) {
        Ok(path) => path,
        Err(_) => {
            let _ = fs::remove_file(pointer_path);
            return Ok(false);
        }
    };
    let mut transaction = match read_transaction(&transaction_path) {
        Ok(transaction) => transaction,
        Err(_) => {
            let _ = fs::remove_file(pointer_path);
            return Ok(false);
        }
    };

    if same_version(config.current_version, &transaction.target_version) {
        let _ = cleanup_completed_update(&cache_root, &transaction_path);
        return Ok(false);
    }
    if !same_version(config.current_version, &transaction.current_version)
        || transaction.application_id != config.application_id
        || pointer.target_version != transaction.target_version
        || pointer.installer_sha256 != transaction.installer_sha256
    {
        fail_prepared_update(
            &cache_root,
            &transaction_path,
            &mut transaction,
            "prepared full update does not match the running application",
        )?;
        return Ok(false);
    }

    if transaction.state == FullUpdateState::Launching {
        let launch_is_recent = transaction.launched_at_ms.is_some_and(|launched_at| {
            now_millis().saturating_sub(launched_at) <= INSTALLER_PROCESS_GRACE_MS
        });
        if launch_is_recent && transaction.launched_pid.is_some_and(process_is_running) {
            return Ok(true);
        }
        if transaction.launch_attempts >= MAX_AUTOMATIC_LAUNCH_ATTEMPTS {
            fail_prepared_update(
                &cache_root,
                &transaction_path,
                &mut transaction,
                "full update installer did not replace the application",
            )?;
            return Ok(false);
        }
    } else if transaction.state != FullUpdateState::Ready {
        fail_prepared_update(
            &cache_root,
            &transaction_path,
            &mut transaction,
            "prepared full update is not launchable",
        )?;
        return Ok(false);
    }

    match launch_full_update_inner(config, &cache_root, &transaction_path, true) {
        Ok(_) => Ok(true),
        Err(error) => {
            let mut transaction = read_transaction(&transaction_path).unwrap_or(transaction);
            fail_prepared_update(
                &cache_root,
                &transaction_path,
                &mut transaction,
                &error.message,
            )?;
            Ok(false)
        }
    }
}

fn launch_full_update_inner(
    config: &FileUpdateHelperConfig,
    cache_root: &Path,
    transaction_path: &Path,
    automatic: bool,
) -> Result<u32, FileUpdateCommandError> {
    let pointer = read_json::<FullPreparedPointer>(&cache_root.join("prepared.json"))?;
    let mut transaction = validate_transaction(config, cache_root, transaction_path, &pointer)?;
    if transaction.state != FullUpdateState::Ready
        && !(automatic && transaction.state == FullUpdateState::Launching)
    {
        return Err(FileUpdateCommandError::from(format!(
            "full update transaction is not ready: {:?}",
            transaction.state
        )));
    }
    let current_digest = sha256_file(std::env::current_exe()?)?;
    if current_digest.sha256 != transaction.source_executable_sha256 {
        return Err(FileUpdateCommandError::from(
            "running executable does not match the prepared full update source",
        ));
    }

    transaction.state = FullUpdateState::Launching;
    transaction.launch_attempts = transaction.launch_attempts.saturating_add(1);
    transaction.launched_pid = None;
    transaction.launched_at_ms = Some(now_millis());
    transaction.updated_at_ms = now_millis();
    transaction.failure_reason = None;
    write_json_file_atomic(transaction_path, &transaction)?;

    match launch_installer_process(&transaction.installer_path) {
        Ok(pid) => {
            transaction.launched_pid = Some(pid);
            transaction.updated_at_ms = now_millis();
            write_json_file_atomic(transaction_path, &transaction)?;
            Ok(pid)
        }
        Err(error) => {
            if !automatic {
                transaction.state = FullUpdateState::Ready;
                transaction.launched_pid = None;
                transaction.updated_at_ms = now_millis();
                transaction.failure_reason = Some(error.message.clone());
                write_json_file_atomic(transaction_path, &transaction)?;
            }
            Err(error)
        }
    }
}

fn load_reusable_update(
    cache_root: &Path,
    config: &FileUpdateHelperConfig,
    target_version: &str,
) -> Result<Option<PreparedFullUpdate>, FileUpdateCommandError> {
    let pointer_path = cache_root.join("prepared.json");
    if !pointer_path.is_file() {
        return Ok(None);
    }
    let pointer = match read_json::<FullPreparedPointer>(&pointer_path) {
        Ok(pointer) => pointer,
        Err(_) => return Ok(None),
    };
    if !same_version(&pointer.target_version, target_version) {
        return Ok(None);
    }
    let transaction_path = match validate_transaction_path(cache_root, &pointer.transaction_path) {
        Ok(path) => path,
        Err(_) => return Ok(None),
    };
    let transaction = match validate_transaction(config, cache_root, &transaction_path, &pointer) {
        Ok(transaction) => transaction,
        Err(_) => return Ok(None),
    };
    if transaction.state != FullUpdateState::Ready {
        return Ok(None);
    }
    let current_digest = sha256_file(std::env::current_exe()?)?;
    if current_digest.sha256 != transaction.source_executable_sha256 {
        return Ok(None);
    }
    Ok(Some(PreparedFullUpdate {
        kind: PreparedFullUpdateKind::AlreadyPrepared,
        installer_path: transaction.installer_path.clone(),
        transaction,
        transaction_path,
        bytes_downloaded: 0,
    }))
}

fn validate_transaction(
    config: &FileUpdateHelperConfig,
    cache_root: &Path,
    transaction_path: &Path,
    pointer: &FullPreparedPointer,
) -> Result<FullUpdateTransaction, FileUpdateCommandError> {
    let transaction_path = validate_transaction_path(cache_root, transaction_path)?;
    let transaction = read_transaction(&transaction_path)?;
    if transaction.application_id != config.application_id {
        return Err(FileUpdateCommandError::from(
            "full update transaction belongs to another application",
        ));
    }
    if !same_version(&transaction.current_version, config.current_version) {
        return Err(FileUpdateCommandError::from(
            "full update transaction source version does not match the application",
        ));
    }
    validate_version_upgrade(&transaction.current_version, &transaction.target_version)?;
    if pointer.transaction_path.canonicalize()? != transaction_path
        || pointer.target_version != transaction.target_version
        || pointer.installer_sha256 != transaction.installer_sha256
    {
        return Err(FileUpdateCommandError::from(
            "full update prepared pointer does not match its transaction",
        ));
    }
    let transaction_root = transaction_path
        .parent()
        .ok_or_else(|| FileUpdateCommandError::from("full update transaction has no parent"))?;
    let installer_path = transaction.installer_path.canonicalize()?;
    if !installer_path.starts_with(transaction_root)
        || installer_path.file_name() != Some(std::ffi::OsStr::new("installer.exe"))
    {
        return Err(FileUpdateCommandError::from(
            "full update installer is outside its transaction",
        ));
    }
    ensure_plain_file(&installer_path)?;
    let digest = sha256_file(&installer_path)?;
    if digest.size != transaction.installer_size || digest.sha256 != transaction.installer_sha256 {
        return Err(FileUpdateCommandError::from(
            "full update installer hash or size does not match its transaction",
        ));
    }
    ensure_windows_installer(&installer_path)?;
    verify_updater_signature(
        &installer_path,
        config.updater_public_key,
        &transaction.updater_signature,
    )?;
    Ok(transaction)
}

fn read_transaction(path: &Path) -> Result<FullUpdateTransaction, FileUpdateCommandError> {
    let transaction = read_json::<FullUpdateTransaction>(path)?;
    if transaction.schema_version != TRANSACTION_SCHEMA_VERSION {
        return Err(FileUpdateCommandError::from(format!(
            "unsupported full update transaction schema version: {}",
            transaction.schema_version
        )));
    }
    Ok(transaction)
}

fn reset_full_update_cache(cache_root: &Path) -> Result<(), FileUpdateCommandError> {
    let pointer_path = cache_root.join("prepared.json");
    if pointer_path.is_file() {
        fs::remove_file(pointer_path)?;
    }
    let transactions_root = cache_root.join("transactions");
    if transactions_root.exists() {
        ensure_plain_directory(&transactions_root)?;
        fs::remove_dir_all(&transactions_root)?;
    }
    fs::create_dir_all(transactions_root)?;
    Ok(())
}

fn cleanup_completed_update(
    cache_root: &Path,
    transaction_path: &Path,
) -> Result<(), FileUpdateCommandError> {
    let transaction_path = validate_transaction_path(cache_root, transaction_path)?;
    let transaction_root = transaction_path
        .parent()
        .ok_or_else(|| FileUpdateCommandError::from("full update transaction has no parent"))?;
    let pointer_path = cache_root.join("prepared.json");
    if pointer_path.is_file() {
        fs::remove_file(pointer_path)?;
    }
    ensure_plain_directory(transaction_root)?;
    fs::remove_dir_all(transaction_root)?;
    Ok(())
}

fn fail_prepared_update(
    cache_root: &Path,
    transaction_path: &Path,
    transaction: &mut FullUpdateTransaction,
    reason: &str,
) -> Result<(), FileUpdateCommandError> {
    transaction.state = FullUpdateState::Failed;
    transaction.launched_pid = None;
    transaction.updated_at_ms = now_millis();
    transaction.failure_reason = Some(reason.to_string());
    write_json_file_atomic(transaction_path, transaction)?;
    let pointer_path = cache_root.join("prepared.json");
    if pointer_path.is_file() {
        fs::remove_file(pointer_path)?;
    }
    if transaction.installer_path.is_file() {
        fs::remove_file(&transaction.installer_path)?;
    }
    Ok(())
}

fn full_update_cache_root<R: Runtime>(
    app: &AppHandle<R>,
) -> Result<PathBuf, FileUpdateCommandError> {
    Ok(app
        .path()
        .app_data_dir()?
        .join("hdiff-update")
        .join(FULL_UPDATE_ROOT))
}

fn helper_cache_root(config: &FileUpdateHelperConfig) -> Result<PathBuf, FileUpdateCommandError> {
    let app_data = std::env::var_os("APPDATA")
        .ok_or_else(|| FileUpdateCommandError::from("APPDATA is not available"))?;
    Ok(PathBuf::from(app_data)
        .join(config.application_id)
        .join("hdiff-update")
        .join(FULL_UPDATE_ROOT))
}

fn open_lock(cache_root: &Path) -> Result<fs::File, FileUpdateCommandError> {
    fs::create_dir_all(cache_root)?;
    Ok(fs::OpenOptions::new()
        .create(true)
        .truncate(false)
        .read(true)
        .write(true)
        .open(cache_root.join("lock"))?)
}

fn validate_transaction_path(
    cache_root: &Path,
    transaction_path: &Path,
) -> Result<PathBuf, FileUpdateCommandError> {
    let expected_root = cache_root.join("transactions").canonicalize()?;
    let transaction_path = transaction_path.canonicalize()?;
    if !transaction_path.starts_with(expected_root)
        || transaction_path.file_name() != Some(std::ffi::OsStr::new("transaction.json"))
    {
        return Err(FileUpdateCommandError::from(format!(
            "full update transaction is outside the managed cache: {}",
            transaction_path.display()
        )));
    }
    Ok(transaction_path)
}

fn verify_updater_signature(
    installer_path: &Path,
    encoded_public_key: &str,
    encoded_signature: &str,
) -> Result<(), FileUpdateCommandError> {
    let public_key_text = decode_base64_utf8(encoded_public_key, "updater public key")?;
    let signature_text = decode_base64_utf8(encoded_signature, "updater signature")?;
    let public_key = PublicKey::decode(&public_key_text)?;
    let signature = Signature::decode(&signature_text)?;
    let mut verifier = public_key.verify_stream(&signature)?;
    let mut installer = fs::File::open(installer_path)?;
    let mut buffer = vec![0_u8; 1024 * 1024];
    loop {
        let read = installer.read(&mut buffer)?;
        if read == 0 {
            break;
        }
        verifier.update(&buffer[..read]);
    }
    verifier.finalize()?;
    Ok(())
}

fn decode_base64_utf8(value: &str, label: &str) -> Result<String, FileUpdateCommandError> {
    let decoded = base64::engine::general_purpose::STANDARD
        .decode(value.trim())
        .map_err(|error| FileUpdateCommandError::from(format!("invalid {label}: {error}")))?;
    String::from_utf8(decoded)
        .map_err(|error| FileUpdateCommandError::from(format!("invalid {label}: {error}")))
}

fn ensure_windows_installer(path: &Path) -> Result<(), FileUpdateCommandError> {
    let mut file = fs::File::open(path)?;
    let mut magic = [0_u8; 2];
    file.read_exact(&mut magic)?;
    if magic != *b"MZ" {
        return Err(FileUpdateCommandError::from(
            "full update artifact is not a Windows executable",
        ));
    }
    Ok(())
}

fn ensure_plain_directory(path: &Path) -> Result<(), FileUpdateCommandError> {
    let metadata = fs::symlink_metadata(path)?;
    if !metadata.is_dir() || metadata.file_type().is_symlink() || is_reparse_point(&metadata) {
        return Err(FileUpdateCommandError::from(format!(
            "directory is missing or is a reparse point: {}",
            path.display()
        )));
    }
    Ok(())
}

fn ensure_plain_file(path: &Path) -> Result<(), FileUpdateCommandError> {
    let metadata = fs::symlink_metadata(path)?;
    if !metadata.is_file() || metadata.file_type().is_symlink() || is_reparse_point(&metadata) {
        return Err(FileUpdateCommandError::from(format!(
            "file is missing or is a reparse point: {}",
            path.display()
        )));
    }
    Ok(())
}

#[cfg(windows)]
fn is_reparse_point(metadata: &fs::Metadata) -> bool {
    use std::os::windows::fs::MetadataExt;
    metadata.file_attributes() & 0x0000_0400 != 0
}

#[cfg(not(windows))]
fn is_reparse_point(_metadata: &fs::Metadata) -> bool {
    false
}

fn validate_version_upgrade(current: &str, target: &str) -> Result<(), FileUpdateCommandError> {
    let current = parse_version(current)?;
    let target = parse_version(target)?;
    if target <= current {
        return Err(FileUpdateCommandError::from(
            "full update target version is not newer than the current version",
        ));
    }
    Ok(())
}

fn same_version(left: &str, right: &str) -> bool {
    match (parse_version(left), parse_version(right)) {
        (Ok(left), Ok(right)) => left == right,
        _ => left.trim() == right.trim(),
    }
}

fn parse_version(value: &str) -> Result<Version, FileUpdateCommandError> {
    Version::parse(value.trim().trim_start_matches('v')).map_err(FileUpdateCommandError::from)
}

fn full_transaction_id(
    application_id: &str,
    current_version: &str,
    target_version: &str,
    download_url: &str,
) -> String {
    let mut hasher = Sha256::new();
    for value in [
        application_id,
        current_version,
        target_version,
        download_url,
    ] {
        hasher.update(value.as_bytes());
        hasher.update([0]);
    }
    hex::encode(hasher.finalize())
}

fn read_json<T: for<'de> Deserialize<'de>>(path: &Path) -> Result<T, FileUpdateCommandError> {
    Ok(serde_json::from_slice(&fs::read(path)?)?)
}

fn now_millis() -> u64 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap_or_default()
        .as_millis()
        .min(u64::MAX as u128) as u64
}

#[cfg(windows)]
fn launch_installer_process(installer_path: &Path) -> Result<u32, FileUpdateCommandError> {
    let verb = wide_null(OsStr::new("runas"))?;
    let executable = wide_null(installer_path.as_os_str())?;
    let parameters = wide_null(OsStr::new("/P /R /UPDATE /ARGS"))?;
    let directory = installer_path
        .parent()
        .map(|path| wide_null(path.as_os_str()))
        .transpose()?;
    let mut execute_info = SHELLEXECUTEINFOW {
        cbSize: std::mem::size_of::<SHELLEXECUTEINFOW>() as u32,
        fMask: SEE_MASK_NOCLOSEPROCESS | SEE_MASK_NOASYNC,
        lpVerb: verb.as_ptr(),
        lpFile: executable.as_ptr(),
        lpParameters: parameters.as_ptr(),
        lpDirectory: directory
            .as_ref()
            .map_or(std::ptr::null(), |value| value.as_ptr()),
        nShow: SW_SHOWNORMAL,
        ..Default::default()
    };
    if unsafe { ShellExecuteExW(&mut execute_info) } == 0 {
        let error = std::io::Error::last_os_error();
        if error.raw_os_error() == Some(ERROR_CANCELLED as i32) {
            return Err(FileUpdateCommandError::from(
                "administrator permission was declined",
            ));
        }
        if error.raw_os_error() == Some(ERROR_ELEVATION_REQUIRED as i32) {
            return Err(FileUpdateCommandError::from(
                "administrator permission is required",
            ));
        }
        return Err(FileUpdateCommandError::from(format!(
            "failed to launch full update installer: {error}"
        )));
    }
    if execute_info.hProcess.is_null() {
        return Err(FileUpdateCommandError::from(
            "full update installer started without a process handle",
        ));
    }
    let pid = unsafe { GetProcessId(execute_info.hProcess) };
    unsafe {
        CloseHandle(execute_info.hProcess);
    }
    if pid == 0 {
        return Err(FileUpdateCommandError::from(
            "full update installer started without a process id",
        ));
    }
    Ok(pid)
}

#[cfg(not(windows))]
fn launch_installer_process(_installer_path: &Path) -> Result<u32, FileUpdateCommandError> {
    Err(FileUpdateCommandError::from(
        "persistent full updates are only implemented on Windows",
    ))
}

#[cfg(windows)]
fn process_is_running(pid: u32) -> bool {
    let process = unsafe { OpenProcess(SYNCHRONIZE | PROCESS_QUERY_LIMITED_INFORMATION, 0, pid) };
    if process.is_null() {
        return false;
    }
    let wait = unsafe { WaitForSingleObject(process, 0) };
    unsafe {
        CloseHandle(process);
    }
    wait == WAIT_TIMEOUT
}

#[cfg(not(windows))]
fn process_is_running(_pid: u32) -> bool {
    false
}

#[cfg(windows)]
fn wide_null(value: &OsStr) -> std::io::Result<Vec<u16>> {
    let mut wide = Vec::new();
    for unit in value.encode_wide() {
        if unit == 0 {
            return Err(std::io::Error::new(
                std::io::ErrorKind::InvalidInput,
                "value contains an embedded null character",
            ));
        }
        wide.push(unit);
    }
    wide.push(0);
    Ok(wide)
}

#[cfg(test)]
mod tests {
    use super::{full_transaction_id, same_version, validate_version_upgrade};

    #[test]
    fn full_transaction_id_is_stable_and_input_sensitive() {
        let first = full_transaction_id("app", "1.0.0", "1.1.0", "https://example/app.exe");
        let second = full_transaction_id("app", "1.0.0", "1.1.0", "https://example/app.exe");
        let different = full_transaction_id("app", "1.0.0", "1.2.0", "https://example/app.exe");
        assert_eq!(first, second);
        assert_ne!(first, different);
        assert_eq!(first.len(), 64);
    }

    #[test]
    fn version_comparison_accepts_v_prefix_and_rejects_non_upgrade() {
        assert!(same_version("v1.2.3", "1.2.3"));
        assert!(validate_version_upgrade("1.2.3", "1.2.4").is_ok());
        assert!(validate_version_upgrade("1.2.3", "1.2.3").is_err());
        assert!(validate_version_upgrade("1.2.3", "1.2.2").is_err());
    }
}