Skip to content

Commit

Permalink
Compare instead of store an hash
Browse files Browse the repository at this point in the history
  • Loading branch information
tiziodcaio committed Mar 20, 2024
1 parent 101bc17 commit 2e249b6
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 28 deletions.
17 changes: 2 additions & 15 deletions native/src/components/runtime.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use std::fs::{read_dir, remove_dir_all, remove_file, File};
use std::io::{Read, Result as IoResult};
use std::fs::{read_dir, remove_dir_all, remove_file};
use std::io::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 @@ -17,14 +16,6 @@ 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 @@ -330,10 +321,6 @@ 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 Down
14 changes: 5 additions & 9 deletions native/src/console/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,11 @@ impl Run for RuntimeInstallCommand {
let dirs = ProjectDirs::new()?;
let runtime = Runtime::new(&dirs)?;

cfg_if! {
if #[cfg(target_os = "linux")] {
if self.link {
runtime.link().context("Failed to link runtime")?
}
} else {
runtime.install().context("Failed to install runtime")?;
}
};
if cfg!(any(linux, bsd)) && self.link {
runtime.link().context("Failed to link runtime")?
} else {
runtime.install().context("Failed to install runtime")?;
}

let runtime = Runtime::new(&dirs)?;
runtime.patch(&dirs, None)?;
Expand Down
18 changes: 15 additions & 3 deletions native/src/console/site.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use std::convert::TryInto;
use std::fs::metadata;
use std::io;
use std::fs::{metadata, File};
use std::io::Write;
use std::io::{self, Read};
use std::path::Path;

use anyhow::{bail, Context, Result};
use blake3::{hash, Hash};
use cfg_if::cfg_if;
use log::{info, warn};
use ulid::Ulid;
Expand Down Expand Up @@ -47,7 +49,17 @@ impl Run for SiteLaunchCommand {
bail!("Runtime not installed");
}

if storage.config.hash != components::runtime::b3hasher() && !storage.config.hash.is_empty()
fn hasher(path: &str) -> Hash {
let mut file = File::open(Path::new(&path).join("firefox")).unwrap();
let mut buf = Vec::new();
let _ = file.read_to_end(&mut buf);

hash(&buf)
}

if storage.config.use_linked_runtime
&& hasher(components::runtime::FFOX)
!= hasher("/home/daniele/.local/share/firefoxpwa/runtime/")
{
runtime.link()?;
}
Expand Down
1 change: 0 additions & 1 deletion native/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ pub struct Config {
/// 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 2e249b6

Please sign in to comment.