diff options
Diffstat (limited to 'src/libstd/sys/unix')
| -rw-r--r-- | src/libstd/sys/unix/c.rs | 3 | ||||
| -rw-r--r-- | src/libstd/sys/unix/ext.rs | 48 | ||||
| -rw-r--r-- | src/libstd/sys/unix/fd.rs | 70 | ||||
| -rw-r--r-- | src/libstd/sys/unix/fs2.rs | 378 | ||||
| -rw-r--r-- | src/libstd/sys/unix/mod.rs | 27 |
5 files changed, 520 insertions, 6 deletions
diff --git a/src/libstd/sys/unix/c.rs b/src/libstd/sys/unix/c.rs index 50a8e6b73e3..9b6ea3218ca 100644 --- a/src/libstd/sys/unix/c.rs +++ b/src/libstd/sys/unix/c.rs @@ -149,6 +149,9 @@ extern { buf: *mut libc::c_char, buflen: libc::size_t, result: *mut *mut passwd) -> libc::c_int; + + pub fn utimes(filename: *const libc::c_char, + times: *const libc::timeval) -> libc::c_int; } #[cfg(any(target_os = "macos", target_os = "ios"))] diff --git a/src/libstd/sys/unix/ext.rs b/src/libstd/sys/unix/ext.rs index 4a82b2807e7..689bbda8322 100644 --- a/src/libstd/sys/unix/ext.rs +++ b/src/libstd/sys/unix/ext.rs @@ -31,11 +31,14 @@ #![unstable(feature = "std_misc")] -use vec::Vec; -use sys::os_str::Buf; -use sys_common::{AsInner, IntoInner, FromInner}; use ffi::{OsStr, OsString}; +use fs::{Permissions, OpenOptions}; +use fs; use libc; +use mem; +use sys::os_str::Buf; +use sys_common::{AsInner, AsInnerMut, IntoInner, FromInner}; +use vec::Vec; use old_io; @@ -54,6 +57,12 @@ impl AsRawFd for old_io::fs::File { } } +impl AsRawFd for fs::File { + fn as_raw_fd(&self) -> Fd { + self.as_inner().fd().raw() + } +} + impl AsRawFd for old_io::pipe::PipeStream { fn as_raw_fd(&self) -> Fd { self.as_inner().fd() @@ -123,18 +132,49 @@ impl OsStringExt for OsString { // Unix-specific extensions to `OsStr`. pub trait OsStrExt { + fn from_byte_slice(slice: &[u8]) -> &OsStr; fn as_byte_slice(&self) -> &[u8]; } impl OsStrExt for OsStr { + fn from_byte_slice(slice: &[u8]) -> &OsStr { + unsafe { mem::transmute(slice) } + } fn as_byte_slice(&self) -> &[u8] { &self.as_inner().inner } } +// Unix-specific extensions to `Permissions` +pub trait PermissionsExt { + fn set_mode(&mut self, mode: i32); +} + +impl PermissionsExt for Permissions { + fn set_mode(&mut self, mode: i32) { + *self = FromInner::from_inner(FromInner::from_inner(mode)); + } +} + +// Unix-specific extensions to `OpenOptions` +pub trait OpenOptionsExt { + /// Set the mode bits that a new file will be created with. + /// + /// If a new file is created as part of a `File::open_opts` call then this + /// specified `mode` will be used as the permission bits for the new file. + fn mode(&mut self, mode: i32) -> &mut Self; +} + +impl OpenOptionsExt for OpenOptions { + fn mode(&mut self, mode: i32) -> &mut OpenOptions { + self.as_inner_mut().mode(mode); self + } +} + /// A prelude for conveniently writing platform-specific code. /// /// Includes all extension traits, and some important type definitions. pub mod prelude { - pub use super::{Fd, AsRawFd}; + #[doc(no_inline)] + pub use super::{Fd, AsRawFd, OsStrExt, OsStringExt, PermissionsExt}; } diff --git a/src/libstd/sys/unix/fd.rs b/src/libstd/sys/unix/fd.rs new file mode 100644 index 00000000000..f0943de5378 --- /dev/null +++ b/src/libstd/sys/unix/fd.rs @@ -0,0 +1,70 @@ +// Copyright 2015 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +use core::prelude::*; +use io::prelude::*; + +use io; +use libc::{self, c_int, size_t, c_void}; +use mem; +use sys::cvt; + +pub type fd_t = c_int; + +pub struct FileDesc { + fd: c_int, +} + +impl FileDesc { + pub fn new(fd: c_int) -> FileDesc { + FileDesc { fd: fd } + } + + pub fn raw(&self) -> c_int { self.fd } + + /// Extract the actual filedescriptor without closing it. + pub fn into_raw(self) -> c_int { + let fd = self.fd; + unsafe { mem::forget(self) }; + fd + } + + pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> { + let ret = try!(cvt(unsafe { + libc::read(self.fd, + buf.as_mut_ptr() as *mut c_void, + buf.len() as size_t) + })); + Ok(ret as usize) + } + + pub fn write(&self, buf: &[u8]) -> io::Result<usize> { + let ret = try!(cvt(unsafe { + libc::write(self.fd, + buf.as_ptr() as *const c_void, + buf.len() as size_t) + })); + Ok(ret as usize) + } +} + +impl Drop for FileDesc { + fn drop(&mut self) { + // closing stdio file handles makes no sense, so never do it. Also, note + // that errors are ignored when closing a file descriptor. The reason + // for this is that if an error occurs we don't actually know if the + // file descriptor was closed or not, and if we retried (for something + // like EINTR), we might close another valid file descriptor (opened + // after we closed ours. + if self.fd > libc::STDERR_FILENO { + let _ = unsafe { libc::close(self.fd) }; + } + } +} diff --git a/src/libstd/sys/unix/fs2.rs b/src/libstd/sys/unix/fs2.rs new file mode 100644 index 00000000000..070b3248349 --- /dev/null +++ b/src/libstd/sys/unix/fs2.rs @@ -0,0 +1,378 @@ +// Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +use core::prelude::*; +use io::prelude::*; +use os::unix::prelude::*; + +use ffi::{self, CString, OsString, AsOsStr, OsStr}; +use io::{self, Error, Seek, SeekFrom}; +use libc::{self, c_int, c_void, size_t, off_t, c_char, mode_t}; +use mem; +use path::{Path, PathBuf}; +use ptr; +use rc::Rc; +use sys::fd::FileDesc; +use sys::{c, cvt, cvt_r}; +use sys_common::FromInner; +use vec::Vec; + +pub struct File(FileDesc); + +pub struct FileAttr { + stat: libc::stat, +} + +pub struct ReadDir { + dirp: *mut libc::DIR, + root: Rc<PathBuf>, +} + +pub struct DirEntry { + buf: Vec<u8>, + dirent: *mut libc::dirent_t, + root: Rc<PathBuf>, +} + +#[derive(Clone)] +pub struct OpenOptions { + flags: c_int, + read: bool, + write: bool, + mode: mode_t, +} + +#[derive(Clone, PartialEq, Eq, Debug)] +pub struct FilePermissions { mode: mode_t } + +impl FileAttr { + pub fn is_dir(&self) -> bool { + (self.stat.st_mode as mode_t) & libc::S_IFMT == libc::S_IFDIR + } + pub fn is_file(&self) -> bool { + (self.stat.st_mode as mode_t) & libc::S_IFMT == libc::S_IFREG + } + pub fn size(&self) -> u64 { self.stat.st_size as u64 } + pub fn perm(&self) -> FilePermissions { + FilePermissions { mode: (self.stat.st_mode as mode_t) & 0o777 } + } + + pub fn accessed(&self) -> u64 { + self.mktime(self.stat.st_atime as u64, self.stat.st_atime_nsec as u64) + } + pub fn modified(&self) -> u64 { + self.mktime(self.stat.st_mtime as u64, self.stat.st_mtime_nsec as u64) + } + + // times are in milliseconds (currently) + fn mktime(&self, secs: u64, nsecs: u64) -> u64 { + secs * 1000 + nsecs / 1000000 + } +} + +impl FilePermissions { + pub fn readonly(&self) -> bool { self.mode & 0o222 == 0 } + pub fn set_readonly(&mut self, readonly: bool) { + if readonly { + self.mode &= !0o222; + } else { + self.mode |= 0o222; + } + } +} + +impl FromInner<i32> for FilePermissions { + fn from_inner(mode: i32) -> FilePermissions { + FilePermissions { mode: mode as mode_t } + } +} + +impl Iterator for ReadDir { + type Item = io::Result<DirEntry>; + + fn next(&mut self) -> Option<io::Result<DirEntry>> { + extern { + fn rust_dirent_t_size() -> c_int; + } + + let mut buf: Vec<u8> = Vec::with_capacity(unsafe { + rust_dirent_t_size() as usize + }); + let ptr = buf.as_mut_ptr() as *mut libc::dirent_t; + + let mut entry_ptr = ptr::null_mut(); + loop { + if unsafe { libc::readdir_r(self.dirp, ptr, &mut entry_ptr) != 0 } { + return Some(Err(Error::last_os_error())) + } + if entry_ptr.is_null() { + return None + } + + let entry = DirEntry { + buf: buf, + dirent: entry_ptr, + root: self.root.clone() + }; + if entry.name_bytes() == b"." || entry.name_bytes() == b".." { + buf = entry.buf; + } else { + return Some(Ok(entry)) + } + } + } +} + +impl Drop for ReadDir { + fn drop(&mut self) { + let r = unsafe { libc::closedir(self.dirp) }; + debug_assert_eq!(r, 0); + } +} + +impl DirEntry { + pub fn path(&self) -> PathBuf { + self.root.join(<OsStr as OsStrExt>::from_byte_slice(self.name_bytes())) + } + + fn name_bytes(&self) -> &[u8] { + extern { + fn rust_list_dir_val(ptr: *mut libc::dirent_t) -> *const c_char; + } + unsafe { + let ptr = rust_list_dir_val(self.dirent); + ffi::c_str_to_bytes(mem::copy_lifetime(self, &ptr)) + } + } +} + +impl OpenOptions { + pub fn new() -> OpenOptions { + OpenOptions { + flags: 0, + read: false, + write: false, + mode: libc::S_IRUSR | libc::S_IWUSR, + } + } + + pub fn read(&mut self, read: bool) { + self.read = read; + } + + pub fn write(&mut self, write: bool) { + self.write = write; + } + + pub fn append(&mut self, append: bool) { + self.flag(libc::O_APPEND, append); + } + + pub fn truncate(&mut self, truncate: bool) { + self.flag(libc::O_TRUNC, truncate); + } + + pub fn create(&mut self, create: bool) { + self.flag(libc::O_CREAT, create); + } + + pub fn mode(&mut self, mode: i32) { + self.mode = mode as mode_t; + } + + fn flag(&mut self, bit: c_int, on: bool) { + if on { + self.flags |= bit; + } else { + self.flags &= !bit; + } + } +} + +impl File { + pub fn open(path: &Path, opts: &OpenOptions) -> io::Result<File> { + let flags = opts.flags | match (opts.read, opts.write) { + (true, true) => libc::O_RDWR, + (false, true) => libc::O_WRONLY, + (true, false) | + (false, false) => libc::O_RDONLY, + }; + let path = cstr(path); + let fd = try!(cvt_r(|| unsafe { + libc::open(path.as_ptr(), flags, opts.mode) + })); + Ok(File(FileDesc::new(fd))) + } + + pub fn file_attr(&self) -> io::Result<FileAttr> { + let mut stat: libc::stat = unsafe { mem::zeroed() }; + try!(cvt(unsafe { libc::fstat(self.0.raw(), &mut stat) })); + Ok(FileAttr { stat: stat }) + } + + pub fn fsync(&self) -> io::Result<()> { + try!(cvt_r(|| unsafe { libc::fsync(self.0.raw()) })); + Ok(()) + } + + pub fn datasync(&self) -> io::Result<()> { + try!(cvt_r(|| unsafe { os_datasync(self.0.raw()) })); + return Ok(()); + + #[cfg(any(target_os = "macos", target_os = "ios"))] + unsafe fn os_datasync(fd: c_int) -> c_int { + libc::fcntl(fd, libc::F_FULLFSYNC) + } + #[cfg(target_os = "linux")] + unsafe fn os_datasync(fd: c_int) -> c_int { libc::fdatasync(fd) } + #[cfg(not(any(target_os = "macos", + target_os = "ios", + target_os = "linux")))] + unsafe fn os_datasync(fd: c_int) -> c_int { libc::fsync(fd) } + } + + pub fn truncate(&self, size: u64) -> io::Result<()> { + try!(cvt_r(|| unsafe { + libc::ftruncate(self.0.raw(), size as libc::off_t) + })); + Ok(()) + } + + pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> { + self.0.read(buf) + } + + pub fn write(&self, buf: &[u8]) -> io::Result<usize> { + self.0.write(buf) + } + + pub fn flush(&self) -> io::Result<()> { Ok(()) } + + pub fn seek(&self, pos: SeekFrom) -> io::Result<u64> { + let (whence, pos) = match pos { + SeekFrom::Start(off) => (libc::SEEK_SET, off as off_t), + SeekFrom::End(off) => (libc::SEEK_END, off as off_t), + SeekFrom::Current(off) => (libc::SEEK_CUR, off as off_t), + }; + let n = try!(cvt(unsafe { libc::lseek(self.0.raw(), pos, whence) })); + Ok(n as u64) + } + + pub fn fd(&self) -> &FileDesc { &self.0 } +} + +fn cstr(path: &Path) -> CString { + CString::from_slice(path.as_os_str().as_byte_slice()) +} + +pub fn mkdir(p: &Path) -> io::Result<()> { + let p = cstr(p); + try!(cvt(unsafe { libc::mkdir(p.as_ptr(), 0o777) })); + Ok(()) +} + +pub fn readdir(p: &Path) -> io::Result<ReadDir> { + let root = Rc::new(p.to_path_buf()); + let p = cstr(p); + unsafe { + let ptr = libc::opendir(p.as_ptr()); + if ptr.is_null() { + Err(Error::last_os_error()) + } else { + Ok(ReadDir { dirp: ptr, root: root }) + } + } +} + +pub fn unlink(p: &Path) -> io::Result<()> { + let p = cstr(p); + try!(cvt(unsafe { libc::unlink(p.as_ptr()) })); + Ok(()) +} + +pub fn rename(old: &Path, new: &Path) -> io::Result<()> { + let old = cstr(old); + let new = cstr(new); + try!(cvt(unsafe { libc::rename(old.as_ptr(), new.as_ptr()) })); + Ok(()) +} + +pub fn set_perm(p: &Path, perm: FilePermissions) -> io::Result<()> { + let p = cstr(p); + try!(cvt_r(|| unsafe { libc::chmod(p.as_ptr(), perm.mode) })); + Ok(()) +} + +pub fn rmdir(p: &Path) -> io::Result<()> { + let p = cstr(p); + try!(cvt(unsafe { libc::rmdir(p.as_ptr()) })); + Ok(()) +} + +pub fn chown(p: &Path, uid: isize, gid: isize) -> io::Result<()> { + let p = cstr(p); + try!(cvt_r(|| unsafe { + libc::chown(p.as_ptr(), uid as libc::uid_t, gid as libc::gid_t) + })); + Ok(()) +} + +pub fn readlink(p: &Path) -> io::Result<PathBuf> { + let c_path = cstr(p); + let p = c_path.as_ptr(); + let mut len = unsafe { libc::pathconf(p as *mut _, libc::_PC_NAME_MAX) }; + if len < 0 { + len = 1024; // FIXME: read PATH_MAX from C ffi? + } + let mut buf: Vec<u8> = Vec::with_capacity(len as usize); + unsafe { + let n = try!(cvt({ + libc::readlink(p, buf.as_ptr() as *mut c_char, len as size_t) + })); + buf.set_len(n as usize); + } + let s: OsString = OsStringExt::from_vec(buf); + Ok(PathBuf::new(&s)) +} + +pub fn symlink(src: &Path, dst: &Path) -> io::Result<()> { + let src = cstr(src); + let dst = cstr(dst); + try!(cvt(unsafe { libc::symlink(src.as_ptr(), dst.as_ptr()) })); + Ok(()) +} + +pub fn link(src: &Path, dst: &Path) -> io::Result<()> { + let src = cstr(src); + let dst = cstr(dst); + try!(cvt(unsafe { libc::link(src.as_ptr(), dst.as_ptr()) })); + Ok(()) +} + +pub fn stat(p: &Path) -> io::Result<FileAttr> { + let p = cstr(p); + let mut stat: libc::stat = unsafe { mem::zeroed() }; + try!(cvt(unsafe { libc::stat(p.as_ptr(), &mut stat) })); + Ok(FileAttr { stat: stat }) +} + +pub fn lstat(p: &Path) -> io::Result<FileAttr> { + let p = cstr(p); + let mut stat: libc::stat = unsafe { mem::zeroed() }; + try!(cvt(unsafe { libc::lstat(p.as_ptr(), &mut stat) })); + Ok(FileAttr { stat: stat }) +} + +pub fn utimes(p: &Path, atime: u64, mtime: u64) -> io::Result<()> { + let p = cstr(p); + let buf = [super::ms_to_timeval(atime), super::ms_to_timeval(mtime)]; + try!(cvt(unsafe { c::utimes(p.as_ptr(), buf.as_ptr()) })); + Ok(()) +} diff --git a/src/libstd/sys/unix/mod.rs b/src/libstd/sys/unix/mod.rs index 427cf21ac70..b5a24278a20 100644 --- a/src/libstd/sys/unix/mod.rs +++ b/src/libstd/sys/unix/mod.rs @@ -23,6 +23,7 @@ use libc; use num::{Int, SignedInt}; use num; use old_io::{self, IoResult, IoError}; +use io; use str; use sys_common::mkerr_libc; @@ -39,9 +40,11 @@ macro_rules! helper_init { (static $name:ident: Helper<$m:ty>) => ( pub mod backtrace; pub mod c; -pub mod ext; pub mod condvar; -pub mod fs; +pub mod ext; +pub mod fd; +pub mod fs; // support for std::old_io +pub mod fs2; // support for std::fs pub mod helper_signal; pub mod mutex; pub mod os; @@ -176,6 +179,26 @@ pub fn retry<T, F> (mut f: F) -> T where } } +pub fn cvt<T: SignedInt>(t: T) -> io::Result<T> { + let one: T = Int::one(); + if t == -one { + Err(io::Error::last_os_error()) + } else { + Ok(t) + } +} + +pub fn cvt_r<T, F>(mut f: F) -> io::Result<T> + where T: SignedInt, F: FnMut() -> T +{ + loop { + match cvt(f()) { + Err(ref e) if e.kind() == ErrorKind::Interrupted => {} + other => return other, + } + } +} + pub fn ms_to_timeval(ms: u64) -> libc::timeval { libc::timeval { tv_sec: (ms / 1000) as libc::time_t, |
