Skip to content

Commit

Permalink
Fix last modified for long paths on windows
Browse files Browse the repository at this point in the history
It is necessary to prefix the file name passed into CreateFile with
"\\?\" to allow file names with more than max_path (260) characters on
windows:
https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilea..

Fixes sbt/sbt#5098.
  • Loading branch information
eatkins authored and eed3si9n committed Sep 19, 2019
1 parent 7e50d3f commit 500238d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
2 changes: 1 addition & 1 deletion io/src/main/scala/sbt/internal/io/Milli.scala
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ private object WinMilli extends MilliNative[FILETIME] {

private def getHandle(lpFileName: String, dwDesiredAccess: Int, dwShareMode: Int): HANDLE = {
val hFile = CreateFile(
lpFileName,
"\\\\?\\" + lpFileName,
dwDesiredAccess,
dwShareMode,
null,
Expand Down
31 changes: 31 additions & 0 deletions io/src/test/scala/sbt/io/LastModifiedSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* sbt IO
*
* Copyright 2011 - 2019, Lightbend, Inc.
* Copyright 2008 - 2010, Mark Harrah
*
* Licensed under Apache License 2.0
* (http://www.apache.org/licenses/LICENSE-2.0).
*/

package sbt.io

import java.nio.file.Files

import org.scalatest.FlatSpec

class LastModifiedSpec extends FlatSpec {
"IO.getModifiedTimeOrZero" should "work with long path names" in IO.withTemporaryDirectory {
dir =>
val fileName = "a" * 32
val nested =
(1 to 8).foldLeft(dir.toPath) {
case (d, _) => Files.createDirectories(d.resolve(fileName))
}
val file = Files.createFile(nested.resolve(fileName)).toFile
// in case target platform only has second precision round to nearest second
val lm = (System.currentTimeMillis / 1000) * 1000
IO.setModifiedTimeOrFalse(file, lm)
assert(IO.getModifiedTimeOrZero(file) == lm)
}
}

0 comments on commit 500238d

Please sign in to comment.