From 81f582c64d295cac38941a15681c30758799c797 Mon Sep 17 00:00:00 2001 From: "Neal H. Walfield" Date: Mon, 7 Feb 2022 14:20:56 +0100 Subject: [PATCH] Fix build failures on Windows. --- src/keystore.rs | 1 + src/lib.rs | 21 +++++++++++++++------ src/pep.rs | 24 ++++++++++++++---------- 3 files changed, 30 insertions(+), 16 deletions(-) diff --git a/src/keystore.rs b/src/keystore.rs index a8cb591..0d2babc 100644 --- a/src/keystore.rs +++ b/src/keystore.rs @@ -1,4 +1,5 @@ use std::cmp; +#[cfg(not(windows))] use std::env; use std::path::Path; use std::path::PathBuf; diff --git a/src/lib.rs b/src/lib.rs index b8c0a49..473b45f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,7 +3,6 @@ use std::convert::TryInto; use std::env; use std::ffi::{ CStr, - OsStr, }; use std::io::{ Read, @@ -19,6 +18,9 @@ use std::time::{ UNIX_EPOCH, }; +#[allow(unused_imports)] +use anyhow::Context; + use libc::{ c_char, c_uint, @@ -284,16 +286,23 @@ ffi!(fn pgp_init_(session: *mut Session, _in_first: bool, #[cfg(not(windows))] let per_user_directory = { + use std::ffi::OsStr; use std::os::unix::ffi::OsStrExt; OsStr::from_bytes(per_user_directory.to_bytes()) }; #[cfg(windows)] let per_user_directory = { - use std::ffi::OsString; - use std::os::windows::prelude::*; - - let os_string = OsString::from_wide(per_user_directory.as_bytes()); - os_string.as_os_str() + // The engine guarantees that it is UTF-8 encoded. + // + // https://gitea.pep.foundation/pEp.foundation/pEpEngine/src/commit/2f0927554ac1b7ca10e27b19650b5158d97dfc3f/src/platform_windows.cpp#L177 + match per_user_directory.to_str() { + Ok(s) => s, + Err(err) => + return Err(Error::IllegalValue( + format!("\ +API violation: per_user_directory not UTF-8 encoded ({:?}: {})", + per_user_directory, err))), + } }; let ks = keystore::Keystore::init(Path::new(per_user_directory))?; diff --git a/src/pep.rs b/src/pep.rs index e004d29..a0f0d92 100644 --- a/src/pep.rs +++ b/src/pep.rs @@ -1,6 +1,7 @@ use std::ops::BitAnd; use std::convert::TryFrom; +#[cfg(not(windows))] use libc::tm; use sequoia_openpgp as openpgp; @@ -218,20 +219,23 @@ pub enum Error { // https://gitea.pep.foundation/pEp.foundation/pEpEngine/src/branch/master/src/timestamp.h #[cfg(not(windows))] pub type Timestamp = tm; + +#[cfg(windows)] +use libc::{c_int, c_long}; #[cfg(windows)] #[repr(C)] // for time values all functions are using POSIX struct tm pub struct Timestamp { - tm_sec: c_int, - tm_min: c_int, - tm_hour: c_int, - tm_mday: c_int, - tm_mon: c_int, - tm_year: c_int, - tm_wday: c_int, - tm_yday: c_int, - tm_isdst: c_int, - tm_gmtoff: c_long, // offset from GMT in seconds + pub tm_sec: c_int, + pub tm_min: c_int, + pub tm_hour: c_int, + pub tm_mday: c_int, + pub tm_mon: c_int, + pub tm_year: c_int, + pub tm_wday: c_int, + pub tm_yday: c_int, + pub tm_isdst: c_int, + pub tm_gmtoff: c_long, // offset from GMT in seconds } // See pEpEngine/src/pEpEngine.h:PEP_comm_format. -- GitLab