Finished up Read directory

This commit is contained in:
2021-11-27 16:22:12 +01:00
parent 03844f85d2
commit ade1518d48

View File

@@ -125,10 +125,10 @@ pub struct SmbFile<'a: 'b, 'b> {
fd: *mut SMBCFILE,
}
pub struct SmbDirectory<'a: 'b, 'b> {
smbc: &'b SmbClient<'a>,
pub fd: *mut smbc_dirent,
}
// pub struct SmbDirectory<'a: 'b, 'b> {
// smbc: &'b SmbClient<'a>,
// pub dir_struct: Vec<&str>,
// }
// 1}}}
/// Default (dummy) credential `WORKGROUP\guest` with empty password
@@ -213,7 +213,7 @@ impl<'a> SmbClient<'a> {
/// Opens [`SmbFile`](struct.SmbFile.html) defined by SMB `path` with `options`.
///
/// See [OpenOptions](struct.OpenOptions.html).
/// See [OpenOptions](struct.OpenOptions.html).Vec<smbc_dirent>
pub fn open_with<'b, P: AsRef<str>>(
&'b self,
path: P,
@@ -238,58 +238,39 @@ impl<'a> SmbClient<'a> {
Ok(SmbFile { smbc: &self, fd })
}
/// READ DIR
///
/// See [OpenOptions](struct.OpenOptions.html).
pub fn read_dir<'b, P: AsRef<str>>(&'b self,path: P) -> Result<SmbDirectory<'a, 'b>>{
let read_dir_fn = self.get_fn(smbc_getFunctionReaddir)?;
let open_dir_fn = self.get_fn(smbc_getFunctionOpendir)?;
let path = cstring(path)?;
trace!(target: "smbc", "opening {:?} with {:?}", path, read_dir_fn);
let dir = result_from_ptr_mut(open_dir_fn(self.ctx, path.as_ptr()))?;
let fd = result_from_ptr_mut(read_dir_fn(self.ctx, dir))?;
if (fd as i64) < 0 {
trace!(target: "smbc", "neg fd");
}
Ok(SmbDirectory { smbc: &self, fd })
}
/// READ DIR
///
/// See [OpenOptions](struct.OpenOptions.html).
pub fn read_dirents<'b, P: AsRef<str>>(&'b self,path: P) -> Result<SmbDirectory<'a, 'b>>{
pub fn read_directory<'b, P: AsRef<str>>(&'b self,path: P) -> Result<Vec<String>>{
let read_dir_fn = self.get_fn(smbc_getFunctionReaddir)?;
let open_dir_fn = self.get_fn(smbc_getFunctionOpendir)?;
let get_dents_fn = self.get_fn(smbc_getFunctionGetdents)?;
let path = cstring(path)?;
trace!(target: "smbc", "opening {:?} with {:?}", path, read_dir_fn);
let dir = result_from_ptr_mut(open_dir_fn(self.ctx, path.as_ptr()))?;
let fd = result_from_ptr_mut(read_dir_fn(self.ctx, dir)).expect("Can't read file");
let _fd = result_from_ptr_mut(read_dir_fn(self.ctx, dir)).expect("Can't read file");
let _fd = result_from_ptr_mut(read_dir_fn(self.ctx, dir)).expect("Can't read file");
let _fd = result_from_ptr_mut(read_dir_fn(self.ctx, dir)).expect("Can't read file");
let _fd = result_from_ptr_mut(read_dir_fn(self.ctx, dir)).expect("Can't read file");
let _fd = result_from_ptr_mut(read_dir_fn(self.ctx, dir)).expect("Can't read file");
let _fd = result_from_ptr_mut(read_dir_fn(self.ctx, dir)).is_err();
println!("{}", _fd);
//let mut return_dirs= HashSet::new();
let mut return_dirs= Vec::new();
//Returns number of read bytes
//let out_test = get_dents_fn(self.ctx, dir, fd, 200);
if (fd as i64) < 0 {
trace!(target: "smbc", "neg fd");
}
// let out_test = get_dents_fn(self.ctx, dir, fd, 2);
// let out_test = get_dents_fn(self.ctx, dir, fd, 2);
//println!("{}", out_test);
use std::ffi::CStr;
loop {
match result_from_ptr_mut(read_dir_fn(self.ctx, dir)) {
Ok(dir_struct) => {
print!("Success: ");
unsafe{
let c_str: &CStr = CStr::from_ptr(&(*dir_struct).name[0]) ;
println!("{}", c_str.to_str().unwrap());
let test = Cow::Borrowed(c_str.to_str().unwrap()).into_owned();
return_dirs.push(test);
}
}
Err(_) => break
};
};
Ok(SmbDirectory { smbc: &self, fd })
Ok(return_dirs )
}
/// Open read-only [`SmbFile`](struct.SmbFile.html) defined by SMB `path`.