Skip to content

Commit

Permalink
Add initial support for checksumming.
Browse files Browse the repository at this point in the history
  • Loading branch information
tiziodcaio committed Mar 20, 2024
1 parent 234daf9 commit 101bc17
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 13 deletions.
20 changes: 20 additions & 0 deletions native/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions native/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ immutable-runtime = []
[dependencies]
ab_glyph = "0.2.23"
anyhow = "1.0.79"
blake3 = "1.5.0"
byteorder = "1.5.0"
cfg-if = "1.0.0"
clap = { version = "^4.4.18", features = ["derive"] }
Expand Down
30 changes: 23 additions & 7 deletions native/src/components/runtime.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use std::fs::{read_dir, remove_dir_all, remove_file};
use std::io::Result as IoResult;
use std::fs::{read_dir, remove_dir_all, remove_file, File};
use std::io::{Read, Result as IoResult};
use std::path::{Path, PathBuf};
use std::process::{Child, Command};

use anyhow::{anyhow, Context, Result};
use blake3::Hasher;
use cfg_if::cfg_if;
use configparser::ini::Ini;
use fs_extra::dir::{copy, CopyOptions};
Expand All @@ -13,6 +14,17 @@ use tempfile::{NamedTempFile, TempDir};
use crate::components::site::Site;
use crate::directories::ProjectDirs;

// TODO: Remove this constant and implement variable firefox path into user documentation
pub const FFOX: &str = "/usr/lib/firefox/";

pub fn b3hasher() -> String {
let mut file = File::open(Path::new(FFOX).join("firefox")).unwrap();
let mut buf = Vec::new();
let _ = file.read_to_end(&mut buf);

Hasher::new().update(&buf).finalize().to_string()
}

cfg_if! {
if #[cfg(any(platform_linux, platform_bsd))] {

Expand Down Expand Up @@ -288,23 +300,21 @@ impl Runtime {

#[cfg(all(any(target_os = "linux", target_os = "bsd"), not(feature = "immutable-runtime")))]
pub fn link(&self) -> Result<()> {
use crate::storage::Storage;
use std::fs::{copy, create_dir_all};
use std::os::unix::fs::symlink;

use crate::storage::Storage;

let dirs = ProjectDirs::new()?;
let mut storage = Storage::load(&dirs)?;

self.uninstall()?;

storage.config.use_linked_runtime = true;
storage.write(&dirs)?;

info!("Linking the runtime");

if Path::new("/usr/lib/firefox/").exists() {
for entry in read_dir("/usr/lib/firefox/")?.flatten() {
if Path::new(FFOX).exists() {
for entry in read_dir(FFOX)?.flatten() {
let entry = entry.path();
match entry.file_name().expect("Couldn't retrieve a file name").to_str() {
// Use a different branch for the "defaults" folder due to the patches to apply afterwhile
Expand All @@ -320,6 +330,10 @@ impl Runtime {
}
Some("firefox") => {
copy(entry, self.directory.join("firefox"))?;

storage.config.hash = b3hasher();

trace!("Hash: {}", b3hasher());
}
Some(&_) => {
let link = self.directory.join(entry.file_name().unwrap());
Expand All @@ -330,6 +344,8 @@ impl Runtime {
}
}

storage.write(&dirs)?;

info!("Runtime linked!");

Ok(())
Expand Down
12 changes: 7 additions & 5 deletions native/src/console/site.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,14 @@ use url::Url;
use crate::components::runtime::Runtime;
use crate::components::site::{Site, SiteConfig};
use crate::console::app::{
SiteInstallCommand,
SiteLaunchCommand,
SiteUninstallCommand,
SiteUpdateCommand,
SiteInstallCommand, SiteLaunchCommand, SiteUninstallCommand, SiteUpdateCommand,
};
use crate::console::{store_value, store_value_vec, Run};
use crate::directories::ProjectDirs;
use crate::integrations;
use crate::integrations::{IntegrationInstallArgs, IntegrationUninstallArgs};
use crate::storage::Storage;
use crate::utils::construct_certificates_and_client;
use crate::{components, integrations};

impl Run for SiteLaunchCommand {
fn run(&self) -> Result<()> {
Expand Down Expand Up @@ -50,6 +47,11 @@ impl Run for SiteLaunchCommand {
bail!("Runtime not installed");
}

if storage.config.hash != components::runtime::b3hasher() && !storage.config.hash.is_empty()
{
runtime.link()?;
}

// Patching on macOS is always needed to correctly show the web app name
// Otherwise, patch runtime and profile only if needed
let should_patch = if cfg!(platform_macos) || storage.config.always_patch {
Expand Down
3 changes: 2 additions & 1 deletion native/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,11 @@ pub struct Config {
/// May be overwritten with a system environment variable.
pub runtime_use_portals: bool,

#[cfg(target_os = "linux")]
#[cfg(any(target_os = "linux", target_os = "bsd"))]
/// Experimental: Using the system runtime to save some disk space.
/// This might not work on your system.
pub use_linked_runtime: bool,
pub hash: String,
}

#[non_exhaustive]
Expand Down

0 comments on commit 101bc17

Please sign in to comment.