Skip to content

Commit b645cb4

Browse files
committed
fix(filetxn): read directory identity through per-OS variants
internal/filetxn type-asserted info.Sys().(*syscall.Stat_t) to read the device and inode of accepted directories, which does not compile on Windows. Move the read behind directoryIdentity: the unix variant keeps Stat_t, and the Windows variant opens the directory without following reparse points and reads the volume serial number and file index through GetFileInformationByHandle, refusing a handle that no longer names the caller's Lstat result. The restart_test.go helpers use the same function. The test helper that replaces a directory now pins the original identity before the rename, because Windows loads an Lstat identity lazily from the path. Fixes #370
1 parent 3d027e2 commit b645cb4

8 files changed

Lines changed: 158 additions & 17 deletions

File tree

‎docs/verification.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ curl -fL -O https://github.com/refactor-ia/cortex/releases/download/v0.1.0-alpha
1313
shasum -a 256 -c --ignore-missing SHA256SUMS && tar xzf "$archive"
1414
```
1515

16-
On Linux, set `archive=cortex_v0.1.0-alpha.8_linux_amd64.tar.gz` and use `sha256sum -c --ignore-missing SHA256SUMS` instead of the macOS checksum line. macOS arm64 and Linux amd64 are the published targets; Windows is not supported and does not currently build.
16+
On Linux, set `archive=cortex_v0.1.0-alpha.8_linux_amd64.tar.gz` and use `sha256sum -c --ignore-missing SHA256SUMS` instead of the macOS checksum line. macOS arm64 and Linux amd64 are the published targets; Windows is not supported: it builds from source, but its filesystem transaction behavior is not yet verified.
1717

1818
### Building from source
1919

‎internal/filetxn/directories.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -255,11 +255,11 @@ func acceptCreatedDirectories(snapshot Snapshot, created []createdDirectory) (Sn
255255
if err != nil || !isRealDirectory(info) || !os.SameFile(directory.createdInfo, info) || info.Mode().Perm() != directory.createdInfo.Mode().Perm() {
256256
return Snapshot{}, fmt.Errorf("accepted directory changed: %s", directory.path)
257257
}
258-
stat, ok := info.Sys().(*syscall.Stat_t)
259-
if !ok || stat.Ino == 0 {
258+
device, inode, ok := directoryIdentity(directory.target, info)
259+
if !ok {
260260
return Snapshot{}, fmt.Errorf("accepted directory identity is unavailable: %s", directory.path)
261261
}
262-
accepted[index] = AcceptedDirectory{Path: absent.Path, Device: uint64(stat.Dev), Inode: uint64(stat.Ino), Mode: uint32(info.Mode().Perm())}
262+
accepted[index] = AcceptedDirectory{Path: absent.Path, Device: device, Inode: inode, Mode: uint32(info.Mode().Perm())}
263263
}
264264
snapshot.Manifest.Version = manifestV3
265265
snapshot.Manifest.AcceptedDirectories = accepted

‎internal/filetxn/directories_test.go‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,11 @@ func replaceDirectoryWithDistinctIdentity(t *testing.T, root, name string) {
354354
if !isRealDirectory(original) {
355355
t.Fatalf("original is not a real directory: %s", name)
356356
}
357+
// Windows loads an Lstat identity lazily from the path on first
358+
// comparison, so pin it before the rename.
359+
if !os.SameFile(original, original) {
360+
t.Fatalf("original directory identity is unavailable: %s", name)
361+
}
357362
retiredPath := filepath.Join(root, "."+name+"-retired")
358363
t.Cleanup(func() {
359364
if err := os.RemoveAll(retiredPath); err != nil {

‎internal/filetxn/identity_test.go‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package filetxn
2+
3+
import (
4+
"os"
5+
"path/filepath"
6+
"testing"
7+
)
8+
9+
func lstatDirectoryIdentity(t *testing.T, path string) (uint64, uint64) {
10+
t.Helper()
11+
info, err := os.Lstat(path)
12+
must(t, err)
13+
device, inode, ok := directoryIdentity(path, info)
14+
if !ok {
15+
t.Fatalf("directory identity is unavailable: %s", path)
16+
}
17+
return device, inode
18+
}
19+
20+
func TestDirectoryIdentity(t *testing.T) {
21+
t.Run("stable for the same directory", func(t *testing.T) {
22+
root := t.TempDir()
23+
must(t, os.Mkdir(filepath.Join(root, "made"), 0o700))
24+
device, inode := lstatDirectoryIdentity(t, filepath.Join(root, "made"))
25+
againDevice, againInode := lstatDirectoryIdentity(t, filepath.Join(root, "made"))
26+
if device != againDevice || inode != againInode {
27+
t.Fatalf("identity changed: %d/%d then %d/%d", device, inode, againDevice, againInode)
28+
}
29+
})
30+
t.Run("distinct for sibling directories", func(t *testing.T) {
31+
root := t.TempDir()
32+
must(t, os.Mkdir(filepath.Join(root, "left"), 0o700))
33+
must(t, os.Mkdir(filepath.Join(root, "right"), 0o700))
34+
leftDevice, leftInode := lstatDirectoryIdentity(t, filepath.Join(root, "left"))
35+
rightDevice, rightInode := lstatDirectoryIdentity(t, filepath.Join(root, "right"))
36+
if leftDevice != rightDevice {
37+
t.Fatalf("siblings report different devices: %d and %d", leftDevice, rightDevice)
38+
}
39+
if leftInode == rightInode {
40+
t.Fatalf("siblings share inode %d", leftInode)
41+
}
42+
})
43+
t.Run("distinct after replacement", func(t *testing.T) {
44+
root := t.TempDir()
45+
must(t, os.Mkdir(filepath.Join(root, "made"), 0o700))
46+
_, before := lstatDirectoryIdentity(t, filepath.Join(root, "made"))
47+
replaceDirectoryWithDistinctIdentity(t, root, "made")
48+
_, after := lstatDirectoryIdentity(t, filepath.Join(root, "made"))
49+
if before == after {
50+
t.Fatalf("replacement kept inode %d", before)
51+
}
52+
})
53+
t.Run("never the replacement identity for a stale lstat", func(t *testing.T) {
54+
root := t.TempDir()
55+
must(t, os.Mkdir(filepath.Join(root, "made"), 0o700))
56+
stale, err := os.Lstat(filepath.Join(root, "made"))
57+
must(t, err)
58+
// Pin the lazily loaded Windows identity, as callers do with os.SameFile.
59+
if !os.SameFile(stale, stale) {
60+
t.Fatal("stale directory identity is unavailable")
61+
}
62+
replaceDirectoryWithDistinctIdentity(t, root, "made")
63+
_, currentInode := lstatDirectoryIdentity(t, filepath.Join(root, "made"))
64+
if _, inode, ok := directoryIdentity(filepath.Join(root, "made"), stale); ok && inode == currentInode {
65+
t.Fatalf("stale lstat result reported the replacement identity %d", inode)
66+
}
67+
})
68+
}

‎internal/filetxn/identity_unix.go‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//go:build unix
2+
3+
package filetxn
4+
5+
import (
6+
"os"
7+
"syscall"
8+
)
9+
10+
// directoryIdentity returns the device and inode numbers of info. The path is
11+
// unused on unix because the identity is already part of the Lstat result.
12+
func directoryIdentity(_ string, info os.FileInfo) (device, inode uint64, ok bool) {
13+
stat, ok := info.Sys().(*syscall.Stat_t)
14+
if !ok || stat.Ino == 0 {
15+
return 0, 0, false
16+
}
17+
return uint64(stat.Dev), uint64(stat.Ino), true
18+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//go:build windows
2+
3+
package filetxn
4+
5+
import (
6+
"os"
7+
"syscall"
8+
)
9+
10+
// directoryIdentity returns the volume serial number and file index of the
11+
// directory at path. Lstat results on Windows carry no file identity, so the
12+
// directory is opened without following reparse points and the handle must
13+
// still name the same object as info.
14+
func directoryIdentity(path string, info os.FileInfo) (device, inode uint64, ok bool) {
15+
if !isRealDirectory(info) {
16+
return 0, 0, false
17+
}
18+
name, err := syscall.UTF16PtrFromString(path)
19+
if err != nil {
20+
return 0, 0, false
21+
}
22+
handle, err := syscall.CreateFile(
23+
name,
24+
0,
25+
syscall.FILE_SHARE_READ|syscall.FILE_SHARE_WRITE|syscall.FILE_SHARE_DELETE,
26+
nil,
27+
syscall.OPEN_EXISTING,
28+
syscall.FILE_FLAG_BACKUP_SEMANTICS|syscall.FILE_FLAG_OPEN_REPARSE_POINT,
29+
0,
30+
)
31+
if err != nil {
32+
return 0, 0, false
33+
}
34+
file := os.NewFile(uintptr(handle), path)
35+
defer file.Close()
36+
var data syscall.ByHandleFileInformation
37+
if err := syscall.GetFileInformationByHandle(handle, &data); err != nil {
38+
return 0, 0, false
39+
}
40+
if data.FileAttributes&syscall.FILE_ATTRIBUTE_DIRECTORY == 0 || data.FileAttributes&syscall.FILE_ATTRIBUTE_REPARSE_POINT != 0 {
41+
return 0, 0, false
42+
}
43+
opened, err := file.Stat()
44+
if err != nil || !os.SameFile(info, opened) {
45+
return 0, 0, false
46+
}
47+
index := uint64(data.FileIndexHigh)<<32 | uint64(data.FileIndexLow)
48+
if index == 0 {
49+
return 0, 0, false
50+
}
51+
return uint64(data.VolumeSerialNumber), index, true
52+
}

‎internal/filetxn/restart.go‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"os"
1010
"path/filepath"
1111
"strings"
12-
"syscall"
1312

1413
"github.com/refactor-ia/cortex/internal/atomicfile"
1514
"github.com/refactor-ia/cortex/internal/safepath"
@@ -386,7 +385,7 @@ func rollbackRestartDirectories(sourceRoot string, plan restartPlan) error {
386385
return errors.New("restart directory evidence is invalid")
387386
}
388387
info, err := os.Lstat(target)
389-
if err != nil || !isRealDirectory(info) || !restartDirectoryMatchesAccepted(info, directory.after) {
388+
if err != nil || !isRealDirectory(info) || !restartDirectoryMatchesAccepted(target, info, directory.after) {
390389
return errors.New("restart directory changed")
391390
}
392391
if err := os.Remove(target); err != nil {
@@ -402,9 +401,9 @@ func rollbackRestartDirectories(sourceRoot string, plan restartPlan) error {
402401
return nil
403402
}
404403

405-
func restartDirectoryMatchesAccepted(info os.FileInfo, accepted AcceptedDirectory) bool {
406-
stat, ok := info.Sys().(*syscall.Stat_t)
407-
return ok && stat.Ino != 0 && uint64(stat.Dev) == accepted.Device && uint64(stat.Ino) == accepted.Inode && uint32(info.Mode().Perm()) == accepted.Mode
404+
func restartDirectoryMatchesAccepted(target string, info os.FileInfo, accepted AcceptedDirectory) bool {
405+
device, inode, ok := directoryIdentity(target, info)
406+
return ok && device == accepted.Device && inode == accepted.Inode && uint32(info.Mode().Perm()) == accepted.Mode
408407
}
409408

410409
func restartIsBefore(plan restartPlan, statuses []restartStatus) bool {
@@ -482,7 +481,7 @@ func classifyRestartDirectories(sourceRoot string, plan restartPlan) (restartPla
482481
if err != nil || !isRealDirectory(info) || plan.snapshot.Manifest.Version == manifestV2 {
483482
return restartPlan{}, errors.New("restart directory is unsafe")
484483
}
485-
if !restartDirectoryMatchesAccepted(info, directory.after) {
484+
if !restartDirectoryMatchesAccepted(target, info, directory.after) {
486485
return restartPlan{}, errors.New("restart directory drifted")
487486
}
488487
directory.status = exactAfter

‎internal/filetxn/restart_test.go‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"os"
55
"path/filepath"
66
"strings"
7-
"syscall"
87
"testing"
98
)
109

@@ -415,11 +414,11 @@ func restartDirectoryPlan(t *testing.T, root string, version int, paths []string
415414
must(t, os.Mkdir(filepath.Join(root, value), 0o700))
416415
info, err := os.Lstat(filepath.Join(root, value))
417416
must(t, err)
418-
stat, ok := info.Sys().(*syscall.Stat_t)
419-
if !ok || stat.Ino == 0 {
417+
device, inode, ok := directoryIdentity(filepath.Join(root, value), info)
418+
if !ok {
420419
t.Fatal("directory identity is unavailable")
421420
}
422-
accepted[index] = AcceptedDirectory{Path: value, Device: uint64(stat.Dev), Inode: uint64(stat.Ino), Mode: uint32(info.Mode().Perm())}
421+
accepted[index] = AcceptedDirectory{Path: value, Device: device, Inode: inode, Mode: uint32(info.Mode().Perm())}
423422
}
424423
plan, err := planRestartEvidence(Snapshot{Manifest: Manifest{Version: version, AbsentDirectories: absent, AcceptedDirectories: accepted}}, nil)
425424
must(t, err)
@@ -572,11 +571,11 @@ func acceptedRestartDirectories(t *testing.T, root string, absent []directoryEnt
572571
for index, directory := range absent {
573572
info, err := os.Lstat(filepath.Join(root, directory.Path))
574573
must(t, err)
575-
stat, ok := info.Sys().(*syscall.Stat_t)
576-
if !ok || stat.Ino == 0 {
574+
device, inode, ok := directoryIdentity(filepath.Join(root, directory.Path), info)
575+
if !ok {
577576
t.Fatal("directory identity is unavailable")
578577
}
579-
accepted[index] = AcceptedDirectory{Path: directory.Path, Device: uint64(stat.Dev), Inode: uint64(stat.Ino), Mode: uint32(info.Mode().Perm())}
578+
accepted[index] = AcceptedDirectory{Path: directory.Path, Device: device, Inode: inode, Mode: uint32(info.Mode().Perm())}
580579
}
581580
return accepted
582581
}

0 commit comments

Comments
 (0)