Skip to content

Commit

Permalink
added quesiton callback support
Browse files Browse the repository at this point in the history
  • Loading branch information
pigeonhands committed Feb 25, 2019
1 parent 5f237a8 commit e5719cc
Show file tree
Hide file tree
Showing 11 changed files with 501 additions and 18 deletions.
2 changes: 1 addition & 1 deletion alpm-rs/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "alpm-rs"
version = "0.1.13"
version = "0.1.14"
authors = ["Sam <sam.mackenzie@live.com>"]
edition = "2018"
license="MIT"
Expand Down
44 changes: 40 additions & 4 deletions alpm-rs/src/callbacks.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,38 @@
use std::os::raw::c_char;
use std::os::raw::{c_char};
use crate::clib;
use crate::question::{alpm_question_t,QuestionArgs};

use super::{Handle,alpm_handle_t};
use crate::clib;


#[link(name="alpm")]
extern{
fn alpm_option_set_logcb(handle : *mut alpm_handle_t, f: extern fn(i32, *mut c_char, clib::VaList)) -> i32;

fn alpm_option_set_dlcb(handle : *mut alpm_handle_t,f: extern fn(filename: *mut c_char, xfered: i64, total: i64)) -> i32;

fn alpm_option_set_questioncb(handle : *mut alpm_handle_t,f: extern fn(question: *mut alpm_question_t)) -> i32;
}


/*
type LogCallback = Option<Fn(i32,String)>;
type DownloadCallback<T: Fn(&str,i64,i64)> = T;
type QuestionCallback<T: Fn(QuestionArgs)> = T;
static mut LOG_CALLBACK : Option<Box<LogCallback>> = None;
static mut DOWNLOAD_CALLBACK : Option<DownloadCallback> = None;
static mut QUESTION_CALLBACK : Option<QuestionCallback> = None;
*/


type LogCallback = fn(i32,String);
type DownloadCallback = fn(filename: &str, downloaded: i64, total: i64);
type QuestionCallback = fn(question: QuestionArgs);


static mut LOG_CALLBACK : Option<LogCallback> = None;
static mut DOWNLOAD_CALLBACK : Option<DownloadCallback> = None;
static mut QUESTION_CALLBACK : Option<QuestionCallback> = None;


extern "C" fn alpm_log_cb_handler(level: i32, fmt: *mut c_char, args: clib::VaList){
Expand Down Expand Up @@ -58,4 +74,24 @@ pub fn set_download_callback(h: &Handle, cb: DownloadCallback){
}
DOWNLOAD_CALLBACK = Some(cb);
}
}

extern "C" fn alpm_question_handler(q_raw: *mut alpm_question_t){
unsafe{
if let Some(f) = QUESTION_CALLBACK{
f(q_raw.into());
}
}
}

pub fn set_question_callback(h: &Handle, cb: QuestionCallback){
unsafe{
match QUESTION_CALLBACK{
None => {
alpm_option_set_questioncb(h.alpm_handle, alpm_question_handler);
},
_ => {},
}
QUESTION_CALLBACK = Some(cb);
}
}
2 changes: 1 addition & 1 deletion alpm-rs/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ extern crate libc;
use std::os::raw::{c_char,c_void};

use crate::package::{PackageList,alpm_pkg_t,Package};
use crate::types::{alpm_list_t, List, AlpmList, AlpmListItem, StringList };
use crate::list::{alpm_list_t, List, AlpmList, AlpmListItem, StringList };

