1 | /* $Id: comparepaths.cpp 93115 2022-01-01 11:31:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Path Comparison.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2022 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*********************************************************************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *********************************************************************************************************************************/
|
---|
31 | #include "internal/iprt.h"
|
---|
32 | #include <iprt/path.h>
|
---|
33 | #include <iprt/errcore.h>
|
---|
34 | #include <iprt/string.h>
|
---|
35 | #include <iprt/uni.h>
|
---|
36 |
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * Helper for RTPathCompare() and RTPathStartsWith().
|
---|
40 | *
|
---|
41 | * @returns similar to strcmp.
|
---|
42 | * @param pszPath1 Path to compare.
|
---|
43 | * @param pszPath2 Path to compare.
|
---|
44 | * @param fLimit Limit the comparison to the length of \a pszPath2
|
---|
45 | * @internal
|
---|
46 | */
|
---|
47 | static int rtPathCompare(const char *pszPath1, const char *pszPath2, bool fLimit)
|
---|
48 | {
|
---|
49 | if (pszPath1 == pszPath2)
|
---|
50 | return 0;
|
---|
51 | if (!pszPath1)
|
---|
52 | return -1;
|
---|
53 | if (!pszPath2)
|
---|
54 | return 1;
|
---|
55 |
|
---|
56 | #if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
|
---|
57 | for (;;)
|
---|
58 | {
|
---|
59 | RTUNICP uc1;
|
---|
60 | int rc = RTStrGetCpEx(&pszPath1, &uc1);
|
---|
61 | if (RT_SUCCESS(rc))
|
---|
62 | {
|
---|
63 | RTUNICP uc2;
|
---|
64 | rc = RTStrGetCpEx(&pszPath2, &uc2);
|
---|
65 | if (RT_SUCCESS(rc))
|
---|
66 | {
|
---|
67 | if (uc1 == uc2)
|
---|
68 | {
|
---|
69 | if (uc1)
|
---|
70 | { /* likely */ }
|
---|
71 | else
|
---|
72 | return 0;
|
---|
73 | }
|
---|
74 | else
|
---|
75 | {
|
---|
76 | if (uc1 == '\\')
|
---|
77 | uc1 = '/';
|
---|
78 | else
|
---|
79 | uc1 = RTUniCpToUpper(uc1);
|
---|
80 | if (uc2 == '\\')
|
---|
81 | uc2 = '/';
|
---|
82 | else
|
---|
83 | uc2 = RTUniCpToUpper(uc2);
|
---|
84 | if (uc1 != uc2)
|
---|
85 | {
|
---|
86 | if (fLimit && uc2 == '\0')
|
---|
87 | return 0;
|
---|
88 | return uc1 > uc2 ? 1 : -1; /* (overflow/underflow paranoia) */
|
---|
89 | }
|
---|
90 | }
|
---|
91 | }
|
---|
92 | else
|
---|
93 | return 1;
|
---|
94 | }
|
---|
95 | else
|
---|
96 | return -1;
|
---|
97 | }
|
---|
98 | #else
|
---|
99 | if (!fLimit)
|
---|
100 | return strcmp(pszPath1, pszPath2);
|
---|
101 | return strncmp(pszPath1, pszPath2, strlen(pszPath2));
|
---|
102 | #endif
|
---|
103 | }
|
---|
104 |
|
---|
105 |
|
---|
106 | /**
|
---|
107 | * Compares two paths.
|
---|
108 | *
|
---|
109 | * The comparison takes platform-dependent details into account,
|
---|
110 | * such as:
|
---|
111 | * <ul>
|
---|
112 | * <li>On DOS-like platforms, both separator chars (|\| and |/|) are considered
|
---|
113 | * to be equal.
|
---|
114 | * <li>On platforms with case-insensitive file systems, mismatching characters
|
---|
115 | * are uppercased and compared again.
|
---|
116 | * </ul>
|
---|
117 | *
|
---|
118 | * @returns @< 0 if the first path less than the second path.
|
---|
119 | * @returns 0 if the first path identical to the second path.
|
---|
120 | * @returns @> 0 if the first path greater than the second path.
|
---|
121 | *
|
---|
122 | * @param pszPath1 Path to compare (must be an absolute path).
|
---|
123 | * @param pszPath2 Path to compare (must be an absolute path).
|
---|
124 | *
|
---|
125 | * @remarks File system details are currently ignored. This means that you won't
|
---|
126 | * get case-insensitive compares on unix systems when a path goes into a
|
---|
127 | * case-insensitive filesystem like FAT, HPFS, HFS, NTFS, JFS, or
|
---|
128 | * similar. For NT, OS/2 and similar you'll won't get case-sensitive
|
---|
129 | * compares on a case-sensitive file system.
|
---|
130 | */
|
---|
131 | RTDECL(int) RTPathCompare(const char *pszPath1, const char *pszPath2)
|
---|
132 | {
|
---|
133 | return rtPathCompare(pszPath1, pszPath2, false /* full path lengths */);
|
---|
134 | }
|
---|
135 |
|
---|
136 |
|
---|
137 | /**
|
---|
138 | * Checks if a path starts with the given parent path.
|
---|
139 | *
|
---|
140 | * This means that either the path and the parent path matches completely, or
|
---|
141 | * that the path is to some file or directory residing in the tree given by the
|
---|
142 | * parent directory.
|
---|
143 | *
|
---|
144 | * The path comparison takes platform-dependent details into account,
|
---|
145 | * see RTPathCompare() for details.
|
---|
146 | *
|
---|
147 | * @returns |true| when \a pszPath starts with \a pszParentPath (or when they
|
---|
148 | * are identical), or |false| otherwise.
|
---|
149 | *
|
---|
150 | * @param pszPath Path to check, must be an absolute path.
|
---|
151 | * @param pszParentPath Parent path, must be an absolute path.
|
---|
152 | * No trailing directory slash!
|
---|
153 | */
|
---|
154 | RTDECL(bool) RTPathStartsWith(const char *pszPath, const char *pszParentPath)
|
---|
155 | {
|
---|
156 | if (pszPath == pszParentPath)
|
---|
157 | return true;
|
---|
158 | if (!pszPath || !pszParentPath)
|
---|
159 | return false;
|
---|
160 |
|
---|
161 | if (rtPathCompare(pszPath, pszParentPath, true /* limited by path 2 */) != 0)
|
---|
162 | return false;
|
---|
163 |
|
---|
164 | const size_t cchParentPath = strlen(pszParentPath);
|
---|
165 | if (RTPATH_IS_SLASH(pszPath[cchParentPath]))
|
---|
166 | return true;
|
---|
167 | if (pszPath[cchParentPath] == '\0')
|
---|
168 | return true;
|
---|
169 |
|
---|
170 | /* Deal with pszParentPath = root (or having a trailing slash). */
|
---|
171 | if ( cchParentPath > 0
|
---|
172 | && RTPATH_IS_SLASH(pszParentPath[cchParentPath - 1])
|
---|
173 | && RTPATH_IS_SLASH(pszPath[cchParentPath - 1]))
|
---|
174 | return true;
|
---|
175 |
|
---|
176 | return false;
|
---|
177 | }
|
---|
178 |
|
---|