diff options
Diffstat (limited to 'src/libs/dutil/WixToolset.DUtil/path2utl.cpp')
-rw-r--r-- | src/libs/dutil/WixToolset.DUtil/path2utl.cpp | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/src/libs/dutil/WixToolset.DUtil/path2utl.cpp b/src/libs/dutil/WixToolset.DUtil/path2utl.cpp new file mode 100644 index 00000000..ff3a946d --- /dev/null +++ b/src/libs/dutil/WixToolset.DUtil/path2utl.cpp | |||
@@ -0,0 +1,104 @@ | |||
1 | // Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. | ||
2 | |||
3 | #include "precomp.h" | ||
4 | |||
5 | |||
6 | // Exit macros | ||
7 | #define PathExitOnLastError(x, s, ...) ExitOnLastErrorSource(DUTIL_SOURCE_PATHUTIL, x, s, __VA_ARGS__) | ||
8 | #define PathExitOnLastErrorDebugTrace(x, s, ...) ExitOnLastErrorDebugTraceSource(DUTIL_SOURCE_PATHUTIL, x, s, __VA_ARGS__) | ||
9 | #define PathExitWithLastError(x, s, ...) ExitWithLastErrorSource(DUTIL_SOURCE_PATHUTIL, x, s, __VA_ARGS__) | ||
10 | #define PathExitOnFailure(x, s, ...) ExitOnFailureSource(DUTIL_SOURCE_PATHUTIL, x, s, __VA_ARGS__) | ||
11 | #define PathExitOnRootFailure(x, s, ...) ExitOnRootFailureSource(DUTIL_SOURCE_PATHUTIL, x, s, __VA_ARGS__) | ||
12 | #define PathExitOnFailureDebugTrace(x, s, ...) ExitOnFailureDebugTraceSource(DUTIL_SOURCE_PATHUTIL, x, s, __VA_ARGS__) | ||
13 | #define PathExitOnNull(p, x, e, s, ...) ExitOnNullSource(DUTIL_SOURCE_PATHUTIL, p, x, e, s, __VA_ARGS__) | ||
14 | #define PathExitOnNullWithLastError(p, x, s, ...) ExitOnNullWithLastErrorSource(DUTIL_SOURCE_PATHUTIL, p, x, s, __VA_ARGS__) | ||
15 | #define PathExitOnNullDebugTrace(p, x, e, s, ...) ExitOnNullDebugTraceSource(DUTIL_SOURCE_PATHUTIL, p, x, e, s, __VA_ARGS__) | ||
16 | #define PathExitOnInvalidHandleWithLastError(p, x, s, ...) ExitOnInvalidHandleWithLastErrorSource(DUTIL_SOURCE_PATHUTIL, p, x, s, __VA_ARGS__) | ||
17 | #define PathExitOnWin32Error(e, x, s, ...) ExitOnWin32ErrorSource(DUTIL_SOURCE_PATHUTIL, e, x, s, __VA_ARGS__) | ||
18 | #define PathExitOnGdipFailure(g, x, s, ...) ExitOnGdipFailureSource(DUTIL_SOURCE_PATHUTIL, g, x, s, __VA_ARGS__) | ||
19 | |||
20 | |||
21 | DAPI_(HRESULT) PathCanonicalizePath( | ||
22 | __in_z LPCWSTR wzPath, | ||
23 | __deref_out_z LPWSTR* psczCanonicalized | ||
24 | ) | ||
25 | { | ||
26 | HRESULT hr = S_OK; | ||
27 | int cch = MAX_PATH + 1; | ||
28 | |||
29 | hr = StrAlloc(psczCanonicalized, cch); | ||
30 | PathExitOnFailure(hr, "Failed to allocate string for the canonicalized path."); | ||
31 | |||
32 | if (::PathCanonicalizeW(*psczCanonicalized, wzPath)) | ||
33 | { | ||
34 | hr = S_OK; | ||
35 | } | ||
36 | else | ||
37 | { | ||
38 | ExitFunctionWithLastError(hr); | ||
39 | } | ||
40 | |||
41 | LExit: | ||
42 | return hr; | ||
43 | } | ||
44 | |||
45 | DAPI_(HRESULT) PathDirectoryContainsPath( | ||
46 | __in_z LPCWSTR wzDirectory, | ||
47 | __in_z LPCWSTR wzPath | ||
48 | ) | ||
49 | { | ||
50 | HRESULT hr = S_OK; | ||
51 | LPWSTR sczPath = NULL; | ||
52 | LPWSTR sczDirectory = NULL; | ||
53 | LPWSTR sczOriginalPath = NULL; | ||
54 | LPWSTR sczOriginalDirectory = NULL; | ||
55 | |||
56 | hr = PathCanonicalizePath(wzPath, &sczOriginalPath); | ||
57 | PathExitOnFailure(hr, "Failed to canonicalize the path."); | ||
58 | |||
59 | hr = PathCanonicalizePath(wzDirectory, &sczOriginalDirectory); | ||
60 | PathExitOnFailure(hr, "Failed to canonicalize the directory."); | ||
61 | |||
62 | if (!sczOriginalPath || !*sczOriginalPath) | ||
63 | { | ||
64 | ExitFunction1(hr = S_FALSE); | ||
65 | } | ||
66 | if (!sczOriginalDirectory || !*sczOriginalDirectory) | ||
67 | { | ||
68 | ExitFunction1(hr = S_FALSE); | ||
69 | } | ||
70 | |||
71 | sczPath = sczOriginalPath; | ||
72 | sczDirectory = sczOriginalDirectory; | ||
73 | |||
74 | for (; *sczDirectory;) | ||
75 | { | ||
76 | if (!*sczPath) | ||
77 | { | ||
78 | ExitFunction1(hr = S_FALSE); | ||
79 | } | ||
80 | |||
81 | if (CSTR_EQUAL != ::CompareStringW(LOCALE_NEUTRAL, NORM_IGNORECASE, sczDirectory, 1, sczPath, 1)) | ||
82 | { | ||
83 | ExitFunction1(hr = S_FALSE); | ||
84 | } | ||
85 | |||
86 | ++sczDirectory; | ||
87 | ++sczPath; | ||
88 | } | ||
89 | |||
90 | --sczDirectory; | ||
91 | if (('\\' == *sczDirectory && *sczPath) || '\\' == *sczPath) | ||
92 | { | ||
93 | hr = S_OK; | ||
94 | } | ||
95 | else | ||
96 | { | ||
97 | hr = S_FALSE; | ||
98 | } | ||
99 | |||
100 | LExit: | ||
101 | ReleaseStr(sczOriginalPath); | ||
102 | ReleaseStr(sczOriginalDirectory); | ||
103 | return hr; | ||
104 | } | ||