2 * Volume management functions
4 * Copyright 1993 Erik Bos
5 * Copyright 1996, 2004 Alexandre Julliard
6 * Copyright 1999 Petr Tomasek
7 * Copyright 2000 Andreas Mohr
8 * Copyright 2003 Eric Pouech
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include "wine/port.h"
41 #include "kernel_private.h"
43 #include "wine/library.h"
44 #include "wine/unicode.h"
45 #include "wine/debug.h"
47 WINE_DEFAULT_DEBUG_CHANNEL(volume);
49 #define SUPERBLOCK_SIZE 2048
51 #define CDFRAMES_PERSEC 75
52 #define CDFRAMES_PERMIN (CDFRAMES_PERSEC * 60)
53 #define FRAME_OF_ADDR(a) ((a)[1] * CDFRAMES_PERMIN + (a)[2] * CDFRAMES_PERSEC + (a)[3])
54 #define FRAME_OF_TOC(toc, idx) FRAME_OF_ADDR((toc)->TrackData[(idx) - (toc)->FirstTrack].Address)
56 #define GETWORD(buf,off) MAKEWORD(buf[(off)],buf[(off+1)])
57 #define GETLONG(buf,off) MAKELONG(GETWORD(buf,off),GETWORD(buf,off+2))
61 FS_ERROR, /* error accessing the device */
62 FS_UNKNOWN, /* unknown file system */
69 /* return default device to use for serial ports */
70 /* result should not be more than 16 characters long */
71 static BOOL get_default_com_device( char *buffer, int num )
73 if (!num || num > 9) return FALSE;
75 sprintf( buffer, "/dev/ttyS%d", num - 1 );
78 FIXME( "no known default for device com%d\n", num );
83 /* return default device to use for parallel ports */
84 /* result should not be more than 16 characters long */
85 static BOOL get_default_lpt_device( char *buffer, int num )
87 if (!num || num > 9) return FALSE;
89 sprintf( buffer, "/dev/lp%d", num - 1 );
92 FIXME( "no known default for device lpt%d\n", num );
97 /* read a Unix symlink; returned buffer must be freed by caller */
98 static char *read_symlink( const char *path )
105 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, size )))
107 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
110 ret = readlink( path, buffer, size );
114 HeapFree( GetProcessHeap(), 0, buffer );
122 HeapFree( GetProcessHeap(), 0, buffer );
127 /* get the path of a dos device symlink in the $WINEPREFIX/dosdevices directory */
128 static char *get_dos_device_path( LPCWSTR name )
130 const char *config_dir = wine_get_config_dir();
134 if (!(buffer = HeapAlloc( GetProcessHeap(), 0,
135 strlen(config_dir) + sizeof("/dosdevices/") + 5 )))
137 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
140 strcpy( buffer, config_dir );
141 strcat( buffer, "/dosdevices/" );
142 dev = buffer + strlen(buffer);
143 /* no codepage conversion, DOS device names are ASCII anyway */
144 for (i = 0; i < 5; i++)
145 if (!(dev[i] = (char)tolowerW(name[i]))) break;
151 /* create symlinks for the DOS drives; helper for VOLUME_CreateDevices */
152 static int create_drives(void)
154 WCHAR name[3], rootW[MAX_PATHNAME_LEN];
157 for (i = 0; i < 26; i++)
159 const char *root = DRIVE_GetRoot( i );
164 if (MultiByteToWideChar( CP_UNIXCP, 0, root, -1, rootW, MAX_PATHNAME_LEN ) &&
165 DefineDosDeviceW( DDD_RAW_TARGET_PATH, name, rootW ))
167 MESSAGE( "Created symlink %s/dosdevices/%c: -> %s\n", wine_get_config_dir(), 'a' + i, root );
175 /***********************************************************************
176 * VOLUME_CreateDevices
178 * Create the device files for the new device naming scheme.
179 * Should go away after a transition period.
181 void VOLUME_CreateDevices(void)
183 const char *config_dir = wine_get_config_dir();
187 if (!(buffer = HeapAlloc( GetProcessHeap(), 0,
188 strlen(config_dir) + sizeof("/dosdevices") + 3 )))
191 strcpy( buffer, config_dir );
192 strcat( buffer, "/dosdevices" );
194 if (!mkdir( buffer, 0777 )) /* we created it, so now create the devices */
198 OBJECT_ATTRIBUTES attr;
199 UNICODE_STRING nameW;
202 WCHAR com[5] = {'C','O','M','1',0};
203 WCHAR lpt[5] = {'L','P','T','1',0};
205 static const WCHAR serialportsW[] = {'M','a','c','h','i','n','e','\\',
206 'S','o','f','t','w','a','r','e','\\',
207 'W','i','n','e','\\','W','i','n','e','\\',
208 'C','o','n','f','i','g','\\',
209 'S','e','r','i','a','l','P','o','r','t','s',0};
210 static const WCHAR parallelportsW[] = {'M','a','c','h','i','n','e','\\',
211 'S','o','f','t','w','a','r','e','\\',
212 'W','i','n','e','\\','W','i','n','e','\\',
213 'C','o','n','f','i','g','\\',
214 'P','a','r','a','l','l','e','l','P','o','r','t','s',0};
216 attr.Length = sizeof(attr);
217 attr.RootDirectory = 0;
218 attr.ObjectName = &nameW;
220 attr.SecurityDescriptor = NULL;
221 attr.SecurityQualityOfService = NULL;
222 RtlInitUnicodeString( &nameW, serialportsW );
224 if (!NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr ))
226 RtlInitUnicodeString( &nameW, com );
227 for (i = 1; i <= 9; i++)
230 if (!NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation,
231 tmp, sizeof(tmp), &dummy ))
233 devnameW = (WCHAR *)((KEY_VALUE_PARTIAL_INFORMATION *)tmp)->Data;
234 if ((p = strchrW( devnameW, ',' ))) *p = 0;
235 if (DefineDosDeviceW( DDD_RAW_TARGET_PATH, com, devnameW ))
238 WideCharToMultiByte(CP_UNIXCP, 0, devnameW, -1,
239 devname, sizeof(devname), NULL, NULL);
240 MESSAGE( "Created symlink %s/dosdevices/com%d -> %s\n", config_dir, i, devname );
248 RtlInitUnicodeString( &nameW, parallelportsW );
249 if (!NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr ))
251 RtlInitUnicodeString( &nameW, lpt );
252 for (i = 1; i <= 9; i++)
255 if (!NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation,
256 tmp, sizeof(tmp), &dummy ))
258 devnameW = (WCHAR *)((KEY_VALUE_PARTIAL_INFORMATION *)tmp)->Data;
259 if ((p = strchrW( devnameW, ',' ))) *p = 0;
260 if (DefineDosDeviceW( DDD_RAW_TARGET_PATH, lpt, devnameW ))
263 WideCharToMultiByte(CP_UNIXCP, 0, devnameW, -1,
264 devname, sizeof(devname), NULL, NULL);
265 MESSAGE( "Created symlink %s/dosdevices/lpt%d -> %s\n", config_dir, i, devname );
272 count += create_drives();
279 /* it is possible that the serial/parallel devices have been created but */
280 /* not the drives; check for at least one drive symlink to catch that case */
281 strcat( buffer, "/a:" );
282 for (i = 0; i < 26; i++)
284 buffer[strlen(buffer)-2] = 'a' + i;
285 if (!lstat( buffer, &st )) break;
287 if (i == 26) count += create_drives();
291 MESSAGE( "\nYou can now remove the [SerialPorts] and [ParallelPorts] sections\n"
292 "in your configuration file, as well as the \"Path=\" definitions in\n"
293 "the drive sections, they are replaced by the above symlinks.\n\n" );
295 HeapFree( GetProcessHeap(), 0, buffer );
299 /******************************************************************
302 HANDLE VOLUME_OpenDevice( LPCWSTR name, DWORD access, DWORD sharing,
303 LPSECURITY_ATTRIBUTES sa, DWORD attributes )
308 if (!(buffer = get_dos_device_path( name ))) return 0;
309 dev = strrchr( buffer, '/' ) + 1;
313 TRACE("trying %s\n", buffer );
315 ret = FILE_CreateFile( buffer, access, sharing, sa, OPEN_EXISTING, 0, 0, TRUE, DRIVE_FIXED );
316 if (ret || GetLastError() != ERROR_FILE_NOT_FOUND) break;
319 /* now try some defaults for it */
320 if (!strcmp( dev, "aux" ))
322 strcpy( dev, "com1" );
325 if (!strcmp( dev, "prn" ))
327 strcpy( dev, "lpt1" );
330 if (!strcmp( dev, "nul" ))
332 strcpy( buffer, "/dev/null" );
333 dev = NULL; /* last try */
336 if (!strncmp( dev, "com", 3 ) && get_default_com_device( buffer, dev[3] - '0' ))
338 dev = NULL; /* last try */
341 if (!strncmp( dev, "lpt", 3 ) && get_default_lpt_device( buffer, dev[3] - '0' ))
343 dev = NULL; /* last try */
349 if (!ret) ERR( "could not open device %s err %ld\n", debugstr_w(name), GetLastError() );
350 HeapFree( GetProcessHeap(), 0, buffer );
355 /******************************************************************
356 * VOLUME_FindCdRomDataBestVoldesc
358 static DWORD VOLUME_FindCdRomDataBestVoldesc( HANDLE handle )
360 BYTE cur_vd_type, max_vd_type = 0;
362 DWORD size, offs, best_offs = 0, extra_offs = 0;
364 for (offs = 0x8000; offs <= 0x9800; offs += 0x800)
366 /* if 'CDROM' occurs at position 8, this is a pre-iso9660 cd, and
367 * the volume label is displaced forward by 8
369 if (SetFilePointer( handle, offs, NULL, FILE_BEGIN ) != offs) break;
370 if (!ReadFile( handle, buffer, sizeof(buffer), &size, NULL )) break;
371 if (size != sizeof(buffer)) break;
372 /* check for non-ISO9660 signature */
373 if (!memcmp( buffer + 11, "ROM", 3 )) extra_offs = 8;
374 cur_vd_type = buffer[extra_offs];
375 if (cur_vd_type == 0xff) /* voldesc set terminator */
377 if (cur_vd_type > max_vd_type)
379 max_vd_type = cur_vd_type;
380 best_offs = offs + extra_offs;
387 /***********************************************************************
388 * VOLUME_ReadFATSuperblock
390 static enum fs_type VOLUME_ReadFATSuperblock( HANDLE handle, BYTE *buff )
394 /* try a fixed disk, with a FAT partition */
395 if (SetFilePointer( handle, 0, NULL, FILE_BEGIN ) != 0 ||
396 !ReadFile( handle, buff, SUPERBLOCK_SIZE, &size, NULL ) ||
397 size != SUPERBLOCK_SIZE)
400 if (buff[0] == 0xE9 || (buff[0] == 0xEB && buff[2] == 0x90))
402 /* guess which type of FAT we have */
403 unsigned int sz, nsect, nclust;
404 sz = GETWORD(buff, 0x16);
405 if (!sz) sz = GETLONG(buff, 0x24);
406 nsect = GETWORD(buff, 0x13);
407 if (!nsect) nsect = GETLONG(buff, 0x20);
408 nsect -= GETWORD(buff, 0x0e) + buff[0x10] * sz +
409 (GETWORD(buff, 0x11) * 32 + (GETWORD(buff, 0x0b) - 1)) / GETWORD(buff, 0x0b);
410 nclust = nsect / buff[0x0d];
414 if (buff[0x26] == 0x29 && !memcmp(buff+0x36, "FAT", 3))
416 /* FIXME: do really all FAT have their name beginning with
417 * "FAT" ? (At least FAT12, FAT16 and FAT32 have :)
422 else if (!memcmp(buff+0x52, "FAT", 3)) return FS_FAT32;
428 /***********************************************************************
429 * VOLUME_ReadCDSuperblock
431 static enum fs_type VOLUME_ReadCDSuperblock( HANDLE handle, BYTE *buff )
433 DWORD size, offs = VOLUME_FindCdRomDataBestVoldesc( handle );
435 if (!offs) return FS_UNKNOWN;
437 if (SetFilePointer( handle, offs, NULL, FILE_BEGIN ) != offs ||
438 !ReadFile( handle, buff, SUPERBLOCK_SIZE, &size, NULL ) ||
439 size != SUPERBLOCK_SIZE)
442 /* check for iso9660 present */
443 if (!memcmp(&buff[1], "CD001", 5)) return FS_ISO9660;
448 /**************************************************************************
449 * VOLUME_GetSuperblockLabel
451 static void VOLUME_GetSuperblockLabel( enum fs_type type, const BYTE *superblock,
452 WCHAR *label, DWORD len )
454 const BYTE *label_ptr = NULL;
464 label_ptr = superblock + 0x2b;
468 label_ptr = superblock + 0x47;
473 BYTE ver = superblock[0x5a];
475 if (superblock[0x58] == 0x25 && superblock[0x59] == 0x2f && /* Unicode ID */
476 ((ver == 0x40) || (ver == 0x43) || (ver == 0x45)))
477 { /* yippee, unicode */
480 if (len > 17) len = 17;
481 for (i = 0; i < len-1; i++)
482 label[i] = (superblock[40+2*i] << 8) | superblock[41+2*i];
484 while (i && label[i-1] == ' ') label[--i] = 0;
487 label_ptr = superblock + 40;
492 if (label_len) RtlMultiByteToUnicodeN( label, (len-1) * sizeof(WCHAR),
493 &label_len, label_ptr, label_len );
494 label_len /= sizeof(WCHAR);
495 label[label_len] = 0;
496 while (label_len && label[label_len-1] == ' ') label[--label_len] = 0;
500 /**************************************************************************
501 * VOLUME_SetSuperblockLabel
503 static BOOL VOLUME_SetSuperblockLabel( enum fs_type type, HANDLE handle, const WCHAR *label )
517 SetLastError( ERROR_ACCESS_DENIED );
520 RtlUnicodeToMultiByteN( label_data, sizeof(label_data), &len,
521 label, strlenW(label) * sizeof(WCHAR) );
522 if (len < sizeof(label_data))
523 memset( label_data + len, ' ', sizeof(label_data) - len );
525 return (SetFilePointer( handle, offset, NULL, FILE_BEGIN ) == offset &&
526 WriteFile( handle, label_data, sizeof(label_data), &len, NULL ));
530 /**************************************************************************
531 * VOLUME_GetSuperblockSerial
533 static DWORD VOLUME_GetSuperblockSerial( enum fs_type type, const BYTE *superblock )
541 return GETLONG( superblock, 0x27 );
543 return GETLONG( superblock, 0x33 );
549 sum[0] = sum[1] = sum[2] = sum[3] = 0;
550 for (i = 0; i < 2048; i += 4)
552 /* DON'T optimize this into DWORD !! (breaks overflow) */
553 sum[0] += superblock[i+0];
554 sum[1] += superblock[i+1];
555 sum[2] += superblock[i+2];
556 sum[3] += superblock[i+3];
559 * OK, another braindead one... argh. Just believe it.
560 * Me$$ysoft chose to reverse the serial number in NT4/W2K.
561 * It's true and nobody will ever be able to change it.
563 if (GetVersion() & 0x80000000)
564 return (sum[3] << 24) | (sum[2] << 16) | (sum[1] << 8) | sum[0];
566 return (sum[0] << 24) | (sum[1] << 16) | (sum[2] << 8) | sum[3];
573 /**************************************************************************
574 * VOLUME_GetAudioCDSerial
576 static DWORD VOLUME_GetAudioCDSerial( const CDROM_TOC *toc )
581 for (i = 0; i <= toc->LastTrack - toc->FirstTrack; i++)
582 serial += ((toc->TrackData[i].Address[1] << 16) |
583 (toc->TrackData[i].Address[2] << 8) |
584 toc->TrackData[i].Address[3]);
587 * dwStart, dwEnd collect the beginning and end of the disc respectively, in
589 * There it is collected for correcting the serial when there are less than
592 if (toc->LastTrack - toc->FirstTrack + 1 < 3)
594 DWORD dwStart = FRAME_OF_TOC(toc, toc->FirstTrack);
595 DWORD dwEnd = FRAME_OF_TOC(toc, toc->LastTrack + 1);
596 serial += dwEnd - dwStart;
602 /***********************************************************************
603 * GetVolumeInformationW (KERNEL32.@)
605 BOOL WINAPI GetVolumeInformationW( LPCWSTR root, LPWSTR label, DWORD label_len,
606 DWORD *serial, DWORD *filename_len, DWORD *flags,
607 LPWSTR fsname, DWORD fsname_len )
609 static const WCHAR audiocdW[] = {'A','u','d','i','o',' ','C','D',0};
610 static const WCHAR fatW[] = {'F','A','T',0};
611 static const WCHAR cdfsW[] = {'C','D','F','S',0};
613 WCHAR device[] = {'\\','\\','.','\\','A',':',0};
615 enum fs_type type = FS_UNKNOWN;
619 WCHAR path[MAX_PATH];
620 GetCurrentDirectoryW( MAX_PATH, path );
625 if (!root[0] || root[1] != ':')
627 SetLastError( ERROR_INVALID_NAME );
633 /* try to open the device */
635 handle = CreateFileW( device, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE,
636 NULL, OPEN_EXISTING, 0, 0 );
637 if (handle != INVALID_HANDLE_VALUE)
639 BYTE superblock[SUPERBLOCK_SIZE];
643 /* check for audio CD */
644 /* FIXME: we only check the first track for now */
645 if (DeviceIoControl( handle, IOCTL_CDROM_READ_TOC, NULL, 0, &toc, sizeof(toc), &br, 0 ))
647 if (!(toc.TrackData[0].Control & 0x04)) /* audio track */
649 TRACE( "%s: found audio CD\n", debugstr_w(device) );
650 if (label) lstrcpynW( label, audiocdW, label_len );
651 if (serial) *serial = VOLUME_GetAudioCDSerial( &toc );
652 CloseHandle( handle );
656 type = VOLUME_ReadCDSuperblock( handle, superblock );
660 type = VOLUME_ReadFATSuperblock( handle, superblock );
661 if (type == FS_UNKNOWN) type = VOLUME_ReadCDSuperblock( handle, superblock );
663 CloseHandle( handle );
664 TRACE( "%s: found fs type %d\n", debugstr_w(device), type );
665 if (type == FS_ERROR) return FALSE;
667 if (label && label_len) VOLUME_GetSuperblockLabel( type, superblock, label, label_len );
668 if (serial) *serial = VOLUME_GetSuperblockSerial( type, superblock );
673 TRACE( "cannot open device %s: err %ld\n", debugstr_w(device), GetLastError() );
674 if (GetLastError() != ERROR_ACCESS_DENIED) return FALSE;
677 /* we couldn't open the device, fallback to default strategy */
679 switch(GetDriveTypeW( root ))
682 case DRIVE_NO_ROOT_DIR:
683 SetLastError( ERROR_NOT_READY );
685 case DRIVE_REMOVABLE:
696 if (label && label_len)
698 WCHAR labelW[] = {'A',':','\\','.','w','i','n','d','o','w','s','-','l','a','b','e','l',0};
700 labelW[0] = device[4];
701 handle = CreateFileW( labelW, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
702 OPEN_EXISTING, 0, 0 );
703 if (handle != INVALID_HANDLE_VALUE)
705 char buffer[256], *p;
708 if (!ReadFile( handle, buffer, sizeof(buffer)-1, &size, NULL )) size = 0;
709 CloseHandle( handle );
711 while (p > buffer && (p[-1] == ' ' || p[-1] == '\r' || p[-1] == '\n')) p--;
713 if (!MultiByteToWideChar( CP_UNIXCP, 0, buffer, -1, label, label_len ))
714 label[label_len-1] = 0;
720 WCHAR serialW[] = {'A',':','\\','.','w','i','n','d','o','w','s','-','s','e','r','i','a','l',0};
722 serialW[0] = device[4];
723 handle = CreateFileW( serialW, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
724 OPEN_EXISTING, 0, 0 );
725 if (handle != INVALID_HANDLE_VALUE)
730 if (!ReadFile( handle, buffer, sizeof(buffer)-1, &size, NULL )) size = 0;
731 CloseHandle( handle );
733 *serial = strtoul( buffer, NULL, 16 );
738 fill_fs_info: /* now fill in the information that depends on the file system type */
743 if (fsname) lstrcpynW( fsname, cdfsW, fsname_len );
744 if (filename_len) *filename_len = 221;
745 if (flags) *flags = FILE_READ_ONLY_VOLUME;
749 default: /* default to FAT file system (FIXME) */
750 if (fsname) lstrcpynW( fsname, fatW, fsname_len );
751 if (filename_len) *filename_len = 255;
752 if (flags) *flags = FILE_CASE_PRESERVED_NAMES; /* FIXME */
759 /***********************************************************************
760 * GetVolumeInformationA (KERNEL32.@)
762 BOOL WINAPI GetVolumeInformationA( LPCSTR root, LPSTR label,
763 DWORD label_len, DWORD *serial,
764 DWORD *filename_len, DWORD *flags,
765 LPSTR fsname, DWORD fsname_len )
767 UNICODE_STRING rootW;
768 LPWSTR labelW, fsnameW;
771 if (root) RtlCreateUnicodeStringFromAsciiz(&rootW, root);
772 else rootW.Buffer = NULL;
773 labelW = label ? HeapAlloc(GetProcessHeap(), 0, label_len * sizeof(WCHAR)) : NULL;
774 fsnameW = fsname ? HeapAlloc(GetProcessHeap(), 0, fsname_len * sizeof(WCHAR)) : NULL;
776 if ((ret = GetVolumeInformationW(rootW.Buffer, labelW, label_len, serial,
777 filename_len, flags, fsnameW, fsname_len)))
779 if (label) WideCharToMultiByte(CP_ACP, 0, labelW, -1, label, label_len, NULL, NULL);
780 if (fsname) WideCharToMultiByte(CP_ACP, 0, fsnameW, -1, fsname, fsname_len, NULL, NULL);
783 RtlFreeUnicodeString(&rootW);
784 if (labelW) HeapFree( GetProcessHeap(), 0, labelW );
785 if (fsnameW) HeapFree( GetProcessHeap(), 0, fsnameW );
791 /***********************************************************************
792 * SetVolumeLabelW (KERNEL32.@)
794 BOOL WINAPI SetVolumeLabelW( LPCWSTR root, LPCWSTR label )
796 WCHAR device[] = {'\\','\\','.','\\','A',':',0};
798 enum fs_type type = FS_UNKNOWN;
802 WCHAR path[MAX_PATH];
803 GetCurrentDirectoryW( MAX_PATH, path );
808 if (!root[0] || root[1] != ':')
810 SetLastError( ERROR_INVALID_NAME );
816 /* try to open the device */
818 handle = CreateFileW( device, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE,
819 NULL, OPEN_EXISTING, 0, 0 );
820 if (handle == INVALID_HANDLE_VALUE)
823 handle = CreateFileW( device, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE,
824 NULL, OPEN_EXISTING, 0, 0 );
825 if (handle != INVALID_HANDLE_VALUE)
827 /* device can be read but not written, return error */
828 CloseHandle( handle );
829 SetLastError( ERROR_ACCESS_DENIED );
834 if (handle != INVALID_HANDLE_VALUE)
836 BYTE superblock[SUPERBLOCK_SIZE];
839 type = VOLUME_ReadFATSuperblock( handle, superblock );
840 ret = VOLUME_SetSuperblockLabel( type, handle, label );
841 CloseHandle( handle );
846 TRACE( "cannot open device %s: err %ld\n", debugstr_w(device), GetLastError() );
847 if (GetLastError() != ERROR_ACCESS_DENIED) return FALSE;
850 /* we couldn't open the device, fallback to default strategy */
852 switch(GetDriveTypeW( root ))
855 case DRIVE_NO_ROOT_DIR:
856 SetLastError( ERROR_NOT_READY );
858 case DRIVE_REMOVABLE:
861 WCHAR labelW[] = {'A',':','\\','.','w','i','n','d','o','w','s','-','l','a','b','e','l',0};
863 labelW[0] = device[4];
864 handle = CreateFileW( labelW, GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
865 CREATE_ALWAYS, 0, 0 );
866 if (handle != INVALID_HANDLE_VALUE)
871 if (!WideCharToMultiByte( CP_UNIXCP, 0, label, -1, buffer, sizeof(buffer), NULL, NULL ))
872 buffer[sizeof(buffer)-1] = 0;
873 WriteFile( handle, buffer, strlen(buffer), &size, NULL );
874 CloseHandle( handle );
882 SetLastError( ERROR_ACCESS_DENIED );
888 /***********************************************************************
889 * SetVolumeLabelA (KERNEL32.@)
891 BOOL WINAPI SetVolumeLabelA(LPCSTR root, LPCSTR volname)
893 UNICODE_STRING rootW, volnameW;
896 if (root) RtlCreateUnicodeStringFromAsciiz(&rootW, root);
897 else rootW.Buffer = NULL;
898 if (volname) RtlCreateUnicodeStringFromAsciiz(&volnameW, volname);
899 else volnameW.Buffer = NULL;
901 ret = SetVolumeLabelW( rootW.Buffer, volnameW.Buffer );
903 RtlFreeUnicodeString(&rootW);
904 RtlFreeUnicodeString(&volnameW);
909 /***********************************************************************
910 * GetVolumeNameForVolumeMountPointW (KERNEL32.@)
912 BOOL WINAPI GetVolumeNameForVolumeMountPointW(LPCWSTR str, LPWSTR dst, DWORD size)
914 FIXME("(%s, %p, %lx): stub\n", debugstr_w(str), dst, size);
919 /***********************************************************************
920 * DefineDosDeviceW (KERNEL32.@)
922 BOOL WINAPI DefineDosDeviceW( DWORD flags, LPCWSTR devname, LPCWSTR targetpath )
926 char *path = NULL, *target, *p;
928 if (!(flags & DDD_RAW_TARGET_PATH))
930 FIXME( "(0x%08lx,%s,%s) DDD_RAW_TARGET_PATH flag not set, not supported yet\n",
931 flags, debugstr_w(devname), debugstr_w(targetpath) );
932 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
936 len = WideCharToMultiByte( CP_UNIXCP, 0, targetpath, -1, NULL, 0, NULL, NULL );
937 if ((target = HeapAlloc( GetProcessHeap(), 0, len )))
939 WideCharToMultiByte( CP_UNIXCP, 0, targetpath, -1, target, len, NULL, NULL );
940 for (p = target; *p; p++) if (*p == '\\') *p = '/';
944 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
948 /* first check for a DOS device */
950 if ((dosdev = RtlIsDosDeviceName_U( devname )))
954 memcpy( name, devname + HIWORD(dosdev)/sizeof(WCHAR), LOWORD(dosdev) );
955 name[LOWORD(dosdev)/sizeof(WCHAR)] = 0;
956 path = get_dos_device_path( name );
958 else if (isalphaW(devname[0]) && devname[1] == ':' && !devname[2]) /* drive mapping */
960 path = get_dos_device_path( devname );
962 else SetLastError( ERROR_FILE_NOT_FOUND );
966 TRACE( "creating symlink %s -> %s\n", path, target );
968 if (!symlink( target, path )) ret = TRUE;
969 else FILE_SetDosError();
970 HeapFree( GetProcessHeap(), 0, path );
972 HeapFree( GetProcessHeap(), 0, target );
977 /***********************************************************************
978 * DefineDosDeviceA (KERNEL32.@)
980 BOOL WINAPI DefineDosDeviceA(DWORD flags, LPCSTR devname, LPCSTR targetpath)
985 if (!RtlCreateUnicodeStringFromAsciiz(&d, devname))
987 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
990 if (!RtlCreateUnicodeStringFromAsciiz(&t, targetpath))
992 RtlFreeUnicodeString(&d);
993 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
996 ret = DefineDosDeviceW(flags, d.Buffer, t.Buffer);
997 RtlFreeUnicodeString(&d);
998 RtlFreeUnicodeString(&t);
1003 /***********************************************************************
1004 * QueryDosDeviceW (KERNEL32.@)
1006 * returns array of strings terminated by \0, terminated by \0
1008 DWORD WINAPI QueryDosDeviceW( LPCWSTR devname, LPWSTR target, DWORD bufsize )
1010 static const WCHAR auxW[] = {'A','U','X',0};
1011 static const WCHAR nulW[] = {'N','U','L',0};
1012 static const WCHAR prnW[] = {'P','R','N',0};
1013 static const WCHAR comW[] = {'C','O','M',0};
1014 static const WCHAR lptW[] = {'L','P','T',0};
1015 static const WCHAR com0W[] = {'C','O','M','0',0};
1016 static const WCHAR com1W[] = {'\\','D','o','s','D','e','v','i','c','e','s','\\','C','O','M','1',0,0};
1017 static const WCHAR lpt1W[] = {'\\','D','o','s','D','e','v','i','c','e','s','\\','L','P','T','1',0,0};
1024 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1032 DWORD dosdev, ret = 0;
1034 if ((dosdev = RtlIsDosDeviceName_U( devname )))
1036 memcpy( name, devname + HIWORD(dosdev)/sizeof(WCHAR), LOWORD(dosdev) );
1037 name[LOWORD(dosdev)/sizeof(WCHAR)] = 0;
1039 else if (devname[0] && devname[1] == ':' && !devname[2])
1041 memcpy( name, devname, 3 * sizeof(WCHAR) );
1045 SetLastError( ERROR_BAD_PATHNAME );
1049 if (!(path = get_dos_device_path( name ))) return 0;
1050 link = read_symlink( path );
1051 HeapFree( GetProcessHeap(), 0, path );
1055 ret = MultiByteToWideChar( CP_UNIXCP, 0, link, -1, target, bufsize );
1056 HeapFree( GetProcessHeap(), 0, link );
1058 else if (dosdev) /* look for device defaults */
1060 if (!strcmpiW( name, auxW ))
1062 if (bufsize >= sizeof(com1W)/sizeof(WCHAR))
1064 memcpy( target, com1W, sizeof(com1W) );
1065 ret = sizeof(com1W)/sizeof(WCHAR);
1067 else SetLastError( ERROR_INSUFFICIENT_BUFFER );
1070 if (!strcmpiW( name, prnW ))
1072 if (bufsize >= sizeof(lpt1W)/sizeof(WCHAR))
1074 memcpy( target, lpt1W, sizeof(lpt1W) );
1075 ret = sizeof(lpt1W)/sizeof(WCHAR);
1077 else SetLastError( ERROR_INSUFFICIENT_BUFFER );
1083 if (!strcmpiW( name, nulW ))
1084 strcpy( buffer, "/dev/null" );
1085 else if (!strncmpiW( name, comW, 3 ))
1086 get_default_com_device( buffer, name[3] - '0' );
1087 else if (!strncmpiW( name, lptW, 3 ))
1088 get_default_lpt_device( buffer, name[3] - '0' );
1090 if (buffer[0] && !stat( buffer, &st ))
1091 ret = MultiByteToWideChar( CP_UNIXCP, 0, buffer, -1, target, bufsize );
1093 SetLastError( ERROR_FILE_NOT_FOUND );
1098 if (ret < bufsize) target[ret++] = 0; /* add an extra null */
1099 for (p = target; *p; p++) if (*p == '/') *p = '\\';
1104 else /* return a list of all devices */
1107 char *path, *dev, buffer[16];
1110 if (bufsize <= (sizeof(auxW)+sizeof(nulW)+sizeof(prnW))/sizeof(WCHAR))
1112 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1116 memcpy( p, auxW, sizeof(auxW) );
1117 p += sizeof(auxW) / sizeof(WCHAR);
1118 memcpy( p, nulW, sizeof(nulW) );
1119 p += sizeof(nulW) / sizeof(WCHAR);
1120 memcpy( p, prnW, sizeof(prnW) );
1121 p += sizeof(prnW) / sizeof(WCHAR);
1123 if (!(path = get_dos_device_path( com0W ))) return 0;
1124 dev = strrchr( path, '/' ) + 1;
1126 for (i = 1; i <= 9; i++)
1128 sprintf( dev, "com%d", i );
1129 if (!stat( path, &st ) ||
1130 (get_default_com_device( buffer, i ) && !stat( buffer, &st )))
1132 if (p + 5 >= target + bufsize)
1134 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1143 for (i = 1; i <= 9; i++)
1145 sprintf( dev, "lpt%d", i );
1146 if (!stat( path, &st ) ||
1147 (get_default_lpt_device( buffer, i ) && !stat( buffer, &st )))
1149 if (p + 5 >= target + bufsize)
1151 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1160 for (i = 0; i < 26; i++)
1162 sprintf( dev, "%c:", 'a' + i );
1163 if (!stat( path, &st ))
1165 if (p + 3 >= target + bufsize)
1167 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1175 *p++ = 0; /* terminating null */
1181 /***********************************************************************
1182 * QueryDosDeviceA (KERNEL32.@)
1184 * returns array of strings terminated by \0, terminated by \0
1186 DWORD WINAPI QueryDosDeviceA( LPCSTR devname, LPSTR target, DWORD bufsize )
1188 DWORD ret = 0, retW;
1189 UNICODE_STRING devnameW;
1190 LPWSTR targetW = HeapAlloc( GetProcessHeap(),0, bufsize * sizeof(WCHAR) );
1194 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1198 if (devname) RtlCreateUnicodeStringFromAsciiz(&devnameW, devname);
1199 else devnameW.Buffer = NULL;
1201 retW = QueryDosDeviceW(devnameW.Buffer, targetW, bufsize);
1203 ret = WideCharToMultiByte(CP_ACP, 0, targetW, retW, target, bufsize, NULL, NULL);
1205 RtlFreeUnicodeString(&devnameW);
1206 HeapFree(GetProcessHeap(), 0, targetW);