2015-02-03 05:39:14 +00:00
|
|
|
// 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 io::prelude::*;
|
|
|
|
use os::unix::prelude::*;
|
|
|
|
|
2015-03-30 18:00:05 +00:00
|
|
|
use ffi::{CString, CStr, OsString, OsStr};
|
2015-04-19 09:27:19 +00:00
|
|
|
use fmt;
|
2015-07-10 08:54:00 +00:00
|
|
|
use io::{self, Error, ErrorKind, SeekFrom};
|
2015-03-10 03:04:35 +00:00
|
|
|
use libc::{self, c_int, size_t, off_t, c_char, mode_t};
|
2015-02-03 05:39:14 +00:00
|
|
|
use mem;
|
|
|
|
use path::{Path, PathBuf};
|
|
|
|
use ptr;
|
2015-02-20 17:46:56 +00:00
|
|
|
use sync::Arc;
|
2015-02-03 05:39:14 +00:00
|
|
|
use sys::fd::FileDesc;
|
2015-04-16 06:21:13 +00:00
|
|
|
use sys::platform::raw;
|
2015-02-03 05:39:14 +00:00
|
|
|
use sys::{c, cvt, cvt_r};
|
2015-04-16 06:21:13 +00:00
|
|
|
use sys_common::{AsInner, FromInner};
|
2015-02-03 05:39:14 +00:00
|
|
|
use vec::Vec;
|
|
|
|
|
|
|
|
pub struct File(FileDesc);
|
|
|
|
|
|
|
|
pub struct FileAttr {
|
2015-04-16 06:21:13 +00:00
|
|
|
stat: raw::stat,
|
2015-02-03 05:39:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct ReadDir {
|
2015-02-20 17:46:56 +00:00
|
|
|
dirp: Dir,
|
|
|
|
root: Arc<PathBuf>,
|
2015-02-03 05:39:14 +00:00
|
|
|
}
|
|
|
|
|
2015-02-20 17:46:56 +00:00
|
|
|
struct Dir(*mut libc::DIR);
|
|
|
|
|
|
|
|
unsafe impl Send for Dir {}
|
|
|
|
unsafe impl Sync for Dir {}
|
|
|
|
|
2015-02-03 05:39:14 +00:00
|
|
|
pub struct DirEntry {
|
2015-02-20 17:46:56 +00:00
|
|
|
buf: Vec<u8>, // actually *mut libc::dirent_t
|
|
|
|
root: Arc<PathBuf>,
|
2015-02-03 05:39:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[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 }
|
|
|
|
|
2015-04-16 06:21:13 +00:00
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
|
|
|
|
pub struct FileType { mode: mode_t }
|
|
|
|
|
2015-04-28 00:29:35 +00:00
|
|
|
pub struct DirBuilder { mode: mode_t }
|
|
|
|
|
2015-02-03 05:39:14 +00:00
|
|
|
impl FileAttr {
|
|
|
|
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 }
|
|
|
|
}
|
|
|
|
|
2015-04-16 06:21:13 +00:00
|
|
|
pub fn file_type(&self) -> FileType {
|
|
|
|
FileType { mode: self.stat.st_mode as mode_t }
|
|
|
|
}
|
2015-02-03 05:39:14 +00:00
|
|
|
}
|
|
|
|
|
2015-04-16 06:21:13 +00:00
|
|
|
impl AsInner<raw::stat> for FileAttr {
|
|
|
|
fn as_inner(&self) -> &raw::stat { &self.stat }
|
|
|
|
}
|
|
|
|
|
std: Stabilize a number of new fs features
This commit stabilizes the following APIs, slating them all to be cherry-picked
into the 1.1 release.
* fs::FileType (and transitively the derived trait implementations)
* fs::Metadata::file_type
* fs::FileType::is_dir
* fs::FileType::is_file
* fs::FileType::is_symlink
* fs::DirEntry::metadata
* fs::DirEntry::file_type
* fs::DirEntry::file_name
* fs::set_permissions
* fs::symlink_metadata
* os::raw::{self, *}
* os::{android, bitrig, linux, ...}::raw::{self, *}
* os::{android, bitrig, linux, ...}::fs::MetadataExt
* os::{android, bitrig, linux, ...}::fs::MetadataExt::as_raw_stat
* os::unix::fs::PermissionsExt
* os::unix::fs::PermissionsExt::mode
* os::unix::fs::PermissionsExt::set_mode
* os::unix::fs::PermissionsExt::from_mode
* os::unix::fs::OpenOptionsExt
* os::unix::fs::OpenOptionsExt::mode
* os::unix::fs::DirEntryExt
* os::unix::fs::DirEntryExt::ino
* os::windows::fs::MetadataExt
* os::windows::fs::MetadataExt::file_attributes
* os::windows::fs::MetadataExt::creation_time
* os::windows::fs::MetadataExt::last_access_time
* os::windows::fs::MetadataExt::last_write_time
* os::windows::fs::MetadataExt::file_size
The `os::unix::fs::Metadata` structure was also removed entirely, moving all of
its associated methods into the `os::unix::fs::MetadataExt` trait instead. The
methods are all marked as `#[stable]` still.
As some minor cleanup, some deprecated and unstable fs apis were also removed:
* File::path
* Metadata::accessed
* Metadata::modified
Features that were explicitly left unstable include:
* fs::WalkDir - the semantics of this were not considered in the recent fs
expansion RFC.
* fs::DirBuilder - it's still not 100% clear if the naming is right here and if
the set of functionality exposed is appropriate.
* fs::canonicalize - the implementation on Windows here is specifically in
question as it always returns a verbatim path. Additionally the Unix
implementation is susceptible to buffer overflows on long paths unfortunately.
* fs::PathExt - as this is just a convenience trait, it is not stabilized at
this time.
* fs::set_file_times - this funciton is still waiting on a time abstraction.
2015-05-27 23:29:55 +00:00
|
|
|
/// OS-specific extension methods for `fs::Metadata`
|
|
|
|
#[stable(feature = "metadata_ext", since = "1.1.0")]
|
2015-04-16 06:21:13 +00:00
|
|
|
pub trait MetadataExt {
|
std: Stabilize a number of new fs features
This commit stabilizes the following APIs, slating them all to be cherry-picked
into the 1.1 release.
* fs::FileType (and transitively the derived trait implementations)
* fs::Metadata::file_type
* fs::FileType::is_dir
* fs::FileType::is_file
* fs::FileType::is_symlink
* fs::DirEntry::metadata
* fs::DirEntry::file_type
* fs::DirEntry::file_name
* fs::set_permissions
* fs::symlink_metadata
* os::raw::{self, *}
* os::{android, bitrig, linux, ...}::raw::{self, *}
* os::{android, bitrig, linux, ...}::fs::MetadataExt
* os::{android, bitrig, linux, ...}::fs::MetadataExt::as_raw_stat
* os::unix::fs::PermissionsExt
* os::unix::fs::PermissionsExt::mode
* os::unix::fs::PermissionsExt::set_mode
* os::unix::fs::PermissionsExt::from_mode
* os::unix::fs::OpenOptionsExt
* os::unix::fs::OpenOptionsExt::mode
* os::unix::fs::DirEntryExt
* os::unix::fs::DirEntryExt::ino
* os::windows::fs::MetadataExt
* os::windows::fs::MetadataExt::file_attributes
* os::windows::fs::MetadataExt::creation_time
* os::windows::fs::MetadataExt::last_access_time
* os::windows::fs::MetadataExt::last_write_time
* os::windows::fs::MetadataExt::file_size
The `os::unix::fs::Metadata` structure was also removed entirely, moving all of
its associated methods into the `os::unix::fs::MetadataExt` trait instead. The
methods are all marked as `#[stable]` still.
As some minor cleanup, some deprecated and unstable fs apis were also removed:
* File::path
* Metadata::accessed
* Metadata::modified
Features that were explicitly left unstable include:
* fs::WalkDir - the semantics of this were not considered in the recent fs
expansion RFC.
* fs::DirBuilder - it's still not 100% clear if the naming is right here and if
the set of functionality exposed is appropriate.
* fs::canonicalize - the implementation on Windows here is specifically in
question as it always returns a verbatim path. Additionally the Unix
implementation is susceptible to buffer overflows on long paths unfortunately.
* fs::PathExt - as this is just a convenience trait, it is not stabilized at
this time.
* fs::set_file_times - this funciton is still waiting on a time abstraction.
2015-05-27 23:29:55 +00:00
|
|
|
/// Gain a reference to the underlying `stat` structure which contains the
|
|
|
|
/// raw information returned by the OS.
|
|
|
|
///
|
|
|
|
/// The contents of the returned `stat` are **not** consistent across Unix
|
|
|
|
/// platforms. The `os::unix::fs::MetadataExt` trait contains the cross-Unix
|
|
|
|
/// abstractions contained within the raw stat.
|
|
|
|
#[stable(feature = "metadata_ext", since = "1.1.0")]
|
2015-04-16 06:21:13 +00:00
|
|
|
fn as_raw_stat(&self) -> &raw::stat;
|
|
|
|
}
|
|
|
|
|
std: Stabilize a number of new fs features
This commit stabilizes the following APIs, slating them all to be cherry-picked
into the 1.1 release.
* fs::FileType (and transitively the derived trait implementations)
* fs::Metadata::file_type
* fs::FileType::is_dir
* fs::FileType::is_file
* fs::FileType::is_symlink
* fs::DirEntry::metadata
* fs::DirEntry::file_type
* fs::DirEntry::file_name
* fs::set_permissions
* fs::symlink_metadata
* os::raw::{self, *}
* os::{android, bitrig, linux, ...}::raw::{self, *}
* os::{android, bitrig, linux, ...}::fs::MetadataExt
* os::{android, bitrig, linux, ...}::fs::MetadataExt::as_raw_stat
* os::unix::fs::PermissionsExt
* os::unix::fs::PermissionsExt::mode
* os::unix::fs::PermissionsExt::set_mode
* os::unix::fs::PermissionsExt::from_mode
* os::unix::fs::OpenOptionsExt
* os::unix::fs::OpenOptionsExt::mode
* os::unix::fs::DirEntryExt
* os::unix::fs::DirEntryExt::ino
* os::windows::fs::MetadataExt
* os::windows::fs::MetadataExt::file_attributes
* os::windows::fs::MetadataExt::creation_time
* os::windows::fs::MetadataExt::last_access_time
* os::windows::fs::MetadataExt::last_write_time
* os::windows::fs::MetadataExt::file_size
The `os::unix::fs::Metadata` structure was also removed entirely, moving all of
its associated methods into the `os::unix::fs::MetadataExt` trait instead. The
methods are all marked as `#[stable]` still.
As some minor cleanup, some deprecated and unstable fs apis were also removed:
* File::path
* Metadata::accessed
* Metadata::modified
Features that were explicitly left unstable include:
* fs::WalkDir - the semantics of this were not considered in the recent fs
expansion RFC.
* fs::DirBuilder - it's still not 100% clear if the naming is right here and if
the set of functionality exposed is appropriate.
* fs::canonicalize - the implementation on Windows here is specifically in
question as it always returns a verbatim path. Additionally the Unix
implementation is susceptible to buffer overflows on long paths unfortunately.
* fs::PathExt - as this is just a convenience trait, it is not stabilized at
this time.
* fs::set_file_times - this funciton is still waiting on a time abstraction.
2015-05-27 23:29:55 +00:00
|
|
|
#[stable(feature = "metadata_ext", since = "1.1.0")]
|
2015-04-16 06:21:13 +00:00
|
|
|
impl MetadataExt for ::fs::Metadata {
|
|
|
|
fn as_raw_stat(&self) -> &raw::stat { &self.as_inner().stat }
|
|
|
|
}
|
|
|
|
|
2015-02-03 05:39:14 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2015-04-16 06:21:13 +00:00
|
|
|
pub fn mode(&self) -> raw::mode_t { self.mode }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FileType {
|
|
|
|
pub fn is_dir(&self) -> bool { self.is(libc::S_IFDIR) }
|
|
|
|
pub fn is_file(&self) -> bool { self.is(libc::S_IFREG) }
|
|
|
|
pub fn is_symlink(&self) -> bool { self.is(libc::S_IFLNK) }
|
|
|
|
|
2015-07-05 21:16:25 +00:00
|
|
|
pub fn is(&self, mode: mode_t) -> bool { self.mode & libc::S_IFMT == mode }
|
2015-02-03 05:39:14 +00:00
|
|
|
}
|
|
|
|
|
2015-04-16 06:21:13 +00:00
|
|
|
impl FromInner<raw::mode_t> for FilePermissions {
|
|
|
|
fn from_inner(mode: raw::mode_t) -> FilePermissions {
|
2015-02-03 05:39:14 +00:00
|
|
|
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 {
|
2015-02-20 17:46:56 +00:00
|
|
|
if unsafe { libc::readdir_r(self.dirp.0, ptr, &mut entry_ptr) != 0 } {
|
2015-02-03 05:39:14 +00:00
|
|
|
return Some(Err(Error::last_os_error()))
|
|
|
|
}
|
|
|
|
if entry_ptr.is_null() {
|
|
|
|
return None
|
|
|
|
}
|
|
|
|
|
|
|
|
let entry = DirEntry {
|
|
|
|
buf: buf,
|
|
|
|
root: self.root.clone()
|
|
|
|
};
|
|
|
|
if entry.name_bytes() == b"." || entry.name_bytes() == b".." {
|
|
|
|
buf = entry.buf;
|
|
|
|
} else {
|
|
|
|
return Some(Ok(entry))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-20 17:46:56 +00:00
|
|
|
impl Drop for Dir {
|
2015-02-03 05:39:14 +00:00
|
|
|
fn drop(&mut self) {
|
2015-02-20 17:46:56 +00:00
|
|
|
let r = unsafe { libc::closedir(self.0) };
|
2015-02-03 05:39:14 +00:00
|
|
|
debug_assert_eq!(r, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DirEntry {
|
|
|
|
pub fn path(&self) -> PathBuf {
|
2015-02-06 17:42:57 +00:00
|
|
|
self.root.join(<OsStr as OsStrExt>::from_bytes(self.name_bytes()))
|
2015-02-03 05:39:14 +00:00
|
|
|
}
|
|
|
|
|
2015-04-16 06:21:13 +00:00
|
|
|
pub fn file_name(&self) -> OsString {
|
|
|
|
OsStr::from_bytes(self.name_bytes()).to_os_string()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn metadata(&self) -> io::Result<FileAttr> {
|
|
|
|
lstat(&self.path())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn file_type(&self) -> io::Result<FileType> {
|
|
|
|
extern {
|
|
|
|
fn rust_dir_get_mode(ptr: *mut libc::dirent_t) -> c_int;
|
|
|
|
}
|
|
|
|
unsafe {
|
|
|
|
match rust_dir_get_mode(self.dirent()) {
|
|
|
|
-1 => lstat(&self.path()).map(|m| m.file_type()),
|
|
|
|
n => Ok(FileType { mode: n as mode_t }),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn ino(&self) -> raw::ino_t {
|
|
|
|
extern {
|
|
|
|
fn rust_dir_get_ino(ptr: *mut libc::dirent_t) -> raw::ino_t;
|
|
|
|
}
|
|
|
|
unsafe { rust_dir_get_ino(self.dirent()) }
|
|
|
|
}
|
|
|
|
|
2015-02-03 05:39:14 +00:00
|
|
|
fn name_bytes(&self) -> &[u8] {
|
|
|
|
extern {
|
|
|
|
fn rust_list_dir_val(ptr: *mut libc::dirent_t) -> *const c_char;
|
|
|
|
}
|
|
|
|
unsafe {
|
2015-02-20 17:46:56 +00:00
|
|
|
CStr::from_ptr(rust_list_dir_val(self.dirent())).to_bytes()
|
2015-02-03 05:39:14 +00:00
|
|
|
}
|
|
|
|
}
|
2015-02-20 17:46:56 +00:00
|
|
|
|
|
|
|
fn dirent(&self) -> *mut libc::dirent_t {
|
|
|
|
self.buf.as_ptr() as *mut _
|
|
|
|
}
|
2015-02-03 05:39:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl OpenOptions {
|
|
|
|
pub fn new() -> OpenOptions {
|
|
|
|
OpenOptions {
|
2015-08-23 18:10:22 +00:00
|
|
|
flags: libc::O_CLOEXEC,
|
2015-02-03 05:39:14 +00:00
|
|
|
read: false,
|
|
|
|
write: false,
|
2015-02-11 22:40:09 +00:00
|
|
|
mode: 0o666,
|
2015-02-03 05:39:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2015-04-16 06:21:13 +00:00
|
|
|
pub fn mode(&mut self, mode: raw::mode_t) {
|
2015-02-03 05:39:14 +00:00
|
|
|
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> {
|
2015-04-03 22:34:15 +00:00
|
|
|
let path = try!(cstr(path));
|
|
|
|
File::open_c(&path, opts)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn open_c(path: &CStr, opts: &OpenOptions) -> io::Result<File> {
|
2015-02-03 05:39:14 +00:00
|
|
|
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 fd = try!(cvt_r(|| unsafe {
|
|
|
|
libc::open(path.as_ptr(), flags, opts.mode)
|
|
|
|
}));
|
2015-04-03 22:30:10 +00:00
|
|
|
let fd = FileDesc::new(fd);
|
2015-08-23 18:10:22 +00:00
|
|
|
// Even though we open with the O_CLOEXEC flag, still set CLOEXEC here,
|
|
|
|
// in case the open flag is not supported (it's just ignored by the OS
|
|
|
|
// in that case).
|
2015-04-03 22:30:10 +00:00
|
|
|
fd.set_cloexec();
|
|
|
|
Ok(File(fd))
|
2015-02-03 05:39:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn file_attr(&self) -> io::Result<FileAttr> {
|
2015-04-16 06:21:13 +00:00
|
|
|
let mut stat: raw::stat = unsafe { mem::zeroed() };
|
|
|
|
try!(cvt(unsafe {
|
|
|
|
libc::fstat(self.0.raw(), &mut stat as *mut _ as *mut _)
|
|
|
|
}));
|
2015-02-03 05:39:14 +00:00
|
|
|
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 }
|
2015-07-16 06:31:24 +00:00
|
|
|
|
|
|
|
pub fn into_fd(self) -> FileDesc { self.0 }
|
2015-02-03 05:39:14 +00:00
|
|
|
}
|
|
|
|
|
2015-04-28 00:29:35 +00:00
|
|
|
impl DirBuilder {
|
|
|
|
pub fn new() -> DirBuilder {
|
|
|
|
DirBuilder { mode: 0o777 }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn mkdir(&self, p: &Path) -> io::Result<()> {
|
|
|
|
let p = try!(cstr(p));
|
|
|
|
try!(cvt(unsafe { libc::mkdir(p.as_ptr(), self.mode) }));
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_mode(&mut self, mode: mode_t) {
|
|
|
|
self.mode = mode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-18 06:47:40 +00:00
|
|
|
fn cstr(path: &Path) -> io::Result<CString> {
|
2015-03-30 22:15:27 +00:00
|
|
|
path.as_os_str().to_cstring().ok_or(
|
2015-03-31 23:20:09 +00:00
|
|
|
io::Error::new(io::ErrorKind::InvalidInput, "path contained a null"))
|
2015-02-03 05:39:14 +00:00
|
|
|
}
|
|
|
|
|
std: Stabilize parts of std::os::platform::io
This commit stabilizes the platform-specific `io` modules, specifically around
the traits having to do with the raw representation of each object on each
platform.
Specifically, the following material was stabilized:
* `AsRaw{Fd,Socket,Handle}`
* `RawFd` (renamed from `Fd`)
* `RawHandle` (renamed from `Handle`)
* `RawSocket` (renamed from `Socket`)
* `AsRaw{Fd,Socket,Handle}` implementations
* `std::os::{unix, windows}::io`
The following material was added as `#[unstable]`:
* `FromRaw{Fd,Socket,Handle}`
* Implementations for various primitives
There are a number of future improvements that are possible to make to this
module, but this should cover a good bit of functionality desired from these
modules for now. Some specific future additions may include:
* `IntoRawXXX` traits to consume the raw representation and cancel the
auto-destructor.
* `Fd`, `Socket`, and `Handle` abstractions that behave like Rust objects and
have nice methods for various syscalls.
At this time though, these are considered backwards-compatible extensions and
will not be stabilized at this time.
This commit is a breaking change due to the addition of `Raw` in from of the
type aliases in each of the platform-specific modules.
[breaking-change]
2015-03-26 23:18:29 +00:00
|
|
|
impl FromInner<c_int> for File {
|
|
|
|
fn from_inner(fd: c_int) -> File {
|
|
|
|
File(FileDesc::new(fd))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-19 09:27:19 +00:00
|
|
|
impl fmt::Debug for File {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
#[cfg(target_os = "linux")]
|
|
|
|
fn get_path(fd: c_int) -> Option<PathBuf> {
|
|
|
|
use string::ToString;
|
|
|
|
let mut p = PathBuf::from("/proc/self/fd");
|
|
|
|
p.push(&fd.to_string());
|
|
|
|
readlink(&p).ok()
|
|
|
|
}
|
|
|
|
|
2015-07-10 14:23:14 +00:00
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
fn get_path(fd: c_int) -> Option<PathBuf> {
|
|
|
|
let mut buf = vec![0;libc::PATH_MAX as usize];
|
|
|
|
let n = unsafe { libc::fcntl(fd, libc::F_GETPATH, buf.as_ptr()) };
|
|
|
|
if n == -1 {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
let l = buf.iter().position(|&c| c == 0).unwrap();
|
|
|
|
buf.truncate(l as usize);
|
|
|
|
Some(PathBuf::from(OsString::from_vec(buf)))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(any(target_os = "linux", target_os = "macos")))]
|
2015-04-19 09:27:19 +00:00
|
|
|
fn get_path(_fd: c_int) -> Option<PathBuf> {
|
|
|
|
// FIXME(#24570): implement this for other Unix platforms
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2015-07-10 14:23:14 +00:00
|
|
|
#[cfg(any(target_os = "linux", target_os = "macos"))]
|
2015-04-19 09:27:19 +00:00
|
|
|
fn get_mode(fd: c_int) -> Option<(bool, bool)> {
|
|
|
|
let mode = unsafe { libc::fcntl(fd, libc::F_GETFL) };
|
|
|
|
if mode == -1 {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
match mode & libc::O_ACCMODE {
|
|
|
|
libc::O_RDONLY => Some((true, false)),
|
|
|
|
libc::O_RDWR => Some((true, true)),
|
|
|
|
libc::O_WRONLY => Some((false, true)),
|
|
|
|
_ => None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-10 14:23:14 +00:00
|
|
|
#[cfg(not(any(target_os = "linux", target_os = "macos")))]
|
2015-04-19 09:27:19 +00:00
|
|
|
fn get_mode(_fd: c_int) -> Option<(bool, bool)> {
|
|
|
|
// FIXME(#24570): implement this for other Unix platforms
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
let fd = self.0.raw();
|
2015-05-17 20:17:26 +00:00
|
|
|
let mut b = f.debug_struct("File");
|
|
|
|
b.field("fd", &fd);
|
2015-04-19 09:27:19 +00:00
|
|
|
if let Some(path) = get_path(fd) {
|
2015-05-17 20:17:26 +00:00
|
|
|
b.field("path", &path);
|
2015-04-19 09:27:19 +00:00
|
|
|
}
|
|
|
|
if let Some((read, write)) = get_mode(fd) {
|
2015-05-17 20:17:26 +00:00
|
|
|
b.field("read", &read).field("write", &write);
|
2015-04-19 09:27:19 +00:00
|
|
|
}
|
|
|
|
b.finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-03 05:39:14 +00:00
|
|
|
pub fn readdir(p: &Path) -> io::Result<ReadDir> {
|
2015-02-20 17:46:56 +00:00
|
|
|
let root = Arc::new(p.to_path_buf());
|
2015-02-18 06:47:40 +00:00
|
|
|
let p = try!(cstr(p));
|
2015-02-03 05:39:14 +00:00
|
|
|
unsafe {
|
|
|
|
let ptr = libc::opendir(p.as_ptr());
|
|
|
|
if ptr.is_null() {
|
|
|
|
Err(Error::last_os_error())
|
|
|
|
} else {
|
2015-02-20 17:46:56 +00:00
|
|
|
Ok(ReadDir { dirp: Dir(ptr), root: root })
|
2015-02-03 05:39:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn unlink(p: &Path) -> io::Result<()> {
|
2015-02-18 06:47:40 +00:00
|
|
|
let p = try!(cstr(p));
|
2015-02-03 05:39:14 +00:00
|
|
|
try!(cvt(unsafe { libc::unlink(p.as_ptr()) }));
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn rename(old: &Path, new: &Path) -> io::Result<()> {
|
2015-02-18 06:47:40 +00:00
|
|
|
let old = try!(cstr(old));
|
|
|
|
let new = try!(cstr(new));
|
2015-02-03 05:39:14 +00:00
|
|
|
try!(cvt(unsafe { libc::rename(old.as_ptr(), new.as_ptr()) }));
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_perm(p: &Path, perm: FilePermissions) -> io::Result<()> {
|
2015-02-18 06:47:40 +00:00
|
|
|
let p = try!(cstr(p));
|
2015-02-03 05:39:14 +00:00
|
|
|
try!(cvt_r(|| unsafe { libc::chmod(p.as_ptr(), perm.mode) }));
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn rmdir(p: &Path) -> io::Result<()> {
|
2015-02-18 06:47:40 +00:00
|
|
|
let p = try!(cstr(p));
|
2015-02-03 05:39:14 +00:00
|
|
|
try!(cvt(unsafe { libc::rmdir(p.as_ptr()) }));
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn readlink(p: &Path) -> io::Result<PathBuf> {
|
2015-02-18 06:47:40 +00:00
|
|
|
let c_path = try!(cstr(p));
|
2015-02-03 05:39:14 +00:00
|
|
|
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);
|
|
|
|
}
|
2015-03-18 16:14:54 +00:00
|
|
|
Ok(PathBuf::from(OsString::from_vec(buf)))
|
2015-02-03 05:39:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn symlink(src: &Path, dst: &Path) -> io::Result<()> {
|
2015-02-18 06:47:40 +00:00
|
|
|
let src = try!(cstr(src));
|
|
|
|
let dst = try!(cstr(dst));
|
2015-02-03 05:39:14 +00:00
|
|
|
try!(cvt(unsafe { libc::symlink(src.as_ptr(), dst.as_ptr()) }));
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn link(src: &Path, dst: &Path) -> io::Result<()> {
|
2015-02-18 06:47:40 +00:00
|
|
|
let src = try!(cstr(src));
|
|
|
|
let dst = try!(cstr(dst));
|
2015-02-03 05:39:14 +00:00
|
|
|
try!(cvt(unsafe { libc::link(src.as_ptr(), dst.as_ptr()) }));
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn stat(p: &Path) -> io::Result<FileAttr> {
|
2015-02-18 06:47:40 +00:00
|
|
|
let p = try!(cstr(p));
|
2015-04-16 06:21:13 +00:00
|
|
|
let mut stat: raw::stat = unsafe { mem::zeroed() };
|
|
|
|
try!(cvt(unsafe {
|
|
|
|
libc::stat(p.as_ptr(), &mut stat as *mut _ as *mut _)
|
|
|
|
}));
|
2015-02-03 05:39:14 +00:00
|
|
|
Ok(FileAttr { stat: stat })
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn lstat(p: &Path) -> io::Result<FileAttr> {
|
2015-02-18 06:47:40 +00:00
|
|
|
let p = try!(cstr(p));
|
2015-04-16 06:21:13 +00:00
|
|
|
let mut stat: raw::stat = unsafe { mem::zeroed() };
|
|
|
|
try!(cvt(unsafe {
|
|
|
|
libc::lstat(p.as_ptr(), &mut stat as *mut _ as *mut _)
|
|
|
|
}));
|
2015-02-03 05:39:14 +00:00
|
|
|
Ok(FileAttr { stat: stat })
|
|
|
|
}
|
|
|
|
|
2015-04-16 06:21:13 +00:00
|
|
|
pub fn canonicalize(p: &Path) -> io::Result<PathBuf> {
|
|
|
|
let path = try!(CString::new(p.as_os_str().as_bytes()));
|
|
|
|
let mut buf = vec![0u8; 16 * 1024];
|
|
|
|
unsafe {
|
|
|
|
let r = c::realpath(path.as_ptr(), buf.as_mut_ptr() as *mut _);
|
|
|
|
if r.is_null() {
|
|
|
|
return Err(io::Error::last_os_error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let p = buf.iter().position(|i| *i == 0).unwrap();
|
|
|
|
buf.truncate(p);
|
|
|
|
Ok(PathBuf::from(OsString::from_vec(buf)))
|
|
|
|
}
|
2015-07-10 08:54:00 +00:00
|
|
|
|
|
|
|
pub fn copy(from: &Path, to: &Path) -> io::Result<u64> {
|
|
|
|
use fs::{File, PathExt, set_permissions};
|
|
|
|
if !from.is_file() {
|
|
|
|
return Err(Error::new(ErrorKind::InvalidInput,
|
2015-08-05 22:25:09 +00:00
|
|
|
"the source path is not an existing regular file"))
|
2015-07-10 08:54:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let mut reader = try!(File::open(from));
|
|
|
|
let mut writer = try!(File::create(to));
|
|
|
|
let perm = try!(reader.metadata()).permissions();
|
|
|
|
|
|
|
|
let ret = try!(io::copy(&mut reader, &mut writer));
|
|
|
|
try!(set_permissions(to, perm));
|
|
|
|
Ok(ret)
|
|
|
|
}
|