1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Take a look at the license at the top of the repository in the LICENSE file.

#[cfg(unix)]
use std::os::unix::io::{AsRawFd, IntoRawFd, RawFd};
use std::{mem, ptr};

use glib::{prelude::*, translate::*};
#[cfg(all(not(unix), docsrs))]
use socket::{AsRawFd, IntoRawFd, RawFd};

use crate::UnixFDList;

impl UnixFDList {
    /// Creates a new #GUnixFDList containing the file descriptors given in
    /// @fds.  The file descriptors become the property of the new list and
    /// may no longer be used by the caller.  The array itself is owned by
    /// the caller.
    ///
    /// Each file descriptor in the array should be set to close-on-exec.
    ///
    /// If @n_fds is -1 then @fds must be terminated with -1.
    /// ## `fds`
    /// the initial list of file descriptors
    ///
    /// # Returns
    ///
    /// a new #GUnixFDList
    #[doc(alias = "g_unix_fd_list_new_from_array")]
    pub fn from_array(fds: impl IntoIterator<Item = impl IntoRawFd>) -> UnixFDList {
        let fds = fds.into_iter().map(|t| t.into_raw_fd()).collect::<Vec<_>>();
        unsafe {
            from_glib_full(ffi::g_unix_fd_list_new_from_array(
                fds.to_glib_none().0,
                fds.len() as i32,
            ))
        }
    }
}

mod sealed {
    pub trait Sealed {}
    impl<T: super::IsA<super::UnixFDList>> Sealed for T {}
}

pub trait UnixFDListExtManual: sealed::Sealed + IsA<UnixFDList> + Sized {
    /// Adds a file descriptor to @self.
    ///
    /// The file descriptor is duplicated using dup(). You keep your copy
    /// of the descriptor and the copy contained in @self will be closed
    /// when @self is finalized.
    ///
    /// A possible cause of failure is exceeding the per-process or
    /// system-wide file descriptor limit.
    ///
    /// The index of the file descriptor in the list is returned.  If you use
    /// this index with g_unix_fd_list_get() then you will receive back a
    /// duplicated copy of the same file descriptor.
    /// ## `fd`
    /// a valid open file descriptor
    ///
    /// # Returns
    ///
    /// the index of the appended fd in case of success, else -1
    ///          (and @error is set)
    #[doc(alias = "g_unix_fd_list_append")]
    fn append<T: AsRawFd>(&self, fd: T) -> Result<i32, glib::Error> {
        unsafe {
            let mut error = ptr::null_mut();
            let ret = ffi::g_unix_fd_list_append(
                self.as_ref().to_glib_none().0,
                fd.as_raw_fd(),
                &mut error,
            );
            if error.is_null() {
                Ok(ret)
            } else {
                Err(from_glib_full(error))
            }
        }
    }

    /// Gets a file descriptor out of @self.
    ///
    /// @index_ specifies the index of the file descriptor to get.  It is a
    /// programmer error for @index_ to be out of range; see
    /// g_unix_fd_list_get_length().
    ///
    /// The file descriptor is duplicated using dup() and set as
    /// close-on-exec before being returned.  You must call close() on it
    /// when you are done.
    ///
    /// A possible cause of failure is exceeding the per-process or
    /// system-wide file descriptor limit.
    /// ## `index_`
    /// the index into the list
    ///
    /// # Returns
    ///
    /// the file descriptor, or -1 in case of error
    #[doc(alias = "g_unix_fd_list_get")]
    fn get(&self, index_: i32) -> Result<RawFd, glib::Error> {
        unsafe {
            let mut error = ptr::null_mut();
            let ret = ffi::g_unix_fd_list_get(self.as_ref().to_glib_none().0, index_, &mut error);
            if error.is_null() {
                Ok(ret)
            } else {
                Err(from_glib_full(error))
            }
        }
    }

    #[doc(alias = "g_unix_fd_list_peek_fds")]

    /// Returns the array of file descriptors that is contained in this
    /// object.
    ///
    /// After this call, the descriptors remain the property of @self.  The
    /// caller must not close them and must not free the array.  The array is
    /// valid only until @self is changed in any way.
    ///
    /// If @length is non-[`None`] then it is set to the number of file
    /// descriptors in the returned array. The returned array is also
    /// terminated with -1.
    ///
    /// This function never returns [`None`]. In case there are no file
    /// descriptors contained in @self, an empty array is returned.
    ///
    /// # Returns
    ///
    /// an array of file
    ///     descriptors
    fn peek_fds(&self) -> Vec<RawFd> {
        unsafe {
            let mut length = mem::MaybeUninit::uninit();
            let ret = FromGlibContainer::from_glib_none_num(
                ffi::g_unix_fd_list_peek_fds(self.as_ref().to_glib_none().0, length.as_mut_ptr()),
                length.assume_init() as usize,
            );
            ret
        }
    }
    /// Returns the array of file descriptors that is contained in this
    /// object.
    ///
    /// After this call, the descriptors are no longer contained in
    /// @self. Further calls will return an empty list (unless more
    /// descriptors have been added).
    ///
    /// The return result of this function must be freed with g_free().
    /// The caller is also responsible for closing all of the file
    /// descriptors.  The file descriptors in the array are set to
    /// close-on-exec.
    ///
    /// If @length is non-[`None`] then it is set to the number of file
    /// descriptors in the returned array. The returned array is also
    /// terminated with -1.
    ///
    /// This function never returns [`None`]. In case there are no file
    /// descriptors contained in @self, an empty array is returned.
    ///
    /// # Returns
    ///
    /// an array of file
    ///     descriptors
    #[doc(alias = "g_unix_fd_list_steal_fds")]
    fn steal_fds(&self) -> Vec<RawFd> {
        unsafe {
            let mut length = mem::MaybeUninit::uninit();
            let ret = FromGlibContainer::from_glib_full_num(
                ffi::g_unix_fd_list_steal_fds(self.as_ref().to_glib_none().0, length.as_mut_ptr()),
                length.assume_init() as usize,
            );
            ret
        }
    }
}

impl<O: IsA<UnixFDList>> UnixFDListExtManual for O {}