kernel: Avoid heap corruption on invalid parameter in GlobalFree().
[wine] / dlls / kernel / volume.c
1 /*
2  * Volume management functions
3  *
4  * Copyright 1993 Erik Bos
5  * Copyright 1996, 2004 Alexandre Julliard
6  * Copyright 1999 Petr Tomasek
7  * Copyright 2000 Andreas Mohr
8  * Copyright 2003 Eric Pouech
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23  */
24
25 #include "config.h"
26 #include "wine/port.h"
27
28 #include <stdarg.h>
29 #include <stdlib.h>
30 #include <stdio.h>
31
32 #include "ntstatus.h"
33 #define WIN32_NO_STATUS
34 #include "windef.h"
35 #include "winbase.h"
36 #include "winnls.h"
37 #include "winternl.h"
38 #include "winioctl.h"
39 #include "ntddstor.h"
40 #include "ntddcdrm.h"
41 #include "kernel_private.h"
42 #include "wine/library.h"
43 #include "wine/unicode.h"
44 #include "wine/debug.h"
45
46 WINE_DEFAULT_DEBUG_CHANNEL(volume);
47
48 #define SUPERBLOCK_SIZE 2048
49
50 #define CDFRAMES_PERSEC         75
51 #define CDFRAMES_PERMIN         (CDFRAMES_PERSEC * 60)
52 #define FRAME_OF_ADDR(a)        ((a)[1] * CDFRAMES_PERMIN + (a)[2] * CDFRAMES_PERSEC + (a)[3])
53 #define FRAME_OF_TOC(toc, idx)  FRAME_OF_ADDR((toc)->TrackData[(idx) - (toc)->FirstTrack].Address)
54
55 #define GETWORD(buf,off)  MAKEWORD(buf[(off)],buf[(off+1)])
56 #define GETLONG(buf,off)  MAKELONG(GETWORD(buf,off),GETWORD(buf,off+2))
57
58 enum fs_type
59 {
60     FS_ERROR,    /* error accessing the device */
61     FS_UNKNOWN,  /* unknown file system */
62     FS_FAT1216,
63     FS_FAT32,
64     FS_ISO9660
65 };
66
67 static const WCHAR drive_types[][8] =
68 {
69     { 0 }, /* DRIVE_UNKNOWN */
70     { 0 }, /* DRIVE_NO_ROOT_DIR */
71     {'f','l','o','p','p','y',0}, /* DRIVE_REMOVABLE */
72     {'h','d',0}, /* DRIVE_FIXED */
73     {'n','e','t','w','o','r','k',0}, /* DRIVE_REMOTE */
74     {'c','d','r','o','m',0}, /* DRIVE_CDROM */
75     {'r','a','m','d','i','s','k',0} /* DRIVE_RAMDISK */
76 };
77
78 /* read a Unix symlink; returned buffer must be freed by caller */
79 static char *read_symlink( const char *path )
80 {
81     char *buffer;
82     int ret, size = 128;
83
84     for (;;)
85     {
86         if (!(buffer = HeapAlloc( GetProcessHeap(), 0, size )))
87         {
88             SetLastError( ERROR_NOT_ENOUGH_MEMORY );
89             return 0;
90         }
91         ret = readlink( path, buffer, size );
92         if (ret == -1)
93         {
94             FILE_SetDosError();
95             HeapFree( GetProcessHeap(), 0, buffer );
96             return 0;
97         }
98         if (ret != size)
99         {
100             buffer[ret] = 0;
101             return buffer;
102         }
103         HeapFree( GetProcessHeap(), 0, buffer );
104         size *= 2;
105     }
106 }
107
108 /* get the path of a dos device symlink in the $WINEPREFIX/dosdevices directory */
109 static char *get_dos_device_path( LPCWSTR name )
110 {
111     const char *config_dir = wine_get_config_dir();
112     char *buffer, *dev;
113     int i;
114
115     if (!(buffer = HeapAlloc( GetProcessHeap(), 0,
116                               strlen(config_dir) + sizeof("/dosdevices/") + 5 )))
117     {
118         SetLastError( ERROR_NOT_ENOUGH_MEMORY );
119         return NULL;
120     }
121     strcpy( buffer, config_dir );
122     strcat( buffer, "/dosdevices/" );
123     dev = buffer + strlen(buffer);
124     /* no codepage conversion, DOS device names are ASCII anyway */
125     for (i = 0; i < 5; i++)
126         if (!(dev[i] = (char)tolowerW(name[i]))) break;
127     dev[5] = 0;
128     return buffer;
129 }
130
131
132 /* open a handle to a device root */
133 static BOOL open_device_root( LPCWSTR root, HANDLE *handle )
134 {
135     static const WCHAR default_rootW[] = {'\\',0};
136     UNICODE_STRING nt_name;
137     OBJECT_ATTRIBUTES attr;
138     IO_STATUS_BLOCK io;
139     NTSTATUS status;
140
141     if (!root) root = default_rootW;
142     if (!RtlDosPathNameToNtPathName_U( root, &nt_name, NULL, NULL ))
143     {
144         SetLastError( ERROR_PATH_NOT_FOUND );
145         return FALSE;
146     }
147     attr.Length = sizeof(attr);
148     attr.RootDirectory = 0;
149     attr.Attributes = OBJ_CASE_INSENSITIVE;
150     attr.ObjectName = &nt_name;
151     attr.SecurityDescriptor = NULL;
152     attr.SecurityQualityOfService = NULL;
153
154     status = NtOpenFile( handle, 0, &attr, &io, 0,
155                          FILE_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT );
156     RtlFreeUnicodeString( &nt_name );
157     if (status != STATUS_SUCCESS)
158     {
159         SetLastError( RtlNtStatusToDosError(status) );
160         return FALSE;
161     }
162     return TRUE;
163 }
164
165
166 /* fetch the type of a drive from the registry */
167 static UINT get_registry_drive_type( const WCHAR *root )
168 {
169     static const WCHAR drive_types_keyW[] = {'M','a','c','h','i','n','e','\\',
170                                              'S','o','f','t','w','a','r','e','\\',
171                                              'W','i','n','e','\\',
172                                              'D','r','i','v','e','s',0 };
173     OBJECT_ATTRIBUTES attr;
174     UNICODE_STRING nameW;
175     HANDLE hkey;
176     DWORD dummy;
177     UINT ret = DRIVE_UNKNOWN;
178     char tmp[32 + sizeof(KEY_VALUE_PARTIAL_INFORMATION)];
179     WCHAR driveW[] = {'A',':',0};
180
181     attr.Length = sizeof(attr);
182     attr.RootDirectory = 0;
183     attr.ObjectName = &nameW;
184     attr.Attributes = 0;
185     attr.SecurityDescriptor = NULL;
186     attr.SecurityQualityOfService = NULL;
187     RtlInitUnicodeString( &nameW, drive_types_keyW );
188     /* @@ Wine registry key: HKLM\Software\Wine\Drives */
189     if (NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr ) != STATUS_SUCCESS) return DRIVE_UNKNOWN;
190
191     if (root) driveW[0] = root[0];
192     else
193     {
194         WCHAR path[MAX_PATH];
195         GetCurrentDirectoryW( MAX_PATH, path );
196         driveW[0] = path[0];
197     }
198
199     RtlInitUnicodeString( &nameW, driveW );
200     if (!NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation, tmp, sizeof(tmp), &dummy ))
201     {
202         unsigned int i;
203         WCHAR *data = (WCHAR *)((KEY_VALUE_PARTIAL_INFORMATION *)tmp)->Data;
204
205         for (i = 0; i < sizeof(drive_types)/sizeof(drive_types[0]); i++)
206         {
207             if (!strcmpiW( data, drive_types[i] ))
208             {
209                 ret = i;
210                 break;
211             }
212         }
213     }
214     NtClose( hkey );
215     return ret;
216 }
217
218
219 /******************************************************************
220  *              VOLUME_FindCdRomDataBestVoldesc
221  */
222 static DWORD VOLUME_FindCdRomDataBestVoldesc( HANDLE handle )
223 {
224     BYTE cur_vd_type, max_vd_type = 0;
225     BYTE buffer[16];
226     DWORD size, offs, best_offs = 0, extra_offs = 0;
227
228     for (offs = 0x8000; offs <= 0x9800; offs += 0x800)
229     {
230         /* if 'CDROM' occurs at position 8, this is a pre-iso9660 cd, and
231          * the volume label is displaced forward by 8
232          */
233         if (SetFilePointer( handle, offs, NULL, FILE_BEGIN ) != offs) break;
234         if (!ReadFile( handle, buffer, sizeof(buffer), &size, NULL )) break;
235         if (size != sizeof(buffer)) break;
236         /* check for non-ISO9660 signature */
237         if (!memcmp( buffer + 11, "ROM", 3 )) extra_offs = 8;
238         cur_vd_type = buffer[extra_offs];
239         if (cur_vd_type == 0xff) /* voldesc set terminator */
240             break;
241         if (cur_vd_type > max_vd_type)
242         {
243             max_vd_type = cur_vd_type;
244             best_offs = offs + extra_offs;
245         }
246     }
247     return best_offs;
248 }
249
250
251 /***********************************************************************
252  *           VOLUME_ReadFATSuperblock
253  */
254 static enum fs_type VOLUME_ReadFATSuperblock( HANDLE handle, BYTE *buff )
255 {
256     DWORD size;
257
258     /* try a fixed disk, with a FAT partition */
259     if (SetFilePointer( handle, 0, NULL, FILE_BEGIN ) != 0 ||
260         !ReadFile( handle, buff, SUPERBLOCK_SIZE, &size, NULL ) ||
261         size != SUPERBLOCK_SIZE)
262         return FS_ERROR;
263
264     /* FIXME: do really all FAT have their name beginning with
265      * "FAT" ? (At least FAT12, FAT16 and FAT32 have :)
266      */
267     if (!memcmp(buff+0x36, "FAT", 3) || !memcmp(buff+0x52, "FAT", 3))
268     {
269         /* guess which type of FAT we have */
270         int reasonable;
271         unsigned int sectors,
272                      sect_per_fat,
273                      total_sectors,
274                      num_boot_sectors,
275                      num_fats,
276                      num_root_dir_ents,
277                      bytes_per_sector,
278                      sectors_per_cluster,
279                      nclust;
280         sect_per_fat = GETWORD(buff, 0x16);
281         if (!sect_per_fat) sect_per_fat = GETLONG(buff, 0x24);
282         total_sectors = GETWORD(buff, 0x13);
283         if (!total_sectors)
284             total_sectors = GETLONG(buff, 0x20);
285         num_boot_sectors = GETWORD(buff, 0x0e);
286         num_fats =  buff[0x10];
287         num_root_dir_ents = GETWORD(buff, 0x11);
288         bytes_per_sector = GETWORD(buff, 0x0b);
289         sectors_per_cluster = buff[0x0d];
290         /* check if the parameters are reasonable and will not cause
291          * arithmetic errors in the calculation */
292         reasonable = num_boot_sectors < 16 &&
293                      num_fats < 16 &&
294                      bytes_per_sector >= 512 && bytes_per_sector % 512 == 0 &&
295                      sectors_per_cluster > 1;
296         if (!reasonable) return FS_UNKNOWN;
297         sectors =  total_sectors - num_boot_sectors - num_fats * sect_per_fat -
298             (num_root_dir_ents * 32 + bytes_per_sector - 1) / bytes_per_sector;
299         nclust = sectors / sectors_per_cluster;
300         if ((buff[0x42] == 0x28 || buff[0x42] == 0x29) &&
301                 !memcmp(buff+0x52, "FAT", 3)) return FS_FAT32;
302         if (nclust < 65525)
303         {
304             if ((buff[0x26] == 0x28 || buff[0x26] == 0x29) &&
305                     !memcmp(buff+0x36, "FAT", 3))
306                 return FS_FAT1216;
307         }
308     }
309     return FS_UNKNOWN;
310 }
311
312
313 /***********************************************************************
314  *           VOLUME_ReadCDSuperblock
315  */
316 static enum fs_type VOLUME_ReadCDSuperblock( HANDLE handle, BYTE *buff )
317 {
318     DWORD size, offs = VOLUME_FindCdRomDataBestVoldesc( handle );
319
320     if (!offs) return FS_UNKNOWN;
321
322     if (SetFilePointer( handle, offs, NULL, FILE_BEGIN ) != offs ||
323         !ReadFile( handle, buff, SUPERBLOCK_SIZE, &size, NULL ) ||
324         size != SUPERBLOCK_SIZE)
325         return FS_ERROR;
326
327     /* check for iso9660 present */
328     if (!memcmp(&buff[1], "CD001", 5)) return FS_ISO9660;
329     return FS_UNKNOWN;
330 }
331
332
333 /**************************************************************************
334  *                              VOLUME_GetSuperblockLabel
335  */
336 static void VOLUME_GetSuperblockLabel( enum fs_type type, const BYTE *superblock,
337                                        WCHAR *label, DWORD len )
338 {
339     const BYTE *label_ptr = NULL;
340     DWORD label_len;
341
342     switch(type)
343     {
344     case FS_ERROR:
345     case FS_UNKNOWN:
346         label_len = 0;
347         break;
348     case FS_FAT1216:
349         label_ptr = superblock + 0x2b;
350         label_len = 11;
351         break;
352     case FS_FAT32:
353         label_ptr = superblock + 0x47;
354         label_len = 11;
355         break;
356     case FS_ISO9660:
357         {
358             BYTE ver = superblock[0x5a];
359
360             if (superblock[0x58] == 0x25 && superblock[0x59] == 0x2f &&  /* Unicode ID */
361                 ((ver == 0x40) || (ver == 0x43) || (ver == 0x45)))
362             { /* yippee, unicode */
363                 unsigned int i;
364
365                 if (len > 17) len = 17;
366                 for (i = 0; i < len-1; i++)
367                     label[i] = (superblock[40+2*i] << 8) | superblock[41+2*i];
368                 label[i] = 0;
369                 while (i && label[i-1] == ' ') label[--i] = 0;
370                 return;
371             }
372             label_ptr = superblock + 40;
373             label_len = 32;
374             break;
375         }
376     }
377     if (label_len) RtlMultiByteToUnicodeN( label, (len-1) * sizeof(WCHAR),
378                                            &label_len, (char*)label_ptr, label_len );
379     label_len /= sizeof(WCHAR);
380     label[label_len] = 0;
381     while (label_len && label[label_len-1] == ' ') label[--label_len] = 0;
382 }
383
384
385 /**************************************************************************
386  *                              VOLUME_GetSuperblockSerial
387  */
388 static DWORD VOLUME_GetSuperblockSerial( enum fs_type type, const BYTE *superblock )
389 {
390     switch(type)
391     {
392     case FS_ERROR:
393     case FS_UNKNOWN:
394         break;
395     case FS_FAT1216:
396         return GETLONG( superblock, 0x27 );
397     case FS_FAT32:
398         return GETLONG( superblock, 0x33 );
399     case FS_ISO9660:
400         {
401             BYTE sum[4];
402             int i;
403
404             sum[0] = sum[1] = sum[2] = sum[3] = 0;
405             for (i = 0; i < 2048; i += 4)
406             {
407                 /* DON'T optimize this into DWORD !! (breaks overflow) */
408                 sum[0] += superblock[i+0];
409                 sum[1] += superblock[i+1];
410                 sum[2] += superblock[i+2];
411                 sum[3] += superblock[i+3];
412             }
413             /*
414              * OK, another braindead one... argh. Just believe it.
415              * Me$$ysoft chose to reverse the serial number in NT4/W2K.
416              * It's true and nobody will ever be able to change it.
417              */
418             if (GetVersion() & 0x80000000)
419                 return (sum[3] << 24) | (sum[2] << 16) | (sum[1] << 8) | sum[0];
420             else
421                 return (sum[0] << 24) | (sum[1] << 16) | (sum[2] << 8) | sum[3];
422         }
423     }
424     return 0;
425 }
426
427
428 /**************************************************************************
429  *                              VOLUME_GetAudioCDSerial
430  */
431 static DWORD VOLUME_GetAudioCDSerial( const CDROM_TOC *toc )
432 {
433     DWORD serial = 0;
434     int i;
435
436     for (i = 0; i <= toc->LastTrack - toc->FirstTrack; i++)
437         serial += ((toc->TrackData[i].Address[1] << 16) |
438                    (toc->TrackData[i].Address[2] << 8) |
439                    toc->TrackData[i].Address[3]);
440
441     /*
442      * dwStart, dwEnd collect the beginning and end of the disc respectively, in
443      * frames.
444      * There it is collected for correcting the serial when there are less than
445      * 3 tracks.
446      */
447     if (toc->LastTrack - toc->FirstTrack + 1 < 3)
448     {
449         DWORD dwStart = FRAME_OF_TOC(toc, toc->FirstTrack);
450         DWORD dwEnd = FRAME_OF_TOC(toc, toc->LastTrack + 1);
451         serial += dwEnd - dwStart;
452     }
453     return serial;
454 }
455
456
457 /***********************************************************************
458  *           GetVolumeInformationW   (KERNEL32.@)
459  */
460 BOOL WINAPI GetVolumeInformationW( LPCWSTR root, LPWSTR label, DWORD label_len,
461                                    DWORD *serial, DWORD *filename_len, DWORD *flags,
462                                    LPWSTR fsname, DWORD fsname_len )
463 {
464     static const WCHAR audiocdW[] = {'A','u','d','i','o',' ','C','D',0};
465     static const WCHAR fatW[] = {'F','A','T',0};
466     static const WCHAR ntfsW[] = {'N','T','F','S',0};
467     static const WCHAR cdfsW[] = {'C','D','F','S',0};
468
469     WCHAR device[] = {'\\','\\','.','\\','A',':',0};
470     HANDLE handle;
471     enum fs_type type = FS_UNKNOWN;
472
473     if (!root)
474     {
475         WCHAR path[MAX_PATH];
476         GetCurrentDirectoryW( MAX_PATH, path );
477         device[4] = path[0];
478     }
479     else
480     {
481         if (!root[0] || root[1] != ':')
482         {
483             SetLastError( ERROR_INVALID_NAME );
484             return FALSE;
485         }
486         device[4] = root[0];
487     }
488
489     /* try to open the device */
490
491     handle = CreateFileW( device, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE,
492                           NULL, OPEN_EXISTING, 0, 0 );
493     if (handle != INVALID_HANDLE_VALUE)
494     {
495         BYTE superblock[SUPERBLOCK_SIZE];
496         CDROM_TOC toc;
497         DWORD br;
498
499         /* check for audio CD */
500         /* FIXME: we only check the first track for now */
501         if (DeviceIoControl( handle, IOCTL_CDROM_READ_TOC, NULL, 0, &toc, sizeof(toc), &br, 0 ))
502         {
503             if (!(toc.TrackData[0].Control & 0x04))  /* audio track */
504             {
505                 TRACE( "%s: found audio CD\n", debugstr_w(device) );
506                 if (label) lstrcpynW( label, audiocdW, label_len );
507                 if (serial) *serial = VOLUME_GetAudioCDSerial( &toc );
508                 CloseHandle( handle );
509                 type = FS_ISO9660;
510                 goto fill_fs_info;
511             }
512             type = VOLUME_ReadCDSuperblock( handle, superblock );
513         }
514         else
515         {
516             type = VOLUME_ReadFATSuperblock( handle, superblock );
517             if (type == FS_UNKNOWN) type = VOLUME_ReadCDSuperblock( handle, superblock );
518         }
519         CloseHandle( handle );
520         TRACE( "%s: found fs type %d\n", debugstr_w(device), type );
521         if (type == FS_ERROR) return FALSE;
522
523         if (label && label_len) VOLUME_GetSuperblockLabel( type, superblock, label, label_len );
524         if (serial) *serial = VOLUME_GetSuperblockSerial( type, superblock );
525         goto fill_fs_info;
526     }
527     else TRACE( "cannot open device %s: err %ld\n", debugstr_w(device), GetLastError() );
528
529     /* we couldn't open the device, fallback to default strategy */
530
531     switch(GetDriveTypeW( root ))
532     {
533     case DRIVE_UNKNOWN:
534     case DRIVE_NO_ROOT_DIR:
535         SetLastError( ERROR_NOT_READY );
536         return FALSE;
537     case DRIVE_REMOVABLE:
538     case DRIVE_FIXED:
539     case DRIVE_REMOTE:
540     case DRIVE_RAMDISK:
541         type = FS_UNKNOWN;
542         break;
543     case DRIVE_CDROM:
544         type = FS_ISO9660;
545         break;
546     }
547
548     if (label && label_len)
549     {
550         WCHAR labelW[] = {'A',':','\\','.','w','i','n','d','o','w','s','-','l','a','b','e','l',0};
551
552         labelW[0] = device[4];
553         handle = CreateFileW( labelW, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
554                               OPEN_EXISTING, 0, 0 );
555         if (handle != INVALID_HANDLE_VALUE)
556         {
557             char buffer[256], *p;
558             DWORD size;
559
560             if (!ReadFile( handle, buffer, sizeof(buffer)-1, &size, NULL )) size = 0;
561             CloseHandle( handle );
562             p = buffer + size;
563             while (p > buffer && (p[-1] == ' ' || p[-1] == '\r' || p[-1] == '\n')) p--;
564             *p = 0;
565             if (!MultiByteToWideChar( CP_UNIXCP, 0, buffer, -1, label, label_len ))
566                 label[label_len-1] = 0;
567         }
568         else label[0] = 0;
569     }
570     if (serial)
571     {
572         WCHAR serialW[] = {'A',':','\\','.','w','i','n','d','o','w','s','-','s','e','r','i','a','l',0};
573
574         serialW[0] = device[4];
575         handle = CreateFileW( serialW, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
576                               OPEN_EXISTING, 0, 0 );
577         if (handle != INVALID_HANDLE_VALUE)
578         {
579             char buffer[32];
580             DWORD size;
581
582             if (!ReadFile( handle, buffer, sizeof(buffer)-1, &size, NULL )) size = 0;
583             CloseHandle( handle );
584             buffer[size] = 0;
585             *serial = strtoul( buffer, NULL, 16 );
586         }
587         else *serial = 0;
588     }
589
590 fill_fs_info:  /* now fill in the information that depends on the file system type */
591
592     switch(type)
593     {
594     case FS_ISO9660:
595         if (fsname) lstrcpynW( fsname, cdfsW, fsname_len );
596         if (filename_len) *filename_len = 221;
597         if (flags) *flags = FILE_READ_ONLY_VOLUME;
598         break;
599     case FS_FAT1216:
600     case FS_FAT32:
601         if (fsname) lstrcpynW( fsname, fatW, fsname_len );
602         if (filename_len) *filename_len = 255;
603         if (flags) *flags = FILE_CASE_PRESERVED_NAMES;  /* FIXME */
604         break;
605     default:
606         if (fsname) lstrcpynW( fsname, ntfsW, fsname_len );
607         if (filename_len) *filename_len = 255;
608         if (flags) *flags = FILE_CASE_PRESERVED_NAMES;
609         break;
610     }
611     return TRUE;
612 }
613
614
615 /***********************************************************************
616  *           GetVolumeInformationA   (KERNEL32.@)
617  */
618 BOOL WINAPI GetVolumeInformationA( LPCSTR root, LPSTR label,
619                                    DWORD label_len, DWORD *serial,
620                                    DWORD *filename_len, DWORD *flags,
621                                    LPSTR fsname, DWORD fsname_len )
622 {
623     WCHAR *rootW = NULL;
624     LPWSTR labelW, fsnameW;
625     BOOL ret;
626
627     if (root && !(rootW = FILE_name_AtoW( root, FALSE ))) return FALSE;
628
629     labelW = label ? HeapAlloc(GetProcessHeap(), 0, label_len * sizeof(WCHAR)) : NULL;
630     fsnameW = fsname ? HeapAlloc(GetProcessHeap(), 0, fsname_len * sizeof(WCHAR)) : NULL;
631
632     if ((ret = GetVolumeInformationW(rootW, labelW, label_len, serial,
633                                     filename_len, flags, fsnameW, fsname_len)))
634     {
635         if (label) FILE_name_WtoA( labelW, -1, label, label_len );
636         if (fsname) FILE_name_WtoA( fsnameW, -1, fsname, fsname_len );
637     }
638
639     HeapFree( GetProcessHeap(), 0, labelW );
640     HeapFree( GetProcessHeap(), 0, fsnameW );
641     return ret;
642 }
643
644
645
646 /***********************************************************************
647  *           SetVolumeLabelW   (KERNEL32.@)
648  */
649 BOOL WINAPI SetVolumeLabelW( LPCWSTR root, LPCWSTR label )
650 {
651     WCHAR device[] = {'\\','\\','.','\\','A',':',0};
652     HANDLE handle;
653     enum fs_type type = FS_UNKNOWN;
654
655     if (!root)
656     {
657         WCHAR path[MAX_PATH];
658         GetCurrentDirectoryW( MAX_PATH, path );
659         device[4] = path[0];
660     }
661     else
662     {
663         if (!root[0] || root[1] != ':')
664         {
665             SetLastError( ERROR_INVALID_NAME );
666             return FALSE;
667         }
668         device[4] = root[0];
669     }
670
671     /* try to open the device */
672
673     handle = CreateFileW( device, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE,
674                           NULL, OPEN_EXISTING, 0, 0 );
675     if (handle != INVALID_HANDLE_VALUE)
676     {
677         BYTE superblock[SUPERBLOCK_SIZE];
678
679         type = VOLUME_ReadFATSuperblock( handle, superblock );
680         if (type == FS_UNKNOWN) type = VOLUME_ReadCDSuperblock( handle, superblock );
681         CloseHandle( handle );
682         if (type != FS_UNKNOWN)
683         {
684             /* we can't set the label on FAT or CDROM file systems */
685             TRACE( "cannot set label on device %s type %d\n", debugstr_w(device), type );
686             SetLastError( ERROR_ACCESS_DENIED );
687             return FALSE;
688         }
689     }
690     else
691     {
692         TRACE( "cannot open device %s: err %ld\n", debugstr_w(device), GetLastError() );
693         if (GetLastError() == ERROR_ACCESS_DENIED) return FALSE;
694     }
695
696     /* we couldn't open the device, fallback to default strategy */
697
698     switch(GetDriveTypeW( root ))
699     {
700     case DRIVE_UNKNOWN:
701     case DRIVE_NO_ROOT_DIR:
702         SetLastError( ERROR_NOT_READY );
703         break;
704     case DRIVE_REMOVABLE:
705     case DRIVE_FIXED:
706         {
707             WCHAR labelW[] = {'A',':','\\','.','w','i','n','d','o','w','s','-','l','a','b','e','l',0};
708
709             labelW[0] = device[4];
710             handle = CreateFileW( labelW, GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
711                                   CREATE_ALWAYS, 0, 0 );
712             if (handle != INVALID_HANDLE_VALUE)
713             {
714                 char buffer[64];
715                 DWORD size;
716
717                 if (!WideCharToMultiByte( CP_UNIXCP, 0, label, -1, buffer, sizeof(buffer), NULL, NULL ))
718                     buffer[sizeof(buffer)-1] = 0;
719                 WriteFile( handle, buffer, strlen(buffer), &size, NULL );
720                 CloseHandle( handle );
721                 return TRUE;
722             }
723             break;
724         }
725     case DRIVE_REMOTE:
726     case DRIVE_RAMDISK:
727     case DRIVE_CDROM:
728         SetLastError( ERROR_ACCESS_DENIED );
729         break;
730     }
731     return FALSE;
732 }
733
734 /***********************************************************************
735  *           SetVolumeLabelA   (KERNEL32.@)
736  */
737 BOOL WINAPI SetVolumeLabelA(LPCSTR root, LPCSTR volname)
738 {
739     WCHAR *rootW = NULL, *volnameW = NULL;
740     BOOL ret;
741
742     if (root && !(rootW = FILE_name_AtoW( root, FALSE ))) return FALSE;
743     if (volname && !(volnameW = FILE_name_AtoW( volname, TRUE ))) return FALSE;
744     ret = SetVolumeLabelW( rootW, volnameW );
745     HeapFree( GetProcessHeap(), 0, volnameW );
746     return ret;
747 }
748
749
750 /***********************************************************************
751  *           GetVolumeNameForVolumeMountPointW   (KERNEL32.@)
752  */
753 BOOL WINAPI GetVolumeNameForVolumeMountPointW(LPCWSTR str, LPWSTR dst, DWORD size)
754 {
755     FIXME("(%s, %p, %lx): stub\n", debugstr_w(str), dst, size);
756     return 0;
757 }
758
759
760 /***********************************************************************
761  *           DefineDosDeviceW       (KERNEL32.@)
762  */
763 BOOL WINAPI DefineDosDeviceW( DWORD flags, LPCWSTR devname, LPCWSTR targetpath )
764 {
765     DWORD len, dosdev;
766     BOOL ret = FALSE;
767     char *path = NULL, *target, *p;
768
769     TRACE("%lx, %s, %s\n", flags, debugstr_w(devname), debugstr_w(targetpath));
770
771     if (!(flags & DDD_REMOVE_DEFINITION))
772     {
773         if (!(flags & DDD_RAW_TARGET_PATH))
774         {
775             FIXME( "(0x%08lx,%s,%s) DDD_RAW_TARGET_PATH flag not set, not supported yet\n",
776                    flags, debugstr_w(devname), debugstr_w(targetpath) );
777             SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
778             return FALSE;
779         }
780
781         len = WideCharToMultiByte( CP_UNIXCP, 0, targetpath, -1, NULL, 0, NULL, NULL );
782         if ((target = HeapAlloc( GetProcessHeap(), 0, len )))
783         {
784             WideCharToMultiByte( CP_UNIXCP, 0, targetpath, -1, target, len, NULL, NULL );
785             for (p = target; *p; p++) if (*p == '\\') *p = '/';
786         }
787         else
788         {
789             SetLastError( ERROR_NOT_ENOUGH_MEMORY );
790             return FALSE;
791         }
792     }
793     else target = NULL;
794
795     /* first check for a DOS device */
796
797     if ((dosdev = RtlIsDosDeviceName_U( devname )))
798     {
799         WCHAR name[5];
800
801         memcpy( name, devname + HIWORD(dosdev)/sizeof(WCHAR), LOWORD(dosdev) );
802         name[LOWORD(dosdev)/sizeof(WCHAR)] = 0;
803         path = get_dos_device_path( name );
804     }
805     else if (isalphaW(devname[0]) && devname[1] == ':' && !devname[2])  /* drive mapping */
806     {
807         path = get_dos_device_path( devname );
808     }
809     else SetLastError( ERROR_FILE_NOT_FOUND );
810
811     if (path)
812     {
813         if (target)
814         {
815             TRACE( "creating symlink %s -> %s\n", path, target );
816             unlink( path );
817             if (!symlink( target, path )) ret = TRUE;
818             else FILE_SetDosError();
819         }
820         else
821         {
822             TRACE( "removing symlink %s\n", path );
823             if (!unlink( path )) ret = TRUE;
824             else FILE_SetDosError();
825         }
826         HeapFree( GetProcessHeap(), 0, path );
827     }
828     HeapFree( GetProcessHeap(), 0, target );
829     return ret;
830 }
831
832
833 /***********************************************************************
834  *           DefineDosDeviceA       (KERNEL32.@)
835  */
836 BOOL WINAPI DefineDosDeviceA(DWORD flags, LPCSTR devname, LPCSTR targetpath)
837 {
838     WCHAR *devW, *targetW = NULL;
839     BOOL ret;
840
841     if (!(devW = FILE_name_AtoW( devname, FALSE ))) return FALSE;
842     if (targetpath && !(targetW = FILE_name_AtoW( targetpath, TRUE ))) return FALSE;
843     ret = DefineDosDeviceW(flags, devW, targetW);
844     HeapFree( GetProcessHeap(), 0, targetW );
845     return ret;
846 }
847
848
849 /***********************************************************************
850  *           QueryDosDeviceW   (KERNEL32.@)
851  *
852  * returns array of strings terminated by \0, terminated by \0
853  */
854 DWORD WINAPI QueryDosDeviceW( LPCWSTR devname, LPWSTR target, DWORD bufsize )
855 {
856     static const WCHAR auxW[] = {'A','U','X',0};
857     static const WCHAR nulW[] = {'N','U','L',0};
858     static const WCHAR prnW[] = {'P','R','N',0};
859     static const WCHAR comW[] = {'C','O','M',0};
860     static const WCHAR lptW[] = {'L','P','T',0};
861     static const WCHAR rootW[] = {'A',':','\\',0};
862     static const WCHAR com0W[] = {'\\','?','?','\\','C','O','M','0',0};
863     static const WCHAR com1W[] = {'\\','D','o','s','D','e','v','i','c','e','s','\\','C','O','M','1',0,0};
864     static const WCHAR lpt1W[] = {'\\','D','o','s','D','e','v','i','c','e','s','\\','L','P','T','1',0,0};
865
866     UNICODE_STRING nt_name;
867     ANSI_STRING unix_name;
868     WCHAR nt_buffer[10];
869     NTSTATUS status;
870
871     if (!bufsize)
872     {
873         SetLastError( ERROR_INSUFFICIENT_BUFFER );
874         return 0;
875     }
876
877     if (devname)
878     {
879         WCHAR *p, name[5];
880         char *path, *link;
881         DWORD dosdev, ret = 0;
882
883         if ((dosdev = RtlIsDosDeviceName_U( devname )))
884         {
885             memcpy( name, devname + HIWORD(dosdev)/sizeof(WCHAR), LOWORD(dosdev) );
886             name[LOWORD(dosdev)/sizeof(WCHAR)] = 0;
887         }
888         else if (devname[0] && devname[1] == ':' && !devname[2])
889         {
890             memcpy( name, devname, 3 * sizeof(WCHAR) );
891         }
892         else
893         {
894             SetLastError( ERROR_BAD_PATHNAME );
895             return 0;
896         }
897
898         if (!(path = get_dos_device_path( name ))) return 0;
899         link = read_symlink( path );
900         HeapFree( GetProcessHeap(), 0, path );
901
902         if (link)
903         {
904             ret = MultiByteToWideChar( CP_UNIXCP, 0, link, -1, target, bufsize );
905             HeapFree( GetProcessHeap(), 0, link );
906         }
907         else if (dosdev)  /* look for device defaults */
908         {
909             if (!strcmpiW( name, auxW ))
910             {
911                 if (bufsize >= sizeof(com1W)/sizeof(WCHAR))
912                 {
913                     memcpy( target, com1W, sizeof(com1W) );
914                     ret = sizeof(com1W)/sizeof(WCHAR);
915                 }
916                 else SetLastError( ERROR_INSUFFICIENT_BUFFER );
917                 return ret;
918             }
919             if (!strcmpiW( name, prnW ))
920             {
921                 if (bufsize >= sizeof(lpt1W)/sizeof(WCHAR))
922                 {
923                     memcpy( target, lpt1W, sizeof(lpt1W) );
924                     ret = sizeof(lpt1W)/sizeof(WCHAR);
925                 }
926                 else SetLastError( ERROR_INSUFFICIENT_BUFFER );
927                 return ret;
928             }
929
930             nt_buffer[0] = '\\';
931             nt_buffer[1] = '?';
932             nt_buffer[2] = '?';
933             nt_buffer[3] = '\\';
934             strcpyW( nt_buffer + 4, name );
935             RtlInitUnicodeString( &nt_name, nt_buffer );
936             status = wine_nt_to_unix_file_name( &nt_name, &unix_name, FILE_OPEN, TRUE );
937             if (status) SetLastError( RtlNtStatusToDosError(status) );
938             else
939             {
940                 ret = MultiByteToWideChar( CP_UNIXCP, 0, unix_name.Buffer, -1, target, bufsize );
941                 RtlFreeAnsiString( &unix_name );
942             }
943         }
944
945         if (ret)
946         {
947             if (ret < bufsize) target[ret++] = 0;  /* add an extra null */
948             for (p = target; *p; p++) if (*p == '/') *p = '\\';
949         }
950
951         return ret;
952     }
953     else  /* return a list of all devices */
954     {
955         WCHAR *p = target;
956         int i;
957
958         if (bufsize <= (sizeof(auxW)+sizeof(nulW)+sizeof(prnW))/sizeof(WCHAR))
959         {
960             SetLastError( ERROR_INSUFFICIENT_BUFFER );
961             return 0;
962         }
963
964         memcpy( p, auxW, sizeof(auxW) );
965         p += sizeof(auxW) / sizeof(WCHAR);
966         memcpy( p, nulW, sizeof(nulW) );
967         p += sizeof(nulW) / sizeof(WCHAR);
968         memcpy( p, prnW, sizeof(prnW) );
969         p += sizeof(prnW) / sizeof(WCHAR);
970
971         strcpyW( nt_buffer, com0W );
972         RtlInitUnicodeString( &nt_name, nt_buffer );
973
974         for (i = 1; i <= 9; i++)
975         {
976             nt_buffer[7] = '0' + i;
977             if (!wine_nt_to_unix_file_name( &nt_name, &unix_name, FILE_OPEN, TRUE ))
978             {
979                 RtlFreeAnsiString( &unix_name );
980                 if (p + 5 >= target + bufsize)
981                 {
982                     SetLastError( ERROR_INSUFFICIENT_BUFFER );
983                     return 0;
984                 }
985                 strcpyW( p, comW );
986                 p[3] = '0' + i;
987                 p[4] = 0;
988                 p += 5;
989             }
990         }
991         strcpyW( nt_buffer + 4, lptW );
992         for (i = 1; i <= 9; i++)
993         {
994             nt_buffer[7] = '0' + i;
995             if (!wine_nt_to_unix_file_name( &nt_name, &unix_name, FILE_OPEN, TRUE ))
996             {
997                 RtlFreeAnsiString( &unix_name );
998                 if (p + 5 >= target + bufsize)
999                 {
1000                     SetLastError( ERROR_INSUFFICIENT_BUFFER );
1001                     return 0;
1002                 }
1003                 strcpyW( p, lptW );
1004                 p[3] = '0' + i;
1005                 p[4] = 0;
1006                 p += 5;
1007             }
1008         }
1009
1010         strcpyW( nt_buffer + 4, rootW );
1011         RtlInitUnicodeString( &nt_name, nt_buffer );
1012
1013         for (i = 0; i < 26; i++)
1014         {
1015             nt_buffer[4] = 'a' + i;
1016             if (!wine_nt_to_unix_file_name( &nt_name, &unix_name, FILE_OPEN, TRUE ))
1017             {
1018                 RtlFreeAnsiString( &unix_name );
1019                 if (p + 3 >= target + bufsize)
1020                 {
1021                     SetLastError( ERROR_INSUFFICIENT_BUFFER );
1022                     return 0;
1023                 }
1024                 *p++ = 'A' + i;
1025                 *p++ = ':';
1026                 *p++ = 0;
1027             }
1028         }
1029         *p++ = 0;  /* terminating null */
1030         return p - target;
1031     }
1032 }
1033
1034
1035 /***********************************************************************
1036  *           QueryDosDeviceA   (KERNEL32.@)
1037  *
1038  * returns array of strings terminated by \0, terminated by \0
1039  */
1040 DWORD WINAPI QueryDosDeviceA( LPCSTR devname, LPSTR target, DWORD bufsize )
1041 {
1042     DWORD ret = 0, retW;
1043     WCHAR *devnameW = NULL;
1044     LPWSTR targetW;
1045
1046     if (devname && !(devnameW = FILE_name_AtoW( devname, FALSE ))) return 0;
1047
1048     targetW = HeapAlloc( GetProcessHeap(),0, bufsize * sizeof(WCHAR) );
1049     if (!targetW)
1050     {
1051         SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1052         return 0;
1053     }
1054
1055     retW = QueryDosDeviceW(devnameW, targetW, bufsize);
1056
1057     ret = FILE_name_WtoA( targetW, retW, target, bufsize );
1058
1059     HeapFree(GetProcessHeap(), 0, targetW);
1060     return ret;
1061 }
1062
1063
1064 /***********************************************************************
1065  *           GetLogicalDrives   (KERNEL32.@)
1066  */
1067 DWORD WINAPI GetLogicalDrives(void)
1068 {
1069     const char *config_dir = wine_get_config_dir();
1070     struct stat st;
1071     char *buffer, *dev;
1072     DWORD ret = 0;
1073     int i;
1074
1075     if (!(buffer = HeapAlloc( GetProcessHeap(), 0, strlen(config_dir) + sizeof("/dosdevices/a:") )))
1076     {
1077         SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1078         return 0;
1079     }
1080     strcpy( buffer, config_dir );
1081     strcat( buffer, "/dosdevices/a:" );
1082     dev = buffer + strlen(buffer) - 2;
1083
1084     for (i = 0; i < 26; i++)
1085     {
1086         *dev = 'a' + i;
1087         if (!stat( buffer, &st )) ret |= (1 << i);
1088     }
1089     HeapFree( GetProcessHeap(), 0, buffer );
1090     return ret;
1091 }
1092
1093
1094 /***********************************************************************
1095  *           GetLogicalDriveStringsA   (KERNEL32.@)
1096  */
1097 UINT WINAPI GetLogicalDriveStringsA( UINT len, LPSTR buffer )
1098 {
1099     DWORD drives = GetLogicalDrives();
1100     UINT drive, count;
1101
1102     for (drive = count = 0; drive < 26; drive++) if (drives & (1 << drive)) count++;
1103     if ((count * 4) + 1 > len) return count * 4 + 1;
1104
1105     for (drive = 0; drive < 26; drive++)
1106     {
1107         if (drives & (1 << drive))
1108         {
1109             *buffer++ = 'a' + drive;
1110             *buffer++ = ':';
1111             *buffer++ = '\\';
1112             *buffer++ = 0;
1113         }
1114     }
1115     *buffer = 0;
1116     return count * 4;
1117 }
1118
1119
1120 /***********************************************************************
1121  *           GetLogicalDriveStringsW   (KERNEL32.@)
1122  */
1123 UINT WINAPI GetLogicalDriveStringsW( UINT len, LPWSTR buffer )
1124 {
1125     DWORD drives = GetLogicalDrives();
1126     UINT drive, count;
1127
1128     for (drive = count = 0; drive < 26; drive++) if (drives & (1 << drive)) count++;
1129     if ((count * 4) + 1 > len) return count * 4 + 1;
1130
1131     for (drive = 0; drive < 26; drive++)
1132     {
1133         if (drives & (1 << drive))
1134         {
1135             *buffer++ = 'a' + drive;
1136             *buffer++ = ':';
1137             *buffer++ = '\\';
1138             *buffer++ = 0;
1139         }
1140     }
1141     *buffer = 0;
1142     return count * 4;
1143 }
1144
1145
1146 /***********************************************************************
1147  *           GetDriveTypeW   (KERNEL32.@)
1148  *
1149  * Returns the type of the disk drive specified. If root is NULL the
1150  * root of the current directory is used.
1151  *
1152  * RETURNS
1153  *
1154  *  Type of drive (from Win32 SDK):
1155  *
1156  *   DRIVE_UNKNOWN     unable to find out anything about the drive
1157  *   DRIVE_NO_ROOT_DIR nonexistent root dir
1158  *   DRIVE_REMOVABLE   the disk can be removed from the machine
1159  *   DRIVE_FIXED       the disk cannot be removed from the machine
1160  *   DRIVE_REMOTE      network disk
1161  *   DRIVE_CDROM       CDROM drive
1162  *   DRIVE_RAMDISK     virtual disk in RAM
1163  */
1164 UINT WINAPI GetDriveTypeW(LPCWSTR root) /* [in] String describing drive */
1165 {
1166     FILE_FS_DEVICE_INFORMATION info;
1167     IO_STATUS_BLOCK io;
1168     NTSTATUS status;
1169     HANDLE handle;
1170     UINT ret;
1171
1172     if (!open_device_root( root, &handle )) return DRIVE_NO_ROOT_DIR;
1173
1174     status = NtQueryVolumeInformationFile( handle, &io, &info, sizeof(info), FileFsDeviceInformation );
1175     NtClose( handle );
1176     if (status != STATUS_SUCCESS)
1177     {
1178         SetLastError( RtlNtStatusToDosError(status) );
1179         ret = DRIVE_UNKNOWN;
1180     }
1181     else if ((ret = get_registry_drive_type( root )) == DRIVE_UNKNOWN)
1182     {
1183         switch (info.DeviceType)
1184         {
1185         case FILE_DEVICE_CD_ROM_FILE_SYSTEM:  ret = DRIVE_CDROM; break;
1186         case FILE_DEVICE_VIRTUAL_DISK:        ret = DRIVE_RAMDISK; break;
1187         case FILE_DEVICE_NETWORK_FILE_SYSTEM: ret = DRIVE_REMOTE; break;
1188         case FILE_DEVICE_DISK_FILE_SYSTEM:
1189             if (info.Characteristics & FILE_REMOTE_DEVICE) ret = DRIVE_REMOTE;
1190             else if (info.Characteristics & FILE_REMOVABLE_MEDIA) ret = DRIVE_REMOVABLE;
1191             else ret = DRIVE_FIXED;
1192             break;
1193         default:
1194             ret = DRIVE_UNKNOWN;
1195             break;
1196         }
1197     }
1198     TRACE( "%s -> %d\n", debugstr_w(root), ret );
1199     return ret;
1200 }
1201
1202
1203 /***********************************************************************
1204  *           GetDriveTypeA   (KERNEL32.@)
1205  *
1206  * See GetDriveTypeW.
1207  */
1208 UINT WINAPI GetDriveTypeA( LPCSTR root )
1209 {
1210     WCHAR *rootW = NULL;
1211
1212     if (root && !(rootW = FILE_name_AtoW( root, FALSE ))) return DRIVE_NO_ROOT_DIR;
1213     return GetDriveTypeW( rootW );
1214 }
1215
1216
1217 /***********************************************************************
1218  *           GetDiskFreeSpaceExW   (KERNEL32.@)
1219  *
1220  *  This function is used to acquire the size of the available and
1221  *  total space on a logical volume.
1222  *
1223  * RETURNS
1224  *
1225  *  Zero on failure, nonzero upon success. Use GetLastError to obtain
1226  *  detailed error information.
1227  *
1228  */
1229 BOOL WINAPI GetDiskFreeSpaceExW( LPCWSTR root, PULARGE_INTEGER avail,
1230                                  PULARGE_INTEGER total, PULARGE_INTEGER totalfree )
1231 {
1232     FILE_FS_SIZE_INFORMATION info;
1233     IO_STATUS_BLOCK io;
1234     NTSTATUS status;
1235     HANDLE handle;
1236     UINT units;
1237
1238     TRACE( "%s,%p,%p,%p\n", debugstr_w(root), avail, total, totalfree );
1239
1240     if (!open_device_root( root, &handle )) return FALSE;
1241
1242     status = NtQueryVolumeInformationFile( handle, &io, &info, sizeof(info), FileFsSizeInformation );
1243     NtClose( handle );
1244     if (status != STATUS_SUCCESS)
1245     {
1246         SetLastError( RtlNtStatusToDosError(status) );
1247         return FALSE;
1248     }
1249
1250     units = info.SectorsPerAllocationUnit * info.BytesPerSector;
1251     if (total) total->QuadPart = info.TotalAllocationUnits.QuadPart * units;
1252     if (totalfree) totalfree->QuadPart = info.AvailableAllocationUnits.QuadPart * units;
1253     /* FIXME: this one should take quotas into account */
1254     if (avail) avail->QuadPart = info.AvailableAllocationUnits.QuadPart * units;
1255     return TRUE;
1256 }
1257
1258
1259 /***********************************************************************
1260  *           GetDiskFreeSpaceExA   (KERNEL32.@)
1261  *
1262  * See GetDiskFreeSpaceExW.
1263  */
1264 BOOL WINAPI GetDiskFreeSpaceExA( LPCSTR root, PULARGE_INTEGER avail,
1265                                  PULARGE_INTEGER total, PULARGE_INTEGER totalfree )
1266 {
1267     WCHAR *rootW = NULL;
1268
1269     if (root && !(rootW = FILE_name_AtoW( root, FALSE ))) return FALSE;
1270     return GetDiskFreeSpaceExW( rootW, avail, total, totalfree );
1271 }
1272
1273
1274 /***********************************************************************
1275  *           GetDiskFreeSpaceW   (KERNEL32.@)
1276  */
1277 BOOL WINAPI GetDiskFreeSpaceW( LPCWSTR root, LPDWORD cluster_sectors,
1278                                LPDWORD sector_bytes, LPDWORD free_clusters,
1279                                LPDWORD total_clusters )
1280 {
1281     FILE_FS_SIZE_INFORMATION info;
1282     IO_STATUS_BLOCK io;
1283     NTSTATUS status;
1284     HANDLE handle;
1285     UINT units;
1286
1287     TRACE( "%s,%p,%p,%p,%p\n", debugstr_w(root),
1288            cluster_sectors, sector_bytes, free_clusters, total_clusters );
1289
1290     if (!open_device_root( root, &handle )) return FALSE;
1291
1292     status = NtQueryVolumeInformationFile( handle, &io, &info, sizeof(info), FileFsSizeInformation );
1293     NtClose( handle );
1294     if (status != STATUS_SUCCESS)
1295     {
1296         SetLastError( RtlNtStatusToDosError(status) );
1297         return FALSE;
1298     }
1299
1300     units = info.SectorsPerAllocationUnit * info.BytesPerSector;
1301
1302     if( GetVersion() & 0x80000000) {    /* win3.x, 9x, ME */
1303         /* cap the size and available at 2GB as per specs */
1304         if (info.TotalAllocationUnits.QuadPart * units > 0x7fffffff) {
1305             info.TotalAllocationUnits.QuadPart = 0x7fffffff / units;
1306             if (info.AvailableAllocationUnits.QuadPart * units > 0x7fffffff)
1307                 info.AvailableAllocationUnits.QuadPart = 0x7fffffff / units;
1308         }
1309         /* nr. of clusters is always <= 65335 */
1310         while( info.TotalAllocationUnits.QuadPart > 65535 ) {
1311             info.TotalAllocationUnits.QuadPart /= 2;
1312             info.AvailableAllocationUnits.QuadPart /= 2;
1313             info.SectorsPerAllocationUnit *= 2;
1314         }
1315     }
1316
1317     if (cluster_sectors) *cluster_sectors = info.SectorsPerAllocationUnit;
1318     if (sector_bytes) *sector_bytes = info.BytesPerSector;
1319     if (free_clusters) *free_clusters = info.AvailableAllocationUnits.u.LowPart;
1320     if (total_clusters) *total_clusters = info.TotalAllocationUnits.u.LowPart;
1321     return TRUE;
1322 }
1323
1324
1325 /***********************************************************************
1326  *           GetDiskFreeSpaceA   (KERNEL32.@)
1327  */
1328 BOOL WINAPI GetDiskFreeSpaceA( LPCSTR root, LPDWORD cluster_sectors,
1329                                LPDWORD sector_bytes, LPDWORD free_clusters,
1330                                LPDWORD total_clusters )
1331 {
1332     WCHAR *rootW = NULL;
1333
1334     if (root && !(rootW = FILE_name_AtoW( root, FALSE ))) return FALSE;
1335     return GetDiskFreeSpaceW( rootW, cluster_sectors, sector_bytes, free_clusters, total_clusters );
1336 }
1337
1338 /***********************************************************************
1339  *           GetVolumePathNameA   (KERNEL32.@)
1340  */
1341 BOOL WINAPI GetVolumePathNameA(LPCSTR filename, LPSTR volumepathname, DWORD buflen)
1342 {
1343     FIXME("(%s, %p, %ld), stub!\n", debugstr_a(filename), volumepathname, buflen);
1344     SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1345     return FALSE;
1346 }
1347
1348 /***********************************************************************
1349  *           GetVolumePathNameW   (KERNEL32.@)
1350  */
1351 BOOL WINAPI GetVolumePathNameW(LPCWSTR filename, LPWSTR volumepathname, DWORD buflen)
1352 {
1353     FIXME("(%s, %p, %ld), stub!\n", debugstr_w(filename), volumepathname, buflen);
1354     SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1355     return FALSE;
1356 }