Skip to content

Commit

Permalink
[#96] Add additional state to process monitoring
Browse files Browse the repository at this point in the history
  • Loading branch information
elfenpiff committed Feb 2, 2024
1 parent 6bd6407 commit c1d6db7
Show file tree
Hide file tree
Showing 3 changed files with 339 additions and 151 deletions.
43 changes: 43 additions & 0 deletions iceoryx2-bb/posix/src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ pub struct FileBuilder {
file_path: FilePath,
access_mode: AccessMode,
permission: Permission,
has_ownership: bool,
owner: Option<u32>,
group: Option<u32>,
truncate_size: Option<usize>,
Expand All @@ -278,13 +279,21 @@ impl FileBuilder {
file_path: *file_path,
access_mode: AccessMode::Read,
permission: Permission::OWNER_ALL,
has_ownership: false,
owner: None,
group: None,
truncate_size: None,
creation_mode: None,
}
}

/// Defines if the created or opened file is owned by the [`File`] object. If it is owned, the
/// [`File`] object will remove the underlying file when it goes out of scope.
pub fn has_ownership(mut self, value: bool) -> Self {
self.has_ownership = value;
self
}

/// Returns a [`FileCreationBuilder`] object to define further settings exclusively
/// for newly created files. Sets the [`AccessMode`] of the file to [`AccessMode::ReadWrite`].
pub fn creation_mode(mut self, value: CreationMode) -> FileCreationBuilder {
Expand Down Expand Up @@ -415,6 +424,25 @@ impl FileCreationBuilder {
pub struct File {
path: Option<FilePath>,
file_descriptor: FileDescriptor,
has_ownership: bool,
}

impl Drop for File {
fn drop(&mut self) {
if self.has_ownership {
match self.path {
None => {
warn!(from self, "Files created from file descriptors cannot remove themselves.")
}
Some(p) => match File::remove(&p) {
Ok(false) | Err(_) => {
warn!(from self, "Failed to remove owned file");
}
Ok(true) => (),
},
};
}
}
}

impl File {
Expand Down Expand Up @@ -462,6 +490,7 @@ impl File {
return Ok(File {
path: Some(config.file_path),
file_descriptor: v,
has_ownership: config.has_ownership,
});
}

Expand Down Expand Up @@ -494,6 +523,7 @@ impl File {
return Ok(File {
path: Some(config.file_path),
file_descriptor: v,
has_ownership: config.has_ownership,
});
}

Expand All @@ -513,13 +543,26 @@ impl File {
);
}

/// Takes the ownership to the underlying file, meaning when [`File`] goes out of scope file
/// in the file system will be removed.
pub fn acquire_ownership(&mut self) {
self.has_ownership = true;
}

/// Releases the ownership to the underlying file, meaning when [`File`] goes out of scope, it
/// does not remove the file from the file system.
pub fn release_ownership(&mut self) {
self.has_ownership = false;
}

/// Takes ownership of a [`FileDescriptor`]. When [`File`] goes out of scope the file
/// descriptor is closed.
pub fn from_file_descriptor(file_descriptor: FileDescriptor) -> Self {
trace!(from "File::from_file_descriptor", "opened {:?}", file_descriptor);
Self {
path: None,
file_descriptor,
has_ownership: false,
}
}

Expand Down
Loading

0 comments on commit c1d6db7

Please sign in to comment.