You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@spark.apache.org by rx...@apache.org on 2013/09/21 00:06:34 UTC

[14/50] git commit: Print out more friendly error if listFiles() fails

Print out more friendly error if listFiles() fails

listFiles() could return null if the I/O fails, and this currently results in an ugly NPE which is hard to diagnose.


Project: http://git-wip-us.apache.org/repos/asf/incubator-spark/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-spark/commit/27726079
Tree: http://git-wip-us.apache.org/repos/asf/incubator-spark/tree/27726079
Diff: http://git-wip-us.apache.org/repos/asf/incubator-spark/diff/27726079

Branch: refs/heads/master
Commit: 27726079e4e6931c071de77e91f991cb1b249d02
Parents: 1e15feb
Author: Evan Chan <ev...@ooyala.com>
Authored: Mon Sep 9 12:58:12 2013 -0700
Committer: Evan Chan <ev...@ooyala.com>
Committed: Mon Sep 9 12:58:12 2013 -0700

----------------------------------------------------------------------
 core/src/main/scala/org/apache/spark/util/Utils.scala | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-spark/blob/27726079/core/src/main/scala/org/apache/spark/util/Utils.scala
----------------------------------------------------------------------
diff --git a/core/src/main/scala/org/apache/spark/util/Utils.scala b/core/src/main/scala/org/apache/spark/util/Utils.scala
index 468800b..a95eae9 100644
--- a/core/src/main/scala/org/apache/spark/util/Utils.scala
+++ b/core/src/main/scala/org/apache/spark/util/Utils.scala
@@ -457,12 +457,18 @@ private[spark] object Utils extends Logging {
   def newDaemonFixedThreadPool(nThreads: Int): ThreadPoolExecutor =
     Executors.newFixedThreadPool(nThreads, daemonThreadFactory).asInstanceOf[ThreadPoolExecutor]
 
+  private def listFilesSafely(file: File): Seq[File] = {
+    val files = file.listFiles()
+    if (files == null) throw new IOException("Failed to list files for dir: " + file)
+    files
+  }
+
   /**
    * Delete a file or directory and its contents recursively.
    */
   def deleteRecursively(file: File) {
     if (file.isDirectory) {
-      for (child <- file.listFiles()) {
+      for (child <- listFilesSafely(file)) {
         deleteRecursively(child)
       }
     }