forked from libspecinfra/specinfra
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.rs
More file actions
106 lines (88 loc) · 2.32 KB
/
Copy pathlib.rs
File metadata and controls
106 lines (88 loc) · 2.32 KB
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
extern crate uname;
extern crate libc;
extern crate users;
extern crate md5;
extern crate sha2;
extern crate nix;
extern crate version_compare;
extern crate regex;
use std::ffi::CStr;
use libc::c_char;
use backend::Backend;
use platform::platform::Platform;
use platform::error::Error;
use platform::error::DetectError;
use resource::file::File;
use resource::service::Service;
use provider::Providers;
pub struct Specinfra<'a> {
pub backend: &'a Backend,
pub platform: Box<Platform>,
pub providers: Box<Providers>,
}
pub fn new(b: &Backend) -> Result<Specinfra, Error> {
let p = try!(b.detect_platform()
.ok_or(DetectError { message: "Failed to detect platform".to_string() }));
let providers = try!(p.get_providers());
Ok(Specinfra {
backend: b,
platform: p,
providers: providers,
})
}
impl<'a> Specinfra<'a> {
pub fn file(&self, name: &'static str) -> File {
File::new(name, self.backend, &self.providers.file)
}
pub fn service(&self, name: &'static str) -> Service {
Service::new(name, self.backend, &self.providers.service)
}
}
// Wrapper functions for FFI
use backend::BackendWrapper;
#[no_mangle]
pub extern "C" fn specinfra_new<'a>(ptr: *const BackendWrapper) -> *const Specinfra<'a> {
let b = unsafe {
assert!(!ptr.is_null());
&*ptr
};
Box::into_raw(Box::new(self::new(&*b.backend).unwrap()))
}
#[no_mangle]
pub extern "C" fn specinfra_free(ptr: *mut Specinfra) {
if ptr.is_null() {
return;
}
unsafe {
Box::from_raw(ptr);
}
}
#[no_mangle]
pub extern "C" fn specinfra_file(ptr: *const Specinfra, name: *const c_char) -> *const File {
let s = unsafe {
assert!(!ptr.is_null());
&*ptr
};
let name = unsafe {
assert!(!name.is_null());
CStr::from_ptr(name)
};
Box::into_raw(Box::new(s.file(name.to_str().unwrap())))
}
#[no_mangle]
pub extern "C" fn specinfra_service(ptr: *const Specinfra, name: *const c_char) -> *const Service {
let s = unsafe {
assert!(!ptr.is_null());
&*ptr
};
let name = unsafe {
assert!(!name.is_null());
CStr::from_ptr(name)
};
Box::into_raw(Box::new(s.service(name.to_str().unwrap())))
}
pub mod backend;
pub mod platform;
pub mod resource;
pub mod provider;
pub mod error;