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"
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, 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_SetSuperblockLabel
388 static BOOL VOLUME_SetSuperblockLabel( enum fs_type type, HANDLE handle, const WCHAR *label )
402 SetLastError( ERROR_ACCESS_DENIED );
405 RtlUnicodeToMultiByteN( label_data, sizeof(label_data), &len,
406 label, strlenW(label) * sizeof(WCHAR) );
407 if (len < sizeof(label_data))
408 memset( label_data + len, ' ', sizeof(label_data) - len );
410 return (SetFilePointer( handle, offset, NULL, FILE_BEGIN ) == offset &&
411 WriteFile( handle, label_data, sizeof(label_data), &len, NULL ));
415 /**************************************************************************
416 * VOLUME_GetSuperblockSerial
418 static DWORD VOLUME_GetSuperblockSerial( enum fs_type type, const BYTE *superblock )
426 return GETLONG( superblock, 0x27 );
428 return GETLONG( superblock, 0x33 );
434 sum[0] = sum[1] = sum[2] = sum[3] = 0;
435 for (i = 0; i < 2048; i += 4)
437 /* DON'T optimize this into DWORD !! (breaks overflow) */
438 sum[0] += superblock[i+0];
439 sum[1] += superblock[i+1];
440 sum[2] += superblock[i+2];
441 sum[3] += superblock[i+3];
444 * OK, another braindead one... argh. Just believe it.
445 * Me$$ysoft chose to reverse the serial number in NT4/W2K.
446 * It's true and nobody will ever be able to change it.
448 if (GetVersion() & 0x80000000)
449 return (sum[3] << 24) | (sum[2] << 16) | (sum[1] << 8) | sum[0];
451 return (sum[0] << 24) | (sum[1] << 16) | (sum[2] << 8) | sum[3];
458 /**************************************************************************
459 * VOLUME_GetAudioCDSerial
461 static DWORD VOLUME_GetAudioCDSerial( const CDROM_TOC *toc )
466 for (i = 0; i <= toc->LastTrack - toc->FirstTrack; i++)
467 serial += ((toc->TrackData[i].Address[1] << 16) |
468 (toc->TrackData[i].Address[2] << 8) |
469 toc->TrackData[i].Address[3]);
472 * dwStart, dwEnd collect the beginning and end of the disc respectively, in
474 * There it is collected for correcting the serial when there are less than
477 if (toc->LastTrack - toc->FirstTrack + 1 < 3)
479 DWORD dwStart = FRAME_OF_TOC(toc, toc->FirstTrack);
480 DWORD dwEnd = FRAME_OF_TOC(toc, toc->LastTrack + 1);
481 serial += dwEnd - dwStart;
487 /***********************************************************************
488 * GetVolumeInformationW (KERNEL32.@)
490 BOOL WINAPI GetVolumeInformationW( LPCWSTR root, LPWSTR label, DWORD label_len,
491 DWORD *serial, DWORD *filename_len, DWORD *flags,
492 LPWSTR fsname, DWORD fsname_len )
494 static const WCHAR audiocdW[] = {'A','u','d','i','o',' ','C','D',0};
495 static const WCHAR fatW[] = {'F','A','T',0};
496 static const WCHAR ntfsW[] = {'N','T','F','S',0};
497 static const WCHAR cdfsW[] = {'C','D','F','S',0};
499 WCHAR device[] = {'\\','\\','.','\\','A',':',0};
501 enum fs_type type = FS_UNKNOWN;
505 WCHAR path[MAX_PATH];
506 GetCurrentDirectoryW( MAX_PATH, path );
511 if (!root[0] || root[1] != ':')
513 SetLastError( ERROR_INVALID_NAME );
519 /* try to open the device */
521 handle = CreateFileW( device, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE,
522 NULL, OPEN_EXISTING, 0, 0 );
523 if (handle != INVALID_HANDLE_VALUE)
525 BYTE superblock[SUPERBLOCK_SIZE];
529 /* check for audio CD */
530 /* FIXME: we only check the first track for now */
531 if (DeviceIoControl( handle, IOCTL_CDROM_READ_TOC, NULL, 0, &toc, sizeof(toc), &br, 0 ))
533 if (!(toc.TrackData[0].Control & 0x04)) /* audio track */
535 TRACE( "%s: found audio CD\n", debugstr_w(device) );
536 if (label) lstrcpynW( label, audiocdW, label_len );
537 if (serial) *serial = VOLUME_GetAudioCDSerial( &toc );
538 CloseHandle( handle );
542 type = VOLUME_ReadCDSuperblock( handle, superblock );
546 type = VOLUME_ReadFATSuperblock( handle, superblock );
547 if (type == FS_UNKNOWN) type = VOLUME_ReadCDSuperblock( handle, superblock );
549 CloseHandle( handle );
550 TRACE( "%s: found fs type %d\n", debugstr_w(device), type );
551 if (type == FS_ERROR) return FALSE;
553 if (label && label_len) VOLUME_GetSuperblockLabel( type, superblock, label, label_len );
554 if (serial) *serial = VOLUME_GetSuperblockSerial( type, superblock );
557 else TRACE( "cannot open device %s: err %ld\n", debugstr_w(device), GetLastError() );
559 /* we couldn't open the device, fallback to default strategy */
561 switch(GetDriveTypeW( root ))
564 case DRIVE_NO_ROOT_DIR:
565 SetLastError( ERROR_NOT_READY );
567 case DRIVE_REMOVABLE:
578 if (label && label_len)
580 WCHAR labelW[] = {'A',':','\\','.','w','i','n','d','o','w','s','-','l','a','b','e','l',0};
582 labelW[0] = device[4];
583 handle = CreateFileW( labelW, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
584 OPEN_EXISTING, 0, 0 );
585 if (handle != INVALID_HANDLE_VALUE)
587 char buffer[256], *p;
590 if (!ReadFile( handle, buffer, sizeof(buffer)-1, &size, NULL )) size = 0;
591 CloseHandle( handle );
593 while (p > buffer && (p[-1] == ' ' || p[-1] == '\r' || p[-1] == '\n')) p--;
595 if (!MultiByteToWideChar( CP_UNIXCP, 0, buffer, -1, label, label_len ))
596 label[label_len-1] = 0;
602 WCHAR serialW[] = {'A',':','\\','.','w','i','n','d','o','w','s','-','s','e','r','i','a','l',0};
604 serialW[0] = device[4];
605 handle = CreateFileW( serialW, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
606 OPEN_EXISTING, 0, 0 );
607 if (handle != INVALID_HANDLE_VALUE)
612 if (!ReadFile( handle, buffer, sizeof(buffer)-1, &size, NULL )) size = 0;
613 CloseHandle( handle );
615 *serial = strtoul( buffer, NULL, 16 );
620 fill_fs_info: /* now fill in the information that depends on the file system type */
625 if (fsname) lstrcpynW( fsname, cdfsW, fsname_len );
626 if (filename_len) *filename_len = 221;
627 if (flags) *flags = FILE_READ_ONLY_VOLUME;
631 if (fsname) lstrcpynW( fsname, fatW, fsname_len );
632 if (filename_len) *filename_len = 255;
633 if (flags) *flags = FILE_CASE_PRESERVED_NAMES; /* FIXME */
636 if (fsname) lstrcpynW( fsname, ntfsW, fsname_len );
637 if (filename_len) *filename_len = 255;
638 if (flags) *flags = FILE_CASE_PRESERVED_NAMES;
645 /***********************************************************************
646 * GetVolumeInformationA (KERNEL32.@)
648 BOOL WINAPI GetVolumeInformationA( LPCSTR root, LPSTR label,
649 DWORD label_len, DWORD *serial,
650 DWORD *filename_len, DWORD *flags,
651 LPSTR fsname, DWORD fsname_len )
654 LPWSTR labelW, fsnameW;
657 if (root && !(rootW = FILE_name_AtoW( root, FALSE ))) return FALSE;
659 labelW = label ? HeapAlloc(GetProcessHeap(), 0, label_len * sizeof(WCHAR)) : NULL;
660 fsnameW = fsname ? HeapAlloc(GetProcessHeap(), 0, fsname_len * sizeof(WCHAR)) : NULL;
662 if ((ret = GetVolumeInformationW(rootW, labelW, label_len, serial,
663 filename_len, flags, fsnameW, fsname_len)))
665 if (label) FILE_name_WtoA( labelW, -1, label, label_len );
666 if (fsname) FILE_name_WtoA( fsnameW, -1, fsname, fsname_len );
669 HeapFree( GetProcessHeap(), 0, labelW );
670 HeapFree( GetProcessHeap(), 0, fsnameW );
676 /***********************************************************************
677 * SetVolumeLabelW (KERNEL32.@)
679 BOOL WINAPI SetVolumeLabelW( LPCWSTR root, LPCWSTR label )
681 WCHAR device[] = {'\\','\\','.','\\','A',':',0};
683 enum fs_type type = FS_UNKNOWN;
687 WCHAR path[MAX_PATH];
688 GetCurrentDirectoryW( MAX_PATH, path );
693 if (!root[0] || root[1] != ':')
695 SetLastError( ERROR_INVALID_NAME );
701 /* try to open the device */
703 handle = CreateFileW( device, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE,
704 NULL, OPEN_EXISTING, 0, 0 );
705 if (handle == INVALID_HANDLE_VALUE)
708 handle = CreateFileW( device, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE,
709 NULL, OPEN_EXISTING, 0, 0 );
710 if (handle != INVALID_HANDLE_VALUE)
712 /* device can be read but not written, return error */
713 CloseHandle( handle );
714 SetLastError( ERROR_ACCESS_DENIED );
719 if (handle != INVALID_HANDLE_VALUE)
721 BYTE superblock[SUPERBLOCK_SIZE];
724 type = VOLUME_ReadFATSuperblock( handle, superblock );
725 ret = VOLUME_SetSuperblockLabel( type, handle, label );
726 CloseHandle( handle );
731 TRACE( "cannot open device %s: err %ld\n", debugstr_w(device), GetLastError() );
732 if (GetLastError() == ERROR_ACCESS_DENIED) return FALSE;
735 /* we couldn't open the device, fallback to default strategy */
737 switch(GetDriveTypeW( root ))
740 case DRIVE_NO_ROOT_DIR:
741 SetLastError( ERROR_NOT_READY );
743 case DRIVE_REMOVABLE:
746 WCHAR labelW[] = {'A',':','\\','.','w','i','n','d','o','w','s','-','l','a','b','e','l',0};
748 labelW[0] = device[4];
749 handle = CreateFileW( labelW, GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
750 CREATE_ALWAYS, 0, 0 );
751 if (handle != INVALID_HANDLE_VALUE)
756 if (!WideCharToMultiByte( CP_UNIXCP, 0, label, -1, buffer, sizeof(buffer), NULL, NULL ))
757 buffer[sizeof(buffer)-1] = 0;
758 WriteFile( handle, buffer, strlen(buffer), &size, NULL );
759 CloseHandle( handle );
767 SetLastError( ERROR_ACCESS_DENIED );
773 /***********************************************************************
774 * SetVolumeLabelA (KERNEL32.@)
776 BOOL WINAPI SetVolumeLabelA(LPCSTR root, LPCSTR volname)
778 WCHAR *rootW = NULL, *volnameW = NULL;
781 if (root && !(rootW = FILE_name_AtoW( root, FALSE ))) return FALSE;
782 if (volname && !(volnameW = FILE_name_AtoW( volname, TRUE ))) return FALSE;
783 ret = SetVolumeLabelW( rootW, volnameW );
784 HeapFree( GetProcessHeap(), 0, volnameW );
789 /***********************************************************************
790 * GetVolumeNameForVolumeMountPointW (KERNEL32.@)
792 BOOL WINAPI GetVolumeNameForVolumeMountPointW(LPCWSTR str, LPWSTR dst, DWORD size)
794 FIXME("(%s, %p, %lx): stub\n", debugstr_w(str), dst, size);
799 /***********************************************************************
800 * DefineDosDeviceW (KERNEL32.@)
802 BOOL WINAPI DefineDosDeviceW( DWORD flags, LPCWSTR devname, LPCWSTR targetpath )
806 char *path = NULL, *target, *p;
808 if (!(flags & DDD_REMOVE_DEFINITION))
810 if (!(flags & DDD_RAW_TARGET_PATH))
812 FIXME( "(0x%08lx,%s,%s) DDD_RAW_TARGET_PATH flag not set, not supported yet\n",
813 flags, debugstr_w(devname), debugstr_w(targetpath) );
814 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
818 len = WideCharToMultiByte( CP_UNIXCP, 0, targetpath, -1, NULL, 0, NULL, NULL );
819 if ((target = HeapAlloc( GetProcessHeap(), 0, len )))
821 WideCharToMultiByte( CP_UNIXCP, 0, targetpath, -1, target, len, NULL, NULL );
822 for (p = target; *p; p++) if (*p == '\\') *p = '/';
826 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
832 /* first check for a DOS device */
834 if ((dosdev = RtlIsDosDeviceName_U( devname )))
838 memcpy( name, devname + HIWORD(dosdev)/sizeof(WCHAR), LOWORD(dosdev) );
839 name[LOWORD(dosdev)/sizeof(WCHAR)] = 0;
840 path = get_dos_device_path( name );
842 else if (isalphaW(devname[0]) && devname[1] == ':' && !devname[2]) /* drive mapping */
844 path = get_dos_device_path( devname );
846 else SetLastError( ERROR_FILE_NOT_FOUND );
852 TRACE( "creating symlink %s -> %s\n", path, target );
854 if (!symlink( target, path )) ret = TRUE;
855 else FILE_SetDosError();
859 TRACE( "removing symlink %s\n", path );
860 if (!unlink( path )) ret = TRUE;
861 else FILE_SetDosError();
863 HeapFree( GetProcessHeap(), 0, path );
865 HeapFree( GetProcessHeap(), 0, target );
870 /***********************************************************************
871 * DefineDosDeviceA (KERNEL32.@)
873 BOOL WINAPI DefineDosDeviceA(DWORD flags, LPCSTR devname, LPCSTR targetpath)
875 WCHAR *devW, *targetW = NULL;
878 if (!(devW = FILE_name_AtoW( devname, FALSE ))) return FALSE;
879 if (targetpath && !(targetW = FILE_name_AtoW( targetpath, TRUE ))) return FALSE;
880 ret = DefineDosDeviceW(flags, devW, targetW);
881 HeapFree( GetProcessHeap(), 0, targetW );
886 /***********************************************************************
887 * QueryDosDeviceW (KERNEL32.@)
889 * returns array of strings terminated by \0, terminated by \0
891 DWORD WINAPI QueryDosDeviceW( LPCWSTR devname, LPWSTR target, DWORD bufsize )
893 static const WCHAR auxW[] = {'A','U','X',0};
894 static const WCHAR nulW[] = {'N','U','L',0};
895 static const WCHAR prnW[] = {'P','R','N',0};
896 static const WCHAR comW[] = {'C','O','M',0};
897 static const WCHAR lptW[] = {'L','P','T',0};
898 static const WCHAR rootW[] = {'A',':','\\',0};
899 static const WCHAR com0W[] = {'\\','?','?','\\','C','O','M','0',0};
900 static const WCHAR com1W[] = {'\\','D','o','s','D','e','v','i','c','e','s','\\','C','O','M','1',0,0};
901 static const WCHAR lpt1W[] = {'\\','D','o','s','D','e','v','i','c','e','s','\\','L','P','T','1',0,0};
903 UNICODE_STRING nt_name;
904 ANSI_STRING unix_name;
910 SetLastError( ERROR_INSUFFICIENT_BUFFER );
918 DWORD dosdev, ret = 0;
920 if ((dosdev = RtlIsDosDeviceName_U( devname )))
922 memcpy( name, devname + HIWORD(dosdev)/sizeof(WCHAR), LOWORD(dosdev) );
923 name[LOWORD(dosdev)/sizeof(WCHAR)] = 0;
925 else if (devname[0] && devname[1] == ':' && !devname[2])
927 memcpy( name, devname, 3 * sizeof(WCHAR) );
931 SetLastError( ERROR_BAD_PATHNAME );
935 if (!(path = get_dos_device_path( name ))) return 0;
936 link = read_symlink( path );
937 HeapFree( GetProcessHeap(), 0, path );
941 ret = MultiByteToWideChar( CP_UNIXCP, 0, link, -1, target, bufsize );
942 HeapFree( GetProcessHeap(), 0, link );
944 else if (dosdev) /* look for device defaults */
946 if (!strcmpiW( name, auxW ))
948 if (bufsize >= sizeof(com1W)/sizeof(WCHAR))
950 memcpy( target, com1W, sizeof(com1W) );
951 ret = sizeof(com1W)/sizeof(WCHAR);
953 else SetLastError( ERROR_INSUFFICIENT_BUFFER );
956 if (!strcmpiW( name, prnW ))
958 if (bufsize >= sizeof(lpt1W)/sizeof(WCHAR))
960 memcpy( target, lpt1W, sizeof(lpt1W) );
961 ret = sizeof(lpt1W)/sizeof(WCHAR);
963 else SetLastError( ERROR_INSUFFICIENT_BUFFER );
971 strcpyW( nt_buffer + 4, name );
972 RtlInitUnicodeString( &nt_name, nt_buffer );
973 status = wine_nt_to_unix_file_name( &nt_name, &unix_name, FILE_OPEN, TRUE );
974 if (status) SetLastError( RtlNtStatusToDosError(status) );
977 ret = MultiByteToWideChar( CP_UNIXCP, 0, unix_name.Buffer, -1, target, bufsize );
978 RtlFreeAnsiString( &unix_name );
984 if (ret < bufsize) target[ret++] = 0; /* add an extra null */
985 for (p = target; *p; p++) if (*p == '/') *p = '\\';
990 else /* return a list of all devices */
995 if (bufsize <= (sizeof(auxW)+sizeof(nulW)+sizeof(prnW))/sizeof(WCHAR))
997 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1001 memcpy( p, auxW, sizeof(auxW) );
1002 p += sizeof(auxW) / sizeof(WCHAR);
1003 memcpy( p, nulW, sizeof(nulW) );
1004 p += sizeof(nulW) / sizeof(WCHAR);
1005 memcpy( p, prnW, sizeof(prnW) );
1006 p += sizeof(prnW) / sizeof(WCHAR);
1008 strcpyW( nt_buffer, com0W );
1009 RtlInitUnicodeString( &nt_name, nt_buffer );
1011 for (i = 1; i <= 9; i++)
1013 nt_buffer[7] = '0' + i;
1014 if (!wine_nt_to_unix_file_name( &nt_name, &unix_name, FILE_OPEN, TRUE ))
1016 RtlFreeAnsiString( &unix_name );
1017 if (p + 5 >= target + bufsize)
1019 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1028 strcpyW( nt_buffer + 4, lptW );
1029 for (i = 1; i <= 9; i++)
1031 nt_buffer[7] = '0' + i;
1032 if (!wine_nt_to_unix_file_name( &nt_name, &unix_name, FILE_OPEN, TRUE ))
1034 RtlFreeAnsiString( &unix_name );
1035 if (p + 5 >= target + bufsize)
1037 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1047 strcpyW( nt_buffer + 4, rootW );
1048 RtlInitUnicodeString( &nt_name, nt_buffer );
1050 for (i = 0; i < 26; i++)
1052 nt_buffer[4] = 'a' + i;
1053 if (!wine_nt_to_unix_file_name( &nt_name, &unix_name, FILE_OPEN, TRUE ))
1055 RtlFreeAnsiString( &unix_name );
1056 if (p + 3 >= target + bufsize)
1058 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1066 *p++ = 0; /* terminating null */
1072 /***********************************************************************
1073 * QueryDosDeviceA (KERNEL32.@)
1075 * returns array of strings terminated by \0, terminated by \0
1077 DWORD WINAPI QueryDosDeviceA( LPCSTR devname, LPSTR target, DWORD bufsize )
1079 DWORD ret = 0, retW;
1080 WCHAR *devnameW = NULL;
1083 if (devname && !(devnameW = FILE_name_AtoW( devname, FALSE ))) return 0;
1085 targetW = HeapAlloc( GetProcessHeap(),0, bufsize * sizeof(WCHAR) );
1088 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1092 retW = QueryDosDeviceW(devnameW, targetW, bufsize);
1094 ret = FILE_name_WtoA( targetW, retW, target, bufsize );
1096 HeapFree(GetProcessHeap(), 0, targetW);
1101 /***********************************************************************
1102 * GetLogicalDrives (KERNEL32.@)
1104 DWORD WINAPI GetLogicalDrives(void)
1106 const char *config_dir = wine_get_config_dir();
1112 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, strlen(config_dir) + sizeof("/dosdevices/a:") )))
1114 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1117 strcpy( buffer, config_dir );
1118 strcat( buffer, "/dosdevices/a:" );
1119 dev = buffer + strlen(buffer) - 2;
1121 for (i = 0; i < 26; i++)
1124 if (!stat( buffer, &st )) ret |= (1 << i);
1126 HeapFree( GetProcessHeap(), 0, buffer );
1131 /***********************************************************************
1132 * GetLogicalDriveStringsA (KERNEL32.@)
1134 UINT WINAPI GetLogicalDriveStringsA( UINT len, LPSTR buffer )
1136 DWORD drives = GetLogicalDrives();
1139 for (drive = count = 0; drive < 26; drive++) if (drives & (1 << drive)) count++;
1140 if ((count * 4) + 1 > len) return count * 4 + 1;
1142 for (drive = 0; drive < 26; drive++)
1144 if (drives & (1 << drive))
1146 *buffer++ = 'a' + drive;
1157 /***********************************************************************
1158 * GetLogicalDriveStringsW (KERNEL32.@)
1160 UINT WINAPI GetLogicalDriveStringsW( UINT len, LPWSTR buffer )
1162 DWORD drives = GetLogicalDrives();
1165 for (drive = count = 0; drive < 26; drive++) if (drives & (1 << drive)) count++;
1166 if ((count * 4) + 1 > len) return count * 4 + 1;
1168 for (drive = 0; drive < 26; drive++)
1170 if (drives & (1 << drive))
1172 *buffer++ = 'a' + drive;
1183 /***********************************************************************
1184 * GetDriveTypeW (KERNEL32.@)
1186 * Returns the type of the disk drive specified. If root is NULL the
1187 * root of the current directory is used.
1191 * Type of drive (from Win32 SDK):
1193 * DRIVE_UNKNOWN unable to find out anything about the drive
1194 * DRIVE_NO_ROOT_DIR nonexistent root dir
1195 * DRIVE_REMOVABLE the disk can be removed from the machine
1196 * DRIVE_FIXED the disk cannot be removed from the machine
1197 * DRIVE_REMOTE network disk
1198 * DRIVE_CDROM CDROM drive
1199 * DRIVE_RAMDISK virtual disk in RAM
1201 UINT WINAPI GetDriveTypeW(LPCWSTR root) /* [in] String describing drive */
1203 FILE_FS_DEVICE_INFORMATION info;
1209 if (!open_device_root( root, &handle )) return DRIVE_NO_ROOT_DIR;
1211 status = NtQueryVolumeInformationFile( handle, &io, &info, sizeof(info), FileFsDeviceInformation );
1213 if (status != STATUS_SUCCESS)
1215 SetLastError( RtlNtStatusToDosError(status) );
1216 ret = DRIVE_UNKNOWN;
1218 else if ((ret = get_registry_drive_type( root )) == DRIVE_UNKNOWN)
1220 switch (info.DeviceType)
1222 case FILE_DEVICE_CD_ROM_FILE_SYSTEM: ret = DRIVE_CDROM; break;
1223 case FILE_DEVICE_VIRTUAL_DISK: ret = DRIVE_RAMDISK; break;
1224 case FILE_DEVICE_NETWORK_FILE_SYSTEM: ret = DRIVE_REMOTE; break;
1225 case FILE_DEVICE_DISK_FILE_SYSTEM:
1226 if (info.Characteristics & FILE_REMOTE_DEVICE) ret = DRIVE_REMOTE;
1227 else if (info.Characteristics & FILE_REMOVABLE_MEDIA) ret = DRIVE_REMOVABLE;
1228 else ret = DRIVE_FIXED;
1231 ret = DRIVE_UNKNOWN;
1235 TRACE( "%s -> %d\n", debugstr_w(root), ret );
1240 /***********************************************************************
1241 * GetDriveTypeA (KERNEL32.@)
1243 UINT WINAPI GetDriveTypeA( LPCSTR root )
1245 WCHAR *rootW = NULL;
1247 if (root && !(rootW = FILE_name_AtoW( root, FALSE ))) return DRIVE_NO_ROOT_DIR;
1248 return GetDriveTypeW( rootW );
1252 /***********************************************************************
1253 * GetDiskFreeSpaceExW (KERNEL32.@)
1255 * This function is used to acquire the size of the available and
1256 * total space on a logical volume.
1260 * Zero on failure, nonzero upon success. Use GetLastError to obtain
1261 * detailed error information.
1264 BOOL WINAPI GetDiskFreeSpaceExW( LPCWSTR root, PULARGE_INTEGER avail,
1265 PULARGE_INTEGER total, PULARGE_INTEGER totalfree )
1267 FILE_FS_SIZE_INFORMATION info;
1273 TRACE( "%s,%p,%p,%p\n", debugstr_w(root), avail, total, totalfree );
1275 if (!open_device_root( root, &handle )) return FALSE;
1277 status = NtQueryVolumeInformationFile( handle, &io, &info, sizeof(info), FileFsSizeInformation );
1279 if (status != STATUS_SUCCESS)
1281 SetLastError( RtlNtStatusToDosError(status) );
1285 units = info.SectorsPerAllocationUnit * info.BytesPerSector;
1286 if (total) total->QuadPart = info.TotalAllocationUnits.QuadPart * units;
1287 if (totalfree) totalfree->QuadPart = info.AvailableAllocationUnits.QuadPart * units;
1288 /* FIXME: this one should take quotas into account */
1289 if (avail) avail->QuadPart = info.AvailableAllocationUnits.QuadPart * units;
1294 /***********************************************************************
1295 * GetDiskFreeSpaceExA (KERNEL32.@)
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 /* cap the size and available at 2GB as per specs */
1336 if (info.AvailableAllocationUnits.QuadPart * units > 0x7fffffff)
1337 info.AvailableAllocationUnits.QuadPart = 0x7fffffff / units;
1338 if (info.TotalAllocationUnits.QuadPart * units > 0x7fffffff)
1339 info.TotalAllocationUnits.QuadPart = 0x7fffffff / units;
1341 if (cluster_sectors) *cluster_sectors = info.SectorsPerAllocationUnit;
1342 if (sector_bytes) *sector_bytes = info.BytesPerSector;
1343 if (free_clusters) *free_clusters = info.AvailableAllocationUnits.u.LowPart;
1344 if (total_clusters) *total_clusters = info.TotalAllocationUnits.u.LowPart;
1349 /***********************************************************************
1350 * GetDiskFreeSpaceA (KERNEL32.@)
1352 BOOL WINAPI GetDiskFreeSpaceA( LPCSTR root, LPDWORD cluster_sectors,
1353 LPDWORD sector_bytes, LPDWORD free_clusters,
1354 LPDWORD total_clusters )
1356 WCHAR *rootW = NULL;
1358 if (root && !(rootW = FILE_name_AtoW( root, FALSE ))) return FALSE;
1359 return GetDiskFreeSpaceW( rootW, cluster_sectors, sector_bytes, free_clusters, total_clusters );
1362 /***********************************************************************
1363 * GetVolumePathNameA (KERNEL32.@)
1365 BOOL WINAPI GetVolumePathNameA(LPCSTR filename, LPSTR volumepathname, DWORD buflen)
1367 FIXME("(%s, %p, %ld), stub!\n", debugstr_a(filename), volumepathname, buflen);
1368 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1372 /***********************************************************************
1373 * GetVolumePathNameW (KERNEL32.@)
1375 BOOL WINAPI GetVolumePathNameW(LPCWSTR filename, LPWSTR volumepathname, DWORD buflen)
1377 FIXME("(%s, %p, %ld), stub!\n", debugstr_w(filename), volumepathname, buflen);
1378 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);