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.
13 use os::windows::prelude::*;
17 use io::{self, Error, SeekFrom};
18 use libc::{self, HANDLE};
20 use path::{Path, PathBuf};
24 use sys::handle::Handle;
26 use sys_common::FromInner;
29 pub struct File { handle: Handle }
32 data: c::WIN32_FILE_ATTRIBUTE_DATA,
36 #[derive(Copy, Clone, PartialEq, Eq, Hash)]
38 Dir, File, Symlink, ReparsePoint
42 handle: FindNextFileHandle,
44 first: Option<libc::WIN32_FIND_DATAW>,
47 struct FindNextFileHandle(libc::HANDLE);
49 unsafe impl Send for FindNextFileHandle {}
50 unsafe impl Sync for FindNextFileHandle {}
54 data: libc::WIN32_FIND_DATAW,
57 #[derive(Clone, Default)]
58 pub struct OpenOptions {
64 desired_access: Option<libc::DWORD>,
65 share_mode: Option<libc::DWORD>,
66 creation_disposition: Option<libc::DWORD>,
67 flags_and_attributes: Option<libc::DWORD>,
70 #[derive(Clone, PartialEq, Eq, Debug)]
71 pub struct FilePermissions { attrs: libc::DWORD }
73 pub struct DirBuilder;
75 impl Iterator for ReadDir {
76 type Item = io::Result<DirEntry>;
77 fn next(&mut self) -> Option<io::Result<DirEntry>> {
78 if let Some(first) = self.first.take() {
79 if let Some(e) = DirEntry::new(&self.root, &first) {
84 let mut wfd = mem::zeroed();
86 if libc::FindNextFileW(self.handle.0, &mut wfd) == 0 {
87 if libc::GetLastError() ==
88 c::ERROR_NO_MORE_FILES as libc::DWORD {
91 return Some(Err(Error::last_os_error()))
94 if let Some(e) = DirEntry::new(&self.root, &wfd) {
102 impl Drop for FindNextFileHandle {
104 let r = unsafe { libc::FindClose(self.0) };
105 debug_assert!(r != 0);
110 fn new(root: &Arc<PathBuf>, wfd: &libc::WIN32_FIND_DATAW) -> Option<DirEntry> {
111 match &wfd.cFileName[0..3] {
112 // check for '.' and '..'
114 [46, 46, 0, ..] => return None,
124 pub fn path(&self) -> PathBuf {
125 self.root.join(&self.file_name())
128 pub fn file_name(&self) -> OsString {
129 let filename = super::truncate_utf16_at_nul(&self.data.cFileName);
130 OsString::from_wide(filename)
133 pub fn file_type(&self) -> io::Result<FileType> {
134 Ok(FileType::new(self.data.dwFileAttributes,
135 self.data.dwReserved0 == c::IO_REPARSE_TAG_SYMLINK))
138 pub fn metadata(&self) -> io::Result<FileAttr> {
140 data: c::WIN32_FILE_ATTRIBUTE_DATA {
141 dwFileAttributes: self.data.dwFileAttributes,
142 ftCreationTime: self.data.ftCreationTime,
143 ftLastAccessTime: self.data.ftLastAccessTime,
144 ftLastWriteTime: self.data.ftLastWriteTime,
145 nFileSizeHigh: self.data.nFileSizeHigh,
146 nFileSizeLow: self.data.nFileSizeLow,
148 is_symlink: self.data.dwReserved0 == c::IO_REPARSE_TAG_SYMLINK,
154 pub fn new() -> OpenOptions { Default::default() }
155 pub fn read(&mut self, read: bool) { self.read = read; }
156 pub fn write(&mut self, write: bool) { self.write = write; }
157 pub fn append(&mut self, append: bool) { self.append = append; }
158 pub fn create(&mut self, create: bool) { self.create = create; }
159 pub fn truncate(&mut self, truncate: bool) { self.truncate = truncate; }
160 pub fn creation_disposition(&mut self, val: i32) {
161 self.creation_disposition = Some(val as libc::DWORD);
163 pub fn flags_and_attributes(&mut self, val: i32) {
164 self.flags_and_attributes = Some(val as libc::DWORD);
166 pub fn desired_access(&mut self, val: i32) {
167 self.desired_access = Some(val as libc::DWORD);
169 pub fn share_mode(&mut self, val: i32) {
170 self.share_mode = Some(val as libc::DWORD);
173 fn get_desired_access(&self) -> libc::DWORD {
174 self.desired_access.unwrap_or({
175 let mut base = if self.read {libc::FILE_GENERIC_READ} else {0} |
176 if self.write {libc::FILE_GENERIC_WRITE} else {0};
178 base &= !libc::FILE_WRITE_DATA;
179 base |= libc::FILE_APPEND_DATA;
185 fn get_share_mode(&self) -> libc::DWORD {
186 // libuv has a good comment about this, but the basic idea is that
187 // we try to emulate unix semantics by enabling all sharing by
188 // allowing things such as deleting a file while it's still open.
189 self.share_mode.unwrap_or(libc::FILE_SHARE_READ |
190 libc::FILE_SHARE_WRITE |
191 libc::FILE_SHARE_DELETE)
194 fn get_creation_disposition(&self) -> libc::DWORD {
195 self.creation_disposition.unwrap_or({
196 match (self.create, self.truncate) {
197 (true, true) => libc::CREATE_ALWAYS,
198 (true, false) => libc::OPEN_ALWAYS,
199 (false, false) => libc::OPEN_EXISTING,
201 if self.write && !self.append {
204 libc::TRUNCATE_EXISTING
211 fn get_flags_and_attributes(&self) -> libc::DWORD {
212 self.flags_and_attributes.unwrap_or(libc::FILE_ATTRIBUTE_NORMAL)
217 fn open_reparse_point(path: &Path) -> io::Result<File> {
218 let mut opts = OpenOptions::new();
220 opts.flags_and_attributes(c::FILE_FLAG_OPEN_REPARSE_POINT as i32);
221 File::open(path, &opts)
224 pub fn open(path: &Path, opts: &OpenOptions) -> io::Result<File> {
225 let path = to_utf16(path);
226 let handle = unsafe {
227 libc::CreateFileW(path.as_ptr(),
228 opts.get_desired_access(),
229 opts.get_share_mode(),
231 opts.get_creation_disposition(),
232 opts.get_flags_and_attributes(),
235 if handle == libc::INVALID_HANDLE_VALUE {
236 Err(Error::last_os_error())
238 Ok(File { handle: Handle::new(handle) })
242 pub fn fsync(&self) -> io::Result<()> {
243 try!(cvt(unsafe { libc::FlushFileBuffers(self.handle.raw()) }));
247 pub fn datasync(&self) -> io::Result<()> { self.fsync() }
249 pub fn truncate(&self, size: u64) -> io::Result<()> {
250 let mut info = c::FILE_END_OF_FILE_INFO {
251 EndOfFile: size as libc::LARGE_INTEGER,
253 let size = mem::size_of_val(&info);
255 c::SetFileInformationByHandle(self.handle.raw(),
256 c::FileEndOfFileInfo,
257 &mut info as *mut _ as *mut _,
263 pub fn file_attr(&self) -> io::Result<FileAttr> {
265 let mut info: c::BY_HANDLE_FILE_INFORMATION = mem::zeroed();
266 try!(cvt(c::GetFileInformationByHandle(self.handle.raw(),
268 let mut attr = FileAttr {
269 data: c::WIN32_FILE_ATTRIBUTE_DATA {
270 dwFileAttributes: info.dwFileAttributes,
271 ftCreationTime: info.ftCreationTime,
272 ftLastAccessTime: info.ftLastAccessTime,
273 ftLastWriteTime: info.ftLastWriteTime,
274 nFileSizeHigh: info.nFileSizeHigh,
275 nFileSizeLow: info.nFileSizeLow,
279 if attr.is_reparse_point() {
280 attr.is_symlink = self.is_symlink();
286 pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
287 self.handle.read(buf)
290 pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
291 self.handle.write(buf)
294 pub fn flush(&self) -> io::Result<()> { Ok(()) }
296 pub fn seek(&self, pos: SeekFrom) -> io::Result<u64> {
297 let (whence, pos) = match pos {
298 SeekFrom::Start(n) => (libc::FILE_BEGIN, n as i64),
299 SeekFrom::End(n) => (libc::FILE_END, n),
300 SeekFrom::Current(n) => (libc::FILE_CURRENT, n),
302 let pos = pos as libc::LARGE_INTEGER;
305 libc::SetFilePointerEx(self.handle.raw(), pos,
311 pub fn handle(&self) -> &Handle { &self.handle }
313 fn is_symlink(&self) -> bool {
314 self.readlink().is_ok()
317 fn readlink(&self) -> io::Result<PathBuf> {
318 let mut space = [0u8; c::MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
323 c::DeviceIoControl(self.handle.raw(),
324 c::FSCTL_GET_REPARSE_POINT,
327 space.as_mut_ptr() as *mut _,
328 space.len() as libc::DWORD,
332 let buf: *const c::REPARSE_DATA_BUFFER = space.as_ptr() as *const _;
333 if (*buf).ReparseTag != c::IO_REPARSE_TAG_SYMLINK {
334 return Err(io::Error::new(io::ErrorKind::Other, "not a symlink"))
336 let info: *const c::SYMBOLIC_LINK_REPARSE_BUFFER =
337 &(*buf).rest as *const _ as *const _;
338 let path_buffer = &(*info).PathBuffer as *const _ as *const u16;
339 let subst_off = (*info).SubstituteNameOffset / 2;
340 let subst_ptr = path_buffer.offset(subst_off as isize);
341 let subst_len = (*info).SubstituteNameLength / 2;
342 let subst = slice::from_raw_parts(subst_ptr, subst_len as usize);
344 Ok(PathBuf::from(OsString::from_wide(subst)))
349 impl FromInner<libc::HANDLE> for File {
350 fn from_inner(handle: libc::HANDLE) -> File {
351 File { handle: Handle::new(handle) }
355 impl fmt::Debug for File {
356 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
357 // FIXME(#24570): add more info here (e.g. path, mode)
358 f.debug_struct("File")
359 .field("handle", &self.handle.raw())
364 pub fn to_utf16(s: &Path) -> Vec<u16> {
365 s.as_os_str().encode_wide().chain(Some(0).into_iter()).collect()
369 pub fn size(&self) -> u64 {
370 ((self.data.nFileSizeHigh as u64) << 32) | (self.data.nFileSizeLow as u64)
373 pub fn perm(&self) -> FilePermissions {
374 FilePermissions { attrs: self.data.dwFileAttributes }
377 pub fn attrs(&self) -> u32 { self.data.dwFileAttributes as u32 }
379 pub fn file_type(&self) -> FileType {
380 FileType::new(self.data.dwFileAttributes, self.is_symlink)
383 pub fn created(&self) -> u64 { self.to_u64(&self.data.ftCreationTime) }
384 pub fn accessed(&self) -> u64 { self.to_u64(&self.data.ftLastAccessTime) }
385 pub fn modified(&self) -> u64 { self.to_u64(&self.data.ftLastWriteTime) }
387 fn to_u64(&self, ft: &libc::FILETIME) -> u64 {
388 (ft.dwLowDateTime as u64) | ((ft.dwHighDateTime as u64) << 32)
391 fn is_reparse_point(&self) -> bool {
392 self.data.dwFileAttributes & libc::FILE_ATTRIBUTE_REPARSE_POINT != 0
396 impl FilePermissions {
397 pub fn readonly(&self) -> bool {
398 self.attrs & c::FILE_ATTRIBUTE_READONLY != 0
401 pub fn set_readonly(&mut self, readonly: bool) {
403 self.attrs |= c::FILE_ATTRIBUTE_READONLY;
405 self.attrs &= !c::FILE_ATTRIBUTE_READONLY;
411 fn new(attrs: libc::DWORD, is_symlink: bool) -> FileType {
412 if attrs & libc::FILE_ATTRIBUTE_REPARSE_POINT != 0 {
416 FileType::ReparsePoint
418 } else if attrs & c::FILE_ATTRIBUTE_DIRECTORY != 0 {
425 pub fn is_dir(&self) -> bool { *self == FileType::Dir }
426 pub fn is_file(&self) -> bool { *self == FileType::File }
427 pub fn is_symlink(&self) -> bool { *self == FileType::Symlink }
431 pub fn new() -> DirBuilder { DirBuilder }
433 pub fn mkdir(&self, p: &Path) -> io::Result<()> {
436 libc::CreateDirectoryW(p.as_ptr(), ptr::null_mut())
442 pub fn readdir(p: &Path) -> io::Result<ReadDir> {
443 let root = p.to_path_buf();
444 let star = p.join("*");
445 let path = to_utf16(&star);
448 let mut wfd = mem::zeroed();
449 let find_handle = libc::FindFirstFileW(path.as_ptr(), &mut wfd);
450 if find_handle != libc::INVALID_HANDLE_VALUE {
452 handle: FindNextFileHandle(find_handle),
453 root: Arc::new(root),
457 Err(Error::last_os_error())
462 pub fn unlink(p: &Path) -> io::Result<()> {
463 let p_utf16 = to_utf16(p);
464 try!(cvt(unsafe { libc::DeleteFileW(p_utf16.as_ptr()) }));
468 pub fn rename(old: &Path, new: &Path) -> io::Result<()> {
469 let old = to_utf16(old);
470 let new = to_utf16(new);
472 libc::MoveFileExW(old.as_ptr(), new.as_ptr(),
473 libc::MOVEFILE_REPLACE_EXISTING)
478 pub fn rmdir(p: &Path) -> io::Result<()> {
480 try!(cvt(unsafe { c::RemoveDirectoryW(p.as_ptr()) }));
484 pub fn readlink(p: &Path) -> io::Result<PathBuf> {
485 let file = try!(File::open_reparse_point(p));
489 pub fn symlink(src: &Path, dst: &Path) -> io::Result<()> {
490 symlink_inner(src, dst, false)
493 pub fn symlink_inner(src: &Path, dst: &Path, dir: bool) -> io::Result<()> {
494 use sys::c::compat::kernel32::CreateSymbolicLinkW;
495 let src = to_utf16(src);
496 let dst = to_utf16(dst);
497 let flags = if dir { c::SYMBOLIC_LINK_FLAG_DIRECTORY } else { 0 };
499 CreateSymbolicLinkW(dst.as_ptr(), src.as_ptr(), flags) as libc::BOOL
504 pub fn link(src: &Path, dst: &Path) -> io::Result<()> {
505 let src = to_utf16(src);
506 let dst = to_utf16(dst);
508 libc::CreateHardLinkW(dst.as_ptr(), src.as_ptr(), ptr::null_mut())
513 pub fn stat(p: &Path) -> io::Result<FileAttr> {
514 let attr = try!(lstat(p));
515 if attr.data.dwFileAttributes & libc::FILE_ATTRIBUTE_REPARSE_POINT != 0 {
516 let opts = OpenOptions::new();
517 let file = try!(File::open(p, &opts));
524 pub fn lstat(p: &Path) -> io::Result<FileAttr> {
525 let utf16 = to_utf16(p);
527 let mut attr: FileAttr = mem::zeroed();
528 try!(cvt(c::GetFileAttributesExW(utf16.as_ptr(),
529 c::GetFileExInfoStandard,
530 &mut attr.data as *mut _ as *mut _)));
531 if attr.is_reparse_point() {
532 attr.is_symlink = File::open_reparse_point(p).map(|f| {
540 pub fn set_perm(p: &Path, perm: FilePermissions) -> io::Result<()> {
543 try!(cvt(c::SetFileAttributesW(p.as_ptr(), perm.attrs)));
548 pub fn utimes(p: &Path, atime: u64, mtime: u64) -> io::Result<()> {
549 let atime = super::ms_to_filetime(atime);
550 let mtime = super::ms_to_filetime(mtime);
552 let mut o = OpenOptions::new();
554 let f = try!(File::open(p, &o));
556 c::SetFileTime(f.handle.raw(), 0 as *const _, &atime, &mtime)
561 pub fn canonicalize(p: &Path) -> io::Result<PathBuf> {
562 use sys::c::compat::kernel32::GetFinalPathNameByHandleW;
564 let mut opts = OpenOptions::new();
566 let f = try!(File::open(p, &opts));
567 super::fill_utf16_buf(|buf, sz| unsafe {
568 GetFinalPathNameByHandleW(f.handle.raw(), buf, sz,
569 libc::VOLUME_NAME_DOS)
571 PathBuf::from(OsString::from_wide(buf))