Skip to content

Commit 1011b41

Browse files
committed
fix(filetxn): open long Windows paths with the extended-length prefix
directoryIdentity passed the caller's path straight to CreateFile. On Windows builds before 10.0.15063 the Go runtime cannot opt the process into long-path support, and the os package then adds the \?\ prefix on its own calls, so a long target would reach every os operation of the transaction and fail only in the identity read. extendedLengthPath now adds the prefix to every long clean absolute path (and the \?\UNC\ form for UNC paths), as os does, so the identity read no longer depends on that opt-in or on the LongPathsEnabled registry value. TestExtendedLengthPath pins the conversion, and the MAX_PATH subtest now exercises the prefixed open on any host. Refs #370
1 parent 948448b commit 1011b41

2 files changed

Lines changed: 39 additions & 6 deletions

File tree

‎internal/filetxn/identity_windows.go‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ package filetxn
44

55
import (
66
"os"
7+
"path/filepath"
8+
"strings"
79
"syscall"
810
)
911

@@ -15,7 +17,7 @@ func directoryIdentity(path string, info os.FileInfo) (device, inode uint64, ok
1517
if !isRealDirectory(info) {
1618
return 0, 0, false
1719
}
18-
name, err := syscall.UTF16PtrFromString(path)
20+
name, err := syscall.UTF16PtrFromString(extendedLengthPath(path))
1921
if err != nil {
2022
return 0, 0, false
2123
}
@@ -54,3 +56,20 @@ func directoryIdentity(path string, info os.FileInfo) (device, inode uint64, ok
5456
}
5557
return uint64(data.VolumeSerialNumber), index, true
5658
}
59+
60+
// extendedLengthPath adds the \\?\ prefix to a long clean absolute path, as the
61+
// os package does when the Go runtime cannot opt the process into long-path
62+
// support (Windows builds before 10.0.15063). Prefixing every long path keeps
63+
// the identity read independent of that opt-in and of the LongPathsEnabled
64+
// registry value. Short, relative, unclean, or already extended paths are
65+
// returned unchanged.
66+
func extendedLengthPath(path string) string {
67+
const maxShortDirectory = 248 // MAX_PATH minus room for an 8.3 name, as in os
68+
if len(path) < maxShortDirectory || !filepath.IsAbs(path) || filepath.Clean(path) != path || strings.HasPrefix(path, `\\?\`) {
69+
return path
70+
}
71+
if strings.HasPrefix(path, `\\`) {
72+
return `\\?\UNC\` + path[2:]
73+
}
74+
return `\\?\` + path
75+
}

‎internal/filetxn/identity_windows_test.go‎

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,23 @@ func pinnedLstat(t *testing.T, path string) os.FileInfo {
2929
return info
3030
}
3131

32+
func TestExtendedLengthPath(t *testing.T) {
33+
long := `C:\` + strings.Repeat(`d\`, 130) + "leaf"
34+
unc := `\\server\share\` + strings.Repeat(`d\`, 130) + "leaf"
35+
for _, test := range []struct{ name, path, want string }{
36+
{"short absolute", `C:\short\dir`, `C:\short\dir`},
37+
{"long absolute", long, `\\?\` + long},
38+
{"long UNC", unc, `\\?\UNC\` + unc[2:]},
39+
{"already extended", `\\?\` + long, `\\?\` + long},
40+
{"long relative", strings.Repeat(`d\`, 130) + "leaf", strings.Repeat(`d\`, 130) + "leaf"},
41+
{"long unclean", long + `\.\x`, long + `\.\x`},
42+
} {
43+
if got := extendedLengthPath(test.path); got != test.want {
44+
t.Errorf("%s: extendedLengthPath(%q) = %q, want %q", test.name, test.path, got, test.want)
45+
}
46+
}
47+
}
48+
3249
func TestDirectoryIdentityWindowsRefusals(t *testing.T) {
3350
t.Run("regular file", func(t *testing.T) {
3451
path := filepath.Join(t.TempDir(), "file.txt")
@@ -71,11 +88,8 @@ func TestDirectoryIdentityWindowsRefusals(t *testing.T) {
7188
t.Fatal("stale lstat reported an identity for the replacement")
7289
}
7390
})
74-
// The Go runtime opts every process into long-path support at startup
75-
// (initLongPathSupport sets the PEB flag on Windows 10 1703 and later),
76-
// and the os package then skips its own \\?\ prefix, so os.MkdirAll and
77-
// syscall.CreateFile resolve long paths the same way regardless of the
78-
// LongPathsEnabled registry value.
91+
// directoryIdentity opens every long path through the \\?\ prefix, so this
92+
// holds without the runtime long-path opt-in or the LongPathsEnabled value.
7993
t.Run("path longer than MAX_PATH", func(t *testing.T) {
8094
path := t.TempDir()
8195
for len(path) <= 300 {

0 commit comments

Comments
 (0)