Open History to see the last commit
1import { execFile } from 'node:child_process'
2import {
3 access,
4 chmod,
5 mkdir,
6 mkdtemp,
7 readFile,
8 rm,
9 symlink,
10 writeFile,
11} from 'node:fs/promises'
12import { tmpdir } from 'node:os'
13import { join } from 'node:path'
14import { describe, expect, it } from 'vitest'
15
16function quote(value: string): string {
17 return `'${value.replace(/'/g, `'\\''`)}'`
18}
19
20function lstatCommand(path: string): string {
21 return `tanstack_lstat_path=${quote(path)}; tanstack_lstat_output=$(stat -c '%f:%s' -- "$tanstack_lstat_path" 2>&1); tanstack_lstat_status=$?; if [ "$tanstack_lstat_status" -eq 0 ]; then printf '%s\n' "$tanstack_lstat_output"; else tanstack_lstat_missing() { tanstack_missing_path=$1; case "$tanstack_missing_path" in /|.) return 1 ;; */*) tanstack_parent=${'$'}{tanstack_missing_path%/*}; tanstack_name=${'$'}{tanstack_missing_path##*/}; [ -n "$tanstack_parent" ] || tanstack_parent=/ ;; *) tanstack_parent=.; tanstack_name=$tanstack_missing_path ;; esac; tanstack_parent_mode=$(stat -L -c '%f' -- "$tanstack_parent" 2>/dev/null); tanstack_parent_status=$?; if [ "$tanstack_parent_status" -ne 0 ]; then tanstack_lstat_missing "$tanstack_parent"; else case "$tanstack_parent_mode" in 4[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]) case "$tanstack_parent" in /*) tanstack_find_parent=$tanstack_parent ;; *) tanstack_find_parent=./$tanstack_parent ;; esac; tanstack_match=$(find -H "$tanstack_find_parent" -mindepth 1 -maxdepth 1 -exec sh -c 'tanstack_target=$1; shift; for tanstack_candidate do [ "${'$'}{tanstack_candidate##*/}" = "$tanstack_target" ] && { printf 1; exit 0; }; done; exit 0' sh "$tanstack_name" '{}' + 2>/dev/null); tanstack_find_status=$?; [ "$tanstack_find_status" -eq 0 ] && [ -z "$tanstack_match" ] ;; *) return 1 ;; esac; fi; }; if tanstack_lstat_missing "$tanstack_lstat_path"; then printf '%s' '__TANSTACK_LSTAT_MISSING__'; else printf '%s\n' "$tanstack_lstat_output" >&2; exit "$tanstack_lstat_status"; fi; fi`
22}
23
24function execFileExitCode(
25 error: { code?: string | number | null } | null,
26): number {
27 if (error == null) return 0
28 if (typeof error.code === 'number') return error.code
29 return 1
30}
31
32function runShell(path: string, cwd?: string) {
33 return new Promise<{ exitCode: number; stdout: string; stderr: string }>(
34 (resolve) => {
35 execFile(
36 'sh',
37 ['-c', lstatCommand(path)],
38 { cwd, encoding: 'utf8' },
39 (error, stdout, stderr) => {
40 resolve({
41 exitCode: execFileExitCode(error),
42 stdout,
43 stderr,
44 })
45 },
46 )
47 },
48 )
49}
50
51const describeShell = process.platform === 'linux' ? describe : describe.skip
52
53describe('remote lstat shell protocol helpers', () => {
54 it('does not treat a spawn failure as exit code 0', () => {
55 expect(
56 execFileExitCode(
57 Object.assign(new Error('spawn sh ENOENT'), { code: 'ENOENT' }),
58 ),
59 ).not.toBe(0)
60 })
61})
62
63describeShell('remote lstat shell protocol', () => {
64 it('proves missing paths and follows a command-line parent symlink', async () => {
65 const root = await mkdtemp(join(tmpdir(), 'tanstack-lstat-'))
66 const directory = join(root, 'directory')
67 const link = join(root, 'directory-link')
68 await mkdir(directory)
69 await symlink(directory, link)
70
71 try {
72 for (const path of [
73 join(directory, 'missing'),
74 join(root, 'missing-parent', 'child'),
75 join(link, 'missing'),
76 ]) {
77 await expect(runShell(path)).resolves.toEqual({
78 exitCode: 0,
79 stdout: '__TANSTACK_LSTAT_MISSING__',
80 stderr: '',
81 })
82 }
83 } finally {
84 await rm(root, { recursive: true, force: true })
85 }
86 })
87
88 it('preserves dangling-link metadata and rejects unverified parents', async () => {
89 const root = await mkdtemp(join(tmpdir(), 'tanstack-lstat-'))
90 const file = join(root, 'file')
91 const dangling = join(root, 'dangling')
92 const loop = join(root, 'loop')
93 await writeFile(file, 'x')
94 await symlink('missing-target', dangling)
95 await symlink('loop', loop)
96
97 try {
98 await expect(runShell(dangling)).resolves.toMatchObject({
99 exitCode: 0,
100 stdout: expect.stringMatching(/^a[0-9a-f]{3}:\d+\n$/i),
101 stderr: '',
102 })
103 for (const path of [join(file, 'child'), join(loop, 'child')]) {
104 const result = await runShell(path)
105 expect(result.exitCode).not.toBe(0)
106 expect(result.stdout).not.toContain('__TANSTACK_LSTAT_MISSING__')
107 expect(result.stderr).not.toBe('')
108 }
109 } finally {
110 await rm(root, { recursive: true, force: true })
111 }
112 })
113
114 it.skipIf(typeof process.getuid === 'function' && process.getuid() === 0)(
115 'preserves EACCES through a symlink parent',
116 async () => {
117 const root = await mkdtemp(join(tmpdir(), 'tanstack-lstat-'))
118 const denied = join(root, 'denied')
119 const link = join(root, 'denied-link')
120 await mkdir(denied)
121 await writeFile(join(denied, 'hidden'), 'x')
122 await symlink(denied, link)
123 await chmod(denied, 0o000)
124
125 try {
126 const result = await runShell(join(link, 'hidden'))
127 expect(result.exitCode).not.toBe(0)
128 expect(result.stdout).not.toContain('__TANSTACK_LSTAT_MISSING__')
129 expect(result.stderr).not.toBe('')
130 } finally {
131 await chmod(denied, 0o700)
132 await rm(root, { recursive: true, force: true })
133 }
134 },
135 )
136
137 it.skipIf(typeof process.getuid === 'function' && process.getuid() === 0)(
138 'preserves EACCES for a relative -H parent',
139 async () => {
140 const root = await mkdtemp(join(tmpdir(), 'tanstack-lstat-'))
141 const denied = join(root, '-H')
142 await mkdir(denied)
143 await writeFile(join(denied, 'hidden'), 'x')
144 await chmod(denied, 0o000)
145
146 try {
147 const result = await runShell('-H/hidden', root)
148 expect(result.exitCode).not.toBe(0)
149 expect(result.stdout).not.toContain('__TANSTACK_LSTAT_MISSING__')
150 expect(result.stderr).not.toBe('')
151 } finally {
152 await chmod(denied, 0o700)
153 await rm(root, { recursive: true, force: true })
154 }
155 },
156 )
157
158 it('treats a relative -delete parent as a path without mutating files', async () => {
159 const root = await mkdtemp(join(tmpdir(), 'tanstack-lstat-'))
160 const optionLikeDirectory = join(root, '-delete')
161 const victim = join(root, 'victim')
162 await mkdir(optionLikeDirectory)
163 await writeFile(victim, 'keep me')
164
165 try {
166 await expect(runShell('-delete/missing', root)).resolves.toEqual({
167 exitCode: 0,
168 stdout: '__TANSTACK_LSTAT_MISSING__',
169 stderr: '',
170 })
171 await expect(access(optionLikeDirectory)).resolves.toBeUndefined()
172 await expect(readFile(victim, 'utf8')).resolves.toBe('keep me')
173 } finally {
174 await rm(root, { recursive: true, force: true })
175 }
176 })
177})