summaryrefslogtreecommitdiffstats
path: root/src/VBox/Runtime/common/vfs/vfschain.cpp
blob: e3811970510ee64e73fe714c365f8910aab52afd (plain)
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
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
/* $Id: vfschain.cpp $ */
/** @file
 * IPRT - Virtual File System, Chains.
 */

/*
 * Copyright (C) 2010-2023 Oracle and/or its affiliates.
 *
 * This file is part of VirtualBox base platform packages, as
 * available from https://www.virtualbox.org.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, in version 3 of the
 * License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses>.
 *
 * The contents of this file may alternatively be used under the terms
 * of the Common Development and Distribution License Version 1.0
 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
 * in the VirtualBox distribution, in which case the provisions of the
 * CDDL are applicable instead of those of the GPL.
 *
 * You may elect to license modified versions of this file under the
 * terms and conditions of either the GPL or the CDDL or both.
 *
 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
 */


/*********************************************************************************************************************************
*   Header Files                                                                                                                 *
*********************************************************************************************************************************/
#include <iprt/vfs.h>
#include <iprt/vfslowlevel.h>

#include <iprt/asm.h>
#include <iprt/critsect.h>
#include <iprt/ctype.h>
#include <iprt/err.h>
#include <iprt/file.h>
#include <iprt/mem.h>
#include <iprt/once.h>
#include <iprt/param.h>
#include <iprt/path.h>
#include <iprt/semaphore.h>
#include <iprt/string.h>

#include "internal/file.h"
#include "internal/magics.h"
//#include "internal/vfs.h"


/*********************************************************************************************************************************
*   Internal Functions                                                                                                           *
*********************************************************************************************************************************/
static PCRTVFSCHAINELEMENTREG rtVfsChainFindProviderLocked(const char *pszProvider);


/*********************************************************************************************************************************
*   Global Variables                                                                                                             *
*********************************************************************************************************************************/
/** Init the critical section once. */
static RTONCE       g_rtVfsChainElementInitOnce = RTONCE_INITIALIZER;
/** Critical section protecting g_rtVfsChainElementProviderList. */
static RTCRITSECTRW g_rtVfsChainElementCritSect;
/** List of VFS chain element providers (RTVFSCHAINELEMENTREG). */
static RTLISTANCHOR g_rtVfsChainElementProviderList;



RTDECL(int) RTVfsChainValidateOpenFileOrIoStream(PRTVFSCHAINSPEC pSpec, PRTVFSCHAINELEMSPEC pElement,
                                                 uint32_t *poffError, PRTERRINFO pErrInfo)
{
    if (pElement->cArgs < 1)
        return VERR_VFS_CHAIN_AT_LEAST_ONE_ARG;
    if (pElement->cArgs > 4)
        return VERR_VFS_CHAIN_AT_MOST_FOUR_ARGS;
    if (!*pElement->paArgs[0].psz)
        return VERR_VFS_CHAIN_EMPTY_ARG;

    /*
     * Calculate the flags, storing them in the first argument.
     */
    const char *pszAccess = pElement->cArgs >= 2 ? pElement->paArgs[1].psz : "";
    if (!*pszAccess)
        pszAccess = (pSpec->fOpenFile & RTFILE_O_ACCESS_MASK) == RTFILE_O_READWRITE ? "rw"
                  : (pSpec->fOpenFile & RTFILE_O_ACCESS_MASK) == RTFILE_O_READ      ? "r"
                  : (pSpec->fOpenFile & RTFILE_O_ACCESS_MASK) == RTFILE_O_WRITE     ? "w"
                  :                                                                   "rw";

    const char *pszDisp = pElement->cArgs >= 3 ? pElement->paArgs[2].psz : "";
    if (!*pszDisp)
        pszDisp = strchr(pszAccess, 'w') != NULL ? "open-create" : "open";

    const char *pszSharing = pElement->cArgs >= 4 ? pElement->paArgs[3].psz : "";

    int rc = RTFileModeToFlagsEx(pszAccess, pszDisp, pszSharing, &pElement->uProvider);
    if (RT_SUCCESS(rc))
        return VINF_SUCCESS;

    /*
     * Now try figure out which argument offended us.
     */
    AssertReturn(pElement->cArgs > 1, VERR_VFS_CHAIN_IPE);
    if (   pElement->cArgs == 2
        || RT_FAILURE(RTFileModeToFlagsEx(pszAccess, "open-create", "", &pElement->uProvider)))
    {
        *poffError = pElement->paArgs[1].offSpec;
        rc = RTErrInfoSet(pErrInfo, VERR_VFS_CHAIN_INVALID_ARGUMENT, "Expected valid access flags: 'r', 'rw', or 'w'");
    }
    else if (   pElement->cArgs == 3
             || RT_FAILURE(RTFileModeToFlagsEx(pszAccess, pszDisp, "", &pElement->uProvider)))
    {
        *poffError = pElement->paArgs[2].offSpec;
        rc = RTErrInfoSet(pErrInfo, VERR_VFS_CHAIN_INVALID_ARGUMENT,
                          "Expected valid open disposition: create, create-replace, open, open-create, open-append, open-truncate");
    }
    else
    {
        *poffError = pElement->paArgs[3].offSpec;
        rc = RTErrInfoSet(pErrInfo, VERR_VFS_CHAIN_INVALID_ARGUMENT, "Expected valid sharing flags: nr, nw, nrw, d");

    }
    return rc;
}


/**
 * @interface_method_impl{RTVFSCHAINELEMENTREG,pfnValidate}
 */
static DECLCALLBACK(int) rtVfsChainOpen_Validate(PCRTVFSCHAINELEMENTREG pProviderReg, PRTVFSCHAINSPEC pSpec,
                                                 PRTVFSCHAINELEMSPEC pElement, uint32_t *poffError, PRTERRINFO pErrInfo)
{
    RT_NOREF(pProviderReg);

    /*
     * Basic checks.
     */
    if (   pElement->enmType != RTVFSOBJTYPE_DIR
        && pElement->enmType != RTVFSOBJTYPE_FILE
        && pElement->enmType != RTVFSOBJTYPE_IO_STREAM)
        return VERR_VFS_CHAIN_ONLY_FILE_OR_IOS_OR_DIR;
    if (   pElement->enmTypeIn != RTVFSOBJTYPE_DIR
        && pElement->enmTypeIn != RTVFSOBJTYPE_FS_STREAM
        && pElement->enmTypeIn != RTVFSOBJTYPE_VFS)
    {
        if (pElement->enmTypeIn == RTVFSOBJTYPE_INVALID)
        {
            /*
             * First element: Transform into 'stdfile' or 'stddir' if registered.
             */
            const char            *pszNewProvider = pElement->enmType == RTVFSOBJTYPE_DIR ? "stddir" : "stdfile";
            PCRTVFSCHAINELEMENTREG pNewProvider   = rtVfsChainFindProviderLocked(pszNewProvider);
            if (pNewProvider)
            {
                pElement->pProvider = pNewProvider;
                return pNewProvider->pfnValidate(pNewProvider, pSpec, pElement, poffError, pErrInfo);
            }
            return VERR_VFS_CHAIN_CANNOT_BE_FIRST_ELEMENT;
        }
        return VERR_VFS_CHAIN_TAKES_DIR_OR_FSS_OR_VFS;
    }

    /*
     * Make common cause with 'stdfile' if we're opening a file or I/O stream.
     * If the input is a FSS, we have to make sure it's a read-only operation.
     */
    if (   pElement->enmType == RTVFSOBJTYPE_FILE
        || pElement->enmType == RTVFSOBJTYPE_IO_STREAM)
    {
        int rc = RTVfsChainValidateOpenFileOrIoStream(pSpec, pElement, poffError, pErrInfo);
        if (RT_SUCCESS(rc))
        {
            if (pElement->enmTypeIn != RTVFSOBJTYPE_FS_STREAM)
                return VINF_SUCCESS;
            if (   !(pElement->uProvider & RTFILE_O_WRITE)
                &&  (pElement->uProvider & RTFILE_O_ACTION_MASK) ==  RTFILE_O_OPEN)
                return VINF_SUCCESS;
            *poffError = pElement->cArgs > 1 ? pElement->paArgs[1].offSpec : pElement->offSpec;
            return VERR_VFS_CHAIN_INVALID_ARGUMENT;
        }
        return rc;
    }


    /*
     * Directory checks.  Path argument only, optional. If not given the root directory of a VFS or the
     */
    if (pElement->cArgs > 1)
        return VERR_VFS_CHAIN_AT_MOST_ONE_ARG;
    pElement->uProvider = 0;
    return VINF_SUCCESS;
}


/**
 * @interface_method_impl{RTVFSCHAINELEMENTREG,pfnInstantiate}
 */
