From 849002feb0548dbc6b7b925bffd6ceef275edb3b Mon Sep 17 00:00:00 2001 From: Olaf Wintermann Date: Tue, 23 Jun 2026 21:21:01 +0200 Subject: [PATCH] extend dav Resource API --- dav-rs/src/dav/resource.rs | 63 +++++++++++++++++++++++++++++++++++++- 1 file changed, 62 insertions(+), 1 deletion(-) diff --git a/dav-rs/src/dav/resource.rs b/dav-rs/src/dav/resource.rs index 084e5e7..e0e6485 100644 --- a/dav-rs/src/dav/resource.rs +++ b/dav-rs/src/dav/resource.rs @@ -27,10 +27,11 @@ */ #![allow(dead_code)] -use std::ffi::{c_char, c_int, c_long, c_void, CString}; +use std::ffi::{c_char, c_int, c_long, c_void, CStr, CString}; use std::io::{Read, Seek, SeekFrom, Write}; use std::marker::PhantomData; use std::ops::Deref; +use std::time::{Duration, SystemTime, UNIX_EPOCH}; use libc::size_t; use crate::dav::context::DavContext; use crate::dav::ffi; @@ -62,8 +63,68 @@ impl<'a> Deref for Resource<'a> { } } +fn cstr2str(cstr: *const c_char) -> String { + if cstr.is_null() { + String::new() + } else { + unsafe { + CStr::from_ptr(cstr) + .to_string_lossy() + .into_owned() + } + } +} impl<'a> ResourceRef<'a> { + + pub fn name(&self) -> String { + unsafe { + cstr2str((*self.ptr).name) + } + } + + pub fn path(&self) -> String { + unsafe { + cstr2str((*self.ptr).path) + } + } + + pub fn href(&self) -> String { + unsafe { + cstr2str((*self.ptr).href) + } + } + + pub fn content_type(&self) -> String { + unsafe { + cstr2str((*self.ptr).contenttype) + } + } + + pub fn content_length(&self) -> usize { + unsafe { + (*self.ptr).contentlength as usize + } + } + + pub fn is_collection(&self) -> bool { + unsafe { + (*self.ptr).iscollection != 0 + } + } + + pub fn lastmodified(&self) -> SystemTime { + unsafe { + UNIX_EPOCH + Duration::from_secs((*self.ptr).lastmodified as u64) + } + } + + pub fn creationdate(&self) -> SystemTime { + unsafe { + UNIX_EPOCH + Duration::from_secs((*self.ptr).lastmodified as u64) + } + } + fn resource_ref(&self, ptr: *mut ffi::DavResource) -> Option> { if ptr.is_null() { return None; -- 2.52.0