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
26 #include "wine/port.h"
33 #define WIN32_NO_STATUS
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))
60 FS_ERROR, /* error accessing the device */
61 FS_UNKNOWN, /* unknown file system */
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 )
86 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, size )))
88 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
91 ret = readlink( path, buffer, size );
95 HeapFree( GetProcessHeap(), 0, buffer );
103 HeapFree( GetProcessHeap(), 0, buffer );
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();
115 if (!(buffer = HeapAlloc( GetProcessHeap(), 0,
116 strlen(config_dir) + sizeof("/dosdevices/") + 5 )))
118 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
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;
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;
141 if (!root) root = default_rootW;
142 if (!RtlDosPathNameToNtPathName_U( root, &nt_name, NULL, NULL ))
144 SetLastError( ERROR_PATH_NOT_FOUND );
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) );
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;
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;
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];
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 /******************************************************************
220 * VOLUME_FindCdRomDataBestVoldesc
222 static DWORD VOLUME_FindCdRomDataBestVoldesc( HANDLE handle )
224 BYTE cur_vd_type, max_vd_type = 0;
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 */
241 if (cur_vd_type > max_vd_type)
243 max_vd_type = cur_vd_type;
244 best_offs = offs + extra_offs;
251 /***********************************************************************
252 * VOLUME_ReadFATSuperblock
254 static enum fs_type VOLUME_ReadFATSuperblock( HANDLE handle, BYTE *buff )
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)
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 */
271 unsigned int sectors,
280 sect_per_fat = GETWORD(buff, 0x16);
281 if (!sect_per_fat) sect_per_fat = GETLONG(buff, 0x24);
282 total_sectors = GETWORD(buff, 0x13);
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 &&
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;
304 if ((buff[0x26] == 0x28 || buff[0x26] == 0x29) &&
305 !memcmp(buff+0x36, "FAT", 3))
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)
327 /* check for iso9660 present */
328 if (!memcmp(&buff[1], "CD001", 5)) return FS_ISO9660;
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;
349 label_ptr = superblock + 0x2b;
353 label_ptr = superblock + 0x47;
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 */
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];
369 while (i && label[i-1] == ' ') label[--i] = 0;
372 label_ptr = superblock + 40;
377 if (label_len) RtlMultiByteToUnicodeN( label, (len-1) * sizeof(WCHAR),
378 &label_len, (LPCSTR)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 )
396 return GETLONG( superblock, 0x27 );
398 return GETLONG( superblock, 0x33 );
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];
421 return (sum[0] << 24) | (sum[1] << 16) | (sum[2] << 8) | sum[3];
428 /**************************************************************************
429 * VOLUME_GetAudioCDSerial
431 static DWORD VOLUME_GetAudioCDSerial( const CDROM_TOC *toc )
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
444 * There it is collected for correcting the serial when there are less than
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;
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};
471 enum fs_type type = FS_UNKNOWN;
475 WCHAR path[MAX_PATH];
476 GetCurrentDirectoryW( MAX_PATH, path );
481 if (!root[0] || root[1] != ':')
483 SetLastError( ERROR_INVALID_NAME );
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];
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 );
512 type = VOLUME_ReadCDSuperblock( handle, superblock );
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 );
527 else TRACE( "cannot open device %s: err %d\n", debugstr_w(device), GetLastError() );
529 /* we couldn't open the device, fallback to default strategy */
531 switch(GetDriveTypeW( root ))
534 case DRIVE_NO_ROOT_DIR:
535 SetLastError( ERROR_NOT_READY );
537 case DRIVE_REMOVABLE:
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;
560 if (!ReadFile( handle, buffer, sizeof(buffer)-1, &size, NULL )) size = 0;
561 CloseHandle( handle );
563 while (p > buffer && (p[-1] == ' ' || p[-1] == '\r' || p[-1] == '\n')) p--;
565 if (!MultiByteToWideChar( CP_UNIXCP, 0, buffer, -1, label, label_len ))
566 label[label_len-1] = 0;
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)
582 if (!ReadFile( handle, buffer, sizeof(buffer)-1, &size, NULL )) size = 0;
583 CloseHandle( handle );
585 *serial = strtoul( buffer, NULL, 16 );
590 fill_fs_info: /* now fill in the information that depends on the file system type */
595 if (fsname) lstrcpynW( fsname, cdfsW, fsname_len );
596 if (filename_len) *filename_len = 221;
597 if (flags) *flags = FILE_READ_ONLY_VOLUME;
601 if (fsname) lstrcpynW( fsname, fatW, fsname_len );
602 if (filename_len) *filename_len = 255;
603 if (flags) *flags = FILE_CASE_PRESERVED_NAMES; /* FIXME */
606 if (fsname) lstrcpynW( fsname, ntfsW, fsname_len );
607 if (filename_len) *filename_len = 255;
608 if (flags) *flags = FILE_CASE_PRESERVED_NAMES;
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 )
624 LPWSTR labelW, fsnameW;
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 );
646 /***********************************************************************
647 * SetVolumeLabelW (KERNEL32.@)
649 BOOL WINAPI SetVolumeLabelW( LPCWSTR root, LPCWSTR label )
651 WCHAR device[] = {'\\','\\','.','\\','A',':',0};
653 enum fs_type type = FS_UNKNOWN;
657 WCHAR path[MAX_PATH];
658 GetCurrentDirectoryW( MAX_PATH, path );
663 if (!root[0] || root[1] != ':')
665 SetLastError( ERROR_INVALID_NAME );
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 );
692 TRACE( "cannot open device %s: err %d\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 ))
701 case DRIVE_NO_ROOT_DIR:
702 SetLastError( ERROR_NOT_READY );
704 case DRIVE_REMOVABLE:
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)
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 );
728 SetLastError( ERROR_ACCESS_DENIED );
734 /***********************************************************************
735 * SetVolumeLabelA (KERNEL32.@)
737 BOOL WINAPI SetVolumeLabelA(LPCSTR root, LPCSTR volname)
739 WCHAR *rootW = NULL, *volnameW = NULL;
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 );
750 /***********************************************************************
751 * GetVolumeNameForVolumeMountPointA (KERNEL32.@)
753 BOOL WINAPI GetVolumeNameForVolumeMountPointA( LPCSTR path, LPSTR volume, DWORD size )
756 WCHAR volumeW[50], *pathW = NULL;
757 DWORD len = min( sizeof(volumeW) / sizeof(WCHAR), size );
759 TRACE("(%s, %p, %x)\n", debugstr_a(path), volume, size);
761 if (!path || !(pathW = FILE_name_AtoW( path, TRUE )))
764 if ((ret = GetVolumeNameForVolumeMountPointW( pathW, volumeW, len )))
765 FILE_name_WtoA( volumeW, -1, volume, len );
767 HeapFree( GetProcessHeap(), 0, pathW );
771 /***********************************************************************
772 * GetVolumeNameForVolumeMountPointW (KERNEL32.@)
774 BOOL WINAPI GetVolumeNameForVolumeMountPointW( LPCWSTR path, LPWSTR volume, DWORD size )
777 static const WCHAR fmt[] =
778 { '\\','\\','?','\\','V','o','l','u','m','e','{','%','0','2','x','}','\\',0 };
780 TRACE("(%s, %p, %x)\n", debugstr_w(path), volume, size);
782 if (!path || !path[0]) return FALSE;
784 if (size >= sizeof(fmt) / sizeof(WCHAR))
786 /* FIXME: will break when we support volume mounts */
787 sprintfW( volume, fmt, tolowerW( path[0] ) - 'a' );
793 /***********************************************************************
794 * DefineDosDeviceW (KERNEL32.@)
796 BOOL WINAPI DefineDosDeviceW( DWORD flags, LPCWSTR devname, LPCWSTR targetpath )
800 char *path = NULL, *target, *p;
802 TRACE("%x, %s, %s\n", flags, debugstr_w(devname), debugstr_w(targetpath));
804 if (!(flags & DDD_REMOVE_DEFINITION))
806 if (!(flags & DDD_RAW_TARGET_PATH))
808 FIXME( "(0x%08x,%s,%s) DDD_RAW_TARGET_PATH flag not set, not supported yet\n",
809 flags, debugstr_w(devname), debugstr_w(targetpath) );
810 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
814 len = WideCharToMultiByte( CP_UNIXCP, 0, targetpath, -1, NULL, 0, NULL, NULL );
815 if ((target = HeapAlloc( GetProcessHeap(), 0, len )))
817 WideCharToMultiByte( CP_UNIXCP, 0, targetpath, -1, target, len, NULL, NULL );
818 for (p = target; *p; p++) if (*p == '\\') *p = '/';
822 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
828 /* first check for a DOS device */
830 if ((dosdev = RtlIsDosDeviceName_U( devname )))
834 memcpy( name, devname + HIWORD(dosdev)/sizeof(WCHAR), LOWORD(dosdev) );
835 name[LOWORD(dosdev)/sizeof(WCHAR)] = 0;
836 path = get_dos_device_path( name );
838 else if (isalphaW(devname[0]) && devname[1] == ':' && !devname[2]) /* drive mapping */
840 path = get_dos_device_path( devname );
842 else SetLastError( ERROR_FILE_NOT_FOUND );
848 TRACE( "creating symlink %s -> %s\n", path, target );
850 if (!symlink( target, path )) ret = TRUE;
851 else FILE_SetDosError();
855 TRACE( "removing symlink %s\n", path );
856 if (!unlink( path )) ret = TRUE;
857 else FILE_SetDosError();
859 HeapFree( GetProcessHeap(), 0, path );
861 HeapFree( GetProcessHeap(), 0, target );
866 /***********************************************************************
867 * DefineDosDeviceA (KERNEL32.@)
869 BOOL WINAPI DefineDosDeviceA(DWORD flags, LPCSTR devname, LPCSTR targetpath)
871 WCHAR *devW, *targetW = NULL;
874 if (!(devW = FILE_name_AtoW( devname, FALSE ))) return FALSE;
875 if (targetpath && !(targetW = FILE_name_AtoW( targetpath, TRUE ))) return FALSE;
876 ret = DefineDosDeviceW(flags, devW, targetW);
877 HeapFree( GetProcessHeap(), 0, targetW );
882 /***********************************************************************
883 * QueryDosDeviceW (KERNEL32.@)
885 * returns array of strings terminated by \0, terminated by \0
887 DWORD WINAPI QueryDosDeviceW( LPCWSTR devname, LPWSTR target, DWORD bufsize )
889 static const WCHAR auxW[] = {'A','U','X',0};
890 static const WCHAR nulW[] = {'N','U','L',0};
891 static const WCHAR prnW[] = {'P','R','N',0};
892 static const WCHAR comW[] = {'C','O','M',0};
893 static const WCHAR lptW[] = {'L','P','T',0};
894 static const WCHAR rootW[] = {'A',':','\\',0};
895 static const WCHAR com0W[] = {'\\','?','?','\\','C','O','M','0',0};
896 static const WCHAR com1W[] = {'\\','D','o','s','D','e','v','i','c','e','s','\\','C','O','M','1',0,0};
897 static const WCHAR lpt1W[] = {'\\','D','o','s','D','e','v','i','c','e','s','\\','L','P','T','1',0,0};
899 UNICODE_STRING nt_name;
900 ANSI_STRING unix_name;
906 SetLastError( ERROR_INSUFFICIENT_BUFFER );
914 DWORD dosdev, ret = 0;
916 if ((dosdev = RtlIsDosDeviceName_U( devname )))
918 memcpy( name, devname + HIWORD(dosdev)/sizeof(WCHAR), LOWORD(dosdev) );
919 name[LOWORD(dosdev)/sizeof(WCHAR)] = 0;
921 else if (devname[0] && devname[1] == ':' && !devname[2])
923 memcpy( name, devname, 3 * sizeof(WCHAR) );
927 SetLastError( ERROR_BAD_PATHNAME );
931 if (!(path = get_dos_device_path( name ))) return 0;
932 link = read_symlink( path );
933 HeapFree( GetProcessHeap(), 0, path );
937 ret = MultiByteToWideChar( CP_UNIXCP, 0, link, -1, target, bufsize );
938 HeapFree( GetProcessHeap(), 0, link );
940 else if (dosdev) /* look for device defaults */
942 if (!strcmpiW( name, auxW ))
944 if (bufsize >= sizeof(com1W)/sizeof(WCHAR))
946 memcpy( target, com1W, sizeof(com1W) );
947 ret = sizeof(com1W)/sizeof(WCHAR);
949 else SetLastError( ERROR_INSUFFICIENT_BUFFER );
952 if (!strcmpiW( name, prnW ))
954 if (bufsize >= sizeof(lpt1W)/sizeof(WCHAR))
956 memcpy( target, lpt1W, sizeof(lpt1W) );
957 ret = sizeof(lpt1W)/sizeof(WCHAR);
959 else SetLastError( ERROR_INSUFFICIENT_BUFFER );
967 strcpyW( nt_buffer + 4, name );
968 RtlInitUnicodeString( &nt_name, nt_buffer );
969 status = wine_nt_to_unix_file_name( &nt_name, &unix_name, FILE_OPEN, TRUE );
970 if (status) SetLastError( RtlNtStatusToDosError(status) );
973 ret = MultiByteToWideChar( CP_UNIXCP, 0, unix_name.Buffer, -1, target, bufsize );
974 RtlFreeAnsiString( &unix_name );
980 if (ret < bufsize) target[ret++] = 0; /* add an extra null */
981 for (p = target; *p; p++) if (*p == '/') *p = '\\';
986 else /* return a list of all devices */
991 if (bufsize <= (sizeof(auxW)+sizeof(nulW)+sizeof(prnW))/sizeof(WCHAR))
993 SetLastError( ERROR_INSUFFICIENT_BUFFER );
997 memcpy( p, auxW, sizeof(auxW) );
998 p += sizeof(auxW) / sizeof(WCHAR);
999 memcpy( p, nulW, sizeof(nulW) );
1000 p += sizeof(nulW) / sizeof(WCHAR);
1001 memcpy( p, prnW, sizeof(prnW) );
1002 p += sizeof(prnW) / sizeof(WCHAR);
1004 strcpyW( nt_buffer, com0W );
1005 RtlInitUnicodeString( &nt_name, nt_buffer );
1007 for (i = 1; i <= 9; i++)
1009 nt_buffer[7] = '0' + i;
1010 if (!wine_nt_to_unix_file_name( &nt_name, &unix_name, FILE_OPEN, TRUE ))
1012 RtlFreeAnsiString( &unix_name );
1013 if (p + 5 >= target + bufsize)
1015 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1024 strcpyW( nt_buffer + 4, lptW );
1025 for (i = 1; i <= 9; i++)
1027 nt_buffer[7] = '0' + i;
1028 if (!wine_nt_to_unix_file_name( &nt_name, &unix_name, FILE_OPEN, TRUE ))
1030 RtlFreeAnsiString( &unix_name );
1031 if (p + 5 >= target + bufsize)
1033 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1043 strcpyW( nt_buffer + 4, rootW );
1044 RtlInitUnicodeString( &nt_name, nt_buffer );
1046 for (i = 0; i < 26; i++)
1048 nt_buffer[4] = 'a' + i;
1049 if (!wine_nt_to_unix_file_name( &nt_name, &unix_name, FILE_OPEN, TRUE ))
1051 RtlFreeAnsiString( &unix_name );
1052 if (p + 3 >= target + bufsize)
1054 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1062 *p++ = 0; /* terminating null */
1068 /***********************************************************************
1069 * QueryDosDeviceA (KERNEL32.@)
1071 * returns array of strings terminated by \0, terminated by \0
1073 DWORD WINAPI QueryDosDeviceA( LPCSTR devname, LPSTR target, DWORD bufsize )
1075 DWORD ret = 0, retW;
1076 WCHAR *devnameW = NULL;
1079 if (devname && !(devnameW = FILE_name_AtoW( devname, FALSE ))) return 0;
1081 targetW = HeapAlloc( GetProcessHeap(),0, bufsize * sizeof(WCHAR) );
1084 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1088 retW = QueryDosDeviceW(devnameW, targetW, bufsize);
1090 ret = FILE_name_WtoA( targetW, retW, target, bufsize );
1092 HeapFree(GetProcessHeap(), 0, targetW);
1097 /***********************************************************************
1098 * GetLogicalDrives (KERNEL32.@)
1100 DWORD WINAPI GetLogicalDrives(void)
1102 const char *config_dir = wine_get_config_dir();
1108 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, strlen(config_dir) + sizeof("/dosdevices/a:") )))
1110 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1113 strcpy( buffer, config_dir );
1114 strcat( buffer, "/dosdevices/a:" );
1115 dev = buffer + strlen(buffer) - 2;
1117 for (i = 0; i < 26; i++)
1120 if (!stat( buffer, &st )) ret |= (1 << i);
1122 HeapFree( GetProcessHeap(), 0, buffer );
1127 /***********************************************************************
1128 * GetLogicalDriveStringsA (KERNEL32.@)
1130 UINT WINAPI GetLogicalDriveStringsA( UINT len, LPSTR buffer )
1132 DWORD drives = GetLogicalDrives();
1135 for (drive = count = 0; drive < 26; drive++) if (drives & (1 << drive)) count++;
1136 if ((count * 4) + 1 > len) return count * 4 + 1;
1138 for (drive = 0; drive < 26; drive++)
1140 if (drives & (1 << drive))
1142 *buffer++ = 'a' + drive;
1153 /***********************************************************************
1154 * GetLogicalDriveStringsW (KERNEL32.@)
1156 UINT WINAPI GetLogicalDriveStringsW( UINT len, LPWSTR buffer )
1158 DWORD drives = GetLogicalDrives();
1161 for (drive = count = 0; drive < 26; drive++) if (drives & (1 << drive)) count++;
1162 if ((count * 4) + 1 > len) return count * 4 + 1;
1164 for (drive = 0; drive < 26; drive++)
1166 if (drives & (1 << drive))
1168 *buffer++ = 'a' + drive;
1179 /***********************************************************************
1180 * GetDriveTypeW (KERNEL32.@)
1182 * Returns the type of the disk drive specified. If root is NULL the
1183 * root of the current directory is used.
1187 * Type of drive (from Win32 SDK):
1189 * DRIVE_UNKNOWN unable to find out anything about the drive
1190 * DRIVE_NO_ROOT_DIR nonexistent root dir
1191 * DRIVE_REMOVABLE the disk can be removed from the machine
1192 * DRIVE_FIXED the disk cannot be removed from the machine
1193 * DRIVE_REMOTE network disk
1194 * DRIVE_CDROM CDROM drive
1195 * DRIVE_RAMDISK virtual disk in RAM
1197 UINT WINAPI GetDriveTypeW(LPCWSTR root) /* [in] String describing drive */
1199 FILE_FS_DEVICE_INFORMATION info;
1205 if (!open_device_root( root, &handle )) return DRIVE_NO_ROOT_DIR;
1207 status = NtQueryVolumeInformationFile( handle, &io, &info, sizeof(info), FileFsDeviceInformation );
1209 if (status != STATUS_SUCCESS)
1211 SetLastError( RtlNtStatusToDosError(status) );
1212 ret = DRIVE_UNKNOWN;
1214 else if ((ret = get_registry_drive_type( root )) == DRIVE_UNKNOWN)
1216 switch (info.DeviceType)
1218 case FILE_DEVICE_CD_ROM_FILE_SYSTEM: ret = DRIVE_CDROM; break;
1219 case FILE_DEVICE_VIRTUAL_DISK: ret = DRIVE_RAMDISK; break;
1220 case FILE_DEVICE_NETWORK_FILE_SYSTEM: ret = DRIVE_REMOTE; break;
1221 case FILE_DEVICE_DISK_FILE_SYSTEM:
1222 if (info.Characteristics & FILE_REMOTE_DEVICE) ret = DRIVE_REMOTE;
1223 else if (info.Characteristics & FILE_REMOVABLE_MEDIA) ret = DRIVE_REMOVABLE;
1224 else ret = DRIVE_FIXED;
1227 ret = DRIVE_UNKNOWN;
1231 TRACE( "%s -> %d\n", debugstr_w(root), ret );
1236 /***********************************************************************
1237 * GetDriveTypeA (KERNEL32.@)
1239 * See GetDriveTypeW.
1241 UINT WINAPI GetDriveTypeA( LPCSTR root )
1243 WCHAR *rootW = NULL;
1245 if (root && !(rootW = FILE_name_AtoW( root, FALSE ))) return DRIVE_NO_ROOT_DIR;
1246 return GetDriveTypeW( rootW );
1250 /***********************************************************************
1251 * GetDiskFreeSpaceExW (KERNEL32.@)
1253 * This function is used to acquire the size of the available and
1254 * total space on a logical volume.
1258 * Zero on failure, nonzero upon success. Use GetLastError to obtain
1259 * detailed error information.
1262 BOOL WINAPI GetDiskFreeSpaceExW( LPCWSTR root, PULARGE_INTEGER avail,
1263 PULARGE_INTEGER total, PULARGE_INTEGER totalfree )
1265 FILE_FS_SIZE_INFORMATION info;
1271 TRACE( "%s,%p,%p,%p\n", debugstr_w(root), avail, total, totalfree );
1273 if (!open_device_root( root, &handle )) return FALSE;
1275 status = NtQueryVolumeInformationFile( handle, &io, &info, sizeof(info), FileFsSizeInformation );
1277 if (status != STATUS_SUCCESS)
1279 SetLastError( RtlNtStatusToDosError(status) );
1283 units = info.SectorsPerAllocationUnit * info.BytesPerSector;
1284 if (total) total->QuadPart = info.TotalAllocationUnits.QuadPart * units;
1285 if (totalfree) totalfree->QuadPart = info.AvailableAllocationUnits.QuadPart * units;
1286 /* FIXME: this one should take quotas into account */
1287 if (avail) avail->QuadPart = info.AvailableAllocationUnits.QuadPart * units;
1292 /***********************************************************************
1293 * GetDiskFreeSpaceExA (KERNEL32.@)
1295 * See GetDiskFreeSpaceExW.
1297 BOOL WINAPI GetDiskFreeSpaceExA( LPCSTR root, PULARGE_INTEGER avail,
1298 PULARGE_INTEGER total, PULARGE_INTEGER totalfree )
1300 WCHAR *rootW = NULL;
1302 if (root && !(rootW = FILE_name_AtoW( root, FALSE ))) return FALSE;
1303 return GetDiskFreeSpaceExW( rootW, avail, total, totalfree );
1307 /***********************************************************************
1308 * GetDiskFreeSpaceW (KERNEL32.@)
1310 BOOL WINAPI GetDiskFreeSpaceW( LPCWSTR root, LPDWORD cluster_sectors,
1311 LPDWORD sector_bytes, LPDWORD free_clusters,
1312 LPDWORD total_clusters )
1314 FILE_FS_SIZE_INFORMATION info;
1320 TRACE( "%s,%p,%p,%p,%p\n", debugstr_w(root),
1321 cluster_sectors, sector_bytes, free_clusters, total_clusters );
1323 if (!open_device_root( root, &handle )) return FALSE;
1325 status = NtQueryVolumeInformationFile( handle, &io, &info, sizeof(info), FileFsSizeInformation );
1327 if (status != STATUS_SUCCESS)
1329 SetLastError( RtlNtStatusToDosError(status) );
1333 units = info.SectorsPerAllocationUnit * info.BytesPerSector;
1335 if( GetVersion() & 0x80000000) { /* win3.x, 9x, ME */
1336 /* cap the size and available at 2GB as per specs */
1337 if (info.TotalAllocationUnits.QuadPart * units > 0x7fffffff) {
1338 info.TotalAllocationUnits.QuadPart = 0x7fffffff / units;
1339 if (info.AvailableAllocationUnits.QuadPart * units > 0x7fffffff)
1340 info.AvailableAllocationUnits.QuadPart = 0x7fffffff / units;
1342 /* nr. of clusters is always <= 65335 */
1343 while( info.TotalAllocationUnits.QuadPart > 65535 ) {
1344 info.TotalAllocationUnits.QuadPart /= 2;
1345 info.AvailableAllocationUnits.QuadPart /= 2;
1346 info.SectorsPerAllocationUnit *= 2;
1350 if (cluster_sectors) *cluster_sectors = info.SectorsPerAllocationUnit;
1351 if (sector_bytes) *sector_bytes = info.BytesPerSector;
1352 if (free_clusters) *free_clusters = info.AvailableAllocationUnits.u.LowPart;
1353 if (total_clusters) *total_clusters = info.TotalAllocationUnits.u.LowPart;
1358 /***********************************************************************
1359 * GetDiskFreeSpaceA (KERNEL32.@)
1361 BOOL WINAPI GetDiskFreeSpaceA( LPCSTR root, LPDWORD cluster_sectors,
1362 LPDWORD sector_bytes, LPDWORD free_clusters,
1363 LPDWORD total_clusters )
1365 WCHAR *rootW = NULL;
1367 if (root && !(rootW = FILE_name_AtoW( root, FALSE ))) return FALSE;
1368 return GetDiskFreeSpaceW( rootW, cluster_sectors, sector_bytes, free_clusters, total_clusters );
1371 /***********************************************************************
1372 * GetVolumePathNameA (KERNEL32.@)
1374 BOOL WINAPI GetVolumePathNameA(LPCSTR filename, LPSTR volumepathname, DWORD buflen)
1376 FIXME("(%s, %p, %d), stub!\n", debugstr_a(filename), volumepathname, buflen);
1377 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1381 /***********************************************************************
1382 * GetVolumePathNameW (KERNEL32.@)
1384 BOOL WINAPI GetVolumePathNameW(LPCWSTR filename, LPWSTR volumepathname, DWORD buflen)
1386 FIXME("(%s, %p, %d), stub!\n", debugstr_w(filename), volumepathname, buflen);
1387 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);