po: Fix a formatting directive in the Danish translation.
[wine/multimedia.git] / dlls / mountmgr.sys / mountmgr.c
blob927e6e8867c3b029eb737809ae5f613e360db310
1 /*
2 * Mount manager service implementation
4 * Copyright 2008 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 <stdarg.h>
22 #include <unistd.h>
24 #define NONAMELESSUNION
25 #define NONAMELESSSTRUCT
27 #include "mountmgr.h"
28 #include "winreg.h"
29 #include "wine/library.h"
30 #include "wine/list.h"
31 #include "wine/unicode.h"
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(mountmgr);
36 #define MIN_ID_LEN 4
38 struct mount_point
40 struct list entry; /* entry in mount points list */
41 DEVICE_OBJECT *device; /* disk device */
42 UNICODE_STRING name; /* device name */
43 UNICODE_STRING link; /* DOS device symlink */
44 void *id; /* device unique id */
45 unsigned int id_len;
48 static struct list mount_points_list = LIST_INIT(mount_points_list);
49 static HKEY mount_key;
51 void set_mount_point_id( struct mount_point *mount, const void *id, unsigned int id_len )
53 RtlFreeHeap( GetProcessHeap(), 0, mount->id );
54 mount->id_len = max( MIN_ID_LEN, id_len );
55 if ((mount->id = RtlAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY, mount->id_len )))
57 memcpy( mount->id, id, id_len );
58 RegSetValueExW( mount_key, mount->link.Buffer, 0, REG_BINARY, mount->id, mount->id_len );
60 else mount->id_len = 0;
63 static struct mount_point *add_mount_point( DEVICE_OBJECT *device, UNICODE_STRING *device_name,
64 const WCHAR *link )
66 struct mount_point *mount;
67 WCHAR *str;
68 UINT len = (strlenW(link) + 1) * sizeof(WCHAR) + device_name->Length + sizeof(WCHAR);
70 if (!(mount = RtlAllocateHeap( GetProcessHeap(), 0, sizeof(*mount) + len ))) return NULL;
72 str = (WCHAR *)(mount + 1);
73 strcpyW( str, link );
74 RtlInitUnicodeString( &mount->link, str );
75 str += strlenW(str) + 1;
76 memcpy( str, device_name->Buffer, device_name->Length );
77 str[device_name->Length / sizeof(WCHAR)] = 0;
78 mount->name.Buffer = str;
79 mount->name.Length = device_name->Length;
80 mount->name.MaximumLength = device_name->Length + sizeof(WCHAR);
81 mount->device = device;
82 mount->id = NULL;
83 list_add_tail( &mount_points_list, &mount->entry );
85 IoCreateSymbolicLink( &mount->link, device_name );
87 TRACE( "created %s id %s for %s\n", debugstr_w(mount->link.Buffer),
88 debugstr_a(mount->id), debugstr_w(mount->name.Buffer) );
89 return mount;
92 /* create the DosDevices mount point symlink for a new device */
93 struct mount_point *add_dosdev_mount_point( DEVICE_OBJECT *device, UNICODE_STRING *device_name, int drive )
95 static const WCHAR driveW[] = {'\\','D','o','s','D','e','v','i','c','e','s','\\','%','c',':',0};
96 WCHAR link[sizeof(driveW)];
98 sprintfW( link, driveW, 'A' + drive );
99 return add_mount_point( device, device_name, link );
102 /* create the Volume mount point symlink for a new device */
103 struct mount_point *add_volume_mount_point( DEVICE_OBJECT *device, UNICODE_STRING *device_name,
104 const GUID *guid )
106 static const WCHAR volumeW[] = {'\\','?','?','\\','V','o','l','u','m','e','{',
107 '%','0','8','x','-','%','0','4','x','-','%','0','4','x','-',
108 '%','0','2','x','%','0','2','x','-','%','0','2','x','%','0','2','x',
109 '%','0','2','x','%','0','2','x','%','0','2','x','%','0','2','x','}',0};
110 WCHAR link[sizeof(volumeW)];
112 sprintfW( link, volumeW, guid->Data1, guid->Data2, guid->Data3,
113 guid->Data4[0], guid->Data4[1], guid->Data4[2], guid->Data4[3],
114 guid->Data4[4], guid->Data4[5], guid->Data4[6], guid->Data4[7]);
115 return add_mount_point( device, device_name, link );
118 /* delete the mount point symlinks when a device goes away */
119 void delete_mount_point( struct mount_point *mount )
121 TRACE( "deleting %s\n", debugstr_w(mount->link.Buffer) );
122 list_remove( &mount->entry );
123 RegDeleteValueW( mount_key, mount->link.Buffer );
124 IoDeleteSymbolicLink( &mount->link );
125 RtlFreeHeap( GetProcessHeap(), 0, mount->id );
126 RtlFreeHeap( GetProcessHeap(), 0, mount );
129 /* check if a given mount point matches the requested specs */
130 static BOOL matching_mount_point( const struct mount_point *mount, const MOUNTMGR_MOUNT_POINT *spec )
132 if (spec->SymbolicLinkNameOffset)
134 const WCHAR *name = (const WCHAR *)((const char *)spec + spec->SymbolicLinkNameOffset);
135 if (spec->SymbolicLinkNameLength != mount->link.Length) return FALSE;
136 if (memicmpW( name, mount->link.Buffer, mount->link.Length/sizeof(WCHAR)))
137 return FALSE;
139 if (spec->DeviceNameOffset)
141 const WCHAR *name = (const WCHAR *)((const char *)spec + spec->DeviceNameOffset);
142 if (spec->DeviceNameLength != mount->name.Length) return FALSE;
143 if (memicmpW( name, mount->name.Buffer, mount->name.Length/sizeof(WCHAR)))
144 return FALSE;
146 if (spec->UniqueIdOffset)
148 const void *id = ((const char *)spec + spec->UniqueIdOffset);
149 if (spec->UniqueIdLength != mount->id_len) return FALSE;
150 if (memcmp( id, mount->id, mount->id_len )) return FALSE;
152 return TRUE;
155 /* implementation of IOCTL_MOUNTMGR_QUERY_POINTS */
156 static NTSTATUS query_mount_points( void *buff, SIZE_T insize,
157 SIZE_T outsize, IO_STATUS_BLOCK *iosb )
159 UINT count, pos, size;
160 MOUNTMGR_MOUNT_POINT *input = buff;
161 MOUNTMGR_MOUNT_POINTS *info;
162 struct mount_point *mount;
164 /* sanity checks */
165 if (input->SymbolicLinkNameOffset + input->SymbolicLinkNameLength > insize ||
166 input->UniqueIdOffset + input->UniqueIdLength > insize ||
167 input->DeviceNameOffset + input->DeviceNameLength > insize ||
168 input->SymbolicLinkNameOffset + input->SymbolicLinkNameLength < input->SymbolicLinkNameOffset ||
169 input->UniqueIdOffset + input->UniqueIdLength < input->UniqueIdOffset ||
170 input->DeviceNameOffset + input->DeviceNameLength < input->DeviceNameOffset)
171 return STATUS_INVALID_PARAMETER;
173 count = size = 0;
174 LIST_FOR_EACH_ENTRY( mount, &mount_points_list, struct mount_point, entry )
176 if (!matching_mount_point( mount, input )) continue;
177 size += mount->name.Length;
178 size += mount->link.Length;
179 size += mount->id_len;
180 size = (size + sizeof(WCHAR) - 1) & ~(sizeof(WCHAR) - 1);
181 count++;
183 pos = FIELD_OFFSET( MOUNTMGR_MOUNT_POINTS, MountPoints[count] );
184 size += pos;
186 if (size > outsize)
188 info = buff;
189 if (size >= sizeof(info->Size)) info->Size = size;
190 iosb->Information = sizeof(info->Size);
191 return STATUS_MORE_ENTRIES;
194 input = HeapAlloc( GetProcessHeap(), 0, insize );
195 if (!input)
196 return STATUS_NO_MEMORY;
197 memcpy( input, buff, insize );
198 info = buff;
200 info->NumberOfMountPoints = count;
201 count = 0;
202 LIST_FOR_EACH_ENTRY( mount, &mount_points_list, struct mount_point, entry )
204 if (!matching_mount_point( mount, input )) continue;
206 info->MountPoints[count].DeviceNameOffset = pos;
207 info->MountPoints[count].DeviceNameLength = mount->name.Length;
208 memcpy( (char *)buff + pos, mount->name.Buffer, mount->name.Length );
209 pos += mount->name.Length;
211 info->MountPoints[count].SymbolicLinkNameOffset = pos;
212 info->MountPoints[count].SymbolicLinkNameLength = mount->link.Length;
213 memcpy( (char *)buff + pos, mount->link.Buffer, mount->link.Length );
214 pos += mount->link.Length;
216 info->MountPoints[count].UniqueIdOffset = pos;
217 info->MountPoints[count].UniqueIdLength = mount->id_len;
218 memcpy( (char *)buff + pos, mount->id, mount->id_len );
219 pos += mount->id_len;
220 pos = (pos + sizeof(WCHAR) - 1) & ~(sizeof(WCHAR) - 1);
221 count++;
223 info->Size = pos;
224 iosb->Information = pos;
225 HeapFree( GetProcessHeap(), 0, input );
226 return STATUS_SUCCESS;
229 /* implementation of IOCTL_MOUNTMGR_DEFINE_UNIX_DRIVE */
230 static NTSTATUS define_unix_drive( const void *in_buff, SIZE_T insize )
232 const struct mountmgr_unix_drive *input = in_buff;
233 const char *mount_point = NULL, *device = NULL;
234 unsigned int i;
235 WCHAR letter = tolowerW( input->letter );
237 if (letter < 'a' || letter > 'z') return STATUS_INVALID_PARAMETER;
238 if (input->type > DRIVE_RAMDISK) return STATUS_INVALID_PARAMETER;
239 if (input->mount_point_offset > insize || input->device_offset > insize)
240 return STATUS_INVALID_PARAMETER;
242 /* make sure string are null-terminated */
243 if (input->mount_point_offset)
245 mount_point = (const char *)in_buff + input->mount_point_offset;
246 for (i = input->mount_point_offset; i < insize; i++)
247 if (!*((const char *)in_buff + i)) break;
248 if (i >= insize) return STATUS_INVALID_PARAMETER;
250 if (input->device_offset)
252 device = (const char *)in_buff + input->device_offset;
253 for (i = input->device_offset; i < insize; i++)
254 if (!*((const char *)in_buff + i)) break;
255 if (i >= insize) return STATUS_INVALID_PARAMETER;
258 if (input->type != DRIVE_NO_ROOT_DIR)
260 enum device_type type = DEVICE_UNKNOWN;
262 TRACE( "defining %c: dev %s mount %s type %u\n",
263 letter, debugstr_a(device), debugstr_a(mount_point), input->type );
264 switch (input->type)
266 case DRIVE_REMOVABLE: type = (letter >= 'c') ? DEVICE_HARDDISK : DEVICE_FLOPPY; break;
267 case DRIVE_REMOTE: type = DEVICE_NETWORK; break;
268 case DRIVE_CDROM: type = DEVICE_CDROM; break;
269 case DRIVE_RAMDISK: type = DEVICE_RAMDISK; break;
270 case DRIVE_FIXED: type = DEVICE_HARDDISK_VOL; break;
272 return add_dos_device( letter - 'a', NULL, device, mount_point, type, NULL );
274 else
276 TRACE( "removing %c:\n", letter );
277 return remove_dos_device( letter - 'a', NULL );
281 /* implementation of IOCTL_MOUNTMGR_QUERY_UNIX_DRIVE */
282 static NTSTATUS query_unix_drive( void *buff, SIZE_T insize,
283 SIZE_T outsize, IO_STATUS_BLOCK *iosb )
285 const struct mountmgr_unix_drive *input = buff;
286 struct mountmgr_unix_drive *output = NULL;
287 char *device, *mount_point;
288 int letter = tolowerW( input->letter );
289 NTSTATUS status;
290 DWORD size, type = DEVICE_UNKNOWN;
291 enum device_type device_type;
292 char *ptr;
294 if (letter < 'a' || letter > 'z') return STATUS_INVALID_PARAMETER;
296 if ((status = query_dos_device( letter - 'a', &device_type, &device, &mount_point ))) return status;
297 switch (device_type)
299 case DEVICE_UNKNOWN: type = DRIVE_UNKNOWN; break;
300 case DEVICE_HARDDISK: type = DRIVE_REMOVABLE; break;
301 case DEVICE_HARDDISK_VOL: type = DRIVE_FIXED; break;
302 case DEVICE_FLOPPY: type = DRIVE_REMOVABLE; break;
303 case DEVICE_CDROM: type = DRIVE_CDROM; break;
304 case DEVICE_DVD: type = DRIVE_CDROM; break;
305 case DEVICE_NETWORK: type = DRIVE_REMOTE; break;
306 case DEVICE_RAMDISK: type = DRIVE_RAMDISK; break;
309 size = sizeof(*output);
310 if (device) size += strlen(device) + 1;
311 if (mount_point) size += strlen(mount_point) + 1;
313 input = NULL;
314 output = buff;
316 if (size > outsize)
318 iosb->Information = 0;
319 if (size >= FIELD_OFFSET( struct mountmgr_unix_drive, size ) + sizeof(output->size))
321 output->size = size;
322 iosb->Information = FIELD_OFFSET( struct mountmgr_unix_drive, size ) + sizeof(output->size);
324 if (size >= FIELD_OFFSET( struct mountmgr_unix_drive, type ) + sizeof(output->type))
326 output->type = type;
327 iosb->Information = FIELD_OFFSET( struct mountmgr_unix_drive, type ) + sizeof(output->type);
329 status = STATUS_MORE_ENTRIES;
330 goto done;
332 output->size = size;
333 output->letter = letter;
334 output->type = type;
335 ptr = (char *)(output + 1);
337 if (mount_point)
339 output->mount_point_offset = ptr - (char *)output;
340 strcpy( ptr, mount_point );
341 ptr += strlen(ptr) + 1;
343 else output->mount_point_offset = 0;
345 if (device)
347 output->device_offset = ptr - (char *)output;
348 strcpy( ptr, device );
349 ptr += strlen(ptr) + 1;
351 else output->device_offset = 0;
353 TRACE( "returning %c: dev %s mount %s type %u\n",
354 letter, debugstr_a(device), debugstr_a(mount_point), type );
356 iosb->Information = ptr - (char *)output;
357 done:
358 RtlFreeHeap( GetProcessHeap(), 0, device );
359 RtlFreeHeap( GetProcessHeap(), 0, mount_point );
360 return status;
363 /* handler for ioctls on the mount manager device */
364 static NTSTATUS WINAPI mountmgr_ioctl( DEVICE_OBJECT *device, IRP *irp )
366 IO_STACK_LOCATION *irpsp = IoGetCurrentIrpStackLocation( irp );
368 TRACE( "ioctl %x insize %u outsize %u\n",
369 irpsp->Parameters.DeviceIoControl.IoControlCode,
370 irpsp->Parameters.DeviceIoControl.InputBufferLength,
371 irpsp->Parameters.DeviceIoControl.OutputBufferLength );
373 switch(irpsp->Parameters.DeviceIoControl.IoControlCode)
375 case IOCTL_MOUNTMGR_QUERY_POINTS:
376 if (irpsp->Parameters.DeviceIoControl.InputBufferLength < sizeof(MOUNTMGR_MOUNT_POINT))
378 irp->IoStatus.u.Status = STATUS_INVALID_PARAMETER;
379 break;
381 irp->IoStatus.u.Status = query_mount_points( irp->AssociatedIrp.SystemBuffer,
382 irpsp->Parameters.DeviceIoControl.InputBufferLength,
383 irpsp->Parameters.DeviceIoControl.OutputBufferLength,
384 &irp->IoStatus );
385 break;
386 case IOCTL_MOUNTMGR_DEFINE_UNIX_DRIVE:
387 if (irpsp->Parameters.DeviceIoControl.InputBufferLength < sizeof(struct mountmgr_unix_drive))
389 irp->IoStatus.u.Status = STATUS_INVALID_PARAMETER;
390 break;
392 irp->IoStatus.Information = 0;
393 irp->IoStatus.u.Status = define_unix_drive( irp->AssociatedIrp.SystemBuffer,
394 irpsp->Parameters.DeviceIoControl.InputBufferLength );
395 break;
396 case IOCTL_MOUNTMGR_QUERY_UNIX_DRIVE:
397 if (irpsp->Parameters.DeviceIoControl.InputBufferLength < sizeof(struct mountmgr_unix_drive))
399 irp->IoStatus.u.Status = STATUS_INVALID_PARAMETER;
400 break;
402 irp->IoStatus.u.Status = query_unix_drive( irp->AssociatedIrp.SystemBuffer,
403 irpsp->Parameters.DeviceIoControl.InputBufferLength,
404 irpsp->Parameters.DeviceIoControl.OutputBufferLength,
405 &irp->IoStatus );
406 break;
407 default:
408 FIXME( "ioctl %x not supported\n", irpsp->Parameters.DeviceIoControl.IoControlCode );
409 irp->IoStatus.u.Status = STATUS_NOT_SUPPORTED;
410 break;
412 IoCompleteRequest( irp, IO_NO_INCREMENT );
413 return irp->IoStatus.u.Status;
416 /* main entry point for the mount point manager driver */
417 NTSTATUS WINAPI DriverEntry( DRIVER_OBJECT *driver, UNICODE_STRING *path )
419 static const WCHAR mounted_devicesW[] = {'S','y','s','t','e','m','\\','M','o','u','n','t','e','d','D','e','v','i','c','e','s',0};
420 static const WCHAR device_mountmgrW[] = {'\\','D','e','v','i','c','e','\\','M','o','u','n','t','P','o','i','n','t','M','a','n','a','g','e','r',0};
421 static const WCHAR link_mountmgrW[] = {'\\','?','?','\\','M','o','u','n','t','P','o','i','n','t','M','a','n','a','g','e','r',0};
422 static const WCHAR harddiskW[] = {'\\','D','r','i','v','e','r','\\','H','a','r','d','d','i','s','k',0};
423 static const WCHAR devicemapW[] = {'H','A','R','D','W','A','R','E','\\','D','E','V','I','C','E','M','A','P',0};
424 static const WCHAR parallelW[] = {'P','A','R','A','L','L','E','L',' ','P','O','R','T','S',0};
425 static const WCHAR serialW[] = {'S','E','R','I','A','L','C','O','M','M',0};
427 UNICODE_STRING nameW, linkW;
428 DEVICE_OBJECT *device;
429 NTSTATUS status;
430 HKEY hkey, devicemap_key;
432 TRACE( "%s\n", debugstr_w(path->Buffer) );
434 driver->MajorFunction[IRP_MJ_DEVICE_CONTROL] = mountmgr_ioctl;
436 RtlInitUnicodeString( &nameW, device_mountmgrW );
437 RtlInitUnicodeString( &linkW, link_mountmgrW );
438 if (!(status = IoCreateDevice( driver, 0, &nameW, 0, 0, FALSE, &device )))
439 status = IoCreateSymbolicLink( &linkW, &nameW );
440 if (status)
442 FIXME( "failed to create device error %x\n", status );
443 return status;
446 RegCreateKeyExW( HKEY_LOCAL_MACHINE, mounted_devicesW, 0, NULL,
447 REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &mount_key, NULL );
449 if (!RegCreateKeyExW( HKEY_LOCAL_MACHINE, devicemapW, 0, NULL, REG_OPTION_VOLATILE,
450 KEY_ALL_ACCESS, NULL, &devicemap_key, NULL ))
452 if (!RegCreateKeyExW( devicemap_key, parallelW, 0, NULL, REG_OPTION_VOLATILE,
453 KEY_ALL_ACCESS, NULL, &hkey, NULL ))
454 RegCloseKey( hkey );
455 if (!RegCreateKeyExW( devicemap_key, serialW, 0, NULL, REG_OPTION_VOLATILE,
456 KEY_ALL_ACCESS, NULL, &hkey, NULL ))
457 RegCloseKey( hkey );
458 RegCloseKey( devicemap_key );
461 RtlInitUnicodeString( &nameW, harddiskW );
462 status = IoCreateDriver( &nameW, harddisk_driver_entry );
464 initialize_dbus();
465 initialize_diskarbitration();
467 return status;