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 UNICODE_STRING *device, WCHAR *label, DWORD len )
214 static const WCHAR labelW[] = {'.','w','i','n','d','o','w','s','-','l','a','b','e','l',0};
218 OBJECT_ATTRIBUTES attr;
222 attr.Length = sizeof(attr);
223 attr.RootDirectory = 0;
224 attr.Attributes = OBJ_CASE_INSENSITIVE;
225 attr.ObjectName = &name;
226 attr.SecurityDescriptor = NULL;
227 attr.SecurityQualityOfService = NULL;
229 name.MaximumLength = device->Length + sizeof(labelW);
230 name.Length = name.MaximumLength - sizeof(WCHAR);
231 if (!(name.Buffer = HeapAlloc( GetProcessHeap(), 0, name.MaximumLength ))) return;
233 memcpy( name.Buffer, device->Buffer, device->Length );
234 memcpy( name.Buffer + device->Length / sizeof(WCHAR), labelW, sizeof(labelW) );
235 if (!NtOpenFile( &handle, GENERIC_READ, &attr, &io, FILE_SHARE_READ|FILE_SHARE_WRITE,
236 FILE_NON_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT ))
238 char buffer[256], *p;
241 if (!ReadFile( handle, buffer, sizeof(buffer)-1, &size, NULL )) size = 0;
242 CloseHandle( handle );
244 while (p > buffer && (p[-1] == ' ' || p[-1] == '\r' || p[-1] == '\n')) p--;
246 if (!MultiByteToWideChar( CP_UNIXCP, 0, buffer, -1, label, len ))
249 RtlFreeUnicodeString( &name );
252 /* get the serial number by reading it from a file at the root of the filesystem */
253 static DWORD get_filesystem_serial( const UNICODE_STRING *device )
255 static const WCHAR serialW[] = {'.','w','i','n','d','o','w','s','-','s','e','r','i','a','l',0};
259 OBJECT_ATTRIBUTES attr;
262 attr.Length = sizeof(attr);
263 attr.RootDirectory = 0;
264 attr.Attributes = OBJ_CASE_INSENSITIVE;
265 attr.ObjectName = &name;
266 attr.SecurityDescriptor = NULL;
267 attr.SecurityQualityOfService = NULL;
269 name.MaximumLength = device->Length + sizeof(serialW);
270 name.Length = name.MaximumLength - sizeof(WCHAR);
271 if (!(name.Buffer = HeapAlloc( GetProcessHeap(), 0, name.MaximumLength ))) return 0;
273 memcpy( name.Buffer, device->Buffer, device->Length );
274 memcpy( name.Buffer + device->Length / sizeof(WCHAR), serialW, sizeof(serialW) );
275 if (!NtOpenFile( &handle, GENERIC_READ, &attr, &io, FILE_SHARE_READ|FILE_SHARE_WRITE,
276 FILE_SYNCHRONOUS_IO_NONALERT ))
281 if (!ReadFile( handle, buffer, sizeof(buffer)-1, &size, NULL )) size = 0;
282 CloseHandle( handle );
284 ret = strtoul( buffer, NULL, 16 );
286 RtlFreeUnicodeString( &name );
291 /******************************************************************
292 * VOLUME_FindCdRomDataBestVoldesc
294 static DWORD VOLUME_FindCdRomDataBestVoldesc( HANDLE handle )
296 BYTE cur_vd_type, max_vd_type = 0;
298 DWORD size, offs, best_offs = 0, extra_offs = 0;
300 for (offs = 0x8000; offs <= 0x9800; offs += 0x800)
302 /* if 'CDROM' occurs at position 8, this is a pre-iso9660 cd, and
303 * the volume label is displaced forward by 8
305 if (SetFilePointer( handle, offs, NULL, FILE_BEGIN ) != offs) break;
306 if (!ReadFile( handle, buffer, sizeof(buffer), &size, NULL )) break;
307 if (size != sizeof(buffer)) break;
308 /* check for non-ISO9660 signature */
309 if (!memcmp( buffer + 11, "ROM", 3 )) extra_offs = 8;
310 cur_vd_type = buffer[extra_offs];
311 if (cur_vd_type == 0xff) /* voldesc set terminator */
313 if (cur_vd_type > max_vd_type)
315 max_vd_type = cur_vd_type;
316 best_offs = offs + extra_offs;
323 /***********************************************************************
324 * VOLUME_ReadFATSuperblock
326 static enum fs_type VOLUME_ReadFATSuperblock( HANDLE handle, BYTE *buff )
330 /* try a fixed disk, with a FAT partition */
331 if (SetFilePointer( handle, 0, NULL, FILE_BEGIN ) != 0 ||
332 !ReadFile( handle, buff, SUPERBLOCK_SIZE, &size, NULL ))
334 if (GetLastError() == ERROR_BAD_DEV_TYPE) return FS_UNKNOWN; /* not a real device */
338 if (size < SUPERBLOCK_SIZE) return FS_UNKNOWN;
340 /* FIXME: do really all FAT have their name beginning with
341 * "FAT" ? (At least FAT12, FAT16 and FAT32 have :)
343 if (!memcmp(buff+0x36, "FAT", 3) || !memcmp(buff+0x52, "FAT", 3))
345 /* guess which type of FAT we have */
347 unsigned int sectors,
356 sect_per_fat = GETWORD(buff, 0x16);
357 if (!sect_per_fat) sect_per_fat = GETLONG(buff, 0x24);
358 total_sectors = GETWORD(buff, 0x13);
360 total_sectors = GETLONG(buff, 0x20);
361 num_boot_sectors = GETWORD(buff, 0x0e);
362 num_fats = buff[0x10];
363 num_root_dir_ents = GETWORD(buff, 0x11);
364 bytes_per_sector = GETWORD(buff, 0x0b);
365 sectors_per_cluster = buff[0x0d];
366 /* check if the parameters are reasonable and will not cause
367 * arithmetic errors in the calculation */
368 reasonable = num_boot_sectors < total_sectors &&
370 bytes_per_sector >= 512 && bytes_per_sector % 512 == 0 &&
371 sectors_per_cluster >= 1;
372 if (!reasonable) return FS_UNKNOWN;
373 sectors = total_sectors - num_boot_sectors - num_fats * sect_per_fat -
374 (num_root_dir_ents * 32 + bytes_per_sector - 1) / bytes_per_sector;
375 nclust = sectors / sectors_per_cluster;
376 if ((buff[0x42] == 0x28 || buff[0x42] == 0x29) &&
377 !memcmp(buff+0x52, "FAT", 3)) return FS_FAT32;
380 if ((buff[0x26] == 0x28 || buff[0x26] == 0x29) &&
381 !memcmp(buff+0x36, "FAT", 3))
389 /***********************************************************************
390 * VOLUME_ReadCDSuperblock
392 static enum fs_type VOLUME_ReadCDSuperblock( HANDLE handle, BYTE *buff )
394 DWORD size, offs = VOLUME_FindCdRomDataBestVoldesc( handle );
396 if (!offs) return FS_UNKNOWN;
398 if (SetFilePointer( handle, offs, NULL, FILE_BEGIN ) != offs ||
399 !ReadFile( handle, buff, SUPERBLOCK_SIZE, &size, NULL ) ||
400 size != SUPERBLOCK_SIZE)
403 /* check for iso9660 present */
404 if (!memcmp(&buff[1], "CD001", 5)) return FS_ISO9660;
409 /**************************************************************************
410 * VOLUME_GetSuperblockLabel
412 static void VOLUME_GetSuperblockLabel( const UNICODE_STRING *device, enum fs_type type,
413 const BYTE *superblock, WCHAR *label, DWORD len )
415 const BYTE *label_ptr = NULL;
424 get_filesystem_label( device, label, len );
427 label_ptr = superblock + 0x2b;
431 label_ptr = superblock + 0x47;
436 BYTE ver = superblock[0x5a];
438 if (superblock[0x58] == 0x25 && superblock[0x59] == 0x2f && /* Unicode ID */
439 ((ver == 0x40) || (ver == 0x43) || (ver == 0x45)))
440 { /* yippee, unicode */
443 if (len > 17) len = 17;
444 for (i = 0; i < len-1; i++)
445 label[i] = (superblock[40+2*i] << 8) | superblock[41+2*i];
447 while (i && label[i-1] == ' ') label[--i] = 0;
450 label_ptr = superblock + 40;
455 if (label_len) RtlMultiByteToUnicodeN( label, (len-1) * sizeof(WCHAR),
456 &label_len, (LPCSTR)label_ptr, label_len );
457 label_len /= sizeof(WCHAR);
458 label[label_len] = 0;
459 while (label_len && label[label_len-1] == ' ') label[--label_len] = 0;
463 /**************************************************************************
464 * VOLUME_GetSuperblockSerial
466 static DWORD VOLUME_GetSuperblockSerial( const UNICODE_STRING *device, enum fs_type type,
467 const BYTE *superblock )
474 return get_filesystem_serial( device );
476 return GETLONG( superblock, 0x27 );
478 return GETLONG( superblock, 0x33 );
484 sum[0] = sum[1] = sum[2] = sum[3] = 0;
485 for (i = 0; i < 2048; i += 4)
487 /* DON'T optimize this into DWORD !! (breaks overflow) */
488 sum[0] += superblock[i+0];
489 sum[1] += superblock[i+1];
490 sum[2] += superblock[i+2];
491 sum[3] += superblock[i+3];
494 * OK, another braindead one... argh. Just believe it.
495 * Me$$ysoft chose to reverse the serial number in NT4/W2K.
496 * It's true and nobody will ever be able to change it.
498 if (GetVersion() & 0x80000000)
499 return (sum[3] << 24) | (sum[2] << 16) | (sum[1] << 8) | sum[0];
501 return (sum[0] << 24) | (sum[1] << 16) | (sum[2] << 8) | sum[3];
508 /**************************************************************************
509 * VOLUME_GetAudioCDSerial
511 static DWORD VOLUME_GetAudioCDSerial( const CDROM_TOC *toc )
516 for (i = 0; i <= toc->LastTrack - toc->FirstTrack; i++)
517 serial += ((toc->TrackData[i].Address[1] << 16) |
518 (toc->TrackData[i].Address[2] << 8) |
519 toc->TrackData[i].Address[3]);
522 * dwStart, dwEnd collect the beginning and end of the disc respectively, in
524 * There it is collected for correcting the serial when there are less than
527 if (toc->LastTrack - toc->FirstTrack + 1 < 3)
529 DWORD dwStart = FRAME_OF_TOC(toc, toc->FirstTrack);
530 DWORD dwEnd = FRAME_OF_TOC(toc, toc->LastTrack + 1);
531 serial += dwEnd - dwStart;
537 /***********************************************************************
538 * GetVolumeInformationW (KERNEL32.@)
540 BOOL WINAPI GetVolumeInformationW( LPCWSTR root, LPWSTR label, DWORD label_len,
541 DWORD *serial, DWORD *filename_len, DWORD *flags,
542 LPWSTR fsname, DWORD fsname_len )
544 static const WCHAR audiocdW[] = {'A','u','d','i','o',' ','C','D',0};
545 static const WCHAR fatW[] = {'F','A','T',0};
546 static const WCHAR fat32W[] = {'F','A','T','3','2',0};
547 static const WCHAR ntfsW[] = {'N','T','F','S',0};
548 static const WCHAR cdfsW[] = {'C','D','F','S',0};
549 static const WCHAR default_rootW[] = {'\\',0};
553 UNICODE_STRING nt_name;
555 OBJECT_ATTRIBUTES attr;
556 FILE_FS_DEVICE_INFORMATION info;
558 enum fs_type type = FS_UNKNOWN;
561 if (!root) root = default_rootW;
562 if (!RtlDosPathNameToNtPathName_U( root, &nt_name, NULL, NULL ))
564 SetLastError( ERROR_PATH_NOT_FOUND );
567 /* there must be exactly one backslash in the name, at the end */
568 p = memchrW( nt_name.Buffer + 4, '\\', (nt_name.Length - 4) / sizeof(WCHAR) );
569 if (p != nt_name.Buffer + nt_name.Length / sizeof(WCHAR) - 1)
571 /* check if root contains an explicit subdir */
572 if (root[0] && root[1] == ':') root += 2;
573 while (*root == '\\') root++;
574 if (strchrW( root, '\\' ))
575 SetLastError( ERROR_DIR_NOT_ROOT );
577 SetLastError( ERROR_INVALID_NAME );
581 /* try to open the device */
583 attr.Length = sizeof(attr);
584 attr.RootDirectory = 0;
585 attr.Attributes = OBJ_CASE_INSENSITIVE;
586 attr.ObjectName = &nt_name;
587 attr.SecurityDescriptor = NULL;
588 attr.SecurityQualityOfService = NULL;
590 nt_name.Length -= sizeof(WCHAR); /* without trailing slash */
591 status = NtOpenFile( &handle, GENERIC_READ, &attr, &io, FILE_SHARE_READ | FILE_SHARE_WRITE,
592 FILE_NON_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT );
593 nt_name.Length += sizeof(WCHAR);
595 if (status == STATUS_SUCCESS)
597 BYTE superblock[SUPERBLOCK_SIZE];
601 /* check for audio CD */
602 /* FIXME: we only check the first track for now */
603 if (DeviceIoControl( handle, IOCTL_CDROM_READ_TOC, NULL, 0, &toc, sizeof(toc), &br, 0 ))
605 if (!(toc.TrackData[0].Control & 0x04)) /* audio track */
607 TRACE( "%s: found audio CD\n", debugstr_w(nt_name.Buffer) );
608 if (label) lstrcpynW( label, audiocdW, label_len );
609 if (serial) *serial = VOLUME_GetAudioCDSerial( &toc );
610 CloseHandle( handle );
614 type = VOLUME_ReadCDSuperblock( handle, superblock );
618 type = VOLUME_ReadFATSuperblock( handle, superblock );
619 if (type == FS_UNKNOWN) type = VOLUME_ReadCDSuperblock( handle, superblock );
621 CloseHandle( handle );
622 TRACE( "%s: found fs type %d\n", debugstr_w(nt_name.Buffer), type );
623 if (type == FS_ERROR) goto done;
625 if (label && label_len) VOLUME_GetSuperblockLabel( &nt_name, type, superblock, label, label_len );
626 if (serial) *serial = VOLUME_GetSuperblockSerial( &nt_name, type, superblock );
629 else TRACE( "cannot open device %s: %x\n", debugstr_w(nt_name.Buffer), status );
631 /* we couldn't open the device, fallback to default strategy */
633 status = NtOpenFile( &handle, 0, &attr, &io, 0, FILE_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT );
634 if (status != STATUS_SUCCESS)
636 SetLastError( RtlNtStatusToDosError(status) );
639 status = NtQueryVolumeInformationFile( handle, &io, &info, sizeof(info), FileFsDeviceInformation );
641 if (status != STATUS_SUCCESS)
643 SetLastError( RtlNtStatusToDosError(status) );
646 if (info.DeviceType == FILE_DEVICE_CD_ROM_FILE_SYSTEM) type = FS_ISO9660;
648 if (label && label_len) get_filesystem_label( &nt_name, label, label_len );
649 if (serial) *serial = get_filesystem_serial( &nt_name );
651 fill_fs_info: /* now fill in the information that depends on the file system type */
656 if (fsname) lstrcpynW( fsname, cdfsW, fsname_len );
657 if (filename_len) *filename_len = 221;
658 if (flags) *flags = FILE_READ_ONLY_VOLUME;
661 if (fsname) lstrcpynW( fsname, fatW, fsname_len );
663 if (type == FS_FAT32 && fsname) lstrcpynW( fsname, fat32W, fsname_len );
664 if (filename_len) *filename_len = 255;
665 if (flags) *flags = FILE_CASE_PRESERVED_NAMES; /* FIXME */
668 if (fsname) lstrcpynW( fsname, ntfsW, fsname_len );
669 if (filename_len) *filename_len = 255;
670 if (flags) *flags = FILE_CASE_PRESERVED_NAMES;
676 RtlFreeUnicodeString( &nt_name );
681 /***********************************************************************
682 * GetVolumeInformationA (KERNEL32.@)
684 BOOL WINAPI GetVolumeInformationA( LPCSTR root, LPSTR label,
685 DWORD label_len, DWORD *serial,
686 DWORD *filename_len, DWORD *flags,
687 LPSTR fsname, DWORD fsname_len )
690 LPWSTR labelW, fsnameW;
693 if (root && !(rootW = FILE_name_AtoW( root, FALSE ))) return FALSE;
695 labelW = label ? HeapAlloc(GetProcessHeap(), 0, label_len * sizeof(WCHAR)) : NULL;
696 fsnameW = fsname ? HeapAlloc(GetProcessHeap(), 0, fsname_len * sizeof(WCHAR)) : NULL;
698 if ((ret = GetVolumeInformationW(rootW, labelW, label_len, serial,
699 filename_len, flags, fsnameW, fsname_len)))
701 if (label) FILE_name_WtoA( labelW, -1, label, label_len );
702 if (fsname) FILE_name_WtoA( fsnameW, -1, fsname, fsname_len );
705 HeapFree( GetProcessHeap(), 0, labelW );
706 HeapFree( GetProcessHeap(), 0, fsnameW );
712 /***********************************************************************
713 * SetVolumeLabelW (KERNEL32.@)
715 BOOL WINAPI SetVolumeLabelW( LPCWSTR root, LPCWSTR label )
717 WCHAR device[] = {'\\','\\','.','\\','A',':',0};
719 enum fs_type type = FS_UNKNOWN;
723 WCHAR path[MAX_PATH];
724 GetCurrentDirectoryW( MAX_PATH, path );
729 if (!root[0] || root[1] != ':')
731 SetLastError( ERROR_INVALID_NAME );
737 /* try to open the device */
739 handle = CreateFileW( device, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE,
740 NULL, OPEN_EXISTING, 0, 0 );
741 if (handle != INVALID_HANDLE_VALUE)
743 BYTE superblock[SUPERBLOCK_SIZE];
745 type = VOLUME_ReadFATSuperblock( handle, superblock );
746 if (type == FS_UNKNOWN) type = VOLUME_ReadCDSuperblock( handle, superblock );
747 CloseHandle( handle );
748 if (type != FS_UNKNOWN)
750 /* we can't set the label on FAT or CDROM file systems */
751 TRACE( "cannot set label on device %s type %d\n", debugstr_w(device), type );
752 SetLastError( ERROR_ACCESS_DENIED );
758 TRACE( "cannot open device %s: err %d\n", debugstr_w(device), GetLastError() );
759 if (GetLastError() == ERROR_ACCESS_DENIED) return FALSE;
762 /* we couldn't open the device, fallback to default strategy */
764 switch(GetDriveTypeW( root ))
767 case DRIVE_NO_ROOT_DIR:
768 SetLastError( ERROR_NOT_READY );
770 case DRIVE_REMOVABLE:
773 WCHAR labelW[] = {'A',':','\\','.','w','i','n','d','o','w','s','-','l','a','b','e','l',0};
775 labelW[0] = device[4];
777 if (!label[0]) /* delete label file when setting an empty label */
778 return DeleteFileW( labelW ) || GetLastError() == ERROR_FILE_NOT_FOUND;
780 handle = CreateFileW( labelW, GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
781 CREATE_ALWAYS, 0, 0 );
782 if (handle != INVALID_HANDLE_VALUE)
787 if (!WideCharToMultiByte( CP_UNIXCP, 0, label, -1, buffer, sizeof(buffer)-1, NULL, NULL ))
788 buffer[sizeof(buffer)-2] = 0;
789 strcat( buffer, "\n" );
790 WriteFile( handle, buffer, strlen(buffer), &size, NULL );
791 CloseHandle( handle );
799 SetLastError( ERROR_ACCESS_DENIED );
805 /***********************************************************************
806 * SetVolumeLabelA (KERNEL32.@)
808 BOOL WINAPI SetVolumeLabelA(LPCSTR root, LPCSTR volname)
810 WCHAR *rootW = NULL, *volnameW = NULL;
813 if (root && !(rootW = FILE_name_AtoW( root, FALSE ))) return FALSE;
814 if (volname && !(volnameW = FILE_name_AtoW( volname, TRUE ))) return FALSE;
815 ret = SetVolumeLabelW( rootW, volnameW );
816 HeapFree( GetProcessHeap(), 0, volnameW );
821 /***********************************************************************
822 * GetVolumeNameForVolumeMountPointA (KERNEL32.@)
824 BOOL WINAPI GetVolumeNameForVolumeMountPointA( LPCSTR path, LPSTR volume, DWORD size )
827 WCHAR volumeW[50], *pathW = NULL;
828 DWORD len = min( sizeof(volumeW) / sizeof(WCHAR), size );
830 TRACE("(%s, %p, %x)\n", debugstr_a(path), volume, size);
832 if (!path || !(pathW = FILE_name_AtoW( path, TRUE )))
835 if ((ret = GetVolumeNameForVolumeMountPointW( pathW, volumeW, len )))
836 FILE_name_WtoA( volumeW, -1, volume, len );
838 HeapFree( GetProcessHeap(), 0, pathW );
842 /***********************************************************************
843 * GetVolumeNameForVolumeMountPointW (KERNEL32.@)
845 BOOL WINAPI GetVolumeNameForVolumeMountPointW( LPCWSTR path, LPWSTR volume, DWORD size )
847 static const WCHAR prefixW[] = {'\\','D','o','s','D','e','v','i','c','e','s','\\',0};
848 static const WCHAR volumeW[] = {'\\','?','?','\\','V','o','l','u','m','e','{',0};
849 static const WCHAR trailingW[] = {'\\',0};
851 MOUNTMGR_MOUNT_POINT *input = NULL, *o1;
852 MOUNTMGR_MOUNT_POINTS *output = NULL;
855 DWORD i, i_size = 1024, o_size = 1024;
856 WCHAR *nonpersist_name;
857 WCHAR symlink_name[MAX_PATH];
859 HANDLE mgr = INVALID_HANDLE_VALUE;
862 TRACE("(%s, %p, %x)\n", debugstr_w(path), volume, size);
863 if (path[lstrlenW(path)-1] != '\\')
865 SetLastError( ERROR_INVALID_NAME );
871 SetLastError( ERROR_FILENAME_EXCED_RANGE );
874 /* if length of input is > 3 then it must be a mounted folder */
875 if (lstrlenW(path) > 3)
877 FIXME("Mounted Folders are not yet supported\n");
878 SetLastError( ERROR_NOT_A_REPARSE_POINT );
882 mgr = CreateFileW( MOUNTMGR_DOS_DEVICE_NAME, 0, FILE_SHARE_READ,
883 NULL, OPEN_EXISTING, 0, 0 );
884 if (mgr == INVALID_HANDLE_VALUE) return FALSE;
886 if (!(input = HeapAlloc( GetProcessHeap(), 0, i_size )))
888 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
892 if (!(output = HeapAlloc( GetProcessHeap(), 0, o_size )))
894 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
898 /* construct the symlink name as "\DosDevices\C:" */
899 lstrcpyW( symlink_name, prefixW );
900 lstrcatW( symlink_name, path );
901 symlink_name[lstrlenW(symlink_name)-1] = 0;
903 /* Take the mount point and get the "nonpersistent name" */
904 /* We will then take that and get the volume name */
905 nonpersist_name = (WCHAR *)(input + 1);
906 status = read_nt_symlink( symlink_name, nonpersist_name, i_size - sizeof(*input) );
907 TRACE("read_nt_symlink got stat=%x, for %s, got <%s>\n", status,
908 debugstr_w(symlink_name), debugstr_w(nonpersist_name));
909 if (status != STATUS_SUCCESS)
911 SetLastError( ERROR_FILE_NOT_FOUND );
915 /* Now take the "nonpersistent name" and ask the mountmgr */
916 /* to give us all the mount points. One of them will be */
917 /* the volume name (format of \??\Volume{). */
918 memset( input, 0, sizeof(*input) ); /* clear all input parameters */
919 input->DeviceNameOffset = sizeof(*input);
920 input->DeviceNameLength = lstrlenW( nonpersist_name) * sizeof(WCHAR);
921 i_size = input->DeviceNameOffset + input->DeviceNameLength;
923 output->Size = o_size;
925 /* now get the true volume name from the mountmgr */
926 if (!DeviceIoControl( mgr, IOCTL_MOUNTMGR_QUERY_POINTS, input, i_size,
927 output, o_size, NULL, NULL ))
930 /* Verify and return the data, note string is not null terminated */
931 TRACE("found %d matching mount points\n", output->NumberOfMountPoints);
932 if (output->NumberOfMountPoints < 1)
934 SetLastError( ERROR_NO_VOLUME_ID );
937 o1 = &output->MountPoints[0];
939 /* look for the volume name in returned values */
940 for(i=0;i<output->NumberOfMountPoints;i++)
942 p = (WCHAR*)((char *)output + o1->SymbolicLinkNameOffset);
943 r = (char *)output + o1->UniqueIdOffset;
944 TRACE("found symlink=%s, unique=%s, devname=%s\n",
945 debugstr_wn(p, o1->SymbolicLinkNameLength/sizeof(WCHAR)),
946 debugstr_an(r, o1->UniqueIdLength),
947 debugstr_wn((WCHAR*)((char *)output + o1->DeviceNameOffset),
948 o1->DeviceNameLength/sizeof(WCHAR)));
950 if (!strncmpW( p, volumeW, (sizeof(volumeW)-1)/sizeof(WCHAR) ))
952 /* is there space in the return variable ?? */
953 if ((o1->SymbolicLinkNameLength/sizeof(WCHAR))+2 > size)
955 SetLastError( ERROR_FILENAME_EXCED_RANGE );
958 memcpy( volume, p, o1->SymbolicLinkNameLength );
959 volume[o1->SymbolicLinkNameLength / sizeof(WCHAR)] = 0;
960 lstrcatW( volume, trailingW );
961 /* change second char from '?' to '\' */
970 HeapFree( GetProcessHeap(), 0, input );
971 HeapFree( GetProcessHeap(), 0, output );
976 /***********************************************************************
977 * DefineDosDeviceW (KERNEL32.@)
979 BOOL WINAPI DefineDosDeviceW( DWORD flags, LPCWSTR devname, LPCWSTR targetpath )
983 char *path = NULL, *target, *p;
985 TRACE("%x, %s, %s\n", flags, debugstr_w(devname), debugstr_w(targetpath));
987 if (!(flags & DDD_REMOVE_DEFINITION))
989 if (!(flags & DDD_RAW_TARGET_PATH))
991 FIXME( "(0x%08x,%s,%s) DDD_RAW_TARGET_PATH flag not set, not supported yet\n",
992 flags, debugstr_w(devname), debugstr_w(targetpath) );
993 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
997 len = WideCharToMultiByte( CP_UNIXCP, 0, targetpath, -1, NULL, 0, NULL, NULL );
998 if ((target = HeapAlloc( GetProcessHeap(), 0, len )))
1000 WideCharToMultiByte( CP_UNIXCP, 0, targetpath, -1, target, len, NULL, NULL );
1001 for (p = target; *p; p++) if (*p == '\\') *p = '/';
1005 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1011 /* first check for a DOS device */
1013 if ((dosdev = RtlIsDosDeviceName_U( devname )))
1017 memcpy( name, devname + HIWORD(dosdev)/sizeof(WCHAR), LOWORD(dosdev) );
1018 name[LOWORD(dosdev)/sizeof(WCHAR)] = 0;
1019 path = get_dos_device_path( name );
1021 else if (isalphaW(devname[0]) && devname[1] == ':' && !devname[2]) /* drive mapping */
1023 path = get_dos_device_path( devname );
1025 else SetLastError( ERROR_FILE_NOT_FOUND );
1031 TRACE( "creating symlink %s -> %s\n", path, target );
1033 if (!symlink( target, path )) ret = TRUE;
1034 else FILE_SetDosError();
1038 TRACE( "removing symlink %s\n", path );
1039 if (!unlink( path )) ret = TRUE;
1040 else FILE_SetDosError();
1042 HeapFree( GetProcessHeap(), 0, path );
1044 HeapFree( GetProcessHeap(), 0, target );
1049 /***********************************************************************
1050 * DefineDosDeviceA (KERNEL32.@)
1052 BOOL WINAPI DefineDosDeviceA(DWORD flags, LPCSTR devname, LPCSTR targetpath)
1054 WCHAR *devW, *targetW = NULL;
1057 if (!(devW = FILE_name_AtoW( devname, FALSE ))) return FALSE;
1058 if (targetpath && !(targetW = FILE_name_AtoW( targetpath, TRUE ))) return FALSE;
1059 ret = DefineDosDeviceW(flags, devW, targetW);
1060 HeapFree( GetProcessHeap(), 0, targetW );
1065 /***********************************************************************
1066 * QueryDosDeviceW (KERNEL32.@)
1068 * returns array of strings terminated by \0, terminated by \0
1070 DWORD WINAPI QueryDosDeviceW( LPCWSTR devname, LPWSTR target, DWORD bufsize )
1072 static const WCHAR auxW[] = {'A','U','X',0};
1073 static const WCHAR nulW[] = {'N','U','L',0};
1074 static const WCHAR prnW[] = {'P','R','N',0};
1075 static const WCHAR comW[] = {'C','O','M',0};
1076 static const WCHAR lptW[] = {'L','P','T',0};
1077 static const WCHAR com0W[] = {'\\','?','?','\\','C','O','M','0',0};
1078 static const WCHAR com1W[] = {'\\','D','o','s','D','e','v','i','c','e','s','\\','C','O','M','1',0,0};
1079 static const WCHAR lpt1W[] = {'\\','D','o','s','D','e','v','i','c','e','s','\\','L','P','T','1',0,0};
1080 static const WCHAR dosdevW[] = {'\\','D','o','s','D','e','v','i','c','e','s','\\',0};
1082 UNICODE_STRING nt_name;
1083 ANSI_STRING unix_name;
1084 WCHAR nt_buffer[10];
1089 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1097 DWORD dosdev, ret = 0;
1099 if ((dosdev = RtlIsDosDeviceName_U( devname )))
1101 memcpy( name, devname + HIWORD(dosdev)/sizeof(WCHAR), LOWORD(dosdev) );
1102 name[LOWORD(dosdev)/sizeof(WCHAR)] = 0;
1108 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, sizeof(dosdevW) + strlenW(devname)*sizeof(WCHAR) )))
1110 SetLastError( ERROR_OUTOFMEMORY );
1113 memcpy( buffer, dosdevW, sizeof(dosdevW) );
1114 strcatW( buffer, devname );
1115 status = read_nt_symlink( buffer, target, bufsize );
1116 HeapFree( GetProcessHeap(), 0, buffer );
1119 SetLastError( RtlNtStatusToDosError(status) );
1122 ret = strlenW( target ) + 1;
1126 /* FIXME: should read NT symlink for all devices */
1128 if (!(path = get_dos_device_path( name ))) return 0;
1129 link = read_symlink( path );
1130 HeapFree( GetProcessHeap(), 0, path );
1134 ret = MultiByteToWideChar( CP_UNIXCP, 0, link, -1, target, bufsize );
1135 HeapFree( GetProcessHeap(), 0, link );
1137 else if (dosdev) /* look for device defaults */
1139 if (!strcmpiW( name, auxW ))
1141 if (bufsize >= sizeof(com1W)/sizeof(WCHAR))
1143 memcpy( target, com1W, sizeof(com1W) );
1144 ret = sizeof(com1W)/sizeof(WCHAR);
1146 else SetLastError( ERROR_INSUFFICIENT_BUFFER );
1149 if (!strcmpiW( name, prnW ))
1151 if (bufsize >= sizeof(lpt1W)/sizeof(WCHAR))
1153 memcpy( target, lpt1W, sizeof(lpt1W) );
1154 ret = sizeof(lpt1W)/sizeof(WCHAR);
1156 else SetLastError( ERROR_INSUFFICIENT_BUFFER );
1160 nt_buffer[0] = '\\';
1163 nt_buffer[3] = '\\';
1164 strcpyW( nt_buffer + 4, name );
1165 RtlInitUnicodeString( &nt_name, nt_buffer );
1166 status = wine_nt_to_unix_file_name( &nt_name, &unix_name, FILE_OPEN, TRUE );
1167 if (status) SetLastError( RtlNtStatusToDosError(status) );
1170 ret = MultiByteToWideChar( CP_UNIXCP, 0, unix_name.Buffer, -1, target, bufsize );
1171 RtlFreeAnsiString( &unix_name );
1177 if (ret < bufsize) target[ret++] = 0; /* add an extra null */
1178 for (p = target; *p; p++) if (*p == '/') *p = '\\';
1183 else /* return a list of all devices */
1185 OBJECT_ATTRIBUTES attr;
1190 if (bufsize <= (sizeof(auxW)+sizeof(nulW)+sizeof(prnW))/sizeof(WCHAR))
1192 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1196 /* FIXME: these should be NT symlinks too */
1198 memcpy( p, auxW, sizeof(auxW) );
1199 p += sizeof(auxW) / sizeof(WCHAR);
1200 memcpy( p, nulW, sizeof(nulW) );
1201 p += sizeof(nulW) / sizeof(WCHAR);
1202 memcpy( p, prnW, sizeof(prnW) );
1203 p += sizeof(prnW) / sizeof(WCHAR);
1205 strcpyW( nt_buffer, com0W );
1206 RtlInitUnicodeString( &nt_name, nt_buffer );
1208 for (i = 1; i <= 9; i++)
1210 nt_buffer[7] = '0' + i;
1211 if (!wine_nt_to_unix_file_name( &nt_name, &unix_name, FILE_OPEN, TRUE ))
1213 RtlFreeAnsiString( &unix_name );
1214 if (p + 5 >= target + bufsize)
1216 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1225 strcpyW( nt_buffer + 4, lptW );
1226 for (i = 1; i <= 9; i++)
1228 nt_buffer[7] = '0' + i;
1229 if (!wine_nt_to_unix_file_name( &nt_name, &unix_name, FILE_OPEN, TRUE ))
1231 RtlFreeAnsiString( &unix_name );
1232 if (p + 5 >= target + bufsize)
1234 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1244 RtlInitUnicodeString( &nt_name, dosdevW );
1245 nt_name.Length -= sizeof(WCHAR); /* without trailing slash */
1246 attr.Length = sizeof(attr);
1247 attr.RootDirectory = 0;
1248 attr.ObjectName = &nt_name;
1249 attr.Attributes = OBJ_CASE_INSENSITIVE;
1250 attr.SecurityDescriptor = NULL;
1251 attr.SecurityQualityOfService = NULL;
1252 status = NtOpenDirectoryObject( &handle, FILE_LIST_DIRECTORY, &attr );
1256 DIRECTORY_BASIC_INFORMATION *info = (DIRECTORY_BASIC_INFORMATION *)data;
1259 while (!NtQueryDirectoryObject( handle, info, sizeof(data), 1, 0, &ctx, &len ))
1261 if (p + info->ObjectName.Length/sizeof(WCHAR) + 1 >= target + bufsize)
1263 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1267 memcpy( p, info->ObjectName.Buffer, info->ObjectName.Length );
1268 p += info->ObjectName.Length/sizeof(WCHAR);
1274 *p++ = 0; /* terminating null */
1280 /***********************************************************************
1281 * QueryDosDeviceA (KERNEL32.@)
1283 * returns array of strings terminated by \0, terminated by \0
1285 DWORD WINAPI QueryDosDeviceA( LPCSTR devname, LPSTR target, DWORD bufsize )
1287 DWORD ret = 0, retW;
1288 WCHAR *devnameW = NULL;
1291 if (devname && !(devnameW = FILE_name_AtoW( devname, FALSE ))) return 0;
1293 targetW = HeapAlloc( GetProcessHeap(),0, bufsize * sizeof(WCHAR) );
1296 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1300 retW = QueryDosDeviceW(devnameW, targetW, bufsize);
1302 ret = FILE_name_WtoA( targetW, retW, target, bufsize );
1304 HeapFree(GetProcessHeap(), 0, targetW);
1309 /***********************************************************************
1310 * GetLogicalDrives (KERNEL32.@)
1312 DWORD WINAPI GetLogicalDrives(void)
1314 const char *config_dir = wine_get_config_dir();
1320 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, strlen(config_dir) + sizeof("/dosdevices/a:") )))
1322 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1325 strcpy( buffer, config_dir );
1326 strcat( buffer, "/dosdevices/a:" );
1327 dev = buffer + strlen(buffer) - 2;
1329 for (i = 0; i < 26; i++)
1332 if (!stat( buffer, &st )) ret |= (1 << i);
1334 HeapFree( GetProcessHeap(), 0, buffer );
1339 /***********************************************************************
1340 * GetLogicalDriveStringsA (KERNEL32.@)
1342 UINT WINAPI GetLogicalDriveStringsA( UINT len, LPSTR buffer )
1344 DWORD drives = GetLogicalDrives();
1347 for (drive = count = 0; drive < 26; drive++) if (drives & (1 << drive)) count++;
1348 if ((count * 4) + 1 > len) return count * 4 + 1;
1350 for (drive = 0; drive < 26; drive++)
1352 if (drives & (1 << drive))
1354 *buffer++ = 'A' + drive;
1365 /***********************************************************************
1366 * GetLogicalDriveStringsW (KERNEL32.@)
1368 UINT WINAPI GetLogicalDriveStringsW( UINT len, LPWSTR buffer )
1370 DWORD drives = GetLogicalDrives();
1373 for (drive = count = 0; drive < 26; drive++) if (drives & (1 << drive)) count++;
1374 if ((count * 4) + 1 > len) return count * 4 + 1;
1376 for (drive = 0; drive < 26; drive++)
1378 if (drives & (1 << drive))
1380 *buffer++ = 'A' + drive;
1391 /***********************************************************************
1392 * GetDriveTypeW (KERNEL32.@)
1394 * Returns the type of the disk drive specified. If root is NULL the
1395 * root of the current directory is used.
1399 * Type of drive (from Win32 SDK):
1401 * DRIVE_UNKNOWN unable to find out anything about the drive
1402 * DRIVE_NO_ROOT_DIR nonexistent root dir
1403 * DRIVE_REMOVABLE the disk can be removed from the machine
1404 * DRIVE_FIXED the disk cannot be removed from the machine
1405 * DRIVE_REMOTE network disk
1406 * DRIVE_CDROM CDROM drive
1407 * DRIVE_RAMDISK virtual disk in RAM
1409 UINT WINAPI GetDriveTypeW(LPCWSTR root) /* [in] String describing drive */
1411 FILE_FS_DEVICE_INFORMATION info;
1417 if (!open_device_root( root, &handle )) return DRIVE_NO_ROOT_DIR;
1419 status = NtQueryVolumeInformationFile( handle, &io, &info, sizeof(info), FileFsDeviceInformation );
1421 if (status != STATUS_SUCCESS)
1423 SetLastError( RtlNtStatusToDosError(status) );
1424 ret = DRIVE_UNKNOWN;
1428 switch (info.DeviceType)
1430 case FILE_DEVICE_CD_ROM_FILE_SYSTEM: ret = DRIVE_CDROM; break;
1431 case FILE_DEVICE_VIRTUAL_DISK: ret = DRIVE_RAMDISK; break;
1432 case FILE_DEVICE_NETWORK_FILE_SYSTEM: ret = DRIVE_REMOTE; break;
1433 case FILE_DEVICE_DISK_FILE_SYSTEM:
1434 if (info.Characteristics & FILE_REMOTE_DEVICE) ret = DRIVE_REMOTE;
1435 else if (info.Characteristics & FILE_REMOVABLE_MEDIA) ret = DRIVE_REMOVABLE;
1436 else if ((ret = get_mountmgr_drive_type( root )) == DRIVE_UNKNOWN) ret = DRIVE_FIXED;
1439 ret = DRIVE_UNKNOWN;
1443 TRACE( "%s -> %d\n", debugstr_w(root), ret );
1448 /***********************************************************************
1449 * GetDriveTypeA (KERNEL32.@)
1451 * See GetDriveTypeW.
1453 UINT WINAPI GetDriveTypeA( LPCSTR root )
1455 WCHAR *rootW = NULL;
1457 if (root && !(rootW = FILE_name_AtoW( root, FALSE ))) return DRIVE_NO_ROOT_DIR;
1458 return GetDriveTypeW( rootW );
1462 /***********************************************************************
1463 * GetDiskFreeSpaceExW (KERNEL32.@)
1465 * This function is used to acquire the size of the available and
1466 * total space on a logical volume.
1470 * Zero on failure, nonzero upon success. Use GetLastError to obtain
1471 * detailed error information.
1474 BOOL WINAPI GetDiskFreeSpaceExW( LPCWSTR root, PULARGE_INTEGER avail,
1475 PULARGE_INTEGER total, PULARGE_INTEGER totalfree )
1477 FILE_FS_SIZE_INFORMATION info;
1483 TRACE( "%s,%p,%p,%p\n", debugstr_w(root), avail, total, totalfree );
1485 if (!open_device_root( root, &handle )) return FALSE;
1487 status = NtQueryVolumeInformationFile( handle, &io, &info, sizeof(info), FileFsSizeInformation );
1489 if (status != STATUS_SUCCESS)
1491 SetLastError( RtlNtStatusToDosError(status) );
1495 units = info.SectorsPerAllocationUnit * info.BytesPerSector;
1496 if (total) total->QuadPart = info.TotalAllocationUnits.QuadPart * units;
1497 if (totalfree) totalfree->QuadPart = info.AvailableAllocationUnits.QuadPart * units;
1498 /* FIXME: this one should take quotas into account */
1499 if (avail) avail->QuadPart = info.AvailableAllocationUnits.QuadPart * units;
1504 /***********************************************************************
1505 * GetDiskFreeSpaceExA (KERNEL32.@)
1507 * See GetDiskFreeSpaceExW.
1509 BOOL WINAPI GetDiskFreeSpaceExA( LPCSTR root, PULARGE_INTEGER avail,
1510 PULARGE_INTEGER total, PULARGE_INTEGER totalfree )
1512 WCHAR *rootW = NULL;
1514 if (root && !(rootW = FILE_name_AtoW( root, FALSE ))) return FALSE;
1515 return GetDiskFreeSpaceExW( rootW, avail, total, totalfree );
1519 /***********************************************************************
1520 * GetDiskFreeSpaceW (KERNEL32.@)
1522 BOOL WINAPI GetDiskFreeSpaceW( LPCWSTR root, LPDWORD cluster_sectors,
1523 LPDWORD sector_bytes, LPDWORD free_clusters,
1524 LPDWORD total_clusters )
1526 FILE_FS_SIZE_INFORMATION info;
1532 TRACE( "%s,%p,%p,%p,%p\n", debugstr_w(root),
1533 cluster_sectors, sector_bytes, free_clusters, total_clusters );
1535 if (!open_device_root( root, &handle )) return FALSE;
1537 status = NtQueryVolumeInformationFile( handle, &io, &info, sizeof(info), FileFsSizeInformation );
1539 if (status != STATUS_SUCCESS)
1541 SetLastError( RtlNtStatusToDosError(status) );
1545 units = info.SectorsPerAllocationUnit * info.BytesPerSector;
1547 if( GetVersion() & 0x80000000) { /* win3.x, 9x, ME */
1548 /* cap the size and available at 2GB as per specs */
1549 if (info.TotalAllocationUnits.QuadPart * units > 0x7fffffff) {
1550 info.TotalAllocationUnits.QuadPart = 0x7fffffff / units;
1551 if (info.AvailableAllocationUnits.QuadPart * units > 0x7fffffff)
1552 info.AvailableAllocationUnits.QuadPart = 0x7fffffff / units;
1554 /* nr. of clusters is always <= 65335 */
1555 while( info.TotalAllocationUnits.QuadPart > 65535 ) {
1556 info.TotalAllocationUnits.QuadPart /= 2;
1557 info.AvailableAllocationUnits.QuadPart /= 2;
1558 info.SectorsPerAllocationUnit *= 2;
1562 if (cluster_sectors) *cluster_sectors = info.SectorsPerAllocationUnit;
1563 if (sector_bytes) *sector_bytes = info.BytesPerSector;
1564 if (free_clusters) *free_clusters = info.AvailableAllocationUnits.u.LowPart;
1565 if (total_clusters) *total_clusters = info.TotalAllocationUnits.u.LowPart;
1570 /***********************************************************************
1571 * GetDiskFreeSpaceA (KERNEL32.@)
1573 BOOL WINAPI GetDiskFreeSpaceA( LPCSTR root, LPDWORD cluster_sectors,
1574 LPDWORD sector_bytes, LPDWORD free_clusters,
1575 LPDWORD total_clusters )
1577 WCHAR *rootW = NULL;
1579 if (root && !(rootW = FILE_name_AtoW( root, FALSE ))) return FALSE;
1580 return GetDiskFreeSpaceW( rootW, cluster_sectors, sector_bytes, free_clusters, total_clusters );
1583 /***********************************************************************
1584 * GetVolumePathNameA (KERNEL32.@)
1586 BOOL WINAPI GetVolumePathNameA(LPCSTR filename, LPSTR volumepathname, DWORD buflen)
1589 WCHAR *filenameW = NULL, *volumeW;
1591 FIXME("(%s, %p, %d), stub!\n", debugstr_a(filename), volumepathname, buflen);
1593 if (filename && !(filenameW = FILE_name_AtoW( filename, FALSE ))) return FALSE;
1594 if (!(volumeW = HeapAlloc( GetProcessHeap(), 0, buflen * sizeof(WCHAR) ))) return FALSE;
1596 if ((ret = GetVolumePathNameW( filenameW, volumeW, buflen )))
1597 FILE_name_WtoA( volumeW, -1, volumepathname, buflen );
1599 HeapFree( GetProcessHeap(), 0, volumeW );
1603 /***********************************************************************
1604 * GetVolumePathNameW (KERNEL32.@)
1606 BOOL WINAPI GetVolumePathNameW(LPCWSTR filename, LPWSTR volumepathname, DWORD buflen)
1608 const WCHAR *p = filename;
1610 FIXME("(%s, %p, %d), stub!\n", debugstr_w(filename), volumepathname, buflen);
1612 if (p && tolowerW(p[0]) >= 'a' && tolowerW(p[0]) <= 'z' && p[1] ==':' && p[2] == '\\' && buflen >= 4)
1614 volumepathname[0] = p[0];
1615 volumepathname[1] = ':';
1616 volumepathname[2] = '\\';
1617 volumepathname[3] = 0;
1623 /***********************************************************************
1624 * GetVolumePathNamesForVolumeNameA (KERNEL32.@)
1626 BOOL WINAPI GetVolumePathNamesForVolumeNameA(LPCSTR volumename, LPSTR volumepathname, DWORD buflen, PDWORD returnlen)
1629 WCHAR *volumenameW = NULL, *volumepathnameW;
1631 if (volumename && !(volumenameW = FILE_name_AtoW( volumename, TRUE ))) return FALSE;
1632 if (!(volumepathnameW = HeapAlloc( GetProcessHeap(), 0, buflen * sizeof(WCHAR) )))
1634 HeapFree( GetProcessHeap(), 0, volumenameW );
1637 if ((ret = GetVolumePathNamesForVolumeNameW( volumenameW, volumepathnameW, buflen, returnlen )))
1639 char *path = volumepathname;
1640 const WCHAR *pathW = volumepathnameW;
1644 int len = strlenW( pathW ) + 1;
1645 FILE_name_WtoA( pathW, len, path, buflen );
1652 HeapFree( GetProcessHeap(), 0, volumenameW );
1653 HeapFree( GetProcessHeap(), 0, volumepathnameW );
1657 static MOUNTMGR_MOUNT_POINTS *query_mount_points( HANDLE mgr, MOUNTMGR_MOUNT_POINT *input, DWORD insize )
1659 MOUNTMGR_MOUNT_POINTS *output;
1660 DWORD outsize = 1024;
1664 if (!(output = HeapAlloc( GetProcessHeap(), 0, outsize )))
1666 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1669 if (DeviceIoControl( mgr, IOCTL_MOUNTMGR_QUERY_POINTS, input, insize, output, outsize, NULL, NULL )) break;
1670 outsize = output->Size;
1671 HeapFree( GetProcessHeap(), 0, output );
1672 if (GetLastError() != ERROR_MORE_DATA) return NULL;
1676 /***********************************************************************
1677 * GetVolumePathNamesForVolumeNameW (KERNEL32.@)
1679 BOOL WINAPI GetVolumePathNamesForVolumeNameW(LPCWSTR volumename, LPWSTR volumepathname, DWORD buflen, PDWORD returnlen)
1681 static const WCHAR dosdevicesW[] = {'\\','D','o','s','D','e','v','i','c','e','s','\\'};
1684 MOUNTMGR_MOUNT_POINT *spec;
1685 MOUNTMGR_MOUNT_POINTS *link, *target = NULL;
1690 TRACE("%s, %p, %u, %p\n", debugstr_w(volumename), volumepathname, buflen, returnlen);
1692 if (!volumename || (len = strlenW( volumename )) != 49)
1694 SetLastError( ERROR_INVALID_NAME );
1697 mgr = CreateFileW( MOUNTMGR_DOS_DEVICE_NAME, 0, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0 );
1698 if (mgr == INVALID_HANDLE_VALUE) return FALSE;
1700 size = sizeof(*spec) + sizeof(WCHAR) * (len - 1); /* remove trailing backslash */
1701 if (!(spec = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size ))) goto done;
1702 spec->SymbolicLinkNameOffset = sizeof(*spec);
1703 spec->SymbolicLinkNameLength = size - sizeof(*spec);
1704 name = (WCHAR *)((char *)spec + spec->SymbolicLinkNameOffset);
1705 memcpy( name, volumename, size - sizeof(*spec) );
1706 name[1] = '?'; /* map \\?\ to \??\ */
1708 target = query_mount_points( mgr, spec, size );
1709 HeapFree( GetProcessHeap(), 0, spec );
1714 if (!target->NumberOfMountPoints)
1716 SetLastError( ERROR_FILE_NOT_FOUND );
1720 path = volumepathname;
1721 for (i = 0; i < target->NumberOfMountPoints; i++)
1724 if (target->MountPoints[i].DeviceNameOffset)
1726 const WCHAR *device = (const WCHAR *)((const char *)target + target->MountPoints[i].DeviceNameOffset);
1727 USHORT device_len = target->MountPoints[i].DeviceNameLength;
1729 size = sizeof(*spec) + device_len;
1730 if (!(spec = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size ))) goto done;
1731 spec->DeviceNameOffset = sizeof(*spec);
1732 spec->DeviceNameLength = device_len;
1733 memcpy( (char *)spec + spec->DeviceNameOffset, device, device_len );
1735 link = query_mount_points( mgr, spec, size );
1736 HeapFree( GetProcessHeap(), 0, spec );
1738 else if (target->MountPoints[i].UniqueIdOffset)
1740 const WCHAR *id = (const WCHAR *)((const char *)target + target->MountPoints[i].UniqueIdOffset);
1741 USHORT id_len = target->MountPoints[i].UniqueIdLength;
1743 size = sizeof(*spec) + id_len;
1744 if (!(spec = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size ))) goto done;
1745 spec->UniqueIdOffset = sizeof(*spec);
1746 spec->UniqueIdLength = id_len;
1747 memcpy( (char *)spec + spec->UniqueIdOffset, id, id_len );
1749 link = query_mount_points( mgr, spec, size );
1750 HeapFree( GetProcessHeap(), 0, spec );
1752 if (!link) continue;
1753 for (j = 0; j < link->NumberOfMountPoints; j++)
1755 const WCHAR *linkname;
1757 if (!link->MountPoints[j].SymbolicLinkNameOffset) continue;
1758 linkname = (const WCHAR *)((const char *)link + link->MountPoints[j].SymbolicLinkNameOffset);
1760 if (link->MountPoints[j].SymbolicLinkNameLength == sizeof(dosdevicesW) + 2 * sizeof(WCHAR) &&
1761 !memicmpW( linkname, dosdevicesW, sizeof(dosdevicesW) / sizeof(WCHAR) ))
1764 if (volumepathname && len < buflen)
1766 path[0] = linkname[sizeof(dosdevicesW) / sizeof(WCHAR)];
1774 HeapFree( GetProcessHeap(), 0, link );
1776 if (buflen <= len) SetLastError( ERROR_MORE_DATA );
1777 else if (volumepathname)
1779 volumepathname[len] = 0;
1782 if (returnlen) *returnlen = len + 1;
1785 HeapFree( GetProcessHeap(), 0, target );
1790 /***********************************************************************
1791 * FindFirstVolumeA (KERNEL32.@)
1793 HANDLE WINAPI FindFirstVolumeA(LPSTR volume, DWORD len)
1795 WCHAR *buffer = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1796 HANDLE handle = FindFirstVolumeW( buffer, len );
1798 if (handle != INVALID_HANDLE_VALUE)
1800 if (!WideCharToMultiByte( CP_ACP, 0, buffer, -1, volume, len, NULL, NULL ))
1802 FindVolumeClose( handle );
1803 handle = INVALID_HANDLE_VALUE;
1806 HeapFree( GetProcessHeap(), 0, buffer );
1810 /***********************************************************************
1811 * FindFirstVolumeW (KERNEL32.@)
1813 HANDLE WINAPI FindFirstVolumeW( LPWSTR volume, DWORD len )
1816 HANDLE mgr = CreateFileW( MOUNTMGR_DOS_DEVICE_NAME, 0, FILE_SHARE_READ|FILE_SHARE_WRITE,
1817 NULL, OPEN_EXISTING, 0, 0 );
1818 if (mgr == INVALID_HANDLE_VALUE) return INVALID_HANDLE_VALUE;
1822 MOUNTMGR_MOUNT_POINT input;
1823 MOUNTMGR_MOUNT_POINTS *output;
1825 if (!(output = HeapAlloc( GetProcessHeap(), 0, size )))
1827 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1830 memset( &input, 0, sizeof(input) );
1832 if (!DeviceIoControl( mgr, IOCTL_MOUNTMGR_QUERY_POINTS, &input, sizeof(input),
1833 output, size, NULL, NULL ))
1835 if (GetLastError() != ERROR_MORE_DATA) break;
1836 size = output->Size;
1837 HeapFree( GetProcessHeap(), 0, output );
1841 /* abuse the Size field to store the current index */
1843 if (!FindNextVolumeW( output, volume, len ))
1845 HeapFree( GetProcessHeap(), 0, output );
1846 return INVALID_HANDLE_VALUE;
1851 return INVALID_HANDLE_VALUE;
1854 /***********************************************************************
1855 * FindNextVolumeA (KERNEL32.@)
1857 BOOL WINAPI FindNextVolumeA( HANDLE handle, LPSTR volume, DWORD len )
1859 WCHAR *buffer = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1862 if ((ret = FindNextVolumeW( handle, buffer, len )))
1864 if (!WideCharToMultiByte( CP_ACP, 0, buffer, -1, volume, len, NULL, NULL )) ret = FALSE;
1866 HeapFree( GetProcessHeap(), 0, buffer );
1870 /***********************************************************************
1871 * FindNextVolumeW (KERNEL32.@)
1873 BOOL WINAPI FindNextVolumeW( HANDLE handle, LPWSTR volume, DWORD len )
1875 MOUNTMGR_MOUNT_POINTS *data = handle;
1877 while (data->Size < data->NumberOfMountPoints)
1879 static const WCHAR volumeW[] = {'\\','?','?','\\','V','o','l','u','m','e','{',};
1880 WCHAR *link = (WCHAR *)((char *)data + data->MountPoints[data->Size].SymbolicLinkNameOffset);
1881 DWORD size = data->MountPoints[data->Size].SymbolicLinkNameLength;
1883 /* skip non-volumes */
1884 if (size < sizeof(volumeW) || memcmp( link, volumeW, sizeof(volumeW) )) continue;
1885 if (size + sizeof(WCHAR) >= len * sizeof(WCHAR))
1887 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1890 memcpy( volume, link, size );
1891 volume[1] = '\\'; /* map \??\ to \\?\ */
1892 volume[size / sizeof(WCHAR)] = '\\'; /* Windows appends a backslash */
1893 volume[size / sizeof(WCHAR) + 1] = 0;
1894 TRACE( "returning entry %u %s\n", data->Size - 1, debugstr_w(volume) );
1897 SetLastError( ERROR_NO_MORE_FILES );
1901 /***********************************************************************
1902 * FindVolumeClose (KERNEL32.@)
1904 BOOL WINAPI FindVolumeClose(HANDLE handle)
1906 return HeapFree( GetProcessHeap(), 0, handle );
1909 /***********************************************************************
1910 * FindFirstVolumeMountPointA (KERNEL32.@)
1912 HANDLE WINAPI FindFirstVolumeMountPointA(LPCSTR root, LPSTR mount_point, DWORD len)
1914 FIXME("(%s, %p, %d), stub!\n", debugstr_a(root), mount_point, len);
1915 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1916 return INVALID_HANDLE_VALUE;
1919 /***********************************************************************
1920 * FindFirstVolumeMountPointW (KERNEL32.@)
1922 HANDLE WINAPI FindFirstVolumeMountPointW(LPCWSTR root, LPWSTR mount_point, DWORD len)
1924 FIXME("(%s, %p, %d), stub!\n", debugstr_w(root), mount_point, len);
1925 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1926 return INVALID_HANDLE_VALUE;
1929 /***********************************************************************
1930 * FindVolumeMountPointClose (KERNEL32.@)
1932 BOOL WINAPI FindVolumeMountPointClose(HANDLE h)
1934 FIXME("(%p), stub!\n", h);