Reformat with default cargo fmt
This commit is contained in:
@@ -44,7 +44,6 @@ impl fmt::Display for Error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl error::Error for Error {
|
impl error::Error for Error {
|
||||||
|
|
||||||
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
|
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
|
||||||
match *self {
|
match *self {
|
||||||
Error::NewContext(ref err) => Some(err),
|
Error::NewContext(ref err) => Some(err),
|
||||||
|
|||||||
118
src/smbc.rs
118
src/smbc.rs
@@ -26,13 +26,13 @@ use std::panic;
|
|||||||
use std::ptr;
|
use std::ptr;
|
||||||
|
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
use std::io::{Read, Write, Seek, SeekFrom};
|
use std::io::{Read, Seek, SeekFrom, Write};
|
||||||
|
|
||||||
use libc::{self, c_char, c_int, c_void, mode_t, off_t};
|
use libc::{self, c_char, c_int, c_void, mode_t, off_t};
|
||||||
|
|
||||||
|
use result::Result;
|
||||||
use smbclient_sys::*;
|
use smbclient_sys::*;
|
||||||
use util::*;
|
use util::*;
|
||||||
use result::Result;
|
|
||||||
// 1}}}
|
// 1}}}
|
||||||
|
|
||||||
const SMBC_FALSE: smbc_bool = 0;
|
const SMBC_FALSE: smbc_bool = 0;
|
||||||
@@ -127,7 +127,11 @@ pub struct SmbFile<'a: 'b, 'b> {
|
|||||||
// 1}}}
|
// 1}}}
|
||||||
|
|
||||||
/// Default (dummy) credential `WORKGROUP\guest` with empty password
|
/// Default (dummy) credential `WORKGROUP\guest` with empty password
|
||||||
const DEF_CRED: (Cow<'static, str>, Cow<'static, str>, Cow<'static, str>) = (Cow::Borrowed("WORKGROUP"), Cow::Borrowed("guest"), Cow::Borrowed(""));
|
const DEF_CRED: (Cow<'static, str>, Cow<'static, str>, Cow<'static, str>) = (
|
||||||
|
Cow::Borrowed("WORKGROUP"),
|
||||||
|
Cow::Borrowed("guest"),
|
||||||
|
Cow::Borrowed(""),
|
||||||
|
);
|
||||||
|
|
||||||
// SmbClient {{{1
|
// SmbClient {{{1
|
||||||
impl<'a> SmbClient<'a> {
|
impl<'a> SmbClient<'a> {
|
||||||
@@ -141,7 +145,9 @@ impl<'a> SmbClient<'a> {
|
|||||||
///
|
///
|
||||||
/// Should *return* tuple `(workgroup, username, password)` as a result.
|
/// Should *return* tuple `(workgroup, username, password)` as a result.
|
||||||
pub fn new<F>(auth_fn: &'a F) -> Result<SmbClient<'a>>
|
pub fn new<F>(auth_fn: &'a F) -> Result<SmbClient<'a>>
|
||||||
where F: for<'b> Fn(&'b str, &'b str) -> (Cow<'a, str>, Cow<'a, str>, Cow<'a, str>) {
|
where
|
||||||
|
F: for<'b> Fn(&'b str, &'b str) -> (Cow<'a, str>, Cow<'a, str>, Cow<'a, str>),
|
||||||
|
{
|
||||||
let mut smbc = SmbClient {
|
let mut smbc = SmbClient {
|
||||||
ctx: ptr::null_mut(),
|
ctx: ptr::null_mut(),
|
||||||
auth_fn,
|
auth_fn,
|
||||||
@@ -166,17 +172,20 @@ impl<'a> SmbClient<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Auth wrapper passed to `SMBCCTX` to authenticate requests to SMB servers.
|
/// Auth wrapper passed to `SMBCCTX` to authenticate requests to SMB servers.
|
||||||
extern "C" fn auth_wrapper<F: 'a>(ctx: *mut SMBCCTX,
|
extern "C" fn auth_wrapper<F: 'a>(
|
||||||
srv: *const c_char,
|
ctx: *mut SMBCCTX,
|
||||||
shr: *const c_char,
|
srv: *const c_char,
|
||||||
wg: *mut c_char,
|
shr: *const c_char,
|
||||||
wglen: c_int,
|
wg: *mut c_char,
|
||||||
un: *mut c_char,
|
wglen: c_int,
|
||||||
unlen: c_int,
|
un: *mut c_char,
|
||||||
pw: *mut c_char,
|
unlen: c_int,
|
||||||
pwlen: c_int)
|
pw: *mut c_char,
|
||||||
-> ()
|
pwlen: c_int,
|
||||||
where F: for<'b> Fn(&'b str, &'b str) -> (Cow<'a, str>, Cow<'a, str>, Cow<'a, str>) {
|
) -> ()
|
||||||
|
where
|
||||||
|
F: for<'b> Fn(&'b str, &'b str) -> (Cow<'a, str>, Cow<'a, str>, Cow<'a, str>),
|
||||||
|
{
|
||||||
unsafe {
|
unsafe {
|
||||||
let srv = cstr(srv);
|
let srv = cstr(srv);
|
||||||
let shr = cstr(shr);
|
let shr = cstr(shr);
|
||||||
@@ -200,10 +209,11 @@ impl<'a> SmbClient<'a> {
|
|||||||
/// Opens [`SmbFile`](struct.SmbFile.html) defined by SMB `path` with `options`.
|
/// Opens [`SmbFile`](struct.SmbFile.html) defined by SMB `path` with `options`.
|
||||||
///
|
///
|
||||||
/// See [OpenOptions](struct.OpenOptions.html).
|
/// See [OpenOptions](struct.OpenOptions.html).
|
||||||
pub fn open_with<'b, P: AsRef<str>>(&'b self,
|
pub fn open_with<'b, P: AsRef<str>>(
|
||||||
path: P,
|
&'b self,
|
||||||
options: OpenOptions)
|
path: P,
|
||||||
-> Result<SmbFile<'a, 'b>> {
|
options: OpenOptions,
|
||||||
|
) -> Result<SmbFile<'a, 'b>> {
|
||||||
trace!(target: "smbc", "open_with {:?}", options);
|
trace!(target: "smbc", "open_with {:?}", options);
|
||||||
|
|
||||||
let open_fn = self.get_fn(smbc_getFunctionOpen)?;
|
let open_fn = self.get_fn(smbc_getFunctionOpen)?;
|
||||||
@@ -211,17 +221,16 @@ impl<'a> SmbClient<'a> {
|
|||||||
let path = cstring(path)?;
|
let path = cstring(path)?;
|
||||||
trace!(target: "smbc", "opening {:?}", path);
|
trace!(target: "smbc", "opening {:?}", path);
|
||||||
|
|
||||||
let fd = result_from_ptr_mut(open_fn(self.ctx,
|
let fd = result_from_ptr_mut(open_fn(
|
||||||
path.as_ptr(),
|
self.ctx,
|
||||||
options.to_flags()?,
|
path.as_ptr(),
|
||||||
options.mode))?;
|
options.to_flags()?,
|
||||||
|
options.mode,
|
||||||
|
))?;
|
||||||
if (fd as i64) < 0 {
|
if (fd as i64) < 0 {
|
||||||
trace!(target: "smbc", "neg fd");
|
trace!(target: "smbc", "neg fd");
|
||||||
}
|
}
|
||||||
Ok(SmbFile {
|
Ok(SmbFile { smbc: &self, fd })
|
||||||
smbc: &self,
|
|
||||||
fd,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Open read-only [`SmbFile`](struct.SmbFile.html) defined by SMB `path`.
|
/// Open read-only [`SmbFile`](struct.SmbFile.html) defined by SMB `path`.
|
||||||
@@ -255,7 +264,14 @@ impl<'a> SmbClient<'a> {
|
|||||||
///
|
///
|
||||||
/// See [`open_with(..)`](struct.SmbClient.html#method.open_with).
|
/// See [`open_with(..)`](struct.SmbClient.html#method.open_with).
|
||||||
pub fn open_wo<'b, P: AsRef<str>>(&'b self, path: P) -> Result<SmbFile<'a, 'b>> {
|
pub fn open_wo<'b, P: AsRef<str>>(&'b self, path: P) -> Result<SmbFile<'a, 'b>> {
|
||||||
self.open_with(path, OpenOptions::default().read(false).write(true).create(true).truncate(true))
|
self.open_with(
|
||||||
|
path,
|
||||||
|
OpenOptions::default()
|
||||||
|
.read(false)
|
||||||
|
.write(true)
|
||||||
|
.create(true)
|
||||||
|
.truncate(true),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Open read-write [`SmbFile`](struct.SmbFile.html) defined by SMB `path`.
|
/// Open read-write [`SmbFile`](struct.SmbFile.html) defined by SMB `path`.
|
||||||
@@ -264,7 +280,10 @@ impl<'a> SmbClient<'a> {
|
|||||||
///
|
///
|
||||||
/// See [`open_with(..)`](struct.SmbClient.html#method.open_with).
|
/// See [`open_with(..)`](struct.SmbClient.html#method.open_with).
|
||||||
pub fn open_rw<'b, P: AsRef<str>>(&'b self, path: P) -> Result<SmbFile<'a, 'b>> {
|
pub fn open_rw<'b, P: AsRef<str>>(&'b self, path: P) -> Result<SmbFile<'a, 'b>> {
|
||||||
self.open_with(path, OpenOptions::default().read(true).write(true).create(true))
|
self.open_with(
|
||||||
|
path,
|
||||||
|
OpenOptions::default().read(true).write(true).create(true),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
@@ -297,7 +316,10 @@ impl<'a> SmbClient<'a> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_fn<T>(&self, get_func: unsafe extern "C" fn(*mut SMBCCTX) -> Option<T>) -> io::Result<T> {
|
fn get_fn<T>(
|
||||||
|
&self,
|
||||||
|
get_func: unsafe extern "C" fn(*mut SMBCCTX) -> Option<T>,
|
||||||
|
) -> io::Result<T> {
|
||||||
unsafe { get_func(self.ctx).ok_or(io::Error::from_raw_os_error(libc::EINVAL as i32)) }
|
unsafe { get_func(self.ctx).ok_or(io::Error::from_raw_os_error(libc::EINVAL as i32)) }
|
||||||
}
|
}
|
||||||
} // 2}}}
|
} // 2}}}
|
||||||
@@ -312,7 +334,7 @@ impl<'a> Drop for SmbClient<'a> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} // 2}}}
|
} // 2}}}
|
||||||
// 1}}}
|
// 1}}}
|
||||||
|
|
||||||
// OpenOptions {{{1
|
// OpenOptions {{{1
|
||||||
/// Describes options for opening file:
|
/// Describes options for opening file:
|
||||||
@@ -393,15 +415,14 @@ impl OpenOptions {
|
|||||||
fn to_flags(&self) -> Result<c_int> {
|
fn to_flags(&self) -> Result<c_int> {
|
||||||
let base_mode = match (self.read, self.write) {
|
let base_mode = match (self.read, self.write) {
|
||||||
// defaults to read only
|
// defaults to read only
|
||||||
(false, false) |
|
(false, false) | (true, false) => libc::O_RDONLY,
|
||||||
(true, false) => libc::O_RDONLY,
|
|
||||||
(false, true) => libc::O_WRONLY,
|
(false, true) => libc::O_WRONLY,
|
||||||
(true, true) => libc::O_RDWR,
|
(true, true) => libc::O_RDWR,
|
||||||
};
|
};
|
||||||
Ok(base_mode | self.flags)
|
Ok(base_mode | self.flags)
|
||||||
}
|
}
|
||||||
} // }}}
|
} // }}}
|
||||||
// 1}}}
|
// 1}}}
|
||||||
|
|
||||||
impl Default for OpenOptions {
|
impl Default for OpenOptions {
|
||||||
/// Default [`OpenOptions`](struct.OpenOptions.html) is
|
/// Default [`OpenOptions`](struct.OpenOptions.html) is
|
||||||
@@ -427,12 +448,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 = self.smbc.get_fn(smbc_getFunctionRead)?;
|
let read_fn = self.smbc.get_fn(smbc_getFunctionRead)?;
|
||||||
let bytes_read = to_result_with_le(
|
let bytes_read = to_result_with_le(read_fn(
|
||||||
read_fn(self.smbc.ctx,
|
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)
|
||||||
}
|
}
|
||||||
} // }}}
|
} // }}}
|
||||||
@@ -442,12 +463,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 = self.smbc.get_fn(smbc_getFunctionWrite)?;
|
let write_fn = self.smbc.get_fn(smbc_getFunctionWrite)?;
|
||||||
let bytes_wrote = to_result_with_le(
|
let bytes_wrote = to_result_with_le(write_fn(
|
||||||
write_fn(self.smbc.ctx,
|
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)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -467,7 +488,8 @@ 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 = to_result_with_errno(lseek_fn(self.smbc.ctx, self.fd, off, whence), libc::EINVAL)?;
|
let res = lseek_fn(self.smbc.ctx, self.fd, off, whence);
|
||||||
|
let res = to_result_with_errno(res, libc::EINVAL)?;
|
||||||
Ok(res as u64)
|
Ok(res as u64)
|
||||||
}
|
}
|
||||||
} // }}}
|
} // }}}
|
||||||
@@ -481,6 +503,6 @@ impl<'a, 'b> Drop for SmbFile<'a, 'b> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} // }}}
|
} // }}}
|
||||||
// 1}}}
|
// 1}}}
|
||||||
|
|
||||||
// vim: fen:fdm=marker:fdl=1:
|
// vim: fen:fdm=marker:fdl=1:
|
||||||
|
|||||||
19
src/util.rs
19
src/util.rs
@@ -28,13 +28,21 @@ use result::*;
|
|||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
/// Ok(ptr) for non-null ptr or Err(last_os_error) otherwise
|
/// Ok(ptr) for non-null ptr or Err(last_os_error) otherwise
|
||||||
pub fn result_from_ptr_mut<T>(ptr: *mut T) -> io::Result<*mut T> {
|
pub fn result_from_ptr_mut<T>(ptr: *mut T) -> io::Result<*mut T> {
|
||||||
if ptr.is_null() { Err(io::Error::last_os_error()) } else { Ok(ptr) }
|
if ptr.is_null() {
|
||||||
|
Err(io::Error::last_os_error())
|
||||||
|
} else {
|
||||||
|
Ok(ptr)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
/// Ok(ptr) for non-null ptr or Err(last_os_error) otherwise
|
/// Ok(ptr) for non-null ptr or Err(last_os_error) otherwise
|
||||||
pub fn result_from_ptr<T>(ptr: *const T) -> io::Result<*const T> {
|
pub fn result_from_ptr<T>(ptr: *const T) -> io::Result<*const T> {
|
||||||
if ptr.is_null() { Err(io::Error::last_os_error()) } else { Ok(ptr) }
|
if ptr.is_null() {
|
||||||
|
Err(io::Error::last_os_error())
|
||||||
|
} else {
|
||||||
|
Ok(ptr)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub unsafe fn cstr<'a, T>(p: *const T) -> Cow<'a, str> {
|
pub unsafe fn cstr<'a, T>(p: *const T) -> Cow<'a, str> {
|
||||||
@@ -76,6 +84,9 @@ pub fn to_result_with_errno<T: Eq + From<i8>>(t: T, errno: c_int) -> io::Result<
|
|||||||
|
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn to_result_with_error<T: Eq + From<i8>>(t: T, err: io::Error) -> io::Result<T> {
|
fn to_result_with_error<T: Eq + From<i8>>(t: T, err: io::Error) -> io::Result<T> {
|
||||||
if t == T::from(-1) { Err(err) } else { Ok(t) }
|
if t == T::from(-1) {
|
||||||
|
Err(err)
|
||||||
|
} else {
|
||||||
|
Ok(t)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user