VirtualBox

source: vbox/trunk/src/VBox/Additions/WINNT/VBoxTray/VBoxSharedFolders.cpp@ 34575

Last change on this file since 34575 was 33966, checked in by vboxsync, 14 years ago

VBoxTray: Cleaning up, refactoring, grouping window messages.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.3 KB
Line 
1/* $Id: VBoxSharedFolders.cpp 33966 2010-11-11 10:32:07Z 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 "VBoxHelpers.h"
21
22#include <iprt/mem.h>
23#include <VBox/VBoxGuestLib.h>
24
25int 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;
34 VBGLR3SHAREDFOLDERMAPPING *paMappings;
35
36 rc = VbglR3SharedFolderGetMappings(u32ClientId, true /* Only process auto-mounted folders */,
37 &paMappings, &cMappings);
38 if (RT_SUCCESS(rc))
39 {
40#if 0
41 /* Check for a fixed/virtual auto-mount share. */
42 if (VbglR3SharedFolderExists(u32ClientId, "vbsfAutoMount"))
43 {
44 Log(("VBoxTray: Hosts supports auto-mount root\n"));
45 }
46 else
47 {
48#endif
49 Log(("VBoxTray: Got %u shared folder mappings\n", cMappings));
50 for (uint32_t i = 0; i < cMappings && RT_SUCCESS(rc); i++)
51 {
52 char *pszName = NULL;
53 rc = VbglR3SharedFolderGetName(u32ClientId, paMappings[i].u32Root, &pszName);
54 if ( RT_SUCCESS(rc)
55 && *pszName)
56 {
57 Log(("VBoxTray: Connecting share %u (%s) ...\n", i+1, pszName));
58
59 char *pszShareName;
60 if (RTStrAPrintf(&pszShareName, "\\\\vboxsrv\\%s", pszName) >= 0)
61 {
62 char chDrive = 'D'; /* Start probing whether drive D: is free to use. */
63 do
64 {
65 char szCurDrive[3];
66 RTStrPrintf(szCurDrive, sizeof(szCurDrive), "%c:", chDrive++);
67
68 NETRESOURCE resource;
69 RT_ZERO(resource);
70 resource.dwType = RESOURCETYPE_ANY;
71 resource.lpLocalName = TEXT(szCurDrive);
72 resource.lpRemoteName = TEXT(pszShareName);
73 /* Go straight to our network provider in order to get maximum lookup speed. */
74 resource.lpProvider = TEXT("VirtualBox Shared Folders");
75
76 /** @todo Figure out how to map the drives in a block (F,G,H, ...).
77 Save the mapping for later use. */
78 DWORD dwErr = WNetAddConnection2A(&resource, NULL, NULL, 0);
79 if (dwErr == NO_ERROR)
80 {
81 LogRel(("VBoxTray: Shared folder \"%s\" was mounted to drive \"%s\"\n", pszName, szCurDrive));
82 break;
83 }
84 else
85 {
86 LogRel(("VBoxTray: Mounting \"%s\" to \"%s\" resulted in dwErr = %ld\n", pszName, szCurDrive, dwErr));
87
88 switch (dwErr)
89 {
90 /*
91 * The local device specified by the lpLocalName member is already
92 * connected to a network resource. Try next drive ...
93 */
94 case ERROR_ALREADY_ASSIGNED:
95 break;
96
97 default:
98 LogRel(("VBoxTray: Error while mounting shared folder \"%s\" to \"%s\", error = %ld\n",
99 pszName, szCurDrive, dwErr));
100 break;
101 }
102 }
103 } while (chDrive <= 'Z');
104
105 if (chDrive > 'Z')
106 {
107 LogRel(("VBoxTray: No free driver letter found to assign shared folder \"%s\", aborting\n", pszName));
108 break;
109 }
110
111 RTStrFree(pszShareName);
112 }
113 else
114 rc = VERR_NO_STR_MEMORY;
115 RTStrFree(pszName);
116 }
117 else
118 Log(("VBoxTray: Error while getting the shared folder name for root node = %u, rc = %Rrc\n",
119 paMappings[i].u32Root, rc));
120 }
121#if 0
122 }
123#endif
124 RTMemFree(paMappings);
125 }
126 else
127 Log(("VBoxTray: Error while getting the shared folder mappings, rc = %Rrc\n", rc));
128 VbglR3SharedFolderDisconnect(u32ClientId);
129 }
130 return rc;
131}
132
133int VBoxSharedFoldersAutoUnmount(void)
134{
135 uint32_t u32ClientId;
136 int rc = VbglR3SharedFolderConnect(&u32ClientId);
137 if (!RT_SUCCESS(rc))
138 Log(("VBoxTray: Failed to connect to the shared folder service, error %Rrc\n", rc));
139 else
140 {
141 uint32_t cMappings;
142 VBGLR3SHAREDFOLDERMAPPING *paMappings;
143
144 rc = VbglR3SharedFolderGetMappings(u32ClientId, true /* Only process auto-mounted folders */,
145 &paMappings, &cMappings);
146 if (RT_SUCCESS(rc))
147 {
148 for (uint32_t i = 0; i < cMappings && RT_SUCCESS(rc); i++)
149 {
150 char *pszName = NULL;
151 rc = VbglR3SharedFolderGetName(u32ClientId, paMappings[i].u32Root, &pszName);
152 if ( RT_SUCCESS(rc)
153 && *pszName)
154 {
155 Log(("VBoxTray: Disconnecting share %u (%s) ...\n", i+1, pszName));
156
157 char *pszShareName;
158 if (RTStrAPrintf(&pszShareName, "\\\\vboxsrv\\%s", pszName) >= 0)
159 {
160 DWORD dwErr = WNetCancelConnection2(pszShareName, 0, FALSE /* Force disconnect */);
161 if (dwErr == NO_ERROR)
162 {
163 LogRel(("VBoxTray: Share \"%s\" was disconnected\n", pszShareName));
164 RTStrFree(pszShareName);
165 RTStrFree(pszName);
166 break;
167 }
168
169 LogRel(("VBoxTray: Disconnecting \"%s\" failed, dwErr = %ld\n", pszShareName, dwErr));
170
171 switch (dwErr)
172 {
173 case ERROR_NOT_CONNECTED:
174 break;
175
176 default:
177 LogRel(("VBoxTray: Error while disconnecting shared folder \"%s\", error = %ld\n",
178 pszShareName, dwErr));
179 break;
180 }
181
182 RTStrFree(pszShareName);
183 }
184 else
185 rc = VERR_NO_MEMORY;
186 RTStrFree(pszName);
187 }
188 else
189 Log(("VBoxTray: Error while getting the shared folder name for root node = %u, rc = %Rrc\n",
190 paMappings[i].u32Root, rc));
191 }
192 RTMemFree(paMappings);
193 }
194 else
195 Log(("VBoxTray: Error while getting the shared folder mappings, rc = %Rrc\n", rc));
196 VbglR3SharedFolderDisconnect(u32ClientId);
197 }
198 return rc;
199}
200
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette