Skip to content

Commit 9d845e2

Browse files
yuriksalexcrichton
authored andcommitted
---
yaml --- r: 114046 b: refs/heads/master c: 8c55fcd h: refs/heads/master v: v3
1 parent 01b10db commit 9d845e2

File tree

7 files changed

+56
-23
lines changed

7 files changed

+56
-23
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
refs/heads/master: f096516d2b5e0ac1a634742a11520dff4b59014b
2+
refs/heads/master: 8c55fcd1f2ef3674d4bda4e38e2e7cacdd7cd5b8
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: ec0258a381b88b5574e3f8ce72ae553ac3a574b7
55
refs/heads/try: 7c6c492fb2af9a85f21ff952942df3523b22fd17

trunk/src/libnative/io/file_unix.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,14 @@ impl rtio::RtioFileStream for FileDesc {
166166
libc::ftruncate(self.fd(), offset as libc::off_t)
167167
}))
168168
}
169+
170+
fn fstat(&mut self) -> IoResult<io::FileStat> {
171+
let mut stat: libc::stat = unsafe { mem::uninit() };
172+
match retry(|| unsafe { libc::fstat(self.fd(), &mut stat) }) {
173+
0 => Ok(mkstat(&stat)),
174+
_ => Err(super::last_error()),
175+
}
176+
}
169177
}
170178

171179
impl rtio::RtioPipe for FileDesc {
@@ -317,6 +325,10 @@ impl rtio::RtioFileStream for CFile {
317325
fn truncate(&mut self, offset: i64) -> Result<(), IoError> {
318326
self.flush().and_then(|()| self.fd.truncate(offset))
319327
}
328+
329+
fn fstat(&mut self) -> IoResult<io::FileStat> {
330+
self.flush().and_then(|()| self.fd.fstat())
331+
}
320332
}
321333

322334
impl Drop for CFile {
@@ -455,9 +467,7 @@ pub fn link(src: &CString, dst: &CString) -> IoResult<()> {
455467
}))
456468
}
457469

