1 | /* $Id: DnDDir.cpp 56900 2015-07-09 14:24:26Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * DnD: Directory handling.
|
---|
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.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 | * Header Files *
|
---|
20 | ******************************************************************************/
|
---|
21 |
|
---|
22 | #include <iprt/assert.h>
|
---|
23 | #include <iprt/dir.h>
|
---|
24 | #include <iprt/path.h>
|
---|
25 | #include <iprt/string.h>
|
---|
26 |
|
---|
27 | #include <VBox/GuestHost/DragAndDrop.h>
|
---|
28 |
|
---|
29 | int DnDDirDroppedAddFile(PDNDDIRDROPPEDFILES pDir, const char *pszFile)
|
---|
30 | {
|
---|
31 | AssertPtrReturn(pDir, VERR_INVALID_POINTER);
|
---|
32 | AssertPtrReturn(pszFile, VERR_INVALID_POINTER);
|
---|
33 |
|
---|
34 | if (!pDir->lstFiles.contains(pszFile))
|
---|
35 | pDir->lstFiles.append(pszFile);
|
---|
36 | return VINF_SUCCESS;
|
---|
37 | }
|
---|
38 |
|
---|
39 | int DnDDirDroppedAddDir(PDNDDIRDROPPEDFILES pDir, const char *pszDir)
|
---|
40 | {
|
---|
41 | AssertPtrReturn(pDir, VERR_INVALID_POINTER);
|
---|
42 | AssertPtrReturn(pszDir, VERR_INVALID_POINTER);
|
---|
43 |
|
---|
44 | if (!pDir->lstDirs.contains(pszDir))
|
---|
45 | pDir->lstDirs.append(pszDir);
|
---|
46 | return VINF_SUCCESS;
|
---|
47 | }
|
---|
48 |
|
---|
49 | int DnDDirDroppedFilesCreateAndOpenEx(const char *pszPath, PDNDDIRDROPPEDFILES pDir)
|
---|
50 | {
|
---|
51 | AssertPtrReturn(pszPath, VERR_INVALID_POINTER);
|
---|
52 | AssertPtrReturn(pDir, VERR_INVALID_POINTER);
|
---|
53 |
|
---|
54 | char pszDropDir[RTPATH_MAX];
|
---|
55 | if (RTStrPrintf(pszDropDir, sizeof(pszDropDir), "%s", pszPath) <= 0)
|
---|
56 | return VERR_NO_MEMORY;
|
---|
57 |
|
---|
58 | /** @todo On Windows we also could use the registry to override
|
---|
59 | * this path, on Posix a dotfile and/or a guest property
|
---|
60 | * can be used. */
|
---|
61 |
|
---|
62 | /* Append our base drop directory. */
|
---|
63 | int rc = RTPathAppend(pszDropDir, sizeof(pszDropDir), "VirtualBox Dropped Files"); /** @todo Make this tag configurable? */
|
---|
64 | if (RT_FAILURE(rc))
|
---|
65 | return rc;
|
---|
66 |
|
---|
67 | /* Create it when necessary. */
|
---|
68 | if (!RTDirExists(pszDropDir))
|
---|
69 | {
|
---|
70 | rc = RTDirCreateFullPath(pszDropDir, RTFS_UNIX_IRWXU);
|
---|
71 | if (RT_FAILURE(rc))
|
---|
72 | return rc;
|
---|
73 | }
|
---|
74 |
|
---|
75 | /* The actually drop directory consist of the current time stamp and a
|
---|
76 | * unique number when necessary. */
|
---|
77 | char pszTime[64];
|
---|
78 | RTTIMESPEC time;
|
---|
79 | if (!RTTimeSpecToString(RTTimeNow(&time), pszTime, sizeof(pszTime)))
|
---|
80 | return VERR_BUFFER_OVERFLOW;
|
---|
81 | rc = DnDPathSanitizeFilename(pszTime, sizeof(pszTime));
|
---|
82 | if (RT_FAILURE(rc))
|
---|
83 | return rc;
|
---|
84 |
|
---|
85 | rc = RTPathAppend(pszDropDir, sizeof(pszDropDir), pszTime);
|
---|
86 | if (RT_FAILURE(rc))
|
---|
87 | return rc;
|
---|
88 |
|
---|
89 | /* Create it (only accessible by the current user) */
|
---|
90 | rc = RTDirCreateUniqueNumbered(pszDropDir, sizeof(pszDropDir), RTFS_UNIX_IRWXU, 3, '-');
|
---|
91 | if (RT_SUCCESS(rc))
|
---|
92 | {
|
---|
93 | PRTDIR phDir;
|
---|
94 | rc = RTDirOpen(&phDir, pszDropDir);
|
---|
95 | if (RT_SUCCESS(rc))
|
---|
96 | {
|
---|
97 | pDir->hDir = phDir;
|
---|
98 | pDir->strPathAbs = pszDropDir;
|
---|
99 | pDir->fOpen = true;
|
---|
100 | }
|
---|
101 | }
|
---|
102 |
|
---|
103 | return rc;
|
---|
104 | }
|
---|
105 |
|
---|
106 | int DnDDirDroppedFilesCreateAndOpenTemp(PDNDDIRDROPPEDFILES pDir)
|
---|
107 | {
|
---|
108 | AssertPtrReturn(pDir, VERR_INVALID_POINTER);
|
---|
109 |
|
---|
110 | char szTemp[RTPATH_MAX];
|
---|
111 |
|
---|
112 | /*
|
---|
113 | * Get the user's temp directory. Don't use the user's root directory (or
|
---|
114 | * something inside it) because we don't know for how long/if the data will
|
---|
115 | * be kept after the guest OS used it.
|
---|
116 | */
|
---|
117 | int rc = RTPathTemp(szTemp, sizeof(szTemp));
|
---|
118 | if (RT_FAILURE(rc))
|
---|
119 | return rc;
|
---|
120 |
|
---|
121 | return DnDDirDroppedFilesCreateAndOpenEx(szTemp, pDir);
|
---|
122 | }
|
---|
123 |
|
---|
124 | int DnDDirDroppedFilesClose(PDNDDIRDROPPEDFILES pDir, bool fRemove)
|
---|
125 | {
|
---|
126 | AssertPtrReturn(pDir, VERR_INVALID_POINTER);
|
---|
127 |
|
---|
128 | int rc = VINF_SUCCESS;
|
---|
129 | if (pDir->fOpen)
|
---|
130 | {
|
---|
131 | rc = RTDirClose(pDir->hDir);
|
---|
132 | if (RT_SUCCESS(rc))
|
---|
133 | pDir->fOpen = false;
|
---|
134 | }
|
---|
135 | if (RT_SUCCESS(rc))
|
---|
136 | {
|
---|
137 | pDir->lstDirs.clear();
|
---|
138 | pDir->lstFiles.clear();
|
---|
139 |
|
---|
140 | if ( fRemove
|
---|
141 | && pDir->strPathAbs.isNotEmpty())
|
---|
142 | {
|
---|
143 | /* Try removing the (empty) drop directory in any case. */
|
---|
144 | rc = RTDirRemove(pDir->strPathAbs.c_str());
|
---|
145 | if (RT_SUCCESS(rc)) /* Only clear if successfully removed. */
|
---|
146 | pDir->strPathAbs = "";
|
---|
147 | }
|
---|
148 | }
|
---|
149 |
|
---|
150 | return rc;
|
---|
151 | }
|
---|
152 |
|
---|
153 | const char *DnDDirDroppedFilesGetDirAbs(PDNDDIRDROPPEDFILES pDir)
|
---|
154 | {
|
---|
155 | AssertPtrReturn(pDir, NULL);
|
---|
156 | return pDir->strPathAbs.c_str();
|
---|
157 | }
|
---|
158 |
|
---|
159 | int DnDDirDroppedFilesRollback(PDNDDIRDROPPEDFILES pDir)
|
---|
160 | {
|
---|
161 | AssertPtrReturn(pDir, VERR_INVALID_POINTER);
|
---|
162 |
|
---|
163 | if (pDir->strPathAbs.isEmpty())
|
---|
164 | return VINF_SUCCESS;
|
---|
165 |
|
---|
166 | int rc = VINF_SUCCESS;
|
---|
167 | int rc2;
|
---|
168 |
|
---|
169 | /* Rollback by removing any stuff created.
|
---|
170 | * Note: Only remove empty directories, never ever delete
|
---|
171 | * anything recursive here! Steam (tm) knows best ... :-) */
|
---|
172 | for (size_t i = 0; i < pDir->lstFiles.size(); i++)
|
---|
173 | {
|
---|
174 | rc2 = RTFileDelete(pDir->lstFiles.at(i).c_str());
|
---|
175 | if (RT_SUCCESS(rc))
|
---|
176 | rc = rc2;
|
---|
177 | }
|
---|
178 |
|
---|
179 | for (size_t i = 0; i < pDir->lstDirs.size(); i++)
|
---|
180 | {
|
---|
181 | rc2 = RTDirRemove(pDir->lstDirs.at(i).c_str());
|
---|
182 | if (RT_SUCCESS(rc))
|
---|
183 | rc = rc2;
|
---|
184 | }
|
---|
185 |
|
---|
186 | /* Try to remove the empty root dropped files directory as well. */
|
---|
187 | rc2 = RTDirRemove(pDir->strPathAbs.c_str());
|
---|
188 | if (RT_SUCCESS(rc))
|
---|
189 | rc = rc2;
|
---|
190 |
|
---|
191 | return rc;
|
---|
192 | }
|
---|
193 |
|
---|