]>
Commit | Line | Data |
---|---|---|
85aaf69f SL |
1 | // Copyright 2013-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. | |
4 | // | |
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. | |
10 | ||
11 | use core::prelude::*; | |
12 | use io::prelude::*; | |
13 | use os::unix::prelude::*; | |
14 | ||
c34b1796 | 15 | use ffi::{CString, CStr, OsString, OsStr}; |
d9579d0f | 16 | use fmt; |
c1a9b12d | 17 | use io::{self, Error, ErrorKind, SeekFrom}; |
c34b1796 | 18 | use libc::{self, c_int, size_t, off_t, c_char, mode_t}; |
85aaf69f SL |
19 | use mem; |
20 | use path::{Path, PathBuf}; | |
21 | use ptr; | |
c34b1796 | 22 | use sync::Arc; |
85aaf69f | 23 | use sys::fd::FileDesc; |
d9579d0f | 24 | use sys::platform::raw; |
85aaf69f | 25 | use sys::{c, cvt, cvt_r}; |
d9579d0f | 26 | use sys_common::{AsInner, FromInner}; |
85aaf69f SL |
27 | use vec::Vec; |
28 | ||
29 | pub struct File(FileDesc); | |
30 | ||
31 | pub struct FileAttr { | |
d9579d0f | 32 | stat: raw::stat, |
85aaf69f SL |
33 | } |
34 | ||
35 | pub struct ReadDir { | |
c34b1796 AL |
36 | dirp: Dir, |
37 | root: Arc<PathBuf>, | |
85aaf69f SL |
38 | } |
39 | ||
c34b1796 AL |
40 | struct Dir(*mut libc::DIR); |
41 | ||
42 | unsafe impl Send for Dir {} | |
43 | unsafe impl Sync for Dir {} | |
44 | ||
85aaf69f | 45 | pub struct DirEntry { |
c34b1796 AL |
46 | buf: Vec<u8>, // actually *mut libc::dirent_t |
47 | root: Arc<PathBuf>, | |
85aaf69f SL |
48 | } |
49 | ||
50 | #[derive(Clone)] | |
51 | pub struct OpenOptions { | |
52 | flags: c_int, | |
53 | read: bool, | |
54 | write: bool, | |
55 | mode: mode_t, | |
56 | } | |
57 | ||
58 | #[derive(Clone, PartialEq, Eq, Debug)] | |
59 | pub struct FilePermissions { mode: mode_t } | |
60 | ||
d9579d0f AL |
61 | #[derive(Copy, Clone, PartialEq, Eq, Hash)] |
62 | pub struct FileType { mode: mode_t } | |
63 | ||
64 | pub struct DirBuilder { mode: mode_t } | |
65 | ||
85aaf69f | 66 | impl FileAttr { |
85aaf69f SL |
67 | pub fn size(&self) -> u64 { self.stat.st_size as u64 } |
68 | pub fn perm(&self) -> FilePermissions { | |
69 | FilePermissions { mode: (self.stat.st_mode as mode_t) & 0o777 } | |
70 | } | |
71 | ||
d9579d0f AL |
72 | pub fn file_type(&self) -> FileType { |
73 | FileType { mode: self.stat.st_mode as mode_t } | |
85aaf69f | 74 | } |
d9579d0f | 75 | } |
85aaf69f | 76 | |
d9579d0f AL |
77 | impl AsInner<raw::stat> for FileAttr { |
78 | fn as_inner(&self) -> &raw::stat { &self.stat } | |
79 | } | |
80 | ||
81 | /// OS-specific extension methods for `fs::Metadata` | |
82 | #[stable(feature = "metadata_ext", since = "1.1.0")] | |
83 | pub trait MetadataExt { | |
84 | /// Gain a reference to the underlying `stat` structure which contains the | |
85 | /// raw information returned by the OS. | |
86 | /// | |
87 | /// The contents of the returned `stat` are **not** consistent across Unix | |
88 | /// platforms. The `os::unix::fs::MetadataExt` trait contains the cross-Unix | |
89 | /// abstractions contained within the raw stat. | |
90 | #[stable(feature = "metadata_ext", since = "1.1.0")] | |
91 | fn as_raw_stat(&self) -> &raw::stat; | |
92 | } | |
93 | ||
94 | #[stable(feature = "metadata_ext", since = "1.1.0")] | |
95 | impl MetadataExt for ::fs::Metadata { | |
96 | fn as_raw_stat(&self) -> &raw::stat { &self.as_inner().stat } | |
85aaf69f SL |
97 | } |
98 | ||
99 | impl FilePermissions { | |
100 | pub fn readonly(&self) -> bool { self.mode & 0o222 == 0 } | |
101 | pub fn set_readonly(&mut self, readonly: bool) { | |
102 | if readonly { | |
103 | self.mode &= !0o222; | |
104 | } else { | |
105 | self.mode |= 0o222; | |
106 | } | |
107 | } | |
d9579d0f AL |
108 | pub fn mode(&self) -> raw::mode_t { self.mode } |
109 | } | |
110 | ||
111 | impl FileType { | |
112 | pub fn is_dir(&self) -> bool { self.is(libc::S_IFDIR) } | |
113 | pub fn is_file(&self) -> bool { self.is(libc::S_IFREG) } | |
114 | pub fn is_symlink(&self) -> bool { self.is(libc::S_IFLNK) } | |
115 | ||
c1a9b12d | 116 | pub fn is(&self, mode: mode_t) -> bool { self.mode & libc::S_IFMT == mode } |
85aaf69f SL |
117 | } |
118 | ||
d9579d0f AL |
119 | impl FromInner<raw::mode_t> for FilePermissions { |
120 | fn from_inner(mode: raw::mode_t) -> FilePermissions { | |
85aaf69f SL |
121 | FilePermissions { mode: mode as mode_t } |
122 | } | |
123 | } | |
124 | ||
125 | impl Iterator for ReadDir { | |
126 | type Item = io::Result<DirEntry>; | |
127 | ||
128 | fn next(&mut self) -> Option<io::Result<DirEntry>> { | |
129 | extern { | |
130 | fn rust_dirent_t_size() -> c_int; | |
131 | } | |
132 | ||
133 | let mut buf: Vec<u8> = Vec::with_capacity(unsafe { | |
134 | rust_dirent_t_size() as usize | |
135 | }); | |
136 | let ptr = buf.as_mut_ptr() as *mut libc::dirent_t; | |
137 | ||
138 | let mut entry_ptr = ptr::null_mut(); | |
139 | loop { | |
c34b1796 | 140 | if unsafe { libc::readdir_r(self.dirp.0, ptr, &mut entry_ptr) != 0 } { |
85aaf69f SL |
141 | return Some(Err(Error::last_os_error())) |
142 | } | |
143 | if entry_ptr.is_null() { | |
144 | return None | |
145 | } | |
146 | ||
147 | let entry = DirEntry { | |
148 | buf: buf, | |
85aaf69f SL |
149 | root: self.root.clone() |
150 | }; | |
151 | if entry.name_bytes() == b"." || entry.name_bytes() == b".." { | |
152 | buf = entry.buf; | |
153 | } else { | |
154 | return Some(Ok(entry)) | |
155 | } | |
156 | } | |
157 | } | |
158 | } | |
159 | ||
c34b1796 | 160 | impl Drop for Dir { |
85aaf69f | 161 | fn drop(&mut self) { |
c34b1796 | 162 | let r = unsafe { libc::closedir(self.0) }; |
85aaf69f SL |
163 | debug_assert_eq!(r, 0); |
164 | } | |
165 | } | |
166 | ||
167 | impl DirEntry { | |
168 | pub fn path(&self) -> PathBuf { | |
169 | self.root.join(<OsStr as OsStrExt>::from_bytes(self.name_bytes())) | |
170 | } | |
171 | ||
d9579d0f AL |
172 | pub fn file_name(&self) -> OsString { |
173 | OsStr::from_bytes(self.name_bytes()).to_os_string() | |
174 | } | |
175 | ||
176 | pub fn metadata(&self) -> io::Result<FileAttr> { | |
177 | lstat(&self.path()) | |
178 | } | |
179 | ||
180 | pub fn file_type(&self) -> io::Result<FileType> { | |
181 | extern { | |
182 | fn rust_dir_get_mode(ptr: *mut libc::dirent_t) -> c_int; | |
183 | } | |
184 | unsafe { | |
185 | match rust_dir_get_mode(self.dirent()) { | |
186 | -1 => lstat(&self.path()).map(|m| m.file_type()), | |
187 | n => Ok(FileType { mode: n as mode_t }), | |
188 | } | |
189 | } | |
190 | } | |
191 | ||
192 | pub fn ino(&self) -> raw::ino_t { | |
193 | extern { | |
194 | fn rust_dir_get_ino(ptr: *mut libc::dirent_t) -> raw::ino_t; | |
195 | } | |
196 | unsafe { rust_dir_get_ino(self.dirent()) } | |
197 | } | |
198 | ||
85aaf69f SL |
199 | fn name_bytes(&self) -> &[u8] { |
200 | extern { | |
201 | fn rust_list_dir_val(ptr: *mut libc::dirent_t) -> *const c_char; | |
202 | } | |
203 | unsafe { | |
c34b1796 | 204 | CStr::from_ptr(rust_list_dir_val(self.dirent())).to_bytes() |
85aaf69f SL |
205 | } |
206 | } | |
c34b1796 AL |
207 | |
208 | fn dirent(&self) -> *mut libc::dirent_t { | |
209 | self.buf.as_ptr() as *mut _ | |
210 | } | |
85aaf69f SL |
211 | } |
212 | ||
213 | impl OpenOptions { | |
214 | pub fn new() -> OpenOptions { | |
215 | OpenOptions { | |
216 | flags: 0, | |
217 | read: false, | |
218 | write: false, | |
219 | mode: 0o666, | |
220 | } | |
221 | } | |
222 | ||
223 | pub fn read(&mut self, read: bool) { | |
224 | self.read = read; | |
225 | } | |
226 | ||
227 | pub fn write(&mut self, write: bool) { | |
228 | self.write = write; | |
229 | } | |
230 | ||
231 | pub fn append(&mut self, append: bool) { | |
232 | self.flag(libc::O_APPEND, append); | |
233 | } | |
234 | ||
235 | pub fn truncate(&mut self, truncate: bool) { | |
236 | self.flag(libc::O_TRUNC, truncate); | |
237 | } | |
238 | ||
239 | pub fn create(&mut self, create: bool) { | |
240 | self.flag(libc::O_CREAT, create); | |
241 | } | |
242 | ||
d9579d0f | 243 | pub fn mode(&mut self, mode: raw::mode_t) { |
85aaf69f SL |
244 | self.mode = mode as mode_t; |
245 | } | |
246 | ||
247 | fn flag(&mut self, bit: c_int, on: bool) { | |
248 | if on { | |
249 | self.flags |= bit; | |
250 | } else { | |
251 | self.flags &= !bit; | |
252 | } | |
253 | } | |
254 | } | |
255 | ||
256 | impl File { | |
257 | pub fn open(path: &Path, opts: &OpenOptions) -> io::Result<File> { | |
9346a6ac AL |
258 | let path = try!(cstr(path)); |
259 | File::open_c(&path, opts) | |
260 | } | |
261 | ||
262 | pub fn open_c(path: &CStr, opts: &OpenOptions) -> io::Result<File> { | |
85aaf69f SL |
263 | let flags = opts.flags | match (opts.read, opts.write) { |
264 | (true, true) => libc::O_RDWR, | |
265 | (false, true) => libc::O_WRONLY, | |
266 | (true, false) | | |
267 | (false, false) => libc::O_RDONLY, | |
268 | }; | |
85aaf69f SL |
269 | let fd = try!(cvt_r(|| unsafe { |
270 | libc::open(path.as_ptr(), flags, opts.mode) | |
271 | })); | |
9346a6ac AL |
272 | let fd = FileDesc::new(fd); |
273 | fd.set_cloexec(); | |
274 | Ok(File(fd)) | |
85aaf69f SL |
275 | } |
276 | ||
277 | pub fn file_attr(&self) -> io::Result<FileAttr> { | |
d9579d0f AL |
278 | let mut stat: raw::stat = unsafe { mem::zeroed() }; |
279 | try!(cvt(unsafe { | |
280 | libc::fstat(self.0.raw(), &mut stat as *mut _ as *mut _) | |
281 | })); | |
85aaf69f SL |
282 | Ok(FileAttr { stat: stat }) |
283 | } | |
284 | ||
285 | pub fn fsync(&self) -> io::Result<()> { | |
286 | try!(cvt_r(|| unsafe { libc::fsync(self.0.raw()) })); | |
287 | Ok(()) | |
288 | } | |
289 | ||
290 | pub fn datasync(&self) -> io::Result<()> { | |
291 | try!(cvt_r(|| unsafe { os_datasync(self.0.raw()) })); | |
292 | return Ok(()); | |
293 | ||
294 | #[cfg(any(target_os = "macos", target_os = "ios"))] | |
295 | unsafe fn os_datasync(fd: c_int) -> c_int { | |
296 | libc::fcntl(fd, libc::F_FULLFSYNC) | |
297 | } | |
298 | #[cfg(target_os = "linux")] | |
299 | unsafe fn os_datasync(fd: c_int) -> c_int { libc::fdatasync(fd) } | |
300 | #[cfg(not(any(target_os = "macos", | |
301 | target_os = "ios", | |
302 | target_os = "linux")))] | |
303 | unsafe fn os_datasync(fd: c_int) -> c_int { libc::fsync(fd) } | |
304 | } | |
305 | ||
306 | pub fn truncate(&self, size: u64) -> io::Result<()> { | |
307 | try!(cvt_r(|| unsafe { | |
308 | libc::ftruncate(self.0.raw(), size as libc::off_t) | |
309 | })); | |
310 | Ok(()) | |
311 | } | |
312 | ||
313 | pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> { | |
314 | self.0.read(buf) | |
315 | } | |
316 | ||
317 | pub fn write(&self, buf: &[u8]) -> io::Result<usize> { | |
318 | self.0.write(buf) | |
319 | } | |
320 | ||
321 | pub fn flush(&self) -> io::Result<()> { Ok(()) } | |
322 | ||
323 | pub fn seek(&self, pos: SeekFrom) -> io::Result<u64> { | |
324 | let (whence, pos) = match pos { | |
325 | SeekFrom::Start(off) => (libc::SEEK_SET, off as off_t), | |
326 | SeekFrom::End(off) => (libc::SEEK_END, off as off_t), | |
327 | SeekFrom::Current(off) => (libc::SEEK_CUR, off as off_t), | |
328 | }; | |
329 | let n = try!(cvt(unsafe { libc::lseek(self.0.raw(), pos, whence) })); | |
330 | Ok(n as u64) | |
331 | } | |
332 | ||
333 | pub fn fd(&self) -> &FileDesc { &self.0 } | |
c1a9b12d SL |
334 | |
335 | pub fn into_fd(self) -> FileDesc { self.0 } | |
85aaf69f SL |
336 | } |
337 | ||
d9579d0f AL |
338 | impl DirBuilder { |
339 | pub fn new() -> DirBuilder { | |
340 | DirBuilder { mode: 0o777 } | |
341 | } | |
342 | ||
343 | pub fn mkdir(&self, p: &Path) -> io::Result<()> { | |
344 | let p = try!(cstr(p)); | |
345 | try!(cvt(unsafe { libc::mkdir(p.as_ptr(), self.mode) })); | |
346 | Ok(()) | |
347 | } | |
348 | ||
349 | pub fn set_mode(&mut self, mode: mode_t) { | |
350 | self.mode = mode; | |
351 | } | |
352 | } | |
353 | ||
85aaf69f | 354 | fn cstr(path: &Path) -> io::Result<CString> { |
c34b1796 AL |
355 | path.as_os_str().to_cstring().ok_or( |
356 | io::Error::new(io::ErrorKind::InvalidInput, "path contained a null")) | |
357 | } | |
358 | ||
359 | impl FromInner<c_int> for File { | |
360 | fn from_inner(fd: c_int) -> File { | |
361 | File(FileDesc::new(fd)) | |
362 | } | |
85aaf69f SL |
363 | } |
364 | ||
d9579d0f AL |
365 | impl fmt::Debug for File { |
366 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
367 | #[cfg(target_os = "linux")] | |
368 | fn get_path(fd: c_int) -> Option<PathBuf> { | |
369 | use string::ToString; | |
370 | let mut p = PathBuf::from("/proc/self/fd"); | |
371 | p.push(&fd.to_string()); | |
372 | readlink(&p).ok() | |
373 | } | |
374 | ||
c1a9b12d SL |
375 | #[cfg(target_os = "macos")] |
376 | fn get_path(fd: c_int) -> Option<PathBuf> { | |
377 | let mut buf = vec![0;libc::PATH_MAX as usize]; | |
378 | let n = unsafe { libc::fcntl(fd, libc::F_GETPATH, buf.as_ptr()) }; | |
379 | if n == -1 { | |
380 | return None; | |
381 | } | |
382 | let l = buf.iter().position(|&c| c == 0).unwrap(); | |
383 | buf.truncate(l as usize); | |
384 | Some(PathBuf::from(OsString::from_vec(buf))) | |
385 | } | |
386 | ||
387 | #[cfg(not(any(target_os = "linux", target_os = "macos")))] | |
d9579d0f AL |
388 | fn get_path(_fd: c_int) -> Option<PathBuf> { |
389 | // FIXME(#24570): implement this for other Unix platforms | |
390 | None | |
391 | } | |
392 | ||
c1a9b12d | 393 | #[cfg(any(target_os = "linux", target_os = "macos"))] |
d9579d0f AL |
394 | fn get_mode(fd: c_int) -> Option<(bool, bool)> { |
395 | let mode = unsafe { libc::fcntl(fd, libc::F_GETFL) }; | |
396 | if mode == -1 { | |
397 | return None; | |
398 | } | |
399 | match mode & libc::O_ACCMODE { | |
400 | libc::O_RDONLY => Some((true, false)), | |
401 | libc::O_RDWR => Some((true, true)), | |
402 | libc::O_WRONLY => Some((false, true)), | |
403 | _ => None | |
404 | } | |
405 | } | |
406 | ||
c1a9b12d | 407 | #[cfg(not(any(target_os = "linux", target_os = "macos")))] |
d9579d0f AL |
408 | fn get_mode(_fd: c_int) -> Option<(bool, bool)> { |
409 | // FIXME(#24570): implement this for other Unix platforms | |
410 | None | |
411 | } | |
412 | ||
413 | let fd = self.0.raw(); | |
62682a34 SL |
414 | let mut b = f.debug_struct("File"); |
415 | b.field("fd", &fd); | |
d9579d0f | 416 | if let Some(path) = get_path(fd) { |
62682a34 | 417 | b.field("path", &path); |
d9579d0f AL |
418 | } |
419 | if let Some((read, write)) = get_mode(fd) { | |
62682a34 | 420 | b.field("read", &read).field("write", &write); |
d9579d0f AL |
421 | } |
422 | b.finish() | |
423 | } | |
85aaf69f SL |
424 | } |
425 | ||
426 | pub fn readdir(p: &Path) -> io::Result<ReadDir> { | |
c34b1796 | 427 | let root = Arc::new(p.to_path_buf()); |
85aaf69f SL |
428 | let p = try!(cstr(p)); |
429 | unsafe { | |
430 | let ptr = libc::opendir(p.as_ptr()); | |
431 | if ptr.is_null() { | |
432 | Err(Error::last_os_error()) | |
433 | } else { | |
c34b1796 | 434 | Ok(ReadDir { dirp: Dir(ptr), root: root }) |
85aaf69f SL |
435 | } |
436 | } | |
437 | } | |
438 | ||
439 | pub fn unlink(p: &Path) -> io::Result<()> { | |
440 | let p = try!(cstr(p)); | |
441 | try!(cvt(unsafe { libc::unlink(p.as_ptr()) })); | |
442 | Ok(()) | |
443 | } | |
444 | ||
445 | pub fn rename(old: &Path, new: &Path) -> io::Result<()> { | |
446 | let old = try!(cstr(old)); | |
447 | let new = try!(cstr(new)); | |
448 | try!(cvt(unsafe { libc::rename(old.as_ptr(), new.as_ptr()) })); | |
449 | Ok(()) | |
450 | } | |
451 | ||
452 | pub fn set_perm(p: &Path, perm: FilePermissions) -> io::Result<()> { | |
453 | let p = try!(cstr(p)); | |
454 | try!(cvt_r(|| unsafe { libc::chmod(p.as_ptr(), perm.mode) })); | |
455 | Ok(()) | |
456 | } | |
457 | ||
458 | pub fn rmdir(p: &Path) -> io::Result<()> { | |
459 | let p = try!(cstr(p)); | |
460 | try!(cvt(unsafe { libc::rmdir(p.as_ptr()) })); | |
461 | Ok(()) | |
462 | } | |
463 | ||
85aaf69f SL |
464 | pub fn readlink(p: &Path) -> io::Result<PathBuf> { |
465 | let c_path = try!(cstr(p)); | |
466 | let p = c_path.as_ptr(); | |
467 | let mut len = unsafe { libc::pathconf(p as *mut _, libc::_PC_NAME_MAX) }; | |
468 | if len < 0 { | |
469 | len = 1024; // FIXME: read PATH_MAX from C ffi? | |
470 | } | |
471 | let mut buf: Vec<u8> = Vec::with_capacity(len as usize); | |
472 | unsafe { | |
473 | let n = try!(cvt({ | |
474 | libc::readlink(p, buf.as_ptr() as *mut c_char, len as size_t) | |
475 | })); | |
476 | buf.set_len(n as usize); | |
477 | } | |
c34b1796 | 478 | Ok(PathBuf::from(OsString::from_vec(buf))) |
85aaf69f SL |
479 | } |
480 | ||
481 | pub fn symlink(src: &Path, dst: &Path) -> io::Result<()> { | |
482 | let src = try!(cstr(src)); | |
483 | let dst = try!(cstr(dst)); | |
484 | try!(cvt(unsafe { libc::symlink(src.as_ptr(), dst.as_ptr()) })); | |
485 | Ok(()) | |
486 | } | |
487 | ||
488 | pub fn link(src: &Path, dst: &Path) -> io::Result<()> { | |
489 | let src = try!(cstr(src)); | |
490 | let dst = try!(cstr(dst)); | |
491 | try!(cvt(unsafe { libc::link(src.as_ptr(), dst.as_ptr()) })); | |
492 | Ok(()) | |
493 | } | |
494 | ||
495 | pub fn stat(p: &Path) -> io::Result<FileAttr> { | |
496 | let p = try!(cstr(p)); | |
d9579d0f AL |
497 | let mut stat: raw::stat = unsafe { mem::zeroed() }; |
498 | try!(cvt(unsafe { | |
499 | libc::stat(p.as_ptr(), &mut stat as *mut _ as *mut _) | |
500 | })); | |
85aaf69f SL |
501 | Ok(FileAttr { stat: stat }) |
502 | } | |
503 | ||
504 | pub fn lstat(p: &Path) -> io::Result<FileAttr> { | |
505 | let p = try!(cstr(p)); | |
d9579d0f AL |
506 | let mut stat: raw::stat = unsafe { mem::zeroed() }; |
507 | try!(cvt(unsafe { | |
508 | libc::lstat(p.as_ptr(), &mut stat as *mut _ as *mut _) | |
509 | })); | |
85aaf69f SL |
510 | Ok(FileAttr { stat: stat }) |
511 | } | |
512 | ||
513 | pub fn utimes(p: &Path, atime: u64, mtime: u64) -> io::Result<()> { | |
514 | let p = try!(cstr(p)); | |
515 | let buf = [super::ms_to_timeval(atime), super::ms_to_timeval(mtime)]; | |
516 | try!(cvt(unsafe { c::utimes(p.as_ptr(), buf.as_ptr()) })); | |
517 | Ok(()) | |
518 | } | |
d9579d0f AL |
519 | |
520 | pub fn canonicalize(p: &Path) -> io::Result<PathBuf> { | |
521 | let path = try!(CString::new(p.as_os_str().as_bytes())); | |
522 | let mut buf = vec![0u8; 16 * 1024]; | |
523 | unsafe { | |
524 | let r = c::realpath(path.as_ptr(), buf.as_mut_ptr() as *mut _); | |
525 | if r.is_null() { | |
526 | return Err(io::Error::last_os_error()) | |
527 | } | |
528 | } | |
529 | let p = buf.iter().position(|i| *i == 0).unwrap(); | |
530 | buf.truncate(p); | |
531 | Ok(PathBuf::from(OsString::from_vec(buf))) | |
532 | } | |
c1a9b12d SL |
533 | |
534 | pub fn copy(from: &Path, to: &Path) -> io::Result<u64> { | |
535 | use fs::{File, PathExt, set_permissions}; | |
536 | if !from.is_file() { | |
537 | return Err(Error::new(ErrorKind::InvalidInput, | |
538 | "the source path is not an existing file")) | |
539 | } | |
540 | ||
541 | let mut reader = try!(File::open(from)); | |
542 | let mut writer = try!(File::create(to)); | |
543 | let perm = try!(reader.metadata()).permissions(); | |
544 | ||
545 | let ret = try!(io::copy(&mut reader, &mut writer)); | |
546 | try!(set_permissions(to, perm)); | |
547 | Ok(ret) | |
548 | } |