Skip to main content

gio/auto/
subprocess_launcher.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::{Subprocess, SubprocessFlags, ffi};
6use glib::translate::*;
7#[cfg(unix)]
8#[cfg_attr(docsrs, doc(cfg(unix)))]
9use std::boxed::Box as Box_;
10
11glib::wrapper! {
12    /// This class contains a set of options for launching child processes,
13    /// such as where its standard input and output will be directed, the
14    /// argument list, the environment, and more.
15    ///
16    /// While the [`Subprocess`][crate::Subprocess] class has high level functions covering
17    /// popular cases, use of this class allows access to more advanced
18    /// options.  It can also be used to launch multiple subprocesses with
19    /// a similar configuration.
20    ///
21    /// ## Properties
22    ///
23    ///
24    /// #### `flags`
25    ///  [`SubprocessFlags`][crate::SubprocessFlags] for launched processes.
26    ///
27    /// Writable | Construct Only
28    ///
29    /// # Implements
30    ///
31    /// [`trait@glib::ObjectExt`]
32    #[doc(alias = "GSubprocessLauncher")]
33    pub struct SubprocessLauncher(Object<ffi::GSubprocessLauncher>);
34
35    match fn {
36        type_ => || ffi::g_subprocess_launcher_get_type(),
37    }
38}
39
40impl SubprocessLauncher {
41    /// Creates a new #GSubprocessLauncher.
42    ///
43    /// The launcher is created with the default options.  A copy of the
44    /// environment of the calling process is made at the time of this call
45    /// and will be used as the environment that the process is launched in.
46    /// ## `flags`
47    /// #GSubprocessFlags
48    #[doc(alias = "g_subprocess_launcher_new")]
49    pub fn new(flags: SubprocessFlags) -> SubprocessLauncher {
50        unsafe { from_glib_full(ffi::g_subprocess_launcher_new(flags.into_glib())) }
51    }
52
53    /// Closes all the file descriptors previously passed to the object with
54    /// g_subprocess_launcher_take_fd(), g_subprocess_launcher_take_stderr_fd(), etc.
55    ///
56    /// After calling this method, any subsequent calls to g_subprocess_launcher_spawn() or g_subprocess_launcher_spawnv() will
57    /// return [`IOErrorEnum::Closed`][crate::IOErrorEnum::Closed]. This method is idempotent if
58    /// called more than once.
59    ///
60    /// This function is called automatically when the #GSubprocessLauncher
61    /// is disposed, but is provided separately so that garbage collected
62    /// language bindings can call it earlier to guarantee when FDs are closed.
63    #[cfg(unix)]
64    #[cfg_attr(docsrs, doc(cfg(unix)))]
65    #[cfg(feature = "v2_68")]
66    #[cfg_attr(docsrs, doc(cfg(feature = "v2_68")))]
67    #[doc(alias = "g_subprocess_launcher_close")]
68    pub fn close(&self) {
69        unsafe {
70            ffi::g_subprocess_launcher_close(self.to_glib_none().0);
71        }
72    }
73
74    /// Returns the value of the environment variable @variable in the
75    /// environment of processes launched from this launcher.
76    ///
77    /// On UNIX, the returned string can be an arbitrary byte string.
78    /// On Windows, it will be UTF-8.
79    /// ## `variable`
80    /// the environment variable to get
81    ///
82    /// # Returns
83    ///
84    /// the value of the environment variable,
85    ///     [`None`] if unset
86    #[doc(alias = "g_subprocess_launcher_getenv")]
87    pub fn getenv(&self, variable: impl AsRef<std::path::Path>) -> Option<std::path::PathBuf> {
88        unsafe {
89            from_glib_none(ffi::g_subprocess_launcher_getenv(
90                self.to_glib_none().0,
91                variable.as_ref().to_glib_none().0,
92            ))
93        }
94    }
95
96    /// Sets up a child setup function.
97    ///
98    /// The child setup function will be called after fork() but before
99    /// exec() on the child's side.
100    ///
101    /// @destroy_notify will not be automatically called on the child's side
102    /// of the fork().  It will only be called when the last reference on the
103    /// #GSubprocessLauncher is dropped or when a new child setup function is
104    /// given.
105    ///
106    /// [`None`] can be given as @child_setup to disable the functionality.
107    ///
108    /// Child setup functions are only available on UNIX.
109    /// ## `child_setup`
110    /// a #GSpawnChildSetupFunc to use as the child setup function
111    /// ## `destroy_notify`
112    /// a #GDestroyNotify for @user_data
113    #[cfg(unix)]
114    #[cfg_attr(docsrs, doc(cfg(unix)))]
115    #[doc(alias = "g_subprocess_launcher_set_child_setup")]
116    pub fn set_child_setup<P: Fn() + 'static>(&self, child_setup: P) {
117        let child_setup_data: Box_<P> = Box_::new(child_setup);
118        unsafe extern "C" fn child_setup_func<P: Fn() + 'static>(data: glib::ffi::gpointer) {
119            unsafe {
120                let callback = &*(data as *mut P);
121                (*callback)()
122            }
123        }
124        let child_setup = Some(child_setup_func::<P> as _);
125        unsafe extern "C" fn destroy_notify_func<P: Fn() + 'static>(data: glib::ffi::gpointer) {
126            unsafe {
127                let _callback = Box_::from_raw(data as *mut P);
128            }
129        }
130        let destroy_call3 = Some(destroy_notify_func::<P> as _);
131        let super_callback0: Box_<P> = child_setup_data;
132        unsafe {
133            ffi::g_subprocess_launcher_set_child_setup(
134                self.to_glib_none().0,
135                child_setup,
136                Box_::into_raw(super_callback0) as *mut _,
137                destroy_call3,
138            );
139        }
140    }
141
142    /// Sets the current working directory that processes will be launched
143    /// with.
144    ///
145    /// By default processes are launched with the current working directory
146    /// of the launching process at the time of launch.
147    /// ## `cwd`
148    /// the cwd for launched processes
149    #[doc(alias = "g_subprocess_launcher_set_cwd")]
150    pub fn set_cwd(&self, cwd: impl AsRef<std::path::Path>) {
151        unsafe {
152            ffi::g_subprocess_launcher_set_cwd(
153                self.to_glib_none().0,
154                cwd.as_ref().to_glib_none().0,
155            );
156        }
157    }
158
159    /// Sets the flags on the launcher.
160    ///
161    /// The default flags are [`SubprocessFlags::NONE`][crate::SubprocessFlags::NONE].
162    ///
163    /// You may not set flags that specify conflicting options for how to
164    /// handle a particular stdio stream (eg: specifying both
165    /// [`SubprocessFlags::STDIN_PIPE`][crate::SubprocessFlags::STDIN_PIPE] and
166    /// [`SubprocessFlags::STDIN_INHERIT`][crate::SubprocessFlags::STDIN_INHERIT]).
167    ///
168    /// You may also not set a flag that conflicts with a previous call to a
169    /// function like g_subprocess_launcher_set_stdin_file_path() or
170    /// g_subprocess_launcher_take_stdout_fd().
171    /// ## `flags`
172    /// #GSubprocessFlags
173    #[doc(alias = "g_subprocess_launcher_set_flags")]
174    pub fn set_flags(&self, flags: SubprocessFlags) {
175        unsafe {
176            ffi::g_subprocess_launcher_set_flags(self.to_glib_none().0, flags.into_glib());
177        }
178    }
179
180    /// ' at the shell.
181    ///
182    /// If you want to send both stdout and stderr to the same file then use
183    /// [`SubprocessFlags::STDERR_MERGE`][crate::SubprocessFlags::STDERR_MERGE].
184    ///
185    /// You may not set a stderr file path if a stderr fd is already set or
186    /// if the launcher flags contain any flags directing stderr elsewhere.
187    ///
188    /// This feature is only available on UNIX.
189    /// ## `path`
190    /// a filename or [`None`]
191    #[cfg(unix)]
192    #[cfg_attr(docsrs, doc(cfg(unix)))]
193    #[doc(alias = "g_subprocess_launcher_set_stderr_file_path")]
194    pub fn set_stderr_file_path(&self, path: Option<impl AsRef<std::path::Path>>) {
195        unsafe {
196            ffi::g_subprocess_launcher_set_stderr_file_path(
197                self.to_glib_none().0,
198                path.as_ref().map(|p| p.as_ref()).to_glib_none().0,
199            );
200        }
201    }
202
203    /// Sets the file path to use as the stdin for spawned processes.
204    ///
205    /// If @path is [`None`] then any previously given path is unset.
206    ///
207    /// The file must exist or spawning the process will fail.
208    ///
209    /// You may not set a stdin file path if a stdin fd is already set or if
210    /// the launcher flags contain any flags directing stdin elsewhere.
211    ///
212    /// This feature is only available on UNIX.
213    /// ## `path`
214    /// a filename or [`None`]
215    #[cfg(unix)]
216    #[cfg_attr(docsrs, doc(cfg(unix)))]
217    #[doc(alias = "g_subprocess_launcher_set_stdin_file_path")]
218    pub fn set_stdin_file_path(&self, path: Option<impl AsRef<std::path::Path>>) {
219        unsafe {
220            ffi::g_subprocess_launcher_set_stdin_file_path(
221                self.to_glib_none().0,
222                path.as_ref().map(|p| p.as_ref()).to_glib_none().0,
223            );
224        }
225    }
226
227    /// ' at the shell.
228    ///
229    /// You may not set a stdout file path if a stdout fd is already set or
230    /// if the launcher flags contain any flags directing stdout elsewhere.
231    ///
232    /// This feature is only available on UNIX.
233    /// ## `path`
234    /// a filename or [`None`]
235    #[cfg(unix)]
236    #[cfg_attr(docsrs, doc(cfg(unix)))]
237    #[doc(alias = "g_subprocess_launcher_set_stdout_file_path")]
238    pub fn set_stdout_file_path(&self, path: Option<impl AsRef<std::path::Path>>) {
239        unsafe {
240            ffi::g_subprocess_launcher_set_stdout_file_path(
241                self.to_glib_none().0,
242                path.as_ref().map(|p| p.as_ref()).to_glib_none().0,
243            );
244        }
245    }
246
247    /// Sets the environment variable @variable in the environment of
248    /// processes launched from this launcher.
249    ///
250    /// On UNIX, both the variable's name and value can be arbitrary byte
251    /// strings, except that the variable's name cannot contain '='.
252    /// On Windows, they should be in UTF-8.
253    /// ## `variable`
254    /// the environment variable to set,
255    ///     must not contain '='
256    /// ## `value`
257    /// the new value for the variable
258    /// ## `overwrite`
259    /// whether to change the variable if it already exists
260    #[doc(alias = "g_subprocess_launcher_setenv")]
261    pub fn setenv(
262        &self,
263        variable: impl AsRef<std::ffi::OsStr>,
264        value: impl AsRef<std::ffi::OsStr>,
265        overwrite: bool,
266    ) {
267        unsafe {
268            ffi::g_subprocess_launcher_setenv(
269                self.to_glib_none().0,
270                variable.as_ref().to_glib_none().0,
271                value.as_ref().to_glib_none().0,
272                overwrite.into_glib(),
273            );
274        }
275    }
276
277    //#[doc(alias = "g_subprocess_launcher_spawn")]
278    //pub fn spawn(&self, error: &mut glib::Error, argv0: &str, : /*Unknown conversion*//*Unimplemented*/Basic: VarArgs) -> Subprocess {
279    //    unsafe { TODO: call ffi:g_subprocess_launcher_spawn() }
280    //}
281
282    /// Creates a #GSubprocess given a provided varargs list of arguments.
283    /// ## `error`
284    /// Error
285    /// ## `argv0`
286    /// Command line arguments
287    ///
288    /// # Returns
289    ///
290    /// A new #GSubprocess, or [`None`] on error (and @error will be set)
291    #[doc(alias = "g_subprocess_launcher_spawnv")]
292    #[doc(alias = "spawnv")]
293    pub fn spawn(&self, argv: &[&std::ffi::OsStr]) -> Result<Subprocess, glib::Error> {
294        unsafe {
295            let mut error = std::ptr::null_mut();
296            let ret = ffi::g_subprocess_launcher_spawnv(
297                self.to_glib_none().0,
298                argv.to_glib_none().0,
299                &mut error,
300            );
301            if error.is_null() {
302                Ok(from_glib_full(ret))
303            } else {
304                Err(from_glib_full(error))
305            }
306        }
307    }
308
309    /// Removes the environment variable @variable from the environment of
310    /// processes launched from this launcher.
311    ///
312    /// On UNIX, the variable's name can be an arbitrary byte string not
313    /// containing '='. On Windows, it should be in UTF-8.
314    /// ## `variable`
315    /// the environment variable to unset,
316    ///     must not contain '='
317    #[doc(alias = "g_subprocess_launcher_unsetenv")]
318    pub fn unsetenv(&self, variable: impl AsRef<std::ffi::OsStr>) {
319        unsafe {
320            ffi::g_subprocess_launcher_unsetenv(
321                self.to_glib_none().0,
322                variable.as_ref().to_glib_none().0,
323            );
324        }
325    }
326}