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
33 #include <wine/debug.h>
44 WINE_DEFAULT_DEBUG_CHANNEL(winecfg);
46 struct drive drives[26]; /* one for each drive letter */
48 static inline int letter_to_index(char letter)
50 return (toupper(letter) - 'A');
53 /* This function produces a mask for each drive letter that isn't
54 * currently used. Each bit of the long result represents a letter,
55 * with A being the least significant bit, and Z being the most
58 * To calculate this, we loop over each letter, and see if we can get
59 * a drive entry for it. If so, we set the appropriate bit. At the
60 * end, we flip each bit, to give the desired result.
62 * The letter parameter is always marked as being available. This is
63 * so the edit dialog can display the currently used drive letter
64 * alongside the available ones.
66 long drive_available_mask(char letter)
74 for(i = 0; i < 26; i++)
76 if (!drives[i].in_use) continue;
77 result |= (1 << (toupper(drives[i].letter) - 'A'));
81 if (letter) result |= DRIVE_MASK_BIT(letter);
83 WINE_TRACE("finished drive letter loop with %lx\n", result);
87 BOOL add_drive(char letter, char *targetpath, char *label, char *serial, uint type)
89 int driveIndex = letter_to_index(letter);
91 if(drives[driveIndex].in_use)
94 WINE_TRACE("letter == '%c', unixpath == '%s', label == '%s', serial == '%s', type == %d\n",
95 letter, targetpath, label, serial, type);
97 drives[driveIndex].letter = toupper(letter);
98 drives[driveIndex].unixpath = strdupA(targetpath);
99 drives[driveIndex].label = strdupA(label);
100 drives[driveIndex].serial = strdupA(serial);
101 drives[driveIndex].type = type;
102 drives[driveIndex].in_use = TRUE;
107 /* deallocates the contents of the drive. does not free the drive itself */
108 void delete_drive(struct drive *d)
110 HeapFree(GetProcessHeap(), 0, d->unixpath);
112 HeapFree(GetProcessHeap(), 0, d->label);
114 HeapFree(GetProcessHeap(), 0, d->serial);
122 /* currently unused, but if users have this burning desire to be able to rename drives,
123 we can put it back in.
126 BOOL copyDrive(struct drive *pSrc, struct drive *pDst)
130 WINE_TRACE("pDst already in use\n");
134 if(!pSrc->unixpath) WINE_TRACE("!pSrc->unixpath\n");
135 if(!pSrc->label) WINE_TRACE("!pSrc->label\n");
136 if(!pSrc->serial) WINE_TRACE("!pSrc->serial\n");
138 pDst->unixpath = strdupA(pSrc->unixpath);
139 pDst->label = strdupA(pSrc->label);
140 pDst->serial = strdupA(pSrc->serial);
141 pDst->type = pSrc->type;
147 BOOL moveDrive(struct drive *pSrc, struct drive *pDst)
149 WINE_TRACE("pSrc->letter == %c, pDst->letter == %c\n", pSrc->letter, pDst->letter);
151 if(!copyDrive(pSrc, pDst))
153 WINE_TRACE("copyDrive failed\n");
163 /* Load currently defined drives into the drives array */
168 int drivecount = 0, i;
170 static const int arraysize = 512;
174 /* FIXME: broken symlinks in $WINEPREFIX/dosdevices will not be
175 returned by this API, so we need to handle that */
177 /* setup the drives array */
178 dev = devices = HeapAlloc(GetProcessHeap(), 0, arraysize);
179 len = GetLogicalDriveStrings(arraysize, devices);
181 /* make all devices unused */
182 for (i = 0; i < 26; i++)
184 drives[i].letter = 'A' + i;
185 drives[i].in_use = FALSE;
187 HeapFree(GetProcessHeap(), 0, drives[i].unixpath);
188 drives[i].unixpath = NULL;
190 HeapFree(GetProcessHeap(), 0, drives[i].label);
191 drives[i].label = NULL;
193 HeapFree(GetProcessHeap(), 0, drives[i].serial);
194 drives[i].serial = NULL;
197 /* work backwards through the result of GetLogicalDriveStrings */
200 char volname[512]; /* volume name */
206 char targetpath[256];
209 *devices = toupper(*devices);
211 WINE_TRACE("devices == '%s'\n", devices);
215 retval = GetVolumeInformation(devices,
225 WINE_ERR("GetVolumeInformation() for '%s' failed, setting serial to 0\n", devices);
230 WINE_TRACE("serial: '0x%lX'\n", serial);
232 /* build rootpath for GetDriveType() */
233 strncpy(rootpath, devices, sizeof(rootpath));
234 pathlen = strlen(rootpath);
236 /* ensure that we have a backslash on the root path */
237 if ((rootpath[pathlen - 1] != '\\') && (pathlen < sizeof(rootpath)))
239 rootpath[pathlen] = '\\';
240 rootpath[pathlen + 1] = 0;
243 strncpy(simplepath, devices, 2); /* QueryDosDevice() requires no trailing backslash */
245 QueryDosDevice(simplepath, targetpath, sizeof(targetpath));
247 /* targetpath may have forward slashes rather than backslashes, so correct */
249 do if (*c == '\\') *c = '/'; while (*c++);
251 snprintf(serialstr, sizeof(serialstr), "%lX", serial);
252 WINE_TRACE("serialstr: '%s'\n", serialstr);
253 add_drive(*devices, targetpath, volname, serialstr, GetDriveType(rootpath));
255 len -= strlen(devices);
256 devices += strlen(devices);
258 /* skip over any nulls */
259 while ((*devices == 0) && (len))
268 WINE_TRACE("found %d drives\n", drivecount);
270 HeapFree(GetProcessHeap(), 0, dev);
273 /* some of this code appears to be broken by bugs in Wine: the label
274 * setting code has no effect, for instance */
275 void apply_drive_changes()
279 CHAR targetpath[256];
281 CHAR volumeNameBuffer[512];
283 DWORD maxComponentLength;
284 DWORD fileSystemFlags;
285 CHAR fileSystemName[128];
291 /* add each drive and remove as we go */
292 for(i = 0; i < 26; i++)
294 defineDevice = FALSE;
296 snprintf(devicename, sizeof(devicename), "%c:", 'A' + i);
299 if(QueryDosDevice(devicename, targetpath, sizeof(targetpath)))
303 /* correct the slashes in the path to be UNIX style */
304 while ((cursor = strchr(targetpath, '\\'))) *cursor = '/';
309 /* if we found a drive and have a drive then compare things */
310 if(foundDrive && drives[i].in_use)
312 char newSerialNumberText[256];
314 volumeNameBuffer[0] = 0;
316 WINE_TRACE("drives[i].letter: '%c'\n", drives[i].letter);
318 snprintf(devicename, sizeof(devicename), "%c:\\", 'A' + i);
319 retval = GetVolumeInformation(devicename,
321 sizeof(volumeNameBuffer),
326 sizeof(fileSystemName));
329 WINE_TRACE(" GetVolumeInformation() for '%s' failed\n", devicename);
330 WINE_TRACE(" Skipping this drive\n");
332 continue; /* skip this drive */
335 snprintf(newSerialNumberText, sizeof(newSerialNumberText), "%lX", serialNumber);
337 WINE_TRACE(" current path: '%s', new path: '%s'\n",
338 targetpath, drives[i].unixpath);
339 WINE_TRACE(" current label: '%s', new label: '%s'\n",
340 volumeNameBuffer, drives[i].label);
341 WINE_TRACE(" current serial: '%s', new serial: '%s'\n",
342 newSerialNumberText, drives[i].serial);
344 /* compare to what we have */
345 /* do we have the same targetpath? */
346 if(strcmp(drives[i].unixpath, targetpath) ||
347 strcmp(drives[i].label, volumeNameBuffer) ||
348 strcmp(drives[i].serial, newSerialNumberText))
351 WINE_TRACE(" making changes to drive '%s'\n", devicename);
355 WINE_TRACE(" no changes to drive '%s'\n", devicename);
358 else if(foundDrive && !drives[i].in_use)
360 /* remove this drive */
361 if(!DefineDosDevice(DDD_REMOVE_DEFINITION, devicename, drives[i].unixpath))
363 WINE_ERR("unable to remove devicename of '%s', targetpath of '%s'\n",
364 devicename, drives[i].unixpath);
369 WINE_TRACE("removed devicename of '%s', targetpath of '%s'\n",
370 devicename, drives[i].unixpath);
373 else if(drives[i].in_use) /* foundDrive must be false from the above check */
378 /* adding and modifying are the same steps */
386 char driveValue[256];
388 /* define this drive */
389 /* DefineDosDevice() requires that NO trailing slash be present */
390 snprintf(devicename, sizeof(devicename), "%c:", 'A' + i);
391 if(!DefineDosDevice(DDD_RAW_TARGET_PATH, devicename, drives[i].unixpath))
393 WINE_ERR(" unable to define devicename of '%s', targetpath of '%s'\n",
394 devicename, drives[i].unixpath);
399 WINE_TRACE(" added devicename of '%s', targetpath of '%s'\n",
400 devicename, drives[i].unixpath);
402 /* SetVolumeLabel() requires a trailing slash */
403 snprintf(devicename, sizeof(devicename), "%c:\\", 'A' + i);
404 if(!SetVolumeLabel(devicename, drives[i].label))
406 WINE_WARN("unable to set volume label for devicename of '%s', label of '%s'\n",
407 devicename, drives[i].label);
412 WINE_TRACE(" set volume label for devicename of '%s', label of '%s'\n",
413 devicename, drives[i].label);
417 /* Set the drive type in the registry */
418 if(drives[i].type == DRIVE_FIXED)
420 else if(drives[i].type == DRIVE_REMOTE)
421 typeText = "network";
422 else if(drives[i].type == DRIVE_REMOVABLE)
424 else /* must be DRIVE_CDROM */
428 snprintf(driveValue, sizeof(driveValue), "%c:", toupper(drives[i].letter));
430 retval = RegOpenKey(HKEY_LOCAL_MACHINE,
431 "Software\\Wine\\Drives",
434 if(retval != ERROR_SUCCESS)
436 WINE_TRACE(" Unable to open '%s'\n", "Software\\Wine\\Drives");
440 retval = RegSetValueEx(
446 strlen(typeText) + 1);
447 if(retval != ERROR_SUCCESS)
449 WINE_TRACE(" Unable to set value of '%s' to '%s'\n",
450 driveValue, typeText);
454 WINE_TRACE(" Finished setting value of '%s' to '%s'\n",
455 driveValue, typeText);
460 /* Set the drive serial number via a .windows-serial file in */
461 /* the targetpath directory */
462 snprintf(filename, sizeof(filename), "%c:\\.windows-serial", drives[i].letter);
463 WINE_TRACE(" Putting serial number of '%ld' into file '%s'\n",
464 serialNumber, filename);
465 hFile = CreateFile(filename,
470 FILE_ATTRIBUTE_NORMAL,
472 if (hFile != INVALID_HANDLE_VALUE)
475 WINE_TRACE(" writing serial number of '%s'\n", drives[i].serial);
478 strlen(drives[i].serial),
490 WINE_TRACE(" CreateFile() error with file '%s'\n", filename);