VBoxGuestR3LibSharedFolders.cpp revision c08d89470d5384eaa863a8a371c0e4a38e9ded60
5923a1e1a9eff5786a4dc5a281e11b7dc4fd0811vboxsync * VBoxGuestR3Lib - Ring-3 Support Library for VirtualBox guest additions, shared folders.
5923a1e1a9eff5786a4dc5a281e11b7dc4fd0811vboxsync * Copyright (C) 2010 Oracle Corporation
5923a1e1a9eff5786a4dc5a281e11b7dc4fd0811vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
5923a1e1a9eff5786a4dc5a281e11b7dc4fd0811vboxsync * available from http://www.virtualbox.org. This file is free software;
5923a1e1a9eff5786a4dc5a281e11b7dc4fd0811vboxsync * you can redistribute it and/or modify it under the terms of the GNU
5923a1e1a9eff5786a4dc5a281e11b7dc4fd0811vboxsync * General Public License (GPL) as published by the Free Software
5923a1e1a9eff5786a4dc5a281e11b7dc4fd0811vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
5923a1e1a9eff5786a4dc5a281e11b7dc4fd0811vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
5923a1e1a9eff5786a4dc5a281e11b7dc4fd0811vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
5923a1e1a9eff5786a4dc5a281e11b7dc4fd0811vboxsync * The contents of this file may alternatively be used under the terms
5923a1e1a9eff5786a4dc5a281e11b7dc4fd0811vboxsync * of the Common Development and Distribution License Version 1.0
5923a1e1a9eff5786a4dc5a281e11b7dc4fd0811vboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
5923a1e1a9eff5786a4dc5a281e11b7dc4fd0811vboxsync * VirtualBox OSE distribution, in which case the provisions of the
5923a1e1a9eff5786a4dc5a281e11b7dc4fd0811vboxsync * CDDL are applicable instead of those of the GPL.
5923a1e1a9eff5786a4dc5a281e11b7dc4fd0811vboxsync * You may elect to license modified versions of this file under the
5923a1e1a9eff5786a4dc5a281e11b7dc4fd0811vboxsync * terms and conditions of either the GPL or the CDDL or both.
5923a1e1a9eff5786a4dc5a281e11b7dc4fd0811vboxsync/*******************************************************************************
5923a1e1a9eff5786a4dc5a281e11b7dc4fd0811vboxsync* Header Files *
5923a1e1a9eff5786a4dc5a281e11b7dc4fd0811vboxsync*******************************************************************************/
5923a1e1a9eff5786a4dc5a281e11b7dc4fd0811vboxsync#include <VBox/shflsvc.h> /** @todo File should be moved to VBox/HostServices/SharedFolderSvc.h */
5923a1e1a9eff5786a4dc5a281e11b7dc4fd0811vboxsync * Connects to the shared folder service.
5923a1e1a9eff5786a4dc5a281e11b7dc4fd0811vboxsync * @returns VBox status code
5923a1e1a9eff5786a4dc5a281e11b7dc4fd0811vboxsync * @param pu32ClientId Where to put the client id on success. The client id
5923a1e1a9eff5786a4dc5a281e11b7dc4fd0811vboxsync * must be passed to all the other calls to the service.
5923a1e1a9eff5786a4dc5a281e11b7dc4fd0811vboxsyncVBGLR3DECL(int) VbglR3SharedFolderConnect(uint32_t *pu32ClientId)
5923a1e1a9eff5786a4dc5a281e11b7dc4fd0811vboxsync strcpy(Info.Loc.u.host.achName, "VBoxSharedFolders");
ad27e1d5e48ca41245120c331cc88b50464813cevboxsync Info.u32ClientID = UINT32_MAX; /* try make valgrind shut up. */
5923a1e1a9eff5786a4dc5a281e11b7dc4fd0811vboxsync int rc = vbglR3DoIOCtl(VBOXGUEST_IOCTL_HGCM_CONNECT, &Info, sizeof(Info));
5923a1e1a9eff5786a4dc5a281e11b7dc4fd0811vboxsync * Disconnect from the shared folder service.
5923a1e1a9eff5786a4dc5a281e11b7dc4fd0811vboxsync * @returns VBox status code.
5923a1e1a9eff5786a4dc5a281e11b7dc4fd0811vboxsync * @param u32ClientId The client id returned by VbglR3InfoSvcConnect().
5923a1e1a9eff5786a4dc5a281e11b7dc4fd0811vboxsyncVBGLR3DECL(int) VbglR3SharedFolderDisconnect(uint32_t u32ClientId)
5923a1e1a9eff5786a4dc5a281e11b7dc4fd0811vboxsync int rc = vbglR3DoIOCtl(VBOXGUEST_IOCTL_HGCM_DISCONNECT, &Info, sizeof(Info));
3e6da1894f26075b74820b75bf5ef7e4a64070edvboxsync * Checks whether a shared folder share exists or not.
3e6da1894f26075b74820b75bf5ef7e4a64070edvboxsync * @returns True if shared folder exists, false if not.
3e6da1894f26075b74820b75bf5ef7e4a64070edvboxsync * @param u32ClientId The client id returned by VbglR3InfoSvcConnect().
3e6da1894f26075b74820b75bf5ef7e4a64070edvboxsync * @param pszShareName Shared folder name to check.
6d22c1a25757a476158a4ed2b463f6baf09ae813vboxsyncVBGLR3DECL(bool) VbglR3SharedFolderExists(uint32_t u32ClientId, const char *pszShareName)
3e6da1894f26075b74820b75bf5ef7e4a64070edvboxsync /** @todo Use some caching here? */
3e6da1894f26075b74820b75bf5ef7e4a64070edvboxsync bool fFound = false;
3e6da1894f26075b74820b75bf5ef7e4a64070edvboxsync int rc = VbglR3SharedFolderGetMappings(u32ClientId, true /* Only process auto-mounted folders */,
3e6da1894f26075b74820b75bf5ef7e4a64070edvboxsync for (uint32_t i = 0; i < cMappings && !fFound; i++)
3e6da1894f26075b74820b75bf5ef7e4a64070edvboxsync rc = VbglR3SharedFolderGetName(u32ClientId, paMappings[i].u32Root, &pszName);
5923a1e1a9eff5786a4dc5a281e11b7dc4fd0811vboxsync * Get the list of available shared folders.
5923a1e1a9eff5786a4dc5a281e11b7dc4fd0811vboxsync * @returns VBox status code.
ba31bc205e96548d3557ae82087dc020a52b6a0avboxsync * @param u32ClientId The client id returned by VbglR3SharedFolderConnect().
dd90fee6b212c87998fe80f39a1645e4bbe1acb9vboxsync * @param fAutoMountOnly Flag whether only auto-mounted shared folders
dd90fee6b212c87998fe80f39a1645e4bbe1acb9vboxsync * should be reported.
ba31bc205e96548d3557ae82087dc020a52b6a0avboxsync * @param ppaMappings Allocated array which will retrieve the mapping info. Needs
ba31bc205e96548d3557ae82087dc020a52b6a0avboxsync * to be freed with VbglR3SharedFolderFreeMappings() later.
ba31bc205e96548d3557ae82087dc020a52b6a0avboxsync * @param pcMappings The number of mappings returned in @a ppaMappings.
dd90fee6b212c87998fe80f39a1645e4bbe1acb9vboxsyncVBGLR3DECL(int) VbglR3SharedFolderGetMappings(uint32_t u32ClientId, bool fAutoMountOnly,
ba31bc205e96548d3557ae82087dc020a52b6a0avboxsync PVBGLR3SHAREDFOLDERMAPPING *ppaMappings, uint32_t *pcMappings)
5923a1e1a9eff5786a4dc5a281e11b7dc4fd0811vboxsync /* Set the mapping flags. */
dd90fee6b212c87998fe80f39a1645e4bbe1acb9vboxsync uint32_t u32Flags = 0; /** @todo SHFL_MF_UTF8 is not implemented yet. */
dd90fee6b212c87998fe80f39a1645e4bbe1acb9vboxsync if (fAutoMountOnly) /* We only want the mappings which get auto-mounted. */
ba31bc205e96548d3557ae82087dc020a52b6a0avboxsync * Prepare and get the actual mappings from the host service.
ba31bc205e96548d3557ae82087dc020a52b6a0avboxsync uint32_t cMappings = 8; /* Should be a good default value. */
ba31bc205e96548d3557ae82087dc020a52b6a0avboxsync uint32_t cbSize = cMappings * sizeof(VBGLR3SHAREDFOLDERMAPPING);
ba31bc205e96548d3557ae82087dc020a52b6a0avboxsync VBGLR3SHAREDFOLDERMAPPING *ppaMappingsTemp = (PVBGLR3SHAREDFOLDERMAPPING)RTMemAllocZ(cbSize);
ba31bc205e96548d3557ae82087dc020a52b6a0avboxsync VbglHGCMParmUInt32Set(&Msg.numberOfMappings, cMappings);
ba31bc205e96548d3557ae82087dc020a52b6a0avboxsync VbglHGCMParmPtrSet(&Msg.mappings, ppaMappingsTemp, cbSize);
ba31bc205e96548d3557ae82087dc020a52b6a0avboxsync rc = vbglR3DoIOCtl(VBOXGUEST_IOCTL_HGCM_CALL(sizeof(Msg)), &Msg, sizeof(Msg));
ba31bc205e96548d3557ae82087dc020a52b6a0avboxsync VbglHGCMParmUInt32Get(&Msg.numberOfMappings, pcMappings);
ba31bc205e96548d3557ae82087dc020a52b6a0avboxsync /* Do we have more mappings than we have allocated space for? */
ba31bc205e96548d3557ae82087dc020a52b6a0avboxsync cbSize = cMappings * sizeof(VBGLR3SHAREDFOLDERMAPPING);
ba31bc205e96548d3557ae82087dc020a52b6a0avboxsync void *pvNew = RTMemRealloc(ppaMappingsTemp, cbSize);
ba31bc205e96548d3557ae82087dc020a52b6a0avboxsync ppaMappingsTemp = (PVBGLR3SHAREDFOLDERMAPPING)pvNew;
ba31bc205e96548d3557ae82087dc020a52b6a0avboxsync * Frees the shared folder mappings allocated by
ba31bc205e96548d3557ae82087dc020a52b6a0avboxsync * VbglR3SharedFolderGetMappings() before.
ba31bc205e96548d3557ae82087dc020a52b6a0avboxsync * @param paMappings What
ba31bc205e96548d3557ae82087dc020a52b6a0avboxsyncVBGLR3DECL(void) VbglR3SharedFolderFreeMappings(PVBGLR3SHAREDFOLDERMAPPING paMappings)
5923a1e1a9eff5786a4dc5a281e11b7dc4fd0811vboxsync * Get the real name of a shared folder.
5923a1e1a9eff5786a4dc5a281e11b7dc4fd0811vboxsync * @returns VBox status code.
5923a1e1a9eff5786a4dc5a281e11b7dc4fd0811vboxsync * @param u32ClientId The client id returned by VbglR3InvsSvcConnect().
5923a1e1a9eff5786a4dc5a281e11b7dc4fd0811vboxsync * @param u32Root Root ID of shared folder to get the name for.
dd90fee6b212c87998fe80f39a1645e4bbe1acb9vboxsync * @param ppszName Where to return the name string. This shall be
dd90fee6b212c87998fe80f39a1645e4bbe1acb9vboxsync * freed by calling RTStrFree.
dd90fee6b212c87998fe80f39a1645e4bbe1acb9vboxsyncVBGLR3DECL(int) VbglR3SharedFolderGetName(uint32_t u32ClientId, uint32_t u32Root, char **ppszName)
dd90fee6b212c87998fe80f39a1645e4bbe1acb9vboxsync uint32_t cbString = sizeof(SHFLSTRING) + SHFL_MAX_LEN;
5923a1e1a9eff5786a4dc5a281e11b7dc4fd0811vboxsync PSHFLSTRING pString = (PSHFLSTRING)RTMemAlloc(cbString);
5923a1e1a9eff5786a4dc5a281e11b7dc4fd0811vboxsync rc = vbglR3DoIOCtl(VBOXGUEST_IOCTL_HGCM_CALL(sizeof(Msg)), &Msg, sizeof(Msg));
ba31bc205e96548d3557ae82087dc020a52b6a0avboxsync rc = RTUtf16ToUtf8(&pString->String.ucs2[0], ppszName);
7753dc7a3bcd14c9ec9d969cbc1a0155b0673c06vboxsync * Retrieves the prefix for a shared folder mount point. If no prefix
7753dc7a3bcd14c9ec9d969cbc1a0155b0673c06vboxsync * is set in the guest properties "sf_" is returned.
7753dc7a3bcd14c9ec9d969cbc1a0155b0673c06vboxsync * @returns VBox status code.
7753dc7a3bcd14c9ec9d969cbc1a0155b0673c06vboxsync * @param ppszPrefix Where to return the prefix string. This shall be
7753dc7a3bcd14c9ec9d969cbc1a0155b0673c06vboxsync * freed by calling RTStrFree.
7753dc7a3bcd14c9ec9d969cbc1a0155b0673c06vboxsyncVBGLR3DECL(int) VbglR3SharedFolderGetMountPrefix(char **ppszPrefix)
f450e6365ee42333841dd494583809428b504e37vboxsync rc = VbglR3GuestPropConnect(&u32ClientIdGuestProp);
7753dc7a3bcd14c9ec9d969cbc1a0155b0673c06vboxsync rc = VbglR3GuestPropReadValueAlloc(u32ClientIdGuestProp, "/VirtualBox/GuestAdd/SharedFolders/MountPrefix", ppszPrefix);
7753dc7a3bcd14c9ec9d969cbc1a0155b0673c06vboxsync if (rc == VERR_NOT_FOUND) /* No prefix set? Then set the default. */
04b02ffb8824a60fd37777bc1f7d2f35104a274cvboxsync * Retrieves the mount root directory for auto-mounted shared
04b02ffb8824a60fd37777bc1f7d2f35104a274cvboxsync * folders. mount point. If no string is set (VERR_NOT_FOUND)
04b02ffb8824a60fd37777bc1f7d2f35104a274cvboxsync * it's up on the caller (guest) to decide where to mount.
4bb5cfa1f4d9e95d7d34b5d6ede18d9f4d433bc6vboxsync * @returns VBox status code.
04b02ffb8824a60fd37777bc1f7d2f35104a274cvboxsync * @param ppszDir Where to return the directory
4bb5cfa1f4d9e95d7d34b5d6ede18d9f4d433bc6vboxsync * string. This shall be freed by
4bb5cfa1f4d9e95d7d34b5d6ede18d9f4d433bc6vboxsync * calling RTStrFree.
4bb5cfa1f4d9e95d7d34b5d6ede18d9f4d433bc6vboxsyncVBGLR3DECL(int) VbglR3SharedFolderGetMountDir(char **ppszDir)
4bb5cfa1f4d9e95d7d34b5d6ede18d9f4d433bc6vboxsync rc = VbglR3GuestPropConnect(&u32ClientIdGuestProp);