wined3d: Drop support for WINED3DFMT_D32_UNORM.
[wine.git] / dlls / kernel32 / volume.c
blob4fd913aa227f6271dbdf1e3b89cb6834b2427b68
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 /* get the path of a dos device symlink in the $WINEPREFIX/dosdevices directory */
72 static char *get_dos_device_path( LPCWSTR name )
74 const char *config_dir = wine_get_config_dir();
75 char *buffer, *dev;
76 int i;
78 if (!(buffer = HeapAlloc( GetProcessHeap(), 0,
79 strlen(config_dir) + sizeof("/dosdevices/") + 5 )))
81 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
82 return NULL;
84 strcpy( buffer, config_dir );
85 strcat( buffer, "/dosdevices/" );
86 dev = buffer + strlen(buffer);
87 /* no codepage conversion, DOS device names are ASCII anyway */
88 for (i = 0; i < 5; i++)
89 if (!(dev[i] = (char)tolowerW(name[i]))) break;
90 dev[5] = 0;
91 return buffer;
94 /* read the contents of an NT symlink object */
95 static NTSTATUS read_nt_symlink( const WCHAR *name, WCHAR *target, DWORD size )
97 NTSTATUS status;
98 OBJECT_ATTRIBUTES attr;
99 UNICODE_STRING nameW;
100 HANDLE handle;
102 attr.Length = sizeof(attr);
103 attr.RootDirectory = 0;
104 attr.Attributes = OBJ_CASE_INSENSITIVE;
105 attr.ObjectName = &nameW;
106 attr.SecurityDescriptor = NULL;
107 attr.SecurityQualityOfService = NULL;
108 RtlInitUnicodeString( &nameW, name );
110 if (!(status = NtOpenSymbolicLinkObject( &handle, SYMBOLIC_LINK_QUERY, &attr )))
112 UNICODE_STRING targetW;
113 targetW.Buffer = target;
114 targetW.MaximumLength = (size - 1) * sizeof(WCHAR);
115 status = NtQuerySymbolicLinkObject( handle, &targetW, NULL );
116 if (!status) target[targetW.Length / sizeof(WCHAR)] = 0;
117 NtClose( handle );
119 return status;
122 /* open a handle to a device root */
123 static BOOL open_device_root( LPCWSTR root, HANDLE *handle )
125 static const WCHAR default_rootW[] = {'\\',0};
126 UNICODE_STRING nt_name;
127 OBJECT_ATTRIBUTES attr;
128 IO_STATUS_BLOCK io;
129 NTSTATUS status;
131 if (!root) root = default_rootW;
132 if (!RtlDosPathNameToNtPathName_U( root, &nt_name, NULL, NULL ))
134 SetLastError( ERROR_PATH_NOT_FOUND );
135 return FALSE;
137 attr.Length = sizeof(attr);
138 attr.RootDirectory = 0;
139 attr.Attributes = OBJ_CASE_INSENSITIVE;
140 attr.ObjectName = &nt_name;
141 attr.SecurityDescriptor = NULL;
142 attr.SecurityQualityOfService = NULL;
144 status = NtOpenFile( handle, SYNCHRONIZE, &attr, &io, 0,
145 FILE_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT );
146 RtlFreeUnicodeString( &nt_name );
147 if (status != STATUS_SUCCESS)
149 SetLastError( RtlNtStatusToDosError(status) );
150 return FALSE;
152 return TRUE;
155 /* query the type of a drive from the mount manager */
156 static DWORD get_mountmgr_drive_type( LPCWSTR root )
158 HANDLE mgr;
159 struct mountmgr_unix_drive data;
160 DWORD br;
162 memset( &data, 0, sizeof(data) );
163 if (root) data.letter = root[0];
164 else
166 WCHAR curdir[MAX_PATH];
167 GetCurrentDirectoryW( MAX_PATH, curdir );
168 if (curdir[1] != ':' || curdir[2] != '\\') return DRIVE_UNKNOWN;
169 data.letter = curdir[0];
172 mgr = CreateFileW( MOUNTMGR_DOS_DEVICE_NAME, GENERIC_READ,
173 FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0 );
174 if (mgr == INVALID_HANDLE_VALUE) return DRIVE_UNKNOWN;
176 if (!DeviceIoControl( mgr, IOCTL_MOUNTMGR_QUERY_UNIX_DRIVE, &data, sizeof(data), &data,
177 sizeof(data), &br, NULL ) && GetLastError() != ERROR_MORE_DATA)
178 data.type = DRIVE_UNKNOWN;
180 CloseHandle( mgr );
181 return data.type;
184 /* get the label by reading it from a file at the root of the filesystem */
185 static void get_filesystem_label( const UNICODE_STRING *device, WCHAR *label, DWORD len )
187 static const WCHAR labelW[] = {'.','w','i','n','d','o','w','s','-','l','a','b','e','l',0};
188 HANDLE handle;
189 UNICODE_STRING name;
190 IO_STATUS_BLOCK io;
191 OBJECT_ATTRIBUTES attr;
193 label[0] = 0;
195 attr.Length = sizeof(attr);
196 attr.RootDirectory = 0;
197 attr.Attributes = OBJ_CASE_INSENSITIVE;
198 attr.ObjectName = &name;
199 attr.SecurityDescriptor = NULL;
200 attr.SecurityQualityOfService = NULL;
202 name.MaximumLength = device->Length + sizeof(labelW);
203 name.Length = name.MaximumLength - sizeof(WCHAR);
204 if (!(name.Buffer = HeapAlloc( GetProcessHeap(), 0, name.MaximumLength ))) return;
206 memcpy( name.Buffer, device->Buffer, device->Length );
207 memcpy( name.Buffer + device->Length / sizeof(WCHAR), labelW, sizeof(labelW) );
208 if (!NtOpenFile( &handle, GENERIC_READ | SYNCHRONIZE, &attr, &io, FILE_SHARE_READ|FILE_SHARE_WRITE,
209 FILE_NON_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT ))
211 char buffer[256], *p;
212 DWORD size;
214 if (!ReadFile( handle, buffer, sizeof(buffer)-1, &size, NULL )) size = 0;
215 CloseHandle( handle );
216 p = buffer + size;
217 while (p > buffer && (p[-1] == ' ' || p[-1] == '\r' || p[-1] == '\n')) p--;
218 *p = 0;
219 if (!MultiByteToWideChar( CP_UNIXCP, 0, buffer, -1, label, len ))
220 label[len-1] = 0;
222 RtlFreeUnicodeString( &name );
225 /* get the serial number by reading it from a file at the root of the filesystem */
226 static DWORD get_filesystem_serial( const UNICODE_STRING *device )
228 static const WCHAR serialW[] = {'.','w','i','n','d','o','w','s','-','s','e','r','i','a','l',0};
229 HANDLE handle;
230 UNICODE_STRING name;
231 IO_STATUS_BLOCK io;
232 OBJECT_ATTRIBUTES attr;
233 DWORD ret = 0;
235 attr.Length = sizeof(attr);
236 attr.RootDirectory = 0;
237 attr.Attributes = OBJ_CASE_INSENSITIVE;
238 attr.ObjectName = &name;
239 attr.SecurityDescriptor = NULL;
240 attr.SecurityQualityOfService = NULL;
242 name.MaximumLength = device->Length + sizeof(serialW);
243 name.Length = name.MaximumLength - sizeof(WCHAR);
244 if (!(name.Buffer = HeapAlloc( GetProcessHeap(), 0, name.MaximumLength ))) return 0;
246 memcpy( name.Buffer, device->Buffer, device->Length );
247 memcpy( name.Buffer + device->Length / sizeof(WCHAR), serialW, sizeof(serialW) );
248 if (!NtOpenFile( &handle, GENERIC_READ | SYNCHRONIZE, &attr, &io, FILE_SHARE_READ|FILE_SHARE_WRITE,
249 FILE_SYNCHRONOUS_IO_NONALERT ))
251 char buffer[32];
252 DWORD size;
254 if (!ReadFile( handle, buffer, sizeof(buffer)-1, &size, NULL )) size = 0;
255 CloseHandle( handle );
256 buffer[size] = 0;
257 ret = strtoul( buffer, NULL, 16 );
259 RtlFreeUnicodeString( &name );
260 return ret;
264 /******************************************************************
265 * VOLUME_FindCdRomDataBestVoldesc
267 static DWORD VOLUME_FindCdRomDataBestVoldesc( HANDLE handle )
269 BYTE cur_vd_type, max_vd_type = 0;
270 BYTE buffer[0x800];
271 DWORD size, offs, best_offs = 0, extra_offs = 0;
273 for (offs = 0x8000; offs <= 0x9800; offs += 0x800)
275 /* if 'CDROM' occurs at position 8, this is a pre-iso9660 cd, and
276 * the volume label is displaced forward by 8
278 if (SetFilePointer( handle, offs, NULL, FILE_BEGIN ) != offs) break;
279 if (!ReadFile( handle, buffer, sizeof(buffer), &size, NULL )) break;
280 if (size != sizeof(buffer)) break;
281 /* check for non-ISO9660 signature */
282 if (!memcmp( buffer + 11, "ROM", 3 )) extra_offs = 8;
283 cur_vd_type = buffer[extra_offs];
284 if (cur_vd_type == 0xff) /* voldesc set terminator */
285 break;
286 if (cur_vd_type > max_vd_type)
288 max_vd_type = cur_vd_type;
289 best_offs = offs + extra_offs;
292 return best_offs;
296 /***********************************************************************
297 * VOLUME_ReadFATSuperblock
299 static enum fs_type VOLUME_ReadFATSuperblock( HANDLE handle, BYTE *buff )
301 DWORD size;
303 /* try a fixed disk, with a FAT partition */
304 if (SetFilePointer( handle, 0, NULL, FILE_BEGIN ) != 0 ||
305 !ReadFile( handle, buff, SUPERBLOCK_SIZE, &size, NULL ))
307 if (GetLastError() == ERROR_BAD_DEV_TYPE) return FS_UNKNOWN; /* not a real device */
308 return FS_ERROR;
311 if (size < SUPERBLOCK_SIZE) return FS_UNKNOWN;
313 /* FIXME: do really all FAT have their name beginning with
314 * "FAT" ? (At least FAT12, FAT16 and FAT32 have :)
316 if (!memcmp(buff+0x36, "FAT", 3) || !memcmp(buff+0x52, "FAT", 3))
318 /* guess which type of FAT we have */
319 int reasonable;
320 unsigned int sectors,
321 sect_per_fat,
322 total_sectors,
323 num_boot_sectors,
324 num_fats,
325 num_root_dir_ents,
326 bytes_per_sector,
327 sectors_per_cluster,
328 nclust;
329 sect_per_fat = GETWORD(buff, 0x16);
330 if (!sect_per_fat) sect_per_fat = GETLONG(buff, 0x24);
331 total_sectors = GETWORD(buff, 0x13);
332 if (!total_sectors)
333 total_sectors = GETLONG(buff, 0x20);
334 num_boot_sectors = GETWORD(buff, 0x0e);
335 num_fats = buff[0x10];
336 num_root_dir_ents = GETWORD(buff, 0x11);
337 bytes_per_sector = GETWORD(buff, 0x0b);
338 sectors_per_cluster = buff[0x0d];
339 /* check if the parameters are reasonable and will not cause
340 * arithmetic errors in the calculation */
341 reasonable = num_boot_sectors < total_sectors &&
342 num_fats < 16 &&
343 bytes_per_sector >= 512 && bytes_per_sector % 512 == 0 &&
344 sectors_per_cluster >= 1;
345 if (!reasonable) return FS_UNKNOWN;
346 sectors = total_sectors - num_boot_sectors - num_fats * sect_per_fat -
347 (num_root_dir_ents * 32 + bytes_per_sector - 1) / bytes_per_sector;
348 nclust = sectors / sectors_per_cluster;
349 if ((buff[0x42] == 0x28 || buff[0x42] == 0x29) &&
350 !memcmp(buff+0x52, "FAT", 3)) return FS_FAT32;
351 if (nclust < 65525)
353 if ((buff[0x26] == 0x28 || buff[0x26] == 0x29) &&
354 !memcmp(buff+0x36, "FAT", 3))
355 return FS_FAT1216;
358 return FS_UNKNOWN;
362 /***********************************************************************
363 * VOLUME_ReadCDBlock
365 static BOOL VOLUME_ReadCDBlock( HANDLE handle, BYTE *buff, INT offs )
367 DWORD size, whence = offs >= 0 ? FILE_BEGIN : FILE_END;
369 if (SetFilePointer( handle, offs, NULL, whence ) != offs ||
370 !ReadFile( handle, buff, SUPERBLOCK_SIZE, &size, NULL ) ||
371 size != SUPERBLOCK_SIZE)
372 return FALSE;
374 return TRUE;
378 /***********************************************************************
379 * VOLUME_ReadCDSuperblock
381 static enum fs_type VOLUME_ReadCDSuperblock( HANDLE handle, BYTE *buff )
383 int i;
384 DWORD offs;
386 /* Check UDF first as UDF and ISO9660 structures can coexist on the same medium
387 * Starting from sector 16, we may find :
388 * - a CD-ROM Volume Descriptor Set (ISO9660) containing one or more Volume Descriptors
389 * - an Extended Area (UDF) -- [E] 2/8.3.1 and [U] 2.1.7
390 * There is no explicit end so read 16 sectors and then give up */
391 for( i=16; i<16+16; i++)
393 if (!VOLUME_ReadCDBlock(handle, buff, i*BLOCK_SIZE))
394 continue;
396 /* We are supposed to check "BEA01", "NSR0x" and "TEA01" IDs + verify tag checksum
397 * but we assume the volume is well-formatted */
398 if (!memcmp(&buff[1], "BEA01", 5)) return FS_UDF;
401 offs = VOLUME_FindCdRomDataBestVoldesc( handle );
402 if (!offs) return FS_UNKNOWN;
404 if (!VOLUME_ReadCDBlock(handle, buff, offs))
405 return FS_ERROR;
407 /* check for the iso9660 identifier */
408 if (!memcmp(&buff[1], "CD001", 5)) return FS_ISO9660;
409 return FS_UNKNOWN;
413 /**************************************************************************
414 * UDF_Find_PVD
415 * Find the Primary Volume Descriptor
417 static BOOL UDF_Find_PVD( HANDLE handle, BYTE pvd[] )
419 unsigned int i;
420 DWORD offset;
421 INT locations[] = { 256, -1, -257, 512 };
423 for(i=0; i<ARRAY_SIZE(locations); i++)
425 if (!VOLUME_ReadCDBlock(handle, pvd, locations[i]*BLOCK_SIZE))
426 return FALSE;
428 /* Tag Identifier of Anchor Volume Descriptor Pointer is 2 -- [E] 3/10.2.1 */
429 if (pvd[0]==2 && pvd[1]==0)
431 /* Tag location (Uint32) at offset 12, little-endian */
432 offset = pvd[20 + 0];
433 offset |= pvd[20 + 1] << 8;
434 offset |= pvd[20 + 2] << 16;
435 offset |= pvd[20 + 3] << 24;
436 offset *= BLOCK_SIZE;
438 if (!VOLUME_ReadCDBlock(handle, pvd, offset))
439 return FALSE;
441 /* Check for the Primary Volume Descriptor Tag Id -- [E] 3/10.1.1 */
442 if (pvd[0]!=1 || pvd[1]!=0)
443 return FALSE;
445 /* 8 or 16 bits per character -- [U] 2.1.1 */
446 if (!(pvd[24]==8 || pvd[24]==16))
447 return FALSE;
449 return TRUE;
453 return FALSE;
457 /**************************************************************************
458 * VOLUME_GetSuperblockLabel
460 static void VOLUME_GetSuperblockLabel( const UNICODE_STRING *device, HANDLE handle,
461 enum fs_type type, const BYTE *superblock,
462 WCHAR *label, DWORD len )
464 const BYTE *label_ptr = NULL;
465 DWORD label_len;
467 switch(type)
469 case FS_ERROR:
470 label_len = 0;
471 break;
472 case FS_UNKNOWN:
473 get_filesystem_label( device, label, len );
474 return;
475 case FS_FAT1216:
476 label_ptr = superblock + 0x2b;
477 label_len = 11;
478 break;
479 case FS_FAT32:
480 label_ptr = superblock + 0x47;
481 label_len = 11;
482 break;
483 case FS_ISO9660:
485 BYTE ver = superblock[0x5a];
487 if (superblock[0x58] == 0x25 && superblock[0x59] == 0x2f && /* Unicode ID */
488 ((ver == 0x40) || (ver == 0x43) || (ver == 0x45)))
489 { /* yippee, unicode */
490 unsigned int i;
492 if (len > 17) len = 17;
493 for (i = 0; i < len-1; i++)
494 label[i] = (superblock[40+2*i] << 8) | superblock[41+2*i];
495 label[i] = 0;
496 while (i && label[i-1] == ' ') label[--i] = 0;
497 return;
499 label_ptr = superblock + 40;
500 label_len = 32;
501 break;
503 case FS_UDF:
505 BYTE pvd[BLOCK_SIZE];
507 if(!UDF_Find_PVD(handle, pvd))
509 label_len = 0;
510 break;
513 /* [E] 3/10.1.4 and [U] 2.1.1 */
514 if(pvd[24]==8)
516 label_ptr = pvd + 24 + 1;
517 label_len = pvd[24+32-1];
518 break;
520 else
522 unsigned int i;
524 label_len = 1 + pvd[24+32-1];
525 for(i=0; i<label_len && i<len; i+=2)
526 label[i/2] = (pvd[24+1 +i] << 8) | pvd[24+1 +i+1];
527 label[label_len] = 0;
528 return;
532 if (label_len) RtlMultiByteToUnicodeN( label, (len-1) * sizeof(WCHAR),
533 &label_len, (LPCSTR)label_ptr, label_len );
534 label_len /= sizeof(WCHAR);
535 label[label_len] = 0;
536 while (label_len && label[label_len-1] == ' ') label[--label_len] = 0;
540 /**************************************************************************
541 * UDF_Find_FSD_Sector
542 * Find the File Set Descriptor used to compute the serial of a UDF volume
544 static int UDF_Find_FSD_Sector( HANDLE handle, BYTE block[] )
546 int i, PVD_sector, PD_sector, PD_length;
548 if(!UDF_Find_PVD(handle,block))
549 goto default_sector;
551 /* Retrieve the tag location of the PVD -- [E] 3/7.2 */
552 PVD_sector = block[12 + 0];
553 PVD_sector |= block[12 + 1] << 8;
554 PVD_sector |= block[12 + 2] << 16;
555 PVD_sector |= block[12 + 3] << 24;
557 /* Find the Partition Descriptor */
558 for(i=PVD_sector+1; ; i++)
560 if(!VOLUME_ReadCDBlock(handle, block, i*BLOCK_SIZE))
561 goto default_sector;
563 /* Partition Descriptor Tag Id -- [E] 3/10.5.1 */
564 if(block[0]==5 && block[1]==0)
565 break;
567 /* Terminating Descriptor Tag Id -- [E] 3/10.9.1 */
568 if(block[0]==8 && block[1]==0)
569 goto default_sector;
572 /* Find the partition starting location -- [E] 3/10.5.8 */
573 PD_sector = block[188 + 0];
574 PD_sector |= block[188 + 1] << 8;
575 PD_sector |= block[188 + 2] << 16;
576 PD_sector |= block[188 + 3] << 24;
578 /* Find the partition length -- [E] 3/10.5.9 */
579 PD_length = block[192 + 0];
580 PD_length |= block[192 + 1] << 8;
581 PD_length |= block[192 + 2] << 16;
582 PD_length |= block[192 + 3] << 24;
584 for(i=PD_sector; i<PD_sector+PD_length; i++)
586 if(!VOLUME_ReadCDBlock(handle, block, i*BLOCK_SIZE))
587 goto default_sector;
589 /* File Set Descriptor Tag Id -- [E] 3/14.1.1 */
590 if(block[0]==0 && block[1]==1)
591 return i;
594 default_sector:
595 WARN("FSD sector not found, serial may be incorrect\n");
596 return 257;
600 /**************************************************************************
601 * VOLUME_GetSuperblockSerial
603 static DWORD VOLUME_GetSuperblockSerial( const UNICODE_STRING *device, HANDLE handle,
604 enum fs_type type, const BYTE *superblock )
606 int FSD_sector;
607 BYTE block[BLOCK_SIZE];
609 switch(type)
611 case FS_ERROR:
612 break;
613 case FS_UNKNOWN:
614 return get_filesystem_serial( device );
615 case FS_FAT1216:
616 return GETLONG( superblock, 0x27 );
617 case FS_FAT32:
618 return GETLONG( superblock, 0x33 );
619 case FS_UDF:
620 FSD_sector = UDF_Find_FSD_Sector(handle, block);
621 if (!VOLUME_ReadCDBlock(handle, block, FSD_sector*BLOCK_SIZE))
622 break;
623 superblock = block;
624 /* fallthrough */
625 case FS_ISO9660:
627 BYTE sum[4];
628 int i;
630 sum[0] = sum[1] = sum[2] = sum[3] = 0;
631 for (i = 0; i < 2048; i += 4)
633 /* DON'T optimize this into DWORD !! (breaks overflow) */
634 sum[0] += superblock[i+0];
635 sum[1] += superblock[i+1];
636 sum[2] += superblock[i+2];
637 sum[3] += superblock[i+3];
640 * OK, another braindead one... argh. Just believe it.
641 * Me$$ysoft chose to reverse the serial number in NT4/W2K.
642 * It's true and nobody will ever be able to change it.
644 if ((GetVersion() & 0x80000000) || type == FS_UDF)
645 return (sum[3] << 24) | (sum[2] << 16) | (sum[1] << 8) | sum[0];
646 else
647 return (sum[0] << 24) | (sum[1] << 16) | (sum[2] << 8) | sum[3];
650 return 0;
654 /**************************************************************************
655 * VOLUME_GetAudioCDSerial
657 static DWORD VOLUME_GetAudioCDSerial( const CDROM_TOC *toc )
659 DWORD serial = 0;
660 int i;
662 for (i = 0; i <= toc->LastTrack - toc->FirstTrack; i++)
663 serial += ((toc->TrackData[i].Address[1] << 16) |
664 (toc->TrackData[i].Address[2] << 8) |
665 toc->TrackData[i].Address[3]);
668 * dwStart, dwEnd collect the beginning and end of the disc respectively, in
669 * frames.
670 * There it is collected for correcting the serial when there are less than
671 * 3 tracks.
673 if (toc->LastTrack - toc->FirstTrack + 1 < 3)
675 DWORD dwStart = FRAME_OF_TOC(toc, toc->FirstTrack);
676 DWORD dwEnd = FRAME_OF_TOC(toc, toc->LastTrack + 1);
677 serial += dwEnd - dwStart;
679 return serial;
683 /***********************************************************************
684 * GetVolumeInformationW (KERNEL32.@)
686 BOOL WINAPI GetVolumeInformationW( LPCWSTR root, LPWSTR label, DWORD label_len,
687 DWORD *serial, DWORD *filename_len, DWORD *flags,
688 LPWSTR fsname, DWORD fsname_len )
690 static const WCHAR audiocdW[] = {'A','u','d','i','o',' ','C','D',0};
691 static const WCHAR fatW[] = {'F','A','T',0};
692 static const WCHAR fat32W[] = {'F','A','T','3','2',0};
693 static const WCHAR ntfsW[] = {'N','T','F','S',0};
694 static const WCHAR cdfsW[] = {'C','D','F','S',0};
695 static const WCHAR udfW[] = {'U','D','F',0};
696 static const WCHAR default_rootW[] = {'\\',0};
698 HANDLE handle;
699 NTSTATUS status;
700 UNICODE_STRING nt_name;
701 IO_STATUS_BLOCK io;
702 OBJECT_ATTRIBUTES attr;
703 FILE_FS_DEVICE_INFORMATION info;
704 WCHAR *p;
705 enum fs_type type = FS_UNKNOWN;
706 BOOL ret = FALSE;
708 if (!root) root = default_rootW;
709 if (!RtlDosPathNameToNtPathName_U( root, &nt_name, NULL, NULL ))
711 SetLastError( ERROR_PATH_NOT_FOUND );
712 return FALSE;
714 /* there must be exactly one backslash in the name, at the end */
715 p = memchrW( nt_name.Buffer + 4, '\\', (nt_name.Length - 4) / sizeof(WCHAR) );
716 if (p != nt_name.Buffer + nt_name.Length / sizeof(WCHAR) - 1)
718 /* check if root contains an explicit subdir */
719 if (root[0] && root[1] == ':') root += 2;
720 while (*root == '\\') root++;
721 if (strchrW( root, '\\' ))
722 SetLastError( ERROR_DIR_NOT_ROOT );
723 else
724 SetLastError( ERROR_INVALID_NAME );
725 goto done;
728 /* try to open the device */
730 attr.Length = sizeof(attr);
731 attr.RootDirectory = 0;
732 attr.Attributes = OBJ_CASE_INSENSITIVE;
733 attr.ObjectName = &nt_name;
734 attr.SecurityDescriptor = NULL;
735 attr.SecurityQualityOfService = NULL;
737 nt_name.Length -= sizeof(WCHAR); /* without trailing slash */
738 status = NtOpenFile( &handle, GENERIC_READ | SYNCHRONIZE, &attr, &io, FILE_SHARE_READ | FILE_SHARE_WRITE,
739 FILE_NON_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT );
740 nt_name.Length += sizeof(WCHAR);
742 if (status == STATUS_SUCCESS)
744 BYTE superblock[SUPERBLOCK_SIZE];
745 CDROM_TOC toc;
746 DWORD br;
748 /* check for audio CD */
749 /* FIXME: we only check the first track for now */
750 if (DeviceIoControl( handle, IOCTL_CDROM_READ_TOC, NULL, 0, &toc, sizeof(toc), &br, 0 ))
752 if (!(toc.TrackData[0].Control & 0x04)) /* audio track */
754 TRACE( "%s: found audio CD\n", debugstr_w(nt_name.Buffer) );
755 if (label) lstrcpynW( label, audiocdW, label_len );
756 if (serial) *serial = VOLUME_GetAudioCDSerial( &toc );
757 CloseHandle( handle );
758 type = FS_ISO9660;
759 goto fill_fs_info;
761 type = VOLUME_ReadCDSuperblock( handle, superblock );
763 else
765 type = VOLUME_ReadFATSuperblock( handle, superblock );
766 if (type == FS_UNKNOWN) type = VOLUME_ReadCDSuperblock( handle, superblock );
768 TRACE( "%s: found fs type %d\n", debugstr_w(nt_name.Buffer), type );
769 if (type == FS_ERROR)
771 CloseHandle( handle );
772 goto done;
775 if (label && label_len) VOLUME_GetSuperblockLabel( &nt_name, handle, type, superblock, label, label_len );
776 if (serial) *serial = VOLUME_GetSuperblockSerial( &nt_name, handle, type, superblock );
777 CloseHandle( handle );
778 goto fill_fs_info;
780 else TRACE( "cannot open device %s: %x\n", debugstr_w(nt_name.Buffer), status );
782 /* we couldn't open the device, fallback to default strategy */
784 status = NtOpenFile( &handle, SYNCHRONIZE, &attr, &io, 0, FILE_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT );
785 if (status != STATUS_SUCCESS)
787 SetLastError( RtlNtStatusToDosError(status) );
788 goto done;
790 status = NtQueryVolumeInformationFile( handle, &io, &info, sizeof(info), FileFsDeviceInformation );
791 NtClose( handle );
792 if (status != STATUS_SUCCESS)
794 SetLastError( RtlNtStatusToDosError(status) );
795 goto done;
797 if (info.DeviceType == FILE_DEVICE_CD_ROM_FILE_SYSTEM) type = FS_ISO9660;
799 if (label && label_len) get_filesystem_label( &nt_name, label, label_len );
800 if (serial) *serial = get_filesystem_serial( &nt_name );
802 fill_fs_info: /* now fill in the information that depends on the file system type */
804 switch(type)
806 case FS_ISO9660:
807 if (fsname) lstrcpynW( fsname, cdfsW, fsname_len );
808 if (filename_len) *filename_len = 221;
809 if (flags) *flags = FILE_READ_ONLY_VOLUME;
810 break;
811 case FS_UDF:
812 if (fsname) lstrcpynW( fsname, udfW, fsname_len );
813 if (filename_len) *filename_len = 255;
814 if (flags)
815 *flags = FILE_READ_ONLY_VOLUME | FILE_UNICODE_ON_DISK | FILE_CASE_SENSITIVE_SEARCH;
816 break;
817 case FS_FAT1216:
818 if (fsname) lstrcpynW( fsname, fatW, fsname_len );
819 case FS_FAT32:
820 if (type == FS_FAT32 && fsname) lstrcpynW( fsname, fat32W, fsname_len );
821 if (filename_len) *filename_len = 255;
822 if (flags) *flags = FILE_CASE_PRESERVED_NAMES; /* FIXME */
823 break;
824 default:
825 if (fsname) lstrcpynW( fsname, ntfsW, fsname_len );
826 if (filename_len) *filename_len = 255;
827 if (flags) *flags = FILE_CASE_PRESERVED_NAMES | FILE_PERSISTENT_ACLS;
828 break;
830 ret = TRUE;
832 done:
833 RtlFreeUnicodeString( &nt_name );
834 return ret;
838 /***********************************************************************
839 * GetVolumeInformationA (KERNEL32.@)
841 BOOL WINAPI GetVolumeInformationA( LPCSTR root, LPSTR label,
842 DWORD label_len, DWORD *serial,
843 DWORD *filename_len, DWORD *flags,
844 LPSTR fsname, DWORD fsname_len )
846 WCHAR *rootW = NULL;
847 LPWSTR labelW, fsnameW;
848 BOOL ret;
850 if (root && !(rootW = FILE_name_AtoW( root, FALSE ))) return FALSE;
852 labelW = label ? HeapAlloc(GetProcessHeap(), 0, label_len * sizeof(WCHAR)) : NULL;
853 fsnameW = fsname ? HeapAlloc(GetProcessHeap(), 0, fsname_len * sizeof(WCHAR)) : NULL;
855 if ((ret = GetVolumeInformationW(rootW, labelW, label_len, serial,
856 filename_len, flags, fsnameW, fsname_len)))
858 if (label) FILE_name_WtoA( labelW, -1, label, label_len );
859 if (fsname) FILE_name_WtoA( fsnameW, -1, fsname, fsname_len );
862 HeapFree( GetProcessHeap(), 0, labelW );
863 HeapFree( GetProcessHeap(), 0, fsnameW );
864 return ret;
869 /***********************************************************************
870 * SetVolumeLabelW (KERNEL32.@)
872 BOOL WINAPI SetVolumeLabelW( LPCWSTR root, LPCWSTR label )
874 WCHAR device[] = {'\\','\\','.','\\','A',':',0};
875 HANDLE handle;
876 enum fs_type type = FS_UNKNOWN;
878 if (!root)
880 WCHAR path[MAX_PATH];
881 GetCurrentDirectoryW( MAX_PATH, path );
882 device[4] = path[0];
884 else
886 if (!root[0] || root[1] != ':')
888 SetLastError( ERROR_INVALID_NAME );
889 return FALSE;
891 device[4] = root[0];
894 /* try to open the device */
896 handle = CreateFileW( device, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE,
897 NULL, OPEN_EXISTING, 0, 0 );
898 if (handle != INVALID_HANDLE_VALUE)
900 BYTE superblock[SUPERBLOCK_SIZE];
902 type = VOLUME_ReadFATSuperblock( handle, superblock );
903 if (type == FS_UNKNOWN) type = VOLUME_ReadCDSuperblock( handle, superblock );
904 CloseHandle( handle );
905 if (type != FS_UNKNOWN)
907 /* we can't set the label on FAT or CDROM file systems */
908 TRACE( "cannot set label on device %s type %d\n", debugstr_w(device), type );
909 SetLastError( ERROR_ACCESS_DENIED );
910 return FALSE;
913 else
915 TRACE( "cannot open device %s: err %d\n", debugstr_w(device), GetLastError() );
916 if (GetLastError() == ERROR_ACCESS_DENIED) return FALSE;
919 /* we couldn't open the device, fallback to default strategy */
921 switch(GetDriveTypeW( root ))
923 case DRIVE_UNKNOWN:
924 case DRIVE_NO_ROOT_DIR:
925 SetLastError( ERROR_NOT_READY );
926 break;
927 case DRIVE_REMOVABLE:
928 case DRIVE_FIXED:
930 WCHAR labelW[] = {'A',':','\\','.','w','i','n','d','o','w','s','-','l','a','b','e','l',0};
932 labelW[0] = device[4];
934 if (!label[0]) /* delete label file when setting an empty label */
935 return DeleteFileW( labelW ) || GetLastError() == ERROR_FILE_NOT_FOUND;
937 handle = CreateFileW( labelW, GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
938 CREATE_ALWAYS, 0, 0 );
939 if (handle != INVALID_HANDLE_VALUE)
941 char buffer[64];
942 DWORD size;
944 if (!WideCharToMultiByte( CP_UNIXCP, 0, label, -1, buffer, sizeof(buffer)-1, NULL, NULL ))
945 buffer[sizeof(buffer)-2] = 0;
946 strcat( buffer, "\n" );
947 WriteFile( handle, buffer, strlen(buffer), &size, NULL );
948 CloseHandle( handle );
949 return TRUE;
951 break;
953 case DRIVE_REMOTE:
954 case DRIVE_RAMDISK:
955 case DRIVE_CDROM:
956 SetLastError( ERROR_ACCESS_DENIED );
957 break;
959 return FALSE;
962 /***********************************************************************
963 * SetVolumeLabelA (KERNEL32.@)
965 BOOL WINAPI SetVolumeLabelA(LPCSTR root, LPCSTR volname)
967 WCHAR *rootW = NULL, *volnameW = NULL;
968 BOOL ret;
970 if (root && !(rootW = FILE_name_AtoW( root, FALSE ))) return FALSE;
971 if (volname && !(volnameW = FILE_name_AtoW( volname, TRUE ))) return FALSE;
972 ret = SetVolumeLabelW( rootW, volnameW );
973 HeapFree( GetProcessHeap(), 0, volnameW );
974 return ret;
978 /***********************************************************************
979 * GetVolumeNameForVolumeMountPointA (KERNEL32.@)
981 BOOL WINAPI GetVolumeNameForVolumeMountPointA( LPCSTR path, LPSTR volume, DWORD size )
983 BOOL ret;
984 WCHAR volumeW[50], *pathW = NULL;
985 DWORD len = min(ARRAY_SIZE(volumeW), size );
987 TRACE("(%s, %p, %x)\n", debugstr_a(path), volume, size);
989 if (!path || !(pathW = FILE_name_AtoW( path, TRUE )))
990 return FALSE;
992 if ((ret = GetVolumeNameForVolumeMountPointW( pathW, volumeW, len )))
993 FILE_name_WtoA( volumeW, -1, volume, len );
995 HeapFree( GetProcessHeap(), 0, pathW );
996 return ret;
999 /***********************************************************************
1000 * GetVolumeNameForVolumeMountPointW (KERNEL32.@)
1002 BOOL WINAPI GetVolumeNameForVolumeMountPointW( LPCWSTR path, LPWSTR volume, DWORD size )
1004 static const WCHAR prefixW[] = {'\\','D','o','s','D','e','v','i','c','e','s','\\',0};
1005 static const WCHAR volumeW[] = {'\\','?','?','\\','V','o','l','u','m','e','{'};
1006 static const WCHAR trailingW[] = {'\\',0};
1008 MOUNTMGR_MOUNT_POINT *input = NULL, *o1;
1009 MOUNTMGR_MOUNT_POINTS *output = NULL;
1010 WCHAR *p;
1011 char *r;
1012 DWORD i, i_size = 1024, o_size = 1024;
1013 WCHAR *nonpersist_name;
1014 WCHAR symlink_name[MAX_PATH];
1015 NTSTATUS status;
1016 HANDLE mgr = INVALID_HANDLE_VALUE;
1017 BOOL ret = FALSE;
1018 DWORD br;
1020 TRACE("(%s, %p, %x)\n", debugstr_w(path), volume, size);
1021 if (path[lstrlenW(path)-1] != '\\')
1023 SetLastError( ERROR_INVALID_NAME );
1024 return FALSE;
1027 if (size < 50)
1029 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1030 return FALSE;
1032 /* if length of input is > 3 then it must be a mounted folder */
1033 if (lstrlenW(path) > 3)
1035 FIXME("Mounted Folders are not yet supported\n");
1036 SetLastError( ERROR_NOT_A_REPARSE_POINT );
1037 return FALSE;
1040 mgr = CreateFileW( MOUNTMGR_DOS_DEVICE_NAME, 0, FILE_SHARE_READ,
1041 NULL, OPEN_EXISTING, 0, 0 );
1042 if (mgr == INVALID_HANDLE_VALUE) return FALSE;
1044 if (!(input = HeapAlloc( GetProcessHeap(), 0, i_size )))
1046 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1047 goto err_ret;
1050 if (!(output = HeapAlloc( GetProcessHeap(), 0, o_size )))
1052 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1053 goto err_ret;
1056 /* construct the symlink name as "\DosDevices\C:" */
1057 lstrcpyW( symlink_name, prefixW );
1058 lstrcatW( symlink_name, path );
1059 symlink_name[lstrlenW(symlink_name)-1] = 0;
1061 /* Take the mount point and get the "nonpersistent name" */
1062 /* We will then take that and get the volume name */
1063 nonpersist_name = (WCHAR *)(input + 1);
1064 status = read_nt_symlink( symlink_name, nonpersist_name, i_size - sizeof(*input) );
1065 TRACE("read_nt_symlink got stat=%x, for %s, got <%s>\n", status,
1066 debugstr_w(symlink_name), debugstr_w(nonpersist_name));
1067 if (status != STATUS_SUCCESS)
1069 SetLastError( ERROR_FILE_NOT_FOUND );
1070 goto err_ret;
1073 /* Now take the "nonpersistent name" and ask the mountmgr */
1074 /* to give us all the mount points. One of them will be */
1075 /* the volume name (format of \??\Volume{). */
1076 memset( input, 0, sizeof(*input) ); /* clear all input parameters */
1077 input->DeviceNameOffset = sizeof(*input);
1078 input->DeviceNameLength = lstrlenW( nonpersist_name) * sizeof(WCHAR);
1079 i_size = input->DeviceNameOffset + input->DeviceNameLength;
1081 output->Size = o_size;
1083 /* now get the true volume name from the mountmgr */
1084 if (!DeviceIoControl( mgr, IOCTL_MOUNTMGR_QUERY_POINTS, input, i_size,
1085 output, o_size, &br, NULL ))
1086 goto err_ret;
1088 /* Verify and return the data, note string is not null terminated */
1089 TRACE("found %d matching mount points\n", output->NumberOfMountPoints);
1090 if (output->NumberOfMountPoints < 1)
1092 SetLastError( ERROR_NO_VOLUME_ID );
1093 goto err_ret;
1095 o1 = &output->MountPoints[0];
1097 /* look for the volume name in returned values */
1098 for(i=0;i<output->NumberOfMountPoints;i++)
1100 p = (WCHAR*)((char *)output + o1->SymbolicLinkNameOffset);
1101 r = (char *)output + o1->UniqueIdOffset;
1102 TRACE("found symlink=%s, unique=%s, devname=%s\n",
1103 debugstr_wn(p, o1->SymbolicLinkNameLength/sizeof(WCHAR)),
1104 debugstr_an(r, o1->UniqueIdLength),
1105 debugstr_wn((WCHAR*)((char *)output + o1->DeviceNameOffset),
1106 o1->DeviceNameLength/sizeof(WCHAR)));
1108 if (!strncmpW( p, volumeW, ARRAY_SIZE( volumeW )))
1110 /* is there space in the return variable ?? */
1111 if ((o1->SymbolicLinkNameLength/sizeof(WCHAR))+2 > size)
1113 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1114 goto err_ret;
1116 memcpy( volume, p, o1->SymbolicLinkNameLength );
1117 volume[o1->SymbolicLinkNameLength / sizeof(WCHAR)] = 0;
1118 lstrcatW( volume, trailingW );
1119 /* change second char from '?' to '\' */
1120 volume[1] = '\\';
1121 ret = TRUE;
1122 break;
1124 o1++;
1127 err_ret:
1128 HeapFree( GetProcessHeap(), 0, input );
1129 HeapFree( GetProcessHeap(), 0, output );
1130 CloseHandle( mgr );
1131 return ret;
1134 /***********************************************************************
1135 * DefineDosDeviceW (KERNEL32.@)
1137 BOOL WINAPI DefineDosDeviceW( DWORD flags, LPCWSTR devname, LPCWSTR targetpath )
1139 DWORD len, dosdev;
1140 BOOL ret = FALSE;
1141 char *path = NULL, *target, *p;
1143 TRACE("%x, %s, %s\n", flags, debugstr_w(devname), debugstr_w(targetpath));
1145 if (!(flags & DDD_REMOVE_DEFINITION))
1147 if (!(flags & DDD_RAW_TARGET_PATH))
1149 FIXME( "(0x%08x,%s,%s) DDD_RAW_TARGET_PATH flag not set, not supported yet\n",
1150 flags, debugstr_w(devname), debugstr_w(targetpath) );
1151 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1152 return FALSE;
1155 len = WideCharToMultiByte( CP_UNIXCP, 0, targetpath, -1, NULL, 0, NULL, NULL );
1156 if ((target = HeapAlloc( GetProcessHeap(), 0, len )))
1158 WideCharToMultiByte( CP_UNIXCP, 0, targetpath, -1, target, len, NULL, NULL );
1159 for (p = target; *p; p++) if (*p == '\\') *p = '/';
1161 else
1163 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1164 return FALSE;
1167 else target = NULL;
1169 /* first check for a DOS device */
1171 if ((dosdev = RtlIsDosDeviceName_U( devname )))
1173 WCHAR name[5];
1175 memcpy( name, devname + HIWORD(dosdev)/sizeof(WCHAR), LOWORD(dosdev) );
1176 name[LOWORD(dosdev)/sizeof(WCHAR)] = 0;
1177 path = get_dos_device_path( name );
1179 else if (isalphaW(devname[0]) && devname[1] == ':' && !devname[2]) /* drive mapping */
1181 path = get_dos_device_path( devname );
1183 else SetLastError( ERROR_FILE_NOT_FOUND );
1185 if (path)
1187 if (target)
1189 TRACE( "creating symlink %s -> %s\n", path, target );
1190 unlink( path );
1191 if (!symlink( target, path )) ret = TRUE;
1192 else FILE_SetDosError();
1194 else
1196 TRACE( "removing symlink %s\n", path );
1197 if (!unlink( path )) ret = TRUE;
1198 else FILE_SetDosError();
1200 HeapFree( GetProcessHeap(), 0, path );
1202 HeapFree( GetProcessHeap(), 0, target );
1203 return ret;
1207 /***********************************************************************
1208 * DefineDosDeviceA (KERNEL32.@)
1210 BOOL WINAPI DefineDosDeviceA(DWORD flags, LPCSTR devname, LPCSTR targetpath)
1212 WCHAR *devW, *targetW = NULL;
1213 BOOL ret;
1215 if (!(devW = FILE_name_AtoW( devname, FALSE ))) return FALSE;
1216 if (targetpath && !(targetW = FILE_name_AtoW( targetpath, TRUE ))) return FALSE;
1217 ret = DefineDosDeviceW(flags, devW, targetW);
1218 HeapFree( GetProcessHeap(), 0, targetW );
1219 return ret;
1223 /***********************************************************************
1224 * QueryDosDeviceW (KERNEL32.@)
1226 * returns array of strings terminated by \0, terminated by \0
1228 DWORD WINAPI QueryDosDeviceW( LPCWSTR devname, LPWSTR target, DWORD bufsize )
1230 static const WCHAR dosdevW[] = {'\\','D','o','s','D','e','v','i','c','e','s','\\',0};
1232 UNICODE_STRING nt_name;
1233 NTSTATUS status;
1235 if (!bufsize)
1237 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1238 return 0;
1241 if (devname)
1243 WCHAR name[8];
1244 WCHAR *buffer;
1245 DWORD dosdev, ret = 0;
1247 if ((dosdev = RtlIsDosDeviceName_U( devname )))
1249 memcpy( name, devname + HIWORD(dosdev)/sizeof(WCHAR), LOWORD(dosdev) );
1250 name[LOWORD(dosdev)/sizeof(WCHAR)] = 0;
1251 devname = name;
1254 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, sizeof(dosdevW) + strlenW(devname)*sizeof(WCHAR) )))
1256 SetLastError( ERROR_OUTOFMEMORY );
1257 return 0;
1259 memcpy( buffer, dosdevW, sizeof(dosdevW) );
1260 strcatW( buffer, devname );
1261 status = read_nt_symlink( buffer, target, bufsize );
1262 HeapFree( GetProcessHeap(), 0, buffer );
1263 if (status)
1265 SetLastError( RtlNtStatusToDosError(status) );
1266 return 0;
1268 ret = strlenW( target ) + 1;
1269 if (ret < bufsize) target[ret++] = 0; /* add an extra null */
1270 return ret;
1272 else /* return a list of all devices */
1274 OBJECT_ATTRIBUTES attr;
1275 HANDLE handle;
1276 WCHAR *p = target;
1278 RtlInitUnicodeString( &nt_name, dosdevW );
1279 nt_name.Length -= sizeof(WCHAR); /* without trailing slash */
1280 attr.Length = sizeof(attr);
1281 attr.RootDirectory = 0;
1282 attr.ObjectName = &nt_name;
1283 attr.Attributes = OBJ_CASE_INSENSITIVE;
1284 attr.SecurityDescriptor = NULL;
1285 attr.SecurityQualityOfService = NULL;
1286 status = NtOpenDirectoryObject( &handle, FILE_LIST_DIRECTORY, &attr );
1287 if (!status)
1289 char data[1024];
1290 DIRECTORY_BASIC_INFORMATION *info = (DIRECTORY_BASIC_INFORMATION *)data;
1291 ULONG ctx = 0, len;
1293 while (!NtQueryDirectoryObject( handle, info, sizeof(data), 1, 0, &ctx, &len ))
1295 if (p + info->ObjectName.Length/sizeof(WCHAR) + 1 >= target + bufsize)
1297 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1298 NtClose( handle );
1299 return 0;
1301 memcpy( p, info->ObjectName.Buffer, info->ObjectName.Length );
1302 p += info->ObjectName.Length/sizeof(WCHAR);
1303 *p++ = 0;
1305 NtClose( handle );
1308 *p++ = 0; /* terminating null */
1309 return p - target;
1314 /***********************************************************************
1315 * QueryDosDeviceA (KERNEL32.@)
1317 * returns array of strings terminated by \0, terminated by \0
1319 DWORD WINAPI QueryDosDeviceA( LPCSTR devname, LPSTR target, DWORD bufsize )
1321 DWORD ret = 0, retW;
1322 WCHAR *devnameW = NULL;
1323 LPWSTR targetW;
1325 if (devname && !(devnameW = FILE_name_AtoW( devname, FALSE ))) return 0;
1327 targetW = HeapAlloc( GetProcessHeap(),0, bufsize * sizeof(WCHAR) );
1328 if (!targetW)
1330 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1331 return 0;
1334 retW = QueryDosDeviceW(devnameW, targetW, bufsize);
1336 ret = FILE_name_WtoA( targetW, retW, target, bufsize );
1338 HeapFree(GetProcessHeap(), 0, targetW);
1339 return ret;
1343 /***********************************************************************
1344 * GetLogicalDrives (KERNEL32.@)
1346 DWORD WINAPI GetLogicalDrives(void)
1348 static const WCHAR dosdevW[] = {'\\','D','o','s','D','e','v','i','c','e','s','\\',0};
1349 OBJECT_ATTRIBUTES attr;
1350 UNICODE_STRING nt_name;
1351 DWORD bitmask = 0;
1352 NTSTATUS status;
1353 HANDLE handle;
1355 RtlInitUnicodeString( &nt_name, dosdevW );
1356 nt_name.Length -= sizeof(WCHAR); /* without trailing slash */
1357 attr.Length = sizeof(attr);
1358 attr.RootDirectory = 0;
1359 attr.ObjectName = &nt_name;
1360 attr.Attributes = OBJ_CASE_INSENSITIVE;
1361 attr.SecurityDescriptor = NULL;
1362 attr.SecurityQualityOfService = NULL;
1363 status = NtOpenDirectoryObject( &handle, FILE_LIST_DIRECTORY, &attr );
1364 if (!status)
1366 char data[1024];
1367 DIRECTORY_BASIC_INFORMATION *info = (DIRECTORY_BASIC_INFORMATION *)data;
1368 ULONG ctx = 0, len;
1370 while (!NtQueryDirectoryObject( handle, info, sizeof(data), 1, 0, &ctx, &len ))
1371 if(info->ObjectName.Length == 2*sizeof(WCHAR) && info->ObjectName.Buffer[1] == ':')
1372 bitmask |= 1 << (info->ObjectName.Buffer[0] - 'A');
1374 NtClose( handle );
1377 return bitmask;
1381 /***********************************************************************
1382 * GetLogicalDriveStringsA (KERNEL32.@)
1384 UINT WINAPI GetLogicalDriveStringsA( UINT len, LPSTR buffer )
1386 DWORD drives = GetLogicalDrives();
1387 UINT drive, count;
1389 for (drive = count = 0; drive < 26; drive++) if (drives & (1 << drive)) count++;
1390 if ((count * 4) + 1 > len) return count * 4 + 1;
1392 for (drive = 0; drive < 26; drive++)
1394 if (drives & (1 << drive))
1396 *buffer++ = 'A' + drive;
1397 *buffer++ = ':';
1398 *buffer++ = '\\';
1399 *buffer++ = 0;
1402 *buffer = 0;
1403 return count * 4;
1407 /***********************************************************************
1408 * GetLogicalDriveStringsW (KERNEL32.@)
1410 UINT WINAPI GetLogicalDriveStringsW( UINT len, LPWSTR buffer )
1412 DWORD drives = GetLogicalDrives();
1413 UINT drive, count;
1415 for (drive = count = 0; drive < 26; drive++) if (drives & (1 << drive)) count++;
1416 if ((count * 4) + 1 > len) return count * 4 + 1;
1418 for (drive = 0; drive < 26; drive++)
1420 if (drives & (1 << drive))
1422 *buffer++ = 'A' + drive;
1423 *buffer++ = ':';
1424 *buffer++ = '\\';
1425 *buffer++ = 0;
1428 *buffer = 0;
1429 return count * 4;
1433 /***********************************************************************
1434 * GetDriveTypeW (KERNEL32.@)
1436 * Returns the type of the disk drive specified. If root is NULL the
1437 * root of the current directory is used.
1439 * RETURNS
1441 * Type of drive (from Win32 SDK):
1443 * DRIVE_UNKNOWN unable to find out anything about the drive
1444 * DRIVE_NO_ROOT_DIR nonexistent root dir
1445 * DRIVE_REMOVABLE the disk can be removed from the machine
1446 * DRIVE_FIXED the disk cannot be removed from the machine
1447 * DRIVE_REMOTE network disk
1448 * DRIVE_CDROM CDROM drive
1449 * DRIVE_RAMDISK virtual disk in RAM
1451 UINT WINAPI GetDriveTypeW(LPCWSTR root) /* [in] String describing drive */
1453 FILE_FS_DEVICE_INFORMATION info;
1454 IO_STATUS_BLOCK io;
1455 NTSTATUS status;
1456 HANDLE handle;
1457 UINT ret;
1459 if (!open_device_root( root, &handle ))
1461 /* CD ROM devices do not necessarily have a volume, but a drive type */
1462 ret = get_mountmgr_drive_type( root );
1463 if (ret == DRIVE_CDROM || ret == DRIVE_REMOVABLE)
1464 return ret;
1466 return DRIVE_NO_ROOT_DIR;
1469 status = NtQueryVolumeInformationFile( handle, &io, &info, sizeof(info), FileFsDeviceInformation );
1470 NtClose( handle );
1471 if (status != STATUS_SUCCESS)
1473 SetLastError( RtlNtStatusToDosError(status) );
1474 ret = DRIVE_UNKNOWN;
1476 else
1478 switch (info.DeviceType)
1480 case FILE_DEVICE_CD_ROM_FILE_SYSTEM: ret = DRIVE_CDROM; break;
1481 case FILE_DEVICE_VIRTUAL_DISK: ret = DRIVE_RAMDISK; break;
1482 case FILE_DEVICE_NETWORK_FILE_SYSTEM: ret = DRIVE_REMOTE; break;
1483 case FILE_DEVICE_DISK_FILE_SYSTEM:
1484 if (info.Characteristics & FILE_REMOTE_DEVICE) ret = DRIVE_REMOTE;
1485 else if (info.Characteristics & FILE_REMOVABLE_MEDIA) ret = DRIVE_REMOVABLE;
1486 else if ((ret = get_mountmgr_drive_type( root )) == DRIVE_UNKNOWN) ret = DRIVE_FIXED;
1487 break;
1488 default:
1489 ret = DRIVE_UNKNOWN;
1490 break;
1493 TRACE( "%s -> %d\n", debugstr_w(root), ret );
1494 return ret;
1498 /***********************************************************************
1499 * GetDriveTypeA (KERNEL32.@)
1501 * See GetDriveTypeW.
1503 UINT WINAPI GetDriveTypeA( LPCSTR root )
1505 WCHAR *rootW = NULL;
1507 if (root && !(rootW = FILE_name_AtoW( root, FALSE ))) return DRIVE_NO_ROOT_DIR;
1508 return GetDriveTypeW( rootW );
1512 /***********************************************************************
1513 * GetDiskFreeSpaceExW (KERNEL32.@)
1515 * This function is used to acquire the size of the available and
1516 * total space on a logical volume.
1518 * RETURNS
1520 * Zero on failure, nonzero upon success. Use GetLastError to obtain
1521 * detailed error information.
1524 BOOL WINAPI GetDiskFreeSpaceExW( LPCWSTR root, PULARGE_INTEGER avail,
1525 PULARGE_INTEGER total, PULARGE_INTEGER totalfree )
1527 FILE_FS_SIZE_INFORMATION info;
1528 IO_STATUS_BLOCK io;
1529 NTSTATUS status;
1530 HANDLE handle;
1531 UINT units;
1533 TRACE( "%s,%p,%p,%p\n", debugstr_w(root), avail, total, totalfree );
1535 if (!open_device_root( root, &handle )) return FALSE;
1537 status = NtQueryVolumeInformationFile( handle, &io, &info, sizeof(info), FileFsSizeInformation );
1538 NtClose( handle );
1539 if (status != STATUS_SUCCESS)
1541 SetLastError( RtlNtStatusToDosError(status) );
1542 return FALSE;
1545 units = info.SectorsPerAllocationUnit * info.BytesPerSector;
1546 if (total) total->QuadPart = info.TotalAllocationUnits.QuadPart * units;
1547 if (totalfree) totalfree->QuadPart = info.AvailableAllocationUnits.QuadPart * units;
1548 /* FIXME: this one should take quotas into account */
1549 if (avail) avail->QuadPart = info.AvailableAllocationUnits.QuadPart * units;
1550 return TRUE;
1554 /***********************************************************************
1555 * GetDiskFreeSpaceExA (KERNEL32.@)
1557 * See GetDiskFreeSpaceExW.
1559 BOOL WINAPI GetDiskFreeSpaceExA( LPCSTR root, PULARGE_INTEGER avail,
1560 PULARGE_INTEGER total, PULARGE_INTEGER totalfree )
1562 WCHAR *rootW = NULL;
1564 if (root && !(rootW = FILE_name_AtoW( root, FALSE ))) return FALSE;
1565 return GetDiskFreeSpaceExW( rootW, avail, total, totalfree );
1569 /***********************************************************************
1570 * GetDiskFreeSpaceW (KERNEL32.@)
1572 BOOL WINAPI GetDiskFreeSpaceW( LPCWSTR root, LPDWORD cluster_sectors,
1573 LPDWORD sector_bytes, LPDWORD free_clusters,
1574 LPDWORD total_clusters )
1576 FILE_FS_SIZE_INFORMATION info;
1577 IO_STATUS_BLOCK io;
1578 NTSTATUS status;
1579 HANDLE handle;
1580 UINT units;
1582 TRACE( "%s,%p,%p,%p,%p\n", debugstr_w(root),
1583 cluster_sectors, sector_bytes, free_clusters, total_clusters );
1585 if (!open_device_root( root, &handle )) return FALSE;
1587 status = NtQueryVolumeInformationFile( handle, &io, &info, sizeof(info), FileFsSizeInformation );
1588 NtClose( handle );
1589 if (status != STATUS_SUCCESS)
1591 SetLastError( RtlNtStatusToDosError(status) );
1592 return FALSE;
1595 units = info.SectorsPerAllocationUnit * info.BytesPerSector;
1597 if( GetVersion() & 0x80000000) { /* win3.x, 9x, ME */
1598 /* cap the size and available at 2GB as per specs */
1599 if (info.TotalAllocationUnits.QuadPart * units > 0x7fffffff) {
1600 info.TotalAllocationUnits.QuadPart = 0x7fffffff / units;
1601 if (info.AvailableAllocationUnits.QuadPart * units > 0x7fffffff)
1602 info.AvailableAllocationUnits.QuadPart = 0x7fffffff / units;
1604 /* nr. of clusters is always <= 65335 */
1605 while( info.TotalAllocationUnits.QuadPart > 65535 ) {
1606 info.TotalAllocationUnits.QuadPart /= 2;
1607 info.AvailableAllocationUnits.QuadPart /= 2;
1608 info.SectorsPerAllocationUnit *= 2;
1612 if (cluster_sectors) *cluster_sectors = info.SectorsPerAllocationUnit;
1613 if (sector_bytes) *sector_bytes = info.BytesPerSector;
1614 if (free_clusters) *free_clusters = info.AvailableAllocationUnits.u.LowPart;
1615 if (total_clusters) *total_clusters = info.TotalAllocationUnits.u.LowPart;
1616 TRACE("%#08x, %#08x, %#08x, %#08x\n", info.SectorsPerAllocationUnit, info.BytesPerSector,
1617 info.AvailableAllocationUnits.u.LowPart, info.TotalAllocationUnits.u.LowPart);
1618 return TRUE;
1622 /***********************************************************************
1623 * GetDiskFreeSpaceA (KERNEL32.@)
1625 BOOL WINAPI GetDiskFreeSpaceA( LPCSTR root, LPDWORD cluster_sectors,
1626 LPDWORD sector_bytes, LPDWORD free_clusters,
1627 LPDWORD total_clusters )
1629 WCHAR *rootW = NULL;
1631 if (root && !(rootW = FILE_name_AtoW( root, FALSE ))) return FALSE;
1632 return GetDiskFreeSpaceW( rootW, cluster_sectors, sector_bytes, free_clusters, total_clusters );
1635 /***********************************************************************
1636 * GetVolumePathNameA (KERNEL32.@)
1638 BOOL WINAPI GetVolumePathNameA(LPCSTR filename, LPSTR volumepathname, DWORD buflen)
1640 BOOL ret;
1641 WCHAR *filenameW = NULL, *volumeW = NULL;
1643 TRACE("(%s, %p, %d)\n", debugstr_a(filename), volumepathname, buflen);
1645 if (filename && !(filenameW = FILE_name_AtoW( filename, FALSE )))
1646 return FALSE;
1647 if (volumepathname && !(volumeW = HeapAlloc( GetProcessHeap(), 0, buflen * sizeof(WCHAR) )))
1648 return FALSE;
1650 if ((ret = GetVolumePathNameW( filenameW, volumeW, buflen )))
1651 FILE_name_WtoA( volumeW, -1, volumepathname, buflen );
1653 HeapFree( GetProcessHeap(), 0, volumeW );
1654 return ret;
1657 /***********************************************************************
1658 * GetVolumePathNameW (KERNEL32.@)
1660 * This routine is intended to find the most basic path on the same filesystem
1661 * for any particular path name. Since we can have very complicated drive/path
1662 * relationships on Unix systems, due to symbolic links, the safest way to
1663 * handle this is to start with the full path and work our way back folder by
1664 * folder unil we find a folder on a different drive (or run out of folders).
1666 BOOL WINAPI GetVolumePathNameW(LPCWSTR filename, LPWSTR volumepathname, DWORD buflen)
1668 static const WCHAR deviceprefixW[] = { '\\','?','?','\\',0 };
1669 static const WCHAR ntprefixW[] = { '\\','\\','?','\\',0 };
1670 WCHAR fallbackpathW[] = { 'C',':','\\',0 };
1671 NTSTATUS status = STATUS_SUCCESS;
1672 WCHAR *volumenameW = NULL, *c;
1673 int pos, last_pos, stop_pos;
1674 UNICODE_STRING nt_name;
1675 ANSI_STRING unix_name;
1676 BOOL first_run = TRUE;
1677 dev_t search_dev = 0;
1678 struct stat st;
1680 TRACE("(%s, %p, %d)\n", debugstr_w(filename), volumepathname, buflen);
1682 if (!filename || !volumepathname || !buflen)
1684 SetLastError(ERROR_INVALID_PARAMETER);
1685 return FALSE;
1688 last_pos = pos = strlenW( filename );
1689 /* allocate enough memory for searching the path (need room for a slash and a NULL terminator) */
1690 if (!(volumenameW = HeapAlloc( GetProcessHeap(), 0, (pos + 2) * sizeof(WCHAR) )))
1692 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1693 return FALSE;
1695 strcpyW( volumenameW, filename );
1697 /* Normalize path */
1698 for (c = volumenameW; *c; c++) if (*c == '/') *c = '\\';
1700 stop_pos = 0;
1701 /* stop searching slashes early for NT-type and nearly NT-type paths */
1702 if (strncmpW(ntprefixW, filename, strlenW(ntprefixW)) == 0)
1703 stop_pos = strlenW(ntprefixW)-1;
1704 else if (strncmpW(ntprefixW, filename, 2) == 0)
1705 stop_pos = 2;
1709 volumenameW[pos+0] = '\\';
1710 volumenameW[pos+1] = '\0';
1711 if (!RtlDosPathNameToNtPathName_U( volumenameW, &nt_name, NULL, NULL ))
1712 goto cleanup;
1713 volumenameW[pos] = '\0';
1714 status = wine_nt_to_unix_file_name( &nt_name, &unix_name, FILE_OPEN, FALSE );
1715 RtlFreeUnicodeString( &nt_name );
1716 if (status == STATUS_SUCCESS)
1718 if (stat( unix_name.Buffer, &st ) != 0)
1720 RtlFreeAnsiString( &unix_name );
1721 status = STATUS_OBJECT_NAME_INVALID;
1722 goto cleanup;
1724 if (first_run)
1726 first_run = FALSE;
1727 search_dev = st.st_dev;
1729 else if (st.st_dev != search_dev)
1731 /* folder is on a new filesystem, return the last folder */
1732 RtlFreeAnsiString( &unix_name );
1733 break;
1736 RtlFreeAnsiString( &unix_name );
1737 last_pos = pos;
1738 c = strrchrW( volumenameW, '\\' );
1739 if (c != NULL)
1740 pos = c-volumenameW;
1741 } while (c != NULL && pos > stop_pos);
1743 if (status != STATUS_SUCCESS)
1745 WCHAR cwdW[MAX_PATH];
1747 /* the path was completely invalid */
1748 if (filename[0] == '\\' && strncmpW(deviceprefixW, filename, strlenW(deviceprefixW)) != 0)
1750 /* NT-style paths (that are not device paths) fail */
1751 status = STATUS_OBJECT_NAME_INVALID;
1752 goto cleanup;
1755 /* DOS-style paths (anything not beginning with a slash) have fallback replies */
1756 if (filename[1] == ':')
1758 /* if the path is semi-sane (X:) then use the given drive letter (if it is mounted) */
1759 fallbackpathW[0] = filename[0];
1760 if (!isalphaW(filename[0]) || GetDriveTypeW( fallbackpathW ) == DRIVE_NO_ROOT_DIR)
1762 status = STATUS_OBJECT_NAME_NOT_FOUND;
1763 goto cleanup;
1766 else if (GetCurrentDirectoryW(ARRAY_SIZE(cwdW), cwdW ))
1768 /* if the path is completely bogus then revert to the drive of the working directory */
1769 fallbackpathW[0] = cwdW[0];
1771 else
1773 status = STATUS_OBJECT_NAME_INVALID;
1774 goto cleanup;
1776 last_pos = strlenW(fallbackpathW) - 1; /* points to \\ */
1777 filename = fallbackpathW;
1778 status = STATUS_SUCCESS;
1781 if (last_pos + 1 <= buflen)
1783 memcpy(volumepathname, filename, last_pos * sizeof(WCHAR));
1784 if (last_pos + 2 <= buflen) volumepathname[last_pos++] = '\\';
1785 volumepathname[last_pos] = '\0';
1787 /* DOS-style paths always return upper-case drive letters */
1788 if (volumepathname[1] == ':')
1789 volumepathname[0] = toupperW(volumepathname[0]);
1791 TRACE("Successfully translated path %s to mount-point %s\n",
1792 debugstr_w(filename), debugstr_w(volumepathname));
1794 else
1795 status = STATUS_NAME_TOO_LONG;
1797 cleanup:
1798 HeapFree( GetProcessHeap(), 0, volumenameW );
1800 if (status != STATUS_SUCCESS)
1801 SetLastError( RtlNtStatusToDosError(status) );
1802 return (status == STATUS_SUCCESS);
1806 /***********************************************************************
1807 * GetVolumePathNamesForVolumeNameA (KERNEL32.@)
1809 BOOL WINAPI GetVolumePathNamesForVolumeNameA(LPCSTR volumename, LPSTR volumepathname, DWORD buflen, PDWORD returnlen)
1811 BOOL ret;
1812 WCHAR *volumenameW = NULL, *volumepathnameW;
1814 if (volumename && !(volumenameW = FILE_name_AtoW( volumename, TRUE ))) return FALSE;
1815 if (!(volumepathnameW = HeapAlloc( GetProcessHeap(), 0, buflen * sizeof(WCHAR) )))
1817 HeapFree( GetProcessHeap(), 0, volumenameW );
1818 return FALSE;
1820 if ((ret = GetVolumePathNamesForVolumeNameW( volumenameW, volumepathnameW, buflen, returnlen )))
1822 char *path = volumepathname;
1823 const WCHAR *pathW = volumepathnameW;
1825 while (*pathW)
1827 int len = strlenW( pathW ) + 1;
1828 FILE_name_WtoA( pathW, len, path, buflen );
1829 buflen -= len;
1830 pathW += len;
1831 path += len;
1833 path[0] = 0;
1835 HeapFree( GetProcessHeap(), 0, volumenameW );
1836 HeapFree( GetProcessHeap(), 0, volumepathnameW );
1837 return ret;
1840 static MOUNTMGR_MOUNT_POINTS *query_mount_points( HANDLE mgr, MOUNTMGR_MOUNT_POINT *input, DWORD insize )
1842 MOUNTMGR_MOUNT_POINTS *output;
1843 DWORD outsize = 1024;
1844 DWORD br;
1846 for (;;)
1848 if (!(output = HeapAlloc( GetProcessHeap(), 0, outsize )))
1850 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1851 return NULL;
1853 if (DeviceIoControl( mgr, IOCTL_MOUNTMGR_QUERY_POINTS, input, insize, output, outsize, &br, NULL )) break;
1854 outsize = output->Size;
1855 HeapFree( GetProcessHeap(), 0, output );
1856 if (GetLastError() != ERROR_MORE_DATA) return NULL;
1858 return output;
1860 /***********************************************************************
1861 * GetVolumePathNamesForVolumeNameW (KERNEL32.@)
1863 BOOL WINAPI GetVolumePathNamesForVolumeNameW(LPCWSTR volumename, LPWSTR volumepathname, DWORD buflen, PDWORD returnlen)
1865 static const WCHAR dosdevicesW[] = {'\\','D','o','s','D','e','v','i','c','e','s','\\'};
1866 HANDLE mgr;
1867 DWORD len, size;
1868 MOUNTMGR_MOUNT_POINT *spec;
1869 MOUNTMGR_MOUNT_POINTS *link, *target = NULL;
1870 WCHAR *name, *path;
1871 BOOL ret = FALSE;
1872 UINT i, j;
1874 TRACE("%s, %p, %u, %p\n", debugstr_w(volumename), volumepathname, buflen, returnlen);
1876 if (!volumename || (len = strlenW( volumename )) != 49)
1878 SetLastError( ERROR_INVALID_NAME );
1879 return FALSE;
1881 mgr = CreateFileW( MOUNTMGR_DOS_DEVICE_NAME, 0, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0 );
1882 if (mgr == INVALID_HANDLE_VALUE) return FALSE;
1884 size = sizeof(*spec) + sizeof(WCHAR) * (len - 1); /* remove trailing backslash */
1885 if (!(spec = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size ))) goto done;
1886 spec->SymbolicLinkNameOffset = sizeof(*spec);
1887 spec->SymbolicLinkNameLength = size - sizeof(*spec);
1888 name = (WCHAR *)((char *)spec + spec->SymbolicLinkNameOffset);
1889 memcpy( name, volumename, size - sizeof(*spec) );
1890 name[1] = '?'; /* map \\?\ to \??\ */
1892 target = query_mount_points( mgr, spec, size );
1893 HeapFree( GetProcessHeap(), 0, spec );
1894 if (!target)
1896 goto done;
1898 if (!target->NumberOfMountPoints)
1900 SetLastError( ERROR_FILE_NOT_FOUND );
1901 goto done;
1903 len = 0;
1904 path = volumepathname;
1905 for (i = 0; i < target->NumberOfMountPoints; i++)
1907 link = NULL;
1908 if (target->MountPoints[i].DeviceNameOffset)
1910 const WCHAR *device = (const WCHAR *)((const char *)target + target->MountPoints[i].DeviceNameOffset);
1911 USHORT device_len = target->MountPoints[i].DeviceNameLength;
1913 size = sizeof(*spec) + device_len;
1914 if (!(spec = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size ))) goto done;
1915 spec->DeviceNameOffset = sizeof(*spec);
1916 spec->DeviceNameLength = device_len;
1917 memcpy( (char *)spec + spec->DeviceNameOffset, device, device_len );
1919 link = query_mount_points( mgr, spec, size );
1920 HeapFree( GetProcessHeap(), 0, spec );
1922 else if (target->MountPoints[i].UniqueIdOffset)
1924 const WCHAR *id = (const WCHAR *)((const char *)target + target->MountPoints[i].UniqueIdOffset);
1925 USHORT id_len = target->MountPoints[i].UniqueIdLength;
1927 size = sizeof(*spec) + id_len;
1928 if (!(spec = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size ))) goto done;
1929 spec->UniqueIdOffset = sizeof(*spec);
1930 spec->UniqueIdLength = id_len;
1931 memcpy( (char *)spec + spec->UniqueIdOffset, id, id_len );
1933 link = query_mount_points( mgr, spec, size );
1934 HeapFree( GetProcessHeap(), 0, spec );
1936 if (!link) continue;
1937 for (j = 0; j < link->NumberOfMountPoints; j++)
1939 const WCHAR *linkname;
1941 if (!link->MountPoints[j].SymbolicLinkNameOffset) continue;
1942 linkname = (const WCHAR *)((const char *)link + link->MountPoints[j].SymbolicLinkNameOffset);
1944 if (link->MountPoints[j].SymbolicLinkNameLength == sizeof(dosdevicesW) + 2 * sizeof(WCHAR) &&
1945 !strncmpiW( linkname, dosdevicesW, ARRAY_SIZE( dosdevicesW )))
1947 len += 4;
1948 if (volumepathname && len < buflen)
1950 path[0] = linkname[ARRAY_SIZE( dosdevicesW )];
1951 path[1] = ':';
1952 path[2] = '\\';
1953 path[3] = 0;
1954 path += 4;
1958 HeapFree( GetProcessHeap(), 0, link );
1960 if (buflen <= len) SetLastError( ERROR_MORE_DATA );
1961 else if (volumepathname)
1963 volumepathname[len] = 0;
1964 ret = TRUE;
1966 if (returnlen) *returnlen = len + 1;
1968 done:
1969 HeapFree( GetProcessHeap(), 0, target );
1970 CloseHandle( mgr );
1971 return ret;
1974 /***********************************************************************
1975 * FindFirstVolumeA (KERNEL32.@)
1977 HANDLE WINAPI FindFirstVolumeA(LPSTR volume, DWORD len)
1979 WCHAR *buffer = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1980 HANDLE handle = FindFirstVolumeW( buffer, len );
1982 if (handle != INVALID_HANDLE_VALUE)
1984 if (!WideCharToMultiByte( CP_ACP, 0, buffer, -1, volume, len, NULL, NULL ))
1986 FindVolumeClose( handle );
1987 handle = INVALID_HANDLE_VALUE;
1990 HeapFree( GetProcessHeap(), 0, buffer );
1991 return handle;
1994 /***********************************************************************
1995 * FindFirstVolumeW (KERNEL32.@)
1997 HANDLE WINAPI FindFirstVolumeW( LPWSTR volume, DWORD len )
1999 DWORD size = 1024;
2000 DWORD br;
2001 HANDLE mgr = CreateFileW( MOUNTMGR_DOS_DEVICE_NAME, 0, FILE_SHARE_READ|FILE_SHARE_WRITE,
2002 NULL, OPEN_EXISTING, 0, 0 );
2003 if (mgr == INVALID_HANDLE_VALUE) return INVALID_HANDLE_VALUE;
2005 for (;;)
2007 MOUNTMGR_MOUNT_POINT input;
2008 MOUNTMGR_MOUNT_POINTS *output;
2010 if (!(output = HeapAlloc( GetProcessHeap(), 0, size )))
2012 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
2013 break;
2015 memset( &input, 0, sizeof(input) );
2017 if (!DeviceIoControl( mgr, IOCTL_MOUNTMGR_QUERY_POINTS, &input, sizeof(input),
2018 output, size, &br, NULL ))
2020 if (GetLastError() != ERROR_MORE_DATA) break;
2021 size = output->Size;
2022 HeapFree( GetProcessHeap(), 0, output );
2023 continue;
2025 CloseHandle( mgr );
2026 /* abuse the Size field to store the current index */
2027 output->Size = 0;
2028 if (!FindNextVolumeW( output, volume, len ))
2030 HeapFree( GetProcessHeap(), 0, output );
2031 return INVALID_HANDLE_VALUE;
2033 return output;
2035 CloseHandle( mgr );
2036 return INVALID_HANDLE_VALUE;
2039 /***********************************************************************
2040 * FindNextVolumeA (KERNEL32.@)
2042 BOOL WINAPI FindNextVolumeA( HANDLE handle, LPSTR volume, DWORD len )
2044 WCHAR *buffer = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
2045 BOOL ret;
2047 if ((ret = FindNextVolumeW( handle, buffer, len )))
2049 if (!WideCharToMultiByte( CP_ACP, 0, buffer, -1, volume, len, NULL, NULL )) ret = FALSE;
2051 HeapFree( GetProcessHeap(), 0, buffer );
2052 return ret;
2055 /***********************************************************************
2056 * FindNextVolumeW (KERNEL32.@)
2058 BOOL WINAPI FindNextVolumeW( HANDLE handle, LPWSTR volume, DWORD len )
2060 MOUNTMGR_MOUNT_POINTS *data = handle;
2062 while (data->Size < data->NumberOfMountPoints)
2064 static const WCHAR volumeW[] = {'\\','?','?','\\','V','o','l','u','m','e','{',};
2065 WCHAR *link = (WCHAR *)((char *)data + data->MountPoints[data->Size].SymbolicLinkNameOffset);
2066 DWORD size = data->MountPoints[data->Size].SymbolicLinkNameLength;
2067 data->Size++;
2068 /* skip non-volumes */
2069 if (size < sizeof(volumeW) || memcmp( link, volumeW, sizeof(volumeW) )) continue;
2070 if (size + sizeof(WCHAR) >= len * sizeof(WCHAR))
2072 SetLastError( ERROR_FILENAME_EXCED_RANGE );
2073 return FALSE;
2075 memcpy( volume, link, size );
2076 volume[1] = '\\'; /* map \??\ to \\?\ */
2077 volume[size / sizeof(WCHAR)] = '\\'; /* Windows appends a backslash */
2078 volume[size / sizeof(WCHAR) + 1] = 0;
2079 TRACE( "returning entry %u %s\n", data->Size - 1, debugstr_w(volume) );
2080 return TRUE;
2082 SetLastError( ERROR_NO_MORE_FILES );
2083 return FALSE;
2086 /***********************************************************************
2087 * FindVolumeClose (KERNEL32.@)
2089 BOOL WINAPI FindVolumeClose(HANDLE handle)
2091 return HeapFree( GetProcessHeap(), 0, handle );
2094 /***********************************************************************
2095 * FindFirstVolumeMountPointA (KERNEL32.@)
2097 HANDLE WINAPI FindFirstVolumeMountPointA(LPCSTR root, LPSTR mount_point, DWORD len)
2099 FIXME("(%s, %p, %d), stub!\n", debugstr_a(root), mount_point, len);
2100 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
2101 return INVALID_HANDLE_VALUE;
2104 /***********************************************************************
2105 * FindFirstVolumeMountPointW (KERNEL32.@)
2107 HANDLE WINAPI FindFirstVolumeMountPointW(LPCWSTR root, LPWSTR mount_point, DWORD len)
2109 FIXME("(%s, %p, %d), stub!\n", debugstr_w(root), mount_point, len);
2110 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
2111 return INVALID_HANDLE_VALUE;
2114 /***********************************************************************
2115 * FindVolumeMountPointClose (KERNEL32.@)
2117 BOOL WINAPI FindVolumeMountPointClose(HANDLE h)
2119 FIXME("(%p), stub!\n", h);
2120 return FALSE;
2123 /***********************************************************************
2124 * DeleteVolumeMountPointA (KERNEL32.@)
2126 BOOL WINAPI DeleteVolumeMountPointA(LPCSTR mountpoint)
2128 FIXME("(%s), stub!\n", debugstr_a(mountpoint));
2129 return FALSE;
2132 /***********************************************************************
2133 * DeleteVolumeMountPointW (KERNEL32.@)
2135 BOOL WINAPI DeleteVolumeMountPointW(LPCWSTR mountpoint)
2137 FIXME("(%s), stub!\n", debugstr_w(mountpoint));
2138 return FALSE;
2141 /***********************************************************************
2142 * SetVolumeMountPointA (KERNEL32.@)
2144 BOOL WINAPI SetVolumeMountPointA(LPCSTR path, LPCSTR volume)
2146 FIXME("(%s, %s), stub!\n", debugstr_a(path), debugstr_a(volume));
2147 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
2148 return FALSE;
2151 /***********************************************************************
2152 * SetVolumeMountPointW (KERNEL32.@)
2154 BOOL WINAPI SetVolumeMountPointW(LPCWSTR path, LPCWSTR volume)
2156 FIXME("(%s, %s), stub!\n", debugstr_w(path), debugstr_w(volume));
2157 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
2158 return FALSE;
2161 /***********************************************************************
2162 * GetVolumeInformationByHandleW (KERNEL32.@)
2164 BOOL WINAPI GetVolumeInformationByHandleW(HANDLE handle, WCHAR *volnamebuf, DWORD volnamesize, DWORD *volserial, DWORD *maxlength, DWORD *flags, WCHAR *fsnamebuf, DWORD fsnamesize)
2166 FIXME("%p %p %d %p %p %p %p %d\n", handle, volnamebuf, volnamesize, volserial, maxlength, flags, fsnamebuf, fsnamesize);
2168 if(volnamebuf && volnamesize)
2169 *volnamebuf = 0;
2170 if(volserial)
2171 *volserial = 0;
2172 if(maxlength)
2173 *maxlength = 0;
2174 if(flags)
2175 *flags = 0;
2176 if(fsnamebuf && fsnamesize)
2177 *fsnamebuf = 0;
2179 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
2180 return FALSE;