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(char letter, const char *targetpath, const WCHAR *label, DWORD serial, DWORD type)
92 int driveIndex = letter_to_index(letter);
94 if(drives[driveIndex].in_use)
97 WINE_TRACE("letter == '%c', unixpath == '%s', label == %s, serial == %08x, type == %d\n",
98 letter, targetpath, wine_dbgstr_w(label), serial, type);
100 drives[driveIndex].letter = toupper(letter);
101 drives[driveIndex].unixpath = strdupA(targetpath);
102 drives[driveIndex].label = strdupW(label);
103 drives[driveIndex].serial = serial;
104 drives[driveIndex].type = type;
105 drives[driveIndex].in_use = TRUE;
106 drives[driveIndex].modified = TRUE;
111 /* deallocates the contents of the drive. does not free the drive itself */
112 void delete_drive(struct drive *d)
114 HeapFree(GetProcessHeap(), 0, d->unixpath);
116 HeapFree(GetProcessHeap(), 0, d->label);
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 (!lstrcmpi( buffer, "hd" )) ret = DRIVE_FIXED;
172 else if (!lstrcmpi( buffer, "network" )) ret = DRIVE_REMOTE;
173 else if (!lstrcmpi( buffer, "floppy" )) ret = DRIVE_REMOVABLE;
174 else if (!lstrcmpi( buffer, "cdrom" )) ret = DRIVE_CDROM;
182 static void set_drive_label( char letter, const WCHAR *label )
184 static const WCHAR emptyW[1];
185 WCHAR device[] = {'a',':','\\',0}; /* SetVolumeLabel() requires a trailing slash */
188 if (!label) label = emptyW;
189 if(!SetVolumeLabelW(device, label))
191 WINE_WARN("unable to set volume label for devicename of %s, label of %s\n",
192 wine_dbgstr_w(device), wine_dbgstr_w(label));
197 WINE_TRACE(" set volume label for devicename of %s, label of %s\n",
198 wine_dbgstr_w(device), wine_dbgstr_w(label));
202 /* set the drive serial number via a .windows-serial file */
203 static void set_drive_serial( char letter, DWORD serial )
205 char filename[] = "a:\\.windows-serial";
208 filename[0] = letter;
209 WINE_TRACE("Putting serial number of %08x into file '%s'\n", serial, filename);
210 hFile = CreateFile(filename, GENERIC_WRITE, FILE_SHARE_READ, NULL,
211 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
212 if (hFile != INVALID_HANDLE_VALUE)
217 sprintf( buffer, "%X\n", serial );
218 WriteFile(hFile, buffer, strlen(buffer), &w, NULL);
225 /* currently unused, but if users have this burning desire to be able to rename drives,
226 we can put it back in.
229 BOOL copyDrive(struct drive *pSrc, struct drive *pDst)
233 WINE_TRACE("pDst already in use\n");
237 if(!pSrc->unixpath) WINE_TRACE("!pSrc->unixpath\n");
238 if(!pSrc->label) WINE_TRACE("!pSrc->label\n");
239 if(!pSrc->serial) WINE_TRACE("!pSrc->serial\n");
241 pDst->unixpath = strdupA(pSrc->unixpath);
242 pDst->label = strdupA(pSrc->label);
243 pDst->serial = strdupA(pSrc->serial);
244 pDst->type = pSrc->type;
250 BOOL moveDrive(struct drive *pSrc, struct drive *pDst)
252 WINE_TRACE("pSrc->letter == %c, pDst->letter == %c\n", pSrc->letter, pDst->letter);
254 if(!copyDrive(pSrc, pDst))
256 WINE_TRACE("copyDrive failed\n");
266 /* Load currently defined drives into the drives array */
267 void load_drives(void)
269 WCHAR *devices, *dev;
271 int drivecount = 0, i;
273 static const int arraysize = 512;
274 const char *config_dir = wine_get_config_dir();
279 /* setup the drives array */
280 dev = devices = HeapAlloc(GetProcessHeap(), 0, arraysize * sizeof(WCHAR));
281 len = GetLogicalDriveStringsW(arraysize, devices);
283 /* make all devices unused */
284 for (i = 0; i < 26; i++)
286 drives[i].letter = 'A' + i;
287 drives[i].in_use = FALSE;
288 drives[i].serial = 0;
290 HeapFree(GetProcessHeap(), 0, drives[i].unixpath);
291 drives[i].unixpath = NULL;
293 HeapFree(GetProcessHeap(), 0, drives[i].label);
294 drives[i].label = NULL;
297 /* work backwards through the result of GetLogicalDriveStrings */
300 WCHAR volname[512]; /* volume name */
303 char targetpath[256];
306 WINE_TRACE("devices == %s\n", wine_dbgstr_w(devices));
310 retval = GetVolumeInformationW(devices, volname, sizeof(volname)/sizeof(WCHAR),
311 &serial, NULL, NULL, NULL, 0);
314 WINE_ERR("GetVolumeInformation() for %s failed, setting serial to 0\n",
315 wine_dbgstr_w(devices));
320 WINE_TRACE("serial: '0x%X'\n", serial);
322 /* QueryDosDevice() requires no trailing backslash */
323 simplepath[0] = devices[0];
326 QueryDosDevice(simplepath, targetpath, sizeof(targetpath));
328 /* targetpath may have forward slashes rather than backslashes, so correct */
330 do if (*c == '\\') *c = '/'; while (*c++);
332 add_drive(*devices, targetpath, volname, serial, get_drive_type(devices[0]) );
334 len -= lstrlenW(devices);
335 devices += lstrlenW(devices);
337 /* skip over any nulls */
338 while ((*devices == 0) && (len))
347 /* Find all the broken symlinks we might have and add them as well. */
349 len = strlen(config_dir) + sizeof("/dosdevices/a:");
350 if (!(path = HeapAlloc(GetProcessHeap(), 0, len)))
353 strcpy(path, config_dir);
354 strcat(path, "/dosdevices/a:");
356 for (i = 0; i < 26; i++)
362 if (drives[i].in_use) continue;
363 path[len - 3] = 'a' + i;
365 if (lstat(path, &st) == -1 || !S_ISLNK(st.st_mode)) continue;
366 if ((cnt = readlink(path, buff, sizeof(buff))) == -1) continue;
369 WINE_TRACE("found broken symlink %s -> %s\n", path, buff);
370 add_drive('A' + i, buff, NULL, 0, DRIVE_UNKNOWN);
375 /* reset modified flags */
376 for (i = 0; i < 26; i++) drives[i].modified = FALSE;
378 WINE_TRACE("found %d drives\n", drivecount);
380 HeapFree(GetProcessHeap(), 0, path);
381 HeapFree(GetProcessHeap(), 0, dev);
384 /* some of this code appears to be broken by bugs in Wine: the label
385 * setting code has no effect, for instance */
386 void apply_drive_changes(void)
390 CHAR targetpath[256];
394 /* add each drive and remove as we go */
395 for(i = 0; i < 26; i++)
397 if (!drives[i].modified) continue;
398 drives[i].modified = FALSE;
400 snprintf(devicename, sizeof(devicename), "%c:", 'A' + i);
402 if (drives[i].in_use)
404 /* define this drive */
405 /* DefineDosDevice() requires that NO trailing slash be present */
406 if(!DefineDosDevice(DDD_RAW_TARGET_PATH, devicename, drives[i].unixpath))
408 WINE_ERR(" unable to define devicename of '%s', targetpath of '%s'\n",
409 devicename, drives[i].unixpath);
414 WINE_TRACE(" added devicename of '%s', targetpath of '%s'\n",
415 devicename, drives[i].unixpath);
417 set_drive_label( drives[i].letter, drives[i].label );
418 set_drive_serial( drives[i].letter, drives[i].serial );
419 set_drive_type( drives[i].letter, drives[i].type );
421 else if (QueryDosDevice(devicename, targetpath, sizeof(targetpath)))
423 /* remove this drive */
424 if(!DefineDosDevice(DDD_REMOVE_DEFINITION, devicename, drives[i].unixpath))
426 WINE_ERR("unable to remove devicename of '%s', targetpath of '%s'\n",
427 devicename, drives[i].unixpath);
432 WINE_TRACE("removed devicename of '%s', targetpath of '%s'\n",
433 devicename, drives[i].unixpath);
436 set_drive_type( drives[i].letter, DRIVE_UNKNOWN );