static DECLCALLBACK(int) rtVfsChainOpen_Instantiate(PCRTVFSCHAINELEMENTREG pProviderReg, PCRTVFSCHAINSPEC pSpec,
                                                    PCRTVFSCHAINELEMSPEC pElement, RTVFSOBJ hPrevVfsObj,
                                                    PRTVFSOBJ phVfsObj, uint32_t *poffError, PRTERRINFO pErrInfo)
{
    RT_NOREF(pProviderReg, pSpec, pElement, poffError, pErrInfo);
    AssertReturn(hPrevVfsObj != NIL_RTVFSOBJ, VERR_VFS_CHAIN_IPE);

    /*
     * File system stream: Seek thru the stream looking for the object to open.
     */
    RTVFSFSSTREAM hVfsFssIn = RTVfsObjToFsStream(hPrevVfsObj);
    if (hVfsFssIn != NIL_RTVFSFSSTREAM)
    {
        return VERR_NOT_IMPLEMENTED;
    }

    /*
     * VFS: Use RTVfsFileOpen or RTVfsDirOpen.
     */
    RTVFS hVfsIn = RTVfsObjToVfs(hPrevVfsObj);
    if (hVfsIn != NIL_RTVFS)
    {
        if (   pElement->enmType == RTVFSOBJTYPE_FILE
            || pElement->enmType == RTVFSOBJTYPE_IO_STREAM)
        {
            RTVFSFILE hVfsFile = NIL_RTVFSFILE;
            int rc = RTVfsFileOpen(hVfsIn, pElement->paArgs[0].psz, pElement->uProvider, &hVfsFile);
            RTVfsRelease(hVfsIn);
            if (RT_SUCCESS(rc))
            {
                *phVfsObj = RTVfsObjFromFile(hVfsFile);
                RTVfsFileRelease(hVfsFile);
                if (*phVfsObj != NIL_RTVFSOBJ)
                    return VINF_SUCCESS;
                rc = VERR_VFS_CHAIN_CAST_FAILED;
            }
            return rc;
        }
        if (pElement->enmType == RTVFSOBJTYPE_DIR)
        {
            RTVFSDIR hVfsDir = NIL_RTVFSDIR;
            int rc = RTVfsDirOpen(hVfsIn, pElement->paArgs[0].psz, (uint32_t)pElement->uProvider, &hVfsDir);
            RTVfsRelease(hVfsIn);
            if (RT_SUCCESS(rc))
            {
                *phVfsObj = RTVfsObjFromDir(hVfsDir);
                RTVfsDirRelease(hVfsDir);
                if (*phVfsObj != NIL_RTVFSOBJ)
                    return VINF_SUCCESS;
                rc = VERR_VFS_CHAIN_CAST_FAILED;
            }
            return rc;
        }
        RTVfsRelease(hVfsIn);
        return VERR_VFS_CHAIN_IPE;
    }

    /*
     * Directory: Similar to above, just relative to a directory.
     */
    RTVFSDIR hVfsDirIn = RTVfsObjToDir(hPrevVfsObj);
    if (hVfsDirIn != NIL_RTVFSDIR)
    {
        if (   pElement->enmType == RTVFSOBJTYPE_FILE
            || pElement->enmType == RTVFSOBJTYPE_IO_STREAM)
        {
            RTVFSFILE hVfsFile = NIL_RTVFSFILE;
            int rc = RTVfsDirOpenFile(hVfsDirIn, pElement->paArgs[0].psz, pElement->uProvider, &hVfsFile);
            RTVfsDirRelease(hVfsDirIn);
            if (RT_SUCCESS(rc))
            {
                *phVfsObj = RTVfsObjFromFile(hVfsFile);
                RTVfsFileRelease(hVfsFile);
                if (*phVfsObj != NIL_RTVFSOBJ)
                    return VINF_SUCCESS;
                rc = VERR_VFS_CHAIN_CAST_FAILED;
            }
            return rc;
        }
        if (pElement->enmType == RTVFSOBJTYPE_DIR)
        {
            RTVFSDIR hVfsDir = NIL_RTVFSDIR;
            int rc = RTVfsDirOpenDir(hVfsDirIn, pElement->paArgs[0].psz, pElement->uProvider, &hVfsDir);
            RTVfsDirRelease(hVfsDirIn);
            if (RT_SUCCESS(rc))
            {
                *phVfsObj = RTVfsObjFromDir(hVfsDir);
                RTVfsDirRelease(hVfsDir);
                if (*phVfsObj != NIL_RTVFSOBJ)
                    return VINF_SUCCESS;
                rc = VERR_VFS_CHAIN_CAST_FAILED;
            }
            return rc;
        }
        RTVfsDirRelease(hVfsDirIn);
        return VERR_VFS_CHAIN_IPE;
    }

    AssertFailed();
    return VERR_VFS_CHAIN_CAST_FAILED;
}


/**
 * @interface_method_impl{RTVFSCHAINELEMENTREG,pfnCanReuseElement}
 */
static DECLCALLBACK(bool) rtVfsChainOpen_CanReuseElement(PCRTVFSCHAINELEMENTREG pProviderReg,
                                                         PCRTVFSCHAINSPEC pSpec, PCRTVFSCHAINELEMSPEC pElement,
                                                         PCRTVFSCHAINSPEC pReuseSpec, PCRTVFSCHAINELEMSPEC pReuseElement)
{
    RT_NOREF(pProviderReg, pSpec, pElement, pReuseSpec, pReuseElement);
    return false;
}


/** VFS chain element 'gunzip'. */
static RTVFSCHAINELEMENTREG g_rtVfsChainGunzipReg =
{
    /* uVersion = */            RTVFSCHAINELEMENTREG_VERSION,
    /* fReserved = */           0,
    /* pszName = */             "open",
    /* ListEntry = */           { NULL, NULL },
    /* pszHelp = */             "Generic VFS open, that can open files (or I/O stream) and directories in a VFS, directory or file system stream.\n"
                                "If used as the first element in a chain, it will work like 'stdfile' or 'stddir' and work on the real file system.\n"
                                "First argument is the filename or directory path.\n"
                                "Second argument is access mode, files only, optional: r, w, rw.\n"
                                "Third argument is open disposition, files only, optional: create, create-replace, open, open-create, open-append, open-truncate.\n"
                                "Forth argument is file sharing, files only, optional: nr, nw, nrw, d.",
    /* pfnValidate = */         rtVfsChainOpen_Validate,
    /* pfnInstantiate = */      rtVfsChainOpen_Instantiate,
    /* pfnCanReuseElement = */  rtVfsChainOpen_CanReuseElement,
    /* uEndMarker = */          RTVFSCHAINELEMENTREG_VERSION
};

RTVFSCHAIN_AUTO_REGISTER_ELEMENT_PROVIDER(&g_rtVfsChainGunzipReg, rtVfsChainGunzipReg);




/**
 * Initializes the globals via RTOnce.
 *
 * @returns IPRT status code
 * @param   pvUser              Unused, ignored.
 */
static DECLCALLBACK(int) rtVfsChainElementRegisterInit(void *pvUser)
{
    NOREF(pvUser);
    if (!g_rtVfsChainElementProviderList.pNext)
        RTListInit(&g_rtVfsChainElementProviderList);
    int rc = RTCritSectRwInit(&g_rtVfsChainElementCritSect);
    if (RT_SUCCESS(rc))
    {
    }
    return rc;
}


RTDECL(int) RTVfsChainElementRegisterProvider(PRTVFSCHAINELEMENTREG pRegRec, bool fFromCtor)
{
    int rc;

    /*
     * Input validation.
     */
    AssertPtrReturn(pRegRec, VERR_INVALID_POINTER);
    AssertMsgReturn(pRegRec->uVersion   == RTVFSCHAINELEMENTREG_VERSION, ("%#x", pRegRec->uVersion),    VERR_INVALID_POINTER);
    AssertMsgReturn(pRegRec->uEndMarker == RTVFSCHAINELEMENTREG_VERSION, ("%#zx", pRegRec->uEndMarker), VERR_INVALID_POINTER);
    AssertReturn(pRegRec->fReserved == 0, VERR_INVALID_POINTER);
    AssertPtrReturn(pRegRec->pszName,               VERR_INVALID_POINTER);
    AssertPtrReturn(pRegRec->pfnValidate,           VERR_INVALID_POINTER);
    AssertPtrReturn(pRegRec->pfnInstantiate,        VERR_INVALID_POINTER);
    AssertPtrReturn(pRegRec->pfnCanReuseElement,    VERR_INVALID_POINTER);

    /*
     * Init and take the lock.
     */
    if (!fFromCtor)
    {
        rc = RTOnce(&g_rtVfsChainElementInitOnce, rtVfsChainElementRegisterInit, NULL);
        if (RT_FAILURE(rc))
            return rc;
        rc = RTCritSectRwEnterExcl(&g_rtVfsChainElementCritSect);
        if (RT_FAILURE(rc))
            return rc;
    }
    else if (!g_rtVfsChainElementProviderList.pNext)
        RTListInit(&g_rtVfsChainElementProviderList);

    /*
     * Duplicate name?
     */
    rc = VINF_SUCCESS;
    PRTVFSCHAINELEMENTREG pIterator, pIterNext;
    RTListForEachSafe(&g_rtVfsChainElementProviderList, pIterator, pIterNext, RTVFSCHAINELEMENTREG, ListEntry)
    {
        if (!strcmp(pIterator->pszName, pRegRec->pszName))
        {
            AssertMsgFailed(("duplicate name '%s' old=%p new=%p\n",  pIterator->pszName, pIterator, pRegRec));
            rc = VERR_ALREADY_EXISTS;
            break;
        }
    }

    /*
     * If not, append the record to the list.
     */
    if (RT_SUCCESS(rc))
        RTListAppend(&g_rtVfsChainElementProviderList, &pRegRec->ListEntry);

    /*
     * Leave the lock and return.
     */
    if (!fFromCtor)
        RTCritSectRwLeaveExcl(&g_rtVfsChainElementCritSect);
    return rc;
}


/**
 * Allocates and initializes an empty spec
 *
 * @returns Pointer to the spec on success, NULL on failure.
 */
