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 /// Writeable | 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 /// Sets the file path to use as the stderr for spawned processes.
181 ///
182 /// If @path is [`None`] then any previously given path is unset.
183 ///
184 /// The file will be created or truncated when the process is spawned, as
185 /// would be the case if using '2>' at the shell.
186 ///
187 /// If you want to send both stdout and stderr to the same file then use
188 /// [`SubprocessFlags::STDERR_MERGE`][crate::SubprocessFlags::STDERR_MERGE].
189 ///
190 /// You may not set a stderr file path if a stderr fd is already set or
191 /// if the launcher flags contain any flags directing stderr elsewhere.
192 ///
193 /// This feature is only available on UNIX.
194 /// ## `path`
195 /// a filename or [`None`]
196 #[cfg(unix)]
197 #[cfg_attr(docsrs, doc(cfg(unix)))]
198 #[doc(alias = "g_subprocess_launcher_set_stderr_file_path")]
199 pub fn set_stderr_file_path(&self, path: Option<impl AsRef<std::path::Path>>) {
200 unsafe {
201 ffi::g_subprocess_launcher_set_stderr_file_path(
202 self.to_glib_none().0,
203 path.as_ref().map(|p| p.as_ref()).to_glib_none().0,
204 );
205 }
206 }
207
208 /// Sets the file path to use as the stdin for spawned processes.
209 ///
210 /// If @path is [`None`] then any previously given path is unset.
211 ///
212 /// The file must exist or spawning the process will fail.
213 ///
214 /// You may not set a stdin file path if a stdin fd is already set or if
215 /// the launcher flags contain any flags directing stdin elsewhere.
216 ///
217 /// This feature is only available on UNIX.
218 /// ## `path`
219 /// a filename or [`None`]
220 #[cfg(unix)]
221 #[cfg_attr(docsrs, doc(cfg(unix)))]
222 #[doc(alias = "g_subprocess_launcher_set_stdin_file_path")]
223 pub fn set_stdin_file_path(&self, path: Option<impl AsRef<std::path::Path>>) {
224 unsafe {
225 ffi::g_subprocess_launcher_set_stdin_file_path(
226 self.to_glib_none().0,
227 path.as_ref().map(|p| p.as_ref()).to_glib_none().0,
228 );
229 }
230 }
231
232 /// Sets the file path to use as the stdout for spawned processes.
233 ///
234 /// If @path is [`None`] then any previously given path is unset.
235 ///
236 /// The file will be created or truncated when the process is spawned, as
237 /// would be the case if using '>' at the shell.
238 ///
239 /// You may not set a stdout file path if a stdout fd is already set or
240 /// if the launcher flags contain any flags directing stdout elsewhere.
241 ///
242 /// This feature is only available on UNIX.
243 /// ## `path`
244 /// a filename or [`None`]
245 #[cfg(unix)]
246 #[cfg_attr(docsrs, doc(cfg(unix)))]
247 #[doc(alias = "g_subprocess_launcher_set_stdout_file_path")]
248 pub fn set_stdout_file_path(&self, path: Option<impl AsRef<std::path::Path>>) {
249 unsafe {
250 ffi::g_subprocess_launcher_set_stdout_file_path(
251 self.to_glib_none().0,
252 path.as_ref().map(|p| p.as_ref()).to_glib_none().0,
253 );
254 }
255 }
256
257 /// Sets the environment variable @variable in the environment of
258 /// processes launched from this launcher.
259 ///
260 /// On UNIX, both the variable's name and value can be arbitrary byte
261 /// strings, except that the variable's name cannot contain '='.
262 /// On Windows, they should be in UTF-8.
263 /// ## `variable`
264 /// the environment variable to set,
265 /// must not contain '='
266 /// ## `value`
267 /// the new value for the variable
268 /// ## `overwrite`
269 /// whether to change the variable if it already exists
270 #[doc(alias = "g_subprocess_launcher_setenv")]
271 pub fn setenv(
272 &self,
273 variable: impl AsRef<std::ffi::OsStr>,
274 value: impl AsRef<std::ffi::OsStr>,
275 overwrite: bool,
276 ) {
277 unsafe {
278 ffi::g_subprocess_launcher_setenv(
279 self.to_glib_none().0,
280 variable.as_ref().to_glib_none().0,
281 value.as_ref().to_glib_none().0,
282 overwrite.into_glib(),
283 );
284 }
285 }
286
287 //#[doc(alias = "g_subprocess_launcher_spawn")]
288 //pub fn spawn(&self, error: &mut glib::Error, argv0: &str, : /*Unknown conversion*//*Unimplemented*/Basic: VarArgs) -> Subprocess {
289 // unsafe { TODO: call ffi:g_subprocess_launcher_spawn() }
290 //}
291
292 /// Creates a #GSubprocess given a provided varargs list of arguments.
293 /// ## `argv0`
294 /// Command line arguments
295 ///
296 /// # Returns
297 ///
298 /// A new #GSubprocess, or [`None`] on error (and @error will be set)
299 #[doc(alias = "g_subprocess_launcher_spawnv")]
300 #[doc(alias = "spawnv")]
301 pub fn spawn(&self, argv: &[&std::ffi::OsStr]) -> Result<Subprocess, glib::Error> {
302 unsafe {
303 let mut error = std::ptr::null_mut();
304 let ret = ffi::g_subprocess_launcher_spawnv(
305 self.to_glib_none().0,
306 argv.to_glib_none().0,
307 &mut error,
308 );
309 if error.is_null() {
310 Ok(from_glib_full(ret))
311 } else {
312 Err(from_glib_full(error))
313 }
314 }
315 }
316
317 /// Removes the environment variable @variable from the environment of
318 /// processes launched from this launcher.
319 ///
320 /// On UNIX, the variable's name can be an arbitrary byte string not
321 /// containing '='. On Windows, it should be in UTF-8.
322 /// ## `variable`
323 /// the environment variable to unset,
324 /// must not contain '='
325 #[doc(alias = "g_subprocess_launcher_unsetenv")]
326 pub fn unsetenv(&self, variable: impl AsRef<std::ffi::OsStr>) {
327 unsafe {
328 ffi::g_subprocess_launcher_unsetenv(
329 self.to_glib_none().0,
330 variable.as_ref().to_glib_none().0,
331 );
332 }
333 }
334}