Merge pull request #2 from purpleP/cleanup/2020-10-28

Update for rust 1.42+ (std Error api change, ? operator,  unneeded `unsafe`s)
This commit is contained in:
Konstantin Gribov
2021-01-24 22:33:14 +03:00
committed by GitHub
3 changed files with 30 additions and 45 deletions

View File

@@ -28,7 +28,6 @@ pub type Result<T> = result::Result<T, Error>;
pub enum Error { pub enum Error {
NewContext(io::Error), NewContext(io::Error),
InitContext(io::Error), InitContext(io::Error),
AuthCallbackPaniced(Box<error::Error>),
NulInPath(ffi::NulError), NulInPath(ffi::NulError),
Io(io::Error), Io(io::Error),
} }
@@ -40,29 +39,18 @@ impl fmt::Display for Error {
Error::InitContext(ref err) => write!(f, "Init context error: {}", err), Error::InitContext(ref err) => write!(f, "Init context error: {}", err),
Error::Io(ref err) => write!(f, "IO error: {}", err), Error::Io(ref err) => write!(f, "IO error: {}", err),
Error::NulInPath(ref err) => write!(f, "NUL in path: {}", err), Error::NulInPath(ref err) => write!(f, "NUL in path: {}", err),
Error::AuthCallbackPaniced(ref err) => write!(f, "Auth callback paniced last time: {}", err)
} }
} }
} }
impl error::Error for Error { impl error::Error for Error {
fn description(&self) -> &str {
match *self {
Error::NewContext(ref err) => err.description(),
Error::InitContext(ref err) => err.description(),
Error::Io(ref err) => err.description(),
Error::NulInPath(ref err) => err.description(),
Error::AuthCallbackPaniced(ref _err) => "panic in auth callback",
}
}
fn cause(&self) -> Option<&error::Error> { fn source(&self) -> Option<&(dyn error::Error + 'static)> {
match *self { match *self {
Error::NewContext(ref err) => Some(err), Error::NewContext(ref err) => Some(err),
Error::InitContext(ref err) => Some(err), Error::InitContext(ref err) => Some(err),
Error::Io(ref err) => Some(err), Error::Io(ref err) => Some(err),
Error::NulInPath(ref err) => Some(err), Error::NulInPath(ref err) => Some(err),
Error::AuthCallbackPaniced(ref err) => Some(err.as_ref()),
} }
} }
} }

View File

