Skip to content

Commit

Permalink
[#96] Fix windows build
Browse files Browse the repository at this point in the history
  • Loading branch information
elfenpiff committed Feb 3, 2024
1 parent 54c3805 commit fb82e10
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 55 deletions.
2 changes: 1 addition & 1 deletion iceoryx2-bb/posix/src/process_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -800,7 +800,7 @@ impl ProcessCleaner {
"{} since an interrupt signal was received.", msg);
}
Err(e) => {
fail!(from origin, with ProcessCleanerCreateError::Interrupt,
fail!(from origin, with ProcessCleanerCreateError::UnknownError,
"{} due to an unknown failure ({:?}).", msg, e);
}
}
Expand Down
40 changes: 6 additions & 34 deletions iceoryx2-bb/posix/tests/process_state_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,17 @@
//
// SPDX-License-Identifier: Apache-2.0 OR MIT

use std::time::Duration;

use iceoryx2_bb_container::semantic_string::SemanticString;
use iceoryx2_bb_posix::config::*;
use iceoryx2_bb_posix::file::{File, FileBuilder};
use iceoryx2_bb_posix::file_descriptor::FileDescriptorBased;
use iceoryx2_bb_posix::file_descriptor::FileDescriptorManagement;
use iceoryx2_bb_posix::file_lock::LockType;
use iceoryx2_bb_posix::shared_memory::Permission;
use iceoryx2_bb_posix::unix_datagram_socket::CreationMode;
use iceoryx2_bb_posix::{process_state::*, unique_system_id::UniqueSystemId};
use iceoryx2_bb_system_types::{file_name::FileName, file_path::FilePath};
use iceoryx2_bb_testing::assert_that;
use iceoryx2_pal_posix::posix::{self, Struct};