static PRTVFSCHAINSPEC rtVfsChainSpecAlloc(void)
{
    PRTVFSCHAINSPEC pSpec = (PRTVFSCHAINSPEC)RTMemTmpAlloc(sizeof(*pSpec));
    if (pSpec)
    {
        pSpec->fOpenFile      = 0;
        pSpec->fOpenDir       = 0;
        pSpec->cElements      = 0;
        pSpec->paElements     = NULL;
    }
    return pSpec;
}


/**
 * Checks if @a ch is a character that can be escaped.
 *
 * @returns true / false.
 * @param   ch          The character to consider.
 */
DECLINLINE(bool) rtVfsChainSpecIsEscapableChar(char ch)
{
    return ch == '('
        || ch == ')'
        || ch == '{'
        || ch == '}'
        || ch == '\\'
        || ch == ','
        || ch == '|'
        || ch == ':';
}


/**
 * Duplicate a spec string after unescaping it.
 *
 * This differs from RTStrDupN in that it uses RTMemTmpAlloc instead of
 * RTMemAlloc.
 *
 * @returns String copy on success, NULL on failure.
 * @param   psz         The string to duplicate.
 * @param   cch         The number of bytes to duplicate.
 * @param   prc         The status code variable to set on failure. (Leeps the
 *                      code shorter. -lazy bird)
 */
DECLINLINE(char *) rtVfsChainSpecDupStrN(const char *psz, size_t cch, int *prc)
{
    char *pszCopy = (char *)RTMemTmpAlloc(cch + 1);
    if (pszCopy)
    {
        if (!memchr(psz, '\\', cch))
        {
            /* Plain string, copy it raw. */
            memcpy(pszCopy, psz, cch);
            pszCopy[cch] = '\0';
        }
        else
        {
            /* Has escape sequences, must unescape it. */
            char *pszDst = pszCopy;
            while (cch-- > 0)
            {
                char ch = *psz++;
                if (ch == '\\' && cch > 0)
                {
                    char ch2 = *psz;
                    if (rtVfsChainSpecIsEscapableChar(ch2))
                    {
                        psz++;
                        cch--;
                        ch = ch2;
                    }
                }
                *pszDst++ = ch;
            }
            *pszDst = '\0';
        }
    }
    else
        *prc = VERR_NO_TMP_MEMORY;
    return pszCopy;
}


/**
 * Adds an empty element to the chain specification.
 *
 * The caller is responsible for filling it the element attributes.
 *
 * @returns Pointer to the new element on success, NULL on failure.  The
 *          pointer is only valid till the next call to this function.
 * @param   pSpec       The chain specification.
 * @param   prc         The status code variable to set on failure. (Leeps the
 *                      code shorter. -lazy bird)
 */
static PRTVFSCHAINELEMSPEC rtVfsChainSpecAddElement(PRTVFSCHAINSPEC pSpec, uint16_t offSpec, int *prc)
{
    AssertPtr(pSpec);

    /*
     * Resize the element table if necessary.
     */
    uint32_t const iElement = pSpec->cElements;
    if ((iElement % 32) == 0)
    {
        PRTVFSCHAINELEMSPEC paNew = (PRTVFSCHAINELEMSPEC)RTMemTmpAlloc((iElement + 32) * sizeof(paNew[0]));
        if (!paNew)
        {
            *prc = VERR_NO_TMP_MEMORY;
            return NULL;
        }

        if (iElement)
            memcpy(paNew, pSpec->paElements, iElement * sizeof(paNew[0]));
        RTMemTmpFree(pSpec->paElements);
        pSpec->paElements = paNew;
    }

    /*
     * Initialize and add the new element.
     */
    PRTVFSCHAINELEMSPEC pElement = &pSpec->paElements[iElement];
    pElement->pszProvider = NULL;
    pElement->enmTypeIn   = iElement ? pSpec->paElements[iElement - 1].enmType : RTVFSOBJTYPE_INVALID;
    pElement->enmType     = RTVFSOBJTYPE_INVALID;
    pElement->offSpec     = offSpec;
    pElement->cchSpec     = 0;
    pElement->cArgs       = 0;
    pElement->paArgs      = NULL;
    pElement->pProvider   = NULL;
    pElement->hVfsObj     = NIL_RTVFSOBJ;

    pSpec->cElements = iElement + 1;
    return pElement;
}


/**
 * Adds an argument to the element spec.
 *
 * @returns IPRT status code.
 * @param   pElement            The element.
 * @param   psz                 The start of the argument string.
 * @param   cch                 The length of the argument string, escape
 *                              sequences counted twice.
 */
static int rtVfsChainSpecElementAddArg(PRTVFSCHAINELEMSPEC pElement, const char *psz, size_t cch, uint16_t offSpec)
{
    uint32_t iArg = pElement->cArgs;
    if ((iArg % 32) == 0)
    {
        PRTVFSCHAINELEMENTARG paNew = (PRTVFSCHAINELEMENTARG)RTMemTmpAlloc((iArg + 32) * sizeof(paNew[0]));
        if (!paNew)
            return VERR_NO_TMP_MEMORY;
        if (iArg)
            memcpy(paNew, pElement->paArgs, iArg * sizeof(paNew[0]));
        RTMemTmpFree(pElement->paArgs);
        pElement->paArgs = paNew;
    }

    int rc = VINF_SUCCESS;
    pElement->paArgs[iArg].psz     = rtVfsChainSpecDupStrN(psz, cch, &rc);
    pElement->paArgs[iArg].offSpec = offSpec;
    pElement->cArgs = iArg + 1;
    return rc;
}


RTDECL(void)    RTVfsChainSpecFree(PRTVFSCHAINSPEC pSpec)
{
    if (!pSpec)
        return;

    uint32_t i = pSpec->cElements;
    while (i-- > 0)
    {
        uint32_t iArg = pSpec->paElements[i].cArgs;
        while (iArg-- > 0)
            RTMemTmpFree(pSpec->paElements[i].paArgs[iArg].psz);
        RTMemTmpFree(pSpec->paElements[i].paArgs);
        RTMemTmpFree(pSpec->paElements[i].pszProvider);
        if (pSpec->paElements[i].hVfsObj != NIL_RTVFSOBJ)
        {
            RTVfsObjRelease(pSpec->paElements[i].hVfsObj);
            pSpec->paElements[i].hVfsObj = NIL_RTVFSOBJ;
        }
    }

    RTMemTmpFree(pSpec->paElements);
    pSpec->paElements = NULL;
    RTMemTmpFree(pSpec);
}


/**
 * Checks if @a psz is pointing to the final element specification.
 *
 * @returns true / false.
 * @param   psz         Start of an element or path.
 * @param   pcch        Where to return the length.
 */
static bool rtVfsChainSpecIsFinalElement(const char *psz, size_t *pcch)
{
    size_t off = 0;
    char   ch;
    while ((ch = psz[off]) != '\0')
    {
        if (ch == '|' || ch == ':')
            return false;
        if (   ch == '\\'
            && rtVfsChainSpecIsEscapableChar(psz[off + 1]))
            off++;
        off++;
    }
    *pcch = off;
    return off > 0;
}


/**
 * Makes the final path element.
 * @returns IPRT status code
 * @param   pElement    The element.
 * @param   pszPath     The path.
 * @param   cchPath     The path length.
 */
static int rtVfsChainSpecMakeFinalPathElement(PRTVFSCHAINELEMSPEC pElement, const char *pszPath, size_t cchPath)
{
    pElement->pszProvider = NULL;
    pElement->enmType     = RTVFSOBJTYPE_END;
    pElement->cchSpec     = (uint16_t)cchPath;
    return rtVfsChainSpecElementAddArg(pElement, pszPath, cchPath, pElement->offSpec);
}


/**
 * Finds the end of the argument string.
 *
 * @returns The offset of the end character relative to @a psz.
 * @param   psz             The argument string.
 * @param   chCloseParen    The closing parenthesis.
 */
static size_t rtVfsChainSpecFindArgEnd(const char *psz, char const chCloseParen)
{
    size_t off = 0;
    char   ch;
    while (  (ch = psz[off]) != '\0'
           && ch != ','
           && ch != chCloseParen)
    {
        if (   ch == '\\'
            && rtVfsChainSpecIsEscapableChar(psz[off+1]))
            off++;
        off++;
    }
    return off;
}


