include!("./fs_copy_flags.inc.rs");
include!("./fs_open_flags.inc.rs");
include!("./fs_mode_flags.rs");
include!("./fs_symlink_flags.inc.rs");
include!("./fs_types.inc.rs");
use crate::{FromInner, FsReq, Inner, IntoInner};
use std::ffi::CString;
use uv::{
uv_fs_access, uv_fs_chmod, uv_fs_chown, uv_fs_close, uv_fs_closedir, uv_fs_copyfile,
uv_fs_fchmod, uv_fs_fchown, uv_fs_fdatasync, uv_fs_fstat, uv_fs_fsync, uv_fs_ftruncate,
uv_fs_futime, uv_fs_lchown, uv_fs_link, uv_fs_lstat, uv_fs_mkdir, uv_fs_mkdtemp, uv_fs_mkstemp,
uv_fs_open, uv_fs_opendir, uv_fs_read, uv_fs_readdir, uv_fs_readlink, uv_fs_realpath,
uv_fs_rename, uv_fs_rmdir, uv_fs_scandir, uv_fs_scandir_next, uv_fs_sendfile, uv_fs_stat,
uv_fs_statfs, uv_fs_symlink, uv_fs_unlink, uv_fs_utime, uv_fs_write,
};
pub mod dir;
pub use dir::*;
pub mod dirent;
pub use dirent::*;
pub mod misc;
pub use misc::*;
pub mod stat;
pub use stat::*;
pub mod statfs;
pub use statfs::*;
pub mod timespec;
pub use timespec::*;
type FsReqResult = crate::Result<FsReq>;
type FsReqErrResult = Result<FsReq, Box<dyn std::error::Error>>;
type SyncResult = crate::Result<usize>;
type SyncErrResult = Result<usize, Box<dyn std::error::Error>>;
pub type File = i32;
#[cfg(windows)]
pub type OsFile = *mut std::ffi::c_void;
#[cfg(not(windows))]
pub type OsFile = i32;
#[cfg(windows)]
pub type Socket = u64;
#[cfg(not(windows))]
pub type Socket = i32;
#[cfg(windows)]
pub type Uid = u8;
#[cfg(not(windows))]
pub type Uid = u32;
#[cfg(windows)]
pub type Gid = u8;
#[cfg(not(windows))]
pub type Gid = u32;
fn destroy_req_return_result(mut req: FsReq) -> SyncResult {
let result = req.result();
req.destroy();
result
}
fn destroy_req_return_boxed_result(req: FsReq) -> SyncErrResult {
destroy_req_return_result(req).map_err(|e| Box::new(e) as _)
}
impl crate::Loop {
fn _fs_close<CB: Into<crate::FsCB<'static>>>(&self, file: File, cb: CB) -> FsReqResult {
let cb = cb.into();
let uv_cb = use_c_callback!(crate::uv_fs_cb, cb);
let mut req = FsReq::new(cb)?;
let result =
crate::uvret(unsafe { uv_fs_close(self.into_inner(), req.inner(), file as _, uv_cb) });
if result.is_err() {
req.destroy();
}
result.map(|_| req)
}
pub fn fs_close<CB: Into<crate::FsCB<'static>>>(&self, file: File, cb: CB) -> FsReqResult {
self._fs_close(file, cb)
}
pub fn fs_close_sync(&self, file: File) -> SyncResult {
self._fs_close(file, ()).and_then(destroy_req_return_result)
}
fn _fs_open<CB: Into<crate::FsCB<'static>>>(
&self,
path: &str,
flags: FsOpenFlags,
mode: FsModeFlags,
cb: CB,
) -> FsReqErrResult {
let cb = cb.into();
let uv_cb = use_c_callback!(crate::uv_fs_cb, cb);
let path = CString::new(path)?;
let mut req = FsReq::new(cb)?;
let result = crate::uvret(unsafe {
uv_fs_open(
self.into_inner(),
req.inner(),
path.as_ptr(),
flags.bits(),
mode.bits(),
uv_cb,
)
})
.map_err(|e| Box::new(e) as _);
if result.is_err() {
req.destroy();
}
result.map(|_| req)
}
pub fn fs_open<CB: Into<crate::FsCB<'static>>>(
&self,
path: &str,
flags: FsOpenFlags,
mode: FsModeFlags,
cb: CB,
) -> FsReqErrResult {
self._fs_open(path, flags, mode, cb)
}
pub fn fs_open_sync(
&self,
path: &str,
flags: FsOpenFlags,
mode: FsModeFlags,
) -> Result<File, Box<dyn std::error::Error>> {
self._fs_open(path, flags, mode, ()).and_then(|mut req| {
let file = req.result();
req.destroy();
file.map(|f| f as _).map_err(|e| Box::new(e) as _)
})
}
fn _fs_read<CB: Into<crate::FsCB<'static>>>(
&self,
file: File,
bufs: &[crate::Buf],
offset: i64,
cb: CB,
) -> FsReqResult {
let cb = cb.into();
let uv_cb = use_c_callback!(crate::uv_fs_cb, cb);
let mut req = FsReq::new(cb)?;
let (bufs_ptr, bufs_len, _) = bufs.into_inner();
let result = crate::uvret(unsafe {
uv_fs_read(
self.into_inner(),
req.inner(),
file as _,
bufs_ptr as _,
bufs_len as _,
offset,
uv_cb,
)
});
if result.is_err() {
req.destroy();
}
result.map(|_| req)
}
pub fn fs_read<CB: Into<crate::FsCB<'static>>>(
&self,
file: File,
bufs: &[crate::Buf],
offset: i64,
cb: CB,
) -> FsReqResult {
self._fs_read(file, bufs, offset, cb)
}
pub fn fs_read_sync(&self, file: File, bufs: &[crate::Buf], offset: i64) -> SyncResult {
self._fs_read(file, bufs, offset, ())
.and_then(destroy_req_return_result)
}
fn _fs_unlink<CB: Into<crate::FsCB<'static>>>(&self, path: &str, cb: CB) -> FsReqErrResult {
let cb = cb.into();
let uv_cb = use_c_callback!(crate::uv_fs_cb, cb);
let path = CString::new(path)?;
let mut req = FsReq::new(cb)?;
let result = crate::uvret(unsafe {
uv_fs_unlink(self.into_inner(), req.inner(), path.as_ptr(), uv_cb)
})
.map_err(|e| Box::new(e) as _);
if result.is_err() {
req.destroy();
}
result.map(|_| req)
}
pub fn fs_unlink<CB: Into<crate::FsCB<'static>>>(&self, path: &str, cb: CB) -> FsReqErrResult {
self._fs_unlink(path, cb)
}
pub fn fs_unlink_sync(&self, path: &str) -> SyncErrResult {
self._fs_unlink(path, ())
.and_then(destroy_req_return_boxed_result)
}
fn _fs_write<CB: Into<crate::FsCB<'static>>>(
&self,
file: File,
bufs: &[impl crate::BufTrait],
offset: i64,
cb: CB,
) -> FsReqResult {
let cb = cb.into();
let uv_cb = use_c_callback!(crate::uv_fs_cb, cb);
let mut req = FsReq::new(cb)?;
let (bufs_ptr, bufs_len, _) = bufs.into_inner();
let result = crate::uvret(unsafe {
uv_fs_write(
self.into_inner(),
req.inner(),
file as _,
bufs_ptr as _,
bufs_len as _,
offset,
uv_cb,
)
});
if result.is_err() {
req.destroy();
}
result.map(|_| req)
}
pub fn fs_write<CB: Into<crate::FsCB<'static>>>(
&self,
file: File,
bufs: &[impl crate::BufTrait],
offset: i64,
cb: CB,
) -> FsReqResult {
self._fs_write(file, bufs, offset, cb)
}
pub fn fs_write_sync(
&self,
file: File,
bufs: &[impl crate::BufTrait],
offset: i64,
) -> SyncResult {
self._fs_write(file, bufs, offset, ())
.and_then(destroy_req_return_result)
}
fn _fs_mkdir<CB: Into<crate::FsCB<'static>>>(
&self,
path: &str,
mode: FsModeFlags,
cb: CB,
) -> FsReqErrResult {
let cb = cb.into();
let uv_cb = use_c_callback!(crate::uv_fs_cb, cb);
let path = CString::new(path)?;
let mut req = FsReq::new(cb)?;
let result = crate::uvret(unsafe {
uv_fs_mkdir(
self.into_inner(),
req.inner(),
path.as_ptr() as _,
mode.bits(),
uv_cb,
)
})
.map_err(|e| Box::new(e) as _);
if result.is_err() {
req.destroy();
}
result.map(|_| req)
}
pub fn fs_mkdir<CB: Into<crate::FsCB<'static>>>(
&self,
path: &str,
mode: FsModeFlags,
cb: CB,
) -> FsReqErrResult {
self._fs_mkdir(path, mode, cb)
}
pub fn fs_mkdir_sync(&self, path: &str, mode: FsModeFlags) -> SyncErrResult {
self._fs_mkdir(path, mode, ())
.and_then(destroy_req_return_boxed_result)
}
fn _fs_mkdtemp<CB: Into<crate::FsCB<'static>>>(&self, tpl: &str, cb: CB) -> FsReqErrResult {
let cb = cb.into();
let uv_cb = use_c_callback!(crate::uv_fs_cb, cb);
let tpl = CString::new(tpl)?;
let mut req = FsReq::new(cb)?;
let result = crate::uvret(unsafe {
uv_fs_mkdtemp(self.into_inner(), req.inner(), tpl.as_ptr(), uv_cb)
})
.map_err(|e| Box::new(e) as _);
if result.is_err() {
req.destroy()
}
result.map(|_| req)
}
pub fn fs_mkdtemp<CB: Into<crate::FsCB<'static>>>(&self, tpl: &str, cb: CB) -> FsReqErrResult {
self._fs_mkdtemp(tpl, cb)
}
pub fn fs_mkdtemp_sync(&self, tpl: &str) -> Result<String, Box<dyn std::error::Error>> {
self._fs_mkdtemp(tpl, ()).map(|mut req| {
let path = req.path();
req.destroy();
return path;
})
}
fn _fs_mkstemp<CB: Into<crate::FsCB<'static>>>(&self, tpl: &str, cb: CB) -> FsReqErrResult {
let cb = cb.into();
let uv_cb = use_c_callback!(crate::uv_fs_cb, cb);
let tpl = CString::new(tpl)?;
let mut req = FsReq::new(cb)?;
let result = crate::uvret(unsafe {
uv_fs_mkstemp(self.into_inner(), req.inner(), tpl.as_ptr(), uv_cb)
})
.map_err(|e| Box::new(e) as _);
if result.is_err() {
req.destroy();
}
result.map(|_| req)
}
pub fn fs_mkstemp<CB: Into<crate::FsCB<'static>>>(&self, tpl: &str, cb: CB) -> FsReqErrResult {
self._fs_mkstemp(tpl, cb)
}
pub fn fs_mkstemp_sync(&self, tpl: &str) -> SyncErrResult {
self._fs_mkstemp(tpl, ())
.and_then(destroy_req_return_boxed_result)
}
fn _fs_rmdir<CB: Into<crate::FsCB<'static>>>(&self, path: &str, cb: CB) -> FsReqErrResult {
let cb = cb.into();
let uv_cb = use_c_callback!(crate::uv_fs_cb, cb);
let path = CString::new(path)?;
let mut req = FsReq::new(cb)?;
let result = crate::uvret(unsafe {
uv_fs_rmdir(self.into_inner(), req.inner(), path.as_ptr(), uv_cb)
})
.map_err(|e| Box::new(e) as _);
if result.is_err() {
req.destroy();
}
result.map(|_| req)
}
pub fn fs_rmdir<CB: Into<crate::FsCB<'static>>>(&self, path: &str, cb: CB) -> FsReqErrResult {
self._fs_rmdir(path, cb)
}
pub fn fs_rmdir_sync(&self, path: &str) -> SyncErrResult {
self._fs_rmdir(path, ())
.and_then(destroy_req_return_boxed_result)
}
fn _fs_opendir<CB: Into<crate::FsCB<'static>>>(&self, path: &str, cb: CB) -> FsReqErrResult {
let cb = cb.into();
let uv_cb = use_c_callback!(crate::uv_fs_cb, cb);
let path = CString::new(path)?;
let mut req = FsReq::new(cb)?;
let result = crate::uvret(unsafe {
uv_fs_opendir(self.into_inner(), req.inner(), path.as_ptr(), uv_cb)
})
.map_err(|e| Box::new(e) as _);
if result.is_err() {
req.destroy();
}
result.map(|_| req)
}
pub fn fs_opendir<CB: Into<crate::FsCB<'static>>>(&self, path: &str, cb: CB) -> FsReqErrResult {
self._fs_opendir(path, cb)
}
pub fn fs_opendir_sync(&self, path: &str) -> Result<crate::Dir, Box<dyn std::error::Error>> {
self._fs_opendir(path, ()).and_then(|mut req| {
let dir = req.dir();
req.destroy();
dir.ok_or_else(|| Box::new(crate::Error::EINVAL) as _)
})
}
fn _fs_closedir<CB: Into<crate::FsCB<'static>>>(&self, dir: &Dir, cb: CB) -> FsReqResult {
let cb = cb.into();
let uv_cb = use_c_callback!(crate::uv_fs_cb, cb);
let mut req = FsReq::new(cb)?;
let result = crate::uvret(unsafe {
uv_fs_closedir(self.into_inner(), req.inner(), dir.into_inner(), uv_cb)
});
if result.is_err() {
req.destroy();
}
result.map(|_| req)
}
pub fn fs_closedir<CB: Into<crate::FsCB<'static>>>(&self, dir: &Dir, cb: CB) -> FsReqResult {
self._fs_closedir(dir, cb)
}
pub fn fs_closedir_sync(&self, dir: &Dir) -> SyncResult {
self._fs_closedir(dir, ())
.and_then(destroy_req_return_result)
}
fn _fs_readdir<CB: Into<crate::FsCB<'static>>>(&self, dir: &Dir, cb: CB) -> FsReqResult {
let cb = cb.into();
let uv_cb = use_c_callback!(crate::uv_fs_cb, cb);
let mut req = FsReq::new(cb)?;
let result = crate::uvret(unsafe {
uv_fs_readdir(self.into_inner(), req.inner(), dir.into_inner(), uv_cb)
});
if result.is_err() {
req.destroy();
}
result.map(|_| req)
}
pub fn fs_readdir<CB: Into<crate::FsCB<'static>>>(&self, dir: &Dir, cb: CB) -> FsReqResult {
self._fs_readdir(dir, cb).and_then(|req| {
if let Some(dir) = req.dir().as_mut() {
let result = req.result()?;
dir.set_len(result as _);
}
Ok(req)
})
}
pub fn fs_readdir_sync(&self, dir: &Dir) -> SyncResult {
self._fs_readdir(dir, ())
.and_then(destroy_req_return_result)
}
fn _fs_scandir<CB: Into<crate::FsCB<'static>>>(
&self,
path: &str,
flags: FsOpenFlags,
cb: CB,
) -> FsReqErrResult {
let cb = cb.into();
let uv_cb = use_c_callback!(crate::uv_fs_cb, cb);
let path = CString::new(path)?;
let mut req = FsReq::new(cb)?;
let result = crate::uvret(unsafe {
uv_fs_scandir(
self.into_inner(),
req.inner(),
path.as_ptr(),
flags.bits(),
uv_cb,
)
})
.map_err(|e| Box::new(e) as _);
if result.is_err() {
req.destroy();
}
result.map(|_| req)
}
pub fn fs_scandir(
&self,
path: &str,
flags: FsOpenFlags,
mut cb: impl FnMut(ScandirIter) + 'static,
) -> FsReqErrResult {
self._fs_scandir(path, flags, move |req| cb(ScandirIter { req }))
}
pub fn fs_scandir_sync(
&self,
path: &str,
flags: FsOpenFlags,
) -> Result<ScandirIter, Box<dyn std::error::Error>> {
self._fs_scandir(path, flags, ())
.map(|req| ScandirIter { req })
}
fn _fs_stat<CB: Into<crate::FsCB<'static>>>(&self, path: &str, cb: CB) -> FsReqErrResult {
let cb = cb.into();
let uv_cb = use_c_callback!(crate::uv_fs_cb, cb);
let path = CString::new(path)?;
let mut req = FsReq::new(cb)?;
let result = crate::uvret(unsafe {
uv_fs_stat(self.into_inner(), req.inner(), path.as_ptr(), uv_cb)
})
.map_err(|e| Box::new(e) as _);
if result.is_err() {
req.destroy();
}
result.map(|_| req)
}
pub fn fs_stat<CB: Into<crate::FsCB<'static>>>(&self, path: &str, cb: CB) -> FsReqErrResult {
self._fs_stat(path, cb)
}
pub fn fs_stat_sync(&self, path: &str) -> Result<Stat, Box<dyn std::error::Error>> {
self._fs_stat(path, ()).map(|mut req| {
let stat = req.stat();
req.destroy();
return stat;
})
}
fn _fs_fstat<CB: Into<crate::FsCB<'static>>>(&self, file: File, cb: CB) -> FsReqResult {
let cb = cb.into();
let uv_cb = use_c_callback!(crate::uv_fs_cb, cb);
let mut req = FsReq::new(cb)?;
let result =
crate::uvret(unsafe { uv_fs_fstat(self.into_inner(), req.inner(), file as _, uv_cb) });
if result.is_err() {
req.destroy();
}
result.map(|_| req)
}
pub fn fs_fstat<CB: Into<crate::FsCB<'static>>>(&self, file: File, cb: CB) -> FsReqResult {
self._fs_fstat(file, cb)
}
pub fn fs_fstat_sync(&self, file: File) -> crate::Result<Stat> {
self._fs_fstat(file, ()).map(|mut req| {
let stat = req.stat();
req.destroy();
return stat;
})
}
fn _fs_lstat<CB: Into<crate::FsCB<'static>>>(&self, path: &str, cb: CB) -> FsReqErrResult {
let cb = cb.into();
let uv_cb = use_c_callback!(crate::uv_fs_cb, cb);
let path = CString::new(path)?;
let mut req = FsReq::new(cb)?;
let result = crate::uvret(unsafe {
uv_fs_lstat(self.into_inner(), req.inner(), path.as_ptr(), uv_cb)
})
.map_err(|e| Box::new(e) as _);
if result.is_err() {
req.destroy();
}
result.map(|_| req)
}
pub fn fs_lstat<CB: Into<crate::FsCB<'static>>>(&self, path: &str, cb: CB) -> FsReqErrResult {
self._fs_lstat(path, cb)
}
pub fn fs_lstat_sync(&self, path: &str) -> Result<Stat, Box<dyn std::error::Error>> {
self._fs_lstat(path, ()).map(|mut req| {
let stat = req.stat();
req.destroy();
return stat;
})
}
fn _fs_statfs<CB: Into<crate::FsCB<'static>>>(&self, path: &str, cb: CB) -> FsReqErrResult {
let cb = cb.into();
let uv_cb = use_c_callback!(crate::uv_fs_cb, cb);
let path = CString::new(path)?;
let mut req = FsReq::new(cb)?;
let result = crate::uvret(unsafe {
uv_fs_statfs(self.into_inner(), req.inner(), path.as_ptr(), uv_cb)
})
.map_err(|e| Box::new(e) as _);
if result.is_err() {
req.destroy();
}
result.map(|_| req)
}
pub fn fs_statfs<CB: Into<crate::FsCB<'static>>>(&self, path: &str, cb: CB) -> FsReqErrResult {
self._fs_statfs(path, cb)
}
pub fn fs_statfs_sync(&self, path: &str) -> Result<StatFs, Box<dyn std::error::Error>> {
self._fs_statfs(path, ()).and_then(|mut req| {
let statfs = req.statfs();
req.destroy();
statfs.ok_or_else(|| Box::new(crate::Error::EINVAL) as _)
})
}
fn _fs_rename<CB: Into<crate::FsCB<'static>>>(
&self,
path: &str,
new_path: &str,
cb: CB,
) -> FsReqErrResult {
let cb = cb.into();
let uv_cb = use_c_callback!(crate::uv_fs_cb, cb);
let path = CString::new(path)?;
let new_path = CString::new(new_path)?;
let mut req = FsReq::new(cb)?;
let result = crate::uvret(unsafe {
uv_fs_rename(
self.into_inner(),
req.inner(),
path.as_ptr(),
new_path.as_ptr(),
uv_cb,
)
})
.map_err(|e| Box::new(e) as _);
if result.is_err() {
req.destroy();
}
result.map(|_| req)
}
pub fn fs_rename<CB: Into<crate::FsCB<'static>>>(
&self,
path: &str,
new_path: &str,
cb: CB,
) -> FsReqErrResult {
self._fs_rename(path, new_path, cb)
}
pub fn fs_rename_sync(&self, path: &str, new_path: &str) -> SyncErrResult {
self._fs_rename(path, new_path, ())
.and_then(destroy_req_return_boxed_result)
}
fn _fs_fsync<CB: Into<crate::FsCB<'static>>>(&self, file: File, cb: CB) -> FsReqResult {
let cb = cb.into();
let uv_cb = use_c_callback!(crate::uv_fs_cb, cb);
let mut req = FsReq::new(cb)?;
let result =
crate::uvret(unsafe { uv_fs_fsync(self.into_inner(), req.inner(), file as _, uv_cb) });
if result.is_err() {
req.destroy();
}
result.map(|_| req)
}
pub fn fs_fsync<CB: Into<crate::FsCB<'static>>>(&self, file: File, cb: CB) -> FsReqResult {
self._fs_fsync(file, cb)
}
pub fn fs_fsync_sync(&self, file: File) -> SyncResult {
self._fs_fsync(file, ()).and_then(destroy_req_return_result)
}
fn _fs_fdatasync<CB: Into<crate::FsCB<'static>>>(&self, file: File, cb: CB) -> FsReqResult {
let cb = cb.into();
let uv_cb = use_c_callback!(crate::uv_fs_cb, cb);
let mut req = FsReq::new(cb)?;
let result = crate::uvret(unsafe {
uv_fs_fdatasync(self.into_inner(), req.inner(), file as _, uv_cb)
});
if result.is_err() {
req.destroy();
}
result.map(|_| req)
}
pub fn fs_fdatasync<CB: Into<crate::FsCB<'static>>>(&self, file: File, cb: CB) -> FsReqResult {
self._fs_fdatasync(file, cb)
}
pub fn fs_fdatasync_sync(&self, file: File) -> SyncResult {
self._fs_fdatasync(file, ())
.and_then(destroy_req_return_result)
}
fn _fs_ftruncate<CB: Into<crate::FsCB<'static>>>(
&self,
file: File,
offset: i64,
cb: CB,
) -> FsReqResult {
let cb = cb.into();
let uv_cb = use_c_callback!(crate::uv_fs_cb, cb);
let mut req = FsReq::new(cb)?;
let result = crate::uvret(unsafe {
uv_fs_ftruncate(self.into_inner(), req.inner(), file as _, offset, uv_cb)
});
if result.is_err() {
req.destroy();
}
result.map(|_| req)
}
pub fn fs_ftruncate<CB: Into<crate::FsCB<'static>>>(
&self,
file: File,
offset: i64,
cb: CB,
) -> FsReqResult {
self._fs_ftruncate(file, offset, cb)
}
pub fn fs_ftruncate_sync(&self, file: File, offset: i64) -> SyncResult {
self._fs_ftruncate(file, offset, ())
.and_then(destroy_req_return_result)
}
fn _fs_copyfile<CB: Into<crate::FsCB<'static>>>(
&self,
path: &str,
new_path: &str,
flags: FsCopyFlags,
cb: CB,
) -> FsReqErrResult {
let cb = cb.into();
let uv_cb = use_c_callback!(crate::uv_fs_cb, cb);
let path = CString::new(path)?;
let new_path = CString::new(new_path)?;
let mut req = FsReq::new(cb)?;
let result = crate::uvret(unsafe {
uv_fs_copyfile(
self.into_inner(),
req.inner(),
path.as_ptr(),
new_path.as_ptr(),
flags.bits(),
uv_cb,
)
})
.map_err(|e| Box::new(e) as _);
if result.is_err() {
req.destroy();
}
result.map(|_| req)
}
pub fn fs_copyfile<CB: Into<crate::FsCB<'static>>>(
&self,
path: &str,
new_path: &str,
flags: FsCopyFlags,
cb: CB,
) -> FsReqErrResult {
self._fs_copyfile(path, new_path, flags, cb)
}
pub fn fs_copyfile_sync(
&self,
path: &str,
new_path: &str,
flags: FsCopyFlags,
) -> SyncErrResult {
self._fs_copyfile(path, new_path, flags, ())
.and_then(destroy_req_return_boxed_result)
}
fn _fs_sendfile<CB: Into<crate::FsCB<'static>>>(
&self,
out_file: File,
in_file: File,
offset: i64,
len: usize,
cb: CB,
) -> FsReqResult {
let cb = cb.into();
let uv_cb = use_c_callback!(crate::uv_fs_cb, cb);
let mut req = FsReq::new(cb)?;
let result = crate::uvret(unsafe {
uv_fs_sendfile(
self.into_inner(),
req.inner(),
out_file as _,
in_file as _,
offset,
len as _,
uv_cb,
)
});
if result.is_err() {
req.destroy();
}
result.map(|_| req)
}
pub fn fs_sendfile<CB: Into<crate::FsCB<'static>>>(
&self,
out_file: File,
in_file: File,
offset: i64,
len: usize,
cb: CB,
) -> FsReqResult {
self._fs_sendfile(out_file, in_file, offset, len, cb)
}
pub fn fs_sendfile_sync(
&self,
out_file: File,
in_file: File,
offset: i64,
len: usize,
) -> SyncResult {
self._fs_sendfile(out_file, in_file, offset, len, ())
.and_then(destroy_req_return_result)
}
fn _fs_access<CB: Into<crate::FsCB<'static>>>(
&self,
path: &str,
mode: FsAccessFlags,
cb: CB,
) -> FsReqErrResult {
let cb = cb.into();
let uv_cb = use_c_callback!(crate::uv_fs_cb, cb);
let path = CString::new(path)?;
let mut req = FsReq::new(cb)?;
let result = crate::uvret(unsafe {
uv_fs_access(
self.into_inner(),
req.inner(),
path.as_ptr(),
mode.bits(),
uv_cb,
)
})
.map_err(|e| Box::new(e) as _);
if result.is_err() {
req.destroy();
}
result.map(|_| req)
}
pub fn fs_access<CB: Into<crate::FsCB<'static>>>(
&self,
path: &str,
mode: FsAccessFlags,
cb: CB,
) -> FsReqErrResult {
self._fs_access(path, mode, cb)
}
pub fn fs_access_sync(&self, path: &str, mode: FsAccessFlags) -> SyncErrResult {
self._fs_access(path, mode, ())
.and_then(destroy_req_return_boxed_result)
}
fn _fs_chmod<CB: Into<crate::FsCB<'static>>>(
&self,
path: &str,
mode: FsModeFlags,
cb: CB,
) -> FsReqErrResult {
let cb = cb.into();
let uv_cb = use_c_callback!(crate::uv_fs_cb, cb);
let path = CString::new(path)?;
let mut req = FsReq::new(cb)?;
let result = crate::uvret(unsafe {
uv_fs_chmod(
self.into_inner(),
req.inner(),
path.as_ptr(),
mode.bits(),
uv_cb,
)
})
.map_err(|e| Box::new(e) as _);
if result.is_err() {
req.destroy();
}
result.map(|_| req)
}
pub fn fs_chmod<CB: Into<crate::FsCB<'static>>>(
&self,
path: &str,
mode: FsModeFlags,
cb: CB,
) -> FsReqErrResult {
self._fs_chmod(path, mode, cb)
}
pub fn fs_chmod_sync(&self, path: &str, mode: FsModeFlags) -> SyncErrResult {
self._fs_chmod(path, mode, ())
.and_then(destroy_req_return_boxed_result)
}
fn _fs_fchmod<CB: Into<crate::FsCB<'static>>>(
&self,
file: File,
mode: FsModeFlags,
cb: CB,
) -> FsReqResult {
let cb = cb.into();
let uv_cb = use_c_callback!(crate::uv_fs_cb, cb);
let mut req = FsReq::new(cb)?;
let result = crate::uvret(unsafe {
uv_fs_fchmod(
self.into_inner(),
req.inner(),
file as _,
mode.bits(),
uv_cb,
)
});
if result.is_err() {
req.destroy();
}
result.map(|_| req)
}
pub fn fs_fchmod<CB: Into<crate::FsCB<'static>>>(
&self,
file: File,
mode: FsModeFlags,
cb: CB,
) -> FsReqResult {
self._fs_fchmod(file, mode, cb)
}
pub fn fs_fchmod_sync(&self, file: File, mode: FsModeFlags) -> SyncResult {
self._fs_fchmod(file, mode, ())
.and_then(destroy_req_return_result)
}
fn _fs_utime<CB: Into<crate::FsCB<'static>>>(
&self,
path: &str,
atime: f64,
mtime: f64,
cb: CB,
) -> FsReqErrResult {
let cb = cb.into();
let uv_cb = use_c_callback!(crate::uv_fs_cb, cb);
let path = CString::new(path)?;
let mut req = FsReq::new(cb)?;
let result = crate::uvret(unsafe {
uv_fs_utime(
self.into_inner(),
req.inner(),
path.as_ptr(),
atime,
mtime,
uv_cb,
)
})
.map_err(|e| Box::new(e) as _);
if result.is_err() {
req.destroy();
}
result.map(|_| req)
}
pub fn fs_utime<CB: Into<crate::FsCB<'static>>>(
&self,
path: &str,
atime: f64,
mtime: f64,
cb: CB,
) -> FsReqErrResult {
self._fs_utime(path, atime, mtime, cb)
}
pub fn fs_utime_sync(&self, path: &str, atime: f64, mtime: f64) -> SyncErrResult {
self._fs_utime(path, atime, mtime, ())
.and_then(destroy_req_return_boxed_result)
}
fn _fs_futime<CB: Into<crate::FsCB<'static>>>(
&self,
file: File,
atime: f64,
mtime: f64,
cb: CB,
) -> FsReqResult {
let cb = cb.into();
let uv_cb = use_c_callback!(crate::uv_fs_cb, cb);
let mut req = FsReq::new(cb)?;
let result = crate::uvret(unsafe {
uv_fs_futime(
self.into_inner(),
req.inner(),
file as _,
atime,
mtime,
uv_cb,
)
});
if result.is_err() {
req.destroy();
}
result.map(|_| req)
}
pub fn fs_futime<CB: Into<crate::FsCB<'static>>>(
&self,
file: File,
atime: f64,
mtime: f64,
cb: CB,
) -> FsReqResult {
self._fs_futime(file, atime, mtime, cb)
}
pub fn fs_futime_sync(&self, file: File, atime: f64, mtime: f64) -> SyncResult {
self._fs_futime(file, atime, mtime, ())
.and_then(destroy_req_return_result)
}
fn _fs_link<CB: Into<crate::FsCB<'static>>>(
&self,
path: &str,
new_path: &str,
cb: CB,
) -> FsReqErrResult {
let cb = cb.into();
let uv_cb = use_c_callback!(crate::uv_fs_cb, cb);
let path = CString::new(path)?;
let new_path = CString::new(new_path)?;
let mut req = FsReq::new(cb)?;
let result = crate::uvret(unsafe {
uv_fs_link(
self.into_inner(),
req.inner(),
path.as_ptr(),
new_path.as_ptr(),
uv_cb,
)
})
.map_err(|e| Box::new(e) as _);
if result.is_err() {
req.destroy();
}
result.map(|_| req)
}
pub fn fs_link<CB: Into<crate::FsCB<'static>>>(
&self,
path: &str,
new_path: &str,
cb: CB,
) -> FsReqErrResult {
self._fs_link(path, new_path, cb)
}
pub fn fs_link_sync(&self, path: &str, new_path: &str) -> SyncErrResult {
self._fs_link(path, new_path, ())
.and_then(destroy_req_return_boxed_result)
}
fn _fs_symlink<CB: Into<crate::FsCB<'static>>>(
&self,
path: &str,
new_path: &str,
flags: FsSymlinkFlags,
cb: CB,
) -> FsReqErrResult {
let cb = cb.into();
let uv_cb = use_c_callback!(crate::uv_fs_cb, cb);
let path = CString::new(path)?;
let new_path = CString::new(new_path)?;
let mut req = FsReq::new(cb)?;
let result = crate::uvret(unsafe {
uv_fs_symlink(
self.into_inner(),
req.inner(),
path.as_ptr(),
new_path.as_ptr(),
flags.bits(),
uv_cb,
)
})
.map_err(|e| Box::new(e) as _);
if result.is_err() {
req.destroy();
}
result.map(|_| req)
}
pub fn fs_symlink<CB: Into<crate::FsCB<'static>>>(
&self,
path: &str,
new_path: &str,
flags: FsSymlinkFlags,
cb: CB,
) -> FsReqErrResult {
self._fs_symlink(path, new_path, flags, cb)
}
pub fn fs_symlink_sync(
&self,
path: &str,
new_path: &str,
flags: FsSymlinkFlags,
) -> SyncErrResult {
self._fs_symlink(path, new_path, flags, ())
.and_then(destroy_req_return_boxed_result)
}
fn _fs_readlink<CB: Into<crate::FsCB<'static>>>(&self, path: &str, cb: CB) -> FsReqErrResult {
let cb = cb.into();
let uv_cb = use_c_callback!(crate::uv_fs_cb, cb);
let path = CString::new(path)?;
let mut req = FsReq::new(cb)?;
let result = crate::uvret(unsafe {
uv_fs_readlink(self.into_inner(), req.inner(), path.as_ptr(), uv_cb)
})
.map_err(|e| Box::new(e) as _);
if result.is_err() {
req.destroy();
}
result.map(|_| req)
}
pub fn fs_readlink<CB: Into<crate::FsCB<'static>>>(
&self,
path: &str,
cb: CB,
) -> FsReqErrResult {
self._fs_readlink(path, cb)
}
pub fn fs_readlink_sync(&self, path: &str) -> Result<String, Box<dyn std::error::Error>> {
self._fs_readlink(path, ()).and_then(|mut req| {
let path = req.real_path();
req.destroy();
path.ok_or_else(|| Box::new(crate::Error::EINVAL) as _)
})
}
fn _fs_realpath<CB: Into<crate::FsCB<'static>>>(&self, path: &str, cb: CB) -> FsReqErrResult {
let cb = cb.into();
let uv_cb = use_c_callback!(crate::uv_fs_cb, cb);
let path = CString::new(path)?;
let mut req = FsReq::new(cb)?;
let result = crate::uvret(unsafe {
uv_fs_realpath(self.into_inner(), req.inner(), path.as_ptr(), uv_cb)
})
.map_err(|e| Box::new(e) as _);
if result.is_err() {
req.destroy();
}
result.map(|_| req)
}
pub fn fs_realpath<CB: Into<crate::FsCB<'static>>>(
&self,
path: &str,
cb: CB,
) -> FsReqErrResult {
self._fs_realpath(path, cb)
}
pub fn fs_realpath_sync(&self, path: &str) -> Result<String, Box<dyn std::error::Error>> {
self._fs_realpath(path, ()).and_then(|mut req| {
let path = req.real_path();
req.destroy();
path.ok_or_else(|| Box::new(crate::Error::EINVAL) as _)
})
}
fn _fs_chown<CB: Into<crate::FsCB<'static>>>(
&self,
path: &str,
uid: Uid,
gid: Gid,
cb: CB,
) -> FsReqErrResult {
let cb = cb.into();
let uv_cb = use_c_callback!(crate::uv_fs_cb, cb);
let path = CString::new(path)?;
let mut req = FsReq::new(cb)?;
let result = crate::uvret(unsafe {
uv_fs_chown(
self.into_inner(),
req.inner(),
path.as_ptr(),
uid as _,
gid as _,
uv_cb,
)
})
.map_err(|e| Box::new(e) as _);
if result.is_err() {
req.destroy();
}
result.map(|_| req)
}
pub fn fs_chown<CB: Into<crate::FsCB<'static>>>(
&self,
path: &str,
uid: Uid,
gid: Gid,
cb: CB,
) -> FsReqErrResult {
self._fs_chown(path, uid, gid, cb)
}
pub fn fs_chown_sync(&self, path: &str, uid: Uid, gid: Gid) -> SyncErrResult {
self._fs_chown(path, uid, gid, ())
.and_then(destroy_req_return_boxed_result)
}
fn _fs_fchown<CB: Into<crate::FsCB<'static>>>(
&self,
file: File,
uid: Uid,
gid: Gid,
cb: CB,
) -> FsReqResult {
let cb = cb.into();
let uv_cb = use_c_callback!(crate::uv_fs_cb, cb);
let mut req = FsReq::new(cb)?;
let result = crate::uvret(unsafe {
uv_fs_fchown(
self.into_inner(),
req.inner(),
file as _,
uid as _,
gid as _,
uv_cb,
)
});
if result.is_err() {
req.destroy();
}
result.map(|_| req)
}
pub fn fs_fchown<CB: Into<crate::FsCB<'static>>>(
&self,
file: File,
uid: Uid,
gid: Gid,
cb: CB,
) -> FsReqResult {
self._fs_fchown(file, uid, gid, cb)
}
pub fn fs_fchown_sync(&self, file: File, uid: Uid, gid: Gid) -> SyncResult {
self._fs_fchown(file, uid, gid, ())
.and_then(destroy_req_return_result)
}
fn _fs_lchown<CB: Into<crate::FsCB<'static>>>(
&self,
path: &str,
uid: Uid,
gid: Gid,
cb: CB,
) -> FsReqErrResult {
let cb = cb.into();
let uv_cb = use_c_callback!(crate::uv_fs_cb, cb);
let path = CString::new(path)?;
let mut req = FsReq::new(cb)?;
let result = crate::uvret(unsafe {
uv_fs_lchown(
self.into_inner(),
req.inner(),
path.as_ptr(),
uid as _,
gid as _,
uv_cb,
)
})
.map_err(|e| Box::new(e) as _);
if result.is_err() {
req.destroy();
}
result.map(|_| req)
}
pub fn fs_lchown<CB: Into<crate::FsCB<'static>>>(
&self,
path: &str,
uid: Uid,
gid: Gid,
cb: CB,
) -> FsReqErrResult {
self._fs_lchown(path, uid, gid, cb)
}
pub fn fs_lchown_sync(&self, path: &str, uid: Uid, gid: Gid) -> SyncErrResult {
self._fs_lchown(path, uid, gid, ())
.and_then(destroy_req_return_boxed_result)
}
}
pub struct ScandirIter {
pub req: FsReq,
}
impl Iterator for ScandirIter {
type Item = crate::Result<crate::Dirent>;
fn next(&mut self) -> Option<Self::Item> {
let mut dirent: uv::uv_dirent_t = unsafe { std::mem::zeroed() };
let result =
crate::uvret(unsafe { uv_fs_scandir_next(self.req.inner(), &mut dirent as _) });
match result {
Ok(_) => Some(Ok(crate::Dirent::from_inner(
&dirent as *const uv::uv_dirent_t,
))),
Err(crate::Error::EOF) => None,
Err(e) => Some(Err(e)),
}
}
}
impl std::iter::FusedIterator for ScandirIter {}
impl Drop for ScandirIter {
fn drop(&mut self) {
self.req.destroy();
}
}