#[link(name="alpm")]
extern {
Expand Down
24 changes: 23 additions & 1 deletion alpm-rs/src/enums.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![allow(dead_code)]

#[allow(non_camel_case_types)]

pub const ALPM_LOG_ERROR: i32 = 1;
pub const ALPM_LOG_WARNING : i32 = (1 << 1);
pub const ALPM_LOG_DEBUG : i32 = (1 << 2);
Expand All @@ -21,6 +21,28 @@ pub enum PkgFrom {
ALPM_PKG_FROM_SYNCDB
}


#[derive(Copy, Clone)]
#[allow(non_camel_case_types)]
#[repr(C)]
pub enum alpm_depmod {
_invalid,
ALPM_DEP_MOD_ANY,
ALPM_DEP_MOD_EQ,
ALPM_DEP_MOD_GE,
ALPM_DEP_MOD_LE,
ALPM_DEP_MOD_GT,
ALPM_DEP_MOD_LT
}

pub const ALPM_QUESTION_INSTALL_IGNOREPKG : i32 = (1 << 0);
pub const ALPM_QUESTION_REPLACE_PKG : i32 = (1 << 1);
pub const ALPM_QUESTION_CONFLICT_PKG : i32 = (1 << 2);
pub const ALPM_QUESTION_CORRUPTED_PKG : i32 = (1 << 3);
pub const ALPM_QUESTION_REMOVE_PKGS : i32 = (1 << 4);
pub const ALPM_QUESTION_SELECT_PROVIDER : i32 = (1 << 5);
pub const ALPM_QUESTION_IMPORT_KEY : i32 = (1 << 6);

pub const ALPM_TRANS_FLAG_NODEPS : i32 = 1;
/** Ignore file conflicts and overwrite files. */
pub const ALPM_TRANS_FLAG_FORCE : i32 = (1 << 1);
Expand Down
7 changes: 4 additions & 3 deletions alpm-rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ pub mod error;
pub mod enums;
pub mod db;
pub mod package;
pub mod types;
pub mod list;
pub mod callbacks;
pub mod question;

pub use crate::types::{List, AlpmListItem};
pub use crate::list::{List, AlpmListItem};


extern crate libc;
Expand All @@ -18,7 +19,7 @@ use std::error::Error;
use std::os::raw::c_char;

use crate::package::{Package,alpm_pkg_t};
use crate::types::{alpm_list_t};
use crate::list::{alpm_list_t};
use crate::db::{AlpmDB,alpm_db_t, DBList};
use crate::enums::ErrorNo;

Expand Down
12 changes: 6 additions & 6 deletions alpm-rs/src/types.rs → alpm-rs/src/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::marker::PhantomData;

#[link(name="alpm")]
extern {
fn alpm_list_free(list: *mut alpm_list_t);
//fn alpm_list_free(list: *mut alpm_list_t);
fn alpm_list_add(list: *mut alpm_list_t, data: *mut c_void) -> *mut alpm_list_t;
}

Expand Down Expand Up @@ -106,12 +106,12 @@ pub struct AlpmListIterator<T> {

impl<T> Drop for AlpmList<T> {
fn drop(&mut self) {
unsafe{
/* unsafe{
if self.list != std::ptr::null_mut(){
//causing undefined behaviours
// alpm_list_free(self.list);
causing undefined behaviours
alpm_list_free(self.list);
}
}
}*/
}
}

Expand Down Expand Up @@ -161,7 +161,7 @@ pub struct StringList {
alpm_list: AlpmList<StringItem>,
}

impl crate::types::List<StringItem> for StringList {
impl crate::list::List<StringItem> for StringList {
fn empty() -> Self {
StringList{
alpm_list: AlpmList::empty(),
Expand Down
11 changes: 11 additions & 0 deletions alpm-rs/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,17 @@ macro_rules! cstr {
};
}

macro_rules! cstring {
($e:expr) => {
if $e == std::ptr::null_mut() {
"".to_string()
}else{
unsafe { std::ffi::CStr::from_ptr($e).to_str().unwrap().to_string() }
}
};
}


macro_rules! to_bool {
($e:expr) => {
if $e == -1 { false } else { true }
Expand Down
2 changes: 1 addition & 1 deletion alpm-rs/src/package.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::os::raw::{c_char, c_void};

use crate::enums::PkgFrom;
use crate::types::{alpm_list_t, AlpmList,AlpmListItem};
use crate::list::{alpm_list_t, AlpmList,AlpmListItem};
use crate::db::{alpm_db_t, AlpmDB,DBList};

#[link(name="alpm")]
Expand Down
Loading

0 comments on commit e5719cc

Please sign in to comment.