@@ -80,7 +80,7 @@ const SMBC_TRUE: smbc_bool = 1;
pub struct SmbClient<'a> { pub struct SmbClient<'a> {
ctx: *mut SMBCCTX, ctx: *mut SMBCCTX,
#[allow(dead_code)] #[allow(dead_code)]
auth_fn: &'a for<'b> Fn(&'b str, &'b str) -> (Cow<'a, str>, Cow<'a, str>, Cow<'a, str>), auth_fn: &'a dyn for<'b> Fn(&'b str, &'b str) -> (Cow<'a, str>, Cow<'a, str>, Cow<'a, str>),
} }
// {{{2 // {{{2
@@ -148,7 +148,7 @@ impl<'a> SmbClient<'a> {
}; };
unsafe { unsafe {
let ctx = try!(result_from_ptr_mut(smbc_new_context())); let ctx = result_from_ptr_mut(smbc_new_context())?;
smbc_setOptionUserData(ctx, auth_fn as *const _ as *mut c_void); smbc_setOptionUserData(ctx, auth_fn as *const _ as *mut c_void);
smbc_setFunctionAuthDataWithContext(ctx, Some(Self::auth_wrapper::<F>)); smbc_setFunctionAuthDataWithContext(ctx, Some(Self::auth_wrapper::<F>));
@@ -158,7 +158,7 @@ impl<'a> SmbClient<'a> {
smbc_setOptionDebugToStderr(ctx, SMBC_TRUE); smbc_setOptionDebugToStderr(ctx, SMBC_TRUE);
//smbc_setDebug(ctx, 10); //smbc_setDebug(ctx, 10);
smbc.ctx = try!(result_from_ptr_mut(smbc_init_context(ctx))); smbc.ctx = result_from_ptr_mut(smbc_init_context(ctx))?;
} }
trace!(target: "smbc", "new smbclient"); trace!(target: "smbc", "new smbclient");
@@ -208,22 +208,20 @@ impl<'a> SmbClient<'a> {
let open_fn = try_ufn!(smbc_getFunctionOpen <- self); let open_fn = try_ufn!(smbc_getFunctionOpen <- self);
let path = try!(cstring(path)); let path = cstring(path)?;
trace!(target: "smbc", "opening {:?} with {:?}", path, open_fn); trace!(target: "smbc", "opening {:?} with {:?}", path, open_fn);
unsafe { let fd = result_from_ptr_mut(open_fn(self.ctx,
let fd = try!(result_from_ptr_mut(open_fn(self.ctx, path.as_ptr(),
path.as_ptr(), options.to_flags()?,
try!(options.to_flags()), options.mode))?;
options.mode))); if (fd as i64) < 0 {
if (fd as i64) < 0 { trace!(target: "smbc", "neg fd");
trace!(target: "smbc", "neg fd");
}
Ok(SmbFile {
smbc: &self,
fd: fd,
})
} }
Ok(SmbFile {
smbc: &self,
fd: fd,
})
} }
/// Open read-only [`SmbFile`](struct.SmbFile.html) defined by SMB `path`. /// Open read-only [`SmbFile`](struct.SmbFile.html) defined by SMB `path`.
@@ -272,17 +270,16 @@ impl<'a> SmbClient<'a> {
#[doc(hidden)] #[doc(hidden)]
/// Get metadata for file at `path` /// Get metadata for file at `path`
pub fn metadata<P: AsRef<str>>(&self, path: P) -> Result<()> { pub fn metadata<P: AsRef<str>>(&self, path: P) -> Result<()> {
let stat_fn = try_ufn!(smbc_getFunctionStat <- self); let _stat_fn = try_ufn!(smbc_getFunctionStat <- self);
let path = try!(cstring(path)); let _path = cstring(path)?;
unimplemented!(); unimplemented!();
} }
/// Create new directory at SMB `path` /// Create new directory at SMB `path`
pub fn create_dir<P: AsRef<str>>(&self, path: P) -> Result<()> { pub fn create_dir<P: AsRef<str>>(&self, path: P) -> Result<()> {
let mkdir_fn = try_ufn!(smbc_getFunctionMkdir <- self); let mkdir_fn = try_ufn!(smbc_getFunctionMkdir <- self);
let path = try!(cstring(path)); let path = cstring(path)?;
try!(to_result_with_le(unsafe { mkdir_fn(self.ctx, path.as_ptr(), 0o755) })); to_result_with_le(mkdir_fn(self.ctx, path.as_ptr(), 0o755))?;
Ok(()) Ok(())
} }
@@ -295,8 +292,8 @@ impl<'a> SmbClient<'a> {
/// Directory should be empty to delete it. /// Directory should be empty to delete it.
pub fn remove_dir<P: AsRef<str>>(&self, path: P) -> Result<()> { pub fn remove_dir<P: AsRef<str>>(&self, path: P) -> Result<()> {
let rmdir_fn = try_ufn!(smbc_getFunctionRmdir <- self); let rmdir_fn = try_ufn!(smbc_getFunctionRmdir <- self);
let path = try!(cstring(path)); let path = cstring(path)?;
try!(to_result_with_le(unsafe { rmdir_fn(self.ctx, path.as_ptr()) })); to_result_with_le(rmdir_fn(self.ctx, path.as_ptr()))?;
Ok(()) Ok(())
} }
} // 2}}} } // 2}}}
@@ -426,12 +423,12 @@ impl<'a, 'b> Read for SmbFile<'a, 'b> {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
trace!(target: "smbc", "reading file to buf [{:?};{}]", buf.as_ptr(), buf.len()); trace!(target: "smbc", "reading file to buf [{:?};{}]", buf.as_ptr(), buf.len());
let read_fn = try_ufn!(smbc_getFunctionRead <- self.smbc); let read_fn = try_ufn!(smbc_getFunctionRead <- self.smbc);
let bytes_read = try!(to_result_with_le(unsafe { let bytes_read = to_result_with_le(
read_fn(self.smbc.ctx, read_fn(self.smbc.ctx,
self.fd, self.fd,
buf.as_mut_ptr() as *mut c_void, buf.as_mut_ptr() as *mut c_void,
buf.len() as _) buf.len() as _)
})); )?;
Ok(bytes_read as usize) Ok(bytes_read as usize)
} }
} // }}} } // }}}
@@ -441,12 +438,12 @@ impl<'a, 'b> Write for SmbFile<'a, 'b> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> { fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
trace!(target: "smbc", "writing buf [{:?};{}] to file", buf.as_ptr(), buf.len()); trace!(target: "smbc", "writing buf [{:?};{}] to file", buf.as_ptr(), buf.len());
let write_fn = try_ufn!(smbc_getFunctionWrite <- self.smbc); let write_fn = try_ufn!(smbc_getFunctionWrite <- self.smbc);
let bytes_wrote = try!(to_result_with_le(unsafe { let bytes_wrote = to_result_with_le(
write_fn(self.smbc.ctx, write_fn(self.smbc.ctx,
self.fd, self.fd,
buf.as_ptr() as *const c_void, buf.as_ptr() as *const c_void,
buf.len() as _) buf.len() as _)
})); )?;
Ok(bytes_wrote as usize) Ok(bytes_wrote as usize)
} }
@@ -466,7 +463,7 @@ impl<'a, 'b> Seek for SmbFile<'a, 'b> {
SeekFrom::End(p) => (libc::SEEK_END, p as off_t), SeekFrom::End(p) => (libc::SEEK_END, p as off_t),
SeekFrom::Current(p) => (libc::SEEK_CUR, p as off_t), SeekFrom::Current(p) => (libc::SEEK_CUR, p as off_t),
}; };
let res = try!(to_result_with_errno(unsafe { lseek_fn(self.smbc.ctx, self.fd, off, whence) }, libc::EINVAL)); let res = to_result_with_errno(lseek_fn(self.smbc.ctx, self.fd, off, whence), libc::EINVAL)?;
Ok(res as u64) Ok(res as u64)
} }
} // }}} } // }}}

