list packages (after manual make)

This commit is contained in:
Chris West (Faux) 2017-07-12 21:18:34 +01:00
parent 39d8438d3a
commit da91ba6908
9 changed files with 90 additions and 30 deletions

2
.gitignore vendored
View File

@ -3,3 +3,5 @@
Cargo.lock Cargo.lock
.idea .idea
*.iml *.iml
apt-c/lib.o
apt-c/libapt-c.a

View File

@ -4,6 +4,4 @@ version = "0.1.0"
authors = ["Chris West (Faux) <git@goeswhere.com>"] authors = ["Chris West (Faux) <git@goeswhere.com>"]
[dependencies] [dependencies]
libc = "0.2.26"
[build-dependencies]
bindgen = "0.26.3"

11
apt-c/Makefile Normal file
View File

@ -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

43
apt-c/lib.cpp Normal file
View File

@ -0,0 +1,43 @@
#include <apt-pkg/pkgcache.h>
#include <apt-pkg/cachefile.h>
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;
}

0
apt-c/lib.h Normal file
View File

View File

@ -1,20 +1,3 @@
extern crate bindgen;
use std::env;
use std::path::PathBuf;
fn main() { fn main() {
// command to cargo: println!("cargo:rustc-link-search=apt-c")
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!");
} }

View File

@ -1,10 +1,26 @@
mod raw; extern crate libc;
pub mod raw;
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; 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] #[test]
fn it_works() { fn it_works() {
unsafe {
let cache = raw::get_pkg_cache();
println!("{:?}", raw::iterate_all_packages(cache, print_arg));
raw::free_pkg_cache(cache);
}
} }
} }

View File

@ -1,5 +1,16 @@
#![allow(non_upper_case_globals)] use libc::c_void;
#![allow(non_camel_case_types)] use libc::c_char;
#![allow(non_snake_case)] 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;
}

View File

@ -1,4 +0,0 @@
#define APT_8_CLEANER_HEADERS
#define APT_9_CLEANER_HEADERS
#define APT_10_CLEANER_HEADERS
#include <apt-pkg/strutl.h>