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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
//! Which file a `Command` loads — on the spawn paths that currently consult it.
//!
//! **Coverage today: only the Windows raw `CreateProcessW` backend (sync and its tokio mirror),
//! and only for a `Search` program** — i.e. one recorded by `Command::executable()`, or the
//! argv[0]/first-token fallback when neither setter was called. That backend is reached when
//! `Command::executable_path()` is set or an fd >= 3 is mapped.
//!
//! An `Exact` program, from `Command::raw_executable()`, deliberately does NOT come through
//! [`resolve`]: "load exactly this file" is the absence of this module's SEARCH policy, not an
//! application of it. Where a sink would search a relative name, it is put in a form no sink
//! searches: on the unelevated POSIX spawn and the CLI elevation backends a `./`-anchored name
//! ([`exact::anchor_posix`]), under `osascript` an absolute path ([`exact::complete_posix`]), and on the Windows
//! elevated path an absolute path from `windows_raw::resolve::absolutise_exact` — see its doc for
//! why `ShellExecuteEx` forces that step where `CreateProcessW` does not.
//!
//! It does, however, share this module's naming CLASSIFIERS: every `Exact` arm refuses a program
//! that names no file via [`names_no_file`], so the two axes cannot drift apart on what counts as
//! a filename. Classifying is not searching.
//!
//! Every other spawn path resolves a `Search` program itself, ignorant of this module: POSIX
//! spawning calls `execvp`/`posix_spawn`'s own PATH search, and the Windows elevated
//! (`ShellExecuteEx`) path passes a `Search` `lpFile` through unresolved. `src/lib.rs`'s
//! `#[cfg_attr(not(windows), allow(dead_code, reason = "..."))]` on this module tracks exactly
//! that: the `allow` goes away once those paths route through [`resolve`] too.
//! Producing an ABSOLUTE path is what would let a backend skip its own search once it is wired
//! up — `execvp` does not search a name containing a separator, and `ShellExecuteEx` skips its
//! directory search for an absolute `lpFile` (though not `PATHEXT`; see
//! [`reject_unloadable_image`]) — but that wiring has not happened yet for either.
//!
//! Classification is byte-level and parameterised by [`ResolveInput::windows`] rather than using
//! `std::path`, whose parsing is host-specific — `Path::new("C:tool").prefix()` is `None` off
//! Windows, so a `Path`-based rule could not be exercised from a POSIX host at all.
//!
//! A bare name is resolved from [`ResolveInput::system_dirs`] (Windows only) and then `PATH` —
//! **never** the current directory, in either sense of that phrase: bare-name resolution never
//! reads [`ResolveInput::cwd`] (the CHILD's working directory) at all, a structural guarantee of
//! this module, and never reaches THIS PROCESS's own cwd either, because [`accepted`] rejects any
//! joined candidate that is not fully qualified, so a relative `PATH` element (which would resolve
//! against this process's cwd if followed) never produces a match. And, in practice, never the app
//! directory (the directory this process's own image loaded from) either: `system_dirs` is an
//! arbitrary caller-supplied closure, so this module CAN be handed the app directory (the test
//! suite does exactly that as a positive control), but the one production caller,
//! `windows_raw::resolve::windows_system_dirs`, never includes it. `system_dirs` is likewise taken
//! as a parameter rather than queried from the OS here, for the
//! same host-independence reason; see its doc for what it contains and why it precedes `PATH`.
//!
//! # Which error kind
//!
//! The two kinds [`resolve`] returns answer different questions, and the split is part of the
//! public contract (see [`crate::Command::executable`]):
//!
//! - [`std::io::ErrorKind::InvalidInput`] — the string was NOT ACCEPTED. It was refused on its
//! shape; no search ran, and no filesystem result is being reported.
//! - [`std::io::ErrorKind::NotFound`] — the string was acceptable, the search ran to this
//! module's policy, and nothing matched.
//!
//! The operational test for a new rule is **could a different filesystem make this input
//! succeed?** No — the refusal is a property of the string, not of the disk — means
//! `InvalidInput`. Yes means `NotFound`. So `C:tool` (relative to a drive's own current
//! directory, which cosca does not track) and `C:\` (a directory, whatever is on the disk) are
//! refusals, while a missing `tool` or `C:\abs\missing.exe` is a miss. A refusal must be stated
//! explicitly and early, never left to fall out of candidate filtering: that reports the right
//! kind by accident and changes it silently when the filtering does.
use crateError;
use OsStr;
use ;
pub
pub
/// Everything the policy reads. Taken as parameters so the rules are testable without touching
/// the ambient environment.
pub
/// [`ResolveInput::normalise`] for a test that probes every candidate as joined.
pub
/// [`ResolveInput::system_dirs`] for a test with no system directories to search — `PATH` only.
pub
/// How the program names its file, which decides whether `PATH` (and, on Windows,
/// `system_dirs`) is consulted at all.
pub
/// How Win32 reads a path, decided as `RtlDetermineDosPathNameType_U` decides it: separators
/// first, then a drive. Every Windows-grammar classifier here, and the raw backend's completion
/// of a `raw_executable()` token or `current_dir`, reads a path through this one function, so no
/// two of them can disagree on the same string.
pub
/// `program`'s Win32 path type. See [`PathType`].
pub
/// The byte length of a drive prefix — any ONE UTF-16 unit, then `:` — or `None`. Win32 takes any
/// unit there, not only a letter (`RtlDetermineDosPathNameType_U` tests `path[1] == ':'`), so
/// `1:tool` names drive `1`. Byte-level over the WTF-8 encoding: a lead byte of up to three bytes
/// is one unit, a four-byte one (a supplementary character) is two.
///
/// Not the whole story on its own: [`path_type`] checks separators first, so `\:x` is rooted.
pub
/// `program` split after its drive (`C:`, any one unit and `:`), for a drive-absolute or
/// drive-relative path; `None` for any other type.
pub
pub
/// Whether [`resolve`] reads [`ResolveInput::cwd`] for `program`: exactly for a relative located
/// name. A bare name is searched, an absolute one is its own location, and a drive-relative or
/// share-less UNC-shaped name is refused before any base is read. A caller that supplies the base
/// itself asks this to know whether it must.
pub
/// Whether Win32 reads `program` as UNC — it starts with two separators, either kind — while no
/// UNC prefix parses from it, because the share is missing or empty (`\\tool.exe`, `//tool.exe`,
/// `\\srv\\x.exe`). Windows only.
/// Whether `program` is ABSOLUTE in the simulated platform's grammar: on POSIX a leading `/`; on
/// Windows a drive and a root (`C:\`), or a UNC, verbatim or device prefix (`\\server\share`,
/// `\\?\`, `\\.\`). A rooted `\tool` and a drive-relative `C:tool` are not: each still
/// needs a current directory.
pub
/// Whether `bytes` is a verbatim UNC prefix (`\\?\UNC\`) missing its server or share, which
/// names no share as the plain `\\srv` does. [`windows_prefix_len`] counts such a prefix whole,
/// since nothing may be appended to it either way.
///
/// Split on `\` alone, as [`join`] parses a verbatim prefix and NT reads one, so the path this
/// admits is the one the join completes: `\\?\UNC\srv/shr` is server `srv/shr` with no share,
/// and `\\?\UNC/srv` is no UNC path at all but the namespace `UNC/srv`. std's `parse_prefix`
/// reads the latter as a share-less UNC path, since it rewrites `/` to `\` in the first eight bytes.
/// An ASCII-letter drive, for the one place Win32 does not parse the path at all: after a `\\?\`
/// marker, where `std` and `PureWindowsPath` recognise only a letter. Everywhere else a drive is
/// [`drive_len`]'s.
/// Whether `program` is DRIVE-RELATIVE: a drive prefix not followed by a separator, as in
/// `C:tool` or `D:sub\x`. Such a name is relative to that drive's own current directory — state
/// cosca does not track, so no filesystem can make it resolve. Refused, never searched; see the
/// module doc's error-kind rule.
/// The length of the Windows PREFIX — the leading run naming a volume, share, device or verbatim
/// namespace rather than anything inside it: `C:`, `\\server\share`, `\\?\C:`,
/// `\\?\UNC\server\share`, `\\?\namespace`, `\\.\device`. `0` when there is none.
///
/// A prefix's own components are not filenames, so nothing may be appended to one: `\\server\share`
/// is a share root exactly as `C:\` is a volume root. `Path::file_name` agrees — it is `None` for
/// every prefix-only path — but only on a Windows HOST, which is why this is parsed byte-wise here.
///
/// `/` and `\` separate components here exactly as they do in [`final_component`], so the whole
/// string is read under ONE separator rule. `std` is deliberately not followed on this point:
/// `parse_next_component(.., verbatim: true)` splits the components of a verbatim prefix on `\`
/// alone, which would let the prefix swallow `srv/shr\a` in `\\?\UNC\srv/shr\a` and leave an empty
/// final component — refusing a path that names the file `a`. `PureWindowsPath` is the reference
/// (see [`names_no_file`]) and it normalises `/` to `\` everywhere.
///
/// Two `std` rules ARE followed, because they decide whether a prefix is present rather than how
/// its components are split: the `\\?\` marker must be spelt with literal backslashes (a `/` among
/// those four bytes means no verbatim prefix), and `\\server` with no share is no prefix at all.
///
/// The `UNC` marker is NT's, not `std`'s: it marks a UNC path only before `\`, since NT does not
/// read `/` as a separator after `\\?\`. So `\\?\UNC/srv\tool.exe` is the namespace `\\?\UNC`
/// holding `srv\tool.exe`, not a share. `std` rewrites `/` to `\` in the first eight bytes and would
/// read a share there. The share-less gate and [`join`] read the marker the same way.
/// Whether `program` names a DIRECTORY rather than a file, so no search could make it executable.
///
/// True when the final component — taken after the Windows prefix, if any — is empty (a
/// separator-terminated name, a root, a bare prefix such as `\\server\share`, or the empty
/// string) or is `.`/`..`. NOTHING ELSE, on either platform.
///
/// On what counts as a NAME, `PureWindowsPath` agrees: `C:\dir\...` is named `...`, `C:\dir\tool.`
/// is named `tool.`, a lone space is a name. Win32's trimming of trailing dots and spaces is not
/// modelled, because it describes what an API does to a path STRING on its way in and not what may
/// exist on disk — Microsoft's own rule concedes a trailing-space name can be created, so trimming
/// here would refuse names that name real files.
///
/// It is not the authority on what is REFUSED, because it normalises the string first, and each of
/// the three shapes has inputs where that shows: it collapses `C:\dir\y\` to `y`, it drops the `.`
/// in `C:\dir\.` to give `dir`, and it reports `..` as an ordinary name. This module reads the RAW
/// string, where each of those final components survives, and refuses all three. (On the commonest
/// spellings it does agree — a lone `.`, a root, a bare prefix and `""` are all nameless there
/// too; it is the longer forms that diverge.)
///
/// `Path::file_name` answers the same question, but host-specifically: off Windows it sees neither
/// `\` nor any prefix, so a `Path`-based rule could not be exercised from a POSIX host at all.
/// Byte-level and parameterised, like every other classifier here — see the module doc.
///
/// `pub(crate)` for `windows_raw::resolve::reject_unnameable_program`: the `Exact` arms refuse the
/// same shapes, and sharing the predicate is what stops the two axes drifting apart on what counts
/// as a filename.
pub
/// The run after the last separator, with any Windows prefix taken off first. Shared so that
/// "which component does the rule read" has ONE answer: `names_no_file` classifies on it and
/// [`takes_the_exe_fallback`] tests its extension, and a separator set or prefix rule that changed
/// in only one of them would leave the two judging different bytes.
///
/// BOTH halves are gated on `windows`, not just the prefix: off Windows `\` is an ordinary
/// filename character, so `final_component(r"C:\dir\tool", false)` is the whole string. Byte-level
/// and parameterised, like every other classifier here — see the module doc.
/// The filenames tried in each candidate directory, in order.
///
/// **`.exe` is a property of names that get SEARCHED, not of files that get LOADED.** That split
/// is what the two shapes encode, and it is the platform's own, not this crate's invention:
///
/// - The PE/COFF format makes no extension normative — an image is identified by `MZ`, the `PE\0\0`
/// signature at the offset in `0x3c`, and header flags such as `IMAGE_FILE_DLL`. A valid PE
/// named `myapp` or `payload.tmp` is a valid PE.
/// - `CreateProcessW` documents, for the `lpApplicationName` this resolver actually feeds: "This
/// parameter must include the file name extension; **no default extension is assumed**." Its
/// command-line mode agrees the moment a path is involved: ".exe is appended" only "if the file
/// name does not contain a directory path".
/// - `PATHEXT` is a `cmd.exe` feature, and it cannot express "no extension" — there is no entry
/// meaning "try the bare name". That, not the loader, is why an extensionless image is
/// unreachable by bare name.
/// - Microsoft's current app model draws the same line: `uap5:ExecutionAlias`/`uap8:ExecutionAlias`
/// require the `Alias` — the name a user TYPES — to end in `.exe`, while the manifest's
/// `Executable=` path, which names the file, carries no such constraint.
///
/// # The rule
///
/// - POSIX (`windows` is `false`): `name` unchanged, always, either shape. POSIX has no
/// loader-level notion of an "executable extension" for this crate to reproduce.
/// - Windows, final path component already ending in `.exe`/`.com` (compared CASE-INSENSITIVELY,
/// so `TOOL.EXE` is never doubled into `TOOL.EXE.exe`): `name` unchanged, either shape. There is
/// nothing to append that would not name a file the caller did not write.
/// - Windows, [`Shape::BareName`]: `name` + `.exe`, and ONLY that, appended to the name AS
/// WRITTEN. `tool.` yields `tool..exe` and `...` yields `....exe` — odd-looking, but the one
/// rule unbranched, and appending to anything but what the caller wrote would search for a file
/// they did not name. See the monotonicity note, which counts this as a widening.
/// - Windows, [`Shape::Located`]: `name` first, always. `name` + `.exe` follows ONLY when the
/// name carries no extension at all AND names a file — see [`takes_the_exe_fallback`], which is
/// what keeps this axis from widening against `main`.
///
/// # Why `Located` tries the exact name first
///
/// A located name visits exactly one directory: the one the caller named. There is no search to
/// bias, so the ordering answers only "did the caller mean this file?" — and they wrote it, so yes.
/// Refusing it would make `executable()` unable to name a perfectly loadable image (a staged
/// `payload.tmp`, a content-addressed cache entry, a `.scr`, anything named by hash), a restriction
/// neither the format, the loader, nor the app model imposes.
///
/// The `.exe` fallback stays because it is the reason the rule exists at all: someone writing
/// `executable("bin/my-program")` for code that builds on both platforms must not be forced to
/// append `.exe` conditionally themselves.
///
/// One consequence, stated plainly: where both `bin/tool` and `bin/tool.exe` exist, the
/// extensionless one wins. A writer who can create files in that directory but not overwrite a
/// locked, running `tool.exe` can therefore decide the outcome. This is the ordering `main`
/// shipped, kept deliberately — the alternative loses the ability to name an exact file, which is
/// the whole point of the located axis.
///
/// # Why `.exe` *and* `.com`
///
/// `.exe` and `.com` are exactly the two extensions `CreateProcessW` loads directly as a PE image.
/// A `.com` file on a modern Windows install (`more.com`, `chcp.com`, `tree.com`, all shipped in
/// `System32`) is an ordinary PE whose extension is cosmetic; leaving `.com` off the allowlist
/// would turn `args(["more.com"])` into a search for the nonexistent `more.com.exe`, breaking
/// resolution of a name the system-directory search makes work today.
///
/// Script extensions (`.bat`/`.cmd`) are deliberately NOT in the allowlist — but NOT because this
/// crate treats a script as an illegitimate target. Resolving a bare name to a script is a
/// SEPARATE, CURRENTLY UNIMPLEMENTED feature: batch support needs its own `cmd.exe`
/// metacharacter quoter (alongside the existing MSVCRT one) plus PATHEXT-based resolution, and is
/// planned as its own follow-up PR. This is unrelated to — and does not weaken — this crate's
/// existing, separate batch-path rejection (see `reject_batch_path`'s own message: "cmd.exe batch
/// escaping is not implemented (CVE-2024-24576)").
///
/// # Monotonicity of the BARE-name rule — stated honestly, in both directions
///
/// This section justifies the single candidate for [`Shape::BareName`] ONLY. It is measured on the
/// surfaces that resolve a bare name, and deliberately does not reach the located axis above.
///
/// Measured on real Windows CI (amd64 and arm64 alike): `CreateProcessW` with a NULL
/// `lpApplicationName`, and `cmd.exe`, `pwsh` 7, and Windows PowerShell 5.1 alike, all refuse to
/// run an extensionless PE by bare name — a directory holding only `tool` (no extension) yields
/// `ERROR_FILE_NOT_FOUND`/"not recognized" from every one of them. So this NARROWS for a bare
/// extensionless name: the old rule tried `tool.exe` then `tool`, and that second candidate never
/// actually ran on any of those four surfaces — dropping it removes a candidate nothing on the
/// platform could launch anyway, and removes the hazard of an ambient extensionless file winning
/// in some directory the search visits.
///
/// Those same three shells (unlike `CreateProcessW` itself) resolve a bare DOTTED name via
/// PATHEXT — typing `foo.bar` runs `foo.bar.exe` when that file exists — while the OLD has-a-dot
/// heuristic here refused to append `.exe` to anything already containing a `.`, so `python3.11`
/// could never resolve even though every one of those shells finds `python3.11.exe`. So this rule
/// also WIDENS for a dotted bare name: it now appends `.exe` where it used to leave the name alone.
/// That widening is deliberate and matches the three shells, measured — it does NOT match
/// `CreateProcessW`'s own NULL-`lpApplicationName` behaviour, and is not meant to: that parity is
/// already given up on for `.bat`/`.cmd`, above.
///
/// Membership in that widening set is `Path::extension().is_some()` and nothing else — never a
/// judgement about where the dot sits. `tool.`, `...`, `.. ` and `tool. ` are all in it, and are
/// worth calling out because they are the plantable ones: `main` searched only for the literal
/// `tool.`, which Win32 opens as `tool`, whereas this rule searches for `tool..exe`, a name `main`
/// never looked for and a writer of any searched directory can create. Kept anyway, because
/// trimming is worse: it searches for `tool.exe`, a name the caller did not write EITHER, and buys
/// the refusal of `...` — which names a file, so refusing it reports `InvalidInput` for an input
/// some filesystem satisfies, breaking this module's own kind rule. The shells' PATHEXT behaviour
/// was measured on `foo.bar` and on none of these; that they fall under the same rule is an
/// argument from uniformity, not a measurement.
///
/// # Not a permanent rule
///
/// The one-candidate rule for a BARE name is expected to be superseded by proper PATHEXT-based
/// resolution once batch support lands (see the `.bat`/`.cmd` note above) — a future reader should
/// not assume it is permanent. The `Located` behaviour is expected to survive that change
/// unchanged, because PATHEXT is a search mechanism and the located axis does not search.
/// Whether a LOCATED name gets the second, `.exe` candidate.
///
/// Distinct from [`has_loadable_extension`], which asks the narrower "is it already `.exe`/`.com`".
/// This decides the fallback, and it exists to keep the located axis from WIDENING against `main`,
/// which keyed on `Path::extension().is_none()` and appended via `Path::with_extension`.
///
/// Two ways to get that wrong, both of which this rules out: a name whose final component already
/// carries an extension must not gain `name.exe` (`main` refused it), and a name that names no
/// file must not gain one either — there `.exe` becomes the whole final component, naming a
/// dotfile INSIDE the directory (`C:\tools\dir\` -> `C:\tools\dir\.exe`) or a sibling of it
/// (`C:\t\.` -> `C:\t\..exe`), either of which a writer of that directory can plant and which
/// `main` — whose `Path::with_extension` is a no-op on a path with no file name — never looked
/// for. That second question is [`names_no_file`]'s, and is asked by calling it rather than by
/// re-deriving it here, because the bare axis asks the same question and two spellings of one rule
/// drift apart.
///
/// [`resolve`] refuses such a name before reaching here, so this is defence in depth for any
/// future caller of this function that does not go through it.
/// Whether `name` already ends in `.exe` or `.com`, compared case-insensitively — `TOOL.EXE` must
/// not become `TOOL.EXE.exe`. These are the two extensions `CreateProcessW` loads directly as a PE
/// image; see `filename_candidates`'s doc for why exactly these two and no others.
///
/// Unlike its sibling classifiers this does NOT split off the final component first, and so takes
/// no `windows` flag. Neither suffix contains a separator, so for every input that REACHES here
/// the two readings coincide: both callers ([`resolve`] and [`reject_unloadable_image`]) refuse a
/// [`names_no_file`] name first, and only such a name can have its final component swallowed by a
/// prefix (`\\server\share.exe` ends in `.exe` while its final component is empty). A future
/// caller bypassing that refusal would get the whole-string answer; compare
/// `final_component(name, windows)` instead if it needs the other.
/// Refuse an image whose name does not end in `.exe`/`.com` — the ELEVATED path's allowlist.
///
/// `ShellExecuteEx` does not open `lpFile`, it RESOLVES it, and it applies `PATHEXT` even when
/// `lpFile` is absolute. Measured on both CI architectures and on a real desktop: `PATHEXT`
/// OUTRANKS AN EXISTING FILE, so an absolute `C:\dir\tool` with a planted `C:\dir\tool.bat`
/// beside it runs the batch file even when `tool` is a real PE. From an unelevated caller that
/// child runs at High integrity, and the consent dialog reads "Windows Command Processor",
/// Microsoft Corporation (verified) — the batch resolves to its signed handler, so the bypass
/// launders the one thing the user is being asked to judge.
///
/// Existence therefore cannot be the test; only the NAME can be. An ALLOWLIST rather than a
/// denylist of script extensions, because `ShellExecuteEx` resolves any registered association:
/// `.lnk`, `.vbs`, `.ps1`, `.js`, `.hta` and `.msi` are all refused here by construction, where
/// a denylist would have to enumerate a registry the caller's machine controls. `.lnk` is the
/// sharp case — a shortcut's target can be `cmd.exe /c ...`, elevating a program never named.
///
/// Those measurements are of a launch without a class. cosca launches as `exefile`
/// (`SEE_MASK_CLASSNAME`), and whether that launch applies `PATHEXT` on the consent route an
/// unelevated caller takes is unmeasured.
///
/// The allowlist closes the extensionless case only, whatever that answer is. Whether
/// `ShellExecuteEx` applies `PATHEXT` to an `lpFile` that already ends in `.exe` — a real
/// `tool.exe` beside a planted `tool.exe.bat` — is unmeasured on every route, and this rule makes
/// no claim about it.
///
/// # Deliberately stricter than an unelevated spawn
///
/// `CreateProcessW` does NOT extend `lpApplicationName` ("no default extension is assumed"), so
/// it loads an extensionless PE by absolute path quite safely; `ShellExecuteEx` does extend, so
/// the same name is plantable there. The asymmetry is the platform's, and this rule follows it
/// rather than papering over it: `.elevate()` refuses names an ordinary spawn accepts.
/// `raw_executable("tool").elevate()` wants `tool.exe`.
///
/// Over-rejection is the safe direction and is taken where the two disagree: `tool.exe.` (trailing
/// dot) resolves to `tool.exe` on Windows but is refused here, because a trailing dot does NOT
/// suppress `PATHEXT` (measured) and reasoning about which spellings Windows silently trims is
/// how the plantability bug got in.
pub
/// Split a `PATH` value on the simulated platform's separator.
///
/// On Windows a `"` toggles quoting, so a `;` inside quotes does not split — letting a directory
/// that contains a literal `;` (or leading/trailing space) survive as ONE element rather than
/// being torn in half by a naive byte-level `;` split. `"C:\a;b"` is one element, `C:\a;b`; a
/// plain, unquoted `C:\bin` passes through unchanged.
///
/// Every `"` is consumed as a delimiter wherever it appears, not only a wrapping pair: an
/// interior `C:\Pro"gram Files"\bin` yields `C:\Program Files\bin`, with the quotes removed and
/// the remainder concatenated. That matches `std::env::split_paths`'s own Windows parser, which
/// this replaced, so the behaviour is deliberate — but it does mean a `"` is never preserved as
/// a literal path character on Windows.
///
/// Stripping is what keeps a quoted entry alive at all: leaving the quotes IN would fail the
/// `is_absolute()` filter the caller applies afterwards (a leading `"` is not a recognised drive
/// prefix), so the entry would be SILENTLY DROPPED rather than erroring.
///
/// On POSIX, `"` is an ordinary filename character and `;` is not a separator: quoting is
/// deliberately NOT applied there — only `:` splits, and any quote characters in an element are
/// preserved literally, matching every POSIX shell's own (quote-free) `PATH` handling.
/// Windows `PATH` splitting with quote handling — see [`split_path_var`]'s doc for the rule.
/// Whether a candidate is a file this platform could actually exec.
///
/// On POSIX the execute bit is part of the answer: `execvp` skips a readable-but-non-executable
/// match and keeps searching (measured), so keying on existence alone stops at a file that would
/// have been passed over and hands it to exec, turning a working command into `EACCES`.
/// `faccessat(AT_EACCESS)` asks for the ids that will actually exec, unlike `access`.
///
/// That argument is about the `PATH` SEARCH, and it is applied to a [`Shape::Located`] name too,
/// where `execvp` does not search and would report `EACCES` rather than skipping. So once the
/// POSIX spawn path routes through this module (see the module doc), `resolve("./tool")` against
/// a non-executable `./tool` will report `NotFound` where the platform reports
/// `PermissionDenied`. That is deliberate — one predicate for "can this be exec'd" beats a
/// shape-dependent one, and the caller learns the file is unusable either way — but the error
/// KIND diverges, and anything matching on it should know that before the POSIX path lands.
///
/// Note the `windows` parameter is a runtime flag while the `faccessat` call sits behind
/// `#[cfg(unix)]`: simulating POSIX on a Windows HOST therefore skips the execute-bit check
/// entirely.
///
/// `Err` when the filesystem could not say whether `path` exists (a permission, I/O or network
/// failure), as distinct from saying it does not: see [`resolve`] for what each caller does with
/// that. Only a missing file or directory, a non-directory in the path, and a name no filesystem
/// accepts are a definite "not here".
/// The answer of an `X_OK` `faccessat` that returned `rc` with `errno`: executable, not (denied, or
/// the file gone since `metadata` saw it), or undeterminable (any other failure), so that `resolve`
/// applies the same disposition as to a failed `metadata`.
/// Whether a metadata error says definitely "no such file": nothing at the path, a non-directory
/// in it, a name no filesystem accepts, or (Windows) no such drive.
///
/// Decided on the raw OS code, not [`std::io::ErrorKind`]: std maps `ERROR_BAD_NETPATH` and
/// `ERROR_BAD_NET_NAME` to `NotFound` too, and an unreachable share may only be unreachable for now.
/// `candidate` under `dir`: on the Windows grammar by [`join::join`], the one Windows join, with
/// the host's separator since the result is probed on this host; otherwise `PathBuf::join`. An empty
/// `dir` is an absolute name's own location.
/// Whether a joined candidate is fully qualified, so no current directory, the process's or a
/// drive's, decides which file it names. Either reading counts: `std`'s knows the host's grammar
/// (and on Windows only letter drives), [`is_absolute_name`] the simulated one.
/// Whether `name` is rooted (`\x`) and `base` verbatim: a name Win32 completes off the base's
/// volume, which [`resolve`] and the raw backend's completion both refuse.
pub
/// The refusal for [`is_rooted_on_verbatim`].
pub
pub