Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #3738: javalib Files.createTemp*(bogusPath) no longer loops forever #3739

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 14 additions & 2 deletions javalib/src/main/scala/java/nio/file/Files.scala
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,13 @@ object Files {
attrs: Array[FileAttribute[_]]
): Path = {
val p = if (prefix == null) "" else prefix
val temp = FileHelpers.createTempFile(p, "", dir, minLength = false)
val temp = FileHelpers.createTempFile(
p,
"",
dir,
minLength = false,
throwOnError = true
)
if (temp.delete() && temp.mkdir()) {
val tempPath = temp.toPath()
setAttributes(tempPath, attrs)
Expand Down Expand Up @@ -307,7 +313,13 @@ object Files {
attrs: Array[FileAttribute[_]]
): Path = {
val p = if (prefix == null) "" else prefix
val temp = FileHelpers.createTempFile(p, suffix, dir, minLength = false)
val temp = FileHelpers.createTempFile(
p,
suffix,
dir,
minLength = false,
throwOnError = true
)
val tempPath = temp.toPath()
setAttributes(tempPath, attrs)
tempPath
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import StandardCopyOption._
import org.junit.Test
import org.junit.Assert._
import org.junit.Assume._
import org.junit.Ignore

import scala.util.{Try, Failure}

Expand Down Expand Up @@ -552,6 +553,31 @@ class FilesTest {
} finally Files.delete(dir)
}

/* This test is @Ignore'd for Continuous Integration (CI) because
* it creates, on purpose, an infinite loop when the condition described
* in Issue 3738 is present: bad path argument causes an infinite loop.
*
* The test can be run manually. It passes on JVM and should soon pass
* on Scala Native.
*
* If ever Scala Native gets the JUnit test timeout feature, this
* test can be changed for use in CI.
*/

// Issue 3738
@Ignore
@Test def filesCreateTempDirectoryDoesNotLoopInfinitely(): Unit = {
val path = Paths.get("/+")

try {
val f = Files.createTempFile(path, "Alpha", "Omega")
Files.delete(f)
fail("expected IOException or a sub-class of it")
} catch {
case e: IOException =>
}
}

@Test def filesCreateTempFileWorksWithNullPrefix(): Unit = {
val file = Files.createTempFile(null, "txt")
try {
Expand All @@ -576,6 +602,24 @@ class FilesTest {
} finally Files.delete(file)
}

/* See comments above filesCreateTempDirectoryDoesNotLoopInfinitely()
* test above. Same song, next verse.
*/

// Issue 3738
@Ignore
@Test def filesCreateTempFileDoesNotLoopInfinitely(): Unit = {
val path = Paths.get("/+")

try {
val f = Files.createTempFile(path, "Alpha", "Omega")
Files.delete(f)
fail("expected IOException or a sub-class of it")
} catch {
case e: IOException =>
}
}

@Test def filesIsRegularFileReportsFilesAsSuch(): Unit = {
withTemporaryDirectory { dirFile =>
val dir = dirFile.toPath
Expand Down