VirtualBox

source: vbox/trunk/src/VBox/GuestHost/DragAndDrop/DnDURIList.cpp@ 71191

Last change on this file since 71191 was 69758, checked in by vboxsync, 7 years ago

VBoxManageGuestCtrl.cpp,DnDURIList.cpp: Fix RTDIRENTRYTYPE_UNKNOWN problem by using RTDirReadExA. Untested.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 18.1 KB
Line 
1/* $Id: DnDURIList.cpp 69758 2017-11-19 15:23:57Z vboxsync $ */
2/** @file
3 * DnD: URI list class.
4 */
5
6/*
7 * Copyright (C) 2014-2017 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
18
19/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22
23#include <iprt/dir.h>
24#include <iprt/file.h>
25#include <iprt/fs.h>
26#include <iprt/path.h>
27#include <iprt/string.h>
28#include <iprt/symlink.h>
29#include <iprt/uri.h>
30
31#ifdef LOG_GROUP
32 #undef LOG_GROUP
33#endif
34#define LOG_GROUP LOG_GROUP_GUEST_DND
35#include <VBox/log.h>
36
37#include <VBox/GuestHost/DragAndDrop.h>
38
39DnDURIList::DnDURIList(void)
40 : m_cTotal(0)
41 , m_cbTotal(0)
42{
43}
44
45DnDURIList::~DnDURIList(void)
46{
47 Clear();
48}
49
50int DnDURIList::addEntry(const char *pcszSource, const char *pcszTarget, uint32_t fFlags)
51{
52 AssertPtrReturn(pcszSource, VERR_INVALID_POINTER);
53 AssertPtrReturn(pcszTarget, VERR_INVALID_POINTER);
54
55 LogFlowFunc(("pcszSource=%s, pcszTarget=%s, fFlags=0x%x\n", pcszSource, pcszTarget, fFlags));
56
57 RTFSOBJINFO objInfo;
58 int rc = RTPathQueryInfo(pcszSource, &objInfo, RTFSOBJATTRADD_NOTHING);
59 if (RT_SUCCESS(rc))
60 {
61 if (RTFS_IS_FILE(objInfo.Attr.fMode))
62 {
63 LogFlowFunc(("File '%s' -> '%s' (%RU64 bytes, file mode 0x%x)\n",
64 pcszSource, pcszTarget, (uint64_t)objInfo.cbObject, objInfo.Attr.fMode));
65
66 DnDURIObject *pObjFile = new DnDURIObject(DnDURIObject::File, pcszSource, pcszTarget);
67 if (pObjFile)
68 {
69 if (fFlags & DNDURILIST_FLAGS_KEEP_OPEN) /* Shall we keep the file open while being added to this list? */
70 {
71 /** @todo Add a standard fOpen mode for this list. */
72 rc = pObjFile->Open(DnDURIObject::Source, RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_DENY_WRITE, objInfo.Attr.fMode);
73 }
74
75 if (RT_SUCCESS(rc))
76 {
77 m_lstTree.append(pObjFile);
78
79 m_cTotal++;
80 m_cbTotal += pObjFile->GetSize();
81 }
82 else
83 delete pObjFile;
84 }
85 else
86 rc = VERR_NO_MEMORY;
87 }
88 else if (RTFS_IS_DIRECTORY(objInfo.Attr.fMode))
89 {
90 LogFlowFunc(("Directory '%s' -> '%s' (file mode 0x%x)\n", pcszSource, pcszTarget, objInfo.Attr.fMode));
91
92 DnDURIObject *pObjDir = new DnDURIObject(DnDURIObject::Directory, pcszSource, pcszTarget,
93 objInfo.Attr.fMode, 0 /* Size */);
94 if (pObjDir)
95 {
96 m_lstTree.append(pObjDir);
97
98 /** @todo Add DNDURILIST_FLAGS_KEEP_OPEN handling? */
99 m_cTotal++;
100 }
101 else
102 rc = VERR_NO_MEMORY;
103 }
104 /* Note: Symlinks already should have been resolved at this point. */
105 else
106 rc = VERR_NOT_SUPPORTED;
107 }
108
109 LogFlowFuncLeaveRC(rc);
110 return rc;
111}
112
113int DnDURIList::appendPathRecursive(const char *pcszSrcPath,
114 const char *pcszDstPath, const char *pcszDstBase, size_t cchDstBase, uint32_t fFlags)
115{
116 AssertPtrReturn(pcszSrcPath, VERR_INVALID_POINTER);
117 AssertPtrReturn(pcszDstBase, VERR_INVALID_POINTER);
118 AssertPtrReturn(pcszDstPath, VERR_INVALID_POINTER);
119
120 LogFlowFunc(("pcszSrcPath=%s, pcszDstPath=%s, pcszDstBase=%s, cchDstBase=%zu\n",
121 pcszSrcPath, pcszDstPath, pcszDstBase, cchDstBase));
122
123 RTFSOBJINFO objInfo;
124 int rc = RTPathQueryInfo(pcszSrcPath, &objInfo, RTFSOBJATTRADD_NOTHING);
125 if (RT_SUCCESS(rc))
126 {
127 if (RTFS_IS_DIRECTORY(objInfo.Attr.fMode))
128 {
129 rc = addEntry(pcszSrcPath, &pcszDstPath[cchDstBase], fFlags);
130 if (RT_SUCCESS(rc))
131 {
132 RTDIR hDir;
133 rc = RTDirOpen(&hDir, pcszSrcPath);
134 if (RT_SUCCESS(rc))
135 {
136 size_t cbDirEntry = 0;
137 PRTDIRENTRYEX pDirEntry = NULL;
138 do
139 {
140 /* Retrieve the next directory entry. */
141 rc = RTDirReadExA(hDir, &pDirEntry, &cbDirEntry, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK);
142 if (RT_FAILURE(rc))
143 {
144 if (rc == VERR_NO_MORE_FILES)
145 rc = VINF_SUCCESS;
146 break;
147 }
148
149 switch (pDirEntry->Info.Attr.fMode & RTFS_TYPE_MASK)
150 {
151 case RTFS_TYPE_DIRECTORY:
152 {
153 /* Skip "." and ".." entries. */
154 if (RTDirEntryExIsStdDotLink(pDirEntry))
155 break;
156
157 char *pszSrc = RTPathJoinA(pcszSrcPath, pDirEntry->szName);
158 if (pszSrc)
159 {
160 char *pszDst = RTPathJoinA(pcszDstPath, pDirEntry->szName);
161 if (pszDst)
162 {
163 rc = appendPathRecursive(pszSrc, pszDst, pcszDstBase, cchDstBase, fFlags);
164 RTStrFree(pszDst);
165 }
166 else
167 rc = VERR_NO_MEMORY;
168
169 RTStrFree(pszSrc);
170 }
171 else
172 rc = VERR_NO_MEMORY;
173 break;
174 }
175
176 case RTFS_TYPE_FILE:
177 {
178 char *pszSrc = RTPathJoinA(pcszSrcPath, pDirEntry->szName);
179 if (pszSrc)
180 {
181 char *pszDst = RTPathJoinA(pcszDstPath, pDirEntry->szName);
182 if (pszDst)
183 {
184 rc = addEntry(pszSrc, &pszDst[cchDstBase], fFlags);
185 RTStrFree(pszDst);
186 }
187 else
188 rc = VERR_NO_MEMORY;
189 RTStrFree(pszSrc);
190 }
191 else
192 rc = VERR_NO_MEMORY;
193 break;
194 }
195 case RTFS_TYPE_SYMLINK:
196 {
197 if (fFlags & DNDURILIST_FLAGS_RESOLVE_SYMLINKS)
198 {
199 char *pszSrc = RTPathRealDup(pcszDstBase);
200 if (pszSrc)
201 {
202 rc = RTPathQueryInfo(pszSrc, &objInfo, RTFSOBJATTRADD_NOTHING);
203 if (RT_SUCCESS(rc))
204 {
205 if (RTFS_IS_DIRECTORY(objInfo.Attr.fMode))
206 {
207 LogFlowFunc(("Directory entry is symlink to directory\n"));
208 rc = appendPathRecursive(pszSrc, pcszDstPath, pcszDstBase, cchDstBase, fFlags);
209 }
210 else if (RTFS_IS_FILE(objInfo.Attr.fMode))
211 {
212 LogFlowFunc(("Directory entry is symlink to file\n"));
213 rc = addEntry(pszSrc, &pcszDstPath[cchDstBase], fFlags);
214 }
215 else
216 rc = VERR_NOT_SUPPORTED;
217 }
218
219 RTStrFree(pszSrc);
220 }
221 else
222 rc = VERR_NO_MEMORY;
223 }
224 break;
225 }
226
227 default:
228 break;
229 }
230
231 } while (RT_SUCCESS(rc));
232
233 RTDirReadExAFree(&pDirEntry, &cbDirEntry);
234 RTDirClose(hDir);
235 }
236 }
237 }
238 else if (RTFS_IS_FILE(objInfo.Attr.fMode))
239 {
240 rc = addEntry(pcszSrcPath, &pcszDstPath[cchDstBase], fFlags);
241 }
242 else if (RTFS_IS_SYMLINK(objInfo.Attr.fMode))
243 {
244 if (fFlags & DNDURILIST_FLAGS_RESOLVE_SYMLINKS)
245 {
246 char *pszSrc = RTPathRealDup(pcszSrcPath);
247 if (pszSrc)
248 {
249 rc = RTPathQueryInfo(pszSrc, &objInfo, RTFSOBJATTRADD_NOTHING);
250 if (RT_SUCCESS(rc))
251 {
252 if (RTFS_IS_DIRECTORY(objInfo.Attr.fMode))
253 {
254 LogFlowFunc(("Symlink to directory\n"));
255 rc = appendPathRecursive(pszSrc, pcszDstPath, pcszDstBase, cchDstBase, fFlags);
256 }
257 else if (RTFS_IS_FILE(objInfo.Attr.fMode))
258 {
259 LogFlowFunc(("Symlink to file\n"));
260 rc = addEntry(pszSrc, &pcszDstPath[cchDstBase], fFlags);
261 }
262 else
263 rc = VERR_NOT_SUPPORTED;
264 }
265
266 RTStrFree(pszSrc);
267 }
268 else
269 rc = VERR_NO_MEMORY;
270 }
271 }
272 else
273 rc = VERR_NOT_SUPPORTED;
274 }
275
276 LogFlowFuncLeaveRC(rc);
277 return rc;
278}
279
280int DnDURIList::AppendNativePath(const char *pszPath, uint32_t fFlags)
281{
282 AssertPtrReturn(pszPath, VERR_INVALID_POINTER);
283
284 int rc;
285 char *pszPathNative = RTStrDup(pszPath);
286 if (pszPathNative)
287 {
288 RTPathChangeToUnixSlashes(pszPathNative, true /* fForce */);
289
290 char *pszPathURI = RTUriCreate("file" /* pszScheme */, NULL /* pszAuthority */,
291 pszPathNative, NULL /* pszQuery */, NULL /* pszFragment */);
292 if (pszPathURI)
293 {
294 rc = AppendURIPath(pszPathURI, fFlags);
295 RTStrFree(pszPathURI);
296 }
297 else
298 rc = VERR_INVALID_PARAMETER;
299
300 RTStrFree(pszPathNative);
301 }
302 else
303 rc = VERR_NO_MEMORY;
304
305 return rc;
306}
307
308int DnDURIList::AppendNativePathsFromList(const char *pszNativePaths, size_t cbNativePaths,
309 uint32_t fFlags)
310{
311 AssertPtrReturn(pszNativePaths, VERR_INVALID_POINTER);
312 AssertReturn(cbNativePaths, VERR_INVALID_PARAMETER);
313
314 RTCList<RTCString> lstPaths
315 = RTCString(pszNativePaths, cbNativePaths - 1).split("\r\n");
316 return AppendNativePathsFromList(lstPaths, fFlags);
317}
318
319int DnDURIList::AppendNativePathsFromList(const RTCList<RTCString> &lstNativePaths,
320 uint32_t fFlags)
321{
322 int rc = VINF_SUCCESS;
323
324 for (size_t i = 0; i < lstNativePaths.size(); i++)
325 {
326 const RTCString &strPath = lstNativePaths.at(i);
327 rc = AppendNativePath(strPath.c_str(), fFlags);
328 if (RT_FAILURE(rc))
329 break;
330 }
331
332 LogFlowFuncLeaveRC(rc);
333 return rc;
334}
335
336int DnDURIList::AppendURIPath(const char *pszURI, uint32_t fFlags)
337{
338 AssertPtrReturn(pszURI, VERR_INVALID_POINTER);
339
340 /** @todo Check for string termination? */
341#ifdef DEBUG_andy
342 LogFlowFunc(("pszPath=%s, fFlags=0x%x\n", pszURI, fFlags));
343#endif
344 int rc = VINF_SUCCESS;
345
346 /* Query the path component of a file URI. If this hasn't a
347 * file scheme NULL is returned. */
348 char *pszSrcPath = RTUriFilePath(pszURI);
349 if (pszSrcPath)
350 {
351 /* Add the path to our internal file list (recursive in
352 * the case of a directory). */
353 size_t cbPathLen = RTPathStripTrailingSlash(pszSrcPath);
354 if (cbPathLen)
355 {
356 char *pszFileName = RTPathFilename(pszSrcPath);
357 if (pszFileName)
358 {
359 Assert(pszFileName >= pszSrcPath);
360 size_t cchDstBase = (fFlags & DNDURILIST_FLAGS_ABSOLUTE_PATHS)
361 ? 0 /* Use start of path as root. */
362 : pszFileName - pszSrcPath;
363 char *pszDstPath = &pszSrcPath[cchDstBase];
364 m_lstRoot.append(pszDstPath);
365
366 LogFlowFunc(("pszFilePath=%s, pszFileName=%s, pszRoot=%s\n",
367 pszSrcPath, pszFileName, pszDstPath));
368
369 rc = appendPathRecursive(pszSrcPath, pszSrcPath, pszSrcPath, cchDstBase, fFlags);
370 }
371 else
372 rc = VERR_PATH_NOT_FOUND;
373 }
374 else
375 rc = VERR_INVALID_PARAMETER;
376
377 RTStrFree(pszSrcPath);
378 }
379 else
380 rc = VERR_INVALID_PARAMETER;
381
382 LogFlowFuncLeaveRC(rc);
383 return rc;
384}
385
386int DnDURIList::AppendURIPathsFromList(const char *pszURIPaths, size_t cbURIPaths,
387 uint32_t fFlags)
388{
389 AssertPtrReturn(pszURIPaths, VERR_INVALID_POINTER);
390 AssertReturn(cbURIPaths, VERR_INVALID_PARAMETER);
391
392 RTCList<RTCString> lstPaths
393 = RTCString(pszURIPaths, cbURIPaths - 1).split("\r\n");
394 return AppendURIPathsFromList(lstPaths, fFlags);
395}
396
397int DnDURIList::AppendURIPathsFromList(const RTCList<RTCString> &lstURI,
398 uint32_t fFlags)
399{
400 int rc = VINF_SUCCESS;
401
402 for (size_t i = 0; i < lstURI.size(); i++)
403 {
404 RTCString strURI = lstURI.at(i);
405 rc = AppendURIPath(strURI.c_str(), fFlags);
406
407 if (RT_FAILURE(rc))
408 break;
409 }
410
411 LogFlowFuncLeaveRC(rc);
412 return rc;
413}
414
415void DnDURIList::Clear(void)
416{
417 m_lstRoot.clear();
418
419 for (size_t i = 0; i < m_lstTree.size(); i++)
420 {
421 DnDURIObject *pCurObj = m_lstTree.at(i);
422 AssertPtr(pCurObj);
423 RTMemFree(pCurObj);
424 }
425 m_lstTree.clear();
426
427 m_cTotal = 0;
428 m_cbTotal = 0;
429}
430
431void DnDURIList::RemoveFirst(void)
432{
433 if (m_lstTree.isEmpty())
434 return;
435
436 DnDURIObject *pCurObj = m_lstTree.first();
437 AssertPtr(pCurObj);
438
439 uint64_t cbSize = pCurObj->GetSize();
440 Assert(m_cbTotal >= cbSize);
441 m_cbTotal -= cbSize; /* Adjust total size. */
442
443 pCurObj->Close();
444 RTMemFree(pCurObj);
445
446 m_lstTree.removeFirst();
447}
448
449int DnDURIList::RootFromURIData(const void *pvData, size_t cbData, uint32_t fFlags)
450{
451 Assert(fFlags == 0); RT_NOREF1(fFlags);
452 AssertPtrReturn(pvData, VERR_INVALID_POINTER);
453 AssertReturn(cbData, VERR_INVALID_PARAMETER);
454
455 if (!RTStrIsValidEncoding(static_cast<const char *>(pvData)))
456 return VERR_INVALID_PARAMETER;
457
458 RTCList<RTCString> lstURI =
459 RTCString(static_cast<const char *>(pvData), cbData - 1).split("\r\n");
460 if (lstURI.isEmpty())
461 return VINF_SUCCESS;
462
463 int rc = VINF_SUCCESS;
464
465 for (size_t i = 0; i < lstURI.size(); ++i)
466 {
467 /* Query the path component of a file URI. If this hasn't a
468 * file scheme, NULL is returned. */
469 const char *pszURI = lstURI.at(i).c_str();
470 char *pszFilePath = RTUriFilePath(pszURI);
471#ifdef DEBUG_andy
472 LogFlowFunc(("pszURI=%s, pszFilePath=%s\n", pszURI, pszFilePath));
473#endif
474 if (pszFilePath)
475 {
476 rc = DnDPathSanitize(pszFilePath, strlen(pszFilePath));
477 if (RT_SUCCESS(rc))
478 {
479 m_lstRoot.append(pszFilePath);
480 m_cTotal++;
481 }
482
483 RTStrFree(pszFilePath);
484 }
485 else
486 rc = VERR_INVALID_PARAMETER;
487
488 if (RT_FAILURE(rc))
489 break;
490 }
491
492 return rc;
493}
494
495RTCString DnDURIList::RootToString(const RTCString &strPathBase /* = "" */,
496 const RTCString &strSeparator /* = "\r\n" */) const
497{
498 RTCString strRet;
499 for (size_t i = 0; i < m_lstRoot.size(); i++)
500 {
501 const char *pszCurRoot = m_lstRoot.at(i).c_str();
502#ifdef DEBUG_andy
503 LogFlowFunc(("pszCurRoot=%s\n", pszCurRoot));
504#endif
505 if (strPathBase.isNotEmpty())
506 {
507 char *pszPath = RTPathJoinA(strPathBase.c_str(), pszCurRoot);
508 if (pszPath)
509 {
510 char *pszPathURI = RTUriFileCreate(pszPath);
511 if (pszPathURI)
512 {
513 strRet += RTCString(pszPathURI) + strSeparator;
514 LogFlowFunc(("URI (Base): %s\n", strRet.c_str()));
515 RTStrFree(pszPathURI);
516 }
517
518 RTStrFree(pszPath);
519
520 if (!pszPathURI)
521 break;
522 }
523 else
524 break;
525 }
526 else
527 {
528 char *pszPathURI = RTUriFileCreate(pszCurRoot);
529 if (pszPathURI)
530 {
531 strRet += RTCString(pszPathURI) + strSeparator;
532 LogFlowFunc(("URI: %s\n", strRet.c_str()));
533 RTStrFree(pszPathURI);
534 }
535 else
536 break;
537 }
538 }
539
540 return strRet;
541}
542
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