mshtml: Better timer handling.
[wine/wine-jacek.git] / programs / winecfg / drive.c
blobdd3befd60343dd8531773eb4c1b7466841dcc828
1 /*
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
24 #include "config.h"
25 #include "wine/port.h"
27 #include <assert.h>
28 #include <stdarg.h>
29 #include <stdio.h>
30 #include <string.h>
32 #include <windef.h>
33 #include <winbase.h>
34 #include <winreg.h>
35 #include <wine/debug.h>
36 #include <shellapi.h>
37 #include <objbase.h>
38 #include <shlguid.h>
39 #include <shlwapi.h>
40 #include <shlobj.h>
41 #include <wine/library.h>
43 #include "winecfg.h"
44 #include "resource.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
59 * significant.
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)
71 long result = 0;
72 int i;
74 WINE_TRACE("\n");
77 for(i = 0; i < 26; i++)
79 if (!drives[i].in_use) continue;
80 result |= (1 << (letter_to_index(drives[i].letter)));
83 result = ~result;
84 if (letter) result |= DRIVE_MASK_BIT(letter);
86 WINE_TRACE("finished drive letter loop with %lx\n", result);
87 return 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)
95 return FALSE;
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;
107 return 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);
114 d->unixpath = NULL;
115 HeapFree(GetProcessHeap(), 0, d->label);
116 d->label = NULL;
117 HeapFree(GetProcessHeap(), 0, d->serial);
118 d->serial = NULL;
120 d->in_use = FALSE;
123 static void set_drive_type( char letter, DWORD type )
125 HKEY hKey;
126 char driveValue[4];
127 const char *typeText = NULL;
129 sprintf(driveValue, "%c:", letter);
131 /* Set the drive type in the registry */
132 if (type == DRIVE_FIXED)
133 typeText = "hd";
134 else if (type == DRIVE_REMOTE)
135 typeText = "network";
136 else if (type == DRIVE_REMOVABLE)
137 typeText = "floppy";
138 else if (type == DRIVE_CDROM)
139 typeText = "cdrom";
141 if (RegCreateKey(HKEY_LOCAL_MACHINE, "Software\\Wine\\Drives", &hKey) != ERROR_SUCCESS)
142 WINE_TRACE(" Unable to open '%s'\n", "Software\\Wine\\Drives");
143 else
145 if (typeText)
146 RegSetValueEx( hKey, driveValue, 0, REG_SZ, (const BYTE *)typeText, strlen(typeText) + 1 );
147 else
148 RegDeleteValue( hKey, driveValue );
149 RegCloseKey(hKey);
153 static DWORD get_drive_type( char letter )
155 HKEY hKey;
156 char driveValue[4];
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" );
163 else
165 char buffer[80];
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;
176 RegCloseKey(hKey);
178 return ret;
182 static void set_drive_label( char letter, const char *label )
184 char device[] = "a:\\"; /* SetVolumeLabel() requires a trailing slash */
185 device[0] = letter;
187 if(!SetVolumeLabel(device, label))
189 WINE_WARN("unable to set volume label for devicename of '%s', label of '%s'\n",
190 device, label);
191 PRINTERROR();
193 else
195 WINE_TRACE(" set volume label for devicename of '%s', label of '%s'\n",
196 device, label);
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";
204 HANDLE hFile;
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)
212 DWORD w;
213 WriteFile(hFile, serial, strlen(serial), &w, NULL);
214 WriteFile(hFile, "\n", 1, &w, NULL);
215 CloseHandle(hFile);
219 #if 0
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)
227 if(pDst->in_use)
229 WINE_TRACE("pDst already in use\n");
230 return FALSE;
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;
241 pDst->in_use = TRUE;
243 return TRUE;
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");
253 return FALSE;
256 delete_drive(pSrc);
257 return TRUE;
260 #endif
262 /* Load currently defined drives into the drives array */
263 void load_drives(void)
265 char *devices, *dev;
266 int len;
267 int drivecount = 0, i;
268 int retval;
269 static const int arraysize = 512;
270 const char *config_dir = wine_get_config_dir();
271 char *path;
273 WINE_TRACE("\n");
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 */
296 while (len)
298 char volname[512]; /* volume name */
299 DWORD serial;
300 char serialstr[256];
301 char rootpath[256];
302 char simplepath[3];
303 int pathlen;
304 char targetpath[256];
305 char *c;
307 *devices = toupper(*devices);
309 WINE_TRACE("devices == '%s'\n", devices);
311 volname[0] = 0;
313 retval = GetVolumeInformation(devices,
314 volname,
315 sizeof(volname),
316 &serial,
317 NULL,
318 NULL,
319 NULL,
321 if(!retval)
323 WINE_ERR("GetVolumeInformation() for '%s' failed, setting serial to 0\n", devices);
324 PRINTERROR();
325 serial = 0;
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 */
346 c = targetpath;
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))
359 len--;
360 devices++;
363 drivecount++;
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)))
370 return;
372 strcpy(path, config_dir);
373 strcat(path, "/dosdevices/a:");
375 for (i = 0; i < 26; i++)
377 char buff[MAX_PATH];
378 struct stat st;
379 int cnt;
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;
386 buff[cnt] = '\0';
388 WINE_TRACE("found broken symlink %s -> %s\n", path, buff);
389 add_drive('A' + i, buff, "", "0", DRIVE_UNKNOWN);
391 drivecount++;
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)
404 int i;
405 CHAR devicename[4];
406 CHAR targetpath[256];
407 BOOL foundDrive;
408 CHAR volumeNameBuffer[512];
409 DWORD serialNumber;
410 DWORD maxComponentLength;
411 DWORD fileSystemFlags;
412 CHAR fileSystemName[128];
413 char newSerialNumberText[32];
414 int retval;
415 BOOL defineDevice;
417 WINE_TRACE("\n");
419 /* add each drive and remove as we go */
420 for(i = 0; i < 26; i++)
422 defineDevice = FALSE;
423 foundDrive = FALSE;
424 volumeNameBuffer[0] = 0;
425 serialNumber = 0;
426 snprintf(devicename, sizeof(devicename), "%c:", 'A' + i);
428 /* get a drive */
429 if(QueryDosDevice(devicename, targetpath, sizeof(targetpath)))
431 char *cursor;
433 /* correct the slashes in the path to be UNIX style */
434 while ((cursor = strchr(targetpath, '\\'))) *cursor = '/';
436 foundDrive = TRUE;
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,
446 volumeNameBuffer,
447 sizeof(volumeNameBuffer),
448 &serialNumber,
449 &maxComponentLength,
450 &fileSystemFlags,
451 fileSystemName,
452 sizeof(fileSystemName));
453 if(!retval)
455 WINE_TRACE(" GetVolumeInformation() for '%s' failed\n", devicename);
456 PRINTERROR();
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))
467 defineDevice = TRUE;
468 WINE_TRACE(" making changes to drive '%s'\n", devicename);
470 else
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);
482 PRINTERROR();
484 else
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 );
491 continue;
493 else if(drives[i].in_use) /* foundDrive must be false from the above check */
495 defineDevice = TRUE;
498 /* adding and modifying are the same steps */
499 if(defineDevice)
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);
508 PRINTERROR();
510 else
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 );