Skip to main content

gio/auto/
credentials.rs

1// This file was generated by gir (https://github.com/gtk-rs/gir)
2// from gir-files (https://github.com/gtk-rs/gir-files)
3// DO NOT EDIT
4
5use crate::ffi;
6use glib::translate::*;
7
8glib::wrapper! {
9    /// The `GCredentials` type is a reference-counted wrapper for native
10    /// credentials.
11    ///
12    /// The information in `GCredentials` is typically used for identifying,
13    /// authenticating and authorizing other processes.
14    ///
15    /// Some operating systems supports looking up the credentials of the remote
16    /// peer of a communication endpoint - see e.g. [`SocketExt::credentials()`][crate::prelude::SocketExt::credentials()].
17    ///
18    /// Some operating systems supports securely sending and receiving
19    /// credentials over a Unix Domain Socket, see [`UnixCredentialsMessage`][crate::UnixCredentialsMessage],
20    /// [`UnixConnectionExt::send_credentials()`][crate::prelude::UnixConnectionExt::send_credentials()] and
21    /// [`UnixConnectionExt::receive_credentials()`][crate::prelude::UnixConnectionExt::receive_credentials()] for details.
22    ///
23    /// On Linux, the native credential type is a `struct ucred` - see the
24    /// [`unix(7)` man page](man:unix(7)) for details. This corresponds to
25    /// `G_CREDENTIALS_TYPE_LINUX_UCRED`.
26    ///
27    /// On Apple operating systems (including iOS, tvOS, and macOS), the native credential
28    /// type is a `struct xucred`. This corresponds to `G_CREDENTIALS_TYPE_APPLE_XUCRED`.
29    ///
30    /// On FreeBSD, Debian GNU/kFreeBSD, and GNU/Hurd, the native credential type is a
31    /// `struct cmsgcred`. This corresponds to `G_CREDENTIALS_TYPE_FREEBSD_CMSGCRED`.
32    ///
33    /// On NetBSD, the native credential type is a `struct unpcbid`.
34    /// This corresponds to `G_CREDENTIALS_TYPE_NETBSD_UNPCBID`.
35    ///
36    /// On OpenBSD, the native credential type is a `struct sockpeercred`.
37    /// This corresponds to `G_CREDENTIALS_TYPE_OPENBSD_SOCKPEERCRED`.
38    ///
39    /// On Solaris (including OpenSolaris and its derivatives), the native credential type
40    /// is a `ucred_t`. This corresponds to `G_CREDENTIALS_TYPE_SOLARIS_UCRED`.
41    ///
42    /// Since GLib 2.72, on Windows, the native credentials may contain the PID of a
43    /// process. This corresponds to `G_CREDENTIALS_TYPE_WIN32_PID`.
44    ///
45    /// Since GLib 2.92, on Windows, [`is_same_user()`][Self::is_same_user()] looks up
46    /// the user who owns each of the processes in the credentials, and compares the
47    /// two.  The lookup is made when the method is called.  A process that is gone,
48    /// or that cannot be inspected (as it is more privileged than the caller), is
49    /// not the same user as anyone.
50    ///
51    /// # Implements
52    ///
53    /// [`trait@glib::ObjectExt`]
54    #[doc(alias = "GCredentials")]
55    pub struct Credentials(Object<ffi::GCredentials, ffi::GCredentialsClass>);
56
57    match fn {
58        type_ => || ffi::g_credentials_get_type(),
59    }
60}
61
62impl Credentials {
63    /// Creates a new #GCredentials object with credentials matching the
64    /// the current process.
65    ///
66    /// # Returns
67    ///
68    /// A #GCredentials. Free with g_object_unref().
69    #[doc(alias = "g_credentials_new")]
70    pub fn new() -> Credentials {
71        unsafe { from_glib_full(ffi::g_credentials_new()) }
72    }
73
74    /// Checks if @self and @other_credentials is the same user.
75    ///
76    /// This operation can fail if #GCredentials is not supported on the
77    /// the OS.
78    ///
79    /// On Windows, since 2.92, the owners of the two processes are compared.  If
80    /// one of the processes cannot be inspected, the result is [`false`] and @error
81    /// is not set.
82    /// ## `other_credentials`
83    /// A #GCredentials.
84    ///
85    /// # Returns
86    ///
87    /// [`true`] if @self and @other_credentials has the same
88    /// user, [`false`] otherwise or if @error is set.
89    #[doc(alias = "g_credentials_is_same_user")]
90    pub fn is_same_user(&self, other_credentials: &Credentials) -> Result<(), glib::Error> {
91        unsafe {
92            let mut error = std::ptr::null_mut();
93            let is_ok = ffi::g_credentials_is_same_user(
94                self.to_glib_none().0,
95                other_credentials.to_glib_none().0,
96                &mut error,
97            );
98            debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
99            if error.is_null() {
100                Ok(())
101            } else {
102                Err(from_glib_full(error))
103            }
104        }
105    }
106
107    /// Creates a human-readable textual representation of @self
108    /// that can be used in logging and debug messages. The format of the
109    /// returned string may change in future GLib release.
110    ///
111    /// # Returns
112    ///
113    /// A string that should be freed with g_free().
114    #[doc(alias = "g_credentials_to_string")]
115    #[doc(alias = "to_string")]
116    pub fn to_str(&self) -> glib::GString {
117        unsafe { from_glib_full(ffi::g_credentials_to_string(self.to_glib_none().0)) }
118    }
119}
120
121impl Default for Credentials {
122    fn default() -> Self {
123        Self::new()
124    }
125}
126
127impl std::fmt::Display for Credentials {
128    #[inline]
129    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
130        f.write_str(&self.to_str())
131    }
132}