458-
fn mkstat(stat: &libc::stat, path: &CString) -> io::FileStat {
459-
let path = unsafe { CString::new(path.with_ref(|p| p), false) };
460-
470+
fn mkstat(stat: &libc::stat) -> io::FileStat {
461471
// FileStat times are in milliseconds
462472
fn mktime(secs: u64, nsecs: u64) -> u64 { secs * 1000 + nsecs / 1000000 }
463473

@@ -481,7 +491,6 @@ fn mkstat(stat: &libc::stat, path: &CString) -> io::FileStat {
481491
fn gen(_stat: &libc::stat) -> u64 { 0 }
482492

483493
io::FileStat {
484-
path: Path::new(path),
485494
size: stat.st_size as u64,
486495
kind: kind,
487496
perm: unsafe {
@@ -508,15 +517,15 @@ fn mkstat(stat: &libc::stat, path: &CString) -> io::FileStat {
508517
pub fn stat(p: &CString) -> IoResult<io::FileStat> {
509518
let mut stat: libc::stat = unsafe { mem::uninit() };
510519
match retry(|| unsafe { libc::stat(p.with_ref(|p| p), &mut stat) }) {
511-
0 => Ok(mkstat(&stat, p)),
520+
0 => Ok(mkstat(&stat)),
512521
_ => Err(super::last_error()),
513522
}
514523
}
515524

516525
pub fn lstat(p: &CString) -> IoResult<io::FileStat> {
517526
let mut stat: libc::stat = unsafe { mem::uninit() };
518527
match retry(|| unsafe { libc::lstat(p.with_ref(|p| p), &mut stat) }) {
519-
0 => Ok(mkstat(&stat, p)),
528+
0 => Ok(mkstat(&stat)),
520529
_ => Err(super::last_error()),
521530
}
522531
}

trunk/src/libnative/io/file_win32.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,14 @@ impl rtio::RtioFileStream for FileDesc {
197197
let _ = self.seek(orig_pos as i64, io::SeekSet);
198198
return ret;
199199
}
200+
201+
fn fstat(&mut self) -> IoResult<io::FileStat> {
202+
let mut stat: libc::stat = unsafe { mem::uninit() };
203+
match unsafe { libc::fstat(self.fd(), &mut stat) } {
204+
0 => Ok(mkstat(&stat)),
205+
_ => Err(super::last_error()),
206+
}
207+
}
200208
}
201209

202210
impl rtio::RtioPipe for FileDesc {
@@ -471,8 +479,7 @@ pub fn link(src: &CString, dst: &CString) -> IoResult<()> {
471479
}))
472480
}
473481

474-
fn mkstat(stat: &libc::stat, path: &CString) -> io::FileStat {
475-
let path = unsafe { CString::new(path.with_ref(|p| p), false) };
482+
fn mkstat(stat: &libc::stat) -> io::FileStat {
476483
let kind = match (stat.st_mode as c_int) & libc::S_IFMT {
477484
libc::S_IFREG => io::TypeFile,
478485
libc::S_IFDIR => io::TypeDirectory,
@@ -483,7 +490,6 @@ fn mkstat(stat: &libc::stat, path: &CString) -> io::FileStat {
483490
};
484491

485492
io::FileStat {
486-
path: Path::new(path),
487493
size: stat.st_size as u64,
488494
kind: kind,
489495
perm: unsafe {
@@ -511,7 +517,7 @@ pub fn stat(p: &CString) -> IoResult<io::FileStat> {
511517
let mut stat: libc::stat = unsafe { mem::uninit() };
512518
as_utf16_p(p.as_str().unwrap(), |up| {
513519
match unsafe { libc::wstat(up, &mut stat) } {
514-
0 => Ok(mkstat(&stat, p)),
520+
0 => Ok(mkstat(&stat)),
515521
_ => Err(super::last_error()),
516522
}
517523
})

trunk/src/librustuv/file.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ impl FsRequest {
7070
}).map(|req| req.mkstat())
7171
}
7272

73+
pub fn fstat(loop_: &Loop, fd: c_int) -> Result<FileStat, UvError> {
74+
execute(|req, cb| unsafe {
75+
uvll::uv_fs_fstat(loop_.handle, req, fd, cb)
76+
}).map(|req| req.mkstat())
77+
}
78+
7379
pub fn write(loop_: &Loop, fd: c_int, buf: &[u8], offset: i64)
7480
-> Result<(), UvError>
7581
{
@@ -262,8 +268,6 @@ impl FsRequest {
262268
}
263269

264270
pub fn mkstat(&self) -> FileStat {
265-
let path = unsafe { uvll::get_path_from_fs_req(self.req) };
266-
let path = unsafe { Path::new(CString::new(path, false)) };
267271
let stat = self.get_stat();
268272
fn to_msec(stat: uvll::uv_timespec_t) -> u64 {
269273
// Be sure to cast to u64 first to prevent overflowing if the tv_sec
@@ -279,7 +283,6 @@ impl FsRequest {
279283
_ => io::TypeUnknown,
280284
};
281285
FileStat {
282-
path: path,
283286
size: stat.st_size as u64,
284287
kind: kind,
285288
perm: unsafe {
@@ -463,6 +466,11 @@ impl rtio::RtioFileStream for FileWatcher {
463466
let r = FsRequest::truncate(&self.loop_, self.fd, offset);
464467
r.map_err(uv_error_to_io_error)
465468
}
469+
470+
fn fstat(&mut self) -> Result<FileStat, IoError> {
471+
let _m = self.fire_homing_missile();
472+
FsRequest::fstat(&self.loop_, self.fd).map_err(uv_error_to_io_error)
473+
}
466474
}
467475

468476
#[cfg(test)]
@@ -537,6 +545,10 @@ mod test {
537545
assert!(result.is_ok());
538546
assert_eq!(result.unwrap().size, 5);
539547

548+
let result = FsRequest::fstat(l(), file.fd);
549+
assert!(result.is_ok());
550+
assert_eq!(result.unwrap().size, 5);
551+
540552
fn free<T>(_: T) {}
541553
free(file);
542554

trunk/src/libstd/io/fs.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,11 @@ impl File {
214214
pub fn eof(&self) -> bool {
215215
self.last_nread == 0
216216
}
217+
218+
/// Queries information about the underlying file.
219+
pub fn stat(&mut self) -> IoResult<FileStat> {
220+
self.fd.fstat()
221+
}
217222
}
218223

219224
/// Unlink a file from the underlying filesystem.
@@ -887,9 +892,12 @@ mod test {
887892
let tmpdir = tmpdir();
888893
let filename = &tmpdir.join("file_stat_correct_on_is_file.txt");
889894
{
890-
let mut fs = File::open_mode(filename, Open, ReadWrite);
895+
let mut fs = check!(File::open_mode(filename, Open, ReadWrite));
891896
let msg = "hw";
892897
fs.write(msg.as_bytes()).unwrap();
898+
899+
let fstat_res = check!(fs.stat());
900+
assert_eq!(fstat_res.kind, io::TypeFile);
893901
}
894902
let stat_res_fn = check!(stat(filename));
895903
assert_eq!(stat_res_fn.kind, io::TypeFile);
@@ -1228,23 +1236,23 @@ mod test {
12281236
check!(file.fsync());
12291237

12301238
// Do some simple things with truncation
1231-
assert_eq!(check!(stat(&path)).size, 3);
1239+
assert_eq!(check!(file.stat()).size, 3);
12321240
check!(file.truncate(10));
1233-
assert_eq!(check!(stat(&path)).size, 10);
1241+
assert_eq!(check!(file.stat()).size, 10);
12341242
check!(file.write(bytes!("bar")));
12351243
check!(file.fsync());
1236-
assert_eq!(check!(stat(&path)).size, 10);
1244+
assert_eq!(check!(file.stat()).size, 10);
12371245
assert_eq!(check!(File::open(&path).read_to_end()),
12381246
(Vec::from_slice(bytes!("foobar", 0, 0, 0, 0))));
12391247

12401248
// Truncate to a smaller length, don't seek, and then write something.
12411249
// Ensure that the intermediate zeroes are all filled in (we're seeked
12421250
// past the end of the file).
12431251
check!(file.truncate(2));
1244-
assert_eq!(check!(stat(&path)).size, 2);
1252+
assert_eq!(check!(file.stat()).size, 2);
12451253
check!(file.write(bytes!("wut")));
12461254
check!(file.fsync());
1247-
assert_eq!(check!(stat(&path)).size, 9);
1255+
assert_eq!(check!(file.stat()).size, 9);
12481256
assert_eq!(check!(File::open(&path).read_to_end()),
12491257
(Vec::from_slice(bytes!("fo", 0, 0, 0, 0, "wut"))));
12501258
drop(file);

trunk/src/libstd/io/mod.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,6 @@ use ops::{BitOr, BitAnd, Sub};
228228
use option::{Option, Some, None};
229229
use os;
230230
use owned::Box;
231-
use path::Path;
232231
use result::{Ok, Err, Result};
233232
use slice::{Vector, MutableVector, ImmutableVector};
234233
use str::{StrSlice, StrAllocating};
@@ -1516,8 +1515,6 @@ pub enum FileType {
15161515
/// ```
15171516
#[deriving(Hash)]
15181517
pub struct FileStat {
1519-
/// The path that this stat structure is describing
1520-
pub path: Path,
15211518
/// The size of the file, in bytes
15221519
pub size: u64,
15231520
/// The kind of file this path points to (directory, file, pipe, etc.)

trunk/src/libstd/rt/rtio.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ pub trait RtioFileStream {
269269
fn fsync(&mut self) -> IoResult<()>;
270270
fn datasync(&mut self) -> IoResult<()>;
271271
fn truncate(&mut self, offset: i64) -> IoResult<()>;
272+
fn fstat(&mut self) -> IoResult<FileStat>;
272273
}
273274

274275
pub trait RtioProcess {

0 commit comments

Comments
 (0)