Skip to content

Commit

Permalink
Prefer File.getCanonicalFile over Path.normalize
Browse files Browse the repository at this point in the history
The former normalizes Windows 8.3 paths to long paths, whereas the
latter doesn't.
  • Loading branch information
alexarchambault committed May 23, 2021
1 parent 53c33bb commit 603dc76
Showing 1 changed file with 17 additions and 3 deletions.
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

0 comments on commit 603dc76

Please sign in to comment.