mountmgr: Convert the registry calls to Unicode.
[wine/multimedia.git] / dlls / mountmgr.sys / device.c
blob4328de4d70ee3255dc5f6dbb0fddc862a3aedbf6
1 /*
2 * Dynamic devices support
4 * Copyright 2006 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <errno.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <sys/time.h>
30 #include "mountmgr.h"
31 #include "winreg.h"
32 #include "winuser.h"
33 #include "dbt.h"
35 #include "wine/library.h"
36 #include "wine/list.h"
37 #include "wine/unicode.h"
38 #include "wine/debug.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(mountmgr);
42 #define MAX_DOS_DRIVES 26
44 static const WCHAR drive_types[][8] =
46 { 0 }, /* DRIVE_UNKNOWN */
47 { 0 }, /* DRIVE_NO_ROOT_DIR */
48 {'f','l','o','p','p','y',0}, /* DRIVE_REMOVABLE */
49 {'h','d',0}, /* DRIVE_FIXED */
50 {'n','e','t','w','o','r','k',0}, /* DRIVE_REMOTE */
51 {'c','d','r','o','m',0}, /* DRIVE_CDROM */
52 {'r','a','m','d','i','s','k',0} /* DRIVE_RAMDISK */
55 static const WCHAR drives_keyW[] = {'S','o','f','t','w','a','r','e','\\',
56 'W','i','n','e','\\','D','r','i','v','e','s',0};
58 struct dos_drive
60 struct list entry; /* entry in drives list */
61 char *udi; /* unique identifier for dynamic drives */
62 int drive; /* drive letter (0 = A: etc.) */
63 DWORD type; /* drive type */
64 DEVICE_OBJECT *device; /* disk device allocated for this drive */
65 UNICODE_STRING name; /* device name */
66 STORAGE_DEVICE_NUMBER devnum; /* device number info */
67 struct mount_point *dosdev; /* DosDevices mount point */
68 struct mount_point *volume; /* Volume{xxx} mount point */
69 char *unix_mount; /* unix mount point path */
72 static struct list drives_list = LIST_INIT(drives_list);
74 static DRIVER_OBJECT *harddisk_driver;
76 static char *get_dosdevices_path( char **drive )
78 const char *config_dir = wine_get_config_dir();
79 size_t len = strlen(config_dir) + sizeof("/dosdevices/a::");
80 char *path = HeapAlloc( GetProcessHeap(), 0, len );
81 if (path)
83 strcpy( path, config_dir );
84 strcat( path, "/dosdevices/a::" );
85 *drive = path + len - 4;
87 return path;
90 /* read a Unix symlink; returned buffer must be freed by caller */
91 static char *read_symlink( const char *path )
93 char *buffer;
94 int ret, size = 128;
96 for (;;)
98 if (!(buffer = RtlAllocateHeap( GetProcessHeap(), 0, size )))
100 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
101 return 0;
103 ret = readlink( path, buffer, size );
104 if (ret == -1)
106 RtlFreeHeap( GetProcessHeap(), 0, buffer );
107 return 0;
109 if (ret != size)
111 buffer[ret] = 0;
112 return buffer;
114 RtlFreeHeap( GetProcessHeap(), 0, buffer );
115 size *= 2;
119 /* send notification about a change to a given drive */
120 static void send_notify( int drive, int code )
122 DWORD_PTR result;
123 DEV_BROADCAST_VOLUME info;
125 info.dbcv_size = sizeof(info);
126 info.dbcv_devicetype = DBT_DEVTYP_VOLUME;
127 info.dbcv_reserved = 0;
128 info.dbcv_unitmask = 1 << drive;
129 info.dbcv_flags = DBTF_MEDIA;
130 result = BroadcastSystemMessageW( BSF_FORCEIFHUNG|BSF_QUERY, NULL,
131 WM_DEVICECHANGE, code, (LPARAM)&info );
134 /* create the disk device for a given drive */
135 static NTSTATUS create_disk_device( const char *udi, DWORD type, struct dos_drive **drive_ret )
137 static const WCHAR harddiskW[] = {'\\','D','e','v','i','c','e',
138 '\\','H','a','r','d','d','i','s','k','V','o','l','u','m','e','%','u',0};
139 static const WCHAR cdromW[] = {'\\','D','e','v','i','c','e','\\','C','d','R','o','m','%','u',0};
140 static const WCHAR floppyW[] = {'\\','D','e','v','i','c','e','\\','F','l','o','p','p','y','%','u',0};
142 UINT i, first = 0;
143 NTSTATUS status = 0;
144 const WCHAR *format;
145 UNICODE_STRING name;
146 DEVICE_OBJECT *dev_obj;
147 struct dos_drive *drive;
149 switch(type)
151 case DRIVE_REMOVABLE:
152 format = floppyW;
153 break;
154 case DRIVE_CDROM:
155 format = cdromW;
156 break;
157 case DRIVE_FIXED:
158 default: /* FIXME */
159 format = harddiskW;
160 first = 1; /* harddisk volumes start counting from 1 */
161 break;
164 name.MaximumLength = (strlenW(format) + 10) * sizeof(WCHAR);
165 name.Buffer = RtlAllocateHeap( GetProcessHeap(), 0, name.MaximumLength );
166 for (i = first; i < 32; i++)
168 sprintfW( name.Buffer, format, i );
169 name.Length = strlenW(name.Buffer) * sizeof(WCHAR);
170 status = IoCreateDevice( harddisk_driver, sizeof(*drive), &name, 0, 0, FALSE, &dev_obj );
171 if (status != STATUS_OBJECT_NAME_COLLISION) break;
173 if (!status)
175 drive = dev_obj->DeviceExtension;
176 drive->drive = -1;
177 drive->device = dev_obj;
178 drive->name = name;
179 drive->type = type;
180 drive->dosdev = NULL;
181 drive->volume = NULL;
182 drive->unix_mount = NULL;
183 if (udi)
185 if (!(drive->udi = HeapAlloc( GetProcessHeap(), 0, strlen(udi)+1 )))
187 RtlFreeUnicodeString( &name );
188 IoDeleteDevice( drive->device );
189 return STATUS_NO_MEMORY;
191 strcpy( drive->udi, udi );
193 switch (type)
195 case DRIVE_REMOVABLE:
196 drive->devnum.DeviceType = FILE_DEVICE_DISK;
197 drive->devnum.DeviceNumber = i;
198 drive->devnum.PartitionNumber = ~0u;
199 break;
200 case DRIVE_CDROM:
201 drive->devnum.DeviceType = FILE_DEVICE_CD_ROM;
202 drive->devnum.DeviceNumber = i;
203 drive->devnum.PartitionNumber = ~0u;
204 break;
205 case DRIVE_FIXED:
206 default: /* FIXME */
207 drive->devnum.DeviceType = FILE_DEVICE_DISK;
208 drive->devnum.DeviceNumber = 0;
209 drive->devnum.PartitionNumber = i;
210 break;
212 list_add_tail( &drives_list, &drive->entry );
213 *drive_ret = drive;
214 TRACE( "created device %s\n", debugstr_w(name.Buffer) );
216 else
218 FIXME( "IoCreateDevice %s got %x\n", debugstr_w(name.Buffer), status );
219 RtlFreeUnicodeString( &name );
221 return status;
224 /* delete the disk device for a given drive */
225 static void delete_disk_device( struct dos_drive *drive )
227 TRACE( "deleting device %s\n", debugstr_w(drive->name.Buffer) );
228 list_remove( &drive->entry );
229 if (drive->dosdev) delete_mount_point( drive->dosdev );
230 if (drive->volume) delete_mount_point( drive->volume );
231 RtlFreeHeap( GetProcessHeap(), 0, drive->unix_mount );
232 RtlFreeHeap( GetProcessHeap(), 0, drive->udi );
233 RtlFreeUnicodeString( &drive->name );
234 IoDeleteDevice( drive->device );
237 /* set or change the drive letter for an existing drive */
238 static void set_drive_letter( struct dos_drive *drive, int letter )
240 void *id = NULL;
241 unsigned int id_len = 0;
243 if (drive->drive == letter) return;
244 if (drive->dosdev) delete_mount_point( drive->dosdev );
245 if (drive->volume) delete_mount_point( drive->volume );
246 drive->drive = letter;
247 if (letter == -1) return;
248 if (drive->unix_mount)
250 id = drive->unix_mount;
251 id_len = strlen( drive->unix_mount ) + 1;
253 drive->dosdev = add_dosdev_mount_point( drive->device, &drive->name, letter, id, id_len );
254 drive->volume = add_volume_mount_point( drive->device, &drive->name, letter, id, id_len );
257 static inline int is_valid_device( struct stat *st )
259 #if defined(linux) || defined(__sun__)
260 return S_ISBLK( st->st_mode );
261 #else
262 /* disks are char devices on *BSD */
263 return S_ISCHR( st->st_mode );
264 #endif
267 /* find or create a DOS drive for the corresponding device */
268 static int add_drive( const char *device, DWORD type )
270 char *path, *p;
271 char in_use[26];
272 struct stat dev_st, drive_st;
273 int drive, first, last, avail = 0;
275 if (stat( device, &dev_st ) == -1 || !is_valid_device( &dev_st )) return -1;
277 if (!(path = get_dosdevices_path( &p ))) return -1;
279 memset( in_use, 0, sizeof(in_use) );
281 first = 2;
282 last = 26;
283 if (type == DRIVE_REMOVABLE)
285 first = 0;
286 last = 2;
289 while (avail != -1)
291 avail = -1;
292 for (drive = first; drive < last; drive++)
294 if (in_use[drive]) continue; /* already checked */
295 *p = 'a' + drive;
296 if (stat( path, &drive_st ) == -1)
298 if (lstat( path, &drive_st ) == -1 && errno == ENOENT) /* this is a candidate */
300 if (avail == -1)
302 p[2] = 0;
303 /* if mount point symlink doesn't exist either, it's available */
304 if (lstat( path, &drive_st ) == -1 && errno == ENOENT) avail = drive;
305 p[2] = ':';
308 else in_use[drive] = 1;
310 else
312 in_use[drive] = 1;
313 if (!is_valid_device( &drive_st )) continue;
314 if (dev_st.st_rdev == drive_st.st_rdev) goto done;
317 if (avail != -1)
319 /* try to use the one we found */
320 drive = avail;
321 *p = 'a' + drive;
322 if (symlink( device, path ) != -1) goto done;
323 /* failed, retry the search */
326 drive = -1;
328 done:
329 HeapFree( GetProcessHeap(), 0, path );
330 return drive;
333 static BOOL set_unix_mount_point( struct dos_drive *drive, const char *mount_point )
335 char *path, *p;
336 BOOL modified = FALSE;
338 if (!(path = get_dosdevices_path( &p ))) return FALSE;
339 p[0] = 'a' + drive->drive;
340 p[2] = 0;
342 if (mount_point && mount_point[0])
344 /* try to avoid unlinking if already set correctly */
345 if (!drive->unix_mount || strcmp( drive->unix_mount, mount_point ))
347 unlink( path );
348 symlink( mount_point, path );
349 modified = TRUE;
351 RtlFreeHeap( GetProcessHeap(), 0, drive->unix_mount );
352 if ((drive->unix_mount = RtlAllocateHeap( GetProcessHeap(), 0, strlen(mount_point) + 1 )))
353 strcpy( drive->unix_mount, mount_point );
354 if (drive->dosdev) set_mount_point_id( drive->dosdev, mount_point, strlen(mount_point) + 1 );
355 if (drive->volume) set_mount_point_id( drive->volume, mount_point, strlen(mount_point) + 1 );
357 else
359 if (unlink( path ) != -1) modified = TRUE;
360 RtlFreeHeap( GetProcessHeap(), 0, drive->unix_mount );
361 drive->unix_mount = NULL;
362 if (drive->dosdev) set_mount_point_id( drive->dosdev, NULL, 0 );
363 if (drive->volume) set_mount_point_id( drive->volume, NULL, 0 );
366 HeapFree( GetProcessHeap(), 0, path );
367 return modified;
370 /* create devices for mapped drives */
371 static void create_drive_devices(void)
373 char *path, *p, *link;
374 struct dos_drive *drive;
375 unsigned int i;
377 if (!(path = get_dosdevices_path( &p ))) return;
379 for (i = 0; i < MAX_DOS_DRIVES; i++)
381 p[0] = 'a' + i;
382 p[2] = 0;
383 if (!(link = read_symlink( path ))) continue;
384 if (!create_disk_device( NULL, i < 2 ? DRIVE_REMOVABLE : DRIVE_FIXED, &drive ))
386 drive->unix_mount = link;
387 set_drive_letter( drive, i );
389 else RtlFreeHeap( GetProcessHeap(), 0, link );
391 RtlFreeHeap( GetProcessHeap(), 0, path );
394 BOOL add_dos_device( const char *udi, const char *device, const char *mount_point, DWORD type )
396 struct dos_drive *drive, *next;
397 int letter = add_drive( device, type );
399 if (letter == -1) return FALSE;
401 LIST_FOR_EACH_ENTRY_SAFE( drive, next, &drives_list, struct dos_drive, entry )
403 if (drive->udi && !strcmp( udi, drive->udi ))
405 if (type == drive->type) goto found;
406 delete_disk_device( drive );
407 continue;
409 if (drive->drive == letter) delete_disk_device( drive );
412 if (create_disk_device( udi, type, &drive )) return FALSE;
414 found:
415 set_drive_letter( drive, letter );
416 set_unix_mount_point( drive, mount_point );
418 if (drive->drive != -1)
420 HKEY hkey;
422 TRACE( "added device %c: udi %s for %s on %s type %u\n",
423 'a' + drive->drive, wine_dbgstr_a(udi), wine_dbgstr_a(device),
424 wine_dbgstr_a(mount_point), type );
426 /* hack: force the drive type in the registry */
427 if (!RegCreateKeyW( HKEY_LOCAL_MACHINE, drives_keyW, &hkey ))
429 const WCHAR *type_name = drive_types[type];
430 WCHAR name[3] = {'a',':',0};
432 name[0] += drive->drive;
433 if (type_name[0])
434 RegSetValueExW( hkey, name, 0, REG_SZ, (const BYTE *)type_name,
435 (strlenW(type_name) + 1) * sizeof(WCHAR) );
436 else
437 RegDeleteValueW( hkey, name );
438 RegCloseKey( hkey );
441 send_notify( drive->drive, DBT_DEVICEARRIVAL );
443 return TRUE;
446 BOOL remove_dos_device( const char *udi )
448 HKEY hkey;
449 struct dos_drive *drive;
451 LIST_FOR_EACH_ENTRY( drive, &drives_list, struct dos_drive, entry )
453 if (!drive->udi || strcmp( udi, drive->udi )) continue;
455 if (drive->drive != -1)
457 BOOL modified = set_unix_mount_point( drive, NULL );
459 /* clear the registry key too */
460 if (!RegOpenKeyW( HKEY_LOCAL_MACHINE, drives_keyW, &hkey ))
462 WCHAR name[3] = {'a',':',0};
463 name[0] += drive->drive;
464 RegDeleteValueW( hkey, name );
465 RegCloseKey( hkey );
468 if (modified) send_notify( drive->drive, DBT_DEVICEREMOVECOMPLETE );
470 delete_disk_device( drive );
471 return TRUE;
473 return FALSE;
476 /* handler for ioctls on the harddisk device */
477 static NTSTATUS WINAPI harddisk_ioctl( DEVICE_OBJECT *device, IRP *irp )
479 IO_STACK_LOCATION *irpsp = irp->Tail.Overlay.s.u.CurrentStackLocation;
480 struct dos_drive *drive = device->DeviceExtension;
482 TRACE( "ioctl %x insize %u outsize %u\n",
483 irpsp->Parameters.DeviceIoControl.IoControlCode,
484 irpsp->Parameters.DeviceIoControl.InputBufferLength,
485 irpsp->Parameters.DeviceIoControl.OutputBufferLength );
487 switch(irpsp->Parameters.DeviceIoControl.IoControlCode)
489 case IOCTL_DISK_GET_DRIVE_GEOMETRY:
491 DISK_GEOMETRY info;
492 DWORD len = min( sizeof(info), irpsp->Parameters.DeviceIoControl.OutputBufferLength );
494 info.Cylinders.QuadPart = 10000;
495 info.MediaType = (drive->devnum.DeviceType == FILE_DEVICE_DISK) ? FixedMedia : RemovableMedia;
496 info.TracksPerCylinder = 255;
497 info.SectorsPerTrack = 63;
498 info.BytesPerSector = 512;
499 memcpy( irp->MdlAddress->StartVa, &info, len );
500 irp->IoStatus.Information = len;
501 irp->IoStatus.u.Status = STATUS_SUCCESS;
502 break;
504 case IOCTL_STORAGE_GET_DEVICE_NUMBER:
506 DWORD len = min( sizeof(drive->devnum), irpsp->Parameters.DeviceIoControl.OutputBufferLength );
508 memcpy( irp->MdlAddress->StartVa, &drive->devnum, len );
509 irp->IoStatus.Information = len;
510 irp->IoStatus.u.Status = STATUS_SUCCESS;
511 break;
513 case IOCTL_CDROM_READ_TOC:
514 irp->IoStatus.u.Status = STATUS_INVALID_DEVICE_REQUEST;
515 break;
516 default:
517 FIXME( "unsupported ioctl %x\n", irpsp->Parameters.DeviceIoControl.IoControlCode );
518 irp->IoStatus.u.Status = STATUS_NOT_SUPPORTED;
519 break;
521 return irp->IoStatus.u.Status;
524 /* driver entry point for the harddisk driver */
525 NTSTATUS WINAPI harddisk_driver_entry( DRIVER_OBJECT *driver, UNICODE_STRING *path )
527 static const WCHAR harddisk0W[] = {'\\','D','e','v','i','c','e',
528 '\\','H','a','r','d','d','i','s','k','0',0};
529 static const WCHAR physdrive0W[] = {'\\','?','?','\\','P','h','y','s','i','c','a','l','D','r','i','v','e','0',0};
531 UNICODE_STRING nameW, linkW;
532 DEVICE_OBJECT *device;
533 NTSTATUS status;
534 struct dos_drive *drive;
536 harddisk_driver = driver;
537 driver->MajorFunction[IRP_MJ_DEVICE_CONTROL] = harddisk_ioctl;
539 RtlInitUnicodeString( &nameW, harddisk0W );
540 RtlInitUnicodeString( &linkW, physdrive0W );
541 if (!(status = IoCreateDevice( driver, sizeof(*drive), &nameW, 0, 0, FALSE, &device )))
542 status = IoCreateSymbolicLink( &linkW, &nameW );
543 if (status)
545 FIXME( "failed to create device error %x\n", status );
546 return status;
548 drive = device->DeviceExtension;
549 drive->drive = -1;
550 drive->udi = NULL;
551 drive->type = DRIVE_FIXED;
552 drive->name = nameW;
553 drive->device = device;
554 drive->devnum.DeviceType = FILE_DEVICE_DISK;
555 drive->devnum.DeviceNumber = 0;
556 drive->devnum.PartitionNumber = 0;
558 create_drive_devices();
560 return status;