Repository navigation
Expand file tree
/
Copy pathWindowsLockFile.ts
More file actions
141 lines (129 loc) · 5.2 KB
/
Copy pathWindowsLockFile.ts
File metadata and controls
141 lines (129 loc) · 5.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import * as fs from 'node:fs';
import type { ILockFileHandle, ITryAcquireResult } from './LockFile';
// Public libuv Windows flag (supported since libuv 1.17), not exposed in fs.constants.
// https://docs.libuv.org/en/v1.x/fs.html#c.UV_FS_O_EXLOCK
const UV_FS_O_EXLOCK: number = 0x10000000;
const CLEAN_MARKER: Buffer = Buffer.from('rushstack-lockfile-clean-v1\n');
function hasCode(error: unknown, code: string): boolean {
return typeof error === 'object' && error !== null && 'code' in error && error.code === code;
}
/** @internal */
export function getWindowsLockFileDirtyPath(filePath: string): string {
return `${filePath}.dirty`;
}
/**
* Uses Windows sharing exclusion; O_EXCL alone only guarantees exclusive creation.
* Called only by the Windows dispatcher. Effective support is checked on every acquisition.
* @internal
*/
export function tryAcquireWindowsLockFile(filePath: string): ITryAcquireResult | undefined {
// eslint-disable-next-line no-bitwise
const flags: number = fs.constants.O_RDWR | UV_FS_O_EXLOCK;
let descriptor: number | undefined;
let created: boolean = false;
try {
// eslint-disable-next-line no-bitwise
descriptor = fs.openSync(filePath, flags | fs.constants.O_CREAT | fs.constants.O_EXCL);
created = true;
} catch (error) {
if (hasCode(error, 'EBUSY')) return undefined;
if (!hasCode(error, 'EEXIST')) throw error;
try {
descriptor = fs.openSync(filePath, flags);
} catch (openError) {
if (hasCode(openError, 'EBUSY') || hasCode(openError, 'ENOENT')) return undefined;
throw openError;
}
}
try {
if (!hasExclusiveSharing(filePath)) return undefined;
const stats: fs.BigIntStats = fs.fstatSync(descriptor, { bigint: true });
const pathStats: fs.BigIntStats = fs.lstatSync(filePath, { bigint: true });
if (
!stats.isFile() ||
!pathStats.isFile() ||
stats.nlink !== 1n ||
stats.dev !== pathStats.dev ||
stats.ino !== pathStats.ino
) {
throw new Error(`The lock path must identify one unshared regular file: ${filePath}`);
}
// A previous release may delete the main pathname after a subsequent owner has already exited.
// This companion survives that close/delete race and is cleared only under exclusive ownership.
const dirtyPath: string = getWindowsLockFileDirtyPath(filePath);
const dirtyMarkerExists: boolean = fs.lstatSync(dirtyPath, { throwIfNoEntry: false }) !== undefined;
const dirtyWhenAcquired: boolean =
dirtyMarkerExists || (!created && !hasCleanMarker(descriptor, stats.size));
markDirty(dirtyPath);
let heldDescriptor: number | undefined = descriptor;
const fileWriter: ILockFileHandle = {
prepareForRelease: (deleteFile) => {
if (!deleteFile) return;
if (heldDescriptor === undefined) throw new Error(`The lock is already closed: ${filePath}`);
fs.ftruncateSync(heldDescriptor, 0);
if (fs.writeSync(heldDescriptor, CLEAN_MARKER, 0, CLEAN_MARKER.length, 0) !== CLEAN_MARKER.length) {
throw new Error(`Could not record the clean lock release: ${filePath}`);
}
try {
fs.unlinkSync(dirtyPath);
} catch (error) {
if (!hasCode(error, 'ENOENT')) throw error;
}
},
close: () => {
if (heldDescriptor !== undefined) {
try {
fs.closeSync(heldDescriptor);
} catch (error) {
// Preparation may already have removed the companion. Conservatively restore it without
// touching the main file, since a failed close leaves native ownership uncertain.
try {
markDirty(dirtyPath);
} catch (dirtyError) {
throw new AggregateError(
[error, dirtyError],
`Failed to close the lock and restore its dirty marker: ${filePath}`
);
}
throw error;
}
heldDescriptor = undefined;
}
}
};
descriptor = undefined;
return { fileWriter, filePath, dirtyWhenAcquired };
} finally {
if (descriptor !== undefined) fs.closeSync(descriptor);
}
}
function hasExclusiveSharing(filePath: string): boolean {
let sharedDescriptor: number | undefined;
try {
sharedDescriptor = fs.openSync(filePath, fs.constants.O_RDONLY);
} catch (error) {
// libuv maps ERROR_SHARING_VIOLATION to EBUSY. Permission errors do not prove exclusion.
if (hasCode(error, 'EBUSY')) return true;
throw error;
} finally {
if (sharedDescriptor !== undefined) fs.closeSync(sharedDescriptor);
}
return false;
}
function markDirty(dirtyPath: string): void {
try {
fs.writeFileSync(dirtyPath, '', { flag: 'wx', mode: 0o600 });
} catch (error) {
if (!hasCode(error, 'EEXIST')) throw error;
}
}
function hasCleanMarker(descriptor: number, size: bigint): boolean {
if (size !== BigInt(CLEAN_MARKER.length)) return false;
const contents: Buffer = Buffer.alloc(CLEAN_MARKER.length);
return (
fs.readSync(descriptor, contents, 0, contents.length, 0) === contents.length &&
contents.equals(CLEAN_MARKER)
);
}