Skip to content

Commit

Permalink
Change versioning strategies and use version file instead of metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
Systemcluster committed Sep 6, 2020
1 parent 5dcff07 commit 307dacd
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 24 deletions.
1 change: 1 addition & 0 deletions src/args.rs
Expand Up @@ -62,6 +62,7 @@ pub fn get_versioning(versioning: &str) -> u8 {
"sidebyside" => 0,
"default" => 0,
"replace" => 1,
"none" => 2,
_ => {
println!(
"{}: {}",
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Expand Up @@ -37,7 +37,7 @@ pub struct Args {
/// Unpack directory name [default: inferred from input directory]
#[clap(short = "d", long)]
unpack_directory: Option<String>,
/// Versioning strategy (sidebyside, replace)
/// Versioning strategy (sidebyside, replace, none)
#[clap(short = "v", long, default_value = "sidebyside")]
versioning: String,
/// Open a console when starting the runner on Windows
Expand Down
43 changes: 20 additions & 23 deletions startpe/src/main.rs
Expand Up @@ -5,7 +5,6 @@ use std::{
env::current_exe,
fs::{create_dir_all, metadata, read_link, File},
io::{copy, BufReader, ErrorKind, Seek, SeekFrom},
time::SystemTime,
};

use memmap::MmapOptions;
Expand All @@ -29,6 +28,9 @@ use decompress::*;
mod command;
use command::*;

mod versioning;
use versioning::*;

#[repr(C)]
#[derive(Clone, Copy, SizeWith, IOread)]
struct DistributionInfo {
Expand Down Expand Up @@ -97,6 +99,15 @@ fn main() {

println!("payload size: {}", payload_size);

let version = std::str::from_utf8(
&info.uid[0..(info
.uid
.iter()
.position(|&c| c == b'\0')
.unwrap_or(info.uid.len()))],
)
.unwrap();

let unpack_root = match info.unpack_target {
0 => std::env::temp_dir(),
1 => dirs::data_local_dir().unwrap(),
Expand All @@ -114,31 +125,15 @@ fn main() {
.unwrap(),
);
if info.versioning == 0 {
unpack_dir = unpack_dir.join(
std::str::from_utf8(
&info.uid[0..(info
.uid
.iter()
.position(|&c| c == b'\0')
.unwrap_or(info.uid.len()))],
)
.unwrap(),
);
unpack_dir = unpack_dir.join(version);
}
println!("extracting to: {}", unpack_dir.display());

let should_extract = info.versioning == 1
|| metadata(&unpack_dir)
.map(|meta| {
meta.modified().unwrap_or(SystemTime::UNIX_EPOCH)
< current_exe()
.unwrap()
.metadata()
.unwrap()
.modified()
.unwrap_or(SystemTime::UNIX_EPOCH)
})
.unwrap_or(true);
let should_extract = match info.versioning {
0 => get_version(&unpack_dir) != version,
1 => get_version(&unpack_dir) != version,
_ => true,
};

println!("should extract: {}", should_extract);

Expand Down Expand Up @@ -221,6 +216,8 @@ fn main() {
symlink(&header.link_name().unwrap().unwrap(), &target).unwrap();
}
});

set_version(&unpack_dir, version);
}

let current_dir = std::env::current_dir().unwrap();
Expand Down
14 changes: 14 additions & 0 deletions startpe/src/versioning.rs
@@ -0,0 +1,14 @@
use std::{
fs::{read_to_string, write},
path::Path,
};

const version_file: &str = "._wrappe_uid_";

pub fn get_version(target: &Path) -> String {
read_to_string(target.join(&version_file)).unwrap_or("0".to_string())
}

pub fn set_version(target: &Path, version: &str) {
write(target.join(&version_file), version).unwrap()
}

0 comments on commit 307dacd

Please sign in to comment.