2014-02-26 12:57:00 -08: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.
|
|
|
|
|
|
|
|
//! Blocking win32-based file I/O
|
|
|
|
|
2014-05-19 18:00:52 -07:00
|
|
|
use alloc::arc::Arc;
|
|
|
|
use libc::{c_int, c_void};
|
|
|
|
use libc;
|
2014-02-26 12:57:00 -08:00
|
|
|
use std::c_str::CString;
|
|
|
|
use std::mem;
|
2014-06-02 14:51:58 -07:00
|
|
|
use std::os::win32::fill_utf16_buf_and_decode;
|
2014-02-26 12:57:00 -08:00
|
|
|
use std::ptr;
|
|
|
|
use std::rt::rtio;
|
2014-06-09 19:55:28 -07:00
|
|
|
use std::rt::rtio::{IoResult, IoError};
|
2014-02-26 12:57:00 -08:00
|
|
|
use std::str;
|
2014-05-03 18:20:35 -07:00
|
|
|
use std::vec;
|
2014-02-26 12:57:00 -08:00
|
|
|
|
|
|
|
pub type fd_t = libc::c_int;
|
|
|
|
|
|
|
|
struct Inner {
|
|
|
|
fd: fd_t,
|
|
|
|
close_on_drop: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct FileDesc {
|
2014-05-19 18:00:52 -07:00
|
|
|
inner: Arc<Inner>
|
2014-02-26 12:57:00 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl FileDesc {
|
|
|
|
/// Create a `FileDesc` from an open C file descriptor.
|
|
|
|
///
|
|
|
|
/// The `FileDesc` will take ownership of the specified file descriptor and
|
|
|
|
/// close it upon destruction if the `close_on_drop` flag is true, otherwise
|
|
|
|
/// it will not close the file descriptor when this `FileDesc` is dropped.
|
|
|
|
///
|
|
|
|
/// Note that all I/O operations done on this object will be *blocking*, but
|
|
|
|
/// they do not require the runtime to be active.
|
|
|
|
pub fn new(fd: fd_t, close_on_drop: bool) -> FileDesc {
|
2014-05-19 18:00:52 -07:00
|
|
|
FileDesc { inner: Arc::new(Inner {
|
2014-02-26 12:57:00 -08:00
|
|
|
fd: fd,
|
|
|
|
close_on_drop: close_on_drop
|
|
|
|
}) }
|
|
|
|
}
|
|
|
|
|
2014-06-04 00:01:40 -07:00
|
|
|
pub fn inner_read(&mut self, buf: &mut [u8]) -> IoResult<uint> {
|
2014-02-26 12:57:00 -08:00
|
|
|
let mut read = 0;
|
|
|
|
let ret = unsafe {
|
|
|
|
libc::ReadFile(self.handle(), buf.as_ptr() as libc::LPVOID,
|
|
|
|
buf.len() as libc::DWORD, &mut read,
|
|
|
|
ptr::mut_null())
|
|
|
|
};
|
|
|
|
if ret != 0 {
|
|
|
|
Ok(read as uint)
|
|
|
|
} else {
|
|
|
|
Err(super::last_error())
|
|
|
|
}
|
|
|
|
}
|
2014-06-04 00:01:40 -07:00
|
|
|
pub fn inner_write(&mut self, buf: &[u8]) -> IoResult<()> {
|
2014-02-26 12:57:00 -08:00
|
|
|
let mut cur = buf.as_ptr();
|
|
|
|
let mut remaining = buf.len();
|
|
|
|
while remaining > 0 {
|
|
|
|
let mut amt = 0;
|
|
|
|
let ret = unsafe {
|
|
|
|
libc::WriteFile(self.handle(), cur as libc::LPVOID,
|
|
|
|
remaining as libc::DWORD, &mut amt,
|
|
|
|
ptr::mut_null())
|
|
|
|
};
|
|
|
|
if ret != 0 {
|
|
|
|
remaining -= amt as uint;
|
|
|
|
cur = unsafe { cur.offset(amt as int) };
|
|
|
|
} else {
|
|
|
|
return Err(super::last_error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2014-05-19 18:00:52 -07:00
|
|
|
pub fn fd(&self) -> fd_t { self.inner.fd }
|
2014-02-26 12:57:00 -08:00
|
|
|
|
|
|
|
pub fn handle(&self) -> libc::HANDLE {
|
|
|
|
unsafe { libc::get_osfhandle(self.fd()) as libc::HANDLE }
|
|
|
|
}
|
2014-05-28 13:47:30 +03:00
|
|
|
|
|
|
|
// A version of seek that takes &self so that tell can call it
|
|
|
|
// - the private seek should of course take &mut self.
|
2014-06-04 00:01:40 -07:00
|
|
|
fn seek_common(&self, pos: i64, style: rtio::SeekStyle) -> IoResult<u64> {
|
2014-05-28 13:47:30 +03:00
|
|
|
let whence = match style {
|
2014-06-04 00:01:40 -07:00
|
|
|
rtio::SeekSet => libc::FILE_BEGIN,
|
|
|
|
rtio::SeekEnd => libc::FILE_END,
|
|
|
|
rtio::SeekCur => libc::FILE_CURRENT,
|
2014-05-28 13:47:30 +03:00
|
|
|
};
|
|
|
|
unsafe {
|
|
|
|
let mut newpos = 0;
|
|
|
|
match libc::SetFilePointerEx(self.handle(), pos, &mut newpos,
|
|
|
|
whence) {
|
|
|
|
0 => Err(super::last_error()),
|
|
|
|
_ => Ok(newpos as u64),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-26 12:57:00 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl rtio::RtioFileStream for FileDesc {
|
2014-06-04 00:01:40 -07:00
|
|
|
fn read(&mut self, buf: &mut [u8]) -> IoResult<int> {
|
2014-02-26 12:57:00 -08:00
|
|
|
self.inner_read(buf).map(|i| i as int)
|
|
|
|
}
|
2014-06-04 00:01:40 -07:00
|
|
|
fn write(&mut self, buf: &[u8]) -> IoResult<()> {
|
2014-02-26 12:57:00 -08:00
|
|
|
self.inner_write(buf)
|
|
|
|
}
|
|
|
|
|
2014-06-04 00:01:40 -07:00
|
|
|
fn pread(&mut self, buf: &mut [u8], offset: u64) -> IoResult<int> {
|
2014-02-26 12:57:00 -08:00
|
|
|
let mut read = 0;
|
2014-05-17 00:56:00 -07:00
|
|
|
let mut overlap: libc::OVERLAPPED = unsafe { mem::zeroed() };
|
2014-02-26 12:57:00 -08:00
|
|
|
overlap.Offset = offset as libc::DWORD;
|
|
|
|
overlap.OffsetHigh = (offset >> 32) as libc::DWORD;
|
|
|
|
let ret = unsafe {
|
|
|
|
libc::ReadFile(self.handle(), buf.as_ptr() as libc::LPVOID,
|
|
|
|
buf.len() as libc::DWORD, &mut read,
|
|
|
|
&mut overlap)
|
|
|
|
};
|
|
|
|
if ret != 0 {
|
|
|
|
Ok(read as int)
|
|
|
|
} else {
|
|
|
|
Err(super::last_error())
|
|
|
|
}
|
|
|
|
}
|
2014-06-04 00:01:40 -07:00
|
|
|
fn pwrite(&mut self, buf: &[u8], mut offset: u64) -> IoResult<()> {
|
2014-02-26 12:57:00 -08:00
|
|
|
let mut cur = buf.as_ptr();
|
|
|
|
let mut remaining = buf.len();
|
2014-05-17 00:56:00 -07:00
|
|
|
let mut overlap: libc::OVERLAPPED = unsafe { mem::zeroed() };
|
2014-02-26 12:57:00 -08:00
|
|
|
while remaining > 0 {
|
|
|
|
overlap.Offset = offset as libc::DWORD;
|
|
|
|
overlap.OffsetHigh = (offset >> 32) as libc::DWORD;
|
|
|
|
let mut amt = 0;
|
|
|
|
let ret = unsafe {
|
|
|
|
libc::WriteFile(self.handle(), cur as libc::LPVOID,
|
|
|
|
remaining as libc::DWORD, &mut amt,
|
|
|
|
&mut overlap)
|
|
|
|
};
|
|
|
|
if ret != 0 {
|
|
|
|
remaining -= amt as uint;
|
|
|
|
cur = unsafe { cur.offset(amt as int) };
|
|
|
|
offset += amt as u64;
|
|
|
|
} else {
|
|
|
|
return Err(super::last_error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
2014-05-28 13:47:30 +03:00
|
|
|
|
2014-06-04 00:01:40 -07:00
|
|
|
fn seek(&mut self, pos: i64, style: rtio::SeekStyle) -> IoResult<u64> {
|
2014-05-28 13:47:30 +03:00
|
|
|
self.seek_common(pos, style)
|
2014-02-26 12:57:00 -08:00
|
|
|
}
|
2014-05-28 13:47:30 +03:00
|
|
|
|
2014-06-04 00:01:40 -07:00
|
|
|
fn tell(&self) -> IoResult<u64> {
|
|
|
|
self.seek_common(0, rtio::SeekCur)
|
2014-02-26 12:57:00 -08:00
|
|
|
}
|
|
|
|
|
2014-06-04 00:01:40 -07:00
|
|
|
fn fsync(&mut self) -> IoResult<()> {
|
2014-02-26 12:57:00 -08:00
|
|
|
super::mkerr_winbool(unsafe {
|
|
|
|
libc::FlushFileBuffers(self.handle())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-06-04 00:01:40 -07:00
|
|
|
fn datasync(&mut self) -> IoResult<()> { return self.fsync(); }
|
2014-02-26 12:57:00 -08:00
|
|
|
|
2014-06-04 00:01:40 -07:00
|
|
|
fn truncate(&mut self, offset: i64) -> IoResult<()> {
|
2014-02-26 12:57:00 -08:00
|
|
|
let orig_pos = try!(self.tell());
|
2014-06-04 00:01:40 -07:00
|
|
|
let _ = try!(self.seek(offset, rtio::SeekSet));
|
2014-02-26 12:57:00 -08:00
|
|
|
let ret = unsafe {
|
|
|
|
match libc::SetEndOfFile(self.handle()) {
|
|
|
|
0 => Err(super::last_error()),
|
|
|
|
_ => Ok(())
|
|
|
|
}
|
|
|
|
};
|
2014-06-04 00:01:40 -07:00
|
|
|
let _ = self.seek(orig_pos as i64, rtio::SeekSet);
|
2014-02-26 12:57:00 -08:00
|
|
|
return ret;
|
|
|
|
}
|
2014-05-12 02:31:22 -03:00
|
|
|
|
2014-06-04 00:01:40 -07:00
|
|
|
fn fstat(&mut self) -> IoResult<rtio::FileStat> {
|
2014-05-23 20:53:56 -07:00
|
|
|
let mut stat: libc::stat = unsafe { mem::zeroed() };
|
2014-05-12 02:31:22 -03:00
|
|
|
match unsafe { libc::fstat(self.fd(), &mut stat) } {
|
|
|
|
0 => Ok(mkstat(&stat)),
|
|
|
|
_ => Err(super::last_error()),
|
|
|
|
}
|
|
|
|
}
|
2014-02-26 12:57:00 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl rtio::RtioPipe for FileDesc {
|
2014-06-04 00:01:40 -07:00
|
|
|
fn read(&mut self, buf: &mut [u8]) -> IoResult<uint> {
|
2014-02-26 12:57:00 -08:00
|
|
|
self.inner_read(buf)
|
|
|
|
}
|
2014-06-04 00:01:40 -07:00
|
|
|
fn write(&mut self, buf: &[u8]) -> IoResult<()> {
|
2014-02-26 12:57:00 -08:00
|
|
|
self.inner_write(buf)
|
|
|
|
}
|
2014-06-14 11:03:34 -07:00
|
|
|
fn clone(&self) -> Box<rtio::RtioPipe + Send> {
|
|
|
|
box FileDesc { inner: self.inner.clone() } as Box<rtio::RtioPipe + Send>
|
2014-02-26 12:57:00 -08:00
|
|
|
}
|
2014-04-24 18:48:21 -07:00
|
|
|
|
|
|
|
// Only supported on named pipes currently. Note that this doesn't have an
|
|
|
|
// impact on the std::io primitives, this is never called via
|
|
|
|
// std::io::PipeStream. If the functionality is exposed in the future, then
|
|
|
|
// these methods will need to be implemented.
|
|
|
|
fn close_read(&mut self) -> IoResult<()> {
|
2014-06-04 00:01:40 -07:00
|
|
|
Err(super::unimpl())
|
2014-04-24 18:48:21 -07:00
|
|
|
}
|
|
|
|
fn close_write(&mut self) -> IoResult<()> {
|
2014-06-04 00:01:40 -07:00
|
|
|
Err(super::unimpl())
|
2014-04-24 18:48:21 -07:00
|
|
|
}
|
2014-04-25 20:50:22 -07:00
|
|
|
fn set_timeout(&mut self, _t: Option<u64>) {}
|
|
|
|
fn set_read_timeout(&mut self, _t: Option<u64>) {}
|
|
|
|
fn set_write_timeout(&mut self, _t: Option<u64>) {}
|
2014-02-26 12:57:00 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl rtio::RtioTTY for FileDesc {
|
2014-06-04 00:01:40 -07:00
|
|
|
fn read(&mut self, buf: &mut [u8]) -> IoResult<uint> {
|
2014-02-26 12:57:00 -08:00
|
|
|
self.inner_read(buf)
|
|
|
|
}
|
2014-06-04 00:01:40 -07:00
|
|
|
fn write(&mut self, buf: &[u8]) -> IoResult<()> {
|
2014-02-26 12:57:00 -08:00
|
|
|
self.inner_write(buf)
|
|
|
|
}
|
2014-06-04 00:01:40 -07:00
|
|
|
fn set_raw(&mut self, _raw: bool) -> IoResult<()> {
|
2014-02-26 12:57:00 -08:00
|
|
|
Err(super::unimpl())
|
|
|
|
}
|
2014-06-04 00:01:40 -07:00
|
|
|
fn get_winsize(&mut self) -> IoResult<(int, int)> {
|
2014-02-26 12:57:00 -08:00
|
|
|
Err(super::unimpl())
|
|
|
|
}
|
|
|
|
fn isatty(&self) -> bool { false }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for Inner {
|
|
|
|
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.close_on_drop && self.fd > libc::STDERR_FILENO {
|
|
|
|
let n = unsafe { libc::close(self.fd) };
|
|
|
|
if n != 0 {
|
2014-03-12 23:34:31 -07:00
|
|
|
println!("error {} when closing file descriptor {}", n, self.fd);
|
2014-02-26 12:57:00 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-02 14:51:58 -07:00
|
|
|
pub fn to_utf16(s: &CString) -> IoResult<Vec<u16>> {
|
|
|
|
match s.as_str() {
|
2014-05-31 13:02:29 +02:00
|
|
|
Some(s) => Ok(s.utf16_units().collect::<Vec<u16>>().append_one(0)),
|
2014-06-02 14:51:58 -07:00
|
|
|
None => Err(IoError {
|
|
|
|
code: libc::ERROR_INVALID_NAME as uint,
|
|
|
|
extra: 0,
|
2014-06-21 03:39:03 -07:00
|
|
|
detail: Some("valid unicode input required".to_string()),
|
2014-06-02 14:51:58 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-04 00:01:40 -07:00
|
|
|
pub fn open(path: &CString, fm: rtio::FileMode, fa: rtio::FileAccess)
|
2014-02-26 12:57:00 -08:00
|
|
|
-> IoResult<FileDesc> {
|
|
|
|
// Flags passed to open_osfhandle
|
|
|
|
let flags = match fm {
|
2014-06-04 00:01:40 -07:00
|
|
|
rtio::Open => 0,
|
|
|
|
rtio::Append => libc::O_APPEND,
|
|
|
|
rtio::Truncate => libc::O_TRUNC,
|
2014-02-26 12:57:00 -08:00
|
|
|
};
|
|
|
|
let flags = match fa {
|
2014-06-04 00:01:40 -07:00
|
|
|
rtio::Read => flags | libc::O_RDONLY,
|
|
|
|
rtio::Write => flags | libc::O_WRONLY | libc::O_CREAT,
|
|
|
|
rtio::ReadWrite => flags | libc::O_RDWR | libc::O_CREAT,
|
2014-02-26 12:57:00 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
let mut dwDesiredAccess = match fa {
|
2014-06-04 00:01:40 -07:00
|
|
|
rtio::Read => libc::FILE_GENERIC_READ,
|
|
|
|
rtio::Write => libc::FILE_GENERIC_WRITE,
|
|
|
|
rtio::ReadWrite => libc::FILE_GENERIC_READ | libc::FILE_GENERIC_WRITE
|
2014-02-26 12:57:00 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
// libuv has a good comment about this, but the basic idea is what we try to
|
|
|
|
// emulate unix semantics by enabling all sharing by allowing things such as
|
|
|
|
// deleting a file while it's still open.
|
|
|
|
let dwShareMode = libc::FILE_SHARE_READ | libc::FILE_SHARE_WRITE |
|
|
|
|
libc::FILE_SHARE_DELETE;
|
|
|
|
|
|
|
|
let dwCreationDisposition = match (fm, fa) {
|
2014-06-04 00:01:40 -07:00
|
|
|
(rtio::Truncate, rtio::Read) => libc::TRUNCATE_EXISTING,
|
|
|
|
(rtio::Truncate, _) => libc::CREATE_ALWAYS,
|
|
|
|
(rtio::Open, rtio::Read) => libc::OPEN_EXISTING,
|
|
|
|
(rtio::Open, _) => libc::OPEN_ALWAYS,
|
|
|
|
(rtio::Append, rtio::Read) => {
|
2014-02-26 12:57:00 -08:00
|
|
|
dwDesiredAccess |= libc::FILE_APPEND_DATA;
|
|
|
|
libc::OPEN_EXISTING
|
|
|
|
}
|
2014-06-04 00:01:40 -07:00
|
|
|
(rtio::Append, _) => {
|
2014-02-26 12:57:00 -08:00
|
|
|
dwDesiredAccess &= !libc::FILE_WRITE_DATA;
|
|
|
|
dwDesiredAccess |= libc::FILE_APPEND_DATA;
|
|
|
|
libc::OPEN_ALWAYS
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut dwFlagsAndAttributes = libc::FILE_ATTRIBUTE_NORMAL;
|
|
|
|
// Compat with unix, this allows opening directories (see libuv)
|
|
|
|
dwFlagsAndAttributes |= libc::FILE_FLAG_BACKUP_SEMANTICS;
|
|
|
|
|
2014-06-02 14:51:58 -07:00
|
|
|
let path = try!(to_utf16(path));
|
|
|
|
let handle = unsafe {
|
|
|
|
libc::CreateFileW(path.as_ptr(),
|
2014-02-26 12:57:00 -08:00
|
|
|
dwDesiredAccess,
|
|
|
|
dwShareMode,
|
|
|
|
ptr::mut_null(),
|
|
|
|
dwCreationDisposition,
|
|
|
|
dwFlagsAndAttributes,
|
|
|
|
ptr::mut_null())
|
2014-06-02 14:51:58 -07:00
|
|
|
};
|
2014-08-07 16:40:12 -04:00
|
|
|
if handle == libc::INVALID_HANDLE_VALUE {
|
2014-02-26 12:57:00 -08:00
|
|
|
Err(super::last_error())
|
|
|
|
} else {
|
|
|
|
let fd = unsafe {
|
|
|
|
libc::open_osfhandle(handle as libc::intptr_t, flags)
|
|
|
|
};
|
|
|
|
if fd < 0 {
|
|
|
|
let _ = unsafe { libc::CloseHandle(handle) };
|
|
|
|
Err(super::last_error())
|
|
|
|
} else {
|
|
|
|
Ok(FileDesc::new(fd, true))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-04 00:01:40 -07:00
|
|
|
pub fn mkdir(p: &CString, _mode: uint) -> IoResult<()> {
|
2014-06-02 14:51:58 -07:00
|
|
|
let p = try!(to_utf16(p));
|
2014-02-26 12:57:00 -08:00
|
|
|
super::mkerr_winbool(unsafe {
|
|
|
|
// FIXME: turn mode into something useful? #2623
|
2014-06-02 14:51:58 -07:00
|
|
|
libc::CreateDirectoryW(p.as_ptr(), ptr::mut_null())
|
2014-02-26 12:57:00 -08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-06-02 22:11:19 -07:00
|
|
|
pub fn readdir(p: &CString) -> IoResult<Vec<CString>> {
|
2014-05-09 22:59:46 -04:00
|
|
|
use std::rt::libc_heap::malloc_raw;
|
2014-02-26 12:57:00 -08:00
|
|
|
|
2014-06-02 22:11:19 -07:00
|
|
|
fn prune(root: &CString, dirs: Vec<Path>) -> Vec<CString> {
|
2014-06-14 22:50:07 +10:00
|
|
|
let root = unsafe { CString::new(root.as_ptr(), false) };
|
2014-02-26 12:57:00 -08:00
|
|
|
let root = Path::new(root);
|
|
|
|
|
|
|
|
dirs.move_iter().filter(|path| {
|
2014-06-18 20:25:36 +02:00
|
|
|
path.as_vec() != b"." && path.as_vec() != b".."
|
2014-06-02 22:11:19 -07:00
|
|
|
}).map(|path| root.join(path).to_c_str()).collect()
|
2014-02-26 12:57:00 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
extern {
|
|
|
|
fn rust_list_dir_wfd_size() -> libc::size_t;
|
2014-06-25 12:47:34 -07:00
|
|
|
fn rust_list_dir_wfd_fp_buf(wfd: *mut libc::c_void) -> *const u16;
|
2014-02-26 12:57:00 -08:00
|
|
|
}
|
|
|
|
let star = Path::new(unsafe {
|
2014-06-14 22:50:07 +10:00
|
|
|
CString::new(p.as_ptr(), false)
|
2014-02-26 12:57:00 -08:00
|
|
|
}).join("*");
|
2014-06-02 14:51:58 -07:00
|
|
|
let path = try!(to_utf16(&star.to_c_str()));
|
|
|
|
|
|
|
|
unsafe {
|
2014-02-26 12:57:00 -08:00
|
|
|
let wfd_ptr = malloc_raw(rust_list_dir_wfd_size() as uint);
|
2014-06-25 12:47:34 -07:00
|
|
|
let find_handle = libc::FindFirstFileW(path.as_ptr(),
|
|
|
|
wfd_ptr as libc::HANDLE);
|
2014-08-07 16:40:12 -04:00
|
|
|
if find_handle != libc::INVALID_HANDLE_VALUE {
|
2014-04-09 11:45:20 +10:00
|
|
|
let mut paths = vec!();
|
2014-02-26 12:57:00 -08:00
|
|
|
let mut more_files = 1 as libc::c_int;
|
|
|
|
while more_files != 0 {
|
2014-06-25 12:47:34 -07:00
|
|
|
let fp_buf = rust_list_dir_wfd_fp_buf(wfd_ptr as *mut c_void);
|
2014-02-26 12:57:00 -08:00
|
|
|
if fp_buf as uint == 0 {
|
|
|
|
fail!("os::list_dir() failure: got null ptr from wfd");
|
|
|
|
} else {
|
2014-05-03 18:20:35 -07:00
|
|
|
let fp_vec = vec::raw::from_buf(fp_buf, libc::wcslen(fp_buf) as uint);
|
|
|
|
let fp_trimmed = str::truncate_utf16_at_nul(fp_vec.as_slice());
|
2014-07-10 17:43:03 +02:00
|
|
|
let fp_str = String::from_utf16(fp_trimmed)
|
2014-02-26 12:57:00 -08:00
|
|
|
.expect("rust_list_dir_wfd_fp_buf returned invalid UTF-16");
|
|
|
|
paths.push(Path::new(fp_str));
|
|
|
|
}
|
|
|
|
more_files = libc::FindNextFileW(find_handle,
|
|
|
|
wfd_ptr as libc::HANDLE);
|
|
|
|
}
|
|
|
|
assert!(libc::FindClose(find_handle) != 0);
|
|
|
|
libc::free(wfd_ptr as *mut c_void);
|
|
|
|
Ok(prune(p, paths))
|
|
|
|
} else {
|
|
|
|
Err(super::last_error())
|
|
|
|
}
|
2014-06-02 14:51:58 -07:00
|
|
|
}
|
2014-02-26 12:57:00 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn unlink(p: &CString) -> IoResult<()> {
|
2014-06-02 14:51:58 -07:00
|
|
|
let p = try!(to_utf16(p));
|
2014-02-26 12:57:00 -08:00
|
|
|
super::mkerr_winbool(unsafe {
|
2014-06-02 14:51:58 -07:00
|
|
|
libc::DeleteFileW(p.as_ptr())
|
2014-02-26 12:57:00 -08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn rename(old: &CString, new: &CString) -> IoResult<()> {
|
2014-06-02 14:51:58 -07:00
|
|
|
let old = try!(to_utf16(old));
|
|
|
|
let new = try!(to_utf16(new));
|
2014-02-26 12:57:00 -08:00
|
|
|
super::mkerr_winbool(unsafe {
|
2014-06-02 14:51:58 -07:00
|
|
|
libc::MoveFileExW(old.as_ptr(), new.as_ptr(),
|
|
|
|
libc::MOVEFILE_REPLACE_EXISTING)
|
2014-02-26 12:57:00 -08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-06-04 00:01:40 -07:00
|
|
|
pub fn chmod(p: &CString, mode: uint) -> IoResult<()> {
|
2014-06-02 14:51:58 -07:00
|
|
|
let p = try!(to_utf16(p));
|
|
|
|
super::mkerr_libc(unsafe {
|
2014-06-09 19:55:28 -07:00
|
|
|
libc::wchmod(p.as_ptr(), mode as libc::c_int)
|
2014-06-02 14:51:58 -07:00
|
|
|
})
|
2014-02-26 12:57:00 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn rmdir(p: &CString) -> IoResult<()> {
|
2014-06-02 14:51:58 -07:00
|
|
|
let p = try!(to_utf16(p));
|
|
|
|
super::mkerr_libc(unsafe { libc::wrmdir(p.as_ptr()) })
|
2014-02-26 12:57:00 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn chown(_p: &CString, _uid: int, _gid: int) -> IoResult<()> {
|
|
|
|
// libuv has this as a no-op, so seems like this should as well?
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2014-06-04 00:01:40 -07:00
|
|
|
pub fn readlink(p: &CString) -> IoResult<CString> {
|
2014-02-26 12:57:00 -08:00
|
|
|
// FIXME: I have a feeling that this reads intermediate symlinks as well.
|
2014-05-07 11:06:15 -07:00
|
|
|
use io::c::compat::kernel32::GetFinalPathNameByHandleW;
|
2014-06-02 14:51:58 -07:00
|
|
|
let p = try!(to_utf16(p));
|
2014-02-26 12:57:00 -08:00
|
|
|
let handle = unsafe {
|
2014-06-02 14:51:58 -07:00
|
|
|
libc::CreateFileW(p.as_ptr(),
|
|
|
|
libc::GENERIC_READ,
|
|
|
|
libc::FILE_SHARE_READ,
|
|
|
|
ptr::mut_null(),
|
|
|
|
libc::OPEN_EXISTING,
|
|
|
|
libc::FILE_ATTRIBUTE_NORMAL,
|
|
|
|
ptr::mut_null())
|
2014-02-26 12:57:00 -08:00
|
|
|
};
|
2014-08-07 16:40:12 -04:00
|
|
|
if handle == libc::INVALID_HANDLE_VALUE {
|
2014-02-26 12:57:00 -08:00
|
|
|
return Err(super::last_error())
|
|
|
|
}
|
|
|
|
// Specify (sz - 1) because the documentation states that it's the size
|
|
|
|
// without the null pointer
|
|
|
|
let ret = fill_utf16_buf_and_decode(|buf, sz| unsafe {
|
2014-05-03 14:27:36 -07:00
|
|
|
GetFinalPathNameByHandleW(handle,
|
2014-06-25 12:47:34 -07:00
|
|
|
buf as *const u16,
|
2014-05-03 14:27:36 -07:00
|
|
|
sz - 1,
|
|
|
|
libc::VOLUME_NAME_DOS)
|
2014-02-26 12:57:00 -08:00
|
|
|
});
|
|
|
|
let ret = match ret {
|
2014-05-19 23:19:56 -07:00
|
|
|
Some(ref s) if s.as_slice().starts_with(r"\\?\") => {
|
2014-06-04 00:01:40 -07:00
|
|
|
Ok(Path::new(s.as_slice().slice_from(4)).to_c_str())
|
2014-05-19 23:19:56 -07:00
|
|
|
}
|
2014-06-04 00:01:40 -07:00
|
|
|
Some(s) => Ok(Path::new(s).to_c_str()),
|
2014-02-26 12:57:00 -08:00
|
|
|
None => Err(super::last_error()),
|
|
|
|
};
|
|
|
|
assert!(unsafe { libc::CloseHandle(handle) } != 0);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn symlink(src: &CString, dst: &CString) -> IoResult<()> {
|
2014-05-07 11:06:15 -07:00
|
|
|
use io::c::compat::kernel32::CreateSymbolicLinkW;
|
2014-06-02 14:51:58 -07:00
|
|
|
let src = try!(to_utf16(src));
|
|
|
|
let dst = try!(to_utf16(dst));
|
|
|
|
super::mkerr_winbool(unsafe {
|
|
|
|
CreateSymbolicLinkW(dst.as_ptr(), src.as_ptr(), 0) as libc::BOOL
|
|
|
|
})
|
2014-02-26 12:57:00 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn link(src: &CString, dst: &CString) -> IoResult<()> {
|
2014-06-02 14:51:58 -07:00
|
|
|
let src = try!(to_utf16(src));
|
|
|
|
let dst = try!(to_utf16(dst));
|
|
|
|
super::mkerr_winbool(unsafe {
|
|
|
|
libc::CreateHardLinkW(dst.as_ptr(), src.as_ptr(), ptr::mut_null())
|
|
|
|
})
|
2014-02-26 12:57:00 -08:00
|
|
|
}
|
|
|
|
|
2014-06-04 00:01:40 -07:00
|
|
|
fn mkstat(stat: &libc::stat) -> rtio::FileStat {
|
|
|
|
rtio::FileStat {
|
2014-02-26 12:57:00 -08:00
|
|
|
size: stat.st_size as u64,
|
2014-06-04 00:01:40 -07:00
|
|
|
kind: stat.st_mode as u64,
|
|
|
|
perm: stat.st_mode as u64,
|
2014-02-26 12:57:00 -08:00
|
|
|
created: stat.st_ctime as u64,
|
|
|
|
modified: stat.st_mtime as u64,
|
|
|
|
accessed: stat.st_atime as u64,
|
2014-06-04 00:01:40 -07:00
|
|
|
device: stat.st_dev as u64,
|
|
|
|
inode: stat.st_ino as u64,
|
|
|
|
rdev: stat.st_rdev as u64,
|
|
|
|
nlink: stat.st_nlink as u64,
|
|
|
|
uid: stat.st_uid as u64,
|
|
|
|
gid: stat.st_gid as u64,
|
|
|
|
blksize: 0,
|
|
|
|
blocks: 0,
|
|
|
|
flags: 0,
|
|
|
|
gen: 0,
|
2014-02-26 12:57:00 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-04 00:01:40 -07:00
|
|
|
pub fn stat(p: &CString) -> IoResult<rtio::FileStat> {
|
2014-05-23 20:53:56 -07:00
|
|
|
let mut stat: libc::stat = unsafe { mem::zeroed() };
|
2014-06-02 14:51:58 -07:00
|
|
|
let p = try!(to_utf16(p));
|
|
|
|
match unsafe { libc::wstat(p.as_ptr(), &mut stat) } {
|
|
|
|
0 => Ok(mkstat(&stat)),
|
|
|
|
_ => Err(super::last_error()),
|
|
|
|
}
|
2014-02-26 12:57:00 -08:00
|
|
|
}
|
|
|
|
|
2014-06-04 00:01:40 -07:00
|
|
|
pub fn lstat(_p: &CString) -> IoResult<rtio::FileStat> {
|
2014-02-26 12:57:00 -08:00
|
|
|
// FIXME: implementation is missing
|
|
|
|
Err(super::unimpl())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn utime(p: &CString, atime: u64, mtime: u64) -> IoResult<()> {
|
2014-06-25 12:47:34 -07:00
|
|
|
let mut buf = libc::utimbuf {
|
2014-08-02 10:52:49 -07:00
|
|
|
actime: atime as libc::time64_t,
|
|
|
|
modtime: mtime as libc::time64_t,
|
2014-02-26 12:57:00 -08:00
|
|
|
};
|
2014-06-02 14:51:58 -07:00
|
|
|
let p = try!(to_utf16(p));
|
|
|
|
super::mkerr_libc(unsafe {
|
2014-06-25 12:47:34 -07:00
|
|
|
libc::wutime(p.as_ptr(), &mut buf)
|
2014-06-02 14:51:58 -07:00
|
|
|
})
|
2014-02-26 12:57:00 -08:00
|
|
|
}
|