2 * Drive management code
4 * Copyright 2003 Mark Westcott
5 * Copyright 2003-2004 Mike Hearn
6 * Copyright 2004 Chris Morgan
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 #include <wine/debug.h>
43 WINE_DEFAULT_DEBUG_CHANNEL(winecfg);
45 struct drive drives[26]; /* one for each drive letter */
47 static inline int letter_to_index(char letter)
49 return (toupper(letter) - 'A');
52 /* This function produces a mask for each drive letter that isn't
53 * currently used. Each bit of the long result represents a letter,
54 * with A being the least significant bit, and Z being the most
57 * To calculate this, we loop over each letter, and see if we can get
58 * a drive entry for it. If so, we set the appropriate bit. At the
59 * end, we flip each bit, to give the desired result.
61 * The letter parameter is always marked as being available. This is
62 * so the edit dialog can display the currently used drive letter
63 * alongside the available ones.
65 long drive_available_mask(char letter)
73 for(i = 0; i < 26; i++)
75 if (!drives[i].in_use) continue;
76 result |= (1 << (toupper(drives[i].letter) - 'A'));
80 if (letter) result |= DRIVE_MASK_BIT(letter);
82 WINE_TRACE("finished drive letter loop with %lx\n", result);
86 BOOL add_drive(const char letter, const char *targetpath, const char *label, const char *serial, unsigned int type)
88 int driveIndex = letter_to_index(letter);
90 if(drives[driveIndex].in_use)
93 WINE_TRACE("letter == '%c', unixpath == '%s', label == '%s', serial == '%s', type == %d\n",
94 letter, targetpath, label, serial, type);
96 drives[driveIndex].letter = toupper(letter);
97 drives[driveIndex].unixpath = strdupA(targetpath);
98 drives[driveIndex].label = strdupA(label);
99 drives[driveIndex].serial = strdupA(serial);
100 drives[driveIndex].type = type;
101 drives[driveIndex].in_use = TRUE;
106 /* deallocates the contents of the drive. does not free the drive itself */
107 void delete_drive(struct drive *d)
109 HeapFree(GetProcessHeap(), 0, d->unixpath);
111 HeapFree(GetProcessHeap(), 0, d->label);
113 HeapFree(GetProcessHeap(), 0, d->serial);
119 static void set_drive_type( char letter, DWORD type )
123 const char *typeText = NULL;
125 sprintf(driveValue, "%c:", letter);
127 /* Set the drive type in the registry */
128 if (type == DRIVE_FIXED)
130 else if (type == DRIVE_REMOTE)
131 typeText = "network";
132 else if (type == DRIVE_REMOVABLE)
134 else if (type == DRIVE_CDROM)
137 if (RegCreateKey(HKEY_LOCAL_MACHINE, "Software\\Wine\\Drives", &hKey) != ERROR_SUCCESS)
138 WINE_TRACE(" Unable to open '%s'\n", "Software\\Wine\\Drives");
142 RegSetValueEx( hKey, driveValue, 0, REG_SZ, (LPBYTE)typeText, strlen(typeText) + 1 );
144 RegDeleteValue( hKey, driveValue );
149 static DWORD get_drive_type( char letter )
153 DWORD ret = DRIVE_UNKNOWN;
155 sprintf(driveValue, "%c:", letter);
157 if (RegOpenKey(HKEY_LOCAL_MACHINE, "Software\\Wine\\Drives", &hKey) != ERROR_SUCCESS)
158 WINE_TRACE(" Unable to open Software\\Wine\\Drives\n" );
162 DWORD size = sizeof(buffer);
164 if (!RegQueryValueExA( hKey, driveValue, NULL, NULL, (LPBYTE)buffer, &size ))
166 WINE_TRACE("Got type '%s' for %s\n", buffer, driveValue );
167 if (!strcasecmp( buffer, "hd" )) ret = DRIVE_FIXED;
168 else if (!strcasecmp( buffer, "network" )) ret = DRIVE_REMOTE;
169 else if (!strcasecmp( buffer, "floppy" )) ret = DRIVE_REMOVABLE;
170 else if (!strcasecmp( buffer, "cdrom" )) ret = DRIVE_CDROM;
179 /* currently unused, but if users have this burning desire to be able to rename drives,
180 we can put it back in.
183 BOOL copyDrive(struct drive *pSrc, struct drive *pDst)
187 WINE_TRACE("pDst already in use\n");
191 if(!pSrc->unixpath) WINE_TRACE("!pSrc->unixpath\n");
192 if(!pSrc->label) WINE_TRACE("!pSrc->label\n");
193 if(!pSrc->serial) WINE_TRACE("!pSrc->serial\n");
195 pDst->unixpath = strdupA(pSrc->unixpath);
196 pDst->label = strdupA(pSrc->label);
197 pDst->serial = strdupA(pSrc->serial);
198 pDst->type = pSrc->type;
204 BOOL moveDrive(struct drive *pSrc, struct drive *pDst)
206 WINE_TRACE("pSrc->letter == %c, pDst->letter == %c\n", pSrc->letter, pDst->letter);
208 if(!copyDrive(pSrc, pDst))
210 WINE_TRACE("copyDrive failed\n");
220 /* Load currently defined drives into the drives array */
225 int drivecount = 0, i;
227 static const int arraysize = 512;
231 /* FIXME: broken symlinks in $WINEPREFIX/dosdevices will not be
232 returned by this API, so we need to handle that */
234 /* setup the drives array */
235 dev = devices = HeapAlloc(GetProcessHeap(), 0, arraysize);
236 len = GetLogicalDriveStrings(arraysize, devices);
238 /* make all devices unused */
239 for (i = 0; i < 26; i++)
241 drives[i].letter = 'A' + i;
242 drives[i].in_use = FALSE;
244 HeapFree(GetProcessHeap(), 0, drives[i].unixpath);
245 drives[i].unixpath = NULL;
247 HeapFree(GetProcessHeap(), 0, drives[i].label);
248 drives[i].label = NULL;
250 HeapFree(GetProcessHeap(), 0, drives[i].serial);
251 drives[i].serial = NULL;
254 /* work backwards through the result of GetLogicalDriveStrings */
257 char volname[512]; /* volume name */
263 char targetpath[256];
266 *devices = toupper(*devices);
268 WINE_TRACE("devices == '%s'\n", devices);
272 retval = GetVolumeInformation(devices,
282 WINE_ERR("GetVolumeInformation() for '%s' failed, setting serial to 0\n", devices);
287 WINE_TRACE("serial: '0x%lX'\n", serial);
289 /* build rootpath for GetDriveType() */
290 lstrcpynA(rootpath, devices, sizeof(rootpath));
291 pathlen = strlen(rootpath);
293 /* ensure that we have a backslash on the root path */
294 if ((rootpath[pathlen - 1] != '\\') && (pathlen < sizeof(rootpath)))
296 rootpath[pathlen] = '\\';
297 rootpath[pathlen + 1] = 0;
300 /* QueryDosDevice() requires no trailing backslash */
301 lstrcpynA(simplepath, devices, 3);
302 QueryDosDevice(simplepath, targetpath, sizeof(targetpath));
304 /* targetpath may have forward slashes rather than backslashes, so correct */
306 do if (*c == '\\') *c = '/'; while (*c++);
308 snprintf(serialstr, sizeof(serialstr), "%lX", serial);
309 WINE_TRACE("serialstr: '%s'\n", serialstr);
310 add_drive(*devices, targetpath, volname, serialstr, get_drive_type(devices[0]) );
312 len -= strlen(devices);
313 devices += strlen(devices);
315 /* skip over any nulls */
316 while ((*devices == 0) && (len))
325 WINE_TRACE("found %d drives\n", drivecount);
327 HeapFree(GetProcessHeap(), 0, dev);
330 /* some of this code appears to be broken by bugs in Wine: the label
331 * setting code has no effect, for instance */
332 void apply_drive_changes(void)
336 CHAR targetpath[256];
338 CHAR volumeNameBuffer[512];
340 DWORD maxComponentLength;
341 DWORD fileSystemFlags;
342 CHAR fileSystemName[128];
348 /* add each drive and remove as we go */
349 for(i = 0; i < 26; i++)
351 defineDevice = FALSE;
353 snprintf(devicename, sizeof(devicename), "%c:", 'A' + i);
356 if(QueryDosDevice(devicename, targetpath, sizeof(targetpath)))
360 /* correct the slashes in the path to be UNIX style */
361 while ((cursor = strchr(targetpath, '\\'))) *cursor = '/';
366 /* if we found a drive and have a drive then compare things */
367 if(foundDrive && drives[i].in_use)
369 char newSerialNumberText[256];
371 volumeNameBuffer[0] = 0;
373 WINE_TRACE("drives[i].letter: '%c'\n", drives[i].letter);
375 snprintf(devicename, sizeof(devicename), "%c:\\", 'A' + i);
376 retval = GetVolumeInformation(devicename,
378 sizeof(volumeNameBuffer),
383 sizeof(fileSystemName));
386 WINE_TRACE(" GetVolumeInformation() for '%s' failed\n", devicename);
387 WINE_TRACE(" Skipping this drive\n");
389 continue; /* skip this drive */
392 snprintf(newSerialNumberText, sizeof(newSerialNumberText), "%lX", serialNumber);
394 WINE_TRACE(" current path: '%s', new path: '%s'\n",
395 targetpath, drives[i].unixpath);
396 WINE_TRACE(" current label: '%s', new label: '%s'\n",
397 volumeNameBuffer, drives[i].label);
398 WINE_TRACE(" current serial: '%s', new serial: '%s'\n",
399 newSerialNumberText, drives[i].serial);
401 /* compare to what we have */
402 /* do we have the same targetpath? */
403 if(strcmp(drives[i].unixpath, targetpath) ||
404 strcmp(drives[i].label, volumeNameBuffer) ||
405 strcmp(drives[i].serial, newSerialNumberText))
408 WINE_TRACE(" making changes to drive '%s'\n", devicename);
412 WINE_TRACE(" no changes to drive '%s'\n", devicename);
415 else if(foundDrive && !drives[i].in_use)
417 /* remove this drive */
418 if(!DefineDosDevice(DDD_REMOVE_DEFINITION, devicename, drives[i].unixpath))
420 WINE_ERR("unable to remove devicename of '%s', targetpath of '%s'\n",
421 devicename, drives[i].unixpath);
426 WINE_TRACE("removed devicename of '%s', targetpath of '%s'\n",
427 devicename, drives[i].unixpath);
430 set_drive_type( drives[i].letter, DRIVE_UNKNOWN );
433 else if(drives[i].in_use) /* foundDrive must be false from the above check */
438 /* adding and modifying are the same steps */
444 /* define this drive */
445 /* DefineDosDevice() requires that NO trailing slash be present */
446 snprintf(devicename, sizeof(devicename), "%c:", 'A' + i);
447 if(!DefineDosDevice(DDD_RAW_TARGET_PATH, devicename, drives[i].unixpath))
449 WINE_ERR(" unable to define devicename of '%s', targetpath of '%s'\n",
450 devicename, drives[i].unixpath);
455 WINE_TRACE(" added devicename of '%s', targetpath of '%s'\n",
456 devicename, drives[i].unixpath);
458 /* SetVolumeLabel() requires a trailing slash */
459 snprintf(devicename, sizeof(devicename), "%c:\\", 'A' + i);
460 if(!SetVolumeLabel(devicename, drives[i].label))
462 WINE_WARN("unable to set volume label for devicename of '%s', label of '%s'\n",
463 devicename, drives[i].label);
468 WINE_TRACE(" set volume label for devicename of '%s', label of '%s'\n",
469 devicename, drives[i].label);
474 /* Set the drive serial number via a .windows-serial file in */
475 /* the targetpath directory */
476 snprintf(filename, sizeof(filename), "%c:\\.windows-serial", drives[i].letter);
477 WINE_TRACE(" Putting serial number of '%ld' into file '%s'\n",
478 serialNumber, filename);
479 hFile = CreateFile(filename,
484 FILE_ATTRIBUTE_NORMAL,
486 if (hFile != INVALID_HANDLE_VALUE)
489 WINE_TRACE(" writing serial number of '%s'\n", drives[i].serial);
492 strlen(drives[i].serial),
504 WINE_TRACE(" CreateFile() error with file '%s'\n", filename);
508 set_drive_type( drives[i].letter, drives[i].type );