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