Skip to content

Commit

Permalink
🐛 Fixed panic in std:::env::temp_dir used in target wasm32-wasi
Browse files Browse the repository at this point in the history
  • Loading branch information
ChanTsune committed Dec 14, 2024
1 parent b42a658 commit 899bb18
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 7 deletions.
21 changes: 17 additions & 4 deletions cli/src/command/commons.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
use crate::{
cli::{CipherAlgorithmArgs, CompressionAlgorithmArgs, HashAlgorithmArgs},
utils::{self, PathPartExt},
utils::{self, env::temp_dir, PathPartExt},
};
use normalize_path::*;
use pna::{
prelude::*, Archive, EntryBuilder, EntryName, EntryPart, EntryReference, NormalEntry,
ReadEntry, SolidEntryBuilder, WriteOptions, MIN_CHUNK_BYTES_SIZE, PNA_HEADER,
};
use std::{
env::temp_dir,
fs,
io::{self, prelude::*},
path::{Path, PathBuf},
Expand Down Expand Up @@ -591,7 +590,14 @@ where
let password = password_provider();
let output_path = output_path.as_ref();
let random = rand::random::<usize>();
let temp_path = temp_dir().join(format!("{}.pna.tmp", random));
let temp_path = temp_dir()
.unwrap_or_else(|| {
output_path
.parent()
.map(PathBuf::from)
.unwrap_or_else(|| PathBuf::from("."))
})
.join(format!("{}.pna.tmp", random));
let outfile = fs::File::create(&temp_path)?;
let mut out_archive = Archive::write_header(outfile)?;

Expand Down Expand Up @@ -648,7 +654,14 @@ where
let password = password_provider();
let output_path = output_path.as_ref();
let random = rand::random::<usize>();
let temp_path = temp_dir().join(format!("{}.pna.tmp", random));
let temp_path = temp_dir()
.unwrap_or_else(|| {
output_path
.parent()
.map(PathBuf::from)
.unwrap_or_else(|| PathBuf::from("."))
})
.join(format!("{}.pna.tmp", random));
let outfile = fs::File::create(&temp_path)?;
let mut out_archive = Archive::write_header(outfile)?;

Expand Down
12 changes: 9 additions & 3 deletions cli/src/command/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,12 @@ use crate::{
},
Command,
},
utils::{self, PathPartExt},
utils::{self, env::temp_dir, PathPartExt},
};
use clap::{ArgGroup, Parser, ValueHint};
use normalize_path::*;
use pna::{Archive, Metadata};
use std::{
env::temp_dir,
fs, io,
path::{Path, PathBuf},
time::SystemTime,
Expand Down Expand Up @@ -200,7 +199,14 @@ fn update_archive<Strategy: TransformStrategy>(args: UpdateCommand) -> io::Resul
let (tx, rx) = std::sync::mpsc::channel();

let random = rand::random::<usize>();
let outfile_path = temp_dir().join(format!("{}.pna.tmp", random));
let outfile_path = temp_dir()
.unwrap_or_else(|| {
archive_path
.parent()
.map(PathBuf::from)
.unwrap_or_else(|| PathBuf::from("."))
})
.join(format!("{}.pna.tmp", random));

let outfile = fs::File::create(&outfile_path)?;
let mut out_archive = Archive::write_header(outfile)?;
Expand Down
1 change: 1 addition & 0 deletions cli/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#[cfg(feature = "acl")]
pub(crate) mod acl;
pub(crate) mod env;
pub(crate) mod fmt;
pub(crate) mod fs;
mod globs;
Expand Down
9 changes: 9 additions & 0 deletions cli/src/utils/env.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use std::path::PathBuf;

pub(crate) fn temp_dir() -> Option<PathBuf> {
if cfg!(target_os = "wasi") {
None
} else {
Some(std::env::temp_dir())
}
}

0 comments on commit 899bb18

Please sign in to comment.