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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include "wine/port.h"
41 #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
51 #define CDFRAMES_PERSEC 75
52 #define CDFRAMES_PERMIN (CDFRAMES_PERSEC * 60)
53 #define FRAME_OF_ADDR(a) ((a)[1] * CDFRAMES_PERMIN + (a)[2] * CDFRAMES_PERSEC + (a)[3])
54 #define FRAME_OF_TOC(toc, idx) FRAME_OF_ADDR((toc)->TrackData[(idx) - (toc)->FirstTrack].Address)
56 #define GETWORD(buf,off) MAKEWORD(buf[(off)],buf[(off+1)])
57 #define GETLONG(buf,off) MAKELONG(GETWORD(buf,off),GETWORD(buf,off+2))
61 FS_ERROR
, /* error accessing the device */
62 FS_UNKNOWN
, /* unknown file system */
68 static const WCHAR drive_types
[][8] =
70 { 0 }, /* DRIVE_UNKNOWN */
71 { 0 }, /* DRIVE_NO_ROOT_DIR */
72 {'f','l','o','p','p','y',0}, /* DRIVE_REMOVABLE */
73 {'h','d',0}, /* DRIVE_FIXED */
74 {'n','e','t','w','o','r','k',0}, /* DRIVE_REMOTE */
75 {'c','d','r','o','m',0}, /* DRIVE_CDROM */
76 {'r','a','m','d','i','s','k',0} /* DRIVE_RAMDISK */
79 /* read a Unix symlink; returned buffer must be freed by caller */
80 static char *read_symlink( const char *path
)
87 if (!(buffer
= HeapAlloc( GetProcessHeap(), 0, size
)))
89 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
92 ret
= readlink( path
, buffer
, size
);
96 HeapFree( GetProcessHeap(), 0, buffer
);
104 HeapFree( GetProcessHeap(), 0, buffer
);
109 /* get the path of a dos device symlink in the $WINEPREFIX/dosdevices directory */
110 static char *get_dos_device_path( LPCWSTR name
)
112 const char *config_dir
= wine_get_config_dir();
116 if (!(buffer
= HeapAlloc( GetProcessHeap(), 0,
117 strlen(config_dir
) + sizeof("/dosdevices/") + 5 )))
119 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
122 strcpy( buffer
, config_dir
);
123 strcat( buffer
, "/dosdevices/" );
124 dev
= buffer
+ strlen(buffer
);
125 /* no codepage conversion, DOS device names are ASCII anyway */
126 for (i
= 0; i
< 5; i
++)
127 if (!(dev
[i
] = (char)tolowerW(name
[i
]))) break;
133 /* open a handle to a device root */
134 static BOOL
open_device_root( LPCWSTR root
, HANDLE
*handle
)
136 static const WCHAR default_rootW
[] = {'\\',0};
137 UNICODE_STRING nt_name
;
138 OBJECT_ATTRIBUTES attr
;
142 if (!root
) root
= default_rootW
;
143 if (!RtlDosPathNameToNtPathName_U( root
, &nt_name
, NULL
, NULL
))
145 SetLastError( ERROR_PATH_NOT_FOUND
);
148 attr
.Length
= sizeof(attr
);
149 attr
.RootDirectory
= 0;
150 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
151 attr
.ObjectName
= &nt_name
;
152 attr
.SecurityDescriptor
= NULL
;
153 attr
.SecurityQualityOfService
= NULL
;
155 status
= NtOpenFile( handle
, 0, &attr
, &io
, 0,
156 FILE_DIRECTORY_FILE
| FILE_SYNCHRONOUS_IO_NONALERT
);
157 RtlFreeUnicodeString( &nt_name
);
158 if (status
!= STATUS_SUCCESS
)
160 SetLastError( RtlNtStatusToDosError(status
) );
167 /* fetch the type of a drive from the registry */
168 static UINT
get_registry_drive_type( const WCHAR
*root
)
170 static const WCHAR drive_types_keyW
[] = {'M','a','c','h','i','n','e','\\',
171 'S','o','f','t','w','a','r','e','\\',
172 'W','i','n','e','\\',
173 'D','r','i','v','e','s',0 };
174 OBJECT_ATTRIBUTES attr
;
175 UNICODE_STRING nameW
;
178 UINT ret
= DRIVE_UNKNOWN
;
179 char tmp
[32 + sizeof(KEY_VALUE_PARTIAL_INFORMATION
)];
180 WCHAR driveW
[] = {'A',':',0};
182 attr
.Length
= sizeof(attr
);
183 attr
.RootDirectory
= 0;
184 attr
.ObjectName
= &nameW
;
186 attr
.SecurityDescriptor
= NULL
;
187 attr
.SecurityQualityOfService
= NULL
;
188 RtlInitUnicodeString( &nameW
, drive_types_keyW
);
189 if (NtOpenKey( &hkey
, KEY_ALL_ACCESS
, &attr
) != STATUS_SUCCESS
) return DRIVE_UNKNOWN
;
191 if (root
) driveW
[0] = root
[0];
194 WCHAR path
[MAX_PATH
];
195 GetCurrentDirectoryW( MAX_PATH
, path
);
199 RtlInitUnicodeString( &nameW
, driveW
);
200 if (!NtQueryValueKey( hkey
, &nameW
, KeyValuePartialInformation
, tmp
, sizeof(tmp
), &dummy
))
203 WCHAR
*data
= (WCHAR
*)((KEY_VALUE_PARTIAL_INFORMATION
*)tmp
)->Data
;
205 for (i
= 0; i
< sizeof(drive_types
)/sizeof(drive_types
[0]); i
++)
207 if (!strcmpiW( data
, drive_types
[i
] ))
219 /* create symlinks for the DOS drives; helper for VOLUME_CreateDevices */
220 static int create_drives( int devices_only
)
222 static const WCHAR PathW
[] = {'P','a','t','h',0};
223 static const WCHAR DeviceW
[] = {'D','e','v','i','c','e',0};
224 WCHAR driveW
[] = {'M','a','c','h','i','n','e','\\','S','o','f','t','w','a','r','e','\\',
225 'W','i','n','e','\\','W','i','n','e','\\',
226 'C','o','n','f','i','g','\\','D','r','i','v','e',' ','A',0};
227 OBJECT_ATTRIBUTES attr
;
228 UNICODE_STRING nameW
;
229 char tmp
[1024*sizeof(WCHAR
) + sizeof(KEY_VALUE_PARTIAL_INFORMATION
)];
236 attr
.Length
= sizeof(attr
);
237 attr
.RootDirectory
= 0;
238 attr
.ObjectName
= &nameW
;
240 attr
.SecurityDescriptor
= NULL
;
241 attr
.SecurityQualityOfService
= NULL
;
243 /* create symlinks for the drive roots */
245 if (!devices_only
) for (i
= 0; i
< 26; i
++)
247 RtlInitUnicodeString( &nameW
, driveW
);
248 nameW
.Buffer
[(nameW
.Length
/ sizeof(WCHAR
)) - 1] = 'A' + i
;
249 if (NtOpenKey( &hkey
, KEY_ALL_ACCESS
, &attr
) != STATUS_SUCCESS
) continue;
251 RtlInitUnicodeString( &nameW
, PathW
);
252 if (!NtQueryValueKey( hkey
, &nameW
, KeyValuePartialInformation
, tmp
, sizeof(tmp
), &dummy
))
255 WCHAR
*data
= (WCHAR
*)((KEY_VALUE_PARTIAL_INFORMATION
*)tmp
)->Data
;
256 ExpandEnvironmentStringsW( data
, path
, sizeof(path
)/sizeof(WCHAR
) );
258 p
= path
+ strlenW(path
) - 1;
259 while ((p
> path
) && (*p
== '/')) *p
-- = '\0';
267 /* relative paths are relative to config dir */
268 memmove( path
+ 3, path
, (strlenW(path
) + 1) * sizeof(WCHAR
) );
273 if (DefineDosDeviceW( DDD_RAW_TARGET_PATH
, name
, path
))
275 WideCharToMultiByte(CP_UNIXCP
, 0, path
, -1, dest
, sizeof(dest
), NULL
, NULL
);
276 MESSAGE( "Created symlink %s/dosdevices/%c: -> %s\n",
277 wine_get_config_dir(), 'a' + i
, dest
);
284 /* create symlinks for the drive devices */
286 for (i
= 0; i
< 26; i
++)
288 RtlInitUnicodeString( &nameW
, driveW
);
289 nameW
.Buffer
[(nameW
.Length
/ sizeof(WCHAR
)) - 1] = 'A' + i
;
290 if (NtOpenKey( &hkey
, KEY_ALL_ACCESS
, &attr
) != STATUS_SUCCESS
) continue;
292 RtlInitUnicodeString( &nameW
, DeviceW
);
293 if (!NtQueryValueKey( hkey
, &nameW
, KeyValuePartialInformation
, tmp
, sizeof(tmp
), &dummy
))
296 WCHAR devname
[] = {'A',':',':',0 };
297 WCHAR
*data
= (WCHAR
*)((KEY_VALUE_PARTIAL_INFORMATION
*)tmp
)->Data
;
298 WideCharToMultiByte(CP_UNIXCP
, 0, data
, -1, dest
, sizeof(dest
), NULL
, NULL
);
299 path
= get_dos_device_path( devname
);
300 p
= path
+ strlen(path
);
302 if (!symlink( dest
, path
))
304 MESSAGE( "Created symlink %s/dosdevices/%c:: -> %s\n",
305 wine_get_config_dir(), 'a' + i
, dest
);
308 HeapFree( GetProcessHeap(), 0, path
);
317 /***********************************************************************
318 * VOLUME_CreateDevices
320 * Create the device files for the new device naming scheme.
321 * Should go away after a transition period.
323 void VOLUME_CreateDevices(void)
325 const char *config_dir
= wine_get_config_dir();
329 if (!(buffer
= HeapAlloc( GetProcessHeap(), 0,
330 strlen(config_dir
) + sizeof("/dosdevices/a::") )))
333 strcpy( buffer
, config_dir
);
334 strcat( buffer
, "/dosdevices" );
336 if (!mkdir( buffer
, 0777 )) /* we created it, so now create the devices */
340 OBJECT_ATTRIBUTES attr
;
341 UNICODE_STRING nameW
;
344 WCHAR com
[5] = {'C','O','M','1',0};
345 WCHAR lpt
[5] = {'L','P','T','1',0};
347 static const WCHAR serialportsW
[] = {'M','a','c','h','i','n','e','\\',
348 'S','o','f','t','w','a','r','e','\\',
349 'W','i','n','e','\\','W','i','n','e','\\',
350 'C','o','n','f','i','g','\\',
351 'S','e','r','i','a','l','P','o','r','t','s',0};
352 static const WCHAR parallelportsW
[] = {'M','a','c','h','i','n','e','\\',
353 'S','o','f','t','w','a','r','e','\\',
354 'W','i','n','e','\\','W','i','n','e','\\',
355 'C','o','n','f','i','g','\\',
356 'P','a','r','a','l','l','e','l','P','o','r','t','s',0};
358 attr
.Length
= sizeof(attr
);
359 attr
.RootDirectory
= 0;
360 attr
.ObjectName
= &nameW
;
362 attr
.SecurityDescriptor
= NULL
;
363 attr
.SecurityQualityOfService
= NULL
;
364 RtlInitUnicodeString( &nameW
, serialportsW
);
366 if (!NtOpenKey( &hkey
, KEY_ALL_ACCESS
, &attr
))
368 RtlInitUnicodeString( &nameW
, com
);
369 for (i
= 1; i
<= 9; i
++)
372 if (!NtQueryValueKey( hkey
, &nameW
, KeyValuePartialInformation
,
373 tmp
, sizeof(tmp
), &dummy
))
375 devnameW
= (WCHAR
*)((KEY_VALUE_PARTIAL_INFORMATION
*)tmp
)->Data
;
376 if ((p
= strchrW( devnameW
, ',' ))) *p
= 0;
377 if (DefineDosDeviceW( DDD_RAW_TARGET_PATH
, com
, devnameW
))
380 WideCharToMultiByte(CP_UNIXCP
, 0, devnameW
, -1,
381 devname
, sizeof(devname
), NULL
, NULL
);
382 MESSAGE( "Created symlink %s/dosdevices/com%d -> %s\n", config_dir
, i
, devname
);
390 RtlInitUnicodeString( &nameW
, parallelportsW
);
391 if (!NtOpenKey( &hkey
, KEY_ALL_ACCESS
, &attr
))
393 RtlInitUnicodeString( &nameW
, lpt
);
394 for (i
= 1; i
<= 9; i
++)
397 if (!NtQueryValueKey( hkey
, &nameW
, KeyValuePartialInformation
,
398 tmp
, sizeof(tmp
), &dummy
))
400 devnameW
= (WCHAR
*)((KEY_VALUE_PARTIAL_INFORMATION
*)tmp
)->Data
;
401 if ((p
= strchrW( devnameW
, ',' ))) *p
= 0;
402 if (DefineDosDeviceW( DDD_RAW_TARGET_PATH
, lpt
, devnameW
))
405 WideCharToMultiByte(CP_UNIXCP
, 0, devnameW
, -1,
406 devname
, sizeof(devname
), NULL
, NULL
);
407 MESSAGE( "Created symlink %s/dosdevices/lpt%d -> %s\n", config_dir
, i
, devname
);
414 count
+= create_drives( FALSE
);
421 /* it is possible that the serial/parallel devices have been created but */
422 /* not the drives; check for at least one drive symlink to catch that case */
423 strcat( buffer
, "/a:" );
424 for (i
= 0; i
< 26; i
++)
426 buffer
[strlen(buffer
)-2] = 'a' + i
;
427 if (!lstat( buffer
, &st
)) break;
429 if (i
== 26) count
+= create_drives( FALSE
);
432 strcat( buffer
, ":" );
433 for (i
= 0; i
< 26; i
++)
435 buffer
[strlen(buffer
)-3] = 'a' + i
;
436 if (!lstat( buffer
, &st
)) break;
438 if (i
== 26) count
+= create_drives( TRUE
);
443 MESSAGE( "\nYou can now remove the [SerialPorts], [ParallelPorts], and [Drive] sections\n"
444 "in your configuration file, they are replaced by the above symlinks.\n\n" );
446 HeapFree( GetProcessHeap(), 0, buffer
);
450 /******************************************************************
451 * VOLUME_FindCdRomDataBestVoldesc
453 static DWORD
VOLUME_FindCdRomDataBestVoldesc( HANDLE handle
)
455 BYTE cur_vd_type
, max_vd_type
= 0;
457 DWORD size
, offs
, best_offs
= 0, extra_offs
= 0;
459 for (offs
= 0x8000; offs
<= 0x9800; offs
+= 0x800)
461 /* if 'CDROM' occurs at position 8, this is a pre-iso9660 cd, and
462 * the volume label is displaced forward by 8
464 if (SetFilePointer( handle
, offs
, NULL
, FILE_BEGIN
) != offs
) break;
465 if (!ReadFile( handle
, buffer
, sizeof(buffer
), &size
, NULL
)) break;
466 if (size
!= sizeof(buffer
)) break;
467 /* check for non-ISO9660 signature */
468 if (!memcmp( buffer
+ 11, "ROM", 3 )) extra_offs
= 8;
469 cur_vd_type
= buffer
[extra_offs
];
470 if (cur_vd_type
== 0xff) /* voldesc set terminator */
472 if (cur_vd_type
> max_vd_type
)
474 max_vd_type
= cur_vd_type
;
475 best_offs
= offs
+ extra_offs
;
482 /***********************************************************************
483 * VOLUME_ReadFATSuperblock
485 static enum fs_type
VOLUME_ReadFATSuperblock( HANDLE handle
, BYTE
*buff
)
489 /* try a fixed disk, with a FAT partition */
490 if (SetFilePointer( handle
, 0, NULL
, FILE_BEGIN
) != 0 ||
491 !ReadFile( handle
, buff
, SUPERBLOCK_SIZE
, &size
, NULL
) ||
492 size
!= SUPERBLOCK_SIZE
)
495 if (buff
[0] == 0xE9 || (buff
[0] == 0xEB && buff
[2] == 0x90))
497 /* guess which type of FAT we have */
498 unsigned int sz
, nsect
, nclust
;
499 sz
= GETWORD(buff
, 0x16);
500 if (!sz
) sz
= GETLONG(buff
, 0x24);
501 nsect
= GETWORD(buff
, 0x13);
502 if (!nsect
) nsect
= GETLONG(buff
, 0x20);
503 nsect
-= GETWORD(buff
, 0x0e) + buff
[0x10] * sz
+
504 (GETWORD(buff
, 0x11) * 32 + (GETWORD(buff
, 0x0b) - 1)) / GETWORD(buff
, 0x0b);
505 nclust
= nsect
/ buff
[0x0d];
509 if (buff
[0x26] == 0x29 && !memcmp(buff
+0x36, "FAT", 3))
511 /* FIXME: do really all FAT have their name beginning with
512 * "FAT" ? (At least FAT12, FAT16 and FAT32 have :)
517 else if (!memcmp(buff
+0x52, "FAT", 3)) return FS_FAT32
;
523 /***********************************************************************
524 * VOLUME_ReadCDSuperblock
526 static enum fs_type
VOLUME_ReadCDSuperblock( HANDLE handle
, BYTE
*buff
)
528 DWORD size
, offs
= VOLUME_FindCdRomDataBestVoldesc( handle
);
530 if (!offs
) return FS_UNKNOWN
;
532 if (SetFilePointer( handle
, offs
, NULL
, FILE_BEGIN
) != offs
||
533 !ReadFile( handle
, buff
, SUPERBLOCK_SIZE
, &size
, NULL
) ||
534 size
!= SUPERBLOCK_SIZE
)
537 /* check for iso9660 present */
538 if (!memcmp(&buff
[1], "CD001", 5)) return FS_ISO9660
;
543 /**************************************************************************
544 * VOLUME_GetSuperblockLabel
546 static void VOLUME_GetSuperblockLabel( enum fs_type type
, const BYTE
*superblock
,
547 WCHAR
*label
, DWORD len
)
549 const BYTE
*label_ptr
= NULL
;
559 label_ptr
= superblock
+ 0x2b;
563 label_ptr
= superblock
+ 0x47;
568 BYTE ver
= superblock
[0x5a];
570 if (superblock
[0x58] == 0x25 && superblock
[0x59] == 0x2f && /* Unicode ID */
571 ((ver
== 0x40) || (ver
== 0x43) || (ver
== 0x45)))
572 { /* yippee, unicode */
575 if (len
> 17) len
= 17;
576 for (i
= 0; i
< len
-1; i
++)
577 label
[i
] = (superblock
[40+2*i
] << 8) | superblock
[41+2*i
];
579 while (i
&& label
[i
-1] == ' ') label
[--i
] = 0;
582 label_ptr
= superblock
+ 40;
587 if (label_len
) RtlMultiByteToUnicodeN( label
, (len
-1) * sizeof(WCHAR
),
588 &label_len
, label_ptr
, label_len
);
589 label_len
/= sizeof(WCHAR
);
590 label
[label_len
] = 0;
591 while (label_len
&& label
[label_len
-1] == ' ') label
[--label_len
] = 0;
595 /**************************************************************************
596 * VOLUME_SetSuperblockLabel
598 static BOOL
VOLUME_SetSuperblockLabel( enum fs_type type
, HANDLE handle
, const WCHAR
*label
)
612 SetLastError( ERROR_ACCESS_DENIED
);
615 RtlUnicodeToMultiByteN( label_data
, sizeof(label_data
), &len
,
616 label
, strlenW(label
) * sizeof(WCHAR
) );
617 if (len
< sizeof(label_data
))
618 memset( label_data
+ len
, ' ', sizeof(label_data
) - len
);
620 return (SetFilePointer( handle
, offset
, NULL
, FILE_BEGIN
) == offset
&&
621 WriteFile( handle
, label_data
, sizeof(label_data
), &len
, NULL
));
625 /**************************************************************************
626 * VOLUME_GetSuperblockSerial
628 static DWORD
VOLUME_GetSuperblockSerial( enum fs_type type
, const BYTE
*superblock
)
636 return GETLONG( superblock
, 0x27 );
638 return GETLONG( superblock
, 0x33 );
644 sum
[0] = sum
[1] = sum
[2] = sum
[3] = 0;
645 for (i
= 0; i
< 2048; i
+= 4)
647 /* DON'T optimize this into DWORD !! (breaks overflow) */
648 sum
[0] += superblock
[i
+0];
649 sum
[1] += superblock
[i
+1];
650 sum
[2] += superblock
[i
+2];
651 sum
[3] += superblock
[i
+3];
654 * OK, another braindead one... argh. Just believe it.
655 * Me$$ysoft chose to reverse the serial number in NT4/W2K.
656 * It's true and nobody will ever be able to change it.
658 if (GetVersion() & 0x80000000)
659 return (sum
[3] << 24) | (sum
[2] << 16) | (sum
[1] << 8) | sum
[0];
661 return (sum
[0] << 24) | (sum
[1] << 16) | (sum
[2] << 8) | sum
[3];
668 /**************************************************************************
669 * VOLUME_GetAudioCDSerial
671 static DWORD
VOLUME_GetAudioCDSerial( const CDROM_TOC
*toc
)
676 for (i
= 0; i
<= toc
->LastTrack
- toc
->FirstTrack
; i
++)
677 serial
+= ((toc
->TrackData
[i
].Address
[1] << 16) |
678 (toc
->TrackData
[i
].Address
[2] << 8) |
679 toc
->TrackData
[i
].Address
[3]);
682 * dwStart, dwEnd collect the beginning and end of the disc respectively, in
684 * There it is collected for correcting the serial when there are less than
687 if (toc
->LastTrack
- toc
->FirstTrack
+ 1 < 3)
689 DWORD dwStart
= FRAME_OF_TOC(toc
, toc
->FirstTrack
);
690 DWORD dwEnd
= FRAME_OF_TOC(toc
, toc
->LastTrack
+ 1);
691 serial
+= dwEnd
- dwStart
;
697 /***********************************************************************
698 * GetVolumeInformationW (KERNEL32.@)
700 BOOL WINAPI
GetVolumeInformationW( LPCWSTR root
, LPWSTR label
, DWORD label_len
,
701 DWORD
*serial
, DWORD
*filename_len
, DWORD
*flags
,
702 LPWSTR fsname
, DWORD fsname_len
)
704 static const WCHAR audiocdW
[] = {'A','u','d','i','o',' ','C','D',0};
705 static const WCHAR fatW
[] = {'F','A','T',0};
706 static const WCHAR cdfsW
[] = {'C','D','F','S',0};
708 WCHAR device
[] = {'\\','\\','.','\\','A',':',0};
710 enum fs_type type
= FS_UNKNOWN
;
714 WCHAR path
[MAX_PATH
];
715 GetCurrentDirectoryW( MAX_PATH
, path
);
720 if (!root
[0] || root
[1] != ':')
722 SetLastError( ERROR_INVALID_NAME
);
728 /* try to open the device */
730 handle
= CreateFileW( device
, GENERIC_READ
, FILE_SHARE_READ
|FILE_SHARE_WRITE
,
731 NULL
, OPEN_EXISTING
, 0, 0 );
732 if (handle
!= INVALID_HANDLE_VALUE
)
734 BYTE superblock
[SUPERBLOCK_SIZE
];
738 /* check for audio CD */
739 /* FIXME: we only check the first track for now */
740 if (DeviceIoControl( handle
, IOCTL_CDROM_READ_TOC
, NULL
, 0, &toc
, sizeof(toc
), &br
, 0 ))
742 if (!(toc
.TrackData
[0].Control
& 0x04)) /* audio track */
744 TRACE( "%s: found audio CD\n", debugstr_w(device
) );
745 if (label
) lstrcpynW( label
, audiocdW
, label_len
);
746 if (serial
) *serial
= VOLUME_GetAudioCDSerial( &toc
);
747 CloseHandle( handle
);
751 type
= VOLUME_ReadCDSuperblock( handle
, superblock
);
755 type
= VOLUME_ReadFATSuperblock( handle
, superblock
);
756 if (type
== FS_UNKNOWN
) type
= VOLUME_ReadCDSuperblock( handle
, superblock
);
758 CloseHandle( handle
);
759 TRACE( "%s: found fs type %d\n", debugstr_w(device
), type
);
760 if (type
== FS_ERROR
) return FALSE
;
762 if (label
&& label_len
) VOLUME_GetSuperblockLabel( type
, superblock
, label
, label_len
);
763 if (serial
) *serial
= VOLUME_GetSuperblockSerial( type
, superblock
);
768 TRACE( "cannot open device %s: err %ld\n", debugstr_w(device
), GetLastError() );
769 if (GetLastError() != ERROR_FILE_NOT_FOUND
) return FALSE
;
772 /* we couldn't open the device, fallback to default strategy */
774 switch(GetDriveTypeW( root
))
777 case DRIVE_NO_ROOT_DIR
:
778 SetLastError( ERROR_NOT_READY
);
780 case DRIVE_REMOVABLE
:
791 if (label
&& label_len
)
793 WCHAR labelW
[] = {'A',':','\\','.','w','i','n','d','o','w','s','-','l','a','b','e','l',0};
795 labelW
[0] = device
[4];
796 handle
= CreateFileW( labelW
, GENERIC_READ
, FILE_SHARE_READ
|FILE_SHARE_WRITE
, NULL
,
797 OPEN_EXISTING
, 0, 0 );
798 if (handle
!= INVALID_HANDLE_VALUE
)
800 char buffer
[256], *p
;
803 if (!ReadFile( handle
, buffer
, sizeof(buffer
)-1, &size
, NULL
)) size
= 0;
804 CloseHandle( handle
);
806 while (p
> buffer
&& (p
[-1] == ' ' || p
[-1] == '\r' || p
[-1] == '\n')) p
--;
808 if (!MultiByteToWideChar( CP_UNIXCP
, 0, buffer
, -1, label
, label_len
))
809 label
[label_len
-1] = 0;
815 WCHAR serialW
[] = {'A',':','\\','.','w','i','n','d','o','w','s','-','s','e','r','i','a','l',0};
817 serialW
[0] = device
[4];
818 handle
= CreateFileW( serialW
, GENERIC_READ
, FILE_SHARE_READ
|FILE_SHARE_WRITE
, NULL
,
819 OPEN_EXISTING
, 0, 0 );
820 if (handle
!= INVALID_HANDLE_VALUE
)
825 if (!ReadFile( handle
, buffer
, sizeof(buffer
)-1, &size
, NULL
)) size
= 0;
826 CloseHandle( handle
);
828 *serial
= strtoul( buffer
, NULL
, 16 );
833 fill_fs_info
: /* now fill in the information that depends on the file system type */
838 if (fsname
) lstrcpynW( fsname
, cdfsW
, fsname_len
);
839 if (filename_len
) *filename_len
= 221;
840 if (flags
) *flags
= FILE_READ_ONLY_VOLUME
;
844 default: /* default to FAT file system (FIXME) */
845 if (fsname
) lstrcpynW( fsname
, fatW
, fsname_len
);
846 if (filename_len
) *filename_len
= 255;
847 if (flags
) *flags
= FILE_CASE_PRESERVED_NAMES
; /* FIXME */
854 /***********************************************************************
855 * GetVolumeInformationA (KERNEL32.@)
857 BOOL WINAPI
GetVolumeInformationA( LPCSTR root
, LPSTR label
,
858 DWORD label_len
, DWORD
*serial
,
859 DWORD
*filename_len
, DWORD
*flags
,
860 LPSTR fsname
, DWORD fsname_len
)
862 UNICODE_STRING rootW
;
863 LPWSTR labelW
, fsnameW
;
866 if (root
) RtlCreateUnicodeStringFromAsciiz(&rootW
, root
);
867 else rootW
.Buffer
= NULL
;
868 labelW
= label
? HeapAlloc(GetProcessHeap(), 0, label_len
* sizeof(WCHAR
)) : NULL
;
869 fsnameW
= fsname
? HeapAlloc(GetProcessHeap(), 0, fsname_len
* sizeof(WCHAR
)) : NULL
;
871 if ((ret
= GetVolumeInformationW(rootW
.Buffer
, labelW
, label_len
, serial
,
872 filename_len
, flags
, fsnameW
, fsname_len
)))
874 if (label
) WideCharToMultiByte(CP_ACP
, 0, labelW
, -1, label
, label_len
, NULL
, NULL
);
875 if (fsname
) WideCharToMultiByte(CP_ACP
, 0, fsnameW
, -1, fsname
, fsname_len
, NULL
, NULL
);
878 RtlFreeUnicodeString(&rootW
);
879 if (labelW
) HeapFree( GetProcessHeap(), 0, labelW
);
880 if (fsnameW
) HeapFree( GetProcessHeap(), 0, fsnameW
);
886 /***********************************************************************
887 * SetVolumeLabelW (KERNEL32.@)
889 BOOL WINAPI
SetVolumeLabelW( LPCWSTR root
, LPCWSTR label
)
891 WCHAR device
[] = {'\\','\\','.','\\','A',':',0};
893 enum fs_type type
= FS_UNKNOWN
;
897 WCHAR path
[MAX_PATH
];
898 GetCurrentDirectoryW( MAX_PATH
, path
);
903 if (!root
[0] || root
[1] != ':')
905 SetLastError( ERROR_INVALID_NAME
);
911 /* try to open the device */
913 handle
= CreateFileW( device
, GENERIC_READ
|GENERIC_WRITE
, FILE_SHARE_READ
|FILE_SHARE_WRITE
,
914 NULL
, OPEN_EXISTING
, 0, 0 );
915 if (handle
== INVALID_HANDLE_VALUE
)
918 handle
= CreateFileW( device
, GENERIC_READ
, FILE_SHARE_READ
|FILE_SHARE_WRITE
,
919 NULL
, OPEN_EXISTING
, 0, 0 );
920 if (handle
!= INVALID_HANDLE_VALUE
)
922 /* device can be read but not written, return error */
923 CloseHandle( handle
);
924 SetLastError( ERROR_ACCESS_DENIED
);
929 if (handle
!= INVALID_HANDLE_VALUE
)
931 BYTE superblock
[SUPERBLOCK_SIZE
];
934 type
= VOLUME_ReadFATSuperblock( handle
, superblock
);
935 ret
= VOLUME_SetSuperblockLabel( type
, handle
, label
);
936 CloseHandle( handle
);
941 TRACE( "cannot open device %s: err %ld\n", debugstr_w(device
), GetLastError() );
942 if (GetLastError() != ERROR_ACCESS_DENIED
) return FALSE
;
945 /* we couldn't open the device, fallback to default strategy */
947 switch(GetDriveTypeW( root
))
950 case DRIVE_NO_ROOT_DIR
:
951 SetLastError( ERROR_NOT_READY
);
953 case DRIVE_REMOVABLE
:
956 WCHAR labelW
[] = {'A',':','\\','.','w','i','n','d','o','w','s','-','l','a','b','e','l',0};
958 labelW
[0] = device
[4];
959 handle
= CreateFileW( labelW
, GENERIC_WRITE
, FILE_SHARE_READ
|FILE_SHARE_WRITE
, NULL
,
960 CREATE_ALWAYS
, 0, 0 );
961 if (handle
!= INVALID_HANDLE_VALUE
)
966 if (!WideCharToMultiByte( CP_UNIXCP
, 0, label
, -1, buffer
, sizeof(buffer
), NULL
, NULL
))
967 buffer
[sizeof(buffer
)-1] = 0;
968 WriteFile( handle
, buffer
, strlen(buffer
), &size
, NULL
);
969 CloseHandle( handle
);
977 SetLastError( ERROR_ACCESS_DENIED
);
983 /***********************************************************************
984 * SetVolumeLabelA (KERNEL32.@)
986 BOOL WINAPI
SetVolumeLabelA(LPCSTR root
, LPCSTR volname
)
988 UNICODE_STRING rootW
, volnameW
;
991 if (root
) RtlCreateUnicodeStringFromAsciiz(&rootW
, root
);
992 else rootW
.Buffer
= NULL
;
993 if (volname
) RtlCreateUnicodeStringFromAsciiz(&volnameW
, volname
);
994 else volnameW
.Buffer
= NULL
;
996 ret
= SetVolumeLabelW( rootW
.Buffer
, volnameW
.Buffer
);
998 RtlFreeUnicodeString(&rootW
);
999 RtlFreeUnicodeString(&volnameW
);
1004 /***********************************************************************
1005 * GetVolumeNameForVolumeMountPointW (KERNEL32.@)
1007 BOOL WINAPI
GetVolumeNameForVolumeMountPointW(LPCWSTR str
, LPWSTR dst
, DWORD size
)
1009 FIXME("(%s, %p, %lx): stub\n", debugstr_w(str
), dst
, size
);
1014 /***********************************************************************
1015 * DefineDosDeviceW (KERNEL32.@)
1017 BOOL WINAPI
DefineDosDeviceW( DWORD flags
, LPCWSTR devname
, LPCWSTR targetpath
)
1021 char *path
= NULL
, *target
, *p
;
1023 if (!(flags
& DDD_REMOVE_DEFINITION
))
1025 if (!(flags
& DDD_RAW_TARGET_PATH
))
1027 FIXME( "(0x%08lx,%s,%s) DDD_RAW_TARGET_PATH flag not set, not supported yet\n",
1028 flags
, debugstr_w(devname
), debugstr_w(targetpath
) );
1029 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1033 len
= WideCharToMultiByte( CP_UNIXCP
, 0, targetpath
, -1, NULL
, 0, NULL
, NULL
);
1034 if ((target
= HeapAlloc( GetProcessHeap(), 0, len
)))
1036 WideCharToMultiByte( CP_UNIXCP
, 0, targetpath
, -1, target
, len
, NULL
, NULL
);
1037 for (p
= target
; *p
; p
++) if (*p
== '\\') *p
= '/';
1041 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1047 /* first check for a DOS device */
1049 if ((dosdev
= RtlIsDosDeviceName_U( devname
)))
1053 memcpy( name
, devname
+ HIWORD(dosdev
)/sizeof(WCHAR
), LOWORD(dosdev
) );
1054 name
[LOWORD(dosdev
)/sizeof(WCHAR
)] = 0;
1055 path
= get_dos_device_path( name
);
1057 else if (isalphaW(devname
[0]) && devname
[1] == ':' && !devname
[2]) /* drive mapping */
1059 path
= get_dos_device_path( devname
);
1061 else SetLastError( ERROR_FILE_NOT_FOUND
);
1067 TRACE( "creating symlink %s -> %s\n", path
, target
);
1069 if (!symlink( target
, path
)) ret
= TRUE
;
1070 else FILE_SetDosError();
1074 TRACE( "removing symlink %s\n", path
);
1075 if (!unlink( path
)) ret
= TRUE
;
1076 else FILE_SetDosError();
1078 HeapFree( GetProcessHeap(), 0, path
);
1080 if (target
) HeapFree( GetProcessHeap(), 0, target
);
1085 /***********************************************************************
1086 * DefineDosDeviceA (KERNEL32.@)
1088 BOOL WINAPI
DefineDosDeviceA(DWORD flags
, LPCSTR devname
, LPCSTR targetpath
)
1090 UNICODE_STRING d
, t
;
1093 if (!RtlCreateUnicodeStringFromAsciiz(&d
, devname
))
1095 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1098 if (!RtlCreateUnicodeStringFromAsciiz(&t
, targetpath
))
1100 RtlFreeUnicodeString(&d
);
1101 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1104 ret
= DefineDosDeviceW(flags
, d
.Buffer
, t
.Buffer
);
1105 RtlFreeUnicodeString(&d
);
1106 RtlFreeUnicodeString(&t
);
1111 /***********************************************************************
1112 * QueryDosDeviceW (KERNEL32.@)
1114 * returns array of strings terminated by \0, terminated by \0
1116 DWORD WINAPI
QueryDosDeviceW( LPCWSTR devname
, LPWSTR target
, DWORD bufsize
)
1118 static const WCHAR auxW
[] = {'A','U','X',0};
1119 static const WCHAR nulW
[] = {'N','U','L',0};
1120 static const WCHAR prnW
[] = {'P','R','N',0};
1121 static const WCHAR comW
[] = {'C','O','M',0};
1122 static const WCHAR lptW
[] = {'L','P','T',0};
1123 static const WCHAR rootW
[] = {'A',':','\\',0};
1124 static const WCHAR com0W
[] = {'\\','?','?','\\','C','O','M','0',0};
1125 static const WCHAR com1W
[] = {'\\','D','o','s','D','e','v','i','c','e','s','\\','C','O','M','1',0,0};
1126 static const WCHAR lpt1W
[] = {'\\','D','o','s','D','e','v','i','c','e','s','\\','L','P','T','1',0,0};
1128 UNICODE_STRING nt_name
;
1129 ANSI_STRING unix_name
;
1130 WCHAR nt_buffer
[10];
1135 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
1143 DWORD dosdev
, ret
= 0;
1145 if ((dosdev
= RtlIsDosDeviceName_U( devname
)))
1147 memcpy( name
, devname
+ HIWORD(dosdev
)/sizeof(WCHAR
), LOWORD(dosdev
) );
1148 name
[LOWORD(dosdev
)/sizeof(WCHAR
)] = 0;
1150 else if (devname
[0] && devname
[1] == ':' && !devname
[2])
1152 memcpy( name
, devname
, 3 * sizeof(WCHAR
) );
1156 SetLastError( ERROR_BAD_PATHNAME
);
1160 if (!(path
= get_dos_device_path( name
))) return 0;
1161 link
= read_symlink( path
);
1162 HeapFree( GetProcessHeap(), 0, path
);
1166 ret
= MultiByteToWideChar( CP_UNIXCP
, 0, link
, -1, target
, bufsize
);
1167 HeapFree( GetProcessHeap(), 0, link
);
1169 else if (dosdev
) /* look for device defaults */
1171 if (!strcmpiW( name
, auxW
))
1173 if (bufsize
>= sizeof(com1W
)/sizeof(WCHAR
))
1175 memcpy( target
, com1W
, sizeof(com1W
) );
1176 ret
= sizeof(com1W
)/sizeof(WCHAR
);
1178 else SetLastError( ERROR_INSUFFICIENT_BUFFER
);
1181 if (!strcmpiW( name
, prnW
))
1183 if (bufsize
>= sizeof(lpt1W
)/sizeof(WCHAR
))
1185 memcpy( target
, lpt1W
, sizeof(lpt1W
) );
1186 ret
= sizeof(lpt1W
)/sizeof(WCHAR
);
1188 else SetLastError( ERROR_INSUFFICIENT_BUFFER
);
1192 nt_buffer
[0] = '\\';
1195 nt_buffer
[3] = '\\';
1196 strcpyW( nt_buffer
+ 4, name
);
1197 RtlInitUnicodeString( &nt_name
, nt_buffer
);
1198 status
= wine_nt_to_unix_file_name( &nt_name
, &unix_name
, FILE_OPEN
, TRUE
);
1199 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
1202 ret
= MultiByteToWideChar( CP_UNIXCP
, 0, unix_name
.Buffer
, -1, target
, bufsize
);
1203 RtlFreeAnsiString( &unix_name
);
1209 if (ret
< bufsize
) target
[ret
++] = 0; /* add an extra null */
1210 for (p
= target
; *p
; p
++) if (*p
== '/') *p
= '\\';
1215 else /* return a list of all devices */
1220 if (bufsize
<= (sizeof(auxW
)+sizeof(nulW
)+sizeof(prnW
))/sizeof(WCHAR
))
1222 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
1226 memcpy( p
, auxW
, sizeof(auxW
) );
1227 p
+= sizeof(auxW
) / sizeof(WCHAR
);
1228 memcpy( p
, nulW
, sizeof(nulW
) );
1229 p
+= sizeof(nulW
) / sizeof(WCHAR
);
1230 memcpy( p
, prnW
, sizeof(prnW
) );
1231 p
+= sizeof(prnW
) / sizeof(WCHAR
);
1233 strcpyW( nt_buffer
, com0W
);
1234 RtlInitUnicodeString( &nt_name
, nt_buffer
);
1236 for (i
= 1; i
<= 9; i
++)
1238 nt_buffer
[7] = '0' + i
;
1239 if (!wine_nt_to_unix_file_name( &nt_name
, &unix_name
, FILE_OPEN
, TRUE
))
1241 RtlFreeAnsiString( &unix_name
);
1242 if (p
+ 5 >= target
+ bufsize
)
1244 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
1253 strcpyW( nt_buffer
+ 4, lptW
);
1254 for (i
= 1; i
<= 9; i
++)
1256 nt_buffer
[7] = '0' + i
;
1257 if (!wine_nt_to_unix_file_name( &nt_name
, &unix_name
, FILE_OPEN
, TRUE
))
1259 RtlFreeAnsiString( &unix_name
);
1260 if (p
+ 5 >= target
+ bufsize
)
1262 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
1272 strcpyW( nt_buffer
+ 4, rootW
);
1273 RtlInitUnicodeString( &nt_name
, nt_buffer
);
1275 for (i
= 0; i
< 26; i
++)
1277 nt_buffer
[4] = 'a' + i
;
1278 if (!wine_nt_to_unix_file_name( &nt_name
, &unix_name
, FILE_OPEN
, TRUE
))
1280 RtlFreeAnsiString( &unix_name
);
1281 if (p
+ 3 >= target
+ bufsize
)
1283 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
1291 *p
++ = 0; /* terminating null */
1297 /***********************************************************************
1298 * QueryDosDeviceA (KERNEL32.@)
1300 * returns array of strings terminated by \0, terminated by \0
1302 DWORD WINAPI
QueryDosDeviceA( LPCSTR devname
, LPSTR target
, DWORD bufsize
)
1304 DWORD ret
= 0, retW
;
1305 UNICODE_STRING devnameW
;
1306 LPWSTR targetW
= HeapAlloc( GetProcessHeap(),0, bufsize
* sizeof(WCHAR
) );
1310 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1314 if (devname
) RtlCreateUnicodeStringFromAsciiz(&devnameW
, devname
);
1315 else devnameW
.Buffer
= NULL
;
1317 retW
= QueryDosDeviceW(devnameW
.Buffer
, targetW
, bufsize
);
1319 ret
= WideCharToMultiByte(CP_ACP
, 0, targetW
, retW
, target
, bufsize
, NULL
, NULL
);
1321 RtlFreeUnicodeString(&devnameW
);
1322 HeapFree(GetProcessHeap(), 0, targetW
);
1327 /***********************************************************************
1328 * GetLogicalDrives (KERNEL32.@)
1330 DWORD WINAPI
GetLogicalDrives(void)
1332 const char *config_dir
= wine_get_config_dir();
1338 if (!(buffer
= HeapAlloc( GetProcessHeap(), 0, strlen(config_dir
) + sizeof("/dosdevices/a:") )))
1340 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1343 strcpy( buffer
, config_dir
);
1344 strcat( buffer
, "/dosdevices/a:" );
1345 dev
= buffer
+ strlen(buffer
) - 2;
1347 for (i
= 0; i
< 26; i
++)
1350 if (!stat( buffer
, &st
)) ret
|= (1 << i
);
1352 HeapFree( GetProcessHeap(), 0, buffer
);
1357 /***********************************************************************
1358 * GetLogicalDriveStringsA (KERNEL32.@)
1360 UINT WINAPI
GetLogicalDriveStringsA( UINT len
, LPSTR buffer
)
1362 DWORD drives
= GetLogicalDrives();
1365 for (drive
= count
= 0; drive
< 26; drive
++) if (drives
& (1 << drive
)) count
++;
1366 if ((count
* 4) + 1 > len
) return count
* 4 + 1;
1368 for (drive
= 0; drive
< 26; drive
++)
1370 if (drives
& (1 << drive
))
1372 *buffer
++ = 'a' + drive
;
1383 /***********************************************************************
1384 * GetLogicalDriveStringsW (KERNEL32.@)
1386 UINT WINAPI
GetLogicalDriveStringsW( UINT len
, LPWSTR buffer
)
1388 DWORD drives
= GetLogicalDrives();
1391 for (drive
= count
= 0; drive
< 26; drive
++) if (drives
& (1 << drive
)) count
++;
1392 if ((count
* 4) + 1 > len
) return count
* 4 + 1;
1394 for (drive
= 0; drive
< 26; drive
++)
1396 if (drives
& (1 << drive
))
1398 *buffer
++ = 'a' + drive
;
1409 /***********************************************************************
1410 * GetDriveTypeW (KERNEL32.@)
1412 * Returns the type of the disk drive specified. If root is NULL the
1413 * root of the current directory is used.
1417 * Type of drive (from Win32 SDK):
1419 * DRIVE_UNKNOWN unable to find out anything about the drive
1420 * DRIVE_NO_ROOT_DIR nonexistent root dir
1421 * DRIVE_REMOVABLE the disk can be removed from the machine
1422 * DRIVE_FIXED the disk can not be removed from the machine
1423 * DRIVE_REMOTE network disk
1424 * DRIVE_CDROM CDROM drive
1425 * DRIVE_RAMDISK virtual disk in RAM
1427 UINT WINAPI
GetDriveTypeW(LPCWSTR root
) /* [in] String describing drive */
1429 FILE_FS_DEVICE_INFORMATION info
;
1435 if (!open_device_root( root
, &handle
)) return DRIVE_NO_ROOT_DIR
;
1437 status
= NtQueryVolumeInformationFile( handle
, &io
, &info
, sizeof(info
), FileFsDeviceInformation
);
1439 if (status
!= STATUS_SUCCESS
)
1441 SetLastError( RtlNtStatusToDosError(status
) );
1442 ret
= DRIVE_UNKNOWN
;
1444 else if ((ret
= get_registry_drive_type( root
)) == DRIVE_UNKNOWN
)
1446 switch (info
.DeviceType
)
1448 case FILE_DEVICE_CD_ROM_FILE_SYSTEM
: ret
= DRIVE_CDROM
; break;
1449 case FILE_DEVICE_VIRTUAL_DISK
: ret
= DRIVE_RAMDISK
; break;
1450 case FILE_DEVICE_NETWORK_FILE_SYSTEM
: ret
= DRIVE_REMOTE
; break;
1451 case FILE_DEVICE_DISK_FILE_SYSTEM
:
1452 if (info
.Characteristics
& FILE_REMOTE_DEVICE
) ret
= DRIVE_REMOTE
;
1453 else if (info
.Characteristics
& FILE_REMOVABLE_MEDIA
) ret
= DRIVE_REMOVABLE
;
1454 else ret
= DRIVE_FIXED
;
1457 ret
= DRIVE_UNKNOWN
;
1461 TRACE( "%s -> %d\n", debugstr_w(root
), ret
);
1466 /***********************************************************************
1467 * GetDriveTypeA (KERNEL32.@)
1469 UINT WINAPI
GetDriveTypeA( LPCSTR root
)
1471 UNICODE_STRING rootW
;
1476 if( !RtlCreateUnicodeStringFromAsciiz(&rootW
, root
))
1478 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1479 return DRIVE_NO_ROOT_DIR
;
1482 else rootW
.Buffer
= NULL
;
1484 ret
= GetDriveTypeW(rootW
.Buffer
);
1486 RtlFreeUnicodeString(&rootW
);
1491 /***********************************************************************
1492 * GetDiskFreeSpaceExW (KERNEL32.@)
1494 * This function is used to acquire the size of the available and
1495 * total space on a logical volume.
1499 * Zero on failure, nonzero upon success. Use GetLastError to obtain
1500 * detailed error information.
1503 BOOL WINAPI
GetDiskFreeSpaceExW( LPCWSTR root
, PULARGE_INTEGER avail
,
1504 PULARGE_INTEGER total
, PULARGE_INTEGER totalfree
)
1506 FILE_FS_SIZE_INFORMATION info
;
1512 TRACE( "%s,%p,%p,%p\n", debugstr_w(root
), avail
, total
, totalfree
);
1514 if (!open_device_root( root
, &handle
)) return FALSE
;
1516 status
= NtQueryVolumeInformationFile( handle
, &io
, &info
, sizeof(info
), FileFsSizeInformation
);
1518 if (status
!= STATUS_SUCCESS
)
1520 SetLastError( RtlNtStatusToDosError(status
) );
1524 units
= info
.SectorsPerAllocationUnit
* info
.BytesPerSector
;
1525 if (total
) total
->QuadPart
= info
.TotalAllocationUnits
.QuadPart
* units
;
1526 if (totalfree
) totalfree
->QuadPart
= info
.AvailableAllocationUnits
.QuadPart
* units
;
1527 /* FIXME: this one should take quotas into account */
1528 if (avail
) avail
->QuadPart
= info
.AvailableAllocationUnits
.QuadPart
* units
;
1533 /***********************************************************************
1534 * GetDiskFreeSpaceExA (KERNEL32.@)
1536 BOOL WINAPI
GetDiskFreeSpaceExA( LPCSTR root
, PULARGE_INTEGER avail
,
1537 PULARGE_INTEGER total
, PULARGE_INTEGER totalfree
)
1539 UNICODE_STRING rootW
;
1542 if (root
) RtlCreateUnicodeStringFromAsciiz(&rootW
, root
);
1543 else rootW
.Buffer
= NULL
;
1545 ret
= GetDiskFreeSpaceExW( rootW
.Buffer
, avail
, total
, totalfree
);
1547 RtlFreeUnicodeString(&rootW
);
1552 /***********************************************************************
1553 * GetDiskFreeSpaceW (KERNEL32.@)
1555 BOOL WINAPI
GetDiskFreeSpaceW( LPCWSTR root
, LPDWORD cluster_sectors
,
1556 LPDWORD sector_bytes
, LPDWORD free_clusters
,
1557 LPDWORD total_clusters
)
1559 FILE_FS_SIZE_INFORMATION info
;
1565 TRACE( "%s,%p,%p,%p,%p\n", debugstr_w(root
),
1566 cluster_sectors
, sector_bytes
, free_clusters
, total_clusters
);
1568 if (!open_device_root( root
, &handle
)) return FALSE
;
1570 status
= NtQueryVolumeInformationFile( handle
, &io
, &info
, sizeof(info
), FileFsSizeInformation
);
1572 if (status
!= STATUS_SUCCESS
)
1574 SetLastError( RtlNtStatusToDosError(status
) );
1578 units
= info
.SectorsPerAllocationUnit
* info
.BytesPerSector
;
1580 /* cap the size and available at 2GB as per specs */
1581 if (info
.AvailableAllocationUnits
.QuadPart
* units
> 0x7fffffff)
1582 info
.AvailableAllocationUnits
.QuadPart
= 0x7fffffff / units
;
1583 if (info
.TotalAllocationUnits
.QuadPart
* units
> 0x7fffffff)
1584 info
.TotalAllocationUnits
.QuadPart
= 0x7fffffff / units
;
1586 if (cluster_sectors
) *cluster_sectors
= info
.SectorsPerAllocationUnit
;
1587 if (sector_bytes
) *sector_bytes
= info
.BytesPerSector
;
1588 if (free_clusters
) *free_clusters
= info
.AvailableAllocationUnits
.u
.LowPart
;
1589 if (total_clusters
) *total_clusters
= info
.TotalAllocationUnits
.u
.LowPart
;
1594 /***********************************************************************
1595 * GetDiskFreeSpaceA (KERNEL32.@)
1597 BOOL WINAPI
GetDiskFreeSpaceA( LPCSTR root
, LPDWORD cluster_sectors
,
1598 LPDWORD sector_bytes
, LPDWORD free_clusters
,
1599 LPDWORD total_clusters
)
1601 UNICODE_STRING rootW
;
1606 if(!RtlCreateUnicodeStringFromAsciiz(&rootW
, root
))
1608 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1612 else rootW
.Buffer
= NULL
;
1614 ret
= GetDiskFreeSpaceW(rootW
.Buffer
, cluster_sectors
, sector_bytes
,
1615 free_clusters
, total_clusters
);
1616 RtlFreeUnicodeString(&rootW
);