diff --git a/iceoryx2-bb/posix/src/process_state.rs b/iceoryx2-bb/posix/src/process_state.rs index 89b5e80a8..d93e391a9 100644 --- a/iceoryx2-bb/posix/src/process_state.rs +++ b/iceoryx2-bb/posix/src/process_state.rs @@ -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); } } diff --git a/iceoryx2-bb/posix/tests/process_state_tests.rs b/iceoryx2-bb/posix/tests/process_state_tests.rs index 7ec48b728..c534b5044 100644 --- a/iceoryx2-bb/posix/tests/process_state_tests.rs +++ b/iceoryx2-bb/posix/tests/process_state_tests.rs @@ -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(); @@ -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(); @@ -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(); @@ -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() { @@ -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); diff --git a/iceoryx2-bb/system-types/src/path.rs b/iceoryx2-bb/system-types/src/path.rs index fa125b96d..585b49697 100644 --- a/iceoryx2-bb/system-types/src/path.rs +++ b/iceoryx2-bb/system-types/src/path.rs @@ -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]) } } } diff --git a/iceoryx2-pal/posix/src/windows/fcntl.rs b/iceoryx2-pal/posix/src/windows/fcntl.rs index 59c64032e..a5413fead 100644 --- a/iceoryx2-pal/posix/src/windows/fcntl.rs +++ b/iceoryx2-pal/posix/src/windows/fcntl.rs @@ -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; } diff --git a/iceoryx2-pal/posix/src/windows/win32_call.rs b/iceoryx2-pal/posix/src/windows/win32_call.rs index fe0569b7e..249b08423 100644 --- a/iceoryx2-pal/posix/src/windows/win32_call.rs +++ b/iceoryx2-pal/posix/src/windows/win32_call.rs @@ -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::{ @@ -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), } }