winecfg: Enable add button when choosing lib from combobox (Libraries tabsheet).
[wine] / programs / winecfg / drive.c
1 /*
2  * Drive management code
3  *
4  * Copyright 2003 Mark Westcott
5  * Copyright 2003-2004 Mike Hearn
6  * Copyright 2004 Chris Morgan
7  *
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.
12  *
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.
17  *
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
21  *
22  */
23
24 #include "config.h"
25 #include "wine/port.h"
26
27 #include <assert.h>
28 #include <stdarg.h>
29 #include <stdio.h>
30 #include <string.h>
31
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>
42
43 #include "winecfg.h"
44 #include "resource.h"
45
46
47 WINE_DEFAULT_DEBUG_CHANNEL(winecfg);
48
49 struct drive drives[26]; /* one for each drive letter */
50
51 static inline int letter_to_index(char letter)
52 {
53     return (toupper(letter) - 'A');
54 }
55
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.
60  *
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.
64  *
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.
68  */
69 long drive_available_mask(char letter)
70 {
71   long result = 0;
72   int i;
73
74   WINE_TRACE("\n");
75
76
77   for(i = 0; i < 26; i++)
78   {
79       if (!drives[i].in_use) continue;
80       result |= (1 << (letter_to_index(drives[i].letter)));
81   }
82
83   result = ~result;
84   if (letter) result |= DRIVE_MASK_BIT(letter);
85
86   WINE_TRACE("finished drive letter loop with %lx\n", result);
87   return result;
88 }
89
90 BOOL add_drive(const char letter, const char *targetpath, const char *label, const char *serial, unsigned int type)
91 {
92     int driveIndex = letter_to_index(letter);
93
94     if(drives[driveIndex].in_use)
95         return FALSE;
96
97     WINE_TRACE("letter == '%c', unixpath == '%s', label == '%s', serial == '%s', type == %d\n",
98                letter, targetpath, label, serial, type);
99
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;
106
107     return TRUE;
108 }
109
110 /* deallocates the contents of the drive. does not free the drive itself  */
111 void delete_drive(struct drive *d)
112 {
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;
119
120     d->in_use = FALSE;
121 }
122
123 static void set_drive_type( char letter, DWORD type )
124 {
125     HKEY hKey;
126     char driveValue[4];
127     const char *typeText = NULL;
128
129     sprintf(driveValue, "%c:", letter);
130
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";
140
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
144     {
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);
150     }
151 }
152
153 static DWORD get_drive_type( char letter )
154 {
155     HKEY hKey;
156     char driveValue[4];
157     DWORD ret = DRIVE_UNKNOWN;
158
159     sprintf(driveValue, "%c:", letter);
160
161     if (RegOpenKey(HKEY_LOCAL_MACHINE, "Software\\Wine\\Drives", &hKey) != ERROR_SUCCESS)
162         WINE_TRACE("  Unable to open Software\\Wine\\Drives\n" );
163     else
164     {
165         char buffer[80];
166         DWORD size = sizeof(buffer);
167
168         if (!RegQueryValueExA( hKey, driveValue, NULL, NULL, (LPBYTE)buffer, &size ))
169         {
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;
175         }
176         RegCloseKey(hKey);
177     }
178     return ret;
179 }
180
181
182 static void set_drive_label( char letter, const char *label )
183 {
184     char device[] = "a:\\";  /* SetVolumeLabel() requires a trailing slash */
185     device[0] = letter;
186
187     if(!SetVolumeLabel(device, label))
188     {
189         WINE_WARN("unable to set volume label for devicename of '%s', label of '%s'\n",
190                   device, label);
191         PRINTERROR();
192     }
193     else
194     {
195         WINE_TRACE("  set volume label for devicename of '%s', label of '%s'\n",
196                    device, label);
197     }
198 }
199
200 /* set the drive serial number via a .windows-serial file */
201 static void set_drive_serial( char letter, const char *serial )
202 {
203     char filename[] = "a:\\.windows-serial";
204     HANDLE hFile;
205
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)
211     {
212         DWORD w;
213         WriteFile(hFile, serial, strlen(serial), &w, NULL);
214         WriteFile(hFile, "\n", 1, &w, NULL);
215         CloseHandle(hFile);
216     }
217 }
218
219 #if 0
220
221 /* currently unused, but if users have this burning desire to be able to rename drives,
222    we can put it back in.
223  */
224
225 BOOL copyDrive(struct drive *pSrc, struct drive *pDst)
226 {
227     if(pDst->in_use)
228     {
229         WINE_TRACE("pDst already in use\n");
230         return FALSE;
231     }
232
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");
236
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;
242
243     return TRUE;
244 }
245
246 BOOL moveDrive(struct drive *pSrc, struct drive *pDst)
247 {
248     WINE_TRACE("pSrc->letter == %c, pDst->letter == %c\n", pSrc->letter, pDst->letter);
249
250     if(!copyDrive(pSrc, pDst))
251     {
252         WINE_TRACE("copyDrive failed\n");
253         return FALSE;
254     }
255
256     delete_drive(pSrc);
257     return TRUE;
258 }
259
260 #endif
261
262 /* Load currently defined drives into the drives array  */
263 void load_drives(void)
264 {
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;
272
273     WINE_TRACE("\n");
274
275     /* setup the drives array */
276     dev = devices = HeapAlloc(GetProcessHeap(), 0, arraysize);
277     len = GetLogicalDriveStrings(arraysize, devices);
278
279     /* make all devices unused */
280     for (i = 0; i < 26; i++)
281     {
282         drives[i].letter = 'A' + i;
283         drives[i].in_use = FALSE;
284
285         HeapFree(GetProcessHeap(), 0, drives[i].unixpath);
286         drives[i].unixpath = NULL;
287
288         HeapFree(GetProcessHeap(), 0, drives[i].label);
289         drives[i].label = NULL;
290
291         HeapFree(GetProcessHeap(), 0, drives[i].serial);
292         drives[i].serial = NULL;
293     }
294
295     /* work backwards through the result of GetLogicalDriveStrings  */
296     while (len)
297     {
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;
306
307         *devices = toupper(*devices);
308
309         WINE_TRACE("devices == '%s'\n", devices);
310
311         volname[0] = 0;
312
313         retval = GetVolumeInformation(devices,
314                                       volname,
315                                       sizeof(volname),
316                                       &serial,
317                                       NULL,
318                                       NULL,
319                                       NULL,
320                                       0);
321         if(!retval)
322         {
323             WINE_ERR("GetVolumeInformation() for '%s' failed, setting serial to 0\n", devices);
324             PRINTERROR();
325             serial = 0;
326         }
327
328         WINE_TRACE("serial: '0x%X'\n", serial);
329
330         /* build rootpath for GetDriveType() */
331         lstrcpynA(rootpath, devices, sizeof(rootpath));
332         pathlen = strlen(rootpath);
333
334         /* ensure that we have a backslash on the root path */
335         if ((rootpath[pathlen - 1] != '\\') && (pathlen < sizeof(rootpath)))
336         {
337             rootpath[pathlen] = '\\';
338             rootpath[pathlen + 1] = 0;
339         }
340
341         /* QueryDosDevice() requires no trailing backslash */
342         lstrcpynA(simplepath, devices, 3);
343         QueryDosDevice(simplepath, targetpath, sizeof(targetpath));
344
345         /* targetpath may have forward slashes rather than backslashes, so correct */
346         c = targetpath;
347         do if (*c == '\\') *c = '/'; while (*c++);
348
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]) );
352
353         len -= strlen(devices);
354         devices += strlen(devices);
355
356         /* skip over any nulls */
357         while ((*devices == 0) && (len))
358         {
359             len--;
360             devices++;
361         }
362
363         drivecount++;
364     }
365
366     /* Find all the broken symlinks we might have and add them as well. */
367
368     len = strlen(config_dir) + sizeof("/dosdevices/a:");
369     if (!(path = HeapAlloc(GetProcessHeap(), 0, len)))
370         return;
371
372     strcpy(path, config_dir);
373     strcat(path, "/dosdevices/a:");
374
375     for (i = 0; i < 26; i++)
376     {
377         char buff[MAX_PATH];
378         struct stat st;
379         int cnt;
380
381         if (drives[i].in_use) continue;
382         path[len - 3] = 'a' + i;
383
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';
387
388         WINE_TRACE("found broken symlink %s -> %s\n", path, buff);
389         add_drive('A' + i, buff, "", "0", DRIVE_UNKNOWN);
390
391         drivecount++;
392     }
393
394     WINE_TRACE("found %d drives\n", drivecount);
395
396     HeapFree(GetProcessHeap(), 0, path);
397     HeapFree(GetProcessHeap(), 0, dev);
398 }
399
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)
403 {
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;
416
417     WINE_TRACE("\n");
418
419     /* add each drive and remove as we go */
420     for(i = 0; i < 26; i++)
421     {
422         defineDevice = FALSE;
423         foundDrive = FALSE;
424         volumeNameBuffer[0] = 0;
425         serialNumber = 0;
426         snprintf(devicename, sizeof(devicename), "%c:", 'A' + i);
427
428         /* get a drive */
429         if(QueryDosDevice(devicename, targetpath, sizeof(targetpath)))
430         {
431             char *cursor;
432             
433             /* correct the slashes in the path to be UNIX style */
434             while ((cursor = strchr(targetpath, '\\'))) *cursor = '/';
435
436             foundDrive = TRUE;
437         }
438
439         /* if we found a drive and have a drive then compare things */
440         if(foundDrive && drives[i].in_use)
441         {
442             WINE_TRACE("drives[i].letter: '%c'\n", drives[i].letter);
443
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)
454             {
455                 WINE_TRACE("  GetVolumeInformation() for '%s' failed\n", devicename);
456                 PRINTERROR();
457                 volumeNameBuffer[0] = '\0';
458             }
459
460             WINE_TRACE("  current path:   '%s', new path:   '%s'\n",
461                        targetpath, drives[i].unixpath);
462
463             /* compare to what we have */
464             /* do we have the same targetpath? */
465             if(strcmp(drives[i].unixpath, targetpath))
466             {
467                 defineDevice = TRUE;
468                 WINE_TRACE("  making changes to drive '%s'\n", devicename);
469             }
470             else
471             {
472                 WINE_TRACE("  no changes to drive '%s'\n", devicename);
473             }
474         }
475         else if(foundDrive && !drives[i].in_use)
476         {
477             /* remove this drive */
478             if(!DefineDosDevice(DDD_REMOVE_DEFINITION, devicename, drives[i].unixpath))
479             {
480                 WINE_ERR("unable to remove devicename of '%s', targetpath of '%s'\n",
481                     devicename, drives[i].unixpath);
482                 PRINTERROR();
483             }
484             else
485             {
486                 WINE_TRACE("removed devicename of '%s', targetpath of '%s'\n",
487                            devicename, drives[i].unixpath);
488             }
489
490             set_drive_type( drives[i].letter, DRIVE_UNKNOWN );
491             continue;
492         }
493         else if(drives[i].in_use) /* foundDrive must be false from the above check */
494         {
495             defineDevice = TRUE;
496         }
497
498         /* adding and modifying are the same steps */
499         if(defineDevice)
500         {
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))
505             {
506                 WINE_ERR("  unable to define devicename of '%s', targetpath of '%s'\n",
507                     devicename, drives[i].unixpath);
508                 PRINTERROR();
509             }
510             else
511             {
512                 WINE_TRACE("  added devicename of '%s', targetpath of '%s'\n",
513                            devicename, drives[i].unixpath);
514             }
515         }
516
517         if (drives[i].label && strcmp(drives[i].label, volumeNameBuffer))
518             set_drive_label( drives[i].letter, drives[i].label );
519
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 );
523
524         set_drive_type( drives[i].letter, drives[i].type );
525     }
526 }