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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
//! Storj DCS Uplink configuration.
use crate::{helpers, Result};
use std::ffi::CString;
use std::time::Duration;
use uplink_sys as ulksys;
/// Defines configuration for using Uplink library.
#[derive(Debug)]
pub struct Config<'a> {
/// The configuration type of the FFI that an instance of this struct represents and guard its
/// lifetime until this instance drops.
pub(crate) inner: ulksys::UplinkConfig,
/// Identifies the application how is contacting with the satellite.
/// The user agent is used for statistics and for identifying the usage coming from associated
/// partners.
user_agent: &'a str,
/// Defines how long the client should wait for establishing a connection to peers.
dial_timeout: Duration,
/// Path to a directory to be used for storing temporary files when running completely in memory
/// is disabled. It's `None` when running only in memory.
temp_dir: Option<&'a str>,
/// Specifies to only operates using memory, hence it doesn't off-load data to disk.
in_memory: bool,
}
impl<'a> Config<'a> {
/// Creates a configuration with the specific user agent, dial timeout and using a specific
/// directory path for creating temporary files.
///
/// Some operations performed by this configuration or any instance created from it may offload
/// data from memory to disk.
///
/// When `temp_dir`is `None` or an empty string, a random directory path will be used.
///
/// NOTE:
/// * Even that the FFI offers this option, it may not use it and just fully operates in memory.
/// * The directory path isn't checked so the result of using a directory which doesn't exist
/// will depend on the result of the FFI at the moment of using the configuration.
pub fn new(
user_agent: &'a str,
dial_timeout: Duration,
temp_dir: Option<&'a str>,
) -> Result<Self> {
let inner;
{
let uagent = helpers::cstring_from_str_fn_arg("user_agent", user_agent)?;
let tdir = temp_dir.unwrap_or("");
let tdir = helpers::cstring_from_str_fn_arg("temp_dir", tdir)?;
inner = ulksys::UplinkConfig {
user_agent: uagent.into_raw(),
dial_timeout_milliseconds: dial_timeout.as_millis() as i32,
temp_directory: tdir.into_raw(),
};
}
Ok(Config {
inner,
user_agent,
dial_timeout,
temp_dir,
in_memory: false,
})
}
/// Creates a configuration with the specific user agent and dial timeout.
/// All the operations performed by this configuration or any instance created from it will
/// operate entirely in memory.
pub fn new_inmemory(user_agent: &'a str, dial_timeout: Duration) -> Result<Self> {
let inner;
{
let uagent = helpers::cstring_from_str_fn_arg("user_agent", user_agent)?;
let temp_dir = CString::new("inmemory")
.expect("BUG: hard-coded temp_dir string must never contains null bytes (0 byte)");
inner = ulksys::UplinkConfig {
user_agent: uagent.into_raw(),
dial_timeout_milliseconds: dial_timeout.as_millis() as i32,
temp_directory: temp_dir.into_raw(),
};
}
Ok(Config {
inner,
user_agent,
dial_timeout,
temp_dir: None,
in_memory: true,
})
}
/// Returns the configured dial timeout.
pub fn dial_timeout(&self) -> Duration {
self.dial_timeout
}
/// Returns if the configuration is specifying to use only memory or not.
///
/// It returns `true` and always `None` when it only uses memory, otherwise `false` and:
/// * `None` when using a random directory.
/// * `Some` when a temporary directory path is specified.
pub fn is_inmemory(&self) -> (bool, Option<&str>) {
if self.in_memory {
(true, None)
} else {
(false, self.temp_dir)
}
}
/// Returns the configured user agent.
pub fn user_agent(&self) -> &str {
self.user_agent
}
/// Returns the FFI representation of this configuration.
pub(crate) fn as_ffi_config(&self) -> ulksys::UplinkConfig {
self.inner
}
}
impl Drop for Config<'_> {
fn drop(&mut self) {
use std::os::raw::c_char;
// SAFETY: The inner field is initialized when an instance of this struct is initialized and
// it's only used by this crate to passed to the FFI.
// The FFI never free the memory or mutate the fields of its exposed struct instance held by
// the inner field, hence the lifetime of its fields which are pointers belong to this
// instance, so we must free when this instance drops.
// The 2 pointers explicitly freed here came from the call to the `into_raw` method of the
// `CString` instances crated from `&str`.
unsafe {
// Retake ownership of the CString(s) transferred to `self.inner`
// for freeing its memory when the created CString drops.
// `self.inner.user_agent` and `self.inner.temp_directory` are never
// null, otherwise there is bug in the implementation of this
// struct.
let _ = CString::from_raw(self.inner.user_agent as *mut c_char);
let _ = CString::from_raw(self.inner.temp_directory as *mut c_char);
}
}
}
#[cfg(test)]
mod test {
use super::*;
use crate::helpers::test::assert_c_string;
use crate::{error, Error};
#[test]
fn test_new() {
{
// OK case: use a randomly generated temp directory.
let ua = "rust-uplink";
let config = Config::new(ua, Duration::new(2, 5000000), None)
.expect("new shouldn't fail when 'user agent' doesn't contain any null character");
assert_eq!(config.user_agent, ua, "user_agent");
assert_eq!(
config.dial_timeout,
Duration::new(2, 5000000),
"dial_timeout"
);
assert_eq!(config.temp_dir, None, "temp_dir");
assert!(!config.in_memory, "in_memory");
assert_c_string(config.inner.user_agent, ua);
assert_ne!(config.inner.temp_directory, std::ptr::null());
assert_eq!(
config.inner.dial_timeout_milliseconds, 2005,
"inner.dial_tiemout_milliseconds"
);
}
{
// OK case: use a specific temp directory.
let ua = "rust-uplink-custom-temp-dir";
let temp_dir = "/tmp/rust-uplink";
let config = Config::new(ua, Duration::new(1, 785999999), Some(temp_dir))
.expect("new shouldn't fail when 'user agent' doesn't contain any null character");
assert_eq!(config.user_agent, ua, "user_agent");
assert_eq!(
config.dial_timeout,
Duration::new(1, 785999999),
"dial_timeout"
);
assert_eq!(config.temp_dir, Some(temp_dir), "temp_dir");
assert!(!config.in_memory, "in_memory");
assert_c_string(config.inner.user_agent, ua);
assert_c_string(config.inner.temp_directory, temp_dir);
assert_eq!(
config.inner.dial_timeout_milliseconds, 1785,
"inner.dial_tiemout_milliseconds"
);
}
{
// Error case: User agent has null characters.
if let Error::InvalidArguments(error::Args { names, msg }) =
Config::new("rust-uplink\0", Duration::new(3, 0), None)
.expect_err("new passing a user agent with NULL bytes")
{
assert_eq!(names, "user_agent", "invalid error argument name");
assert_eq!(
msg, "cannot contains null bytes (0 byte). Null byte found at 11",
"invalid error argument message"
);
} else {
panic!("expected an invalid argument error");
}
}
{
// Error case: Temp directory has null characters.
if let Error::InvalidArguments(error::Args { names, msg }) =
Config::new("rust-uplink", Duration::new(3, 0), Some("\0invalid"))
.expect_err("new passing a user agent with NULL bytes")
{
assert_eq!(names, "temp_dir", "invalid error argument name");
assert_eq!(
msg, "cannot contains null bytes (0 byte). Null byte found at 0",
"invalid error argument message"
);
} else {
panic!("expected an invalid argument error");
}
}
}
#[test]
fn test_new_inmemory() {
{
// OK case.
let config = Config::new_inmemory("rust-uplink", Duration::new(3, 0))
.expect("new shouldn't fail when 'user agent' doesn't contain any null character");
assert_eq!(config.user_agent, "rust-uplink", "user_agent");
assert_eq!(config.dial_timeout, Duration::new(3, 0), "dial_timeout");
assert_eq!(config.temp_dir, None, "temp_dir");
assert!(config.in_memory, "in_memory");
assert_c_string(config.inner.user_agent, "rust-uplink");
assert_c_string(config.inner.temp_directory, "inmemory");
assert_eq!(
config.inner.dial_timeout_milliseconds, 3000,
"inner.dial_tiemout_milliseconds"
);
}
{
// Error case.
if let Error::InvalidArguments(error::Args { names, msg }) =
Config::new_inmemory("rust\0uplink", Duration::new(3, 0))
.expect_err("new passing a user agent with NULL bytes")
{
assert_eq!(names, "user_agent", "invalid error argument name");
assert_eq!(
msg, "cannot contains null bytes (0 byte). Null byte found at 4",
"invalid error argument message"
);
} else {
panic!("expected an invalid argument error");
}
}
}
#[test]
fn test_dial_timeout() {
let config = Config::new("rust-uplink", Duration::new(1, 635578), None)
.expect("new shouldn't fail when 'user agent' doesn't contain any null character");
assert_eq!(
config.dial_timeout(),
Duration::new(1, 635578),
"dial_timeout"
);
}
#[test]
fn test_is_inmeory() {
{
// Using disk with random temp directory path.
let config = Config::new("rust-uplink", Duration::new(1, 635578), None)
.expect("new shouldn't fail when 'user agent' doesn't contain any null character");
assert_eq!(
config.is_inmemory(),
(false, None),
"disk and random directory"
);
}
{
// Using disk with a specific temp directory path.
let config = Config::new(
"rust-uplink",
Duration::new(1, 635578),
Some("/tmp/uplink-rs"),
)
.expect("new shouldn't fail when 'user agent' doesn't contain any null character");
assert_eq!(
config.is_inmemory(),
(false, Some("/tmp/uplink-rs")),
"disk and specific directory "
);
}
{
// Using only memory case.
let config = Config::new_inmemory("rust-uplink", Duration::new(1, 635578))
.expect("new shouldn't fail when 'user agent' doesn't contain any null character");
assert_eq!(config.is_inmemory(), (true, None), "using only memory");
}
}
#[test]
fn test_user_agent() {
let config = Config::new("rust-uplink", Duration::new(1, 635578), None)
.expect("new shouldn't fail when 'user agent' doesn't contain any null character");
assert_eq!(config.user_agent(), "rust-uplink", "user_agent");
}
}