RTDECL(int) RTVfsChainSpecParse(const char *pszSpec, uint32_t fFlags, RTVFSOBJTYPE enmDesiredType,
                                PRTVFSCHAINSPEC *ppSpec, uint32_t *poffError)
{
    if (poffError)
    {
        AssertPtrReturn(poffError, VERR_INVALID_POINTER);
        *poffError = 0;
    }
    AssertPtrReturn(ppSpec, VERR_INVALID_POINTER);
    *ppSpec = NULL;
    AssertPtrReturn(pszSpec, VERR_INVALID_POINTER);
    AssertReturn(!(fFlags & ~RTVFSCHAIN_PF_VALID_MASK), VERR_INVALID_PARAMETER);
    AssertReturn(enmDesiredType > RTVFSOBJTYPE_INVALID && enmDesiredType < RTVFSOBJTYPE_END, VERR_INVALID_PARAMETER);

    /*
     * Check the start of the specification and allocate an empty return spec.
     */
    if (strncmp(pszSpec, RTVFSCHAIN_SPEC_PREFIX, sizeof(RTVFSCHAIN_SPEC_PREFIX) - 1))
        return VERR_VFS_CHAIN_NO_PREFIX;
    const char *pszSrc = RTStrStripL(pszSpec + sizeof(RTVFSCHAIN_SPEC_PREFIX) - 1);
    if (!*pszSrc)
        return VERR_VFS_CHAIN_EMPTY;

    PRTVFSCHAINSPEC pSpec = rtVfsChainSpecAlloc();
    if (!pSpec)
        return VERR_NO_TMP_MEMORY;
    pSpec->enmDesiredType = enmDesiredType;

    /*
     * Parse the spec one element at a time.
     */
    int rc = VINF_SUCCESS;
    while (*pszSrc && RT_SUCCESS(rc))
    {
        /*
         * Digest element separator, except for the first element.
         */
        if (*pszSrc == '|' || *pszSrc == ':')
        {
            if (pSpec->cElements != 0)
                pszSrc = RTStrStripL(pszSrc + 1);
            else
            {
                rc = VERR_VFS_CHAIN_LEADING_SEPARATOR;
                break;
            }
        }
        else if (pSpec->cElements != 0)
        {
            rc = VERR_VFS_CHAIN_EXPECTED_SEPARATOR;
            break;
        }

        /*
         * Ok, there should be an element here so add one to the return struct.
         */
        PRTVFSCHAINELEMSPEC pElement = rtVfsChainSpecAddElement(pSpec, (uint16_t)(pszSrc - pszSpec), &rc);
        if (!pElement)
            break;

        /*
         * First up is the VFS object type followed by a parenthesis/curly, or
         * this could be the trailing action.  Alternatively, we could have a
         * final path-only element here.
         */
        size_t cch;
        if (strncmp(pszSrc, "base", cch = 4) == 0)
            pElement->enmType = RTVFSOBJTYPE_BASE;
        else if (strncmp(pszSrc, "vfs",  cch = 3) == 0)
            pElement->enmType = RTVFSOBJTYPE_VFS;
        else if (strncmp(pszSrc, "fss",  cch = 3) == 0)
            pElement->enmType = RTVFSOBJTYPE_FS_STREAM;
        else if (strncmp(pszSrc, "ios",  cch = 3) == 0)
            pElement->enmType = RTVFSOBJTYPE_IO_STREAM;
        else if (strncmp(pszSrc, "dir",  cch = 3) == 0)
            pElement->enmType = RTVFSOBJTYPE_DIR;
        else if (strncmp(pszSrc, "file", cch = 4) == 0)
            pElement->enmType = RTVFSOBJTYPE_FILE;
        else if (strncmp(pszSrc, "sym",  cch = 3) == 0)
            pElement->enmType = RTVFSOBJTYPE_SYMLINK;
        else
        {
            if (rtVfsChainSpecIsFinalElement(pszSrc, &cch))
                rc = rtVfsChainSpecMakeFinalPathElement(pElement, pszSrc, cch);
            else if (*pszSrc == '\0')
                rc = VERR_VFS_CHAIN_TRAILING_SEPARATOR;
            else
                rc = VERR_VFS_CHAIN_UNKNOWN_TYPE;
            break;
        }

        /* Check and skip past the parenthesis/curly.  If not there, we might
           have a final path element at our hands. */
        char const chOpenParen = pszSrc[cch];
        if (chOpenParen != '(' && chOpenParen != '{')
        {
            if (rtVfsChainSpecIsFinalElement(pszSrc, &cch))
                rc = rtVfsChainSpecMakeFinalPathElement(pElement, pszSrc, cch);
            else
                rc = VERR_VFS_CHAIN_EXPECTED_LEFT_PARENTHESES;
            break;
        }
        char const chCloseParen = (chOpenParen == '(' ? ')' : '}');
        pszSrc = RTStrStripL(pszSrc + cch + 1);

        /*
         * The name of the element provider.
         */
        cch = rtVfsChainSpecFindArgEnd(pszSrc, chCloseParen);
        if (!cch)
        {
            rc = VERR_VFS_CHAIN_EXPECTED_PROVIDER_NAME;
            break;
        }
        pElement->pszProvider = rtVfsChainSpecDupStrN(pszSrc, cch, &rc);
        if (!pElement->pszProvider)
            break;
        pszSrc += cch;

        /*
         * The arguments.
         */
        while (*pszSrc == ',')
        {
            pszSrc = RTStrStripL(pszSrc + 1);
            cch = rtVfsChainSpecFindArgEnd(pszSrc, chCloseParen);
            rc = rtVfsChainSpecElementAddArg(pElement, pszSrc, cch, (uint16_t)(pszSrc - pszSpec));
            if (RT_FAILURE(rc))
                break;
            pszSrc += cch;
        }
        if (RT_FAILURE(rc))
            break;

        /* Must end with a right parentheses/curly. */
        if (*pszSrc != chCloseParen)
        {
            rc = VERR_VFS_CHAIN_EXPECTED_RIGHT_PARENTHESES;
            break;
        }
        pElement->cchSpec = (uint16_t)(pszSrc - pszSpec) - pElement->offSpec + 1;

        pszSrc = RTStrStripL(pszSrc + 1);
    }

#if 0
    /*
     * Dump the chain.  Useful for debugging the above code.
     */
    RTAssertMsg2("dbg: cElements=%d rc=%Rrc\n", pSpec->cElements, rc);
    for (uint32_t i = 0; i < pSpec->cElements; i++)
    {
        uint32_t const cArgs = pSpec->paElements[i].cArgs;
        RTAssertMsg2("dbg: #%u: enmTypeIn=%d enmType=%d cArgs=%d",
                     i, pSpec->paElements[i].enmTypeIn, pSpec->paElements[i].enmType, cArgs);
        for (uint32_t j = 0; j < cArgs; j++)
            RTAssertMsg2(j == 0 ? (cArgs > 1 ? " [%s" : " [%s]") : j + 1 < cArgs ? ", %s" : ", %s]",
                         pSpec->paElements[i].paArgs[j].psz);
        RTAssertMsg2(" offSpec=%d cchSpec=%d", pSpec->paElements[i].offSpec, pSpec->paElements[i].cchSpec);
        RTAssertMsg2(" spec: %.*s\n", pSpec->paElements[i].cchSpec, &pszSpec[pSpec->paElements[i].offSpec]);
    }
#endif

    /*
     * Return the chain on success; Cleanup and set the error indicator on
     * failure.
     */
    if (RT_SUCCESS(rc))
        *ppSpec = pSpec;
    else
    {
        if (poffError)
            *poffError = (uint32_t)(pszSrc - pszSpec);
        RTVfsChainSpecFree(pSpec);
    }
    return rc;
}


/**
 * Looks up @a pszProvider among the registered providers.
 *
 * @returns Pointer to registration record if found, NULL if not.
 * @param   pszProvider         The provider.
 */
static PCRTVFSCHAINELEMENTREG rtVfsChainFindProviderLocked(const char *pszProvider)
{
    PCRTVFSCHAINELEMENTREG pIterator;
    RTListForEach(&g_rtVfsChainElementProviderList, pIterator, RTVFSCHAINELEMENTREG, ListEntry)
    {
        if (strcmp(pIterator->pszName, pszProvider) == 0)
            return pIterator;
    }
    return NULL;
}


/**
 * Does reusable object type matching.
 *
 * @returns true if the types matches, false if not.
 * @param   pElement        The target element specification.
 * @param   pReuseElement   The source element specification.
 */
static bool rtVfsChainMatchReusableType(PRTVFSCHAINELEMSPEC pElement, PRTVFSCHAINELEMSPEC pReuseElement)
{
    if (pElement->enmType == pReuseElement->enmType)
        return true;

    /* File objects can always be cast to I/O streams.  */
    if (   pElement->enmType == RTVFSOBJTYPE_IO_STREAM
        && pReuseElement->enmType == RTVFSOBJTYPE_FILE)
        return true;

    /* I/O stream objects may be file objects. */
    if (   pElement->enmType == RTVFSOBJTYPE_FILE
        && pReuseElement->enmType == RTVFSOBJTYPE_IO_STREAM)
    {
        RTVFSFILE hVfsFile = RTVfsObjToFile(pReuseElement->hVfsObj);
        if (hVfsFile != NIL_RTVFSFILE)
        {
            RTVfsFileRelease(hVfsFile);
            return true;
        }
    }
    return false;
}


