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
32 #include <wine/debug.h>
43 WINE_DEFAULT_DEBUG_CHANNEL(winecfg
);
45 struct drive drives
[26]; /* one for each drive letter */
47 static inline int letter_to_index(char letter
)
49 return (toupper(letter
) - 'A');
52 /* This function produces a mask for each drive letter that isn't
53 * currently used. Each bit of the long result represents a letter,
54 * with A being the least significant bit, and Z being the most
57 * To calculate this, we loop over each letter, and see if we can get
58 * a drive entry for it. If so, we set the appropriate bit. At the
59 * end, we flip each bit, to give the desired result.
61 * The letter parameter is always marked as being available. This is
62 * so the edit dialog can display the currently used drive letter
63 * alongside the available ones.
65 long drive_available_mask(char letter
)
73 for(i
= 0; i
< 26; i
++)
75 if (!drives
[i
].in_use
) continue;
76 result
|= (1 << (letter_to_index(drives
[i
].letter
)));
80 if (letter
) result
|= DRIVE_MASK_BIT(letter
);
82 WINE_TRACE("finished drive letter loop with %lx\n", result
);
86 BOOL
add_drive(const char letter
, const char *targetpath
, const char *label
, const char *serial
, unsigned int type
)
88 int driveIndex
= letter_to_index(letter
);
90 if(drives
[driveIndex
].in_use
)
93 WINE_TRACE("letter == '%c', unixpath == '%s', label == '%s', serial == '%s', type == %d\n",
94 letter
, targetpath
, label
, serial
, type
);
96 drives
[driveIndex
].letter
= toupper(letter
);
97 drives
[driveIndex
].unixpath
= strdupA(targetpath
);
98 drives
[driveIndex
].label
= strdupA(label
);
99 drives
[driveIndex
].serial
= strdupA(serial
);
100 drives
[driveIndex
].type
= type
;
101 drives
[driveIndex
].in_use
= TRUE
;
106 /* deallocates the contents of the drive. does not free the drive itself */
107 void delete_drive(struct drive
*d
)
109 HeapFree(GetProcessHeap(), 0, d
->unixpath
);
111 HeapFree(GetProcessHeap(), 0, d
->label
);
113 HeapFree(GetProcessHeap(), 0, d
->serial
);
119 static void set_drive_type( char letter
, DWORD type
)
123 const char *typeText
= NULL
;
125 sprintf(driveValue
, "%c:", letter
);
127 /* Set the drive type in the registry */
128 if (type
== DRIVE_FIXED
)
130 else if (type
== DRIVE_REMOTE
)
131 typeText
= "network";
132 else if (type
== DRIVE_REMOVABLE
)
134 else if (type
== DRIVE_CDROM
)
137 if (RegCreateKey(HKEY_LOCAL_MACHINE
, "Software\\Wine\\Drives", &hKey
) != ERROR_SUCCESS
)
138 WINE_TRACE(" Unable to open '%s'\n", "Software\\Wine\\Drives");
142 RegSetValueEx( hKey
, driveValue
, 0, REG_SZ
, (LPBYTE
)typeText
, strlen(typeText
) + 1 );
144 RegDeleteValue( hKey
, driveValue
);
149 static DWORD
get_drive_type( char letter
)
153 DWORD ret
= DRIVE_UNKNOWN
;
155 sprintf(driveValue
, "%c:", letter
);
157 if (RegOpenKey(HKEY_LOCAL_MACHINE
, "Software\\Wine\\Drives", &hKey
) != ERROR_SUCCESS
)
158 WINE_TRACE(" Unable to open Software\\Wine\\Drives\n" );
162 DWORD size
= sizeof(buffer
);
164 if (!RegQueryValueExA( hKey
, driveValue
, NULL
, NULL
, (LPBYTE
)buffer
, &size
))
166 WINE_TRACE("Got type '%s' for %s\n", buffer
, driveValue
);
167 if (!strcasecmp( buffer
, "hd" )) ret
= DRIVE_FIXED
;
168 else if (!strcasecmp( buffer
, "network" )) ret
= DRIVE_REMOTE
;
169 else if (!strcasecmp( buffer
, "floppy" )) ret
= DRIVE_REMOVABLE
;
170 else if (!strcasecmp( buffer
, "cdrom" )) ret
= DRIVE_CDROM
;
178 static void set_drive_label( char letter
, const char *label
)
180 char device
[] = "a:\\"; /* SetVolumeLabel() requires a trailing slash */
183 if(!SetVolumeLabel(device
, label
))
185 WINE_WARN("unable to set volume label for devicename of '%s', label of '%s'\n",
191 WINE_TRACE(" set volume label for devicename of '%s', label of '%s'\n",
196 /* set the drive serial number via a .windows-serial file */
197 static void set_drive_serial( char letter
, const char *serial
)
199 char filename
[] = "a:\\.windows-serial";
202 filename
[0] = letter
;
203 WINE_TRACE("Putting serial number of '%s' into file '%s'\n", serial
, filename
);
204 hFile
= CreateFile(filename
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
205 CREATE_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
206 if (hFile
!= INVALID_HANDLE_VALUE
)
209 WriteFile(hFile
, serial
, strlen(serial
), &w
, NULL
);
210 WriteFile(hFile
, "\n", 1, &w
, NULL
);
217 /* currently unused, but if users have this burning desire to be able to rename drives,
218 we can put it back in.
221 BOOL
copyDrive(struct drive
*pSrc
, struct drive
*pDst
)
225 WINE_TRACE("pDst already in use\n");
229 if(!pSrc
->unixpath
) WINE_TRACE("!pSrc->unixpath\n");
230 if(!pSrc
->label
) WINE_TRACE("!pSrc->label\n");
231 if(!pSrc
->serial
) WINE_TRACE("!pSrc->serial\n");
233 pDst
->unixpath
= strdupA(pSrc
->unixpath
);
234 pDst
->label
= strdupA(pSrc
->label
);
235 pDst
->serial
= strdupA(pSrc
->serial
);
236 pDst
->type
= pSrc
->type
;
242 BOOL
moveDrive(struct drive
*pSrc
, struct drive
*pDst
)
244 WINE_TRACE("pSrc->letter == %c, pDst->letter == %c\n", pSrc
->letter
, pDst
->letter
);
246 if(!copyDrive(pSrc
, pDst
))
248 WINE_TRACE("copyDrive failed\n");
258 /* Load currently defined drives into the drives array */
263 int drivecount
= 0, i
;
265 static const int arraysize
= 512;
269 /* FIXME: broken symlinks in $WINEPREFIX/dosdevices will not be
270 returned by this API, so we need to handle that */
272 /* setup the drives array */
273 dev
= devices
= HeapAlloc(GetProcessHeap(), 0, arraysize
);
274 len
= GetLogicalDriveStrings(arraysize
, devices
);
276 /* make all devices unused */
277 for (i
= 0; i
< 26; i
++)
279 drives
[i
].letter
= 'A' + i
;
280 drives
[i
].in_use
= FALSE
;
282 HeapFree(GetProcessHeap(), 0, drives
[i
].unixpath
);
283 drives
[i
].unixpath
= NULL
;
285 HeapFree(GetProcessHeap(), 0, drives
[i
].label
);
286 drives
[i
].label
= NULL
;
288 HeapFree(GetProcessHeap(), 0, drives
[i
].serial
);
289 drives
[i
].serial
= NULL
;
292 /* work backwards through the result of GetLogicalDriveStrings */
295 char volname
[512]; /* volume name */
301 char targetpath
[256];
304 *devices
= toupper(*devices
);
306 WINE_TRACE("devices == '%s'\n", devices
);
310 retval
= GetVolumeInformation(devices
,
320 WINE_ERR("GetVolumeInformation() for '%s' failed, setting serial to 0\n", devices
);
325 WINE_TRACE("serial: '0x%lX'\n", serial
);
327 /* build rootpath for GetDriveType() */
328 lstrcpynA(rootpath
, devices
, sizeof(rootpath
));
329 pathlen
= strlen(rootpath
);
331 /* ensure that we have a backslash on the root path */
332 if ((rootpath
[pathlen
- 1] != '\\') && (pathlen
< sizeof(rootpath
)))
334 rootpath
[pathlen
] = '\\';
335 rootpath
[pathlen
+ 1] = 0;
338 /* QueryDosDevice() requires no trailing backslash */
339 lstrcpynA(simplepath
, devices
, 3);
340 QueryDosDevice(simplepath
, targetpath
, sizeof(targetpath
));
342 /* targetpath may have forward slashes rather than backslashes, so correct */
344 do if (*c
== '\\') *c
= '/'; while (*c
++);
346 snprintf(serialstr
, sizeof(serialstr
), "%lX", serial
);
347 WINE_TRACE("serialstr: '%s'\n", serialstr
);
348 add_drive(*devices
, targetpath
, volname
, serialstr
, get_drive_type(devices
[0]) );
350 len
-= strlen(devices
);
351 devices
+= strlen(devices
);
353 /* skip over any nulls */
354 while ((*devices
== 0) && (len
))
363 WINE_TRACE("found %d drives\n", drivecount
);
365 HeapFree(GetProcessHeap(), 0, dev
);
368 /* some of this code appears to be broken by bugs in Wine: the label
369 * setting code has no effect, for instance */
370 void apply_drive_changes(void)
374 CHAR targetpath
[256];
376 CHAR volumeNameBuffer
[512];
378 DWORD maxComponentLength
;
379 DWORD fileSystemFlags
;
380 CHAR fileSystemName
[128];
381 char newSerialNumberText
[32];
387 /* add each drive and remove as we go */
388 for(i
= 0; i
< 26; i
++)
390 defineDevice
= FALSE
;
392 volumeNameBuffer
[0] = 0;
394 snprintf(devicename
, sizeof(devicename
), "%c:", 'A' + i
);
397 if(QueryDosDevice(devicename
, targetpath
, sizeof(targetpath
)))
401 /* correct the slashes in the path to be UNIX style */
402 while ((cursor
= strchr(targetpath
, '\\'))) *cursor
= '/';
407 /* if we found a drive and have a drive then compare things */
408 if(foundDrive
&& drives
[i
].in_use
)
410 WINE_TRACE("drives[i].letter: '%c'\n", drives
[i
].letter
);
412 snprintf(devicename
, sizeof(devicename
), "%c:\\", 'A' + i
);
413 retval
= GetVolumeInformation(devicename
,
415 sizeof(volumeNameBuffer
),
420 sizeof(fileSystemName
));
423 WINE_TRACE(" GetVolumeInformation() for '%s' failed\n", devicename
);
424 WINE_TRACE(" Skipping this drive\n");
426 continue; /* skip this drive */
429 WINE_TRACE(" current path: '%s', new path: '%s'\n",
430 targetpath
, drives
[i
].unixpath
);
432 /* compare to what we have */
433 /* do we have the same targetpath? */
434 if(strcmp(drives
[i
].unixpath
, targetpath
))
437 WINE_TRACE(" making changes to drive '%s'\n", devicename
);
441 WINE_TRACE(" no changes to drive '%s'\n", devicename
);
444 else if(foundDrive
&& !drives
[i
].in_use
)
446 /* remove this drive */
447 if(!DefineDosDevice(DDD_REMOVE_DEFINITION
, devicename
, drives
[i
].unixpath
))
449 WINE_ERR("unable to remove devicename of '%s', targetpath of '%s'\n",
450 devicename
, drives
[i
].unixpath
);
455 WINE_TRACE("removed devicename of '%s', targetpath of '%s'\n",
456 devicename
, drives
[i
].unixpath
);
459 set_drive_type( drives
[i
].letter
, DRIVE_UNKNOWN
);
462 else if(drives
[i
].in_use
) /* foundDrive must be false from the above check */
467 /* adding and modifying are the same steps */
470 /* define this drive */
471 /* DefineDosDevice() requires that NO trailing slash be present */
472 snprintf(devicename
, sizeof(devicename
), "%c:", 'A' + i
);
473 if(!DefineDosDevice(DDD_RAW_TARGET_PATH
, devicename
, drives
[i
].unixpath
))
475 WINE_ERR(" unable to define devicename of '%s', targetpath of '%s'\n",
476 devicename
, drives
[i
].unixpath
);
481 WINE_TRACE(" added devicename of '%s', targetpath of '%s'\n",
482 devicename
, drives
[i
].unixpath
);
486 if (drives
[i
].label
&& strcmp(drives
[i
].label
, volumeNameBuffer
))
487 set_drive_label( drives
[i
].letter
, drives
[i
].label
);
489 snprintf(newSerialNumberText
, sizeof(newSerialNumberText
), "%lX", serialNumber
);
490 if (drives
[i
].serial
&& drives
[i
].serial
[0] && strcmp(drives
[i
].serial
, newSerialNumberText
))
491 set_drive_serial( drives
[i
].letter
, drives
[i
].serial
);
493 set_drive_type( drives
[i
].letter
, drives
[i
].type
);