user32: Fix user_thread_info for 64-bits
[wine/wine64.git] / dlls / kernel32 / volume.c
blob6baf8c5e888613cf5a2f22d9234b02303ad14d81
1 /*
2 * Volume management functions
4 * Copyright 1993 Erik Bos
5 * Copyright 1996, 2004 Alexandre Julliard
6 * Copyright 1999 Petr Tomasek
7 * Copyright 2000 Andreas Mohr
8 * Copyright 2003 Eric Pouech
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #include "config.h"
26 #include "wine/port.h"
28 #include <stdarg.h>
29 #include <stdlib.h>
30 #include <stdio.h>
32 #include "ntstatus.h"
33 #define WIN32_NO_STATUS
34 #include "windef.h"
35 #include "winbase.h"
36 #include "winnls.h"
37 #include "winternl.h"
38 #include "winioctl.h"
39 #include "ntddcdrm.h"
40 #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))
60 enum fs_type
62 FS_ERROR, /* error accessing the device */
63 FS_UNKNOWN, /* unknown file system */
64 FS_FAT1216,
65 FS_FAT32,
66 FS_ISO9660
69 /* read a Unix symlink; returned buffer must be freed by caller */
70 static char *read_symlink( const char *path )
72 char *buffer;
73 int ret, size = 128;
75 for (;;)
77 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, size )))
79 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
80 return 0;
82 ret = readlink( path, buffer, size );
83 if (ret == -1)
85 FILE_SetDosError();
86 HeapFree( GetProcessHeap(), 0, buffer );
87 return 0;
89 if (ret != size)
91 buffer[ret] = 0;
92 return buffer;
94 HeapFree( GetProcessHeap(), 0, buffer );
95 size *= 2;
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();
103 char *buffer, *dev;
104 int i;
106 if (!(buffer = HeapAlloc( GetProcessHeap(), 0,
107 strlen(config_dir) + sizeof("/dosdevices/") + 5 )))
109 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
110 return NULL;
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;
118 dev[5] = 0;
119 return buffer;
122 /* read the contents of an NT symlink object */
123 static NTSTATUS read_nt_symlink( const WCHAR *name, WCHAR *target, DWORD size )
125 NTSTATUS status;
126 OBJECT_ATTRIBUTES attr;
127 UNICODE_STRING nameW;
128 HANDLE handle;
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;
145 NtClose( handle );
147 return status;
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;
156 IO_STATUS_BLOCK io;
157 NTSTATUS status;
159 if (!root) root = default_rootW;
160 if (!RtlDosPathNameToNtPathName_U( root, &nt_name, NULL, NULL ))
162 SetLastError( ERROR_PATH_NOT_FOUND );
163 return FALSE;
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) );
178 return FALSE;
180 return TRUE;
183 /* query the type of a drive from the mount manager */
184 static DWORD get_mountmgr_drive_type( LPCWSTR root )
186 HANDLE mgr;
187 struct mountmgr_unix_drive data;
189 memset( &data, 0, sizeof(data) );
190 if (root) data.letter = root[0];
191 else
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;
207 CloseHandle( mgr );
208 return data.type;
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 )
214 HANDLE handle;
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;
223 DWORD size;
225 if (!ReadFile( handle, buffer, sizeof(buffer)-1, &size, NULL )) size = 0;
226 CloseHandle( handle );
227 p = buffer + size;
228 while (p > buffer && (p[-1] == ' ' || p[-1] == '\r' || p[-1] == '\n')) p--;
229 *p = 0;
230 if (!MultiByteToWideChar( CP_UNIXCP, 0, buffer, -1, label, len ))
231 label[len-1] = 0;
233 else label[0] = 0;
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 )
239 HANDLE handle;
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)
247 char buffer[32];
248 DWORD size;
250 if (!ReadFile( handle, buffer, sizeof(buffer)-1, &size, NULL )) size = 0;
251 CloseHandle( handle );
252 buffer[size] = 0;
253 return strtoul( buffer, NULL, 16 );
255 else return 0;
259 /******************************************************************
260 * VOLUME_FindCdRomDataBestVoldesc
262 static DWORD VOLUME_FindCdRomDataBestVoldesc( HANDLE handle )
264 BYTE cur_vd_type, max_vd_type = 0;
265 BYTE buffer[0x800];
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 */
280 break;
281 if (cur_vd_type > max_vd_type)
283 max_vd_type = cur_vd_type;
284 best_offs = offs + extra_offs;
287 return best_offs;
291 /***********************************************************************
292 * VOLUME_ReadFATSuperblock
294 static enum fs_type VOLUME_ReadFATSuperblock( HANDLE handle, BYTE *buff )
296 DWORD size;
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 */
303 return FS_ERROR;
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 */
314 int reasonable;
315 unsigned int sectors,
316 sect_per_fat,
317 total_sectors,
318 num_boot_sectors,
319 num_fats,
320 num_root_dir_ents,
321 bytes_per_sector,
322 sectors_per_cluster,
323 nclust;
324 sect_per_fat = GETWORD(buff, 0x16);
325 if (!sect_per_fat) sect_per_fat = GETLONG(buff, 0x24);
326 total_sectors = GETWORD(buff, 0x13);
327 if (!total_sectors)
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 &&
337 num_fats < 16 &&
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;
346 if (nclust < 65525)
348 if ((buff[0x26] == 0x28 || buff[0x26] == 0x29) &&
349 !memcmp(buff+0x36, "FAT", 3))
350 return FS_FAT1216;
353 return FS_UNKNOWN;
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)
369 return FS_ERROR;
371 /* check for iso9660 present */
372 if (!memcmp(&buff[1], "CD001", 5)) return FS_ISO9660;
373 return FS_UNKNOWN;
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;
384 DWORD label_len;
386 switch(type)
388 case FS_ERROR:
389 label_len = 0;
390 break;
391 case FS_UNKNOWN:
392 get_filesystem_label( device, label, len );
393 return;
394 case FS_FAT1216:
395 label_ptr = superblock + 0x2b;
396 label_len = 11;
397 break;
398 case FS_FAT32:
399 label_ptr = superblock + 0x47;
400 label_len = 11;
401 break;
402 case FS_ISO9660:
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 */
409 unsigned int i;
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];
414 label[i] = 0;
415 while (i && label[i-1] == ' ') label[--i] = 0;
416 return;
418 label_ptr = superblock + 40;
419 label_len = 32;
420 break;
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 )
436 switch(type)
438 case FS_ERROR:
439 break;
440 case FS_UNKNOWN:
441 return get_filesystem_serial( device );
442 case FS_FAT1216:
443 return GETLONG( superblock, 0x27 );
444 case FS_FAT32:
445 return GETLONG( superblock, 0x33 );
446 case FS_ISO9660:
448 BYTE sum[4];
449 int i;
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];
467 else
468 return (sum[0] << 24) | (sum[1] << 16) | (sum[2] << 8) | sum[3];
471 return 0;
475 /**************************************************************************
476 * VOLUME_GetAudioCDSerial
478 static DWORD VOLUME_GetAudioCDSerial( const CDROM_TOC *toc )
480 DWORD serial = 0;
481 int i;
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
490 * frames.
491 * There it is collected for correcting the serial when there are less than
492 * 3 tracks.
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;
500 return serial;
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};
517 WCHAR device[] = {'\\','\\','.','\\','A',':',0};
518 HANDLE handle;
519 enum fs_type type = FS_UNKNOWN;
521 if (!root)
523 WCHAR path[MAX_PATH];
524 GetCurrentDirectoryW( MAX_PATH, path );
525 device[4] = path[0];
527 else
529 if (!root[0] || root[1] != ':')
531 SetLastError( ERROR_INVALID_NAME );
532 return FALSE;
534 device[4] = root[0];
537 /* try to open the device */
539 handle = CreateFileW( device, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE,
540 NULL, OPEN_EXISTING, 0, 0 );
541 if (handle != INVALID_HANDLE_VALUE)
543 BYTE superblock[SUPERBLOCK_SIZE];
544 CDROM_TOC toc;
545 DWORD br;
547 /* check for audio CD */
548 /* FIXME: we only check the first track for now */
549 if (DeviceIoControl( handle, IOCTL_CDROM_READ_TOC, NULL, 0, &toc, sizeof(toc), &br, 0 ))
551 if (!(toc.TrackData[0].Control & 0x04)) /* audio track */
553 TRACE( "%s: found audio CD\n", debugstr_w(device) );
554 if (label) lstrcpynW( label, audiocdW, label_len );
555 if (serial) *serial = VOLUME_GetAudioCDSerial( &toc );
556 CloseHandle( handle );
557 type = FS_ISO9660;
558 goto fill_fs_info;
560 type = VOLUME_ReadCDSuperblock( handle, superblock );
562 else
564 type = VOLUME_ReadFATSuperblock( handle, superblock );
565 if (type == FS_UNKNOWN) type = VOLUME_ReadCDSuperblock( handle, superblock );
567 CloseHandle( handle );
568 TRACE( "%s: found fs type %d\n", debugstr_w(device), type );
569 if (type == FS_ERROR) return FALSE;
571 if (label && label_len) VOLUME_GetSuperblockLabel( device, type, superblock, label, label_len );
572 if (serial) *serial = VOLUME_GetSuperblockSerial( device, type, superblock );
573 goto fill_fs_info;
575 else TRACE( "cannot open device %s: err %d\n", debugstr_w(device), GetLastError() );
577 /* we couldn't open the device, fallback to default strategy */
579 switch(GetDriveTypeW( root ))
581 case DRIVE_UNKNOWN:
582 case DRIVE_NO_ROOT_DIR:
583 SetLastError( ERROR_NOT_READY );
584 return FALSE;
585 case DRIVE_REMOVABLE:
586 case DRIVE_FIXED:
587 case DRIVE_REMOTE:
588 case DRIVE_RAMDISK:
589 type = FS_UNKNOWN;
590 break;
591 case DRIVE_CDROM:
592 type = FS_ISO9660;
593 break;
596 if (label && label_len) get_filesystem_label( device, label, label_len );
597 if (serial) *serial = get_filesystem_serial( device );
599 fill_fs_info: /* now fill in the information that depends on the file system type */
601 switch(type)
603 case FS_ISO9660:
604 if (fsname) lstrcpynW( fsname, cdfsW, fsname_len );
605 if (filename_len) *filename_len = 221;
606 if (flags) *flags = FILE_READ_ONLY_VOLUME;
607 break;
608 case FS_FAT1216:
609 if (fsname) lstrcpynW( fsname, fatW, fsname_len );
610 case FS_FAT32:
611 if (type == FS_FAT32 && fsname) lstrcpynW( fsname, fat32W, fsname_len );
612 if (filename_len) *filename_len = 255;
613 if (flags) *flags = FILE_CASE_PRESERVED_NAMES; /* FIXME */
614 break;
615 default:
616 if (fsname) lstrcpynW( fsname, ntfsW, fsname_len );
617 if (filename_len) *filename_len = 255;
618 if (flags) *flags = FILE_CASE_PRESERVED_NAMES;
619 break;
621 return TRUE;
625 /***********************************************************************
626 * GetVolumeInformationA (KERNEL32.@)
628 BOOL WINAPI GetVolumeInformationA( LPCSTR root, LPSTR label,
629 DWORD label_len, DWORD *serial,
630 DWORD *filename_len, DWORD *flags,
631 LPSTR fsname, DWORD fsname_len )
633 WCHAR *rootW = NULL;
634 LPWSTR labelW, fsnameW;
635 BOOL ret;
637 if (root && !(rootW = FILE_name_AtoW( root, FALSE ))) return FALSE;
639 labelW = label ? HeapAlloc(GetProcessHeap(), 0, label_len * sizeof(WCHAR)) : NULL;
640 fsnameW = fsname ? HeapAlloc(GetProcessHeap(), 0, fsname_len * sizeof(WCHAR)) : NULL;
642 if ((ret = GetVolumeInformationW(rootW, labelW, label_len, serial,
643 filename_len, flags, fsnameW, fsname_len)))
645 if (label) FILE_name_WtoA( labelW, -1, label, label_len );
646 if (fsname) FILE_name_WtoA( fsnameW, -1, fsname, fsname_len );
649 HeapFree( GetProcessHeap(), 0, labelW );
650 HeapFree( GetProcessHeap(), 0, fsnameW );
651 return ret;
656 /***********************************************************************
657 * SetVolumeLabelW (KERNEL32.@)
659 BOOL WINAPI SetVolumeLabelW( LPCWSTR root, LPCWSTR label )
661 WCHAR device[] = {'\\','\\','.','\\','A',':',0};
662 HANDLE handle;
663 enum fs_type type = FS_UNKNOWN;
665 if (!root)
667 WCHAR path[MAX_PATH];
668 GetCurrentDirectoryW( MAX_PATH, path );
669 device[4] = path[0];
671 else
673 if (!root[0] || root[1] != ':')
675 SetLastError( ERROR_INVALID_NAME );
676 return FALSE;
678 device[4] = root[0];
681 /* try to open the device */
683 handle = CreateFileW( device, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE,
684 NULL, OPEN_EXISTING, 0, 0 );
685 if (handle != INVALID_HANDLE_VALUE)
687 BYTE superblock[SUPERBLOCK_SIZE];
689 type = VOLUME_ReadFATSuperblock( handle, superblock );
690 if (type == FS_UNKNOWN) type = VOLUME_ReadCDSuperblock( handle, superblock );
691 CloseHandle( handle );
692 if (type != FS_UNKNOWN)
694 /* we can't set the label on FAT or CDROM file systems */
695 TRACE( "cannot set label on device %s type %d\n", debugstr_w(device), type );
696 SetLastError( ERROR_ACCESS_DENIED );
697 return FALSE;
700 else
702 TRACE( "cannot open device %s: err %d\n", debugstr_w(device), GetLastError() );
703 if (GetLastError() == ERROR_ACCESS_DENIED) return FALSE;
706 /* we couldn't open the device, fallback to default strategy */
708 switch(GetDriveTypeW( root ))
710 case DRIVE_UNKNOWN:
711 case DRIVE_NO_ROOT_DIR:
712 SetLastError( ERROR_NOT_READY );
713 break;
714 case DRIVE_REMOVABLE:
715 case DRIVE_FIXED:
717 WCHAR labelW[] = {'A',':','\\','.','w','i','n','d','o','w','s','-','l','a','b','e','l',0};
719 labelW[0] = device[4];
721 if (!label[0]) /* delete label file when setting an empty label */
722 return DeleteFileW( labelW ) || GetLastError() == ERROR_FILE_NOT_FOUND;
724 handle = CreateFileW( labelW, GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
725 CREATE_ALWAYS, 0, 0 );
726 if (handle != INVALID_HANDLE_VALUE)
728 char buffer[64];
729 DWORD size;
731 if (!WideCharToMultiByte( CP_UNIXCP, 0, label, -1, buffer, sizeof(buffer)-1, NULL, NULL ))
732 buffer[sizeof(buffer)-2] = 0;
733 strcat( buffer, "\n" );
734 WriteFile( handle, buffer, strlen(buffer), &size, NULL );
735 CloseHandle( handle );
736 return TRUE;
738 break;
740 case DRIVE_REMOTE:
741 case DRIVE_RAMDISK:
742 case DRIVE_CDROM:
743 SetLastError( ERROR_ACCESS_DENIED );
744 break;
746 return FALSE;
749 /***********************************************************************
750 * SetVolumeLabelA (KERNEL32.@)
752 BOOL WINAPI SetVolumeLabelA(LPCSTR root, LPCSTR volname)
754 WCHAR *rootW = NULL, *volnameW = NULL;
755 BOOL ret;
757 if (root && !(rootW = FILE_name_AtoW( root, FALSE ))) return FALSE;
758 if (volname && !(volnameW = FILE_name_AtoW( volname, TRUE ))) return FALSE;
759 ret = SetVolumeLabelW( rootW, volnameW );
760 HeapFree( GetProcessHeap(), 0, volnameW );
761 return ret;
765 /***********************************************************************
766 * GetVolumeNameForVolumeMountPointA (KERNEL32.@)
768 BOOL WINAPI GetVolumeNameForVolumeMountPointA( LPCSTR path, LPSTR volume, DWORD size )
770 BOOL ret;
771 WCHAR volumeW[50], *pathW = NULL;
772 DWORD len = min( sizeof(volumeW) / sizeof(WCHAR), size );
774 TRACE("(%s, %p, %x)\n", debugstr_a(path), volume, size);
776 if (!path || !(pathW = FILE_name_AtoW( path, TRUE )))
777 return FALSE;
779 if ((ret = GetVolumeNameForVolumeMountPointW( pathW, volumeW, len )))
780 FILE_name_WtoA( volumeW, -1, volume, len );
782 HeapFree( GetProcessHeap(), 0, pathW );
783 return ret;
786 /***********************************************************************
787 * GetVolumeNameForVolumeMountPointW (KERNEL32.@)
789 BOOL WINAPI GetVolumeNameForVolumeMountPointW( LPCWSTR path, LPWSTR volume, DWORD size )
791 BOOL ret = FALSE;
792 static const WCHAR fmt[] =
793 { '\\','\\','?','\\','V','o','l','u','m','e','{','%','0','2','x','}','\\',0 };
795 TRACE("(%s, %p, %x)\n", debugstr_w(path), volume, size);
797 if (!path || !path[0]) return FALSE;
799 if (size >= sizeof(fmt) / sizeof(WCHAR))
801 /* FIXME: will break when we support volume mounts */
802 sprintfW( volume, fmt, tolowerW( path[0] ) - 'a' );
803 ret = TRUE;
805 return ret;
808 /***********************************************************************
809 * DefineDosDeviceW (KERNEL32.@)
811 BOOL WINAPI DefineDosDeviceW( DWORD flags, LPCWSTR devname, LPCWSTR targetpath )
813 DWORD len, dosdev;
814 BOOL ret = FALSE;
815 char *path = NULL, *target, *p;
817 TRACE("%x, %s, %s\n", flags, debugstr_w(devname), debugstr_w(targetpath));
819 if (!(flags & DDD_REMOVE_DEFINITION))
821 if (!(flags & DDD_RAW_TARGET_PATH))
823 FIXME( "(0x%08x,%s,%s) DDD_RAW_TARGET_PATH flag not set, not supported yet\n",
824 flags, debugstr_w(devname), debugstr_w(targetpath) );
825 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
826 return FALSE;
829 len = WideCharToMultiByte( CP_UNIXCP, 0, targetpath, -1, NULL, 0, NULL, NULL );
830 if ((target = HeapAlloc( GetProcessHeap(), 0, len )))
832 WideCharToMultiByte( CP_UNIXCP, 0, targetpath, -1, target, len, NULL, NULL );
833 for (p = target; *p; p++) if (*p == '\\') *p = '/';
835 else
837 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
838 return FALSE;
841 else target = NULL;
843 /* first check for a DOS device */
845 if ((dosdev = RtlIsDosDeviceName_U( devname )))
847 WCHAR name[5];
849 memcpy( name, devname + HIWORD(dosdev)/sizeof(WCHAR), LOWORD(dosdev) );
850 name[LOWORD(dosdev)/sizeof(WCHAR)] = 0;
851 path = get_dos_device_path( name );
853 else if (isalphaW(devname[0]) && devname[1] == ':' && !devname[2]) /* drive mapping */
855 path = get_dos_device_path( devname );
857 else SetLastError( ERROR_FILE_NOT_FOUND );
859 if (path)
861 if (target)
863 TRACE( "creating symlink %s -> %s\n", path, target );
864 unlink( path );
865 if (!symlink( target, path )) ret = TRUE;
866 else FILE_SetDosError();
868 else
870 TRACE( "removing symlink %s\n", path );
871 if (!unlink( path )) ret = TRUE;
872 else FILE_SetDosError();
874 HeapFree( GetProcessHeap(), 0, path );
876 HeapFree( GetProcessHeap(), 0, target );
877 return ret;
881 /***********************************************************************
882 * DefineDosDeviceA (KERNEL32.@)
884 BOOL WINAPI DefineDosDeviceA(DWORD flags, LPCSTR devname, LPCSTR targetpath)
886 WCHAR *devW, *targetW = NULL;
887 BOOL ret;
889 if (!(devW = FILE_name_AtoW( devname, FALSE ))) return FALSE;
890 if (targetpath && !(targetW = FILE_name_AtoW( targetpath, TRUE ))) return FALSE;
891 ret = DefineDosDeviceW(flags, devW, targetW);
892 HeapFree( GetProcessHeap(), 0, targetW );
893 return ret;
897 /***********************************************************************
898 * QueryDosDeviceW (KERNEL32.@)
900 * returns array of strings terminated by \0, terminated by \0
902 DWORD WINAPI QueryDosDeviceW( LPCWSTR devname, LPWSTR target, DWORD bufsize )
904 static const WCHAR auxW[] = {'A','U','X',0};
905 static const WCHAR nulW[] = {'N','U','L',0};
906 static const WCHAR prnW[] = {'P','R','N',0};
907 static const WCHAR comW[] = {'C','O','M',0};
908 static const WCHAR lptW[] = {'L','P','T',0};
909 static const WCHAR rootW[] = {'A',':','\\',0};
910 static const WCHAR com0W[] = {'\\','?','?','\\','C','O','M','0',0};
911 static const WCHAR com1W[] = {'\\','D','o','s','D','e','v','i','c','e','s','\\','C','O','M','1',0,0};
912 static const WCHAR lpt1W[] = {'\\','D','o','s','D','e','v','i','c','e','s','\\','L','P','T','1',0,0};
913 static const WCHAR driveW[] = {'\\','D','o','s','D','e','v','i','c','e','s','\\','A',':',0};
915 UNICODE_STRING nt_name;
916 ANSI_STRING unix_name;
917 WCHAR nt_buffer[10];
918 NTSTATUS status;
920 if (!bufsize)
922 SetLastError( ERROR_INSUFFICIENT_BUFFER );
923 return 0;
926 if (devname)
928 WCHAR *p, name[5];
929 char *path, *link;
930 DWORD dosdev, ret = 0;
932 if ((dosdev = RtlIsDosDeviceName_U( devname )))
934 memcpy( name, devname + HIWORD(dosdev)/sizeof(WCHAR), LOWORD(dosdev) );
935 name[LOWORD(dosdev)/sizeof(WCHAR)] = 0;
937 else if (devname[0] && devname[1] == ':' && !devname[2])
939 /* FIXME: should do this for all devices, not just drives */
940 NTSTATUS status;
941 WCHAR buffer[sizeof(driveW)/sizeof(WCHAR)];
943 memcpy( buffer, driveW, sizeof(driveW) );
944 buffer[12] = devname[0];
945 if ((status = read_nt_symlink( buffer, target, bufsize )))
947 SetLastError( RtlNtStatusToDosError(status) );
948 return 0;
950 ret = strlenW( target ) + 1;
951 goto done;
953 else
955 SetLastError( ERROR_BAD_PATHNAME );
956 return 0;
959 if (!(path = get_dos_device_path( name ))) return 0;
960 link = read_symlink( path );
961 HeapFree( GetProcessHeap(), 0, path );
963 if (link)
965 ret = MultiByteToWideChar( CP_UNIXCP, 0, link, -1, target, bufsize );
966 HeapFree( GetProcessHeap(), 0, link );
968 else if (dosdev) /* look for device defaults */
970 if (!strcmpiW( name, auxW ))
972 if (bufsize >= sizeof(com1W)/sizeof(WCHAR))
974 memcpy( target, com1W, sizeof(com1W) );
975 ret = sizeof(com1W)/sizeof(WCHAR);
977 else SetLastError( ERROR_INSUFFICIENT_BUFFER );
978 return ret;
980 if (!strcmpiW( name, prnW ))
982 if (bufsize >= sizeof(lpt1W)/sizeof(WCHAR))
984 memcpy( target, lpt1W, sizeof(lpt1W) );
985 ret = sizeof(lpt1W)/sizeof(WCHAR);
987 else SetLastError( ERROR_INSUFFICIENT_BUFFER );
988 return ret;
991 nt_buffer[0] = '\\';
992 nt_buffer[1] = '?';
993 nt_buffer[2] = '?';
994 nt_buffer[3] = '\\';
995 strcpyW( nt_buffer + 4, name );
996 RtlInitUnicodeString( &nt_name, nt_buffer );
997 status = wine_nt_to_unix_file_name( &nt_name, &unix_name, FILE_OPEN, TRUE );
998 if (status) SetLastError( RtlNtStatusToDosError(status) );
999 else
1001 ret = MultiByteToWideChar( CP_UNIXCP, 0, unix_name.Buffer, -1, target, bufsize );
1002 RtlFreeAnsiString( &unix_name );
1005 done:
1006 if (ret)
1008 if (ret < bufsize) target[ret++] = 0; /* add an extra null */
1009 for (p = target; *p; p++) if (*p == '/') *p = '\\';
1012 return ret;
1014 else /* return a list of all devices */
1016 WCHAR *p = target;
1017 int i;
1019 if (bufsize <= (sizeof(auxW)+sizeof(nulW)+sizeof(prnW))/sizeof(WCHAR))
1021 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1022 return 0;
1025 memcpy( p, auxW, sizeof(auxW) );
1026 p += sizeof(auxW) / sizeof(WCHAR);
1027 memcpy( p, nulW, sizeof(nulW) );
1028 p += sizeof(nulW) / sizeof(WCHAR);
1029 memcpy( p, prnW, sizeof(prnW) );
1030 p += sizeof(prnW) / sizeof(WCHAR);
1032 strcpyW( nt_buffer, com0W );
1033 RtlInitUnicodeString( &nt_name, nt_buffer );
1035 for (i = 1; i <= 9; i++)
1037 nt_buffer[7] = '0' + i;
1038 if (!wine_nt_to_unix_file_name( &nt_name, &unix_name, FILE_OPEN, TRUE ))
1040 RtlFreeAnsiString( &unix_name );
1041 if (p + 5 >= target + bufsize)
1043 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1044 return 0;
1046 strcpyW( p, comW );
1047 p[3] = '0' + i;
1048 p[4] = 0;
1049 p += 5;
1052 strcpyW( nt_buffer + 4, lptW );
1053 for (i = 1; i <= 9; i++)
1055 nt_buffer[7] = '0' + i;
1056 if (!wine_nt_to_unix_file_name( &nt_name, &unix_name, FILE_OPEN, TRUE ))
1058 RtlFreeAnsiString( &unix_name );
1059 if (p + 5 >= target + bufsize)
1061 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1062 return 0;
1064 strcpyW( p, lptW );
1065 p[3] = '0' + i;
1066 p[4] = 0;
1067 p += 5;
1071 strcpyW( nt_buffer + 4, rootW );
1072 RtlInitUnicodeString( &nt_name, nt_buffer );
1074 /* FIXME: should simply enumerate the DosDevices directory instead */
1075 for (i = 0; i < 26; i++)
1077 WCHAR buffer[sizeof(driveW)/sizeof(WCHAR)], dummy[8];
1078 NTSTATUS status;
1080 memcpy( buffer, driveW, sizeof(driveW) );
1081 buffer[12] = 'A' + i;
1082 status = read_nt_symlink( buffer, dummy, sizeof(dummy)/sizeof(WCHAR) );
1083 if (status == STATUS_SUCCESS || status == STATUS_BUFFER_TOO_SMALL)
1085 if (p + 3 >= target + bufsize)
1087 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1088 return 0;
1090 *p++ = 'A' + i;
1091 *p++ = ':';
1092 *p++ = 0;
1095 *p++ = 0; /* terminating null */
1096 return p - target;
1101 /***********************************************************************
1102 * QueryDosDeviceA (KERNEL32.@)
1104 * returns array of strings terminated by \0, terminated by \0
1106 DWORD WINAPI QueryDosDeviceA( LPCSTR devname, LPSTR target, DWORD bufsize )
1108 DWORD ret = 0, retW;
1109 WCHAR *devnameW = NULL;
1110 LPWSTR targetW;
1112 if (devname && !(devnameW = FILE_name_AtoW( devname, FALSE ))) return 0;
1114 targetW = HeapAlloc( GetProcessHeap(),0, bufsize * sizeof(WCHAR) );
1115 if (!targetW)
1117 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1118 return 0;
1121 retW = QueryDosDeviceW(devnameW, targetW, bufsize);
1123 ret = FILE_name_WtoA( targetW, retW, target, bufsize );
1125 HeapFree(GetProcessHeap(), 0, targetW);
1126 return ret;
1130 /***********************************************************************
1131 * GetLogicalDrives (KERNEL32.@)
1133 DWORD WINAPI GetLogicalDrives(void)
1135 const char *config_dir = wine_get_config_dir();
1136 struct stat st;
1137 char *buffer, *dev;
1138 DWORD ret = 0;
1139 int i;
1141 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, strlen(config_dir) + sizeof("/dosdevices/a:") )))
1143 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1144 return 0;
1146 strcpy( buffer, config_dir );
1147 strcat( buffer, "/dosdevices/a:" );
1148 dev = buffer + strlen(buffer) - 2;
1150 for (i = 0; i < 26; i++)
1152 *dev = 'a' + i;
1153 if (!stat( buffer, &st )) ret |= (1 << i);
1155 HeapFree( GetProcessHeap(), 0, buffer );
1156 return ret;
1160 /***********************************************************************
1161 * GetLogicalDriveStringsA (KERNEL32.@)
1163 UINT WINAPI GetLogicalDriveStringsA( UINT len, LPSTR buffer )
1165 DWORD drives = GetLogicalDrives();
1166 UINT drive, count;
1168 for (drive = count = 0; drive < 26; drive++) if (drives & (1 << drive)) count++;
1169 if ((count * 4) + 1 > len) return count * 4 + 1;
1171 for (drive = 0; drive < 26; drive++)
1173 if (drives & (1 << drive))
1175 *buffer++ = 'A' + drive;
1176 *buffer++ = ':';
1177 *buffer++ = '\\';
1178 *buffer++ = 0;
1181 *buffer = 0;
1182 return count * 4;
1186 /***********************************************************************
1187 * GetLogicalDriveStringsW (KERNEL32.@)
1189 UINT WINAPI GetLogicalDriveStringsW( UINT len, LPWSTR buffer )
1191 DWORD drives = GetLogicalDrives();
1192 UINT drive, count;
1194 for (drive = count = 0; drive < 26; drive++) if (drives & (1 << drive)) count++;
1195 if ((count * 4) + 1 > len) return count * 4 + 1;
1197 for (drive = 0; drive < 26; drive++)
1199 if (drives & (1 << drive))
1201 *buffer++ = 'A' + drive;
1202 *buffer++ = ':';
1203 *buffer++ = '\\';
1204 *buffer++ = 0;
1207 *buffer = 0;
1208 return count * 4;
1212 /***********************************************************************
1213 * GetDriveTypeW (KERNEL32.@)
1215 * Returns the type of the disk drive specified. If root is NULL the
1216 * root of the current directory is used.
1218 * RETURNS
1220 * Type of drive (from Win32 SDK):
1222 * DRIVE_UNKNOWN unable to find out anything about the drive
1223 * DRIVE_NO_ROOT_DIR nonexistent root dir
1224 * DRIVE_REMOVABLE the disk can be removed from the machine
1225 * DRIVE_FIXED the disk cannot be removed from the machine
1226 * DRIVE_REMOTE network disk
1227 * DRIVE_CDROM CDROM drive
1228 * DRIVE_RAMDISK virtual disk in RAM
1230 UINT WINAPI GetDriveTypeW(LPCWSTR root) /* [in] String describing drive */
1232 FILE_FS_DEVICE_INFORMATION info;
1233 IO_STATUS_BLOCK io;
1234 NTSTATUS status;
1235 HANDLE handle;
1236 UINT ret;
1238 if (!open_device_root( root, &handle )) return DRIVE_NO_ROOT_DIR;
1240 status = NtQueryVolumeInformationFile( handle, &io, &info, sizeof(info), FileFsDeviceInformation );
1241 NtClose( handle );
1242 if (status != STATUS_SUCCESS)
1244 SetLastError( RtlNtStatusToDosError(status) );
1245 ret = DRIVE_UNKNOWN;
1247 else
1249 switch (info.DeviceType)
1251 case FILE_DEVICE_CD_ROM_FILE_SYSTEM: ret = DRIVE_CDROM; break;
1252 case FILE_DEVICE_VIRTUAL_DISK: ret = DRIVE_RAMDISK; break;
1253 case FILE_DEVICE_NETWORK_FILE_SYSTEM: ret = DRIVE_REMOTE; break;
1254 case FILE_DEVICE_DISK_FILE_SYSTEM:
1255 if (info.Characteristics & FILE_REMOTE_DEVICE) ret = DRIVE_REMOTE;
1256 else if (info.Characteristics & FILE_REMOVABLE_MEDIA) ret = DRIVE_REMOVABLE;
1257 else if ((ret = get_mountmgr_drive_type( root )) == DRIVE_UNKNOWN) ret = DRIVE_FIXED;
1258 break;
1259 default:
1260 ret = DRIVE_UNKNOWN;
1261 break;
1264 TRACE( "%s -> %d\n", debugstr_w(root), ret );
1265 return ret;
1269 /***********************************************************************
1270 * GetDriveTypeA (KERNEL32.@)
1272 * See GetDriveTypeW.
1274 UINT WINAPI GetDriveTypeA( LPCSTR root )
1276 WCHAR *rootW = NULL;
1278 if (root && !(rootW = FILE_name_AtoW( root, FALSE ))) return DRIVE_NO_ROOT_DIR;
1279 return GetDriveTypeW( rootW );
1283 /***********************************************************************
1284 * GetDiskFreeSpaceExW (KERNEL32.@)
1286 * This function is used to acquire the size of the available and
1287 * total space on a logical volume.
1289 * RETURNS
1291 * Zero on failure, nonzero upon success. Use GetLastError to obtain
1292 * detailed error information.
1295 BOOL WINAPI GetDiskFreeSpaceExW( LPCWSTR root, PULARGE_INTEGER avail,
1296 PULARGE_INTEGER total, PULARGE_INTEGER totalfree )
1298 FILE_FS_SIZE_INFORMATION info;
1299 IO_STATUS_BLOCK io;
1300 NTSTATUS status;
1301 HANDLE handle;
1302 UINT units;
1304 TRACE( "%s,%p,%p,%p\n", debugstr_w(root), avail, total, totalfree );
1306 if (!open_device_root( root, &handle )) return FALSE;
1308 status = NtQueryVolumeInformationFile( handle, &io, &info, sizeof(info), FileFsSizeInformation );
1309 NtClose( handle );
1310 if (status != STATUS_SUCCESS)
1312 SetLastError( RtlNtStatusToDosError(status) );
1313 return FALSE;
1316 units = info.SectorsPerAllocationUnit * info.BytesPerSector;
1317 if (total) total->QuadPart = info.TotalAllocationUnits.QuadPart * units;
1318 if (totalfree) totalfree->QuadPart = info.AvailableAllocationUnits.QuadPart * units;
1319 /* FIXME: this one should take quotas into account */
1320 if (avail) avail->QuadPart = info.AvailableAllocationUnits.QuadPart * units;
1321 return TRUE;
1325 /***********************************************************************
1326 * GetDiskFreeSpaceExA (KERNEL32.@)
1328 * See GetDiskFreeSpaceExW.
1330 BOOL WINAPI GetDiskFreeSpaceExA( LPCSTR root, PULARGE_INTEGER avail,
1331 PULARGE_INTEGER total, PULARGE_INTEGER totalfree )
1333 WCHAR *rootW = NULL;
1335 if (root && !(rootW = FILE_name_AtoW( root, FALSE ))) return FALSE;
1336 return GetDiskFreeSpaceExW( rootW, avail, total, totalfree );
1340 /***********************************************************************
1341 * GetDiskFreeSpaceW (KERNEL32.@)
1343 BOOL WINAPI GetDiskFreeSpaceW( LPCWSTR root, LPDWORD cluster_sectors,
1344 LPDWORD sector_bytes, LPDWORD free_clusters,
1345 LPDWORD total_clusters )
1347 FILE_FS_SIZE_INFORMATION info;
1348 IO_STATUS_BLOCK io;
1349 NTSTATUS status;
1350 HANDLE handle;
1351 UINT units;
1353 TRACE( "%s,%p,%p,%p,%p\n", debugstr_w(root),
1354 cluster_sectors, sector_bytes, free_clusters, total_clusters );
1356 if (!open_device_root( root, &handle )) return FALSE;
1358 status = NtQueryVolumeInformationFile( handle, &io, &info, sizeof(info), FileFsSizeInformation );
1359 NtClose( handle );
1360 if (status != STATUS_SUCCESS)
1362 SetLastError( RtlNtStatusToDosError(status) );
1363 return FALSE;
1366 units = info.SectorsPerAllocationUnit * info.BytesPerSector;
1368 if( GetVersion() & 0x80000000) { /* win3.x, 9x, ME */
1369 /* cap the size and available at 2GB as per specs */
1370 if (info.TotalAllocationUnits.QuadPart * units > 0x7fffffff) {
1371 info.TotalAllocationUnits.QuadPart = 0x7fffffff / units;
1372 if (info.AvailableAllocationUnits.QuadPart * units > 0x7fffffff)
1373 info.AvailableAllocationUnits.QuadPart = 0x7fffffff / units;
1375 /* nr. of clusters is always <= 65335 */
1376 while( info.TotalAllocationUnits.QuadPart > 65535 ) {
1377 info.TotalAllocationUnits.QuadPart /= 2;
1378 info.AvailableAllocationUnits.QuadPart /= 2;
1379 info.SectorsPerAllocationUnit *= 2;
1383 if (cluster_sectors) *cluster_sectors = info.SectorsPerAllocationUnit;
1384 if (sector_bytes) *sector_bytes = info.BytesPerSector;
1385 if (free_clusters) *free_clusters = info.AvailableAllocationUnits.u.LowPart;
1386 if (total_clusters) *total_clusters = info.TotalAllocationUnits.u.LowPart;
1387 return TRUE;
1391 /***********************************************************************
1392 * GetDiskFreeSpaceA (KERNEL32.@)
1394 BOOL WINAPI GetDiskFreeSpaceA( LPCSTR root, LPDWORD cluster_sectors,
1395 LPDWORD sector_bytes, LPDWORD free_clusters,
1396 LPDWORD total_clusters )
1398 WCHAR *rootW = NULL;
1400 if (root && !(rootW = FILE_name_AtoW( root, FALSE ))) return FALSE;
1401 return GetDiskFreeSpaceW( rootW, cluster_sectors, sector_bytes, free_clusters, total_clusters );
1404 /***********************************************************************
1405 * GetVolumePathNameA (KERNEL32.@)
1407 BOOL WINAPI GetVolumePathNameA(LPCSTR filename, LPSTR volumepathname, DWORD buflen)
1409 FIXME("(%s, %p, %d), stub!\n", debugstr_a(filename), volumepathname, buflen);
1410 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1411 return FALSE;
1414 /***********************************************************************
1415 * GetVolumePathNameW (KERNEL32.@)
1417 BOOL WINAPI GetVolumePathNameW(LPCWSTR filename, LPWSTR volumepathname, DWORD buflen)
1419 FIXME("(%s, %p, %d), stub!\n", debugstr_w(filename), volumepathname, buflen);
1420 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1421 return FALSE;
1424 /***********************************************************************
1425 * FindFirstVolumeA (KERNEL32.@)
1427 HANDLE WINAPI FindFirstVolumeA(LPSTR volume, DWORD len)
1429 WCHAR *buffer = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1430 HANDLE handle = FindFirstVolumeW( buffer, len );
1432 if (handle != INVALID_HANDLE_VALUE)
1434 if (!WideCharToMultiByte( CP_ACP, 0, buffer, -1, volume, len, NULL, NULL ))
1436 FindVolumeClose( handle );
1437 handle = INVALID_HANDLE_VALUE;
1440 HeapFree( GetProcessHeap(), 0, buffer );
1441 return handle;
1444 /***********************************************************************
1445 * FindFirstVolumeW (KERNEL32.@)
1447 HANDLE WINAPI FindFirstVolumeW( LPWSTR volume, DWORD len )
1449 DWORD size = 1024;
1450 HANDLE mgr = CreateFileW( MOUNTMGR_DOS_DEVICE_NAME, 0, FILE_SHARE_READ|FILE_SHARE_WRITE,
1451 NULL, OPEN_EXISTING, 0, 0 );
1452 if (mgr == INVALID_HANDLE_VALUE) return INVALID_HANDLE_VALUE;
1454 for (;;)
1456 MOUNTMGR_MOUNT_POINT input;
1457 MOUNTMGR_MOUNT_POINTS *output;
1459 if (!(output = HeapAlloc( GetProcessHeap(), 0, size )))
1461 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1462 break;
1464 memset( &input, 0, sizeof(input) );
1466 if (!DeviceIoControl( mgr, IOCTL_MOUNTMGR_QUERY_POINTS, &input, sizeof(input),
1467 output, size, NULL, NULL ))
1469 if (GetLastError() != ERROR_MORE_DATA) break;
1470 size = output->Size;
1471 HeapFree( GetProcessHeap(), 0, output );
1472 continue;
1474 CloseHandle( mgr );
1475 /* abuse the Size field to store the current index */
1476 output->Size = 0;
1477 if (!FindNextVolumeW( output, volume, len ))
1479 HeapFree( GetProcessHeap(), 0, output );
1480 return INVALID_HANDLE_VALUE;
1482 return (HANDLE)output;
1484 CloseHandle( mgr );
1485 return INVALID_HANDLE_VALUE;
1488 /***********************************************************************
1489 * FindNextVolumeA (KERNEL32.@)
1491 BOOL WINAPI FindNextVolumeA( HANDLE handle, LPSTR volume, DWORD len )
1493 WCHAR *buffer = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1494 BOOL ret;
1496 if ((ret = FindNextVolumeW( handle, buffer, len )))
1498 if (!WideCharToMultiByte( CP_ACP, 0, buffer, -1, volume, len, NULL, NULL )) ret = FALSE;
1500 HeapFree( GetProcessHeap(), 0, buffer );
1501 return ret;
1504 /***********************************************************************
1505 * FindNextVolumeW (KERNEL32.@)
1507 BOOL WINAPI FindNextVolumeW( HANDLE handle, LPWSTR volume, DWORD len )
1509 MOUNTMGR_MOUNT_POINTS *data = handle;
1511 while (data->Size < data->NumberOfMountPoints)
1513 static const WCHAR volumeW[] = {'\\','?','?','\\','V','o','l','u','m','e','{',};
1514 WCHAR *link = (WCHAR *)((char *)data + data->MountPoints[data->Size].SymbolicLinkNameOffset);
1515 DWORD size = data->MountPoints[data->Size].SymbolicLinkNameLength;
1516 data->Size++;
1517 /* skip non-volumes */
1518 if (size < sizeof(volumeW) || memcmp( link, volumeW, sizeof(volumeW) )) continue;
1519 if (size + sizeof(WCHAR) >= len * sizeof(WCHAR))
1521 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1522 return FALSE;
1524 memcpy( volume, link, size );
1525 volume[1] = '\\'; /* map \??\ to \\?\ */
1526 volume[size / sizeof(WCHAR)] = '\\'; /* Windows appends a backslash */
1527 volume[size / sizeof(WCHAR) + 1] = 0;
1528 TRACE( "returning entry %u %s\n", data->Size - 1, debugstr_w(volume) );
1529 return TRUE;
1531 SetLastError( ERROR_NO_MORE_FILES );
1532 return FALSE;
1535 /***********************************************************************
1536 * FindVolumeClose (KERNEL32.@)
1538 BOOL WINAPI FindVolumeClose(HANDLE handle)
1540 return HeapFree( GetProcessHeap(), 0, handle );
1543 /***********************************************************************
1544 * FindFirstVolumeMountPointA (KERNEL32.@)
1546 HANDLE WINAPI FindFirstVolumeMountPointA(LPCSTR root, LPSTR mount_point, DWORD len)
1548 FIXME("(%s, %p, %d), stub!\n", debugstr_a(root), mount_point, len);
1549 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1550 return INVALID_HANDLE_VALUE;
1553 /***********************************************************************
1554 * FindFirstVolumeMountPointW (KERNEL32.@)
1556 HANDLE WINAPI FindFirstVolumeMountPointW(LPCWSTR root, LPWSTR mount_point, DWORD len)
1558 FIXME("(%s, %p, %d), stub!\n", debugstr_w(root), mount_point, len);
1559 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1560 return INVALID_HANDLE_VALUE;
1563 /***********************************************************************
1564 * FindVolumeMountPointClose (KERNEL32.@)
1566 BOOL WINAPI FindVolumeMountPointClose(HANDLE h)
1568 FIXME("(%p), stub!\n", h);
1569 return FALSE;