summaryrefslogtreecommitdiff
path: root/src/VBox/GuestHost/DragAndDrop/DnDDroppedFiles.cpp
blob: bde2b2cccdfd016cd7854fec890eeaee5355dd2d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/* $Id$ */
/** @file
 * DnD - Directory handling.
 */

/*
 * Copyright (C) 2014-2022 Oracle Corporation
 *
 * This file is part of VirtualBox Open Source Edition (OSE), as
 * available from http://www.virtualbox.org. This file is free software;
 * you can redistribute it and/or modify it under the terms of the GNU
 * General Public License (GPL) as published by the Free Software
 * Foundation, in version 2 as it comes in the "COPYING" file of the
 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
 */


/*********************************************************************************************************************************
*   Header Files                                                                                                                 *
*********************************************************************************************************************************/
#define LOG_GROUP LOG_GROUP_GUEST_DND
#include <VBox/GuestHost/DragAndDrop.h>

#include <iprt/assert.h>
#include <iprt/dir.h>
#include <iprt/err.h>
#include <iprt/file.h>
#include <iprt/mem.h>
#include <iprt/path.h>
#include <iprt/string.h>

#include <VBox/log.h>


/*********************************************************************************************************************************
*   Prototypes                                                                                                                   *
*********************************************************************************************************************************/
static int dndDroppedFilesCloseInternal(PDNDDROPPEDFILES pDF);


/**
 * Initializes a DnD Dropped Files struct, internal version.
 *
 * @returns VBox status code.
 * @param   pDF                 DnD Dropped Files to initialize.
 */
static int dndDroppedFilesInitInternal(PDNDDROPPEDFILES pDF)
{
    pDF->m_fOpen    = 0;
    pDF->m_hDir     = NIL_RTDIR;
    pDF->pszPathAbs = NULL;

    RTListInit(&pDF->m_lstDirs);
    RTListInit(&pDF->m_lstFiles);

    return VINF_SUCCESS;
}

/**
 * Initializes a DnD Dropped Files struct, extended version.
 *
 * @returns VBox status code.
 * @param   pDF                 DnD Dropped Files to initialize.
 * @param   fFlags              Dropped Files flags to use for initialization.
 */
int DnDDroppedFilesInitEx(PDNDDROPPEDFILES pDF,
                          const char *pszPath, DNDURIDROPPEDFILEFLAGS fFlags /* = DNDURIDROPPEDFILE_FLAGS_NONE */)
{
    int rc = dndDroppedFilesInitInternal(pDF);
    if (RT_FAILURE(rc))
        return rc;

    return DnDDroppedFilesOpenEx(pDF, pszPath, fFlags);
}

/**
 * Initializes a DnD Dropped Files struct.
 *
 * @returns VBox status code.
 * @param   pDF                 DnD Dropped Files to initialize.
 */
int DnDDroppedFilesInit(PDNDDROPPEDFILES pDF)
{
    return dndDroppedFilesInitInternal(pDF);
}

/**
 * Destroys a DnD Dropped Files struct.
 *
 * Note: This does *not* (physically) delete any added content.
 *       Make sure to call DnDDroppedFilesReset() then.
 *
 * @param   pDF                 DnD Dropped Files to destroy.
 */
void DnDDroppedFilesDestroy(PDNDDROPPEDFILES pDF)
{
    /* Only make sure to not leak any handles and stuff, don't delete any
     * directories / files here. */
    dndDroppedFilesCloseInternal(pDF);

    RTStrFree(pDF->pszPathAbs);
    pDF->pszPathAbs = NULL;
}

/**
 * Adds a file reference to a Dropped Files directory.
 *
 * @returns VBox status code.
 * @param   pDF                 DnD Dropped Files to add file to.
 * @param   pszFile             Path of file entry to add.
 */
int DnDDroppedFilesAddFile(PDNDDROPPEDFILES pDF, const char *pszFile)
{
    AssertPtrReturn(pszFile, VERR_INVALID_POINTER);

    PDNDDROPPEDFILESENTRY pEntry = (PDNDDROPPEDFILESENTRY)RTMemAlloc(sizeof(DNDDROPPEDFILESENTRY));
    if (!pEntry)
        return VERR_NO_MEMORY;

    pEntry->pszPath = RTStrDup(pszFile);
    if (pEntry->pszPath)
    {
        RTListAppend(&pDF->m_lstFiles, &pEntry->Node);
        return VINF_SUCCESS;
    }

    RTMemFree(pEntry);
    return VERR_NO_MEMORY;
}

/**
 * Adds a directory reference to a Dropped Files directory.
 *
 * Note: This does *not* (recursively) add sub entries.
 *
 * @returns VBox status code.
 * @param   pDF                 DnD Dropped Files to add directory to.
 * @param   pszDir              Path of directory entry to add.
 */
