drive.c 15.4 KB
Newer Older
1
/*
Mike Hearn's avatar
Mike Hearn committed
2
 * Drive management code
3 4
 *
 * Copyright 2003 Mark Westcott
Mike Hearn's avatar
Mike Hearn committed
5
 * Copyright 2003-2004 Mike Hearn
6
 * Copyright 2004 Chris Morgan
7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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 25 26
#include "config.h"
#include "wine/port.h"

27 28
#include <assert.h>
#include <stdarg.h>
29
#include <stdio.h>
30 31 32 33 34 35
#include <string.h>

#include <windef.h>
#include <winbase.h>
#include <winreg.h>
#include <wine/debug.h>
36
#include <shellapi.h>
37
#include <objbase.h>
38 39 40
#include <shlguid.h>
#include <shlwapi.h>
#include <shlobj.h>
41
#include <wine/library.h>
42 43 44 45

#include "winecfg.h"
#include "resource.h"

46

47 48
WINE_DEFAULT_DEBUG_CHANNEL(winecfg);

Mike Hearn's avatar
Mike Hearn committed
49 50 51
struct drive drives[26]; /* one for each drive letter */

static inline int letter_to_index(char letter)
52
{
Mike Hearn's avatar
Mike Hearn committed
53 54
    return (toupper(letter) - 'A');
}
55

Mike Hearn's avatar
Mike Hearn committed
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
/* This function produces a mask for each drive letter that isn't
 * currently used. Each bit of the long result represents a letter,
 * with A being the least significant bit, and Z being the most
 * significant.
 *
 * To calculate this, we loop over each letter, and see if we can get
 * a drive entry for it. If so, we set the appropriate bit. At the
 * end, we flip each bit, to give the desired result.
 *
 * The letter parameter is always marked as being available. This is
 * so the edit dialog can display the currently used drive letter
 * alongside the available ones.
 */
long drive_available_mask(char letter)
{
  long result = 0;
  int i;
73

Mike Hearn's avatar
Mike Hearn committed
74
  WINE_TRACE("\n");
75

Mike Hearn's avatar
Mike Hearn committed
76 77 78 79

  for(i = 0; i < 26; i++)
  {
      if (!drives[i].in_use) continue;
80
      result |= (1 << (letter_to_index(drives[i].letter)));
Mike Hearn's avatar
Mike Hearn committed
81 82 83 84 85 86 87
  }

  result = ~result;
  if (letter) result |= DRIVE_MASK_BIT(letter);

  WINE_TRACE("finished drive letter loop with %lx\n", result);
  return result;
88
}
89

90
BOOL add_drive(const char letter, const char *targetpath, const char *label, const char *serial, unsigned int type)
91
{
Mike Hearn's avatar
Mike Hearn committed
92
    int driveIndex = letter_to_index(letter);
93 94

    if(drives[driveIndex].in_use)
95
        return FALSE;
96

97 98
    WINE_TRACE("letter == '%c', unixpath == '%s', label == '%s', serial == '%s', type == %d\n",
               letter, targetpath, label, serial, type);
99

100
    drives[driveIndex].letter   = toupper(letter);
Mike Hearn's avatar
Mike Hearn committed
101 102 103
    drives[driveIndex].unixpath = strdupA(targetpath);
    drives[driveIndex].label    = strdupA(label);
    drives[driveIndex].serial   = strdupA(serial);
104 105
    drives[driveIndex].type     = type;
    drives[driveIndex].in_use   = TRUE;
106

107 108
    return TRUE;
}
109

Mike Hearn's avatar
Mike Hearn committed
110 111
/* deallocates the contents of the drive. does not free the drive itself  */
void delete_drive(struct drive *d)
112
{
Mike Hearn's avatar
Mike Hearn committed
113
    HeapFree(GetProcessHeap(), 0, d->unixpath);
114
    d->unixpath = NULL;
Mike Hearn's avatar
Mike Hearn committed
115
    HeapFree(GetProcessHeap(), 0, d->label);
116
    d->label = NULL;
Mike Hearn's avatar
Mike Hearn committed
117
    HeapFree(GetProcessHeap(), 0, d->serial);
118
    d->serial = NULL;
119

Mike Hearn's avatar
Mike Hearn committed
120
    d->in_use = FALSE;
121 122
}

