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