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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#![allow(unsafe_code)]
pub mod access_container;
pub mod cipher_opt;
pub mod immutable_data;
pub mod ipc;
pub mod logging;
pub mod mdata_info;
pub mod crypto;
pub mod mutable_data;
pub mod nfs;
#[cfg(any(test, feature = "testing"))]
pub mod test_utils;
mod helper;
#[cfg(test)]
mod tests;
use super::App;
use super::errors::AppError;
use config_file_handler;
use ffi_utils::{FFI_RESULT_OK, FfiResult, OpaqueCtx, ReprC, catch_unwind_cb, from_c_str};
use futures::Future;
use maidsafe_utilities::serialisation::deserialise;
use safe_core::{self, FutureExt};
use safe_core::ffi::AccountInfo as FfiAccountInfo;
use safe_core::ffi::ipc::resp::AuthGranted as FfiAuthGranted;
use safe_core::ipc::{AuthGranted, BootstrapConfig};
use std::ffi::{CStr, CString, OsStr};
use std::os::raw::{c_char, c_void};
use std::slice;
#[no_mangle]
pub unsafe extern "C" fn app_unregistered(
bootstrap_config: *const u8,
bootstrap_config_len: usize,
user_data: *mut c_void,
o_disconnect_notifier_cb: extern "C" fn(user_data: *mut c_void),
o_cb: extern "C" fn(user_data: *mut c_void,
result: *const FfiResult,
app: *mut App),
) {
catch_unwind_cb(user_data, o_cb, || -> Result<_, AppError> {
let user_data = OpaqueCtx(user_data);
let config = if bootstrap_config_len == 0 || bootstrap_config.is_null() {
None
} else {
let config_serialised = slice::from_raw_parts(bootstrap_config, bootstrap_config_len);
Some(deserialise::<BootstrapConfig>(config_serialised)?)
};
let app = App::unregistered(move || o_disconnect_notifier_cb(user_data.0), config)?;
o_cb(user_data.0, FFI_RESULT_OK, Box::into_raw(Box::new(app)));
Ok(())
})
}
#[no_mangle]
pub unsafe extern "C" fn app_registered(
app_id: *const c_char,
auth_granted: *const FfiAuthGranted,
user_data: *mut c_void,
o_disconnect_notifier_cb: extern "C" fn(user_data: *mut c_void),
o_cb: extern "C" fn(user_data: *mut c_void,
result: *const FfiResult,
app: *mut App),
) {
catch_unwind_cb(user_data, o_cb, || -> Result<_, AppError> {
let user_data = OpaqueCtx(user_data);
let app_id = from_c_str(app_id)?;
let auth_granted = AuthGranted::clone_from_repr_c(auth_granted)?;
let app = App::registered(app_id, auth_granted, move || {
o_disconnect_notifier_cb(user_data.0)
})?;
o_cb(user_data.0, FFI_RESULT_OK, Box::into_raw(Box::new(app)));
Ok(())
})
}
#[no_mangle]
pub unsafe extern "C" fn app_reconnect(
app: *mut App,
user_data: *mut c_void,
o_cb: extern "C" fn(user_data: *mut c_void, result: *const FfiResult),
) {
catch_unwind_cb(user_data, o_cb, || -> Result<_, AppError> {
let user_data = OpaqueCtx(user_data);
(*app).send(move |client, _| {
try_cb!(
client.restart_routing().map_err(AppError::from),
user_data.0,
o_cb
);
o_cb(user_data.0, FFI_RESULT_OK);
None
})
})
}
#[no_mangle]
pub unsafe extern "C" fn app_account_info(
app: *mut App,
user_data: *mut c_void,
o_cb: extern "C" fn(user_data: *mut c_void,
result: *const FfiResult,
account_info: *const FfiAccountInfo),
) {
catch_unwind_cb(user_data, o_cb, || -> Result<_, AppError> {
let user_data = OpaqueCtx(user_data);
(*app).send(move |client, _| {
client
.get_account_info()
.map(move |acc_info| {
let ffi_acc = FfiAccountInfo {
mutations_done: acc_info.mutations_done,
mutations_available: acc_info.mutations_available,
};
o_cb(user_data.0, FFI_RESULT_OK, &ffi_acc);
})
.map_err(move |e| {
call_result_cb!(Err::<(), _>(AppError::from(e)), user_data, o_cb);
})
.into_box()
.into()
})
})
}
#[no_mangle]
pub unsafe extern "C" fn app_exe_file_stem(
user_data: *mut c_void,
o_cb: extern "C" fn(user_data: *mut c_void,
result: *const FfiResult,
filename: *const c_char),
) {
catch_unwind_cb(user_data, o_cb, || -> Result<_, AppError> {
if let Ok(path) = config_file_handler::exe_file_stem()?.into_string() {
let path_c_str = CString::new(path)?;
o_cb(user_data, FFI_RESULT_OK, path_c_str.as_ptr());
} else {
call_result_cb!(
Err::<(), _>(AppError::from(
"config_file_handler returned invalid string",
)),
user_data,
o_cb
);
}
Ok(())
});
}
#[no_mangle]
pub unsafe extern "C" fn app_set_additional_search_path(
new_path: *const c_char,
user_data: *mut c_void,
o_cb: extern "C" fn(user_data: *mut c_void, result: *const FfiResult),
) {
catch_unwind_cb(user_data, o_cb, || -> Result<_, AppError> {
let new_path = CStr::from_ptr(new_path).to_str()?;
config_file_handler::set_additional_search_path(OsStr::new(new_path));
o_cb(user_data, FFI_RESULT_OK);
Ok(())
});
}
#[no_mangle]
pub unsafe extern "C" fn app_free(app: *mut App) {
let _ = Box::from_raw(app);
}
#[no_mangle]
pub unsafe extern "C" fn app_reset_object_cache(
app: *mut App,
user_data: *mut c_void,
o_cb: extern "C" fn(user_data: *mut c_void, result: *const FfiResult),
) {
catch_unwind_cb(user_data, o_cb, || -> Result<_, AppError> {
let user_data = OpaqueCtx(user_data);
(*app).send(move |_, context| {
context.object_cache().reset();
o_cb(user_data.0, FFI_RESULT_OK);
None
})
})
}
#[no_mangle]
pub unsafe extern "C" fn app_container_name(
app_id: *const c_char,
user_data: *mut c_void,
o_cb: extern "C" fn(user_data: *mut c_void,
result: *const FfiResult,
container_name: *const c_char),
) {
catch_unwind_cb(user_data, o_cb, || -> Result<_, AppError> {
let name = CString::new(safe_core::app_container_name(
CStr::from_ptr(app_id).to_str()?,
))?;
o_cb(user_data, FFI_RESULT_OK, name.as_ptr());
Ok(())
})
}