@@ -37,7 +37,78 @@ struct Candidate {
3737/// path parser. Used by repair (references the ledger does not cover), by
3838/// the orphan sweeps (`vendor --revert`, `scan --prune`: a dir a lockfile
3939/// still points at is never deleted) and by rollback.
40+ ///
41+ /// Every escape and separator spelling of a reference counts (an older
42+ /// composer.lock's `\/`, any run of Windows backslashes, `\u` /
43+ /// XML-entity escapes). This form REFUSES on what it finds (`vendor`'s
44+ /// ledger-missing gate, rollback's ledger-less gate, `vendor --check`), so
45+ /// it folds ASCII case only where the project's file system does: on a
46+ /// case-sensitive disk another case names another directory. The orphan
47+ /// sweep's keep-vs-delete reads [`scan_vendor_references_any_case`].
4048pub ( crate ) async fn scan_vendor_references ( project_root : & Path ) -> Vec < ( String , String , String ) > {
49+ let fold_case = socket_patch_core:: utils:: fs:: folds_case (
50+ & project_root. join ( socket_patch_core:: constants:: SOCKET_DIR ) ,
51+ )
52+ . await ;
53+ scan_vendor_references_folding ( project_root, fold_case) . await
54+ }
55+
56+ /// [`scan_vendor_references`] matching the `.socket/vendor/<eco>/<uuid>`
57+ /// anchor in any ASCII case, as the revert keep-checks
58+ /// ([`socket_patch_core::utils::path_spelling::PathNeedle::keep`]) do: the
59+ /// orphan sweep only ever KEEPS a dir on what it finds, so erring toward
60+ /// "referenced" is the safe direction and the two deletion paths agree.
61+ pub ( crate ) async fn scan_vendor_references_any_case (
62+ project_root : & Path ,
63+ ) -> Vec < ( String , String , String ) > {
64+ scan_vendor_references_folding ( project_root, true ) . await
65+ }
66+
67+ /// Recover a vendored reference from a folded `candidate`, ASCII
68+ /// case-insensitively when `fold_case` (eco and uuid lower-cased, the leaf
69+ /// and the relpath kept as spelled).
70+ fn parse_reference_folding ( candidate : & str , fold_case : bool ) -> Option < ( String , String , String ) > {
71+ if let Some ( parts) = parse_vendor_reference ( candidate) {
72+ let rel = candidate
73+ . trim_start_matches ( "./" )
74+ . trim_end_matches ( '/' )
75+ . to_string ( ) ;
76+ return Some ( ( parts. eco , parts. uuid , rel) ) ;
77+ }
78+ if !fold_case {
79+ return None ;
80+ }
81+ let parts = parse_vendor_reference ( & candidate. to_ascii_lowercase ( ) ) ?;
82+ // ASCII lower-casing keeps every byte offset, and the leaf is the
83+ // candidate's own tail.
84+ let trimmed = candidate. trim_end_matches ( '/' ) ;
85+ let leaf = & trimmed[ trimmed. len ( ) - parts. leaf . len ( ) ..] ;
86+ let mut rel = format ! ( ".socket/vendor/{}/{}" , parts. eco, parts. uuid) ;
87+ if !leaf. is_empty ( ) {
88+ rel. push ( '/' ) ;
89+ rel. push_str ( leaf) ;
90+ }
91+ Some ( ( parts. eco , parts. uuid , rel) )
92+ }
93+
94+ /// The byte offset of the first `.socket` in `text`, ASCII
95+ /// case-insensitively when `fold_case`.
96+ fn find_socket_anchor ( text : & str , fold_case : bool ) -> Option < usize > {
97+ const ANCHOR : & str = ".socket" ;
98+ if !fold_case {
99+ return text. find ( ANCHOR ) ;
100+ }
101+ text. match_indices ( '.' ) . map ( |( i, _) | i) . find ( |& i| {
102+ text. as_bytes ( )
103+ . get ( i..i + ANCHOR . len ( ) )
104+ . is_some_and ( |s| s. eq_ignore_ascii_case ( ANCHOR . as_bytes ( ) ) )
105+ } )
106+ }
107+
108+ async fn scan_vendor_references_folding (
109+ project_root : & Path ,
110+ fold_case : bool ,
111+ ) -> Vec < ( String , String , String ) > {
41112 let mut seen: HashSet < ( String , String ) > = HashSet :: new ( ) ;
42113 let mut out = Vec :: new ( ) ;
43114 // The binary lock is read unless a readable text lock shadows it: a
@@ -72,8 +143,13 @@ pub(crate) async fn scan_vendor_references(project_root: &Path) -> Vec<(String,
72143 let Ok ( text) = read_regular_to_string ( & project_root. join ( file) ) . await else {
73144 continue ;
74145 } ;
75- let mut rest = text. as_str ( ) ;
76- while let Some ( idx) = rest. find ( ".socket" ) {
146+ // Every spelling folded first: escapes decoded and every run of `/`
147+ // and `\` collapsed to one `/` (an older composer.lock's `\/`, any
148+ // number of Windows backslashes). Missing one here lets the orphan
149+ // sweep delete a dir a lock still installs from.
150+ let text = socket_patch_core:: utils:: path_spelling:: canonical_path_text ( & text) ;
151+ let mut rest: & str = & text;
152+ while let Some ( idx) = find_socket_anchor ( rest, fold_case) {
77153 let slice = & rest[ idx..] ;
78154 // `:` ends a reference too: pnpm snapshot keys are
79155 // `name@file:<path>:` and yaml mappings suffix the path with a
@@ -84,18 +160,11 @@ pub(crate) async fn scan_vendor_references(project_root: &Path) -> Vec<(String,
84160 '"' , '\'' , '`' , ' ' , '\t' , '\n' , '\r' , ',' , ')' , ']' , '}' , ';' , ':' , '<' ,
85161 ] )
86162 . unwrap_or ( slice. len ( ) ) ;
87- let candidate = slice[ ..end] . replace ( '\\' , "/" ) ;
163+ let candidate = & slice[ ..end] ;
88164 // NuGet's feed and Maven's repository name the uuid dir itself.
89- if let Some ( parts) = parse_vendor_reference ( & candidate) {
90- if seen. insert ( ( parts. eco . to_string ( ) , parts. uuid . clone ( ) ) ) {
91- out. push ( (
92- parts. eco . to_string ( ) ,
93- parts. uuid . clone ( ) ,
94- candidate
95- . trim_start_matches ( "./" )
96- . trim_end_matches ( '/' )
97- . to_string ( ) ,
98- ) ) ;
165+ if let Some ( ( eco, uuid, rel) ) = parse_reference_folding ( candidate, fold_case) {
166+ if seen. insert ( ( eco. clone ( ) , uuid. clone ( ) ) ) {
167+ out. push ( ( eco, uuid, rel) ) ;
99168 }
100169 }
101170 rest = & rest[ idx + ".socket" . len ( ) ..] ;
@@ -1275,6 +1344,70 @@ mod tests {
12751344 ) ;
12761345 }
12771346
1347+ /// An older composer.lock spells slashes `\/` and a Windows checkout may
1348+ /// double (or quadruple) its backslashes: the scan folds every run to the
1349+ /// canonical path, so the orphan sweep keeps a dir such a lock still
1350+ /// installs from. A different uuid's dir is never this one's reference.
1351+ #[ tokio:: test]
1352+ async fn scan_recovers_escaped_and_doubled_backslash_references ( ) {
1353+ let uuid = "33333333-3333-4333-8333-333333333333" ;
1354+ let other = "44444444-3333-4333-8333-333333333333" ;
1355+ let path = format ! ( ".socket/vendor/composer/{uuid}/psr/log@3.0.2" ) ;
1356+ for lock in [
1357+ format ! ( "{{\" url\" : \" {}\" }}" , path. replace( '/' , "\\ /" ) ) ,
1358+ format ! ( "{{\" url\" : \" {}\" }}" , path. replace( '/' , "\\ \\ " ) ) ,
1359+ format ! ( "{{\" url\" : \" {}\" }}" , path. replace( '/' , "\\ \\ \\ \\ " ) ) ,
1360+ format ! ( "{{\" url\" : \" {}\" }}" , path. replace( '/' , "\\ \\ \\ /" ) ) ,
1361+ format ! ( "{{\" url\" : \" ./{}\" }}" , path. replace( '/' , "\\ /" ) ) ,
1362+ ] {
1363+ let tmp = tempfile:: tempdir ( ) . unwrap ( ) ;
1364+ tokio:: fs:: write ( tmp. path ( ) . join ( "composer.lock" ) , & lock)
1365+ . await
1366+ . unwrap ( ) ;
1367+ let refs = scan_vendor_references ( tmp. path ( ) ) . await ;
1368+ assert_eq ! (
1369+ refs,
1370+ vec![ ( "composer" . to_string( ) , uuid. to_string( ) , path. clone( ) ) ] ,
1371+ "{lock}"
1372+ ) ;
1373+ assert ! (
1374+ refs. iter( ) . all( |( _, u, _) | u != other) ,
1375+ "a different uuid's dir is never this one's reference"
1376+ ) ;
1377+ assert_eq ! ( scan_vendor_references_any_case( tmp. path( ) ) . await , refs) ;
1378+ }
1379+ }
1380+
1381+ /// The orphan sweep's scan matches the anchor in any case, as the revert
1382+ /// keep-checks do, so a re-cased reference keeps the dir (eco and uuid
1383+ /// reported canonical, the leaf as spelled). The refusal-side scan folds
1384+ /// case only where the disk does.
1385+ #[ tokio:: test]
1386+ async fn any_case_scan_recovers_a_recased_reference ( ) {
1387+ let uuid = "55555555-3333-4333-8333-333333333333" ;
1388+ let tmp = tempfile:: tempdir ( ) . unwrap ( ) ;
1389+ let lock = format ! (
1390+ "{{\" url\" : \" {}\" }}" ,
1391+ format!(
1392+ ".Socket\\ Vendor\\ Composer\\ {}\\ Psr\\ Log@3.0.2" ,
1393+ uuid. to_ascii_uppercase( )
1394+ )
1395+ ) ;
1396+ tokio:: fs:: write ( tmp. path ( ) . join ( "composer.lock" ) , & lock)
1397+ . await
1398+ . unwrap ( ) ;
1399+ assert_eq ! (
1400+ scan_vendor_references_any_case( tmp. path( ) ) . await ,
1401+ vec![ (
1402+ "composer" . to_string( ) ,
1403+ uuid. to_string( ) ,
1404+ format!( ".socket/vendor/composer/{uuid}/Psr/Log@3.0.2" )
1405+ ) ]
1406+ ) ;
1407+ // No `.socket/` to probe: the refusal-side scan keeps case.
1408+ assert ! ( scan_vendor_references( tmp. path( ) ) . await . is_empty( ) ) ;
1409+ }
1410+
12781411 /// The scanner's false-positive guard: a `.socket` mention that is NOT
12791412 /// a parseable vendored-artifact path (the committed manifest, a
12801413 /// non-uuid path segment) must never be reported as a vendor reference
0 commit comments