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

Prefer File.getCanonicalFile over Path.normalize #79

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions os/src/Path.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package os

import collection.JavaConverters._
import util.Properties

trait PathChunk{
def segments: Seq[String]
Expand Down Expand Up @@ -384,7 +385,20 @@ object Path {
throw PathError.AbsolutePathOutsideRoot
}

val normalized = f.normalize()
fromNIO(f)
}

private def fromNIO(p: java.nio.file.Path): Path = {
// On Windows, File.getCanonicalFile normalizes 8.3 paths as long paths,
// whereas Path.normalize doesn't. So we use the former here.
val shouldUseIO = Properties.isWin &&
p.getFileSystem.getClass.getName == "sun.nio.fs.WindowsFileSystem"
val normalized =
if (shouldUseIO) {
require(p.isAbsolute, s"$p is not an absolute path")
p.toFile.getCanonicalFile.toPath
}
else p.normalize()
new Path(normalized)
}

Expand Down Expand Up @@ -437,8 +451,8 @@ class Path private[os](val wrapped: java.nio.file.Path)

def /(chunk: PathChunk): ThisType = {
if (chunk.ups > wrapped.getNameCount) throw PathError.AbsolutePathOutsideRoot
val resolved = wrapped.resolve(chunk.toString).normalize()
new Path(resolved)
val resolved = wrapped.resolve(chunk.toString)
Path.fromNIO(resolved)
}
override def toString = wrapped.toString

Expand Down
17 changes: 17 additions & 0 deletions os/test/src/PathTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import java.nio.file.Paths

import os._
import utest.{assert => _, _}
import scala.util.Properties
object PathTests extends TestSuite{
val tests = Tests {
test("Basic"){
Expand Down Expand Up @@ -381,5 +382,21 @@ object PathTests extends TestSuite{
}
}
}
test("Windows short paths"){
if (Properties.isWin) {
val dir = os.temp.dir(prefix = "os-lib-tests")

val file = dir / "long-dir-name" / "long-file-name.txt"
// uncomment this line to make the test fail :/
// val shortBefore = dir / "LONG-D~1" / "LONG-F~1.txt"

val content = "os-lib test"
os.write(file, content, createFolders = true)

val short = dir / "LONG-D~1" / "LONG-F~1.txt"
assert(os.read(short) == content)
assert(file == short)
}
}
}
}