rtio: Remove usage of `Path`

The rtio interface is a thin low-level interface over the I/O subsystems, and
the `Path` type is a little too high-level for this interface.
This commit is contained in:
Alex Crichton 2014-06-02 22:11:19 -07:00
parent b830b4b86b
commit a3f9aa9ef8
7 changed files with 28 additions and 24 deletions

View File

@ -361,17 +361,17 @@ pub fn mkdir(p: &CString, mode: io::FilePermission) -> IoResult<()> {
})) }))
} }
pub fn readdir(p: &CString) -> IoResult<Vec<Path>> { pub fn readdir(p: &CString) -> IoResult<Vec<CString>> {
use libc::{dirent_t}; use libc::{dirent_t};
use libc::{opendir, readdir_r, closedir}; use libc::{opendir, readdir_r, closedir};
fn prune(root: &CString, dirs: Vec<Path>) -> Vec<Path> { fn prune(root: &CString, dirs: Vec<Path>) -> Vec<CString> {
let root = unsafe { CString::new(root.with_ref(|p| p), false) }; let root = unsafe { CString::new(root.with_ref(|p| p), false) };
let root = Path::new(root); let root = Path::new(root);
dirs.move_iter().filter(|path| { dirs.move_iter().filter(|path| {
path.as_vec() != bytes!(".") && path.as_vec() != bytes!("..") path.as_vec() != bytes!(".") && path.as_vec() != bytes!("..")
}).map(|path| root.join(path)).collect() }).map(|path| root.join(path).to_c_str()).collect()
} }
extern { extern {
@ -431,7 +431,7 @@ pub fn chown(p: &CString, uid: int, gid: int) -> IoResult<()> {
})) }))
} }
pub fn readlink(p: &CString) -> IoResult<Path> { pub fn readlink(p: &CString) -> IoResult<CString> {
let p = p.with_ref(|p| p); let p = p.with_ref(|p| p);
let mut len = unsafe { libc::pathconf(p, libc::_PC_NAME_MAX) }; let mut len = unsafe { libc::pathconf(p, libc::_PC_NAME_MAX) };
if len == -1 { if len == -1 {
@ -446,7 +446,7 @@ pub fn readlink(p: &CString) -> IoResult<Path> {
n => { n => {
assert!(n > 0); assert!(n > 0);
unsafe { buf.set_len(n as uint); } unsafe { buf.set_len(n as uint); }
Ok(Path::new(buf)) Ok(buf.as_slice().to_c_str())
} }
} }
} }

View File

@ -347,16 +347,16 @@ pub fn mkdir(p: &CString, _mode: io::FilePermission) -> IoResult<()> {
}) })
} }
pub fn readdir(p: &CString) -> IoResult<Vec<Path>> { pub fn readdir(p: &CString) -> IoResult<Vec<CString>> {
use std::rt::libc_heap::malloc_raw; use std::rt::libc_heap::malloc_raw;
fn prune(root: &CString, dirs: Vec<Path>) -> Vec<Path> { fn prune(root: &CString, dirs: Vec<Path>) -> Vec<CString> {
let root = unsafe { CString::new(root.with_ref(|p| p), false) }; let root = unsafe { CString::new(root.with_ref(|p| p), false) };
let root = Path::new(root); let root = Path::new(root);
dirs.move_iter().filter(|path| { dirs.move_iter().filter(|path| {
path.as_vec() != bytes!(".") && path.as_vec() != bytes!("..") path.as_vec() != bytes!(".") && path.as_vec() != bytes!("..")
}).map(|path| root.join(path)).collect() }).map(|path| root.join(path).to_c_str()).collect()
} }
extern { extern {

View File

@ -232,7 +232,7 @@ impl rtio::IoFactory for IoFactory {
fn fs_rename(&mut self, path: &CString, to: &CString) -> IoResult<()> { fn fs_rename(&mut self, path: &CString, to: &CString) -> IoResult<()> {
file::rename(path, to) file::rename(path, to)
} }
fn fs_readdir(&mut self, path: &CString, _flags: c_int) -> IoResult<Vec<Path>> { fn fs_readdir(&mut self, path: &CString, _flags: c_int) -> IoResult<Vec<CString>> {
file::readdir(path) file::readdir(path)
} }
fn fs_lstat(&mut self, path: &CString) -> IoResult<io::FileStat> { fn fs_lstat(&mut self, path: &CString) -> IoResult<io::FileStat> {
@ -241,7 +241,7 @@ impl rtio::IoFactory for IoFactory {
fn fs_chown(&mut self, path: &CString, uid: int, gid: int) -> IoResult<()> { fn fs_chown(&mut self, path: &CString, uid: int, gid: int) -> IoResult<()> {
file::chown(path, uid, gid) file::chown(path, uid, gid)
} }
fn fs_readlink(&mut self, path: &CString) -> IoResult<Path> { fn fs_readlink(&mut self, path: &CString) -> IoResult<CString> {
file::readlink(path) file::readlink(path)
} }
fn fs_symlink(&mut self, src: &CString, dst: &CString) -> IoResult<()> { fn fs_symlink(&mut self, src: &CString, dst: &CString) -> IoResult<()> {

View File

@ -157,7 +157,7 @@ impl FsRequest {
} }
pub fn readdir(loop_: &Loop, path: &CString, flags: c_int) pub fn readdir(loop_: &Loop, path: &CString, flags: c_int)
-> Result<Vec<Path>, UvError> -> Result<Vec<CString>, UvError>
{ {
execute(|req, cb| unsafe { execute(|req, cb| unsafe {
uvll::uv_fs_readdir(loop_.handle, uvll::uv_fs_readdir(loop_.handle,
@ -170,20 +170,22 @@ impl FsRequest {
Some(req.get_result() as uint), Some(req.get_result() as uint),
|rel| { |rel| {
let p = rel.as_bytes(); let p = rel.as_bytes();
paths.push(parent.join(p.slice_to(rel.len()))); paths.push(parent.join(p.slice_to(rel.len())).to_c_str());
}); });
paths paths
}) })
} }
pub fn readlink(loop_: &Loop, path: &CString) -> Result<Path, UvError> { pub fn readlink(loop_: &Loop, path: &CString) -> Result<CString, UvError> {
execute(|req, cb| unsafe { execute(|req, cb| unsafe {
uvll::uv_fs_readlink(loop_.handle, req, uvll::uv_fs_readlink(loop_.handle, req,
path.with_ref(|p| p), cb) path.with_ref(|p| p), cb)
}).map(|req| { }).map(|req| {
Path::new(unsafe { // Be sure to clone the cstring so we get an independently owned
CString::new(req.get_ptr() as *libc::c_char, false) // allocation to work with and return.
}) unsafe {
CString::new(req.get_ptr() as *libc::c_char, false).clone()
}
}) })
} }

View File

@ -22,7 +22,6 @@ use libc::c_int;
use libc::{O_CREAT, O_APPEND, O_TRUNC, O_RDWR, O_RDONLY, O_WRONLY, S_IRUSR, use libc::{O_CREAT, O_APPEND, O_TRUNC, O_RDWR, O_RDONLY, O_WRONLY, S_IRUSR,
S_IWUSR}; S_IWUSR};
use libc; use libc;
use std::path::Path;
use std::rt::rtio; use std::rt::rtio;
use std::rt::rtio::{ProcessConfig, IoFactory, EventLoop}; use std::rt::rtio::{ProcessConfig, IoFactory, EventLoop};
use ai = std::io::net::addrinfo; use ai = std::io::net::addrinfo;
@ -241,7 +240,7 @@ impl IoFactory for UvIoFactory {
r.map_err(uv_error_to_io_error) r.map_err(uv_error_to_io_error)
} }
fn fs_readdir(&mut self, path: &CString, flags: c_int) fn fs_readdir(&mut self, path: &CString, flags: c_int)
-> Result<Vec<Path>, IoError> -> Result<Vec<CString>, IoError>
{ {
let r = FsRequest::readdir(&self.loop_, path, flags); let r = FsRequest::readdir(&self.loop_, path, flags);
r.map_err(uv_error_to_io_error) r.map_err(uv_error_to_io_error)
@ -258,7 +257,7 @@ impl IoFactory for UvIoFactory {
let r = FsRequest::chown(&self.loop_, path, uid, gid); let r = FsRequest::chown(&self.loop_, path, uid, gid);
r.map_err(uv_error_to_io_error) r.map_err(uv_error_to_io_error)
} }
fn fs_readlink(&mut self, path: &CString) -> Result<Path, IoError> { fn fs_readlink(&mut self, path: &CString) -> Result<CString, IoError> {
let r = FsRequest::readlink(&self.loop_, path); let r = FsRequest::readlink(&self.loop_, path);
r.map_err(uv_error_to_io_error) r.map_err(uv_error_to_io_error)
} }

View File

@ -410,7 +410,9 @@ pub fn symlink(src: &Path, dst: &Path) -> IoResult<()> {
/// This function will return an error on failure. Failure conditions include /// This function will return an error on failure. Failure conditions include
/// reading a file that does not exist or reading a file which is not a symlink. /// reading a file that does not exist or reading a file which is not a symlink.
pub fn readlink(path: &Path) -> IoResult<Path> { pub fn readlink(path: &Path) -> IoResult<Path> {
LocalIo::maybe_raise(|io| io.fs_readlink(&path.to_c_str())) LocalIo::maybe_raise(|io| {
Ok(Path::new(try!(io.fs_readlink(&path.to_c_str()))))
})
} }
/// Create a new, empty directory at the provided path /// Create a new, empty directory at the provided path
@ -487,7 +489,9 @@ pub fn rmdir(path: &Path) -> IoResult<()> {
/// file /// file
pub fn readdir(path: &Path) -> IoResult<Vec<Path>> { pub fn readdir(path: &Path) -> IoResult<Vec<Path>> {
LocalIo::maybe_raise(|io| { LocalIo::maybe_raise(|io| {
io.fs_readdir(&path.to_c_str(), 0) Ok(try!(io.fs_readdir(&path.to_c_str(), 0)).move_iter().map(|a| {
Path::new(a)
}).collect())
}) })
} }

View File

@ -19,7 +19,6 @@ use mem;
use ops::Drop; use ops::Drop;
use option::{Option, Some, None}; use option::{Option, Some, None};
use owned::Box; use owned::Box;
use path::Path;
use result::Err; use result::Err;
use rt::local::Local; use rt::local::Local;
use rt::task::Task; use rt::task::Task;
@ -223,11 +222,11 @@ pub trait IoFactory {
fn fs_rmdir(&mut self, path: &CString) -> IoResult<()>; fn fs_rmdir(&mut self, path: &CString) -> IoResult<()>;
fn fs_rename(&mut self, path: &CString, to: &CString) -> IoResult<()>; fn fs_rename(&mut self, path: &CString, to: &CString) -> IoResult<()>;
fn fs_readdir(&mut self, path: &CString, flags: c_int) -> fn fs_readdir(&mut self, path: &CString, flags: c_int) ->
IoResult<Vec<Path>>; IoResult<Vec<CString>>;
fn fs_lstat(&mut self, path: &CString) -> IoResult<FileStat>; fn fs_lstat(&mut self, path: &CString) -> IoResult<FileStat>;
fn fs_chown(&mut self, path: &CString, uid: int, gid: int) -> fn fs_chown(&mut self, path: &CString, uid: int, gid: int) ->
IoResult<()>; IoResult<()>;
fn fs_readlink(&mut self, path: &CString) -> IoResult<Path>; fn fs_readlink(&mut self, path: &CString) -> IoResult<CString>;
fn fs_symlink(&mut self, src: &CString, dst: &CString) -> IoResult<()>; fn fs_symlink(&mut self, src: &CString, dst: &CString) -> IoResult<()>;
fn fs_link(&mut self, src: &CString, dst: &CString) -> IoResult<()>; fn fs_link(&mut self, src: &CString, dst: &CString) -> IoResult<()>;
fn fs_utime(&mut self, src: &CString, atime: u64, mtime: u64) -> fn fs_utime(&mut self, src: &CString, atime: u64, mtime: u64) ->