drive.c 44.5 KB
Newer Older
Alexandre Julliard's avatar
Alexandre Julliard committed
1
/*
Alexandre Julliard's avatar
Alexandre Julliard committed
2
 * DOS drives handling functions
Alexandre Julliard's avatar
Alexandre Julliard committed
3 4 5
 *
 * Copyright 1993 Erik Bos
 * Copyright 1996 Alexandre Julliard
6 7 8 9 10
 *
 * Label & serial number read support.
 *  (c) 1999 Petr Tomasek <tomasek@etf.cuni.cz>
 *  (c) 2000 Andreas Mohr (changes)
 *
Alexandre Julliard's avatar
Alexandre Julliard committed
11 12
 */

Alexandre Julliard's avatar
Alexandre Julliard committed
13 14
#include "config.h"

Alexandre Julliard's avatar
Alexandre Julliard committed
15
#include <assert.h>
Alexandre Julliard's avatar
Alexandre Julliard committed
16
#include <ctype.h>
Alexandre Julliard's avatar
Alexandre Julliard committed
17
#include <string.h>
18
#include <stdio.h>
Alexandre Julliard's avatar
Alexandre Julliard committed
19
#include <stdlib.h>
Alexandre Julliard's avatar
Alexandre Julliard committed
20
#include <sys/types.h>
Alexandre Julliard's avatar
Alexandre Julliard committed
21
#include <sys/stat.h>
Alexandre Julliard's avatar
Alexandre Julliard committed
22
#include <fcntl.h>
Alexandre Julliard's avatar
Alexandre Julliard committed
23
#include <errno.h>
24
#include <unistd.h>
Alexandre Julliard's avatar
Alexandre Julliard committed
25

Alexandre Julliard's avatar
Alexandre Julliard committed
26 27
#ifdef HAVE_SYS_PARAM_H
# include <sys/param.h>
Alexandre Julliard's avatar
Alexandre Julliard committed
28
#endif
Alexandre Julliard's avatar
Alexandre Julliard committed
29 30 31 32 33 34 35 36 37 38
#ifdef STATFS_DEFINED_BY_SYS_VFS
# include <sys/vfs.h>
#else
# ifdef STATFS_DEFINED_BY_SYS_MOUNT
#  include <sys/mount.h>
# else
#  ifdef STATFS_DEFINED_BY_SYS_STATFS
#   include <sys/statfs.h>
#  endif
# endif
Alexandre Julliard's avatar
Alexandre Julliard committed
39
#endif
Alexandre Julliard's avatar
Alexandre Julliard committed
40

Alexandre Julliard's avatar
Alexandre Julliard committed
41
#include "winbase.h"
42
#include "ntddk.h"
43 44
#include "wine/winbase16.h"   /* for GetCurrentTask */
#include "wine/winestring.h"  /* for lstrcpyAtoW */
45
#include "winerror.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
46
#include "drive.h"
47
#include "cdrom.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
48
#include "file.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
49
#include "heap.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
50
#include "msdos.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
51
#include "options.h"
52
#include "wine/port.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
53
#include "task.h"
54
#include "debugtools.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
55

56
DEFAULT_DEBUG_CHANNEL(dosfs)
57 58
DECLARE_DEBUG_CHANNEL(file)

Alexandre Julliard's avatar
Alexandre Julliard committed
59 60
typedef struct
{
Alexandre Julliard's avatar
Alexandre Julliard committed
61 62 63
    char     *root;      /* root dir in Unix format without trailing / */
    char     *dos_cwd;   /* cwd in DOS format without leading or trailing \ */
    char     *unix_cwd;  /* cwd in Unix format without leading or trailing / */
Alexandre Julliard's avatar
Alexandre Julliard committed
64
    char     *device;    /* raw device path */
65 66 67
    char      label_conf[12]; /* drive label as cfg'd in wine.conf */
    char      label_read[12]; /* drive label as read from device */
    DWORD     serial_conf;    /* drive serial number as cfg'd in wine.conf */
Alexandre Julliard's avatar
Alexandre Julliard committed
68
    DRIVETYPE type;      /* drive type */
69
    UINT      flags;     /* drive flags */
Alexandre Julliard's avatar
Alexandre Julliard committed
70 71
    dev_t     dev;       /* unix device number */
    ino_t     ino;       /* unix inode number */
Alexandre Julliard's avatar
Alexandre Julliard committed
72 73
} DOSDRIVE;

Alexandre Julliard's avatar
Alexandre Julliard committed
74

Alexandre Julliard's avatar
Alexandre Julliard committed
75
static const char * const DRIVE_Types[] =
Alexandre Julliard's avatar
Alexandre Julliard committed
76 77 78 79 80 81 82 83
{
    "floppy",   /* TYPE_FLOPPY */
    "hd",       /* TYPE_HD */
    "cdrom",    /* TYPE_CDROM */
    "network"   /* TYPE_NETWORK */
};


Alexandre Julliard's avatar
Alexandre Julliard committed
84 85 86 87 88
/* Known filesystem types */

typedef struct
{
    const char *name;
89
    UINT      flags;
Alexandre Julliard's avatar
Alexandre Julliard committed
90 91 92 93 94 95 96 97 98 99 100 101 102 103
} FS_DESCR;

static const FS_DESCR DRIVE_Filesystems[] =
{
    { "unix",   DRIVE_CASE_SENSITIVE | DRIVE_CASE_PRESERVING },
    { "msdos",  DRIVE_SHORT_NAMES },
    { "dos",    DRIVE_SHORT_NAMES },
    { "fat",    DRIVE_SHORT_NAMES },
    { "vfat",   DRIVE_CASE_PRESERVING },
    { "win95",  DRIVE_CASE_PRESERVING },
    { NULL, 0 }
};


Alexandre Julliard's avatar
Alexandre Julliard committed
104
static DOSDRIVE DOSDrives[MAX_DOS_DRIVES];
Alexandre Julliard's avatar
Alexandre Julliard committed
105
static int DRIVE_CurDrive = -1;
Alexandre Julliard's avatar
Alexandre Julliard committed
106

Alexandre Julliard's avatar
Alexandre Julliard committed
107
static HTASK16 DRIVE_LastTask = 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
108

Alexandre Julliard's avatar
Alexandre Julliard committed
109 110 111 112 113 114 115 116 117 118 119 120

/***********************************************************************
 *           DRIVE_GetDriveType
 */
static DRIVETYPE DRIVE_GetDriveType( const char *name )
{
    char buffer[20];
    int i;

    PROFILE_GetWineIniString( name, "Type", "hd", buffer, sizeof(buffer) );
    for (i = 0; i < sizeof(DRIVE_Types)/sizeof(DRIVE_Types[0]); i++)
    {
Alexandre Julliard's avatar
Alexandre Julliard committed
121
        if (!strcasecmp( buffer, DRIVE_Types[i] )) return (DRIVETYPE)i;
Alexandre Julliard's avatar
Alexandre Julliard committed
122
    }
123 124
    MESSAGE("%s: unknown drive type '%s', defaulting to 'hd'.\n",
	name, buffer );
Alexandre Julliard's avatar
Alexandre Julliard committed
125 126 127 128
    return TYPE_HD;
}


Alexandre Julliard's avatar
Alexandre Julliard committed
129 130 131
/***********************************************************************
 *           DRIVE_GetFSFlags
 */
132
static UINT DRIVE_GetFSFlags( const char *name, const char *value )
Alexandre Julliard's avatar
Alexandre Julliard committed
133 134 135 136
{
    const FS_DESCR *descr;

    for (descr = DRIVE_Filesystems; descr->name; descr++)
Alexandre Julliard's avatar
Alexandre Julliard committed
137
        if (!strcasecmp( value, descr->name )) return descr->flags;
138
    MESSAGE("%s: unknown filesystem type '%s', defaulting to 'win95'.\n",
Alexandre Julliard's avatar
Alexandre Julliard committed
139
	name, value );
140
    return DRIVE_CASE_PRESERVING;
Alexandre Julliard's avatar
Alexandre Julliard committed
141 142 143
}


Alexandre Julliard's avatar
Alexandre Julliard committed
144 145 146 147 148
/***********************************************************************
 *           DRIVE_Init
 */
