11import { createHash } from "node:crypto" ;
2- import fsSync , { type Stats } from "node:fs" ;
2+ import fsSync , { type BigIntStats , type Stats } from "node:fs" ;
33import type { FileHandle } from "node:fs/promises" ;
44import fs from "node:fs/promises" ;
55import path from "node:path" ;
@@ -78,15 +78,15 @@ function directoryOpenFlags(): number {
7878 ) ;
7979}
8080
81- async function openNativeParent ( binding : NativeBinding , filePath : string ) : Promise < {
81+ async function openNativeParent ( filePath : string ) : Promise < {
8282 basename : string ;
8383 handle : FileHandle ;
8484} > {
8585 const parentPath = path . dirname ( filePath ) ;
8686 const handle = await fs . open ( parentPath , directoryOpenFlags ( ) ) ;
8787 try {
88- const pathname = await fs . lstat ( parentPath ) ;
89- const opened = binding . fstatIdentity ( handle . fd ) ;
88+ const pathname = await fs . lstat ( parentPath , { bigint : true } ) ;
89+ const opened = await handle . stat ( { bigint : true } ) ;
9090 if ( pathname . isSymbolicLink ( ) || ! sameFileIdentity ( pathname , opened ) ) {
9191 throw new FsSafeError ( "path-mismatch" , "publication parent changed while opening" ) ;
9292 }
@@ -100,10 +100,12 @@ async function openNativeParent(binding: NativeBinding, filePath: string): Promi
100100async function assertPinnedSourceCurrent ( params : {
101101 sourcePath : string ;
102102 handle : FileHandle ;
103- identity : Stats ;
103+ identity : BigIntStats ;
104104} ) : Promise < void > {
105- const opened = await params . handle . stat ( ) ;
106- const current = await fs . lstat ( params . sourcePath ) ;
105+ // Windows file indexes can exceed Number.MAX_SAFE_INTEGER, so publication
106+ // fences must compare bigint stats instead of rounded numeric identities.
107+ const opened = await params . handle . stat ( { bigint : true } ) ;
108+ const current = await fs . lstat ( params . sourcePath , { bigint : true } ) ;
107109 if (
108110 ! opened . isFile ( ) ||
109111 current . isSymbolicLink ( ) ||
@@ -121,7 +123,12 @@ async function copyPinnedSource(params: {
121123 native ?: NativeBinding ;
122124 targetNativeParent ?: Awaited < ReturnType < typeof openNativeParent > > ;
123125 failure : PublishFailureState ;
124- } ) : Promise < { handle : FileHandle ; stat : Stats ; digest : string ; bytes : number } > {
126+ } ) : Promise < {
127+ handle : FileHandle ;
128+ exactIdentity : BigIntStats ;
129+ digest : string ;
130+ bytes : number ;
131+ } > {
125132 if ( params . native && params . targetNativeParent ) {
126133 for ( const method of [ "clone" , "copy-file-range" ] as const ) {
127134 let nativeFd : number | undefined ;
@@ -153,17 +160,17 @@ async function copyPinnedSource(params: {
153160 }
154161
155162 let target : FileHandle | undefined ;
156- const createdIdentity = params . native . fstatIdentity ( nativeFd ) ;
163+ const createdIdentity = fsSync . fstatSync ( nativeFd , { bigint : true } ) ;
157164 rememberCreatedTarget ( params . failure , createdIdentity , "copy-verify" ) ;
158165 try {
159166 await getFsSafeTestHooks ( ) ?. afterPublishTargetCreated ?.(
160167 "exclusive-copy" ,
161168 params . targetPath ,
162169 createdIdentity ,
163170 ) ;
164- const identity = await fs . lstat ( params . targetPath ) ;
171+ const identity = await fs . lstat ( params . targetPath , { bigint : true } ) ;
165172 target = await fs . open ( params . targetPath , sourceOpenFlags ( ) ) ;
166- const opened = await target . stat ( ) ;
173+ const opened = await target . stat ( { bigint : true } ) ;
167174 if (
168175 identity . isSymbolicLink ( ) ||
169176 ! identity . isFile ( ) ||
@@ -177,7 +184,7 @@ async function copyPinnedSource(params: {
177184 nativeFd = undefined ;
178185 return {
179186 handle : target ,
180- stat : await target . stat ( ) ,
187+ exactIdentity : opened ,
181188 digest : hashed . digest ,
182189 bytes : hashed . bytes ,
183190 } ;
@@ -191,13 +198,13 @@ async function copyPinnedSource(params: {
191198 }
192199
193200 const target = await fs . open ( params . targetPath , "wx+" , 0o600 ) ;
194- const identity = await target . stat ( ) ;
195- rememberCreatedTarget ( params . failure , identity , "copy-verify" ) ;
201+ const exactIdentity = await target . stat ( { bigint : true } ) ;
202+ rememberCreatedTarget ( params . failure , exactIdentity , "copy-verify" ) ;
196203 try {
197204 await getFsSafeTestHooks ( ) ?. afterPublishTargetCreated ?.(
198205 "exclusive-copy" ,
199206 params . targetPath ,
200- identity ,
207+ exactIdentity ,
201208 ) ;
202209 const hash = createHash ( "sha256" ) ;
203210 const buffer = Buffer . allocUnsafe ( 64 * 1024 ) ;
@@ -224,7 +231,7 @@ async function copyPinnedSource(params: {
224231 position += bytesRead ;
225232 }
226233 await target . sync ( ) ;
227- return { handle : target , stat : identity , digest : hash . digest ( "hex" ) , bytes : position } ;
234+ return { handle : target , exactIdentity , digest : hash . digest ( "hex" ) , bytes : position } ;
228235 } catch ( error ) {
229236 await target . close ( ) . catch ( ( ) => undefined ) ;
230237 throw error ;
@@ -239,7 +246,7 @@ async function removeCreatedTargetIfUnchanged(
239246 return "unknown" ;
240247 }
241248 try {
242- const current = await fs . lstat ( targetPath ) ;
249+ const current = await fs . lstat ( targetPath , { bigint : true } ) ;
243250 if ( ! current . isSymbolicLink ( ) && sameFileIdentity ( current , identity ) ) {
244251 await fs . rm ( targetPath ) ;
245252 return "removed" ;
@@ -305,22 +312,32 @@ export async function publishFileExclusive(params: {
305312 label : "publication parent" ,
306313 } ) ;
307314 const sourceIdentity = await source . stat ( ) ;
315+ const sourceExactIdentity = await source . stat ( { bigint : true } ) ;
316+ const sourcePathExactIdentity = await fs . lstat ( sourcePath , { bigint : true } ) ;
308317 if (
309- ! sameFileIdentity ( sourcePathStat , sourceIdentity ) ||
318+ sourcePathExactIdentity . isSymbolicLink ( ) ||
319+ ! sourcePathExactIdentity . isFile ( ) ||
320+ ! sameFileIdentity ( sourcePathExactIdentity , sourceExactIdentity ) ||
310321 ( params . expectedSourceIdentity &&
311- ! sameFileIdentity ( params . expectedSourceIdentity , sourceIdentity ) )
322+ ! sameFileIdentity (
323+ params . expectedSourceIdentity ,
324+ typeof params . expectedSourceIdentity . dev === "bigint" ||
325+ typeof params . expectedSourceIdentity . ino === "bigint"
326+ ? sourceExactIdentity
327+ : sourceIdentity ,
328+ ) )
312329 ) {
313330 throw new FsSafeError ( "path-mismatch" , "publication source identity did not match" ) ;
314331 }
315332 await parent . assertCurrent ( ) ;
316- await assertPinnedSourceCurrent ( { sourcePath, handle : source , identity : sourceIdentity } ) ;
333+ await assertPinnedSourceCurrent ( { sourcePath, handle : source , identity : sourceExactIdentity } ) ;
317334
318335 const native = params . strategy === "rename-noreplace"
319336 ? requireNativeBinding ( )
320337 : getNativeBinding ( ) ;
321338 if ( native ) {
322- sourceNativeParent = await openNativeParent ( native , sourcePath ) ;
323- targetNativeParent = await openNativeParent ( native , targetPath ) ;
339+ sourceNativeParent = await openNativeParent ( sourcePath ) ;
340+ targetNativeParent = await openNativeParent ( targetPath ) ;
324341 }
325342
326343 if ( params . strategy === "rename-noreplace" ) {
@@ -331,19 +348,19 @@ export async function publishFileExclusive(params: {
331348 targetNativeParent ! . handle . fd ,
332349 targetNativeParent ! . basename ,
333350 ) ;
334- rememberCreatedTarget ( failure , sourceIdentity , "rename-verify" ) ;
351+ rememberCreatedTarget ( failure , sourceExactIdentity , "rename-verify" ) ;
335352 // A failed post-rename fence must not delete the only remaining name.
336353 failure . preserveTarget = true ;
337354 await getFsSafeTestHooks ( ) ?. afterPublishTargetCreated ?.(
338355 "rename-noreplace" ,
339356 targetPath ,
340- sourceIdentity ,
357+ sourceExactIdentity ,
341358 ) ;
342- const targetIdentity = await fs . lstat ( targetPath ) ;
359+ const targetExactIdentity = await fs . lstat ( targetPath , { bigint : true } ) ;
343360 if (
344- targetIdentity . isSymbolicLink ( ) ||
345- ! targetIdentity . isFile ( ) ||
346- ! sameFileIdentity ( targetIdentity , sourceIdentity )
361+ targetExactIdentity . isSymbolicLink ( ) ||
362+ ! targetExactIdentity . isFile ( ) ||
363+ ! sameFileIdentity ( targetExactIdentity , sourceExactIdentity )
347364 ) {
348365 throw new FsSafeError ( "path-mismatch" , "no-replace publication target changed" ) ;
349366 }
@@ -356,6 +373,7 @@ export async function publishFileExclusive(params: {
356373 }
357374 }
358375 syncNativeFileBestEffort ( sourceNativeParent ! . handle . fd ) ;
376+ const targetIdentity = await fs . lstat ( targetPath ) ;
359377 return {
360378 method : "rename-noreplace" ,
361379 identity : targetIdentity ,
@@ -379,21 +397,22 @@ export async function publishFileExclusive(params: {
379397 } else {
380398 await fs . link ( sourcePath , targetPath ) ;
381399 }
382- rememberCreatedTarget ( failure , sourceIdentity , "hardlink-verify" ) ;
400+ rememberCreatedTarget ( failure , sourceExactIdentity , "hardlink-verify" ) ;
383401 await getFsSafeTestHooks ( ) ?. afterPublishTargetCreated ?.(
384402 "hardlink" ,
385403 targetPath ,
386- sourceIdentity ,
404+ sourceExactIdentity ,
387405 ) ;
388- const targetIdentity = await fs . lstat ( targetPath ) ;
406+ const targetExactIdentity = await fs . lstat ( targetPath , { bigint : true } ) ;
389407 if (
390- targetIdentity . isSymbolicLink ( ) ||
391- ! targetIdentity . isFile ( ) ||
392- ! sameFileIdentity ( targetIdentity , sourceIdentity )
408+ targetExactIdentity . isSymbolicLink ( ) ||
409+ ! targetExactIdentity . isFile ( ) ||
410+ ! sameFileIdentity ( targetExactIdentity , sourceExactIdentity )
393411 ) {
394412 throw new FsSafeError ( "path-mismatch" , "hardlink publication target changed" ) ;
395413 }
396- await assertPinnedSourceCurrent ( { sourcePath, handle : source , identity : sourceIdentity } ) ;
414+ await assertPinnedSourceCurrent ( { sourcePath, handle : source , identity : sourceExactIdentity } ) ;
415+ const targetIdentity = await fs . lstat ( targetPath ) ;
397416 return {
398417 method : "hardlink" ,
399418 identity : targetIdentity ,
@@ -426,21 +445,23 @@ export async function publishFileExclusive(params: {
426445 failure,
427446 } ) ;
428447 target = copied . handle ;
429- targetIdentity = copied . stat ;
448+ targetIdentity = await target . stat ( ) ;
430449 const targetPathStat = await fs . lstat ( targetPath ) ;
450+ const targetPathExactStat = await fs . lstat ( targetPath , { bigint : true } ) ;
431451 const copiedBack = await hashFileHandle ( target , native ) ;
432452 const sourceAfter = await hashFileHandle ( source , native ) ;
433453 if (
434454 targetPathStat . isSymbolicLink ( ) ||
435- ! sameFileIdentity ( targetPathStat , targetIdentity ) ||
455+ targetPathExactStat . isSymbolicLink ( ) ||
456+ ! sameFileIdentity ( targetPathExactStat , copied . exactIdentity ) ||
436457 copiedBack . bytes !== copied . bytes ||
437458 copiedBack . digest !== copied . digest ||
438459 sourceAfter . bytes !== copied . bytes ||
439460 sourceAfter . digest !== copied . digest
440461 ) {
441462 throw new FsSafeError ( "path-mismatch" , "exclusive publication copy failed content fencing" ) ;
442463 }
443- await assertPinnedSourceCurrent ( { sourcePath, handle : source , identity : sourceIdentity } ) ;
464+ await assertPinnedSourceCurrent ( { sourcePath, handle : source , identity : sourceExactIdentity } ) ;
444465 const directorySync = await syncPublishedParent ( {
445466 parent,
446467 failure,
@@ -461,7 +482,7 @@ export async function publishFileExclusive(params: {
461482 failure . phase === "directory-sync" && params . onSyncFailure === "preserve" ;
462483 const cleanup = failure . preserveTarget || preserveSyncFailure
463484 ? "preserved"
464- : await removeCreatedTargetIfUnchanged ( targetPath , failure . targetIdentity ) ;
485+ : await removeCreatedTargetIfUnchanged ( targetPath , failure . targetCleanupIdentity ) ;
465486 throw publicationFailure ( error , failure , cleanup ) ;
466487 } finally {
467488 await sourceNativeParent ?. handle . close ( ) . catch ( ( ) => undefined ) ;
0 commit comments