1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! Blocking Windows-based file I/O
14 use libc::{self, c_int};
17 use sys::os::fill_utf16_buf_and_decode;
26 use sys_common::{keep_going, eof, mkerr_libc};
28 use io::{FilePermission, Write, UnstableFileStat, Open, FileAccess, FileMode};
29 use io::{IoResult, IoError, FileStat, SeekStyle};
30 use io::{Read, Truncate, SeekCur, SeekSet, ReadWrite, SeekEnd, Append};
32 pub type fd_t = libc::c_int;
35 /// The underlying C file descriptor.
38 /// Whether to close the file descriptor on drop.
43 pub fn new(fd: fd_t, close_on_drop: bool) -> FileDesc {
44 FileDesc { fd: fd, close_on_drop: close_on_drop }
47 pub fn read(&self, buf: &mut [u8]) -> IoResult<uint> {
50 libc::ReadFile(self.handle(), buf.as_ptr() as libc::LPVOID,
51 buf.len() as libc::DWORD, &mut read,
57 Err(super::last_error())
61 pub fn write(&self, buf: &[u8]) -> IoResult<()> {
62 let mut cur = buf.as_ptr();
63 let mut remaining = buf.len();
67 libc::WriteFile(self.handle(), cur as libc::LPVOID,
68 remaining as libc::DWORD, &mut amt,
72 remaining -= amt as uint;
73 cur = unsafe { cur.offset(amt as int) };
75 return Err(super::last_error())
81 pub fn fd(&self) -> fd_t { self.fd }
83 pub fn handle(&self) -> libc::HANDLE {
84 unsafe { libc::get_osfhandle(self.fd()) as libc::HANDLE }
87 // A version of seek that takes &self so that tell can call it
88 // - the private seek should of course take &mut self.
89 fn seek_common(&self, pos: i64, style: SeekStyle) -> IoResult<u64> {
90 let whence = match style {
91 SeekSet => libc::FILE_BEGIN,
92 SeekEnd => libc::FILE_END,
93 SeekCur => libc::FILE_CURRENT,
97 match libc::SetFilePointerEx(self.handle(), pos, &mut newpos, whence) {
98 0 => Err(super::last_error()),
99 _ => Ok(newpos as u64),
104 pub fn seek(&mut self, pos: i64, style: SeekStyle) -> IoResult<u64> {
105 self.seek_common(pos, style)
108 pub fn tell(&self) -> IoResult<u64> {
109 self.seek_common(0, SeekCur)
112 pub fn fsync(&mut self) -> IoResult<()> {
113 super::mkerr_winbool(unsafe {
114 libc::FlushFileBuffers(self.handle())
118 pub fn datasync(&mut self) -> IoResult<()> { return self.fsync(); }
120 pub fn truncate(&mut self, offset: i64) -> IoResult<()> {
121 let orig_pos = try!(self.tell());
122 let _ = try!(self.seek(offset, SeekSet));
124 match libc::SetEndOfFile(self.handle()) {
125 0 => Err(super::last_error()),
129 let _ = self.seek(orig_pos as i64, SeekSet);
133 pub fn fstat(&self) -> IoResult<io::FileStat> {
134 let mut stat: libc::stat = unsafe { mem::zeroed() };
135 match unsafe { libc::fstat(self.fd(), &mut stat) } {
136 0 => Ok(mkstat(&stat)),
137 _ => Err(super::last_error()),
141 /// Extract the actual filedescriptor without closing it.
142 pub fn unwrap(self) -> fd_t {
144 unsafe { mem::forget(self) };
149 impl Drop for FileDesc {
151 // closing stdio file handles makes no sense, so never do it. Also, note
152 // that errors are ignored when closing a file descriptor. The reason
153 // for this is that if an error occurs we don't actually know if the
154 // file descriptor was closed or not, and if we retried (for something
155 // like EINTR), we might close another valid file descriptor (opened
156 // after we closed ours.
157 if self.close_on_drop && self.fd > libc::STDERR_FILENO {
158 let n = unsafe { libc::close(self.fd) };
160 println!("error {} when closing file descriptor {}", n, self.fd);
166 pub fn to_utf16(s: &Path) -> IoResult<Vec<u16>> {
167 sys::to_utf16(s.as_str())
170 pub fn open(path: &Path, fm: FileMode, fa: FileAccess) -> IoResult<FileDesc> {
171 // Flags passed to open_osfhandle
172 let flags = match fm {
174 Append => libc::O_APPEND,
175 Truncate => libc::O_TRUNC,
177 let flags = match fa {
178 Read => flags | libc::O_RDONLY,
179 Write => flags | libc::O_WRONLY | libc::O_CREAT,
180 ReadWrite => flags | libc::O_RDWR | libc::O_CREAT,
182 let mut dwDesiredAccess = match fa {
183 Read => libc::FILE_GENERIC_READ,
184 Write => libc::FILE_GENERIC_WRITE,
185 ReadWrite => libc::FILE_GENERIC_READ | libc::FILE_GENERIC_WRITE
188 // libuv has a good comment about this, but the basic idea is what we try to
189 // emulate unix semantics by enabling all sharing by allowing things such as
190 // deleting a file while it's still open.
191 let dwShareMode = libc::FILE_SHARE_READ | libc::FILE_SHARE_WRITE |
192 libc::FILE_SHARE_DELETE;
194 let dwCreationDisposition = match (fm, fa) {
195 (Truncate, Read) => libc::TRUNCATE_EXISTING,
196 (Truncate, _) => libc::CREATE_ALWAYS,
197 (Open, Read) => libc::OPEN_EXISTING,
198 (Open, _) => libc::OPEN_ALWAYS,
200 dwDesiredAccess |= libc::FILE_APPEND_DATA;
204 dwDesiredAccess &= !libc::FILE_WRITE_DATA;
205 dwDesiredAccess |= libc::FILE_APPEND_DATA;
210 let mut dwFlagsAndAttributes = libc::FILE_ATTRIBUTE_NORMAL;
211 // Compat with unix, this allows opening directories (see libuv)
212 dwFlagsAndAttributes |= libc::FILE_FLAG_BACKUP_SEMANTICS;
214 let path = try!(to_utf16(path));
215 let handle = unsafe {
216 libc::CreateFileW(path.as_ptr(),
220 dwCreationDisposition,
221 dwFlagsAndAttributes,
224 if handle == libc::INVALID_HANDLE_VALUE {
225 Err(super::last_error())
228 libc::open_osfhandle(handle as libc::intptr_t, flags)
231 let _ = unsafe { libc::CloseHandle(handle) };
232 Err(super::last_error())
234 Ok(FileDesc::new(fd, true))
239 pub fn mkdir(p: &Path, _mode: uint) -> IoResult<()> {
240 let p = try!(to_utf16(p));
241 super::mkerr_winbool(unsafe {
242 // FIXME: turn mode into something useful? #2623
243 libc::CreateDirectoryW(p.as_ptr(), ptr::null_mut())
247 pub fn readdir(p: &Path) -> IoResult<Vec<Path>> {
248 fn prune(root: &Path, dirs: Vec<Path>) -> Vec<Path> {
249 dirs.into_iter().filter(|path| {
250 path.as_vec() != b"." && path.as_vec() != b".."
251 }).map(|path| root.join(path)).collect()
254 let star = p.join("*");
255 let path = try!(to_utf16(&star));
258 let mut wfd = mem::zeroed();
259 let find_handle = libc::FindFirstFileW(path.as_ptr(), &mut wfd);
260 if find_handle != libc::INVALID_HANDLE_VALUE {
261 let mut paths = vec![];
262 let mut more_files = 1 as libc::BOOL;
263 while more_files != 0 {
265 let filename = os::truncate_utf16_at_nul(&wfd.cFileName);
266 match String::from_utf16(filename) {
267 Ok(filename) => paths.push(Path::new(filename)),
269 assert!(libc::FindClose(find_handle) != 0);
271 kind: io::InvalidInput,
272 desc: "path was not valid UTF-16",
273 detail: Some(format!("path was not valid UTF-16: {}", filename)),
275 }, // FIXME #12056: Convert the UCS-2 to invalid utf-8 instead of erroring
278 more_files = libc::FindNextFileW(find_handle, &mut wfd);
280 assert!(libc::FindClose(find_handle) != 0);
283 Err(super::last_error())
288 pub fn unlink(p: &Path) -> IoResult<()> {
289 fn do_unlink(p_utf16: &Vec<u16>) -> IoResult<()> {
290 super::mkerr_winbool(unsafe { libc::DeleteFileW(p_utf16.as_ptr()) })
293 let p_utf16 = try!(to_utf16(p));
294 let res = do_unlink(&p_utf16);
298 // FIXME: change the code below to use more direct calls
299 // than `stat` and `chmod`, to avoid re-conversion to
302 // On unix, a readonly file can be successfully removed. On windows,
303 // however, it cannot. To keep the two platforms in line with
304 // respect to their behavior, catch this case on windows, attempt to
305 // change it to read-write, and then remove the file.
306 if e.kind == io::PermissionDenied {
307 let stat = match stat(p) {
309 Err(..) => return Err(e),
311 if stat.perm.intersects(io::USER_WRITE) { return Err(e) }
313 match chmod(p, (stat.perm | io::USER_WRITE).bits() as uint) {
314 Ok(()) => do_unlink(&p_utf16),
316 // Try to put it back as we found it
317 let _ = chmod(p, stat.perm.bits() as uint);
328 pub fn rename(old: &Path, new: &Path) -> IoResult<()> {
329 let old = try!(to_utf16(old));
330 let new = try!(to_utf16(new));
331 super::mkerr_winbool(unsafe {
332 libc::MoveFileExW(old.as_ptr(), new.as_ptr(), libc::MOVEFILE_REPLACE_EXISTING)
336 pub fn chmod(p: &Path, mode: uint) -> IoResult<()> {
337 let p = try!(to_utf16(p));
339 libc::wchmod(p.as_ptr(), mode as libc::c_int)
343 pub fn rmdir(p: &Path) -> IoResult<()> {
344 let p = try!(to_utf16(p));
345 mkerr_libc(unsafe { libc::wrmdir(p.as_ptr()) })
348 pub fn chown(_p: &Path, _uid: int, _gid: int) -> IoResult<()> {
349 // libuv has this as a no-op, so seems like this should as well?
353 pub fn readlink(p: &Path) -> IoResult<Path> {
354 // FIXME: I have a feeling that this reads intermediate symlinks as well.
355 use sys::c::compat::kernel32::GetFinalPathNameByHandleW;
356 let p = try!(to_utf16(p));
357 let handle = unsafe {
358 libc::CreateFileW(p.as_ptr(),
360 libc::FILE_SHARE_READ,
363 libc::FILE_ATTRIBUTE_NORMAL,
366 if handle == libc::INVALID_HANDLE_VALUE {
367 return Err(super::last_error())
369 // Specify (sz - 1) because the documentation states that it's the size
370 // without the null pointer
371 let ret = fill_utf16_buf_and_decode(|buf, sz| unsafe {
372 GetFinalPathNameByHandleW(handle,
375 libc::VOLUME_NAME_DOS)
377 let ret = match ret {
378 Some(ref s) if s.starts_with(r"\\?\") => { // "
379 Ok(Path::new(s.slice_from(4)))
381 Some(s) => Ok(Path::new(s)),
382 None => Err(super::last_error()),
384 assert!(unsafe { libc::CloseHandle(handle) } != 0);
388 pub fn symlink(src: &Path, dst: &Path) -> IoResult<()> {
389 use sys::c::compat::kernel32::CreateSymbolicLinkW;
390 let src = try!(to_utf16(src));
391 let dst = try!(to_utf16(dst));
392 super::mkerr_winbool(unsafe {
393 CreateSymbolicLinkW(dst.as_ptr(), src.as_ptr(), 0) as libc::BOOL
397 pub fn link(src: &Path, dst: &Path) -> IoResult<()> {
398 let src = try!(to_utf16(src));
399 let dst = try!(to_utf16(dst));
400 super::mkerr_winbool(unsafe {
401 libc::CreateHardLinkW(dst.as_ptr(), src.as_ptr(), ptr::null_mut())
405 fn mkstat(stat: &libc::stat) -> FileStat {
407 size: stat.st_size as u64,
408 kind: match (stat.st_mode as libc::c_int) & libc::S_IFMT {
409 libc::S_IFREG => io::FileType::RegularFile,
410 libc::S_IFDIR => io::FileType::Directory,
411 libc::S_IFIFO => io::FileType::NamedPipe,
412 libc::S_IFBLK => io::FileType::BlockSpecial,
413 libc::S_IFLNK => io::FileType::Symlink,
414 _ => io::FileType::Unknown,
416 perm: FilePermission::from_bits_truncate(stat.st_mode as u32),
417 created: stat.st_ctime as u64,
418 modified: stat.st_mtime as u64,
419 accessed: stat.st_atime as u64,
420 unstable: UnstableFileStat {
421 device: stat.st_dev as u64,
422 inode: stat.st_ino as u64,
423 rdev: stat.st_rdev as u64,
424 nlink: stat.st_nlink as u64,
425 uid: stat.st_uid as u64,
426 gid: stat.st_gid as u64,
435 pub fn stat(p: &Path) -> IoResult<FileStat> {
436 let mut stat: libc::stat = unsafe { mem::zeroed() };
437 let p = try!(to_utf16(p));
438 match unsafe { libc::wstat(p.as_ptr(), &mut stat) } {
439 0 => Ok(mkstat(&stat)),
440 _ => Err(super::last_error()),
444 // FIXME: move this to platform-specific modules (for now)?
445 pub fn lstat(_p: &Path) -> IoResult<FileStat> {
446 // FIXME: implementation is missing
450 pub fn utime(p: &Path, atime: u64, mtime: u64) -> IoResult<()> {
451 let mut buf = libc::utimbuf {
452 actime: atime as libc::time64_t,
453 modtime: mtime as libc::time64_t,
455 let p = try!(to_utf16(p));
457 libc::wutime(p.as_ptr(), &mut buf)