1 | /* $Id: VBoxSharedFolders.cpp 31027 2010-07-23 00:13:31Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxSharedFolders - Handling for shared folders
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2010 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 | #include "VBoxSharedFolders.h"
|
---|
19 | #include "VBoxTray.h"
|
---|
20 | #include "helpers.h"
|
---|
21 |
|
---|
22 | #include <iprt/mem.h>
|
---|
23 | #include <VBox/VBoxGuestLib.h>
|
---|
24 |
|
---|
25 | int VBoxSharedFoldersAutoMount(void)
|
---|
26 | {
|
---|
27 | uint32_t u32ClientId;
|
---|
28 | int rc = VbglR3SharedFolderConnect(&u32ClientId);
|
---|
29 | if (!RT_SUCCESS(rc))
|
---|
30 | Log(("VBoxTray: Failed to connect to the shared folder service, error %Rrc\n", rc));
|
---|
31 | else
|
---|
32 | {
|
---|
33 | uint32_t cMappings = 64; /* See shflsvc.h for define; should be used later. */
|
---|
34 | uint32_t cbMappings = cMappings * sizeof(VBGLR3SHAREDFOLDERMAPPING);
|
---|
35 | VBGLR3SHAREDFOLDERMAPPING *paMappings = (PVBGLR3SHAREDFOLDERMAPPING)RTMemAlloc(cbMappings);
|
---|
36 |
|
---|
37 | if (paMappings)
|
---|
38 | {
|
---|
39 | rc = VbglR3SharedFolderGetMappings(u32ClientId, true /* Only process auto-mounted folders */,
|
---|
40 | paMappings, cbMappings,
|
---|
41 | &cMappings);
|
---|
42 | if (RT_SUCCESS(rc))
|
---|
43 | {
|
---|
44 | RT_CLAMP(cMappings, 0, 64); /* Maximum mappings, see shflsvc.h */
|
---|
45 | for (uint32_t i = 0; i < cMappings; i++)
|
---|
46 | {
|
---|
47 | char *pszName = NULL;
|
---|
48 | rc = VbglR3SharedFolderGetName(u32ClientId, paMappings[i].u32Root, &pszName);
|
---|
49 | if ( RT_SUCCESS(rc)
|
---|
50 | && *pszName)
|
---|
51 | {
|
---|
52 | char *pszShareName = NULL;
|
---|
53 | RTStrAPrintf(&pszShareName, "\\\\vboxsrv\\%s", pszName);
|
---|
54 | if (pszShareName)
|
---|
55 | {
|
---|
56 | NETRESOURCE resource;
|
---|
57 | RT_ZERO(resource);
|
---|
58 | resource.dwType = RESOURCETYPE_ANY;
|
---|
59 | resource.lpLocalName = TEXT("f:");
|
---|
60 | resource.lpRemoteName = TEXT(pszShareName);
|
---|
61 |
|
---|
62 | /** @todo Figure out how to map the drives in a block (F,G,H, ...).
|
---|
63 | Save the mapping for later use. */
|
---|
64 | DWORD dwErr = WNetAddConnection2A(&resource, NULL, NULL, 0);
|
---|
65 | if (dwErr == NO_ERROR)
|
---|
66 | {
|
---|
67 | LogRel(("VBoxTray: Shared folder \"%s\" was mounted to share \"%s\"\n", pszName, dwErr));
|
---|
68 | }
|
---|
69 | else
|
---|
70 | {
|
---|
71 | switch (dwErr)
|
---|
72 | {
|
---|
73 | case ERROR_ALREADY_ASSIGNED:
|
---|
74 | break;
|
---|
75 |
|
---|
76 | default:
|
---|
77 | LogRel(("VBoxTray: Error while mounting shared folder \"%s\", error = %ld\n",
|
---|
78 | pszName, dwErr));
|
---|
79 | }
|
---|
80 | }
|
---|
81 | RTStrFree(pszShareName);
|
---|
82 | }
|
---|
83 | RTStrFree(pszName);
|
---|
84 | }
|
---|
85 | else
|
---|
86 | Log(("VBoxTray: Error while getting the shared folder name for root node = %u, rc = %Rrc\n",
|
---|
87 | paMappings[i].u32Root, rc));
|
---|
88 | }
|
---|
89 | }
|
---|
90 | else
|
---|
91 | Log(("VBoxTray: Error while getting the shared folder mappings, rc = %Rrc\n", rc));
|
---|
92 | RTMemFree(paMappings);
|
---|
93 | }
|
---|
94 | else
|
---|
95 | rc = VERR_NO_MEMORY;
|
---|
96 | VbglR3SharedFolderDisconnect(u32ClientId);
|
---|
97 | }
|
---|
98 | return rc;
|
---|
99 | }
|
---|
100 |
|
---|
101 | int VBoxSharedFoldersAutoUnmount(void)
|
---|
102 | {
|
---|
103 | //WNetCancelConnection2(name, 0, 1 /* Force disconnect */);
|
---|
104 | return 0;
|
---|
105 | }
|
---|