- implement encoding and decoding of enumerated types, unsigned
[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 "windef.h"
33 #include "winbase.h"
34 #include "winreg.h"
35 #include "winnls.h"
36 #include "winternl.h"
37 #include "ntstatus.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     HKEY 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, 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_SetSuperblockLabel
387  */
388 static BOOL VOLUME_SetSuperblockLabel( enum fs_type type, HANDLE handle, const WCHAR *label )
389 {
390     BYTE label_data[11];
391     DWORD offset, len;
392
393     switch(type)
394     {
395     case FS_FAT1216:
396         offset = 0x2b;
397         break;
398     case FS_FAT32:
399         offset = 0x47;
400         break;
401     default:
402         SetLastError( ERROR_ACCESS_DENIED );
403         return FALSE;
404     }
405     RtlUnicodeToMultiByteN( label_data, sizeof(label_data), &len,
406                             label, strlenW(label) * sizeof(WCHAR) );
407     if (len < sizeof(label_data))
408         memset( label_data + len, ' ', sizeof(label_data) - len );
409
410     return (SetFilePointer( handle, offset, NULL, FILE_BEGIN ) == offset &&
411             WriteFile( handle, label_data, sizeof(label_data), &len, NULL ));
412 }
413
414
415 /**************************************************************************
416  *                              VOLUME_GetSuperblockSerial
417  */
418 static DWORD VOLUME_GetSuperblockSerial( enum fs_type type, const BYTE *superblock )
419 {
420     switch(type)
421     {
422     case FS_ERROR:
423     case FS_UNKNOWN:
424         break;
425     case FS_FAT1216:
426         return GETLONG( superblock, 0x27 );
427     case FS_FAT32:
428         return GETLONG( superblock, 0x33 );
429     case FS_ISO9660:
430         {
431             BYTE sum[4];
432             int i;
433
434             sum[0] = sum[1] = sum[2] = sum[3] = 0;
435             for (i = 0; i < 2048; i += 4)
436             {
437                 /* DON'T optimize this into DWORD !! (breaks overflow) */
438                 sum[0] += superblock[i+0];
439                 sum[1] += superblock[i+1];
440                 sum[2] += superblock[i+2];
441                 sum[3] += superblock[i+3];
442             }
443             /*
444              * OK, another braindead one... argh. Just believe it.
445              * Me$$ysoft chose to reverse the serial number in NT4/W2K.
446              * It's true and nobody will ever be able to change it.
447              */
448             if (GetVersion() & 0x80000000)
449                 return (sum[3] << 24) | (sum[2] << 16) | (sum[1] << 8) | sum[0];
450             else
451                 return (sum[0] << 24) | (sum[1] << 16) | (sum[2] << 8) | sum[3];
452         }
453     }
454     return 0;
455 }
456
457
458 /**************************************************************************
459  *                              VOLUME_GetAudioCDSerial
460  */
461 static DWORD VOLUME_GetAudioCDSerial( const CDROM_TOC *toc )
462 {
463     DWORD serial = 0;
464     int i;
465
466     for (i = 0; i <= toc->LastTrack - toc->FirstTrack; i++)
467         serial += ((toc->TrackData[i].Address[1] << 16) |
468                    (toc->TrackData[i].Address[2] << 8) |
469                    toc->TrackData[i].Address[3]);
470
471     /*
472      * dwStart, dwEnd collect the beginning and end of the disc respectively, in
473      * frames.
474      * There it is collected for correcting the serial when there are less than
475      * 3 tracks.
476      */
477     if (toc->LastTrack - toc->FirstTrack + 1 < 3)
478     {
479         DWORD dwStart = FRAME_OF_TOC(toc, toc->FirstTrack);
480         DWORD dwEnd = FRAME_OF_TOC(toc, toc->LastTrack + 1);
481         serial += dwEnd - dwStart;
482     }
483     return serial;
484 }
485
486
487 /***********************************************************************
488  *           GetVolumeInformationW   (KERNEL32.@)
489  */
490 BOOL WINAPI GetVolumeInformationW( LPCWSTR root, LPWSTR label, DWORD label_len,
491                                    DWORD *serial, DWORD *filename_len, DWORD *flags,
492                                    LPWSTR fsname, DWORD fsname_len )
493 {
494     static const WCHAR audiocdW[] = {'A','u','d','i','o',' ','C','D',0};
495     static const WCHAR fatW[] = {'F','A','T',0};
496     static const WCHAR ntfsW[] = {'N','T','F','S',0};
497     static const WCHAR cdfsW[] = {'C','D','F','S',0};
498
499     WCHAR device[] = {'\\','\\','.','\\','A',':',0};
500     HANDLE handle;
501     enum fs_type type = FS_UNKNOWN;
502
503     if (!root)
504     {
505         WCHAR path[MAX_PATH];
506         GetCurrentDirectoryW( MAX_PATH, path );
507         device[4] = path[0];
508     }
509     else
510     {
511         if (!root[0] || root[1] != ':')
512         {
513             SetLastError( ERROR_INVALID_NAME );
514             return FALSE;
515         }
516         device[4] = root[0];
517     }
518
519     /* try to open the device */
520
521     handle = CreateFileW( device, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE,
522                           NULL, OPEN_EXISTING, 0, 0 );
523     if (handle != INVALID_HANDLE_VALUE)
524     {
525         BYTE superblock[SUPERBLOCK_SIZE];
526         CDROM_TOC toc;
527         DWORD br;
528
529         /* check for audio CD */
530         /* FIXME: we only check the first track for now */
531         if (DeviceIoControl( handle, IOCTL_CDROM_READ_TOC, NULL, 0, &toc, sizeof(toc), &br, 0 ))
532         {
533             if (!(toc.TrackData[0].Control & 0x04))  /* audio track */
534             {
535                 TRACE( "%s: found audio CD\n", debugstr_w(device) );
536                 if (label) lstrcpynW( label, audiocdW, label_len );
537                 if (serial) *serial = VOLUME_GetAudioCDSerial( &toc );
538                 CloseHandle( handle );
539                 type = FS_ISO9660;
540                 goto fill_fs_info;
541             }
542             type = VOLUME_ReadCDSuperblock( handle, superblock );
543         }
544         else
545         {
546             type = VOLUME_ReadFATSuperblock( handle, superblock );
547             if (type == FS_UNKNOWN) type = VOLUME_ReadCDSuperblock( handle, superblock );
548         }
549         CloseHandle( handle );
550         TRACE( "%s: found fs type %d\n", debugstr_w(device), type );
551         if (type == FS_ERROR) return FALSE;
552
553         if (label && label_len) VOLUME_GetSuperblockLabel( type, superblock, label, label_len );
554         if (serial) *serial = VOLUME_GetSuperblockSerial( type, superblock );
555         goto fill_fs_info;
556     }
557     else TRACE( "cannot open device %s: err %ld\n", debugstr_w(device), GetLastError() );
558
559     /* we couldn't open the device, fallback to default strategy */
560
561     switch(GetDriveTypeW( root ))
562     {
563     case DRIVE_UNKNOWN:
564     case DRIVE_NO_ROOT_DIR:
565         SetLastError( ERROR_NOT_READY );
566         return FALSE;
567     case DRIVE_REMOVABLE:
568     case DRIVE_FIXED:
569     case DRIVE_REMOTE:
570     case DRIVE_RAMDISK:
571         type = FS_UNKNOWN;
572         break;
573     case DRIVE_CDROM:
574         type = FS_ISO9660;
575         break;
576     }
577
578     if (label && label_len)
579     {
580         WCHAR labelW[] = {'A',':','\\','.','w','i','n','d','o','w','s','-','l','a','b','e','l',0};
581
582         labelW[0] = device[4];
583         handle = CreateFileW( labelW, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
584                               OPEN_EXISTING, 0, 0 );
585         if (handle != INVALID_HANDLE_VALUE)
586         {
587             char buffer[256], *p;
588             DWORD size;
589
590             if (!ReadFile( handle, buffer, sizeof(buffer)-1, &size, NULL )) size = 0;
591             CloseHandle( handle );
592             p = buffer + size;
593             while (p > buffer && (p[-1] == ' ' || p[-1] == '\r' || p[-1] == '\n')) p--;
594             *p = 0;
595             if (!MultiByteToWideChar( CP_UNIXCP, 0, buffer, -1, label, label_len ))
596                 label[label_len-1] = 0;
597         }
598         else label[0] = 0;
599     }
600     if (serial)
601     {
602         WCHAR serialW[] = {'A',':','\\','.','w','i','n','d','o','w','s','-','s','e','r','i','a','l',0};
603
604         serialW[0] = device[4];
605         handle = CreateFileW( serialW, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
606                               OPEN_EXISTING, 0, 0 );
607         if (handle != INVALID_HANDLE_VALUE)
608         {
609             char buffer[32];
610             DWORD size;
611
612             if (!ReadFile( handle, buffer, sizeof(buffer)-1, &size, NULL )) size = 0;
613             CloseHandle( handle );
614             buffer[size] = 0;
615             *serial = strtoul( buffer, NULL, 16 );
616         }
617         else *serial = 0;
618     }
619
620 fill_fs_info:  /* now fill in the information that depends on the file system type */
621
622     switch(type)
623     {
624     case FS_ISO9660:
625         if (fsname) lstrcpynW( fsname, cdfsW, fsname_len );
626         if (filename_len) *filename_len = 221;
627         if (flags) *flags = FILE_READ_ONLY_VOLUME;
628         break;
629     case FS_FAT1216:
630     case FS_FAT32:
631         if (fsname) lstrcpynW( fsname, fatW, fsname_len );
632         if (filename_len) *filename_len = 255;
633         if (flags) *flags = FILE_CASE_PRESERVED_NAMES;  /* FIXME */
634         break;
635     default:
636         if (fsname) lstrcpynW( fsname, ntfsW, fsname_len );
637         if (filename_len) *filename_len = 255;
638         if (flags) *flags = FILE_CASE_PRESERVED_NAMES;
639         break;
640     }
641     return TRUE;
642 }
643
644
645 /***********************************************************************
646  *           GetVolumeInformationA   (KERNEL32.@)
647  */
648 BOOL WINAPI GetVolumeInformationA( LPCSTR root, LPSTR label,
649                                    DWORD label_len, DWORD *serial,
650                                    DWORD *filename_len, DWORD *flags,
651                                    LPSTR fsname, DWORD fsname_len )
652 {
653     WCHAR *rootW = NULL;
654     LPWSTR labelW, fsnameW;
655     BOOL ret;
656
657     if (root && !(rootW = FILE_name_AtoW( root, FALSE ))) return FALSE;
658
659     labelW = label ? HeapAlloc(GetProcessHeap(), 0, label_len * sizeof(WCHAR)) : NULL;
660     fsnameW = fsname ? HeapAlloc(GetProcessHeap(), 0, fsname_len * sizeof(WCHAR)) : NULL;
661
662     if ((ret = GetVolumeInformationW(rootW, labelW, label_len, serial,
663                                     filename_len, flags, fsnameW, fsname_len)))
664     {
665         if (label) FILE_name_WtoA( labelW, -1, label, label_len );
666         if (fsname) FILE_name_WtoA( fsnameW, -1, fsname, fsname_len );
667     }
668
669     HeapFree( GetProcessHeap(), 0, labelW );
670     HeapFree( GetProcessHeap(), 0, fsnameW );
671     return ret;
672 }
673
674
675
676 /***********************************************************************
677  *           SetVolumeLabelW   (KERNEL32.@)
678  */
679 BOOL WINAPI SetVolumeLabelW( LPCWSTR root, LPCWSTR label )
680 {
681     WCHAR device[] = {'\\','\\','.','\\','A',':',0};
682     HANDLE handle;
683     enum fs_type type = FS_UNKNOWN;
684
685     if (!root)
686     {
687         WCHAR path[MAX_PATH];
688         GetCurrentDirectoryW( MAX_PATH, path );
689         device[4] = path[0];
690     }
691     else
692     {
693         if (!root[0] || root[1] != ':')
694         {
695             SetLastError( ERROR_INVALID_NAME );
696             return FALSE;
697         }
698         device[4] = root[0];
699     }
700
701     /* try to open the device */
702
703     handle = CreateFileW( device, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE,
704                           NULL, OPEN_EXISTING, 0, 0 );
705     if (handle == INVALID_HANDLE_VALUE)
706     {
707         /* try read-only */
708         handle = CreateFileW( device, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE,
709                               NULL, OPEN_EXISTING, 0, 0 );
710         if (handle != INVALID_HANDLE_VALUE)
711         {
712             /* device can be read but not written, return error */
713             CloseHandle( handle );
714             SetLastError( ERROR_ACCESS_DENIED );
715             return FALSE;
716         }
717     }
718
719     if (handle != INVALID_HANDLE_VALUE)
720     {
721         BYTE superblock[SUPERBLOCK_SIZE];
722         BOOL ret;
723
724         type = VOLUME_ReadFATSuperblock( handle, superblock );
725         ret = VOLUME_SetSuperblockLabel( type, handle, label );
726         CloseHandle( handle );
727         return ret;
728     }
729     else
730     {
731         TRACE( "cannot open device %s: err %ld\n", debugstr_w(device), GetLastError() );
732         if (GetLastError() == ERROR_ACCESS_DENIED) return FALSE;
733     }
734
735     /* we couldn't open the device, fallback to default strategy */
736
737     switch(GetDriveTypeW( root ))
738     {
739     case DRIVE_UNKNOWN:
740     case DRIVE_NO_ROOT_DIR:
741         SetLastError( ERROR_NOT_READY );
742         break;
743     case DRIVE_REMOVABLE:
744     case DRIVE_FIXED:
745         {
746             WCHAR labelW[] = {'A',':','\\','.','w','i','n','d','o','w','s','-','l','a','b','e','l',0};
747
748             labelW[0] = device[4];
749             handle = CreateFileW( labelW, GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
750                                   CREATE_ALWAYS, 0, 0 );
751             if (handle != INVALID_HANDLE_VALUE)
752             {
753                 char buffer[64];
754                 DWORD size;
755
756                 if (!WideCharToMultiByte( CP_UNIXCP, 0, label, -1, buffer, sizeof(buffer), NULL, NULL ))
757                     buffer[sizeof(buffer)-1] = 0;
758                 WriteFile( handle, buffer, strlen(buffer), &size, NULL );
759                 CloseHandle( handle );
760                 return TRUE;
761             }
762             break;
763         }
764     case DRIVE_REMOTE:
765     case DRIVE_RAMDISK:
766     case DRIVE_CDROM:
767         SetLastError( ERROR_ACCESS_DENIED );
768         break;
769     }
770     return FALSE;
771 }
772
773 /***********************************************************************
774  *           SetVolumeLabelA   (KERNEL32.@)
775  */
776 BOOL WINAPI SetVolumeLabelA(LPCSTR root, LPCSTR volname)
777 {
778     WCHAR *rootW = NULL, *volnameW = NULL;
779     BOOL ret;
780
781     if (root && !(rootW = FILE_name_AtoW( root, FALSE ))) return FALSE;
782     if (volname && !(volnameW = FILE_name_AtoW( volname, TRUE ))) return FALSE;
783     ret = SetVolumeLabelW( rootW, volnameW );
784     HeapFree( GetProcessHeap(), 0, volnameW );
785     return ret;
786 }
787
788
789 /***********************************************************************
790  *           GetVolumeNameForVolumeMountPointW   (KERNEL32.@)
791  */
792 BOOL WINAPI GetVolumeNameForVolumeMountPointW(LPCWSTR str, LPWSTR dst, DWORD size)
793 {
794     FIXME("(%s, %p, %lx): stub\n", debugstr_w(str), dst, size);
795     return 0;
796 }
797
798
799 /***********************************************************************
800  *           DefineDosDeviceW       (KERNEL32.@)
801  */
802 BOOL WINAPI DefineDosDeviceW( DWORD flags, LPCWSTR devname, LPCWSTR targetpath )
803 {
804     DWORD len, dosdev;
805     BOOL ret = FALSE;
806     char *path = NULL, *target, *p;
807
808     if (!(flags & DDD_REMOVE_DEFINITION))
809     {
810         if (!(flags & DDD_RAW_TARGET_PATH))
811         {
812             FIXME( "(0x%08lx,%s,%s) DDD_RAW_TARGET_PATH flag not set, not supported yet\n",
813                    flags, debugstr_w(devname), debugstr_w(targetpath) );
814             SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
815             return FALSE;
816         }
817
818         len = WideCharToMultiByte( CP_UNIXCP, 0, targetpath, -1, NULL, 0, NULL, NULL );
819         if ((target = HeapAlloc( GetProcessHeap(), 0, len )))
820         {
821             WideCharToMultiByte( CP_UNIXCP, 0, targetpath, -1, target, len, NULL, NULL );
822             for (p = target; *p; p++) if (*p == '\\') *p = '/';
823         }
824         else
825         {
826             SetLastError( ERROR_NOT_ENOUGH_MEMORY );
827             return FALSE;
828         }
829     }
830     else target = NULL;
831
832     /* first check for a DOS device */
833
834     if ((dosdev = RtlIsDosDeviceName_U( devname )))
835     {
836         WCHAR name[5];
837
838         memcpy( name, devname + HIWORD(dosdev)/sizeof(WCHAR), LOWORD(dosdev) );
839         name[LOWORD(dosdev)/sizeof(WCHAR)] = 0;
840         path = get_dos_device_path( name );
841     }
842     else if (isalphaW(devname[0]) && devname[1] == ':' && !devname[2])  /* drive mapping */
843     {
844         path = get_dos_device_path( devname );
845     }
846     else SetLastError( ERROR_FILE_NOT_FOUND );
847
848     if (path)
849     {
850         if (target)
851         {
852             TRACE( "creating symlink %s -> %s\n", path, target );
853             unlink( path );
854             if (!symlink( target, path )) ret = TRUE;
855             else FILE_SetDosError();
856         }
857         else
858         {
859             TRACE( "removing symlink %s\n", path );
860             if (!unlink( path )) ret = TRUE;
861             else FILE_SetDosError();
862         }
863         HeapFree( GetProcessHeap(), 0, path );
864     }
865     HeapFree( GetProcessHeap(), 0, target );
866     return ret;
867 }
868
869
870 /***********************************************************************
871  *           DefineDosDeviceA       (KERNEL32.@)
872  */
873 BOOL WINAPI DefineDosDeviceA(DWORD flags, LPCSTR devname, LPCSTR targetpath)
874 {
875     WCHAR *devW, *targetW = NULL;
876     BOOL ret;
877
878     if (!(devW = FILE_name_AtoW( devname, FALSE ))) return FALSE;
879     if (targetpath && !(targetW = FILE_name_AtoW( targetpath, TRUE ))) return FALSE;
880     ret = DefineDosDeviceW(flags, devW, targetW);
881     HeapFree( GetProcessHeap(), 0, targetW );
882     return ret;
883 }
884
885
886 /***********************************************************************
887  *           QueryDosDeviceW   (KERNEL32.@)
888  *
889  * returns array of strings terminated by \0, terminated by \0
890  */
891 DWORD WINAPI QueryDosDeviceW( LPCWSTR devname, LPWSTR target, DWORD bufsize )
892 {
893     static const WCHAR auxW[] = {'A','U','X',0};
894     static const WCHAR nulW[] = {'N','U','L',0};
895     static const WCHAR prnW[] = {'P','R','N',0};
896     static const WCHAR comW[] = {'C','O','M',0};
897     static const WCHAR lptW[] = {'L','P','T',0};
898     static const WCHAR rootW[] = {'A',':','\\',0};
899     static const WCHAR com0W[] = {'\\','?','?','\\','C','O','M','0',0};
900     static const WCHAR com1W[] = {'\\','D','o','s','D','e','v','i','c','e','s','\\','C','O','M','1',0,0};
901     static const WCHAR lpt1W[] = {'\\','D','o','s','D','e','v','i','c','e','s','\\','L','P','T','1',0,0};
902
903     UNICODE_STRING nt_name;
904     ANSI_STRING unix_name;
905     WCHAR nt_buffer[10];
906     NTSTATUS status;
907
908     if (!bufsize)
909     {
910         SetLastError( ERROR_INSUFFICIENT_BUFFER );
911         return 0;
912     }
913
914     if (devname)
915     {
916         WCHAR *p, name[5];
917         char *path, *link;
918         DWORD dosdev, ret = 0;
919
920         if ((dosdev = RtlIsDosDeviceName_U( devname )))
921         {
922             memcpy( name, devname + HIWORD(dosdev)/sizeof(WCHAR), LOWORD(dosdev) );
923             name[LOWORD(dosdev)/sizeof(WCHAR)] = 0;
924         }
925         else if (devname[0] && devname[1] == ':' && !devname[2])
926         {
927             memcpy( name, devname, 3 * sizeof(WCHAR) );
928         }
929         else
930         {
931             SetLastError( ERROR_BAD_PATHNAME );
932             return 0;
933         }
934
935         if (!(path = get_dos_device_path( name ))) return 0;
936         link = read_symlink( path );
937         HeapFree( GetProcessHeap(), 0, path );
938
939         if (link)
940         {
941             ret = MultiByteToWideChar( CP_UNIXCP, 0, link, -1, target, bufsize );
942             HeapFree( GetProcessHeap(), 0, link );
943         }
944         else if (dosdev)  /* look for device defaults */
945         {
946             if (!strcmpiW( name, auxW ))
947             {
948                 if (bufsize >= sizeof(com1W)/sizeof(WCHAR))
949                 {
950                     memcpy( target, com1W, sizeof(com1W) );
951                     ret = sizeof(com1W)/sizeof(WCHAR);
952                 }
953                 else SetLastError( ERROR_INSUFFICIENT_BUFFER );
954                 return ret;
955             }
956             if (!strcmpiW( name, prnW ))
957             {
958                 if (bufsize >= sizeof(lpt1W)/sizeof(WCHAR))
959                 {
960                     memcpy( target, lpt1W, sizeof(lpt1W) );
961                     ret = sizeof(lpt1W)/sizeof(WCHAR);
962                 }
963                 else SetLastError( ERROR_INSUFFICIENT_BUFFER );
964                 return ret;
965             }
966
967             nt_buffer[0] = '\\';
968             nt_buffer[1] = '?';
969             nt_buffer[2] = '?';
970             nt_buffer[3] = '\\';
971             strcpyW( nt_buffer + 4, name );
972             RtlInitUnicodeString( &nt_name, nt_buffer );
973             status = wine_nt_to_unix_file_name( &nt_name, &unix_name, FILE_OPEN, TRUE );
974             if (status) SetLastError( RtlNtStatusToDosError(status) );
975             else
976             {
977                 ret = MultiByteToWideChar( CP_UNIXCP, 0, unix_name.Buffer, -1, target, bufsize );
978                 RtlFreeAnsiString( &unix_name );
979             }
980         }
981
982         if (ret)
983         {
984             if (ret < bufsize) target[ret++] = 0;  /* add an extra null */
985             for (p = target; *p; p++) if (*p == '/') *p = '\\';
986         }
987
988         return ret;
989     }
990     else  /* return a list of all devices */
991     {
992         WCHAR *p = target;
993         int i;
994
995         if (bufsize <= (sizeof(auxW)+sizeof(nulW)+sizeof(prnW))/sizeof(WCHAR))
996         {
997             SetLastError( ERROR_INSUFFICIENT_BUFFER );
998             return 0;
999         }
1000
1001         memcpy( p, auxW, sizeof(auxW) );
1002         p += sizeof(auxW) / sizeof(WCHAR);
1003         memcpy( p, nulW, sizeof(nulW) );
1004         p += sizeof(nulW) / sizeof(WCHAR);
1005         memcpy( p, prnW, sizeof(prnW) );
1006         p += sizeof(prnW) / sizeof(WCHAR);
1007
1008         strcpyW( nt_buffer, com0W );
1009         RtlInitUnicodeString( &nt_name, nt_buffer );
1010
1011         for (i = 1; i <= 9; i++)
1012         {
1013             nt_buffer[7] = '0' + i;
1014             if (!wine_nt_to_unix_file_name( &nt_name, &unix_name, FILE_OPEN, TRUE ))
1015             {
1016                 RtlFreeAnsiString( &unix_name );
1017                 if (p + 5 >= target + bufsize)
1018                 {
1019                     SetLastError( ERROR_INSUFFICIENT_BUFFER );
1020                     return 0;
1021                 }
1022                 strcpyW( p, comW );
1023                 p[3] = '0' + i;
1024                 p[4] = 0;
1025                 p += 5;
1026             }
1027         }
1028         strcpyW( nt_buffer + 4, lptW );
1029         for (i = 1; i <= 9; i++)
1030         {
1031             nt_buffer[7] = '0' + i;
1032             if (!wine_nt_to_unix_file_name( &nt_name, &unix_name, FILE_OPEN, TRUE ))
1033             {
1034                 RtlFreeAnsiString( &unix_name );
1035                 if (p + 5 >= target + bufsize)
1036                 {
1037                     SetLastError( ERROR_INSUFFICIENT_BUFFER );
1038                     return 0;
1039                 }
1040                 strcpyW( p, lptW );
1041                 p[3] = '0' + i;
1042                 p[4] = 0;
1043                 p += 5;
1044             }
1045         }
1046
1047         strcpyW( nt_buffer + 4, rootW );
1048         RtlInitUnicodeString( &nt_name, nt_buffer );
1049
1050         for (i = 0; i < 26; i++)
1051         {
1052             nt_buffer[4] = 'a' + i;
1053             if (!wine_nt_to_unix_file_name( &nt_name, &unix_name, FILE_OPEN, TRUE ))
1054             {
1055                 RtlFreeAnsiString( &unix_name );
1056                 if (p + 3 >= target + bufsize)
1057                 {
1058                     SetLastError( ERROR_INSUFFICIENT_BUFFER );
1059                     return 0;
1060                 }
1061                 *p++ = 'A' + i;
1062                 *p++ = ':';
1063                 *p++ = 0;
1064             }
1065         }
1066         *p++ = 0;  /* terminating null */
1067         return p - target;
1068     }
1069 }
1070
1071
1072 /***********************************************************************
1073  *           QueryDosDeviceA   (KERNEL32.@)
1074  *
1075  * returns array of strings terminated by \0, terminated by \0
1076  */
1077 DWORD WINAPI QueryDosDeviceA( LPCSTR devname, LPSTR target, DWORD bufsize )
1078 {
1079     DWORD ret = 0, retW;
1080     WCHAR *devnameW = NULL;
1081     LPWSTR targetW;
1082
1083     if (devname && !(devnameW = FILE_name_AtoW( devname, FALSE ))) return 0;
1084
1085     targetW = HeapAlloc( GetProcessHeap(),0, bufsize * sizeof(WCHAR) );
1086     if (!targetW)
1087     {
1088         SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1089         return 0;
1090     }
1091
1092     retW = QueryDosDeviceW(devnameW, targetW, bufsize);
1093
1094     ret = FILE_name_WtoA( targetW, retW, target, bufsize );
1095
1096     HeapFree(GetProcessHeap(), 0, targetW);
1097     return ret;
1098 }
1099
1100
1101 /***********************************************************************
1102  *           GetLogicalDrives   (KERNEL32.@)
1103  */
1104 DWORD WINAPI GetLogicalDrives(void)
1105 {
1106     const char *config_dir = wine_get_config_dir();
1107     struct stat st;
1108     char *buffer, *dev;
1109     DWORD ret = 0;
1110     int i;
1111
1112     if (!(buffer = HeapAlloc( GetProcessHeap(), 0, strlen(config_dir) + sizeof("/dosdevices/a:") )))
1113     {
1114         SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1115         return 0;
1116     }
1117     strcpy( buffer, config_dir );
1118     strcat( buffer, "/dosdevices/a:" );
1119     dev = buffer + strlen(buffer) - 2;
1120
1121     for (i = 0; i < 26; i++)
1122     {
1123         *dev = 'a' + i;
1124         if (!stat( buffer, &st )) ret |= (1 << i);
1125     }
1126     HeapFree( GetProcessHeap(), 0, buffer );
1127     return ret;
1128 }
1129
1130
1131 /***********************************************************************
1132  *           GetLogicalDriveStringsA   (KERNEL32.@)
1133  */
1134 UINT WINAPI GetLogicalDriveStringsA( UINT len, LPSTR buffer )
1135 {
1136     DWORD drives = GetLogicalDrives();
1137     UINT drive, count;
1138
1139     for (drive = count = 0; drive < 26; drive++) if (drives & (1 << drive)) count++;
1140     if ((count * 4) + 1 > len) return count * 4 + 1;
1141
1142     for (drive = 0; drive < 26; drive++)
1143     {
1144         if (drives & (1 << drive))
1145         {
1146             *buffer++ = 'a' + drive;
1147             *buffer++ = ':';
1148             *buffer++ = '\\';
1149             *buffer++ = 0;
1150         }
1151     }
1152     *buffer = 0;
1153     return count * 4;
1154 }
1155
1156
1157 /***********************************************************************
1158  *           GetLogicalDriveStringsW   (KERNEL32.@)
1159  */
1160 UINT WINAPI GetLogicalDriveStringsW( UINT len, LPWSTR buffer )
1161 {
1162     DWORD drives = GetLogicalDrives();
1163     UINT drive, count;
1164
1165     for (drive = count = 0; drive < 26; drive++) if (drives & (1 << drive)) count++;
1166     if ((count * 4) + 1 > len) return count * 4 + 1;
1167
1168     for (drive = 0; drive < 26; drive++)
1169     {
1170         if (drives & (1 << drive))
1171         {
1172             *buffer++ = 'a' + drive;
1173             *buffer++ = ':';
1174             *buffer++ = '\\';
1175             *buffer++ = 0;
1176         }
1177     }
1178     *buffer = 0;
1179     return count * 4;
1180 }
1181
1182
1183 /***********************************************************************
1184  *           GetDriveTypeW   (KERNEL32.@)
1185  *
1186  * Returns the type of the disk drive specified. If root is NULL the
1187  * root of the current directory is used.
1188  *
1189  * RETURNS
1190  *
1191  *  Type of drive (from Win32 SDK):
1192  *
1193  *   DRIVE_UNKNOWN     unable to find out anything about the drive
1194  *   DRIVE_NO_ROOT_DIR nonexistent root dir
1195  *   DRIVE_REMOVABLE   the disk can be removed from the machine
1196  *   DRIVE_FIXED       the disk cannot be removed from the machine
1197  *   DRIVE_REMOTE      network disk
1198  *   DRIVE_CDROM       CDROM drive
1199  *   DRIVE_RAMDISK     virtual disk in RAM
1200  */
1201 UINT WINAPI GetDriveTypeW(LPCWSTR root) /* [in] String describing drive */
1202 {
1203     FILE_FS_DEVICE_INFORMATION info;
1204     IO_STATUS_BLOCK io;
1205     NTSTATUS status;
1206     HANDLE handle;
1207     UINT ret;
1208
1209     if (!open_device_root( root, &handle )) return DRIVE_NO_ROOT_DIR;
1210
1211     status = NtQueryVolumeInformationFile( handle, &io, &info, sizeof(info), FileFsDeviceInformation );
1212     NtClose( handle );
1213     if (status != STATUS_SUCCESS)
1214     {
1215         SetLastError( RtlNtStatusToDosError(status) );
1216         ret = DRIVE_UNKNOWN;
1217     }
1218     else if ((ret = get_registry_drive_type( root )) == DRIVE_UNKNOWN)
1219     {
1220         switch (info.DeviceType)
1221         {
1222         case FILE_DEVICE_CD_ROM_FILE_SYSTEM:  ret = DRIVE_CDROM; break;
1223         case FILE_DEVICE_VIRTUAL_DISK:        ret = DRIVE_RAMDISK; break;
1224         case FILE_DEVICE_NETWORK_FILE_SYSTEM: ret = DRIVE_REMOTE; break;
1225         case FILE_DEVICE_DISK_FILE_SYSTEM:
1226             if (info.Characteristics & FILE_REMOTE_DEVICE) ret = DRIVE_REMOTE;
1227             else if (info.Characteristics & FILE_REMOVABLE_MEDIA) ret = DRIVE_REMOVABLE;
1228             else ret = DRIVE_FIXED;
1229             break;
1230         default:
1231             ret = DRIVE_UNKNOWN;
1232             break;
1233         }
1234     }
1235     TRACE( "%s -> %d\n", debugstr_w(root), ret );
1236     return ret;
1237 }
1238
1239
1240 /***********************************************************************
1241  *           GetDriveTypeA   (KERNEL32.@)
1242  */
1243 UINT WINAPI GetDriveTypeA( LPCSTR root )
1244 {
1245     WCHAR *rootW = NULL;
1246
1247     if (root && !(rootW = FILE_name_AtoW( root, FALSE ))) return DRIVE_NO_ROOT_DIR;
1248     return GetDriveTypeW( rootW );
1249 }
1250
1251
1252 /***********************************************************************
1253  *           GetDiskFreeSpaceExW   (KERNEL32.@)
1254  *
1255  *  This function is used to acquire the size of the available and
1256  *  total space on a logical volume.
1257  *
1258  * RETURNS
1259  *
1260  *  Zero on failure, nonzero upon success. Use GetLastError to obtain
1261  *  detailed error information.
1262  *
1263  */
1264 BOOL WINAPI GetDiskFreeSpaceExW( LPCWSTR root, PULARGE_INTEGER avail,
1265                                  PULARGE_INTEGER total, PULARGE_INTEGER totalfree )
1266 {
1267     FILE_FS_SIZE_INFORMATION info;
1268     IO_STATUS_BLOCK io;
1269     NTSTATUS status;
1270     HANDLE handle;
1271     UINT units;
1272
1273     TRACE( "%s,%p,%p,%p\n", debugstr_w(root), avail, total, totalfree );
1274
1275     if (!open_device_root( root, &handle )) return FALSE;
1276
1277     status = NtQueryVolumeInformationFile( handle, &io, &info, sizeof(info), FileFsSizeInformation );
1278     NtClose( handle );
1279     if (status != STATUS_SUCCESS)
1280     {
1281         SetLastError( RtlNtStatusToDosError(status) );
1282         return FALSE;
1283     }
1284
1285     units = info.SectorsPerAllocationUnit * info.BytesPerSector;
1286     if (total) total->QuadPart = info.TotalAllocationUnits.QuadPart * units;
1287     if (totalfree) totalfree->QuadPart = info.AvailableAllocationUnits.QuadPart * units;
1288     /* FIXME: this one should take quotas into account */
1289     if (avail) avail->QuadPart = info.AvailableAllocationUnits.QuadPart * units;
1290     return TRUE;
1291 }
1292
1293
1294 /***********************************************************************
1295  *           GetDiskFreeSpaceExA   (KERNEL32.@)
1296  */
1297 BOOL WINAPI GetDiskFreeSpaceExA( LPCSTR root, PULARGE_INTEGER avail,
1298                                  PULARGE_INTEGER total, PULARGE_INTEGER totalfree )
1299 {
1300     WCHAR *rootW = NULL;
1301
1302     if (root && !(rootW = FILE_name_AtoW( root, FALSE ))) return FALSE;
1303     return GetDiskFreeSpaceExW( rootW, avail, total, totalfree );
1304 }
1305
1306
1307 /***********************************************************************
1308  *           GetDiskFreeSpaceW   (KERNEL32.@)
1309  */
1310 BOOL WINAPI GetDiskFreeSpaceW( LPCWSTR root, LPDWORD cluster_sectors,
1311                                LPDWORD sector_bytes, LPDWORD free_clusters,
1312                                LPDWORD total_clusters )
1313 {
1314     FILE_FS_SIZE_INFORMATION info;
1315     IO_STATUS_BLOCK io;
1316     NTSTATUS status;
1317     HANDLE handle;
1318     UINT units;
1319
1320     TRACE( "%s,%p,%p,%p,%p\n", debugstr_w(root),
1321            cluster_sectors, sector_bytes, free_clusters, total_clusters );
1322
1323     if (!open_device_root( root, &handle )) return FALSE;
1324
1325     status = NtQueryVolumeInformationFile( handle, &io, &info, sizeof(info), FileFsSizeInformation );
1326     NtClose( handle );
1327     if (status != STATUS_SUCCESS)
1328     {
1329         SetLastError( RtlNtStatusToDosError(status) );
1330         return FALSE;
1331     }
1332
1333     units = info.SectorsPerAllocationUnit * info.BytesPerSector;
1334
1335     /* cap the size and available at 2GB as per specs */
1336     if (info.AvailableAllocationUnits.QuadPart * units > 0x7fffffff)
1337         info.AvailableAllocationUnits.QuadPart = 0x7fffffff / units;
1338     if (info.TotalAllocationUnits.QuadPart * units > 0x7fffffff)
1339         info.TotalAllocationUnits.QuadPart = 0x7fffffff / units;
1340
1341     if (cluster_sectors) *cluster_sectors = info.SectorsPerAllocationUnit;
1342     if (sector_bytes) *sector_bytes = info.BytesPerSector;
1343     if (free_clusters) *free_clusters = info.AvailableAllocationUnits.u.LowPart;
1344     if (total_clusters) *total_clusters = info.TotalAllocationUnits.u.LowPart;
1345     return TRUE;
1346 }
1347
1348
1349 /***********************************************************************
1350  *           GetDiskFreeSpaceA   (KERNEL32.@)
1351  */
1352 BOOL WINAPI GetDiskFreeSpaceA( LPCSTR root, LPDWORD cluster_sectors,
1353                                LPDWORD sector_bytes, LPDWORD free_clusters,
1354                                LPDWORD total_clusters )
1355 {
1356     WCHAR *rootW = NULL;
1357
1358     if (root && !(rootW = FILE_name_AtoW( root, FALSE ))) return FALSE;
1359     return GetDiskFreeSpaceW( rootW, cluster_sectors, sector_bytes, free_clusters, total_clusters );
1360 }
1361
1362 /***********************************************************************
1363  *           GetVolumePathNameA   (KERNEL32.@)
1364  */
1365 BOOL WINAPI GetVolumePathNameA(LPCSTR filename, LPSTR volumepathname, DWORD buflen)
1366 {
1367     FIXME("(%s, %p, %ld), stub!\n", debugstr_a(filename), volumepathname, buflen);
1368     SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1369     return FALSE;
1370 }
1371
1372 /***********************************************************************
1373  *           GetVolumePathNameW   (KERNEL32.@)
1374  */
1375 BOOL WINAPI GetVolumePathNameW(LPCWSTR filename, LPWSTR volumepathname, DWORD buflen)
1376 {
1377     FIXME("(%s, %p, %ld), stub!\n", debugstr_w(filename), volumepathname, buflen);
1378     SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1379     return FALSE;
1380 }