Skip to main content

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")]
19#[non_exhaustive]
20pub enum EventRenameDestinationFile<'a> {
21    /// The destination file already exist at the time of the event.
22    #[non_exhaustive]
23    ExistingFile {
24        /// The file that will be overwritten by the rename operation.
25        file: File<'a>
26    },
27    /// The destination doesn't exist at the time of the event.
28    #[non_exhaustive]
29    NewPath {
30        /// The directory into which the file will be renamed.
31        directory: File<'a>,
32        /// The filename of the destination of the rename operation.
33        filename: &'a OsStr,
34    },
35}
36
37impl<'a> EventRename<'a> {
38    /// The source file that is being renamed.
39    #[inline(always)]
40    pub fn source(&self) -> File<'a> {
41        // Safety: 'a tied to self, object obtained through ES
42        File::new(unsafe { self.raw.source() })
43    }
44
45    /// Information about the destination of the renamed file.
46    #[inline(always)]
47    pub fn destination(&self) -> Option<EventRenameDestinationFile<'a>> {
48        match self.raw.destination_type {
49            es_destination_type_t::ES_DESTINATION_TYPE_EXISTING_FILE => {
50                // Safety: Safe as we select the union field corresponding to that type.
51                Some(EventRenameDestinationFile::ExistingFile {
52                    file: File::new(unsafe { self.raw.destination.existing_file.as_ref() })
53                })
54            },
55            es_destination_type_t::ES_DESTINATION_TYPE_NEW_PATH => {
56                // Safety: Safe as we select the union fields corresponding to that type.
57                let new_path = unsafe { &self.raw.destination.new_path };
58                Some(EventRenameDestinationFile::NewPath {
59                    // Safety: 'a tied to self, object obtained through ES
60                    directory: File::new(unsafe { new_path.dir() }),
61                    // Safety: 'a tied to self, object obtained through ES
62                    filename: unsafe { new_path.filename.as_os_str() },
63                })
64            },
65            _ => None,
66        }
67    }
68}
69
70// Safety: safe to send across threads: does not contain any interior mutability nor depend on current thread state
71unsafe impl Send for EventRename<'_> {}
72// Safety: safe to share across threads: does not contain any interior mutability nor depend on current thread state
73unsafe impl Sync for EventRename<'_> {}
74
75impl_debug_eq_hash_with_functions!(EventRename<'a>; source, destination);