VirtualBox

source: vbox/trunk/src/VBox/GuestHost/DragAndDrop/DnDDir.cpp@ 57372

Last change on this file since 57372 was 57372, checked in by vboxsync, 10 years ago

scm: fixes in previous cleanup run.

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

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