use sys::{cvt, cvt_r};
use sys_common::{AsInner, FromInner};
-#[cfg(target_os = "linux")]
+#[cfg(any(target_os = "linux", target_os = "emscripten"))]
use libc::{stat64, fstat64, lstat64, off64_t, ftruncate64, lseek64, dirent64, readdir64_r, open64};
#[cfg(target_os = "android")]
use libc::{stat as stat64, fstat as fstat64, lstat as lstat64, off64_t, ftruncate64, lseek64,
dirent as dirent64, open as open64};
-#[cfg(not(any(target_os = "linux", target_os = "android")))]
+#[cfg(not(any(target_os = "linux",
+ target_os = "emscripten",
+ target_os = "android")))]
use libc::{stat as stat64, fstat as fstat64, lstat as lstat64, off_t as off64_t,
ftruncate as ftruncate64, lseek as lseek64, dirent as dirent64, open as open64};
-#[cfg(not(any(target_os = "linux", target_os = "solaris")))]
+#[cfg(not(any(target_os = "linux",
+ target_os = "emscripten",
+ target_os = "solaris")))]
use libc::{readdir_r as readdir64_r};
pub struct File(FileDesc);
impl File {
pub fn open(path: &Path, opts: &OpenOptions) -> io::Result<File> {
- let path = try!(cstr(path));
+ let path = cstr(path)?;
File::open_c(&path, opts)
}
pub fn open_c(path: &CStr, opts: &OpenOptions) -> io::Result<File> {
let flags = libc::O_CLOEXEC |
- try!(opts.get_access_mode()) |
- try!(opts.get_creation_mode()) |
+ opts.get_access_mode()? |
+ opts.get_creation_mode()? |
(opts.custom_flags as c_int & !libc::O_ACCMODE);
- let fd = try!(cvt_r(|| unsafe {
+ let fd = cvt_r(|| unsafe {
open64(path.as_ptr(), flags, opts.mode as c_int)
- }));
+ })?;
let fd = FileDesc::new(fd);
// Currently the standard library supports Linux 2.6.18 which did not
pub fn file_attr(&self) -> io::Result<FileAttr> {
let mut stat: stat64 = unsafe { mem::zeroed() };
- try!(cvt(unsafe {
+ cvt(unsafe {
fstat64(self.0.raw(), &mut stat)
- }));
+ })?;
Ok(FileAttr { stat: stat })
}
pub fn fsync(&self) -> io::Result<()> {
- try!(cvt_r(|| unsafe { libc::fsync(self.0.raw()) }));
+ cvt_r(|| unsafe { libc::fsync(self.0.raw()) })?;
Ok(())
}
pub fn datasync(&self) -> io::Result<()> {
- try!(cvt_r(|| unsafe { os_datasync(self.0.raw()) }));
+ cvt_r(|| unsafe { os_datasync(self.0.raw()) })?;
return Ok(());
#[cfg(any(target_os = "macos", target_os = "ios"))]
}
pub fn truncate(&self, size: u64) -> io::Result<()> {
- try!(cvt_r(|| unsafe {
+ cvt_r(|| unsafe {
ftruncate64(self.0.raw(), size as off64_t)
- }));
+ })?;
Ok(())
}
self.0.read(buf)
}
+ pub fn read_to_end(&self, buf: &mut Vec<u8>) -> io::Result<usize> {
+ self.0.read_to_end(buf)
+ }
+
pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
self.0.write(buf)
}
SeekFrom::End(off) => (libc::SEEK_END, off as off64_t),
SeekFrom::Current(off) => (libc::SEEK_CUR, off as off64_t),
};
- let n = try!(cvt(unsafe { lseek64(self.0.raw(), pos, whence) }));
+ let n = cvt(unsafe { lseek64(self.0.raw(), pos, whence) })?;
Ok(n as u64)
}
}
pub fn mkdir(&self, p: &Path) -> io::Result<()> {
- let p = try!(cstr(p));
- try!(cvt(unsafe { libc::mkdir(p.as_ptr(), self.mode) }));
+ let p = cstr(p)?;
+ cvt(unsafe { libc::mkdir(p.as_ptr(), self.mode) })?;
Ok(())
}
}
fn cstr(path: &Path) -> io::Result<CString> {
- Ok(try!(CString::new(path.as_os_str().as_bytes())))
+ Ok(CString::new(path.as_os_str().as_bytes())?)
}
impl FromInner<c_int> for File {
pub fn readdir(p: &Path) -> io::Result<ReadDir> {
let root = Arc::new(p.to_path_buf());
- let p = try!(cstr(p));
+ let p = cstr(p)?;
unsafe {
let ptr = libc::opendir(p.as_ptr());
if ptr.is_null() {
}
pub fn unlink(p: &Path) -> io::Result<()> {
- let p = try!(cstr(p));
- try!(cvt(unsafe { libc::unlink(p.as_ptr()) }));
+ let p = cstr(p)?;
+ cvt(unsafe { libc::unlink(p.as_ptr()) })?;
Ok(())
}
pub fn rename(old: &Path, new: &Path) -> io::Result<()> {
- let old = try!(cstr(old));
- let new = try!(cstr(new));
- try!(cvt(unsafe { libc::rename(old.as_ptr(), new.as_ptr()) }));
+ let old = cstr(old)?;
+ let new = cstr(new)?;
+ cvt(unsafe { libc::rename(old.as_ptr(), new.as_ptr()) })?;
Ok(())
}
pub fn set_perm(p: &Path, perm: FilePermissions) -> io::Result<()> {
- let p = try!(cstr(p));
- try!(cvt_r(|| unsafe { libc::chmod(p.as_ptr(), perm.mode) }));
+ let p = cstr(p)?;
+ cvt_r(|| unsafe { libc::chmod(p.as_ptr(), perm.mode) })?;
Ok(())
}
pub fn rmdir(p: &Path) -> io::Result<()> {
- let p = try!(cstr(p));
- try!(cvt(unsafe { libc::rmdir(p.as_ptr()) }));
+ let p = cstr(p)?;
+ cvt(unsafe { libc::rmdir(p.as_ptr()) })?;
Ok(())
}
pub fn remove_dir_all(path: &Path) -> io::Result<()> {
- let filetype = try!(lstat(path)).file_type();
+ let filetype = lstat(path)?.file_type();
if filetype.is_symlink() {
unlink(path)
} else {
}
fn remove_dir_all_recursive(path: &Path) -> io::Result<()> {
- for child in try!(readdir(path)) {
- let child = try!(child);
- if try!(child.file_type()).is_dir() {
- try!(remove_dir_all_recursive(&child.path()));
+ for child in readdir(path)? {
+ let child = child?;
+ if child.file_type()?.is_dir() {
+ remove_dir_all_recursive(&child.path())?;
} else {
- try!(unlink(&child.path()));
+ unlink(&child.path())?;
}
}
rmdir(path)
}
pub fn readlink(p: &Path) -> io::Result<PathBuf> {
- let c_path = try!(cstr(p));
+ let c_path = cstr(p)?;
let p = c_path.as_ptr();
let mut buf = Vec::with_capacity(256);
loop {
- let buf_read = try!(cvt(unsafe {
+ let buf_read = cvt(unsafe {
libc::readlink(p, buf.as_mut_ptr() as *mut _, buf.capacity() as libc::size_t)
- })) as usize;
+ })? as usize;
unsafe { buf.set_len(buf_read); }
}
pub fn symlink(src: &Path, dst: &Path) -> io::Result<()> {
- let src = try!(cstr(src));
- let dst = try!(cstr(dst));
- try!(cvt(unsafe { libc::symlink(src.as_ptr(), dst.as_ptr()) }));
+ let src = cstr(src)?;
+ let dst = cstr(dst)?;
+ cvt(unsafe { libc::symlink(src.as_ptr(), dst.as_ptr()) })?;
Ok(())
}
pub fn link(src: &Path, dst: &Path) -> io::Result<()> {
- let src = try!(cstr(src));
- let dst = try!(cstr(dst));
- try!(cvt(unsafe { libc::link(src.as_ptr(), dst.as_ptr()) }));
+ let src = cstr(src)?;
+ let dst = cstr(dst)?;
+ cvt(unsafe { libc::link(src.as_ptr(), dst.as_ptr()) })?;
Ok(())
}
pub fn stat(p: &Path) -> io::Result<FileAttr> {
- let p = try!(cstr(p));
+ let p = cstr(p)?;
let mut stat: stat64 = unsafe { mem::zeroed() };
- try!(cvt(unsafe {
+ cvt(unsafe {
stat64(p.as_ptr(), &mut stat as *mut _ as *mut _)
- }));
+ })?;
Ok(FileAttr { stat: stat })
}
pub fn lstat(p: &Path) -> io::Result<FileAttr> {
- let p = try!(cstr(p));
+ let p = cstr(p)?;
let mut stat: stat64 = unsafe { mem::zeroed() };
- try!(cvt(unsafe {
+ cvt(unsafe {
lstat64(p.as_ptr(), &mut stat as *mut _ as *mut _)
- }));
+ })?;
Ok(FileAttr { stat: stat })
}
pub fn canonicalize(p: &Path) -> io::Result<PathBuf> {
- let path = try!(CString::new(p.as_os_str().as_bytes()));
+ let path = CString::new(p.as_os_str().as_bytes())?;
let buf;
unsafe {
let r = libc::realpath(path.as_ptr(), ptr::null_mut());
"the source path is not an existing regular file"))
}
- let mut reader = try!(File::open(from));
- let mut writer = try!(File::create(to));
- let perm = try!(reader.metadata()).permissions();
+ let mut reader = File::open(from)?;
+ let mut writer = File::create(to)?;
+ let perm = reader.metadata()?.permissions();
- let ret = try!(io::copy(&mut reader, &mut writer));
- try!(set_permissions(to, perm));
+ let ret = io::copy(&mut reader, &mut writer)?;
+ set_permissions(to, perm)?;
Ok(ret)
}