Skip to content

Commit e56b5fe

Browse files
sdroegebilelmoussaoui
authored andcommitted
gio: Use correct types for UNIX-specific Credentials API
And also bind native setter/getter. See https://gitlab.gnome.org/GNOME/glib/-/issues/3311 for why this is necessary.
1 parent 7c63428 commit e56b5fe

File tree

4 files changed

+82
-59
lines changed

4 files changed

+82
-59
lines changed

gio/Gir.toml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,14 +438,27 @@ status = "generate"
438438
[[object.function]]
439439
name = "get_unix_pid"
440440
cfg_condition = "unix"
441+
# Use correct libc type
442+
# https://gitlab.gnome.org/GNOME/glib/-/issues/3311
443+
manual = true
441444
[[object.function]]
442445
name = "get_unix_user"
443446
cfg_condition = "unix"
447+
# Use correct libc type
448+
# https://gitlab.gnome.org/GNOME/glib/-/issues/3311
449+
manual = true
444450
[object.function.return]
445451
use_return_for_result = true
446452
[[object.function]]
447453
name = "set_unix_user"
448454
cfg_condition = "unix"
455+
# Use correct libc type
456+
# https://gitlab.gnome.org/GNOME/glib/-/issues/3311
457+
manual = true
458+
[[object.function]]
459+
pattern = "[gs]et_native"
460+
# Unsafe and using raw pointers
461+
manual = true
449462

450463
[[object]]
451464
name = "Gio.CredentialsType"

gio/src/auto/credentials.rs

Lines changed: 0 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -19,44 +19,6 @@ impl Credentials {
1919
unsafe { from_glib_full(ffi::g_credentials_new()) }
2020
}
2121