int DRIVE_Init(void)
{
Alexandre Julliard's avatar
Alexandre Julliard committed
149 150
    int i, len, count = 0;
    char name[] = "Drive A";
151
    char drive_env[] = "=A:";
Alexandre Julliard's avatar
Alexandre Julliard committed
152
    char path[MAX_PATHNAME_LEN];
Alexandre Julliard's avatar
Alexandre Julliard committed
153
    char buffer[80];
Alexandre Julliard's avatar
Alexandre Julliard committed
154
    struct stat drive_stat_buffer;
Alexandre Julliard's avatar
Alexandre Julliard committed
155
    char *p;
Alexandre Julliard's avatar
Alexandre Julliard committed
156
    DOSDRIVE *drive;
Alexandre Julliard's avatar
Alexandre Julliard committed
157

Alexandre Julliard's avatar
Alexandre Julliard committed
158
    for (i = 0, drive = DOSDrives; i < MAX_DOS_DRIVES; i++, name[6]++, drive++)
Alexandre Julliard's avatar
Alexandre Julliard committed
159
    {
Alexandre Julliard's avatar
Alexandre Julliard committed
160
        PROFILE_GetWineIniString( name, "Path", "", path, sizeof(path)-1 );
Alexandre Julliard's avatar
Alexandre Julliard committed
161 162 163 164
        if (path[0])
        {
            p = path + strlen(path) - 1;
            while ((p > path) && ((*p == '/') || (*p == '\\'))) *p-- = '\0';
Alexandre Julliard's avatar
Alexandre Julliard committed
165 166 167 168
            if (!path[0]) strcpy( path, "/" );

            if (stat( path, &drive_stat_buffer ))
            {
169
                MESSAGE("Could not stat %s, ignoring drive %c:\n", path, 'A' + i );
Alexandre Julliard's avatar
Alexandre Julliard committed
170 171 172 173
                continue;
            }
            if (!S_ISDIR(drive_stat_buffer.st_mode))
            {
174
                MESSAGE("%s is not a directory, ignoring drive %c:\n",
Alexandre Julliard's avatar
Alexandre Julliard committed
175
		    path, 'A' + i );
Alexandre Julliard's avatar
Alexandre Julliard committed
176 177 178
                continue;
            }

179 180 181
            drive->root = HEAP_strdupA( GetProcessHeap(), 0, path );
            drive->dos_cwd  = HEAP_strdupA( GetProcessHeap(), 0, "" );
            drive->unix_cwd = HEAP_strdupA( GetProcessHeap(), 0, "" );
Alexandre Julliard's avatar
Alexandre Julliard committed
182
            drive->type     = DRIVE_GetDriveType( name );
Alexandre Julliard's avatar
Alexandre Julliard committed
183
            drive->device   = NULL;
Alexandre Julliard's avatar
Alexandre Julliard committed
184
            drive->flags    = 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
185 186
            drive->dev      = drive_stat_buffer.st_dev;
            drive->ino      = drive_stat_buffer.st_ino;
Alexandre Julliard's avatar
Alexandre Julliard committed
187 188

            /* Get the drive label */
189 190
            PROFILE_GetWineIniString( name, "Label", name, drive->label_conf, 12 );
            if ((len = strlen(drive->label_conf)) < 11)
Alexandre Julliard's avatar
Alexandre Julliard committed
191 192
            {
                /* Pad label with spaces */
193 194
                memset( drive->label_conf + len, ' ', 11 - len );
                drive->label_conf[11] = '\0';
Alexandre Julliard's avatar
Alexandre Julliard committed
195 196 197 198 199
            }

            /* Get the serial number */
            PROFILE_GetWineIniString( name, "Serial", "12345678",
                                      buffer, sizeof(buffer) );
200
            drive->serial_conf = strtoul( buffer, NULL, 16 );
Alexandre Julliard's avatar
Alexandre Julliard committed
201

Alexandre Julliard's avatar
Alexandre Julliard committed
202
            /* Get the filesystem type */
203
            PROFILE_GetWineIniString( name, "Filesystem", "win95",
Alexandre Julliard's avatar
Alexandre Julliard committed
204 205 206
                                      buffer, sizeof(buffer) );
            drive->flags = DRIVE_GetFSFlags( name, buffer );

Alexandre Julliard's avatar
Alexandre Julliard committed
207 208 209 210
            /* Get the device */
            PROFILE_GetWineIniString( name, "Device", "",
                                      buffer, sizeof(buffer) );
            if (buffer[0])
211
	    {
212
                drive->device = HEAP_strdupA( GetProcessHeap(), 0, buffer );
213 214
		if (PROFILE_GetWineIniBool( name, "ReadVolInfo", 1))
                    drive->flags |= DRIVE_READ_VOL_INFO;
215
	    }
216 217 218 219

            /* Get the FailReadOnly flag */
            if (PROFILE_GetWineIniBool( name, "FailReadOnly", 0 ))
                drive->flags |= DRIVE_FAIL_READ_ONLY;
Alexandre Julliard's avatar
Alexandre Julliard committed
220

Alexandre Julliard's avatar
Alexandre Julliard committed
221 222 223 224
            /* Make the first hard disk the current drive */
            if ((DRIVE_CurDrive == -1) && (drive->type == TYPE_HD))
                DRIVE_CurDrive = i;

Alexandre Julliard's avatar
Alexandre Julliard committed
225
            count++;
226 227 228
            TRACE("%s: path=%s type=%s label='%s' serial=%08lx "
                  "flags=%08x dev=%x ino=%x\n",
                  name, path, DRIVE_Types[drive->type],
229
                  drive->label_conf, drive->serial_conf, drive->flags,
230
                  (int)drive->dev, (int)drive->ino );
Alexandre Julliard's avatar
Alexandre Julliard committed
231
        }
232
        else WARN("%s: not defined\n", name );
Alexandre Julliard's avatar
Alexandre Julliard committed
233 234 235 236
    }

    if (!count) 
    {
237
        MESSAGE("Warning: no valid DOS drive found, check your configuration file.\n" );
Alexandre Julliard's avatar
Alexandre Julliard committed
238
        /* Create a C drive pointing to Unix root dir */
239 240 241
        DOSDrives[2].root     = HEAP_strdupA( GetProcessHeap(), 0, "/" );
        DOSDrives[2].dos_cwd  = HEAP_strdupA( GetProcessHeap(), 0, "" );
        DOSDrives[2].unix_cwd = HEAP_strdupA( GetProcessHeap(), 0, "" );
242 243
        strcpy( DOSDrives[2].label_conf, "Drive C    " );
        DOSDrives[2].serial_conf   = 12345678;
Alexandre Julliard's avatar
Alexandre Julliard committed
244
        DOSDrives[2].type     = TYPE_HD;
245
        DOSDrives[2].device   = NULL;
Alexandre Julliard's avatar
Alexandre Julliard committed
246
        DOSDrives[2].flags    = 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
247
        DRIVE_CurDrive = 2;
Alexandre Julliard's avatar
Alexandre Julliard committed
248 249
    }

Alexandre Julliard's avatar
Alexandre Julliard committed
250 251
    /* Make sure the current drive is valid */
    if (DRIVE_CurDrive == -1)
Alexandre Julliard's avatar
Alexandre Julliard committed
252
    {
Alexandre Julliard's avatar
Alexandre Julliard committed
253
        for (i = 0, drive = DOSDrives; i < MAX_DOS_DRIVES; i++, drive++)
Alexandre Julliard's avatar
Alexandre Julliard committed
254
        {
Alexandre Julliard's avatar
Alexandre Julliard committed
255
            if (drive->root && !(drive->flags & DRIVE_DISABLED))
Alexandre Julliard's avatar
Alexandre Julliard committed
256 257 258 259
            {
                DRIVE_CurDrive = i;
                break;
            }
Alexandre Julliard's avatar
Alexandre Julliard committed
260 261
        }
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
262

263 264 265 266 267 268 269 270
    /* get current working directory info for all drives */
    for (i = 0; i < MAX_DOS_DRIVES; i++, drive_env[1]++)
    {
        if (!GetEnvironmentVariableA(drive_env, path, sizeof(path))) continue;
        /* sanity check */
        if (toupper(path[0]) != drive_env[1] || path[1] != ':') continue;
        DRIVE_Chdir( i, path + 2 );
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
271 272 273 274 275 276 277 278 279 280
    return 1;
}


/***********************************************************************
 *           DRIVE_IsValid
 */
int DRIVE_IsValid( int drive )
{
    if ((drive < 0) || (drive >= MAX_DOS_DRIVES)) return 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
281 282
    return (DOSDrives[drive].root &&
            !(DOSDrives[drive].flags & DRIVE_DISABLED));
Alexandre Julliard's avatar
Alexandre Julliard committed
283 284 285 286 287 288 289 290
}


/***********************************************************************
 *           DRIVE_GetCurrentDrive
 */
int DRIVE_GetCurrentDrive(void)
{
Alexandre Julliard's avatar
Alexandre Julliard committed
291
    TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
Alexandre Julliard's avatar
Alexandre Julliard committed
292 293 294 295 296 297 298 299 300 301
    if (pTask && (pTask->curdrive & 0x80)) return pTask->curdrive & ~0x80;
    return DRIVE_CurDrive;
}


/***********************************************************************
 *           DRIVE_SetCurrentDrive
 */
int DRIVE_SetCurrentDrive( int drive )
{
Alexandre Julliard's avatar
Alexandre Julliard committed
302
    TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
Alexandre Julliard's avatar
Alexandre Julliard committed
303 304
    if (!DRIVE_IsValid( drive ))
    {
305
        SetLastError( ERROR_INVALID_DRIVE );
Alexandre Julliard's avatar
Alexandre Julliard committed
306 307
        return 0;
    }
308
    TRACE("%c:\n", 'A' + drive );
Alexandre Julliard's avatar
Alexandre Julliard committed
309 310
    DRIVE_CurDrive = drive;
    if (pTask) pTask->curdrive = drive | 0x80;
311
    chdir(DRIVE_GetUnixCwd(drive));
Alexandre Julliard's avatar
Alexandre Julliard committed
312 313 314 315 316 317 318
    return 1;
}


/***********************************************************************
 *           DRIVE_FindDriveRoot
 *
319
 * Find a drive for which the root matches the beginning of the given path.
Alexandre Julliard's avatar
Alexandre Julliard committed
320 321 322 323 324 325
 * This can be used to translate a Unix path into a drive + DOS path.
 * Return value is the drive, or -1 on error. On success, path is modified
 * to point to the beginning of the DOS path.
 */
int DRIVE_FindDriveRoot( const char **path )
{
Alexandre Julliard's avatar
Alexandre Julliard committed
326 327 328 329 330 331
    /* idea: check at all '/' positions.
     * If the device and inode of that path is identical with the
     * device and inode of the current drive then we found a solution.
     * If there is another drive pointing to a deeper position in
     * the file tree, we want to find that one, not the earlier solution.
     */
Alexandre Julliard's avatar
Alexandre Julliard committed
332
    int drive, rootdrive = -1;
Alexandre Julliard's avatar
Alexandre Julliard committed
333 334 335 336
    char buffer[MAX_PATHNAME_LEN];
    char *next = buffer;
    const char *p = *path;
    struct stat st;
Alexandre Julliard's avatar
Alexandre Julliard committed
337

Alexandre Julliard's avatar
Alexandre Julliard committed
338 339
    strcpy( buffer, "/" );
    for (;;)
Alexandre Julliard's avatar
Alexandre Julliard committed
340
    {
Alexandre Julliard's avatar
Alexandre Julliard committed
341 342 343 344 345
        if (stat( buffer, &st ) || !S_ISDIR( st.st_mode )) break;

        /* Find the drive */

        for (drive = 0; drive < MAX_DOS_DRIVES; drive++)
Alexandre Julliard's avatar
Alexandre Julliard committed
346
        {
Alexandre Julliard's avatar
Alexandre Julliard committed
347 348 349 350 351 352 353 354
           if (!DOSDrives[drive].root ||
               (DOSDrives[drive].flags & DRIVE_DISABLED)) continue;

           if ((DOSDrives[drive].dev == st.st_dev) &&
               (DOSDrives[drive].ino == st.st_ino))
           {
               rootdrive = drive;
               *path = p;
355
	       break;
Alexandre Julliard's avatar
Alexandre Julliard committed
356
           }
Alexandre Julliard's avatar
Alexandre Julliard committed
357
        }
Alexandre Julliard's avatar
Alexandre Julliard committed
358 359 360 361 362 363 364 365

        /* Get the next path component */

        *next++ = '/';
        while ((*p == '/') || (*p == '\\')) p++;
        if (!*p) break;
        while (!IS_END_OF_NAME(*p)) *next++ = *p++;
        *next = 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
366
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
367
    *next = 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
368 369

    if (rootdrive != -1)
370 371
        TRACE("%s -> drive %c:, root='%s', name='%s'\n",
              buffer, 'A' + rootdrive, DOSDrives[rootdrive].root, *path );
Alexandre Julliard's avatar
Alexandre Julliard committed
372
    return rootdrive;
Alexandre Julliard's avatar
Alexandre Julliard committed
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
}


/***********************************************************************
 *           DRIVE_GetRoot
 */
const char * DRIVE_GetRoot( int drive )
{
    if (!DRIVE_IsValid( drive )) return NULL;
    return DOSDrives[drive].root;
}


/***********************************************************************
 *           DRIVE_GetDosCwd
 */
const char * DRIVE_GetDosCwd( int drive )
{
Alexandre Julliard's avatar
Alexandre Julliard committed
391
    TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
Alexandre Julliard's avatar
Alexandre Julliard committed
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
    if (!DRIVE_IsValid( drive )) return NULL;

    /* Check if we need to change the directory to the new task. */
    if (pTask && (pTask->curdrive & 0x80) &&    /* The task drive is valid */
        ((pTask->curdrive & ~0x80) == drive) && /* and it's the one we want */
        (DRIVE_LastTask != GetCurrentTask()))   /* and the task changed */
    {
        /* Perform the task-switch */
        if (!DRIVE_Chdir( drive, pTask->curdir )) DRIVE_Chdir( drive, "\\" );
        DRIVE_LastTask = GetCurrentTask();
    }
    return DOSDrives[drive].dos_cwd;
}


/***********************************************************************
 *           DRIVE_GetUnixCwd
 */
const char * DRIVE_GetUnixCwd( int drive )
{
Alexandre Julliard's avatar
Alexandre Julliard committed
412
    TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
Alexandre Julliard's avatar
Alexandre Julliard committed
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
    if (!DRIVE_IsValid( drive )) return NULL;

    /* Check if we need to change the directory to the new task. */
    if (pTask && (pTask->curdrive & 0x80) &&    /* The task drive is valid */
        ((pTask->curdrive & ~0x80) == drive) && /* and it's the one we want */
        (DRIVE_LastTask != GetCurrentTask()))   /* and the task changed */
    {
        /* Perform the task-switch */
        if (!DRIVE_Chdir( drive, pTask->curdir )) DRIVE_Chdir( drive, "\\" );
        DRIVE_LastTask = GetCurrentTask();
    }
    return DOSDrives[drive].unix_cwd;
}


428 429 430 431 432 433 434 435 436 437 438 439
/***********************************************************************
 *           DRIVE_GetDevice
 */
const char * DRIVE_GetDevice( int drive )
{
    return (DRIVE_IsValid( drive )) ? DOSDrives[drive].device : NULL;
}


/***********************************************************************
 *           DRIVE_ReadSuperblock
 *
440 441 442
 * NOTE 
 *      DRIVE_SetLabel and DRIVE_SetSerialNumber use this in order
 * to check, that they are writing on a FAT filesystem !
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
 */
int DRIVE_ReadSuperblock (int drive, char * buff)
{
#define DRIVE_SUPER 96
    int fd;
    off_t offs;

    if (memset(buff,0,DRIVE_SUPER)!=buff) return -1;
    if ((fd=open(DOSDrives[drive].device,O_RDONLY)) == -1)
    {
	struct stat st;
	if (!DOSDrives[drive].device)
	    ERR("No device configured for drive %c: !\n", 'A'+drive);
	else
	    ERR("Couldn't open device '%s' for drive %c: ! (%s)\n", DOSDrives[drive].device, 'A'+drive,
		 (stat(DOSDrives[drive].device, &st)) ?
			"not available or symlink not valid ?" : "no permission");
	ERR("Can't read drive volume info ! Either pre-set it or make sure the device to read it from is accessible !\n");
	PROFILE_UsageWineIni();
	return -1;
    }

    switch(DOSDrives[drive].type)
    {
	case TYPE_FLOPPY:
	case TYPE_HD:
	    offs = 0;
	    break;
	case TYPE_CDROM:
472
	    offs = CDROM_Data_FindBestVoldesc(fd);
473 474 475 476 477 478 479 480 481 482 483 484 485
	    break;
		default:
		    offs = 0;
		    break;
    }

    if ((offs) && (lseek(fd,offs,SEEK_SET)!=offs)) return -4;
    if (read(fd,buff,DRIVE_SUPER)!=DRIVE_SUPER) return -2;

    switch(DOSDrives[drive].type)
    {
	case TYPE_FLOPPY:
	case TYPE_HD:
486
	    if ((buff[0x26]!=0x29) ||  /* Check for FAT present */
487
                /* FIXME: do really all FAT have their name beginning with
488 489 490 491 492 493 494 495
                   "FAT" ? (At least FAT12, FAT16 and FAT32 have :) */
	    	memcmp( buff+0x36,"FAT",3))
            {
                ERR("The filesystem is not FAT !! (device=%s)\n",
                    DOSDrives[drive].device);
                return -3;
            }
            break;
496 497 498 499 500 501 502 503 504 505 506 507 508 509
	case TYPE_CDROM:
	    if (strncmp(&buff[1],"CD001",5)) /* Check for iso9660 present */
		return -3;
	    /* FIXME: do we need to check for "CDROM", too ? (high sierra) */
		break;
	default:
		return -3;
		break;
    }

    return close(fd);
}


510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545
/***********************************************************************
 *           DRIVE_WriteSuperblockEntry
 *
 * NOTE
 *	We are writing as little as possible (ie. not the whole SuperBlock)
 * not to interfere with kernel. The drive can be mounted !
 */
int DRIVE_WriteSuperblockEntry (int drive, off_t ofs, size_t len, char * buff)
{
    int fd;

    if ((fd=open(DOSDrives[drive].device,O_WRONLY))==-1) 
    {
        ERR("Cannot open the device %s (for writing)\n",
            DOSDrives[drive].device); 
        return -1;
    }
    if (lseek(fd,ofs,SEEK_SET)!=ofs)
    {
        ERR("lseek failed on device %s !\n",
            DOSDrives[drive].device); 
        close(fd);
        return -2;
    }
    if (write(fd,buff,len)!=len) 
    {
        close(fd);
        ERR("Cannot write on %s !\n",
            DOSDrives[drive].device); 
        return -3;
    }
    return close (fd);
}



Alexandre Julliard's avatar
Alexandre Julliard committed
546 547 548 549 550
/***********************************************************************
 *           DRIVE_GetLabel
 */
const char * DRIVE_GetLabel( int drive )
{
551 552 553 554
    int read = 0;
    char buff[DRIVE_SUPER];
    int offs = -1;

Alexandre Julliard's avatar
Alexandre Julliard committed
555
    if (!DRIVE_IsValid( drive )) return NULL;
556 557
    if (DRIVE_GetType(drive) == TYPE_CDROM)
    {
558
	read = CDROM_GetLabel(drive, DOSDrives[drive].label_read); 
559
    }
560 561
    else
    if (DOSDrives[drive].flags & DRIVE_READ_VOL_INFO)
562 563 564 565 566 567 568 569 570
    {
	if (DRIVE_ReadSuperblock(drive,(char *) buff))
	    ERR("Invalid or unreadable superblock on %s (%c:).\n",
		DOSDrives[drive].device, (char)(drive+'A'));
	else {
	    if (DOSDrives[drive].type == TYPE_FLOPPY ||
		DOSDrives[drive].type == TYPE_HD)
		offs = 0x2b;

571
	    /* FIXME: ISO9660 uses a 32 bytes long label. Should we do also? */
572 573 574 575 576 577 578 579
	    if (offs != -1) memcpy(DOSDrives[drive].label_read,buff+offs,11);
	    DOSDrives[drive].label_read[11]='\0';
	    read = 1;
	}
    }

    return (read) ?
	DOSDrives[drive].label_read : DOSDrives[drive].label_conf;
Alexandre Julliard's avatar
Alexandre Julliard committed
580 581 582 583 584 585 586 587
}


/***********************************************************************
 *           DRIVE_GetSerialNumber
 */
DWORD DRIVE_GetSerialNumber( int drive )
{
588
    DWORD serial = 0;
589 590
char buff[DRIVE_SUPER];

Alexandre Julliard's avatar
Alexandre Julliard committed
591
    if (!DRIVE_IsValid( drive )) return 0;
592
    
593
    if (DOSDrives[drive].flags & DRIVE_READ_VOL_INFO)
594
    {
595 596 597 598
	switch(DOSDrives[drive].type)
	{
	    case TYPE_FLOPPY:
	    case TYPE_HD:
599 600
      if (DRIVE_ReadSuperblock(drive,(char *) buff))
        MESSAGE("Invalid or unreadable superblock on %s (%c:)."
601 602
			" Maybe not FAT?\n" ,
			DOSDrives[drive].device, 'A'+drive);
603
      else
604 605 606 607 608 609 610 611
		    serial = *((DWORD*)(buff+0x27));
		break;
	    case TYPE_CDROM:
		serial = CDROM_GetSerial(drive);
		break;
	    default:
	        FIXME("Serial number reading from file system on drive %c: not supported yet.\n", drive+'A');
	}
612
    }
613 614
		
    return (serial) ? serial : DOSDrives[drive].serial_conf;
Alexandre Julliard's avatar
Alexandre Julliard committed
615 616 617 618 619 620 621 622
}


/***********************************************************************
 *           DRIVE_SetSerialNumber
 */
int DRIVE_SetSerialNumber( int drive, DWORD serial )
{
623 624
    char buff[DRIVE_SUPER];

Alexandre Julliard's avatar
Alexandre Julliard committed
625
    if (!DRIVE_IsValid( drive )) return 0;
626

627
    if (DOSDrives[drive].flags & DRIVE_READ_VOL_INFO)
628 629 630 631 632 633 634 635 636 637
    {
        if ((DOSDrives[drive].type != TYPE_FLOPPY) &&
            (DOSDrives[drive].type != TYPE_HD)) return 0;
        /* check, if the drive has a FAT filesystem */ 
        if (DRIVE_ReadSuperblock(drive, buff)) return 0;
        if (DRIVE_WriteSuperblockEntry(drive, 0x27, 4, (char *) &serial)) return 0;
        return 1;
    }

    if (DOSDrives[drive].type == TYPE_CDROM) return 0;
638
    DOSDrives[drive].serial_conf = serial;
Alexandre Julliard's avatar
Alexandre Julliard committed
639 640 641 642
    return 1;
}


Alexandre Julliard's avatar
Alexandre Julliard committed
643 644 645 646 647 648 649 650 651 652
/***********************************************************************
 *           DRIVE_GetType
 */
DRIVETYPE DRIVE_GetType( int drive )
{
    if (!DRIVE_IsValid( drive )) return TYPE_INVALID;
    return DOSDrives[drive].type;
}


Alexandre Julliard's avatar
Alexandre Julliard committed
653 654 655
/***********************************************************************
 *           DRIVE_GetFlags
 */
656
UINT DRIVE_GetFlags( int drive )
Alexandre Julliard's avatar
Alexandre Julliard committed
657 658 659 660 661 662
{
    if ((drive < 0) || (drive >= MAX_DOS_DRIVES)) return 0;
    return DOSDrives[drive].flags;
}


Alexandre Julliard's avatar
Alexandre Julliard committed
663 664 665 666 667
/***********************************************************************
 *           DRIVE_Chdir
 */
int DRIVE_Chdir( int drive, const char *path )
{
Alexandre Julliard's avatar
Alexandre Julliard committed
668
    DOS_FULL_NAME full_name;
Alexandre Julliard's avatar
Alexandre Julliard committed
669
    char buffer[MAX_PATHNAME_LEN];
Alexandre Julliard's avatar
Alexandre Julliard committed
670
    LPSTR unix_cwd;
Alexandre Julliard's avatar
Alexandre Julliard committed
671
    BY_HANDLE_FILE_INFORMATION info;
Alexandre Julliard's avatar
Alexandre Julliard committed
672
    TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
Alexandre Julliard's avatar
Alexandre Julliard committed
673 674 675

    strcpy( buffer, "A:" );
    buffer[0] += drive;
676
    TRACE("(%s,%s)\n", buffer, path );
677
    lstrcpynA( buffer + 2, path, sizeof(buffer) - 2 );
Alexandre Julliard's avatar
Alexandre Julliard committed
678

Alexandre Julliard's avatar
Alexandre Julliard committed
679 680
    if (!DOSFS_GetFullName( buffer, TRUE, &full_name )) return 0;
    if (!FILE_Stat( full_name.long_name, &info )) return 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
681
    if (!(info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
Alexandre Julliard's avatar
Alexandre Julliard committed
682
    {
683
        SetLastError( ERROR_FILE_NOT_FOUND );
Alexandre Julliard's avatar
Alexandre Julliard committed
684 685
        return 0;
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
686
    unix_cwd = full_name.long_name + strlen( DOSDrives[drive].root );
Alexandre Julliard's avatar
Alexandre Julliard committed
687 688
    while (*unix_cwd == '/') unix_cwd++;

689
    TRACE("(%c:): unix_cwd=%s dos_cwd=%s\n",
Alexandre Julliard's avatar
Alexandre Julliard committed
690
                   'A' + drive, unix_cwd, full_name.short_name + 3 );
Alexandre Julliard's avatar
Alexandre Julliard committed
691

692 693 694
    HeapFree( GetProcessHeap(), 0, DOSDrives[drive].dos_cwd );
    HeapFree( GetProcessHeap(), 0, DOSDrives[drive].unix_cwd );
    DOSDrives[drive].dos_cwd  = HEAP_strdupA( GetProcessHeap(), 0,
Alexandre Julliard's avatar
Alexandre Julliard committed
695
                                              full_name.short_name + 3 );
696
    DOSDrives[drive].unix_cwd = HEAP_strdupA( GetProcessHeap(), 0, unix_cwd );
Alexandre Julliard's avatar
Alexandre Julliard committed
697 698 699 700

    if (pTask && (pTask->curdrive & 0x80) && 
        ((pTask->curdrive & ~0x80) == drive))
    {
701
        lstrcpynA( pTask->curdir, full_name.short_name + 2,
Alexandre Julliard's avatar
Alexandre Julliard committed
702
                     sizeof(pTask->curdir) );
Alexandre Julliard's avatar
Alexandre Julliard committed
703
        DRIVE_LastTask = GetCurrentTask();
704
        chdir(unix_cwd); /* Only change if on current drive */
Alexandre Julliard's avatar
Alexandre Julliard committed
705 706 707 708 709 710 711 712 713 714 715 716
    }
    return 1;
}


/***********************************************************************
 *           DRIVE_Disable
 */
int DRIVE_Disable( int drive  )
{
    if ((drive < 0) || (drive >= MAX_DOS_DRIVES) || !DOSDrives[drive].root)
    {
717
        SetLastError( ERROR_INVALID_DRIVE );
Alexandre Julliard's avatar
Alexandre Julliard committed
718 719
        return 0;
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
720
    DOSDrives[drive].flags |= DRIVE_DISABLED;
Alexandre Julliard's avatar
Alexandre Julliard committed
721 722 723 724 725 726 727 728 729 730 731
    return 1;
}


/***********************************************************************
 *           DRIVE_Enable
 */
int DRIVE_Enable( int drive  )
{
    if ((drive < 0) || (drive >= MAX_DOS_DRIVES) || !DOSDrives[drive].root)
    {
732
        SetLastError( ERROR_INVALID_DRIVE );
Alexandre Julliard's avatar
Alexandre Julliard committed
733 734
        return 0;
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
735
    DOSDrives[drive].flags &= ~DRIVE_DISABLED;
Alexandre Julliard's avatar
Alexandre Julliard committed
736 737 738 739
    return 1;
}


Alexandre Julliard's avatar
Alexandre Julliard committed
740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756
/***********************************************************************
 *           DRIVE_SetLogicalMapping
 */
int DRIVE_SetLogicalMapping ( int existing_drive, int new_drive )
{
 /* If new_drive is already valid, do nothing and return 0
    otherwise, copy DOSDrives[existing_drive] to DOSDrives[new_drive] */
  
    DOSDRIVE *old, *new;
    
    old = DOSDrives + existing_drive;
    new = DOSDrives + new_drive;

    if ((existing_drive < 0) || (existing_drive >= MAX_DOS_DRIVES) ||
        !old->root ||
	(new_drive < 0) || (new_drive >= MAX_DOS_DRIVES))
    {
757
        SetLastError( ERROR_INVALID_DRIVE );
Alexandre Julliard's avatar
Alexandre Julliard committed
758 759 760 761 762
        return 0;
    }

    if ( new->root )
    {
763 764
        TRACE("Can't map drive %c: to already existing drive %c:\n",
              'A' + existing_drive, 'A' + new_drive );
Alexandre Julliard's avatar
Alexandre Julliard committed
765 766 767
	/* it is already mapped there, so return success */
	if (!strcmp(old->root,new->root))
	    return 1;
Alexandre Julliard's avatar
Alexandre Julliard committed
768 769 770
	return 0;
    }

771 772 773
    new->root = HEAP_strdupA( GetProcessHeap(), 0, old->root );
    new->dos_cwd = HEAP_strdupA( GetProcessHeap(), 0, old->dos_cwd );
    new->unix_cwd = HEAP_strdupA( GetProcessHeap(), 0, old->unix_cwd );
774
    new->device = HEAP_strdupA( GetProcessHeap(), 0, old->device );
775
    memcpy ( new->label_conf, old->label_conf, 12 );
776
    memcpy ( new->label_read, old->label_read, 12 );
777
    new->serial_conf = old->serial_conf;
Alexandre Julliard's avatar
Alexandre Julliard committed
778 779 780 781 782
    new->type = old->type;
    new->flags = old->flags;
    new->dev = old->dev;
    new->ino = old->ino;

783
    TRACE("Drive %c: is now equal to drive %c:\n",
784
          'A' + new_drive, 'A' + existing_drive );
Alexandre Julliard's avatar
Alexandre Julliard committed
785 786 787 788 789

    return 1;
}


Alexandre Julliard's avatar
Alexandre Julliard committed
790 791 792 793 794 795 796
/***********************************************************************
 *           DRIVE_OpenDevice
 *
 * Open the drive raw device and return a Unix fd (or -1 on error).
 */
int DRIVE_OpenDevice( int drive, int flags )
{
Alexandre Julliard's avatar
Alexandre Julliard committed
797
    if (!DRIVE_IsValid( drive )) return -1;
Alexandre Julliard's avatar
Alexandre Julliard committed
798 799 800
    return open( DOSDrives[drive].device, flags );
}

Alexandre Julliard's avatar
Alexandre Julliard committed
801

802 803 804 805 806
/***********************************************************************
 *           DRIVE_RawRead
 *
 * Read raw sectors from a device
 */
807
int DRIVE_RawRead(BYTE drive, DWORD begin, DWORD nr_sect, BYTE *dataptr, BOOL fake_success)
808 809 810 811 812 813 814 815 816 817 818 819 820
{
    int fd;

    if ((fd = DRIVE_OpenDevice( drive, O_RDONLY )) != -1)
    {
        lseek( fd, begin * 512, SEEK_SET );
        /* FIXME: check errors */
        read( fd, dataptr, nr_sect * 512 );
        close( fd );
    }
    else
    {
        memset(dataptr, 0, nr_sect * 512);
821
	if (fake_success)
822
        {
823 824 825 826 827
	    if (begin == 0 && nr_sect > 1) *(dataptr + 512) = 0xf8;
	    if (begin == 1) *dataptr = 0xf8;
	}
	else
	    return 0;
828
    }
829
    return 1;
830 831 832 833 834 835 836 837
}


/***********************************************************************
 *           DRIVE_RawWrite
 *
 * Write raw sectors to a device
 */
838
int DRIVE_RawWrite(BYTE drive, DWORD begin, DWORD nr_sect, BYTE *dataptr, BOOL fake_success)
839
{
840
    int fd;
841 842 843 844 845 846 847 848 849

    if ((fd = DRIVE_OpenDevice( drive, O_RDONLY )) != -1)
    {
        lseek( fd, begin * 512, SEEK_SET );
        /* FIXME: check errors */
        write( fd, dataptr, nr_sect * 512 );
        close( fd );
    }
    else
850 851
    if (!(fake_success))
	return 0;
852

853
    return 1;
854 855 856
}


Alexandre Julliard's avatar
Alexandre Julliard committed
857 858 859
/***********************************************************************
 *           DRIVE_GetFreeSpace
 */
860 861
static int DRIVE_GetFreeSpace( int drive, PULARGE_INTEGER size, 
			       PULARGE_INTEGER available )
Alexandre Julliard's avatar
Alexandre Julliard committed
862 863 864 865 866
{
    struct statfs info;

    if (!DRIVE_IsValid(drive))
    {
867
        SetLastError( ERROR_INVALID_DRIVE );
Alexandre Julliard's avatar
Alexandre Julliard committed
868 869 870
        return 0;
    }

Alexandre Julliard's avatar
Alexandre Julliard committed
871
/* FIXME: add autoconf check for this */
872
#if defined(__svr4__) || defined(_SCO_DS) || defined(__sun)
Alexandre Julliard's avatar
Alexandre Julliard committed
873 874 875 876 877 878
    if (statfs( DOSDrives[drive].root, &info, 0, 0) < 0)
#else
    if (statfs( DOSDrives[drive].root, &info) < 0)
#endif
    {
        FILE_SetDosError();
879
        WARN("cannot do statfs(%s)\n", DOSDrives[drive].root);
Alexandre Julliard's avatar
Alexandre Julliard committed
880 881 882
        return 0;
    }

883
    size->QuadPart = RtlEnlargedUnsignedMultiply( info.f_bsize, info.f_blocks );
Alexandre Julliard's avatar
Alexandre Julliard committed
884
#ifdef STATFS_HAS_BAVAIL
885
    available->QuadPart = RtlEnlargedUnsignedMultiply( info.f_bavail, info.f_bsize );
Alexandre Julliard's avatar
Alexandre Julliard committed
886 887
#else
# ifdef STATFS_HAS_BFREE
888
    available->QuadPart = RtlEnlargedUnsignedMultiply( info.f_bfree, info.f_bsize );
Alexandre Julliard's avatar
Alexandre Julliard committed
889 890 891
# else
#  error "statfs has no bfree/bavail member!"
# endif
Alexandre Julliard's avatar
Alexandre Julliard committed
892
#endif
893 894
    if (DRIVE_GetType(drive) == TYPE_CDROM)
    { /* ALWAYS 0, even if no real CD-ROM mounted there !! */
895
        available->QuadPart = 0;
896
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
897 898 899
    return 1;
}

900
/***********************************************************************
901 902 903 904 905 906 907 908 909 910 911 912 913 914 915
 *       DRIVE_GetCurrentDirectory
 * Returns "X:\\path\\etc\\".
 *
 * Despite the API description, return required length including the 
 * terminating null when buffer too small. This is the real behaviour.
*/

static UINT DRIVE_GetCurrentDirectory( UINT buflen, LPSTR buf )
{
    UINT ret;
    const char *s = DRIVE_GetDosCwd( DRIVE_GetCurrentDrive() );

    assert(s);
    ret = strlen(s) + 3; /* length of WHOLE current directory */
    if (ret >= buflen) return ret + 1;
916
    lstrcpynA( buf, "A:\\", min( 4, buflen ) );
917 918 919 920
    if (buflen) buf[0] += DRIVE_GetCurrentDrive();
    if (buflen > 3) lstrcpynA( buf + 3, s, buflen - 3 );
    return ret;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
921

922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949

/***********************************************************************
 *           DRIVE_BuildEnv
 *
 * Build the environment array containing the drives current directories.
 * Resulting pointer must be freed with HeapFree.
 */
char *DRIVE_BuildEnv(void)
{
    int i, length = 0;
    const char *cwd[MAX_DOS_DRIVES];
    char *env, *p;

    for (i = 0; i < MAX_DOS_DRIVES; i++)
    {
        if ((cwd[i] = DRIVE_GetDosCwd(i)) && cwd[i][0]) length += strlen(cwd[i]) + 8;
    }
    if (!(env = HeapAlloc( GetProcessHeap(), 0, length+1 ))) return NULL;
    for (i = 0, p = env; i < MAX_DOS_DRIVES; i++)
    {
        if (cwd[i] && cwd[i][0])
            p += sprintf( p, "=%c:=%c:\\%s", 'A'+i, 'A'+i, cwd[i] ) + 1;
    }
    *p = 0;
    return env;
}


Alexandre Julliard's avatar
Alexandre Julliard committed
950 951 952
/***********************************************************************
 *           GetDiskFreeSpace16   (KERNEL.422)
 */
Alexandre Julliard's avatar
Alexandre Julliard committed
953 954 955
BOOL16 WINAPI GetDiskFreeSpace16( LPCSTR root, LPDWORD cluster_sectors,
                                  LPDWORD sector_bytes, LPDWORD free_clusters,
                                  LPDWORD total_clusters )
Alexandre Julliard's avatar
Alexandre Julliard committed
956
{
957
    return GetDiskFreeSpaceA( root, cluster_sectors, sector_bytes,
Alexandre Julliard's avatar
Alexandre Julliard committed
958 959 960 961 962
                                free_clusters, total_clusters );
}


/***********************************************************************
963
 *           GetDiskFreeSpaceA   (KERNEL32.206)
964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987
 *
 * Fails if expression resulting from current drive's dir and "root"
 * is not a root dir of the target drive.
 *
 * UNDOC: setting some LPDWORDs to NULL is perfectly possible 
 * if the corresponding info is unneeded.
 *
 * FIXME: needs to support UNC names from Win95 OSR2 on.
 *
 * Behaviour under Win95a:
 * CurrDir     root   result
 * "E:\\TEST"  "E:"   FALSE
 * "E:\\"      "E:"   TRUE
 * "E:\\"      "E"    FALSE
 * "E:\\"      "\\"   TRUE
 * "E:\\TEST"  "\\"   TRUE
 * "E:\\TEST"  ":\\"  FALSE
 * "E:\\TEST"  "E:\\" TRUE
 * "E:\\TEST"  ""     FALSE
 * "E:\\"      ""     FALSE (!)
 * "E:\\"      0x0    TRUE
 * "E:\\TEST"  0x0    TRUE  (!)
 * "E:\\TEST"  "C:"   TRUE  (when CurrDir of "C:" set to "\\")
 * "E:\\TEST"  "C:"   FALSE (when CurrDir of "C:" set to "\\TEST")
Alexandre Julliard's avatar
Alexandre Julliard committed
988
 */
989
BOOL WINAPI GetDiskFreeSpaceA( LPCSTR root, LPDWORD cluster_sectors,
Alexandre Julliard's avatar
Alexandre Julliard committed
990 991
                                   LPDWORD sector_bytes, LPDWORD free_clusters,
                                   LPDWORD total_clusters )
Alexandre Julliard's avatar
Alexandre Julliard committed
992
{
993
    int	drive, sec_size;
994
    ULARGE_INTEGER size,available;
995 996
    LPCSTR path;
    DWORD cluster_sec;
Alexandre Julliard's avatar
Alexandre Julliard committed
997

998
    if ((!root) || (strcmp(root,"\\") == 0))
999
	drive = DRIVE_GetCurrentDrive();
Alexandre Julliard's avatar
Alexandre Julliard committed
1000
    else
1001
    if ( (strlen(root) >= 2) && (root[1] == ':')) /* root contains drive tag */
Alexandre Julliard's avatar
Alexandre Julliard committed
1002 1003
    {
        drive = toupper(root[0]) - 'A';
1004 1005 1006 1007 1008 1009 1010 1011
	path = &root[2];
	if (path[0] == '\0')
	    path = DRIVE_GetDosCwd(drive);
	else
	if (path[0] == '\\')
	    path++;
	if (strlen(path)) /* oops, we are in a subdir */
	    return FALSE;
Alexandre Julliard's avatar
Alexandre Julliard committed
1012
    }
1013 1014 1015
    else
        return FALSE;

Alexandre Julliard's avatar
Alexandre Julliard committed
1016 1017
    if (!DRIVE_GetFreeSpace(drive, &size, &available)) return FALSE;

Alexandre Julliard's avatar
Alexandre Julliard committed
1018
    /* Cap the size and available at 2GB as per specs.  */
1019
    if ((size.s.HighPart) ||(size.s.LowPart > 0x7fffffff))
1020 1021 1022 1023
    {
	size.s.HighPart = 0;
	size.s.LowPart = 0x7fffffff;
    }
1024
    if ((available.s.HighPart) ||(available.s.LowPart > 0x7fffffff))
1025
    {
1026 1027
	available.s.HighPart =0;
	available.s.LowPart = 0x7fffffff;
Alexandre Julliard's avatar
Alexandre Julliard committed
1028
    }
1029 1030 1031
    sec_size = (DRIVE_GetType(drive)==TYPE_CDROM) ? 2048 : 512;
    size.s.LowPart            /= sec_size;
    available.s.LowPart       /= sec_size;
Alexandre Julliard's avatar
Alexandre Julliard committed
1032
    /* fixme: probably have to adjust those variables too for CDFS */
1033
    cluster_sec = 1;
1034
    while (cluster_sec * 65536 < size.s.LowPart) cluster_sec *= 2;
1035 1036 1037

    if (cluster_sectors)
	*cluster_sectors = cluster_sec;
1038 1039
    if (sector_bytes)
	*sector_bytes    = sec_size;
1040
    if (free_clusters)
1041
	*free_clusters   = available.s.LowPart / cluster_sec;
1042
    if (total_clusters)
1043
	*total_clusters  = size.s.LowPart / cluster_sec;
Alexandre Julliard's avatar
Alexandre Julliard committed
1044 1045 1046 1047 1048
    return TRUE;
}


/***********************************************************************
1049
 *           GetDiskFreeSpaceW   (KERNEL32.207)
Alexandre Julliard's avatar
Alexandre Julliard committed
1050
 */
1051
BOOL WINAPI GetDiskFreeSpaceW( LPCWSTR root, LPDWORD cluster_sectors,
Alexandre Julliard's avatar
Alexandre Julliard committed
1052 1053
                                   LPDWORD sector_bytes, LPDWORD free_clusters,
                                   LPDWORD total_clusters )
Alexandre Julliard's avatar
Alexandre Julliard committed
1054 1055
{
    LPSTR xroot;
1056
    BOOL ret;
Alexandre Julliard's avatar
Alexandre Julliard committed
1057

Alexandre Julliard's avatar
Alexandre Julliard committed
1058
    xroot = HEAP_strdupWtoA( GetProcessHeap(), 0, root);
1059
    ret = GetDiskFreeSpaceA( xroot,cluster_sectors, sector_bytes,
Alexandre Julliard's avatar
Alexandre Julliard committed
1060
                               free_clusters, total_clusters );
Alexandre Julliard's avatar
Alexandre Julliard committed
1061
    HeapFree( GetProcessHeap(), 0, xroot );
Alexandre Julliard's avatar
Alexandre Julliard committed
1062 1063 1064 1065
    return ret;
}


Alexandre Julliard's avatar
Alexandre Julliard committed
1066
/***********************************************************************
1067
 *           GetDiskFreeSpaceExA   (KERNEL32.871)
1068 1069 1070 1071 1072 1073 1074 1075 1076
 *
 *  This function is used to aquire the size of the available and
 *  total space on a logical volume.
 *
 * RETURNS
 *
 *  Zero on failure, nonzero upon success. Use GetLastError to obtain
 *  detailed error information.
 *
Alexandre Julliard's avatar
Alexandre Julliard committed
1077
 */
1078
BOOL WINAPI GetDiskFreeSpaceExA( LPCSTR root,
1079 1080 1081
				     PULARGE_INTEGER avail,
				     PULARGE_INTEGER total,
				     PULARGE_INTEGER totalfree)
Alexandre Julliard's avatar
Alexandre Julliard committed
1082 1083
{
    int	drive;
1084
    ULARGE_INTEGER size,available;
Alexandre Julliard's avatar
Alexandre Julliard committed
1085 1086 1087 1088 1089 1090

    if (!root) drive = DRIVE_GetCurrentDrive();
    else
    {
        if ((root[1]) && ((root[1] != ':') || (root[2] != '\\')))
        {
1091
            FIXME("there are valid root names which are not supported yet\n");
1092 1093
	    /* ..like UNC names, for instance. */

1094
            WARN("invalid root '%s'\n", root );
Alexandre Julliard's avatar
Alexandre Julliard committed
1095 1096 1097 1098
            return FALSE;
        }
        drive = toupper(root[0]) - 'A';
    }
1099

Alexandre Julliard's avatar
Alexandre Julliard committed
1100
    if (!DRIVE_GetFreeSpace(drive, &size, &available)) return FALSE;
1101 1102 1103

    if (total)
    {
1104
        total->s.HighPart = size.s.HighPart;
1105
        total->s.LowPart = size.s.LowPart;
1106
    }
1107 1108 1109

    if (totalfree)
    {
1110
        totalfree->s.HighPart = available.s.HighPart;
1111
        totalfree->s.LowPart = available.s.LowPart;
1112
    }
1113 1114 1115

    if (avail)
    {
1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128
        if (FIXME_ON(dosfs))
	{
            /* On Windows2000, we need to check the disk quota
	       allocated for the user owning the calling process. We
	       don't want to be more obtrusive than necessary with the
	       FIXME messages, so don't print the FIXME unless Wine is
	       actually masquerading as Windows2000. */

            OSVERSIONINFOA ovi;
	    ovi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOA);
	    if (GetVersionExA(&ovi))
	    {
	      if (ovi.dwPlatformId == VER_PLATFORM_WIN32_NT && ovi.dwMajorVersion > 4)
1129
                  FIXME("no per-user quota support yet\n");
1130 1131 1132 1133 1134
	    }
	}

        /* Quick hack, should eventually be fixed to work 100% with
           Windows2000 (see comment above). */
1135
        avail->s.HighPart = available.s.HighPart;
1136
        avail->s.LowPart = available.s.LowPart;
1137 1138
    }

Alexandre Julliard's avatar
Alexandre Julliard committed
1139 1140 1141 1142
    return TRUE;
}

/***********************************************************************
1143
 *           GetDiskFreeSpaceExW   (KERNEL32.873)
Alexandre Julliard's avatar
Alexandre Julliard committed
1144
 */
1145
BOOL WINAPI GetDiskFreeSpaceExW( LPCWSTR root, PULARGE_INTEGER avail,
1146 1147
				     PULARGE_INTEGER total,
				     PULARGE_INTEGER  totalfree)
Alexandre Julliard's avatar
Alexandre Julliard committed
1148 1149
{
    LPSTR xroot;
1150
    BOOL ret;
Alexandre Julliard's avatar
Alexandre Julliard committed
1151 1152

    xroot = HEAP_strdupWtoA( GetProcessHeap(), 0, root);
1153
    ret = GetDiskFreeSpaceExA( xroot, avail, total, totalfree);
Alexandre Julliard's avatar
Alexandre Julliard committed
1154 1155 1156 1157
    HeapFree( GetProcessHeap(), 0, xroot );
    return ret;
}

Alexandre Julliard's avatar
Alexandre Julliard committed
1158
/***********************************************************************
Alexandre Julliard's avatar
Alexandre Julliard committed
1159
 *           GetDriveType16   (KERNEL.136)
1160
 * This function returns the type of a drive in Win16. 
Alexandre Julliard's avatar
Alexandre Julliard committed
1161
 * Note that it returns DRIVE_REMOTE for CD-ROMs, since MSCDEX uses the
1162 1163
 * remote drive API. The return value DRIVE_REMOTE for CD-ROMs has been
 * verified on Win 3.11 and Windows 95. Some programs rely on it, so don't
Alexandre Julliard's avatar
Alexandre Julliard committed
1164 1165 1166 1167
 * do any pseudo-clever changes.
 *
 * RETURNS
 *	drivetype DRIVE_xxx
Alexandre Julliard's avatar
Alexandre Julliard committed
1168
 */
Alexandre Julliard's avatar
Alexandre Julliard committed
1169 1170 1171
UINT16 WINAPI GetDriveType16(
	UINT16 drive	/* [in] number (NOT letter) of drive */
) {
1172
    TRACE("(%c:)\n", 'A' + drive );
Alexandre Julliard's avatar
Alexandre Julliard committed
1173 1174 1175 1176
    switch(DRIVE_GetType(drive))
    {
    case TYPE_FLOPPY:  return DRIVE_REMOVABLE;
    case TYPE_HD:      return DRIVE_FIXED;
Alexandre Julliard's avatar
Alexandre Julliard committed
1177
    case TYPE_CDROM:   return DRIVE_REMOTE;
Alexandre Julliard's avatar
Alexandre Julliard committed
1178 1179 1180 1181
    case TYPE_NETWORK: return DRIVE_REMOTE;
    case TYPE_INVALID:
    default:           return DRIVE_CANNOTDETERMINE;
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
1182
}
Alexandre Julliard's avatar
Alexandre Julliard committed
1183 1184


Alexandre Julliard's avatar
Alexandre Julliard committed
1185
/***********************************************************************
1186
 *           GetDriveTypeA   (KERNEL32.208)
Alexandre Julliard's avatar
Alexandre Julliard committed
1187
 *
1188
 * Returns the type of the disk drive specified. If root is NULL the
Alexandre Julliard's avatar
Alexandre Julliard committed
1189 1190 1191 1192 1193 1194 1195
 * root of the current directory is used.
 *
 * RETURNS
 *
 *  Type of drive (from Win32 SDK):
 *
 *   DRIVE_UNKNOWN     unable to find out anything about the drive
1196
 *   DRIVE_NO_ROOT_DIR nonexistent root dir
Alexandre Julliard's avatar
Alexandre Julliard committed
1197 1198 1199 1200
 *   DRIVE_REMOVABLE   the disk can be removed from the machine
 *   DRIVE_FIXED       the disk can not be removed from the machine
 *   DRIVE_REMOTE      network disk
 *   DRIVE_CDROM       CDROM drive
1201
 *   DRIVE_RAMDISK     virtual disk in RAM
Alexandre Julliard's avatar
Alexandre Julliard committed
1202
 *
1203 1204
 *   DRIVE_DOESNOTEXIST    FIXME Not valid return value
 *   DRIVE_CANNOTDETERMINE FIXME Not valid return value
Alexandre Julliard's avatar
Alexandre Julliard committed
1205 1206 1207
 *   
 * BUGS
 *
1208 1209
 *  Currently returns DRIVE_DOESNOTEXIST and DRIVE_CANNOTDETERMINE
 *  when it really should return DRIVE_NO_ROOT_DIR and DRIVE_UNKNOWN.
1210
 *  Why were the former defines used?
1211 1212
 *
 *  DRIVE_RAMDISK is unsupported.
Alexandre Julliard's avatar
Alexandre Julliard committed
1213
 */
1214
UINT WINAPI GetDriveTypeA(LPCSTR root /* String describing drive */)
Alexandre Julliard's avatar
Alexandre Julliard committed
1215
{
1216
    int drive;
1217
    TRACE("(%s)\n", debugstr_a(root));
Alexandre Julliard's avatar
Alexandre Julliard committed
1218

1219 1220
    if (NULL == root) drive = DRIVE_GetCurrentDrive();
    else
Alexandre Julliard's avatar
Alexandre Julliard committed
1221
    {
1222 1223
        if ((root[1]) && (root[1] != ':'))
	{
1224
	    WARN("invalid root '%s'\n", debugstr_a(root));
1225 1226 1227
	    return DRIVE_DOESNOTEXIST;
	}
	drive = toupper(root[0]) - 'A';
Alexandre Julliard's avatar
Alexandre Julliard committed
1228
    }
1229
    switch(DRIVE_GetType(drive))
Alexandre Julliard's avatar
Alexandre Julliard committed
1230 1231 1232
    {
    case TYPE_FLOPPY:  return DRIVE_REMOVABLE;
    case TYPE_HD:      return DRIVE_FIXED;
Alexandre Julliard's avatar
Alexandre Julliard committed
1233
    case TYPE_CDROM:   return DRIVE_CDROM;
Alexandre Julliard's avatar
Alexandre Julliard committed
1234
    case TYPE_NETWORK: return DRIVE_REMOTE;
Alexandre Julliard's avatar
Alexandre Julliard committed
1235
    case TYPE_INVALID: return DRIVE_DOESNOTEXIST;
Alexandre Julliard's avatar
Alexandre Julliard committed
1236 1237 1238 1239 1240
    default:           return DRIVE_CANNOTDETERMINE;
    }
}


Alexandre Julliard's avatar
Alexandre Julliard committed
1241
/***********************************************************************
1242
 *           GetDriveTypeW   (KERNEL32.209)
Alexandre Julliard's avatar
Alexandre Julliard committed
1243
 */
1244
UINT WINAPI GetDriveTypeW( LPCWSTR root )
Alexandre Julliard's avatar
Alexandre Julliard committed
1245
{
Alexandre Julliard's avatar
Alexandre Julliard committed
1246
    LPSTR xpath = HEAP_strdupWtoA( GetProcessHeap(), 0, root );
1247
    UINT ret = GetDriveTypeA( xpath );
Alexandre Julliard's avatar
Alexandre Julliard committed
1248
    HeapFree( GetProcessHeap(), 0, xpath );
Alexandre Julliard's avatar
Alexandre Julliard committed
1249 1250 1251 1252 1253 1254 1255
    return ret;
}


/***********************************************************************
 *           GetCurrentDirectory16   (KERNEL.411)
 */
Alexandre Julliard's avatar
Alexandre Julliard committed
1256
UINT16 WINAPI GetCurrentDirectory16( UINT16 buflen, LPSTR buf )
Alexandre Julliard's avatar
Alexandre Julliard committed
1257
{
1258
    return (UINT16)DRIVE_GetCurrentDirectory(buflen, buf);
Alexandre Julliard's avatar
Alexandre Julliard committed
1259 1260 1261 1262
}


/***********************************************************************
1263
 *           GetCurrentDirectoryA   (KERNEL32.196)
Alexandre Julliard's avatar
Alexandre Julliard committed
1264
 */
1265
UINT WINAPI GetCurrentDirectoryA( UINT buflen, LPSTR buf )
Alexandre Julliard's avatar
Alexandre Julliard committed
1266
{
1267
    UINT ret;
1268
    char longname[MAX_PATHNAME_LEN];
1269 1270 1271 1272 1273 1274 1275
    char shortname[MAX_PATHNAME_LEN];
    ret = DRIVE_GetCurrentDirectory(MAX_PATHNAME_LEN, shortname);
    if ( ret > MAX_PATHNAME_LEN ) {
      ERR_(file)("pathnamelength (%d) > MAX_PATHNAME_LEN!\n", ret );
      return ret;
    }
    GetLongPathNameA(shortname, longname, MAX_PATHNAME_LEN);
1276
    ret = strlen( longname ) + 1;
1277
    if (ret > buflen) return ret;
1278
    strcpy(buf, longname);
1279
    return ret - 1;
Alexandre Julliard's avatar
Alexandre Julliard committed
1280 1281 1282
}

/***********************************************************************
1283
 *           GetCurrentDirectoryW   (KERNEL32.197)
Alexandre Julliard's avatar
Alexandre Julliard committed
1284
 */
1285
UINT WINAPI GetCurrentDirectoryW( UINT buflen, LPWSTR buf )
Alexandre Julliard's avatar
Alexandre Julliard committed
1286
{
Alexandre Julliard's avatar
Alexandre Julliard committed
1287
    LPSTR xpath = HeapAlloc( GetProcessHeap(), 0, buflen+1 );
1288
    UINT ret = GetCurrentDirectoryA( buflen, xpath );
1289
    if (ret < buflen) lstrcpyAtoW ( buf, xpath );
Alexandre Julliard's avatar
Alexandre Julliard committed
1290
    HeapFree( GetProcessHeap(), 0, xpath );
Alexandre Julliard's avatar
Alexandre Julliard committed
1291
    return ret;
Alexandre Julliard's avatar
Alexandre Julliard committed
1292 1293 1294 1295 1296 1297
}


/***********************************************************************
 *           SetCurrentDirectory   (KERNEL.412)
 */
Alexandre Julliard's avatar
Alexandre Julliard committed
1298
BOOL16 WINAPI SetCurrentDirectory16( LPCSTR dir )
Alexandre Julliard's avatar
Alexandre Julliard committed
1299
{
1300
    return SetCurrentDirectoryA( dir );
Alexandre Julliard's avatar
Alexandre Julliard committed
1301 1302 1303 1304
}


/***********************************************************************
1305
 *           SetCurrentDirectoryA   (KERNEL32.479)
Alexandre Julliard's avatar
Alexandre Julliard committed
1306
 */
1307
BOOL WINAPI SetCurrentDirectoryA( LPCSTR dir )
Alexandre Julliard's avatar
Alexandre Julliard committed
1308
{
1309
    int drive, olddrive = DRIVE_GetCurrentDrive();
Alexandre Julliard's avatar
Alexandre Julliard committed
1310

Alexandre Julliard's avatar
Alexandre Julliard committed
1311
    if (!dir) {
1312
    	ERR_(file)("(NULL)!\n");
Alexandre Julliard's avatar
Alexandre Julliard committed
1313 1314
	return FALSE;
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
1315 1316
    if (dir[0] && (dir[1]==':'))
    {
1317
        drive = toupper( *dir ) - 'A';
Alexandre Julliard's avatar
Alexandre Julliard committed
1318 1319
        dir += 2;
    }
1320 1321
    else
	drive = olddrive;
1322 1323 1324 1325 1326

    /* WARNING: we need to set the drive before the dir, as DRIVE_Chdir
       sets pTask->curdir only if pTask->curdrive is drive */
    if (!(DRIVE_SetCurrentDrive( drive )))
	return FALSE;
Alexandre Julliard's avatar
Alexandre Julliard committed
1327
    /* FIXME: what about empty strings? Add a \\ ? */
1328 1329 1330 1331 1332
    if (!DRIVE_Chdir( drive, dir )) {
	DRIVE_SetCurrentDrive(olddrive);
	return FALSE;
    }
    return TRUE;
Alexandre Julliard's avatar
Alexandre Julliard committed
1333
}
Alexandre Julliard's avatar
Alexandre Julliard committed
1334

Alexandre Julliard's avatar
Alexandre Julliard committed
1335 1336

/***********************************************************************
1337
 *           SetCurrentDirectoryW   (KERNEL32.480)
Alexandre Julliard's avatar
Alexandre Julliard committed
1338
 */
1339
BOOL WINAPI SetCurrentDirectoryW( LPCWSTR dirW )
Alexandre Julliard's avatar
Alexandre Julliard committed
1340
{
Alexandre Julliard's avatar
Alexandre Julliard committed
1341
    LPSTR dir = HEAP_strdupWtoA( GetProcessHeap(), 0, dirW );
1342
    BOOL res = SetCurrentDirectoryA( dir );
Alexandre Julliard's avatar
Alexandre Julliard committed
1343
    HeapFree( GetProcessHeap(), 0, dir );
Alexandre Julliard's avatar
Alexandre Julliard committed
1344 1345 1346
    return res;
}

Alexandre Julliard's avatar
Alexandre Julliard committed
1347 1348

/***********************************************************************
1349
 *           GetLogicalDriveStringsA   (KERNEL32.231)
Alexandre Julliard's avatar
Alexandre Julliard committed
1350
 */
1351
UINT WINAPI GetLogicalDriveStringsA( UINT len, LPSTR buffer )
Alexandre Julliard's avatar
Alexandre Julliard committed
1352 1353 1354 1355 1356
{
    int drive, count;

    for (drive = count = 0; drive < MAX_DOS_DRIVES; drive++)
        if (DRIVE_IsValid(drive)) count++;
1357
    if ((count * 4) + 1 <= len)
Alexandre Julliard's avatar
Alexandre Julliard committed
1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368
    {
        LPSTR p = buffer;
        for (drive = 0; drive < MAX_DOS_DRIVES; drive++)
            if (DRIVE_IsValid(drive))
            {
                *p++ = 'a' + drive;
                *p++ = ':';
                *p++ = '\\';
                *p++ = '\0';
            }
        *p = '\0';
1369
        return count * 4;
Alexandre Julliard's avatar
Alexandre Julliard committed
1370
    }
1371
    else
1372
        return (count * 4) + 1; /* account for terminating null */
1373
    /* The API tells about these different return values */
Alexandre Julliard's avatar
Alexandre Julliard committed
1374 1375 1376 1377
}


/***********************************************************************
1378
 *           GetLogicalDriveStringsW   (KERNEL32.232)
Alexandre Julliard's avatar
Alexandre Julliard committed
1379
 */
1380
UINT WINAPI GetLogicalDriveStringsW( UINT len, LPWSTR buffer )
Alexandre Julliard's avatar
Alexandre Julliard committed
1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405
{
    int drive, count;

    for (drive = count = 0; drive < MAX_DOS_DRIVES; drive++)
        if (DRIVE_IsValid(drive)) count++;
    if (count * 4 * sizeof(WCHAR) <= len)
    {
        LPWSTR p = buffer;
        for (drive = 0; drive < MAX_DOS_DRIVES; drive++)
            if (DRIVE_IsValid(drive))
            {
                *p++ = (WCHAR)('a' + drive);
                *p++ = (WCHAR)':';
                *p++ = (WCHAR)'\\';
                *p++ = (WCHAR)'\0';
            }
        *p = (WCHAR)'\0';
    }
    return count * 4 * sizeof(WCHAR);
}


/***********************************************************************
 *           GetLogicalDrives   (KERNEL32.233)
 */
Alexandre Julliard's avatar
Alexandre Julliard committed
1406
DWORD WINAPI GetLogicalDrives(void)
Alexandre Julliard's avatar
Alexandre Julliard committed
1407 1408 1409 1410 1411
{
    DWORD ret = 0;
    int drive;

    for (drive = 0; drive < MAX_DOS_DRIVES; drive++)
1412 1413 1414 1415 1416
    {
        if ( (DRIVE_IsValid(drive)) ||
            (DOSDrives[drive].type == TYPE_CDROM)) /* audio CD is also valid */
            ret |= (1 << drive);
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
1417 1418
    return ret;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
1419 1420 1421


/***********************************************************************
1422
 *           GetVolumeInformationA   (KERNEL32.309)
Alexandre Julliard's avatar
Alexandre Julliard committed
1423
 */
1424
BOOL WINAPI GetVolumeInformationA( LPCSTR root, LPSTR label,
Alexandre Julliard's avatar
Alexandre Julliard committed
1425 1426 1427
                                       DWORD label_len, DWORD *serial,
                                       DWORD *filename_len, DWORD *flags,
                                       LPSTR fsname, DWORD fsname_len )
Alexandre Julliard's avatar
Alexandre Julliard committed
1428 1429
{
    int drive;
Alexandre Julliard's avatar
Alexandre Julliard committed
1430
    char *cp;
Alexandre Julliard's avatar
Alexandre Julliard committed
1431

1432
    /* FIXME, SetLastError()s missing */
Alexandre Julliard's avatar
Alexandre Julliard committed
1433 1434 1435 1436

    if (!root) drive = DRIVE_GetCurrentDrive();
    else
    {
Alexandre Julliard's avatar
Alexandre Julliard committed
1437
        if ((root[1]) && (root[1] != ':'))
Alexandre Julliard's avatar
Alexandre Julliard committed
1438
        {
1439
            WARN("invalid root '%s'\n",root);
Alexandre Julliard's avatar
Alexandre Julliard committed
1440 1441 1442 1443 1444
            return FALSE;
        }
        drive = toupper(root[0]) - 'A';
    }
    if (!DRIVE_IsValid( drive )) return FALSE;
Alexandre Julliard's avatar
Alexandre Julliard committed
1445 1446
    if (label)
    {
1447
       lstrcpynA( label, DRIVE_GetLabel(drive), label_len );
Alexandre Julliard's avatar
Alexandre Julliard committed
1448 1449 1450 1451
       for (cp = label; *cp; cp++);
       while (cp != label && *(cp-1) == ' ') cp--;
       *cp = '\0';
    }
1452
    if (serial) *serial = DRIVE_GetSerialNumber(drive);
Alexandre Julliard's avatar
Alexandre Julliard committed
1453 1454

    /* Set the filesystem information */
1455
    /* Note: we only emulate a FAT fs at present */
Alexandre Julliard's avatar
Alexandre Julliard committed
1456

Alexandre Julliard's avatar
Alexandre Julliard committed
1457 1458 1459 1460 1461 1462
    if (filename_len) {
    	if (DOSDrives[drive].flags & DRIVE_SHORT_NAMES)
	    *filename_len = 12;
	else
	    *filename_len = 255;
    }
1463 1464 1465 1466 1467 1468
    if (flags)
      {
       *flags=0;
       if (DOSDrives[drive].flags & DRIVE_CASE_SENSITIVE)
         *flags|=FS_CASE_SENSITIVE;
       if (DOSDrives[drive].flags & DRIVE_CASE_PRESERVING)
1469
         *flags|=FS_CASE_IS_PRESERVED;
1470
      }
Alexandre Julliard's avatar
Alexandre Julliard committed
1471 1472 1473
    if (fsname) {
    	/* Diablo checks that return code ... */
    	if (DRIVE_GetType(drive)==TYPE_CDROM)
1474
	    lstrcpynA( fsname, "CDFS", fsname_len );
Alexandre Julliard's avatar
Alexandre Julliard committed
1475
	else
1476
	    lstrcpynA( fsname, "FAT", fsname_len );
Alexandre Julliard's avatar
Alexandre Julliard committed
1477
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
1478 1479 1480 1481 1482
    return TRUE;
}


/***********************************************************************
1483
 *           GetVolumeInformationW   (KERNEL32.310)
Alexandre Julliard's avatar
Alexandre Julliard committed
1484
 */
1485
BOOL WINAPI GetVolumeInformationW( LPCWSTR root, LPWSTR label,
Alexandre Julliard's avatar
Alexandre Julliard committed
1486 1487 1488
                                       DWORD label_len, DWORD *serial,
                                       DWORD *filename_len, DWORD *flags,
                                       LPWSTR fsname, DWORD fsname_len )
Alexandre Julliard's avatar
Alexandre Julliard committed
1489
{
Alexandre Julliard's avatar
Alexandre Julliard committed
1490 1491 1492
    LPSTR xroot    = HEAP_strdupWtoA( GetProcessHeap(), 0, root );
    LPSTR xvolname = label ? HeapAlloc(GetProcessHeap(),0,label_len) : NULL;
    LPSTR xfsname  = fsname ? HeapAlloc(GetProcessHeap(),0,fsname_len) : NULL;
1493
    BOOL ret = GetVolumeInformationA( xroot, xvolname, label_len, serial,
Alexandre Julliard's avatar
Alexandre Julliard committed
1494 1495 1496 1497
                                          filename_len, flags, xfsname,
                                          fsname_len );
    if (ret)
    {
Alexandre Julliard's avatar
Alexandre Julliard committed
1498 1499
        if (label) lstrcpyAtoW( label, xvolname );
        if (fsname) lstrcpyAtoW( fsname, xfsname );
Alexandre Julliard's avatar
Alexandre Julliard committed
1500
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
1501 1502 1503
    HeapFree( GetProcessHeap(), 0, xroot );
    HeapFree( GetProcessHeap(), 0, xvolname );
    HeapFree( GetProcessHeap(), 0, xfsname );
Alexandre Julliard's avatar
Alexandre Julliard committed
1504 1505
    return ret;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
1506

1507 1508 1509
/***********************************************************************
 *           SetVolumeLabelA   (KERNEL32.675)
 */
1510
BOOL WINAPI SetVolumeLabelA( LPCSTR root, LPCSTR volname )
1511
{
1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532
    int drive;
    
    /* FIXME, SetLastErrors missing */

    if (!root) drive = DRIVE_GetCurrentDrive();
    else
    {
        if ((root[1]) && (root[1] != ':'))
        {
            WARN("invalid root '%s'\n",root);
            return FALSE;
        }
        drive = toupper(root[0]) - 'A';
    }
    if (!DRIVE_IsValid( drive )) return FALSE;

    /* some copy protection stuff check this */
    if (DRIVE_GetType( drive ) == TYPE_CDROM) return FALSE;

    FIXME("(%s,%s),stub!\n", root, volname);
    return TRUE;
Alexandre Julliard's avatar
Alexandre Julliard committed
1533
}
1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549

/***********************************************************************
 *           SetVolumeLabelW   (KERNEL32.676)
 */
BOOL WINAPI SetVolumeLabelW(LPCWSTR rootpath,LPCWSTR volname)
{
    LPSTR xroot, xvol;
    BOOL ret;

    xroot = HEAP_strdupWtoA( GetProcessHeap(), 0, rootpath);
    xvol = HEAP_strdupWtoA( GetProcessHeap(), 0, volname);
    ret = SetVolumeLabelA( xroot, xvol );
    HeapFree( GetProcessHeap(), 0, xroot );
    HeapFree( GetProcessHeap(), 0, xvol );
    return ret;
}