Skip to main content

endpoint_sec/event/
event_copyfile.rs

1//! [`EventCopyFile`]
2
3use std::ffi::OsStr;
4
5use endpoint_sec_sys::{es_event_copyfile_t, mode_t};
6
7use crate::File;
8
9/// Copy a file using the `copyfile()` system call.
10///
11/// Note: Not to be confused with `copyfile(3)`.
12///
13/// Note: Prior to macOS 12.0, the copyfile syscall fired open, unlink and auth create events,
14/// but no notify create, nor write or close events.
15#[doc(alias = "es_event_copyfile_t")]
16pub struct EventCopyFile<'a> {
17    /// Raw event
18    pub(crate) raw: &'a es_event_copyfile_t,
19}
20
21impl<'a> EventCopyFile<'a> {
22    /// The file that will be copied.
23    #[inline(always)]
24    pub fn source(&self) -> File<'a> {
25        // Safety: 'a tied to self, object obtained through ES
26        File::new(unsafe { self.raw.source() })
27    }
28
29    /// The file that will be overwritten by the operation, if any.
30    #[inline(always)]
31    pub fn target_file(&self) -> Option<File<'a>> {
32        // Safety: 'a tied to self, object obtained through ES
33        unsafe { self.raw.target_file() }.map(File::new)
34    }
35
36    /// The directory into which the [`Self::source()`] file will be copied.
37    #[inline(always)]
38    pub fn target_dir(&self) -> File<'a> {
39        // Safety: 'a tied to self, object obtained through ES
40        File::new(unsafe { self.raw.target_dir() })
41    }
42
43    /// Name of the new file to which [`Self::source()`] will be copied.
44    #[inline(always)]
45    pub fn target_name(&self) -> &'a OsStr {
46        // Safety: 'a tied to self, object obtained through ES
47        unsafe { self.raw.target_name.as_os_str() }
48    }
49
50    /// Corresponds to mode argument of the `copyfile()` syscall.
51    #[inline(always)]
52    pub fn mode(&self) -> mode_t {
53        self.raw.mode
54    }
55
56    /// Corresponds to flags argument of the `copyfile()` syscall.
57    #[inline(always)]
58    pub fn flags(&self) -> i32 {
59        self.raw.flags
60    }
61}
62
63// Safety: safe to send across threads: does not contain any interior mutability nor depend on current thread state
64unsafe impl Send for EventCopyFile<'_> {}
65// Safety: safe to share across threads: does not contain any interior mutability nor depend on current thread state
66unsafe impl Sync for EventCopyFile<'_> {}
67
68impl_debug_eq_hash_with_functions!(EventCopyFile<'a>; source, target_file, target_dir, target_name, mode, flags);