VirtualBox

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

Last change on this file since 74714 was 74714, checked in by vboxsync, 6 years ago

DnD: Cleaned up DnDURIObject API / internal state handling.

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