dsh-delete-guard 2.4.1 → 2.4.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +174 -0
- package/README.en.md +27 -31
- package/README.ja.md +16 -16
- package/README.md +35 -39
- package/SECURITY.md +87 -0
- package/lib/guard.js +204 -19
- package/lib/index.js +90 -0
- package/lib/rewrite-guard.js +313 -30
- package/lib/staging.js +9 -1
- package/package.json +1 -1
- package/scripts/recycle-bin.ps1 +185 -6
- package/scripts/verify-readme-structure.mjs +90 -0
package/scripts/recycle-bin.ps1
CHANGED
|
@@ -87,9 +87,33 @@ function Write-Json {
|
|
|
87
87
|
}
|
|
88
88
|
|
|
89
89
|
function Resolve-NormalizedPath {
|
|
90
|
-
<#
|
|
90
|
+
<#
|
|
91
|
+
Resolve to an absolute path without requiring existence, and drop a literal
|
|
92
|
+
`\\?\` prefix.
|
|
93
|
+
|
|
94
|
+
The prefix has one legitimate use — naming a path longer than MAX_PATH, or one
|
|
95
|
+
whose name Win32 would otherwise trim — and callers do hand it over. But
|
|
96
|
+
SHFileOperationW does not accept it: measured 2026-11-25, a `\\?\`-prefixed
|
|
97
|
+
file returns 0x7C (DE_INVALIDFILES) where the same file without the prefix
|
|
98
|
+
recycles. And a path over 260 characters recycles fine *without* the prefix, so
|
|
99
|
+
stripping loses nothing.
|
|
100
|
+
|
|
101
|
+
Left in place, the prefix was worse than useless: Test-RecyclableVolume matched
|
|
102
|
+
`\\*` and reported "a network location has no Recycle Bin" for a local path.
|
|
103
|
+
|
|
104
|
+
`\\?\UNC\host\share\x` becomes the ordinary UNC spelling, which is then judged
|
|
105
|
+
as the network location it is.
|
|
106
|
+
#>
|
|
91
107
|
param([Parameter(Mandatory)][string] $Path)
|
|
92
|
-
|
|
108
|
+
|
|
109
|
+
$text = $Path
|
|
110
|
+
if ($text.StartsWith('\\?\UNC\', [System.StringComparison]::OrdinalIgnoreCase)) {
|
|
111
|
+
$text = '\\' + $text.Substring(8)
|
|
112
|
+
} elseif ($text.StartsWith('\\?\', [System.StringComparison]::Ordinal)) {
|
|
113
|
+
$text = $text.Substring(4)
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
$expanded = [Environment]::ExpandEnvironmentVariables($text)
|
|
93
117
|
$full = [System.IO.Path]::GetFullPath($expanded)
|
|
94
118
|
# GetFullPath keeps a trailing separator on directories; SHFileOperationW
|
|
95
119
|
# requires a double-null terminated list and tolerates either form, but a
|
|
@@ -112,13 +136,22 @@ function Get-DriveRoot {
|
|
|
112
136
|
# --- Operations --------------------------------------------------------------
|
|
113
137
|
|
|
114
138
|
function Get-ShFileOperationMessage {
|
|
115
|
-
<#
|
|
139
|
+
<#
|
|
140
|
+
Map the documented SHFileOperation error codes to something readable.
|
|
141
|
+
|
|
142
|
+
0x2 is deliberately **not** "the path no longer exists": measured 2026-11-25, a
|
|
143
|
+
directory whose contents include a name Win32 cannot address returns 0x2 while
|
|
144
|
+
the path is right there. The code means "one of the files it needed was not
|
|
145
|
+
found", and Get-FailureDiagnosis appends which one. The wording below says only
|
|
146
|
+
what the code supports.
|
|
147
|
+
#>
|
|
116
148
|
param([Parameter(Mandatory)][int] $Code)
|
|
117
149
|
# A bare `switch` statement with explicit `return`s: `return switch (...) { ... }`
|
|
118
150
|
# is a parse error on this pwsh build ("unexpected token '{'").
|
|
119
151
|
switch ($Code) {
|
|
120
|
-
0x2 { return '
|
|
121
|
-
0x3 { return '
|
|
152
|
+
0x2 { return 'A file in that path could not be found.' }
|
|
153
|
+
0x3 { return 'A directory in that path could not be found.' }
|
|
154
|
+
0x20 { return 'Access is denied.' }
|
|
122
155
|
0x71 { return 'The source and destination are the same.' }
|
|
123
156
|
0x72 { return 'The operation was cancelled.' }
|
|
124
157
|
0x74 { return 'The path is too long.' }
|
|
@@ -127,6 +160,7 @@ function Get-ShFileOperationMessage {
|
|
|
127
160
|
0x78 { return 'Access was denied.' }
|
|
128
161
|
0x79 { return 'The operation was aborted.' }
|
|
129
162
|
0x7A { return 'The source is in use by another process.' }
|
|
163
|
+
0x7B { return 'The path is invalid.' }
|
|
130
164
|
0x7C { return 'The path is invalid.' }
|
|
131
165
|
default { return ("SHFileOperationW failed with code 0x{0:X}" -f $Code) }
|
|
132
166
|
}
|
|
@@ -161,6 +195,146 @@ function Test-RecyclableVolume {
|
|
|
161
195
|
return "Volume $root has no Recycle Bin (file system: $shown), so recycling here would erase the item permanently. Move it to an NTFS volume first, or delete it from Explorer where Windows can warn the user."
|
|
162
196
|
}
|
|
163
197
|
|
|
198
|
+
function Get-NamesWin32CannotAddress {
|
|
199
|
+
<#
|
|
200
|
+
Find entries whose name Windows will not resolve when handed as a path.
|
|
201
|
+
|
|
202
|
+
A name ending in a space or a dot can exist on NTFS — WSL, a zip extractor, a
|
|
203
|
+
sync client or a port of a POSIX tool can create it — but Win32 trims those
|
|
204
|
+
trailing characters when the name is written into a path, so `Open` looks for a
|
|
205
|
+
name that is not there. That is why a directory holding such a child cannot be
|
|
206
|
+
recycled as a whole: SHFileOperationW walks the tree, the child's own open fails
|
|
207
|
+
with ERROR_FILE_NOT_FOUND, and the whole operation returns 0x2 even though the
|
|
208
|
+
path plainly exists.
|
|
209
|
+
|
|
210
|
+
The listing itself has to go through `\\?\` — that prefix is the only spelling in
|
|
211
|
+
which the name is taken literally. The prefix is used *here and only here*, to
|
|
212
|
+
read names; SHFileOperationW is never handed one (it returns 0x7C for those).
|
|
213
|
+
|
|
214
|
+
Returns up to `Limit` offending entries as `relative/path` strings, each with a
|
|
215
|
+
reason, so the message can name them.
|
|
216
|
+
#>
|
|
217
|
+
param(
|
|
218
|
+
[Parameter(Mandatory)][string] $Path,
|
|
219
|
+
[int] $Limit = 8
|
|
220
|
+
)
|
|
221
|
+
|
|
222
|
+
if (-not (Test-Path -LiteralPath $Path -PathType Container)) { return @() }
|
|
223
|
+
|
|
224
|
+
$found = [System.Collections.Generic.List[object]]::new()
|
|
225
|
+
$queue = [System.Collections.Generic.Queue[string]]::new()
|
|
226
|
+
$queue.Enqueue($Path)
|
|
227
|
+
|
|
228
|
+
while ($queue.Count -gt 0 -and $found.Count -lt $Limit) {
|
|
229
|
+
$current = $queue.Dequeue()
|
|
230
|
+
|
|
231
|
+
# Listing through the literal prefix, so a malformed name is returned as itself
|
|
232
|
+
# instead of being silently trimmed to a sibling that may not exist.
|
|
233
|
+
$literal = if ($current.StartsWith('\\')) { '\\?\UNC\' + $current.Substring(2) } else { '\\?\' + $current }
|
|
234
|
+
$entries = $null
|
|
235
|
+
try {
|
|
236
|
+
$entries = [System.IO.Directory]::GetFileSystemEntries($literal)
|
|
237
|
+
} catch {
|
|
238
|
+
# A tree we cannot enumerate is not this diagnostic's problem; the caller
|
|
239
|
+
# still reports the original failure.
|
|
240
|
+
return $found
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
foreach ($entry in $entries) {
|
|
244
|
+
if ($found.Count -ge $Limit) { break }
|
|
245
|
+
|
|
246
|
+
# The literal listing returns names with the prefix attached.
|
|
247
|
+
$name = [System.IO.Path]::GetFileName($entry)
|
|
248
|
+
$reason = $null
|
|
249
|
+
if ($name.EndsWith(' ')) { $reason = 'ends with a space' }
|
|
250
|
+
elseif ($name.EndsWith('.')) { $reason = 'ends with a dot' }
|
|
251
|
+
elseif ($name -match '[\x00-\x1F]') { $reason = 'contains a control character' }
|
|
252
|
+
|
|
253
|
+
if ($null -ne $reason) {
|
|
254
|
+
$relative = $entry
|
|
255
|
+
foreach ($prefix in @('\\?\UNC\', '\\?\')) {
|
|
256
|
+
if ($relative.StartsWith($prefix, [System.StringComparison]::Ordinal)) {
|
|
257
|
+
$relative = $relative.Substring($prefix.Length)
|
|
258
|
+
break
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
$found.Add([pscustomobject]@{ path = $relative; reason = $reason })
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
$isDirectory = $false
|
|
265
|
+
try { $isDirectory = [System.IO.Directory]::Exists($entry) } catch { $isDirectory = $false }
|
|
266
|
+
if ($isDirectory -and -not ($name.EndsWith(' ') -or $name.EndsWith('.'))) {
|
|
267
|
+
$queue.Enqueue($entry)
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
return $found.ToArray()
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
function Get-FailureDiagnosis {
|
|
276
|
+
<#
|
|
277
|
+
Turn a bare SHFileOperation failure into something a caller can act on.
|
|
278
|
+
|
|
279
|
+
Measured 2026-11-25. Two failures are worth diagnosing, because both used to
|
|
280
|
+
produce a message that pointed the caller at the wrong thing:
|
|
281
|
+
|
|
282
|
+
0x2 DE_FILENOTFOUND — the message said "the path no longer exists" while the
|
|
283
|
+
path was right there. The real cause was a child whose name Win32 cannot
|
|
284
|
+
address (trims a trailing space or dot), so the tree could not be walked.
|
|
285
|
+
0x20 DE_ACCESSDENIED — a directory held as some process's working directory, or
|
|
286
|
+
an open file inside it. Every file inside can be deleted; the directory
|
|
287
|
+
itself cannot, and the shell reports access denied with no hint of who holds
|
|
288
|
+
it. Enumerating handles needs elevation, so this reports what is observable
|
|
289
|
+
and says so rather than guessing a PID.
|
|
290
|
+
|
|
291
|
+
Returns extra text to append to the message, or $null.
|
|
292
|
+
#>
|
|
293
|
+
param(
|
|
294
|
+
[Parameter(Mandatory)][int] $Code,
|
|
295
|
+
[Parameter(Mandatory)][string] $Path
|
|
296
|
+
)
|
|
297
|
+
|
|
298
|
+
$exists = $false
|
|
299
|
+
try { $exists = Test-Path -LiteralPath $Path } catch { $exists = $false }
|
|
300
|
+
|
|
301
|
+
if ($Code -eq 0x2) {
|
|
302
|
+
if (-not $exists) {
|
|
303
|
+
return 'The path is not there now — something else removed it between the check and the call.'
|
|
304
|
+
}
|
|
305
|
+
$bad = Get-NamesWin32CannotAddress -Path $Path
|
|
306
|
+
if ($bad.Count -eq 0) {
|
|
307
|
+
return $null
|
|
308
|
+
}
|
|
309
|
+
$shown = ($bad | ForEach-Object { "$($_.path) ($($_.reason))" }) -join '; '
|
|
310
|
+
$more = if ($bad.Count -ge 8) { ' (listing stopped at 8)' } else { '' }
|
|
311
|
+
# One expression, not several lines joined with `+`: a `return` ends the
|
|
312
|
+
# statement at the line break, so a trailing `+` continuation would return
|
|
313
|
+
# only the first fragment and silently drop the rest.
|
|
314
|
+
$text = "The path exists, but it contains entries Windows cannot open by name: $shown$more. "
|
|
315
|
+
$text += 'Win32 trims a trailing space or dot when a name is written into a path, so the tree cannot be walked. '
|
|
316
|
+
$text += 'Renaming those entries to names without a trailing space or dot makes the directory recyclable; '
|
|
317
|
+
$text += 'they are usually left by WSL, an archive extractor or a sync client. '
|
|
318
|
+
$text += 'This tool does not rename or delete inside a directory it was asked to delete.'
|
|
319
|
+
return $text
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
if ($Code -eq 0x20 -or $Code -eq 0x78 -or $Code -eq 0x7A) {
|
|
323
|
+
if (-not $exists) { return $null }
|
|
324
|
+
# The base message already says "access is denied"; start from the meaning.
|
|
325
|
+
$text = 'For a directory this usually means a process is using it: its working directory is set '
|
|
326
|
+
$text += 'to this path, or it holds one of the files inside open. Every file inside can still be '
|
|
327
|
+
$text += 'deleted individually; the directory itself cannot until that process lets go. '
|
|
328
|
+
$text += 'This tool does not enumerate which process holds it — that needs elevation or handle-scanning, '
|
|
329
|
+
$text += 'and it will not guess. If you need the holder, check for a process started in this directory '
|
|
330
|
+
$text += '(a Python environment, a build or a shell whose cwd is here), or retry after it exits.'
|
|
331
|
+
return $text
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
return $null
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
|
|
164
338
|
function Invoke-RecycleOne {
|
|
165
339
|
<#
|
|
166
340
|
Recycle a single path.
|
|
@@ -190,7 +364,12 @@ function Invoke-RecycleOne {
|
|
|
190
364
|
)
|
|
191
365
|
|
|
192
366
|
$rc = [RecycleBinNative]::SHFileOperationW([ref]$op)
|
|
193
|
-
if ($rc -ne 0) {
|
|
367
|
+
if ($rc -ne 0) {
|
|
368
|
+
$message = Get-ShFileOperationMessage -Code $rc
|
|
369
|
+
$diagnosis = Get-FailureDiagnosis -Code $rc -Path $Path
|
|
370
|
+
if ($null -ne $diagnosis) { return "$message $diagnosis" }
|
|
371
|
+
return $message
|
|
372
|
+
}
|
|
194
373
|
if ($op.fAnyOperationsAborted) { return 'The shell aborted the move to the Recycle Bin.' }
|
|
195
374
|
# The call reports success for the whole request; confirm this path actually left.
|
|
196
375
|
if (Test-Path -LiteralPath $Path) { return 'The path still exists after the recycle operation.' }
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Structural check for the three READMEs.
|
|
3
|
+
*
|
|
4
|
+
* `README.md` / `README.en.md` / `README.ja.md` are the same document in three
|
|
5
|
+
* languages. Editing one and forgetting another does not show up in a diff — the
|
|
6
|
+
* wording differs anyway — so the check is on structure: the same sections, the same
|
|
7
|
+
* number of table rows, one H1.
|
|
8
|
+
*
|
|
9
|
+
* This exists because a previous README rewrite silently duplicated each file inside
|
|
10
|
+
* itself (a `String.replace` replacement string contained `$`, which JavaScript
|
|
11
|
+
* expanded). Counting H1s was what found it; the numbers below are what the fix has
|
|
12
|
+
* to keep matching.
|
|
13
|
+
*
|
|
14
|
+
* Run: node scripts/verify-readme-structure.mjs
|
|
15
|
+
* Exit code 1 when the three files disagree or a file has more than one H1.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import { readFileSync } from 'node:fs';
|
|
19
|
+
import { dirname, join } from 'node:path';
|
|
20
|
+
import { fileURLToPath } from 'node:url';
|
|
21
|
+
|
|
22
|
+
const ROOT = join(dirname(fileURLToPath(import.meta.url)), '..');
|
|
23
|
+
const FILES = ['README.md', 'README.en.md', 'README.ja.md'];
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Measure the structural facts that must agree across the three files.
|
|
27
|
+
*
|
|
28
|
+
* Headings are counted **outside code fences only**: a Markdown block that shows a
|
|
29
|
+
* shell command can contain `# comment` lines, and an early version of this script
|
|
30
|
+
* reported five H1s because of them. Counting inside fences would flag every README
|
|
31
|
+
* that documents a PowerShell snippet.
|
|
32
|
+
*
|
|
33
|
+
* @param text - the file's contents.
|
|
34
|
+
* @returns the counts and the section headings, in order.
|
|
35
|
+
*/
|
|
36
|
+
function measure(text) {
|
|
37
|
+
const lines = text.split(/\r?\n/);
|
|
38
|
+
let inFence = false;
|
|
39
|
+
let h1 = 0;
|
|
40
|
+
const h2 = [];
|
|
41
|
+
let tableRows = 0;
|
|
42
|
+
let codeFences = 0;
|
|
43
|
+
|
|
44
|
+
for (const line of lines) {
|
|
45
|
+
if (/^```/.test(line)) {
|
|
46
|
+
inFence = !inFence;
|
|
47
|
+
codeFences += 1;
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
if (inFence) continue;
|
|
51
|
+
if (/^# /.test(line)) h1 += 1;
|
|
52
|
+
const section = /^## (.+)$/.exec(line);
|
|
53
|
+
if (section !== null) h2.push(section[1].trim());
|
|
54
|
+
if (/^\| /.test(line)) tableRows += 1;
|
|
55
|
+
}
|
|
56
|
+
return { h1, h2Count: h2.length, h2, tableRows, codeFences };
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const measured = FILES.map((name) => ({ name, ...measure(readFileSync(join(ROOT, name), 'utf8')) }));
|
|
60
|
+
|
|
61
|
+
let failed = false;
|
|
62
|
+
const base = measured[0];
|
|
63
|
+
|
|
64
|
+
for (const m of measured) {
|
|
65
|
+
const problems = [];
|
|
66
|
+
if (m.h1 !== 1) problems.push(`H1 count is ${m.h1}, expected 1`);
|
|
67
|
+
if (m.h2Count !== base.h2Count) problems.push(`H2 count ${m.h2Count}, ${base.name} has ${base.h2Count}`);
|
|
68
|
+
if (m.tableRows !== base.tableRows) problems.push(`table rows ${m.tableRows}, ${base.name} has ${base.tableRows}`);
|
|
69
|
+
if (m.codeFences % 2 !== 0) problems.push(`odd number of code fences (${m.codeFences}) — an unclosed block`);
|
|
70
|
+
|
|
71
|
+
const status = problems.length === 0 ? 'ok ' : 'FAIL';
|
|
72
|
+
if (problems.length > 0) failed = true;
|
|
73
|
+
console.log(`${status} ${m.name.padEnd(14)} H1=${m.h1} H2=${m.h2Count} rows=${m.tableRows} fences=${m.codeFences}`);
|
|
74
|
+
for (const p of problems) console.log(` ${p}`);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// Section titles differ by language, so only the position and count are comparable.
|
|
78
|
+
// Print them side by side anyway: a missing section is easier to spot by name.
|
|
79
|
+
const width = Math.max(...measured.map((m) => m.h2.length));
|
|
80
|
+
console.log('\nsections by position:');
|
|
81
|
+
for (let i = 0; i < width; i += 1) {
|
|
82
|
+
const row = measured.map((m) => (m.h2[i] ?? '(none)').slice(0, 34).padEnd(36)).join('');
|
|
83
|
+
console.log(' ' + row);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if (failed) {
|
|
87
|
+
console.error('\nREADME structure differs between languages, or a file has a stray H1.');
|
|
88
|
+
process.exit(1);
|
|
89
|
+
}
|
|
90
|
+
console.log('\nthree READMEs are structurally identical');
|