Skip to content

Commit 3200ce5

Browse files
committed
clarify docs for std:io::fs::Path::{is_dir,is_file,exists}; add lstat
Clarifies the interaction of `is_dir`, `is_file` and `exists` with symbolic links. Adds a convenience `lstat` function alongside of `stat`. Removes references to conditions. Closes issue #12583.
1 parent 6c3bdbe commit 3200ce5

File tree

1 file changed

+28
-28
lines changed

1 file changed

+28
-28
lines changed

src/libstd/io/fs.rs

+28-28
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
1+
// Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
22
// file at the top-level directory of this distribution and at
33
// http://rust-lang.org/COPYRIGHT.
44
//
@@ -244,10 +244,6 @@ pub fn unlink(path: &Path) -> IoResult<()> {
244244
/// directory, etc. This function will traverse symlinks to query
245245
/// information about the destination file.
246246
///
247-
/// Returns a fully-filled out stat structure on success, and on failure it
248-
/// will return a dummy stat structure (it is expected that the condition
249-
/// raised is handled as well).
250-
///
251247
/// # Example
252248
///
253249
/// ```rust
@@ -652,45 +648,41 @@ impl Seek for File {
652648
impl path::Path {
653649
/// Get information on the file, directory, etc at this path.
654650
///
655-
/// Consult the `file::stat` documentation for more info.
651+
/// Consult the `fs::stat` documentation for more info.
656652
///
657653
/// This call preserves identical runtime/error semantics with `file::stat`.
658654
pub fn stat(&self) -> IoResult<FileStat> { stat(self) }
659655

660-
/// Boolean value indicator whether the underlying file exists on the local
661-
/// filesystem. This will return true if the path points to either a
662-
/// directory or a file.
656+
/// Get information on the file, directory, etc at this path, not following
657+
/// symlinks.
663658
///
664-
/// # Error
659+
/// Consult the `fs::lstat` documentation for more info.
665660
///
666-
/// Will not raise a condition
661+
/// This call preserves identical runtime/error semantics with `file::lstat`.
662+
pub fn lstat(&self) -> IoResult<FileStat> { lstat(self) }
663+
664+
/// Boolean value indicator whether the underlying file exists on the local
665+
/// filesystem. Returns false in exactly the cases where `fs::stat` fails.
667666
pub fn exists(&self) -> bool {
668667
self.stat().is_ok()
669668
}
670669

671670
/// Whether the underlying implementation (be it a file path, or something
672671
/// else) points at a "regular file" on the FS. Will return false for paths
673672
/// to non-existent locations or directories or other non-regular files
674-
/// (named pipes, etc).
675-
///
676-
/// # Error
677-
///
678-
/// Will not raise a condition
673+
/// (named pipes, etc). Follows links when making this determination.
679674
pub fn is_file(&self) -> bool {
680675
match self.stat() {
681676
Ok(s) => s.kind == io::TypeFile,
682677
Err(..) => false
683678
}
684679
}
685680

686-
/// Whether the underlying implementation (be it a file path,
687-
/// or something else) is pointing at a directory in the underlying FS.
688-
/// Will return false for paths to non-existent locations or if the item is
689-
/// not a directory (eg files, named pipes, links, etc)
690-
///
691-
/// # Error
692-
///
693-
/// Will not raise a condition
681+
/// Whether the underlying implementation (be it a file path, or something
682+
/// else) is pointing at a directory in the underlying FS. Will return
683+
/// false for paths to non-existent locations or if the item is not a
684+
/// directory (eg files, named pipes, etc). Follows links when making this
685+
/// determination.
694686
pub fn is_dir(&self) -> bool {
695687
match self.stat() {
696688
Ok(s) => s.kind == io::TypeDirectory,
@@ -898,17 +890,21 @@ mod test {
898890
let msg = "hw";
899891
fs.write(msg.as_bytes()).unwrap();
900892
}
901-
let stat_res = check!(stat(filename));
902-
assert_eq!(stat_res.kind, io::TypeFile);
893+
let stat_res_fn = check!(stat(filename));
894+
assert_eq!(stat_res_fn.kind, io::TypeFile);
895+
let stat_res_meth = check!(filename.stat());
896+
assert_eq!(stat_res_meth.kind, io::TypeFile);
903897
check!(unlink(filename));
904898
})
905899

906900
iotest!(fn file_test_stat_is_correct_on_is_dir() {
907901
let tmpdir = tmpdir();
908902
let filename = &tmpdir.join("file_stat_correct_on_is_dir");
909903
check!(mkdir(filename, io::UserRWX));
910-
let stat_res = check!(filename.stat());
911-
assert!(stat_res.kind == io::TypeDirectory);
904+
let stat_res_fn = check!(stat(filename));
905+
assert!(stat_res_fn.kind == io::TypeDirectory);
906+
let stat_res_meth = check!(filename.stat());
907+
assert!(stat_res_meth.kind == io::TypeDirectory);
912908
check!(rmdir(filename));
913909
})
914910

@@ -1139,6 +1135,7 @@ mod test {
11391135
check!(symlink(&input, &out));
11401136
if cfg!(not(windows)) {
11411137
assert_eq!(check!(lstat(&out)).kind, io::TypeSymlink);
1138+
assert_eq!(check!(out.lstat()).kind, io::TypeSymlink);
11421139
}
11431140
assert_eq!(check!(stat(&out)).size, check!(stat(&input)).size);
11441141
assert_eq!(check!(File::open(&out).read_to_end()),
@@ -1170,9 +1167,12 @@ mod test {
11701167
check!(link(&input, &out));
11711168
if cfg!(not(windows)) {
11721169
assert_eq!(check!(lstat(&out)).kind, io::TypeFile);
1170+
assert_eq!(check!(out.lstat()).kind, io::TypeFile);
11731171
assert_eq!(check!(stat(&out)).unstable.nlink, 2);
1172+
assert_eq!(check!(out.stat()).unstable.nlink, 2);
11741173
}
11751174
assert_eq!(check!(stat(&out)).size, check!(stat(&input)).size);
1175+
assert_eq!(check!(stat(&out)).size, check!(input.stat()).size);
11761176
assert_eq!(check!(File::open(&out).read_to_end()),
11771177
(Vec::from_slice(bytes!("foobar"))));
11781178

0 commit comments

Comments
 (0)