Changes of picasa-wine-2.2.2820-5 except to configure
[wine/hacks.git] / dlls / kernel / volume.c
blobc3bf1e83b7781b644792a5fd0185678cccacd6cc
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 "ntddstor.h"
40 #include "ntddcdrm.h"
41 #include "kernel_private.h"
42 #include "wine/library.h"
43 #include "wine/unicode.h"
44 #include "wine/debug.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(volume);
48 #define SUPERBLOCK_SIZE 2048
50 #define CDFRAMES_PERSEC 75
51 #define CDFRAMES_PERMIN (CDFRAMES_PERSEC * 60)
52 #define FRAME_OF_ADDR(a) ((a)[1] * CDFRAMES_PERMIN + (a)[2] * CDFRAMES_PERSEC + (a)[3])
53 #define FRAME_OF_TOC(toc, idx) FRAME_OF_ADDR((toc)->TrackData[(idx) - (toc)->FirstTrack].Address)
55 #define GETWORD(buf,off) MAKEWORD(buf[(off)],buf[(off+1)])
56 #define GETLONG(buf,off) MAKELONG(GETWORD(buf,off),GETWORD(buf,off+2))
58 enum fs_type
60 FS_ERROR, /* error accessing the device */
61 FS_UNKNOWN, /* unknown file system */
62 FS_FAT1216,
63 FS_FAT32,
64 FS_ISO9660
67 static const WCHAR drive_types[][8] =
69 { 0 }, /* DRIVE_UNKNOWN */
70 { 0 }, /* DRIVE_NO_ROOT_DIR */
71 {'f','l','o','p','p','y',0}, /* DRIVE_REMOVABLE */
72 {'h','d',0}, /* DRIVE_FIXED */
73 {'n','e','t','w','o','r','k',0}, /* DRIVE_REMOTE */
74 {'c','d','r','o','m',0}, /* DRIVE_CDROM */
75 {'r','a','m','d','i','s','k',0} /* DRIVE_RAMDISK */
78 /* read a Unix symlink; returned buffer must be freed by caller */
79 static char *read_symlink( const char *path )
81 char *buffer;
82 int ret, size = 128;
84 for (;;)
86 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, size )))
88 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
89 return 0;
91 ret = readlink( path, buffer, size );
92 if (ret == -1)
94 FILE_SetDosError();
95 HeapFree( GetProcessHeap(), 0, buffer );
96 return 0;
98 if (ret != size)
100 buffer[ret] = 0;
101 return buffer;
103 HeapFree( GetProcessHeap(), 0, buffer );
104 size *= 2;
108 /* get the path of a dos device symlink in the $WINEPREFIX/dosdevices directory */
109 static char *get_dos_device_path( LPCWSTR name )
111 const char *config_dir = wine_get_config_dir();
112 char *buffer, *dev;
113 int i;
115 if (!(buffer = HeapAlloc( GetProcessHeap(), 0,
116 strlen(config_dir) + sizeof("/dosdevices/") + 5 )))
118 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
119 return NULL;
121 strcpy( buffer, config_dir );
122 strcat( buffer, "/dosdevices/" );
123 dev = buffer + strlen(buffer);
124 /* no codepage conversion, DOS device names are ASCII anyway */
125 for (i = 0; i < 5; i++)
126 if (!(dev[i] = (char)tolowerW(name[i]))) break;
127 dev[5] = 0;
128 return buffer;
132 /* open a handle to a device root */
133 static BOOL open_device_root( LPCWSTR root, HANDLE *handle )
135 static const WCHAR default_rootW[] = {'\\',0};
136 UNICODE_STRING nt_name;
137 OBJECT_ATTRIBUTES attr;
138 IO_STATUS_BLOCK io;
139 NTSTATUS status;
141 if (!root) root = default_rootW;
142 if (!RtlDosPathNameToNtPathName_U( root, &nt_name, NULL, NULL ))
144 SetLastError( ERROR_PATH_NOT_FOUND );
145 return FALSE;
147 attr.Length = sizeof(attr);
148 attr.RootDirectory = 0;
149 attr.Attributes = OBJ_CASE_INSENSITIVE;
150 attr.ObjectName = &nt_name;
151 attr.SecurityDescriptor = NULL;
152 attr.SecurityQualityOfService = NULL;
154 status = NtOpenFile( handle, 0, &attr, &io, 0,
155 FILE_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT );
156 RtlFreeUnicodeString( &nt_name );
157 if (status != STATUS_SUCCESS)
159 SetLastError( RtlNtStatusToDosError(status) );
160 return FALSE;
162 return TRUE;
166 /* fetch the type of a drive from the registry */
167 static UINT get_registry_drive_type( const WCHAR *root )
169 static const WCHAR drive_types_keyW[] = {'M','a','c','h','i','n','e','\\',
170 'S','o','f','t','w','a','r','e','\\',
171 'W','i','n','e','\\',
172 'D','r','i','v','e','s',0 };
173 OBJECT_ATTRIBUTES attr;
174 UNICODE_STRING nameW;
175 HANDLE hkey;
176 DWORD dummy;
177 UINT ret = DRIVE_UNKNOWN;
178 char tmp[32 + sizeof(KEY_VALUE_PARTIAL_INFORMATION)];
179 WCHAR driveW[] = {'A',':',0};
181 attr.Length = sizeof(attr);
182 attr.RootDirectory = 0;
183 attr.ObjectName = &nameW;
184 attr.Attributes = 0;
185 attr.SecurityDescriptor = NULL;
186 attr.SecurityQualityOfService = NULL;
187 RtlInitUnicodeString( &nameW, drive_types_keyW );
188 /* @@ Wine registry key: HKLM\Software\Wine\Drives */
189 if (NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr ) != STATUS_SUCCESS) return DRIVE_UNKNOWN;
191 if (root) driveW[0] = root[0];
192 else
194 WCHAR path[MAX_PATH];
195 GetCurrentDirectoryW( MAX_PATH, path );
196 driveW[0] = path[0];
199 RtlInitUnicodeString( &nameW, driveW );
200 if (!NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation, tmp, sizeof(tmp), &dummy ))
202 unsigned int i;
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] ))
209 ret = i;
210 break;
214 NtClose( hkey );
215 return ret;
219 /******************************************************************
220 * VOLUME_FindCdRomDataBestVoldesc
222 static DWORD VOLUME_FindCdRomDataBestVoldesc( HANDLE handle )
224 BYTE cur_vd_type, max_vd_type = 0;
225 BYTE buffer[16];
226 DWORD size, offs, best_offs = 0, extra_offs = 0;
228 for (offs = 0x8000; offs <= 0x9800; offs += 0x800)
230 /* if 'CDROM' occurs at position 8, this is a pre-iso9660 cd, and
231 * the volume label is displaced forward by 8
233 if (SetFilePointer( handle, offs, NULL, FILE_BEGIN ) != offs) break;
234 if (!ReadFile( handle, buffer, sizeof(buffer), &size, NULL )) break;
235 if (size != sizeof(buffer)) break;
236 /* check for non-ISO9660 signature */
237 if (!memcmp( buffer + 11, "ROM", 3 )) extra_offs = 8;
238 cur_vd_type = buffer[extra_offs];
239 if (cur_vd_type == 0xff) /* voldesc set terminator */
240 break;
241 if (cur_vd_type > max_vd_type)
243 max_vd_type = cur_vd_type;
244 best_offs = offs + extra_offs;
247 return best_offs;
251 /***********************************************************************
252 * VOLUME_ReadFATSuperblock
254 static enum fs_type VOLUME_ReadFATSuperblock( HANDLE handle, BYTE *buff )
256 DWORD size;
258 /* try a fixed disk, with a FAT partition */
259 if (SetFilePointer( handle, 0, NULL, FILE_BEGIN ) != 0 ||
260 !ReadFile( handle, buff, SUPERBLOCK_SIZE, &size, NULL ) ||
261 size != SUPERBLOCK_SIZE)
262 return FS_ERROR;
264 /* FIXME: do really all FAT have their name beginning with
265 * "FAT" ? (At least FAT12, FAT16 and FAT32 have :)
267 if (!memcmp(buff+0x36, "FAT", 3) || !memcmp(buff+0x52, "FAT", 3))
269 /* guess which type of FAT we have */
270 int reasonable;
271 unsigned int sectors,
272 sect_per_fat,
273 total_sectors,
274 num_boot_sectors,
275 num_fats,
276 num_root_dir_ents,
277 bytes_per_sector,
278 sectors_per_cluster,
279 nclust;
280 sect_per_fat = GETWORD(buff, 0x16);
281 if (!sect_per_fat) sect_per_fat = GETLONG(buff, 0x24);
282 total_sectors = GETWORD(buff, 0x13);
283 if (!total_sectors)
284 total_sectors = GETLONG(buff, 0x20);
285 num_boot_sectors = GETWORD(buff, 0x0e);
286 num_fats = buff[0x10];
287 num_root_dir_ents = GETWORD(buff, 0x11);
288 bytes_per_sector = GETWORD(buff, 0x0b);
289 sectors_per_cluster = buff[0x0d];
290 /* check if the parameters are reasonable and will not cause
291 * arithmetic errors in the calculation */
292 reasonable = num_boot_sectors < 16 &&
293 num_fats < 16 &&
294 bytes_per_sector >= 512 && bytes_per_sector % 512 == 0 &&
295 sectors_per_cluster > 1;
296 if (!reasonable) return FS_UNKNOWN;
297 sectors = total_sectors - num_boot_sectors - num_fats * sect_per_fat -
298 (num_root_dir_ents * 32 + bytes_per_sector - 1) / bytes_per_sector;
299 nclust = sectors / sectors_per_cluster;
300 if ((buff[0x42] == 0x28 || buff[0x42] == 0x29) &&
301 !memcmp(buff+0x52, "FAT", 3)) return FS_FAT32;
302 if (nclust < 65525)
304 if ((buff[0x26] == 0x28 || buff[0x26] == 0x29) &&
305 !memcmp(buff+0x36, "FAT", 3))
306 return FS_FAT1216;
309 return FS_UNKNOWN;
313 /***********************************************************************
314 * VOLUME_ReadCDSuperblock
316 static enum fs_type VOLUME_ReadCDSuperblock( HANDLE handle, BYTE *buff )
318 DWORD size, offs = VOLUME_FindCdRomDataBestVoldesc( handle );
320 if (!offs) return FS_UNKNOWN;
322 if (SetFilePointer( handle, offs, NULL, FILE_BEGIN ) != offs ||
323 !ReadFile( handle, buff, SUPERBLOCK_SIZE, &size, NULL ) ||
324 size != SUPERBLOCK_SIZE)
325 return FS_ERROR;
327 /* check for iso9660 present */
328 if (!memcmp(&buff[1], "CD001", 5)) return FS_ISO9660;
329 return FS_UNKNOWN;
333 /**************************************************************************
334 * VOLUME_GetSuperblockLabel
336 static void VOLUME_GetSuperblockLabel( enum fs_type type, const BYTE *superblock,
337 WCHAR *label, DWORD len )
339 const BYTE *label_ptr = NULL;
340 DWORD label_len;
342 switch(type)
344 case FS_ERROR:
345 case FS_UNKNOWN:
346 label_len = 0;
347 break;
348 case FS_FAT1216:
349 label_ptr = superblock + 0x2b;
350 label_len = 11;
351 break;
352 case FS_FAT32:
353 label_ptr = superblock + 0x47;
354 label_len = 11;
355 break;
356 case FS_ISO9660:
358 BYTE ver = superblock[0x5a];
360 if (superblock[0x58] == 0x25 && superblock[0x59] == 0x2f && /* Unicode ID */
361 ((ver == 0x40) || (ver == 0x43) || (ver == 0x45)))
362 { /* yippee, unicode */
363 unsigned int i;
365 if (len > 17) len = 17;
366 for (i = 0; i < len-1; i++)
367 label[i] = (superblock[40+2*i] << 8) | superblock[41+2*i];
368 label[i] = 0;
369 while (i && label[i-1] == ' ') label[--i] = 0;
370 return;
372 label_ptr = superblock + 40;
373 label_len = 32;
374 break;
377 if (label_len) RtlMultiByteToUnicodeN( label, (len-1) * sizeof(WCHAR),
378 &label_len, (char*)label_ptr, label_len );
379 label_len /= sizeof(WCHAR);
380 label[label_len] = 0;
381 while (label_len && label[label_len-1] == ' ') label[--label_len] = 0;
385 /**************************************************************************
386 * VOLUME_GetSuperblockSerial
388 static DWORD VOLUME_GetSuperblockSerial( enum fs_type type, const BYTE *superblock )
390 switch(type)
392 case FS_ERROR:
393 case FS_UNKNOWN:
394 break;
395 case FS_FAT1216:
396 return GETLONG( superblock, 0x27 );
397 case FS_FAT32:
398 return GETLONG( superblock, 0x33 );
399 case FS_ISO9660:
401 BYTE sum[4];
402 int i;
404 sum[0] = sum[1] = sum[2] = sum[3] = 0;
405 for (i = 0; i < 2048; i += 4)
407 /* DON'T optimize this into DWORD !! (breaks overflow) */
408 sum[0] += superblock[i+0];
409 sum[1] += superblock[i+1];
410 sum[2] += superblock[i+2];
411 sum[3] += superblock[i+3];
414 * OK, another braindead one... argh. Just believe it.
415 * Me$$ysoft chose to reverse the serial number in NT4/W2K.
416 * It's true and nobody will ever be able to change it.
418 if (GetVersion() & 0x80000000)
419 return (sum[3] << 24) | (sum[2] << 16) | (sum[1] << 8) | sum[0];
420 else
421 return (sum[0] << 24) | (sum[1] << 16) | (sum[2] << 8) | sum[3];
424 return 0;
428 /**************************************************************************
429 * VOLUME_GetAudioCDSerial
431 static DWORD VOLUME_GetAudioCDSerial( const CDROM_TOC *toc )
433 DWORD serial = 0;
434 int i;
436 for (i = 0; i <= toc->LastTrack - toc->FirstTrack; i++)
437 serial += ((toc->TrackData[i].Address[1] << 16) |
438 (toc->TrackData[i].Address[2] << 8) |
439 toc->TrackData[i].Address[3]);
442 * dwStart, dwEnd collect the beginning and end of the disc respectively, in
443 * frames.
444 * There it is collected for correcting the serial when there are less than
445 * 3 tracks.
447 if (toc->LastTrack - toc->FirstTrack + 1 < 3)
449 DWORD dwStart = FRAME_OF_TOC(toc, toc->FirstTrack);
450 DWORD dwEnd = FRAME_OF_TOC(toc, toc->LastTrack + 1);
451 serial += dwEnd - dwStart;
453 return serial;
457 /***********************************************************************
458 * GetVolumeInformationW (KERNEL32.@)
460 BOOL WINAPI GetVolumeInformationW( LPCWSTR root, LPWSTR label, DWORD label_len,
461 DWORD *serial, DWORD *filename_len, DWORD *flags,
462 LPWSTR fsname, DWORD fsname_len )
464 static const WCHAR audiocdW[] = {'A','u','d','i','o',' ','C','D',0};
465 static const WCHAR fatW[] = {'F','A','T',0};
466 static const WCHAR ntfsW[] = {'N','T','F','S',0};
467 static const WCHAR cdfsW[] = {'C','D','F','S',0};
469 WCHAR device[] = {'\\','\\','.','\\','A',':',0};
470 HANDLE handle;
471 enum fs_type type = FS_UNKNOWN;
473 if (!root)
475 WCHAR path[MAX_PATH];
476 GetCurrentDirectoryW( MAX_PATH, path );
477 device[4] = path[0];
479 else
481 if (!root[0] || root[1] != ':')
483 SetLastError( ERROR_INVALID_NAME );
484 return FALSE;
486 device[4] = root[0];
489 /* try to open the device */
491 handle = CreateFileW( device, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE,
492 NULL, OPEN_EXISTING, 0, 0 );
493 if (handle != INVALID_HANDLE_VALUE)
495 BYTE superblock[SUPERBLOCK_SIZE];
496 CDROM_TOC toc;
497 DWORD br;
499 /* check for audio CD */
500 /* FIXME: we only check the first track for now */
501 if (DeviceIoControl( handle, IOCTL_CDROM_READ_TOC, NULL, 0, &toc, sizeof(toc), &br, 0 ))
503 if (!(toc.TrackData[0].Control & 0x04)) /* audio track */
505 TRACE( "%s: found audio CD\n", debugstr_w(device) );
506 if (label) lstrcpynW( label, audiocdW, label_len );
507 if (serial) *serial = VOLUME_GetAudioCDSerial( &toc );
508 CloseHandle( handle );
509 type = FS_ISO9660;
510 goto fill_fs_info;
512 type = VOLUME_ReadCDSuperblock( handle, superblock );
514 else
516 type = VOLUME_ReadFATSuperblock( handle, superblock );
517 if (type == FS_UNKNOWN) type = VOLUME_ReadCDSuperblock( handle, superblock );
519 CloseHandle( handle );
520 TRACE( "%s: found fs type %d\n", debugstr_w(device), type );
521 if (type == FS_ERROR) return FALSE;
523 if (label && label_len) VOLUME_GetSuperblockLabel( type, superblock, label, label_len );
524 if (serial) *serial = VOLUME_GetSuperblockSerial( type, superblock );
525 goto fill_fs_info;
527 else TRACE( "cannot open device %s: err %ld\n", debugstr_w(device), GetLastError() );
529 /* we couldn't open the device, fallback to default strategy */
531 switch(GetDriveTypeW( root ))
533 case DRIVE_UNKNOWN:
534 case DRIVE_NO_ROOT_DIR:
535 SetLastError( ERROR_NOT_READY );
536 return FALSE;
537 case DRIVE_REMOVABLE:
538 case DRIVE_FIXED:
539 case DRIVE_REMOTE:
540 case DRIVE_RAMDISK:
541 type = FS_UNKNOWN;
542 break;
543 case DRIVE_CDROM:
544 type = FS_ISO9660;
545 break;
548 if (label && label_len)
550 WCHAR labelW[] = {'A',':','\\','.','w','i','n','d','o','w','s','-','l','a','b','e','l',0};
552 labelW[0] = device[4];
553 handle = CreateFileW( labelW, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
554 OPEN_EXISTING, 0, 0 );
555 if (handle != INVALID_HANDLE_VALUE)
557 char buffer[256], *p;
558 DWORD size;
560 if (!ReadFile( handle, buffer, sizeof(buffer)-1, &size, NULL )) size = 0;
561 CloseHandle( handle );
562 p = buffer + size;
563 while (p > buffer && (p[-1] == ' ' || p[-1] == '\r' || p[-1] == '\n')) p--;
564 *p = 0;
565 if (!MultiByteToWideChar( CP_UNIXCP, 0, buffer, -1, label, label_len ))
566 label[label_len-1] = 0;
568 else label[0] = 0;
570 if (serial)
572 WCHAR serialW[] = {'A',':','\\','.','w','i','n','d','o','w','s','-','s','e','r','i','a','l',0};
574 serialW[0] = device[4];
575 handle = CreateFileW( serialW, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
576 OPEN_EXISTING, 0, 0 );
577 if (handle != INVALID_HANDLE_VALUE)
579 char buffer[32];
580 DWORD size;
582 if (!ReadFile( handle, buffer, sizeof(buffer)-1, &size, NULL )) size = 0;
583 CloseHandle( handle );
584 buffer[size] = 0;
585 *serial = strtoul( buffer, NULL, 16 );
587 else *serial = 0;
590 fill_fs_info: /* now fill in the information that depends on the file system type */
592 switch(type)
594 case FS_ISO9660:
595 if (fsname) lstrcpynW( fsname, cdfsW, fsname_len );
596 if (filename_len) *filename_len = 221;
597 if (flags) *flags = FILE_READ_ONLY_VOLUME;
598 break;
599 case FS_FAT1216:
600 case FS_FAT32:
601 if (fsname) lstrcpynW( fsname, fatW, fsname_len );
602 if (filename_len) *filename_len = 255;
603 if (flags) *flags = FILE_CASE_PRESERVED_NAMES; /* FIXME */
604 break;
605 default:
606 if (fsname) lstrcpynW( fsname, ntfsW, fsname_len );
607 if (filename_len) *filename_len = 255;
608 if (flags) *flags = FILE_CASE_PRESERVED_NAMES;
609 break;
611 return TRUE;
615 /***********************************************************************
616 * GetVolumeInformationA (KERNEL32.@)
618 BOOL WINAPI GetVolumeInformationA( LPCSTR root, LPSTR label,
619 DWORD label_len, DWORD *serial,
620 DWORD *filename_len, DWORD *flags,
621 LPSTR fsname, DWORD fsname_len )
623 WCHAR *rootW = NULL;
624 LPWSTR labelW, fsnameW;
625 BOOL ret;
627 if (root && !(rootW = FILE_name_AtoW( root, FALSE ))) return FALSE;
629 labelW = label ? HeapAlloc(GetProcessHeap(), 0, label_len * sizeof(WCHAR)) : NULL;
630 fsnameW = fsname ? HeapAlloc(GetProcessHeap(), 0, fsname_len * sizeof(WCHAR)) : NULL;
632 if ((ret = GetVolumeInformationW(rootW, labelW, label_len, serial,
633 filename_len, flags, fsnameW, fsname_len)))
635 if (label) FILE_name_WtoA( labelW, -1, label, label_len );
636 if (fsname) FILE_name_WtoA( fsnameW, -1, fsname, fsname_len );
639 HeapFree( GetProcessHeap(), 0, labelW );
640 HeapFree( GetProcessHeap(), 0, fsnameW );
641 return ret;
646 /***********************************************************************
647 * SetVolumeLabelW (KERNEL32.@)
649 BOOL WINAPI SetVolumeLabelW( LPCWSTR root, LPCWSTR label )
651 WCHAR device[] = {'\\','\\','.','\\','A',':',0};
652 HANDLE handle;
653 enum fs_type type = FS_UNKNOWN;
655 if (!root)
657 WCHAR path[MAX_PATH];
658 GetCurrentDirectoryW( MAX_PATH, path );
659 device[4] = path[0];
661 else
663 if (!root[0] || root[1] != ':')
665 SetLastError( ERROR_INVALID_NAME );
666 return FALSE;
668 device[4] = root[0];
671 /* try to open the device */
673 handle = CreateFileW( device, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE,
674 NULL, OPEN_EXISTING, 0, 0 );
675 if (handle != INVALID_HANDLE_VALUE)
677 BYTE superblock[SUPERBLOCK_SIZE];
679 type = VOLUME_ReadFATSuperblock( handle, superblock );
680 if (type == FS_UNKNOWN) type = VOLUME_ReadCDSuperblock( handle, superblock );
681 CloseHandle( handle );
682 if (type != FS_UNKNOWN)
684 /* we can't set the label on FAT or CDROM file systems */
685 TRACE( "cannot set label on device %s type %d\n", debugstr_w(device), type );
686 SetLastError( ERROR_ACCESS_DENIED );
687 return FALSE;
690 else
692 TRACE( "cannot open device %s: err %ld\n", debugstr_w(device), GetLastError() );
693 if (GetLastError() == ERROR_ACCESS_DENIED) return FALSE;
696 /* we couldn't open the device, fallback to default strategy */
698 switch(GetDriveTypeW( root ))
700 case DRIVE_UNKNOWN:
701 case DRIVE_NO_ROOT_DIR:
702 SetLastError( ERROR_NOT_READY );
703 break;
704 case DRIVE_REMOVABLE:
705 case DRIVE_FIXED:
707 WCHAR labelW[] = {'A',':','\\','.','w','i','n','d','o','w','s','-','l','a','b','e','l',0};
709 labelW[0] = device[4];
710 handle = CreateFileW( labelW, GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
711 CREATE_ALWAYS, 0, 0 );
712 if (handle != INVALID_HANDLE_VALUE)
714 char buffer[64];
715 DWORD size;
717 if (!WideCharToMultiByte( CP_UNIXCP, 0, label, -1, buffer, sizeof(buffer), NULL, NULL ))
718 buffer[sizeof(buffer)-1] = 0;
719 WriteFile( handle, buffer, strlen(buffer), &size, NULL );
720 CloseHandle( handle );
721 return TRUE;
723 break;
725 case DRIVE_REMOTE:
726 case DRIVE_RAMDISK:
727 case DRIVE_CDROM:
728 SetLastError( ERROR_ACCESS_DENIED );
729 break;
731 return FALSE;
734 /***********************************************************************
735 * SetVolumeLabelA (KERNEL32.@)
737 BOOL WINAPI SetVolumeLabelA(LPCSTR root, LPCSTR volname)
739 WCHAR *rootW = NULL, *volnameW = NULL;
740 BOOL ret;
742 if (root && !(rootW = FILE_name_AtoW( root, FALSE ))) return FALSE;
743 if (volname && !(volnameW = FILE_name_AtoW( volname, TRUE ))) return FALSE;
744 ret = SetVolumeLabelW( rootW, volnameW );
745 HeapFree( GetProcessHeap(), 0, volnameW );
746 return ret;
750 /***********************************************************************
751 * GetVolumeNameForVolumeMountPointW (KERNEL32.@)
753 BOOL WINAPI GetVolumeNameForVolumeMountPointW(LPCWSTR str, LPWSTR dst, DWORD size)
755 FIXME("(%s, %p, %lx): stub\n", debugstr_w(str), dst, size);
756 return 0;
760 /***********************************************************************
761 * DefineDosDeviceW (KERNEL32.@)
763 BOOL WINAPI DefineDosDeviceW( DWORD flags, LPCWSTR devname, LPCWSTR targetpath )
765 DWORD len, dosdev;
766 BOOL ret = FALSE;
767 char *path = NULL, *target, *p;
769 TRACE("%lx, %s, %s\n", flags, debugstr_w(devname), debugstr_w(targetpath));
771 if (!(flags & DDD_REMOVE_DEFINITION))
773 if (!(flags & DDD_RAW_TARGET_PATH))
775 FIXME( "(0x%08lx,%s,%s) DDD_RAW_TARGET_PATH flag not set, not supported yet\n",
776 flags, debugstr_w(devname), debugstr_w(targetpath) );
777 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
778 return FALSE;
781 len = WideCharToMultiByte( CP_UNIXCP, 0, targetpath, -1, NULL, 0, NULL, NULL );
782 if ((target = HeapAlloc( GetProcessHeap(), 0, len )))
784 WideCharToMultiByte( CP_UNIXCP, 0, targetpath, -1, target, len, NULL, NULL );
785 for (p = target; *p; p++) if (*p == '\\') *p = '/';
787 else
789 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
790 return FALSE;
793 else target = NULL;
795 /* first check for a DOS device */
797 if ((dosdev = RtlIsDosDeviceName_U( devname )))
799 WCHAR name[5];
801 memcpy( name, devname + HIWORD(dosdev)/sizeof(WCHAR), LOWORD(dosdev) );
802 name[LOWORD(dosdev)/sizeof(WCHAR)] = 0;
803 path = get_dos_device_path( name );
805 else if (isalphaW(devname[0]) && devname[1] == ':' && !devname[2]) /* drive mapping */
807 path = get_dos_device_path( devname );
809 else SetLastError( ERROR_FILE_NOT_FOUND );
811 if (path)
813 if (target)
815 TRACE( "creating symlink %s -> %s\n", path, target );
816 unlink( path );
817 if (!symlink( target, path )) ret = TRUE;
818 else FILE_SetDosError();
820 else
822 TRACE( "removing symlink %s\n", path );
823 if (!unlink( path )) ret = TRUE;
824 else FILE_SetDosError();
826 HeapFree( GetProcessHeap(), 0, path );
828 HeapFree( GetProcessHeap(), 0, target );
829 return ret;
833 /***********************************************************************
834 * DefineDosDeviceA (KERNEL32.@)
836 BOOL WINAPI DefineDosDeviceA(DWORD flags, LPCSTR devname, LPCSTR targetpath)
838 WCHAR *devW, *targetW = NULL;
839 BOOL ret;
841 if (!(devW = FILE_name_AtoW( devname, FALSE ))) return FALSE;
842 if (targetpath && !(targetW = FILE_name_AtoW( targetpath, TRUE ))) return FALSE;
843 ret = DefineDosDeviceW(flags, devW, targetW);
844 HeapFree( GetProcessHeap(), 0, targetW );
845 return ret;
849 /***********************************************************************
850 * QueryDosDeviceW (KERNEL32.@)
852 * returns array of strings terminated by \0, terminated by \0
854 DWORD WINAPI QueryDosDeviceW( LPCWSTR devname, LPWSTR target, DWORD bufsize )
856 static const WCHAR auxW[] = {'A','U','X',0};
857 static const WCHAR nulW[] = {'N','U','L',0};
858 static const WCHAR prnW[] = {'P','R','N',0};
859 static const WCHAR comW[] = {'C','O','M',0};
860 static const WCHAR lptW[] = {'L','P','T',0};
861 static const WCHAR rootW[] = {'A',':','\\',0};
862 static const WCHAR com0W[] = {'\\','?','?','\\','C','O','M','0',0};
863 static const WCHAR com1W[] = {'\\','D','o','s','D','e','v','i','c','e','s','\\','C','O','M','1',0,0};
864 static const WCHAR lpt1W[] = {'\\','D','o','s','D','e','v','i','c','e','s','\\','L','P','T','1',0,0};
866 UNICODE_STRING nt_name;
867 ANSI_STRING unix_name;
868 WCHAR nt_buffer[10];
869 NTSTATUS status;
871 if (!bufsize)
873 SetLastError( ERROR_INSUFFICIENT_BUFFER );
874 return 0;
877 if (devname)
879 WCHAR *p, name[5];
880 char *path, *link;
881 DWORD dosdev, ret = 0;
883 if ((dosdev = RtlIsDosDeviceName_U( devname )))
885 memcpy( name, devname + HIWORD(dosdev)/sizeof(WCHAR), LOWORD(dosdev) );
886 name[LOWORD(dosdev)/sizeof(WCHAR)] = 0;
888 else if (devname[0] && devname[1] == ':' && !devname[2])
890 memcpy( name, devname, 3 * sizeof(WCHAR) );
892 else
894 SetLastError( ERROR_BAD_PATHNAME );
895 return 0;
898 if (!(path = get_dos_device_path( name ))) return 0;
899 link = read_symlink( path );
900 HeapFree( GetProcessHeap(), 0, path );
902 if (link)
904 ret = MultiByteToWideChar( CP_UNIXCP, 0, link, -1, target, bufsize );
905 HeapFree( GetProcessHeap(), 0, link );
907 else if (dosdev) /* look for device defaults */
909 if (!strcmpiW( name, auxW ))
911 if (bufsize >= sizeof(com1W)/sizeof(WCHAR))
913 memcpy( target, com1W, sizeof(com1W) );
914 ret = sizeof(com1W)/sizeof(WCHAR);
916 else SetLastError( ERROR_INSUFFICIENT_BUFFER );
917 return ret;
919 if (!strcmpiW( name, prnW ))
921 if (bufsize >= sizeof(lpt1W)/sizeof(WCHAR))
923 memcpy( target, lpt1W, sizeof(lpt1W) );
924 ret = sizeof(lpt1W)/sizeof(WCHAR);
926 else SetLastError( ERROR_INSUFFICIENT_BUFFER );
927 return ret;
930 nt_buffer[0] = '\\';
931 nt_buffer[1] = '?';
932 nt_buffer[2] = '?';
933 nt_buffer[3] = '\\';
934 strcpyW( nt_buffer + 4, name );
935 RtlInitUnicodeString( &nt_name, nt_buffer );
936 status = wine_nt_to_unix_file_name( &nt_name, &unix_name, FILE_OPEN, TRUE );
937 if (status) SetLastError( RtlNtStatusToDosError(status) );
938 else
940 ret = MultiByteToWideChar( CP_UNIXCP, 0, unix_name.Buffer, -1, target, bufsize );
941 RtlFreeAnsiString( &unix_name );
945 if (ret)
947 if (ret < bufsize) target[ret++] = 0; /* add an extra null */
948 for (p = target; *p; p++) if (*p == '/') *p = '\\';
951 return ret;
953 else /* return a list of all devices */
955 WCHAR *p = target;
956 int i;
958 if (bufsize <= (sizeof(auxW)+sizeof(nulW)+sizeof(prnW))/sizeof(WCHAR))
960 SetLastError( ERROR_INSUFFICIENT_BUFFER );
961 return 0;
964 memcpy( p, auxW, sizeof(auxW) );
965 p += sizeof(auxW) / sizeof(WCHAR);
966 memcpy( p, nulW, sizeof(nulW) );
967 p += sizeof(nulW) / sizeof(WCHAR);
968 memcpy( p, prnW, sizeof(prnW) );
969 p += sizeof(prnW) / sizeof(WCHAR);
971 strcpyW( nt_buffer, com0W );
972 RtlInitUnicodeString( &nt_name, nt_buffer );
974 for (i = 1; i <= 9; i++)
976 nt_buffer[7] = '0' + i;
977 if (!wine_nt_to_unix_file_name( &nt_name, &unix_name, FILE_OPEN, TRUE ))
979 RtlFreeAnsiString( &unix_name );
980 if (p + 5 >= target + bufsize)
982 SetLastError( ERROR_INSUFFICIENT_BUFFER );
983 return 0;
985 strcpyW( p, comW );
986 p[3] = '0' + i;
987 p[4] = 0;
988 p += 5;
991 strcpyW( nt_buffer + 4, lptW );
992 for (i = 1; i <= 9; i++)
994 nt_buffer[7] = '0' + i;
995 if (!wine_nt_to_unix_file_name( &nt_name, &unix_name, FILE_OPEN, TRUE ))
997 RtlFreeAnsiString( &unix_name );
998 if (p + 5 >= target + bufsize)
1000 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1001 return 0;
1003 strcpyW( p, lptW );
1004 p[3] = '0' + i;
1005 p[4] = 0;
1006 p += 5;
1010 strcpyW( nt_buffer + 4, rootW );
1011 RtlInitUnicodeString( &nt_name, nt_buffer );
1013 for (i = 0; i < 26; i++)
1015 nt_buffer[4] = 'a' + i;
1016 if (!wine_nt_to_unix_file_name( &nt_name, &unix_name, FILE_OPEN, TRUE ))
1018 RtlFreeAnsiString( &unix_name );
1019 if (p + 3 >= target + bufsize)
1021 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1022 return 0;
1024 *p++ = 'A' + i;
1025 *p++ = ':';
1026 *p++ = 0;
1029 *p++ = 0; /* terminating null */
1030 return p - target;
1035 /***********************************************************************
1036 * QueryDosDeviceA (KERNEL32.@)
1038 * returns array of strings terminated by \0, terminated by \0
1040 DWORD WINAPI QueryDosDeviceA( LPCSTR devname, LPSTR target, DWORD bufsize )
1042 DWORD ret = 0, retW;
1043 WCHAR *devnameW = NULL;
1044 LPWSTR targetW;
1046 if (devname && !(devnameW = FILE_name_AtoW( devname, FALSE ))) return 0;
1048 targetW = HeapAlloc( GetProcessHeap(),0, bufsize * sizeof(WCHAR) );
1049 if (!targetW)
1051 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1052 return 0;
1055 retW = QueryDosDeviceW(devnameW, targetW, bufsize);
1057 ret = FILE_name_WtoA( targetW, retW, target, bufsize );
1059 HeapFree(GetProcessHeap(), 0, targetW);
1060 return ret;
1064 /***********************************************************************
1065 * GetLogicalDrives (KERNEL32.@)
1067 DWORD WINAPI GetLogicalDrives(void)
1069 const char *config_dir = wine_get_config_dir();
1070 struct stat st;
1071 char *buffer, *dev;
1072 DWORD ret = 0;
1073 int i;
1075 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, strlen(config_dir) + sizeof("/dosdevices/a:") )))
1077 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1078 return 0;
1080 strcpy( buffer, config_dir );
1081 strcat( buffer, "/dosdevices/a:" );
1082 dev = buffer + strlen(buffer) - 2;
1084 for (i = 0; i < 26; i++)
1086 *dev = 'a' + i;
1087 if (*dev == 'c')
1088 continue;
1089 if (!stat( buffer, &st )) ret |= (1 << i);
1091 HeapFree( GetProcessHeap(), 0, buffer );
1092 return ret;
1096 /***********************************************************************
1097 * GetLogicalDriveStringsA (KERNEL32.@)
1099 UINT WINAPI GetLogicalDriveStringsA( UINT len, LPSTR buffer )
1101 DWORD drives = GetLogicalDrives();
1102 UINT drive, count;
1104 for (drive = count = 0; drive < 26; drive++) if (drives & (1 << drive)) count++;
1105 if ((count * 4) + 1 > len) return count * 4 + 1;
1107 for (drive = 0; drive < 26; drive++)
1109 if (drives & (1 << drive))
1111 *buffer++ = 'a' + drive;
1112 *buffer++ = ':';
1113 *buffer++ = '\\';
1114 *buffer++ = 0;
1117 *buffer = 0;
1118 return count * 4;
1122 /***********************************************************************
1123 * GetLogicalDriveStringsW (KERNEL32.@)
1125 UINT WINAPI GetLogicalDriveStringsW( UINT len, LPWSTR buffer )
1127 DWORD drives = GetLogicalDrives();
1128 UINT drive, count;
1130 for (drive = count = 0; drive < 26; drive++) if (drives & (1 << drive)) count++;
1131 if ((count * 4) + 1 > len) return count * 4 + 1;
1133 for (drive = 0; drive < 26; drive++)
1135 if (drives & (1 << drive))
1137 *buffer++ = 'a' + drive;
1138 *buffer++ = ':';
1139 *buffer++ = '\\';
1140 *buffer++ = 0;
1143 *buffer = 0;
1144 return count * 4;
1148 /***********************************************************************
1149 * GetDriveTypeW (KERNEL32.@)
1151 * Returns the type of the disk drive specified. If root is NULL the
1152 * root of the current directory is used.
1154 * RETURNS
1156 * Type of drive (from Win32 SDK):
1158 * DRIVE_UNKNOWN unable to find out anything about the drive
1159 * DRIVE_NO_ROOT_DIR nonexistent root dir
1160 * DRIVE_REMOVABLE the disk can be removed from the machine
1161 * DRIVE_FIXED the disk cannot be removed from the machine
1162 * DRIVE_REMOTE network disk
1163 * DRIVE_CDROM CDROM drive
1164 * DRIVE_RAMDISK virtual disk in RAM
1166 UINT WINAPI GetDriveTypeW(LPCWSTR root) /* [in] String describing drive */
1168 FILE_FS_DEVICE_INFORMATION info;
1169 IO_STATUS_BLOCK io;
1170 NTSTATUS status;
1171 HANDLE handle;
1172 UINT ret;
1174 if (!open_device_root( root, &handle )) return DRIVE_NO_ROOT_DIR;
1176 status = NtQueryVolumeInformationFile( handle, &io, &info, sizeof(info), FileFsDeviceInformation );
1177 NtClose( handle );
1178 if (status != STATUS_SUCCESS)
1180 SetLastError( RtlNtStatusToDosError(status) );
1181 ret = DRIVE_UNKNOWN;
1183 else if ((ret = get_registry_drive_type( root )) == DRIVE_UNKNOWN)
1185 switch (info.DeviceType)
1187 case FILE_DEVICE_CD_ROM_FILE_SYSTEM: ret = DRIVE_CDROM; break;
1188 case FILE_DEVICE_VIRTUAL_DISK: ret = DRIVE_RAMDISK; break;
1189 case FILE_DEVICE_NETWORK_FILE_SYSTEM: ret = DRIVE_REMOTE; break;
1190 case FILE_DEVICE_DISK_FILE_SYSTEM:
1191 if (info.Characteristics & FILE_REMOTE_DEVICE) ret = DRIVE_REMOTE;
1192 else if (info.Characteristics & FILE_REMOVABLE_MEDIA) ret = DRIVE_REMOVABLE;
1193 else ret = DRIVE_FIXED;
1194 break;
1195 default:
1196 ret = DRIVE_UNKNOWN;
1197 break;
1200 TRACE( "%s -> %d\n", debugstr_w(root), ret );
1201 return ret;
1205 /***********************************************************************
1206 * GetDriveTypeA (KERNEL32.@)
1208 * See GetDriveTypeW.
1210 UINT WINAPI GetDriveTypeA( LPCSTR root )
1212 WCHAR *rootW = NULL;
1214 if (root && !(rootW = FILE_name_AtoW( root, FALSE ))) return DRIVE_NO_ROOT_DIR;
1215 return GetDriveTypeW( rootW );
1219 /***********************************************************************
1220 * GetDiskFreeSpaceExW (KERNEL32.@)
1222 * This function is used to acquire the size of the available and
1223 * total space on a logical volume.
1225 * RETURNS
1227 * Zero on failure, nonzero upon success. Use GetLastError to obtain
1228 * detailed error information.
1231 BOOL WINAPI GetDiskFreeSpaceExW( LPCWSTR root, PULARGE_INTEGER avail,
1232 PULARGE_INTEGER total, PULARGE_INTEGER totalfree )
1234 FILE_FS_SIZE_INFORMATION info;
1235 IO_STATUS_BLOCK io;
1236 NTSTATUS status;
1237 HANDLE handle;
1238 UINT units;
1240 TRACE( "%s,%p,%p,%p\n", debugstr_w(root), avail, total, totalfree );
1242 if (!open_device_root( root, &handle )) return FALSE;
1244 status = NtQueryVolumeInformationFile( handle, &io, &info, sizeof(info), FileFsSizeInformation );
1245 NtClose( handle );
1246 if (status != STATUS_SUCCESS)
1248 SetLastError( RtlNtStatusToDosError(status) );
1249 return FALSE;
1252 units = info.SectorsPerAllocationUnit * info.BytesPerSector;
1253 if (total) total->QuadPart = info.TotalAllocationUnits.QuadPart * units;
1254 if (totalfree) totalfree->QuadPart = info.AvailableAllocationUnits.QuadPart * units;
1255 /* FIXME: this one should take quotas into account */
1256 if (avail) avail->QuadPart = info.AvailableAllocationUnits.QuadPart * units;
1257 return TRUE;
1261 /***********************************************************************
1262 * GetDiskFreeSpaceExA (KERNEL32.@)
1264 * See GetDiskFreeSpaceExW.
1266 BOOL WINAPI GetDiskFreeSpaceExA( LPCSTR root, PULARGE_INTEGER avail,
1267 PULARGE_INTEGER total, PULARGE_INTEGER totalfree )
1269 WCHAR *rootW = NULL;
1271 if (root && !(rootW = FILE_name_AtoW( root, FALSE ))) return FALSE;
1272 return GetDiskFreeSpaceExW( rootW, avail, total, totalfree );
1276 /***********************************************************************
1277 * GetDiskFreeSpaceW (KERNEL32.@)
1279 BOOL WINAPI GetDiskFreeSpaceW( LPCWSTR root, LPDWORD cluster_sectors,
1280 LPDWORD sector_bytes, LPDWORD free_clusters,
1281 LPDWORD total_clusters )
1283 FILE_FS_SIZE_INFORMATION info;
1284 IO_STATUS_BLOCK io;
1285 NTSTATUS status;
1286 HANDLE handle;
1287 UINT units;
1289 TRACE( "%s,%p,%p,%p,%p\n", debugstr_w(root),
1290 cluster_sectors, sector_bytes, free_clusters, total_clusters );
1292 if (!open_device_root( root, &handle )) return FALSE;
1294 status = NtQueryVolumeInformationFile( handle, &io, &info, sizeof(info), FileFsSizeInformation );
1295 NtClose( handle );
1296 if (status != STATUS_SUCCESS)
1298 SetLastError( RtlNtStatusToDosError(status) );
1299 return FALSE;
1302 units = info.SectorsPerAllocationUnit * info.BytesPerSector;
1304 if( GetVersion() & 0x80000000) { /* win3.x, 9x, ME */
1305 /* cap the size and available at 2GB as per specs */
1306 if (info.TotalAllocationUnits.QuadPart * units > 0x7fffffff) {
1307 info.TotalAllocationUnits.QuadPart = 0x7fffffff / units;
1308 if (info.AvailableAllocationUnits.QuadPart * units > 0x7fffffff)
1309 info.AvailableAllocationUnits.QuadPart = 0x7fffffff / units;
1311 /* nr. of clusters is always <= 65335 */
1312 while( info.TotalAllocationUnits.QuadPart > 65535 ) {
1313 info.TotalAllocationUnits.QuadPart /= 2;
1314 info.AvailableAllocationUnits.QuadPart /= 2;
1315 info.SectorsPerAllocationUnit *= 2;
1319 if (cluster_sectors) *cluster_sectors = info.SectorsPerAllocationUnit;
1320 if (sector_bytes) *sector_bytes = info.BytesPerSector;
1321 if (free_clusters) *free_clusters = info.AvailableAllocationUnits.u.LowPart;
1322 if (total_clusters) *total_clusters = info.TotalAllocationUnits.u.LowPart;
1323 return TRUE;
1327 /***********************************************************************
1328 * GetDiskFreeSpaceA (KERNEL32.@)
1330 BOOL WINAPI GetDiskFreeSpaceA( LPCSTR root, LPDWORD cluster_sectors,
1331 LPDWORD sector_bytes, LPDWORD free_clusters,
1332 LPDWORD total_clusters )
1334 WCHAR *rootW = NULL;
1336 if (root && !(rootW = FILE_name_AtoW( root, FALSE ))) return FALSE;
1337 return GetDiskFreeSpaceW( rootW, cluster_sectors, sector_bytes, free_clusters, total_clusters );
1340 /***********************************************************************
1341 * GetVolumePathNameA (KERNEL32.@)
1343 BOOL WINAPI GetVolumePathNameA(LPCSTR filename, LPSTR volumepathname, DWORD buflen)
1345 FIXME("(%s, %p, %ld), stub!\n", debugstr_a(filename), volumepathname, buflen);
1346 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1347 return FALSE;
1350 /***********************************************************************
1351 * GetVolumePathNameW (KERNEL32.@)
1353 BOOL WINAPI GetVolumePathNameW(LPCWSTR filename, LPWSTR volumepathname, DWORD buflen)
1355 FIXME("(%s, %p, %ld), stub!\n", debugstr_w(filename), volumepathname, buflen);
1356 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1357 return FALSE;