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