wbemprox: Implement Win32_CDROMDrive.MediaType.
[wine/wine-gecko.git] / dlls / kernel32 / volume.c
blob1509d73f8381ad4d4f97b23ab36ea2e347205041
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 unsigned 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 unsigned 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 * UDF_Find_FSD_Sector
571 * Find the File Set Descriptor used to compute the serial of a UDF volume
573 static int UDF_Find_FSD_Sector( HANDLE handle, BYTE block[] )
575 int i, PVD_sector, PD_sector, PD_length;
577 if(!UDF_Find_PVD(handle,block))
578 goto default_sector;
580 /* Retrieve the tag location of the PVD -- [E] 3/7.2 */
581 PVD_sector = block[12 + 0];
582 PVD_sector |= block[12 + 1] << 8;
583 PVD_sector |= block[12 + 2] << 16;
584 PVD_sector |= block[12 + 3] << 24;
586 /* Find the Partition Descriptor */
587 for(i=PVD_sector+1; ; i++)
589 if(!VOLUME_ReadCDBlock(handle, block, i*BLOCK_SIZE))
590 goto default_sector;
592 /* Partition Descriptor Tag Id -- [E] 3/10.5.1 */
593 if(block[0]==5 && block[1]==0)
594 break;
596 /* Terminating Descriptor Tag Id -- [E] 3/10.9.1 */
597 if(block[0]==8 && block[1]==0)
598 goto default_sector;
601 /* Find the partition starting location -- [E] 3/10.5.8 */
602 PD_sector = block[188 + 0];
603 PD_sector |= block[188 + 1] << 8;
604 PD_sector |= block[188 + 2] << 16;
605 PD_sector |= block[188 + 3] << 24;
607 /* Find the partition length -- [E] 3/10.5.9 */
608 PD_length = block[192 + 0];
609 PD_length |= block[192 + 1] << 8;
610 PD_length |= block[192 + 2] << 16;
611 PD_length |= block[192 + 3] << 24;
613 for(i=PD_sector; i<PD_sector+PD_length; i++)
615 if(!VOLUME_ReadCDBlock(handle, block, i*BLOCK_SIZE))
616 goto default_sector;
618 /* File Set Descriptor Tag Id -- [E] 3/14.1.1 */
619 if(block[0]==0 && block[1]==1)
620 return i;
623 default_sector:
624 WARN("FSD sector not found, serial may be incorrect\n");
625 return 257;
629 /**************************************************************************
630 * VOLUME_GetSuperblockSerial
632 static DWORD VOLUME_GetSuperblockSerial( const UNICODE_STRING *device, HANDLE handle,
633 enum fs_type type, const BYTE *superblock )
635 int FSD_sector;
636 BYTE block[BLOCK_SIZE];
638 switch(type)
640 case FS_ERROR:
641 break;
642 case FS_UNKNOWN:
643 return get_filesystem_serial( device );
644 case FS_FAT1216:
645 return GETLONG( superblock, 0x27 );
646 case FS_FAT32:
647 return GETLONG( superblock, 0x33 );
648 case FS_UDF:
649 FSD_sector = UDF_Find_FSD_Sector(handle, block);
650 if (!VOLUME_ReadCDBlock(handle, block, FSD_sector*BLOCK_SIZE))
651 break;
652 superblock = block;
653 /* fallthrough */
654 case FS_ISO9660:
656 BYTE sum[4];
657 int i;
659 sum[0] = sum[1] = sum[2] = sum[3] = 0;
660 for (i = 0; i < 2048; i += 4)
662 /* DON'T optimize this into DWORD !! (breaks overflow) */
663 sum[0] += superblock[i+0];
664 sum[1] += superblock[i+1];
665 sum[2] += superblock[i+2];
666 sum[3] += superblock[i+3];
669 * OK, another braindead one... argh. Just believe it.
670 * Me$$ysoft chose to reverse the serial number in NT4/W2K.
671 * It's true and nobody will ever be able to change it.
673 if ((GetVersion() & 0x80000000) || type == FS_UDF)
674 return (sum[3] << 24) | (sum[2] << 16) | (sum[1] << 8) | sum[0];
675 else
676 return (sum[0] << 24) | (sum[1] << 16) | (sum[2] << 8) | sum[3];
679 return 0;
683 /**************************************************************************
684 * VOLUME_GetAudioCDSerial
686 static DWORD VOLUME_GetAudioCDSerial( const CDROM_TOC *toc )
688 DWORD serial = 0;
689 int i;
691 for (i = 0; i <= toc->LastTrack - toc->FirstTrack; i++)
692 serial += ((toc->TrackData[i].Address[1] << 16) |
693 (toc->TrackData[i].Address[2] << 8) |
694 toc->TrackData[i].Address[3]);
697 * dwStart, dwEnd collect the beginning and end of the disc respectively, in
698 * frames.
699 * There it is collected for correcting the serial when there are less than
700 * 3 tracks.
702 if (toc->LastTrack - toc->FirstTrack + 1 < 3)
704 DWORD dwStart = FRAME_OF_TOC(toc, toc->FirstTrack);
705 DWORD dwEnd = FRAME_OF_TOC(toc, toc->LastTrack + 1);
706 serial += dwEnd - dwStart;
708 return serial;
712 /***********************************************************************
713 * GetVolumeInformationW (KERNEL32.@)
715 BOOL WINAPI GetVolumeInformationW( LPCWSTR root, LPWSTR label, DWORD label_len,
716 DWORD *serial, DWORD *filename_len, DWORD *flags,
717 LPWSTR fsname, DWORD fsname_len )
719 static const WCHAR audiocdW[] = {'A','u','d','i','o',' ','C','D',0};
720 static const WCHAR fatW[] = {'F','A','T',0};
721 static const WCHAR fat32W[] = {'F','A','T','3','2',0};
722 static const WCHAR ntfsW[] = {'N','T','F','S',0};
723 static const WCHAR cdfsW[] = {'C','D','F','S',0};
724 static const WCHAR udfW[] = {'U','D','F',0};
725 static const WCHAR default_rootW[] = {'\\',0};
727 HANDLE handle;
728 NTSTATUS status;
729 UNICODE_STRING nt_name;
730 IO_STATUS_BLOCK io;
731 OBJECT_ATTRIBUTES attr;
732 FILE_FS_DEVICE_INFORMATION info;
733 WCHAR *p;
734 enum fs_type type = FS_UNKNOWN;
735 BOOL ret = FALSE;
737 if (!root) root = default_rootW;
738 if (!RtlDosPathNameToNtPathName_U( root, &nt_name, NULL, NULL ))
740 SetLastError( ERROR_PATH_NOT_FOUND );
741 return FALSE;
743 /* there must be exactly one backslash in the name, at the end */
744 p = memchrW( nt_name.Buffer + 4, '\\', (nt_name.Length - 4) / sizeof(WCHAR) );
745 if (p != nt_name.Buffer + nt_name.Length / sizeof(WCHAR) - 1)
747 /* check if root contains an explicit subdir */
748 if (root[0] && root[1] == ':') root += 2;
749 while (*root == '\\') root++;
750 if (strchrW( root, '\\' ))
751 SetLastError( ERROR_DIR_NOT_ROOT );
752 else
753 SetLastError( ERROR_INVALID_NAME );
754 goto done;
757 /* try to open the device */
759 attr.Length = sizeof(attr);
760 attr.RootDirectory = 0;
761 attr.Attributes = OBJ_CASE_INSENSITIVE;
762 attr.ObjectName = &nt_name;
763 attr.SecurityDescriptor = NULL;
764 attr.SecurityQualityOfService = NULL;
766 nt_name.Length -= sizeof(WCHAR); /* without trailing slash */
767 status = NtOpenFile( &handle, GENERIC_READ, &attr, &io, FILE_SHARE_READ | FILE_SHARE_WRITE,
768 FILE_NON_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT );
769 nt_name.Length += sizeof(WCHAR);
771 if (status == STATUS_SUCCESS)
773 BYTE superblock[SUPERBLOCK_SIZE];
774 CDROM_TOC toc;
775 DWORD br;
777 /* check for audio CD */
778 /* FIXME: we only check the first track for now */
779 if (DeviceIoControl( handle, IOCTL_CDROM_READ_TOC, NULL, 0, &toc, sizeof(toc), &br, 0 ))
781 if (!(toc.TrackData[0].Control & 0x04)) /* audio track */
783 TRACE( "%s: found audio CD\n", debugstr_w(nt_name.Buffer) );
784 if (label) lstrcpynW( label, audiocdW, label_len );
785 if (serial) *serial = VOLUME_GetAudioCDSerial( &toc );
786 CloseHandle( handle );
787 type = FS_ISO9660;
788 goto fill_fs_info;
790 type = VOLUME_ReadCDSuperblock( handle, superblock );
792 else
794 type = VOLUME_ReadFATSuperblock( handle, superblock );
795 if (type == FS_UNKNOWN) type = VOLUME_ReadCDSuperblock( handle, superblock );
797 TRACE( "%s: found fs type %d\n", debugstr_w(nt_name.Buffer), type );
798 if (type == FS_ERROR)
800 CloseHandle( handle );
801 goto done;
804 if (label && label_len) VOLUME_GetSuperblockLabel( &nt_name, handle, type, superblock, label, label_len );
805 if (serial) *serial = VOLUME_GetSuperblockSerial( &nt_name, handle, type, superblock );
806 CloseHandle( handle );
807 goto fill_fs_info;
809 else TRACE( "cannot open device %s: %x\n", debugstr_w(nt_name.Buffer), status );
811 /* we couldn't open the device, fallback to default strategy */
813 status = NtOpenFile( &handle, 0, &attr, &io, 0, FILE_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT );
814 if (status != STATUS_SUCCESS)
816 SetLastError( RtlNtStatusToDosError(status) );
817 goto done;
819 status = NtQueryVolumeInformationFile( handle, &io, &info, sizeof(info), FileFsDeviceInformation );
820 NtClose( handle );
821 if (status != STATUS_SUCCESS)
823 SetLastError( RtlNtStatusToDosError(status) );
824 goto done;
826 if (info.DeviceType == FILE_DEVICE_CD_ROM_FILE_SYSTEM) type = FS_ISO9660;
828 if (label && label_len) get_filesystem_label( &nt_name, label, label_len );
829 if (serial) *serial = get_filesystem_serial( &nt_name );
831 fill_fs_info: /* now fill in the information that depends on the file system type */
833 switch(type)
835 case FS_ISO9660:
836 if (fsname) lstrcpynW( fsname, cdfsW, fsname_len );
837 if (filename_len) *filename_len = 221;
838 if (flags) *flags = FILE_READ_ONLY_VOLUME;
839 break;
840 case FS_UDF:
841 if (fsname) lstrcpynW( fsname, udfW, fsname_len );
842 if (filename_len) *filename_len = 255;
843 if (flags)
844 *flags = FILE_READ_ONLY_VOLUME | FILE_UNICODE_ON_DISK | FILE_CASE_SENSITIVE_SEARCH;
845 break;
846 case FS_FAT1216:
847 if (fsname) lstrcpynW( fsname, fatW, fsname_len );
848 case FS_FAT32:
849 if (type == FS_FAT32 && fsname) lstrcpynW( fsname, fat32W, fsname_len );
850 if (filename_len) *filename_len = 255;
851 if (flags) *flags = FILE_CASE_PRESERVED_NAMES; /* FIXME */
852 break;
853 default:
854 if (fsname) lstrcpynW( fsname, ntfsW, fsname_len );
855 if (filename_len) *filename_len = 255;
856 if (flags) *flags = FILE_CASE_PRESERVED_NAMES | FILE_PERSISTENT_ACLS;
857 break;
859 ret = TRUE;
861 done:
862 RtlFreeUnicodeString( &nt_name );
863 return ret;
867 /***********************************************************************
868 * GetVolumeInformationA (KERNEL32.@)
870 BOOL WINAPI GetVolumeInformationA( LPCSTR root, LPSTR label,
871 DWORD label_len, DWORD *serial,
872 DWORD *filename_len, DWORD *flags,
873 LPSTR fsname, DWORD fsname_len )
875 WCHAR *rootW = NULL;
876 LPWSTR labelW, fsnameW;
877 BOOL ret;
879 if (root && !(rootW = FILE_name_AtoW( root, FALSE ))) return FALSE;
881 labelW = label ? HeapAlloc(GetProcessHeap(), 0, label_len * sizeof(WCHAR)) : NULL;
882 fsnameW = fsname ? HeapAlloc(GetProcessHeap(), 0, fsname_len * sizeof(WCHAR)) : NULL;
884 if ((ret = GetVolumeInformationW(rootW, labelW, label_len, serial,
885 filename_len, flags, fsnameW, fsname_len)))
887 if (label) FILE_name_WtoA( labelW, -1, label, label_len );
888 if (fsname) FILE_name_WtoA( fsnameW, -1, fsname, fsname_len );
891 HeapFree( GetProcessHeap(), 0, labelW );
892 HeapFree( GetProcessHeap(), 0, fsnameW );
893 return ret;
898 /***********************************************************************
899 * SetVolumeLabelW (KERNEL32.@)
901 BOOL WINAPI SetVolumeLabelW( LPCWSTR root, LPCWSTR label )
903 WCHAR device[] = {'\\','\\','.','\\','A',':',0};
904 HANDLE handle;
905 enum fs_type type = FS_UNKNOWN;
907 if (!root)
909 WCHAR path[MAX_PATH];
910 GetCurrentDirectoryW( MAX_PATH, path );
911 device[4] = path[0];
913 else
915 if (!root[0] || root[1] != ':')
917 SetLastError( ERROR_INVALID_NAME );
918 return FALSE;
920 device[4] = root[0];
923 /* try to open the device */
925 handle = CreateFileW( device, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE,
926 NULL, OPEN_EXISTING, 0, 0 );
927 if (handle != INVALID_HANDLE_VALUE)
929 BYTE superblock[SUPERBLOCK_SIZE];
931 type = VOLUME_ReadFATSuperblock( handle, superblock );
932 if (type == FS_UNKNOWN) type = VOLUME_ReadCDSuperblock( handle, superblock );
933 CloseHandle( handle );
934 if (type != FS_UNKNOWN)
936 /* we can't set the label on FAT or CDROM file systems */
937 TRACE( "cannot set label on device %s type %d\n", debugstr_w(device), type );
938 SetLastError( ERROR_ACCESS_DENIED );
939 return FALSE;
942 else
944 TRACE( "cannot open device %s: err %d\n", debugstr_w(device), GetLastError() );
945 if (GetLastError() == ERROR_ACCESS_DENIED) return FALSE;
948 /* we couldn't open the device, fallback to default strategy */
950 switch(GetDriveTypeW( root ))
952 case DRIVE_UNKNOWN:
953 case DRIVE_NO_ROOT_DIR:
954 SetLastError( ERROR_NOT_READY );
955 break;
956 case DRIVE_REMOVABLE:
957 case DRIVE_FIXED:
959 WCHAR labelW[] = {'A',':','\\','.','w','i','n','d','o','w','s','-','l','a','b','e','l',0};
961 labelW[0] = device[4];
963 if (!label[0]) /* delete label file when setting an empty label */
964 return DeleteFileW( labelW ) || GetLastError() == ERROR_FILE_NOT_FOUND;
966 handle = CreateFileW( labelW, GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
967 CREATE_ALWAYS, 0, 0 );
968 if (handle != INVALID_HANDLE_VALUE)
970 char buffer[64];
971 DWORD size;
973 if (!WideCharToMultiByte( CP_UNIXCP, 0, label, -1, buffer, sizeof(buffer)-1, NULL, NULL ))
974 buffer[sizeof(buffer)-2] = 0;
975 strcat( buffer, "\n" );
976 WriteFile( handle, buffer, strlen(buffer), &size, NULL );
977 CloseHandle( handle );
978 return TRUE;
980 break;
982 case DRIVE_REMOTE:
983 case DRIVE_RAMDISK:
984 case DRIVE_CDROM:
985 SetLastError( ERROR_ACCESS_DENIED );
986 break;
988 return FALSE;
991 /***********************************************************************
992 * SetVolumeLabelA (KERNEL32.@)
994 BOOL WINAPI SetVolumeLabelA(LPCSTR root, LPCSTR volname)
996 WCHAR *rootW = NULL, *volnameW = NULL;
997 BOOL ret;
999 if (root && !(rootW = FILE_name_AtoW( root, FALSE ))) return FALSE;
1000 if (volname && !(volnameW = FILE_name_AtoW( volname, TRUE ))) return FALSE;
1001 ret = SetVolumeLabelW( rootW, volnameW );
1002 HeapFree( GetProcessHeap(), 0, volnameW );
1003 return ret;
1007 /***********************************************************************
1008 * GetVolumeNameForVolumeMountPointA (KERNEL32.@)
1010 BOOL WINAPI GetVolumeNameForVolumeMountPointA( LPCSTR path, LPSTR volume, DWORD size )
1012 BOOL ret;
1013 WCHAR volumeW[50], *pathW = NULL;
1014 DWORD len = min( sizeof(volumeW) / sizeof(WCHAR), size );
1016 TRACE("(%s, %p, %x)\n", debugstr_a(path), volume, size);
1018 if (!path || !(pathW = FILE_name_AtoW( path, TRUE )))
1019 return FALSE;
1021 if ((ret = GetVolumeNameForVolumeMountPointW( pathW, volumeW, len )))
1022 FILE_name_WtoA( volumeW, -1, volume, len );
1024 HeapFree( GetProcessHeap(), 0, pathW );
1025 return ret;
1028 /***********************************************************************
1029 * GetVolumeNameForVolumeMountPointW (KERNEL32.@)
1031 BOOL WINAPI GetVolumeNameForVolumeMountPointW( LPCWSTR path, LPWSTR volume, DWORD size )
1033 static const WCHAR prefixW[] = {'\\','D','o','s','D','e','v','i','c','e','s','\\',0};
1034 static const WCHAR volumeW[] = {'\\','?','?','\\','V','o','l','u','m','e','{'};
1035 static const WCHAR trailingW[] = {'\\',0};
1037 MOUNTMGR_MOUNT_POINT *input = NULL, *o1;
1038 MOUNTMGR_MOUNT_POINTS *output = NULL;
1039 WCHAR *p;
1040 char *r;
1041 DWORD i, i_size = 1024, o_size = 1024;
1042 WCHAR *nonpersist_name;
1043 WCHAR symlink_name[MAX_PATH];
1044 NTSTATUS status;
1045 HANDLE mgr = INVALID_HANDLE_VALUE;
1046 BOOL ret = FALSE;
1048 TRACE("(%s, %p, %x)\n", debugstr_w(path), volume, size);
1049 if (path[lstrlenW(path)-1] != '\\')
1051 SetLastError( ERROR_INVALID_NAME );
1052 return FALSE;
1055 if (size < 50)
1057 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1058 return FALSE;
1060 /* if length of input is > 3 then it must be a mounted folder */
1061 if (lstrlenW(path) > 3)
1063 FIXME("Mounted Folders are not yet supported\n");
1064 SetLastError( ERROR_NOT_A_REPARSE_POINT );
1065 return FALSE;
1068 mgr = CreateFileW( MOUNTMGR_DOS_DEVICE_NAME, 0, FILE_SHARE_READ,
1069 NULL, OPEN_EXISTING, 0, 0 );
1070 if (mgr == INVALID_HANDLE_VALUE) return FALSE;
1072 if (!(input = HeapAlloc( GetProcessHeap(), 0, i_size )))
1074 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1075 goto err_ret;
1078 if (!(output = HeapAlloc( GetProcessHeap(), 0, o_size )))
1080 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1081 goto err_ret;
1084 /* construct the symlink name as "\DosDevices\C:" */
1085 lstrcpyW( symlink_name, prefixW );
1086 lstrcatW( symlink_name, path );
1087 symlink_name[lstrlenW(symlink_name)-1] = 0;
1089 /* Take the mount point and get the "nonpersistent name" */
1090 /* We will then take that and get the volume name */
1091 nonpersist_name = (WCHAR *)(input + 1);
1092 status = read_nt_symlink( symlink_name, nonpersist_name, i_size - sizeof(*input) );
1093 TRACE("read_nt_symlink got stat=%x, for %s, got <%s>\n", status,
1094 debugstr_w(symlink_name), debugstr_w(nonpersist_name));
1095 if (status != STATUS_SUCCESS)
1097 SetLastError( ERROR_FILE_NOT_FOUND );
1098 goto err_ret;
1101 /* Now take the "nonpersistent name" and ask the mountmgr */
1102 /* to give us all the mount points. One of them will be */
1103 /* the volume name (format of \??\Volume{). */
1104 memset( input, 0, sizeof(*input) ); /* clear all input parameters */
1105 input->DeviceNameOffset = sizeof(*input);
1106 input->DeviceNameLength = lstrlenW( nonpersist_name) * sizeof(WCHAR);
1107 i_size = input->DeviceNameOffset + input->DeviceNameLength;
1109 output->Size = o_size;
1111 /* now get the true volume name from the mountmgr */
1112 if (!DeviceIoControl( mgr, IOCTL_MOUNTMGR_QUERY_POINTS, input, i_size,
1113 output, o_size, NULL, NULL ))
1114 goto err_ret;
1116 /* Verify and return the data, note string is not null terminated */
1117 TRACE("found %d matching mount points\n", output->NumberOfMountPoints);
1118 if (output->NumberOfMountPoints < 1)
1120 SetLastError( ERROR_NO_VOLUME_ID );
1121 goto err_ret;
1123 o1 = &output->MountPoints[0];
1125 /* look for the volume name in returned values */
1126 for(i=0;i<output->NumberOfMountPoints;i++)
1128 p = (WCHAR*)((char *)output + o1->SymbolicLinkNameOffset);
1129 r = (char *)output + o1->UniqueIdOffset;
1130 TRACE("found symlink=%s, unique=%s, devname=%s\n",
1131 debugstr_wn(p, o1->SymbolicLinkNameLength/sizeof(WCHAR)),
1132 debugstr_an(r, o1->UniqueIdLength),
1133 debugstr_wn((WCHAR*)((char *)output + o1->DeviceNameOffset),
1134 o1->DeviceNameLength/sizeof(WCHAR)));
1136 if (!strncmpW( p, volumeW, sizeof(volumeW)/sizeof(WCHAR) ))
1138 /* is there space in the return variable ?? */
1139 if ((o1->SymbolicLinkNameLength/sizeof(WCHAR))+2 > size)
1141 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1142 goto err_ret;
1144 memcpy( volume, p, o1->SymbolicLinkNameLength );
1145 volume[o1->SymbolicLinkNameLength / sizeof(WCHAR)] = 0;
1146 lstrcatW( volume, trailingW );
1147 /* change second char from '?' to '\' */
1148 volume[1] = '\\';
1149 ret = TRUE;
1150 break;
1152 o1++;
1155 err_ret:
1156 HeapFree( GetProcessHeap(), 0, input );
1157 HeapFree( GetProcessHeap(), 0, output );
1158 CloseHandle( mgr );
1159 return ret;
1162 /***********************************************************************
1163 * DefineDosDeviceW (KERNEL32.@)
1165 BOOL WINAPI DefineDosDeviceW( DWORD flags, LPCWSTR devname, LPCWSTR targetpath )
1167 DWORD len, dosdev;
1168 BOOL ret = FALSE;
1169 char *path = NULL, *target, *p;
1171 TRACE("%x, %s, %s\n", flags, debugstr_w(devname), debugstr_w(targetpath));
1173 if (!(flags & DDD_REMOVE_DEFINITION))
1175 if (!(flags & DDD_RAW_TARGET_PATH))
1177 FIXME( "(0x%08x,%s,%s) DDD_RAW_TARGET_PATH flag not set, not supported yet\n",
1178 flags, debugstr_w(devname), debugstr_w(targetpath) );
1179 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1180 return FALSE;
1183 len = WideCharToMultiByte( CP_UNIXCP, 0, targetpath, -1, NULL, 0, NULL, NULL );
1184 if ((target = HeapAlloc( GetProcessHeap(), 0, len )))
1186 WideCharToMultiByte( CP_UNIXCP, 0, targetpath, -1, target, len, NULL, NULL );
1187 for (p = target; *p; p++) if (*p == '\\') *p = '/';
1189 else
1191 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1192 return FALSE;
1195 else target = NULL;
1197 /* first check for a DOS device */
1199 if ((dosdev = RtlIsDosDeviceName_U( devname )))
1201 WCHAR name[5];
1203 memcpy( name, devname + HIWORD(dosdev)/sizeof(WCHAR), LOWORD(dosdev) );
1204 name[LOWORD(dosdev)/sizeof(WCHAR)] = 0;
1205 path = get_dos_device_path( name );
1207 else if (isalphaW(devname[0]) && devname[1] == ':' && !devname[2]) /* drive mapping */
1209 path = get_dos_device_path( devname );
1211 else SetLastError( ERROR_FILE_NOT_FOUND );
1213 if (path)
1215 if (target)
1217 TRACE( "creating symlink %s -> %s\n", path, target );
1218 unlink( path );
1219 if (!symlink( target, path )) ret = TRUE;
1220 else FILE_SetDosError();
1222 else
1224 TRACE( "removing symlink %s\n", path );
1225 if (!unlink( path )) ret = TRUE;
1226 else FILE_SetDosError();
1228 HeapFree( GetProcessHeap(), 0, path );
1230 HeapFree( GetProcessHeap(), 0, target );
1231 return ret;
1235 /***********************************************************************
1236 * DefineDosDeviceA (KERNEL32.@)
1238 BOOL WINAPI DefineDosDeviceA(DWORD flags, LPCSTR devname, LPCSTR targetpath)
1240 WCHAR *devW, *targetW = NULL;
1241 BOOL ret;
1243 if (!(devW = FILE_name_AtoW( devname, FALSE ))) return FALSE;
1244 if (targetpath && !(targetW = FILE_name_AtoW( targetpath, TRUE ))) return FALSE;
1245 ret = DefineDosDeviceW(flags, devW, targetW);
1246 HeapFree( GetProcessHeap(), 0, targetW );
1247 return ret;
1251 /***********************************************************************
1252 * QueryDosDeviceW (KERNEL32.@)
1254 * returns array of strings terminated by \0, terminated by \0
1256 DWORD WINAPI QueryDosDeviceW( LPCWSTR devname, LPWSTR target, DWORD bufsize )
1258 static const WCHAR auxW[] = {'A','U','X',0};
1259 static const WCHAR nulW[] = {'N','U','L',0};
1260 static const WCHAR prnW[] = {'P','R','N',0};
1261 static const WCHAR comW[] = {'C','O','M',0};
1262 static const WCHAR lptW[] = {'L','P','T',0};
1263 static const WCHAR com0W[] = {'\\','?','?','\\','C','O','M','0',0};
1264 static const WCHAR com1W[] = {'\\','D','o','s','D','e','v','i','c','e','s','\\','C','O','M','1',0,0};
1265 static const WCHAR lpt1W[] = {'\\','D','o','s','D','e','v','i','c','e','s','\\','L','P','T','1',0,0};
1266 static const WCHAR dosdevW[] = {'\\','D','o','s','D','e','v','i','c','e','s','\\',0};
1268 UNICODE_STRING nt_name;
1269 ANSI_STRING unix_name;
1270 WCHAR nt_buffer[10];
1271 NTSTATUS status;
1273 if (!bufsize)
1275 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1276 return 0;
1279 if (devname)
1281 WCHAR *p, name[5];
1282 char *path, *link;
1283 DWORD dosdev, ret = 0;
1285 if ((dosdev = RtlIsDosDeviceName_U( devname )))
1287 memcpy( name, devname + HIWORD(dosdev)/sizeof(WCHAR), LOWORD(dosdev) );
1288 name[LOWORD(dosdev)/sizeof(WCHAR)] = 0;
1290 else
1292 WCHAR *buffer;
1294 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, sizeof(dosdevW) + strlenW(devname)*sizeof(WCHAR) )))
1296 SetLastError( ERROR_OUTOFMEMORY );
1297 return 0;
1299 memcpy( buffer, dosdevW, sizeof(dosdevW) );
1300 strcatW( buffer, devname );
1301 status = read_nt_symlink( buffer, target, bufsize );
1302 HeapFree( GetProcessHeap(), 0, buffer );
1303 if (status)
1305 SetLastError( RtlNtStatusToDosError(status) );
1306 return 0;
1308 ret = strlenW( target ) + 1;
1309 goto done;
1312 /* FIXME: should read NT symlink for all devices */
1314 if (!(path = get_dos_device_path( name ))) return 0;
1315 link = read_symlink( path );
1316 HeapFree( GetProcessHeap(), 0, path );
1318 if (link)
1320 ret = MultiByteToWideChar( CP_UNIXCP, 0, link, -1, target, bufsize );
1321 HeapFree( GetProcessHeap(), 0, link );
1323 else if (dosdev) /* look for device defaults */
1325 if (!strcmpiW( name, auxW ))
1327 if (bufsize >= sizeof(com1W)/sizeof(WCHAR))
1329 memcpy( target, com1W, sizeof(com1W) );
1330 ret = sizeof(com1W)/sizeof(WCHAR);
1332 else SetLastError( ERROR_INSUFFICIENT_BUFFER );
1333 return ret;
1335 if (!strcmpiW( name, prnW ))
1337 if (bufsize >= sizeof(lpt1W)/sizeof(WCHAR))
1339 memcpy( target, lpt1W, sizeof(lpt1W) );
1340 ret = sizeof(lpt1W)/sizeof(WCHAR);
1342 else SetLastError( ERROR_INSUFFICIENT_BUFFER );
1343 return ret;
1346 nt_buffer[0] = '\\';
1347 nt_buffer[1] = '?';
1348 nt_buffer[2] = '?';
1349 nt_buffer[3] = '\\';
1350 strcpyW( nt_buffer + 4, name );
1351 RtlInitUnicodeString( &nt_name, nt_buffer );
1352 status = wine_nt_to_unix_file_name( &nt_name, &unix_name, FILE_OPEN, TRUE );
1353 if (status) SetLastError( RtlNtStatusToDosError(status) );
1354 else
1356 ret = MultiByteToWideChar( CP_UNIXCP, 0, unix_name.Buffer, -1, target, bufsize );
1357 RtlFreeAnsiString( &unix_name );
1360 done:
1361 if (ret)
1363 if (ret < bufsize) target[ret++] = 0; /* add an extra null */
1364 for (p = target; *p; p++) if (*p == '/') *p = '\\';
1367 return ret;
1369 else /* return a list of all devices */
1371 OBJECT_ATTRIBUTES attr;
1372 HANDLE handle;
1373 WCHAR *p = target;
1374 int i;
1376 if (bufsize <= (sizeof(auxW)+sizeof(nulW)+sizeof(prnW))/sizeof(WCHAR))
1378 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1379 return 0;
1382 /* FIXME: these should be NT symlinks too */
1384 memcpy( p, auxW, sizeof(auxW) );
1385 p += sizeof(auxW) / sizeof(WCHAR);
1386 memcpy( p, nulW, sizeof(nulW) );
1387 p += sizeof(nulW) / sizeof(WCHAR);
1388 memcpy( p, prnW, sizeof(prnW) );
1389 p += sizeof(prnW) / sizeof(WCHAR);
1391 strcpyW( nt_buffer, com0W );
1392 RtlInitUnicodeString( &nt_name, nt_buffer );
1394 for (i = 1; i <= 9; i++)
1396 nt_buffer[7] = '0' + i;
1397 if (!wine_nt_to_unix_file_name( &nt_name, &unix_name, FILE_OPEN, TRUE ))
1399 RtlFreeAnsiString( &unix_name );
1400 if (p + 5 >= target + bufsize)
1402 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1403 return 0;
1405 strcpyW( p, comW );
1406 p[3] = '0' + i;
1407 p[4] = 0;
1408 p += 5;
1411 strcpyW( nt_buffer + 4, lptW );
1412 for (i = 1; i <= 9; i++)
1414 nt_buffer[7] = '0' + i;
1415 if (!wine_nt_to_unix_file_name( &nt_name, &unix_name, FILE_OPEN, TRUE ))
1417 RtlFreeAnsiString( &unix_name );
1418 if (p + 5 >= target + bufsize)
1420 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1421 return 0;
1423 strcpyW( p, lptW );
1424 p[3] = '0' + i;
1425 p[4] = 0;
1426 p += 5;
1430 RtlInitUnicodeString( &nt_name, dosdevW );
1431 nt_name.Length -= sizeof(WCHAR); /* without trailing slash */
1432 attr.Length = sizeof(attr);
1433 attr.RootDirectory = 0;
1434 attr.ObjectName = &nt_name;
1435 attr.Attributes = OBJ_CASE_INSENSITIVE;
1436 attr.SecurityDescriptor = NULL;
1437 attr.SecurityQualityOfService = NULL;
1438 status = NtOpenDirectoryObject( &handle, FILE_LIST_DIRECTORY, &attr );
1439 if (!status)
1441 char data[1024];
1442 DIRECTORY_BASIC_INFORMATION *info = (DIRECTORY_BASIC_INFORMATION *)data;
1443 ULONG ctx = 0, len;
1445 while (!NtQueryDirectoryObject( handle, info, sizeof(data), 1, 0, &ctx, &len ))
1447 if (p + info->ObjectName.Length/sizeof(WCHAR) + 1 >= target + bufsize)
1449 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1450 NtClose( handle );
1451 return 0;
1453 memcpy( p, info->ObjectName.Buffer, info->ObjectName.Length );
1454 p += info->ObjectName.Length/sizeof(WCHAR);
1455 *p++ = 0;
1457 NtClose( handle );
1460 *p++ = 0; /* terminating null */
1461 return p - target;
1466 /***********************************************************************
1467 * QueryDosDeviceA (KERNEL32.@)
1469 * returns array of strings terminated by \0, terminated by \0
1471 DWORD WINAPI QueryDosDeviceA( LPCSTR devname, LPSTR target, DWORD bufsize )
1473 DWORD ret = 0, retW;
1474 WCHAR *devnameW = NULL;
1475 LPWSTR targetW;
1477 if (devname && !(devnameW = FILE_name_AtoW( devname, FALSE ))) return 0;
1479 targetW = HeapAlloc( GetProcessHeap(),0, bufsize * sizeof(WCHAR) );
1480 if (!targetW)
1482 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1483 return 0;
1486 retW = QueryDosDeviceW(devnameW, targetW, bufsize);
1488 ret = FILE_name_WtoA( targetW, retW, target, bufsize );
1490 HeapFree(GetProcessHeap(), 0, targetW);
1491 return ret;
1495 /***********************************************************************
1496 * GetLogicalDrives (KERNEL32.@)
1498 DWORD WINAPI GetLogicalDrives(void)
1500 static const WCHAR dosdevW[] = {'\\','D','o','s','D','e','v','i','c','e','s','\\',0};
1501 OBJECT_ATTRIBUTES attr;
1502 UNICODE_STRING nt_name;
1503 DWORD bitmask = 0;
1504 NTSTATUS status;
1505 HANDLE handle;
1507 RtlInitUnicodeString( &nt_name, dosdevW );
1508 nt_name.Length -= sizeof(WCHAR); /* without trailing slash */
1509 attr.Length = sizeof(attr);
1510 attr.RootDirectory = 0;
1511 attr.ObjectName = &nt_name;
1512 attr.Attributes = OBJ_CASE_INSENSITIVE;
1513 attr.SecurityDescriptor = NULL;
1514 attr.SecurityQualityOfService = NULL;
1515 status = NtOpenDirectoryObject( &handle, FILE_LIST_DIRECTORY, &attr );
1516 if (!status)
1518 char data[1024];
1519 DIRECTORY_BASIC_INFORMATION *info = (DIRECTORY_BASIC_INFORMATION *)data;
1520 ULONG ctx = 0, len;
1522 while (!NtQueryDirectoryObject( handle, info, sizeof(data), 1, 0, &ctx, &len ))
1523 if(info->ObjectName.Length == 2*sizeof(WCHAR) && info->ObjectName.Buffer[1] == ':')
1524 bitmask |= 1 << (info->ObjectName.Buffer[0] - 'A');
1526 NtClose( handle );
1529 return bitmask;
1533 /***********************************************************************
1534 * GetLogicalDriveStringsA (KERNEL32.@)
1536 UINT WINAPI GetLogicalDriveStringsA( UINT len, LPSTR buffer )
1538 DWORD drives = GetLogicalDrives();
1539 UINT drive, count;
1541 for (drive = count = 0; drive < 26; drive++) if (drives & (1 << drive)) count++;
1542 if ((count * 4) + 1 > len) return count * 4 + 1;
1544 for (drive = 0; drive < 26; drive++)
1546 if (drives & (1 << drive))
1548 *buffer++ = 'A' + drive;
1549 *buffer++ = ':';
1550 *buffer++ = '\\';
1551 *buffer++ = 0;
1554 *buffer = 0;
1555 return count * 4;
1559 /***********************************************************************
1560 * GetLogicalDriveStringsW (KERNEL32.@)
1562 UINT WINAPI GetLogicalDriveStringsW( UINT len, LPWSTR buffer )
1564 DWORD drives = GetLogicalDrives();
1565 UINT drive, count;
1567 for (drive = count = 0; drive < 26; drive++) if (drives & (1 << drive)) count++;
1568 if ((count * 4) + 1 > len) return count * 4 + 1;
1570 for (drive = 0; drive < 26; drive++)
1572 if (drives & (1 << drive))
1574 *buffer++ = 'A' + drive;
1575 *buffer++ = ':';
1576 *buffer++ = '\\';
1577 *buffer++ = 0;
1580 *buffer = 0;
1581 return count * 4;
1585 /***********************************************************************
1586 * GetDriveTypeW (KERNEL32.@)
1588 * Returns the type of the disk drive specified. If root is NULL the
1589 * root of the current directory is used.
1591 * RETURNS
1593 * Type of drive (from Win32 SDK):
1595 * DRIVE_UNKNOWN unable to find out anything about the drive
1596 * DRIVE_NO_ROOT_DIR nonexistent root dir
1597 * DRIVE_REMOVABLE the disk can be removed from the machine
1598 * DRIVE_FIXED the disk cannot be removed from the machine
1599 * DRIVE_REMOTE network disk
1600 * DRIVE_CDROM CDROM drive
1601 * DRIVE_RAMDISK virtual disk in RAM
1603 UINT WINAPI GetDriveTypeW(LPCWSTR root) /* [in] String describing drive */
1605 FILE_FS_DEVICE_INFORMATION info;
1606 IO_STATUS_BLOCK io;
1607 NTSTATUS status;
1608 HANDLE handle;
1609 UINT ret;
1611 if (!open_device_root( root, &handle )) return DRIVE_NO_ROOT_DIR;
1613 status = NtQueryVolumeInformationFile( handle, &io, &info, sizeof(info), FileFsDeviceInformation );
1614 NtClose( handle );
1615 if (status != STATUS_SUCCESS)
1617 SetLastError( RtlNtStatusToDosError(status) );
1618 ret = DRIVE_UNKNOWN;
1620 else
1622 switch (info.DeviceType)
1624 case FILE_DEVICE_CD_ROM_FILE_SYSTEM: ret = DRIVE_CDROM; break;
1625 case FILE_DEVICE_VIRTUAL_DISK: ret = DRIVE_RAMDISK; break;
1626 case FILE_DEVICE_NETWORK_FILE_SYSTEM: ret = DRIVE_REMOTE; break;
1627 case FILE_DEVICE_DISK_FILE_SYSTEM:
1628 if (info.Characteristics & FILE_REMOTE_DEVICE) ret = DRIVE_REMOTE;
1629 else if (info.Characteristics & FILE_REMOVABLE_MEDIA) ret = DRIVE_REMOVABLE;
1630 else if ((ret = get_mountmgr_drive_type( root )) == DRIVE_UNKNOWN) ret = DRIVE_FIXED;
1631 break;
1632 default:
1633 ret = DRIVE_UNKNOWN;
1634 break;
1637 TRACE( "%s -> %d\n", debugstr_w(root), ret );
1638 return ret;
1642 /***********************************************************************
1643 * GetDriveTypeA (KERNEL32.@)
1645 * See GetDriveTypeW.
1647 UINT WINAPI GetDriveTypeA( LPCSTR root )
1649 WCHAR *rootW = NULL;
1651 if (root && !(rootW = FILE_name_AtoW( root, FALSE ))) return DRIVE_NO_ROOT_DIR;
1652 return GetDriveTypeW( rootW );
1656 /***********************************************************************
1657 * GetDiskFreeSpaceExW (KERNEL32.@)
1659 * This function is used to acquire the size of the available and
1660 * total space on a logical volume.
1662 * RETURNS
1664 * Zero on failure, nonzero upon success. Use GetLastError to obtain
1665 * detailed error information.
1668 BOOL WINAPI GetDiskFreeSpaceExW( LPCWSTR root, PULARGE_INTEGER avail,
1669 PULARGE_INTEGER total, PULARGE_INTEGER totalfree )
1671 FILE_FS_SIZE_INFORMATION info;
1672 IO_STATUS_BLOCK io;
1673 NTSTATUS status;
1674 HANDLE handle;
1675 UINT units;
1677 TRACE( "%s,%p,%p,%p\n", debugstr_w(root), avail, total, totalfree );
1679 if (!open_device_root( root, &handle )) return FALSE;
1681 status = NtQueryVolumeInformationFile( handle, &io, &info, sizeof(info), FileFsSizeInformation );
1682 NtClose( handle );
1683 if (status != STATUS_SUCCESS)
1685 SetLastError( RtlNtStatusToDosError(status) );
1686 return FALSE;
1689 units = info.SectorsPerAllocationUnit * info.BytesPerSector;
1690 if (total) total->QuadPart = info.TotalAllocationUnits.QuadPart * units;
1691 if (totalfree) totalfree->QuadPart = info.AvailableAllocationUnits.QuadPart * units;
1692 /* FIXME: this one should take quotas into account */
1693 if (avail) avail->QuadPart = info.AvailableAllocationUnits.QuadPart * units;
1694 return TRUE;
1698 /***********************************************************************
1699 * GetDiskFreeSpaceExA (KERNEL32.@)
1701 * See GetDiskFreeSpaceExW.
1703 BOOL WINAPI GetDiskFreeSpaceExA( LPCSTR root, PULARGE_INTEGER avail,
1704 PULARGE_INTEGER total, PULARGE_INTEGER totalfree )
1706 WCHAR *rootW = NULL;
1708 if (root && !(rootW = FILE_name_AtoW( root, FALSE ))) return FALSE;
1709 return GetDiskFreeSpaceExW( rootW, avail, total, totalfree );
1713 /***********************************************************************
1714 * GetDiskFreeSpaceW (KERNEL32.@)
1716 BOOL WINAPI GetDiskFreeSpaceW( LPCWSTR root, LPDWORD cluster_sectors,
1717 LPDWORD sector_bytes, LPDWORD free_clusters,
1718 LPDWORD total_clusters )
1720 FILE_FS_SIZE_INFORMATION info;
1721 IO_STATUS_BLOCK io;
1722 NTSTATUS status;
1723 HANDLE handle;
1724 UINT units;
1726 TRACE( "%s,%p,%p,%p,%p\n", debugstr_w(root),
1727 cluster_sectors, sector_bytes, free_clusters, total_clusters );
1729 if (!open_device_root( root, &handle )) return FALSE;
1731 status = NtQueryVolumeInformationFile( handle, &io, &info, sizeof(info), FileFsSizeInformation );
1732 NtClose( handle );
1733 if (status != STATUS_SUCCESS)
1735 SetLastError( RtlNtStatusToDosError(status) );
1736 return FALSE;
1739 units = info.SectorsPerAllocationUnit * info.BytesPerSector;
1741 if( GetVersion() & 0x80000000) { /* win3.x, 9x, ME */
1742 /* cap the size and available at 2GB as per specs */
1743 if (info.TotalAllocationUnits.QuadPart * units > 0x7fffffff) {
1744 info.TotalAllocationUnits.QuadPart = 0x7fffffff / units;
1745 if (info.AvailableAllocationUnits.QuadPart * units > 0x7fffffff)
1746 info.AvailableAllocationUnits.QuadPart = 0x7fffffff / units;
1748 /* nr. of clusters is always <= 65335 */
1749 while( info.TotalAllocationUnits.QuadPart > 65535 ) {
1750 info.TotalAllocationUnits.QuadPart /= 2;
1751 info.AvailableAllocationUnits.QuadPart /= 2;
1752 info.SectorsPerAllocationUnit *= 2;
1756 if (cluster_sectors) *cluster_sectors = info.SectorsPerAllocationUnit;
1757 if (sector_bytes) *sector_bytes = info.BytesPerSector;
1758 if (free_clusters) *free_clusters = info.AvailableAllocationUnits.u.LowPart;
1759 if (total_clusters) *total_clusters = info.TotalAllocationUnits.u.LowPart;
1760 TRACE("%#08x, %#08x, %#08x, %#08x\n", info.SectorsPerAllocationUnit, info.BytesPerSector,
1761 info.AvailableAllocationUnits.u.LowPart, info.TotalAllocationUnits.u.LowPart);
1762 return TRUE;
1766 /***********************************************************************
1767 * GetDiskFreeSpaceA (KERNEL32.@)
1769 BOOL WINAPI GetDiskFreeSpaceA( LPCSTR root, LPDWORD cluster_sectors,
1770 LPDWORD sector_bytes, LPDWORD free_clusters,
1771 LPDWORD total_clusters )
1773 WCHAR *rootW = NULL;
1775 if (root && !(rootW = FILE_name_AtoW( root, FALSE ))) return FALSE;
1776 return GetDiskFreeSpaceW( rootW, cluster_sectors, sector_bytes, free_clusters, total_clusters );
1779 /***********************************************************************
1780 * GetVolumePathNameA (KERNEL32.@)
1782 BOOL WINAPI GetVolumePathNameA(LPCSTR filename, LPSTR volumepathname, DWORD buflen)
1784 BOOL ret;
1785 WCHAR *filenameW = NULL, *volumeW = NULL;
1787 FIXME("(%s, %p, %d), stub!\n", debugstr_a(filename), volumepathname, buflen);
1789 if (filename && !(filenameW = FILE_name_AtoW( filename, FALSE )))
1790 return FALSE;
1791 if (volumepathname && !(volumeW = HeapAlloc( GetProcessHeap(), 0, buflen * sizeof(WCHAR) )))
1792 return FALSE;
1794 if ((ret = GetVolumePathNameW( filenameW, volumeW, buflen )))
1795 FILE_name_WtoA( volumeW, -1, volumepathname, buflen );
1797 HeapFree( GetProcessHeap(), 0, volumeW );
1798 return ret;
1801 /***********************************************************************
1802 * GetVolumePathNameW (KERNEL32.@)
1804 BOOL WINAPI GetVolumePathNameW(LPCWSTR filename, LPWSTR volumepathname, DWORD buflen)
1806 const WCHAR *p = filename;
1808 FIXME("(%s, %p, %d), stub!\n", debugstr_w(filename), volumepathname, buflen);
1810 if (!filename || !volumepathname || !buflen)
1812 SetLastError(ERROR_INVALID_PARAMETER);
1813 return FALSE;
1816 if (p && tolowerW(p[0]) >= 'a' && tolowerW(p[0]) <= 'z' && p[1] ==':' && p[2] == '\\')
1818 if (buflen < 4)
1820 SetLastError(ERROR_FILENAME_EXCED_RANGE);
1821 return FALSE;
1823 volumepathname[0] = p[0];
1824 volumepathname[1] = ':';
1825 volumepathname[2] = '\\';
1826 volumepathname[3] = 0;
1827 return TRUE;
1830 SetLastError(ERROR_INVALID_NAME);
1831 return FALSE;
1834 /***********************************************************************
1835 * GetVolumePathNamesForVolumeNameA (KERNEL32.@)
1837 BOOL WINAPI GetVolumePathNamesForVolumeNameA(LPCSTR volumename, LPSTR volumepathname, DWORD buflen, PDWORD returnlen)
1839 BOOL ret;
1840 WCHAR *volumenameW = NULL, *volumepathnameW;
1842 if (volumename && !(volumenameW = FILE_name_AtoW( volumename, TRUE ))) return FALSE;
1843 if (!(volumepathnameW = HeapAlloc( GetProcessHeap(), 0, buflen * sizeof(WCHAR) )))
1845 HeapFree( GetProcessHeap(), 0, volumenameW );
1846 return FALSE;
1848 if ((ret = GetVolumePathNamesForVolumeNameW( volumenameW, volumepathnameW, buflen, returnlen )))
1850 char *path = volumepathname;
1851 const WCHAR *pathW = volumepathnameW;
1853 while (*pathW)
1855 int len = strlenW( pathW ) + 1;
1856 FILE_name_WtoA( pathW, len, path, buflen );
1857 buflen -= len;
1858 pathW += len;
1859 path += len;
1861 path[0] = 0;
1863 HeapFree( GetProcessHeap(), 0, volumenameW );
1864 HeapFree( GetProcessHeap(), 0, volumepathnameW );
1865 return ret;
1868 static MOUNTMGR_MOUNT_POINTS *query_mount_points( HANDLE mgr, MOUNTMGR_MOUNT_POINT *input, DWORD insize )
1870 MOUNTMGR_MOUNT_POINTS *output;
1871 DWORD outsize = 1024;
1873 for (;;)
1875 if (!(output = HeapAlloc( GetProcessHeap(), 0, outsize )))
1877 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1878 return NULL;
1880 if (DeviceIoControl( mgr, IOCTL_MOUNTMGR_QUERY_POINTS, input, insize, output, outsize, NULL, NULL )) break;
1881 outsize = output->Size;
1882 HeapFree( GetProcessHeap(), 0, output );
1883 if (GetLastError() != ERROR_MORE_DATA) return NULL;
1885 return output;
1887 /***********************************************************************
1888 * GetVolumePathNamesForVolumeNameW (KERNEL32.@)
1890 BOOL WINAPI GetVolumePathNamesForVolumeNameW(LPCWSTR volumename, LPWSTR volumepathname, DWORD buflen, PDWORD returnlen)
1892 static const WCHAR dosdevicesW[] = {'\\','D','o','s','D','e','v','i','c','e','s','\\'};
1893 HANDLE mgr;
1894 DWORD len, size;
1895 MOUNTMGR_MOUNT_POINT *spec;
1896 MOUNTMGR_MOUNT_POINTS *link, *target = NULL;
1897 WCHAR *name, *path;
1898 BOOL ret = FALSE;
1899 UINT i, j;
1901 TRACE("%s, %p, %u, %p\n", debugstr_w(volumename), volumepathname, buflen, returnlen);
1903 if (!volumename || (len = strlenW( volumename )) != 49)
1905 SetLastError( ERROR_INVALID_NAME );
1906 return FALSE;
1908 mgr = CreateFileW( MOUNTMGR_DOS_DEVICE_NAME, 0, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0 );
1909 if (mgr == INVALID_HANDLE_VALUE) return FALSE;
1911 size = sizeof(*spec) + sizeof(WCHAR) * (len - 1); /* remove trailing backslash */
1912 if (!(spec = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size ))) goto done;
1913 spec->SymbolicLinkNameOffset = sizeof(*spec);
1914 spec->SymbolicLinkNameLength = size - sizeof(*spec);
1915 name = (WCHAR *)((char *)spec + spec->SymbolicLinkNameOffset);
1916 memcpy( name, volumename, size - sizeof(*spec) );
1917 name[1] = '?'; /* map \\?\ to \??\ */
1919 target = query_mount_points( mgr, spec, size );
1920 HeapFree( GetProcessHeap(), 0, spec );
1921 if (!target)
1923 goto done;
1925 if (!target->NumberOfMountPoints)
1927 SetLastError( ERROR_FILE_NOT_FOUND );
1928 goto done;
1930 len = 0;
1931 path = volumepathname;
1932 for (i = 0; i < target->NumberOfMountPoints; i++)
1934 link = NULL;
1935 if (target->MountPoints[i].DeviceNameOffset)
1937 const WCHAR *device = (const WCHAR *)((const char *)target + target->MountPoints[i].DeviceNameOffset);
1938 USHORT device_len = target->MountPoints[i].DeviceNameLength;
1940 size = sizeof(*spec) + device_len;
1941 if (!(spec = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size ))) goto done;
1942 spec->DeviceNameOffset = sizeof(*spec);
1943 spec->DeviceNameLength = device_len;
1944 memcpy( (char *)spec + spec->DeviceNameOffset, device, device_len );
1946 link = query_mount_points( mgr, spec, size );
1947 HeapFree( GetProcessHeap(), 0, spec );
1949 else if (target->MountPoints[i].UniqueIdOffset)
1951 const WCHAR *id = (const WCHAR *)((const char *)target + target->MountPoints[i].UniqueIdOffset);
1952 USHORT id_len = target->MountPoints[i].UniqueIdLength;
1954 size = sizeof(*spec) + id_len;
1955 if (!(spec = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size ))) goto done;
1956 spec->UniqueIdOffset = sizeof(*spec);
1957 spec->UniqueIdLength = id_len;
1958 memcpy( (char *)spec + spec->UniqueIdOffset, id, id_len );
1960 link = query_mount_points( mgr, spec, size );
1961 HeapFree( GetProcessHeap(), 0, spec );
1963 if (!link) continue;
1964 for (j = 0; j < link->NumberOfMountPoints; j++)
1966 const WCHAR *linkname;
1968 if (!link->MountPoints[j].SymbolicLinkNameOffset) continue;
1969 linkname = (const WCHAR *)((const char *)link + link->MountPoints[j].SymbolicLinkNameOffset);
1971 if (link->MountPoints[j].SymbolicLinkNameLength == sizeof(dosdevicesW) + 2 * sizeof(WCHAR) &&
1972 !memicmpW( linkname, dosdevicesW, sizeof(dosdevicesW) / sizeof(WCHAR) ))
1974 len += 4;
1975 if (volumepathname && len < buflen)
1977 path[0] = linkname[sizeof(dosdevicesW) / sizeof(WCHAR)];
1978 path[1] = ':';
1979 path[2] = '\\';
1980 path[3] = 0;
1981 path += 4;
1985 HeapFree( GetProcessHeap(), 0, link );
1987 if (buflen <= len) SetLastError( ERROR_MORE_DATA );
1988 else if (volumepathname)
1990 volumepathname[len] = 0;
1991 ret = TRUE;
1993 if (returnlen) *returnlen = len + 1;
1995 done:
1996 HeapFree( GetProcessHeap(), 0, target );
1997 CloseHandle( mgr );
1998 return ret;
2001 /***********************************************************************
2002 * FindFirstVolumeA (KERNEL32.@)
2004 HANDLE WINAPI FindFirstVolumeA(LPSTR volume, DWORD len)
2006 WCHAR *buffer = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
2007 HANDLE handle = FindFirstVolumeW( buffer, len );
2009 if (handle != INVALID_HANDLE_VALUE)
2011 if (!WideCharToMultiByte( CP_ACP, 0, buffer, -1, volume, len, NULL, NULL ))
2013 FindVolumeClose( handle );
2014 handle = INVALID_HANDLE_VALUE;
2017 HeapFree( GetProcessHeap(), 0, buffer );
2018 return handle;
2021 /***********************************************************************
2022 * FindFirstVolumeW (KERNEL32.@)
2024 HANDLE WINAPI FindFirstVolumeW( LPWSTR volume, DWORD len )
2026 DWORD size = 1024;
2027 HANDLE mgr = CreateFileW( MOUNTMGR_DOS_DEVICE_NAME, 0, FILE_SHARE_READ|FILE_SHARE_WRITE,
2028 NULL, OPEN_EXISTING, 0, 0 );
2029 if (mgr == INVALID_HANDLE_VALUE) return INVALID_HANDLE_VALUE;
2031 for (;;)
2033 MOUNTMGR_MOUNT_POINT input;
2034 MOUNTMGR_MOUNT_POINTS *output;
2036 if (!(output = HeapAlloc( GetProcessHeap(), 0, size )))
2038 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
2039 break;
2041 memset( &input, 0, sizeof(input) );
2043 if (!DeviceIoControl( mgr, IOCTL_MOUNTMGR_QUERY_POINTS, &input, sizeof(input),
2044 output, size, NULL, NULL ))
2046 if (GetLastError() != ERROR_MORE_DATA) break;
2047 size = output->Size;
2048 HeapFree( GetProcessHeap(), 0, output );
2049 continue;
2051 CloseHandle( mgr );
2052 /* abuse the Size field to store the current index */
2053 output->Size = 0;
2054 if (!FindNextVolumeW( output, volume, len ))
2056 HeapFree( GetProcessHeap(), 0, output );
2057 return INVALID_HANDLE_VALUE;
2059 return output;
2061 CloseHandle( mgr );
2062 return INVALID_HANDLE_VALUE;
2065 /***********************************************************************
2066 * FindNextVolumeA (KERNEL32.@)
2068 BOOL WINAPI FindNextVolumeA( HANDLE handle, LPSTR volume, DWORD len )
2070 WCHAR *buffer = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
2071 BOOL ret;
2073 if ((ret = FindNextVolumeW( handle, buffer, len )))
2075 if (!WideCharToMultiByte( CP_ACP, 0, buffer, -1, volume, len, NULL, NULL )) ret = FALSE;
2077 HeapFree( GetProcessHeap(), 0, buffer );
2078 return ret;
2081 /***********************************************************************
2082 * FindNextVolumeW (KERNEL32.@)
2084 BOOL WINAPI FindNextVolumeW( HANDLE handle, LPWSTR volume, DWORD len )
2086 MOUNTMGR_MOUNT_POINTS *data = handle;
2088 while (data->Size < data->NumberOfMountPoints)
2090 static const WCHAR volumeW[] = {'\\','?','?','\\','V','o','l','u','m','e','{',};
2091 WCHAR *link = (WCHAR *)((char *)data + data->MountPoints[data->Size].SymbolicLinkNameOffset);
2092 DWORD size = data->MountPoints[data->Size].SymbolicLinkNameLength;
2093 data->Size++;
2094 /* skip non-volumes */
2095 if (size < sizeof(volumeW) || memcmp( link, volumeW, sizeof(volumeW) )) continue;
2096 if (size + sizeof(WCHAR) >= len * sizeof(WCHAR))
2098 SetLastError( ERROR_FILENAME_EXCED_RANGE );
2099 return FALSE;
2101 memcpy( volume, link, size );
2102 volume[1] = '\\'; /* map \??\ to \\?\ */
2103 volume[size / sizeof(WCHAR)] = '\\'; /* Windows appends a backslash */
2104 volume[size / sizeof(WCHAR) + 1] = 0;
2105 TRACE( "returning entry %u %s\n", data->Size - 1, debugstr_w(volume) );
2106 return TRUE;
2108 SetLastError( ERROR_NO_MORE_FILES );
2109 return FALSE;
2112 /***********************************************************************
2113 * FindVolumeClose (KERNEL32.@)
2115 BOOL WINAPI FindVolumeClose(HANDLE handle)
2117 return HeapFree( GetProcessHeap(), 0, handle );
2120 /***********************************************************************
2121 * FindFirstVolumeMountPointA (KERNEL32.@)
2123 HANDLE WINAPI FindFirstVolumeMountPointA(LPCSTR root, LPSTR mount_point, DWORD len)
2125 FIXME("(%s, %p, %d), stub!\n", debugstr_a(root), mount_point, len);
2126 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
2127 return INVALID_HANDLE_VALUE;
2130 /***********************************************************************
2131 * FindFirstVolumeMountPointW (KERNEL32.@)
2133 HANDLE WINAPI FindFirstVolumeMountPointW(LPCWSTR root, LPWSTR mount_point, DWORD len)
2135 FIXME("(%s, %p, %d), stub!\n", debugstr_w(root), mount_point, len);
2136 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
2137 return INVALID_HANDLE_VALUE;
2140 /***********************************************************************
2141 * FindVolumeMountPointClose (KERNEL32.@)
2143 BOOL WINAPI FindVolumeMountPointClose(HANDLE h)
2145 FIXME("(%p), stub!\n", h);
2146 return FALSE;
2149 /***********************************************************************
2150 * DeleteVolumeMountPointA (KERNEL32.@)
2152 BOOL WINAPI DeleteVolumeMountPointA(LPCSTR mountpoint)
2154 FIXME("(%s), stub!\n", debugstr_a(mountpoint));
2155 return FALSE;
2158 /***********************************************************************
2159 * DeleteVolumeMountPointW (KERNEL32.@)
2161 BOOL WINAPI DeleteVolumeMountPointW(LPCWSTR mountpoint)
2163 FIXME("(%s), stub!\n", debugstr_w(mountpoint));
2164 return FALSE;