123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
static void set_drive_type( char letter, DWORD type )
{
    HKEY hKey;
    char driveValue[4];
    const char *typeText = NULL;

    sprintf(driveValue, "%c:", letter);

    /* Set the drive type in the registry */
    if (type == DRIVE_FIXED)
        typeText = "hd";
    else if (type == DRIVE_REMOTE)
        typeText = "network";
    else if (type == DRIVE_REMOVABLE)
        typeText = "floppy";
    else if (type == DRIVE_CDROM)
        typeText = "cdrom";

    if (RegCreateKey(HKEY_LOCAL_MACHINE, "Software\\Wine\\Drives", &hKey) != ERROR_SUCCESS)
        WINE_TRACE("  Unable to open '%s'\n", "Software\\Wine\\Drives");
    else
    {
        if (typeText)
146
            RegSetValueEx( hKey, driveValue, 0, REG_SZ, (const BYTE *)typeText, strlen(typeText) + 1 );
147 148 149 150 151 152
        else
            RegDeleteValue( hKey, driveValue );
        RegCloseKey(hKey);
    }
}

153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
static DWORD get_drive_type( char letter )
{
    HKEY hKey;
    char driveValue[4];
    DWORD ret = DRIVE_UNKNOWN;

    sprintf(driveValue, "%c:", letter);

    if (RegOpenKey(HKEY_LOCAL_MACHINE, "Software\\Wine\\Drives", &hKey) != ERROR_SUCCESS)
        WINE_TRACE("  Unable to open Software\\Wine\\Drives\n" );
    else
    {
        char buffer[80];
        DWORD size = sizeof(buffer);

        if (!RegQueryValueExA( hKey, driveValue, NULL, NULL, (LPBYTE)buffer, &size ))
        {
            WINE_TRACE("Got type '%s' for %s\n", buffer, driveValue );
171 172 173 174
            if (!lstrcmpi( buffer, "hd" )) ret = DRIVE_FIXED;
            else if (!lstrcmpi( buffer, "network" )) ret = DRIVE_REMOTE;
            else if (!lstrcmpi( buffer, "floppy" )) ret = DRIVE_REMOVABLE;
            else if (!lstrcmpi( buffer, "cdrom" )) ret = DRIVE_CDROM;
175 176 177 178 179 180
        }
        RegCloseKey(hKey);
    }
    return ret;
}

181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218

static void set_drive_label( char letter, const char *label )
{
    char device[] = "a:\\";  /* SetVolumeLabel() requires a trailing slash */
    device[0] = letter;

    if(!SetVolumeLabel(device, label))
    {
        WINE_WARN("unable to set volume label for devicename of '%s', label of '%s'\n",
                  device, label);
        PRINTERROR();
    }
    else
    {
        WINE_TRACE("  set volume label for devicename of '%s', label of '%s'\n",
                   device, label);
    }
}

