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 );
117 if (ret != sizeof(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 /***********************************************************************
152 * VOLUME_CreateDevices
154 * Create the device files for the new device naming scheme.
155 * Should go away after a transition period.
157 void VOLUME_CreateDevices(void)
159 const char *config_dir = wine_get_config_dir();
163 if (!(buffer = HeapAlloc( GetProcessHeap(), 0,
164 strlen(config_dir) + sizeof("/dosdevices") )))
167 strcpy( buffer, config_dir );
168 strcat( buffer, "/dosdevices" );
170 if (!mkdir( buffer, 0777 )) /* we created it, so now create the devices */
174 OBJECT_ATTRIBUTES attr;
175 UNICODE_STRING nameW;
178 WCHAR com[5] = {'C','O','M','1',0};
179 WCHAR lpt[5] = {'L','P','T','1',0};
181 static const WCHAR serialportsW[] = {'M','a','c','h','i','n','e','\\',
182 'S','o','f','t','w','a','r','e','\\',
183 'W','i','n','e','\\','W','i','n','e','\\',
184 'C','o','n','f','i','g','\\',
185 'S','e','r','i','a','l','P','o','r','t','s',0};
186 static const WCHAR parallelportsW[] = {'M','a','c','h','i','n','e','\\',
187 'S','o','f','t','w','a','r','e','\\',
188 'W','i','n','e','\\','W','i','n','e','\\',
189 'C','o','n','f','i','g','\\',
190 'P','a','r','a','l','l','e','l','P','o','r','t','s',0};
192 attr.Length = sizeof(attr);
193 attr.RootDirectory = 0;
194 attr.ObjectName = &nameW;
196 attr.SecurityDescriptor = NULL;
197 attr.SecurityQualityOfService = NULL;
198 RtlInitUnicodeString( &nameW, serialportsW );
200 if (!NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr ))
202 RtlInitUnicodeString( &nameW, com );
203 for (i = 1; i <= 9; i++)
206 if (!NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation,
207 tmp, sizeof(tmp), &dummy ))
209 devnameW = (WCHAR *)((KEY_VALUE_PARTIAL_INFORMATION *)tmp)->Data;
210 if ((p = strchrW( devnameW, ',' ))) *p = 0;
211 if (DefineDosDeviceW( DDD_RAW_TARGET_PATH, com, devnameW ))
214 WideCharToMultiByte(CP_UNIXCP, 0, devnameW, -1,
215 devname, sizeof(devname), NULL, NULL);
216 MESSAGE( "Created symlink %s/dosdevices/com%d -> %s\n", config_dir, i, devname );
224 RtlInitUnicodeString( &nameW, parallelportsW );
225 if (!NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr ))
227 RtlInitUnicodeString( &nameW, lpt );
228 for (i = 1; i <= 9; i++)
231 if (!NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation,
232 tmp, sizeof(tmp), &dummy ))
234 devnameW = (WCHAR *)((KEY_VALUE_PARTIAL_INFORMATION *)tmp)->Data;
235 if ((p = strchrW( devnameW, ',' ))) *p = 0;
236 if (DefineDosDeviceW( DDD_RAW_TARGET_PATH, lpt, devnameW ))
239 WideCharToMultiByte(CP_UNIXCP, 0, devnameW, -1,
240 devname, sizeof(devname), NULL, NULL);
241 MESSAGE( "Created symlink %s/dosdevices/lpt%d -> %s\n", config_dir, i, devname );
249 MESSAGE( "\nYou can now remove the [SerialPorts] and [ParallelPorts] sections\n"
250 "in your configuration file, they are replaced by the above symlinks.\n\n" );
252 HeapFree( GetProcessHeap(), 0, buffer );
256 /******************************************************************
259 HANDLE VOLUME_OpenDevice( LPCWSTR name, DWORD access, DWORD sharing,
260 LPSECURITY_ATTRIBUTES sa, DWORD attributes )
265 if (!(buffer = get_dos_device_path( name ))) return 0;
266 dev = strrchr( buffer, '/' ) + 1;
270 TRACE("trying %s\n", buffer );
272 ret = FILE_CreateFile( buffer, access, sharing, sa, OPEN_EXISTING, 0, 0, TRUE, DRIVE_FIXED );
273 if (ret || GetLastError() != ERROR_FILE_NOT_FOUND) break;
276 /* now try some defaults for it */
277 if (!strcmp( dev, "aux" ))
279 strcpy( dev, "com1" );
282 if (!strcmp( dev, "prn" ))
284 strcpy( dev, "lpt1" );
287 if (!strcmp( dev, "nul" ))
289 strcpy( buffer, "/dev/null" );
290 dev = NULL; /* last try */
293 if (!strncmp( dev, "com", 3 ) && get_default_com_device( buffer, dev[3] - '0' ))
295 dev = NULL; /* last try */
298 if (!strncmp( dev, "lpt", 3 ) && get_default_lpt_device( buffer, dev[3] - '0' ))
300 dev = NULL; /* last try */
306 if (!ret) ERR( "could not open device %s err %ld\n", debugstr_w(name), GetLastError() );
307 HeapFree( GetProcessHeap(), 0, buffer );
312 /******************************************************************
313 * VOLUME_FindCdRomDataBestVoldesc
315 static DWORD VOLUME_FindCdRomDataBestVoldesc( HANDLE handle )
317 BYTE cur_vd_type, max_vd_type = 0;
319 DWORD size, offs, best_offs = 0, extra_offs = 0;
321 for (offs = 0x8000; offs <= 0x9800; offs += 0x800)
323 /* if 'CDROM' occurs at position 8, this is a pre-iso9660 cd, and
324 * the volume label is displaced forward by 8
326 if (SetFilePointer( handle, offs, NULL, FILE_BEGIN ) != offs) break;
327 if (!ReadFile( handle, buffer, sizeof(buffer), &size, NULL )) break;
328 if (size != sizeof(buffer)) break;
329 /* check for non-ISO9660 signature */
330 if (!memcmp( buffer + 11, "ROM", 3 )) extra_offs = 8;
331 cur_vd_type = buffer[extra_offs];
332 if (cur_vd_type == 0xff) /* voldesc set terminator */
334 if (cur_vd_type > max_vd_type)
336 max_vd_type = cur_vd_type;
337 best_offs = offs + extra_offs;
344 /***********************************************************************
345 * VOLUME_ReadFATSuperblock
347 static enum fs_type VOLUME_ReadFATSuperblock( HANDLE handle, BYTE *buff )
351 /* try a fixed disk, with a FAT partition */
352 if (SetFilePointer( handle, 0, NULL, FILE_BEGIN ) != 0 ||
353 !ReadFile( handle, buff, SUPERBLOCK_SIZE, &size, NULL ) ||
354 size != SUPERBLOCK_SIZE)
357 if (buff[0] == 0xE9 || (buff[0] == 0xEB && buff[2] == 0x90))
359 /* guess which type of FAT we have */
360 unsigned int sz, nsect, nclust;
361 sz = GETWORD(buff, 0x16);
362 if (!sz) sz = GETLONG(buff, 0x24);
363 nsect = GETWORD(buff, 0x13);
364 if (!nsect) nsect = GETLONG(buff, 0x20);
365 nsect -= GETWORD(buff, 0x0e) + buff[0x10] * sz +
366 (GETWORD(buff, 0x11) * 32 + (GETWORD(buff, 0x0b) - 1)) / GETWORD(buff, 0x0b);
367 nclust = nsect / buff[0x0d];
371 if (buff[0x26] == 0x29 && !memcmp(buff+0x36, "FAT", 3))
373 /* FIXME: do really all FAT have their name beginning with
374 * "FAT" ? (At least FAT12, FAT16 and FAT32 have :)
379 else if (!memcmp(buff+0x52, "FAT", 3)) return FS_FAT32;
385 /***********************************************************************
386 * VOLUME_ReadCDSuperblock
388 static enum fs_type VOLUME_ReadCDSuperblock( HANDLE handle, BYTE *buff )
390 DWORD size, offs = VOLUME_FindCdRomDataBestVoldesc( handle );
392 if (!offs) return FS_UNKNOWN;
394 if (SetFilePointer( handle, offs, NULL, FILE_BEGIN ) != offs ||
395 !ReadFile( handle, buff, SUPERBLOCK_SIZE, &size, NULL ) ||
396 size != SUPERBLOCK_SIZE)
399 /* check for iso9660 present */
400 if (!memcmp(&buff[1], "CD001", 5)) return FS_ISO9660;
405 /**************************************************************************
406 * VOLUME_GetSuperblockLabel
408 static void VOLUME_GetSuperblockLabel( enum fs_type type, const BYTE *superblock,
409 WCHAR *label, DWORD len )
411 const BYTE *label_ptr = NULL;
421 label_ptr = superblock + 0x2b;
425 label_ptr = superblock + 0x47;
430 BYTE ver = superblock[0x5a];
432 if (superblock[0x58] == 0x25 && superblock[0x59] == 0x2f && /* Unicode ID */
433 ((ver == 0x40) || (ver == 0x43) || (ver == 0x45)))
434 { /* yippee, unicode */
437 if (len > 17) len = 17;
438 for (i = 0; i < len-1; i++)
439 label[i] = (superblock[40+2*i] << 8) | superblock[41+2*i];
441 while (i && label[i-1] == ' ') label[--i] = 0;
444 label_ptr = superblock + 40;
449 if (label_len) RtlMultiByteToUnicodeN( label, (len-1) * sizeof(WCHAR),
450 &label_len, label_ptr, label_len );
451 label_len /= sizeof(WCHAR);
452 label[label_len] = 0;
453 while (label_len && label[label_len-1] == ' ') label[--label_len] = 0;
457 /**************************************************************************
458 * VOLUME_SetSuperblockLabel
460 static BOOL VOLUME_SetSuperblockLabel( enum fs_type type, HANDLE handle, const WCHAR *label )
474 SetLastError( ERROR_ACCESS_DENIED );
477 RtlUnicodeToMultiByteN( label_data, sizeof(label_data), &len,
478 label, strlenW(label) * sizeof(WCHAR) );
479 if (len < sizeof(label_data))
480 memset( label_data + len, ' ', sizeof(label_data) - len );
482 return (SetFilePointer( handle, offset, NULL, FILE_BEGIN ) == offset &&
483 WriteFile( handle, label_data, sizeof(label_data), &len, NULL ));
487 /**************************************************************************
488 * VOLUME_GetSuperblockSerial
490 static DWORD VOLUME_GetSuperblockSerial( enum fs_type type, const BYTE *superblock )
498 return GETLONG( superblock, 0x27 );
500 return GETLONG( superblock, 0x33 );
506 sum[0] = sum[1] = sum[2] = sum[3] = 0;
507 for (i = 0; i < 2048; i += 4)
509 /* DON'T optimize this into DWORD !! (breaks overflow) */
510 sum[0] += superblock[i+0];
511 sum[1] += superblock[i+1];
512 sum[2] += superblock[i+2];
513 sum[3] += superblock[i+3];
516 * OK, another braindead one... argh. Just believe it.
517 * Me$$ysoft chose to reverse the serial number in NT4/W2K.
518 * It's true and nobody will ever be able to change it.
520 if (GetVersion() & 0x80000000)
521 return (sum[3] << 24) | (sum[2] << 16) | (sum[1] << 8) | sum[0];
523 return (sum[0] << 24) | (sum[1] << 16) | (sum[2] << 8) | sum[3];
530 /**************************************************************************
531 * VOLUME_GetAudioCDSerial
533 static DWORD VOLUME_GetAudioCDSerial( const CDROM_TOC *toc )
538 for (i = 0; i <= toc->LastTrack - toc->FirstTrack; i++)
539 serial += ((toc->TrackData[i].Address[1] << 16) |
540 (toc->TrackData[i].Address[2] << 8) |
541 toc->TrackData[i].Address[3]);
544 * dwStart, dwEnd collect the beginning and end of the disc respectively, in
546 * There it is collected for correcting the serial when there are less than
549 if (toc->LastTrack - toc->FirstTrack + 1 < 3)
551 DWORD dwStart = FRAME_OF_TOC(toc, toc->FirstTrack);
552 DWORD dwEnd = FRAME_OF_TOC(toc, toc->LastTrack + 1);
553 serial += dwEnd - dwStart;
559 /***********************************************************************
560 * GetVolumeInformationW (KERNEL32.@)
562 BOOL WINAPI GetVolumeInformationW( LPCWSTR root, LPWSTR label, DWORD label_len,
563 DWORD *serial, DWORD *filename_len, DWORD *flags,
564 LPWSTR fsname, DWORD fsname_len )
566 static const WCHAR audiocdW[] = {'A','u','d','i','o',' ','C','D',0};
567 static const WCHAR fatW[] = {'F','A','T',0};
568 static const WCHAR cdfsW[] = {'C','D','F','S',0};
570 WCHAR device[] = {'\\','\\','.','\\','A',':',0};
572 enum fs_type type = FS_UNKNOWN;
576 WCHAR path[MAX_PATH];
577 GetCurrentDirectoryW( MAX_PATH, path );
582 if (!root[0] || root[1] != ':')
584 SetLastError( ERROR_INVALID_NAME );
590 /* try to open the device */
592 handle = CreateFileW( device, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE,
593 NULL, OPEN_EXISTING, 0, 0 );
594 if (handle != INVALID_HANDLE_VALUE)
596 BYTE superblock[SUPERBLOCK_SIZE];
600 /* check for audio CD */
601 /* FIXME: we only check the first track for now */
602 if (DeviceIoControl( handle, IOCTL_CDROM_READ_TOC, NULL, 0, &toc, sizeof(toc), &br, 0 ))
604 if (!(toc.TrackData[0].Control & 0x04)) /* audio track */
606 TRACE( "%s: found audio CD\n", debugstr_w(device) );
607 if (label) lstrcpynW( label, audiocdW, label_len );
608 if (serial) *serial = VOLUME_GetAudioCDSerial( &toc );
609 CloseHandle( handle );
613 type = VOLUME_ReadCDSuperblock( handle, superblock );
617 type = VOLUME_ReadFATSuperblock( handle, superblock );
618 if (type == FS_UNKNOWN) type = VOLUME_ReadCDSuperblock( handle, superblock );
620 CloseHandle( handle );
621 TRACE( "%s: found fs type %d\n", debugstr_w(device), type );
622 if (type == FS_ERROR) return FALSE;
624 if (label && label_len) VOLUME_GetSuperblockLabel( type, superblock, label, label_len );
625 if (serial) *serial = VOLUME_GetSuperblockSerial( type, superblock );
630 TRACE( "cannot open device %s: err %ld\n", debugstr_w(device), GetLastError() );
631 if (GetLastError() != ERROR_ACCESS_DENIED) return FALSE;
634 /* we couldn't open the device, fallback to default strategy */
636 switch(GetDriveTypeW( root ))
639 case DRIVE_NO_ROOT_DIR:
640 SetLastError( ERROR_NOT_READY );
642 case DRIVE_REMOVABLE:
653 if (label && label_len)
655 WCHAR labelW[] = {'A',':','\\','.','w','i','n','d','o','w','s','-','l','a','b','e','l',0};
657 labelW[0] = device[4];
658 handle = CreateFileW( labelW, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
659 OPEN_EXISTING, 0, 0 );
660 if (handle != INVALID_HANDLE_VALUE)
662 char buffer[256], *p;
665 if (!ReadFile( handle, buffer, sizeof(buffer)-1, &size, NULL )) size = 0;
666 CloseHandle( handle );
668 while (p > buffer && (p[-1] == ' ' || p[-1] == '\r' || p[-1] == '\n')) p--;
670 if (!MultiByteToWideChar( CP_UNIXCP, 0, buffer, -1, label, label_len ))
671 label[label_len-1] = 0;
677 WCHAR serialW[] = {'A',':','\\','.','w','i','n','d','o','w','s','-','s','e','r','i','a','l',0};
679 serialW[0] = device[4];
680 handle = CreateFileW( serialW, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
681 OPEN_EXISTING, 0, 0 );
682 if (handle != INVALID_HANDLE_VALUE)
687 if (!ReadFile( handle, buffer, sizeof(buffer)-1, &size, NULL )) size = 0;
688 CloseHandle( handle );
690 *serial = strtoul( buffer, NULL, 16 );
695 fill_fs_info: /* now fill in the information that depends on the file system type */
700 if (fsname) lstrcpynW( fsname, cdfsW, fsname_len );
701 if (filename_len) *filename_len = 221;
702 if (flags) *flags = FILE_READ_ONLY_VOLUME;
706 default: /* default to FAT file system (FIXME) */
707 if (fsname) lstrcpynW( fsname, fatW, fsname_len );
708 if (filename_len) *filename_len = 255;
709 if (flags) *flags = FILE_CASE_PRESERVED_NAMES; /* FIXME */
716 /***********************************************************************
717 * GetVolumeInformationA (KERNEL32.@)
719 BOOL WINAPI GetVolumeInformationA( LPCSTR root, LPSTR label,
720 DWORD label_len, DWORD *serial,
721 DWORD *filename_len, DWORD *flags,
722 LPSTR fsname, DWORD fsname_len )
724 UNICODE_STRING rootW;
725 LPWSTR labelW, fsnameW;
728 if (root) RtlCreateUnicodeStringFromAsciiz(&rootW, root);
729 else rootW.Buffer = NULL;
730 labelW = label ? HeapAlloc(GetProcessHeap(), 0, label_len * sizeof(WCHAR)) : NULL;
731 fsnameW = fsname ? HeapAlloc(GetProcessHeap(), 0, fsname_len * sizeof(WCHAR)) : NULL;
733 if ((ret = GetVolumeInformationW(rootW.Buffer, labelW, label_len, serial,
734 filename_len, flags, fsnameW, fsname_len)))
736 if (label) WideCharToMultiByte(CP_ACP, 0, labelW, -1, label, label_len, NULL, NULL);
737 if (fsname) WideCharToMultiByte(CP_ACP, 0, fsnameW, -1, fsname, fsname_len, NULL, NULL);
740 RtlFreeUnicodeString(&rootW);
741 if (labelW) HeapFree( GetProcessHeap(), 0, labelW );
742 if (fsnameW) HeapFree( GetProcessHeap(), 0, fsnameW );
748 /***********************************************************************
749 * SetVolumeLabelW (KERNEL32.@)
751 BOOL WINAPI SetVolumeLabelW( LPCWSTR root, LPCWSTR label )
753 WCHAR device[] = {'\\','\\','.','\\','A',':',0};
755 enum fs_type type = FS_UNKNOWN;
759 WCHAR path[MAX_PATH];
760 GetCurrentDirectoryW( MAX_PATH, path );
765 if (!root[0] || root[1] != ':')
767 SetLastError( ERROR_INVALID_NAME );
773 /* try to open the device */
775 handle = CreateFileW( device, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE,
776 NULL, OPEN_EXISTING, 0, 0 );
777 if (handle == INVALID_HANDLE_VALUE)
780 handle = CreateFileW( device, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE,
781 NULL, OPEN_EXISTING, 0, 0 );
782 if (handle != INVALID_HANDLE_VALUE)
784 /* device can be read but not written, return error */
785 CloseHandle( handle );
786 SetLastError( ERROR_ACCESS_DENIED );
791 if (handle != INVALID_HANDLE_VALUE)
793 BYTE superblock[SUPERBLOCK_SIZE];
796 type = VOLUME_ReadFATSuperblock( handle, superblock );
797 ret = VOLUME_SetSuperblockLabel( type, handle, label );
798 CloseHandle( handle );
803 TRACE( "cannot open device %s: err %ld\n", debugstr_w(device), GetLastError() );
804 if (GetLastError() != ERROR_ACCESS_DENIED) return FALSE;
807 /* we couldn't open the device, fallback to default strategy */
809 switch(GetDriveTypeW( root ))
812 case DRIVE_NO_ROOT_DIR:
813 SetLastError( ERROR_NOT_READY );
815 case DRIVE_REMOVABLE:
818 WCHAR labelW[] = {'A',':','\\','.','w','i','n','d','o','w','s','-','l','a','b','e','l',0};
820 labelW[0] = device[4];
821 handle = CreateFileW( labelW, GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
822 CREATE_ALWAYS, 0, 0 );
823 if (handle != INVALID_HANDLE_VALUE)
828 if (!WideCharToMultiByte( CP_UNIXCP, 0, label, -1, buffer, sizeof(buffer), NULL, NULL ))
829 buffer[sizeof(buffer)-1] = 0;
830 WriteFile( handle, buffer, strlen(buffer), &size, NULL );
831 CloseHandle( handle );
839 SetLastError( ERROR_ACCESS_DENIED );
845 /***********************************************************************
846 * SetVolumeLabelA (KERNEL32.@)
848 BOOL WINAPI SetVolumeLabelA(LPCSTR root, LPCSTR volname)
850 UNICODE_STRING rootW, volnameW;
853 if (root) RtlCreateUnicodeStringFromAsciiz(&rootW, root);
854 else rootW.Buffer = NULL;
855 if (volname) RtlCreateUnicodeStringFromAsciiz(&volnameW, volname);
856 else volnameW.Buffer = NULL;
858 ret = SetVolumeLabelW( rootW.Buffer, volnameW.Buffer );
860 RtlFreeUnicodeString(&rootW);
861 RtlFreeUnicodeString(&volnameW);
866 /***********************************************************************
867 * GetVolumeNameForVolumeMountPointW (KERNEL32.@)
869 BOOL WINAPI GetVolumeNameForVolumeMountPointW(LPCWSTR str, LPWSTR dst, DWORD size)
871 FIXME("(%s, %p, %lx): stub\n", debugstr_w(str), dst, size);
876 /***********************************************************************
877 * DefineDosDeviceW (KERNEL32.@)
879 BOOL WINAPI DefineDosDeviceW( DWORD flags, LPCWSTR devname, LPCWSTR targetpath )
883 if (!(flags & DDD_RAW_TARGET_PATH))
885 FIXME( "(0x%08lx,%s,%s) DDD_RAW_TARGET_PATH flag not set, not supported yet\n",
886 flags, debugstr_w(devname), debugstr_w(targetpath) );
887 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
891 /* first check for a DOS device */
893 if ((dosdev = RtlIsDosDeviceName_U( devname )))
897 char *path, *target, *p;
900 memcpy( name, devname + HIWORD(dosdev)/sizeof(WCHAR), LOWORD(dosdev) );
901 name[LOWORD(dosdev)/sizeof(WCHAR)] = 0;
902 if (!(path = get_dos_device_path( name ))) return FALSE;
904 len = WideCharToMultiByte( CP_UNIXCP, 0, targetpath, -1, NULL, 0, NULL, NULL );
905 if ((target = HeapAlloc( GetProcessHeap(), 0, len )))
907 WideCharToMultiByte( CP_UNIXCP, 0, targetpath, -1, target, len, NULL, NULL );
908 for (p = target; *p; p++) if (*p == '\\') *p = '/';
909 TRACE( "creating symlink %s -> %s\n", path, target );
911 if (!symlink( target, path )) ret = TRUE;
912 else FILE_SetDosError();
913 HeapFree( GetProcessHeap(), 0, target );
915 else SetLastError( ERROR_NOT_ENOUGH_MEMORY );
916 HeapFree( GetProcessHeap(), 0, path );
920 /* now it must be a drive mapping */
922 FIXME("(0x%08lx,%s,%s) drive mappings not supported yet\n",
923 flags, debugstr_w(devname), debugstr_w(targetpath) );
924 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
929 /***********************************************************************
930 * DefineDosDeviceA (KERNEL32.@)
932 BOOL WINAPI DefineDosDeviceA(DWORD flags, LPCSTR devname, LPCSTR targetpath)
937 if (!RtlCreateUnicodeStringFromAsciiz(&d, devname))
939 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
942 if (!RtlCreateUnicodeStringFromAsciiz(&t, targetpath))
944 RtlFreeUnicodeString(&d);
945 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
948 ret = DefineDosDeviceW(flags, d.Buffer, t.Buffer);
949 RtlFreeUnicodeString(&d);
950 RtlFreeUnicodeString(&t);
955 /***********************************************************************
956 * QueryDosDeviceW (KERNEL32.@)
958 * returns array of strings terminated by \0, terminated by \0
960 DWORD WINAPI QueryDosDeviceW( LPCWSTR devname, LPWSTR target, DWORD bufsize )
962 static const WCHAR auxW[] = {'A','U','X',0};
963 static const WCHAR nulW[] = {'N','U','L',0};
964 static const WCHAR prnW[] = {'P','R','N',0};
965 static const WCHAR comW[] = {'C','O','M',0};
966 static const WCHAR lptW[] = {'L','P','T',0};
967 static const WCHAR com0W[] = {'C','O','M','0',0};
968 static const WCHAR com1W[] = {'\\','D','o','s','D','e','v','i','c','e','s','\\','C','O','M','1',0,0};
969 static const WCHAR lpt1W[] = {'\\','D','o','s','D','e','v','i','c','e','s','\\','L','P','T','1',0,0};
976 SetLastError( ERROR_INSUFFICIENT_BUFFER );
984 DWORD dosdev, ret = 0;
986 if (!(dosdev = RtlIsDosDeviceName_U( devname )))
988 SetLastError( ERROR_BAD_PATHNAME );
991 memcpy( name, devname + HIWORD(dosdev)/sizeof(WCHAR), LOWORD(dosdev) );
992 name[LOWORD(dosdev)/sizeof(WCHAR)] = 0;
993 if (!(path = get_dos_device_path( name ))) return 0;
994 link = read_symlink( path );
995 HeapFree( GetProcessHeap(), 0, path );
999 ret = MultiByteToWideChar( CP_UNIXCP, 0, link, -1, target, bufsize );
1000 HeapFree( GetProcessHeap(), 0, link );
1002 else /* look for defaults */
1004 if (!strcmpiW( name, auxW ))
1006 if (bufsize >= sizeof(com1W)/sizeof(WCHAR))
1008 memcpy( target, com1W, sizeof(com1W) );
1009 ret = sizeof(com1W)/sizeof(WCHAR);
1011 else SetLastError( ERROR_INSUFFICIENT_BUFFER );
1014 if (!strcmpiW( name, prnW ))
1016 if (bufsize >= sizeof(lpt1W)/sizeof(WCHAR))
1018 memcpy( target, lpt1W, sizeof(lpt1W) );
1019 ret = sizeof(lpt1W)/sizeof(WCHAR);
1021 else SetLastError( ERROR_INSUFFICIENT_BUFFER );
1027 if (!strcmpiW( name, nulW ))
1028 strcpy( buffer, "/dev/null" );
1029 else if (!strncmpiW( name, comW, 3 ))
1030 get_default_com_device( buffer, name[3] - '0' );
1031 else if (!strncmpiW( name, lptW, 3 ))
1032 get_default_lpt_device( buffer, name[3] - '0' );
1034 if (buffer[0] && !stat( buffer, &st ))
1035 ret = MultiByteToWideChar( CP_UNIXCP, 0, buffer, -1, target, bufsize );
1037 SetLastError( ERROR_FILE_NOT_FOUND );
1042 if (ret < bufsize) target[ret++] = 0; /* add an extra null */
1043 for (p = target; *p; p++) if (*p == '/') *p = '\\';
1048 else /* return a list of all devices */
1051 char *path, *dev, buffer[16];
1054 if (bufsize <= (sizeof(auxW)+sizeof(nulW)+sizeof(prnW))/sizeof(WCHAR))
1056 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1060 memcpy( p, auxW, sizeof(auxW) );
1061 p += sizeof(auxW) / sizeof(WCHAR);
1062 memcpy( p, nulW, sizeof(nulW) );
1063 p += sizeof(nulW) / sizeof(WCHAR);
1064 memcpy( p, prnW, sizeof(prnW) );
1065 p += sizeof(prnW) / sizeof(WCHAR);
1067 if (!(path = get_dos_device_path( com0W ))) return 0;
1068 dev = strrchr( path, '/' ) + 1;
1070 for (i = 1; i <= 9; i++)
1072 sprintf( dev, "com%d", i );
1073 if (!stat( path, &st ) ||
1074 (get_default_com_device( buffer, i ) && !stat( buffer, &st )))
1076 if (p + 5 >= target + bufsize)
1078 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1087 for (i = 1; i <= 9; i++)
1089 sprintf( dev, "lpt%d", i );
1090 if (!stat( path, &st ) ||
1091 (get_default_lpt_device( buffer, i ) && !stat( buffer, &st )))
1093 if (p + 5 >= target + bufsize)
1095 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1104 *p++ = 0; /* terminating null */
1110 /***********************************************************************
1111 * QueryDosDeviceA (KERNEL32.@)
1113 * returns array of strings terminated by \0, terminated by \0
1115 DWORD WINAPI QueryDosDeviceA( LPCSTR devname, LPSTR target, DWORD bufsize )
1117 DWORD ret = 0, retW;
1118 UNICODE_STRING devnameW;
1119 LPWSTR targetW = HeapAlloc( GetProcessHeap(),0, bufsize * sizeof(WCHAR) );
1123 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1127 if (devname) RtlCreateUnicodeStringFromAsciiz(&devnameW, devname);
1128 else devnameW.Buffer = NULL;
1130 retW = QueryDosDeviceW(devnameW.Buffer, targetW, bufsize);
1132 ret = WideCharToMultiByte(CP_ACP, 0, targetW, retW, target, bufsize, NULL, NULL);
1134 RtlFreeUnicodeString(&devnameW);
1135 HeapFree(GetProcessHeap(), 0, targetW);