RTDECL(int) RTVfsChainSpecCheckAndSetup(PRTVFSCHAINSPEC pSpec, PCRTVFSCHAINSPEC pReuseSpec,
                                        PRTVFSOBJ phVfsObj, const char **ppszFinalPath, uint32_t *poffError, PRTERRINFO pErrInfo)
{
    AssertPtrReturn(poffError, VERR_INVALID_POINTER);
    *poffError = 0;
    AssertPtrReturn(phVfsObj, VERR_INVALID_POINTER);
    *phVfsObj = NIL_RTVFSOBJ;
    AssertPtrReturn(ppszFinalPath, VERR_INVALID_POINTER);
    *ppszFinalPath = NULL;
    AssertPtrReturn(pSpec, VERR_INVALID_POINTER);
    AssertPtrNullReturn(pErrInfo, VERR_INVALID_POINTER);

    /*
     * Check for final path-only component as we will not touch it yet.
     */
    uint32_t cElements = pSpec->cElements;
    if (cElements > 0)
    {
        if (pSpec->paElements[pSpec->cElements - 1].enmType == RTVFSOBJTYPE_END)
        {
            if (cElements > 1)
                cElements--;
            else
            {
                *ppszFinalPath = pSpec->paElements[0].paArgs[0].psz;
                return VERR_VFS_CHAIN_PATH_ONLY;
            }
        }
    }
    else
        return VERR_VFS_CHAIN_EMPTY;

    /*
     * Enter the critical section after making sure it has been initialized.
     */
    int rc = RTOnce(&g_rtVfsChainElementInitOnce, rtVfsChainElementRegisterInit, NULL);
    if (RT_SUCCESS(rc))
        rc = RTCritSectRwEnterShared(&g_rtVfsChainElementCritSect);
    if (RT_SUCCESS(rc))
    {
        /*
         * Resolve and check each element first.
         */
        for (uint32_t i = 0; i < cElements; i++)
        {
            PRTVFSCHAINELEMSPEC const pElement = &pSpec->paElements[i];
            *poffError = pElement->offSpec;
            pElement->pProvider = rtVfsChainFindProviderLocked(pElement->pszProvider);
            if (pElement->pProvider)
            {
                rc = pElement->pProvider->pfnValidate(pElement->pProvider, pSpec, pElement, poffError, pErrInfo);
                if (RT_SUCCESS(rc))
                    continue;
            }
            else
                rc = VERR_VFS_CHAIN_PROVIDER_NOT_FOUND;
            break;
        }

        /*
         * Check that the desired type is compatible with the last element.
         */
        if (RT_SUCCESS(rc))
        {
            PRTVFSCHAINELEMSPEC const pLast = &pSpec->paElements[cElements - 1];
            if (cElements == pSpec->cElements)
            {
                if (   pLast->enmType == pSpec->enmDesiredType
                    || pSpec->enmDesiredType == RTVFSOBJTYPE_BASE
                    || (   pLast->enmType == RTVFSOBJTYPE_FILE
                        && pSpec->enmDesiredType == RTVFSOBJTYPE_IO_STREAM) )
                    rc = VINF_SUCCESS;
                else
                {
                    *poffError = pLast->offSpec;
                    rc = VERR_VFS_CHAIN_FINAL_TYPE_MISMATCH;
                }
            }
            /* Ends with a path-only element, so check the type of the element preceding it. */
            else if (   pLast->enmType == RTVFSOBJTYPE_DIR
                     || pLast->enmType == RTVFSOBJTYPE_VFS
                     || pLast->enmType == RTVFSOBJTYPE_FS_STREAM)
                rc = VINF_SUCCESS;
            else
            {
                *poffError = pLast->offSpec;
                rc = VERR_VFS_CHAIN_TYPE_MISMATCH_PATH_ONLY;
            }
        }

        if (RT_SUCCESS(rc))
        {
            /*
             * Try construct the chain.
             */
            RTVFSOBJ hPrevVfsObj = NIL_RTVFSOBJ; /* No extra reference, kept in chain structure. */
            for (uint32_t i = 0; i < cElements; i++)
            {
                PRTVFSCHAINELEMSPEC const pElement = &pSpec->paElements[i];
                *poffError = pElement->offSpec;

                /*
                 * Try reuse the VFS objects at the start of the passed in reuse chain.
                 */
                if (!pReuseSpec)
                { /* likely */ }
                else
                {
                    if (i < pReuseSpec->cElements)
                    {
                        PRTVFSCHAINELEMSPEC const pReuseElement = &pReuseSpec->paElements[i];
                        if (pReuseElement->hVfsObj != NIL_RTVFSOBJ)
                        {
                            if (strcmp(pElement->pszProvider, pReuseElement->pszProvider) == 0)
                            {
                                if (rtVfsChainMatchReusableType(pElement, pReuseElement))
                                {
                                    if (pElement->pProvider->pfnCanReuseElement(pElement->pProvider, pSpec, pElement,
                                                                                pReuseSpec, pReuseElement))
                                    {
                                        uint32_t cRefs = RTVfsObjRetain(pReuseElement->hVfsObj);
                                        if (cRefs != UINT32_MAX)
                                        {
                                            pElement->hVfsObj = hPrevVfsObj = pReuseElement->hVfsObj;
                                            continue;
                                        }
                                    }
                                }
                            }
                        }
                    }
                    pReuseSpec = NULL;
                }

                /*
                 * Instantiate a new VFS object.
                 */
                RTVFSOBJ hVfsObj = NIL_RTVFSOBJ;
                rc = pElement->pProvider->pfnInstantiate(pElement->pProvider, pSpec, pElement, hPrevVfsObj,
                                                         &hVfsObj, poffError, pErrInfo);
                if (RT_FAILURE(rc))
                    break;
                pElement->hVfsObj = hVfsObj;
                hPrevVfsObj = hVfsObj;
            }

            /*
             * Add another reference to the final object and return.
             */
            if (RT_SUCCESS(rc))
            {
                uint32_t cRefs = RTVfsObjRetain(hPrevVfsObj);
                AssertStmt(cRefs != UINT32_MAX, rc = VERR_VFS_CHAIN_IPE);
                *phVfsObj      = hPrevVfsObj;
                *ppszFinalPath = cElements == pSpec->cElements ? NULL : pSpec->paElements[cElements].paArgs[0].psz;
            }
        }

        int rc2 = RTCritSectRwLeaveShared(&g_rtVfsChainElementCritSect);
        if (RT_FAILURE(rc2) && RT_SUCCESS(rc))
            rc = rc2;
    }
    return rc;
}


RTDECL(int) RTVfsChainElementDeregisterProvider(PRTVFSCHAINELEMENTREG pRegRec, bool fFromDtor)
{
    /*
     * Fend off wildlife.
     */
    if (pRegRec == NULL)
        return VINF_SUCCESS;
    AssertPtrReturn(pRegRec, VERR_INVALID_POINTER);
    AssertMsgReturn(pRegRec->uVersion   == RTVFSCHAINELEMENTREG_VERSION, ("%#x", pRegRec->uVersion),    VERR_INVALID_POINTER);
    AssertMsgReturn(pRegRec->uEndMarker == RTVFSCHAINELEMENTREG_VERSION, ("%#zx", pRegRec->uEndMarker), VERR_INVALID_POINTER);
    AssertPtrReturn(pRegRec->pszName, VERR_INVALID_POINTER);

    /*
     * Take the lock if that's safe.
     */
    if (!fFromDtor)
        RTCritSectRwEnterExcl(&g_rtVfsChainElementCritSect);
    else if (!g_rtVfsChainElementProviderList.pNext)
        RTListInit(&g_rtVfsChainElementProviderList);

    /*
     * Ok, remove it.
     */
    int rc = VERR_NOT_FOUND;
    PRTVFSCHAINELEMENTREG pIterator, pIterNext;
    RTListForEachSafe(&g_rtVfsChainElementProviderList, pIterator, pIterNext, RTVFSCHAINELEMENTREG, ListEntry)
    {
        if (pIterator == pRegRec)
        {
            RTListNodeRemove(&pRegRec->ListEntry);
            rc = VINF_SUCCESS;
            break;
        }
    }

    /*
     * Leave the lock and return.
     */
    if (!fFromDtor)
        RTCritSectRwLeaveExcl(&g_rtVfsChainElementCritSect);
    return rc;
}


RTDECL(int) RTVfsChainOpenObj(const char *pszSpec, uint64_t fFileOpen, uint32_t fObjFlags,
                              PRTVFSOBJ phVfsObj, uint32_t *poffError, PRTERRINFO pErrInfo)
{
    /*
     * Validate input.
     */
    uint32_t offErrorIgn;
    if (!poffError)
        poffError = &offErrorIgn;
    *poffError = 0;
    AssertPtrReturn(pszSpec, VERR_INVALID_POINTER);
    AssertReturn(*pszSpec != '\0', VERR_INVALID_PARAMETER);
    AssertPtrReturn(phVfsObj, VERR_INVALID_POINTER);
    AssertPtrNullReturn(pErrInfo, VERR_INVALID_POINTER);

    int rc = rtFileRecalcAndValidateFlags(&fFileOpen);
    if (RT_FAILURE(rc))
        return rc;
    AssertMsgReturn(   RTPATH_F_IS_VALID(fObjFlags, RTVFSOBJ_F_VALID_MASK)
                    && (fObjFlags & RTVFSOBJ_F_CREATE_MASK) <= RTVFSOBJ_F_CREATE_DIRECTORY,
                    ("fObjFlags=%#x\n", fObjFlags),
                    VERR_INVALID_FLAGS);

    /*
     * Try for a VFS chain first, falling back on regular file system stuff if it's just a path.
     */
    PRTVFSCHAINSPEC pSpec = NULL;
    if (strncmp(pszSpec, RTVFSCHAIN_SPEC_PREFIX, sizeof(RTVFSCHAIN_SPEC_PREFIX) - 1) == 0)
    {
        rc = RTVfsChainSpecParse(pszSpec,  0 /*fFlags*/, RTVFSOBJTYPE_DIR, &pSpec, poffError);
        if (RT_FAILURE(rc))
            return rc;

        Assert(pSpec->cElements > 0);
        if (   pSpec->cElements > 1
            || pSpec->paElements[0].enmType != RTVFSOBJTYPE_END)
        {
            const char *pszFinal = NULL;
            RTVFSOBJ    hVfsObj  = NIL_RTVFSOBJ;
            pSpec->fOpenFile = fFileOpen;
            rc = RTVfsChainSpecCheckAndSetup(pSpec, NULL /*pReuseSpec*/, &hVfsObj, &pszFinal, poffError, pErrInfo);
            if (RT_SUCCESS(rc))
            {
                if (!pszFinal)
                {
                    *phVfsObj = hVfsObj;
                    rc = VINF_SUCCESS;
                }
                else
                {
                    /*
                     * Do a file open with the final path on the returned object.
                     */
                    RTVFS           hVfs    = RTVfsObjToVfs(hVfsObj);
                    RTVFSDIR        hVfsDir = RTVfsObjToDir(hVfsObj);
                    RTVFSFSSTREAM   hVfsFss = RTVfsObjToFsStream(hVfsObj);
                    if (hVfs != NIL_RTVFS)
                        rc = RTVfsObjOpen(hVfs, pszFinal, fFileOpen, fObjFlags, phVfsObj);
                    else if (hVfsDir != NIL_RTVFSDIR)
                        rc = RTVfsDirOpenObj(hVfsDir, pszFinal, fFileOpen, fObjFlags, phVfsObj);
                    else if (hVfsFss != NIL_RTVFSFSSTREAM)
                        rc = VERR_NOT_IMPLEMENTED;
                    else
                        rc = VERR_VFS_CHAIN_TYPE_MISMATCH_PATH_ONLY;
                    RTVfsRelease(hVfs);
                    RTVfsDirRelease(hVfsDir);
                    RTVfsFsStrmRelease(hVfsFss);
                    RTVfsObjRelease(hVfsObj);
                }
            }

            RTVfsChainSpecFree(pSpec);
            return rc;
        }

        /* Only a path element. */
        pszSpec = pSpec->paElements[0].paArgs[0].psz;
    }

    /*
     * Path to regular file system.
     * Go via the directory VFS wrapper to avoid duplicating code.
     */
    RTVFSDIR hVfsParentDir = NIL_RTVFSDIR;
    const char *pszFilename;
    if (RTPathHasPath(pszSpec))
    {
        char *pszCopy = RTStrDup(pszSpec);
        if (pszCopy)
        {
            RTPathStripFilename(pszCopy);
            rc = RTVfsDirOpenNormal(pszCopy, 0 /*fOpen*/, &hVfsParentDir);
            RTStrFree(pszCopy);
        }
        else
            rc = VERR_NO_STR_MEMORY;
        pszFilename = RTPathFilename(pszSpec);
    }
    else
    {
        pszFilename = pszSpec;
        rc = RTVfsDirOpenNormal(".", 0 /*fOpen*/, &hVfsParentDir);
    }
    if (RT_SUCCESS(rc))
    {
        rc = RTVfsDirOpenObj(hVfsParentDir, pszFilename, fFileOpen, fObjFlags, phVfsObj);
        RTVfsDirRelease(hVfsParentDir);
    }

    RTVfsChainSpecFree(pSpec);
    return rc;
}


