use crate::dav::context::DavContext;
use crate::dav::ffi;
use crate::dav::ffi::{DavReadFunc, DavSeekFunc, DavWriteFunc};
-use crate::dav::session::Session;
+use crate::dav::session::{get_session_error, DavError, Session};
pub struct Resource<'a> {
pub base: ResourceRef<'a>
}
}
- pub fn exists(&self) -> bool {
- unsafe { dav_resource_exists(self.ptr) != 0 }
+ pub fn exists(&self) -> Result<bool, DavError> {
+ let res = unsafe { dav_resource_exists(self.ptr) != 0 };
+ if res {
+ Ok(res)
+ } else {
+ let err = unsafe { get_session_error((*self.ptr).session) };
+ match err {
+ DavError::NotFound => Ok(false),
+ _ => Err(err),
+ }
+ }
}
- pub fn create(&self) -> bool {
- unsafe { dav_resource_create(self.ptr) == 0 }
+ pub fn create(&self) -> Result<(), DavError> {
+ unsafe {
+ if dav_resource_create(self.ptr) == 0 {
+ Ok(())
+ } else {
+ Err(get_session_error((*self.ptr).session))
+ }
+ }
}
- pub fn delete(&self) -> bool {
- unsafe { dav_resource_delete(self.ptr) == 0 }
+ pub fn delete(&self) -> Result<(), DavError> {
+ unsafe {
+ if dav_resource_delete(self.ptr) == 0 {
+ Ok(())
+ } else {
+ Err(get_session_error((*self.ptr).session))
+ }
+ }
}
- pub fn load(&self) -> bool {
- unsafe { dav_load(self.ptr) == 0 }
+ pub fn load(&self) -> Result<(), DavError> {
+ unsafe {
+ if dav_load(self.ptr) == 0 {
+ Ok(())
+ } else {
+ Err(get_session_error((*self.ptr).session))
+ }
+ }
}
- pub fn store(&self) -> bool {
- unsafe { dav_store(self.ptr) == 0 }
+ pub fn store(&self) -> Result<(), DavError> {
+ unsafe {
+ if dav_store(self.ptr) == 0 {
+ Ok(())
+ } else {
+ Err(get_session_error((*self.ptr).session))
+ }
+ }
}
- pub fn store_with_data(&self, data: &[u8]) -> bool {
+ pub fn store_with_data(&self, data: &[u8]) -> Result<(), DavError> {
unsafe {
dav_set_content_data(
self.ptr,
data.len() as size_t,
);
- let res = dav_store(self.ptr);
-
- res == 0
+ if dav_store(self.ptr) == 0 {
+ Ok(())
+ } else {
+ Err(get_session_error((*self.ptr).session))
+ }
}
}
- pub fn store_with_seekable_stream<T: Read + Seek>(&self, stream: &mut T) -> bool {
+ pub fn store_with_seekable_stream<T: Read + Seek>(&self, stream: &mut T) -> Result<(), DavError> {
let mut wrapper = DavSeekableInputStream { inner: stream };
unsafe {
Some(seek_callback::<T>),
);
- let res = dav_store(self.ptr);
-
- res == 0
+ if dav_store(self.ptr) == 0 {
+ Ok(())
+ } else {
+ Err(get_session_error((*self.ptr).session))
+ }
}
}
- pub fn store_with_stream<T: Read>(&self, stream: &mut T) -> bool {
+ pub fn store_with_stream<T: Read>(&self, stream: &mut T) -> Result<(), DavError> {
let mut wrapper = DavInputStream { inner: stream };
unsafe {
None,
);
- let res = dav_store(self.ptr);
-
- res == 0
+ if dav_store(self.ptr) == 0 {
+ Ok(())
+ } else {
+ Err(get_session_error((*self.ptr).session))
+ }
}
}
- pub fn get_content<T: Write>(&self, stream: &mut T) -> bool {
+ pub fn get_content<T: Write>(&self, stream: &mut T) -> Result<(), DavError> {
let mut wrapper = DavOutputStream { inner: stream };
-
- let result = unsafe {
- dav_get_content(
- self.ptr,
- &mut wrapper as *mut _ as *mut c_void,
- Some(write_callback::<T>),
- )
- };
-
- result == 0
+ unsafe {
+ let result = dav_get_content(
+ self.ptr,
+ &mut wrapper as *mut _ as *mut c_void,
+ Some(write_callback::<T>));
+ if result == 0 {
+ Ok(())
+ } else {
+ Err(get_session_error((*self.ptr).session))
+ }
+ }
}
- pub fn lock(&self) -> bool {
- unsafe { dav_lock(self.ptr) == 0 }
+ pub fn lock(&self) -> Result<(), DavError> {
+ unsafe {
+ if dav_lock(self.ptr) == 0 {
+ Ok(())
+ } else {
+ Err(get_session_error((*self.ptr).session))
+ }
+ }
}
- pub fn lock_t(&self, timeout: u64) -> bool {
- unsafe { dav_lock_t(self.ptr, timeout as libc::time_t) == 0 }
+ pub fn lock_t(&self, timeout: u64) -> Result<(), DavError> {
+ unsafe {
+ if dav_lock_t(self.ptr, timeout as libc::time_t) == 0 {
+ Ok(())
+ } else {
+ Err(get_session_error((*self.ptr).session))
+ }
+ }
}
- pub fn unlock(&self) -> bool {
- unsafe { dav_unlock(self.ptr) == 0 }
+ pub fn unlock(&self) -> Result<(), DavError> {
+ unsafe {
+ if dav_unlock(self.ptr) == 0 {
+ Ok(())
+ } else {
+ Err(get_session_error((*self.ptr).session))
+ }
+ }
}
}
*/
#![allow(dead_code)]
-use std::ffi::{c_char, CString};
+use std::ffi::{c_char, CStr, CString};
use crate::dav::context::DavContext;
use crate::dav::ffi;
dav_session_set_auth(self.ptr, user_cstr.as_ptr(), password_cstr.as_ptr());
}
}
+
+ pub fn get_error(&self) -> DavError {
+ get_session_error(self.ptr)
+ }
}
}
}
+pub fn get_session_error(sn: *const ffi::DavSession) -> DavError {
+ let err = unsafe { dav_session_get_error(sn) };
+ let errstr = unsafe { dav_session_get_errorstr(sn) };
+
+ match err {
+ ffi::DavError::DavOk => DavError::Ok,
+ ffi::DavError::DavError => {
+ let str = if errstr.is_null() {
+ String::new()
+ } else {
+ unsafe {
+ CStr::from_ptr(errstr)
+ .to_string_lossy()
+ .into_owned()
+ }
+ };
+ DavError::Error(str)
+ },
+ ffi::DavError::DavNotFound => DavError::NotFound,
+ ffi::DavError::DavUnauthorized => DavError::Unauthorized,
+ ffi::DavError::DavForbidden => DavError::Forbidden,
+ ffi::DavError::DavMethodNotAllowed => DavError::MethodNotAllowed,
+ ffi::DavError::DavConflict => DavError::Conflict,
+ ffi::DavError::DavLocked => DavError::Locked,
+ ffi::DavError::DavUnsupportedProtocol => DavError::UnsupportedProtocol,
+ ffi::DavError::DavCouldntResolveProxy => DavError::CouldntResolveProxy,
+ ffi::DavError::DavCouldntResolveHost => DavError::CouldntResolveHost,
+ ffi::DavError::DavCouldntConnect => DavError::CouldntConnect,
+ ffi::DavError::DavTimeout => DavError::Timeout,
+ ffi::DavError::DavSslError => DavError::SslError,
+ ffi::DavError::DavQlError => DavError::QlError,
+ ffi::DavError::DavContentVerificationError => DavError::ContentVerificationError,
+ ffi::DavError::DavPreconditionFailed => DavError::PreconditionFailed,
+ ffi::DavError::DavRequestEntityTooLarge => DavError::RequestEntityTooLarge,
+ ffi::DavError::DavRequestUrlTooLong => DavError::RequestUrlTooLong,
+ ffi::DavError::DavProxyAuthRequired => DavError::ProxyAuthRequired,
+ ffi::DavError::DavNetAuthRequired => DavError::NetAuthRequired,
+ }
+}
+
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub enum DavError {
+ Ok,
+ Error(String),
+ NotFound,
+ Unauthorized,
+ Forbidden,
+ MethodNotAllowed,
+ Conflict,
+ Locked,
+ UnsupportedProtocol,
+ CouldntResolveProxy,
+ CouldntResolveHost,
+ CouldntConnect,
+ Timeout,
+ SslError,
+ QlError,
+ ContentVerificationError,
+ PreconditionFailed,
+ RequestEntityTooLarge,
+ RequestUrlTooLong,
+ ProxyAuthRequired,
+ NetAuthRequired,
+}
unsafe extern "C" {
fn dav_session_new(ctx: *mut ffi::DavContext, base_url: *const libc::c_char) -> *mut ffi::DavSession;
fn dav_session_unref(sn: *mut ffi::DavSession);
fn dav_session_clone(sn: *mut ffi::DavSession) -> *mut ffi::DavSession;
fn dav_session_set_auth(sn: *mut ffi::DavSession, user: *const c_char, password: *const c_char);
+
+ fn dav_session_get_error(sn: *const ffi::DavSession) -> ffi::DavError;
+ fn dav_session_get_errorstr(sn: *const ffi::DavSession) -> *const c_char;
}
\ No newline at end of file