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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #include "wine/port.h"
35 #include <wine/debug.h>
41 #include <wine/library.h>
47 WINE_DEFAULT_DEBUG_CHANNEL(winecfg);
49 struct drive drives[26]; /* one for each drive letter */
51 static inline int letter_to_index(char letter)
53 return (toupper(letter) - 'A');
56 /* This function produces a mask for each drive letter that isn't
57 * currently used. Each bit of the long result represents a letter,
58 * with A being the least significant bit, and Z being the most
61 * To calculate this, we loop over each letter, and see if we can get
62 * a drive entry for it. If so, we set the appropriate bit. At the
63 * end, we flip each bit, to give the desired result.
65 * The letter parameter is always marked as being available. This is
66 * so the edit dialog can display the currently used drive letter
67 * alongside the available ones.
69 long drive_available_mask(char letter)
77 for(i = 0; i < 26; i++)
79 if (!drives[i].in_use) continue;
80 result |= (1 << (letter_to_index(drives[i].letter)));
84 if (letter) result |= DRIVE_MASK_BIT(letter);
86 WINE_TRACE("finished drive letter loop with %lx\n", result);
90 BOOL add_drive(const char letter, const char *targetpath, const char *label, const char *serial, unsigned int type)
92 int driveIndex = letter_to_index(letter);
94 if(drives[driveIndex].in_use)
97 WINE_TRACE("letter == '%c', unixpath == '%s', label == '%s', serial == '%s', type == %d\n",
98 letter, targetpath, label, serial, type);
100 drives[driveIndex].letter = toupper(letter);
101 drives[driveIndex].unixpath = strdupA(targetpath);
102 drives[driveIndex].label = strdupA(label);
103 drives[driveIndex].serial = strdupA(serial);
104 drives[driveIndex].type = type;
105 drives[driveIndex].in_use = TRUE;
110 /* deallocates the contents of the drive. does not free the drive itself */
111 void delete_drive(struct drive *d)
113 HeapFree(GetProcessHeap(), 0, d->unixpath);
115 HeapFree(GetProcessHeap(), 0, d->label);
117 HeapFree(GetProcessHeap(), 0, d->serial);
123 static void set_drive_type( char letter, DWORD type )
127 const char *typeText = NULL;
129 sprintf(driveValue, "%c:", letter);
131 /* Set the drive type in the registry */
132 if (type == DRIVE_FIXED)
134 else if (type == DRIVE_REMOTE)
135 typeText = "network";
136 else if (type == DRIVE_REMOVABLE)
138 else if (type == DRIVE_CDROM)
141 if (RegCreateKey(HKEY_LOCAL_MACHINE, "Software\\Wine\\Drives", &hKey) != ERROR_SUCCESS)
142 WINE_TRACE(" Unable to open '%s'\n", "Software\\Wine\\Drives");
146 RegSetValueEx( hKey, driveValue, 0, REG_SZ, (const BYTE *)typeText, strlen(typeText) + 1 );
148 RegDeleteValue( hKey, driveValue );
153 static DWORD get_drive_type( char letter )
157 DWORD ret = DRIVE_UNKNOWN;
159 sprintf(driveValue, "%c:", letter);
161 if (RegOpenKey(HKEY_LOCAL_MACHINE, "Software\\Wine\\Drives", &hKey) != ERROR_SUCCESS)
162 WINE_TRACE(" Unable to open Software\\Wine\\Drives\n" );
166 DWORD size = sizeof(buffer);
168 if (!RegQueryValueExA( hKey, driveValue, NULL, NULL, (LPBYTE)buffer, &size ))
170 WINE_TRACE("Got type '%s' for %s\n", buffer, driveValue );
171 if (!strcasecmp( buffer, "hd" )) ret = DRIVE_FIXED;
172 else if (!strcasecmp( buffer, "network" )) ret = DRIVE_REMOTE;
173 else if (!strcasecmp( buffer, "floppy" )) ret = DRIVE_REMOVABLE;
174 else if (!strcasecmp( buffer, "cdrom" )) ret = DRIVE_CDROM;
182 static void set_drive_label( char letter, const char *label )
184 char device[] = "a:\\"; /* SetVolumeLabel() requires a trailing slash */
187 if(!SetVolumeLabel(device, label))
189 WINE_WARN("unable to set volume label for devicename of '%s', label of '%s'\n",
195 WINE_TRACE(" set volume label for devicename of '%s', label of '%s'\n",
200 /* set the drive serial number via a .windows-serial file */
201 static void set_drive_serial( char letter, const char *serial )
203 char filename[] = "a:\\.windows-serial";
206 filename[0] = letter;
207 WINE_TRACE("Putting serial number of '%s' into file '%s'\n", serial, filename);
208 hFile = CreateFile(filename, GENERIC_WRITE, FILE_SHARE_READ, NULL,
209 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
210 if (hFile != INVALID_HANDLE_VALUE)
213 WriteFile(hFile, serial, strlen(serial), &w, NULL);
214 WriteFile(hFile, "\n", 1, &w, NULL);
221 /* currently unused, but if users have this burning desire to be able to rename drives,
222 we can put it back in.
225 BOOL copyDrive(struct drive *pSrc, struct drive *pDst)
229 WINE_TRACE("pDst already in use\n");
233 if(!pSrc->unixpath) WINE_TRACE("!pSrc->unixpath\n");
234 if(!pSrc->label) WINE_TRACE("!pSrc->label\n");
235 if(!pSrc->serial) WINE_TRACE("!pSrc->serial\n");
237 pDst->unixpath = strdupA(pSrc->unixpath);
238 pDst->label = strdupA(pSrc->label);
239 pDst->serial = strdupA(pSrc->serial);
240 pDst->type = pSrc->type;
246 BOOL moveDrive(struct drive *pSrc, struct drive *pDst)
248 WINE_TRACE("pSrc->letter == %c, pDst->letter == %c\n", pSrc->letter, pDst->letter);
250 if(!copyDrive(pSrc, pDst))
252 WINE_TRACE("copyDrive failed\n");
262 /* Load currently defined drives into the drives array */
263 void load_drives(void)
267 int drivecount = 0, i;
269 static const int arraysize = 512;
270 const char *config_dir = wine_get_config_dir();
275 /* setup the drives array */
276 dev = devices = HeapAlloc(GetProcessHeap(), 0, arraysize);
277 len = GetLogicalDriveStrings(arraysize, devices);
279 /* make all devices unused */
280 for (i = 0; i < 26; i++)
282 drives[i].letter = 'A' + i;
283 drives[i].in_use = FALSE;
285 HeapFree(GetProcessHeap(), 0, drives[i].unixpath);
286 drives[i].unixpath = NULL;
288 HeapFree(GetProcessHeap(), 0, drives[i].label);
289 drives[i].label = NULL;
291 HeapFree(GetProcessHeap(), 0, drives[i].serial);
292 drives[i].serial = NULL;
295 /* work backwards through the result of GetLogicalDriveStrings */
298 char volname[512]; /* volume name */
304 char targetpath[256];
307 *devices = toupper(*devices);
309 WINE_TRACE("devices == '%s'\n", devices);
313 retval = GetVolumeInformation(devices,
323 WINE_ERR("GetVolumeInformation() for '%s' failed, setting serial to 0\n", devices);
328 WINE_TRACE("serial: '0x%X'\n", serial);
330 /* build rootpath for GetDriveType() */
331 lstrcpynA(rootpath, devices, sizeof(rootpath));
332 pathlen = strlen(rootpath);
334 /* ensure that we have a backslash on the root path */
335 if ((rootpath[pathlen - 1] != '\\') && (pathlen < sizeof(rootpath)))
337 rootpath[pathlen] = '\\';
338 rootpath[pathlen + 1] = 0;
341 /* QueryDosDevice() requires no trailing backslash */
342 lstrcpynA(simplepath, devices, 3);
343 QueryDosDevice(simplepath, targetpath, sizeof(targetpath));
345 /* targetpath may have forward slashes rather than backslashes, so correct */
347 do if (*c == '\\') *c = '/'; while (*c++);
349 snprintf(serialstr, sizeof(serialstr), "%X", serial);
350 WINE_TRACE("serialstr: '%s'\n", serialstr);
351 add_drive(*devices, targetpath, volname, serialstr, get_drive_type(devices[0]) );
353 len -= strlen(devices);
354 devices += strlen(devices);
356 /* skip over any nulls */
357 while ((*devices == 0) && (len))
366 /* Find all the broken symlinks we might have and add them as well. */
368 len = strlen(config_dir) + sizeof("/dosdevices/a:");
369 if (!(path = HeapAlloc(GetProcessHeap(), 0, len)))
372 strcpy(path, config_dir);
373 strcat(path, "/dosdevices/a:");
375 for (i = 0; i < 26; i++)
381 if (drives[i].in_use) continue;
382 path[len - 3] = 'a' + i;
384 if (lstat(path, &st) == -1 || !S_ISLNK(st.st_mode)) continue;
385 if ((cnt = readlink(path, buff, sizeof(buff))) == -1) continue;
388 WINE_TRACE("found broken symlink %s -> %s\n", path, buff);
389 add_drive('A' + i, buff, "", "0", DRIVE_UNKNOWN);
394 WINE_TRACE("found %d drives\n", drivecount);
396 HeapFree(GetProcessHeap(), 0, path);
397 HeapFree(GetProcessHeap(), 0, dev);
400 /* some of this code appears to be broken by bugs in Wine: the label
401 * setting code has no effect, for instance */
402 void apply_drive_changes(void)
406 CHAR targetpath[256];
408 CHAR volumeNameBuffer[512];
410 DWORD maxComponentLength;
411 DWORD fileSystemFlags;
412 CHAR fileSystemName[128];
413 char newSerialNumberText[32];
419 /* add each drive and remove as we go */
420 for(i = 0; i < 26; i++)
422 defineDevice = FALSE;
424 volumeNameBuffer[0] = 0;
426 snprintf(devicename, sizeof(devicename), "%c:", 'A' + i);
429 if(QueryDosDevice(devicename, targetpath, sizeof(targetpath)))
433 /* correct the slashes in the path to be UNIX style */
434 while ((cursor = strchr(targetpath, '\\'))) *cursor = '/';
439 /* if we found a drive and have a drive then compare things */
440 if(foundDrive && drives[i].in_use)
442 WINE_TRACE("drives[i].letter: '%c'\n", drives[i].letter);
444 snprintf(devicename, sizeof(devicename), "%c:\\", 'A' + i);
445 retval = GetVolumeInformation(devicename,
447 sizeof(volumeNameBuffer),
452 sizeof(fileSystemName));
455 WINE_TRACE(" GetVolumeInformation() for '%s' failed\n", devicename);
457 volumeNameBuffer[0] = '\0';
460 WINE_TRACE(" current path: '%s', new path: '%s'\n",
461 targetpath, drives[i].unixpath);
463 /* compare to what we have */
464 /* do we have the same targetpath? */
465 if(strcmp(drives[i].unixpath, targetpath))
468 WINE_TRACE(" making changes to drive '%s'\n", devicename);
472 WINE_TRACE(" no changes to drive '%s'\n", devicename);
475 else if(foundDrive && !drives[i].in_use)
477 /* remove this drive */
478 if(!DefineDosDevice(DDD_REMOVE_DEFINITION, devicename, drives[i].unixpath))
480 WINE_ERR("unable to remove devicename of '%s', targetpath of '%s'\n",
481 devicename, drives[i].unixpath);
486 WINE_TRACE("removed devicename of '%s', targetpath of '%s'\n",
487 devicename, drives[i].unixpath);
490 set_drive_type( drives[i].letter, DRIVE_UNKNOWN );
493 else if(drives[i].in_use) /* foundDrive must be false from the above check */
498 /* adding and modifying are the same steps */
501 /* define this drive */
502 /* DefineDosDevice() requires that NO trailing slash be present */
503 snprintf(devicename, sizeof(devicename), "%c:", 'A' + i);
504 if(!DefineDosDevice(DDD_RAW_TARGET_PATH, devicename, drives[i].unixpath))
506 WINE_ERR(" unable to define devicename of '%s', targetpath of '%s'\n",
507 devicename, drives[i].unixpath);
512 WINE_TRACE(" added devicename of '%s', targetpath of '%s'\n",
513 devicename, drives[i].unixpath);
517 if (drives[i].label && strcmp(drives[i].label, volumeNameBuffer))
518 set_drive_label( drives[i].letter, drives[i].label );
520 snprintf(newSerialNumberText, sizeof(newSerialNumberText), "%X", serialNumber);
521 if (drives[i].serial && drives[i].serial[0] && strcmp(drives[i].serial, newSerialNumberText))
522 set_drive_serial( drives[i].letter, drives[i].serial );
524 set_drive_type( drives[i].letter, drives[i].type );