Repository: tauri-apps/tauri
File: crates/tauri-bundler/src/bundle.rs (Rust)

Code before:
// Copyright 2019-2024 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

mod category;
mod kmp;
#[cfg(target_os = "linux")]
mod linux;
#[cfg(target_os = "macos")]
mod macos;
mod platform;
mod settings;
mod updater_bundle;
mod windows;

use tauri_utils::{display_path, platform::Target as TargetPlatform};

const BUNDLE_VAR_TOKEN: &[u8] = b"__TAURI_BUNDLE_TYPE_VAR_UNK";
/// Patch a binary with bundle type information
fn patch_binary(binary: &PathBuf, package_type: &PackageType) -> crate::Result<()> {
  let mut file_data = std::fs::read(binary).expect("Could not read binary file.");

  if let Some(bundle_var_index) = kmp::index_of(BUNDLE_VAR_TOKEN, &file_data) {
    let bundle_type = match package_type {
      crate::PackageType::Deb => b"__TAURI_BUNDLE_TYPE_VAR_DEB",
      crate::PackageType::Rpm => b"__TAURI_BUNDLE_TYPE_VAR_RPM",
      crate::PackageType::AppImage => b"__TAURI_BUNDLE_TYPE_VAR_APP",
      crate::PackageType::Nsis => b"__TAURI_BUNDLE_TYPE_VAR_NSS",
      crate::PackageType::WindowsMsi => b"__TAURI_BUNDLE_TYPE_VAR_MSI",
      _ => {
        return Err(crate::Error::InvalidPackageType(
          package_type.short_name().to_owned(),
        ))
      }
    };

    file_data[bundle_var_index..bundle_var_index + BUNDLE_VAR_TOKEN.len()]
      .copy_from_slice(bundle_type);

    std::fs::write(binary, &file_data)
      .map_err(|e| crate::Error::BinaryWriteError(e.to_string()))?;
  } else {
    return Err(crate::Error::MissingBundleTypeVar);
  }
  Ok(())
}

pub use self::{
  category::AppCategory,
  settings::{
    AppImageSettings, BundleBinary, BundleSettings, CustomSignCommandSettings, DebianSettings,

Review comment (bug):
Didn't look at it closely yet, but I think this would fail when patching the binary for the second bundle type right? (e.g `.msi` first and `__TAURI_BUNDLE_TYPE` becomes `__TAURI_BUNDLE_TYPE_VAR_MSI` already that when we do it for the NSIS bundle, we can no longer find `__TAURI_BUNDLE_TYPE_VAR_UNK`)

Code after:
// Copyright 2019-2024 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

mod category;
#[cfg(any(target_os = "linux", target_os = "windows"))]
mod kmp;
#[cfg(target_os = "linux")]
mod linux;
#[cfg(target_os = "macos")]
mod macos;
mod platform;
mod settings;
mod updater_bundle;
mod windows;

use tauri_utils::{display_path, platform::Target as TargetPlatform};

#[cfg(any(target_os = "linux", target_os = "windows"))]
const BUNDLE_VAR_TOKEN: &[u8] = b"__TAURI_BUNDLE_TYPE_VAR_UNK";
/// Patch a binary with bundle type information
#[cfg(any(target_os = "linux", target_os = "windows"))]
fn patch_binary(binary: &PathBuf, package_type: &PackageType) -> crate::Result<()> {
  let mut file_data = std::fs::read(binary).expect("Could not read binary file.");

  if let Some(bundle_var_index) = kmp::index_of(BUNDLE_VAR_TOKEN, &file_data) {
    #[cfg(target_os = "linux")]
    let bundle_type = match package_type {
      crate::PackageType::Deb => b"__TAURI_BUNDLE_TYPE_VAR_DEB",
      crate::PackageType::Rpm => b"__TAURI_BUNDLE_TYPE_VAR_RPM",
      crate::PackageType::AppImage => b"__TAURI_BUNDLE_TYPE_VAR_APP",
      _ => {
        return Err(crate::Error::InvalidPackageType(
          package_type.short_name().to_owned(),
          "Linux".to_owned(),
        ))
      }
    };
    #[cfg(target_os = "windows")]
    let bundle_type = match package_type {
      crate::PackageType::Nsis => b"__TAURI_BUNDLE_TYPE_VAR_NSS",
      crate::PackageType::WindowsMsi => b"__TAURI_BUNDLE_TYPE_VAR_MSI",
      _ => {
        return Err(crate::Error::InvalidPackageType(
          package_type.short_name().to_owned(),
          "Windows".to_owned(),
        ))
      }
    };

    file_data[bundle_var_index..bundle_var_index + BUNDLE_VAR_TOKEN.len()]