kernel32: Use the NT name to open the root directory in GetVolumeInformationW.
[wine/wine-gecko.git] / dlls / kernel32 / volume.c
blob0a3cc6aefeb6fe06cb4691b25d961131278be450
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};
516 static const WCHAR default_rootW[] = {'\\',0};
518 WCHAR device[] = {'\\','\\','.','\\','A',':',0};
519 HANDLE handle;
520 NTSTATUS status;
521 UNICODE_STRING nt_name;
522 IO_STATUS_BLOCK io;
523 OBJECT_ATTRIBUTES attr;
524 FILE_FS_DEVICE_INFORMATION info;
525 WCHAR *p;
526 enum fs_type type = FS_UNKNOWN;
527 BOOL ret = FALSE;
529 if (!root) root = default_rootW;
530 if (!RtlDosPathNameToNtPathName_U( root, &nt_name, NULL, NULL ))
532 SetLastError( ERROR_PATH_NOT_FOUND );
533 return FALSE;
535 /* there must be exactly one backslash in the name, at the end */
536 p = memchrW( nt_name.Buffer + 4, '\\', (nt_name.Length - 4) / sizeof(WCHAR) );
537 if (p != nt_name.Buffer + nt_name.Length / sizeof(WCHAR) - 1)
539 SetLastError( ERROR_INVALID_NAME );
540 goto done;
542 device[4] = nt_name.Buffer[4];
544 /* try to open the device */
546 attr.Length = sizeof(attr);
547 attr.RootDirectory = 0;
548 attr.Attributes = OBJ_CASE_INSENSITIVE;
549 attr.ObjectName = &nt_name;
550 attr.SecurityDescriptor = NULL;
551 attr.SecurityQualityOfService = NULL;
553 nt_name.Length -= sizeof(WCHAR); /* without trailing slash */
554 status = NtOpenFile( &handle, GENERIC_READ, &attr, &io, FILE_SHARE_READ | FILE_SHARE_WRITE,
555 FILE_NON_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT );
556 nt_name.Length += sizeof(WCHAR);
558 if (status == STATUS_SUCCESS)
560 BYTE superblock[SUPERBLOCK_SIZE];
561 CDROM_TOC toc;
562 DWORD br;
564 /* check for audio CD */
565 /* FIXME: we only check the first track for now */
566 if (DeviceIoControl( handle, IOCTL_CDROM_READ_TOC, NULL, 0, &toc, sizeof(toc), &br, 0 ))
568 if (!(toc.TrackData[0].Control & 0x04)) /* audio track */
570 TRACE( "%s: found audio CD\n", debugstr_w(nt_name.Buffer) );
571 if (label) lstrcpynW( label, audiocdW, label_len );
572 if (serial) *serial = VOLUME_GetAudioCDSerial( &toc );
573 CloseHandle( handle );
574 type = FS_ISO9660;
575 goto fill_fs_info;
577 type = VOLUME_ReadCDSuperblock( handle, superblock );
579 else
581 type = VOLUME_ReadFATSuperblock( handle, superblock );
582 if (type == FS_UNKNOWN) type = VOLUME_ReadCDSuperblock( handle, superblock );
584 CloseHandle( handle );
585 TRACE( "%s: found fs type %d\n", debugstr_w(nt_name.Buffer), type );
586 if (type == FS_ERROR) goto done;
588 if (label && label_len) VOLUME_GetSuperblockLabel( device, type, superblock, label, label_len );
589 if (serial) *serial = VOLUME_GetSuperblockSerial( device, type, superblock );
590 goto fill_fs_info;
592 else TRACE( "cannot open device %s: err %d\n", debugstr_w(nt_name.Buffer), GetLastError() );
594 /* we couldn't open the device, fallback to default strategy */
596 status = NtOpenFile( &handle, 0, &attr, &io, 0, FILE_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT );
597 if (status != STATUS_SUCCESS)
599 SetLastError( RtlNtStatusToDosError(status) );
600 goto done;
602 status = NtQueryVolumeInformationFile( handle, &io, &info, sizeof(info), FileFsDeviceInformation );
603 NtClose( handle );
604 if (status != STATUS_SUCCESS)
606 SetLastError( RtlNtStatusToDosError(status) );
607 goto done;
609 if (info.DeviceType == FILE_DEVICE_CD_ROM_FILE_SYSTEM) type = FS_ISO9660;
611 if (label && label_len) get_filesystem_label( device, label, label_len );
612 if (serial) *serial = get_filesystem_serial( device );
614 fill_fs_info: /* now fill in the information that depends on the file system type */
616 switch(type)
618 case FS_ISO9660:
619 if (fsname) lstrcpynW( fsname, cdfsW, fsname_len );
620 if (filename_len) *filename_len = 221;
621 if (flags) *flags = FILE_READ_ONLY_VOLUME;
622 break;
623 case FS_FAT1216:
624 if (fsname) lstrcpynW( fsname, fatW, fsname_len );
625 case FS_FAT32:
626 if (type == FS_FAT32 && fsname) lstrcpynW( fsname, fat32W, fsname_len );
627 if (filename_len) *filename_len = 255;
628 if (flags) *flags = FILE_CASE_PRESERVED_NAMES; /* FIXME */
629 break;
630 default:
631 if (fsname) lstrcpynW( fsname, ntfsW, fsname_len );
632 if (filename_len) *filename_len = 255;
633 if (flags) *flags = FILE_CASE_PRESERVED_NAMES;
634 break;
636 ret = TRUE;
638 done:
639 RtlFreeUnicodeString( &nt_name );
640 return ret;
644 /***********************************************************************
645 * GetVolumeInformationA (KERNEL32.@)
647 BOOL WINAPI GetVolumeInformationA( LPCSTR root, LPSTR label,
648 DWORD label_len, DWORD *serial,
649 DWORD *filename_len, DWORD *flags,
650 LPSTR fsname, DWORD fsname_len )
652 WCHAR *rootW = NULL;
653 LPWSTR labelW, fsnameW;
654 BOOL ret;
656 if (root && !(rootW = FILE_name_AtoW( root, FALSE ))) return FALSE;
658 labelW = label ? HeapAlloc(GetProcessHeap(), 0, label_len * sizeof(WCHAR)) : NULL;
659 fsnameW = fsname ? HeapAlloc(GetProcessHeap(), 0, fsname_len * sizeof(WCHAR)) : NULL;
661 if ((ret = GetVolumeInformationW(rootW, labelW, label_len, serial,
662 filename_len, flags, fsnameW, fsname_len)))
664 if (label) FILE_name_WtoA( labelW, -1, label, label_len );
665 if (fsname) FILE_name_WtoA( fsnameW, -1, fsname, fsname_len );
668 HeapFree( GetProcessHeap(), 0, labelW );
669 HeapFree( GetProcessHeap(), 0, fsnameW );
670 return ret;
675 /***********************************************************************
676 * SetVolumeLabelW (KERNEL32.@)
678 BOOL WINAPI SetVolumeLabelW( LPCWSTR root, LPCWSTR label )
680 WCHAR device[] = {'\\','\\','.','\\','A',':',0};
681 HANDLE handle;
682 enum fs_type type = FS_UNKNOWN;
684 if (!root)
686 WCHAR path[MAX_PATH];
687 GetCurrentDirectoryW( MAX_PATH, path );
688 device[4] = path[0];
690 else
692 if (!root[0] || root[1] != ':')
694 SetLastError( ERROR_INVALID_NAME );
695 return FALSE;
697 device[4] = root[0];
700 /* try to open the device */
702 handle = CreateFileW( device, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE,
703 NULL, OPEN_EXISTING, 0, 0 );
704 if (handle != INVALID_HANDLE_VALUE)
706 BYTE superblock[SUPERBLOCK_SIZE];
708 type = VOLUME_ReadFATSuperblock( handle, superblock );
709 if (type == FS_UNKNOWN) type = VOLUME_ReadCDSuperblock( handle, superblock );
710 CloseHandle( handle );
711 if (type != FS_UNKNOWN)
713 /* we can't set the label on FAT or CDROM file systems */
714 TRACE( "cannot set label on device %s type %d\n", debugstr_w(device), type );
715 SetLastError( ERROR_ACCESS_DENIED );
716 return FALSE;
719 else
721 TRACE( "cannot open device %s: err %d\n", debugstr_w(device), GetLastError() );
722 if (GetLastError() == ERROR_ACCESS_DENIED) return FALSE;
725 /* we couldn't open the device, fallback to default strategy */
727 switch(GetDriveTypeW( root ))
729 case DRIVE_UNKNOWN:
730 case DRIVE_NO_ROOT_DIR:
731 SetLastError( ERROR_NOT_READY );
732 break;
733 case DRIVE_REMOVABLE:
734 case DRIVE_FIXED:
736 WCHAR labelW[] = {'A',':','\\','.','w','i','n','d','o','w','s','-','l','a','b','e','l',0};
738 labelW[0] = device[4];
740 if (!label[0]) /* delete label file when setting an empty label */
741 return DeleteFileW( labelW ) || GetLastError() == ERROR_FILE_NOT_FOUND;
743 handle = CreateFileW( labelW, GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
744 CREATE_ALWAYS, 0, 0 );
745 if (handle != INVALID_HANDLE_VALUE)
747 char buffer[64];
748 DWORD size;
750 if (!WideCharToMultiByte( CP_UNIXCP, 0, label, -1, buffer, sizeof(buffer)-1, NULL, NULL ))
751 buffer[sizeof(buffer)-2] = 0;
752 strcat( buffer, "\n" );
753 WriteFile( handle, buffer, strlen(buffer), &size, NULL );
754 CloseHandle( handle );
755 return TRUE;
757 break;
759 case DRIVE_REMOTE:
760 case DRIVE_RAMDISK:
761 case DRIVE_CDROM:
762 SetLastError( ERROR_ACCESS_DENIED );
763 break;
765 return FALSE;
768 /***********************************************************************
769 * SetVolumeLabelA (KERNEL32.@)
771 BOOL WINAPI SetVolumeLabelA(LPCSTR root, LPCSTR volname)
773 WCHAR *rootW = NULL, *volnameW = NULL;
774 BOOL ret;
776 if (root && !(rootW = FILE_name_AtoW( root, FALSE ))) return FALSE;
777 if (volname && !(volnameW = FILE_name_AtoW( volname, TRUE ))) return FALSE;
778 ret = SetVolumeLabelW( rootW, volnameW );
779 HeapFree( GetProcessHeap(), 0, volnameW );
780 return ret;
784 /***********************************************************************
785 * GetVolumeNameForVolumeMountPointA (KERNEL32.@)
787 BOOL WINAPI GetVolumeNameForVolumeMountPointA( LPCSTR path, LPSTR volume, DWORD size )
789 BOOL ret;
790 WCHAR volumeW[50], *pathW = NULL;
791 DWORD len = min( sizeof(volumeW) / sizeof(WCHAR), size );
793 TRACE("(%s, %p, %x)\n", debugstr_a(path), volume, size);
795 if (!path || !(pathW = FILE_name_AtoW( path, TRUE )))
796 return FALSE;
798 if ((ret = GetVolumeNameForVolumeMountPointW( pathW, volumeW, len )))
799 FILE_name_WtoA( volumeW, -1, volume, len );
801 HeapFree( GetProcessHeap(), 0, pathW );
802 return ret;
805 /***********************************************************************
806 * GetVolumeNameForVolumeMountPointW (KERNEL32.@)
808 BOOL WINAPI GetVolumeNameForVolumeMountPointW( LPCWSTR path, LPWSTR volume, DWORD size )
810 static const WCHAR prefixW[] = {'\\','D','o','s','D','e','v','i','c','e','s','\\',0};
811 static const WCHAR volumeW[] = {'\\','?','?','\\','V','o','l','u','m','e','{',0};
812 static const WCHAR trailingW[] = {'\\',0};
814 MOUNTMGR_MOUNT_POINT *input = NULL, *o1;
815 MOUNTMGR_MOUNT_POINTS *output = NULL;
816 WCHAR *p;
817 char *r;
818 DWORD i, i_size = 1024, o_size = 1024;
819 WCHAR *nonpersist_name;
820 WCHAR symlink_name[MAX_PATH];
821 NTSTATUS status;
822 HANDLE mgr = INVALID_HANDLE_VALUE;
823 BOOL ret = FALSE;
825 TRACE("(%s, %p, %x)\n", debugstr_w(path), volume, size);
826 if (path[lstrlenW(path)-1] != '\\')
828 SetLastError( ERROR_INVALID_NAME );
829 return FALSE;
832 if (size < 50)
834 SetLastError( ERROR_FILENAME_EXCED_RANGE );
835 return FALSE;
837 /* if length of input is > 3 then it must be a mounted folder */
838 if (lstrlenW(path) > 3)
840 FIXME("Mounted Folders are not yet supported\n");
841 SetLastError( ERROR_NOT_A_REPARSE_POINT );
842 return FALSE;
845 mgr = CreateFileW( MOUNTMGR_DOS_DEVICE_NAME, 0, FILE_SHARE_READ,
846 NULL, OPEN_EXISTING, 0, 0 );
847 if (mgr == INVALID_HANDLE_VALUE) return FALSE;
849 if (!(input = HeapAlloc( GetProcessHeap(), 0, i_size )))
851 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
852 goto err_ret;
855 if (!(output = HeapAlloc( GetProcessHeap(), 0, o_size )))
857 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
858 goto err_ret;
861 /* construct the symlink name as "\DosDevices\C:" */
862 lstrcpyW( symlink_name, prefixW );
863 lstrcatW( symlink_name, path );
864 symlink_name[lstrlenW(symlink_name)-1] = 0;
866 /* Take the mount point and get the "nonpersistent name" */
867 /* We will then take that and get the volume name */
868 nonpersist_name = (WCHAR *)(input + 1);
869 status = read_nt_symlink( symlink_name, nonpersist_name, i_size - sizeof(*input) );
870 TRACE("read_nt_symlink got stat=%x, for %s, got <%s>\n", status,
871 debugstr_w(symlink_name), debugstr_w(nonpersist_name));
872 if (status != STATUS_SUCCESS)
874 SetLastError( ERROR_FILE_NOT_FOUND );
875 goto err_ret;
878 /* Now take the "nonpersistent name" and ask the mountmgr */
879 /* to give us all the mount points. One of them will be */
880 /* the volume name (format of \??\Volume{). */
881 memset( input, 0, sizeof(*input) ); /* clear all input parameters */
882 input->DeviceNameOffset = sizeof(*input);
883 input->DeviceNameLength = lstrlenW( nonpersist_name) * sizeof(WCHAR);
884 i_size = input->DeviceNameOffset + input->DeviceNameLength;
886 output->Size = o_size;
888 /* now get the true volume name from the mountmgr */
889 if (!DeviceIoControl( mgr, IOCTL_MOUNTMGR_QUERY_POINTS, input, i_size,
890 output, o_size, NULL, NULL ))
891 goto err_ret;
893 /* Verify and return the data, note string is not null terminated */
894 TRACE("found %d matching mount points\n", output->NumberOfMountPoints);
895 if (output->NumberOfMountPoints < 1)
897 SetLastError( ERROR_NO_VOLUME_ID );
898 goto err_ret;
900 o1 = &output->MountPoints[0];
902 /* look for the volume name in returned values */
903 for(i=0;i<output->NumberOfMountPoints;i++)
905 p = (WCHAR*)((char *)output + o1->SymbolicLinkNameOffset);
906 r = (char *)output + o1->UniqueIdOffset;
907 TRACE("found symlink=%s, unique=%s, devname=%s\n",
908 debugstr_wn(p, o1->SymbolicLinkNameLength/sizeof(WCHAR)),
909 debugstr_an(r, o1->UniqueIdLength),
910 debugstr_wn((WCHAR*)((char *)output + o1->DeviceNameOffset),
911 o1->DeviceNameLength/sizeof(WCHAR)));
913 if (!strncmpW( p, volumeW, (sizeof(volumeW)-1)/sizeof(WCHAR) ))
915 /* is there space in the return variable ?? */
916 if ((o1->SymbolicLinkNameLength/sizeof(WCHAR))+2 > size)
918 SetLastError( ERROR_FILENAME_EXCED_RANGE );
919 goto err_ret;
921 memcpy( volume, p, o1->SymbolicLinkNameLength );
922 volume[o1->SymbolicLinkNameLength / sizeof(WCHAR)] = 0;
923 lstrcatW( volume, trailingW );
924 /* change second char from '?' to '\' */
925 volume[1] = '\\';
926 ret = TRUE;
927 break;
929 o1++;
932 err_ret:
933 HeapFree( GetProcessHeap(), 0, input );
934 HeapFree( GetProcessHeap(), 0, output );
935 CloseHandle( mgr );
936 return ret;
939 /***********************************************************************
940 * DefineDosDeviceW (KERNEL32.@)
942 BOOL WINAPI DefineDosDeviceW( DWORD flags, LPCWSTR devname, LPCWSTR targetpath )
944 DWORD len, dosdev;
945 BOOL ret = FALSE;
946 char *path = NULL, *target, *p;
948 TRACE("%x, %s, %s\n", flags, debugstr_w(devname), debugstr_w(targetpath));
950 if (!(flags & DDD_REMOVE_DEFINITION))
952 if (!(flags & DDD_RAW_TARGET_PATH))
954 FIXME( "(0x%08x,%s,%s) DDD_RAW_TARGET_PATH flag not set, not supported yet\n",
955 flags, debugstr_w(devname), debugstr_w(targetpath) );
956 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
957 return FALSE;
960 len = WideCharToMultiByte( CP_UNIXCP, 0, targetpath, -1, NULL, 0, NULL, NULL );
961 if ((target = HeapAlloc( GetProcessHeap(), 0, len )))
963 WideCharToMultiByte( CP_UNIXCP, 0, targetpath, -1, target, len, NULL, NULL );
964 for (p = target; *p; p++) if (*p == '\\') *p = '/';
966 else
968 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
969 return FALSE;
972 else target = NULL;
974 /* first check for a DOS device */
976 if ((dosdev = RtlIsDosDeviceName_U( devname )))
978 WCHAR name[5];
980 memcpy( name, devname + HIWORD(dosdev)/sizeof(WCHAR), LOWORD(dosdev) );
981 name[LOWORD(dosdev)/sizeof(WCHAR)] = 0;
982 path = get_dos_device_path( name );
984 else if (isalphaW(devname[0]) && devname[1] == ':' && !devname[2]) /* drive mapping */
986 path = get_dos_device_path( devname );
988 else SetLastError( ERROR_FILE_NOT_FOUND );
990 if (path)
992 if (target)
994 TRACE( "creating symlink %s -> %s\n", path, target );
995 unlink( path );
996 if (!symlink( target, path )) ret = TRUE;
997 else FILE_SetDosError();
999 else
1001 TRACE( "removing symlink %s\n", path );
1002 if (!unlink( path )) ret = TRUE;
1003 else FILE_SetDosError();
1005 HeapFree( GetProcessHeap(), 0, path );
1007 HeapFree( GetProcessHeap(), 0, target );
1008 return ret;
1012 /***********************************************************************
1013 * DefineDosDeviceA (KERNEL32.@)
1015 BOOL WINAPI DefineDosDeviceA(DWORD flags, LPCSTR devname, LPCSTR targetpath)
1017 WCHAR *devW, *targetW = NULL;
1018 BOOL ret;
1020 if (!(devW = FILE_name_AtoW( devname, FALSE ))) return FALSE;
1021 if (targetpath && !(targetW = FILE_name_AtoW( targetpath, TRUE ))) return FALSE;
1022 ret = DefineDosDeviceW(flags, devW, targetW);
1023 HeapFree( GetProcessHeap(), 0, targetW );
1024 return ret;
1028 /***********************************************************************
1029 * QueryDosDeviceW (KERNEL32.@)
1031 * returns array of strings terminated by \0, terminated by \0
1033 DWORD WINAPI QueryDosDeviceW( LPCWSTR devname, LPWSTR target, DWORD bufsize )
1035 static const WCHAR auxW[] = {'A','U','X',0};
1036 static const WCHAR nulW[] = {'N','U','L',0};
1037 static const WCHAR prnW[] = {'P','R','N',0};
1038 static const WCHAR comW[] = {'C','O','M',0};
1039 static const WCHAR lptW[] = {'L','P','T',0};
1040 static const WCHAR com0W[] = {'\\','?','?','\\','C','O','M','0',0};
1041 static const WCHAR com1W[] = {'\\','D','o','s','D','e','v','i','c','e','s','\\','C','O','M','1',0,0};
1042 static const WCHAR lpt1W[] = {'\\','D','o','s','D','e','v','i','c','e','s','\\','L','P','T','1',0,0};
1043 static const WCHAR dosdevW[] = {'\\','D','o','s','D','e','v','i','c','e','s','\\',0};
1045 UNICODE_STRING nt_name;
1046 ANSI_STRING unix_name;
1047 WCHAR nt_buffer[10];
1048 NTSTATUS status;
1050 if (!bufsize)
1052 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1053 return 0;
1056 if (devname)
1058 WCHAR *p, name[5];
1059 char *path, *link;
1060 DWORD dosdev, ret = 0;
1062 if ((dosdev = RtlIsDosDeviceName_U( devname )))
1064 memcpy( name, devname + HIWORD(dosdev)/sizeof(WCHAR), LOWORD(dosdev) );
1065 name[LOWORD(dosdev)/sizeof(WCHAR)] = 0;
1067 else
1069 NTSTATUS status;
1070 WCHAR *buffer;
1072 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, sizeof(dosdevW) + strlenW(devname)*sizeof(WCHAR) )))
1074 SetLastError( ERROR_OUTOFMEMORY );
1075 return 0;
1077 memcpy( buffer, dosdevW, sizeof(dosdevW) );
1078 strcatW( buffer, devname );
1079 status = read_nt_symlink( buffer, target, bufsize );
1080 HeapFree( GetProcessHeap(), 0, buffer );
1081 if (status)
1083 SetLastError( RtlNtStatusToDosError(status) );
1084 return 0;
1086 ret = strlenW( target ) + 1;
1087 goto done;
1090 /* FIXME: should read NT symlink for all devices */
1092 if (!(path = get_dos_device_path( name ))) return 0;
1093 link = read_symlink( path );
1094 HeapFree( GetProcessHeap(), 0, path );
1096 if (link)
1098 ret = MultiByteToWideChar( CP_UNIXCP, 0, link, -1, target, bufsize );
1099 HeapFree( GetProcessHeap(), 0, link );
1101 else if (dosdev) /* look for device defaults */
1103 if (!strcmpiW( name, auxW ))
1105 if (bufsize >= sizeof(com1W)/sizeof(WCHAR))
1107 memcpy( target, com1W, sizeof(com1W) );
1108 ret = sizeof(com1W)/sizeof(WCHAR);
1110 else SetLastError( ERROR_INSUFFICIENT_BUFFER );
1111 return ret;
1113 if (!strcmpiW( name, prnW ))
1115 if (bufsize >= sizeof(lpt1W)/sizeof(WCHAR))
1117 memcpy( target, lpt1W, sizeof(lpt1W) );
1118 ret = sizeof(lpt1W)/sizeof(WCHAR);
1120 else SetLastError( ERROR_INSUFFICIENT_BUFFER );
1121 return ret;
1124 nt_buffer[0] = '\\';
1125 nt_buffer[1] = '?';
1126 nt_buffer[2] = '?';
1127 nt_buffer[3] = '\\';
1128 strcpyW( nt_buffer + 4, name );
1129 RtlInitUnicodeString( &nt_name, nt_buffer );
1130 status = wine_nt_to_unix_file_name( &nt_name, &unix_name, FILE_OPEN, TRUE );
1131 if (status) SetLastError( RtlNtStatusToDosError(status) );
1132 else
1134 ret = MultiByteToWideChar( CP_UNIXCP, 0, unix_name.Buffer, -1, target, bufsize );
1135 RtlFreeAnsiString( &unix_name );
1138 done:
1139 if (ret)
1141 if (ret < bufsize) target[ret++] = 0; /* add an extra null */
1142 for (p = target; *p; p++) if (*p == '/') *p = '\\';
1145 return ret;
1147 else /* return a list of all devices */
1149 OBJECT_ATTRIBUTES attr;
1150 HANDLE handle;
1151 WCHAR *p = target;
1152 int i;
1154 if (bufsize <= (sizeof(auxW)+sizeof(nulW)+sizeof(prnW))/sizeof(WCHAR))
1156 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1157 return 0;
1160 /* FIXME: these should be NT symlinks too */
1162 memcpy( p, auxW, sizeof(auxW) );
1163 p += sizeof(auxW) / sizeof(WCHAR);
1164 memcpy( p, nulW, sizeof(nulW) );
1165 p += sizeof(nulW) / sizeof(WCHAR);
1166 memcpy( p, prnW, sizeof(prnW) );
1167 p += sizeof(prnW) / sizeof(WCHAR);
1169 strcpyW( nt_buffer, com0W );
1170 RtlInitUnicodeString( &nt_name, nt_buffer );
1172 for (i = 1; i <= 9; i++)
1174 nt_buffer[7] = '0' + i;
1175 if (!wine_nt_to_unix_file_name( &nt_name, &unix_name, FILE_OPEN, TRUE ))
1177 RtlFreeAnsiString( &unix_name );
1178 if (p + 5 >= target + bufsize)
1180 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1181 return 0;
1183 strcpyW( p, comW );
1184 p[3] = '0' + i;
1185 p[4] = 0;
1186 p += 5;
1189 strcpyW( nt_buffer + 4, lptW );
1190 for (i = 1; i <= 9; i++)
1192 nt_buffer[7] = '0' + i;
1193 if (!wine_nt_to_unix_file_name( &nt_name, &unix_name, FILE_OPEN, TRUE ))
1195 RtlFreeAnsiString( &unix_name );
1196 if (p + 5 >= target + bufsize)
1198 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1199 return 0;
1201 strcpyW( p, lptW );
1202 p[3] = '0' + i;
1203 p[4] = 0;
1204 p += 5;
1208 RtlInitUnicodeString( &nt_name, dosdevW );
1209 nt_name.Length -= sizeof(WCHAR); /* without trailing slash */
1210 attr.Length = sizeof(attr);
1211 attr.RootDirectory = 0;
1212 attr.ObjectName = &nt_name;
1213 attr.Attributes = OBJ_CASE_INSENSITIVE;
1214 attr.SecurityDescriptor = NULL;
1215 attr.SecurityQualityOfService = NULL;
1216 status = NtOpenDirectoryObject( &handle, FILE_LIST_DIRECTORY, &attr );
1217 if (!status)
1219 char data[1024];
1220 DIRECTORY_BASIC_INFORMATION *info = (DIRECTORY_BASIC_INFORMATION *)data;
1221 ULONG ctx = 0, len;
1223 while (!NtQueryDirectoryObject( handle, info, sizeof(data), 1, 0, &ctx, &len ))
1225 if (p + info->ObjectName.Length/sizeof(WCHAR) + 1 >= target + bufsize)
1227 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1228 NtClose( handle );
1229 return 0;
1231 memcpy( p, info->ObjectName.Buffer, info->ObjectName.Length );
1232 p += info->ObjectName.Length/sizeof(WCHAR);
1233 *p++ = 0;
1235 NtClose( handle );
1238 *p++ = 0; /* terminating null */
1239 return p - target;
1244 /***********************************************************************
1245 * QueryDosDeviceA (KERNEL32.@)
1247 * returns array of strings terminated by \0, terminated by \0
1249 DWORD WINAPI QueryDosDeviceA( LPCSTR devname, LPSTR target, DWORD bufsize )
1251 DWORD ret = 0, retW;
1252 WCHAR *devnameW = NULL;
1253 LPWSTR targetW;
1255 if (devname && !(devnameW = FILE_name_AtoW( devname, FALSE ))) return 0;
1257 targetW = HeapAlloc( GetProcessHeap(),0, bufsize * sizeof(WCHAR) );
1258 if (!targetW)
1260 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1261 return 0;
1264 retW = QueryDosDeviceW(devnameW, targetW, bufsize);
1266 ret = FILE_name_WtoA( targetW, retW, target, bufsize );
1268 HeapFree(GetProcessHeap(), 0, targetW);
1269 return ret;
1273 /***********************************************************************
1274 * GetLogicalDrives (KERNEL32.@)
1276 DWORD WINAPI GetLogicalDrives(void)
1278 const char *config_dir = wine_get_config_dir();
1279 struct stat st;
1280 char *buffer, *dev;
1281 DWORD ret = 0;
1282 int i;
1284 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, strlen(config_dir) + sizeof("/dosdevices/a:") )))
1286 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1287 return 0;
1289 strcpy( buffer, config_dir );
1290 strcat( buffer, "/dosdevices/a:" );
1291 dev = buffer + strlen(buffer) - 2;
1293 for (i = 0; i < 26; i++)
1295 *dev = 'a' + i;
1296 if (!stat( buffer, &st )) ret |= (1 << i);
1298 HeapFree( GetProcessHeap(), 0, buffer );
1299 return ret;
1303 /***********************************************************************
1304 * GetLogicalDriveStringsA (KERNEL32.@)
1306 UINT WINAPI GetLogicalDriveStringsA( UINT len, LPSTR buffer )
1308 DWORD drives = GetLogicalDrives();
1309 UINT drive, count;
1311 for (drive = count = 0; drive < 26; drive++) if (drives & (1 << drive)) count++;
1312 if ((count * 4) + 1 > len) return count * 4 + 1;
1314 for (drive = 0; drive < 26; drive++)
1316 if (drives & (1 << drive))
1318 *buffer++ = 'A' + drive;
1319 *buffer++ = ':';
1320 *buffer++ = '\\';
1321 *buffer++ = 0;
1324 *buffer = 0;
1325 return count * 4;
1329 /***********************************************************************
1330 * GetLogicalDriveStringsW (KERNEL32.@)
1332 UINT WINAPI GetLogicalDriveStringsW( UINT len, LPWSTR buffer )
1334 DWORD drives = GetLogicalDrives();
1335 UINT drive, count;
1337 for (drive = count = 0; drive < 26; drive++) if (drives & (1 << drive)) count++;
1338 if ((count * 4) + 1 > len) return count * 4 + 1;
1340 for (drive = 0; drive < 26; drive++)
1342 if (drives & (1 << drive))
1344 *buffer++ = 'A' + drive;
1345 *buffer++ = ':';
1346 *buffer++ = '\\';
1347 *buffer++ = 0;
1350 *buffer = 0;
1351 return count * 4;
1355 /***********************************************************************
1356 * GetDriveTypeW (KERNEL32.@)
1358 * Returns the type of the disk drive specified. If root is NULL the
1359 * root of the current directory is used.
1361 * RETURNS
1363 * Type of drive (from Win32 SDK):
1365 * DRIVE_UNKNOWN unable to find out anything about the drive
1366 * DRIVE_NO_ROOT_DIR nonexistent root dir
1367 * DRIVE_REMOVABLE the disk can be removed from the machine
1368 * DRIVE_FIXED the disk cannot be removed from the machine
1369 * DRIVE_REMOTE network disk
1370 * DRIVE_CDROM CDROM drive
1371 * DRIVE_RAMDISK virtual disk in RAM
1373 UINT WINAPI GetDriveTypeW(LPCWSTR root) /* [in] String describing drive */
1375 FILE_FS_DEVICE_INFORMATION info;
1376 IO_STATUS_BLOCK io;
1377 NTSTATUS status;
1378 HANDLE handle;
1379 UINT ret;
1381 if (!open_device_root( root, &handle )) return DRIVE_NO_ROOT_DIR;
1383 status = NtQueryVolumeInformationFile( handle, &io, &info, sizeof(info), FileFsDeviceInformation );
1384 NtClose( handle );
1385 if (status != STATUS_SUCCESS)
1387 SetLastError( RtlNtStatusToDosError(status) );
1388 ret = DRIVE_UNKNOWN;
1390 else
1392 switch (info.DeviceType)
1394 case FILE_DEVICE_CD_ROM_FILE_SYSTEM: ret = DRIVE_CDROM; break;
1395 case FILE_DEVICE_VIRTUAL_DISK: ret = DRIVE_RAMDISK; break;
1396 case FILE_DEVICE_NETWORK_FILE_SYSTEM: ret = DRIVE_REMOTE; break;
1397 case FILE_DEVICE_DISK_FILE_SYSTEM:
1398 if (info.Characteristics & FILE_REMOTE_DEVICE) ret = DRIVE_REMOTE;
1399 else if (info.Characteristics & FILE_REMOVABLE_MEDIA) ret = DRIVE_REMOVABLE;
1400 else if ((ret = get_mountmgr_drive_type( root )) == DRIVE_UNKNOWN) ret = DRIVE_FIXED;
1401 break;
1402 default:
1403 ret = DRIVE_UNKNOWN;
1404 break;
1407 TRACE( "%s -> %d\n", debugstr_w(root), ret );
1408 return ret;
1412 /***********************************************************************
1413 * GetDriveTypeA (KERNEL32.@)
1415 * See GetDriveTypeW.
1417 UINT WINAPI GetDriveTypeA( LPCSTR root )
1419 WCHAR *rootW = NULL;
1421 if (root && !(rootW = FILE_name_AtoW( root, FALSE ))) return DRIVE_NO_ROOT_DIR;
1422 return GetDriveTypeW( rootW );
1426 /***********************************************************************
1427 * GetDiskFreeSpaceExW (KERNEL32.@)
1429 * This function is used to acquire the size of the available and
1430 * total space on a logical volume.
1432 * RETURNS
1434 * Zero on failure, nonzero upon success. Use GetLastError to obtain
1435 * detailed error information.
1438 BOOL WINAPI GetDiskFreeSpaceExW( LPCWSTR root, PULARGE_INTEGER avail,
1439 PULARGE_INTEGER total, PULARGE_INTEGER totalfree )
1441 FILE_FS_SIZE_INFORMATION info;
1442 IO_STATUS_BLOCK io;
1443 NTSTATUS status;
1444 HANDLE handle;
1445 UINT units;
1447 TRACE( "%s,%p,%p,%p\n", debugstr_w(root), avail, total, totalfree );
1449 if (!open_device_root( root, &handle )) return FALSE;
1451 status = NtQueryVolumeInformationFile( handle, &io, &info, sizeof(info), FileFsSizeInformation );
1452 NtClose( handle );
1453 if (status != STATUS_SUCCESS)
1455 SetLastError( RtlNtStatusToDosError(status) );
1456 return FALSE;
1459 units = info.SectorsPerAllocationUnit * info.BytesPerSector;
1460 if (total) total->QuadPart = info.TotalAllocationUnits.QuadPart * units;
1461 if (totalfree) totalfree->QuadPart = info.AvailableAllocationUnits.QuadPart * units;
1462 /* FIXME: this one should take quotas into account */
1463 if (avail) avail->QuadPart = info.AvailableAllocationUnits.QuadPart * units;
1464 return TRUE;
1468 /***********************************************************************
1469 * GetDiskFreeSpaceExA (KERNEL32.@)
1471 * See GetDiskFreeSpaceExW.
1473 BOOL WINAPI GetDiskFreeSpaceExA( LPCSTR root, PULARGE_INTEGER avail,
1474 PULARGE_INTEGER total, PULARGE_INTEGER totalfree )
1476 WCHAR *rootW = NULL;
1478 if (root && !(rootW = FILE_name_AtoW( root, FALSE ))) return FALSE;
1479 return GetDiskFreeSpaceExW( rootW, avail, total, totalfree );
1483 /***********************************************************************
1484 * GetDiskFreeSpaceW (KERNEL32.@)
1486 BOOL WINAPI GetDiskFreeSpaceW( LPCWSTR root, LPDWORD cluster_sectors,
1487 LPDWORD sector_bytes, LPDWORD free_clusters,
1488 LPDWORD total_clusters )
1490 FILE_FS_SIZE_INFORMATION info;
1491 IO_STATUS_BLOCK io;
1492 NTSTATUS status;
1493 HANDLE handle;
1494 UINT units;
1496 TRACE( "%s,%p,%p,%p,%p\n", debugstr_w(root),
1497 cluster_sectors, sector_bytes, free_clusters, total_clusters );
1499 if (!open_device_root( root, &handle )) return FALSE;
1501 status = NtQueryVolumeInformationFile( handle, &io, &info, sizeof(info), FileFsSizeInformation );
1502 NtClose( handle );
1503 if (status != STATUS_SUCCESS)
1505 SetLastError( RtlNtStatusToDosError(status) );
1506 return FALSE;
1509 units = info.SectorsPerAllocationUnit * info.BytesPerSector;
1511 if( GetVersion() & 0x80000000) { /* win3.x, 9x, ME */
1512 /* cap the size and available at 2GB as per specs */
1513 if (info.TotalAllocationUnits.QuadPart * units > 0x7fffffff) {
1514 info.TotalAllocationUnits.QuadPart = 0x7fffffff / units;
1515 if (info.AvailableAllocationUnits.QuadPart * units > 0x7fffffff)
1516 info.AvailableAllocationUnits.QuadPart = 0x7fffffff / units;
1518 /* nr. of clusters is always <= 65335 */
1519 while( info.TotalAllocationUnits.QuadPart > 65535 ) {
1520 info.TotalAllocationUnits.QuadPart /= 2;
1521 info.AvailableAllocationUnits.QuadPart /= 2;
1522 info.SectorsPerAllocationUnit *= 2;
1526 if (cluster_sectors) *cluster_sectors = info.SectorsPerAllocationUnit;
1527 if (sector_bytes) *sector_bytes = info.BytesPerSector;
1528 if (free_clusters) *free_clusters = info.AvailableAllocationUnits.u.LowPart;
1529 if (total_clusters) *total_clusters = info.TotalAllocationUnits.u.LowPart;
1530 return TRUE;
1534 /***********************************************************************
1535 * GetDiskFreeSpaceA (KERNEL32.@)
1537 BOOL WINAPI GetDiskFreeSpaceA( LPCSTR root, LPDWORD cluster_sectors,
1538 LPDWORD sector_bytes, LPDWORD free_clusters,
1539 LPDWORD total_clusters )
1541 WCHAR *rootW = NULL;
1543 if (root && !(rootW = FILE_name_AtoW( root, FALSE ))) return FALSE;
1544 return GetDiskFreeSpaceW( rootW, cluster_sectors, sector_bytes, free_clusters, total_clusters );
1547 /***********************************************************************
1548 * GetVolumePathNameA (KERNEL32.@)
1550 BOOL WINAPI GetVolumePathNameA(LPCSTR filename, LPSTR volumepathname, DWORD buflen)
1552 BOOL ret;
1553 WCHAR *filenameW = NULL, *volumeW;
1555 FIXME("(%s, %p, %d), stub!\n", debugstr_a(filename), volumepathname, buflen);
1557 if (filename && !(filenameW = FILE_name_AtoW( filename, FALSE ))) return FALSE;
1558 if (!(volumeW = HeapAlloc( GetProcessHeap(), 0, buflen * sizeof(WCHAR) ))) return FALSE;
1560 if ((ret = GetVolumePathNameW( filenameW, volumeW, buflen )))
1561 FILE_name_WtoA( volumeW, -1, volumepathname, buflen );
1563 HeapFree( GetProcessHeap(), 0, volumeW );
1564 return ret;
1567 /***********************************************************************
1568 * GetVolumePathNameW (KERNEL32.@)
1570 BOOL WINAPI GetVolumePathNameW(LPCWSTR filename, LPWSTR volumepathname, DWORD buflen)
1572 const WCHAR *p = filename;
1574 FIXME("(%s, %p, %d), stub!\n", debugstr_w(filename), volumepathname, buflen);
1576 if (p && tolowerW(p[0]) >= 'a' && tolowerW(p[0]) <= 'z' && p[1] ==':' && p[2] == '\\' && buflen >= 4)
1578 volumepathname[0] = p[0];
1579 volumepathname[1] = ':';
1580 volumepathname[2] = '\\';
1581 volumepathname[3] = 0;
1582 return TRUE;
1584 return FALSE;
1587 /***********************************************************************
1588 * GetVolumePathNamesForVolumeNameW (KERNEL32.@)
1590 BOOL WINAPI GetVolumePathNamesForVolumeNameW(LPCWSTR volumename, LPWSTR volumepathname, DWORD buflen, PDWORD returnlen)
1592 FIXME("(%s, %p, %d, %p), stub!\n", debugstr_w(volumename), volumepathname, buflen, returnlen);
1593 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1594 return FALSE;
1597 /***********************************************************************
1598 * FindFirstVolumeA (KERNEL32.@)
1600 HANDLE WINAPI FindFirstVolumeA(LPSTR volume, DWORD len)
1602 WCHAR *buffer = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1603 HANDLE handle = FindFirstVolumeW( buffer, len );
1605 if (handle != INVALID_HANDLE_VALUE)
1607 if (!WideCharToMultiByte( CP_ACP, 0, buffer, -1, volume, len, NULL, NULL ))
1609 FindVolumeClose( handle );
1610 handle = INVALID_HANDLE_VALUE;
1613 HeapFree( GetProcessHeap(), 0, buffer );
1614 return handle;
1617 /***********************************************************************
1618 * FindFirstVolumeW (KERNEL32.@)
1620 HANDLE WINAPI FindFirstVolumeW( LPWSTR volume, DWORD len )
1622 DWORD size = 1024;
1623 HANDLE mgr = CreateFileW( MOUNTMGR_DOS_DEVICE_NAME, 0, FILE_SHARE_READ|FILE_SHARE_WRITE,
1624 NULL, OPEN_EXISTING, 0, 0 );
1625 if (mgr == INVALID_HANDLE_VALUE) return INVALID_HANDLE_VALUE;
1627 for (;;)
1629 MOUNTMGR_MOUNT_POINT input;
1630 MOUNTMGR_MOUNT_POINTS *output;
1632 if (!(output = HeapAlloc( GetProcessHeap(), 0, size )))
1634 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1635 break;
1637 memset( &input, 0, sizeof(input) );
1639 if (!DeviceIoControl( mgr, IOCTL_MOUNTMGR_QUERY_POINTS, &input, sizeof(input),
1640 output, size, NULL, NULL ))
1642 if (GetLastError() != ERROR_MORE_DATA) break;
1643 size = output->Size;
1644 HeapFree( GetProcessHeap(), 0, output );
1645 continue;
1647 CloseHandle( mgr );
1648 /* abuse the Size field to store the current index */
1649 output->Size = 0;
1650 if (!FindNextVolumeW( output, volume, len ))
1652 HeapFree( GetProcessHeap(), 0, output );
1653 return INVALID_HANDLE_VALUE;
1655 return output;
1657 CloseHandle( mgr );
1658 return INVALID_HANDLE_VALUE;
1661 /***********************************************************************
1662 * FindNextVolumeA (KERNEL32.@)
1664 BOOL WINAPI FindNextVolumeA( HANDLE handle, LPSTR volume, DWORD len )
1666 WCHAR *buffer = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1667 BOOL ret;
1669 if ((ret = FindNextVolumeW( handle, buffer, len )))
1671 if (!WideCharToMultiByte( CP_ACP, 0, buffer, -1, volume, len, NULL, NULL )) ret = FALSE;
1673 HeapFree( GetProcessHeap(), 0, buffer );
1674 return ret;
1677 /***********************************************************************
1678 * FindNextVolumeW (KERNEL32.@)
1680 BOOL WINAPI FindNextVolumeW( HANDLE handle, LPWSTR volume, DWORD len )
1682 MOUNTMGR_MOUNT_POINTS *data = handle;
1684 while (data->Size < data->NumberOfMountPoints)
1686 static const WCHAR volumeW[] = {'\\','?','?','\\','V','o','l','u','m','e','{',};
1687 WCHAR *link = (WCHAR *)((char *)data + data->MountPoints[data->Size].SymbolicLinkNameOffset);
1688 DWORD size = data->MountPoints[data->Size].SymbolicLinkNameLength;
1689 data->Size++;
1690 /* skip non-volumes */
1691 if (size < sizeof(volumeW) || memcmp( link, volumeW, sizeof(volumeW) )) continue;
1692 if (size + sizeof(WCHAR) >= len * sizeof(WCHAR))
1694 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1695 return FALSE;
1697 memcpy( volume, link, size );
1698 volume[1] = '\\'; /* map \??\ to \\?\ */
1699 volume[size / sizeof(WCHAR)] = '\\'; /* Windows appends a backslash */
1700 volume[size / sizeof(WCHAR) + 1] = 0;
1701 TRACE( "returning entry %u %s\n", data->Size - 1, debugstr_w(volume) );
1702 return TRUE;
1704 SetLastError( ERROR_NO_MORE_FILES );
1705 return FALSE;
1708 /***********************************************************************
1709 * FindVolumeClose (KERNEL32.@)
1711 BOOL WINAPI FindVolumeClose(HANDLE handle)
1713 return HeapFree( GetProcessHeap(), 0, handle );
1716 /***********************************************************************
1717 * FindFirstVolumeMountPointA (KERNEL32.@)
1719 HANDLE WINAPI FindFirstVolumeMountPointA(LPCSTR root, LPSTR mount_point, DWORD len)
1721 FIXME("(%s, %p, %d), stub!\n", debugstr_a(root), mount_point, len);
1722 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1723 return INVALID_HANDLE_VALUE;
1726 /***********************************************************************
1727 * FindFirstVolumeMountPointW (KERNEL32.@)
1729 HANDLE WINAPI FindFirstVolumeMountPointW(LPCWSTR root, LPWSTR mount_point, DWORD len)
1731 FIXME("(%s, %p, %d), stub!\n", debugstr_w(root), mount_point, len);
1732 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1733 return INVALID_HANDLE_VALUE;
1736 /***********************************************************************
1737 * FindVolumeMountPointClose (KERNEL32.@)
1739 BOOL WINAPI FindVolumeMountPointClose(HANDLE h)
1741 FIXME("(%p), stub!\n", h);
1742 return FALSE;