msi: Add more tests for MsiSourceListGetInfo.
[wine.git] / dlls / kernel32 / volume.c
blobdf9180f6a49609521c6f0b01177bb732d9abeeed
1 /*
2 * Volume management functions
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
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.
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.
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
25 #include "config.h"
26 #include "wine/port.h"
28 #include <stdarg.h>
29 #include <stdlib.h>
30 #include <stdio.h>
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"
46 WINE_DEFAULT_DEBUG_CHANNEL(volume);
48 #define SUPERBLOCK_SIZE 2048
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)
55 #define GETWORD(buf,off) MAKEWORD(buf[(off)],buf[(off+1)])
56 #define GETLONG(buf,off) MAKELONG(GETWORD(buf,off),GETWORD(buf,off+2))
58 enum fs_type
60 FS_ERROR, /* error accessing the device */
61 FS_UNKNOWN, /* unknown file system */
62 FS_FAT1216,
63 FS_FAT32,
64 FS_ISO9660
67 static const WCHAR drive_types[][8] =
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 */
78 /* read a Unix symlink; returned buffer must be freed by caller */
79 static char *read_symlink( const char *path )
81 char *buffer;
82 int ret, size = 128;
84 for (;;)
86 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, size )))
88 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
89 return 0;
91 ret = readlink( path, buffer, size );
92 if (ret == -1)
94 FILE_SetDosError();
95 HeapFree( GetProcessHeap(), 0, buffer );
96 return 0;
98 if (ret != size)
100 buffer[ret] = 0;
101 return buffer;
103 HeapFree( GetProcessHeap(), 0, buffer );
104 size *= 2;
108 /* get the path of a dos device symlink in the $WINEPREFIX/dosdevices directory */
109 static char *get_dos_device_path( LPCWSTR name )
111 const char *config_dir = wine_get_config_dir();
112 char *buffer, *dev;
113 int i;
115 if (!(buffer = HeapAlloc( GetProcessHeap(), 0,
116 strlen(config_dir) + sizeof("/dosdevices/") + 5 )))
118 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
119 return NULL;
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;
132 /* open a handle to a device root */
133 static BOOL open_device_root( LPCWSTR root, HANDLE *handle )
135 static const WCHAR default_rootW[] = {'\\',0};
136 UNICODE_STRING nt_name;
137 OBJECT_ATTRIBUTES attr;
138 IO_STATUS_BLOCK io;
139 NTSTATUS status;
141 if (!root) root = default_rootW;
142 if (!RtlDosPathNameToNtPathName_U( root, &nt_name, NULL, NULL ))
144 SetLastError( ERROR_PATH_NOT_FOUND );
145 return FALSE;
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;
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)
159 SetLastError( RtlNtStatusToDosError(status) );
160 return FALSE;
162 return TRUE;
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 )
168 HANDLE handle;
169 WCHAR labelW[] = {'A',':','\\','.','w','i','n','d','o','w','s','-','l','a','b','e','l',0};
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)
176 char buffer[256], *p;
177 DWORD size;
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;
187 else label[0] = 0;
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 )
193 HANDLE handle;
194 WCHAR serialW[] = {'A',':','\\','.','w','i','n','d','o','w','s','-','s','e','r','i','a','l',0};
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)
201 char buffer[32];
202 DWORD size;
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 );
209 else return 0;
212 /* fetch the type of a drive from the registry */
213 static UINT get_registry_drive_type( const WCHAR *root )
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};
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;
237 if (root) driveW[0] = root[0];
238 else
240 WCHAR path[MAX_PATH];
241 GetCurrentDirectoryW( MAX_PATH, path );
242 driveW[0] = path[0];
245 RtlInitUnicodeString( &nameW, driveW );
246 if (!NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation, tmp, sizeof(tmp), &dummy ))
248 unsigned int i;
249 WCHAR *data = (WCHAR *)((KEY_VALUE_PARTIAL_INFORMATION *)tmp)->Data;
251 for (i = 0; i < sizeof(drive_types)/sizeof(drive_types[0]); i++)
253 if (!strcmpiW( data, drive_types[i] ))
255 ret = i;
256 break;
260 NtClose( hkey );
261 return ret;
265 /******************************************************************
266 * VOLUME_FindCdRomDataBestVoldesc
268 static DWORD VOLUME_FindCdRomDataBestVoldesc( HANDLE handle )
270 BYTE cur_vd_type, max_vd_type = 0;
271 BYTE buffer[0x800];
272 DWORD size, offs, best_offs = 0, extra_offs = 0;
274 for (offs = 0x8000; offs <= 0x9800; offs += 0x800)
276 /* if 'CDROM' occurs at position 8, this is a pre-iso9660 cd, and
277 * the volume label is displaced forward by 8
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)
289 max_vd_type = cur_vd_type;
290 best_offs = offs + extra_offs;
293 return best_offs;
297 /***********************************************************************
298 * VOLUME_ReadFATSuperblock
300 static enum fs_type VOLUME_ReadFATSuperblock( HANDLE handle, BYTE *buff )
302 DWORD size;
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 ))
308 if (GetLastError() == ERROR_BAD_DEV_TYPE) return FS_UNKNOWN; /* not a real device */
309 return FS_ERROR;
312 if (size < SUPERBLOCK_SIZE) return FS_UNKNOWN;
314 /* FIXME: do really all FAT have their name beginning with
315 * "FAT" ? (At least FAT12, FAT16 and FAT32 have :)
317 if (!memcmp(buff+0x36, "FAT", 3) || !memcmp(buff+0x52, "FAT", 3))
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 < 16 &&
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)
354 if ((buff[0x26] == 0x28 || buff[0x26] == 0x29) &&
355 !memcmp(buff+0x36, "FAT", 3))
356 return FS_FAT1216;
359 return FS_UNKNOWN;
363 /***********************************************************************
364 * VOLUME_ReadCDSuperblock
366 static enum fs_type VOLUME_ReadCDSuperblock( HANDLE handle, BYTE *buff )
368 DWORD size, offs = VOLUME_FindCdRomDataBestVoldesc( handle );
370 if (!offs) return FS_UNKNOWN;
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;
377 /* check for iso9660 present */
378 if (!memcmp(&buff[1], "CD001", 5)) return FS_ISO9660;
379 return FS_UNKNOWN;
383 /**************************************************************************
384 * VOLUME_GetSuperblockLabel
386 static void VOLUME_GetSuperblockLabel( const WCHAR *device, enum fs_type type, const BYTE *superblock,
387 WCHAR *label, DWORD len )
389 const BYTE *label_ptr = NULL;
390 DWORD label_len;
392 switch(type)
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:
410 BYTE ver = superblock[0x5a];
412 if (superblock[0x58] == 0x25 && superblock[0x59] == 0x2f && /* Unicode ID */
413 ((ver == 0x40) || (ver == 0x43) || (ver == 0x45)))
414 { /* yippee, unicode */
415 unsigned int i;
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;
424 label_ptr = superblock + 40;
425 label_len = 32;
426 break;
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;
437 /**************************************************************************
438 * VOLUME_GetSuperblockSerial
440 static DWORD VOLUME_GetSuperblockSerial( const WCHAR *device, enum fs_type type, const BYTE *superblock )
442 switch(type)
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:
454 BYTE sum[4];
455 int i;
457 sum[0] = sum[1] = sum[2] = sum[3] = 0;
458 for (i = 0; i < 2048; i += 4)
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];
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.
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];
477 return 0;
481 /**************************************************************************
482 * VOLUME_GetAudioCDSerial
484 static DWORD VOLUME_GetAudioCDSerial( const CDROM_TOC *toc )
486 DWORD serial = 0;
487 int i;
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]);
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.
500 if (toc->LastTrack - toc->FirstTrack + 1 < 3)
502 DWORD dwStart = FRAME_OF_TOC(toc, toc->FirstTrack);
503 DWORD dwEnd = FRAME_OF_TOC(toc, toc->LastTrack + 1);
504 serial += dwEnd - dwStart;
506 return serial;
510 /***********************************************************************
511 * GetVolumeInformationW (KERNEL32.@)
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 )
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 ntfsW[] = {'N','T','F','S',0};
520 static const WCHAR cdfsW[] = {'C','D','F','S',0};
522 WCHAR device[] = {'\\','\\','.','\\','A',':',0};
523 HANDLE handle;
524 enum fs_type type = FS_UNKNOWN;
526 if (!root)
528 WCHAR path[MAX_PATH];
529 GetCurrentDirectoryW( MAX_PATH, path );
530 device[4] = path[0];
532 else
534 if (!root[0] || root[1] != ':')
536 SetLastError( ERROR_INVALID_NAME );
537 return FALSE;
539 device[4] = root[0];
542 /* try to open the device */
544 handle = CreateFileW( device, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE,
545 NULL, OPEN_EXISTING, 0, 0 );
546 if (handle != INVALID_HANDLE_VALUE)
548 BYTE superblock[SUPERBLOCK_SIZE];
549 CDROM_TOC toc;
550 DWORD br;
552 /* check for audio CD */
553 /* FIXME: we only check the first track for now */
554 if (DeviceIoControl( handle, IOCTL_CDROM_READ_TOC, NULL, 0, &toc, sizeof(toc), &br, 0 ))
556 if (!(toc.TrackData[0].Control & 0x04)) /* audio track */
558 TRACE( "%s: found audio CD\n", debugstr_w(device) );
559 if (label) lstrcpynW( label, audiocdW, label_len );
560 if (serial) *serial = VOLUME_GetAudioCDSerial( &toc );
561 CloseHandle( handle );
562 type = FS_ISO9660;
563 goto fill_fs_info;
565 type = VOLUME_ReadCDSuperblock( handle, superblock );
567 else
569 type = VOLUME_ReadFATSuperblock( handle, superblock );
570 if (type == FS_UNKNOWN) type = VOLUME_ReadCDSuperblock( handle, superblock );
572 CloseHandle( handle );
573 TRACE( "%s: found fs type %d\n", debugstr_w(device), type );
574 if (type == FS_ERROR) return FALSE;
576 if (label && label_len) VOLUME_GetSuperblockLabel( device, type, superblock, label, label_len );
577 if (serial) *serial = VOLUME_GetSuperblockSerial( device, type, superblock );
578 goto fill_fs_info;
580 else TRACE( "cannot open device %s: err %d\n", debugstr_w(device), GetLastError() );
582 /* we couldn't open the device, fallback to default strategy */
584 switch(GetDriveTypeW( root ))
586 case DRIVE_UNKNOWN:
587 case DRIVE_NO_ROOT_DIR:
588 SetLastError( ERROR_NOT_READY );
589 return FALSE;
590 case DRIVE_REMOVABLE:
591 case DRIVE_FIXED:
592 case DRIVE_REMOTE:
593 case DRIVE_RAMDISK:
594 type = FS_UNKNOWN;
595 break;
596 case DRIVE_CDROM:
597 type = FS_ISO9660;
598 break;
601 if (label && label_len) get_filesystem_label( device, label, label_len );
602 if (serial) *serial = get_filesystem_serial( device );
604 fill_fs_info: /* now fill in the information that depends on the file system type */
606 switch(type)
608 case FS_ISO9660:
609 if (fsname) lstrcpynW( fsname, cdfsW, fsname_len );
610 if (filename_len) *filename_len = 221;
611 if (flags) *flags = FILE_READ_ONLY_VOLUME;
612 break;
613 case FS_FAT1216:
614 case FS_FAT32:
615 if (fsname) lstrcpynW( fsname, fatW, fsname_len );
616 if (filename_len) *filename_len = 255;
617 if (flags) *flags = FILE_CASE_PRESERVED_NAMES; /* FIXME */
618 break;
619 default:
620 if (fsname) lstrcpynW( fsname, ntfsW, fsname_len );
621 if (filename_len) *filename_len = 255;
622 if (flags) *flags = FILE_CASE_PRESERVED_NAMES;
623 break;
625 return TRUE;
629 /***********************************************************************
630 * GetVolumeInformationA (KERNEL32.@)
632 BOOL WINAPI GetVolumeInformationA( LPCSTR root, LPSTR label,
633 DWORD label_len, DWORD *serial,
634 DWORD *filename_len, DWORD *flags,
635 LPSTR fsname, DWORD fsname_len )
637 WCHAR *rootW = NULL;
638 LPWSTR labelW, fsnameW;
639 BOOL ret;
641 if (root && !(rootW = FILE_name_AtoW( root, FALSE ))) return FALSE;
643 labelW = label ? HeapAlloc(GetProcessHeap(), 0, label_len * sizeof(WCHAR)) : NULL;
644 fsnameW = fsname ? HeapAlloc(GetProcessHeap(), 0, fsname_len * sizeof(WCHAR)) : NULL;
646 if ((ret = GetVolumeInformationW(rootW, labelW, label_len, serial,
647 filename_len, flags, fsnameW, fsname_len)))
649 if (label) FILE_name_WtoA( labelW, -1, label, label_len );
650 if (fsname) FILE_name_WtoA( fsnameW, -1, fsname, fsname_len );
653 HeapFree( GetProcessHeap(), 0, labelW );
654 HeapFree( GetProcessHeap(), 0, fsnameW );
655 return ret;
660 /***********************************************************************
661 * SetVolumeLabelW (KERNEL32.@)
663 BOOL WINAPI SetVolumeLabelW( LPCWSTR root, LPCWSTR label )
665 WCHAR device[] = {'\\','\\','.','\\','A',':',0};
666 HANDLE handle;
667 enum fs_type type = FS_UNKNOWN;
669 if (!root)
671 WCHAR path[MAX_PATH];
672 GetCurrentDirectoryW( MAX_PATH, path );
673 device[4] = path[0];
675 else
677 if (!root[0] || root[1] != ':')
679 SetLastError( ERROR_INVALID_NAME );
680 return FALSE;
682 device[4] = root[0];
685 /* try to open the device */
687 handle = CreateFileW( device, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE,
688 NULL, OPEN_EXISTING, 0, 0 );
689 if (handle != INVALID_HANDLE_VALUE)
691 BYTE superblock[SUPERBLOCK_SIZE];
693 type = VOLUME_ReadFATSuperblock( handle, superblock );
694 if (type == FS_UNKNOWN) type = VOLUME_ReadCDSuperblock( handle, superblock );
695 CloseHandle( handle );
696 if (type != FS_UNKNOWN)
698 /* we can't set the label on FAT or CDROM file systems */
699 TRACE( "cannot set label on device %s type %d\n", debugstr_w(device), type );
700 SetLastError( ERROR_ACCESS_DENIED );
701 return FALSE;
704 else
706 TRACE( "cannot open device %s: err %d\n", debugstr_w(device), GetLastError() );
707 if (GetLastError() == ERROR_ACCESS_DENIED) return FALSE;
710 /* we couldn't open the device, fallback to default strategy */
712 switch(GetDriveTypeW( root ))
714 case DRIVE_UNKNOWN:
715 case DRIVE_NO_ROOT_DIR:
716 SetLastError( ERROR_NOT_READY );
717 break;
718 case DRIVE_REMOVABLE:
719 case DRIVE_FIXED:
721 WCHAR labelW[] = {'A',':','\\','.','w','i','n','d','o','w','s','-','l','a','b','e','l',0};
723 labelW[0] = device[4];
724 handle = CreateFileW( labelW, GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
725 CREATE_ALWAYS, 0, 0 );
726 if (handle != INVALID_HANDLE_VALUE)
728 char buffer[64];
729 DWORD size;
731 if (!WideCharToMultiByte( CP_UNIXCP, 0, label, -1, buffer, sizeof(buffer), NULL, NULL ))
732 buffer[sizeof(buffer)-1] = 0;
733 WriteFile( handle, buffer, strlen(buffer), &size, NULL );
734 CloseHandle( handle );
735 return TRUE;
737 break;
739 case DRIVE_REMOTE:
740 case DRIVE_RAMDISK:
741 case DRIVE_CDROM:
742 SetLastError( ERROR_ACCESS_DENIED );
743 break;
745 return FALSE;
748 /***********************************************************************
749 * SetVolumeLabelA (KERNEL32.@)
751 BOOL WINAPI SetVolumeLabelA(LPCSTR root, LPCSTR volname)
753 WCHAR *rootW = NULL, *volnameW = NULL;
754 BOOL ret;
756 if (root && !(rootW = FILE_name_AtoW( root, FALSE ))) return FALSE;
757 if (volname && !(volnameW = FILE_name_AtoW( volname, TRUE ))) return FALSE;
758 ret = SetVolumeLabelW( rootW, volnameW );
759 HeapFree( GetProcessHeap(), 0, volnameW );
760 return ret;
764 /***********************************************************************
765 * GetVolumeNameForVolumeMountPointA (KERNEL32.@)
767 BOOL WINAPI GetVolumeNameForVolumeMountPointA( LPCSTR path, LPSTR volume, DWORD size )
769 BOOL ret;
770 WCHAR volumeW[50], *pathW = NULL;
771 DWORD len = min( sizeof(volumeW) / sizeof(WCHAR), size );
773 TRACE("(%s, %p, %x)\n", debugstr_a(path), volume, size);
775 if (!path || !(pathW = FILE_name_AtoW( path, TRUE )))
776 return FALSE;
778 if ((ret = GetVolumeNameForVolumeMountPointW( pathW, volumeW, len )))
779 FILE_name_WtoA( volumeW, -1, volume, len );
781 HeapFree( GetProcessHeap(), 0, pathW );
782 return ret;
785 /***********************************************************************
786 * GetVolumeNameForVolumeMountPointW (KERNEL32.@)
788 BOOL WINAPI GetVolumeNameForVolumeMountPointW( LPCWSTR path, LPWSTR volume, DWORD size )
790 BOOL ret = FALSE;
791 static const WCHAR fmt[] =
792 { '\\','\\','?','\\','V','o','l','u','m','e','{','%','0','2','x','}','\\',0 };
794 TRACE("(%s, %p, %x)\n", debugstr_w(path), volume, size);
796 if (!path || !path[0]) return FALSE;
798 if (size >= sizeof(fmt) / sizeof(WCHAR))
800 /* FIXME: will break when we support volume mounts */
801 sprintfW( volume, fmt, tolowerW( path[0] ) - 'a' );
802 ret = TRUE;
804 return ret;
807 /***********************************************************************
808 * DefineDosDeviceW (KERNEL32.@)
810 BOOL WINAPI DefineDosDeviceW( DWORD flags, LPCWSTR devname, LPCWSTR targetpath )
812 DWORD len, dosdev;
813 BOOL ret = FALSE;
814 char *path = NULL, *target, *p;
816 TRACE("%x, %s, %s\n", flags, debugstr_w(devname), debugstr_w(targetpath));
818 if (!(flags & DDD_REMOVE_DEFINITION))
820 if (!(flags & DDD_RAW_TARGET_PATH))
822 FIXME( "(0x%08x,%s,%s) DDD_RAW_TARGET_PATH flag not set, not supported yet\n",
823 flags, debugstr_w(devname), debugstr_w(targetpath) );
824 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
825 return FALSE;
828 len = WideCharToMultiByte( CP_UNIXCP, 0, targetpath, -1, NULL, 0, NULL, NULL );
829 if ((target = HeapAlloc( GetProcessHeap(), 0, len )))
831 WideCharToMultiByte( CP_UNIXCP, 0, targetpath, -1, target, len, NULL, NULL );
832 for (p = target; *p; p++) if (*p == '\\') *p = '/';
834 else
836 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
837 return FALSE;
840 else target = NULL;
842 /* first check for a DOS device */
844 if ((dosdev = RtlIsDosDeviceName_U( devname )))
846 WCHAR name[5];
848 memcpy( name, devname + HIWORD(dosdev)/sizeof(WCHAR), LOWORD(dosdev) );
849 name[LOWORD(dosdev)/sizeof(WCHAR)] = 0;
850 path = get_dos_device_path( name );
852 else if (isalphaW(devname[0]) && devname[1] == ':' && !devname[2]) /* drive mapping */
854 path = get_dos_device_path( devname );
856 else SetLastError( ERROR_FILE_NOT_FOUND );
858 if (path)
860 if (target)
862 TRACE( "creating symlink %s -> %s\n", path, target );
863 unlink( path );
864 if (!symlink( target, path )) ret = TRUE;
865 else FILE_SetDosError();
867 else
869 TRACE( "removing symlink %s\n", path );
870 if (!unlink( path )) ret = TRUE;
871 else FILE_SetDosError();
873 HeapFree( GetProcessHeap(), 0, path );
875 HeapFree( GetProcessHeap(), 0, target );
876 return ret;
880 /***********************************************************************
881 * DefineDosDeviceA (KERNEL32.@)
883 BOOL WINAPI DefineDosDeviceA(DWORD flags, LPCSTR devname, LPCSTR targetpath)
885 WCHAR *devW, *targetW = NULL;
886 BOOL ret;
888 if (!(devW = FILE_name_AtoW( devname, FALSE ))) return FALSE;
889 if (targetpath && !(targetW = FILE_name_AtoW( targetpath, TRUE ))) return FALSE;
890 ret = DefineDosDeviceW(flags, devW, targetW);
891 HeapFree( GetProcessHeap(), 0, targetW );
892 return ret;
896 /***********************************************************************
897 * QueryDosDeviceW (KERNEL32.@)
899 * returns array of strings terminated by \0, terminated by \0
901 DWORD WINAPI QueryDosDeviceW( LPCWSTR devname, LPWSTR target, DWORD bufsize )
903 static const WCHAR auxW[] = {'A','U','X',0};
904 static const WCHAR nulW[] = {'N','U','L',0};
905 static const WCHAR prnW[] = {'P','R','N',0};
906 static const WCHAR comW[] = {'C','O','M',0};
907 static const WCHAR lptW[] = {'L','P','T',0};
908 static const WCHAR rootW[] = {'A',':','\\',0};
909 static const WCHAR com0W[] = {'\\','?','?','\\','C','O','M','0',0};
910 static const WCHAR com1W[] = {'\\','D','o','s','D','e','v','i','c','e','s','\\','C','O','M','1',0,0};
911 static const WCHAR lpt1W[] = {'\\','D','o','s','D','e','v','i','c','e','s','\\','L','P','T','1',0,0};
913 UNICODE_STRING nt_name;
914 ANSI_STRING unix_name;
915 WCHAR nt_buffer[10];
916 NTSTATUS status;
918 if (!bufsize)
920 SetLastError( ERROR_INSUFFICIENT_BUFFER );
921 return 0;
924 if (devname)
926 WCHAR *p, name[5];
927 char *path, *link;
928 DWORD dosdev, ret = 0;
930 if ((dosdev = RtlIsDosDeviceName_U( devname )))
932 memcpy( name, devname + HIWORD(dosdev)/sizeof(WCHAR), LOWORD(dosdev) );
933 name[LOWORD(dosdev)/sizeof(WCHAR)] = 0;
935 else if (devname[0] && devname[1] == ':' && !devname[2])
937 memcpy( name, devname, 3 * sizeof(WCHAR) );
939 else
941 SetLastError( ERROR_BAD_PATHNAME );
942 return 0;
945 if (!(path = get_dos_device_path( name ))) return 0;
946 link = read_symlink( path );
947 HeapFree( GetProcessHeap(), 0, path );
949 if (link)
951 ret = MultiByteToWideChar( CP_UNIXCP, 0, link, -1, target, bufsize );
952 HeapFree( GetProcessHeap(), 0, link );
954 else if (dosdev) /* look for device defaults */
956 if (!strcmpiW( name, auxW ))
958 if (bufsize >= sizeof(com1W)/sizeof(WCHAR))
960 memcpy( target, com1W, sizeof(com1W) );
961 ret = sizeof(com1W)/sizeof(WCHAR);
963 else SetLastError( ERROR_INSUFFICIENT_BUFFER );
964 return ret;
966 if (!strcmpiW( name, prnW ))
968 if (bufsize >= sizeof(lpt1W)/sizeof(WCHAR))
970 memcpy( target, lpt1W, sizeof(lpt1W) );
971 ret = sizeof(lpt1W)/sizeof(WCHAR);
973 else SetLastError( ERROR_INSUFFICIENT_BUFFER );
974 return ret;
977 nt_buffer[0] = '\\';
978 nt_buffer[1] = '?';
979 nt_buffer[2] = '?';
980 nt_buffer[3] = '\\';
981 strcpyW( nt_buffer + 4, name );
982 RtlInitUnicodeString( &nt_name, nt_buffer );
983 status = wine_nt_to_unix_file_name( &nt_name, &unix_name, FILE_OPEN, TRUE );
984 if (status) SetLastError( RtlNtStatusToDosError(status) );
985 else
987 ret = MultiByteToWideChar( CP_UNIXCP, 0, unix_name.Buffer, -1, target, bufsize );
988 RtlFreeAnsiString( &unix_name );
992 if (ret)
994 if (ret < bufsize) target[ret++] = 0; /* add an extra null */
995 for (p = target; *p; p++) if (*p == '/') *p = '\\';
998 return ret;
1000 else /* return a list of all devices */
1002 WCHAR *p = target;
1003 int i;
1005 if (bufsize <= (sizeof(auxW)+sizeof(nulW)+sizeof(prnW))/sizeof(WCHAR))
1007 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1008 return 0;
1011 memcpy( p, auxW, sizeof(auxW) );
1012 p += sizeof(auxW) / sizeof(WCHAR);
1013 memcpy( p, nulW, sizeof(nulW) );
1014 p += sizeof(nulW) / sizeof(WCHAR);
1015 memcpy( p, prnW, sizeof(prnW) );
1016 p += sizeof(prnW) / sizeof(WCHAR);
1018 strcpyW( nt_buffer, com0W );
1019 RtlInitUnicodeString( &nt_name, nt_buffer );
1021 for (i = 1; i <= 9; i++)
1023 nt_buffer[7] = '0' + i;
1024 if (!wine_nt_to_unix_file_name( &nt_name, &unix_name, FILE_OPEN, TRUE ))
1026 RtlFreeAnsiString( &unix_name );
1027 if (p + 5 >= target + bufsize)
1029 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1030 return 0;
1032 strcpyW( p, comW );
1033 p[3] = '0' + i;
1034 p[4] = 0;
1035 p += 5;
1038 strcpyW( nt_buffer + 4, lptW );
1039 for (i = 1; i <= 9; i++)
1041 nt_buffer[7] = '0' + i;
1042 if (!wine_nt_to_unix_file_name( &nt_name, &unix_name, FILE_OPEN, TRUE ))
1044 RtlFreeAnsiString( &unix_name );
1045 if (p + 5 >= target + bufsize)
1047 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1048 return 0;
1050 strcpyW( p, lptW );
1051 p[3] = '0' + i;
1052 p[4] = 0;
1053 p += 5;
1057 strcpyW( nt_buffer + 4, rootW );
1058 RtlInitUnicodeString( &nt_name, nt_buffer );
1060 for (i = 0; i < 26; i++)
1062 nt_buffer[4] = 'a' + i;
1063 if (!wine_nt_to_unix_file_name( &nt_name, &unix_name, FILE_OPEN, TRUE ))
1065 RtlFreeAnsiString( &unix_name );
1066 if (p + 3 >= target + bufsize)
1068 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1069 return 0;
1071 *p++ = 'A' + i;
1072 *p++ = ':';
1073 *p++ = 0;
1076 *p++ = 0; /* terminating null */
1077 return p - target;
1082 /***********************************************************************
1083 * QueryDosDeviceA (KERNEL32.@)
1085 * returns array of strings terminated by \0, terminated by \0
1087 DWORD WINAPI QueryDosDeviceA( LPCSTR devname, LPSTR target, DWORD bufsize )
1089 DWORD ret = 0, retW;
1090 WCHAR *devnameW = NULL;
1091 LPWSTR targetW;
1093 if (devname && !(devnameW = FILE_name_AtoW( devname, FALSE ))) return 0;
1095 targetW = HeapAlloc( GetProcessHeap(),0, bufsize * sizeof(WCHAR) );
1096 if (!targetW)
1098 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1099 return 0;
1102 retW = QueryDosDeviceW(devnameW, targetW, bufsize);
1104 ret = FILE_name_WtoA( targetW, retW, target, bufsize );
1106 HeapFree(GetProcessHeap(), 0, targetW);
1107 return ret;
1111 /***********************************************************************
1112 * GetLogicalDrives (KERNEL32.@)
1114 DWORD WINAPI GetLogicalDrives(void)
1116 const char *config_dir = wine_get_config_dir();
1117 struct stat st;
1118 char *buffer, *dev;
1119 DWORD ret = 0;
1120 int i;
1122 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, strlen(config_dir) + sizeof("/dosdevices/a:") )))
1124 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1125 return 0;
1127 strcpy( buffer, config_dir );
1128 strcat( buffer, "/dosdevices/a:" );
1129 dev = buffer + strlen(buffer) - 2;
1131 for (i = 0; i < 26; i++)
1133 *dev = 'a' + i;
1134 if (!stat( buffer, &st )) ret |= (1 << i);
1136 HeapFree( GetProcessHeap(), 0, buffer );
1137 return ret;
1141 /***********************************************************************
1142 * GetLogicalDriveStringsA (KERNEL32.@)
1144 UINT WINAPI GetLogicalDriveStringsA( UINT len, LPSTR buffer )
1146 DWORD drives = GetLogicalDrives();
1147 UINT drive, count;
1149 for (drive = count = 0; drive < 26; drive++) if (drives & (1 << drive)) count++;
1150 if ((count * 4) + 1 > len) return count * 4 + 1;
1152 for (drive = 0; drive < 26; drive++)
1154 if (drives & (1 << drive))
1156 *buffer++ = 'a' + drive;
1157 *buffer++ = ':';
1158 *buffer++ = '\\';
1159 *buffer++ = 0;
1162 *buffer = 0;
1163 return count * 4;
1167 /***********************************************************************
1168 * GetLogicalDriveStringsW (KERNEL32.@)
1170 UINT WINAPI GetLogicalDriveStringsW( UINT len, LPWSTR buffer )
1172 DWORD drives = GetLogicalDrives();
1173 UINT drive, count;
1175 for (drive = count = 0; drive < 26; drive++) if (drives & (1 << drive)) count++;
1176 if ((count * 4) + 1 > len) return count * 4 + 1;
1178 for (drive = 0; drive < 26; drive++)
1180 if (drives & (1 << drive))
1182 *buffer++ = 'a' + drive;
1183 *buffer++ = ':';
1184 *buffer++ = '\\';
1185 *buffer++ = 0;
1188 *buffer = 0;
1189 return count * 4;
1193 /***********************************************************************
1194 * GetDriveTypeW (KERNEL32.@)
1196 * Returns the type of the disk drive specified. If root is NULL the
1197 * root of the current directory is used.
1199 * RETURNS
1201 * Type of drive (from Win32 SDK):
1203 * DRIVE_UNKNOWN unable to find out anything about the drive
1204 * DRIVE_NO_ROOT_DIR nonexistent root dir
1205 * DRIVE_REMOVABLE the disk can be removed from the machine
1206 * DRIVE_FIXED the disk cannot be removed from the machine
1207 * DRIVE_REMOTE network disk
1208 * DRIVE_CDROM CDROM drive
1209 * DRIVE_RAMDISK virtual disk in RAM
1211 UINT WINAPI GetDriveTypeW(LPCWSTR root) /* [in] String describing drive */
1213 FILE_FS_DEVICE_INFORMATION info;
1214 IO_STATUS_BLOCK io;
1215 NTSTATUS status;
1216 HANDLE handle;
1217 UINT ret;
1219 if (!open_device_root( root, &handle )) return DRIVE_NO_ROOT_DIR;
1221 status = NtQueryVolumeInformationFile( handle, &io, &info, sizeof(info), FileFsDeviceInformation );
1222 NtClose( handle );
1223 if (status != STATUS_SUCCESS)
1225 SetLastError( RtlNtStatusToDosError(status) );
1226 ret = DRIVE_UNKNOWN;
1228 else if ((ret = get_registry_drive_type( root )) == DRIVE_UNKNOWN)
1230 switch (info.DeviceType)
1232 case FILE_DEVICE_CD_ROM_FILE_SYSTEM: ret = DRIVE_CDROM; break;
1233 case FILE_DEVICE_VIRTUAL_DISK: ret = DRIVE_RAMDISK; break;
1234 case FILE_DEVICE_NETWORK_FILE_SYSTEM: ret = DRIVE_REMOTE; break;
1235 case FILE_DEVICE_DISK_FILE_SYSTEM:
1236 if (info.Characteristics & FILE_REMOTE_DEVICE) ret = DRIVE_REMOTE;
1237 else if (info.Characteristics & FILE_REMOVABLE_MEDIA) ret = DRIVE_REMOVABLE;
1238 else ret = DRIVE_FIXED;
1239 break;
1240 default:
1241 ret = DRIVE_UNKNOWN;
1242 break;
1245 TRACE( "%s -> %d\n", debugstr_w(root), ret );
1246 return ret;
1250 /***********************************************************************
1251 * GetDriveTypeA (KERNEL32.@)
1253 * See GetDriveTypeW.
1255 UINT WINAPI GetDriveTypeA( LPCSTR root )
1257 WCHAR *rootW = NULL;
1259 if (root && !(rootW = FILE_name_AtoW( root, FALSE ))) return DRIVE_NO_ROOT_DIR;
1260 return GetDriveTypeW( rootW );
1264 /***********************************************************************
1265 * GetDiskFreeSpaceExW (KERNEL32.@)
1267 * This function is used to acquire the size of the available and
1268 * total space on a logical volume.
1270 * RETURNS
1272 * Zero on failure, nonzero upon success. Use GetLastError to obtain
1273 * detailed error information.
1276 BOOL WINAPI GetDiskFreeSpaceExW( LPCWSTR root, PULARGE_INTEGER avail,
1277 PULARGE_INTEGER total, PULARGE_INTEGER totalfree )
1279 FILE_FS_SIZE_INFORMATION info;
1280 IO_STATUS_BLOCK io;
1281 NTSTATUS status;
1282 HANDLE handle;
1283 UINT units;
1285 TRACE( "%s,%p,%p,%p\n", debugstr_w(root), avail, total, totalfree );
1287 if (!open_device_root( root, &handle )) return FALSE;
1289 status = NtQueryVolumeInformationFile( handle, &io, &info, sizeof(info), FileFsSizeInformation );
1290 NtClose( handle );
1291 if (status != STATUS_SUCCESS)
1293 SetLastError( RtlNtStatusToDosError(status) );
1294 return FALSE;
1297 units = info.SectorsPerAllocationUnit * info.BytesPerSector;
1298 if (total) total->QuadPart = info.TotalAllocationUnits.QuadPart * units;
1299 if (totalfree) totalfree->QuadPart = info.AvailableAllocationUnits.QuadPart * units;
1300 /* FIXME: this one should take quotas into account */
1301 if (avail) avail->QuadPart = info.AvailableAllocationUnits.QuadPart * units;
1302 return TRUE;
1306 /***********************************************************************
1307 * GetDiskFreeSpaceExA (KERNEL32.@)
1309 * See GetDiskFreeSpaceExW.
1311 BOOL WINAPI GetDiskFreeSpaceExA( LPCSTR root, PULARGE_INTEGER avail,
1312 PULARGE_INTEGER total, PULARGE_INTEGER totalfree )
1314 WCHAR *rootW = NULL;
1316 if (root && !(rootW = FILE_name_AtoW( root, FALSE ))) return FALSE;
1317 return GetDiskFreeSpaceExW( rootW, avail, total, totalfree );
1321 /***********************************************************************
1322 * GetDiskFreeSpaceW (KERNEL32.@)
1324 BOOL WINAPI GetDiskFreeSpaceW( LPCWSTR root, LPDWORD cluster_sectors,
1325 LPDWORD sector_bytes, LPDWORD free_clusters,
1326 LPDWORD total_clusters )
1328 FILE_FS_SIZE_INFORMATION info;
1329 IO_STATUS_BLOCK io;
1330 NTSTATUS status;
1331 HANDLE handle;
1332 UINT units;
1334 TRACE( "%s,%p,%p,%p,%p\n", debugstr_w(root),
1335 cluster_sectors, sector_bytes, free_clusters, total_clusters );
1337 if (!open_device_root( root, &handle )) return FALSE;
1339 status = NtQueryVolumeInformationFile( handle, &io, &info, sizeof(info), FileFsSizeInformation );
1340 NtClose( handle );
1341 if (status != STATUS_SUCCESS)
1343 SetLastError( RtlNtStatusToDosError(status) );
1344 return FALSE;
1347 units = info.SectorsPerAllocationUnit * info.BytesPerSector;
1349 if( GetVersion() & 0x80000000) { /* win3.x, 9x, ME */
1350 /* cap the size and available at 2GB as per specs */
1351 if (info.TotalAllocationUnits.QuadPart * units > 0x7fffffff) {
1352 info.TotalAllocationUnits.QuadPart = 0x7fffffff / units;
1353 if (info.AvailableAllocationUnits.QuadPart * units > 0x7fffffff)
1354 info.AvailableAllocationUnits.QuadPart = 0x7fffffff / units;
1356 /* nr. of clusters is always <= 65335 */
1357 while( info.TotalAllocationUnits.QuadPart > 65535 ) {
1358 info.TotalAllocationUnits.QuadPart /= 2;
1359 info.AvailableAllocationUnits.QuadPart /= 2;
1360 info.SectorsPerAllocationUnit *= 2;
1364 if (cluster_sectors) *cluster_sectors = info.SectorsPerAllocationUnit;
1365 if (sector_bytes) *sector_bytes = info.BytesPerSector;
1366 if (free_clusters) *free_clusters = info.AvailableAllocationUnits.u.LowPart;
1367 if (total_clusters) *total_clusters = info.TotalAllocationUnits.u.LowPart;
1368 return TRUE;
1372 /***********************************************************************
1373 * GetDiskFreeSpaceA (KERNEL32.@)
1375 BOOL WINAPI GetDiskFreeSpaceA( LPCSTR root, LPDWORD cluster_sectors,
1376 LPDWORD sector_bytes, LPDWORD free_clusters,
1377 LPDWORD total_clusters )
1379 WCHAR *rootW = NULL;
1381 if (root && !(rootW = FILE_name_AtoW( root, FALSE ))) return FALSE;
1382 return GetDiskFreeSpaceW( rootW, cluster_sectors, sector_bytes, free_clusters, total_clusters );
1385 /***********************************************************************
1386 * GetVolumePathNameA (KERNEL32.@)
1388 BOOL WINAPI GetVolumePathNameA(LPCSTR filename, LPSTR volumepathname, DWORD buflen)
1390 FIXME("(%s, %p, %d), stub!\n", debugstr_a(filename), volumepathname, buflen);
1391 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1392 return FALSE;
1395 /***********************************************************************
1396 * GetVolumePathNameW (KERNEL32.@)
1398 BOOL WINAPI GetVolumePathNameW(LPCWSTR filename, LPWSTR volumepathname, DWORD buflen)
1400 FIXME("(%s, %p, %d), stub!\n", debugstr_w(filename), volumepathname, buflen);
1401 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1402 return FALSE;
1405 /***********************************************************************
1406 * FindFirstVolumeA (KERNEL32.@)
1408 HANDLE WINAPI FindFirstVolumeA(LPSTR volume, DWORD len)
1410 WCHAR *buffer = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1411 HANDLE handle = FindFirstVolumeW( buffer, len );
1413 if (handle != INVALID_HANDLE_VALUE)
1415 if (!WideCharToMultiByte( CP_ACP, 0, buffer, -1, volume, len, NULL, NULL ))
1417 FindVolumeClose( handle );
1418 handle = INVALID_HANDLE_VALUE;
1421 HeapFree( GetProcessHeap(), 0, buffer );
1422 return handle;
1425 /***********************************************************************
1426 * FindFirstVolumeW (KERNEL32.@)
1428 HANDLE WINAPI FindFirstVolumeW( LPWSTR volume, DWORD len )
1430 DWORD size = 1024;
1431 HANDLE mgr = CreateFileW( MOUNTMGR_DOS_DEVICE_NAME, 0, FILE_SHARE_READ|FILE_SHARE_WRITE,
1432 NULL, OPEN_EXISTING, 0, 0 );
1433 if (mgr == INVALID_HANDLE_VALUE) return INVALID_HANDLE_VALUE;
1435 for (;;)
1437 MOUNTMGR_MOUNT_POINT input;
1438 MOUNTMGR_MOUNT_POINTS *output;
1440 if (!(output = HeapAlloc( GetProcessHeap(), 0, size )))
1442 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1443 break;
1445 memset( &input, 0, sizeof(input) );
1447 if (!DeviceIoControl( mgr, IOCTL_MOUNTMGR_QUERY_POINTS, &input, sizeof(input),
1448 output, size, NULL, NULL ))
1450 if (GetLastError() != ERROR_MORE_DATA) break;
1451 size = output->Size;
1452 HeapFree( GetProcessHeap(), 0, output );
1453 continue;
1455 CloseHandle( mgr );
1456 /* abuse the Size field to store the current index */
1457 output->Size = 0;
1458 if (!FindNextVolumeW( output, volume, len ))
1460 HeapFree( GetProcessHeap(), 0, output );
1461 return INVALID_HANDLE_VALUE;
1463 return (HANDLE)output;
1465 CloseHandle( mgr );
1466 return INVALID_HANDLE_VALUE;
1469 /***********************************************************************
1470 * FindNextVolumeA (KERNEL32.@)
1472 BOOL WINAPI FindNextVolumeA( HANDLE handle, LPSTR volume, DWORD len )
1474 WCHAR *buffer = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1475 BOOL ret;
1477 if ((ret = FindNextVolumeW( handle, buffer, len )))
1479 if (!WideCharToMultiByte( CP_ACP, 0, buffer, -1, volume, len, NULL, NULL )) ret = FALSE;
1481 HeapFree( GetProcessHeap(), 0, buffer );
1482 return ret;
1485 /***********************************************************************
1486 * FindNextVolumeW (KERNEL32.@)
1488 BOOL WINAPI FindNextVolumeW( HANDLE handle, LPWSTR volume, DWORD len )
1490 MOUNTMGR_MOUNT_POINTS *data = handle;
1492 while (data->Size < data->NumberOfMountPoints)
1494 static const WCHAR volumeW[] = {'\\','?','?','\\','V','o','l','u','m','e','{',};
1495 WCHAR *link = (WCHAR *)((char *)data + data->MountPoints[data->Size].SymbolicLinkNameOffset);
1496 DWORD size = data->MountPoints[data->Size].SymbolicLinkNameLength;
1497 data->Size++;
1498 /* skip non-volumes */
1499 if (size < sizeof(volumeW) || memcmp( link, volumeW, sizeof(volumeW) )) continue;
1500 if (size + sizeof(WCHAR) >= len * sizeof(WCHAR))
1502 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1503 return FALSE;
1505 memcpy( volume, link, size );
1506 volume[1] = '\\'; /* map \??\ to \\?\ */
1507 volume[size / sizeof(WCHAR)] = '\\'; /* Windows appends a backslash */
1508 volume[size / sizeof(WCHAR) + 1] = 0;
1509 TRACE( "returning entry %u %s\n", data->Size - 1, debugstr_w(volume) );
1510 return TRUE;
1512 SetLastError( ERROR_NO_MORE_FILES );
1513 return FALSE;
1516 /***********************************************************************
1517 * FindVolumeClose (KERNEL32.@)
1519 BOOL WINAPI FindVolumeClose(HANDLE handle)
1521 return HeapFree( GetProcessHeap(), 0, handle );
1524 /***********************************************************************
1525 * FindFirstVolumeMountPointA (KERNEL32.@)
1527 HANDLE WINAPI FindFirstVolumeMountPointA(LPCSTR root, LPSTR mount_point, DWORD len)
1529 FIXME("(%s, %p, %d), stub!\n", debugstr_a(root), mount_point, len);
1530 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1531 return INVALID_HANDLE_VALUE;
1534 /***********************************************************************
1535 * FindFirstVolumeMountPointW (KERNEL32.@)
1537 HANDLE WINAPI FindFirstVolumeMountPointW(LPCWSTR root, LPWSTR mount_point, DWORD len)
1539 FIXME("(%s, %p, %d), stub!\n", debugstr_w(root), mount_point, len);
1540 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1541 return INVALID_HANDLE_VALUE;
1544 /***********************************************************************
1545 * FindVolumeMountPointClose (KERNEL32.@)
1547 BOOL WINAPI FindVolumeMountPointClose(HANDLE h)
1549 FIXME("(%p), stub!\n", h);
1550 return FALSE;