22-
//#[doc(alias = "g_credentials_get_native")]
23-
//#[doc(alias = "get_native")]
24-
//pub fn native(&self, native_type: CredentialsType) -> /*Unimplemented*/Option<Basic: Pointer> {
25-
// unsafe { TODO: call ffi:g_credentials_get_native() }
26-
//}
27-
28-
#[cfg(unix)]
29-
#[cfg_attr(docsrs, doc(cfg(unix)))]
30-
#[doc(alias = "g_credentials_get_unix_pid")]
31-
#[doc(alias = "get_unix_pid")]
32-
pub fn unix_pid(&self) -> Result<i32, glib::Error> {
33-
unsafe {
34-
let mut error = std::ptr::null_mut();
35-
let ret = ffi::g_credentials_get_unix_pid(self.to_glib_none().0, &mut error);
36-
if error.is_null() {
37-
Ok(ret)
38-
} else {
39-
Err(from_glib_full(error))
40-
}
41-
}
42-
}
43-
44-
#[cfg(unix)]
45-
#[cfg_attr(docsrs, doc(cfg(unix)))]
46-
#[doc(alias = "g_credentials_get_unix_user")]
47-
#[doc(alias = "get_unix_user")]
48-
pub fn unix_user(&self) -> Result<u32, glib::Error> {
49-
unsafe {
50-
let mut error = std::ptr::null_mut();
51-
let ret = ffi::g_credentials_get_unix_user(self.to_glib_none().0, &mut error);
52-
if error.is_null() {
53-
Ok(ret)
54-
} else {
55-
Err(from_glib_full(error))
56-
}
57-
}
58-
}
59-
6022
#[doc(alias = "g_credentials_is_same_user")]
6123
pub fn is_same_user(&self, other_credentials: &Credentials) -> Result<(), glib::Error> {
6224
unsafe {
@@ -75,27 +37,6 @@ impl Credentials {
7537
}
7638
}
7739

78-
//#[doc(alias = "g_credentials_set_native")]
79-
//pub fn set_native(&self, native_type: CredentialsType, native: /*Unimplemented*/Basic: Pointer) {
80-
// unsafe { TODO: call ffi:g_credentials_set_native() }
81-
//}
82-
83-
#[cfg(unix)]
84-
#[cfg_attr(docsrs, doc(cfg(unix)))]
85-
#[doc(alias = "g_credentials_set_unix_user")]
86-
pub fn set_unix_user(&self, uid: u32) -> Result<(), glib::Error> {
87-
unsafe {
88-
let mut error = std::ptr::null_mut();
89-
let is_ok = ffi::g_credentials_set_unix_user(self.to_glib_none().0, uid, &mut error);
90-
debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
91-
if error.is_null() {
92-
Ok(())
93-
} else {
94-
Err(from_glib_full(error))
95-
}
96-
}
97-
}
98-
9940
#[doc(alias = "g_credentials_to_string")]
10041
#[doc(alias = "to_string")]
10142
pub fn to_str(&self) -> glib::GString {

gio/src/credentials.rs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Take a look at the license at the top of the repository in the LICENSE file.
2+
3+
use glib::translate::*;
4+
5+
use crate::{Credentials, CredentialsType};
6+
7+
impl Credentials {
8+
#[doc(alias = "g_credentials_get_native")]
9+
#[doc(alias = "get_native")]
10+
pub fn native(&self, native_type: CredentialsType) -> glib::ffi::gpointer {
11+
unsafe { ffi::g_credentials_get_native(self.to_glib_none().0, native_type.into_glib()) }
12+
}
13+
14+
#[cfg(unix)]
15+
#[cfg_attr(docsrs, doc(cfg(unix)))]
16+
#[doc(alias = "g_credentials_get_unix_pid")]
17+
#[doc(alias = "get_unix_pid")]
18+
pub fn unix_pid(&self) -> Result<libc::pid_t, glib::Error> {
19+
unsafe {
20+
let mut error = std::ptr::null_mut();
21+
let ret = ffi::g_credentials_get_unix_pid(self.to_glib_none().0, &mut error);
22+
if error.is_null() {
23+
Ok(ret)
24+
} else {
25+
Err(from_glib_full(error))
26+
}
27+
}
28+
}
29+
30+
#[cfg(unix)]
31+
#[cfg_attr(docsrs, doc(cfg(unix)))]
32+
#[doc(alias = "g_credentials_get_unix_user")]
33+
#[doc(alias = "get_unix_user")]
34+
pub fn unix_user(&self) -> Result<libc::uid_t, glib::Error> {
35+
unsafe {
36+
let mut error = std::ptr::null_mut();
37+
let ret = ffi::g_credentials_get_unix_user(self.to_glib_none().0, &mut error);
38+
if error.is_null() {
39+
Ok(ret)
40+
} else {
41+
Err(from_glib_full(error))
42+
}
43+
}
44+
}
45+
46+
#[doc(alias = "g_credentials_set_native")]
47+
pub unsafe fn set_native(&self, native_type: CredentialsType, native: glib::ffi::gpointer) {
48+
unsafe {
49+
ffi::g_credentials_set_native(self.to_glib_none().0, native_type.into_glib(), native)
50+
}
51+
}
52+
53+
#[cfg(unix)]
54+
#[cfg_attr(docsrs, doc(cfg(unix)))]
55+
#[doc(alias = "g_credentials_set_unix_user")]
56+
pub fn set_unix_user(&self, uid: libc::uid_t) -> Result<(), glib::Error> {
57+
unsafe {
58+
let mut error = std::ptr::null_mut();
59+
let is_ok = ffi::g_credentials_set_unix_user(self.to_glib_none().0, uid, &mut error);
60+
debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
61+
if error.is_null() {
62+
Ok(())
63+
} else {
64+
Err(from_glib_full(error))
65+
}
66+
}
67+
}
68+
}

gio/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ mod cancellable;
2121
mod cancellable_future;
2222
pub use crate::cancellable_future::{CancellableFuture, Cancelled};
2323
mod converter;
24+
mod credentials;
2425
mod data_input_stream;
2526
mod datagram_based;
2627
mod dbus;

0 commit comments

Comments
 (0)