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
26 #include "wine/port.h"
33 #define WIN32_NO_STATUS
40 #define WINE_MOUNTMGR_EXTENSIONS
41 #include "ddk/mountmgr.h"
42 #include "kernel_private.h"
43 #include "wine/library.h"
44 #include "wine/unicode.h"
45 #include "wine/debug.h"
47 WINE_DEFAULT_DEBUG_CHANNEL(volume);
49 #define SUPERBLOCK_SIZE 2048
50 #define SYMBOLIC_LINK_QUERY 0x0001
52 #define CDFRAMES_PERSEC 75
53 #define CDFRAMES_PERMIN (CDFRAMES_PERSEC * 60)
54 #define FRAME_OF_ADDR(a) ((a)[1] * CDFRAMES_PERMIN + (a)[2] * CDFRAMES_PERSEC + (a)[3])
55 #define FRAME_OF_TOC(toc, idx) FRAME_OF_ADDR((toc)->TrackData[(idx) - (toc)->FirstTrack].Address)
57 #define GETWORD(buf,off) MAKEWORD(buf[(off)],buf[(off+1)])
58 #define GETLONG(buf,off) MAKELONG(GETWORD(buf,off),GETWORD(buf,off+2))
62 FS_ERROR, /* error accessing the device */
63 FS_UNKNOWN, /* unknown file system */
69 /* read a Unix symlink; returned buffer must be freed by caller */
70 static char *read_symlink( const char *path )
77 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, size )))
79 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
82 ret = readlink( path, buffer, size );
86 HeapFree( GetProcessHeap(), 0, buffer );
94 HeapFree( GetProcessHeap(), 0, buffer );
99 /* get the path of a dos device symlink in the $WINEPREFIX/dosdevices directory */
100 static char *get_dos_device_path( LPCWSTR name )
102 const char *config_dir = wine_get_config_dir();
106 if (!(buffer = HeapAlloc( GetProcessHeap(), 0,
107 strlen(config_dir) + sizeof("/dosdevices/") + 5 )))
109 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
112 strcpy( buffer, config_dir );
113 strcat( buffer, "/dosdevices/" );
114 dev = buffer + strlen(buffer);
115 /* no codepage conversion, DOS device names are ASCII anyway */
116 for (i = 0; i < 5; i++)
117 if (!(dev[i] = (char)tolowerW(name[i]))) break;
122 /* read the contents of an NT symlink object */
123 static NTSTATUS read_nt_symlink( const WCHAR *name, WCHAR *target, DWORD size )
126 OBJECT_ATTRIBUTES attr;
127 UNICODE_STRING nameW;
130 attr.Length = sizeof(attr);
131 attr.RootDirectory = 0;
132 attr.Attributes = OBJ_CASE_INSENSITIVE;
133 attr.ObjectName = &nameW;
134 attr.SecurityDescriptor = NULL;
135 attr.SecurityQualityOfService = NULL;
136 RtlInitUnicodeString( &nameW, name );
138 if (!(status = NtOpenSymbolicLinkObject( &handle, SYMBOLIC_LINK_QUERY, &attr )))
140 UNICODE_STRING targetW;
141 targetW.Buffer = target;
142 targetW.MaximumLength = (size - 1) * sizeof(WCHAR);
143 status = NtQuerySymbolicLinkObject( handle, &targetW, NULL );
144 if (!status) target[targetW.Length / sizeof(WCHAR)] = 0;
150 /* open a handle to a device root */
151 static BOOL open_device_root( LPCWSTR root, HANDLE *handle )
153 static const WCHAR default_rootW[] = {'\\',0};
154 UNICODE_STRING nt_name;
155 OBJECT_ATTRIBUTES attr;
159 if (!root) root = default_rootW;
160 if (!RtlDosPathNameToNtPathName_U( root, &nt_name, NULL, NULL ))
162 SetLastError( ERROR_PATH_NOT_FOUND );
165 attr.Length = sizeof(attr);
166 attr.RootDirectory = 0;
167 attr.Attributes = OBJ_CASE_INSENSITIVE;
168 attr.ObjectName = &nt_name;
169 attr.SecurityDescriptor = NULL;
170 attr.SecurityQualityOfService = NULL;
172 status = NtOpenFile( handle, 0, &attr, &io, 0,
173 FILE_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT );
174 RtlFreeUnicodeString( &nt_name );
175 if (status != STATUS_SUCCESS)
177 SetLastError( RtlNtStatusToDosError(status) );
183 /* query the type of a drive from the mount manager */
184 static DWORD get_mountmgr_drive_type( LPCWSTR root )
187 struct mountmgr_unix_drive data;
189 memset( &data, 0, sizeof(data) );
190 if (root) data.letter = root[0];
193 WCHAR curdir[MAX_PATH];
194 GetCurrentDirectoryW( MAX_PATH, curdir );
195 if (curdir[1] != ':' || curdir[2] != '\\') return DRIVE_UNKNOWN;
196 data.letter = curdir[0];
199 mgr = CreateFileW( MOUNTMGR_DOS_DEVICE_NAME, GENERIC_READ,
200 FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0 );
201 if (mgr == INVALID_HANDLE_VALUE) return DRIVE_UNKNOWN;
203 if (!DeviceIoControl( mgr, IOCTL_MOUNTMGR_QUERY_UNIX_DRIVE, &data, sizeof(data), &data,
204 sizeof(data), NULL, NULL ) && GetLastError() != ERROR_MORE_DATA)
205 data.type = DRIVE_UNKNOWN;
211 /* get the label by reading it from a file at the root of the filesystem */
212 static void get_filesystem_label( const WCHAR *device, WCHAR *label, DWORD len )
215 WCHAR labelW[] = {'A',':','\\','.','w','i','n','d','o','w','s','-','l','a','b','e','l',0};
217 labelW[0] = device[4];
218 handle = CreateFileW( labelW, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
219 OPEN_EXISTING, 0, 0 );
220 if (handle != INVALID_HANDLE_VALUE)
222 char buffer[256], *p;
225 if (!ReadFile( handle, buffer, sizeof(buffer)-1, &size, NULL )) size = 0;
226 CloseHandle( handle );
228 while (p > buffer && (p[-1] == ' ' || p[-1] == '\r' || p[-1] == '\n')) p--;
230 if (!MultiByteToWideChar( CP_UNIXCP, 0, buffer, -1, label, len ))
236 /* get the serial number by reading it from a file at the root of the filesystem */
237 static DWORD get_filesystem_serial( const WCHAR *device )
240 WCHAR serialW[] = {'A',':','\\','.','w','i','n','d','o','w','s','-','s','e','r','i','a','l',0};
242 serialW[0] = device[4];
243 handle = CreateFileW( serialW, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
244 OPEN_EXISTING, 0, 0 );
245 if (handle != INVALID_HANDLE_VALUE)
250 if (!ReadFile( handle, buffer, sizeof(buffer)-1, &size, NULL )) size = 0;
251 CloseHandle( handle );
253 return strtoul( buffer, NULL, 16 );
259 /******************************************************************
260 * VOLUME_FindCdRomDataBestVoldesc
262 static DWORD VOLUME_FindCdRomDataBestVoldesc( HANDLE handle )
264 BYTE cur_vd_type, max_vd_type = 0;
266 DWORD size, offs, best_offs = 0, extra_offs = 0;
268 for (offs = 0x8000; offs <= 0x9800; offs += 0x800)
270 /* if 'CDROM' occurs at position 8, this is a pre-iso9660 cd, and
271 * the volume label is displaced forward by 8
273 if (SetFilePointer( handle, offs, NULL, FILE_BEGIN ) != offs) break;
274 if (!ReadFile( handle, buffer, sizeof(buffer), &size, NULL )) break;
275 if (size != sizeof(buffer)) break;
276 /* check for non-ISO9660 signature */
277 if (!memcmp( buffer + 11, "ROM", 3 )) extra_offs = 8;
278 cur_vd_type = buffer[extra_offs];
279 if (cur_vd_type == 0xff) /* voldesc set terminator */
281 if (cur_vd_type > max_vd_type)
283 max_vd_type = cur_vd_type;
284 best_offs = offs + extra_offs;
291 /***********************************************************************
292 * VOLUME_ReadFATSuperblock
294 static enum fs_type VOLUME_ReadFATSuperblock( HANDLE handle, BYTE *buff )
298 /* try a fixed disk, with a FAT partition */
299 if (SetFilePointer( handle, 0, NULL, FILE_BEGIN ) != 0 ||
300 !ReadFile( handle, buff, SUPERBLOCK_SIZE, &size, NULL ))
302 if (GetLastError() == ERROR_BAD_DEV_TYPE) return FS_UNKNOWN; /* not a real device */
306 if (size < SUPERBLOCK_SIZE) return FS_UNKNOWN;
308 /* FIXME: do really all FAT have their name beginning with
309 * "FAT" ? (At least FAT12, FAT16 and FAT32 have :)
311 if (!memcmp(buff+0x36, "FAT", 3) || !memcmp(buff+0x52, "FAT", 3))
313 /* guess which type of FAT we have */
315 unsigned int sectors,
324 sect_per_fat = GETWORD(buff, 0x16);
325 if (!sect_per_fat) sect_per_fat = GETLONG(buff, 0x24);
326 total_sectors = GETWORD(buff, 0x13);
328 total_sectors = GETLONG(buff, 0x20);
329 num_boot_sectors = GETWORD(buff, 0x0e);
330 num_fats = buff[0x10];
331 num_root_dir_ents = GETWORD(buff, 0x11);
332 bytes_per_sector = GETWORD(buff, 0x0b);
333 sectors_per_cluster = buff[0x0d];
334 /* check if the parameters are reasonable and will not cause
335 * arithmetic errors in the calculation */
336 reasonable = num_boot_sectors < total_sectors &&
338 bytes_per_sector >= 512 && bytes_per_sector % 512 == 0 &&
339 sectors_per_cluster > 1;
340 if (!reasonable) return FS_UNKNOWN;
341 sectors = total_sectors - num_boot_sectors - num_fats * sect_per_fat -
342 (num_root_dir_ents * 32 + bytes_per_sector - 1) / bytes_per_sector;
343 nclust = sectors / sectors_per_cluster;
344 if ((buff[0x42] == 0x28 || buff[0x42] == 0x29) &&
345 !memcmp(buff+0x52, "FAT", 3)) return FS_FAT32;
348 if ((buff[0x26] == 0x28 || buff[0x26] == 0x29) &&
349 !memcmp(buff+0x36, "FAT", 3))
357 /***********************************************************************
358 * VOLUME_ReadCDSuperblock
360 static enum fs_type VOLUME_ReadCDSuperblock( HANDLE handle, BYTE *buff )
362 DWORD size, offs = VOLUME_FindCdRomDataBestVoldesc( handle );
364 if (!offs) return FS_UNKNOWN;
366 if (SetFilePointer( handle, offs, NULL, FILE_BEGIN ) != offs ||
367 !ReadFile( handle, buff, SUPERBLOCK_SIZE, &size, NULL ) ||
368 size != SUPERBLOCK_SIZE)
371 /* check for iso9660 present */
372 if (!memcmp(&buff[1], "CD001", 5)) return FS_ISO9660;
377 /**************************************************************************
378 * VOLUME_GetSuperblockLabel
380 static void VOLUME_GetSuperblockLabel( const WCHAR *device, enum fs_type type, const BYTE *superblock,
381 WCHAR *label, DWORD len )
383 const BYTE *label_ptr = NULL;
392 get_filesystem_label( device, label, len );
395 label_ptr = superblock + 0x2b;
399 label_ptr = superblock + 0x47;
404 BYTE ver = superblock[0x5a];
406 if (superblock[0x58] == 0x25 && superblock[0x59] == 0x2f && /* Unicode ID */
407 ((ver == 0x40) || (ver == 0x43) || (ver == 0x45)))
408 { /* yippee, unicode */
411 if (len > 17) len = 17;
412 for (i = 0; i < len-1; i++)
413 label[i] = (superblock[40+2*i] << 8) | superblock[41+2*i];
415 while (i && label[i-1] == ' ') label[--i] = 0;
418 label_ptr = superblock + 40;
423 if (label_len) RtlMultiByteToUnicodeN( label, (len-1) * sizeof(WCHAR),
424 &label_len, (LPCSTR)label_ptr, label_len );
425 label_len /= sizeof(WCHAR);
426 label[label_len] = 0;
427 while (label_len && label[label_len-1] == ' ') label[--label_len] = 0;
431 /**************************************************************************
432 * VOLUME_GetSuperblockSerial
434 static DWORD VOLUME_GetSuperblockSerial( const WCHAR *device, enum fs_type type, const BYTE *superblock )
441 return get_filesystem_serial( device );
443 return GETLONG( superblock, 0x27 );
445 return GETLONG( superblock, 0x33 );
451 sum[0] = sum[1] = sum[2] = sum[3] = 0;
452 for (i = 0; i < 2048; i += 4)
454 /* DON'T optimize this into DWORD !! (breaks overflow) */
455 sum[0] += superblock[i+0];
456 sum[1] += superblock[i+1];
457 sum[2] += superblock[i+2];
458 sum[3] += superblock[i+3];
461 * OK, another braindead one... argh. Just believe it.
462 * Me$$ysoft chose to reverse the serial number in NT4/W2K.
463 * It's true and nobody will ever be able to change it.
465 if (GetVersion() & 0x80000000)
466 return (sum[3] << 24) | (sum[2] << 16) | (sum[1] << 8) | sum[0];
468 return (sum[0] << 24) | (sum[1] << 16) | (sum[2] << 8) | sum[3];
475 /**************************************************************************
476 * VOLUME_GetAudioCDSerial
478 static DWORD VOLUME_GetAudioCDSerial( const CDROM_TOC *toc )
483 for (i = 0; i <= toc->LastTrack - toc->FirstTrack; i++)
484 serial += ((toc->TrackData[i].Address[1] << 16) |
485 (toc->TrackData[i].Address[2] << 8) |
486 toc->TrackData[i].Address[3]);
489 * dwStart, dwEnd collect the beginning and end of the disc respectively, in
491 * There it is collected for correcting the serial when there are less than
494 if (toc->LastTrack - toc->FirstTrack + 1 < 3)
496 DWORD dwStart = FRAME_OF_TOC(toc, toc->FirstTrack);
497 DWORD dwEnd = FRAME_OF_TOC(toc, toc->LastTrack + 1);
498 serial += dwEnd - dwStart;
504 /***********************************************************************
505 * GetVolumeInformationW (KERNEL32.@)
507 BOOL WINAPI GetVolumeInformationW( LPCWSTR root, LPWSTR label, DWORD label_len,
508 DWORD *serial, DWORD *filename_len, DWORD *flags,
509 LPWSTR fsname, DWORD fsname_len )
511 static const WCHAR audiocdW[] = {'A','u','d','i','o',' ','C','D',0};
512 static const WCHAR fatW[] = {'F','A','T',0};
513 static const WCHAR fat32W[] = {'F','A','T','3','2',0};
514 static const WCHAR ntfsW[] = {'N','T','F','S',0};
515 static const WCHAR cdfsW[] = {'C','D','F','S',0};
516 static const WCHAR default_rootW[] = {'\\',0};
518 WCHAR device[] = {'\\','\\','.','\\','A',':',0};
521 UNICODE_STRING nt_name;
523 OBJECT_ATTRIBUTES attr;
525 enum fs_type type = FS_UNKNOWN;
528 if (!root) root = default_rootW;
529 if (!RtlDosPathNameToNtPathName_U( root, &nt_name, NULL, NULL ))
531 SetLastError( ERROR_PATH_NOT_FOUND );
534 /* there must be exactly one backslash in the name, at the end */
535 p = memchrW( nt_name.Buffer + 4, '\\', (nt_name.Length - 4) / sizeof(WCHAR) );
536 if (p != nt_name.Buffer + nt_name.Length / sizeof(WCHAR) - 1)
538 SetLastError( ERROR_INVALID_NAME );
541 device[4] = nt_name.Buffer[4];
543 /* try to open the device */
545 attr.Length = sizeof(attr);
546 attr.RootDirectory = 0;
547 attr.Attributes = OBJ_CASE_INSENSITIVE;
548 attr.ObjectName = &nt_name;
549 attr.SecurityDescriptor = NULL;
550 attr.SecurityQualityOfService = NULL;
552 nt_name.Length -= sizeof(WCHAR); /* without trailing slash */
553 status = NtOpenFile( &handle, GENERIC_READ, &attr, &io, FILE_SHARE_READ | FILE_SHARE_WRITE,
554 FILE_NON_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT );
555 nt_name.Length += sizeof(WCHAR);
557 if (status == STATUS_SUCCESS)
559 BYTE superblock[SUPERBLOCK_SIZE];
563 /* check for audio CD */
564 /* FIXME: we only check the first track for now */
565 if (DeviceIoControl( handle, IOCTL_CDROM_READ_TOC, NULL, 0, &toc, sizeof(toc), &br, 0 ))
567 if (!(toc.TrackData[0].Control & 0x04)) /* audio track */
569 TRACE( "%s: found audio CD\n", debugstr_w(nt_name.Buffer) );
570 if (label) lstrcpynW( label, audiocdW, label_len );
571 if (serial) *serial = VOLUME_GetAudioCDSerial( &toc );
572 CloseHandle( handle );
576 type = VOLUME_ReadCDSuperblock( handle, superblock );
580 type = VOLUME_ReadFATSuperblock( handle, superblock );
581 if (type == FS_UNKNOWN) type = VOLUME_ReadCDSuperblock( handle, superblock );
583 CloseHandle( handle );
584 TRACE( "%s: found fs type %d\n", debugstr_w(nt_name.Buffer), type );
585 if (type == FS_ERROR) goto done;
587 if (label && label_len) VOLUME_GetSuperblockLabel( device, type, superblock, label, label_len );
588 if (serial) *serial = VOLUME_GetSuperblockSerial( device, type, superblock );
591 else TRACE( "cannot open device %s: err %d\n", debugstr_w(nt_name.Buffer), GetLastError() );
593 /* we couldn't open the device, fallback to default strategy */
595 switch(GetDriveTypeW( root ))
598 case DRIVE_NO_ROOT_DIR:
599 SetLastError( ERROR_NOT_READY );
601 case DRIVE_REMOVABLE:
612 if (label && label_len) get_filesystem_label( device, label, label_len );
613 if (serial) *serial = get_filesystem_serial( device );
615 fill_fs_info: /* now fill in the information that depends on the file system type */
620 if (fsname) lstrcpynW( fsname, cdfsW, fsname_len );
621 if (filename_len) *filename_len = 221;
622 if (flags) *flags = FILE_READ_ONLY_VOLUME;
625 if (fsname) lstrcpynW( fsname, fatW, fsname_len );
627 if (type == FS_FAT32 && fsname) lstrcpynW( fsname, fat32W, fsname_len );
628 if (filename_len) *filename_len = 255;
629 if (flags) *flags = FILE_CASE_PRESERVED_NAMES; /* FIXME */
632 if (fsname) lstrcpynW( fsname, ntfsW, fsname_len );
633 if (filename_len) *filename_len = 255;
634 if (flags) *flags = FILE_CASE_PRESERVED_NAMES;
640 RtlFreeUnicodeString( &nt_name );
645 /***********************************************************************
646 * GetVolumeInformationA (KERNEL32.@)
648 BOOL WINAPI GetVolumeInformationA( LPCSTR root, LPSTR label,
649 DWORD label_len, DWORD *serial,
650 DWORD *filename_len, DWORD *flags,
651 LPSTR fsname, DWORD fsname_len )
654 LPWSTR labelW, fsnameW;
657 if (root && !(rootW = FILE_name_AtoW( root, FALSE ))) return FALSE;
659 labelW = label ? HeapAlloc(GetProcessHeap(), 0, label_len * sizeof(WCHAR)) : NULL;
660 fsnameW = fsname ? HeapAlloc(GetProcessHeap(), 0, fsname_len * sizeof(WCHAR)) : NULL;
662 if ((ret = GetVolumeInformationW(rootW, labelW, label_len, serial,
663 filename_len, flags, fsnameW, fsname_len)))
665 if (label) FILE_name_WtoA( labelW, -1, label, label_len );
666 if (fsname) FILE_name_WtoA( fsnameW, -1, fsname, fsname_len );
669 HeapFree( GetProcessHeap(), 0, labelW );
670 HeapFree( GetProcessHeap(), 0, fsnameW );
676 /***********************************************************************
677 * SetVolumeLabelW (KERNEL32.@)
679 BOOL WINAPI SetVolumeLabelW( LPCWSTR root, LPCWSTR label )
681 WCHAR device[] = {'\\','\\','.','\\','A',':',0};
683 enum fs_type type = FS_UNKNOWN;
687 WCHAR path[MAX_PATH];
688 GetCurrentDirectoryW( MAX_PATH, path );
693 if (!root[0] || root[1] != ':')
695 SetLastError( ERROR_INVALID_NAME );
701 /* try to open the device */
703 handle = CreateFileW( device, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE,
704 NULL, OPEN_EXISTING, 0, 0 );
705 if (handle != INVALID_HANDLE_VALUE)
707 BYTE superblock[SUPERBLOCK_SIZE];
709 type = VOLUME_ReadFATSuperblock( handle, superblock );
710 if (type == FS_UNKNOWN) type = VOLUME_ReadCDSuperblock( handle, superblock );
711 CloseHandle( handle );
712 if (type != FS_UNKNOWN)
714 /* we can't set the label on FAT or CDROM file systems */
715 TRACE( "cannot set label on device %s type %d\n", debugstr_w(device), type );
716 SetLastError( ERROR_ACCESS_DENIED );
722 TRACE( "cannot open device %s: err %d\n", debugstr_w(device), GetLastError() );
723 if (GetLastError() == ERROR_ACCESS_DENIED) return FALSE;
726 /* we couldn't open the device, fallback to default strategy */
728 switch(GetDriveTypeW( root ))
731 case DRIVE_NO_ROOT_DIR:
732 SetLastError( ERROR_NOT_READY );
734 case DRIVE_REMOVABLE:
737 WCHAR labelW[] = {'A',':','\\','.','w','i','n','d','o','w','s','-','l','a','b','e','l',0};
739 labelW[0] = device[4];
741 if (!label[0]) /* delete label file when setting an empty label */
742 return DeleteFileW( labelW ) || GetLastError() == ERROR_FILE_NOT_FOUND;
744 handle = CreateFileW( labelW, GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
745 CREATE_ALWAYS, 0, 0 );
746 if (handle != INVALID_HANDLE_VALUE)
751 if (!WideCharToMultiByte( CP_UNIXCP, 0, label, -1, buffer, sizeof(buffer)-1, NULL, NULL ))
752 buffer[sizeof(buffer)-2] = 0;
753 strcat( buffer, "\n" );
754 WriteFile( handle, buffer, strlen(buffer), &size, NULL );
755 CloseHandle( handle );
763 SetLastError( ERROR_ACCESS_DENIED );
769 /***********************************************************************
770 * SetVolumeLabelA (KERNEL32.@)
772 BOOL WINAPI SetVolumeLabelA(LPCSTR root, LPCSTR volname)
774 WCHAR *rootW = NULL, *volnameW = NULL;
777 if (root && !(rootW = FILE_name_AtoW( root, FALSE ))) return FALSE;
778 if (volname && !(volnameW = FILE_name_AtoW( volname, TRUE ))) return FALSE;
779 ret = SetVolumeLabelW( rootW, volnameW );
780 HeapFree( GetProcessHeap(), 0, volnameW );
785 /***********************************************************************
786 * GetVolumeNameForVolumeMountPointA (KERNEL32.@)
788 BOOL WINAPI GetVolumeNameForVolumeMountPointA( LPCSTR path, LPSTR volume, DWORD size )
791 WCHAR volumeW[50], *pathW = NULL;
792 DWORD len = min( sizeof(volumeW) / sizeof(WCHAR), size );
794 TRACE("(%s, %p, %x)\n", debugstr_a(path), volume, size);
796 if (!path || !(pathW = FILE_name_AtoW( path, TRUE )))
799 if ((ret = GetVolumeNameForVolumeMountPointW( pathW, volumeW, len )))
800 FILE_name_WtoA( volumeW, -1, volume, len );
802 HeapFree( GetProcessHeap(), 0, pathW );
806 /***********************************************************************
807 * GetVolumeNameForVolumeMountPointW (KERNEL32.@)
809 BOOL WINAPI GetVolumeNameForVolumeMountPointW( LPCWSTR path, LPWSTR volume, DWORD size )
811 static const WCHAR prefixW[] = {'\\','D','o','s','D','e','v','i','c','e','s','\\',0};
812 static const WCHAR volumeW[] = {'\\','?','?','\\','V','o','l','u','m','e','{',0};
813 static const WCHAR trailingW[] = {'\\',0};
815 MOUNTMGR_MOUNT_POINT *input = NULL, *o1;
816 MOUNTMGR_MOUNT_POINTS *output = NULL;
819 DWORD i, i_size = 1024, o_size = 1024;
820 WCHAR *nonpersist_name;
821 WCHAR symlink_name[MAX_PATH];
823 HANDLE mgr = INVALID_HANDLE_VALUE;
826 TRACE("(%s, %p, %x)\n", debugstr_w(path), volume, size);
827 if (path[lstrlenW(path)-1] != '\\')
829 SetLastError( ERROR_INVALID_NAME );
835 SetLastError( ERROR_FILENAME_EXCED_RANGE );
838 /* if length of input is > 3 then it must be a mounted folder */
839 if (lstrlenW(path) > 3)
841 FIXME("Mounted Folders are not yet supported\n");
842 SetLastError( ERROR_NOT_A_REPARSE_POINT );
846 mgr = CreateFileW( MOUNTMGR_DOS_DEVICE_NAME, 0, FILE_SHARE_READ,
847 NULL, OPEN_EXISTING, 0, 0 );
848 if (mgr == INVALID_HANDLE_VALUE) return FALSE;
850 if (!(input = HeapAlloc( GetProcessHeap(), 0, i_size )))
852 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
856 if (!(output = HeapAlloc( GetProcessHeap(), 0, o_size )))
858 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
862 /* construct the symlink name as "\DosDevices\C:" */
863 lstrcpyW( symlink_name, prefixW );
864 lstrcatW( symlink_name, path );
865 symlink_name[lstrlenW(symlink_name)-1] = 0;
867 /* Take the mount point and get the "nonpersistent name" */
868 /* We will then take that and get the volume name */
869 nonpersist_name = (WCHAR *)(input + 1);
870 status = read_nt_symlink( symlink_name, nonpersist_name, i_size - sizeof(*input) );
871 TRACE("read_nt_symlink got stat=%x, for %s, got <%s>\n", status,
872 debugstr_w(symlink_name), debugstr_w(nonpersist_name));
873 if (status != STATUS_SUCCESS)
875 SetLastError( ERROR_FILE_NOT_FOUND );
879 /* Now take the "nonpersistent name" and ask the mountmgr */
880 /* to give us all the mount points. One of them will be */
881 /* the volume name (format of \??\Volume{). */
882 memset( input, 0, sizeof(*input) ); /* clear all input parameters */
883 input->DeviceNameOffset = sizeof(*input);
884 input->DeviceNameLength = lstrlenW( nonpersist_name) * sizeof(WCHAR);
885 i_size = input->DeviceNameOffset + input->DeviceNameLength;
887 output->Size = o_size;
889 /* now get the true volume name from the mountmgr */
890 if (!DeviceIoControl( mgr, IOCTL_MOUNTMGR_QUERY_POINTS, input, i_size,
891 output, o_size, NULL, NULL ))
894 /* Verify and return the data, note string is not null terminated */
895 TRACE("found %d matching mount points\n", output->NumberOfMountPoints);
896 if (output->NumberOfMountPoints < 1)
898 SetLastError( ERROR_NO_VOLUME_ID );
901 o1 = &output->MountPoints[0];
903 /* look for the volume name in returned values */
904 for(i=0;i<output->NumberOfMountPoints;i++)
906 p = (WCHAR*)((char *)output + o1->SymbolicLinkNameOffset);
907 r = (char *)output + o1->UniqueIdOffset;
908 TRACE("found symlink=%s, unique=%s, devname=%s\n",
909 debugstr_wn(p, o1->SymbolicLinkNameLength/sizeof(WCHAR)),
910 debugstr_an(r, o1->UniqueIdLength),
911 debugstr_wn((WCHAR*)((char *)output + o1->DeviceNameOffset),
912 o1->DeviceNameLength/sizeof(WCHAR)));
914 if (!strncmpW( p, volumeW, (sizeof(volumeW)-1)/sizeof(WCHAR) ))
916 /* is there space in the return variable ?? */
917 if ((o1->SymbolicLinkNameLength/sizeof(WCHAR))+2 > size)
919 SetLastError( ERROR_FILENAME_EXCED_RANGE );
922 memcpy( volume, p, o1->SymbolicLinkNameLength );
923 volume[o1->SymbolicLinkNameLength / sizeof(WCHAR)] = 0;
924 lstrcatW( volume, trailingW );
925 /* change second char from '?' to '\' */
934 HeapFree( GetProcessHeap(), 0, input );
935 HeapFree( GetProcessHeap(), 0, output );
940 /***********************************************************************
941 * DefineDosDeviceW (KERNEL32.@)
943 BOOL WINAPI DefineDosDeviceW( DWORD flags, LPCWSTR devname, LPCWSTR targetpath )
947 char *path = NULL, *target, *p;
949 TRACE("%x, %s, %s\n", flags, debugstr_w(devname), debugstr_w(targetpath));
951 if (!(flags & DDD_REMOVE_DEFINITION))
953 if (!(flags & DDD_RAW_TARGET_PATH))
955 FIXME( "(0x%08x,%s,%s) DDD_RAW_TARGET_PATH flag not set, not supported yet\n",
956 flags, debugstr_w(devname), debugstr_w(targetpath) );
957 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
961 len = WideCharToMultiByte( CP_UNIXCP, 0, targetpath, -1, NULL, 0, NULL, NULL );
962 if ((target = HeapAlloc( GetProcessHeap(), 0, len )))
964 WideCharToMultiByte( CP_UNIXCP, 0, targetpath, -1, target, len, NULL, NULL );
965 for (p = target; *p; p++) if (*p == '\\') *p = '/';
969 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
975 /* first check for a DOS device */
977 if ((dosdev = RtlIsDosDeviceName_U( devname )))
981 memcpy( name, devname + HIWORD(dosdev)/sizeof(WCHAR), LOWORD(dosdev) );
982 name[LOWORD(dosdev)/sizeof(WCHAR)] = 0;
983 path = get_dos_device_path( name );
985 else if (isalphaW(devname[0]) && devname[1] == ':' && !devname[2]) /* drive mapping */
987 path = get_dos_device_path( devname );
989 else SetLastError( ERROR_FILE_NOT_FOUND );
995 TRACE( "creating symlink %s -> %s\n", path, target );
997 if (!symlink( target, path )) ret = TRUE;
998 else FILE_SetDosError();
1002 TRACE( "removing symlink %s\n", path );
1003 if (!unlink( path )) ret = TRUE;
1004 else FILE_SetDosError();
1006 HeapFree( GetProcessHeap(), 0, path );
1008 HeapFree( GetProcessHeap(), 0, target );
1013 /***********************************************************************
1014 * DefineDosDeviceA (KERNEL32.@)
1016 BOOL WINAPI DefineDosDeviceA(DWORD flags, LPCSTR devname, LPCSTR targetpath)
1018 WCHAR *devW, *targetW = NULL;
1021 if (!(devW = FILE_name_AtoW( devname, FALSE ))) return FALSE;
1022 if (targetpath && !(targetW = FILE_name_AtoW( targetpath, TRUE ))) return FALSE;
1023 ret = DefineDosDeviceW(flags, devW, targetW);
1024 HeapFree( GetProcessHeap(), 0, targetW );
1029 /***********************************************************************
1030 * QueryDosDeviceW (KERNEL32.@)
1032 * returns array of strings terminated by \0, terminated by \0
1034 DWORD WINAPI QueryDosDeviceW( LPCWSTR devname, LPWSTR target, DWORD bufsize )
1036 static const WCHAR auxW[] = {'A','U','X',0};
1037 static const WCHAR nulW[] = {'N','U','L',0};
1038 static const WCHAR prnW[] = {'P','R','N',0};
1039 static const WCHAR comW[] = {'C','O','M',0};
1040 static const WCHAR lptW[] = {'L','P','T',0};
1041 static const WCHAR com0W[] = {'\\','?','?','\\','C','O','M','0',0};
1042 static const WCHAR com1W[] = {'\\','D','o','s','D','e','v','i','c','e','s','\\','C','O','M','1',0,0};
1043 static const WCHAR lpt1W[] = {'\\','D','o','s','D','e','v','i','c','e','s','\\','L','P','T','1',0,0};
1044 static const WCHAR dosdevW[] = {'\\','D','o','s','D','e','v','i','c','e','s','\\',0};
1046 UNICODE_STRING nt_name;
1047 ANSI_STRING unix_name;
1048 WCHAR nt_buffer[10];
1053 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1061 DWORD dosdev, ret = 0;
1063 if ((dosdev = RtlIsDosDeviceName_U( devname )))
1065 memcpy( name, devname + HIWORD(dosdev)/sizeof(WCHAR), LOWORD(dosdev) );
1066 name[LOWORD(dosdev)/sizeof(WCHAR)] = 0;
1073 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, sizeof(dosdevW) + strlenW(devname)*sizeof(WCHAR) )))
1075 SetLastError( ERROR_OUTOFMEMORY );
1078 memcpy( buffer, dosdevW, sizeof(dosdevW) );
1079 strcatW( buffer, devname );
1080 status = read_nt_symlink( buffer, target, bufsize );
1081 HeapFree( GetProcessHeap(), 0, buffer );
1084 SetLastError( RtlNtStatusToDosError(status) );
1087 ret = strlenW( target ) + 1;
1091 /* FIXME: should read NT symlink for all devices */
1093 if (!(path = get_dos_device_path( name ))) return 0;
1094 link = read_symlink( path );
1095 HeapFree( GetProcessHeap(), 0, path );
1099 ret = MultiByteToWideChar( CP_UNIXCP, 0, link, -1, target, bufsize );
1100 HeapFree( GetProcessHeap(), 0, link );
1102 else if (dosdev) /* look for device defaults */
1104 if (!strcmpiW( name, auxW ))
1106 if (bufsize >= sizeof(com1W)/sizeof(WCHAR))
1108 memcpy( target, com1W, sizeof(com1W) );
1109 ret = sizeof(com1W)/sizeof(WCHAR);
1111 else SetLastError( ERROR_INSUFFICIENT_BUFFER );
1114 if (!strcmpiW( name, prnW ))
1116 if (bufsize >= sizeof(lpt1W)/sizeof(WCHAR))
1118 memcpy( target, lpt1W, sizeof(lpt1W) );
1119 ret = sizeof(lpt1W)/sizeof(WCHAR);
1121 else SetLastError( ERROR_INSUFFICIENT_BUFFER );
1125 nt_buffer[0] = '\\';
1128 nt_buffer[3] = '\\';
1129 strcpyW( nt_buffer + 4, name );
1130 RtlInitUnicodeString( &nt_name, nt_buffer );
1131 status = wine_nt_to_unix_file_name( &nt_name, &unix_name, FILE_OPEN, TRUE );
1132 if (status) SetLastError( RtlNtStatusToDosError(status) );
1135 ret = MultiByteToWideChar( CP_UNIXCP, 0, unix_name.Buffer, -1, target, bufsize );
1136 RtlFreeAnsiString( &unix_name );
1142 if (ret < bufsize) target[ret++] = 0; /* add an extra null */
1143 for (p = target; *p; p++) if (*p == '/') *p = '\\';
1148 else /* return a list of all devices */
1150 OBJECT_ATTRIBUTES attr;
1155 if (bufsize <= (sizeof(auxW)+sizeof(nulW)+sizeof(prnW))/sizeof(WCHAR))
1157 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1161 /* FIXME: these should be NT symlinks too */
1163 memcpy( p, auxW, sizeof(auxW) );
1164 p += sizeof(auxW) / sizeof(WCHAR);
1165 memcpy( p, nulW, sizeof(nulW) );
1166 p += sizeof(nulW) / sizeof(WCHAR);
1167 memcpy( p, prnW, sizeof(prnW) );
1168 p += sizeof(prnW) / sizeof(WCHAR);
1170 strcpyW( nt_buffer, com0W );
1171 RtlInitUnicodeString( &nt_name, nt_buffer );
1173 for (i = 1; i <= 9; i++)
1175 nt_buffer[7] = '0' + i;
1176 if (!wine_nt_to_unix_file_name( &nt_name, &unix_name, FILE_OPEN, TRUE ))
1178 RtlFreeAnsiString( &unix_name );
1179 if (p + 5 >= target + bufsize)
1181 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1190 strcpyW( nt_buffer + 4, lptW );
1191 for (i = 1; i <= 9; i++)
1193 nt_buffer[7] = '0' + i;
1194 if (!wine_nt_to_unix_file_name( &nt_name, &unix_name, FILE_OPEN, TRUE ))
1196 RtlFreeAnsiString( &unix_name );
1197 if (p + 5 >= target + bufsize)
1199 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1209 RtlInitUnicodeString( &nt_name, dosdevW );
1210 nt_name.Length -= sizeof(WCHAR); /* without trailing slash */
1211 attr.Length = sizeof(attr);
1212 attr.RootDirectory = 0;
1213 attr.ObjectName = &nt_name;
1214 attr.Attributes = OBJ_CASE_INSENSITIVE;
1215 attr.SecurityDescriptor = NULL;
1216 attr.SecurityQualityOfService = NULL;
1217 status = NtOpenDirectoryObject( &handle, FILE_LIST_DIRECTORY, &attr );
1221 DIRECTORY_BASIC_INFORMATION *info = (DIRECTORY_BASIC_INFORMATION *)data;
1224 while (!NtQueryDirectoryObject( handle, info, sizeof(data), 1, 0, &ctx, &len ))
1226 if (p + info->ObjectName.Length/sizeof(WCHAR) + 1 >= target + bufsize)
1228 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1232 memcpy( p, info->ObjectName.Buffer, info->ObjectName.Length );
1233 p += info->ObjectName.Length/sizeof(WCHAR);
1239 *p++ = 0; /* terminating null */
1245 /***********************************************************************
1246 * QueryDosDeviceA (KERNEL32.@)
1248 * returns array of strings terminated by \0, terminated by \0
1250 DWORD WINAPI QueryDosDeviceA( LPCSTR devname, LPSTR target, DWORD bufsize )
1252 DWORD ret = 0, retW;
1253 WCHAR *devnameW = NULL;
1256 if (devname && !(devnameW = FILE_name_AtoW( devname, FALSE ))) return 0;
1258 targetW = HeapAlloc( GetProcessHeap(),0, bufsize * sizeof(WCHAR) );
1261 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1265 retW = QueryDosDeviceW(devnameW, targetW, bufsize);
1267 ret = FILE_name_WtoA( targetW, retW, target, bufsize );
1269 HeapFree(GetProcessHeap(), 0, targetW);
1274 /***********************************************************************
1275 * GetLogicalDrives (KERNEL32.@)
1277 DWORD WINAPI GetLogicalDrives(void)
1279 const char *config_dir = wine_get_config_dir();
1285 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, strlen(config_dir) + sizeof("/dosdevices/a:") )))
1287 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1290 strcpy( buffer, config_dir );
1291 strcat( buffer, "/dosdevices/a:" );
1292 dev = buffer + strlen(buffer) - 2;
1294 for (i = 0; i < 26; i++)
1297 if (!stat( buffer, &st )) ret |= (1 << i);
1299 HeapFree( GetProcessHeap(), 0, buffer );
1304 /***********************************************************************
1305 * GetLogicalDriveStringsA (KERNEL32.@)
1307 UINT WINAPI GetLogicalDriveStringsA( UINT len, LPSTR buffer )
1309 DWORD drives = GetLogicalDrives();
1312 for (drive = count = 0; drive < 26; drive++) if (drives & (1 << drive)) count++;
1313 if ((count * 4) + 1 > len) return count * 4 + 1;
1315 for (drive = 0; drive < 26; drive++)
1317 if (drives & (1 << drive))
1319 *buffer++ = 'A' + drive;
1330 /***********************************************************************
1331 * GetLogicalDriveStringsW (KERNEL32.@)
1333 UINT WINAPI GetLogicalDriveStringsW( UINT len, LPWSTR buffer )
1335 DWORD drives = GetLogicalDrives();
1338 for (drive = count = 0; drive < 26; drive++) if (drives & (1 << drive)) count++;
1339 if ((count * 4) + 1 > len) return count * 4 + 1;
1341 for (drive = 0; drive < 26; drive++)
1343 if (drives & (1 << drive))
1345 *buffer++ = 'A' + drive;
1356 /***********************************************************************
1357 * GetDriveTypeW (KERNEL32.@)
1359 * Returns the type of the disk drive specified. If root is NULL the
1360 * root of the current directory is used.
1364 * Type of drive (from Win32 SDK):
1366 * DRIVE_UNKNOWN unable to find out anything about the drive
1367 * DRIVE_NO_ROOT_DIR nonexistent root dir
1368 * DRIVE_REMOVABLE the disk can be removed from the machine
1369 * DRIVE_FIXED the disk cannot be removed from the machine
1370 * DRIVE_REMOTE network disk
1371 * DRIVE_CDROM CDROM drive
1372 * DRIVE_RAMDISK virtual disk in RAM
1374 UINT WINAPI GetDriveTypeW(LPCWSTR root) /* [in] String describing drive */
1376 FILE_FS_DEVICE_INFORMATION info;
1382 if (!open_device_root( root, &handle )) return DRIVE_NO_ROOT_DIR;
1384 status = NtQueryVolumeInformationFile( handle, &io, &info, sizeof(info), FileFsDeviceInformation );
1386 if (status != STATUS_SUCCESS)
1388 SetLastError( RtlNtStatusToDosError(status) );
1389 ret = DRIVE_UNKNOWN;
1393 switch (info.DeviceType)
1395 case FILE_DEVICE_CD_ROM_FILE_SYSTEM: ret = DRIVE_CDROM; break;
1396 case FILE_DEVICE_VIRTUAL_DISK: ret = DRIVE_RAMDISK; break;
1397 case FILE_DEVICE_NETWORK_FILE_SYSTEM: ret = DRIVE_REMOTE; break;
1398 case FILE_DEVICE_DISK_FILE_SYSTEM:
1399 if (info.Characteristics & FILE_REMOTE_DEVICE) ret = DRIVE_REMOTE;
1400 else if (info.Characteristics & FILE_REMOVABLE_MEDIA) ret = DRIVE_REMOVABLE;
1401 else if ((ret = get_mountmgr_drive_type( root )) == DRIVE_UNKNOWN) ret = DRIVE_FIXED;
1404 ret = DRIVE_UNKNOWN;
1408 TRACE( "%s -> %d\n", debugstr_w(root), ret );
1413 /***********************************************************************
1414 * GetDriveTypeA (KERNEL32.@)
1416 * See GetDriveTypeW.
1418 UINT WINAPI GetDriveTypeA( LPCSTR root )
1420 WCHAR *rootW = NULL;
1422 if (root && !(rootW = FILE_name_AtoW( root, FALSE ))) return DRIVE_NO_ROOT_DIR;
1423 return GetDriveTypeW( rootW );
1427 /***********************************************************************
1428 * GetDiskFreeSpaceExW (KERNEL32.@)
1430 * This function is used to acquire the size of the available and
1431 * total space on a logical volume.
1435 * Zero on failure, nonzero upon success. Use GetLastError to obtain
1436 * detailed error information.
1439 BOOL WINAPI GetDiskFreeSpaceExW( LPCWSTR root, PULARGE_INTEGER avail,
1440 PULARGE_INTEGER total, PULARGE_INTEGER totalfree )
1442 FILE_FS_SIZE_INFORMATION info;
1448 TRACE( "%s,%p,%p,%p\n", debugstr_w(root), avail, total, totalfree );
1450 if (!open_device_root( root, &handle )) return FALSE;
1452 status = NtQueryVolumeInformationFile( handle, &io, &info, sizeof(info), FileFsSizeInformation );
1454 if (status != STATUS_SUCCESS)
1456 SetLastError( RtlNtStatusToDosError(status) );
1460 units = info.SectorsPerAllocationUnit * info.BytesPerSector;
1461 if (total) total->QuadPart = info.TotalAllocationUnits.QuadPart * units;
1462 if (totalfree) totalfree->QuadPart = info.AvailableAllocationUnits.QuadPart * units;
1463 /* FIXME: this one should take quotas into account */
1464 if (avail) avail->QuadPart = info.AvailableAllocationUnits.QuadPart * units;
1469 /***********************************************************************
1470 * GetDiskFreeSpaceExA (KERNEL32.@)
1472 * See GetDiskFreeSpaceExW.
1474 BOOL WINAPI GetDiskFreeSpaceExA( LPCSTR root, PULARGE_INTEGER avail,
1475 PULARGE_INTEGER total, PULARGE_INTEGER totalfree )
1477 WCHAR *rootW = NULL;
1479 if (root && !(rootW = FILE_name_AtoW( root, FALSE ))) return FALSE;
1480 return GetDiskFreeSpaceExW( rootW, avail, total, totalfree );
1484 /***********************************************************************
1485 * GetDiskFreeSpaceW (KERNEL32.@)
1487 BOOL WINAPI GetDiskFreeSpaceW( LPCWSTR root, LPDWORD cluster_sectors,
1488 LPDWORD sector_bytes, LPDWORD free_clusters,
1489 LPDWORD total_clusters )
1491 FILE_FS_SIZE_INFORMATION info;
1497 TRACE( "%s,%p,%p,%p,%p\n", debugstr_w(root),
1498 cluster_sectors, sector_bytes, free_clusters, total_clusters );
1500 if (!open_device_root( root, &handle )) return FALSE;
1502 status = NtQueryVolumeInformationFile( handle, &io, &info, sizeof(info), FileFsSizeInformation );
1504 if (status != STATUS_SUCCESS)
1506 SetLastError( RtlNtStatusToDosError(status) );
1510 units = info.SectorsPerAllocationUnit * info.BytesPerSector;
1512 if( GetVersion() & 0x80000000) { /* win3.x, 9x, ME */
1513 /* cap the size and available at 2GB as per specs */
1514 if (info.TotalAllocationUnits.QuadPart * units > 0x7fffffff) {
1515 info.TotalAllocationUnits.QuadPart = 0x7fffffff / units;
1516 if (info.AvailableAllocationUnits.QuadPart * units > 0x7fffffff)
1517 info.AvailableAllocationUnits.QuadPart = 0x7fffffff / units;
1519 /* nr. of clusters is always <= 65335 */
1520 while( info.TotalAllocationUnits.QuadPart > 65535 ) {
1521 info.TotalAllocationUnits.QuadPart /= 2;
1522 info.AvailableAllocationUnits.QuadPart /= 2;
1523 info.SectorsPerAllocationUnit *= 2;
1527 if (cluster_sectors) *cluster_sectors = info.SectorsPerAllocationUnit;
1528 if (sector_bytes) *sector_bytes = info.BytesPerSector;
1529 if (free_clusters) *free_clusters = info.AvailableAllocationUnits.u.LowPart;
1530 if (total_clusters) *total_clusters = info.TotalAllocationUnits.u.LowPart;
1535 /***********************************************************************
1536 * GetDiskFreeSpaceA (KERNEL32.@)
1538 BOOL WINAPI GetDiskFreeSpaceA( LPCSTR root, LPDWORD cluster_sectors,
1539 LPDWORD sector_bytes, LPDWORD free_clusters,
1540 LPDWORD total_clusters )
1542 WCHAR *rootW = NULL;
1544 if (root && !(rootW = FILE_name_AtoW( root, FALSE ))) return FALSE;
1545 return GetDiskFreeSpaceW( rootW, cluster_sectors, sector_bytes, free_clusters, total_clusters );
1548 /***********************************************************************
1549 * GetVolumePathNameA (KERNEL32.@)
1551 BOOL WINAPI GetVolumePathNameA(LPCSTR filename, LPSTR volumepathname, DWORD buflen)
1554 WCHAR *filenameW = NULL, *volumeW;
1556 FIXME("(%s, %p, %d), stub!\n", debugstr_a(filename), volumepathname, buflen);
1558 if (filename && !(filenameW = FILE_name_AtoW( filename, FALSE ))) return FALSE;
1559 if (!(volumeW = HeapAlloc( GetProcessHeap(), 0, buflen * sizeof(WCHAR) ))) return FALSE;
1561 if ((ret = GetVolumePathNameW( filenameW, volumeW, buflen )))
1562 FILE_name_WtoA( volumeW, -1, volumepathname, buflen );
1564 HeapFree( GetProcessHeap(), 0, volumeW );
1568 /***********************************************************************
1569 * GetVolumePathNameW (KERNEL32.@)
1571 BOOL WINAPI GetVolumePathNameW(LPCWSTR filename, LPWSTR volumepathname, DWORD buflen)
1573 const WCHAR *p = filename;
1575 FIXME("(%s, %p, %d), stub!\n", debugstr_w(filename), volumepathname, buflen);
1577 if (p && tolowerW(p[0]) >= 'a' && tolowerW(p[0]) <= 'z' && p[1] ==':' && p[2] == '\\' && buflen >= 4)
1579 volumepathname[0] = p[0];
1580 volumepathname[1] = ':';
1581 volumepathname[2] = '\\';
1582 volumepathname[3] = 0;
1588 /***********************************************************************
1589 * GetVolumePathNamesForVolumeNameW (KERNEL32.@)
1591 BOOL WINAPI GetVolumePathNamesForVolumeNameW(LPCWSTR volumename, LPWSTR volumepathname, DWORD buflen, PDWORD returnlen)
1593 FIXME("(%s, %p, %d, %p), stub!\n", debugstr_w(volumename), volumepathname, buflen, returnlen);
1594 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1598 /***********************************************************************
1599 * FindFirstVolumeA (KERNEL32.@)
1601 HANDLE WINAPI FindFirstVolumeA(LPSTR volume, DWORD len)
1603 WCHAR *buffer = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1604 HANDLE handle = FindFirstVolumeW( buffer, len );
1606 if (handle != INVALID_HANDLE_VALUE)
1608 if (!WideCharToMultiByte( CP_ACP, 0, buffer, -1, volume, len, NULL, NULL ))
1610 FindVolumeClose( handle );
1611 handle = INVALID_HANDLE_VALUE;
1614 HeapFree( GetProcessHeap(), 0, buffer );
1618 /***********************************************************************
1619 * FindFirstVolumeW (KERNEL32.@)
1621 HANDLE WINAPI FindFirstVolumeW( LPWSTR volume, DWORD len )
1624 HANDLE mgr = CreateFileW( MOUNTMGR_DOS_DEVICE_NAME, 0, FILE_SHARE_READ|FILE_SHARE_WRITE,
1625 NULL, OPEN_EXISTING, 0, 0 );
1626 if (mgr == INVALID_HANDLE_VALUE) return INVALID_HANDLE_VALUE;
1630 MOUNTMGR_MOUNT_POINT input;
1631 MOUNTMGR_MOUNT_POINTS *output;
1633 if (!(output = HeapAlloc( GetProcessHeap(), 0, size )))
1635 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1638 memset( &input, 0, sizeof(input) );
1640 if (!DeviceIoControl( mgr, IOCTL_MOUNTMGR_QUERY_POINTS, &input, sizeof(input),
1641 output, size, NULL, NULL ))
1643 if (GetLastError() != ERROR_MORE_DATA) break;
1644 size = output->Size;
1645 HeapFree( GetProcessHeap(), 0, output );
1649 /* abuse the Size field to store the current index */
1651 if (!FindNextVolumeW( output, volume, len ))
1653 HeapFree( GetProcessHeap(), 0, output );
1654 return INVALID_HANDLE_VALUE;
1659 return INVALID_HANDLE_VALUE;
1662 /***********************************************************************
1663 * FindNextVolumeA (KERNEL32.@)
1665 BOOL WINAPI FindNextVolumeA( HANDLE handle, LPSTR volume, DWORD len )
1667 WCHAR *buffer = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1670 if ((ret = FindNextVolumeW( handle, buffer, len )))
1672 if (!WideCharToMultiByte( CP_ACP, 0, buffer, -1, volume, len, NULL, NULL )) ret = FALSE;
1674 HeapFree( GetProcessHeap(), 0, buffer );
1678 /***********************************************************************
1679 * FindNextVolumeW (KERNEL32.@)
1681 BOOL WINAPI FindNextVolumeW( HANDLE handle, LPWSTR volume, DWORD len )
1683 MOUNTMGR_MOUNT_POINTS *data = handle;
1685 while (data->Size < data->NumberOfMountPoints)
1687 static const WCHAR volumeW[] = {'\\','?','?','\\','V','o','l','u','m','e','{',};
1688 WCHAR *link = (WCHAR *)((char *)data + data->MountPoints[data->Size].SymbolicLinkNameOffset);
1689 DWORD size = data->MountPoints[data->Size].SymbolicLinkNameLength;
1691 /* skip non-volumes */
1692 if (size < sizeof(volumeW) || memcmp( link, volumeW, sizeof(volumeW) )) continue;
1693 if (size + sizeof(WCHAR) >= len * sizeof(WCHAR))
1695 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1698 memcpy( volume, link, size );
1699 volume[1] = '\\'; /* map \??\ to \\?\ */
1700 volume[size / sizeof(WCHAR)] = '\\'; /* Windows appends a backslash */
1701 volume[size / sizeof(WCHAR) + 1] = 0;
1702 TRACE( "returning entry %u %s\n", data->Size - 1, debugstr_w(volume) );
1705 SetLastError( ERROR_NO_MORE_FILES );
1709 /***********************************************************************
1710 * FindVolumeClose (KERNEL32.@)
1712 BOOL WINAPI FindVolumeClose(HANDLE handle)
1714 return HeapFree( GetProcessHeap(), 0, handle );
1717 /***********************************************************************
1718 * FindFirstVolumeMountPointA (KERNEL32.@)
1720 HANDLE WINAPI FindFirstVolumeMountPointA(LPCSTR root, LPSTR mount_point, DWORD len)
1722 FIXME("(%s, %p, %d), stub!\n", debugstr_a(root), mount_point, len);
1723 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1724 return INVALID_HANDLE_VALUE;
1727 /***********************************************************************
1728 * FindFirstVolumeMountPointW (KERNEL32.@)
1730 HANDLE WINAPI FindFirstVolumeMountPointW(LPCWSTR root, LPWSTR mount_point, DWORD len)
1732 FIXME("(%s, %p, %d), stub!\n", debugstr_w(root), mount_point, len);
1733 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1734 return INVALID_HANDLE_VALUE;
1737 /***********************************************************************
1738 * FindVolumeMountPointClose (KERNEL32.@)
1740 BOOL WINAPI FindVolumeMountPointClose(HANDLE h)
1742 FIXME("(%p), stub!\n", h);