diff --git a/.gitignore b/.gitignore index 87895a9..761daa9 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,5 @@ Cargo.lock .idea *.iml +apt-c/lib.o +apt-c/libapt-c.a diff --git a/Cargo.toml b/Cargo.toml index eb8c644..ff6ee38 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,6 +4,4 @@ version = "0.1.0" authors = ["Chris West (Faux) "] [dependencies] - -[build-dependencies] -bindgen = "0.26.3" \ No newline at end of file +libc = "0.2.26" diff --git a/apt-c/Makefile b/apt-c/Makefile new file mode 100644 index 0000000..6da553a --- /dev/null +++ b/apt-c/Makefile @@ -0,0 +1,11 @@ +CXXFLAGS=-Wall -Wextra + +all: libapt-c.a + +lib.o: lib.cpp lib.h + +libapt-c.a: lib.o + ar rcs $@ $< + +clean: + $(RM) libapt-c.a lib.o diff --git a/apt-c/lib.cpp b/apt-c/lib.cpp new file mode 100644 index 0000000..1e61959 --- /dev/null +++ b/apt-c/lib.cpp @@ -0,0 +1,43 @@ +#include +#include + +struct PCache { + pkgCacheFile *cache_file; + pkgCache *cache; +}; + +extern "C" { + PCache *get_pkg_cache(); + void free_pkg_cache(PCache *cache); + + int iterate_all_packages(PCache *cache, int (*visit)(const char*)); +} + +PCache *get_pkg_cache() { + pkgInitConfig(*_config); + pkgInitSystem(*_config, _system); + + pkgCacheFile *cache_file = new pkgCacheFile(); + pkgCache *cache = cache_file->GetPkgCache(); + + PCache *ret = new PCache(); + ret->cache_file = cache_file; + ret->cache = cache; + + return ret; +} + +void free_pkg_cache(PCache *cache) { + delete cache->cache_file; + delete cache; +} + +int iterate_all_packages(PCache *cache, int (*visit)(const char*)) { + for (pkgCache::PkgIterator iter = cache->cache->PkgBegin(); iter != cache->cache->PkgEnd(); ++iter) { + if (!visit(iter.Name())) { + return false; + } + } + + return true; +} diff --git a/apt-c/lib.h b/apt-c/lib.h new file mode 100644 index 0000000..e69de29 diff --git a/build.rs b/build.rs index 44a5d05..66eaa46 100644 --- a/build.rs +++ b/build.rs @@ -1,20 +1,3 @@ -extern crate bindgen; - -use std::env; -use std::path::PathBuf; - fn main() { - // command to cargo: - println!("cargo:rustc-link-lib=apt-pkg"); - - let bindings = bindgen::Builder::default() - .header("wrapper.hpp") - .whitelisted_type("URI") - .generate() - .expect("Unable to generate bindings"); - - let out_path = PathBuf::from(env::var("OUT_DIR").unwrap()); - bindings - .write_to_file(out_path.join("bindings.rs")) - .expect("Couldn't write bindings!"); + println!("cargo:rustc-link-search=apt-c") } diff --git a/src/lib.rs b/src/lib.rs index c54f278..18deb88 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,10 +1,26 @@ -mod raw; +extern crate libc; + +pub mod raw; #[cfg(test)] mod tests { use super::*; + use std::ffi::CStr; + + extern fn print_arg(arg: *const libc::c_char) -> libc::c_int { + unsafe { + println!("{:?}", CStr::from_ptr(arg).to_str()); + return 1; + } + } + #[test] fn it_works() { + unsafe { + let cache = raw::get_pkg_cache(); + println!("{:?}", raw::iterate_all_packages(cache, print_arg)); + raw::free_pkg_cache(cache); + } } } diff --git a/src/raw.rs b/src/raw.rs index a38a13a..e3c97db 100644 --- a/src/raw.rs +++ b/src/raw.rs @@ -1,5 +1,16 @@ -#![allow(non_upper_case_globals)] -#![allow(non_camel_case_types)] -#![allow(non_snake_case)] +use libc::c_void; +use libc::c_char; +use libc::c_int; -include!(concat!(env!("OUT_DIR"), "/bindings.rs")); +#[link(name = "apt-c")] +#[link(name = "apt-pkg")] +#[link(name = "stdc++")] +extern { + pub fn get_pkg_cache() -> *mut c_void; + pub fn free_pkg_cache(cache: *mut c_void); + + pub fn iterate_all_packages( + cache: *mut c_void, + visit: extern fn(name: *const c_char) -> c_int, + ) -> c_int; +} diff --git a/wrapper.hpp b/wrapper.hpp deleted file mode 100644 index c9b38e7..0000000 --- a/wrapper.hpp +++ /dev/null @@ -1,4 +0,0 @@ -#define APT_8_CLEANER_HEADERS -#define APT_9_CLEANER_HEADERS -#define APT_10_CLEANER_HEADERS -#include \ No newline at end of file