jscript: Fixed some clang warnings.
[wine/multimedia.git] / dlls / kernel32 / volume.c
blob106b4fb504453e06bca2e056f89e52d38c55e8d9
1 /*
2 * Volume management functions
4 * Copyright 1993 Erik Bos
5 * Copyright 1996, 2004 Alexandre Julliard
6 * Copyright 1999 Petr Tomasek
7 * Copyright 2000 Andreas Mohr
8 * Copyright 2003 Eric Pouech
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #include "config.h"
26 #include "wine/port.h"
28 #include <stdarg.h>
29 #include <stdlib.h>
30 #include <stdio.h>
32 #include "ntstatus.h"
33 #define WIN32_NO_STATUS
34 #include "windef.h"
35 #include "winbase.h"
36 #include "winnls.h"
37 #include "winternl.h"
38 #include "winioctl.h"
39 #include "ntddcdrm.h"
40 #define WINE_MOUNTMGR_EXTENSIONS
41 #include "ddk/mountmgr.h"
42 #include "kernel_private.h"
43 #include "wine/library.h"
44 #include "wine/unicode.h"
45 #include "wine/debug.h"
47 WINE_DEFAULT_DEBUG_CHANNEL(volume);
49 #define SUPERBLOCK_SIZE 2048
50 #define SYMBOLIC_LINK_QUERY 0x0001
52 #define CDFRAMES_PERSEC 75
53 #define CDFRAMES_PERMIN (CDFRAMES_PERSEC * 60)
54 #define FRAME_OF_ADDR(a) ((a)[1] * CDFRAMES_PERMIN + (a)[2] * CDFRAMES_PERSEC + (a)[3])
55 #define FRAME_OF_TOC(toc, idx) FRAME_OF_ADDR((toc)->TrackData[(idx) - (toc)->FirstTrack].Address)
57 #define GETWORD(buf,off) MAKEWORD(buf[(off)],buf[(off+1)])
58 #define GETLONG(buf,off) MAKELONG(GETWORD(buf,off),GETWORD(buf,off+2))
60 enum fs_type
62 FS_ERROR, /* error accessing the device */
63 FS_UNKNOWN, /* unknown file system */
64 FS_FAT1216,
65 FS_FAT32,
66 FS_ISO9660
69 /* read a Unix symlink; returned buffer must be freed by caller */
70 static char *read_symlink( const char *path )
72 char *buffer;
73 int ret, size = 128;
75 for (;;)
77 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, size )))
79 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
80 return 0;
82 ret = readlink( path, buffer, size );
83 if (ret == -1)
85 FILE_SetDosError();
86 HeapFree( GetProcessHeap(), 0, buffer );
87 return 0;
89 if (ret != size)
91 buffer[ret] = 0;
92 return buffer;
94 HeapFree( GetProcessHeap(), 0, buffer );
95 size *= 2;
99 /* get the path of a dos device symlink in the $WINEPREFIX/dosdevices directory */
100 static char *get_dos_device_path( LPCWSTR name )
102 const char *config_dir = wine_get_config_dir();
103 char *buffer, *dev;
104 int i;
106 if (!(buffer = HeapAlloc( GetProcessHeap(), 0,
107 strlen(config_dir) + sizeof("/dosdevices/") + 5 )))
109 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
110 return NULL;
112 strcpy( buffer, config_dir );
113 strcat( buffer, "/dosdevices/" );
114 dev = buffer + strlen(buffer);
115 /* no codepage conversion, DOS device names are ASCII anyway */
116 for (i = 0; i < 5; i++)
117 if (!(dev[i] = (char)tolowerW(name[i]))) break;
118 dev[5] = 0;
119 return buffer;
122 /* read the contents of an NT symlink object */
123 static NTSTATUS read_nt_symlink( const WCHAR *name, WCHAR *target, DWORD size )
125 NTSTATUS status;
126 OBJECT_ATTRIBUTES attr;
127 UNICODE_STRING nameW;
128 HANDLE handle;
130 attr.Length = sizeof(attr);
131 attr.RootDirectory = 0;
132 attr.Attributes = OBJ_CASE_INSENSITIVE;
133 attr.ObjectName = &nameW;
134 attr.SecurityDescriptor = NULL;
135 attr.SecurityQualityOfService = NULL;
136 RtlInitUnicodeString( &nameW, name );
138 if (!(status = NtOpenSymbolicLinkObject( &handle, SYMBOLIC_LINK_QUERY, &attr )))
140 UNICODE_STRING targetW;
141 targetW.Buffer = target;
142 targetW.MaximumLength = (size - 1) * sizeof(WCHAR);
143 status = NtQuerySymbolicLinkObject( handle, &targetW, NULL );
144 if (!status) target[targetW.Length / sizeof(WCHAR)] = 0;
145 NtClose( handle );
147 return status;
150 /* open a handle to a device root */
151 static BOOL open_device_root( LPCWSTR root, HANDLE *handle )
153 static const WCHAR default_rootW[] = {'\\',0};
154 UNICODE_STRING nt_name;
155 OBJECT_ATTRIBUTES attr;
156 IO_STATUS_BLOCK io;
157 NTSTATUS status;
159 if (!root) root = default_rootW;
160 if (!RtlDosPathNameToNtPathName_U( root, &nt_name, NULL, NULL ))
162 SetLastError( ERROR_PATH_NOT_FOUND );
163 return FALSE;
165 attr.Length = sizeof(attr);
166 attr.RootDirectory = 0;
167 attr.Attributes = OBJ_CASE_INSENSITIVE;
168 attr.ObjectName = &nt_name;
169 attr.SecurityDescriptor = NULL;
170 attr.SecurityQualityOfService = NULL;
172 status = NtOpenFile( handle, 0, &attr, &io, 0,
173 FILE_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT );
174 RtlFreeUnicodeString( &nt_name );
175 if (status != STATUS_SUCCESS)
177 SetLastError( RtlNtStatusToDosError(status) );
178 return FALSE;
180 return TRUE;
183 /* query the type of a drive from the mount manager */
184 static DWORD get_mountmgr_drive_type( LPCWSTR root )
186 HANDLE mgr;
187 struct mountmgr_unix_drive data;
189 memset( &data, 0, sizeof(data) );
190 if (root) data.letter = root[0];
191 else
193 WCHAR curdir[MAX_PATH];
194 GetCurrentDirectoryW( MAX_PATH, curdir );
195 if (curdir[1] != ':' || curdir[2] != '\\') return DRIVE_UNKNOWN;
196 data.letter = curdir[0];
199 mgr = CreateFileW( MOUNTMGR_DOS_DEVICE_NAME, GENERIC_READ,
200 FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0 );
201 if (mgr == INVALID_HANDLE_VALUE) return DRIVE_UNKNOWN;
203 if (!DeviceIoControl( mgr, IOCTL_MOUNTMGR_QUERY_UNIX_DRIVE, &data, sizeof(data), &data,
204 sizeof(data), NULL, NULL ) && GetLastError() != ERROR_MORE_DATA)
205 data.type = DRIVE_UNKNOWN;
207 CloseHandle( mgr );
208 return data.type;
211 /* get the label by reading it from a file at the root of the filesystem */
212 static void get_filesystem_label( const UNICODE_STRING *device, WCHAR *label, DWORD len )
214 static const WCHAR labelW[] = {'.','w','i','n','d','o','w','s','-','l','a','b','e','l',0};
215 HANDLE handle;
216 UNICODE_STRING name;
217 IO_STATUS_BLOCK io;
218 OBJECT_ATTRIBUTES attr;
220 label[0] = 0;
222 attr.Length = sizeof(attr);
223 attr.RootDirectory = 0;
224 attr.Attributes = OBJ_CASE_INSENSITIVE;
225 attr.ObjectName = &name;
226 attr.SecurityDescriptor = NULL;
227 attr.SecurityQualityOfService = NULL;
229 name.MaximumLength = device->Length + sizeof(labelW);
230 name.Length = name.MaximumLength - sizeof(WCHAR);
231 if (!(name.Buffer = HeapAlloc( GetProcessHeap(), 0, name.MaximumLength ))) return;
233 memcpy( name.Buffer, device->Buffer, device->Length );
234 memcpy( name.Buffer + device->Length / sizeof(WCHAR), labelW, sizeof(labelW) );
235 if (!NtOpenFile( &handle, GENERIC_READ, &attr, &io, FILE_SHARE_READ|FILE_SHARE_WRITE,
236 FILE_NON_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT ))
238 char buffer[256], *p;
239 DWORD size;
241 if (!ReadFile( handle, buffer, sizeof(buffer)-1, &size, NULL )) size = 0;
242 CloseHandle( handle );
243 p = buffer + size;
244 while (p > buffer && (p[-1] == ' ' || p[-1] == '\r' || p[-1] == '\n')) p--;
245 *p = 0;
246 if (!MultiByteToWideChar( CP_UNIXCP, 0, buffer, -1, label, len ))
247 label[len-1] = 0;
249 RtlFreeUnicodeString( &name );
252 /* get the serial number by reading it from a file at the root of the filesystem */
253 static DWORD get_filesystem_serial( const UNICODE_STRING *device )
255 static const WCHAR serialW[] = {'.','w','i','n','d','o','w','s','-','s','e','r','i','a','l',0};
256 HANDLE handle;
257 UNICODE_STRING name;
258 IO_STATUS_BLOCK io;
259 OBJECT_ATTRIBUTES attr;
260 DWORD ret = 0;
262 attr.Length = sizeof(attr);
263 attr.RootDirectory = 0;
264 attr.Attributes = OBJ_CASE_INSENSITIVE;
265 attr.ObjectName = &name;
266 attr.SecurityDescriptor = NULL;
267 attr.SecurityQualityOfService = NULL;
269 name.MaximumLength = device->Length + sizeof(serialW);
270 name.Length = name.MaximumLength - sizeof(WCHAR);
271 if (!(name.Buffer = HeapAlloc( GetProcessHeap(), 0, name.MaximumLength ))) return 0;
273 memcpy( name.Buffer, device->Buffer, device->Length );
274 memcpy( name.Buffer + device->Length / sizeof(WCHAR), serialW, sizeof(serialW) );
275 if (!NtOpenFile( &handle, GENERIC_READ, &attr, &io, FILE_SHARE_READ|FILE_SHARE_WRITE,
276 FILE_SYNCHRONOUS_IO_NONALERT ))
278 char buffer[32];
279 DWORD size;
281 if (!ReadFile( handle, buffer, sizeof(buffer)-1, &size, NULL )) size = 0;
282 CloseHandle( handle );
283 buffer[size] = 0;
284 ret = strtoul( buffer, NULL, 16 );
286 RtlFreeUnicodeString( &name );
287 return ret;
291 /******************************************************************
292 * VOLUME_FindCdRomDataBestVoldesc
294 static DWORD VOLUME_FindCdRomDataBestVoldesc( HANDLE handle )
296 BYTE cur_vd_type, max_vd_type = 0;
297 BYTE buffer[0x800];
298 DWORD size, offs, best_offs = 0, extra_offs = 0;
300 for (offs = 0x8000; offs <= 0x9800; offs += 0x800)
302 /* if 'CDROM' occurs at position 8, this is a pre-iso9660 cd, and
303 * the volume label is displaced forward by 8
305 if (SetFilePointer( handle, offs, NULL, FILE_BEGIN ) != offs) break;
306 if (!ReadFile( handle, buffer, sizeof(buffer), &size, NULL )) break;
307 if (size != sizeof(buffer)) break;
308 /* check for non-ISO9660 signature */
309 if (!memcmp( buffer + 11, "ROM", 3 )) extra_offs = 8;
310 cur_vd_type = buffer[extra_offs];
311 if (cur_vd_type == 0xff) /* voldesc set terminator */
312 break;
313 if (cur_vd_type > max_vd_type)
315 max_vd_type = cur_vd_type;
316 best_offs = offs + extra_offs;
319 return best_offs;
323 /***********************************************************************
324 * VOLUME_ReadFATSuperblock
326 static enum fs_type VOLUME_ReadFATSuperblock( HANDLE handle, BYTE *buff )
328 DWORD size;
330 /* try a fixed disk, with a FAT partition */
331 if (SetFilePointer( handle, 0, NULL, FILE_BEGIN ) != 0 ||
332 !ReadFile( handle, buff, SUPERBLOCK_SIZE, &size, NULL ))
334 if (GetLastError() == ERROR_BAD_DEV_TYPE) return FS_UNKNOWN; /* not a real device */
335 return FS_ERROR;
338 if (size < SUPERBLOCK_SIZE) return FS_UNKNOWN;
340 /* FIXME: do really all FAT have their name beginning with
341 * "FAT" ? (At least FAT12, FAT16 and FAT32 have :)
343 if (!memcmp(buff+0x36, "FAT", 3) || !memcmp(buff+0x52, "FAT", 3))
345 /* guess which type of FAT we have */
346 int reasonable;
347 unsigned int sectors,
348 sect_per_fat,
349 total_sectors,
350 num_boot_sectors,
351 num_fats,
352 num_root_dir_ents,
353 bytes_per_sector,
354 sectors_per_cluster,
355 nclust;
356 sect_per_fat = GETWORD(buff, 0x16);
357 if (!sect_per_fat) sect_per_fat = GETLONG(buff, 0x24);
358 total_sectors = GETWORD(buff, 0x13);
359 if (!total_sectors)
360 total_sectors = GETLONG(buff, 0x20);
361 num_boot_sectors = GETWORD(buff, 0x0e);
362 num_fats = buff[0x10];
363 num_root_dir_ents = GETWORD(buff, 0x11);
364 bytes_per_sector = GETWORD(buff, 0x0b);
365 sectors_per_cluster = buff[0x0d];
366 /* check if the parameters are reasonable and will not cause
367 * arithmetic errors in the calculation */
368 reasonable = num_boot_sectors < total_sectors &&
369 num_fats < 16 &&
370 bytes_per_sector >= 512 && bytes_per_sector % 512 == 0 &&
371 sectors_per_cluster >= 1;
372 if (!reasonable) return FS_UNKNOWN;
373 sectors = total_sectors - num_boot_sectors - num_fats * sect_per_fat -
374 (num_root_dir_ents * 32 + bytes_per_sector - 1) / bytes_per_sector;
375 nclust = sectors / sectors_per_cluster;
376 if ((buff[0x42] == 0x28 || buff[0x42] == 0x29) &&
377 !memcmp(buff+0x52, "FAT", 3)) return FS_FAT32;
378 if (nclust < 65525)
380 if ((buff[0x26] == 0x28 || buff[0x26] == 0x29) &&
381 !memcmp(buff+0x36, "FAT", 3))
382 return FS_FAT1216;
385 return FS_UNKNOWN;
389 /***********************************************************************
390 * VOLUME_ReadCDSuperblock
392 static enum fs_type VOLUME_ReadCDSuperblock( HANDLE handle, BYTE *buff )
394 DWORD size, offs = VOLUME_FindCdRomDataBestVoldesc( handle );
396 if (!offs) return FS_UNKNOWN;
398 if (SetFilePointer( handle, offs, NULL, FILE_BEGIN ) != offs ||
399 !ReadFile( handle, buff, SUPERBLOCK_SIZE, &size, NULL ) ||
400 size != SUPERBLOCK_SIZE)
401 return FS_ERROR;
403 /* check for iso9660 present */
404 if (!memcmp(&buff[1], "CD001", 5)) return FS_ISO9660;
405 return FS_UNKNOWN;
409 /**************************************************************************
410 * VOLUME_GetSuperblockLabel
412 static void VOLUME_GetSuperblockLabel( const UNICODE_STRING *device, enum fs_type type,
413 const BYTE *superblock, WCHAR *label, DWORD len )
415 const BYTE *label_ptr = NULL;
416 DWORD label_len;
418 switch(type)
420 case FS_ERROR:
421 label_len = 0;
422 break;
423 case FS_UNKNOWN:
424 get_filesystem_label( device, label, len );
425 return;
426 case FS_FAT1216:
427 label_ptr = superblock + 0x2b;
428 label_len = 11;
429 break;
430 case FS_FAT32:
431 label_ptr = superblock + 0x47;
432 label_len = 11;
433 break;
434 case FS_ISO9660:
436 BYTE ver = superblock[0x5a];
438 if (superblock[0x58] == 0x25 && superblock[0x59] == 0x2f && /* Unicode ID */
439 ((ver == 0x40) || (ver == 0x43) || (ver == 0x45)))
440 { /* yippee, unicode */
441 unsigned int i;
443 if (len > 17) len = 17;
444 for (i = 0; i < len-1; i++)
445 label[i] = (superblock[40+2*i] << 8) | superblock[41+2*i];
446 label[i] = 0;
447 while (i && label[i-1] == ' ') label[--i] = 0;
448 return;
450 label_ptr = superblock + 40;
451 label_len = 32;
452 break;
455 if (label_len) RtlMultiByteToUnicodeN( label, (len-1) * sizeof(WCHAR),
456 &label_len, (LPCSTR)label_ptr, label_len );
457 label_len /= sizeof(WCHAR);
458 label[label_len] = 0;
459 while (label_len && label[label_len-1] == ' ') label[--label_len] = 0;
463 /**************************************************************************
464 * VOLUME_GetSuperblockSerial
466 static DWORD VOLUME_GetSuperblockSerial( const UNICODE_STRING *device, enum fs_type type,
467 const BYTE *superblock )
469 switch(type)
471 case FS_ERROR:
472 break;
473 case FS_UNKNOWN:
474 return get_filesystem_serial( device );
475 case FS_FAT1216:
476 return GETLONG( superblock, 0x27 );
477 case FS_FAT32:
478 return GETLONG( superblock, 0x33 );
479 case FS_ISO9660:
481 BYTE sum[4];
482 int i;
484 sum[0] = sum[1] = sum[2] = sum[3] = 0;
485 for (i = 0; i < 2048; i += 4)
487 /* DON'T optimize this into DWORD !! (breaks overflow) */
488 sum[0] += superblock[i+0];
489 sum[1] += superblock[i+1];
490 sum[2] += superblock[i+2];
491 sum[3] += superblock[i+3];
494 * OK, another braindead one... argh. Just believe it.
495 * Me$$ysoft chose to reverse the serial number in NT4/W2K.
496 * It's true and nobody will ever be able to change it.
498 if (GetVersion() & 0x80000000)
499 return (sum[3] << 24) | (sum[2] << 16) | (sum[1] << 8) | sum[0];
500 else
501 return (sum[0] << 24) | (sum[1] << 16) | (sum[2] << 8) | sum[3];
504 return 0;
508 /**************************************************************************
509 * VOLUME_GetAudioCDSerial
511 static DWORD VOLUME_GetAudioCDSerial( const CDROM_TOC *toc )
513 DWORD serial = 0;
514 int i;
516 for (i = 0; i <= toc->LastTrack - toc->FirstTrack; i++)
517 serial += ((toc->TrackData[i].Address[1] << 16) |
518 (toc->TrackData[i].Address[2] << 8) |
519 toc->TrackData[i].Address[3]);
522 * dwStart, dwEnd collect the beginning and end of the disc respectively, in
523 * frames.
524 * There it is collected for correcting the serial when there are less than
525 * 3 tracks.
527 if (toc->LastTrack - toc->FirstTrack + 1 < 3)
529 DWORD dwStart = FRAME_OF_TOC(toc, toc->FirstTrack);
530 DWORD dwEnd = FRAME_OF_TOC(toc, toc->LastTrack + 1);
531 serial += dwEnd - dwStart;
533 return serial;
537 /***********************************************************************
538 * GetVolumeInformationW (KERNEL32.@)
540 BOOL WINAPI GetVolumeInformationW( LPCWSTR root, LPWSTR label, DWORD label_len,
541 DWORD *serial, DWORD *filename_len, DWORD *flags,
542 LPWSTR fsname, DWORD fsname_len )
544 static const WCHAR audiocdW[] = {'A','u','d','i','o',' ','C','D',0};
545 static const WCHAR fatW[] = {'F','A','T',0};
546 static const WCHAR fat32W[] = {'F','A','T','3','2',0};
547 static const WCHAR ntfsW[] = {'N','T','F','S',0};
548 static const WCHAR cdfsW[] = {'C','D','F','S',0};
549 static const WCHAR default_rootW[] = {'\\',0};
551 HANDLE handle;
552 NTSTATUS status;
553 UNICODE_STRING nt_name;
554 IO_STATUS_BLOCK io;
555 OBJECT_ATTRIBUTES attr;
556 FILE_FS_DEVICE_INFORMATION info;
557 WCHAR *p;
558 enum fs_type type = FS_UNKNOWN;
559 BOOL ret = FALSE;
561 if (!root) root = default_rootW;
562 if (!RtlDosPathNameToNtPathName_U( root, &nt_name, NULL, NULL ))
564 SetLastError( ERROR_PATH_NOT_FOUND );
565 return FALSE;
567 /* there must be exactly one backslash in the name, at the end */
568 p = memchrW( nt_name.Buffer + 4, '\\', (nt_name.Length - 4) / sizeof(WCHAR) );
569 if (p != nt_name.Buffer + nt_name.Length / sizeof(WCHAR) - 1)
571 /* check if root contains an explicit subdir */
572 if (root[0] && root[1] == ':') root += 2;
573 while (*root == '\\') root++;
574 if (strchrW( root, '\\' ))
575 SetLastError( ERROR_DIR_NOT_ROOT );
576 else
577 SetLastError( ERROR_INVALID_NAME );
578 goto done;
581 /* try to open the device */
583 attr.Length = sizeof(attr);
584 attr.RootDirectory = 0;
585 attr.Attributes = OBJ_CASE_INSENSITIVE;
586 attr.ObjectName = &nt_name;
587 attr.SecurityDescriptor = NULL;
588 attr.SecurityQualityOfService = NULL;
590 nt_name.Length -= sizeof(WCHAR); /* without trailing slash */
591 status = NtOpenFile( &handle, GENERIC_READ, &attr, &io, FILE_SHARE_READ | FILE_SHARE_WRITE,
592 FILE_NON_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT );
593 nt_name.Length += sizeof(WCHAR);
595 if (status == STATUS_SUCCESS)
597 BYTE superblock[SUPERBLOCK_SIZE];
598 CDROM_TOC toc;
599 DWORD br;
601 /* check for audio CD */
602 /* FIXME: we only check the first track for now */
603 if (DeviceIoControl( handle, IOCTL_CDROM_READ_TOC, NULL, 0, &toc, sizeof(toc), &br, 0 ))
605 if (!(toc.TrackData[0].Control & 0x04)) /* audio track */
607 TRACE( "%s: found audio CD\n", debugstr_w(nt_name.Buffer) );
608 if (label) lstrcpynW( label, audiocdW, label_len );
609 if (serial) *serial = VOLUME_GetAudioCDSerial( &toc );
610 CloseHandle( handle );
611 type = FS_ISO9660;
612 goto fill_fs_info;
614 type = VOLUME_ReadCDSuperblock( handle, superblock );
616 else
618 type = VOLUME_ReadFATSuperblock( handle, superblock );
619 if (type == FS_UNKNOWN) type = VOLUME_ReadCDSuperblock( handle, superblock );
621 CloseHandle( handle );
622 TRACE( "%s: found fs type %d\n", debugstr_w(nt_name.Buffer), type );
623 if (type == FS_ERROR) goto done;
625 if (label && label_len) VOLUME_GetSuperblockLabel( &nt_name, type, superblock, label, label_len );
626 if (serial) *serial = VOLUME_GetSuperblockSerial( &nt_name, type, superblock );
627 goto fill_fs_info;
629 else TRACE( "cannot open device %s: %x\n", debugstr_w(nt_name.Buffer), status );
631 /* we couldn't open the device, fallback to default strategy */
633 status = NtOpenFile( &handle, 0, &attr, &io, 0, FILE_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT );
634 if (status != STATUS_SUCCESS)
636 SetLastError( RtlNtStatusToDosError(status) );
637 goto done;
639 status = NtQueryVolumeInformationFile( handle, &io, &info, sizeof(info), FileFsDeviceInformation );
640 NtClose( handle );
641 if (status != STATUS_SUCCESS)
643 SetLastError( RtlNtStatusToDosError(status) );
644 goto done;
646 if (info.DeviceType == FILE_DEVICE_CD_ROM_FILE_SYSTEM) type = FS_ISO9660;
648 if (label && label_len) get_filesystem_label( &nt_name, label, label_len );
649 if (serial) *serial = get_filesystem_serial( &nt_name );
651 fill_fs_info: /* now fill in the information that depends on the file system type */
653 switch(type)
655 case FS_ISO9660:
656 if (fsname) lstrcpynW( fsname, cdfsW, fsname_len );
657 if (filename_len) *filename_len = 221;
658 if (flags) *flags = FILE_READ_ONLY_VOLUME;
659 break;
660 case FS_FAT1216:
661 if (fsname) lstrcpynW( fsname, fatW, fsname_len );
662 case FS_FAT32:
663 if (type == FS_FAT32 && fsname) lstrcpynW( fsname, fat32W, fsname_len );
664 if (filename_len) *filename_len = 255;
665 if (flags) *flags = FILE_CASE_PRESERVED_NAMES; /* FIXME */
666 break;
667 default:
668 if (fsname) lstrcpynW( fsname, ntfsW, fsname_len );
669 if (filename_len) *filename_len = 255;
670 if (flags) *flags = FILE_CASE_PRESERVED_NAMES;
671 break;
673 ret = TRUE;
675 done:
676 RtlFreeUnicodeString( &nt_name );
677 return ret;
681 /***********************************************************************
682 * GetVolumeInformationA (KERNEL32.@)
684 BOOL WINAPI GetVolumeInformationA( LPCSTR root, LPSTR label,
685 DWORD label_len, DWORD *serial,
686 DWORD *filename_len, DWORD *flags,
687 LPSTR fsname, DWORD fsname_len )
689 WCHAR *rootW = NULL;
690 LPWSTR labelW, fsnameW;
691 BOOL ret;
693 if (root && !(rootW = FILE_name_AtoW( root, FALSE ))) return FALSE;
695 labelW = label ? HeapAlloc(GetProcessHeap(), 0, label_len * sizeof(WCHAR)) : NULL;
696 fsnameW = fsname ? HeapAlloc(GetProcessHeap(), 0, fsname_len * sizeof(WCHAR)) : NULL;
698 if ((ret = GetVolumeInformationW(rootW, labelW, label_len, serial,
699 filename_len, flags, fsnameW, fsname_len)))
701 if (label) FILE_name_WtoA( labelW, -1, label, label_len );
702 if (fsname) FILE_name_WtoA( fsnameW, -1, fsname, fsname_len );
705 HeapFree( GetProcessHeap(), 0, labelW );
706 HeapFree( GetProcessHeap(), 0, fsnameW );
707 return ret;
712 /***********************************************************************
713 * SetVolumeLabelW (KERNEL32.@)
715 BOOL WINAPI SetVolumeLabelW( LPCWSTR root, LPCWSTR label )
717 WCHAR device[] = {'\\','\\','.','\\','A',':',0};
718 HANDLE handle;
719 enum fs_type type = FS_UNKNOWN;
721 if (!root)
723 WCHAR path[MAX_PATH];
724 GetCurrentDirectoryW( MAX_PATH, path );
725 device[4] = path[0];
727 else
729 if (!root[0] || root[1] != ':')
731 SetLastError( ERROR_INVALID_NAME );
732 return FALSE;
734 device[4] = root[0];
737 /* try to open the device */
739 handle = CreateFileW( device, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE,
740 NULL, OPEN_EXISTING, 0, 0 );
741 if (handle != INVALID_HANDLE_VALUE)
743 BYTE superblock[SUPERBLOCK_SIZE];
745 type = VOLUME_ReadFATSuperblock( handle, superblock );
746 if (type == FS_UNKNOWN) type = VOLUME_ReadCDSuperblock( handle, superblock );
747 CloseHandle( handle );
748 if (type != FS_UNKNOWN)
750 /* we can't set the label on FAT or CDROM file systems */
751 TRACE( "cannot set label on device %s type %d\n", debugstr_w(device), type );
752 SetLastError( ERROR_ACCESS_DENIED );
753 return FALSE;
756 else
758 TRACE( "cannot open device %s: err %d\n", debugstr_w(device), GetLastError() );
759 if (GetLastError() == ERROR_ACCESS_DENIED) return FALSE;
762 /* we couldn't open the device, fallback to default strategy */
764 switch(GetDriveTypeW( root ))
766 case DRIVE_UNKNOWN:
767 case DRIVE_NO_ROOT_DIR:
768 SetLastError( ERROR_NOT_READY );
769 break;
770 case DRIVE_REMOVABLE:
771 case DRIVE_FIXED:
773 WCHAR labelW[] = {'A',':','\\','.','w','i','n','d','o','w','s','-','l','a','b','e','l',0};
775 labelW[0] = device[4];
777 if (!label[0]) /* delete label file when setting an empty label */
778 return DeleteFileW( labelW ) || GetLastError() == ERROR_FILE_NOT_FOUND;
780 handle = CreateFileW( labelW, GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
781 CREATE_ALWAYS, 0, 0 );
782 if (handle != INVALID_HANDLE_VALUE)
784 char buffer[64];
785 DWORD size;
787 if (!WideCharToMultiByte( CP_UNIXCP, 0, label, -1, buffer, sizeof(buffer)-1, NULL, NULL ))
788 buffer[sizeof(buffer)-2] = 0;
789 strcat( buffer, "\n" );
790 WriteFile( handle, buffer, strlen(buffer), &size, NULL );
791 CloseHandle( handle );
792 return TRUE;
794 break;
796 case DRIVE_REMOTE:
797 case DRIVE_RAMDISK:
798 case DRIVE_CDROM:
799 SetLastError( ERROR_ACCESS_DENIED );
800 break;
802 return FALSE;
805 /***********************************************************************
806 * SetVolumeLabelA (KERNEL32.@)
808 BOOL WINAPI SetVolumeLabelA(LPCSTR root, LPCSTR volname)
810 WCHAR *rootW = NULL, *volnameW = NULL;
811 BOOL ret;
813 if (root && !(rootW = FILE_name_AtoW( root, FALSE ))) return FALSE;
814 if (volname && !(volnameW = FILE_name_AtoW( volname, TRUE ))) return FALSE;
815 ret = SetVolumeLabelW( rootW, volnameW );
816 HeapFree( GetProcessHeap(), 0, volnameW );
817 return ret;
821 /***********************************************************************
822 * GetVolumeNameForVolumeMountPointA (KERNEL32.@)
824 BOOL WINAPI GetVolumeNameForVolumeMountPointA( LPCSTR path, LPSTR volume, DWORD size )
826 BOOL ret;
827 WCHAR volumeW[50], *pathW = NULL;
828 DWORD len = min( sizeof(volumeW) / sizeof(WCHAR), size );
830 TRACE("(%s, %p, %x)\n", debugstr_a(path), volume, size);
832 if (!path || !(pathW = FILE_name_AtoW( path, TRUE )))
833 return FALSE;
835 if ((ret = GetVolumeNameForVolumeMountPointW( pathW, volumeW, len )))
836 FILE_name_WtoA( volumeW, -1, volume, len );
838 HeapFree( GetProcessHeap(), 0, pathW );
839 return ret;
842 /***********************************************************************
843 * GetVolumeNameForVolumeMountPointW (KERNEL32.@)
845 BOOL WINAPI GetVolumeNameForVolumeMountPointW( LPCWSTR path, LPWSTR volume, DWORD size )
847 static const WCHAR prefixW[] = {'\\','D','o','s','D','e','v','i','c','e','s','\\',0};
848 static const WCHAR volumeW[] = {'\\','?','?','\\','V','o','l','u','m','e','{',0};
849 static const WCHAR trailingW[] = {'\\',0};
851 MOUNTMGR_MOUNT_POINT *input = NULL, *o1;
852 MOUNTMGR_MOUNT_POINTS *output = NULL;
853 WCHAR *p;
854 char *r;
855 DWORD i, i_size = 1024, o_size = 1024;
856 WCHAR *nonpersist_name;
857 WCHAR symlink_name[MAX_PATH];
858 NTSTATUS status;
859 HANDLE mgr = INVALID_HANDLE_VALUE;
860 BOOL ret = FALSE;
862 TRACE("(%s, %p, %x)\n", debugstr_w(path), volume, size);
863 if (path[lstrlenW(path)-1] != '\\')
865 SetLastError( ERROR_INVALID_NAME );
866 return FALSE;
869 if (size < 50)
871 SetLastError( ERROR_FILENAME_EXCED_RANGE );
872 return FALSE;
874 /* if length of input is > 3 then it must be a mounted folder */
875 if (lstrlenW(path) > 3)
877 FIXME("Mounted Folders are not yet supported\n");
878 SetLastError( ERROR_NOT_A_REPARSE_POINT );
879 return FALSE;
882 mgr = CreateFileW( MOUNTMGR_DOS_DEVICE_NAME, 0, FILE_SHARE_READ,
883 NULL, OPEN_EXISTING, 0, 0 );
884 if (mgr == INVALID_HANDLE_VALUE) return FALSE;
886 if (!(input = HeapAlloc( GetProcessHeap(), 0, i_size )))
888 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
889 goto err_ret;
892 if (!(output = HeapAlloc( GetProcessHeap(), 0, o_size )))
894 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
895 goto err_ret;
898 /* construct the symlink name as "\DosDevices\C:" */
899 lstrcpyW( symlink_name, prefixW );
900 lstrcatW( symlink_name, path );
901 symlink_name[lstrlenW(symlink_name)-1] = 0;
903 /* Take the mount point and get the "nonpersistent name" */
904 /* We will then take that and get the volume name */
905 nonpersist_name = (WCHAR *)(input + 1);
906 status = read_nt_symlink( symlink_name, nonpersist_name, i_size - sizeof(*input) );
907 TRACE("read_nt_symlink got stat=%x, for %s, got <%s>\n", status,
908 debugstr_w(symlink_name), debugstr_w(nonpersist_name));
909 if (status != STATUS_SUCCESS)
911 SetLastError( ERROR_FILE_NOT_FOUND );
912 goto err_ret;
915 /* Now take the "nonpersistent name" and ask the mountmgr */
916 /* to give us all the mount points. One of them will be */
917 /* the volume name (format of \??\Volume{). */
918 memset( input, 0, sizeof(*input) ); /* clear all input parameters */
919 input->DeviceNameOffset = sizeof(*input);
920 input->DeviceNameLength = lstrlenW( nonpersist_name) * sizeof(WCHAR);
921 i_size = input->DeviceNameOffset + input->DeviceNameLength;
923 output->Size = o_size;
925 /* now get the true volume name from the mountmgr */
926 if (!DeviceIoControl( mgr, IOCTL_MOUNTMGR_QUERY_POINTS, input, i_size,
927 output, o_size, NULL, NULL ))
928 goto err_ret;
930 /* Verify and return the data, note string is not null terminated */
931 TRACE("found %d matching mount points\n", output->NumberOfMountPoints);
932 if (output->NumberOfMountPoints < 1)
934 SetLastError( ERROR_NO_VOLUME_ID );
935 goto err_ret;
937 o1 = &output->MountPoints[0];
939 /* look for the volume name in returned values */
940 for(i=0;i<output->NumberOfMountPoints;i++)
942 p = (WCHAR*)((char *)output + o1->SymbolicLinkNameOffset);
943 r = (char *)output + o1->UniqueIdOffset;
944 TRACE("found symlink=%s, unique=%s, devname=%s\n",
945 debugstr_wn(p, o1->SymbolicLinkNameLength/sizeof(WCHAR)),
946 debugstr_an(r, o1->UniqueIdLength),
947 debugstr_wn((WCHAR*)((char *)output + o1->DeviceNameOffset),
948 o1->DeviceNameLength/sizeof(WCHAR)));
950 if (!strncmpW( p, volumeW, (sizeof(volumeW)-1)/sizeof(WCHAR) ))
952 /* is there space in the return variable ?? */
953 if ((o1->SymbolicLinkNameLength/sizeof(WCHAR))+2 > size)
955 SetLastError( ERROR_FILENAME_EXCED_RANGE );
956 goto err_ret;
958 memcpy( volume, p, o1->SymbolicLinkNameLength );
959 volume[o1->SymbolicLinkNameLength / sizeof(WCHAR)] = 0;
960 lstrcatW( volume, trailingW );
961 /* change second char from '?' to '\' */
962 volume[1] = '\\';
963 ret = TRUE;
964 break;
966 o1++;
969 err_ret:
970 HeapFree( GetProcessHeap(), 0, input );
971 HeapFree( GetProcessHeap(), 0, output );
972 CloseHandle( mgr );
973 return ret;
976 /***********************************************************************
977 * DefineDosDeviceW (KERNEL32.@)
979 BOOL WINAPI DefineDosDeviceW( DWORD flags, LPCWSTR devname, LPCWSTR targetpath )
981 DWORD len, dosdev;
982 BOOL ret = FALSE;
983 char *path = NULL, *target, *p;
985 TRACE("%x, %s, %s\n", flags, debugstr_w(devname), debugstr_w(targetpath));
987 if (!(flags & DDD_REMOVE_DEFINITION))
989 if (!(flags & DDD_RAW_TARGET_PATH))
991 FIXME( "(0x%08x,%s,%s) DDD_RAW_TARGET_PATH flag not set, not supported yet\n",
992 flags, debugstr_w(devname), debugstr_w(targetpath) );
993 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
994 return FALSE;
997 len = WideCharToMultiByte( CP_UNIXCP, 0, targetpath, -1, NULL, 0, NULL, NULL );
998 if ((target = HeapAlloc( GetProcessHeap(), 0, len )))
1000 WideCharToMultiByte( CP_UNIXCP, 0, targetpath, -1, target, len, NULL, NULL );
1001 for (p = target; *p; p++) if (*p == '\\') *p = '/';
1003 else
1005 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1006 return FALSE;
1009 else target = NULL;
1011 /* first check for a DOS device */
1013 if ((dosdev = RtlIsDosDeviceName_U( devname )))
1015 WCHAR name[5];
1017 memcpy( name, devname + HIWORD(dosdev)/sizeof(WCHAR), LOWORD(dosdev) );
1018 name[LOWORD(dosdev)/sizeof(WCHAR)] = 0;
1019 path = get_dos_device_path( name );
1021 else if (isalphaW(devname[0]) && devname[1] == ':' && !devname[2]) /* drive mapping */
1023 path = get_dos_device_path( devname );
1025 else SetLastError( ERROR_FILE_NOT_FOUND );
1027 if (path)
1029 if (target)
1031 TRACE( "creating symlink %s -> %s\n", path, target );
1032 unlink( path );
1033 if (!symlink( target, path )) ret = TRUE;
1034 else FILE_SetDosError();
1036 else
1038 TRACE( "removing symlink %s\n", path );
1039 if (!unlink( path )) ret = TRUE;
1040 else FILE_SetDosError();
1042 HeapFree( GetProcessHeap(), 0, path );
1044 HeapFree( GetProcessHeap(), 0, target );
1045 return ret;
1049 /***********************************************************************
1050 * DefineDosDeviceA (KERNEL32.@)
1052 BOOL WINAPI DefineDosDeviceA(DWORD flags, LPCSTR devname, LPCSTR targetpath)
1054 WCHAR *devW, *targetW = NULL;
1055 BOOL ret;
1057 if (!(devW = FILE_name_AtoW( devname, FALSE ))) return FALSE;
1058 if (targetpath && !(targetW = FILE_name_AtoW( targetpath, TRUE ))) return FALSE;
1059 ret = DefineDosDeviceW(flags, devW, targetW);
1060 HeapFree( GetProcessHeap(), 0, targetW );
1061 return ret;
1065 /***********************************************************************
1066 * QueryDosDeviceW (KERNEL32.@)
1068 * returns array of strings terminated by \0, terminated by \0
1070 DWORD WINAPI QueryDosDeviceW( LPCWSTR devname, LPWSTR target, DWORD bufsize )
1072 static const WCHAR auxW[] = {'A','U','X',0};
1073 static const WCHAR nulW[] = {'N','U','L',0};
1074 static const WCHAR prnW[] = {'P','R','N',0};
1075 static const WCHAR comW[] = {'C','O','M',0};
1076 static const WCHAR lptW[] = {'L','P','T',0};
1077 static const WCHAR com0W[] = {'\\','?','?','\\','C','O','M','0',0};
1078 static const WCHAR com1W[] = {'\\','D','o','s','D','e','v','i','c','e','s','\\','C','O','M','1',0,0};
1079 static const WCHAR lpt1W[] = {'\\','D','o','s','D','e','v','i','c','e','s','\\','L','P','T','1',0,0};
1080 static const WCHAR dosdevW[] = {'\\','D','o','s','D','e','v','i','c','e','s','\\',0};
1082 UNICODE_STRING nt_name;
1083 ANSI_STRING unix_name;
1084 WCHAR nt_buffer[10];
1085 NTSTATUS status;
1087 if (!bufsize)
1089 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1090 return 0;
1093 if (devname)
1095 WCHAR *p, name[5];
1096 char *path, *link;
1097 DWORD dosdev, ret = 0;
1099 if ((dosdev = RtlIsDosDeviceName_U( devname )))
1101 memcpy( name, devname + HIWORD(dosdev)/sizeof(WCHAR), LOWORD(dosdev) );
1102 name[LOWORD(dosdev)/sizeof(WCHAR)] = 0;
1104 else
1106 NTSTATUS status;
1107 WCHAR *buffer;
1109 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, sizeof(dosdevW) + strlenW(devname)*sizeof(WCHAR) )))
1111 SetLastError( ERROR_OUTOFMEMORY );
1112 return 0;
1114 memcpy( buffer, dosdevW, sizeof(dosdevW) );
1115 strcatW( buffer, devname );
1116 status = read_nt_symlink( buffer, target, bufsize );
1117 HeapFree( GetProcessHeap(), 0, buffer );
1118 if (status)
1120 SetLastError( RtlNtStatusToDosError(status) );
1121 return 0;
1123 ret = strlenW( target ) + 1;
1124 goto done;
1127 /* FIXME: should read NT symlink for all devices */
1129 if (!(path = get_dos_device_path( name ))) return 0;
1130 link = read_symlink( path );
1131 HeapFree( GetProcessHeap(), 0, path );
1133 if (link)
1135 ret = MultiByteToWideChar( CP_UNIXCP, 0, link, -1, target, bufsize );
1136 HeapFree( GetProcessHeap(), 0, link );
1138 else if (dosdev) /* look for device defaults */
1140 if (!strcmpiW( name, auxW ))
1142 if (bufsize >= sizeof(com1W)/sizeof(WCHAR))
1144 memcpy( target, com1W, sizeof(com1W) );
1145 ret = sizeof(com1W)/sizeof(WCHAR);
1147 else SetLastError( ERROR_INSUFFICIENT_BUFFER );
1148 return ret;
1150 if (!strcmpiW( name, prnW ))
1152 if (bufsize >= sizeof(lpt1W)/sizeof(WCHAR))
1154 memcpy( target, lpt1W, sizeof(lpt1W) );
1155 ret = sizeof(lpt1W)/sizeof(WCHAR);
1157 else SetLastError( ERROR_INSUFFICIENT_BUFFER );
1158 return ret;
1161 nt_buffer[0] = '\\';
1162 nt_buffer[1] = '?';
1163 nt_buffer[2] = '?';
1164 nt_buffer[3] = '\\';
1165 strcpyW( nt_buffer + 4, name );
1166 RtlInitUnicodeString( &nt_name, nt_buffer );
1167 status = wine_nt_to_unix_file_name( &nt_name, &unix_name, FILE_OPEN, TRUE );
1168 if (status) SetLastError( RtlNtStatusToDosError(status) );
1169 else
1171 ret = MultiByteToWideChar( CP_UNIXCP, 0, unix_name.Buffer, -1, target, bufsize );
1172 RtlFreeAnsiString( &unix_name );
1175 done:
1176 if (ret)
1178 if (ret < bufsize) target[ret++] = 0; /* add an extra null */
1179 for (p = target; *p; p++) if (*p == '/') *p = '\\';
1182 return ret;
1184 else /* return a list of all devices */
1186 OBJECT_ATTRIBUTES attr;
1187 HANDLE handle;
1188 WCHAR *p = target;
1189 int i;
1191 if (bufsize <= (sizeof(auxW)+sizeof(nulW)+sizeof(prnW))/sizeof(WCHAR))
1193 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1194 return 0;
1197 /* FIXME: these should be NT symlinks too */
1199 memcpy( p, auxW, sizeof(auxW) );
1200 p += sizeof(auxW) / sizeof(WCHAR);
1201 memcpy( p, nulW, sizeof(nulW) );
1202 p += sizeof(nulW) / sizeof(WCHAR);
1203 memcpy( p, prnW, sizeof(prnW) );
1204 p += sizeof(prnW) / sizeof(WCHAR);
1206 strcpyW( nt_buffer, com0W );
1207 RtlInitUnicodeString( &nt_name, nt_buffer );
1209 for (i = 1; i <= 9; i++)
1211 nt_buffer[7] = '0' + i;
1212 if (!wine_nt_to_unix_file_name( &nt_name, &unix_name, FILE_OPEN, TRUE ))
1214 RtlFreeAnsiString( &unix_name );
1215 if (p + 5 >= target + bufsize)
1217 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1218 return 0;
1220 strcpyW( p, comW );
1221 p[3] = '0' + i;
1222 p[4] = 0;
1223 p += 5;
1226 strcpyW( nt_buffer + 4, lptW );
1227 for (i = 1; i <= 9; i++)
1229 nt_buffer[7] = '0' + i;
1230 if (!wine_nt_to_unix_file_name( &nt_name, &unix_name, FILE_OPEN, TRUE ))
1232 RtlFreeAnsiString( &unix_name );
1233 if (p + 5 >= target + bufsize)
1235 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1236 return 0;
1238 strcpyW( p, lptW );
1239 p[3] = '0' + i;
1240 p[4] = 0;
1241 p += 5;
1245 RtlInitUnicodeString( &nt_name, dosdevW );
1246 nt_name.Length -= sizeof(WCHAR); /* without trailing slash */
1247 attr.Length = sizeof(attr);
1248 attr.RootDirectory = 0;
1249 attr.ObjectName = &nt_name;
1250 attr.Attributes = OBJ_CASE_INSENSITIVE;
1251 attr.SecurityDescriptor = NULL;
1252 attr.SecurityQualityOfService = NULL;
1253 status = NtOpenDirectoryObject( &handle, FILE_LIST_DIRECTORY, &attr );
1254 if (!status)
1256 char data[1024];
1257 DIRECTORY_BASIC_INFORMATION *info = (DIRECTORY_BASIC_INFORMATION *)data;
1258 ULONG ctx = 0, len;
1260 while (!NtQueryDirectoryObject( handle, info, sizeof(data), 1, 0, &ctx, &len ))
1262 if (p + info->ObjectName.Length/sizeof(WCHAR) + 1 >= target + bufsize)
1264 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1265 NtClose( handle );
1266 return 0;
1268 memcpy( p, info->ObjectName.Buffer, info->ObjectName.Length );
1269 p += info->ObjectName.Length/sizeof(WCHAR);
1270 *p++ = 0;
1272 NtClose( handle );
1275 *p++ = 0; /* terminating null */
1276 return p - target;
1281 /***********************************************************************
1282 * QueryDosDeviceA (KERNEL32.@)
1284 * returns array of strings terminated by \0, terminated by \0
1286 DWORD WINAPI QueryDosDeviceA( LPCSTR devname, LPSTR target, DWORD bufsize )
1288 DWORD ret = 0, retW;
1289 WCHAR *devnameW = NULL;
1290 LPWSTR targetW;
1292 if (devname && !(devnameW = FILE_name_AtoW( devname, FALSE ))) return 0;
1294 targetW = HeapAlloc( GetProcessHeap(),0, bufsize * sizeof(WCHAR) );
1295 if (!targetW)
1297 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1298 return 0;
1301 retW = QueryDosDeviceW(devnameW, targetW, bufsize);
1303 ret = FILE_name_WtoA( targetW, retW, target, bufsize );
1305 HeapFree(GetProcessHeap(), 0, targetW);
1306 return ret;
1310 /***********************************************************************
1311 * GetLogicalDrives (KERNEL32.@)
1313 DWORD WINAPI GetLogicalDrives(void)
1315 const char *config_dir = wine_get_config_dir();
1316 struct stat st;
1317 char *buffer, *dev;
1318 DWORD ret = 0;
1319 int i;
1321 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, strlen(config_dir) + sizeof("/dosdevices/a:") )))
1323 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1324 return 0;
1326 strcpy( buffer, config_dir );
1327 strcat( buffer, "/dosdevices/a:" );
1328 dev = buffer + strlen(buffer) - 2;
1330 for (i = 0; i < 26; i++)
1332 *dev = 'a' + i;
1333 if (!stat( buffer, &st )) ret |= (1 << i);
1335 HeapFree( GetProcessHeap(), 0, buffer );
1336 return ret;
1340 /***********************************************************************
1341 * GetLogicalDriveStringsA (KERNEL32.@)
1343 UINT WINAPI GetLogicalDriveStringsA( UINT len, LPSTR buffer )
1345 DWORD drives = GetLogicalDrives();
1346 UINT drive, count;
1348 for (drive = count = 0; drive < 26; drive++) if (drives & (1 << drive)) count++;
1349 if ((count * 4) + 1 > len) return count * 4 + 1;
1351 for (drive = 0; drive < 26; drive++)
1353 if (drives & (1 << drive))
1355 *buffer++ = 'A' + drive;
1356 *buffer++ = ':';
1357 *buffer++ = '\\';
1358 *buffer++ = 0;
1361 *buffer = 0;
1362 return count * 4;
1366 /***********************************************************************
1367 * GetLogicalDriveStringsW (KERNEL32.@)
1369 UINT WINAPI GetLogicalDriveStringsW( UINT len, LPWSTR buffer )
1371 DWORD drives = GetLogicalDrives();
1372 UINT drive, count;
1374 for (drive = count = 0; drive < 26; drive++) if (drives & (1 << drive)) count++;
1375 if ((count * 4) + 1 > len) return count * 4 + 1;
1377 for (drive = 0; drive < 26; drive++)
1379 if (drives & (1 << drive))
1381 *buffer++ = 'A' + drive;
1382 *buffer++ = ':';
1383 *buffer++ = '\\';
1384 *buffer++ = 0;
1387 *buffer = 0;
1388 return count * 4;
1392 /***********************************************************************
1393 * GetDriveTypeW (KERNEL32.@)
1395 * Returns the type of the disk drive specified. If root is NULL the
1396 * root of the current directory is used.
1398 * RETURNS
1400 * Type of drive (from Win32 SDK):
1402 * DRIVE_UNKNOWN unable to find out anything about the drive
1403 * DRIVE_NO_ROOT_DIR nonexistent root dir
1404 * DRIVE_REMOVABLE the disk can be removed from the machine
1405 * DRIVE_FIXED the disk cannot be removed from the machine
1406 * DRIVE_REMOTE network disk
1407 * DRIVE_CDROM CDROM drive
1408 * DRIVE_RAMDISK virtual disk in RAM
1410 UINT WINAPI GetDriveTypeW(LPCWSTR root) /* [in] String describing drive */
1412 FILE_FS_DEVICE_INFORMATION info;
1413 IO_STATUS_BLOCK io;
1414 NTSTATUS status;
1415 HANDLE handle;
1416 UINT ret;
1418 if (!open_device_root( root, &handle )) return DRIVE_NO_ROOT_DIR;
1420 status = NtQueryVolumeInformationFile( handle, &io, &info, sizeof(info), FileFsDeviceInformation );
1421 NtClose( handle );
1422 if (status != STATUS_SUCCESS)
1424 SetLastError( RtlNtStatusToDosError(status) );
1425 ret = DRIVE_UNKNOWN;
1427 else
1429 switch (info.DeviceType)
1431 case FILE_DEVICE_CD_ROM_FILE_SYSTEM: ret = DRIVE_CDROM; break;
1432 case FILE_DEVICE_VIRTUAL_DISK: ret = DRIVE_RAMDISK; break;
1433 case FILE_DEVICE_NETWORK_FILE_SYSTEM: ret = DRIVE_REMOTE; break;
1434 case FILE_DEVICE_DISK_FILE_SYSTEM:
1435 if (info.Characteristics & FILE_REMOTE_DEVICE) ret = DRIVE_REMOTE;
1436 else if (info.Characteristics & FILE_REMOVABLE_MEDIA) ret = DRIVE_REMOVABLE;
1437 else if ((ret = get_mountmgr_drive_type( root )) == DRIVE_UNKNOWN) ret = DRIVE_FIXED;
1438 break;
1439 default:
1440 ret = DRIVE_UNKNOWN;
1441 break;
1444 TRACE( "%s -> %d\n", debugstr_w(root), ret );
1445 return ret;
1449 /***********************************************************************
1450 * GetDriveTypeA (KERNEL32.@)
1452 * See GetDriveTypeW.
1454 UINT WINAPI GetDriveTypeA( LPCSTR root )
1456 WCHAR *rootW = NULL;
1458 if (root && !(rootW = FILE_name_AtoW( root, FALSE ))) return DRIVE_NO_ROOT_DIR;
1459 return GetDriveTypeW( rootW );
1463 /***********************************************************************
1464 * GetDiskFreeSpaceExW (KERNEL32.@)
1466 * This function is used to acquire the size of the available and
1467 * total space on a logical volume.
1469 * RETURNS
1471 * Zero on failure, nonzero upon success. Use GetLastError to obtain
1472 * detailed error information.
1475 BOOL WINAPI GetDiskFreeSpaceExW( LPCWSTR root, PULARGE_INTEGER avail,
1476 PULARGE_INTEGER total, PULARGE_INTEGER totalfree )
1478 FILE_FS_SIZE_INFORMATION info;
1479 IO_STATUS_BLOCK io;
1480 NTSTATUS status;
1481 HANDLE handle;
1482 UINT units;
1484 TRACE( "%s,%p,%p,%p\n", debugstr_w(root), avail, total, totalfree );
1486 if (!open_device_root( root, &handle )) return FALSE;
1488 status = NtQueryVolumeInformationFile( handle, &io, &info, sizeof(info), FileFsSizeInformation );
1489 NtClose( handle );
1490 if (status != STATUS_SUCCESS)
1492 SetLastError( RtlNtStatusToDosError(status) );
1493 return FALSE;
1496 units = info.SectorsPerAllocationUnit * info.BytesPerSector;
1497 if (total) total->QuadPart = info.TotalAllocationUnits.QuadPart * units;
1498 if (totalfree) totalfree->QuadPart = info.AvailableAllocationUnits.QuadPart * units;
1499 /* FIXME: this one should take quotas into account */
1500 if (avail) avail->QuadPart = info.AvailableAllocationUnits.QuadPart * units;
1501 return TRUE;
1505 /***********************************************************************
1506 * GetDiskFreeSpaceExA (KERNEL32.@)
1508 * See GetDiskFreeSpaceExW.
1510 BOOL WINAPI GetDiskFreeSpaceExA( LPCSTR root, PULARGE_INTEGER avail,
1511 PULARGE_INTEGER total, PULARGE_INTEGER totalfree )
1513 WCHAR *rootW = NULL;
1515 if (root && !(rootW = FILE_name_AtoW( root, FALSE ))) return FALSE;
1516 return GetDiskFreeSpaceExW( rootW, avail, total, totalfree );
1520 /***********************************************************************
1521 * GetDiskFreeSpaceW (KERNEL32.@)
1523 BOOL WINAPI GetDiskFreeSpaceW( LPCWSTR root, LPDWORD cluster_sectors,
1524 LPDWORD sector_bytes, LPDWORD free_clusters,
1525 LPDWORD total_clusters )
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,%p\n", debugstr_w(root),
1534 cluster_sectors, sector_bytes, free_clusters, total_clusters );
1536 if (!open_device_root( root, &handle )) return FALSE;
1538 status = NtQueryVolumeInformationFile( handle, &io, &info, sizeof(info), FileFsSizeInformation );
1539 NtClose( handle );
1540 if (status != STATUS_SUCCESS)
1542 SetLastError( RtlNtStatusToDosError(status) );
1543 return FALSE;
1546 units = info.SectorsPerAllocationUnit * info.BytesPerSector;
1548 if( GetVersion() & 0x80000000) { /* win3.x, 9x, ME */
1549 /* cap the size and available at 2GB as per specs */
1550 if (info.TotalAllocationUnits.QuadPart * units > 0x7fffffff) {
1551 info.TotalAllocationUnits.QuadPart = 0x7fffffff / units;
1552 if (info.AvailableAllocationUnits.QuadPart * units > 0x7fffffff)
1553 info.AvailableAllocationUnits.QuadPart = 0x7fffffff / units;
1555 /* nr. of clusters is always <= 65335 */
1556 while( info.TotalAllocationUnits.QuadPart > 65535 ) {
1557 info.TotalAllocationUnits.QuadPart /= 2;
1558 info.AvailableAllocationUnits.QuadPart /= 2;
1559 info.SectorsPerAllocationUnit *= 2;
1563 if (cluster_sectors) *cluster_sectors = info.SectorsPerAllocationUnit;
1564 if (sector_bytes) *sector_bytes = info.BytesPerSector;
1565 if (free_clusters) *free_clusters = info.AvailableAllocationUnits.u.LowPart;
1566 if (total_clusters) *total_clusters = info.TotalAllocationUnits.u.LowPart;
1567 return TRUE;
1571 /***********************************************************************
1572 * GetDiskFreeSpaceA (KERNEL32.@)
1574 BOOL WINAPI GetDiskFreeSpaceA( LPCSTR root, LPDWORD cluster_sectors,
1575 LPDWORD sector_bytes, LPDWORD free_clusters,
1576 LPDWORD total_clusters )
1578 WCHAR *rootW = NULL;
1580 if (root && !(rootW = FILE_name_AtoW( root, FALSE ))) return FALSE;
1581 return GetDiskFreeSpaceW( rootW, cluster_sectors, sector_bytes, free_clusters, total_clusters );
1584 /***********************************************************************
1585 * GetVolumePathNameA (KERNEL32.@)
1587 BOOL WINAPI GetVolumePathNameA(LPCSTR filename, LPSTR volumepathname, DWORD buflen)
1589 BOOL ret;
1590 WCHAR *filenameW = NULL, *volumeW;
1592 FIXME("(%s, %p, %d), stub!\n", debugstr_a(filename), volumepathname, buflen);
1594 if (filename && !(filenameW = FILE_name_AtoW( filename, FALSE ))) return FALSE;
1595 if (!(volumeW = HeapAlloc( GetProcessHeap(), 0, buflen * sizeof(WCHAR) ))) return FALSE;
1597 if ((ret = GetVolumePathNameW( filenameW, volumeW, buflen )))
1598 FILE_name_WtoA( volumeW, -1, volumepathname, buflen );
1600 HeapFree( GetProcessHeap(), 0, volumeW );
1601 return ret;
1604 /***********************************************************************
1605 * GetVolumePathNameW (KERNEL32.@)
1607 BOOL WINAPI GetVolumePathNameW(LPCWSTR filename, LPWSTR volumepathname, DWORD buflen)
1609 const WCHAR *p = filename;
1611 FIXME("(%s, %p, %d), stub!\n", debugstr_w(filename), volumepathname, buflen);
1613 if (p && tolowerW(p[0]) >= 'a' && tolowerW(p[0]) <= 'z' && p[1] ==':' && p[2] == '\\' && buflen >= 4)
1615 volumepathname[0] = p[0];
1616 volumepathname[1] = ':';
1617 volumepathname[2] = '\\';
1618 volumepathname[3] = 0;
1619 return TRUE;
1621 return FALSE;
1624 /***********************************************************************
1625 * GetVolumePathNamesForVolumeNameW (KERNEL32.@)
1627 BOOL WINAPI GetVolumePathNamesForVolumeNameW(LPCWSTR volumename, LPWSTR volumepathname, DWORD buflen, PDWORD returnlen)
1629 FIXME("(%s, %p, %d, %p), stub!\n", debugstr_w(volumename), volumepathname, buflen, returnlen);
1630 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1631 return FALSE;
1634 /***********************************************************************
1635 * FindFirstVolumeA (KERNEL32.@)
1637 HANDLE WINAPI FindFirstVolumeA(LPSTR volume, DWORD len)
1639 WCHAR *buffer = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1640 HANDLE handle = FindFirstVolumeW( buffer, len );
1642 if (handle != INVALID_HANDLE_VALUE)
1644 if (!WideCharToMultiByte( CP_ACP, 0, buffer, -1, volume, len, NULL, NULL ))
1646 FindVolumeClose( handle );
1647 handle = INVALID_HANDLE_VALUE;
1650 HeapFree( GetProcessHeap(), 0, buffer );
1651 return handle;
1654 /***********************************************************************
1655 * FindFirstVolumeW (KERNEL32.@)
1657 HANDLE WINAPI FindFirstVolumeW( LPWSTR volume, DWORD len )
1659 DWORD size = 1024;
1660 HANDLE mgr = CreateFileW( MOUNTMGR_DOS_DEVICE_NAME, 0, FILE_SHARE_READ|FILE_SHARE_WRITE,
1661 NULL, OPEN_EXISTING, 0, 0 );
1662 if (mgr == INVALID_HANDLE_VALUE) return INVALID_HANDLE_VALUE;
1664 for (;;)
1666 MOUNTMGR_MOUNT_POINT input;
1667 MOUNTMGR_MOUNT_POINTS *output;
1669 if (!(output = HeapAlloc( GetProcessHeap(), 0, size )))
1671 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1672 break;
1674 memset( &input, 0, sizeof(input) );
1676 if (!DeviceIoControl( mgr, IOCTL_MOUNTMGR_QUERY_POINTS, &input, sizeof(input),
1677 output, size, NULL, NULL ))
1679 if (GetLastError() != ERROR_MORE_DATA) break;
1680 size = output->Size;
1681 HeapFree( GetProcessHeap(), 0, output );
1682 continue;
1684 CloseHandle( mgr );
1685 /* abuse the Size field to store the current index */
1686 output->Size = 0;
1687 if (!FindNextVolumeW( output, volume, len ))
1689 HeapFree( GetProcessHeap(), 0, output );
1690 return INVALID_HANDLE_VALUE;
1692 return output;
1694 CloseHandle( mgr );
1695 return INVALID_HANDLE_VALUE;
1698 /***********************************************************************
1699 * FindNextVolumeA (KERNEL32.@)
1701 BOOL WINAPI FindNextVolumeA( HANDLE handle, LPSTR volume, DWORD len )
1703 WCHAR *buffer = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1704 BOOL ret;
1706 if ((ret = FindNextVolumeW( handle, buffer, len )))
1708 if (!WideCharToMultiByte( CP_ACP, 0, buffer, -1, volume, len, NULL, NULL )) ret = FALSE;
1710 HeapFree( GetProcessHeap(), 0, buffer );
1711 return ret;
1714 /***********************************************************************
1715 * FindNextVolumeW (KERNEL32.@)
1717 BOOL WINAPI FindNextVolumeW( HANDLE handle, LPWSTR volume, DWORD len )
1719 MOUNTMGR_MOUNT_POINTS *data = handle;
1721 while (data->Size < data->NumberOfMountPoints)
1723 static const WCHAR volumeW[] = {'\\','?','?','\\','V','o','l','u','m','e','{',};
1724 WCHAR *link = (WCHAR *)((char *)data + data->MountPoints[data->Size].SymbolicLinkNameOffset);
1725 DWORD size = data->MountPoints[data->Size].SymbolicLinkNameLength;
1726 data->Size++;
1727 /* skip non-volumes */
1728 if (size < sizeof(volumeW) || memcmp( link, volumeW, sizeof(volumeW) )) continue;
1729 if (size + sizeof(WCHAR) >= len * sizeof(WCHAR))
1731 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1732 return FALSE;
1734 memcpy( volume, link, size );
1735 volume[1] = '\\'; /* map \??\ to \\?\ */
1736 volume[size / sizeof(WCHAR)] = '\\'; /* Windows appends a backslash */
1737 volume[size / sizeof(WCHAR) + 1] = 0;
1738 TRACE( "returning entry %u %s\n", data->Size - 1, debugstr_w(volume) );
1739 return TRUE;
1741 SetLastError( ERROR_NO_MORE_FILES );
1742 return FALSE;
1745 /***********************************************************************
1746 * FindVolumeClose (KERNEL32.@)
1748 BOOL WINAPI FindVolumeClose(HANDLE handle)
1750 return HeapFree( GetProcessHeap(), 0, handle );
1753 /***********************************************************************
1754 * FindFirstVolumeMountPointA (KERNEL32.@)
1756 HANDLE WINAPI FindFirstVolumeMountPointA(LPCSTR root, LPSTR mount_point, DWORD len)
1758 FIXME("(%s, %p, %d), stub!\n", debugstr_a(root), mount_point, len);
1759 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1760 return INVALID_HANDLE_VALUE;
1763 /***********************************************************************
1764 * FindFirstVolumeMountPointW (KERNEL32.@)
1766 HANDLE WINAPI FindFirstVolumeMountPointW(LPCWSTR root, LPWSTR mount_point, DWORD len)
1768 FIXME("(%s, %p, %d), stub!\n", debugstr_w(root), mount_point, len);
1769 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1770 return INVALID_HANDLE_VALUE;
1773 /***********************************************************************
1774 * FindVolumeMountPointClose (KERNEL32.@)
1776 BOOL WINAPI FindVolumeMountPointClose(HANDLE h)
1778 FIXME("(%p), stub!\n", h);
1779 return FALSE;