RTDECL(int) RTVfsChainOpenDir(const char *pszSpec, uint32_t fOpen,
                              PRTVFSDIR phVfsDir, uint32_t *poffError, PRTERRINFO pErrInfo)
{
    uint32_t offErrorIgn;
    if (!poffError)
        poffError = &offErrorIgn;
    *poffError = 0;
    AssertPtrReturn(pszSpec, VERR_INVALID_POINTER);
    AssertReturn(*pszSpec != '\0', VERR_INVALID_PARAMETER);
    AssertPtrReturn(phVfsDir, VERR_INVALID_POINTER);
    AssertPtrNullReturn(pErrInfo, VERR_INVALID_POINTER);

    /*
     * Try for a VFS chain first, falling back on regular file system stuff if it's just a path.
     */
    int rc;
    PRTVFSCHAINSPEC pSpec = NULL;
    if (strncmp(pszSpec, RTVFSCHAIN_SPEC_PREFIX, sizeof(RTVFSCHAIN_SPEC_PREFIX) - 1) == 0)
    {
        rc = RTVfsChainSpecParse(pszSpec,  0 /*fFlags*/, RTVFSOBJTYPE_DIR, &pSpec, poffError);
        if (RT_FAILURE(rc))
            return rc;

        Assert(pSpec->cElements > 0);
        if (   pSpec->cElements > 1
            || pSpec->paElements[0].enmType != RTVFSOBJTYPE_END)
        {
            const char *pszFinal = NULL;
            RTVFSOBJ    hVfsObj  = NIL_RTVFSOBJ;
            pSpec->fOpenFile = RTFILE_O_READ;
            rc = RTVfsChainSpecCheckAndSetup(pSpec, NULL /*pReuseSpec*/, &hVfsObj, &pszFinal, poffError, pErrInfo);
            if (RT_SUCCESS(rc))
            {
                if (!pszFinal)
                {
                    /* Try convert it to a directory object and we're done. */
                    *phVfsDir = RTVfsObjToDir(hVfsObj);
                    if (*phVfsDir)
                        rc = VINF_SUCCESS;
                    else
                        rc = VERR_VFS_CHAIN_CAST_FAILED;
                }
                else
                {
                    /*
                     * Do a file open with the final path on the returned object.
                     */
                    RTVFS           hVfs    = RTVfsObjToVfs(hVfsObj);
                    RTVFSDIR        hVfsDir = RTVfsObjToDir(hVfsObj);
                    RTVFSFSSTREAM   hVfsFss = RTVfsObjToFsStream(hVfsObj);
                    if (hVfs != NIL_RTVFS)
                        rc = RTVfsDirOpen(hVfs, pszFinal, fOpen, phVfsDir);
                    else if (hVfsDir != NIL_RTVFSDIR)
                        rc = RTVfsDirOpenDir(hVfsDir, pszFinal, fOpen, phVfsDir);
                    else if (hVfsFss != NIL_RTVFSFSSTREAM)
                        rc = VERR_NOT_IMPLEMENTED;
                    else
                        rc = VERR_VFS_CHAIN_TYPE_MISMATCH_PATH_ONLY;
                    RTVfsRelease(hVfs);
                    RTVfsDirRelease(hVfsDir);
                    RTVfsFsStrmRelease(hVfsFss);
                }
                RTVfsObjRelease(hVfsObj);
            }

            RTVfsChainSpecFree(pSpec);
            return rc;
        }

        /* Only a path element. */
        pszSpec = pSpec->paElements[0].paArgs[0].psz;
    }

    /*
     * Path to regular file system.
     */
    rc = RTVfsDirOpenNormal(pszSpec, fOpen, phVfsDir);

    RTVfsChainSpecFree(pSpec);
    return rc;
}


RTDECL(int) RTVfsChainOpenParentDir(const char *pszSpec, uint32_t fOpen, PRTVFSDIR phVfsDir, const char **ppszChild,
                                    uint32_t *poffError, PRTERRINFO pErrInfo)
{
    uint32_t offErrorIgn;
    if (!poffError)
        poffError = &offErrorIgn;
    *poffError = 0;
    AssertPtrReturn(pszSpec, VERR_INVALID_POINTER);
    AssertReturn(*pszSpec != '\0', VERR_INVALID_PARAMETER);
    AssertPtrReturn(phVfsDir, VERR_INVALID_POINTER);
    AssertPtrReturn(ppszChild, VERR_INVALID_POINTER);
    *ppszChild = NULL;
    AssertPtrNullReturn(pErrInfo, VERR_INVALID_POINTER);

    /*
     * Process the spec from the end, trying to find the child part of it.
     * We cannot use RTPathFilename here because we must ignore trailing slashes.
     */
    const char * const pszEnd   = RTStrEnd(pszSpec, RTSTR_MAX);
    const char        *pszChild = pszEnd;
    while (   pszChild != pszSpec
           && RTPATH_IS_SLASH(pszChild[-1]))
        pszChild--;
    while (   pszChild != pszSpec
           && !RTPATH_IS_SLASH(pszChild[-1])
           && !RTPATH_IS_VOLSEP(pszChild[-1]))
        pszChild--;
    size_t const cchChild = pszEnd - pszChild;
    *ppszChild = pszChild;

    /*
     * Try for a VFS chain first, falling back on regular file system stuff if it's just a path.
     */
    int rc;
    PRTVFSCHAINSPEC pSpec = NULL;
    if (strncmp(pszSpec, RTVFSCHAIN_SPEC_PREFIX, sizeof(RTVFSCHAIN_SPEC_PREFIX) - 1) == 0)
    {
        rc = RTVfsChainSpecParse(pszSpec,  0 /*fFlags*/, RTVFSOBJTYPE_DIR, &pSpec, poffError);
        if (RT_FAILURE(rc))
            return rc;

        Assert(pSpec->cElements > 0);
        if (   pSpec->cElements > 1
            || pSpec->paElements[0].enmType != RTVFSOBJTYPE_END)
        {
            /*
             * Check that it ends with a path-only element and that this in turn ends with
             * what pszChild points to.  (We cannot easiy figure out the parent part of
             * an element that isn't path-only, so we don't bother trying try.)
             */
            PRTVFSCHAINELEMSPEC pLast = &pSpec->paElements[pSpec->cElements - 1];
            if (pLast->pszProvider == NULL)
            {
                size_t cchFinal = strlen(pLast->paArgs[0].psz);
                if (   cchFinal >= cchChild
                    && memcmp(&pLast->paArgs[0].psz[cchFinal - cchChild], pszChild, cchChild + 1) == 0)
                {
                    /*
                     * Drop the child part so we have a path to the parent, then setup the chain.
                     */
                    if (cchFinal > cchChild)
                        pLast->paArgs[0].psz[cchFinal - cchChild] = '\0';
                    else
                        pSpec->cElements--;

                    const char *pszFinal = NULL;
                    RTVFSOBJ    hVfsObj  = NIL_RTVFSOBJ;
                    pSpec->fOpenFile = fOpen;
                    rc = RTVfsChainSpecCheckAndSetup(pSpec, NULL /*pReuseSpec*/, &hVfsObj, &pszFinal, poffError, pErrInfo);
                    if (RT_SUCCESS(rc))
                    {
                        if (!pszFinal)
                        {
                            Assert(cchFinal == cchChild);

                            /* Try convert it to a file object and we're done. */
                            *phVfsDir = RTVfsObjToDir(hVfsObj);
                            if (*phVfsDir)
                                rc = VINF_SUCCESS;
                            else
                                rc = VERR_VFS_CHAIN_CAST_FAILED;
                        }
                        else
                        {
                            /*
                             * Do a file open with the final path on the returned object.
                             */
                            RTVFS           hVfs    = RTVfsObjToVfs(hVfsObj);
                            RTVFSDIR        hVfsDir = RTVfsObjToDir(hVfsObj);
                            RTVFSFSSTREAM   hVfsFss = RTVfsObjToFsStream(hVfsObj);
                            if (hVfs != NIL_RTVFS)
                                rc = RTVfsDirOpen(hVfs, pszFinal, fOpen, phVfsDir);
                            else if (hVfsDir != NIL_RTVFSDIR)
                                rc = RTVfsDirOpenDir(hVfsDir, pszFinal, fOpen, phVfsDir);
                            else if (hVfsFss != NIL_RTVFSFSSTREAM)
                                rc = VERR_NOT_IMPLEMENTED;
                            else
                                rc = VERR_VFS_CHAIN_TYPE_MISMATCH_PATH_ONLY;
                            RTVfsRelease(hVfs);
                            RTVfsDirRelease(hVfsDir);
                            RTVfsFsStrmRelease(hVfsFss);
                        }
                        RTVfsObjRelease(hVfsObj);
                    }
                }
                else
                    rc = VERR_VFS_CHAIN_TOO_SHORT_FOR_PARENT;
            }
            else
                rc = VERR_VFS_CHAIN_NOT_PATH_ONLY;

            RTVfsChainSpecFree(pSpec);
            return rc;
        }

        /* Only a path element. */
        pszSpec = pSpec->paElements[0].paArgs[0].psz;
    }

    /*
     * Path to regular file system.
     */
    if (RTPathHasPath(pszSpec))
    {
        char *pszCopy = RTStrDup(pszSpec);
        if (pszCopy)
        {
            RTPathStripFilename(pszCopy);
            rc = RTVfsDirOpenNormal(pszCopy, fOpen, phVfsDir);
            RTStrFree(pszCopy);
        }
        else
            rc = VERR_NO_STR_MEMORY;
    }
    else
        rc = RTVfsDirOpenNormal(".", fOpen, phVfsDir);

    RTVfsChainSpecFree(pSpec);
    return rc;

}


