endpoint_sec/event/event_rename.rs
1//! [`EventRename`]
2
3use std::ffi::OsStr;
4
5use endpoint_sec_sys::{es_destination_type_t, es_event_rename_t};
6
7use crate::File;
8
9/// Rename a file system object event.
10#[doc(alias = "es_event_rename_t")]
11pub struct EventRename<'a> {
12 /// Raw event
13 pub(crate) raw: &'a es_event_rename_t,
14}
15
16/// Represent a destination file for [`EventRename`].
17#[derive(Debug, PartialEq, Eq, Hash)]
18#[doc(alias = "es_destination_type_t")]
19pub enum EventRenameDestinationFile<'a> {
20 /// The destination file already exist at the time of the event.
21 ExistingFile(File<'a>),
22 /// The destination doesn't exist at the time of the event.
23 NewPath {
24 /// The directory into which the file will be renamed.
25 directory: File<'a>,
26 /// The name of the new file that will be created.
27 filename: &'a OsStr,
28 },
29}
30
31impl<'a> EventRename<'a> {
32 /// The source file that is being renamed.
33 #[inline(always)]
34 pub fn source(&self) -> File<'a> {
35 // Safety: 'a tied to self, object obtained through ES
36 File::new(unsafe { self.raw.source() })
37 }
38
39 /// Information about the destination of the renamed file.
40 #[inline(always)]
41 pub fn destination(&self) -> Option<EventRenameDestinationFile<'a>> {
42 match self.raw.destination_type {
43 es_destination_type_t::ES_DESTINATION_TYPE_EXISTING_FILE => {
44 // Safety: Safe as we select the union field corresponding to that type.
45 Some(EventRenameDestinationFile::ExistingFile(unsafe {
46 File::new(self.raw.destination.existing_file.as_ref())
47 }))
48 },
49 es_destination_type_t::ES_DESTINATION_TYPE_NEW_PATH => {
50 // Safety: Safe as we select the union fields corresponding to that type.
51 let new_path = unsafe { &self.raw.destination.new_path };
52 Some(EventRenameDestinationFile::NewPath {
53 // Safety: 'a tied to self, object obtained through ES
54 directory: File::new(unsafe { new_path.dir() }),
55 // Safety: 'a tied to self, object obtained through ES
56 filename: unsafe { new_path.filename.as_os_str() },
57 })
58 },
59 _ => None,
60 }
61 }
62}
63
64// Safety: safe to send across threads: does not contain any interior mutability nor depend on current thread state
65unsafe impl Send for EventRename<'_> {}
66// Safety: safe to share across threads: does not contain any interior mutability nor depend on current thread state
67unsafe impl Sync for EventRename<'_> {}
68
69impl_debug_eq_hash_with_functions!(EventRename<'a>; source, destination);