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
use std::ffi::{CString, CStr};

#[derive(Debug)]
pub enum Error {
//    StringConversionFailed,
    FfiNullPtr(std::ffi::NulError),
    NullPtr,
}

impl From<std::ffi::NulError> for Error {
    fn from(other: std::ffi::NulError) -> Error {
        return Error::FfiNullPtr(other);
    }
}

type Result<T> = std::result::Result<T, Error>;

mod ffi {
    include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
}

#[derive(Debug)]
pub struct PathInfo {
    pub path: String,
    pub deriver: String,
    pub nar_hash: String,
    pub references: String,
    pub registration_time: i64,
    pub nar_size: u64,
    pub ultimate: bool,
    pub signatures: String,
    pub ca: String,
}

impl PathInfo {
    fn from_cpathinfo(cpi: &ffi::CPathInfo) -> Self {
        Self {
            path: unsafe { CStr::from_ptr(cpi.path).to_string_lossy().to_string() },
            deriver: unsafe { CStr::from_ptr(cpi.deriver).to_string_lossy().to_string() },
            nar_hash: unsafe { CStr::from_ptr(cpi.narHash).to_string_lossy().to_string() },
            references: unsafe { CStr::from_ptr(cpi.references).to_string_lossy().to_string() },
            registration_time: cpi.registrationTime as i64,
            nar_size: cpi.narSize,
            ultimate: cpi.ultimate != 0,
            signatures: unsafe { CStr::from_ptr(cpi.signatures).to_string_lossy().to_string() },
            ca: unsafe { CStr::from_ptr(cpi.ca).to_string_lossy().to_string() },
        }
    }
}

#[derive(Debug)]
pub struct Instance {
    instp: *mut ffi::nixstorec_instance,
}

unsafe impl Send for Instance {}
impl Drop for Instance {
    fn drop(&mut self) {
        unsafe { ffi::nixstorec_free_instance(self.instp) };
    }
}

impl Instance {
    pub fn new() -> Result<Instance> {
        let instp = unsafe {
            ffi::nixstorec_new_instance()
        };

        if instp == 0 as _ {
            return Err(Error::NullPtr);
        }

        Ok(Self{
            instp
        })
    }

    pub fn is_valid_path<T: AsRef<str>>(&mut self, path: T) -> Result<bool> {
        let path = std::ffi::CString::new(path.as_ref())?;

        let c_path = path.as_ptr();

        unsafe {
            let ret : bool = ffi::nixstorec_is_valid_path(self.instp, c_path) != 0;
            return Ok(ret);
        }
    }

    pub fn query_path_info<T: AsRef<str>>(&mut self, path: T) -> Result<Option<PathInfo>> {
        let path : CString = std::ffi::CString::new(path.as_ref())?;
        let c_path = path.as_ptr();

        let c_pathinfo_ptr = unsafe {
            ffi::nixstorec_query_path_info(self.instp, c_path)
        };

        if c_pathinfo_ptr == 0 as _ {
            return Ok(None);
        }

        let pathinfo = {
            let c_pathinfo = unsafe { *c_pathinfo_ptr };
            PathInfo::from_cpathinfo(&c_pathinfo)
        };

        unsafe { ffi::nixstorec_free_path_info(c_pathinfo_ptr); }

        return Ok(Some(pathinfo));
    }

    pub fn query_path_from_hash_part<T: AsRef<str>>(&mut self, hash_part: T) -> Result<Option<String>> {

        let hash_path_c = CString::new(hash_part.as_ref())?;

        let path_c = unsafe { ffi::nixstorec_query_path_from_hash_part(self.instp, hash_path_c.as_ptr()) };

        if path_c == 0 as _ {
            return Err(Error::NullPtr);
        }

        let path = unsafe {
            CStr::from_ptr(path_c).to_string_lossy().to_string()
        };

        unsafe { ffi::nixstorec_free(path_c as _); }

        if path.is_empty() {
            Ok(None)
        } else {
            Ok(Some(path))
        }
    }

    pub fn query_path_from_file_hash<T: AsRef<str>>(&mut self, file_hash: T) -> Result<Option<String>> {

        let file_hash_c = CString::new(file_hash.as_ref())?;

        let path_c = unsafe { ffi::nixstorec_query_path_from_file_hash(self.instp, file_hash_c.as_ptr()) };

        if path_c == 0 as _ {
            return Err(Error::NullPtr);
        }

        let path = unsafe {
            CStr::from_ptr(path_c).to_string_lossy().to_string()
        };

        let ptr = path_c as _;

        unsafe {
            ffi::nixstorec_free(ptr);
        }

        if path.is_empty() {
            Ok(None)
        } else {
            Ok(Some(path))
        }
    }
}



#[cfg(test)]
mod tests {
    #[test]
//     fn query_path_from_nar_hash() {
//         let v = super::query_path_from_nar_hash("fooooo");
//         println!("{:?}", v);
//         assert!(v.is_ok());
//     }
//
//     #[test]
//     fn query_path_from_hash_part() {
//         let v = super::query_path_from_hash_part("foooooooooo");
//         println!("{:?}", v);
//         assert!(v.is_ok());
//     }

    #[test]
    fn query_path_info() {
        for i in 0..100 {
            let mut instance = super::Instance::new().unwrap();
            let v = instance.query_path_info("testtesttest");
            println!("{:?}", v);
            assert!(v.unwrap().is_none())
        }
    }
}