|
| 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 | +} |
0 commit comments