DnDDir.cpp revision 82391de567696f10b21a762fde6a06fe3c266d28
74fe172698ba936102e120dae998c9ebd09cfbdfvboxsync/* $Id$ */
74fe172698ba936102e120dae998c9ebd09cfbdfvboxsync/** @file
74fe172698ba936102e120dae998c9ebd09cfbdfvboxsync * DnD: Directory handling.
74fe172698ba936102e120dae998c9ebd09cfbdfvboxsync */
74fe172698ba936102e120dae998c9ebd09cfbdfvboxsync
74fe172698ba936102e120dae998c9ebd09cfbdfvboxsync/*
74fe172698ba936102e120dae998c9ebd09cfbdfvboxsync * Copyright (C) 2014 Oracle Corporation
74fe172698ba936102e120dae998c9ebd09cfbdfvboxsync *
74fe172698ba936102e120dae998c9ebd09cfbdfvboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
74fe172698ba936102e120dae998c9ebd09cfbdfvboxsync * available from http://www.virtualbox.org. This file is free software;
74fe172698ba936102e120dae998c9ebd09cfbdfvboxsync * you can redistribute it and/or modify it under the terms of the GNU
74fe172698ba936102e120dae998c9ebd09cfbdfvboxsync * General Public License (GPL) as published by the Free Software
74fe172698ba936102e120dae998c9ebd09cfbdfvboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
74fe172698ba936102e120dae998c9ebd09cfbdfvboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
74fe172698ba936102e120dae998c9ebd09cfbdfvboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
74fe172698ba936102e120dae998c9ebd09cfbdfvboxsync */
74fe172698ba936102e120dae998c9ebd09cfbdfvboxsync
74fe172698ba936102e120dae998c9ebd09cfbdfvboxsync/******************************************************************************
74fe172698ba936102e120dae998c9ebd09cfbdfvboxsync * Header Files *
74fe172698ba936102e120dae998c9ebd09cfbdfvboxsync ******************************************************************************/
74fe172698ba936102e120dae998c9ebd09cfbdfvboxsync
74fe172698ba936102e120dae998c9ebd09cfbdfvboxsync#include <iprt/assert.h>
74fe172698ba936102e120dae998c9ebd09cfbdfvboxsync#include <iprt/dir.h>
74fe172698ba936102e120dae998c9ebd09cfbdfvboxsync#include <iprt/path.h>
74fe172698ba936102e120dae998c9ebd09cfbdfvboxsync#include <iprt/string.h>
74fe172698ba936102e120dae998c9ebd09cfbdfvboxsync
74fe172698ba936102e120dae998c9ebd09cfbdfvboxsync#include <VBox/GuestHost/DragAndDrop.h>
74fe172698ba936102e120dae998c9ebd09cfbdfvboxsync
74fe172698ba936102e120dae998c9ebd09cfbdfvboxsyncint DnDDirCreateDroppedFilesEx(const char *pszPath,
74fe172698ba936102e120dae998c9ebd09cfbdfvboxsync char *pszDropDir, size_t cbDropDir)
74fe172698ba936102e120dae998c9ebd09cfbdfvboxsync{
74fe172698ba936102e120dae998c9ebd09cfbdfvboxsync AssertPtrReturn(pszPath, VERR_INVALID_POINTER);
74fe172698ba936102e120dae998c9ebd09cfbdfvboxsync AssertPtrReturn(pszDropDir, VERR_INVALID_POINTER);
74fe172698ba936102e120dae998c9ebd09cfbdfvboxsync AssertReturn(cbDropDir, VERR_INVALID_PARAMETER);
74fe172698ba936102e120dae998c9ebd09cfbdfvboxsync
74fe172698ba936102e120dae998c9ebd09cfbdfvboxsync if (RTStrPrintf(pszDropDir, cbDropDir, "%s", pszPath) <= 0)
74fe172698ba936102e120dae998c9ebd09cfbdfvboxsync return VERR_NO_MEMORY;
74fe172698ba936102e120dae998c9ebd09cfbdfvboxsync
74fe172698ba936102e120dae998c9ebd09cfbdfvboxsync /** @todo On Windows we also could use the registry to override
74fe172698ba936102e120dae998c9ebd09cfbdfvboxsync * this path, on Posix a dotfile and/or a guest property
74fe172698ba936102e120dae998c9ebd09cfbdfvboxsync * can be used. */
74fe172698ba936102e120dae998c9ebd09cfbdfvboxsync
74fe172698ba936102e120dae998c9ebd09cfbdfvboxsync /* Append our base drop directory. */
74fe172698ba936102e120dae998c9ebd09cfbdfvboxsync int rc = RTPathAppend(pszDropDir, cbDropDir, "VirtualBox Dropped Files");
74fe172698ba936102e120dae998c9ebd09cfbdfvboxsync if (RT_FAILURE(rc))
74fe172698ba936102e120dae998c9ebd09cfbdfvboxsync return rc;
74fe172698ba936102e120dae998c9ebd09cfbdfvboxsync
74fe172698ba936102e120dae998c9ebd09cfbdfvboxsync /* Create it when necessary. */
74fe172698ba936102e120dae998c9ebd09cfbdfvboxsync if (!RTDirExists(pszDropDir))
74fe172698ba936102e120dae998c9ebd09cfbdfvboxsync {
74fe172698ba936102e120dae998c9ebd09cfbdfvboxsync rc = RTDirCreateFullPath(pszDropDir, RTFS_UNIX_IRWXU);
74fe172698ba936102e120dae998c9ebd09cfbdfvboxsync if (RT_FAILURE(rc))
74fe172698ba936102e120dae998c9ebd09cfbdfvboxsync return rc;
74fe172698ba936102e120dae998c9ebd09cfbdfvboxsync }
74fe172698ba936102e120dae998c9ebd09cfbdfvboxsync
74fe172698ba936102e120dae998c9ebd09cfbdfvboxsync /* The actually drop directory consist of the current time stamp and a
74fe172698ba936102e120dae998c9ebd09cfbdfvboxsync * unique number when necessary. */
74fe172698ba936102e120dae998c9ebd09cfbdfvboxsync char pszTime[64];
74fe172698ba936102e120dae998c9ebd09cfbdfvboxsync RTTIMESPEC time;
74fe172698ba936102e120dae998c9ebd09cfbdfvboxsync if (!RTTimeSpecToString(RTTimeNow(&time), pszTime, sizeof(pszTime)))
74fe172698ba936102e120dae998c9ebd09cfbdfvboxsync return VERR_BUFFER_OVERFLOW;
74fe172698ba936102e120dae998c9ebd09cfbdfvboxsync rc = DnDPathSanitizeFilename(pszTime, sizeof(pszTime));
74fe172698ba936102e120dae998c9ebd09cfbdfvboxsync if (RT_FAILURE(rc))
74fe172698ba936102e120dae998c9ebd09cfbdfvboxsync return rc;
74fe172698ba936102e120dae998c9ebd09cfbdfvboxsync
74fe172698ba936102e120dae998c9ebd09cfbdfvboxsync rc = RTPathAppend(pszDropDir, cbDropDir, pszTime);
74fe172698ba936102e120dae998c9ebd09cfbdfvboxsync if (RT_FAILURE(rc))
74fe172698ba936102e120dae998c9ebd09cfbdfvboxsync return rc;
74fe172698ba936102e120dae998c9ebd09cfbdfvboxsync
74fe172698ba936102e120dae998c9ebd09cfbdfvboxsync /* Create it (only accessible by the current user) */
74fe172698ba936102e120dae998c9ebd09cfbdfvboxsync return RTDirCreateUniqueNumbered(pszDropDir, cbDropDir, RTFS_UNIX_IRWXU, 3, '-');
74fe172698ba936102e120dae998c9ebd09cfbdfvboxsync}
74fe172698ba936102e120dae998c9ebd09cfbdfvboxsync
74fe172698ba936102e120dae998c9ebd09cfbdfvboxsyncint DnDDirCreateDroppedFiles(char *pszDropDir, size_t cbDropDir)
74fe172698ba936102e120dae998c9ebd09cfbdfvboxsync{
74fe172698ba936102e120dae998c9ebd09cfbdfvboxsync AssertPtrReturn(pszDropDir, VERR_INVALID_POINTER);
74fe172698ba936102e120dae998c9ebd09cfbdfvboxsync AssertReturn(cbDropDir, VERR_INVALID_PARAMETER);
74fe172698ba936102e120dae998c9ebd09cfbdfvboxsync
74fe172698ba936102e120dae998c9ebd09cfbdfvboxsync char szTemp[RTPATH_MAX];
74fe172698ba936102e120dae998c9ebd09cfbdfvboxsync
74fe172698ba936102e120dae998c9ebd09cfbdfvboxsync /* Get the user's temp directory. Don't use the user's root directory (or
74fe172698ba936102e120dae998c9ebd09cfbdfvboxsync * something inside it) because we don't know for how long/if the data will
74fe172698ba936102e120dae998c9ebd09cfbdfvboxsync * be kept after the guest OS used it. */
74fe172698ba936102e120dae998c9ebd09cfbdfvboxsync int rc = RTPathTemp(szTemp, sizeof(szTemp));
74fe172698ba936102e120dae998c9ebd09cfbdfvboxsync if (RT_FAILURE(rc))
74fe172698ba936102e120dae998c9ebd09cfbdfvboxsync return rc;
74fe172698ba936102e120dae998c9ebd09cfbdfvboxsync
74fe172698ba936102e120dae998c9ebd09cfbdfvboxsync return DnDDirCreateDroppedFilesEx(szTemp, pszDropDir, cbDropDir);
74fe172698ba936102e120dae998c9ebd09cfbdfvboxsync}
74fe172698ba936102e120dae998c9ebd09cfbdfvboxsync
74fe172698ba936102e120dae998c9ebd09cfbdfvboxsync