forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfile_win32.rs
515 lines (471 loc) · 17.3 KB
/
file_win32.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
// 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
use std::c_str::CString;
use std::cast;
use std::io::IoError;
use std::io;
use libc::{c_int, c_void};
use libc;
use std::mem;
use std::os::win32::{as_utf16_p, fill_utf16_buf_and_decode};
use std::ptr;
use std::rt::rtio;
use std::str;
use std::sync::arc::UnsafeArc;
use std::slice;
use io::IoResult;
pub type fd_t = libc::c_int;
struct Inner {
fd: fd_t,
close_on_drop: bool,
}
pub struct FileDesc {
inner: UnsafeArc<Inner>
}
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 {
FileDesc { inner: UnsafeArc::new(Inner {
fd: fd,
close_on_drop: close_on_drop
}) }
}
pub fn inner_read(&mut self, buf: &mut [u8]) -> Result<uint, IoError> {
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())
}
}
pub fn inner_write(&mut self, buf: &[u8]) -> Result<(), IoError> {
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(())
}
pub fn fd(&self) -> fd_t {
// This unsafety is fine because we're just reading off the file
// descriptor, no one is modifying this.
unsafe { (*self.inner.get()).fd }
}
pub fn handle(&self) -> libc::HANDLE {
unsafe { libc::get_osfhandle(self.fd()) as libc::HANDLE }
}
}
impl io::Reader for FileDesc {
fn read(&mut self, buf: &mut [u8]) -> io::IoResult<uint> {
self.inner_read(buf)
}
}
impl io::Writer for FileDesc {
fn write(&mut self, buf: &[u8]) -> io::IoResult<()> {
self.inner_write(buf)
}
}
impl rtio::RtioFileStream for FileDesc {
fn read(&mut self, buf: &mut [u8]) -> Result<int, IoError> {
self.inner_read(buf).map(|i| i as int)
}
fn write(&mut self, buf: &[u8]) -> Result<(), IoError> {
self.inner_write(buf)
}
fn pread(&mut self, buf: &mut [u8], offset: u64) -> Result<int, IoError> {
let mut read = 0;
let mut overlap: libc::OVERLAPPED = unsafe { mem::init() };
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())
}
}
fn pwrite(&mut self, buf: &[u8], mut offset: u64) -> Result<(), IoError> {
let mut cur = buf.as_ptr();
let mut remaining = buf.len();
let mut overlap: libc::OVERLAPPED = unsafe { mem::init() };
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(())
}
fn seek(&mut self, pos: i64, style: io::SeekStyle) -> Result<u64, IoError> {
let whence = match style {
io::SeekSet => libc::FILE_BEGIN,
io::SeekEnd => libc::FILE_END,
io::SeekCur => libc::FILE_CURRENT,
};
unsafe {
let mut newpos = 0;
match libc::SetFilePointerEx(self.handle(), pos, &mut newpos,
whence) {
0 => Err(super::last_error()),
_ => Ok(newpos as u64),
}
}
}
fn tell(&self) -> Result<u64, IoError> {
// This transmute is fine because our seek implementation doesn't
// actually use the mutable self at all.
unsafe { cast::transmute_mut(self).seek(0, io::SeekCur) }
}
fn fsync(&mut self) -> Result<(), IoError> {
super::mkerr_winbool(unsafe {
libc::FlushFileBuffers(self.handle())
})
}
fn datasync(&mut self) -> Result<(), IoError> { return self.fsync(); }
fn truncate(&mut self, offset: i64) -> Result<(), IoError> {
let orig_pos = try!(self.tell());
let _ = try!(self.seek(offset, io::SeekSet));
let ret = unsafe {
match libc::SetEndOfFile(self.handle()) {
0 => Err(super::last_error()),
_ => Ok(())
}
};
let _ = self.seek(orig_pos as i64, io::SeekSet);
return ret;
}
}
impl rtio::RtioPipe for FileDesc {
fn read(&mut self, buf: &mut [u8]) -> Result<uint, IoError> {
self.inner_read(buf)
}
fn write(&mut self, buf: &[u8]) -> Result<(), IoError> {
self.inner_write(buf)
}
fn clone(&self) -> ~rtio::RtioPipe:Send {
~FileDesc { inner: self.inner.clone() } as ~rtio::RtioPipe:Send
}
}
impl rtio::RtioTTY for FileDesc {
fn read(&mut self, buf: &mut [u8]) -> Result<uint, IoError> {
self.inner_read(buf)
}
fn write(&mut self, buf: &[u8]) -> Result<(), IoError> {
self.inner_write(buf)
}
fn set_raw(&mut self, _raw: bool) -> Result<(), IoError> {
Err(super::unimpl())
}
fn get_winsize(&mut self) -> Result<(int, int), IoError> {
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 {
println!("error {} when closing file descriptor {}", n, self.fd);
}
}
}
}
pub fn open(path: &CString, fm: io::FileMode, fa: io::FileAccess)
-> IoResult<FileDesc> {
// Flags passed to open_osfhandle
let flags = match fm {
io::Open => 0,
io::Append => libc::O_APPEND,
io::Truncate => libc::O_TRUNC,
};
let flags = match fa {
io::Read => flags | libc::O_RDONLY,
io::Write => flags | libc::O_WRONLY | libc::O_CREAT,
io::ReadWrite => flags | libc::O_RDWR | libc::O_CREAT,
};
let mut dwDesiredAccess = match fa {
io::Read => libc::FILE_GENERIC_READ,
io::Write => libc::FILE_GENERIC_WRITE,
io::ReadWrite => libc::FILE_GENERIC_READ | libc::FILE_GENERIC_WRITE
};
// 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) {
(io::Truncate, io::Read) => libc::TRUNCATE_EXISTING,
(io::Truncate, _) => libc::CREATE_ALWAYS,
(io::Open, io::Read) => libc::OPEN_EXISTING,
(io::Open, _) => libc::CREATE_NEW,
(io::Append, io::Read) => {
dwDesiredAccess |= libc::FILE_APPEND_DATA;
libc::OPEN_EXISTING
}
(io::Append, _) => {
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;
let handle = as_utf16_p(path.as_str().unwrap(), |buf| unsafe {
libc::CreateFileW(buf,
dwDesiredAccess,
dwShareMode,
ptr::mut_null(),
dwCreationDisposition,
dwFlagsAndAttributes,
ptr::mut_null())
});
if handle == libc::INVALID_HANDLE_VALUE as libc::HANDLE {
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))
}
}
}
pub fn mkdir(p: &CString, _mode: io::FilePermission) -> IoResult<()> {
super::mkerr_winbool(unsafe {
// FIXME: turn mode into something useful? #2623
as_utf16_p(p.as_str().unwrap(), |buf| {
libc::CreateDirectoryW(buf, ptr::mut_null())
})
})
}
pub fn readdir(p: &CString) -> IoResult<Vec<Path>> {
use rt::global_heap::malloc_raw;
fn prune(root: &CString, dirs: Vec<Path>) -> Vec<Path> {
let root = unsafe { CString::new(root.with_ref(|p| p), false) };
let root = Path::new(root);
dirs.move_iter().filter(|path| {
path.as_vec() != bytes!(".") && path.as_vec() != bytes!("..")
}).map(|path| root.join(path)).collect()
}
extern {
fn rust_list_dir_wfd_size() -> libc::size_t;
fn rust_list_dir_wfd_fp_buf(wfd: *libc::c_void) -> *u16;
}
let star = Path::new(unsafe {
CString::new(p.with_ref(|p| p), false)
}).join("*");
as_utf16_p(star.as_str().unwrap(), |path_ptr| unsafe {
let wfd_ptr = malloc_raw(rust_list_dir_wfd_size() as uint);
let find_handle = libc::FindFirstFileW(path_ptr, wfd_ptr as libc::HANDLE);
if find_handle as libc::c_int != libc::INVALID_HANDLE_VALUE {
let mut paths = vec!();
let mut more_files = 1 as libc::c_int;
while more_files != 0 {
let fp_buf = rust_list_dir_wfd_fp_buf(wfd_ptr as *c_void);
if fp_buf as uint == 0 {
fail!("os::list_dir() failure: got null ptr from wfd");
} else {
let fp_vec = slice::from_buf(fp_buf, libc::wcslen(fp_buf) as uint);
let fp_trimmed = str::truncate_utf16_at_nul(fp_vec);
let fp_str = str::from_utf16(fp_trimmed)
.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())
}
})
}
pub fn unlink(p: &CString) -> IoResult<()> {
super::mkerr_winbool(unsafe {
as_utf16_p(p.as_str().unwrap(), |buf| {
libc::DeleteFileW(buf)
})
})
}
pub fn rename(old: &CString, new: &CString) -> IoResult<()> {
super::mkerr_winbool(unsafe {
as_utf16_p(old.as_str().unwrap(), |old| {
as_utf16_p(new.as_str().unwrap(), |new| {
libc::MoveFileExW(old, new, libc::MOVEFILE_REPLACE_EXISTING)
})
})
})
}
pub fn chmod(p: &CString, mode: io::FilePermission) -> IoResult<()> {
super::mkerr_libc(as_utf16_p(p.as_str().unwrap(), |p| unsafe {
libc::wchmod(p, mode as libc::c_int)
}))
}
pub fn rmdir(p: &CString) -> IoResult<()> {
super::mkerr_libc(as_utf16_p(p.as_str().unwrap(), |p| unsafe {
libc::wrmdir(p)
}))
}
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(())
}
pub fn readlink(p: &CString) -> IoResult<Path> {
// FIXME: I have a feeling that this reads intermediate symlinks as well.
let handle = unsafe {
as_utf16_p(p.as_str().unwrap(), |p| {
libc::CreateFileW(p,
libc::GENERIC_READ,
libc::FILE_SHARE_READ,
ptr::mut_null(),
libc::OPEN_EXISTING,
libc::FILE_ATTRIBUTE_NORMAL,
ptr::mut_null())
})
};
if handle as int == libc::INVALID_HANDLE_VALUE as int {
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 {
libc::GetFinalPathNameByHandleW(handle,
buf as *u16,
sz - 1,
libc::VOLUME_NAME_DOS)
});
let ret = match ret {
Some(ref s) if s.starts_with(r"\\?\") => Ok(Path::new(s.slice_from(4))),
Some(s) => Ok(Path::new(s)),
None => Err(super::last_error()),
};
assert!(unsafe { libc::CloseHandle(handle) } != 0);
return ret;
}
pub fn symlink(src: &CString, dst: &CString) -> IoResult<()> {
super::mkerr_winbool(as_utf16_p(src.as_str().unwrap(), |src| {
as_utf16_p(dst.as_str().unwrap(), |dst| {
unsafe { libc::CreateSymbolicLinkW(dst, src, 0) }
}) as libc::BOOL
}))
}
pub fn link(src: &CString, dst: &CString) -> IoResult<()> {
super::mkerr_winbool(as_utf16_p(src.as_str().unwrap(), |src| {
as_utf16_p(dst.as_str().unwrap(), |dst| {
unsafe { libc::CreateHardLinkW(dst, src, ptr::mut_null()) }
})
}))
}
fn mkstat(stat: &libc::stat, path: &CString) -> io::FileStat {
let path = unsafe { CString::new(path.with_ref(|p| p), false) };
let kind = match (stat.st_mode as c_int) & libc::S_IFMT {
libc::S_IFREG => io::TypeFile,
libc::S_IFDIR => io::TypeDirectory,
libc::S_IFIFO => io::TypeNamedPipe,
libc::S_IFBLK => io::TypeBlockSpecial,
libc::S_IFLNK => io::TypeSymlink,
_ => io::TypeUnknown,
};
io::FileStat {
path: Path::new(path),
size: stat.st_size as u64,
kind: kind,
perm: (stat.st_mode) as io::FilePermission & io::AllPermissions,
created: stat.st_ctime as u64,
modified: stat.st_mtime as u64,
accessed: stat.st_atime as u64,
unstable: io::UnstableFileStat {
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,
}
}
}
pub fn stat(p: &CString) -> IoResult<io::FileStat> {
let mut stat: libc::stat = unsafe { mem::uninit() };
as_utf16_p(p.as_str().unwrap(), |up| {
match unsafe { libc::wstat(up, &mut stat) } {
0 => Ok(mkstat(&stat, p)),
_ => Err(super::last_error()),
}
})
}
pub fn lstat(_p: &CString) -> IoResult<io::FileStat> {
// FIXME: implementation is missing
Err(super::unimpl())
}
pub fn utime(p: &CString, atime: u64, mtime: u64) -> IoResult<()> {
let buf = libc::utimbuf {
actime: (atime / 1000) as libc::time64_t,
modtime: (mtime / 1000) as libc::time64_t,
};
super::mkerr_libc(as_utf16_p(p.as_str().unwrap(), |p| unsafe {
libc::wutime(p, &buf)
}))
}