VirtualBox

source: vbox/trunk/src/VBox/HostServices/SharedFolders/vbsfpathabs.cpp@ 99040

Last change on this file since 99040 was 98103, checked in by vboxsync, 22 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.5 KB
Line 
1/* $Id: vbsfpathabs.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * Shared Folders Service - guest/host path convertion and verification.
4 */
5
6/*
7 * Copyright (C) 2017-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * SPDX-License-Identifier: GPL-3.0-only
26 */
27
28
29/*********************************************************************************************************************************
30* Header Files *
31*********************************************************************************************************************************/
32#define LOG_GROUP LOG_GROUP_SHARED_FOLDERS
33#include <iprt/err.h>
34#include <iprt/path.h>
35#include <iprt/string.h>
36
37
38#if defined(RT_OS_WINDOWS)
39static void vbsfPathResolveRelative(char *pszPathBegin)
40{
41 char *pszCur = pszPathBegin;
42 char * const pszTop = pszCur;
43
44 /*
45 * Get rid of double dot path components by evaluating them.
46 */
47 for (;;)
48 {
49 char const chFirst = pszCur[0];
50 if ( chFirst == '.'
51 && pszCur[1] == '.'
52 && (!pszCur[2] || pszCur[2] == RTPATH_SLASH))
53 {
54 /* rewind to the previous component if any */
55 char *pszPrev = pszCur;
56 if ((uintptr_t)pszPrev > (uintptr_t)pszTop)
57 {
58 pszPrev--;
59 while ( (uintptr_t)pszPrev > (uintptr_t)pszTop
60 && pszPrev[-1] != RTPATH_SLASH)
61 pszPrev--;
62 }
63 if (!pszCur[2])
64 {
65 if (pszPrev != pszTop)
66 pszPrev[-1] = '\0';
67 else
68 *pszPrev = '\0';
69 break;
70 }
71 Assert(pszPrev[-1] == RTPATH_SLASH);
72 memmove(pszPrev, pszCur + 3, strlen(pszCur + 3) + 1);
73 pszCur = pszPrev - 1;
74 }
75 else if ( chFirst == '.'
76 && (!pszCur[1] || pszCur[1] == RTPATH_SLASH))
77 {
78 /* remove unnecessary '.' */
79 if (!pszCur[1])
80 {
81 if (pszCur != pszTop)
82 pszCur[-1] = '\0';
83 else
84 *pszCur = '\0';
85 break;
86 }
87 memmove(pszCur, pszCur + 2, strlen(pszCur + 2) + 1);
88 continue;
89 }
90 else
91 {
92 /* advance to end of component. */
93 while (*pszCur && *pszCur != RTPATH_SLASH)
94 pszCur++;
95 }
96
97 if (!*pszCur)
98 break;
99
100 /* skip the slash */
101 ++pszCur;
102 }
103}
104#endif /* RT_OS_WINDOWS */
105
106int vbsfPathAbs(const char *pszRoot, const char *pszPath, char *pszAbsPath, size_t cbAbsPath)
107{
108#if defined(RT_OS_WINDOWS)
109 /** @todo This code is not needed in 6.0 and later as IPRT translates paths
110 * to //./ (inverted slashes for doxygen) format if they're too long. */
111 const char *pszPathStart = pszRoot? pszRoot: pszPath;
112
113 /* Windows extended-length paths. */
114 if ( RTPATH_IS_SLASH(pszPathStart[0])
115 && RTPATH_IS_SLASH(pszPathStart[1])
116 && pszPathStart[2] == '?'
117 && RTPATH_IS_SLASH(pszPathStart[3])
118 )
119 {
120 /* Maximum total path length of 32,767 characters. */
121 if (cbAbsPath > _32K)
122 cbAbsPath = _32K;
123
124 /* Copy the root to pszAbsPath buffer. */
125 size_t cchRoot = pszRoot? strlen(pszRoot): 0;
126 if (cchRoot >= cbAbsPath)
127 return VERR_FILENAME_TOO_LONG;
128
129 if (pszRoot)
130 {
131 /* Caller must ensure that the path is relative, without the leading path separator. */
132 if (RTPATH_IS_SLASH(pszPath[0]))
133 return VERR_INVALID_PARAMETER;
134
135 if (cchRoot)
136 memcpy(pszAbsPath, pszRoot, cchRoot);
137
138 if (cchRoot == 0 || !RTPATH_IS_SLASH(pszAbsPath[cchRoot - 1]))
139 {
140 /* Append path separator after the root. */
141 ++cchRoot;
142 if (cchRoot >= cbAbsPath)
143 return VERR_FILENAME_TOO_LONG;
144
145 pszAbsPath[cchRoot - 1] = RTPATH_SLASH;
146 }
147 }
148
149 /* Append the path to the pszAbsPath buffer. */
150 const size_t cchPath = strlen(pszPath);
151 if (cchRoot + cchPath >= cbAbsPath)
152 return VERR_FILENAME_TOO_LONG;
153
154 memcpy(&pszAbsPath[cchRoot], pszPath, cchPath + 1); /* Including trailing 0. */
155
156 /* Find out where the actual path begins, i.e. skip the root spec. */
157 char *pszPathBegin = &pszAbsPath[4]; /* Skip the extended-length path prefix "\\?\" */
158 if ( pszPathBegin[0]
159 && RTPATH_IS_VOLSEP(pszPathBegin[1])
160 && pszPathBegin[2] == RTPATH_SLASH)
161 {
162 /* "\\?\C:\" */
163 pszPathBegin += 3;
164 }
165 else if ( pszPathBegin[0] == 'U'
166 && pszPathBegin[1] == 'N'
167 && pszPathBegin[2] == 'C'
168 && pszPathBegin[3] == RTPATH_SLASH)
169 {
170 /* "\\?\UNC\server\share" */
171 pszPathBegin += 4;
172
173 /* Skip "server\share" too. */
174 while (*pszPathBegin != RTPATH_SLASH && *pszPathBegin)
175 ++pszPathBegin;
176 if (*pszPathBegin == RTPATH_SLASH)
177 {
178 ++pszPathBegin;
179 while (*pszPathBegin != RTPATH_SLASH && *pszPathBegin)
180 ++pszPathBegin;
181 if (*pszPathBegin == RTPATH_SLASH)
182 ++pszPathBegin;
183 }
184 }
185 else
186 return VERR_INVALID_NAME;
187
188 /* Process pszAbsPath in place. */
189 vbsfPathResolveRelative(pszPathBegin);
190
191 return VINF_SUCCESS;
192 }
193#endif /* RT_OS_WINDOWS */
194
195 /* Fallback for the common paths. */
196
197 if (*pszPath != '\0')
198 return RTPathAbsEx(pszRoot, pszPath, RTPATH_STR_F_STYLE_HOST, pszAbsPath, &cbAbsPath);
199 return RTPathAbsEx(NULL, pszRoot, RTPATH_STR_F_STYLE_HOST, pszAbsPath, &cbAbsPath);
200}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette