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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
//! Storj DCS metadata types.
use std::collections::HashMap;
use std::ffi::c_char;
use std::ptr;
use std::time::Duration;
use std::vec::Vec;
use uplink_sys as ulksys;
/// It's a container for custom information of a specific "item".
/// It's provided by the users as key-value pairs which must only contain valid
/// UTF-8 characters. Keys are unique, so only one value can be associated with
/// it.
///
/// By convention an application that stores metadata should prepend to the keys
/// a prefix, for example an application named "Image Board" might use the
/// "image-board:" prefix and a key could be "image-board:title".
#[derive(Default, Debug)]
pub struct Custom {
/// The key-value pairs.
entries: HashMap<String, String>,
/// Cached FFI representation of this instance that guards its lifetime while it's hold by this
/// field or this instance drops.
///
/// It's an option because it's only created when calling [`Self::to_ffi_custom_metadata`] and
/// hold it meanwhile this instance isn't mutated.
inner: Option<UplinkCustomMetadataWrapper>,
}
impl Custom {
/// Creates an empty custom metadata with the specified capacity.
pub fn with_capacity(capacity: usize) -> Self {
let map = HashMap::with_capacity(capacity);
Self {
entries: map,
inner: None,
}
}
/// Creates a custom metadata instance from type exposed by the FFI.
///
/// NOTE this method assumes `uc_custom` only contains key-value pairs that have valid UTF-8
/// bytes. In the case that it doesn't then the mapped key-value may not have the same value in
/// that byte position and it isn't either guarantee that the same invalid UTF-8 byte produces
/// the same mapped value.
pub(crate) fn with_ffi_custom_metadata(uc_custom: &ulksys::UplinkCustomMetadata) -> Self {
if uc_custom.count == 0 {
return Default::default();
}
let mut custom = Self::with_capacity(uc_custom.count);
// SAFETY: we trust that the FFI contains a valid pointer to entries and the counter has
// the exact number of entries, and each entry has a key-value C string with exactly the
// length specified without leaning that they end with the NULL byte because they could
// contain NULL bytes.
unsafe {
use crate::helpers::unchecked_ptr_c_char_and_length_to_string;
for i in 0..uc_custom.count as isize {
let entry = uc_custom.entries.offset(i) as *const ulksys::UplinkCustomMetadataEntry;
let key =
unchecked_ptr_c_char_and_length_to_string((*entry).key, (*entry).key_length);
let value = unchecked_ptr_c_char_and_length_to_string(
(*entry).value,
(*entry).value_length,
);
custom.insert(key, value);
}
}
custom
}
/// Returns the current number of entries (i.e. key-value pairs).
pub fn count(&self) -> usize {
self.entries.len()
}
/// Gets the entry's value associated with the passed key. Returns none if
/// there isn't any entry associated to the key.
pub fn get(&self, key: &str) -> Option<&String> {
match self.entries.get(key) {
Some(v) => Some(v),
None => None,
}
}
/// Inserts a new entry with the specified key and value, returning false if
/// the key didn't exit, otherwise true and replace the value associated to
/// the key.
pub fn insert(&mut self, key: String, value: String) -> bool {
self.inner = None;
self.entries.insert(key, value).is_some()
}
/// An iterator for visiting all the metadata key-value pairs.
pub fn iter(&self) -> impl std::iter::Iterator<Item = (&String, &String)> {
self.entries.iter()
}
/// Deletes the entry with the associated key, returning false if the key
/// didn't exist, otherwise true.
pub fn delete(&mut self, key: &str) -> bool {
self.inner = None;
self.entries.remove(key).is_some()
}
/// Returns the FFI representation of this custom metadata container which is valid as long as
/// `self` isn't mutated or dropped.
///
/// When this method is called more than once and `self` isn't mutated in between, the calls
/// after the first are very cheap because the returned value is cached.
#[allow(clippy::wrong_self_convention)]
pub(crate) fn to_ffi_custom_metadata(&mut self) -> ulksys::UplinkCustomMetadata {
if self.inner.is_none() {
self.inner = Some(UplinkCustomMetadataWrapper::from_custom(self));
}
// We have ensured that `inner` is not None just above so `unwrap` will never panic.
self.inner.as_ref().unwrap().custom_metadata
}
}
impl Clone for Custom {
fn clone(&self) -> Self {
Self {
entries: self.entries.clone(),
inner: None,
}
}
}
/// It allows to create an [`uplink_sys::UplinkCustomMetadata`] instance that
/// guards the used memory of its list of items during the lifetime of the
/// instance of this struct.
#[derive(Debug)]
struct UplinkCustomMetadataWrapper {
/// The [`uplink_sys::UplinkCustomMetadata`] instance that `self`
/// represents.
custom_metadata: ulksys::UplinkCustomMetadata,
/// The allocated memory of the list of entries referenced by the FFI value in the field
/// `custom_metadata` and whose lifetime is guarded by an instance of `Self`.
_entries: Vec<ulksys::UplinkCustomMetadataEntry>,
}
impl UplinkCustomMetadataWrapper {
/// Creates a wrapped [`uplink_sys::UplinkCustomMetadata`] which represents
/// the passed [`Custom`].
fn from_custom(custom: &Custom) -> Self {
let num_entries = custom.count();
if num_entries == 0 {
return Default::default();
}
let mut entries = Vec::with_capacity(num_entries);
for (k, v) in custom.iter() {
entries.push(ulksys::UplinkCustomMetadataEntry {
key: k.as_ptr() as *mut c_char,
key_length: k.len(),
value: v.as_ptr() as *mut c_char,
value_length: v.len(),
});
}
UplinkCustomMetadataWrapper {
custom_metadata: ulksys::UplinkCustomMetadata {
entries: entries.as_mut_ptr(),
count: entries.len(),
},
_entries: entries,
}
}
}
impl Default for UplinkCustomMetadataWrapper {
fn default() -> Self {
UplinkCustomMetadataWrapper {
custom_metadata: ulksys::UplinkCustomMetadata {
entries: ptr::null_mut(),
count: 0,
},
_entries: Vec::new(),
}
}
}
/// It's a container of system information of a specific "item".
/// It's provided by the service and only the service can alter it.
#[derive(Debug)]
pub struct System {
/// When the associated "item" was created.
///
/// The time is measured with the number of seconds since the Unix Epoch
/// time.
pub created: Duration,
/// When the associated "item" expires. When it never expires is `None`.
///
/// The time is measured with the number of seconds since the Unix Epoch
/// time.
pub expires: Option<Duration>,
/// Then length of the data associated to this metadata.
///
/// NOTE it's a signed integer because the original library uses a signed
/// integer, so it may be the case now or in the future that negatives
/// numbers are used.
pub content_length: i64,
}
impl System {
/// Create a new instance from its FFI representation.
///
/// The function doesn't check `created` nor `expires` have correct values.
/// For example if they are negative or `expires` is less than `created`. Nonetheless because
/// Rust `std::time::Duration` types don't support negative values, if any of them contains a
/// negative or zero, `created` is set to zero duration and `expires` to `None`.
pub fn with_ffi_system_metadata(uc_system: &ulksys::UplinkSystemMetadata) -> Self {
let created = if uc_system.created > 0 {
Duration::from_secs(uc_system.created as u64)
} else {
Duration::ZERO
};
let expires = if uc_system.expires > 0 {
Some(Duration::from_secs(uc_system.expires as u64))
} else {
None
};
Self {
created,
expires,
content_length: uc_system.content_length,
}
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_custom_with_entries() {
let key1 = "key-a";
let val1 = "val-a";
let key2 = "key-b";
let val2 = "val-b";
let mut custom = Custom::with_capacity(2);
custom.insert(String::from(key1), String::from(val1));
custom.insert(String::from(key2), String::from(val2));
assert_eq!(custom.count(), 2, "count");
assert_eq!(custom.get(key1), Some(&String::from(val1)), "get: 'key1'");
assert_eq!(custom.get(key2), Some(&String::from(val2)), "get: 'key2'");
assert_eq!(custom.get("unexisting"), None, "get 'unexisting'");
}
#[test]
fn test_custom_insert() {
let key1 = "key-a";
let val1 = "val-a";
let val1_2 = "val-a-2";
let key2 = "key-b";
let val2 = "val-b";
let mut custom = Custom::with_capacity(2);
custom.insert(String::from(key1), String::from(val1));
assert_eq!(custom.count(), 1, "count before inserting a new key");
assert_eq!(custom.get(key2), None, "get 'key2' before inserting it");
assert!(
!custom.insert(String::from(key2), String::from(val2)),
"insert 'key2'"
);
assert_eq!(custom.count(), 2, "count after inserting a new key");
assert_eq!(
custom.get(key2),
Some(&String::from(val2)),
"get 'key2' after inserting it"
);
assert_eq!(
custom.get(key1),
Some(&String::from(val1)),
"get 'key1' before updating it"
);
assert!(
custom.insert(String::from(key1), String::from(val1_2)),
"insert 'key1' with new value"
);
assert_eq!(custom.count(), 2, "count after inserting an existing key");
assert_eq!(
custom.get(key1),
Some(&String::from(val1_2)),
"get 'key1' after updating it"
);
}
#[test]
fn test_custom_remove() {
let key1 = "key-a";
let val1 = "val-a";
let key2 = "key-b";
let val2 = "val-b";
let mut custom = Custom::with_capacity(2);
custom.insert(String::from(key1), String::from(val1));
custom.insert(String::from(key2), String::from(val2));
assert_eq!(custom.count(), 2, "count before removing a new key");
assert!(custom.delete(key1), "remove 'key1'");
assert_eq!(custom.count(), 1, "count after removing a new key");
assert_eq!(custom.get(key1), None, "get 'key1'");
assert_eq!(custom.get(key2), Some(&String::from(val2)), "get 'key2'");
assert!(!custom.delete(key1), "remove an unexisting key");
assert_eq!(custom.count(), 1, "count after removing a unexisting key");
}
#[test]
fn test_custom_clone() {
let mut source = Custom::default();
assert_eq!(
source.count(),
0,
"count on 'source' after it's initialized with 'default'"
);
let clone = source.clone();
assert_eq!(
clone.count(),
0,
"count on 'clone' after cloning an instance with 0 entries"
);
let key1 = "key-a";
let val1 = "val-a";
let key2 = "key-b";
let val2 = "val-b";
assert!(
!source.insert(String::from(key1), String::from(val1)),
"insert 'key1' into 'source'"
);
let mut clone = source.clone();
assert_eq!(
clone.count(),
1,
"count on 'clone' after cloning an instance with 1 entries"
);
assert_eq!(
clone.get(key1),
Some(&String::from(val1)),
"get 'key1' from 'clone'"
);
assert!(
!source.insert(String::from(key2), String::from(val2)),
"insert 'key2' into 'soure'"
);
assert_eq!(
clone.count(),
1,
"count of 'clone' after inserting 'key2' in 'source'"
);
assert_eq!(
clone.get(key1),
Some(&String::from(val1)),
"get 'key1' from 'clone' after inserting 'key2' in 'source'"
);
assert_eq!(
clone.get(key2),
None,
"get 'key2' from 'clone' which has never been inserted"
);
assert!(source.delete(key1), "remove 'key1' from 'soruce'");
assert_eq!(
clone.count(),
1,
"count on 'clone' after removing 'key1' of 'source'"
);
assert_eq!(
clone.get(key1),
Some(&String::from(val1)),
"get 'key1' from 'clone' after remove 'key1' of 'source'"
);
assert_eq!(
source.count(),
1,
"count on 'source' before removing 'key1' of 'clone'"
);
assert!(clone.delete(key1), "remove 'key1' from 'clone'");
assert_eq!(
source.count(),
1,
"count on 'source' after removing 'key1' of 'clone'"
);
}
#[test]
fn test_custom_iterator() {
let key1 = "key-a";
let val1 = "val-a";
let key2 = "key-b";
let val2 = "val-b";
let mut custom = Custom::with_capacity(2);
custom.insert(String::from(key1), String::from(val1));
custom.insert(String::from(key2), String::from(val2));
assert_eq!(custom.count(), 2, "number of entries");
for entry in (&custom).iter() {
if entry.0 == &String::from(key1) && entry.1 == &String::from(val1) {
continue;
}
if entry.0 == &String::from(key2) && entry.1 == &String::from(val2) {
continue;
}
panic!("Custom shouldn't contains the entry {:#?}", entry);
}
}
use crate::helpers::test::{assert_c_string, compare_c_string};
#[test]
fn test_custom_with_ffi_custom_metadata() {
let key1 = "key-a";
let val1 = "val-a";
let key2 = "key-b";
let val2 = "val-b";
let from;
{
// This scope drops `to` for doing the commented check right after
// the scope closes.
let mut to = Custom::with_capacity(2);
to.insert(String::from(key1), String::from(val1));
to.insert(String::from(key2), String::from(val2));
from = Custom::with_ffi_custom_metadata(&to.to_ffi_custom_metadata());
assert_eq!(from.count(), 2, "count");
assert_eq!(from.get(key1), Some(&String::from(val1)), "get: 'key1'");
assert_eq!(from.get(key2), Some(&String::from(val2)), "get: 'key2'");
// Ensure that to is dropped.
drop(to);
}
// Check that a Custom instance generated from an UplinkCustomMetadata
// that has dropped is still valid.
assert_eq!(from.count(), 2, "count");
assert_eq!(from.get(key1), Some(&String::from(val1)), "get: 'key1'");
assert_eq!(from.get(key2), Some(&String::from(val2)), "get: 'key2'");
}
#[test]
fn test_custom_to_ffi_custom_metadata() {
let key1 = "key-a";
let val1 = "val-a";
let key2 = "key-b";
let val2 = "val-b";
let mut custom = Custom::with_capacity(2);
custom.insert(String::from(key1), String::from(val1));
custom.insert(String::from(key2), String::from(val2));
let c_custom = custom.to_ffi_custom_metadata();
assert_eq!(c_custom.count, 2, "count");
let c_entries = c_custom.entries as *const ulksys::UplinkCustomMetadataEntry;
unsafe {
for i in 0..1 {
let entry = *c_entries.offset(i);
if compare_c_string(entry.key, key1).is_none() {
assert_c_string(entry.value, val1);
continue;
}
if compare_c_string(entry.key, key2).is_none() {
assert_c_string(entry.value, val2);
continue;
}
panic!("UplinkCustomMetadata instance doesn't contains one of the expected keys ({}, {})", key1, key2);
}
}
// Modify the custom metadata and verify that the methods returns an
// UplinkCustomMetadata which reflets the current custom metadata state.
custom.delete(key1);
let c_custom = custom.to_ffi_custom_metadata();
assert_eq!(c_custom.count, 1, "count");
let c_entries = c_custom.entries as *const ulksys::UplinkCustomMetadataEntry;
unsafe {
let entry = *c_entries;
assert_c_string(entry.key, key2);
assert_c_string(entry.value, val2);
}
}
#[test]
fn test_system_with_ffi_system_metadata() {
{
// 0 expiration
let uc_sysm = ulksys::UplinkSystemMetadata {
created: 3600,
expires: 0,
content_length: 10,
};
let sysm = System::with_ffi_system_metadata(&uc_sysm);
assert_eq!(
sysm.created,
Duration::from_secs(uc_sysm.created as u64),
"positive created"
);
assert_eq!(sysm.expires, None, "zero expires");
assert_eq!(
sysm.content_length, uc_sysm.content_length,
"positive content length"
);
}
{
// postive correct expiration
let uc_sysm = ulksys::UplinkSystemMetadata {
created: 3600,
expires: 3601,
content_length: 10,
};
let sysm = System::with_ffi_system_metadata(&uc_sysm);
assert_eq!(
sysm.created,
Duration::from_secs(uc_sysm.created as u64),
"positive created"
);
assert_eq!(
sysm.expires,
Some(Duration::from_secs(uc_sysm.expires as u64)),
"positive expires"
);
assert_eq!(
sysm.content_length, uc_sysm.content_length,
"positive content length"
);
}
{
// postive incorrect expiration (it's before created)
let uc_sysm = ulksys::UplinkSystemMetadata {
created: 3600,
expires: 100,
content_length: 10,
};
let sysm = System::with_ffi_system_metadata(&uc_sysm);
assert_eq!(
sysm.created,
Duration::from_secs(uc_sysm.created as u64),
"positive created"
);
assert_eq!(
sysm.expires,
Some(Duration::from_secs(uc_sysm.expires as u64)),
"positive expires before created"
);
assert_eq!(
sysm.content_length, uc_sysm.content_length,
"positive content length"
);
}
{
// 0 content length
let uc_sysm = ulksys::UplinkSystemMetadata {
created: 3600,
expires: 9999,
content_length: 0,
};
let sysm = System::with_ffi_system_metadata(&uc_sysm);
assert_eq!(
sysm.created,
Duration::from_secs(uc_sysm.created as u64),
"positive created"
);
assert_eq!(
sysm.expires,
Some(Duration::from_secs(uc_sysm.expires as u64)),
"positive expires"
);
assert_eq!(
sysm.content_length, uc_sysm.content_length,
"zero content length"
);
}
{
// negative content length
let uc_sysm = ulksys::UplinkSystemMetadata {
created: 3600,
expires: 9999,
content_length: -3,
};
let sysm = System::with_ffi_system_metadata(&uc_sysm);
assert_eq!(
sysm.created,
Duration::from_secs(uc_sysm.created as u64),
"positive created"
);
assert_eq!(
sysm.expires,
Some(Duration::from_secs(uc_sysm.expires as u64)),
"positive expires"
);
assert_eq!(
sysm.content_length, uc_sysm.content_length,
"negative content length"
);
}
{
// negative created
let uc_sysm = ulksys::UplinkSystemMetadata {
created: -1,
expires: 9999,
content_length: 99,
};
let sysm = System::with_ffi_system_metadata(&uc_sysm);
assert_eq!(sysm.created, Duration::ZERO, "negative created");
assert_eq!(
sysm.expires,
Some(Duration::from_secs(uc_sysm.expires as u64)),
"positive expires"
);
assert_eq!(
sysm.content_length, uc_sysm.content_length,
"positive content length"
);
}
{
// 0 created
let uc_sysm = ulksys::UplinkSystemMetadata {
created: 0,
expires: 75,
content_length: 99,
};
let sysm = System::with_ffi_system_metadata(&uc_sysm);
assert_eq!(sysm.created, Duration::ZERO, "zero created");
assert_eq!(
sysm.expires,
Some(Duration::from_secs(uc_sysm.expires as u64)),
"positive expires"
);
assert_eq!(
sysm.content_length, uc_sysm.content_length,
"positive content length"
);
}
{
// 0 expiration
let uc_sysm = ulksys::UplinkSystemMetadata {
created: 876543,
expires: 0,
content_length: 99,
};
let sysm = System::with_ffi_system_metadata(&uc_sysm);
assert_eq!(
sysm.created,
Duration::from_secs(uc_sysm.created as u64),
"positive created"
);
assert_eq!(sysm.expires, None, "zero expires");
assert_eq!(
sysm.content_length, uc_sysm.content_length,
"positive content length"
);
}
{
// negative expiration
let uc_sysm = ulksys::UplinkSystemMetadata {
created: 876543,
expires: -1,
content_length: 99,
};
let sysm = System::with_ffi_system_metadata(&uc_sysm);
assert_eq!(
sysm.created,
Duration::from_secs(uc_sysm.created as u64),
"positive created"
);
assert_eq!(sysm.expires, None, "negative expires");
assert_eq!(
sysm.content_length, uc_sysm.content_length,
"positive content length"
);
}
}
}