regedit: Make key names editable also with native comctl32.
[wine/multimedia.git] / dlls / kernel32 / volume.c
blob23bcca151b5494d9f4a6ec27c284f123fac154bb
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 BLOCK_SIZE 2048
50 #define SUPERBLOCK_SIZE BLOCK_SIZE
51 #define SYMBOLIC_LINK_QUERY 0x0001
53 #define CDFRAMES_PERSEC 75
54 #define CDFRAMES_PERMIN (CDFRAMES_PERSEC * 60)
55 #define FRAME_OF_ADDR(a) ((a)[1] * CDFRAMES_PERMIN + (a)[2] * CDFRAMES_PERSEC + (a)[3])
56 #define FRAME_OF_TOC(toc, idx) FRAME_OF_ADDR((toc)->TrackData[(idx) - (toc)->FirstTrack].Address)
58 #define GETWORD(buf,off) MAKEWORD(buf[(off)],buf[(off+1)])
59 #define GETLONG(buf,off) MAKELONG(GETWORD(buf,off),GETWORD(buf,off+2))
61 enum fs_type
63 FS_ERROR, /* error accessing the device */
64 FS_UNKNOWN, /* unknown file system */
65 FS_FAT1216,
66 FS_FAT32,
67 FS_ISO9660,
68 FS_UDF /* For reference [E] = Ecma-167.pdf, [U] = udf260.pdf */
71 /* read a Unix symlink; returned buffer must be freed by caller */
72 static char *read_symlink( const char *path )
74 char *buffer;
75 int ret, size = 128;
77 for (;;)
79 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, size )))
81 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
82 return 0;
84 ret = readlink( path, buffer, size );
85 if (ret == -1)
87 FILE_SetDosError();
88 HeapFree( GetProcessHeap(), 0, buffer );
89 return 0;
91 if (ret != size)
93 buffer[ret] = 0;
94 return buffer;
96 HeapFree( GetProcessHeap(), 0, buffer );
97 size *= 2;
101 /* get the path of a dos device symlink in the $WINEPREFIX/dosdevices directory */
102 static char *get_dos_device_path( LPCWSTR name )
104 const char *config_dir = wine_get_config_dir();
105 char *buffer, *dev;
106 int i;
108 if (!(buffer = HeapAlloc( GetProcessHeap(), 0,
109 strlen(config_dir) + sizeof("/dosdevices/") + 5 )))
111 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
112 return NULL;
114 strcpy( buffer, config_dir );
115 strcat( buffer, "/dosdevices/" );
116 dev = buffer + strlen(buffer);
117 /* no codepage conversion, DOS device names are ASCII anyway */
118 for (i = 0; i < 5; i++)
119 if (!(dev[i] = (char)tolowerW(name[i]))) break;
120 dev[5] = 0;
121 return buffer;
124 /* read the contents of an NT symlink object */
125 static NTSTATUS read_nt_symlink( const WCHAR *name, WCHAR *target, DWORD size )
127 NTSTATUS status;
128 OBJECT_ATTRIBUTES attr;
129 UNICODE_STRING nameW;
130 HANDLE handle;
132 attr.Length = sizeof(attr);
133 attr.RootDirectory = 0;
134 attr.Attributes = OBJ_CASE_INSENSITIVE;
135 attr.ObjectName = &nameW;
136 attr.SecurityDescriptor = NULL;
137 attr.SecurityQualityOfService = NULL;
138 RtlInitUnicodeString( &nameW, name );
140 if (!(status = NtOpenSymbolicLinkObject( &handle, SYMBOLIC_LINK_QUERY, &attr )))
142 UNICODE_STRING targetW;
143 targetW.Buffer = target;
144 targetW.MaximumLength = (size - 1) * sizeof(WCHAR);
145 status = NtQuerySymbolicLinkObject( handle, &targetW, NULL );
146 if (!status) target[targetW.Length / sizeof(WCHAR)] = 0;
147 NtClose( handle );
149 return status;
152 /* open a handle to a device root */
153 static BOOL open_device_root( LPCWSTR root, HANDLE *handle )
155 static const WCHAR default_rootW[] = {'\\',0};
156 UNICODE_STRING nt_name;
157 OBJECT_ATTRIBUTES attr;
158 IO_STATUS_BLOCK io;
159 NTSTATUS status;
161 if (!root) root = default_rootW;
162 if (!RtlDosPathNameToNtPathName_U( root, &nt_name, NULL, NULL ))
164 SetLastError( ERROR_PATH_NOT_FOUND );
165 return FALSE;
167 attr.Length = sizeof(attr);
168 attr.RootDirectory = 0;
169 attr.Attributes = OBJ_CASE_INSENSITIVE;
170 attr.ObjectName = &nt_name;
171 attr.SecurityDescriptor = NULL;
172 attr.SecurityQualityOfService = NULL;
174 status = NtOpenFile( handle, 0, &attr, &io, 0,
175 FILE_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT );
176 RtlFreeUnicodeString( &nt_name );
177 if (status != STATUS_SUCCESS)
179 SetLastError( RtlNtStatusToDosError(status) );
180 return FALSE;
182 return TRUE;
185 /* query the type of a drive from the mount manager */
186 static DWORD get_mountmgr_drive_type( LPCWSTR root )
188 HANDLE mgr;
189 struct mountmgr_unix_drive data;
191 memset( &data, 0, sizeof(data) );
192 if (root) data.letter = root[0];
193 else
195 WCHAR curdir[MAX_PATH];
196 GetCurrentDirectoryW( MAX_PATH, curdir );
197 if (curdir[1] != ':' || curdir[2] != '\\') return DRIVE_UNKNOWN;
198 data.letter = curdir[0];
201 mgr = CreateFileW( MOUNTMGR_DOS_DEVICE_NAME, GENERIC_READ,
202 FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0 );
203 if (mgr == INVALID_HANDLE_VALUE) return DRIVE_UNKNOWN;
205 if (!DeviceIoControl( mgr, IOCTL_MOUNTMGR_QUERY_UNIX_DRIVE, &data, sizeof(data), &data,
206 sizeof(data), NULL, NULL ) && GetLastError() != ERROR_MORE_DATA)
207 data.type = DRIVE_UNKNOWN;
209 CloseHandle( mgr );
210 return data.type;
213 /* get the label by reading it from a file at the root of the filesystem */
214 static void get_filesystem_label( const UNICODE_STRING *device, WCHAR *label, DWORD len )
216 static const WCHAR labelW[] = {'.','w','i','n','d','o','w','s','-','l','a','b','e','l',0};
217 HANDLE handle;
218 UNICODE_STRING name;
219 IO_STATUS_BLOCK io;
220 OBJECT_ATTRIBUTES attr;
222 label[0] = 0;
224 attr.Length = sizeof(attr);
225 attr.RootDirectory = 0;
226 attr.Attributes = OBJ_CASE_INSENSITIVE;
227 attr.ObjectName = &name;
228 attr.SecurityDescriptor = NULL;
229 attr.SecurityQualityOfService = NULL;
231 name.MaximumLength = device->Length + sizeof(labelW);
232 name.Length = name.MaximumLength - sizeof(WCHAR);
233 if (!(name.Buffer = HeapAlloc( GetProcessHeap(), 0, name.MaximumLength ))) return;
235 memcpy( name.Buffer, device->Buffer, device->Length );
236 memcpy( name.Buffer + device->Length / sizeof(WCHAR), labelW, sizeof(labelW) );
237 if (!NtOpenFile( &handle, GENERIC_READ, &attr, &io, FILE_SHARE_READ|FILE_SHARE_WRITE,
238 FILE_NON_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT ))
240 char buffer[256], *p;
241 DWORD size;
243 if (!ReadFile( handle, buffer, sizeof(buffer)-1, &size, NULL )) size = 0;
244 CloseHandle( handle );
245 p = buffer + size;
246 while (p > buffer && (p[-1] == ' ' || p[-1] == '\r' || p[-1] == '\n')) p--;
247 *p = 0;
248 if (!MultiByteToWideChar( CP_UNIXCP, 0, buffer, -1, label, len ))
249 label[len-1] = 0;
251 RtlFreeUnicodeString( &name );
254 /* get the serial number by reading it from a file at the root of the filesystem */
255 static DWORD get_filesystem_serial( const UNICODE_STRING *device )
257 static const WCHAR serialW[] = {'.','w','i','n','d','o','w','s','-','s','e','r','i','a','l',0};
258 HANDLE handle;
259 UNICODE_STRING name;
260 IO_STATUS_BLOCK io;
261 OBJECT_ATTRIBUTES attr;
262 DWORD ret = 0;
264 attr.Length = sizeof(attr);
265 attr.RootDirectory = 0;
266 attr.Attributes = OBJ_CASE_INSENSITIVE;
267 attr.ObjectName = &name;
268 attr.SecurityDescriptor = NULL;
269 attr.SecurityQualityOfService = NULL;
271 name.MaximumLength = device->Length + sizeof(serialW);
272 name.Length = name.MaximumLength - sizeof(WCHAR);
273 if (!(name.Buffer = HeapAlloc( GetProcessHeap(), 0, name.MaximumLength ))) return 0;
275 memcpy( name.Buffer, device->Buffer, device->Length );
276 memcpy( name.Buffer + device->Length / sizeof(WCHAR), serialW, sizeof(serialW) );
277 if (!NtOpenFile( &handle, GENERIC_READ, &attr, &io, FILE_SHARE_READ|FILE_SHARE_WRITE,
278 FILE_SYNCHRONOUS_IO_NONALERT ))
280 char buffer[32];
281 DWORD size;
283 if (!ReadFile( handle, buffer, sizeof(buffer)-1, &size, NULL )) size = 0;
284 CloseHandle( handle );
285 buffer[size] = 0;
286 ret = strtoul( buffer, NULL, 16 );
288 RtlFreeUnicodeString( &name );
289 return ret;
293 /******************************************************************
294 * VOLUME_FindCdRomDataBestVoldesc
296 static DWORD VOLUME_FindCdRomDataBestVoldesc( HANDLE handle )
298 BYTE cur_vd_type, max_vd_type = 0;
299 BYTE buffer[0x800];
300 DWORD size, offs, best_offs = 0, extra_offs = 0;
302 for (offs = 0x8000; offs <= 0x9800; offs += 0x800)
304 /* if 'CDROM' occurs at position 8, this is a pre-iso9660 cd, and
305 * the volume label is displaced forward by 8
307 if (SetFilePointer( handle, offs, NULL, FILE_BEGIN ) != offs) break;
308 if (!ReadFile( handle, buffer, sizeof(buffer), &size, NULL )) break;
309 if (size != sizeof(buffer)) break;
310 /* check for non-ISO9660 signature */
311 if (!memcmp( buffer + 11, "ROM", 3 )) extra_offs = 8;
312 cur_vd_type = buffer[extra_offs];
313 if (cur_vd_type == 0xff) /* voldesc set terminator */
314 break;
315 if (cur_vd_type > max_vd_type)
317 max_vd_type = cur_vd_type;
318 best_offs = offs + extra_offs;
321 return best_offs;
325 /***********************************************************************
326 * VOLUME_ReadFATSuperblock
328 static enum fs_type VOLUME_ReadFATSuperblock( HANDLE handle, BYTE *buff )
330 DWORD size;
332 /* try a fixed disk, with a FAT partition */
333 if (SetFilePointer( handle, 0, NULL, FILE_BEGIN ) != 0 ||
334 !ReadFile( handle, buff, SUPERBLOCK_SIZE, &size, NULL ))
336 if (GetLastError() == ERROR_BAD_DEV_TYPE) return FS_UNKNOWN; /* not a real device */
337 return FS_ERROR;
340 if (size < SUPERBLOCK_SIZE) return FS_UNKNOWN;
342 /* FIXME: do really all FAT have their name beginning with
343 * "FAT" ? (At least FAT12, FAT16 and FAT32 have :)
345 if (!memcmp(buff+0x36, "FAT", 3) || !memcmp(buff+0x52, "FAT", 3))
347 /* guess which type of FAT we have */
348 int reasonable;
349 unsigned int sectors,
350 sect_per_fat,
351 total_sectors,
352 num_boot_sectors,
353 num_fats,
354 num_root_dir_ents,
355 bytes_per_sector,
356 sectors_per_cluster,
357 nclust;
358 sect_per_fat = GETWORD(buff, 0x16);
359 if (!sect_per_fat) sect_per_fat = GETLONG(buff, 0x24);
360 total_sectors = GETWORD(buff, 0x13);
361 if (!total_sectors)
362 total_sectors = GETLONG(buff, 0x20);
363 num_boot_sectors = GETWORD(buff, 0x0e);
364 num_fats = buff[0x10];
365 num_root_dir_ents = GETWORD(buff, 0x11);
366 bytes_per_sector = GETWORD(buff, 0x0b);
367 sectors_per_cluster = buff[0x0d];
368 /* check if the parameters are reasonable and will not cause
369 * arithmetic errors in the calculation */
370 reasonable = num_boot_sectors < total_sectors &&
371 num_fats < 16 &&
372 bytes_per_sector >= 512 && bytes_per_sector % 512 == 0 &&
373 sectors_per_cluster >= 1;
374 if (!reasonable) return FS_UNKNOWN;
375 sectors = total_sectors - num_boot_sectors - num_fats * sect_per_fat -
376 (num_root_dir_ents * 32 + bytes_per_sector - 1) / bytes_per_sector;
377 nclust = sectors / sectors_per_cluster;
378 if ((buff[0x42] == 0x28 || buff[0x42] == 0x29) &&
379 !memcmp(buff+0x52, "FAT", 3)) return FS_FAT32;
380 if (nclust < 65525)
382 if ((buff[0x26] == 0x28 || buff[0x26] == 0x29) &&
383 !memcmp(buff+0x36, "FAT", 3))
384 return FS_FAT1216;
387 return FS_UNKNOWN;
391 /***********************************************************************
392 * VOLUME_ReadCDBlock
394 static BOOL VOLUME_ReadCDBlock( HANDLE handle, BYTE *buff, INT offs )
396 DWORD size, whence = offs >= 0 ? FILE_BEGIN : FILE_END;
398 if (SetFilePointer( handle, offs, NULL, whence ) != offs ||
399 !ReadFile( handle, buff, SUPERBLOCK_SIZE, &size, NULL ) ||
400 size != SUPERBLOCK_SIZE)
401 return FALSE;
403 return TRUE;
407 /***********************************************************************
408 * VOLUME_ReadCDSuperblock
410 static enum fs_type VOLUME_ReadCDSuperblock( HANDLE handle, BYTE *buff )
412 int i;
413 DWORD offs;
415 /* Check UDF first as UDF and ISO9660 structures can coexist on the same medium
416 * Starting from sector 16, we may find :
417 * - a CD-ROM Volume Descriptor Set (ISO9660) containing one or more Volume Descriptors
418 * - an Extented Area (UDF) -- [E] 2/8.3.1 and [U] 2.1.7
419 * There is no explicit end so read 16 sectors and then give up */
420 for( i=16; i<16+16; i++)
422 if (!VOLUME_ReadCDBlock(handle, buff, i*BLOCK_SIZE))
423 continue;
425 /* We are supposed to check "BEA01", "NSR0x" and "TEA01" IDs + verify tag checksum
426 * but we assume the volume is well-formatted */
427 if (!memcmp(&buff[1], "BEA01", 5)) return FS_UDF;
430 offs = VOLUME_FindCdRomDataBestVoldesc( handle );
431 if (!offs) return FS_UNKNOWN;
433 if (!VOLUME_ReadCDBlock(handle, buff, offs))
434 return FS_ERROR;
436 /* check for the iso9660 identifier */
437 if (!memcmp(&buff[1], "CD001", 5)) return FS_ISO9660;
438 return FS_UNKNOWN;
442 /**************************************************************************
443 * UDF_Find_PVD
444 * Find the Primary Volume Descriptor
446 static BOOL UDF_Find_PVD( HANDLE handle, BYTE pvd[] )
448 int i;
449 DWORD offset;
450 INT locations[] = { 256, -1, -257, 512 };
452 for(i=0; i<sizeof(locations)/sizeof(locations[0]); i++)
454 if (!VOLUME_ReadCDBlock(handle, pvd, locations[i]*BLOCK_SIZE))
455 return FALSE;
457 /* Tag Identifier of Anchor Volume Descriptor Pointer is 2 -- [E] 3/10.2.1 */
458 if (pvd[0]==2 && pvd[1]==0)
460 /* Tag location (Uint32) at offset 12, little-endian */
461 offset = pvd[20 + 0];
462 offset |= pvd[20 + 1] << 8;
463 offset |= pvd[20 + 2] << 16;
464 offset |= pvd[20 + 3] << 24;
465 offset *= BLOCK_SIZE;
467 if (!VOLUME_ReadCDBlock(handle, pvd, offset))
468 return FALSE;
470 /* Check for the Primary Volume Descriptor Tag Id -- [E] 3/10.1.1 */
471 if (pvd[0]!=1 || pvd[1]!=0)
472 return FALSE;
474 /* 8 or 16 bits per character -- [U] 2.1.1 */
475 if (!(pvd[24]==8 || pvd[24]==16))
476 return FALSE;
478 return TRUE;
482 return FALSE;
486 /**************************************************************************
487 * VOLUME_GetSuperblockLabel
489 static void VOLUME_GetSuperblockLabel( const UNICODE_STRING *device, HANDLE handle,
490 enum fs_type type, const BYTE *superblock,
491 WCHAR *label, DWORD len )
493 const BYTE *label_ptr = NULL;
494 DWORD label_len;
496 switch(type)
498 case FS_ERROR:
499 label_len = 0;
500 break;
501 case FS_UNKNOWN:
502 get_filesystem_label( device, label, len );
503 return;
504 case FS_FAT1216:
505 label_ptr = superblock + 0x2b;
506 label_len = 11;
507 break;
508 case FS_FAT32:
509 label_ptr = superblock + 0x47;
510 label_len = 11;
511 break;
512 case FS_ISO9660:
514 BYTE ver = superblock[0x5a];
516 if (superblock[0x58] == 0x25 && superblock[0x59] == 0x2f && /* Unicode ID */
517 ((ver == 0x40) || (ver == 0x43) || (ver == 0x45)))
518 { /* yippee, unicode */
519 unsigned int i;
521 if (len > 17) len = 17;
522 for (i = 0; i < len-1; i++)
523 label[i] = (superblock[40+2*i] << 8) | superblock[41+2*i];
524 label[i] = 0;
525 while (i && label[i-1] == ' ') label[--i] = 0;
526 return;
528 label_ptr = superblock + 40;
529 label_len = 32;
530 break;
532 case FS_UDF:
534 BYTE pvd[BLOCK_SIZE];
536 if(!UDF_Find_PVD(handle, pvd))
538 label_len = 0;
539 break;
542 /* [E] 3/10.1.4 and [U] 2.1.1 */
543 if(pvd[24]==8)
545 label_ptr = pvd + 24 + 1;
546 label_len = pvd[24+32-1];
547 break;
549 else
551 int i;
553 label_len = 1 + pvd[24+32-1];
554 for(i=0; i<label_len && i<len; i+=2)
555 label[i/2] = (pvd[24+1 +i] << 8) | pvd[24+1 +i+1];
556 label[label_len] = 0;
557 return;
561 if (label_len) RtlMultiByteToUnicodeN( label, (len-1) * sizeof(WCHAR),
562 &label_len, (LPCSTR)label_ptr, label_len );
563 label_len /= sizeof(WCHAR);
564 label[label_len] = 0;
565 while (label_len && label[label_len-1] == ' ') label[--label_len] = 0;
569 /**************************************************************************
570 * VOLUME_GetSuperblockSerial
572 static DWORD VOLUME_GetSuperblockSerial( const UNICODE_STRING *device, HANDLE handle,
573 enum fs_type type, const BYTE *superblock )
575 BYTE block[BLOCK_SIZE];
577 switch(type)
579 case FS_ERROR:
580 break;
581 case FS_UNKNOWN:
582 return get_filesystem_serial( device );
583 case FS_FAT1216:
584 return GETLONG( superblock, 0x27 );
585 case FS_FAT32:
586 return GETLONG( superblock, 0x33 );
587 case FS_UDF:
588 if (!VOLUME_ReadCDBlock(handle, block, 257*BLOCK_SIZE))
589 break;
590 superblock = block;
591 /* fallthrough */
592 case FS_ISO9660:
594 BYTE sum[4];
595 int i;
597 sum[0] = sum[1] = sum[2] = sum[3] = 0;
598 for (i = 0; i < 2048; i += 4)
600 /* DON'T optimize this into DWORD !! (breaks overflow) */
601 sum[0] += superblock[i+0];
602 sum[1] += superblock[i+1];
603 sum[2] += superblock[i+2];
604 sum[3] += superblock[i+3];
607 * OK, another braindead one... argh. Just believe it.
608 * Me$$ysoft chose to reverse the serial number in NT4/W2K.
609 * It's true and nobody will ever be able to change it.
611 if ((GetVersion() & 0x80000000) || type == FS_UDF)
612 return (sum[3] << 24) | (sum[2] << 16) | (sum[1] << 8) | sum[0];
613 else
614 return (sum[0] << 24) | (sum[1] << 16) | (sum[2] << 8) | sum[3];
617 return 0;
621 /**************************************************************************
622 * VOLUME_GetAudioCDSerial
624 static DWORD VOLUME_GetAudioCDSerial( const CDROM_TOC *toc )
626 DWORD serial = 0;
627 int i;
629 for (i = 0; i <= toc->LastTrack - toc->FirstTrack; i++)
630 serial += ((toc->TrackData[i].Address[1] << 16) |
631 (toc->TrackData[i].Address[2] << 8) |
632 toc->TrackData[i].Address[3]);
635 * dwStart, dwEnd collect the beginning and end of the disc respectively, in
636 * frames.
637 * There it is collected for correcting the serial when there are less than
638 * 3 tracks.
640 if (toc->LastTrack - toc->FirstTrack + 1 < 3)
642 DWORD dwStart = FRAME_OF_TOC(toc, toc->FirstTrack);
643 DWORD dwEnd = FRAME_OF_TOC(toc, toc->LastTrack + 1);
644 serial += dwEnd - dwStart;
646 return serial;
650 /***********************************************************************
651 * GetVolumeInformationW (KERNEL32.@)
653 BOOL WINAPI GetVolumeInformationW( LPCWSTR root, LPWSTR label, DWORD label_len,
654 DWORD *serial, DWORD *filename_len, DWORD *flags,
655 LPWSTR fsname, DWORD fsname_len )
657 static const WCHAR audiocdW[] = {'A','u','d','i','o',' ','C','D',0};
658 static const WCHAR fatW[] = {'F','A','T',0};
659 static const WCHAR fat32W[] = {'F','A','T','3','2',0};
660 static const WCHAR ntfsW[] = {'N','T','F','S',0};
661 static const WCHAR cdfsW[] = {'C','D','F','S',0};
662 static const WCHAR udfW[] = {'U','D','F',0};
663 static const WCHAR default_rootW[] = {'\\',0};
665 HANDLE handle;
666 NTSTATUS status;
667 UNICODE_STRING nt_name;
668 IO_STATUS_BLOCK io;
669 OBJECT_ATTRIBUTES attr;
670 FILE_FS_DEVICE_INFORMATION info;
671 WCHAR *p;
672 enum fs_type type = FS_UNKNOWN;
673 BOOL ret = FALSE;
675 if (!root) root = default_rootW;
676 if (!RtlDosPathNameToNtPathName_U( root, &nt_name, NULL, NULL ))
678 SetLastError( ERROR_PATH_NOT_FOUND );
679 return FALSE;
681 /* there must be exactly one backslash in the name, at the end */
682 p = memchrW( nt_name.Buffer + 4, '\\', (nt_name.Length - 4) / sizeof(WCHAR) );
683 if (p != nt_name.Buffer + nt_name.Length / sizeof(WCHAR) - 1)
685 /* check if root contains an explicit subdir */
686 if (root[0] && root[1] == ':') root += 2;
687 while (*root == '\\') root++;
688 if (strchrW( root, '\\' ))
689 SetLastError( ERROR_DIR_NOT_ROOT );
690 else
691 SetLastError( ERROR_INVALID_NAME );
692 goto done;
695 /* try to open the device */
697 attr.Length = sizeof(attr);
698 attr.RootDirectory = 0;
699 attr.Attributes = OBJ_CASE_INSENSITIVE;
700 attr.ObjectName = &nt_name;
701 attr.SecurityDescriptor = NULL;
702 attr.SecurityQualityOfService = NULL;
704 nt_name.Length -= sizeof(WCHAR); /* without trailing slash */
705 status = NtOpenFile( &handle, GENERIC_READ, &attr, &io, FILE_SHARE_READ | FILE_SHARE_WRITE,
706 FILE_NON_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT );
707 nt_name.Length += sizeof(WCHAR);
709 if (status == STATUS_SUCCESS)
711 BYTE superblock[SUPERBLOCK_SIZE];
712 CDROM_TOC toc;
713 DWORD br;
715 /* check for audio CD */
716 /* FIXME: we only check the first track for now */
717 if (DeviceIoControl( handle, IOCTL_CDROM_READ_TOC, NULL, 0, &toc, sizeof(toc), &br, 0 ))
719 if (!(toc.TrackData[0].Control & 0x04)) /* audio track */
721 TRACE( "%s: found audio CD\n", debugstr_w(nt_name.Buffer) );
722 if (label) lstrcpynW( label, audiocdW, label_len );
723 if (serial) *serial = VOLUME_GetAudioCDSerial( &toc );
724 CloseHandle( handle );
725 type = FS_ISO9660;
726 goto fill_fs_info;
728 type = VOLUME_ReadCDSuperblock( handle, superblock );
730 else
732 type = VOLUME_ReadFATSuperblock( handle, superblock );
733 if (type == FS_UNKNOWN) type = VOLUME_ReadCDSuperblock( handle, superblock );
735 TRACE( "%s: found fs type %d\n", debugstr_w(nt_name.Buffer), type );
736 if (type == FS_ERROR)
738 CloseHandle( handle );
739 goto done;
742 if (label && label_len) VOLUME_GetSuperblockLabel( &nt_name, handle, type, superblock, label, label_len );
743 if (serial) *serial = VOLUME_GetSuperblockSerial( &nt_name, handle, type, superblock );
744 CloseHandle( handle );
745 goto fill_fs_info;
747 else TRACE( "cannot open device %s: %x\n", debugstr_w(nt_name.Buffer), status );
749 /* we couldn't open the device, fallback to default strategy */
751 status = NtOpenFile( &handle, 0, &attr, &io, 0, FILE_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT );
752 if (status != STATUS_SUCCESS)
754 SetLastError( RtlNtStatusToDosError(status) );
755 goto done;
757 status = NtQueryVolumeInformationFile( handle, &io, &info, sizeof(info), FileFsDeviceInformation );
758 NtClose( handle );
759 if (status != STATUS_SUCCESS)
761 SetLastError( RtlNtStatusToDosError(status) );
762 goto done;
764 if (info.DeviceType == FILE_DEVICE_CD_ROM_FILE_SYSTEM) type = FS_ISO9660;
766 if (label && label_len) get_filesystem_label( &nt_name, label, label_len );
767 if (serial) *serial = get_filesystem_serial( &nt_name );
769 fill_fs_info: /* now fill in the information that depends on the file system type */
771 switch(type)
773 case FS_ISO9660:
774 if (fsname) lstrcpynW( fsname, cdfsW, fsname_len );
775 if (filename_len) *filename_len = 221;
776 if (flags) *flags = FILE_READ_ONLY_VOLUME;
777 break;
778 case FS_UDF:
779 if (fsname) lstrcpynW( fsname, udfW, fsname_len );
780 if (filename_len) *filename_len = 255;
781 if (flags)
782 *flags = FILE_READ_ONLY_VOLUME | FILE_UNICODE_ON_DISK | FILE_CASE_SENSITIVE_SEARCH;
783 break;
784 case FS_FAT1216:
785 if (fsname) lstrcpynW( fsname, fatW, fsname_len );
786 case FS_FAT32:
787 if (type == FS_FAT32 && fsname) lstrcpynW( fsname, fat32W, fsname_len );
788 if (filename_len) *filename_len = 255;
789 if (flags) *flags = FILE_CASE_PRESERVED_NAMES; /* FIXME */
790 break;
791 default:
792 if (fsname) lstrcpynW( fsname, ntfsW, fsname_len );
793 if (filename_len) *filename_len = 255;
794 if (flags) *flags = FILE_CASE_PRESERVED_NAMES;
795 break;
797 ret = TRUE;
799 done:
800 RtlFreeUnicodeString( &nt_name );
801 return ret;
805 /***********************************************************************
806 * GetVolumeInformationA (KERNEL32.@)
808 BOOL WINAPI GetVolumeInformationA( LPCSTR root, LPSTR label,
809 DWORD label_len, DWORD *serial,
810 DWORD *filename_len, DWORD *flags,
811 LPSTR fsname, DWORD fsname_len )
813 WCHAR *rootW = NULL;
814 LPWSTR labelW, fsnameW;
815 BOOL ret;
817 if (root && !(rootW = FILE_name_AtoW( root, FALSE ))) return FALSE;
819 labelW = label ? HeapAlloc(GetProcessHeap(), 0, label_len * sizeof(WCHAR)) : NULL;
820 fsnameW = fsname ? HeapAlloc(GetProcessHeap(), 0, fsname_len * sizeof(WCHAR)) : NULL;
822 if ((ret = GetVolumeInformationW(rootW, labelW, label_len, serial,
823 filename_len, flags, fsnameW, fsname_len)))
825 if (label) FILE_name_WtoA( labelW, -1, label, label_len );
826 if (fsname) FILE_name_WtoA( fsnameW, -1, fsname, fsname_len );
829 HeapFree( GetProcessHeap(), 0, labelW );
830 HeapFree( GetProcessHeap(), 0, fsnameW );
831 return ret;
836 /***********************************************************************
837 * SetVolumeLabelW (KERNEL32.@)
839 BOOL WINAPI SetVolumeLabelW( LPCWSTR root, LPCWSTR label )
841 WCHAR device[] = {'\\','\\','.','\\','A',':',0};
842 HANDLE handle;
843 enum fs_type type = FS_UNKNOWN;
845 if (!root)
847 WCHAR path[MAX_PATH];
848 GetCurrentDirectoryW( MAX_PATH, path );
849 device[4] = path[0];
851 else
853 if (!root[0] || root[1] != ':')
855 SetLastError( ERROR_INVALID_NAME );
856 return FALSE;
858 device[4] = root[0];
861 /* try to open the device */
863 handle = CreateFileW( device, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE,
864 NULL, OPEN_EXISTING, 0, 0 );
865 if (handle != INVALID_HANDLE_VALUE)
867 BYTE superblock[SUPERBLOCK_SIZE];
869 type = VOLUME_ReadFATSuperblock( handle, superblock );
870 if (type == FS_UNKNOWN) type = VOLUME_ReadCDSuperblock( handle, superblock );
871 CloseHandle( handle );
872 if (type != FS_UNKNOWN)
874 /* we can't set the label on FAT or CDROM file systems */
875 TRACE( "cannot set label on device %s type %d\n", debugstr_w(device), type );
876 SetLastError( ERROR_ACCESS_DENIED );
877 return FALSE;
880 else
882 TRACE( "cannot open device %s: err %d\n", debugstr_w(device), GetLastError() );
883 if (GetLastError() == ERROR_ACCESS_DENIED) return FALSE;
886 /* we couldn't open the device, fallback to default strategy */
888 switch(GetDriveTypeW( root ))
890 case DRIVE_UNKNOWN:
891 case DRIVE_NO_ROOT_DIR:
892 SetLastError( ERROR_NOT_READY );
893 break;
894 case DRIVE_REMOVABLE:
895 case DRIVE_FIXED:
897 WCHAR labelW[] = {'A',':','\\','.','w','i','n','d','o','w','s','-','l','a','b','e','l',0};
899 labelW[0] = device[4];
901 if (!label[0]) /* delete label file when setting an empty label */
902 return DeleteFileW( labelW ) || GetLastError() == ERROR_FILE_NOT_FOUND;
904 handle = CreateFileW( labelW, GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
905 CREATE_ALWAYS, 0, 0 );
906 if (handle != INVALID_HANDLE_VALUE)
908 char buffer[64];
909 DWORD size;
911 if (!WideCharToMultiByte( CP_UNIXCP, 0, label, -1, buffer, sizeof(buffer)-1, NULL, NULL ))
912 buffer[sizeof(buffer)-2] = 0;
913 strcat( buffer, "\n" );
914 WriteFile( handle, buffer, strlen(buffer), &size, NULL );
915 CloseHandle( handle );
916 return TRUE;
918 break;
920 case DRIVE_REMOTE:
921 case DRIVE_RAMDISK:
922 case DRIVE_CDROM:
923 SetLastError( ERROR_ACCESS_DENIED );
924 break;
926 return FALSE;
929 /***********************************************************************
930 * SetVolumeLabelA (KERNEL32.@)
932 BOOL WINAPI SetVolumeLabelA(LPCSTR root, LPCSTR volname)
934 WCHAR *rootW = NULL, *volnameW = NULL;
935 BOOL ret;
937 if (root && !(rootW = FILE_name_AtoW( root, FALSE ))) return FALSE;
938 if (volname && !(volnameW = FILE_name_AtoW( volname, TRUE ))) return FALSE;
939 ret = SetVolumeLabelW( rootW, volnameW );
940 HeapFree( GetProcessHeap(), 0, volnameW );
941 return ret;
945 /***********************************************************************
946 * GetVolumeNameForVolumeMountPointA (KERNEL32.@)
948 BOOL WINAPI GetVolumeNameForVolumeMountPointA( LPCSTR path, LPSTR volume, DWORD size )
950 BOOL ret;
951 WCHAR volumeW[50], *pathW = NULL;
952 DWORD len = min( sizeof(volumeW) / sizeof(WCHAR), size );
954 TRACE("(%s, %p, %x)\n", debugstr_a(path), volume, size);
956 if (!path || !(pathW = FILE_name_AtoW( path, TRUE )))
957 return FALSE;
959 if ((ret = GetVolumeNameForVolumeMountPointW( pathW, volumeW, len )))
960 FILE_name_WtoA( volumeW, -1, volume, len );
962 HeapFree( GetProcessHeap(), 0, pathW );
963 return ret;
966 /***********************************************************************
967 * GetVolumeNameForVolumeMountPointW (KERNEL32.@)
969 BOOL WINAPI GetVolumeNameForVolumeMountPointW( LPCWSTR path, LPWSTR volume, DWORD size )
971 static const WCHAR prefixW[] = {'\\','D','o','s','D','e','v','i','c','e','s','\\',0};
972 static const WCHAR volumeW[] = {'\\','?','?','\\','V','o','l','u','m','e','{',0};
973 static const WCHAR trailingW[] = {'\\',0};
975 MOUNTMGR_MOUNT_POINT *input = NULL, *o1;
976 MOUNTMGR_MOUNT_POINTS *output = NULL;
977 WCHAR *p;
978 char *r;
979 DWORD i, i_size = 1024, o_size = 1024;
980 WCHAR *nonpersist_name;
981 WCHAR symlink_name[MAX_PATH];
982 NTSTATUS status;
983 HANDLE mgr = INVALID_HANDLE_VALUE;
984 BOOL ret = FALSE;
986 TRACE("(%s, %p, %x)\n", debugstr_w(path), volume, size);
987 if (path[lstrlenW(path)-1] != '\\')
989 SetLastError( ERROR_INVALID_NAME );
990 return FALSE;
993 if (size < 50)
995 SetLastError( ERROR_FILENAME_EXCED_RANGE );
996 return FALSE;
998 /* if length of input is > 3 then it must be a mounted folder */
999 if (lstrlenW(path) > 3)
1001 FIXME("Mounted Folders are not yet supported\n");
1002 SetLastError( ERROR_NOT_A_REPARSE_POINT );
1003 return FALSE;
1006 mgr = CreateFileW( MOUNTMGR_DOS_DEVICE_NAME, 0, FILE_SHARE_READ,
1007 NULL, OPEN_EXISTING, 0, 0 );
1008 if (mgr == INVALID_HANDLE_VALUE) return FALSE;
1010 if (!(input = HeapAlloc( GetProcessHeap(), 0, i_size )))
1012 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1013 goto err_ret;
1016 if (!(output = HeapAlloc( GetProcessHeap(), 0, o_size )))
1018 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1019 goto err_ret;
1022 /* construct the symlink name as "\DosDevices\C:" */
1023 lstrcpyW( symlink_name, prefixW );
1024 lstrcatW( symlink_name, path );
1025 symlink_name[lstrlenW(symlink_name)-1] = 0;
1027 /* Take the mount point and get the "nonpersistent name" */
1028 /* We will then take that and get the volume name */
1029 nonpersist_name = (WCHAR *)(input + 1);
1030 status = read_nt_symlink( symlink_name, nonpersist_name, i_size - sizeof(*input) );
1031 TRACE("read_nt_symlink got stat=%x, for %s, got <%s>\n", status,
1032 debugstr_w(symlink_name), debugstr_w(nonpersist_name));
1033 if (status != STATUS_SUCCESS)
1035 SetLastError( ERROR_FILE_NOT_FOUND );
1036 goto err_ret;
1039 /* Now take the "nonpersistent name" and ask the mountmgr */
1040 /* to give us all the mount points. One of them will be */
1041 /* the volume name (format of \??\Volume{). */
1042 memset( input, 0, sizeof(*input) ); /* clear all input parameters */
1043 input->DeviceNameOffset = sizeof(*input);
1044 input->DeviceNameLength = lstrlenW( nonpersist_name) * sizeof(WCHAR);
1045 i_size = input->DeviceNameOffset + input->DeviceNameLength;
1047 output->Size = o_size;
1049 /* now get the true volume name from the mountmgr */
1050 if (!DeviceIoControl( mgr, IOCTL_MOUNTMGR_QUERY_POINTS, input, i_size,
1051 output, o_size, NULL, NULL ))
1052 goto err_ret;
1054 /* Verify and return the data, note string is not null terminated */
1055 TRACE("found %d matching mount points\n", output->NumberOfMountPoints);
1056 if (output->NumberOfMountPoints < 1)
1058 SetLastError( ERROR_NO_VOLUME_ID );
1059 goto err_ret;
1061 o1 = &output->MountPoints[0];
1063 /* look for the volume name in returned values */
1064 for(i=0;i<output->NumberOfMountPoints;i++)
1066 p = (WCHAR*)((char *)output + o1->SymbolicLinkNameOffset);
1067 r = (char *)output + o1->UniqueIdOffset;
1068 TRACE("found symlink=%s, unique=%s, devname=%s\n",
1069 debugstr_wn(p, o1->SymbolicLinkNameLength/sizeof(WCHAR)),
1070 debugstr_an(r, o1->UniqueIdLength),
1071 debugstr_wn((WCHAR*)((char *)output + o1->DeviceNameOffset),
1072 o1->DeviceNameLength/sizeof(WCHAR)));
1074 if (!strncmpW( p, volumeW, (sizeof(volumeW)-1)/sizeof(WCHAR) ))
1076 /* is there space in the return variable ?? */
1077 if ((o1->SymbolicLinkNameLength/sizeof(WCHAR))+2 > size)
1079 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1080 goto err_ret;
1082 memcpy( volume, p, o1->SymbolicLinkNameLength );
1083 volume[o1->SymbolicLinkNameLength / sizeof(WCHAR)] = 0;
1084 lstrcatW( volume, trailingW );
1085 /* change second char from '?' to '\' */
1086 volume[1] = '\\';
1087 ret = TRUE;
1088 break;
1090 o1++;
1093 err_ret:
1094 HeapFree( GetProcessHeap(), 0, input );
1095 HeapFree( GetProcessHeap(), 0, output );
1096 CloseHandle( mgr );
1097 return ret;
1100 /***********************************************************************
1101 * DefineDosDeviceW (KERNEL32.@)
1103 BOOL WINAPI DefineDosDeviceW( DWORD flags, LPCWSTR devname, LPCWSTR targetpath )
1105 DWORD len, dosdev;
1106 BOOL ret = FALSE;
1107 char *path = NULL, *target, *p;
1109 TRACE("%x, %s, %s\n", flags, debugstr_w(devname), debugstr_w(targetpath));
1111 if (!(flags & DDD_REMOVE_DEFINITION))
1113 if (!(flags & DDD_RAW_TARGET_PATH))
1115 FIXME( "(0x%08x,%s,%s) DDD_RAW_TARGET_PATH flag not set, not supported yet\n",
1116 flags, debugstr_w(devname), debugstr_w(targetpath) );
1117 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1118 return FALSE;
1121 len = WideCharToMultiByte( CP_UNIXCP, 0, targetpath, -1, NULL, 0, NULL, NULL );
1122 if ((target = HeapAlloc( GetProcessHeap(), 0, len )))
1124 WideCharToMultiByte( CP_UNIXCP, 0, targetpath, -1, target, len, NULL, NULL );
1125 for (p = target; *p; p++) if (*p == '\\') *p = '/';
1127 else
1129 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1130 return FALSE;
1133 else target = NULL;
1135 /* first check for a DOS device */
1137 if ((dosdev = RtlIsDosDeviceName_U( devname )))
1139 WCHAR name[5];
1141 memcpy( name, devname + HIWORD(dosdev)/sizeof(WCHAR), LOWORD(dosdev) );
1142 name[LOWORD(dosdev)/sizeof(WCHAR)] = 0;
1143 path = get_dos_device_path( name );
1145 else if (isalphaW(devname[0]) && devname[1] == ':' && !devname[2]) /* drive mapping */
1147 path = get_dos_device_path( devname );
1149 else SetLastError( ERROR_FILE_NOT_FOUND );
1151 if (path)
1153 if (target)
1155 TRACE( "creating symlink %s -> %s\n", path, target );
1156 unlink( path );
1157 if (!symlink( target, path )) ret = TRUE;
1158 else FILE_SetDosError();
1160 else
1162 TRACE( "removing symlink %s\n", path );
1163 if (!unlink( path )) ret = TRUE;
1164 else FILE_SetDosError();
1166 HeapFree( GetProcessHeap(), 0, path );
1168 HeapFree( GetProcessHeap(), 0, target );
1169 return ret;
1173 /***********************************************************************
1174 * DefineDosDeviceA (KERNEL32.@)
1176 BOOL WINAPI DefineDosDeviceA(DWORD flags, LPCSTR devname, LPCSTR targetpath)
1178 WCHAR *devW, *targetW = NULL;
1179 BOOL ret;
1181 if (!(devW = FILE_name_AtoW( devname, FALSE ))) return FALSE;
1182 if (targetpath && !(targetW = FILE_name_AtoW( targetpath, TRUE ))) return FALSE;
1183 ret = DefineDosDeviceW(flags, devW, targetW);
1184 HeapFree( GetProcessHeap(), 0, targetW );
1185 return ret;
1189 /***********************************************************************
1190 * QueryDosDeviceW (KERNEL32.@)
1192 * returns array of strings terminated by \0, terminated by \0
1194 DWORD WINAPI QueryDosDeviceW( LPCWSTR devname, LPWSTR target, DWORD bufsize )
1196 static const WCHAR auxW[] = {'A','U','X',0};
1197 static const WCHAR nulW[] = {'N','U','L',0};
1198 static const WCHAR prnW[] = {'P','R','N',0};
1199 static const WCHAR comW[] = {'C','O','M',0};
1200 static const WCHAR lptW[] = {'L','P','T',0};
1201 static const WCHAR com0W[] = {'\\','?','?','\\','C','O','M','0',0};
1202 static const WCHAR com1W[] = {'\\','D','o','s','D','e','v','i','c','e','s','\\','C','O','M','1',0,0};
1203 static const WCHAR lpt1W[] = {'\\','D','o','s','D','e','v','i','c','e','s','\\','L','P','T','1',0,0};
1204 static const WCHAR dosdevW[] = {'\\','D','o','s','D','e','v','i','c','e','s','\\',0};
1206 UNICODE_STRING nt_name;
1207 ANSI_STRING unix_name;
1208 WCHAR nt_buffer[10];
1209 NTSTATUS status;
1211 if (!bufsize)
1213 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1214 return 0;
1217 if (devname)
1219 WCHAR *p, name[5];
1220 char *path, *link;
1221 DWORD dosdev, ret = 0;
1223 if ((dosdev = RtlIsDosDeviceName_U( devname )))
1225 memcpy( name, devname + HIWORD(dosdev)/sizeof(WCHAR), LOWORD(dosdev) );
1226 name[LOWORD(dosdev)/sizeof(WCHAR)] = 0;
1228 else
1230 WCHAR *buffer;
1232 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, sizeof(dosdevW) + strlenW(devname)*sizeof(WCHAR) )))
1234 SetLastError( ERROR_OUTOFMEMORY );
1235 return 0;
1237 memcpy( buffer, dosdevW, sizeof(dosdevW) );
1238 strcatW( buffer, devname );
1239 status = read_nt_symlink( buffer, target, bufsize );
1240 HeapFree( GetProcessHeap(), 0, buffer );
1241 if (status)
1243 SetLastError( RtlNtStatusToDosError(status) );
1244 return 0;
1246 ret = strlenW( target ) + 1;
1247 goto done;
1250 /* FIXME: should read NT symlink for all devices */
1252 if (!(path = get_dos_device_path( name ))) return 0;
1253 link = read_symlink( path );
1254 HeapFree( GetProcessHeap(), 0, path );
1256 if (link)
1258 ret = MultiByteToWideChar( CP_UNIXCP, 0, link, -1, target, bufsize );
1259 HeapFree( GetProcessHeap(), 0, link );
1261 else if (dosdev) /* look for device defaults */
1263 if (!strcmpiW( name, auxW ))
1265 if (bufsize >= sizeof(com1W)/sizeof(WCHAR))
1267 memcpy( target, com1W, sizeof(com1W) );
1268 ret = sizeof(com1W)/sizeof(WCHAR);
1270 else SetLastError( ERROR_INSUFFICIENT_BUFFER );
1271 return ret;
1273 if (!strcmpiW( name, prnW ))
1275 if (bufsize >= sizeof(lpt1W)/sizeof(WCHAR))
1277 memcpy( target, lpt1W, sizeof(lpt1W) );
1278 ret = sizeof(lpt1W)/sizeof(WCHAR);
1280 else SetLastError( ERROR_INSUFFICIENT_BUFFER );
1281 return ret;
1284 nt_buffer[0] = '\\';
1285 nt_buffer[1] = '?';
1286 nt_buffer[2] = '?';
1287 nt_buffer[3] = '\\';
1288 strcpyW( nt_buffer + 4, name );
1289 RtlInitUnicodeString( &nt_name, nt_buffer );
1290 status = wine_nt_to_unix_file_name( &nt_name, &unix_name, FILE_OPEN, TRUE );
1291 if (status) SetLastError( RtlNtStatusToDosError(status) );
1292 else
1294 ret = MultiByteToWideChar( CP_UNIXCP, 0, unix_name.Buffer, -1, target, bufsize );
1295 RtlFreeAnsiString( &unix_name );
1298 done:
1299 if (ret)
1301 if (ret < bufsize) target[ret++] = 0; /* add an extra null */
1302 for (p = target; *p; p++) if (*p == '/') *p = '\\';
1305 return ret;
1307 else /* return a list of all devices */
1309 OBJECT_ATTRIBUTES attr;
1310 HANDLE handle;
1311 WCHAR *p = target;
1312 int i;
1314 if (bufsize <= (sizeof(auxW)+sizeof(nulW)+sizeof(prnW))/sizeof(WCHAR))
1316 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1317 return 0;
1320 /* FIXME: these should be NT symlinks too */
1322 memcpy( p, auxW, sizeof(auxW) );
1323 p += sizeof(auxW) / sizeof(WCHAR);
1324 memcpy( p, nulW, sizeof(nulW) );
1325 p += sizeof(nulW) / sizeof(WCHAR);
1326 memcpy( p, prnW, sizeof(prnW) );
1327 p += sizeof(prnW) / sizeof(WCHAR);
1329 strcpyW( nt_buffer, com0W );
1330 RtlInitUnicodeString( &nt_name, nt_buffer );
1332 for (i = 1; i <= 9; i++)
1334 nt_buffer[7] = '0' + i;
1335 if (!wine_nt_to_unix_file_name( &nt_name, &unix_name, FILE_OPEN, TRUE ))
1337 RtlFreeAnsiString( &unix_name );
1338 if (p + 5 >= target + bufsize)
1340 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1341 return 0;
1343 strcpyW( p, comW );
1344 p[3] = '0' + i;
1345 p[4] = 0;
1346 p += 5;
1349 strcpyW( nt_buffer + 4, lptW );
1350 for (i = 1; i <= 9; i++)
1352 nt_buffer[7] = '0' + i;
1353 if (!wine_nt_to_unix_file_name( &nt_name, &unix_name, FILE_OPEN, TRUE ))
1355 RtlFreeAnsiString( &unix_name );
1356 if (p + 5 >= target + bufsize)
1358 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1359 return 0;
1361 strcpyW( p, lptW );
1362 p[3] = '0' + i;
1363 p[4] = 0;
1364 p += 5;
1368 RtlInitUnicodeString( &nt_name, dosdevW );
1369 nt_name.Length -= sizeof(WCHAR); /* without trailing slash */
1370 attr.Length = sizeof(attr);
1371 attr.RootDirectory = 0;
1372 attr.ObjectName = &nt_name;
1373 attr.Attributes = OBJ_CASE_INSENSITIVE;
1374 attr.SecurityDescriptor = NULL;
1375 attr.SecurityQualityOfService = NULL;
1376 status = NtOpenDirectoryObject( &handle, FILE_LIST_DIRECTORY, &attr );
1377 if (!status)
1379 char data[1024];
1380 DIRECTORY_BASIC_INFORMATION *info = (DIRECTORY_BASIC_INFORMATION *)data;
1381 ULONG ctx = 0, len;
1383 while (!NtQueryDirectoryObject( handle, info, sizeof(data), 1, 0, &ctx, &len ))
1385 if (p + info->ObjectName.Length/sizeof(WCHAR) + 1 >= target + bufsize)
1387 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1388 NtClose( handle );
1389 return 0;
1391 memcpy( p, info->ObjectName.Buffer, info->ObjectName.Length );
1392 p += info->ObjectName.Length/sizeof(WCHAR);
1393 *p++ = 0;
1395 NtClose( handle );
1398 *p++ = 0; /* terminating null */
1399 return p - target;
1404 /***********************************************************************
1405 * QueryDosDeviceA (KERNEL32.@)
1407 * returns array of strings terminated by \0, terminated by \0
1409 DWORD WINAPI QueryDosDeviceA( LPCSTR devname, LPSTR target, DWORD bufsize )
1411 DWORD ret = 0, retW;
1412 WCHAR *devnameW = NULL;
1413 LPWSTR targetW;
1415 if (devname && !(devnameW = FILE_name_AtoW( devname, FALSE ))) return 0;
1417 targetW = HeapAlloc( GetProcessHeap(),0, bufsize * sizeof(WCHAR) );
1418 if (!targetW)
1420 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1421 return 0;
1424 retW = QueryDosDeviceW(devnameW, targetW, bufsize);
1426 ret = FILE_name_WtoA( targetW, retW, target, bufsize );
1428 HeapFree(GetProcessHeap(), 0, targetW);
1429 return ret;
1433 /***********************************************************************
1434 * GetLogicalDrives (KERNEL32.@)
1436 DWORD WINAPI GetLogicalDrives(void)
1438 const char *config_dir = wine_get_config_dir();
1439 struct stat st;
1440 char *buffer, *dev;
1441 DWORD ret = 0;
1442 int i;
1444 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, strlen(config_dir) + sizeof("/dosdevices/a:") )))
1446 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1447 return 0;
1449 strcpy( buffer, config_dir );
1450 strcat( buffer, "/dosdevices/a:" );
1451 dev = buffer + strlen(buffer) - 2;
1453 for (i = 0; i < 26; i++)
1455 *dev = 'a' + i;
1456 if (!stat( buffer, &st )) ret |= (1 << i);
1458 HeapFree( GetProcessHeap(), 0, buffer );
1459 return ret;
1463 /***********************************************************************
1464 * GetLogicalDriveStringsA (KERNEL32.@)
1466 UINT WINAPI GetLogicalDriveStringsA( UINT len, LPSTR buffer )
1468 DWORD drives = GetLogicalDrives();
1469 UINT drive, count;
1471 for (drive = count = 0; drive < 26; drive++) if (drives & (1 << drive)) count++;
1472 if ((count * 4) + 1 > len) return count * 4 + 1;
1474 for (drive = 0; drive < 26; drive++)
1476 if (drives & (1 << drive))
1478 *buffer++ = 'A' + drive;
1479 *buffer++ = ':';
1480 *buffer++ = '\\';
1481 *buffer++ = 0;
1484 *buffer = 0;
1485 return count * 4;
1489 /***********************************************************************
1490 * GetLogicalDriveStringsW (KERNEL32.@)
1492 UINT WINAPI GetLogicalDriveStringsW( UINT len, LPWSTR buffer )
1494 DWORD drives = GetLogicalDrives();
1495 UINT drive, count;
1497 for (drive = count = 0; drive < 26; drive++) if (drives & (1 << drive)) count++;
1498 if ((count * 4) + 1 > len) return count * 4 + 1;
1500 for (drive = 0; drive < 26; drive++)
1502 if (drives & (1 << drive))
1504 *buffer++ = 'A' + drive;
1505 *buffer++ = ':';
1506 *buffer++ = '\\';
1507 *buffer++ = 0;
1510 *buffer = 0;
1511 return count * 4;
1515 /***********************************************************************
1516 * GetDriveTypeW (KERNEL32.@)
1518 * Returns the type of the disk drive specified. If root is NULL the
1519 * root of the current directory is used.
1521 * RETURNS
1523 * Type of drive (from Win32 SDK):
1525 * DRIVE_UNKNOWN unable to find out anything about the drive
1526 * DRIVE_NO_ROOT_DIR nonexistent root dir
1527 * DRIVE_REMOVABLE the disk can be removed from the machine
1528 * DRIVE_FIXED the disk cannot be removed from the machine
1529 * DRIVE_REMOTE network disk
1530 * DRIVE_CDROM CDROM drive
1531 * DRIVE_RAMDISK virtual disk in RAM
1533 UINT WINAPI GetDriveTypeW(LPCWSTR root) /* [in] String describing drive */
1535 FILE_FS_DEVICE_INFORMATION info;
1536 IO_STATUS_BLOCK io;
1537 NTSTATUS status;
1538 HANDLE handle;
1539 UINT ret;
1541 if (!open_device_root( root, &handle )) return DRIVE_NO_ROOT_DIR;
1543 status = NtQueryVolumeInformationFile( handle, &io, &info, sizeof(info), FileFsDeviceInformation );
1544 NtClose( handle );
1545 if (status != STATUS_SUCCESS)
1547 SetLastError( RtlNtStatusToDosError(status) );
1548 ret = DRIVE_UNKNOWN;
1550 else
1552 switch (info.DeviceType)
1554 case FILE_DEVICE_CD_ROM_FILE_SYSTEM: ret = DRIVE_CDROM; break;
1555 case FILE_DEVICE_VIRTUAL_DISK: ret = DRIVE_RAMDISK; break;
1556 case FILE_DEVICE_NETWORK_FILE_SYSTEM: ret = DRIVE_REMOTE; break;
1557 case FILE_DEVICE_DISK_FILE_SYSTEM:
1558 if (info.Characteristics & FILE_REMOTE_DEVICE) ret = DRIVE_REMOTE;
1559 else if (info.Characteristics & FILE_REMOVABLE_MEDIA) ret = DRIVE_REMOVABLE;
1560 else if ((ret = get_mountmgr_drive_type( root )) == DRIVE_UNKNOWN) ret = DRIVE_FIXED;
1561 break;
1562 default:
1563 ret = DRIVE_UNKNOWN;
1564 break;
1567 TRACE( "%s -> %d\n", debugstr_w(root), ret );
1568 return ret;
1572 /***********************************************************************
1573 * GetDriveTypeA (KERNEL32.@)
1575 * See GetDriveTypeW.
1577 UINT WINAPI GetDriveTypeA( LPCSTR root )
1579 WCHAR *rootW = NULL;
1581 if (root && !(rootW = FILE_name_AtoW( root, FALSE ))) return DRIVE_NO_ROOT_DIR;
1582 return GetDriveTypeW( rootW );
1586 /***********************************************************************
1587 * GetDiskFreeSpaceExW (KERNEL32.@)
1589 * This function is used to acquire the size of the available and
1590 * total space on a logical volume.
1592 * RETURNS
1594 * Zero on failure, nonzero upon success. Use GetLastError to obtain
1595 * detailed error information.
1598 BOOL WINAPI GetDiskFreeSpaceExW( LPCWSTR root, PULARGE_INTEGER avail,
1599 PULARGE_INTEGER total, PULARGE_INTEGER totalfree )
1601 FILE_FS_SIZE_INFORMATION info;
1602 IO_STATUS_BLOCK io;
1603 NTSTATUS status;
1604 HANDLE handle;
1605 UINT units;
1607 TRACE( "%s,%p,%p,%p\n", debugstr_w(root), avail, total, totalfree );
1609 if (!open_device_root( root, &handle )) return FALSE;
1611 status = NtQueryVolumeInformationFile( handle, &io, &info, sizeof(info), FileFsSizeInformation );
1612 NtClose( handle );
1613 if (status != STATUS_SUCCESS)
1615 SetLastError( RtlNtStatusToDosError(status) );
1616 return FALSE;
1619 units = info.SectorsPerAllocationUnit * info.BytesPerSector;
1620 if (total) total->QuadPart = info.TotalAllocationUnits.QuadPart * units;
1621 if (totalfree) totalfree->QuadPart = info.AvailableAllocationUnits.QuadPart * units;
1622 /* FIXME: this one should take quotas into account */
1623 if (avail) avail->QuadPart = info.AvailableAllocationUnits.QuadPart * units;
1624 return TRUE;
1628 /***********************************************************************
1629 * GetDiskFreeSpaceExA (KERNEL32.@)
1631 * See GetDiskFreeSpaceExW.
1633 BOOL WINAPI GetDiskFreeSpaceExA( LPCSTR root, PULARGE_INTEGER avail,
1634 PULARGE_INTEGER total, PULARGE_INTEGER totalfree )
1636 WCHAR *rootW = NULL;
1638 if (root && !(rootW = FILE_name_AtoW( root, FALSE ))) return FALSE;
1639 return GetDiskFreeSpaceExW( rootW, avail, total, totalfree );
1643 /***********************************************************************
1644 * GetDiskFreeSpaceW (KERNEL32.@)
1646 BOOL WINAPI GetDiskFreeSpaceW( LPCWSTR root, LPDWORD cluster_sectors,
1647 LPDWORD sector_bytes, LPDWORD free_clusters,
1648 LPDWORD total_clusters )
1650 FILE_FS_SIZE_INFORMATION info;
1651 IO_STATUS_BLOCK io;
1652 NTSTATUS status;
1653 HANDLE handle;
1654 UINT units;
1656 TRACE( "%s,%p,%p,%p,%p\n", debugstr_w(root),
1657 cluster_sectors, sector_bytes, free_clusters, total_clusters );
1659 if (!open_device_root( root, &handle )) return FALSE;
1661 status = NtQueryVolumeInformationFile( handle, &io, &info, sizeof(info), FileFsSizeInformation );
1662 NtClose( handle );
1663 if (status != STATUS_SUCCESS)
1665 SetLastError( RtlNtStatusToDosError(status) );
1666 return FALSE;
1669 units = info.SectorsPerAllocationUnit * info.BytesPerSector;
1671 if( GetVersion() & 0x80000000) { /* win3.x, 9x, ME */
1672 /* cap the size and available at 2GB as per specs */
1673 if (info.TotalAllocationUnits.QuadPart * units > 0x7fffffff) {
1674 info.TotalAllocationUnits.QuadPart = 0x7fffffff / units;
1675 if (info.AvailableAllocationUnits.QuadPart * units > 0x7fffffff)
1676 info.AvailableAllocationUnits.QuadPart = 0x7fffffff / units;
1678 /* nr. of clusters is always <= 65335 */
1679 while( info.TotalAllocationUnits.QuadPart > 65535 ) {
1680 info.TotalAllocationUnits.QuadPart /= 2;
1681 info.AvailableAllocationUnits.QuadPart /= 2;
1682 info.SectorsPerAllocationUnit *= 2;
1686 if (cluster_sectors) *cluster_sectors = info.SectorsPerAllocationUnit;
1687 if (sector_bytes) *sector_bytes = info.BytesPerSector;
1688 if (free_clusters) *free_clusters = info.AvailableAllocationUnits.u.LowPart;
1689 if (total_clusters) *total_clusters = info.TotalAllocationUnits.u.LowPart;
1690 return TRUE;
1694 /***********************************************************************
1695 * GetDiskFreeSpaceA (KERNEL32.@)
1697 BOOL WINAPI GetDiskFreeSpaceA( LPCSTR root, LPDWORD cluster_sectors,
1698 LPDWORD sector_bytes, LPDWORD free_clusters,
1699 LPDWORD total_clusters )
1701 WCHAR *rootW = NULL;
1703 if (root && !(rootW = FILE_name_AtoW( root, FALSE ))) return FALSE;
1704 return GetDiskFreeSpaceW( rootW, cluster_sectors, sector_bytes, free_clusters, total_clusters );
1707 /***********************************************************************
1708 * GetVolumePathNameA (KERNEL32.@)
1710 BOOL WINAPI GetVolumePathNameA(LPCSTR filename, LPSTR volumepathname, DWORD buflen)
1712 BOOL ret;
1713 WCHAR *filenameW = NULL, *volumeW;
1715 FIXME("(%s, %p, %d), stub!\n", debugstr_a(filename), volumepathname, buflen);
1717 if (filename && !(filenameW = FILE_name_AtoW( filename, FALSE ))) return FALSE;
1718 if (!(volumeW = HeapAlloc( GetProcessHeap(), 0, buflen * sizeof(WCHAR) ))) return FALSE;
1720 if ((ret = GetVolumePathNameW( filenameW, volumeW, buflen )))
1721 FILE_name_WtoA( volumeW, -1, volumepathname, buflen );
1723 HeapFree( GetProcessHeap(), 0, volumeW );
1724 return ret;
1727 /***********************************************************************
1728 * GetVolumePathNameW (KERNEL32.@)
1730 BOOL WINAPI GetVolumePathNameW(LPCWSTR filename, LPWSTR volumepathname, DWORD buflen)
1732 const WCHAR *p = filename;
1734 FIXME("(%s, %p, %d), stub!\n", debugstr_w(filename), volumepathname, buflen);
1736 if (p && tolowerW(p[0]) >= 'a' && tolowerW(p[0]) <= 'z' && p[1] ==':' && p[2] == '\\' && buflen >= 4)
1738 volumepathname[0] = p[0];
1739 volumepathname[1] = ':';
1740 volumepathname[2] = '\\';
1741 volumepathname[3] = 0;
1742 return TRUE;
1744 return FALSE;
1747 /***********************************************************************
1748 * GetVolumePathNamesForVolumeNameA (KERNEL32.@)
1750 BOOL WINAPI GetVolumePathNamesForVolumeNameA(LPCSTR volumename, LPSTR volumepathname, DWORD buflen, PDWORD returnlen)
1752 BOOL ret;
1753 WCHAR *volumenameW = NULL, *volumepathnameW;
1755 if (volumename && !(volumenameW = FILE_name_AtoW( volumename, TRUE ))) return FALSE;
1756 if (!(volumepathnameW = HeapAlloc( GetProcessHeap(), 0, buflen * sizeof(WCHAR) )))
1758 HeapFree( GetProcessHeap(), 0, volumenameW );
1759 return FALSE;
1761 if ((ret = GetVolumePathNamesForVolumeNameW( volumenameW, volumepathnameW, buflen, returnlen )))
1763 char *path = volumepathname;
1764 const WCHAR *pathW = volumepathnameW;
1766 while (*pathW)
1768 int len = strlenW( pathW ) + 1;
1769 FILE_name_WtoA( pathW, len, path, buflen );
1770 buflen -= len;
1771 pathW += len;
1772 path += len;
1774 path[0] = 0;
1776 HeapFree( GetProcessHeap(), 0, volumenameW );
1777 HeapFree( GetProcessHeap(), 0, volumepathnameW );
1778 return ret;
1781 static MOUNTMGR_MOUNT_POINTS *query_mount_points( HANDLE mgr, MOUNTMGR_MOUNT_POINT *input, DWORD insize )
1783 MOUNTMGR_MOUNT_POINTS *output;
1784 DWORD outsize = 1024;
1786 for (;;)
1788 if (!(output = HeapAlloc( GetProcessHeap(), 0, outsize )))
1790 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1791 return NULL;
1793 if (DeviceIoControl( mgr, IOCTL_MOUNTMGR_QUERY_POINTS, input, insize, output, outsize, NULL, NULL )) break;
1794 outsize = output->Size;
1795 HeapFree( GetProcessHeap(), 0, output );
1796 if (GetLastError() != ERROR_MORE_DATA) return NULL;
1798 return output;
1800 /***********************************************************************
1801 * GetVolumePathNamesForVolumeNameW (KERNEL32.@)
1803 BOOL WINAPI GetVolumePathNamesForVolumeNameW(LPCWSTR volumename, LPWSTR volumepathname, DWORD buflen, PDWORD returnlen)
1805 static const WCHAR dosdevicesW[] = {'\\','D','o','s','D','e','v','i','c','e','s','\\'};
1806 HANDLE mgr;
1807 DWORD len, size;
1808 MOUNTMGR_MOUNT_POINT *spec;
1809 MOUNTMGR_MOUNT_POINTS *link, *target = NULL;
1810 WCHAR *name, *path;
1811 BOOL ret = FALSE;
1812 UINT i, j;
1814 TRACE("%s, %p, %u, %p\n", debugstr_w(volumename), volumepathname, buflen, returnlen);
1816 if (!volumename || (len = strlenW( volumename )) != 49)
1818 SetLastError( ERROR_INVALID_NAME );
1819 return FALSE;
1821 mgr = CreateFileW( MOUNTMGR_DOS_DEVICE_NAME, 0, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0 );
1822 if (mgr == INVALID_HANDLE_VALUE) return FALSE;
1824 size = sizeof(*spec) + sizeof(WCHAR) * (len - 1); /* remove trailing backslash */
1825 if (!(spec = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size ))) goto done;
1826 spec->SymbolicLinkNameOffset = sizeof(*spec);
1827 spec->SymbolicLinkNameLength = size - sizeof(*spec);
1828 name = (WCHAR *)((char *)spec + spec->SymbolicLinkNameOffset);
1829 memcpy( name, volumename, size - sizeof(*spec) );
1830 name[1] = '?'; /* map \\?\ to \??\ */
1832 target = query_mount_points( mgr, spec, size );
1833 HeapFree( GetProcessHeap(), 0, spec );
1834 if (!target)
1836 goto done;
1838 if (!target->NumberOfMountPoints)
1840 SetLastError( ERROR_FILE_NOT_FOUND );
1841 goto done;
1843 len = 0;
1844 path = volumepathname;
1845 for (i = 0; i < target->NumberOfMountPoints; i++)
1847 link = NULL;
1848 if (target->MountPoints[i].DeviceNameOffset)
1850 const WCHAR *device = (const WCHAR *)((const char *)target + target->MountPoints[i].DeviceNameOffset);
1851 USHORT device_len = target->MountPoints[i].DeviceNameLength;
1853 size = sizeof(*spec) + device_len;
1854 if (!(spec = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size ))) goto done;
1855 spec->DeviceNameOffset = sizeof(*spec);
1856 spec->DeviceNameLength = device_len;
1857 memcpy( (char *)spec + spec->DeviceNameOffset, device, device_len );
1859 link = query_mount_points( mgr, spec, size );
1860 HeapFree( GetProcessHeap(), 0, spec );
1862 else if (target->MountPoints[i].UniqueIdOffset)
1864 const WCHAR *id = (const WCHAR *)((const char *)target + target->MountPoints[i].UniqueIdOffset);
1865 USHORT id_len = target->MountPoints[i].UniqueIdLength;
1867 size = sizeof(*spec) + id_len;
1868 if (!(spec = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size ))) goto done;
1869 spec->UniqueIdOffset = sizeof(*spec);
1870 spec->UniqueIdLength = id_len;
1871 memcpy( (char *)spec + spec->UniqueIdOffset, id, id_len );
1873 link = query_mount_points( mgr, spec, size );
1874 HeapFree( GetProcessHeap(), 0, spec );
1876 if (!link) continue;
1877 for (j = 0; j < link->NumberOfMountPoints; j++)
1879 const WCHAR *linkname;
1881 if (!link->MountPoints[j].SymbolicLinkNameOffset) continue;
1882 linkname = (const WCHAR *)((const char *)link + link->MountPoints[j].SymbolicLinkNameOffset);
1884 if (link->MountPoints[j].SymbolicLinkNameLength == sizeof(dosdevicesW) + 2 * sizeof(WCHAR) &&
1885 !memicmpW( linkname, dosdevicesW, sizeof(dosdevicesW) / sizeof(WCHAR) ))
1887 len += 4;
1888 if (volumepathname && len < buflen)
1890 path[0] = linkname[sizeof(dosdevicesW) / sizeof(WCHAR)];
1891 path[1] = ':';
1892 path[2] = '\\';
1893 path[3] = 0;
1894 path += 4;
1898 HeapFree( GetProcessHeap(), 0, link );
1900 if (buflen <= len) SetLastError( ERROR_MORE_DATA );
1901 else if (volumepathname)
1903 volumepathname[len] = 0;
1904 ret = TRUE;
1906 if (returnlen) *returnlen = len + 1;
1908 done:
1909 HeapFree( GetProcessHeap(), 0, target );
1910 CloseHandle( mgr );
1911 return ret;
1914 /***********************************************************************
1915 * FindFirstVolumeA (KERNEL32.@)
1917 HANDLE WINAPI FindFirstVolumeA(LPSTR volume, DWORD len)
1919 WCHAR *buffer = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1920 HANDLE handle = FindFirstVolumeW( buffer, len );
1922 if (handle != INVALID_HANDLE_VALUE)
1924 if (!WideCharToMultiByte( CP_ACP, 0, buffer, -1, volume, len, NULL, NULL ))
1926 FindVolumeClose( handle );
1927 handle = INVALID_HANDLE_VALUE;
1930 HeapFree( GetProcessHeap(), 0, buffer );
1931 return handle;
1934 /***********************************************************************
1935 * FindFirstVolumeW (KERNEL32.@)
1937 HANDLE WINAPI FindFirstVolumeW( LPWSTR volume, DWORD len )
1939 DWORD size = 1024;
1940 HANDLE mgr = CreateFileW( MOUNTMGR_DOS_DEVICE_NAME, 0, FILE_SHARE_READ|FILE_SHARE_WRITE,
1941 NULL, OPEN_EXISTING, 0, 0 );
1942 if (mgr == INVALID_HANDLE_VALUE) return INVALID_HANDLE_VALUE;
1944 for (;;)
1946 MOUNTMGR_MOUNT_POINT input;
1947 MOUNTMGR_MOUNT_POINTS *output;
1949 if (!(output = HeapAlloc( GetProcessHeap(), 0, size )))
1951 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1952 break;
1954 memset( &input, 0, sizeof(input) );
1956 if (!DeviceIoControl( mgr, IOCTL_MOUNTMGR_QUERY_POINTS, &input, sizeof(input),
1957 output, size, NULL, NULL ))
1959 if (GetLastError() != ERROR_MORE_DATA) break;
1960 size = output->Size;
1961 HeapFree( GetProcessHeap(), 0, output );
1962 continue;
1964 CloseHandle( mgr );
1965 /* abuse the Size field to store the current index */
1966 output->Size = 0;
1967 if (!FindNextVolumeW( output, volume, len ))
1969 HeapFree( GetProcessHeap(), 0, output );
1970 return INVALID_HANDLE_VALUE;
1972 return output;
1974 CloseHandle( mgr );
1975 return INVALID_HANDLE_VALUE;
1978 /***********************************************************************
1979 * FindNextVolumeA (KERNEL32.@)
1981 BOOL WINAPI FindNextVolumeA( HANDLE handle, LPSTR volume, DWORD len )
1983 WCHAR *buffer = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1984 BOOL ret;
1986 if ((ret = FindNextVolumeW( handle, buffer, len )))
1988 if (!WideCharToMultiByte( CP_ACP, 0, buffer, -1, volume, len, NULL, NULL )) ret = FALSE;
1990 HeapFree( GetProcessHeap(), 0, buffer );
1991 return ret;
1994 /***********************************************************************
1995 * FindNextVolumeW (KERNEL32.@)
1997 BOOL WINAPI FindNextVolumeW( HANDLE handle, LPWSTR volume, DWORD len )
1999 MOUNTMGR_MOUNT_POINTS *data = handle;
2001 while (data->Size < data->NumberOfMountPoints)
2003 static const WCHAR volumeW[] = {'\\','?','?','\\','V','o','l','u','m','e','{',};
2004 WCHAR *link = (WCHAR *)((char *)data + data->MountPoints[data->Size].SymbolicLinkNameOffset);
2005 DWORD size = data->MountPoints[data->Size].SymbolicLinkNameLength;
2006 data->Size++;
2007 /* skip non-volumes */
2008 if (size < sizeof(volumeW) || memcmp( link, volumeW, sizeof(volumeW) )) continue;
2009 if (size + sizeof(WCHAR) >= len * sizeof(WCHAR))
2011 SetLastError( ERROR_FILENAME_EXCED_RANGE );
2012 return FALSE;
2014 memcpy( volume, link, size );
2015 volume[1] = '\\'; /* map \??\ to \\?\ */
2016 volume[size / sizeof(WCHAR)] = '\\'; /* Windows appends a backslash */
2017 volume[size / sizeof(WCHAR) + 1] = 0;
2018 TRACE( "returning entry %u %s\n", data->Size - 1, debugstr_w(volume) );
2019 return TRUE;
2021 SetLastError( ERROR_NO_MORE_FILES );
2022 return FALSE;
2025 /***********************************************************************
2026 * FindVolumeClose (KERNEL32.@)
2028 BOOL WINAPI FindVolumeClose(HANDLE handle)
2030 return HeapFree( GetProcessHeap(), 0, handle );
2033 /***********************************************************************
2034 * FindFirstVolumeMountPointA (KERNEL32.@)
2036 HANDLE WINAPI FindFirstVolumeMountPointA(LPCSTR root, LPSTR mount_point, DWORD len)
2038 FIXME("(%s, %p, %d), stub!\n", debugstr_a(root), mount_point, len);
2039 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
2040 return INVALID_HANDLE_VALUE;
2043 /***********************************************************************
2044 * FindFirstVolumeMountPointW (KERNEL32.@)
2046 HANDLE WINAPI FindFirstVolumeMountPointW(LPCWSTR root, LPWSTR mount_point, DWORD len)
2048 FIXME("(%s, %p, %d), stub!\n", debugstr_w(root), mount_point, len);
2049 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
2050 return INVALID_HANDLE_VALUE;
2053 /***********************************************************************
2054 * FindVolumeMountPointClose (KERNEL32.@)
2056 BOOL WINAPI FindVolumeMountPointClose(HANDLE h)
2058 FIXME("(%p), stub!\n", h);
2059 return FALSE;
2062 /***********************************************************************
2063 * DeleteVolumeMountPointA (KERNEL32.@)
2065 BOOL WINAPI DeleteVolumeMountPointA(LPCSTR mountpoint)
2067 FIXME("(%s), stub!\n", debugstr_a(mountpoint));
2068 return FALSE;
2071 /***********************************************************************
2072 * DeleteVolumeMountPointW (KERNEL32.@)
2074 BOOL WINAPI DeleteVolumeMountPointW(LPCWSTR mountpoint)
2076 FIXME("(%s), stub!\n", debugstr_w(mountpoint));
2077 return FALSE;