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