use libc::{__s32, __u16, __u32, __u64, __u8, c_int};
use std::ffi::OsStr;
use std::os::fd::RawFd;
#[allow(unused_imports)]
use crate::api::*;
#[allow(unused_imports)]
use crate::flags::*;
#[derive(Debug, Clone)]
pub struct Fd {
inner: RawFd,
}
impl Fd {
#[inline]
pub fn new(fd: RawFd) -> Self {
Self { inner: fd }
}
#[inline]
pub fn path(&self) -> Result<std::path::PathBuf, std::io::Error> {
std::fs::read_link(format!("/proc/self/fd/{}", self.inner))
}
#[inline]
pub fn path_from_rawfd(fd: RawFd) -> Result<std::path::PathBuf, std::io::Error> {
std::fs::read_link(format!("/proc/self/fd/{}", fd))
}
#[inline]
pub fn is_valid(&self) -> bool {
self.inner >= 0
}
}
impl Drop for Fd {
fn drop(&mut self) {
if let Err(e) = close(self.inner) {
eprintln!("(fd dropper)Fd :: {}\nErr :: {}", self.inner, e);
}
}
}
impl From<RawFd> for Fd {
fn from(fd: RawFd) -> Self {
Self { inner: fd }
}
}
impl From<&Fd> for RawFd {
fn from(val: &Fd) -> Self {
val.inner
}
}
impl From<Fd> for RawFd {
fn from(fd: Fd) -> Self {
fd.inner
}
}
#[derive(Debug, Clone)]
#[repr(C)]
pub struct fanotify_event_metadata {
pub event_len: __u32,
pub vers: __u8,
pub reserved: __u8,
pub metadata_len: __u16,
pub mask: __u64,
pub fd: __s32,
pub pid: __s32,
}
impl Drop for fanotify_event_metadata {
fn drop(&mut self) {
if self.fd >= 0 {
if let Err(e) = close(self.fd) {
eprintln!("Fd :: {}\nErr :: {}", self.fd, e);
}
}
}
}
#[derive(Debug, Clone, Copy)]
#[allow(non_camel_case_types)]
struct __kernel_fsid_t {
#[allow(dead_code)]
val: [c_int; 2],
}
#[allow(non_camel_case_types)]
#[derive(Debug, Clone)]
#[repr(C)]
pub struct fanotify_event_info_header {
pub info_type: __u8,
pub pad: __u8,
pub len: __u16,
}
#[allow(non_camel_case_types)]
#[derive(Debug, Clone)]
#[repr(C)]
pub struct fanotify_event_info_fid {
hdr: fanotify_event_info_header,
fsid: __kernel_fsid_t,
file_handle: __u8,
}
#[allow(non_camel_case_types)]
#[derive(Debug, Clone)]
#[repr(C)]
pub struct fanotify_event_with_fid {
pub metadata: fanotify_event_metadata,
pub fid: fanotify_event_info_fid,
}
#[allow(non_camel_case_types)]
#[derive(Debug)]
#[repr(C)]
pub struct fanotify_response {
pub fd: __s32,
pub response: __u32,
}
impl fanotify_response {
pub fn new(fd: &Fd, response: __u32) -> Self {
fanotify_response {
fd: fd.inner,
response,
}
}
}
pub trait Path {
fn as_os_str(&self) -> &OsStr;
}
impl Path for std::path::Path {
fn as_os_str(&self) -> &OsStr {
self.as_os_str()
}
}
impl Path for str {
fn as_os_str(&self) -> &OsStr {
OsStr::new(self)
}
}
impl Path for String {
fn as_os_str(&self) -> &OsStr {
OsStr::new(self.as_str())
}
}