VirtualBox

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

最後變更 在這個檔案從55744是 55744,由 vboxsync 提交於 10 年 前

DnDURIList.cpp: Fixed object accounting.

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 12.8 KB
 
1/* $Id: DnDURIList.cpp 55744 2015-05-08 07:36:53Z vboxsync $ */
2/** @file
3 * DnD: URI list class.
4 */
5
6/*
7 * Copyright (C) 2014-2015 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.alldomusa.eu.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 * Header Files *
20 ******************************************************************************/
21
22#include <iprt/dir.h>
23#include <iprt/file.h>
24#include <iprt/fs.h>
25#include <iprt/path.h>
26#include <iprt/uri.h>
27
28#ifdef LOG_GROUP
29 #undef LOG_GROUP
30#endif
31#define LOG_GROUP LOG_GROUP_GUEST_DND
32#include <VBox/log.h>
33
34#include <VBox/GuestHost/DragAndDrop.h>
35
36DnDURIList::DnDURIList(void)
37 : m_cTotal(0)
38 , m_cbTotal(0)
39{
40}
41
42DnDURIList::~DnDURIList(void)
43{
44}
45
46int DnDURIList::appendPathRecursive(const char *pcszPath, size_t cbBaseLen,
47 uint32_t fFlags)
48{
49 AssertPtrReturn(pcszPath, VERR_INVALID_POINTER);
50
51 RTFSOBJINFO objInfo;
52 int rc = RTPathQueryInfo(pcszPath, &objInfo, RTFSOBJATTRADD_NOTHING);
53 if (RT_FAILURE(rc))
54 return rc;
55
56 /*
57 * These are the types we currently support. Symlinks are not directly
58 * supported. First the guest could be an OS which doesn't support it and
59 * second the symlink could point to a file which is out of the base tree.
60 * Both things are hard to support. For now we just copy the target file in
61 * this case.
62 */
63 if (!( RTFS_IS_DIRECTORY(objInfo.Attr.fMode)
64 || RTFS_IS_FILE(objInfo.Attr.fMode)
65 || RTFS_IS_SYMLINK(objInfo.Attr.fMode)))
66 return VINF_SUCCESS;
67
68 uint64_t cbSize = 0;
69 rc = RTFileQuerySize(pcszPath, &cbSize);
70 if (rc == VERR_IS_A_DIRECTORY)
71 rc = VINF_SUCCESS;
72
73 if (RT_FAILURE(rc))
74 return rc;
75
76 m_lstTree.append(DnDURIObject( RTFS_IS_DIRECTORY(objInfo.Attr.fMode)
77 ? DnDURIObject::Directory
78 : DnDURIObject::File,
79 pcszPath, &pcszPath[cbBaseLen],
80 objInfo.Attr.fMode, cbSize));
81 m_cTotal++;
82 m_cbTotal += cbSize;
83#ifdef DEBUG_andy
84 LogFlowFunc(("strSrcPath=%s, strDstPath=%s, fMode=0x%x, cbSize=%RU64, cTotal=%RU32, cbTotal=%zu\n",
85 pcszPath, &pcszPath[cbBaseLen], objInfo.Attr.fMode, cbSize, m_cTotal, m_cbTotal));
86#endif
87
88 /* We have to try to open even symlinks, cause they could
89 * be symlinks to directories. */
90 PRTDIR hDir;
91 rc = RTDirOpen(&hDir, pcszPath);
92
93 /* The following error happens when this was a symlink
94 * to a file or a regular file. */
95 if ( rc == VERR_PATH_NOT_FOUND
96 || rc == VERR_NOT_A_DIRECTORY)
97 return VINF_SUCCESS;
98 if (RT_FAILURE(rc))
99 return rc;
100
101 while (RT_SUCCESS(rc))
102 {
103 RTDIRENTRY DirEntry;
104 rc = RTDirRead(hDir, &DirEntry, NULL);
105 if (RT_FAILURE(rc))
106 {
107 if (rc == VERR_NO_MORE_FILES)
108 rc = VINF_SUCCESS;
109 break;
110 }
111 switch (DirEntry.enmType)
112 {
113 case RTDIRENTRYTYPE_DIRECTORY:
114 {
115 /* Skip "." and ".." entries. */
116 if ( RTStrCmp(DirEntry.szName, ".") == 0
117 || RTStrCmp(DirEntry.szName, "..") == 0)
118 break;
119
120 char *pszRecDir = RTPathJoinA(pcszPath, DirEntry.szName);
121 if (pszRecDir)
122 {
123 rc = appendPathRecursive(pszRecDir, cbBaseLen, fFlags);
124 RTStrFree(pszRecDir);
125 }
126 else
127 rc = VERR_NO_MEMORY;
128 break;
129 }
130 case RTDIRENTRYTYPE_SYMLINK:
131 case RTDIRENTRYTYPE_FILE:
132 {
133 char *pszNewFile = RTPathJoinA(pcszPath, DirEntry.szName);
134 if (pszNewFile)
135 {
136 /* We need the size and the mode of the file. */
137 RTFSOBJINFO objInfo1;
138 rc = RTPathQueryInfo(pszNewFile, &objInfo1, RTFSOBJATTRADD_NOTHING);
139 if (RT_FAILURE(rc))
140 return rc;
141 rc = RTFileQuerySize(pszNewFile, &cbSize);
142 if (rc == VERR_IS_A_DIRECTORY) /* Happens for symlinks. */
143 rc = VINF_SUCCESS;
144
145 if (RT_FAILURE(rc))
146 break;
147
148 if (RTFS_IS_FILE(objInfo.Attr.fMode))
149 {
150 m_lstTree.append(DnDURIObject(DnDURIObject::File,
151 pszNewFile, &pszNewFile[cbBaseLen],
152 objInfo1.Attr.fMode, cbSize));
153 m_cTotal++;
154 m_cbTotal += cbSize;
155#ifdef DEBUG_andy
156 LogFlowFunc(("strSrcPath=%s, strDstPath=%s, fMode=0x%x, cbSize=%RU64, cTotal=%RU32, cbTotal=%zu\n",
157 pszNewFile, &pszNewFile[cbBaseLen], objInfo1.Attr.fMode, cbSize, m_cTotal, m_cbTotal));
158#endif
159 }
160 else /* Handle symlink directories. */
161 rc = appendPathRecursive(pszNewFile, cbBaseLen, fFlags);
162
163 RTStrFree(pszNewFile);
164 }
165 else
166 rc = VERR_NO_MEMORY;
167 break;
168 }
169
170 default:
171 /* Just ignore the rest. */
172 break;
173 }
174 }
175
176 RTDirClose(hDir);
177 return rc;
178}
179
180int DnDURIList::AppendNativePath(const char *pszPath, uint32_t fFlags)
181{
182 AssertPtrReturn(pszPath, VERR_INVALID_POINTER);
183
184 int rc;
185 char *pszPathNative = RTStrDup(pszPath);
186 if (pszPathNative)
187 {
188 RTPathChangeToUnixSlashes(pszPathNative, true /* fForce */);
189
190 char *pszPathURI = RTUriCreate("file" /* pszScheme */, "/" /* pszAuthority */,
191 pszPathNative, NULL /* pszQuery */, NULL /* pszFragment */);
192 if (pszPathURI)
193 {
194 rc = AppendURIPath(pszPathURI, fFlags);
195 RTStrFree(pszPathURI);
196 }
197 else
198 rc = VERR_INVALID_PARAMETER;
199
200 RTStrFree(pszPathNative);
201 }
202 else
203 rc = VERR_NO_MEMORY;
204
205 return rc;
206}
207
208int DnDURIList::AppendNativePathsFromList(const char *pszNativePaths, size_t cbNativePaths,
209 uint32_t fFlags)
210{
211 AssertPtrReturn(pszNativePaths, VERR_INVALID_POINTER);
212 AssertReturn(cbNativePaths, VERR_INVALID_PARAMETER);
213
214 RTCList<RTCString> lstPaths
215 = RTCString(pszNativePaths, cbNativePaths - 1).split("\r\n");
216 return AppendNativePathsFromList(lstPaths, fFlags);
217}
218
219int DnDURIList::AppendNativePathsFromList(const RTCList<RTCString> &lstNativePaths,
220 uint32_t fFlags)
221{
222 int rc = VINF_SUCCESS;
223
224 for (size_t i = 0; i < lstNativePaths.size(); i++)
225 {
226 const RTCString &strPath = lstNativePaths.at(i);
227 rc = AppendNativePath(strPath.c_str(), fFlags);
228 if (RT_FAILURE(rc))
229 break;
230 }
231
232 LogFlowFuncLeaveRC(rc);
233 return rc;
234}
235
236int DnDURIList::AppendURIPath(const char *pszURI, uint32_t fFlags)
237{
238 AssertPtrReturn(pszURI, VERR_INVALID_POINTER);
239
240 /** @todo Check for string termination? */
241#ifdef DEBUG_andy
242 LogFlowFunc(("pszPath=%s, fFlags=0x%x\n", pszURI, fFlags));
243#endif
244 int rc = VINF_SUCCESS;
245
246 /* Query the path component of a file URI. If this hasn't a
247 * file scheme NULL is returned. */
248 char *pszFilePath = RTUriFilePath(pszURI, URI_FILE_FORMAT_AUTO);
249 if (pszFilePath)
250 {
251 /* Add the path to our internal file list (recursive in
252 * the case of a directory). */
253 size_t cbPathLen = RTPathStripTrailingSlash(pszFilePath);
254 if (cbPathLen)
255 {
256 char *pszFileName = RTPathFilename(pszFilePath);
257 if (pszFileName)
258 {
259 Assert(pszFileName >= pszFilePath);
260 size_t cbBase = (fFlags & DNDURILIST_FLAGS_ABSOLUTE_PATHS)
261 ? 0 /* Use start of path as root. */
262 : pszFileName - pszFilePath;
263 char *pszRoot = &pszFilePath[cbBase];
264 m_lstRoot.append(pszRoot);
265#ifdef DEBUG_andy
266 LogFlowFunc(("pszFilePath=%s, pszFileName=%s, pszRoot=%s\n",
267 pszFilePath, pszFileName, pszRoot));
268#endif
269 rc = appendPathRecursive(pszFilePath, cbBase, fFlags);
270 }
271 else
272 rc = VERR_NOT_FOUND;
273 }
274 else
275 rc = VERR_INVALID_PARAMETER;
276
277 RTStrFree(pszFilePath);
278 }
279 else
280 rc = VERR_INVALID_PARAMETER;
281
282 LogFlowFuncLeaveRC(rc);
283 return rc;
284}
285
286int DnDURIList::AppendURIPathsFromList(const char *pszURIPaths, size_t cbURIPaths,
287 uint32_t fFlags)
288{
289 AssertPtrReturn(pszURIPaths, VERR_INVALID_POINTER);
290 AssertReturn(cbURIPaths, VERR_INVALID_PARAMETER);
291
292 RTCList<RTCString> lstPaths
293 = RTCString(pszURIPaths, cbURIPaths - 1).split("\r\n");
294 return AppendURIPathsFromList(lstPaths, fFlags);
295}
296
297int DnDURIList::AppendURIPathsFromList(const RTCList<RTCString> &lstURI,
298 uint32_t fFlags)
299{
300 int rc = VINF_SUCCESS;
301
302 for (size_t i = 0; i < lstURI.size(); i++)
303 {
304 RTCString strURI = lstURI.at(i);
305 rc = AppendURIPath(strURI.c_str(), fFlags);
306
307 if (RT_FAILURE(rc))
308 break;
309 }
310
311 LogFlowFuncLeaveRC(rc);
312 return rc;
313}
314
315void DnDURIList::Clear(void)
316{
317 m_lstRoot.clear();
318 m_lstTree.clear();
319
320 m_cbTotal = 0;
321}
322
323void DnDURIList::RemoveFirst(void)
324{
325 if (m_lstTree.isEmpty())
326 return;
327
328 DnDURIObject &curPath = m_lstTree.first();
329
330 uint64_t cbSize = curPath.GetSize();
331 Assert(m_cbTotal >= cbSize);
332 m_cbTotal -= cbSize; /* Adjust total size. */
333
334 m_lstTree.removeFirst();
335}
336
337int DnDURIList::RootFromURIData(const void *pvData, size_t cbData, uint32_t fFlags)
338{
339 AssertPtrReturn(pvData, VERR_INVALID_POINTER);
340 AssertReturn(cbData, VERR_INVALID_PARAMETER);
341
342 RTCList<RTCString> lstURI =
343 RTCString(static_cast<const char*>(pvData), cbData - 1).split("\r\n");
344 if (lstURI.isEmpty())
345 return VINF_SUCCESS;
346
347 int rc = VINF_SUCCESS;
348
349 for (size_t i = 0; i < lstURI.size(); ++i)
350 {
351 /* Query the path component of a file URI. If this hasn't a
352 * file scheme, NULL is returned. */
353 const char *pszURI = lstURI.at(i).c_str();
354 char *pszFilePath = RTUriFilePath(pszURI,
355 URI_FILE_FORMAT_AUTO);
356#ifdef DEBUG_andy
357 LogFlowFunc(("pszURI=%s, pszFilePath=%s\n", pszURI, pszFilePath));
358#endif
359 if (pszFilePath)
360 {
361 rc = DnDPathSanitize(pszFilePath, strlen(pszFilePath));
362 if (RT_SUCCESS(rc))
363 {
364 m_lstRoot.append(pszFilePath);
365 m_cTotal++;
366 }
367
368 RTStrFree(pszFilePath);
369 }
370 else
371 rc = VERR_INVALID_PARAMETER;
372
373 if (RT_FAILURE(rc))
374 break;
375 }
376
377 return rc;
378}
379
380RTCString DnDURIList::RootToString(const RTCString &strPathBase /* = "" */,
381 const RTCString &strSeparator /* = "\r\n" */)
382{
383 RTCString strRet;
384 for (size_t i = 0; i < m_lstRoot.size(); i++)
385 {
386 const char *pszCurRoot = m_lstRoot.at(i).c_str();
387#ifdef DEBUG_andy
388 LogFlowFunc(("pszCurRoot=%s\n", pszCurRoot));
389#endif
390 if (strPathBase.isNotEmpty())
391 {
392 char *pszPath = RTPathJoinA(strPathBase.c_str(), pszCurRoot);
393 if (pszPath)
394 {
395 char *pszPathURI = RTUriFileCreate(pszPath);
396 if (pszPathURI)
397 {
398 strRet += RTCString(pszPathURI) + strSeparator;
399 LogFlowFunc(("URI: %s\n", strRet.c_str()));
400 RTStrFree(pszPathURI);
401 }
402 else
403 break;
404 RTStrFree(pszPath);
405 }
406 else
407 break;
408 }
409 else
410 {
411 char *pszPathURI = RTUriFileCreate(pszCurRoot);
412 if (pszPathURI)
413 {
414 strRet += RTCString(pszPathURI) + strSeparator;
415 LogFlowFunc(("URI: %s\n", strRet.c_str()));
416 RTStrFree(pszPathURI);
417 }
418 else
419 break;
420 }
421 }
422
423 return strRet;
424}
425
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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