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
use libc;
use std::mem;
use std::ptr;
use std::ffi::{CStr, CString};
use alpm_sys::*;
pub struct UtsName(libc::utsname);
impl UtsName {
pub fn sysname(&self) -> &str {
unsafe {
CStr::from_ptr(self.0.sysname.as_ptr())
.to_str().expect("sysname not valid utf8 (should never happen)")
}
}
pub fn nodename(&self) -> &str {
unsafe {
CStr::from_ptr(self.0.nodename.as_ptr())
.to_str().expect("nodename not valid utf8 (should never happen)")
}
}
pub fn release(&self) -> &str {
unsafe {
CStr::from_ptr(self.0.release.as_ptr())
.to_str().expect("release not valid utf8 (should never happen)")
}
}
pub fn version(&self) -> &str {
unsafe {
CStr::from_ptr(self.0.version.as_ptr())
.to_str().expect("version not valid utf8 (should never happen)")
}
}
pub fn machine(&self) -> &str {
unsafe {
CStr::from_ptr(self.0.machine.as_ptr())
.to_str().expect("machine not valid utf8 (should never happen)")
}
}
}
pub fn uname() -> UtsName {
unsafe {
let mut raw: libc::utsname = mem::zeroed();
let res = libc::uname(&mut raw as *mut libc::utsname);
if res == 0 {
UtsName(raw)
} else {
panic!("uname function errored (this shouldn't happen)")
}
}
}
pub(crate) unsafe fn alpm_list_to_vec<T, F>(raw: *const alpm_list_t, f: F) -> Vec<T>
where F: Fn(*const libc::c_void) -> T
{
let mut vec = Vec::new();
if raw.is_null() {
return vec;
}
let first = raw;
let mut raw = raw;
vec.push(f((*raw).data as *const libc::c_void));
while ! (*raw).next.is_null() {
raw = (*raw).next;
vec.push(f((*raw).data as *const libc::c_void));
}
vec
}
pub(crate) unsafe fn vec_to_alpm_list<T, F>(v: Vec<T>, f: F) -> *const alpm_list_t
where F: Fn(&T) -> *const libc::c_void
{
if v.len() == 0 {
return ptr::null();
}
let list = libc::calloc(1, mem::size_of::<alpm_list_t>()) as *const alpm_list_t;
let mut list_prev = list as *mut alpm_list_t;
(*list_prev).data = f(&v[0]);
for el in v.iter().skip(1) {
let mut list_inner = libc::calloc(1, mem::size_of::<alpm_list_t>()) as *mut alpm_list_t;
(*list_inner).data = f(el);
(*list_inner).prev = list_prev;
(*list_prev).next = list_inner;
list_prev = list_inner;
}
list
}
pub(crate) unsafe fn vec_as_alpm_list<T, F>(v: &Vec<T>, f: F) -> *const alpm_list_t
where F: Fn(&T) -> *const libc::c_void
{
if v.len() == 0 {
return ptr::null();
}
let list = libc::calloc(1, mem::size_of::<alpm_list_t>()) as *const alpm_list_t;
let mut list_prev = list as *mut alpm_list_t;
(*list_prev).data = f(&v[0]);
for el in v.iter().skip(1) {
let mut list_inner = libc::calloc(1, mem::size_of::<alpm_list_t>()) as *mut alpm_list_t;
(*list_inner).data = f(el);
(*list_inner).prev = list_prev;
(*list_prev).next = list_inner;
list_prev = list_inner;
}
list
}
pub(crate) unsafe fn str_to_unowned_char_array(s: *const &str) -> *const libc::c_void {
let len = (*s).len();
let mut p = libc::calloc(len + 1, mem::size_of::<libc::c_char>()) as *mut libc::c_char;
ptr::copy::<libc::c_char>((*s).as_ptr() as *const i8, p, len);
p as *const libc::c_void
}
pub(crate) fn cstring_to_owned_char_array(s: &CString) -> *const libc::c_void {
s.as_ptr() as *const libc::c_void
}
#[deprecated]
pub fn get_servers(path: &str, repo: &str, arch: &str) -> Vec<String> {
use std::fs::File;
use std::io::{BufRead, BufReader};
let mut servers = Vec::new();
let file = BufReader::new(File::open(path).unwrap());
for line in file.lines() {
let line = line.unwrap();
if line.starts_with("Server = ") {
let server = line
.replace("$repo", repo)
.replace("$arch", arch)
.replace("Server = ", "");
servers.push(server);
}
}
servers
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_uname() {
let info = uname();
assert_eq!(info.sysname(), "Linux");
}
}