RTDECL(int) RTVfsChainOpenFile(const char *pszSpec, uint64_t fOpen,
                               PRTVFSFILE phVfsFile, uint32_t *poffError, PRTERRINFO pErrInfo)
{
    uint32_t offErrorIgn;
    if (!poffError)
        poffError = &offErrorIgn;
    *poffError = 0;
    AssertPtrReturn(pszSpec, VERR_INVALID_POINTER);
    AssertReturn(*pszSpec != '\0', VERR_INVALID_PARAMETER);
    AssertPtrReturn(phVfsFile, VERR_INVALID_POINTER);
    AssertPtrNullReturn(pErrInfo, VERR_INVALID_POINTER);

    /*
     * Try for a VFS chain first, falling back on regular file system stuff if it's just a path.
     */
    int rc;
    PRTVFSCHAINSPEC pSpec = NULL;
    if (strncmp(pszSpec, RTVFSCHAIN_SPEC_PREFIX, sizeof(RTVFSCHAIN_SPEC_PREFIX) - 1) == 0)
    {
        rc = RTVfsChainSpecParse(pszSpec,  0 /*fFlags*/, RTVFSOBJTYPE_FILE, &pSpec, poffError);
        if (RT_FAILURE(rc))
            return rc;

        Assert(pSpec->cElements > 0);
        if (   pSpec->cElements > 1
            || pSpec->paElements[0].enmType != RTVFSOBJTYPE_END)
        {
            const char *pszFinal = NULL;
            RTVFSOBJ    hVfsObj  = NIL_RTVFSOBJ;
            pSpec->fOpenFile = fOpen;
            rc = RTVfsChainSpecCheckAndSetup(pSpec, NULL /*pReuseSpec*/, &hVfsObj, &pszFinal, poffError, pErrInfo);
            if (RT_SUCCESS(rc))
            {
                if (!pszFinal)
                {
                    /* Try convert it to a file object and we're done. */
                    *phVfsFile = RTVfsObjToFile(hVfsObj);
                    if (*phVfsFile)
                        rc = VINF_SUCCESS;
                    else
                        rc = VERR_VFS_CHAIN_CAST_FAILED;
                }
                else
                {
                    /*
                     * Do a file open with the final path on the returned object.
                     */
                    RTVFS           hVfs    = RTVfsObjToVfs(hVfsObj);
                    RTVFSDIR        hVfsDir = RTVfsObjToDir(hVfsObj);
                    RTVFSFSSTREAM   hVfsFss = RTVfsObjToFsStream(hVfsObj);
                    if (hVfs != NIL_RTVFS)
                        rc = RTVfsFileOpen(hVfs, pszFinal, fOpen, phVfsFile);
                    else if (hVfsDir != NIL_RTVFSDIR)
                        rc = RTVfsDirOpenFile(hVfsDir, pszFinal, fOpen, phVfsFile);
                    else if (hVfsFss != NIL_RTVFSFSSTREAM)
                        rc = VERR_NOT_IMPLEMENTED;
                    else
                        rc = VERR_VFS_CHAIN_TYPE_MISMATCH_PATH_ONLY;
                    RTVfsRelease(hVfs);
                    RTVfsDirRelease(hVfsDir);
                    RTVfsFsStrmRelease(hVfsFss);
                }
                RTVfsObjRelease(hVfsObj);
            }

            RTVfsChainSpecFree(pSpec);
            return rc;
        }

        /* Only a path element. */
        pszSpec = pSpec->paElements[0].paArgs[0].psz;
    }

    /*
     * Path to regular file system.
     */
    RTFILE hFile;
    rc = RTFileOpen(&hFile, pszSpec, fOpen);
    if (RT_SUCCESS(rc))
    {
        RTVFSFILE hVfsFile;
        rc = RTVfsFileFromRTFile(hFile, fOpen, false /*fLeaveOpen*/, &hVfsFile);
        if (RT_SUCCESS(rc))
            *phVfsFile = hVfsFile;
        else
            RTFileClose(hFile);
    }

    RTVfsChainSpecFree(pSpec);
    return rc;
}


RTDECL(int) RTVfsChainOpenIoStream(const char *pszSpec, uint64_t fOpen,
                                   PRTVFSIOSTREAM phVfsIos, uint32_t *poffError, PRTERRINFO pErrInfo)
{
    uint32_t offErrorIgn;
    if (!poffError)
        poffError = &offErrorIgn;
    *poffError = 0;
    AssertPtrReturn(pszSpec, VERR_INVALID_POINTER);
    AssertReturn(*pszSpec != '\0', VERR_INVALID_PARAMETER);
    AssertPtrReturn(phVfsIos, VERR_INVALID_POINTER);
    AssertPtrNullReturn(pErrInfo, VERR_INVALID_POINTER);

    /*
     * Try for a VFS chain first, falling back on regular file system stuff if it's just a path.
     */
    int rc;
    PRTVFSCHAINSPEC pSpec = NULL;
    if (strncmp(pszSpec, RTVFSCHAIN_SPEC_PREFIX, sizeof(RTVFSCHAIN_SPEC_PREFIX) - 1) == 0)
    {
        rc = RTVfsChainSpecParse(pszSpec,  0 /*fFlags*/, RTVFSOBJTYPE_IO_STREAM, &pSpec, poffError);
        if (RT_FAILURE(rc))
            return rc;

        Assert(pSpec->cElements > 0);
        if (   pSpec->cElements > 1
            || pSpec->paElements[0].enmType != RTVFSOBJTYPE_END)
        {
            const char *pszFinal = NULL;
            RTVFSOBJ    hVfsObj  = NIL_RTVFSOBJ;
            pSpec->fOpenFile = fOpen;
            rc = RTVfsChainSpecCheckAndSetup(pSpec, NULL /*pReuseSpec*/, &hVfsObj, &pszFinal, poffError, pErrInfo);
            if (RT_SUCCESS(rc))
            {
                if (!pszFinal)
                {
                    /* Try convert it to an I/O object and we're done. */
                    *phVfsIos = RTVfsObjToIoStream(hVfsObj);
                    if (*phVfsIos)
                        rc = VINF_SUCCESS;
                    else
                        rc = VERR_VFS_CHAIN_CAST_FAILED;
                }
                else
                {
                    /*
                     * Do a file open with the final path on the returned object.
                     */
                    RTVFS           hVfs    = RTVfsObjToVfs(hVfsObj);
                    RTVFSDIR        hVfsDir = RTVfsObjToDir(hVfsObj);
                    RTVFSFSSTREAM   hVfsFss = RTVfsObjToFsStream(hVfsObj);
                    RTVFSFILE       hVfsFile = NIL_RTVFSFILE;
                    if (hVfs != NIL_RTVFS)
                        rc = RTVfsFileOpen(hVfs, pszFinal, fOpen, &hVfsFile);
                    else if (hVfsDir != NIL_RTVFSDIR)
                        rc = RTVfsDirOpenFile(hVfsDir, pszFinal, fOpen, &hVfsFile);
                    else if (hVfsFss != NIL_RTVFSFSSTREAM)
                        rc = VERR_NOT_IMPLEMENTED;
                    else
                        rc = VERR_VFS_CHAIN_TYPE_MISMATCH_PATH_ONLY;
                    if (RT_SUCCESS(rc))
                    {
                        *phVfsIos = RTVfsFileToIoStream(hVfsFile);
                        if (*phVfsIos)
                            rc = VINF_SUCCESS;
                        else
                            rc = VERR_VFS_CHAIN_CAST_FAILED;
                        RTVfsFileRelease(hVfsFile);
                    }
                    RTVfsRelease(hVfs);
                    RTVfsDirRelease(hVfsDir);
                    RTVfsFsStrmRelease(hVfsFss);
                }
                RTVfsObjRelease(hVfsObj);
            }

            RTVfsChainSpecFree(pSpec);
            return rc;
        }

        /* Only a path element. */
        pszSpec = pSpec->paElements[0].paArgs[0].psz;
    }

    /*
     * Path to regular file system.
     */
    RTFILE hFile;
    rc = RTFileOpen(&hFile, pszSpec, fOpen);
    if (RT_SUCCESS(rc))
    {
        RTVFSFILE hVfsFile;
        rc = RTVfsFileFromRTFile(hFile, fOpen, false /*fLeaveOpen*/, &hVfsFile);
        if (RT_SUCCESS(rc))
        {
            *phVfsIos = RTVfsFileToIoStream(hVfsFile);
            RTVfsFileRelease(hVfsFile);
        }
        else
            RTFileClose(hFile);
    }

    RTVfsChainSpecFree(pSpec);
    return rc;
}