int DnDDroppedFilesAddDir(PDNDDROPPEDFILES pDF, const char *pszDir)
{
    AssertPtrReturn(pszDir, VERR_INVALID_POINTER);

    PDNDDROPPEDFILESENTRY pEntry = (PDNDDROPPEDFILESENTRY)RTMemAlloc(sizeof(DNDDROPPEDFILESENTRY));
    if (!pEntry)
        return VERR_NO_MEMORY;

    pEntry->pszPath = RTStrDup(pszDir);
    if (pEntry->pszPath)
    {
        RTListAppend(&pDF->m_lstDirs, &pEntry->Node);
        return VINF_SUCCESS;
    }

    RTMemFree(pEntry);
    return VERR_NO_MEMORY;
}

/**
 * Closes the dropped files directory handle, internal version.
 *
 * @returns VBox status code.
 * @param   pDF                 DnD Dropped Files to close.
 */
static int dndDroppedFilesCloseInternal(PDNDDROPPEDFILES pDF)
{
    int rc;
    if (pDF->m_hDir != NULL)
    {
        rc = RTDirClose(pDF->m_hDir);
        if (RT_SUCCESS(rc))
            pDF->m_hDir = NULL;
    }
    else
        rc = VINF_SUCCESS;

    LogFlowFuncLeaveRC(rc);
    return rc;
}

/**
 * Closes the dropped files directory handle.
 *
 * @returns VBox status code.
 * @param   pDF                 DnD Dropped Files to close.
 */
int DnDDroppedFilesClose(PDNDDROPPEDFILES pDF)
{
    return dndDroppedFilesCloseInternal(pDF);
}

/**
 * Returns the absolute path of the dropped files directory.
 *
 * @returns Pointer to absolute path of the dropped files directory.
 * @param   pDF                 DnD Dropped Files return absolute path of the dropped files directory for.
 */
const char *DnDDroppedFilesGetDirAbs(PDNDDROPPEDFILES pDF)
{
    return pDF->pszPathAbs;
}

/**
 * Returns whether the dropped files directory has been opened or not.
 *
 * @returns \c true if open, \c false if not.
 * @param   pDF                 DnD Dropped Files to return open status for.
 */
bool DnDDroppedFilesIsOpen(PDNDDROPPEDFILES pDF)
{
    return (pDF->m_hDir != NULL);
}

/**
 * Opens (creates) the dropped files directory.
 *
 * @returns VBox status code.
 * @param   pDF                 DnD Dropped Files to open.
 * @param   pszPath             Absolute path where to create the dropped files directory.
 * @param   fFlags              Dropped files flags to use for this directory.
 */
int DnDDroppedFilesOpenEx(PDNDDROPPEDFILES pDF,
                          const char *pszPath, DNDURIDROPPEDFILEFLAGS fFlags /* = DNDURIDROPPEDFILE_FLAGS_NONE */)
{
    AssertPtrReturn(pszPath, VERR_INVALID_POINTER);
    AssertReturn(fFlags == 0, VERR_INVALID_PARAMETER); /* Flags not supported yet. */

    int rc;

    do
    {
        char szDropDir[RTPATH_MAX];
        RTStrPrintf(szDropDir, sizeof(szDropDir), "%s", pszPath);

        /** @todo On Windows we also could use the registry to override
         *        this path, on Posix a dotfile and/or a guest property
         *        can be used. */

        /* Append our base drop directory. */
        rc = RTPathAppend(szDropDir, sizeof(szDropDir), "VirtualBox Dropped Files"); /** @todo Make this tag configurable? */
        if (RT_FAILURE(rc))
            break;

        /* Create it when necessary. */
        if (!RTDirExists(szDropDir))
        {
            rc = RTDirCreateFullPath(szDropDir, RTFS_UNIX_IRWXU);
            if (RT_FAILURE(rc))
                break;
        }

        /* The actually drop directory consist of the current time stamp and a
         * unique number when necessary. */
        char szTime[64];
        RTTIMESPEC time;
        if (!RTTimeSpecToString(RTTimeNow(&time), szTime, sizeof(szTime)))
        {
            rc = VERR_BUFFER_OVERFLOW;
            break;
        }

        rc = DnDPathSanitizeFileName(szTime, sizeof(szTime));
        if (RT_FAILURE(rc))
            break;

        rc = RTPathAppend(szDropDir, sizeof(szDropDir), szTime);
        if (RT_FAILURE(rc))
            break;

        /* Create it (only accessible by the current user) */
        rc = RTDirCreateUniqueNumbered(szDropDir, sizeof(szDropDir), RTFS_UNIX_IRWXU, 3, '-');
        if (RT_SUCCESS(rc))
        {
            RTDIR hDir;
            rc = RTDirOpen(&hDir, szDropDir);
            if (RT_SUCCESS(rc))
            {
                pDF->pszPathAbs = RTStrDup(szDropDir);
                AssertPtrBreakStmt(pDF->pszPathAbs, rc = VERR_NO_MEMORY);
                pDF->m_hDir     = hDir;
                pDF->m_fOpen    = fFlags;
            }
        }

    } while (0);

    LogFlowFuncLeaveRC(rc);
    return rc;
}

/**
 * Opens (creates) the dropped files directory in the system's temp directory.
 *
 * @returns VBox status code.
 * @param   pDF                 DnD Dropped Files to open.
 * @param   fFlags              Dropped files flags to use for this directory.
 */