/* set the drive serial number via a .windows-serial file */
static void set_drive_serial( char letter, const char *serial )
{
    char filename[] = "a:\\.windows-serial";
    HANDLE hFile;

    filename[0] = letter;
    WINE_TRACE("Putting serial number of '%s' into file '%s'\n", serial, filename);
    hFile = CreateFile(filename, GENERIC_WRITE, FILE_SHARE_READ, NULL,
                       CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
    if (hFile != INVALID_HANDLE_VALUE)
    {
        DWORD w;
        WriteFile(hFile, serial, strlen(serial), &w, NULL);
        WriteFile(hFile, "\n", 1, &w, NULL);
        CloseHandle(hFile);
    }
}

Mike Hearn's avatar
Mike Hearn committed
219
#if 0
220

Mike Hearn's avatar
Mike Hearn committed
221 222 223
/* currently unused, but if users have this burning desire to be able to rename drives,
   we can put it back in.
 */
224

Mike Hearn's avatar
Mike Hearn committed
225
BOOL copyDrive(struct drive *pSrc, struct drive *pDst)
226 227 228 229 230 231 232 233 234 235 236
{
    if(pDst->in_use)
    {
        WINE_TRACE("pDst already in use\n");
        return FALSE;
    }

    if(!pSrc->unixpath) WINE_TRACE("!pSrc->unixpath\n");
    if(!pSrc->label) WINE_TRACE("!pSrc->label\n");
    if(!pSrc->serial) WINE_TRACE("!pSrc->serial\n");

Mike Hearn's avatar
Mike Hearn committed
237 238 239
    pDst->unixpath = strdupA(pSrc->unixpath);
    pDst->label = strdupA(pSrc->label);
    pDst->serial = strdupA(pSrc->serial);
240 241 242 243 244 245
    pDst->type = pSrc->type;
    pDst->in_use = TRUE;

    return TRUE;
}

Mike Hearn's avatar
Mike Hearn committed
246
BOOL moveDrive(struct drive *pSrc, struct drive *pDst)
247 248 249 250 251 252 253 254 255
{
    WINE_TRACE("pSrc->letter == %c, pDst->letter == %c\n", pSrc->letter, pDst->letter);

    if(!copyDrive(pSrc, pDst))
    {
        WINE_TRACE("copyDrive failed\n");
        return FALSE;
    }

Mike Hearn's avatar
Mike Hearn committed
256
    delete_drive(pSrc);
257
    return TRUE;
258 259
}

Mike Hearn's avatar
Mike Hearn committed
260
#endif
261

Mike Hearn's avatar
Mike Hearn committed
262
/* Load currently defined drives into the drives array  */
263
void load_drives(void)
264
{
Mike Hearn's avatar
Mike Hearn committed
265 266 267 268 269
    char *devices, *dev;
    int len;
    int drivecount = 0, i;
    int retval;
    static const int arraysize = 512;
270 271
    const char *config_dir = wine_get_config_dir();
    char *path;
272

Mike Hearn's avatar
Mike Hearn committed
273
    WINE_TRACE("\n");
274

Mike Hearn's avatar
Mike Hearn committed
275 276 277
    /* setup the drives array */
    dev = devices = HeapAlloc(GetProcessHeap(), 0, arraysize);
    len = GetLogicalDriveStrings(arraysize, devices);
278

Mike Hearn's avatar
Mike Hearn committed
279 280
    /* make all devices unused */
    for (i = 0; i < 26; i++)
281
    {
Mike Hearn's avatar
Mike Hearn committed
282 283
        drives[i].letter = 'A' + i;
        drives[i].in_use = FALSE;
284 285 286 287 288 289 290 291 292

        HeapFree(GetProcessHeap(), 0, drives[i].unixpath);
        drives[i].unixpath = NULL;

        HeapFree(GetProcessHeap(), 0, drives[i].label);
        drives[i].label = NULL;

        HeapFree(GetProcessHeap(), 0, drives[i].serial);
        drives[i].serial = NULL;
293 294
    }

Mike Hearn's avatar
Mike Hearn committed
295 296
    /* work backwards through the result of GetLogicalDriveStrings  */
    while (len)
297
    {
Mike Hearn's avatar
Mike Hearn committed
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
        char volname[512]; /* volume name  */
        DWORD serial;
        char serialstr[256];
        char rootpath[256];
        char simplepath[3];
        int pathlen;
        char targetpath[256];
        char *c;

        *devices = toupper(*devices);

        WINE_TRACE("devices == '%s'\n", devices);

        volname[0] = 0;

        retval = GetVolumeInformation(devices,
                                      volname,
                                      sizeof(volname),
                                      &serial,
                                      NULL,
                                      NULL,
                                      NULL,
                                      0);
        if(!retval)
        {
            WINE_ERR("GetVolumeInformation() for '%s' failed, setting serial to 0\n", devices);
            PRINTERROR();
            serial = 0;
        }
327

328
        WINE_TRACE("serial: '0x%X'\n", serial);
329

Mike Hearn's avatar
Mike Hearn committed
330
        /* build rootpath for GetDriveType() */
331
        lstrcpynA(rootpath, devices, sizeof(rootpath));
Mike Hearn's avatar
Mike Hearn committed
332
        pathlen = strlen(rootpath);
333

Mike Hearn's avatar
Mike Hearn committed
334 335 336 337 338 339
        /* ensure that we have a backslash on the root path */
        if ((rootpath[pathlen - 1] != '\\') && (pathlen < sizeof(rootpath)))
        {
            rootpath[pathlen] = '\\';
            rootpath[pathlen + 1] = 0;
        }
340

341 342
	/* QueryDosDevice() requires no trailing backslash */
        lstrcpynA(simplepath, devices, 3);
Mike Hearn's avatar
Mike Hearn committed
343
        QueryDosDevice(simplepath, targetpath, sizeof(targetpath));
344

Mike Hearn's avatar
Mike Hearn committed
345 346 347
        /* targetpath may have forward slashes rather than backslashes, so correct */
        c = targetpath;
        do if (*c == '\\') *c = '/'; while (*c++);
348

349
        snprintf(serialstr, sizeof(serialstr), "%X", serial);
Mike Hearn's avatar
Mike Hearn committed
350
        WINE_TRACE("serialstr: '%s'\n", serialstr);
351
        add_drive(*devices, targetpath, volname, serialstr, get_drive_type(devices[0]) );
352

Mike Hearn's avatar
Mike Hearn committed
353 354
        len -= strlen(devices);
        devices += strlen(devices);
355

Mike Hearn's avatar
Mike Hearn committed
356 357 358 359 360 361
        /* skip over any nulls */
        while ((*devices == 0) && (len))
        {
            len--;
            devices++;
        }
362

Mike Hearn's avatar
Mike Hearn committed
363
        drivecount++;
364 365
    }

366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
    /* Find all the broken symlinks we might have and add them as well. */

    len = strlen(config_dir) + sizeof("/dosdevices/a:");
    if (!(path = HeapAlloc(GetProcessHeap(), 0, len)))
        return;

    strcpy(path, config_dir);
    strcat(path, "/dosdevices/a:");

    for (i = 0; i < 26; i++)
    {
        char buff[MAX_PATH];
        struct stat st;
        int cnt;

        if (drives[i].in_use) continue;
        path[len - 3] = 'a' + i;

        if (lstat(path, &st) == -1 || !S_ISLNK(st.st_mode)) continue;
        if ((cnt = readlink(path, buff, sizeof(buff))) == -1) continue;
        buff[cnt] = '\0';

        WINE_TRACE("found broken symlink %s -> %s\n", path, buff);
        add_drive('A' + i, buff, "", "0", DRIVE_UNKNOWN);

        drivecount++;
    }

Mike Hearn's avatar
Mike Hearn committed
394
    WINE_TRACE("found %d drives\n", drivecount);
395

396
    HeapFree(GetProcessHeap(), 0, path);
Mike Hearn's avatar
Mike Hearn committed
397
    HeapFree(GetProcessHeap(), 0, dev);
398 399
}

Mike Hearn's avatar
Mike Hearn committed
400 401
/* some of this code appears to be broken by bugs in Wine: the label
 * setting code has no effect, for instance  */
402
void apply_drive_changes(void)
403 404 405 406 407 408 409 410 411 412
{
    int i;
    CHAR devicename[4];
    CHAR targetpath[256];
    BOOL foundDrive;
    CHAR volumeNameBuffer[512];
    DWORD serialNumber;
    DWORD maxComponentLength;
    DWORD fileSystemFlags;
    CHAR fileSystemName[128];
413
    char newSerialNumberText[32];
414 415 416 417 418 419 420 421 422 423
    int retval;
    BOOL defineDevice;

    WINE_TRACE("\n");

    /* add each drive and remove as we go */
    for(i = 0; i < 26; i++)
    {
        defineDevice = FALSE;
        foundDrive = FALSE;
424 425
        volumeNameBuffer[0] = 0;
        serialNumber = 0;
Mike Hearn's avatar
Mike Hearn committed
426
        snprintf(devicename, sizeof(devicename), "%c:", 'A' + i);
427 428 429 430

        /* get a drive */
        if(QueryDosDevice(devicename, targetpath, sizeof(targetpath)))
        {
431 432 433 434 435
            char *cursor;
            
            /* correct the slashes in the path to be UNIX style */
            while ((cursor = strchr(targetpath, '\\'))) *cursor = '/';

436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
            foundDrive = TRUE;
        }

        /* if we found a drive and have a drive then compare things */
        if(foundDrive && drives[i].in_use)
        {
            WINE_TRACE("drives[i].letter: '%c'\n", drives[i].letter);

            snprintf(devicename, sizeof(devicename), "%c:\\", 'A' + i);
            retval = GetVolumeInformation(devicename,
                         volumeNameBuffer,
                         sizeof(volumeNameBuffer),
                         &serialNumber,
                         &maxComponentLength,
                         &fileSystemFlags,
                         fileSystemName,
                         sizeof(fileSystemName));
            if(!retval)
            {
                WINE_TRACE("  GetVolumeInformation() for '%s' failed\n", devicename);
                PRINTERROR();
457
                volumeNameBuffer[0] = '\0';
458 459 460 461 462 463 464
            }

            WINE_TRACE("  current path:   '%s', new path:   '%s'\n",
                       targetpath, drives[i].unixpath);

            /* compare to what we have */
            /* do we have the same targetpath? */
465
            if(strcmp(drives[i].unixpath, targetpath))
466 467 468
            {
                defineDevice = TRUE;
                WINE_TRACE("  making changes to drive '%s'\n", devicename);
Mike Hearn's avatar
Mike Hearn committed
469 470
            }
            else
471 472 473
            {
                WINE_TRACE("  no changes to drive '%s'\n", devicename);
            }
Mike Hearn's avatar
Mike Hearn committed
474 475
        }
        else if(foundDrive && !drives[i].in_use)
476 477 478 479 480 481 482
        {
            /* remove this drive */
            if(!DefineDosDevice(DDD_REMOVE_DEFINITION, devicename, drives[i].unixpath))
            {
                WINE_ERR("unable to remove devicename of '%s', targetpath of '%s'\n",
                    devicename, drives[i].unixpath);
                PRINTERROR();
Mike Hearn's avatar
Mike Hearn committed
483 484
            }
            else
485 486 487 488
            {
                WINE_TRACE("removed devicename of '%s', targetpath of '%s'\n",
                           devicename, drives[i].unixpath);
            }
489

490 491
            set_drive_type( drives[i].letter, DRIVE_UNKNOWN );
            continue;
Mike Hearn's avatar
Mike Hearn committed
492 493
        }
        else if(drives[i].in_use) /* foundDrive must be false from the above check */
494 495 496 497 498 499 500 501 502
        {
            defineDevice = TRUE;
        }

        /* adding and modifying are the same steps */
        if(defineDevice)
        {
            /* define this drive */
            /* DefineDosDevice() requires that NO trailing slash be present */
Mike Hearn's avatar
Mike Hearn committed
503
            snprintf(devicename, sizeof(devicename), "%c:", 'A' + i);
504 505 506 507 508
            if(!DefineDosDevice(DDD_RAW_TARGET_PATH, devicename, drives[i].unixpath))
            {
                WINE_ERR("  unable to define devicename of '%s', targetpath of '%s'\n",
                    devicename, drives[i].unixpath);
                PRINTERROR();
Mike Hearn's avatar
Mike Hearn committed
509 510
            }
            else
511 512 513 514
            {
                WINE_TRACE("  added devicename of '%s', targetpath of '%s'\n",
                           devicename, drives[i].unixpath);
            }
515
        }
516

517 518
        if (drives[i].label && strcmp(drives[i].label, volumeNameBuffer))
            set_drive_label( drives[i].letter, drives[i].label );
519

520
        snprintf(newSerialNumberText, sizeof(newSerialNumberText), "%X", serialNumber);
521 522
        if (drives[i].serial && drives[i].serial[0] && strcmp(drives[i].serial, newSerialNumberText))
            set_drive_serial( drives[i].letter, drives[i].serial );
523 524

        set_drive_type( drives[i].letter, drives[i].type );
525
    }
526
}