1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
|
/* $Id: DnDPath.cpp $ */
/** @file
* DnD - Path handling.
*/
/*
* Copyright (C) 2014-2023 Oracle and/or its affiliates.
*
* This file is part of VirtualBox base platform packages, as
* available from https://www.virtualbox.org.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, in version 3 of the
* License.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <https://www.gnu.org/licenses>.
*
* SPDX-License-Identifier: GPL-3.0-only
*/
/*********************************************************************************************************************************
* Header Files *
*********************************************************************************************************************************/
#define LOG_GROUP LOG_GROUP_GUEST_DND
#include <VBox/GuestHost/DragAndDrop.h>
#include <iprt/dir.h>
#include <iprt/err.h>
#include <iprt/file.h>
#include <iprt/path.h>
#include <iprt/string.h>
#include <iprt/uri.h>
/**
* Sanitizes the file name portion of a path so that unsupported characters will be replaced by an underscore ("_").
*
* @return IPRT status code.
* @param pszFileName File name to sanitize.
* @param cbFileName Size (in bytes) of file name to sanitize.
*/
int DnDPathSanitizeFileName(char *pszFileName, size_t cbFileName)
{
if (!pszFileName) /* No path given? Bail out early. */
return VINF_SUCCESS;
AssertReturn(cbFileName, VERR_INVALID_PARAMETER);
int rc = VINF_SUCCESS;
#ifdef RT_OS_WINDOWS
RT_NOREF1(cbFileName);
/* Replace out characters not allowed on Windows platforms, put in by RTTimeSpecToString(). */
/** @todo Use something like RTPathSanitize() if available later some time. */
static const RTUNICP s_uszValidRangePairs[] =
{
' ', ' ',
'(', ')',
'-', '.',
'0', '9',
'A', 'Z',
'a', 'z',
'_', '_',
0xa0, 0xd7af,
'\0'
};
ssize_t cReplaced = RTStrPurgeComplementSet(pszFileName, s_uszValidRangePairs, '_' /* chReplacement */);
if (cReplaced < 0)
rc = VERR_INVALID_UTF8_ENCODING;
#else
RT_NOREF2(pszFileName, cbFileName);
#endif
return rc;
}
/**
* Validates whether a given path matches our set of rules or not.
*
* Rules:
* - An empty path is allowed.
* - Dot components ("." or "..") are forbidden.
* - If \a fMustExist is \c true, the path either has to be a file or a directory and must exist.
* - Symbolic links are forbidden.
*
* @returns VBox status code.
* @param pcszPath Path to validate.
* @param fMustExist Whether the path to validate also must exist.
* @sa shClTransferValidatePath().
*/
int DnDPathValidate(const char *pcszPath, bool fMustExist)
{
if (!pcszPath)
return VERR_INVALID_POINTER;
int rc = VINF_SUCCESS;
if ( RT_SUCCESS(rc)
&& !RTStrIsValidEncoding(pcszPath))
{
rc = VERR_INVALID_UTF8_ENCODING;
}
if ( RT_SUCCESS(rc)
&& RTStrStr(pcszPath, ".."))
{
rc = VERR_INVALID_PARAMETER;
}
if ( RT_SUCCESS(rc)
&& fMustExist)
{
RTFSOBJINFO objInfo;
rc = RTPathQueryInfo(pcszPath, &objInfo, RTFSOBJATTRADD_NOTHING);
if (RT_SUCCESS(rc))
{
if (RTFS_IS_DIRECTORY(objInfo.Attr.fMode))
{
if (!RTDirExists(pcszPath)) /* Path must exist. */
rc = VERR_PATH_NOT_FOUND;
}
else if (RTFS_IS_FILE(objInfo.Attr.fMode))
{
if (!RTFileExists(pcszPath)) /* File must exist. */
rc = VERR_FILE_NOT_FOUND;
}
else /* Everything else (e.g. symbolic links) are not supported. */
rc = VERR_NOT_SUPPORTED;
}
}
return rc;
}
/**
* Converts a DnD path.
*
* @returns VBox status code.
* @param pszPath Path to convert.
* @param cbPath Size (in bytes) of path to convert.
* @param fFlags Conversion flags of type DNDPATHCONVERT_FLAGS_.
*/
int DnDPathConvert(char *pszPath, size_t cbPath, DNDPATHCONVERTFLAGS fFlags)
{
RT_NOREF(cbPath);
AssertReturn(!(fFlags & ~DNDPATHCONVERT_FLAGS_VALID_MASK), VERR_INVALID_FLAGS);
if (fFlags & DNDPATHCONVERT_FLAGS_TO_DOS)
RTPathChangeToDosSlashes(pszPath, true /* fForce */);
else
RTPathChangeToUnixSlashes(pszPath, true /* fForce */);
return VINF_SUCCESS;
}
/**
* Rebases an absolute path from an old path base to a new path base.
* Note: Does *not* do any path conversion.
*
* @return IPRT status code.
* @param pcszPath Path to rebase.
* @param strBaseOld Old base path to rebase from. Optional and can be NULL.
* @param strBaseNew New base path to rebase to.
* @param ppszPath Where to store the allocated rebased path on success. Needs to be free'd with RTStrFree().
*/
int DnDPathRebase(const char *pcszPath, const char *pcszBaseOld, const char *pcszBaseNew,
char **ppszPath)
{
AssertPtrReturn(pcszPath, VERR_INVALID_POINTER);
AssertPtrReturn(pcszBaseOld, VERR_INVALID_POINTER);
AssertPtrReturn(pcszBaseNew, VERR_INVALID_POINTER);
AssertPtrReturn(ppszPath, VERR_INVALID_POINTER);
char szPath[RTPATH_MAX];
/* Do we need to see if the given path is part of the old base? */
size_t idxBase;
if ( pcszBaseOld
&& RTPathStartsWith(pcszPath, pcszBaseOld))
{
idxBase = strlen(pcszBaseOld);
}
else
idxBase = 0;
int rc = RTStrCopy(szPath, sizeof(szPath), pcszBaseNew);
if (RT_SUCCESS(rc))
{
rc = RTPathAppend(szPath, sizeof(szPath), &pcszPath[idxBase]);
if (RT_SUCCESS(rc))
rc = DnDPathValidate(szPath, false /* fMustExist */);
}
if (RT_SUCCESS(rc))
{
char *pszPath = RTStrDup(szPath);
if (pszPath)
*ppszPath = pszPath;
else
rc = VERR_NO_MEMORY;
}
return rc;
}
|