/**
 * The equivalent of RTPathQueryInfoEx
 */
RTDECL(int) RTVfsChainQueryInfo(const char *pszSpec, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAdditionalAttribs,
                                uint32_t fFlags, uint32_t *poffError, PRTERRINFO pErrInfo)
{
    uint32_t offErrorIgn;
    if (!poffError)
        poffError = &offErrorIgn;
    *poffError = 0;
    AssertPtrReturn(pszSpec, VERR_INVALID_POINTER);
    AssertReturn(*pszSpec != '\0', VERR_INVALID_PARAMETER);
    AssertPtrReturn(pObjInfo, VERR_INVALID_POINTER);
    AssertReturn(enmAdditionalAttribs >= RTFSOBJATTRADD_NOTHING && enmAdditionalAttribs <= RTFSOBJATTRADD_LAST,
                 VERR_INVALID_PARAMETER);
    AssertPtrNullReturn(pErrInfo, VERR_INVALID_POINTER);

    /*
     * Try for a VFS chain first, falling back on regular file system stuff if it's just a path.
     */
    int rc;
    PRTVFSCHAINSPEC pSpec = NULL;
    if (strncmp(pszSpec, RTVFSCHAIN_SPEC_PREFIX, sizeof(RTVFSCHAIN_SPEC_PREFIX) - 1) == 0)
    {
        rc = RTVfsChainSpecParse(pszSpec,  0 /*fFlags*/, RTVFSOBJTYPE_BASE, &pSpec, poffError);
        if (RT_FAILURE(rc))
            return rc;

        Assert(pSpec->cElements > 0);
        if (   pSpec->cElements > 1
            || pSpec->paElements[0].enmType != RTVFSOBJTYPE_END)
        {
            const char *pszFinal = NULL;
            RTVFSOBJ    hVfsObj  = NIL_RTVFSOBJ;
            pSpec->fOpenFile = RTFILE_O_READ | RTFILE_O_OPEN;
            rc = RTVfsChainSpecCheckAndSetup(pSpec, NULL /*pReuseSpec*/, &hVfsObj, &pszFinal, poffError, pErrInfo);
            if (RT_SUCCESS(rc))
            {
                if (!pszFinal)
                {
                    /*
                     * Do the job on the final object.
                     */
                    rc = RTVfsObjQueryInfo(hVfsObj, pObjInfo, enmAdditionalAttribs);
                }
                else
                {
                    /*
                     * Do a path query operation on the penultimate object.
                     */
                    RTVFS           hVfs    = RTVfsObjToVfs(hVfsObj);
                    RTVFSDIR        hVfsDir = RTVfsObjToDir(hVfsObj);
                    RTVFSFSSTREAM   hVfsFss = RTVfsObjToFsStream(hVfsObj);
                    if (hVfs != NIL_RTVFS)
                        rc = RTVfsQueryPathInfo(hVfs, pszFinal, pObjInfo, enmAdditionalAttribs, fFlags);
                    else if (hVfsDir != NIL_RTVFSDIR)
                        rc = RTVfsDirQueryPathInfo(hVfsDir, pszFinal, pObjInfo, enmAdditionalAttribs, fFlags);
                    else if (hVfsFss != NIL_RTVFSFSSTREAM)
                        rc = VERR_NOT_SUPPORTED;
                    else
                        rc = VERR_VFS_CHAIN_TYPE_MISMATCH_PATH_ONLY;
                    RTVfsRelease(hVfs);
                    RTVfsDirRelease(hVfsDir);
                    RTVfsFsStrmRelease(hVfsFss);
                }
                RTVfsObjRelease(hVfsObj);
            }

            RTVfsChainSpecFree(pSpec);
            return rc;
        }

        /* Only a path element. */
        pszSpec = pSpec->paElements[0].paArgs[0].psz;
    }

    /*
     * Path to regular file system.
     */
    rc = RTPathQueryInfoEx(pszSpec, pObjInfo, enmAdditionalAttribs, fFlags);

    RTVfsChainSpecFree(pSpec);
    return rc;
}


RTDECL(bool) RTVfsChainIsSpec(const char *pszSpec)
{
    return pszSpec
        && strncmp(pszSpec, RT_STR_TUPLE(RTVFSCHAIN_SPEC_PREFIX)) == 0;
}


RTDECL(int) RTVfsChainQueryFinalPath(const char *pszSpec, char **ppszFinalPath, uint32_t *poffError)
{
    /* Make sure we've got an error info variable. */
    uint32_t offErrorIgn;
    if (!poffError)
        poffError = &offErrorIgn;
    *poffError = 0;

    /*
     * If not chain specifier, just duplicate the input and return.
     */
    if (strncmp(pszSpec, RTVFSCHAIN_SPEC_PREFIX, sizeof(RTVFSCHAIN_SPEC_PREFIX) - 1) != 0)
        return RTStrDupEx(ppszFinalPath, pszSpec);

    /*
     * Parse it and check out the last element.
     */
    PRTVFSCHAINSPEC pSpec = NULL;
    int rc = RTVfsChainSpecParse(pszSpec,  0 /*fFlags*/, RTVFSOBJTYPE_BASE, &pSpec, poffError);
    if (RT_SUCCESS(rc))
    {
        PCRTVFSCHAINELEMSPEC pLast = &pSpec->paElements[pSpec->cElements - 1];
        if (pLast->pszProvider == NULL)
            rc = RTStrDupEx(ppszFinalPath, pLast->paArgs[0].psz);
        else
        {
            rc = VERR_VFS_CHAIN_NOT_PATH_ONLY;
            *poffError = pLast->offSpec;
        }
        RTVfsChainSpecFree(pSpec);
    }
    return rc;
}


RTDECL(int) RTVfsChainSplitOffFinalPath(char *pszSpec, char **ppszSpec, char **ppszFinalPath, uint32_t *poffError)
{
    /* Make sure we've got an error info variable. */
    uint32_t offErrorIgn;
    if (!poffError)
        poffError = &offErrorIgn;
    *poffError = 0;

    /*
     * If not chain specifier, just duplicate the input and return.
     */
    if (strncmp(pszSpec, RTVFSCHAIN_SPEC_PREFIX, sizeof(RTVFSCHAIN_SPEC_PREFIX) - 1) != 0)
    {
        *ppszSpec      = NULL;
        *ppszFinalPath = pszSpec;
        return VINF_SUCCESS;
    }

    /*
     * Parse it and check out the last element.
     */
    PRTVFSCHAINSPEC pSpec = NULL;
    int rc = RTVfsChainSpecParse(pszSpec,  0 /*fFlags*/, RTVFSOBJTYPE_BASE, &pSpec, poffError);
    if (RT_SUCCESS(rc))
    {
        Assert(pSpec->cElements > 0);
        PCRTVFSCHAINELEMSPEC pLast = &pSpec->paElements[pSpec->cElements - 1];
        if (pLast->pszProvider == NULL)
        {
            char *psz = &pszSpec[pLast->offSpec];
            *ppszFinalPath = psz;
            if (pSpec->cElements > 1)
            {
                *ppszSpec = pszSpec;

                /* Remove the separator and any whitespace around it. */
                while (   psz != pszSpec
                       && RT_C_IS_SPACE(psz[-1]))
                    psz--;
                if (    psz != pszSpec
                    && (   psz[-1] == ':'
                        || psz[-1] == '|'))
                    psz--;
                while (   psz != pszSpec
                       && RT_C_IS_SPACE(psz[-1]))
                    psz--;
                *psz = '\0';
            }
            else
                *ppszSpec = NULL;
        }
        else
        {
            *ppszFinalPath = NULL;
            *ppszSpec      = pszSpec;
        }
        RTVfsChainSpecFree(pSpec);
    }
    else
    {
        *ppszSpec      = NULL;
        *ppszFinalPath = NULL;
    }
    return rc;
}