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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
use std::process::Output;
use crate::wine::*;
pub trait WineBootExt {
/// Get base `wineboot` command. Will return `wine wineboot` if `self.wineboot()` is `None`
fn wineboot_command(&self) -> Command;
/// Initialize wine prefix. Runs `wineboot -i` command
///
/// ```no_run
/// use wincompatlib::prelude::*;
///
/// Wine::default()
/// .init_prefix(Some("/path/to/prefix"))
/// .expect("Failed to create prefix");
/// ```
///
/// Use prefix specified in current wine struct:
///
/// ```no_run
/// use wincompatlib::prelude::*;
///
/// Wine::default()
/// .with_prefix("/path/to/prefix")
/// .init_prefix(None::<&str>) // Don't even ask
/// .expect("Failed to create prefix");
/// ```
///
/// If prefix is not specified in `Wine` struct and is not given to `update_prefix` method -
/// then `Err` will be returned
fn init_prefix(&self, path: Option<impl Into<PathBuf>>) -> anyhow::Result<Output>;
/// Update existing wine prefix. Runs `wineboot -u` command
///
/// ```no_run
/// use wincompatlib::prelude::*;
///
/// Wine::default()
/// .update_prefix(Some("/path/to/prefix"))
/// .expect("Failed to update prefix");
/// ```
///
/// Use prefix specified in current wine struct:
///
/// ```no_run
/// use wincompatlib::prelude::*;
///
/// Wine::default()
/// .with_prefix("/path/to/prefix")
/// .update_prefix(None::<&str>) // Don't even ask
/// .expect("Failed to update prefix");
/// ```
///
/// If prefix is not specified in `Wine` struct and is not given to `update_prefix` method -
/// then `Err` will be returned
fn update_prefix(&self, path: Option<impl Into<PathBuf>>) -> anyhow::Result<Output>;
/// Stop running processes. Runs `wineboot -k` command, or `wineboot -f` if `force = true`
///
/// ```no_run
/// use wincompatlib::prelude::*;
///
/// Wine::default()
/// .stop_processes(false)
/// .expect("Failed to update prefix");
/// ```
fn stop_processes(&self, force: bool) -> anyhow::Result<Output>;
/// Imitate windows restart. Runs `wineboot -r` command
///
/// ```no_run
/// use wincompatlib::prelude::*;
///
/// Wine::default()
/// .with_prefix("/path/to/prefix")
/// .restart()
/// .expect("Failed to restart");
/// ```
fn restart(&self) -> anyhow::Result<Output>;
/// Imitate windows shutdown. Runs `wineboot -s` command
///
/// ```no_run
/// use wincompatlib::prelude::*;
///
/// Wine::default()
/// .with_prefix("/path/to/prefix")
/// .shutdown()
/// .expect("Failed to shutdown");
/// ```
fn shutdown(&self) -> anyhow::Result<Output>;
/// End wineboot session. Runs `wineboot -e` command
///
/// ```no_run
/// use wincompatlib::prelude::*;
///
/// Wine::default()
/// .with_prefix("/path/to/prefix")
/// .end_session()
/// .expect("Failed to shutdown");
/// ```
fn end_session(&self) -> anyhow::Result<Output>;
}
impl WineBootExt for Wine {
fn wineboot_command(&self) -> Command {
match self.wineboot() {
Some(WineBoot::Unix(wineboot)) => Command::new(wineboot),
Some(WineBoot::Windows(wineboot)) => {
let mut command = Command::new(&self.binary);
command.arg(wineboot);
command
}
None => {
let mut command = Command::new(&self.binary);
command.arg("wineboot");
command
}
}
}
fn init_prefix(&self, path: Option<impl Into<PathBuf>>) -> anyhow::Result<Output> {
let path = match path {
Some(path) => path.into(),
None => self.prefix.to_owned()
};
// Create all parent directories
if !path.exists() {
std::fs::create_dir_all(&path)?;
}
Ok(self.wineboot_command()
.arg("-i")
.envs(self.get_envs())
.env("WINEPREFIX", path)
.stdin(Stdio::null())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.output()?)
}
fn update_prefix(&self, path: Option<impl Into<PathBuf>>) -> anyhow::Result<Output> {
let path = match path {
Some(path) => path.into(),
None => self.prefix.to_owned()
};
// Create all parent directories
if !path.exists() {
std::fs::create_dir_all(&path)?;
}
Ok(self.wineboot_command()
.arg("-u")
.envs(self.get_envs())
.env("WINEPREFIX", path)
.stdin(Stdio::null())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.output()?)
}
fn stop_processes(&self, force: bool) -> anyhow::Result<Output> {
Ok(self.wineboot_command()
.arg(if force { "-f" } else { "-k" })
.envs(self.get_envs())
.stdin(Stdio::null())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.output()?)
}
fn restart(&self) -> anyhow::Result<Output> {
Ok(self.wineboot_command()
.arg("-r")
.envs(self.get_envs())
.stdin(Stdio::null())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.output()?)
}
fn shutdown(&self) -> anyhow::Result<Output> {
Ok(self.wineboot_command()
.arg("-s")
.envs(self.get_envs())
.stdin(Stdio::null())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.output()?)
}
fn end_session(&self) -> anyhow::Result<Output> {
Ok(self.wineboot_command()
.arg("-e")
.envs(self.get_envs())
.stdin(Stdio::null())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.output()?)
}
}