about summary refs log tree commit diff
path: root/src/libstd/sys
diff options
context:
space:
mode:
Diffstat (limited to 'src/libstd/sys')
-rw-r--r--src/libstd/sys/unix/fs.rs8
-rw-r--r--src/libstd/sys/unix/os.rs41
2 files changed, 32 insertions, 17 deletions
diff --git a/src/libstd/sys/unix/fs.rs b/src/libstd/sys/unix/fs.rs
index 0524851df91..fe946ae0ba4 100644
--- a/src/libstd/sys/unix/fs.rs
+++ b/src/libstd/sys/unix/fs.rs
@@ -205,9 +205,15 @@ impl Iterator for ReadDir {
                 // of the thread safety, on Illumos the readdir(3C) function is safe to use
                 // in threaded applications and it is generally preferred over the
                 // readdir_r(3C) function.
+                super::os::set_errno(0);
                 let entry_ptr = libc::readdir(self.dirp.0);
                 if entry_ptr.is_null() {
-                    return None
+                    // NULL can mean either the end is reached or an error occurred.
+                    // So we had to clear errno beforehand to check for an error now.
+                    return match super::os::errno() {
+                        0 => None,
+                        e => Some(Err(Error::from_raw_os_error(e))),
+                    }
                 }
 
                 let name = (*entry_ptr).d_name.as_ptr();
diff --git a/src/libstd/sys/unix/os.rs b/src/libstd/sys/unix/os.rs
index 21ce6b19ceb..34b36b854f4 100644
--- a/src/libstd/sys/unix/os.rs
+++ b/src/libstd/sys/unix/os.rs
@@ -35,28 +35,37 @@ use vec;
 const TMPBUF_SZ: usize = 128;
 static ENV_LOCK: Mutex = Mutex::new();
 
+
+extern {
+    #[cfg_attr(any(target_os = "linux", target_os = "emscripten"),
+               link_name = "__errno_location")]
+    #[cfg_attr(any(target_os = "bitrig",
+                   target_os = "netbsd",
+                   target_os = "openbsd",
+                   target_os = "android",
+                   target_env = "newlib"),
+               link_name = "__errno")]
+    #[cfg_attr(target_os = "solaris", link_name = "___errno")]
+    #[cfg_attr(any(target_os = "macos",
+                   target_os = "ios",
+                   target_os = "freebsd"),
+               link_name = "__error")]
+    fn errno_location() -> *mut c_int;
+}
+
 /// Returns the platform-specific value of errno
 #[cfg(not(target_os = "dragonfly"))]
 pub fn errno() -> i32 {
-    extern {
-        #[cfg_attr(any(target_os = "linux", target_os = "emscripten"),
-                   link_name = "__errno_location")]
-        #[cfg_attr(any(target_os = "bitrig",
-                       target_os = "netbsd",
-                       target_os = "openbsd",
-                       target_os = "android",
-                       target_env = "newlib"),
-                   link_name = "__errno")]
-        #[cfg_attr(target_os = "solaris", link_name = "___errno")]
-        #[cfg_attr(any(target_os = "macos",
-                       target_os = "ios",
-                       target_os = "freebsd"),
-                   link_name = "__error")]
-        fn errno_location() -> *const c_int;
+    unsafe {
+        (*errno_location()) as i32
     }
+}
 
+/// Sets the platform-specific value of errno
+#[cfg(target_os = "solaris")] // only needed for readdir so far
+pub fn set_errno(e: i32) {
     unsafe {
-        (*errno_location()) as i32
+        *errno_location() = e as c_int
     }
 }