Skip to content

Commit

Permalink
fix(rust): .mode method does not exit on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
etorreborre committed Feb 21, 2023
1 parent 3e8f61c commit f563738
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 18 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions implementations/rust/ockam/ockam_transport_tcp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ rand = "0.7"
hashbrown = { version = "0.13", default-features = false }
tracing = { version = "0.1", default-features = false }
socket2 = "0.4.7"
cfg-if = "1.0.0"

[dev-dependencies]
trybuild = { version = "1.0", features = ["diff"] }
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{TcpRecvProcessor, TcpRouterHandle};
use cfg_if::cfg_if;
use core::time::Duration;
use ockam_core::{
async_trait,
Expand Down Expand Up @@ -180,7 +181,6 @@ impl Worker for TcpSendWorker {

async fn initialize(&mut self, ctx: &mut Self::Context) -> Result<()> {
ctx.set_cluster(crate::CLUSTER_NAME).await?;

if self.tx.is_none() {
debug!(addr = %self.peer, "Connecting");
let connection = match TcpStream::connect(self.peer).await {
Expand All @@ -196,10 +196,16 @@ impl Worker for TcpSendWorker {
}
};

let keepalive = TcpKeepalive::new()
let mut keepalive = TcpKeepalive::new()
.with_time(Duration::from_secs(300))
.with_retries(2)
.with_interval(Duration::from_secs(75));

cfg_if! {
if #[cfg(unix)] {
keepalive = keepalive.with_retries(2);
}
}

let socket = SockRef::from(&connection);
socket.set_tcp_keepalive(&keepalive).unwrap();

Expand Down
29 changes: 18 additions & 11 deletions implementations/rust/ockam/ockam_vault/src/storage/file_storage.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::VaultError;
use cfg_if::cfg_if;
use fs2::FileExt; //locking
use ockam_core::compat::boxed::Box;
use ockam_core::errcode::{Kind, Origin};
Expand Down Expand Up @@ -119,17 +120,23 @@ impl FileStorage {
) -> Result<()> {
let data = serde_json::to_vec(vault).map_err(|_| VaultError::StorageError)?;
use std::io::prelude::*;
use std::os::unix::prelude::*;
let _ = std::fs::remove_file(temp_path);
let mut file = std::fs::OpenOptions::new()
.write(true)
// `create_new` means we error if it exists. This ensures the mode we
// provide is respect (the `mode(0o600)` is only used if creating the
// file)
.create_new(true)
.mode(0o600) // TODO: not portable, what about windows?
.open(temp_path)
.map_err(|_| VaultError::StorageError)?;
cfg_if! {
if #[cfg(windows)] {
let mut file = std::fs::OpenOptions::new()
.write(true)
.create(true)
.open(temp_path)
.map_err(|_| VaultError::StorageError)?;
} else {
use std::os::unix::fs::OpenOptionsExt;
let mut file = std::fs::OpenOptions::new()
.write(true)
.create(true)
.mode(0o600)
.open(temp_path)
.map_err(|_| VaultError::StorageError)?;
}
}
file.write_all(&data)
.map_err(|_| VaultError::StorageError)?;
file.flush().map_err(|_| VaultError::StorageError)?;
Expand Down
1 change: 1 addition & 0 deletions tools/docs/example_test_helper/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ thiserror = "1.0"
tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
tempfile = "3"
cfg-if = "1.0.0"
12 changes: 8 additions & 4 deletions tools/docs/example_test_helper/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
//! assert_eq!(version.matches(".").count(), 2);
//! ```
//!
use cfg_if::cfg_if;
#[cfg(unix)]
use duct::unix::HandleExt;
use duct::{cmd, Handle};
use regex::Regex;
Expand Down Expand Up @@ -228,10 +230,12 @@ impl Drop for CmdRunner {
///
/// On non-unix, kill process.
fn drop(&mut self) {
if cfg!(unix) {
let _ = self.handle.send_signal(libc::SIGINT);
} else {
let _ = self.handle.kill();
cfg_if! {
if #[cfg(unix)] {
let _ = self.handle.send_signal(libc::SIGINT);
} else {
let _ = self.handle.kill();
}
}
}
}
Expand Down

0 comments on commit f563738

Please sign in to comment.