int DnDDroppedFilesOpenTemp(PDNDDROPPEDFILES pDF, DNDURIDROPPEDFILEFLAGS fFlags)
{
    AssertReturn(fFlags == 0, VERR_INVALID_PARAMETER); /* Flags not supported yet. */

    /*
     * Get the user's temp directory. Don't use the user's root directory (or
     * something inside it) because we don't know for how long/if the data will
     * be kept after the guest OS used it.
     */
    char szTemp[RTPATH_MAX];
    int rc = RTPathTemp(szTemp, sizeof(szTemp));
    if (RT_SUCCESS(rc))
        rc = DnDDroppedFilesOpenEx(pDF, szTemp, fFlags);

    return rc;
}

/**
 * Free's an internal DnD Dropped Files entry.
 *
 * @param   pEntry              Pointer to entry to free. The pointer will be invalid after calling.
 */
static void dndDroppedFilesEntryFree(PDNDDROPPEDFILESENTRY pEntry)
{
    if (!pEntry)
        return;
    RTStrFree(pEntry->pszPath);
    RTListNodeRemove(&pEntry->Node);
    RTMemFree(pEntry);
}

/**
 * Resets an internal DnD Dropped Files list.
 *
 * @param   pListAnchor         Pointer to list (anchor) to reset.
 */
static void dndDroppedFilesResetList(PRTLISTANCHOR pListAnchor)
{
    PDNDDROPPEDFILESENTRY pEntryCur, pEntryNext;
    RTListForEachSafe(pListAnchor, pEntryCur, pEntryNext, DNDDROPPEDFILESENTRY, Node)
        dndDroppedFilesEntryFree(pEntryCur);
    Assert(RTListIsEmpty(pListAnchor));
}

/**
 * Resets a droppped files directory.
 *
 * @returns VBox status code.
 * @param   pDF                 DnD Dropped Files to reset.
 * @param   fDelete             Whether to physically delete the directory and its content
 *                              or just clear the internal references.
 */
int DnDDroppedFilesReset(PDNDDROPPEDFILES pDF, bool fDelete)
{
    int rc = dndDroppedFilesCloseInternal(pDF);
    if (RT_SUCCESS(rc))
    {
        if (fDelete)
        {
            rc = DnDDroppedFilesRollback(pDF);
        }
        else
        {
            dndDroppedFilesResetList(&pDF->m_lstDirs);
            dndDroppedFilesResetList(&pDF->m_lstFiles);
        }
    }

    LogFlowFuncLeaveRC(rc);
    return rc;
}

/**
 * Re-opens a droppes files directory.
 *
 * @returns VBox status code, or VERR_NOT_FOUND if the dropped files directory has not been opened before.
 * @param   pDF                 DnD Dropped Files to re-open.
 */
int DnDDroppedFilesReopen(PDNDDROPPEDFILES pDF)
{
    if (!pDF->pszPathAbs)
        return VERR_NOT_FOUND;

    return DnDDroppedFilesOpenEx(pDF, pDF->pszPathAbs, pDF->m_fOpen);
}

/**
 * Performs a rollback of a dropped files directory.
 * This cleans the directory by physically deleting all files / directories which have been added before.
 *
 * @returns VBox status code.
 * @param   pDF                 DnD Dropped Files to roll back.
 */
int DnDDroppedFilesRollback(PDNDDROPPEDFILES pDF)
{
    if (!pDF->pszPathAbs)
        return VINF_SUCCESS;

    int rc = VINF_SUCCESS;

    /* Rollback by removing any stuff created.
     * Note: Only remove empty directories, never ever delete
     *       anything recursive here! Steam (tm) knows best ... :-) */
    int rc2;
    PDNDDROPPEDFILESENTRY pEntryCur, pEntryNext;
    RTListForEachSafe(&pDF->m_lstFiles, pEntryCur, pEntryNext, DNDDROPPEDFILESENTRY, Node)
    {
        rc2 = RTFileDelete(pEntryCur->pszPath);
        if (RT_SUCCESS(rc2))
            dndDroppedFilesEntryFree(pEntryCur);
        else if (RT_SUCCESS(rc))
           rc = rc2;
        /* Keep going. */
    }

    RTListForEachSafe(&pDF->m_lstDirs, pEntryCur, pEntryNext, DNDDROPPEDFILESENTRY, Node)
    {
        rc2 = RTDirRemove(pEntryCur->pszPath);
        if (RT_SUCCESS(rc2))
            dndDroppedFilesEntryFree(pEntryCur);
        else if (RT_SUCCESS(rc))
            rc = rc2;
        /* Keep going. */
    }

    if (RT_SUCCESS(rc))
    {
        rc2 = dndDroppedFilesCloseInternal(pDF);
        if (RT_SUCCESS(rc2))
        {
            /* Try to remove the empty root dropped files directory as well.
             * Might return VERR_DIR_NOT_EMPTY or similar. */
            rc2 = RTDirRemove(pDF->pszPathAbs);
        }
        if (RT_SUCCESS(rc))
            rc = rc2;
    }

    LogFlowFuncLeaveRC(rc);
    return rc;
}