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