View File

@@ -16,7 +16,7 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with smbc. If not, see <http://www.gnu.org/licenses/>. // along with smbc. If not, see <http://www.gnu.org/licenses/>.
use libc::{self, c_char, c_int}; use libc::{c_char, c_int};
use std::borrow::Cow; use std::borrow::Cow;
use std::ffi::{CStr, CString}; use std::ffi::{CStr, CString};
@@ -27,9 +27,9 @@ use result::*;
/// try! get smbc function or return io::Error(EINVAL) /// try! get smbc function or return io::Error(EINVAL)
macro_rules! try_ufn { macro_rules! try_ufn {
($e:ident <- $s:expr) => (try!(unsafe { ($e:ident <- $s:expr) => (unsafe {
$e($s.ctx).ok_or($crate::std::io::Error::from_raw_os_error(libc::EINVAL as i32)) $e($s.ctx).ok_or($crate::std::io::Error::from_raw_os_error(libc::EINVAL as i32))
})) }?)
} }
#[inline(always)] #[inline(always)]
@@ -49,7 +49,7 @@ pub unsafe fn cstr<'a, T>(p: *const T) -> Cow<'a, str> {
} }
pub fn cstring<P: AsRef<str>>(p: P) -> Result<CString> { pub fn cstring<P: AsRef<str>>(p: P) -> Result<CString> {
Ok(try!(CString::new(p.as_ref()))) Ok(CString::new(p.as_ref())?)
} }
pub unsafe fn write_to_cstr(dest: *mut u8, len: usize, src: &str) { pub unsafe fn write_to_cstr(dest: *mut u8, len: usize, src: &str) {