fn generate_file_path() -> FilePath {
let mut file = FileName::new(b"process_state_tests").unwrap();
Expand Down Expand Up @@ -210,10 +209,9 @@ pub fn process_state_cleaner_cannot_be_created_when_process_does_not_exist() {
ProcessCleanerCreateError::DoesNotExist
);

let _file = FileBuilder::new(&path)
let file = FileBuilder::new(&path)
.has_ownership(true)
.creation_mode(CreationMode::PurgeAndCreate)
.permission(Permission::OWNER_WRITE)
.create()
.unwrap();

Expand All @@ -223,11 +221,12 @@ pub fn process_state_cleaner_cannot_be_created_when_process_does_not_exist() {
cleaner.err().unwrap(), eq
ProcessCleanerCreateError::DoesNotExist
);
drop(file);
std::thread::sleep(Duration::from_millis(100));

let _file = FileBuilder::new(&cleaner_path)
.has_ownership(true)
.creation_mode(CreationMode::PurgeAndCreate)
.permission(Permission::OWNER_WRITE)
.create()
.unwrap();

Expand Down Expand Up @@ -258,21 +257,6 @@ pub fn process_state_monitor_detects_alive_state_from_existing_process() {
assert_that!(monitor.state().unwrap(), eq ProcessState::DoesNotExist);
}

#[test]
#[cfg(not(any(target_os = "linux", target_os = "freebsd", target_os = "macos")))]
pub fn process_state_guard_cannot_be_removed_when_locked() {
let path = generate_file_path();

let _guard = ProcessGuard::new(&path).unwrap();
let result = unsafe { ProcessGuard::remove(&path) };

assert_that!(result, is_err);
assert_that!(
result.err().unwrap(), eq
ProcessGuardRemoveError::OwnedByAnotherProcess
);
}

#[test]
#[cfg(not(any(target_os = "linux", target_os = "freebsd", target_os = "macos")))]
pub fn process_state_cleaner_cannot_be_acquired_from_living_process() {
Expand Down Expand Up @@ -300,24 +284,12 @@ pub fn process_state_cleaner_cannot_be_acquired_twice() {
.create()
.unwrap();

let cleaner_file = FileBuilder::new(&cleaner_path)
let _cleaner_file = FileBuilder::new(&cleaner_path)
.has_ownership(true)
.creation_mode(CreationMode::PurgeAndCreate)
.create()
.unwrap();

let mut new_lock_state = posix::flock::new();
new_lock_state.l_type = LockType::Write as _;
new_lock_state.l_whence = posix::SEEK_SET as _;

unsafe {
posix::fcntl(
cleaner_file.file_descriptor().native_handle(),
posix::F_SETLK,
&mut new_lock_state,
)
};

let _cleaner = ProcessCleaner::new(&path).unwrap();
let cleaner = ProcessCleaner::new(&path);
assert_that!(cleaner, is_err);
Expand Down
42 changes: 27 additions & 15 deletions iceoryx2-bb/system-types/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,24 +67,36 @@ semantic_string! {
false
},
normalize: |this: &Path| {
let mut raw_path = [0u8; PATH_LENGTH];

let mut previous_char_is_path_separator = false;
let mut n = 0;
for i in 0..this.value.len() {
if i + 1 == this.value.len() && this.value[i] == PATH_SEPARATOR {
break;
}
let mut raw_path = [0u8; PATH_LENGTH];
let value = this.as_bytes();
let mut n = if let Some(&PATH_SEPARATOR) = value.first() {
raw_path[0] = PATH_SEPARATOR;
1
} else {
0
};

if !(previous_char_is_path_separator && this.value[i] == PATH_SEPARATOR) {
raw_path[n] = this.value[i];
n += 1;
}
let mut path_separator_size = 0;

previous_char_is_path_separator = this.value[i] == PATH_SEPARATOR
}
for entry in value
.split(|c| *c == PATH_SEPARATOR)
.filter(|entry| !entry.is_empty())
.filter(|entry| !(entry.len() == 1 && entry[0] == b'.'))
{
let new_n = n + path_separator_size + entry.len();
if path_separator_size > 0 {
raw_path[n] = PATH_SEPARATOR;
n += 1;
} else {
path_separator_size = 1;
}
raw_path[n..new_n].copy_from_slice(entry);
n = new_n;
}

Path::new(&raw_path[0..n]).expect("A normalized path from a path shall be always valid.")
// SAFETY
// * raw_path contains a valid path since the input `this` is a valid path
unsafe { Path::new_unchecked(&raw_path[0..n]) }
}
}

Expand Down
1 change: 0 additions & 1 deletion iceoryx2-pal/posix/src/windows/fcntl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,6 @@ pub unsafe fn fcntl(fd: int, cmd: int, arg: *mut flock) -> int {
if win32call! {LockFileEx(handle.handle, flags, 0, MAXWORD, MAXWORD, &mut overlapped)}
== FALSE
{
Errno::set(Errno::EINVAL);
return -1;
}

Expand Down
8 changes: 4 additions & 4 deletions iceoryx2-pal/posix/src/windows/win32_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ use windows_sys::Win32::{
ERROR_ACCESS_DENIED, ERROR_ALREADY_EXISTS, ERROR_ARENA_TRASHED, ERROR_BAD_COMMAND,
ERROR_BAD_LENGTH, ERROR_CURRENT_DIRECTORY, ERROR_DEV_NOT_EXIST, ERROR_FILE_EXISTS,
ERROR_FILE_NOT_FOUND, ERROR_FILE_TOO_LARGE, ERROR_HANDLE_DISK_FULL, ERROR_INVALID_ACCESS,
ERROR_INVALID_BLOCK, ERROR_INVALID_DATA, ERROR_INVALID_HANDLE, ERROR_NOT_ENOUGH_MEMORY,
ERROR_NOT_READY, ERROR_OUTOFMEMORY, ERROR_PATH_NOT_FOUND, ERROR_READ_FAULT,
ERROR_SECTOR_NOT_FOUND, ERROR_SHARING_BUFFER_EXCEEDED, ERROR_SUCCESS,
ERROR_INVALID_BLOCK, ERROR_INVALID_DATA, ERROR_INVALID_HANDLE, ERROR_LOCK_VIOLATION,
ERROR_NOT_ENOUGH_MEMORY, ERROR_NOT_READY, ERROR_OUTOFMEMORY, ERROR_PATH_NOT_FOUND,
ERROR_READ_FAULT, ERROR_SECTOR_NOT_FOUND, ERROR_SHARING_BUFFER_EXCEEDED, ERROR_SUCCESS,
ERROR_TOO_MANY_OPEN_FILES, ERROR_WRITE_FAULT, ERROR_WRITE_PROTECT, WIN32_ERROR,
},
Networking::WinSock::{
Expand Down Expand Up @@ -52,7 +52,7 @@ pub unsafe fn system_error_code_to_errno(value: WIN32_ERROR) {
ERROR_HANDLE_DISK_FULL => Errno::set(Errno::ENOBUFS),
ERROR_DEV_NOT_EXIST => Errno::set(Errno::ENODEV),
ERROR_ALREADY_EXISTS | ERROR_FILE_EXISTS => Errno::set(Errno::EEXIST),

ERROR_LOCK_VIOLATION => Errno::set(Errno::EAGAIN),
_ => Errno::set(Errno::EINVAL),
}
}
Expand Down

0 comments on commit fb82e10

Please sign in to comment.