Skip to content

Commit adaec91

Browse files
committed
fix(filetxn): leave device paths unprefixed and pin the prefix wiring
Address the fleet review on #386: - extendedLengthPath now passes device paths (\.\) through unchanged, as the os package does; prefixing them would change their meaning. - TestExtendedLengthPath pins the 247/248-character threshold and a long device path. - TestDirectoryIdentityOpensLongPathsExtended opens a long directory whose name ends with a dot, which only the \?\ form preserves, so the test fails if directoryIdentity stops using the prefix even on hosts where the runtime long-path opt-in makes plain long paths work. - TestRestartDirectoryMatchesAcceptedWindows drives the production restart check with the resolved target on Windows. Refs #370
1 parent 1011b41 commit adaec91

2 files changed

Lines changed: 64 additions & 3 deletions

File tree

‎internal/filetxn/identity_windows.go‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,12 @@ func directoryIdentity(path string, info os.FileInfo) (device, inode uint64, ok
6161
// os package does when the Go runtime cannot opt the process into long-path
6262
// support (Windows builds before 10.0.15063). Prefixing every long path keeps
6363
// 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.
64+
// registry value. Short, relative, unclean, already extended, or device
65+
// (\\.\) paths are returned unchanged; os also leaves device paths alone,
66+
// because the prefix would change their meaning.
6667
func extendedLengthPath(path string) string {
6768
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+
if len(path) < maxShortDirectory || !filepath.IsAbs(path) || filepath.Clean(path) != path || strings.HasPrefix(path, `\\?\`) || strings.HasPrefix(path, `\\.\`) {
6970
return path
7071
}
7172
if strings.HasPrefix(path, `\\`) {

‎internal/filetxn/identity_windows_test.go‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,18 @@ func pinnedLstat(t *testing.T, path string) os.FileInfo {
3232
func TestExtendedLengthPath(t *testing.T) {
3333
long := `C:\` + strings.Repeat(`d\`, 130) + "leaf"
3434
unc := `\\server\share\` + strings.Repeat(`d\`, 130) + "leaf"
35+
device := `\\.\` + long[3:]
36+
// at247 and at248 sit on the os threshold: shorter than 248 stays as is.
37+
at247 := `C:\` + strings.Repeat("a", 244)
38+
at248 := at247 + "a"
39+
if len(at247) != 247 || len(at248) != 248 {
40+
t.Fatalf("boundary fixtures are %d and %d characters", len(at247), len(at248))
41+
}
3542
for _, test := range []struct{ name, path, want string }{
3643
{"short absolute", `C:\short\dir`, `C:\short\dir`},
44+
{"247 characters", at247, at247},
45+
{"248 characters", at248, `\\?\` + at248},
46+
{"long device path", device, device},
3747
{"long absolute", long, `\\?\` + long},
3848
{"long UNC", unc, `\\?\UNC\` + unc[2:]},
3949
{"already extended", `\\?\` + long, `\\?\` + long},
@@ -46,6 +56,56 @@ func TestExtendedLengthPath(t *testing.T) {
4656
}
4757
}
4858

59+
// TestRestartDirectoryMatchesAcceptedWindows drives the production restart
60+
// check with the resolved target it receives in rollback and classification,
61+
// so the Windows wiring of the path argument is exercised, not only the helper.
62+
func TestRestartDirectoryMatchesAcceptedWindows(t *testing.T) {
63+
root := t.TempDir()
64+
target := filepath.Join(root, "made")
65+
must(t, os.Mkdir(target, 0o700))
66+
info := pinnedLstat(t, target)
67+
device, inode, ok := directoryIdentity(target, info)
68+
if !ok {
69+
t.Fatal("directory identity is unavailable")
70+
}
71+
accepted := AcceptedDirectory{Path: "made", Device: device, Inode: inode, Mode: uint32(info.Mode().Perm())}
72+
if !restartDirectoryMatchesAccepted(target, info, accepted) {
73+
t.Fatal("accepted directory does not match its own identity")
74+
}
75+
if restartDirectoryMatchesAccepted("made", info, accepted) {
76+
t.Fatal("root-relative path matched; the check must use the resolved target")
77+
}
78+
replaceDirectoryWithDistinctIdentity(t, root, "made")
79+
if restartDirectoryMatchesAccepted(target, pinnedLstat(t, target), accepted) {
80+
t.Fatal("replacement directory matched the accepted identity")
81+
}
82+
}
83+
84+
// TestDirectoryIdentityOpensLongPathsExtended proves the identity read uses the
85+
// extended-length form even where the runtime long-path opt-in would make a
86+
// plain long path work: a trailing dot survives only in the \\?\ form, since
87+
// Win32 path normalization strips it from a plain path.
88+
func TestDirectoryIdentityOpensLongPathsExtended(t *testing.T) {
89+
parent := t.TempDir()
90+
for len(parent) <= 260 {
91+
parent = filepath.Join(parent, strings.Repeat("d", 40))
92+
}
93+
must(t, os.MkdirAll(parent, 0o700))
94+
path := filepath.Join(parent, "dotted.")
95+
must(t, os.Mkdir(`\\?\`+path, 0o700))
96+
t.Cleanup(func() {
97+
if err := os.Remove(`\\?\` + path); err != nil {
98+
t.Errorf("remove trailing-dot directory: %v", err)
99+
}
100+
})
101+
if _, err := os.Lstat(filepath.Join(parent, "dotted")); !os.IsNotExist(err) {
102+
t.Fatalf("fixture is ambiguous: a plain-normalized twin exists: %v", err)
103+
}
104+
if _, _, ok := directoryIdentity(path, pinnedLstat(t, `\\?\`+path)); !ok {
105+
t.Fatal("long trailing-dot directory has no identity; the read did not use the extended-length form")
106+
}
107+
}
108+
49109
func TestDirectoryIdentityWindowsRefusals(t *testing.T) {
50110
t.Run("regular file", func(t *testing.T) {
51111
path := filepath.Join(t.TempDir(), "file.txt")

0 commit comments

Comments
 (0)