Skip to content

Commit 7106020

Browse files
committed
Set file open flags on Windows
1 parent f5250d3 commit 7106020

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

startpe/src/decompress.rs

+16-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ use std::{
88
time::Duration,
99
};
1010

11+
#[cfg(windows)]
12+
use std::os::windows::fs::OpenOptionsExt;
13+
1114
use filetime::{set_file_times, set_symlink_file_times, FileTime};
1215
use fslock::LockFile;
1316
use rayon::prelude::*;
@@ -215,7 +218,13 @@ pub fn decompress(
215218
}
216219
if verification == 2 {
217220
// verify checksums
218-
let target = File::open(&path);
221+
#[cfg(windows)]
222+
let target = File::options()
223+
.read(true)
224+
.custom_flags(0x08000000) // FILE_FLAG_SEQUENTIAL_SCAN
225+
.open(&path);
226+
#[cfg(not(windows))]
227+
let target = File::options().read(true).open(&path);
219228
if target.is_err() {
220229
eprintln!(
221230
"verification failed: couldn't open file: {}",
@@ -343,7 +352,12 @@ pub fn decompress(
343352
let mut reader = HashReader::new(content, XxHash64::with_seed(HASH_SEED));
344353
{
345354
let mut reader = BufReader::with_capacity(DCtx::in_size(), &mut reader);
346-
let output = File::create(&path).unwrap();
355+
let output = File::options()
356+
.write(true)
357+
.create(true)
358+
.truncate(true)
359+
.open(&path)
360+
.unwrap_or_else(|e| panic!("failed to create file {}: {}", path.display(), e));
347361
let mut output = BufWriter::with_capacity(DCtx::out_size(), output);
348362
let decoder = if let Some(dict) = &dictionary {
349363
Decoder::with_prepared_dictionary(&mut reader, dict)

startpe/src/main.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ use std::{
88
time::SystemTime,
99
};
1010

11+
#[cfg(windows)]
12+
use std::os::windows::fs::OpenOptionsExt;
13+
1114
#[cfg(any(unix, target_os = "redox"))]
1215
use std::os::unix::process::CommandExt;
1316
#[cfg(not(any(unix, target_os = "redox")))]
@@ -66,7 +69,17 @@ fn main() {
6669
while let Ok(link) = read_link(&exe) {
6770
exe = link;
6871
}
69-
let file = File::open(&exe).expect("couldn't open current executable");
72+
#[cfg(windows)]
73+
let file = File::options()
74+
.read(true)
75+
.custom_flags(0x10000000) // FILE_FLAG_RANDOM_ACCESS
76+
.open(&exe)
77+
.expect("couldn't open current executable");
78+
#[cfg(not(windows))]
79+
let file = File::options()
80+
.read(true)
81+
.open(&exe)
82+
.expect("couldn't open current executable");
7083

7184
let mmap = unsafe {
7285
MmapOptions::new()

0 commit comments

Comments
 (0)