1 | /* $Id: pathhost-posix.cpp 76553 2019-01-01 01:45:53Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Path Conversions, POSIX.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2019 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 | #define LOG_GROUP RTLOGGROUP_PATH
|
---|
32 | #include "internal/iprt.h"
|
---|
33 | #include "internal/path.h"
|
---|
34 | #include "internal/string.h"
|
---|
35 | #include "internal/thread.h"
|
---|
36 |
|
---|
37 | #include <iprt/env.h>
|
---|
38 | #include <iprt/err.h>
|
---|
39 | #include <iprt/string.h>
|
---|
40 | #include <iprt/once.h>
|
---|
41 |
|
---|
42 |
|
---|
43 | /*********************************************************************************************************************************
|
---|
44 | * Global Variables *
|
---|
45 | *********************************************************************************************************************************/
|
---|
46 | /** Initialize once object. */
|
---|
47 | static RTONCE g_OnceInitPathConv = RTONCE_INITIALIZER;
|
---|
48 | /** If set, then we can pass UTF-8 thru directly. */
|
---|
49 | static bool g_fPassthruUtf8 = false;
|
---|
50 | /** The UTF-8 to FS iconv cache entry. */
|
---|
51 | static RTSTRICONV g_enmUtf8ToFsIdx = RTSTRICONV_UTF8_TO_LOCALE;
|
---|
52 | /** The FS to UTF-8 iconv cache entry. */
|
---|
53 | static RTSTRICONV g_enmFsToUtf8Idx = RTSTRICONV_LOCALE_TO_UTF8;
|
---|
54 | /** The codeset we're using. */
|
---|
55 | static char g_szFsCodeset[32];
|
---|
56 |
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * Do a case insensitive compare where the 2nd string is known and can be case
|
---|
60 | * folded when writing the code.
|
---|
61 | *
|
---|
62 | * @returns see strcmp.
|
---|
63 | * @param pszStr1 The string to compare against pszLower and
|
---|
64 | * pszUpper.
|
---|
65 | * @param pszUpper The upper case edition of the 2nd string.
|
---|
66 | * @param pszLower The lower case edition of the 2nd string.
|
---|
67 | */
|
---|
68 | static int rtPathStrICmp(const char *pszStr1, const char *pszUpper, const char *pszLower)
|
---|
69 | {
|
---|
70 | Assert(strlen(pszLower) == strlen(pszUpper));
|
---|
71 | for (;;)
|
---|
72 | {
|
---|
73 | char ch1 = *pszStr1++;
|
---|
74 | char ch2Upper = *pszUpper++;
|
---|
75 | char ch2Lower = *pszLower++;
|
---|
76 | if ( ch1 != ch2Upper
|
---|
77 | && ch1 != ch2Lower)
|
---|
78 | return ch1 < ch2Upper ? -1 : 1;
|
---|
79 | if (!ch1)
|
---|
80 | return 0;
|
---|
81 | }
|
---|
82 | }
|
---|
83 |
|
---|
84 | /**
|
---|
85 | * Is the specified codeset something we can treat as UTF-8.
|
---|
86 | *
|
---|
87 | * @returns true if we can do UTF-8 passthru, false if not.
|
---|
88 | * @param pszCodeset The codeset in question.
|
---|
89 | */
|
---|
90 | static bool rtPathConvInitIsUtf8(const char *pszCodeset)
|
---|
91 | {
|
---|
92 | /* Paranoia. */
|
---|
93 | if (!pszCodeset)
|
---|
94 | return false;
|
---|
95 |
|
---|
96 | /*
|
---|
97 | * Avoid RTStrICmp at this point.
|
---|
98 | */
|
---|
99 | static struct
|
---|
100 | {
|
---|
101 | const char *pszUpper;
|
---|
102 | const char *pszLower;
|
---|
103 | } const s_aUtf8Compatible[] =
|
---|
104 | {
|
---|
105 | /* The default locale. */
|
---|
106 | { "C" , "c" },
|
---|
107 | { "POSIX" , "posix" },
|
---|
108 | /* 7-bit ASCII. */
|
---|
109 | { "ANSI_X3.4-1968" , "ansi_x3.4-1968" },
|
---|
110 | { "ANSI_X3.4-1986" , "ansi_x3.4-1986" },
|
---|
111 | { "US-ASCII" , "us-ascii" },
|
---|
112 | { "ISO646-US" , "iso646-us" },
|
---|
113 | { "ISO_646.IRV:1991" , "iso_646.irv:1991" },
|
---|
114 | { "ISO-IR-6" , "iso-ir-6" },
|
---|
115 | { "IBM367" , "ibm367" },
|
---|
116 | /* UTF-8 */
|
---|
117 | { "UTF-8" , "utf-8" },
|
---|
118 | { "UTF8" , "utf8" },
|
---|
119 | { "ISO-10646/UTF-8" , "iso-10646/utf-8" },
|
---|
120 | { "ISO-10646/UTF8" , "iso-10646/utf8" }
|
---|
121 | };
|
---|
122 |
|
---|
123 | for (size_t i = 0; i < RT_ELEMENTS(s_aUtf8Compatible); i++)
|
---|
124 | if (!rtPathStrICmp(pszCodeset, s_aUtf8Compatible[i].pszUpper, s_aUtf8Compatible[i].pszLower))
|
---|
125 | return true;
|
---|
126 |
|
---|
127 | return false;
|
---|
128 | }
|
---|
129 |
|
---|
130 |
|
---|
131 | /**
|
---|
132 | * Init once for the path conversion code.
|
---|
133 | *
|
---|
134 | * @returns IPRT status code.
|
---|
135 | * @param pvUser1 Unused.
|
---|
136 | * @param pvUser2 Unused.
|
---|
137 | */
|
---|
138 | static DECLCALLBACK(int32_t) rtPathConvInitOnce(void *pvUser)
|
---|
139 | {
|
---|
140 | /*
|
---|
141 | * Read the environment variable, no mercy on misconfigs here except that
|
---|
142 | * empty values are quietly ignored. (We use a temp buffer for stripping.)
|
---|
143 | */
|
---|
144 | char *pszEnvValue = NULL;
|
---|
145 | char szEnvValue[sizeof(g_szFsCodeset)];
|
---|
146 | int rc = RTEnvGetEx(RTENV_DEFAULT, RTPATH_CODESET_ENV_VAR, szEnvValue, sizeof(szEnvValue), NULL);
|
---|
147 | if (rc != VERR_ENV_VAR_NOT_FOUND && RT_FAILURE(rc))
|
---|
148 | return rc;
|
---|
149 | if (RT_SUCCESS(rc))
|
---|
150 | pszEnvValue = RTStrStrip(szEnvValue);
|
---|
151 |
|
---|
152 | if (pszEnvValue && *pszEnvValue)
|
---|
153 | {
|
---|
154 | g_fPassthruUtf8 = rtPathConvInitIsUtf8(pszEnvValue);
|
---|
155 | g_enmFsToUtf8Idx = RTSTRICONV_FS_TO_UTF8;
|
---|
156 | g_enmUtf8ToFsIdx = RTSTRICONV_UTF8_TO_FS;
|
---|
157 | strcpy(g_szFsCodeset, pszEnvValue);
|
---|
158 | }
|
---|
159 | else
|
---|
160 | {
|
---|
161 | const char *pszCodeset = rtStrGetLocaleCodeset();
|
---|
162 | size_t cchCodeset = pszCodeset ? strlen(pszCodeset) : sizeof(g_szFsCodeset);
|
---|
163 | if (cchCodeset >= sizeof(g_szFsCodeset))
|
---|
164 | /* This shouldn't happen, but we'll manage. */
|
---|
165 | g_szFsCodeset[0] = '\0';
|
---|
166 | else
|
---|
167 | {
|
---|
168 | memcpy(g_szFsCodeset, pszCodeset, cchCodeset + 1);
|
---|
169 | pszCodeset = g_szFsCodeset;
|
---|
170 | }
|
---|
171 | g_fPassthruUtf8 = rtPathConvInitIsUtf8(pszCodeset);
|
---|
172 | g_enmFsToUtf8Idx = RTSTRICONV_LOCALE_TO_UTF8;
|
---|
173 | g_enmUtf8ToFsIdx = RTSTRICONV_UTF8_TO_LOCALE;
|
---|
174 | }
|
---|
175 |
|
---|
176 | NOREF(pvUser);
|
---|
177 | return VINF_SUCCESS;
|
---|
178 | }
|
---|
179 |
|
---|
180 |
|
---|
181 | int rtPathToNative(char const **ppszNativePath, const char *pszPath, const char *pszBasePath)
|
---|
182 | {
|
---|
183 | *ppszNativePath = NULL;
|
---|
184 |
|
---|
185 | int rc = RTOnce(&g_OnceInitPathConv, rtPathConvInitOnce, NULL);
|
---|
186 | if (RT_SUCCESS(rc))
|
---|
187 | {
|
---|
188 | if (g_fPassthruUtf8 || !*pszPath)
|
---|
189 | *ppszNativePath = pszPath;
|
---|
190 | else
|
---|
191 | rc = rtStrConvert(pszPath, strlen(pszPath), "UTF-8",
|
---|
192 | (char **)ppszNativePath, 0, g_szFsCodeset,
|
---|
193 | 2, g_enmUtf8ToFsIdx);
|
---|
194 | }
|
---|
195 | NOREF(pszBasePath); /* We don't query the FS for codeset preferences. */
|
---|
196 | return rc;
|
---|
197 | }
|
---|
198 |
|
---|
199 |
|
---|
200 | void rtPathFreeNative(char const *pszNativePath, const char *pszPath)
|
---|
201 | {
|
---|
202 | if ( pszNativePath != pszPath
|
---|
203 | && pszNativePath)
|
---|
204 | RTStrFree((char *)pszNativePath);
|
---|
205 | }
|
---|
206 |
|
---|
207 |
|
---|
208 | int rtPathFromNative(const char **ppszPath, const char *pszNativePath, const char *pszBasePath)
|
---|
209 | {
|
---|
210 | *ppszPath = NULL;
|
---|
211 |
|
---|
212 | int rc = RTOnce(&g_OnceInitPathConv, rtPathConvInitOnce, NULL);
|
---|
213 | if (RT_SUCCESS(rc))
|
---|
214 | {
|
---|
215 | if (g_fPassthruUtf8 || !*pszNativePath)
|
---|
216 | {
|
---|
217 | size_t cCpsIgnored;
|
---|
218 | size_t cchNativePath;
|
---|
219 | rc = rtUtf8Length(pszNativePath, RTSTR_MAX, &cCpsIgnored, &cchNativePath);
|
---|
220 | if (RT_SUCCESS(rc))
|
---|
221 | {
|
---|
222 | char *pszPath;
|
---|
223 | *ppszPath = pszPath = RTStrAlloc(cchNativePath + 1);
|
---|
224 | if (pszPath)
|
---|
225 | memcpy(pszPath, pszNativePath, cchNativePath + 1);
|
---|
226 | else
|
---|
227 | rc = VERR_NO_STR_MEMORY;
|
---|
228 | }
|
---|
229 | }
|
---|
230 | else
|
---|
231 | rc = rtStrConvert(pszNativePath, strlen(pszNativePath), g_szFsCodeset,
|
---|
232 | (char **)ppszPath, 0, "UTF-8",
|
---|
233 | 2, g_enmFsToUtf8Idx);
|
---|
234 | }
|
---|
235 | NOREF(pszBasePath); /* We don't query the FS for codeset preferences. */
|
---|
236 | return rc;
|
---|
237 | }
|
---|
238 |
|
---|
239 |
|
---|
240 | void rtPathFreeIprt(const char *pszPath, const char *pszNativePath)
|
---|
241 | {
|
---|
242 | if ( pszPath != pszNativePath
|
---|
243 | && pszPath)
|
---|
244 | RTStrFree((char *)pszPath);
|
---|
245 | }
|
---|
246 |
|
---|
247 |
|
---|
248 | int rtPathFromNativeCopy(char *pszPath, size_t cbPath, const char *pszNativePath, const char *pszBasePath)
|
---|
249 | {
|
---|
250 | int rc = RTOnce(&g_OnceInitPathConv, rtPathConvInitOnce, NULL);
|
---|
251 | if (RT_SUCCESS(rc))
|
---|
252 | {
|
---|
253 | if (g_fPassthruUtf8 || !*pszNativePath)
|
---|
254 | rc = RTStrCopy(pszPath, cbPath, pszNativePath);
|
---|
255 | else if (cbPath)
|
---|
256 | rc = rtStrConvert(pszNativePath, strlen(pszNativePath), g_szFsCodeset,
|
---|
257 | &pszPath, cbPath, "UTF-8",
|
---|
258 | 2, g_enmFsToUtf8Idx);
|
---|
259 | else
|
---|
260 | rc = VERR_BUFFER_OVERFLOW;
|
---|
261 | }
|
---|
262 |
|
---|
263 | NOREF(pszBasePath); /* We don't query the FS for codeset preferences. */
|
---|
264 | return rc;
|
---|
265 | }
|
---|
266 |
|
---|
267 |
|
---|
268 | int rtPathFromNativeDup(char **ppszPath, const char *pszNativePath, const char *pszBasePath)
|
---|
269 | {
|
---|
270 | int rc = RTOnce(&g_OnceInitPathConv, rtPathConvInitOnce, NULL);
|
---|
271 | if (RT_SUCCESS(rc))
|
---|
272 | {
|
---|
273 | if (g_fPassthruUtf8 || !*pszNativePath)
|
---|
274 | rc = RTStrDupEx(ppszPath, pszNativePath);
|
---|
275 | else
|
---|
276 | rc = rtStrConvert(pszNativePath, strlen(pszNativePath), g_szFsCodeset,
|
---|
277 | ppszPath, 0, "UTF-8",
|
---|
278 | 2, g_enmFsToUtf8Idx);
|
---|
279 | }
|
---|
280 |
|
---|
281 | NOREF(pszBasePath); /* We don't query the FS for codeset preferences. */
|
---|
282 | return rc;
|
---|
283 | }
|
---|
284 |
|
---|