mountmgr: Store the contents of the device symlink in the drive object.
[wine/multimedia.git] / dlls / mountmgr.sys / device.c
blob3ed689fe98610e452c39dfb4185acdab2e0f1de3
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 UNICODE_STRING symlink; /* device symlink if any */
67 STORAGE_DEVICE_NUMBER devnum; /* device number info */
68 struct mount_point *dosdev; /* DosDevices mount point */
69 struct mount_point *volume; /* Volume{xxx} mount point */
70 char *unix_device; /* unix device path */
71 char *unix_mount; /* unix mount point path */
74 static struct list drives_list = LIST_INIT(drives_list);
76 static DRIVER_OBJECT *harddisk_driver;
78 static char *get_dosdevices_path( char **drive )
80 const char *config_dir = wine_get_config_dir();
81 size_t len = strlen(config_dir) + sizeof("/dosdevices/a::");
82 char *path = HeapAlloc( GetProcessHeap(), 0, len );
83 if (path)
85 strcpy( path, config_dir );
86 strcat( path, "/dosdevices/a::" );
87 *drive = path + len - 4;
89 return path;
92 static char *strdupA( const char *str )
94 char *ret;
96 if (!str) return NULL;
97 if ((ret = RtlAllocateHeap( GetProcessHeap(), 0, strlen(str) + 1 ))) strcpy( ret, str );
98 return ret;
101 /* read a Unix symlink; returned buffer must be freed by caller */
102 static char *read_symlink( const char *path )
104 char *buffer;
105 int ret, size = 128;
107 for (;;)
109 if (!(buffer = RtlAllocateHeap( GetProcessHeap(), 0, size )))
111 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
112 return 0;
114 ret = readlink( path, buffer, size );
115 if (ret == -1)
117 RtlFreeHeap( GetProcessHeap(), 0, buffer );
118 return 0;
120 if (ret != size)
122 buffer[ret] = 0;
123 return buffer;
125 RtlFreeHeap( GetProcessHeap(), 0, buffer );
126 size *= 2;
130 /* send notification about a change to a given drive */
131 static void send_notify( int drive, int code )
133 DWORD_PTR result;
134 DEV_BROADCAST_VOLUME info;
136 info.dbcv_size = sizeof(info);
137 info.dbcv_devicetype = DBT_DEVTYP_VOLUME;
138 info.dbcv_reserved = 0;
139 info.dbcv_unitmask = 1 << drive;
140 info.dbcv_flags = DBTF_MEDIA;
141 result = BroadcastSystemMessageW( BSF_FORCEIFHUNG|BSF_QUERY, NULL,
142 WM_DEVICECHANGE, code, (LPARAM)&info );
145 /* create the disk device for a given drive */
146 static NTSTATUS create_disk_device( const char *udi, DWORD type, struct dos_drive **drive_ret )
148 static const WCHAR harddiskvolW[] = {'\\','D','e','v','i','c','e',
149 '\\','H','a','r','d','d','i','s','k','V','o','l','u','m','e','%','u',0};
150 static const WCHAR harddiskW[] = {'\\','D','e','v','i','c','e','\\','H','a','r','d','d','i','s','k','%','u',0};
151 static const WCHAR cdromW[] = {'\\','D','e','v','i','c','e','\\','C','d','R','o','m','%','u',0};
152 static const WCHAR floppyW[] = {'\\','D','e','v','i','c','e','\\','F','l','o','p','p','y','%','u',0};
153 static const WCHAR physdriveW[] = {'\\','?','?','\\','P','h','y','s','i','c','a','l','D','r','i','v','e','%','u',0};
155 UINT i, first = 0;
156 NTSTATUS status = 0;
157 const WCHAR *format;
158 UNICODE_STRING name;
159 DEVICE_OBJECT *dev_obj;
160 struct dos_drive *drive;
162 switch(type)
164 case DRIVE_REMOVABLE:
165 format = floppyW;
166 break;
167 case DRIVE_CDROM:
168 format = cdromW;
169 break;
170 case DRIVE_FIXED:
171 default: /* FIXME */
172 if (udi) format = harddiskW;
173 else
175 format = harddiskvolW;
176 first = 1; /* harddisk volumes start counting from 1 */
178 break;
181 name.MaximumLength = (strlenW(format) + 10) * sizeof(WCHAR);
182 name.Buffer = RtlAllocateHeap( GetProcessHeap(), 0, name.MaximumLength );
183 for (i = first; i < 32; i++)
185 sprintfW( name.Buffer, format, i );
186 name.Length = strlenW(name.Buffer) * sizeof(WCHAR);
187 status = IoCreateDevice( harddisk_driver, sizeof(*drive), &name, 0, 0, FALSE, &dev_obj );
188 if (status != STATUS_OBJECT_NAME_COLLISION) break;
190 if (!status)
192 drive = dev_obj->DeviceExtension;
193 drive->drive = -1;
194 drive->device = dev_obj;
195 drive->name = name;
196 drive->type = type;
197 drive->dosdev = NULL;
198 drive->volume = NULL;
199 drive->unix_device = NULL;
200 drive->unix_mount = NULL;
201 drive->symlink.Buffer = NULL;
202 if (udi)
204 if (!(drive->udi = HeapAlloc( GetProcessHeap(), 0, strlen(udi)+1 )))
206 RtlFreeUnicodeString( &name );
207 IoDeleteDevice( drive->device );
208 return STATUS_NO_MEMORY;
210 strcpy( drive->udi, udi );
212 switch (type)
214 case DRIVE_REMOVABLE:
215 drive->devnum.DeviceType = FILE_DEVICE_DISK;
216 drive->devnum.DeviceNumber = i;
217 drive->devnum.PartitionNumber = ~0u;
218 break;
219 case DRIVE_CDROM:
220 drive->devnum.DeviceType = FILE_DEVICE_CD_ROM;
221 drive->devnum.DeviceNumber = i;
222 drive->devnum.PartitionNumber = ~0u;
223 break;
224 case DRIVE_FIXED:
225 default: /* FIXME */
226 drive->devnum.DeviceType = FILE_DEVICE_DISK;
227 if (udi)
229 UNICODE_STRING symlink;
231 symlink.MaximumLength = sizeof(physdriveW) + 10 * sizeof(WCHAR);
232 if ((symlink.Buffer = RtlAllocateHeap( GetProcessHeap(), 0, symlink.MaximumLength)))
234 sprintfW( symlink.Buffer, physdriveW, i );
235 symlink.Length = strlenW(symlink.Buffer) * sizeof(WCHAR);
236 if (!IoCreateSymbolicLink( &symlink, &name )) drive->symlink = symlink;
238 drive->devnum.DeviceNumber = i;
239 drive->devnum.PartitionNumber = 0;
241 else
243 drive->devnum.DeviceNumber = 0;
244 drive->devnum.PartitionNumber = i;
246 break;
248 list_add_tail( &drives_list, &drive->entry );
249 *drive_ret = drive;
250 TRACE( "created device %s\n", debugstr_w(name.Buffer) );
252 else
254 FIXME( "IoCreateDevice %s got %x\n", debugstr_w(name.Buffer), status );
255 RtlFreeUnicodeString( &name );
257 return status;
260 /* delete the disk device for a given drive */
261 static void delete_disk_device( struct dos_drive *drive )
263 TRACE( "deleting device %s\n", debugstr_w(drive->name.Buffer) );
264 list_remove( &drive->entry );
265 if (drive->dosdev) delete_mount_point( drive->dosdev );
266 if (drive->volume) delete_mount_point( drive->volume );
267 if (drive->symlink.Buffer)
269 IoDeleteSymbolicLink( &drive->symlink );
270 RtlFreeUnicodeString( &drive->symlink );
272 RtlFreeHeap( GetProcessHeap(), 0, drive->unix_device );
273 RtlFreeHeap( GetProcessHeap(), 0, drive->unix_mount );
274 RtlFreeHeap( GetProcessHeap(), 0, drive->udi );
275 RtlFreeUnicodeString( &drive->name );
276 IoDeleteDevice( drive->device );
279 /* set or change the drive letter for an existing drive */
280 static void set_drive_letter( struct dos_drive *drive, int letter )
282 void *id = NULL;
283 unsigned int id_len = 0;
285 if (drive->drive == letter) return;
286 if (drive->dosdev) delete_mount_point( drive->dosdev );
287 if (drive->volume) delete_mount_point( drive->volume );
288 drive->drive = letter;
289 if (letter == -1) return;
290 if (drive->unix_mount)
292 id = drive->unix_mount;
293 id_len = strlen( drive->unix_mount ) + 1;
295 drive->dosdev = add_dosdev_mount_point( drive->device, &drive->name, letter, id, id_len );
296 drive->volume = add_volume_mount_point( drive->device, &drive->name, letter, id, id_len );
299 static inline int is_valid_device( struct stat *st )
301 #if defined(linux) || defined(__sun__)
302 return S_ISBLK( st->st_mode );
303 #else
304 /* disks are char devices on *BSD */
305 return S_ISCHR( st->st_mode );
306 #endif
309 /* find or create a DOS drive for the corresponding device */
310 static int add_drive( const char *device, DWORD type )
312 char *path, *p;
313 char in_use[26];
314 struct stat dev_st, drive_st;
315 int drive, first, last, avail = 0;
317 if (stat( device, &dev_st ) == -1 || !is_valid_device( &dev_st )) return -1;
319 if (!(path = get_dosdevices_path( &p ))) return -1;
321 memset( in_use, 0, sizeof(in_use) );
323 first = 2;
324 last = 26;
325 if (type == DRIVE_REMOVABLE)
327 first = 0;
328 last = 2;
331 while (avail != -1)
333 avail = -1;
334 for (drive = first; drive < last; drive++)
336 if (in_use[drive]) continue; /* already checked */
337 *p = 'a' + drive;
338 if (stat( path, &drive_st ) == -1)
340 if (lstat( path, &drive_st ) == -1 && errno == ENOENT) /* this is a candidate */
342 if (avail == -1)
344 p[2] = 0;
345 /* if mount point symlink doesn't exist either, it's available */
346 if (lstat( path, &drive_st ) == -1 && errno == ENOENT) avail = drive;
347 p[2] = ':';
350 else in_use[drive] = 1;
352 else
354 in_use[drive] = 1;
355 if (!is_valid_device( &drive_st )) continue;
356 if (dev_st.st_rdev == drive_st.st_rdev) goto done;
359 if (avail != -1)
361 /* try to use the one we found */
362 drive = avail;
363 *p = 'a' + drive;
364 if (symlink( device, path ) != -1) goto done;
365 /* failed, retry the search */
368 drive = -1;
370 done:
371 HeapFree( GetProcessHeap(), 0, path );
372 return drive;
375 static BOOL set_unix_mount_point( struct dos_drive *drive, const char *mount_point )
377 char *path, *p;
378 BOOL modified = FALSE;
380 if (!(path = get_dosdevices_path( &p ))) return FALSE;
381 p[0] = 'a' + drive->drive;
382 p[2] = 0;
384 if (mount_point && mount_point[0])
386 /* try to avoid unlinking if already set correctly */
387 if (!drive->unix_mount || strcmp( drive->unix_mount, mount_point ))
389 unlink( path );
390 symlink( mount_point, path );
391 modified = TRUE;
393 RtlFreeHeap( GetProcessHeap(), 0, drive->unix_mount );
394 drive->unix_mount = strdupA( mount_point );
395 if (drive->dosdev) set_mount_point_id( drive->dosdev, mount_point, strlen(mount_point) + 1 );
396 if (drive->volume) set_mount_point_id( drive->volume, mount_point, strlen(mount_point) + 1 );
398 else
400 if (unlink( path ) != -1) modified = TRUE;
401 RtlFreeHeap( GetProcessHeap(), 0, drive->unix_mount );
402 drive->unix_mount = NULL;
403 if (drive->dosdev) set_mount_point_id( drive->dosdev, NULL, 0 );
404 if (drive->volume) set_mount_point_id( drive->volume, NULL, 0 );
407 HeapFree( GetProcessHeap(), 0, path );
408 return modified;
411 /* create devices for mapped drives */
412 static void create_drive_devices(void)
414 char *path, *p, *link, *device;
415 struct dos_drive *drive;
416 unsigned int i;
417 HKEY drives_key;
418 DWORD drive_type;
419 WCHAR driveW[] = {'a',':',0};
421 if (!(path = get_dosdevices_path( &p ))) return;
422 if (RegOpenKeyW( HKEY_LOCAL_MACHINE, drives_keyW, &drives_key )) drives_key = 0;
424 for (i = 0; i < MAX_DOS_DRIVES; i++)
426 p[0] = 'a' + i;
427 p[2] = 0;
428 if (!(link = read_symlink( path ))) continue;
429 p[2] = ':';
430 device = read_symlink( path );
432 drive_type = i < 2 ? DRIVE_REMOVABLE : DRIVE_FIXED;
433 if (drives_key)
435 WCHAR buffer[32];
436 DWORD j, type, size = sizeof(buffer);
438 driveW[0] = 'a' + i;
439 if (!RegQueryValueExW( drives_key, driveW, NULL, &type, (BYTE *)buffer, &size ) &&
440 type == REG_SZ)
442 for (j = 0; j < sizeof(drive_types)/sizeof(drive_types[0]); j++)
443 if (drive_types[j][0] && !strcmpiW( buffer, drive_types[j] ))
445 drive_type = j;
446 break;
451 if (!create_disk_device( NULL, drive_type, &drive ))
453 drive->unix_mount = link;
454 drive->unix_device = device;
455 set_drive_letter( drive, i );
457 else
459 RtlFreeHeap( GetProcessHeap(), 0, link );
460 RtlFreeHeap( GetProcessHeap(), 0, device );
463 RegCloseKey( drives_key );
464 RtlFreeHeap( GetProcessHeap(), 0, path );
467 BOOL add_dos_device( const char *udi, const char *device, const char *mount_point, DWORD type )
469 struct dos_drive *drive, *next;
470 int letter = add_drive( device, type );
472 if (letter == -1) return FALSE;
474 LIST_FOR_EACH_ENTRY_SAFE( drive, next, &drives_list, struct dos_drive, entry )
476 if (drive->udi && !strcmp( udi, drive->udi ))
478 if (type == drive->type) goto found;
479 delete_disk_device( drive );
480 continue;
482 if (drive->drive == letter) delete_disk_device( drive );
485 if (create_disk_device( udi, type, &drive )) return FALSE;
487 found:
488 RtlFreeHeap( GetProcessHeap(), 0, drive->unix_device );
489 drive->unix_device = strdupA( device );
490 set_drive_letter( drive, letter );
491 set_unix_mount_point( drive, mount_point );
493 if (drive->drive != -1)
495 HKEY hkey;
497 TRACE( "added device %c: udi %s for %s on %s type %u\n",
498 'a' + drive->drive, wine_dbgstr_a(udi), wine_dbgstr_a(device),
499 wine_dbgstr_a(mount_point), type );
501 /* hack: force the drive type in the registry */
502 if (!RegCreateKeyW( HKEY_LOCAL_MACHINE, drives_keyW, &hkey ))
504 const WCHAR *type_name = drive_types[type];
505 WCHAR name[3] = {'a',':',0};
507 name[0] += drive->drive;
508 if (type_name[0])
509 RegSetValueExW( hkey, name, 0, REG_SZ, (const BYTE *)type_name,
510 (strlenW(type_name) + 1) * sizeof(WCHAR) );
511 else
512 RegDeleteValueW( hkey, name );
513 RegCloseKey( hkey );
516 send_notify( drive->drive, DBT_DEVICEARRIVAL );
518 return TRUE;
521 BOOL remove_dos_device( const char *udi )
523 HKEY hkey;
524 struct dos_drive *drive;
526 LIST_FOR_EACH_ENTRY( drive, &drives_list, struct dos_drive, entry )
528 if (!drive->udi || strcmp( udi, drive->udi )) continue;
530 if (drive->drive != -1)
532 BOOL modified = set_unix_mount_point( drive, NULL );
534 /* clear the registry key too */
535 if (!RegOpenKeyW( HKEY_LOCAL_MACHINE, drives_keyW, &hkey ))
537 WCHAR name[3] = {'a',':',0};
538 name[0] += drive->drive;
539 RegDeleteValueW( hkey, name );
540 RegCloseKey( hkey );
543 if (modified) send_notify( drive->drive, DBT_DEVICEREMOVECOMPLETE );
545 delete_disk_device( drive );
546 return TRUE;
548 return FALSE;
551 /* handler for ioctls on the harddisk device */
552 static NTSTATUS WINAPI harddisk_ioctl( DEVICE_OBJECT *device, IRP *irp )
554 IO_STACK_LOCATION *irpsp = irp->Tail.Overlay.s.u.CurrentStackLocation;
555 struct dos_drive *drive = device->DeviceExtension;
557 TRACE( "ioctl %x insize %u outsize %u\n",
558 irpsp->Parameters.DeviceIoControl.IoControlCode,
559 irpsp->Parameters.DeviceIoControl.InputBufferLength,
560 irpsp->Parameters.DeviceIoControl.OutputBufferLength );
562 switch(irpsp->Parameters.DeviceIoControl.IoControlCode)
564 case IOCTL_DISK_GET_DRIVE_GEOMETRY:
566 DISK_GEOMETRY info;
567 DWORD len = min( sizeof(info), irpsp->Parameters.DeviceIoControl.OutputBufferLength );
569 info.Cylinders.QuadPart = 10000;
570 info.MediaType = (drive->devnum.DeviceType == FILE_DEVICE_DISK) ? FixedMedia : RemovableMedia;
571 info.TracksPerCylinder = 255;
572 info.SectorsPerTrack = 63;
573 info.BytesPerSector = 512;
574 memcpy( irp->MdlAddress->StartVa, &info, len );
575 irp->IoStatus.Information = len;
576 irp->IoStatus.u.Status = STATUS_SUCCESS;
577 break;
579 case IOCTL_STORAGE_GET_DEVICE_NUMBER:
581 DWORD len = min( sizeof(drive->devnum), irpsp->Parameters.DeviceIoControl.OutputBufferLength );
583 memcpy( irp->MdlAddress->StartVa, &drive->devnum, len );
584 irp->IoStatus.Information = len;
585 irp->IoStatus.u.Status = STATUS_SUCCESS;
586 break;
588 case IOCTL_CDROM_READ_TOC:
589 irp->IoStatus.u.Status = STATUS_INVALID_DEVICE_REQUEST;
590 break;
591 default:
592 FIXME( "unsupported ioctl %x\n", irpsp->Parameters.DeviceIoControl.IoControlCode );
593 irp->IoStatus.u.Status = STATUS_NOT_SUPPORTED;
594 break;
596 return irp->IoStatus.u.Status;
599 /* driver entry point for the harddisk driver */
600 NTSTATUS WINAPI harddisk_driver_entry( DRIVER_OBJECT *driver, UNICODE_STRING *path )
602 struct dos_drive *drive;
604 harddisk_driver = driver;
605 driver->MajorFunction[IRP_MJ_DEVICE_CONTROL] = harddisk_ioctl;
607 /* create a harddisk0 device that isn't assigned to any drive */
608 create_disk_device( "harddisk0 placeholder", DRIVE_FIXED, &drive );
610 create_drive_devices();
612 return STATUS_SUCCESS;