2006-10-03 12:40:27 +02:00
|
|
|
/*
|
|
|
|
* Dynamic devices support
|
|
|
|
*
|
|
|
|
* Copyright 2006 Alexandre Julliard
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include "wine/port.h"
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
|
2008-10-21 15:01:52 +02:00
|
|
|
#include "mountmgr.h"
|
2006-10-03 12:40:27 +02:00
|
|
|
#include "winreg.h"
|
|
|
|
#include "winuser.h"
|
|
|
|
#include "dbt.h"
|
|
|
|
|
|
|
|
#include "wine/library.h"
|
|
|
|
#include "wine/list.h"
|
2008-10-15 20:12:27 +02:00
|
|
|
#include "wine/unicode.h"
|
2006-10-03 12:40:27 +02:00
|
|
|
#include "wine/debug.h"
|
|
|
|
|
2008-03-04 07:58:32 +01:00
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(mountmgr);
|
2006-10-03 12:40:27 +02:00
|
|
|
|
2008-10-21 15:02:10 +02:00
|
|
|
#define MAX_DOS_DRIVES 26
|
|
|
|
|
2008-10-15 20:12:27 +02:00
|
|
|
static const WCHAR drive_types[][8] =
|
|
|
|
{
|
2008-11-12 10:47:49 +01:00
|
|
|
{ 0 }, /* DEVICE_UNKNOWN */
|
|
|
|
{ 0 }, /* DEVICE_HARDDISK */
|
|
|
|
{'h','d',0}, /* DEVICE_HARDDISK_VOL */
|
|
|
|
{'f','l','o','p','p','y',0}, /* DEVICE_FLOPPY */
|
|
|
|
{'c','d','r','o','m',0}, /* DEVICE_CDROM */
|
2013-04-18 05:39:11 +02:00
|
|
|
{'c','d','r','o','m',0}, /* DEVICE_DVD */
|
2008-11-12 10:47:49 +01:00
|
|
|
{'n','e','t','w','o','r','k',0}, /* DEVICE_NETWORK */
|
|
|
|
{'r','a','m','d','i','s','k',0} /* DEVICE_RAMDISK */
|
2008-10-15 20:12:27 +02:00
|
|
|
};
|
|
|
|
|
2008-10-21 15:28:23 +02:00
|
|
|
static const WCHAR drives_keyW[] = {'S','o','f','t','w','a','r','e','\\',
|
|
|
|
'W','i','n','e','\\','D','r','i','v','e','s',0};
|
|
|
|
|
2009-07-20 15:46:34 +02:00
|
|
|
struct disk_device
|
2006-10-03 12:40:27 +02:00
|
|
|
{
|
2008-11-12 10:47:49 +01:00
|
|
|
enum device_type type; /* drive type */
|
2009-07-20 15:46:34 +02:00
|
|
|
DEVICE_OBJECT *dev_obj; /* disk device allocated for this volume */
|
2008-10-21 15:20:09 +02:00
|
|
|
UNICODE_STRING name; /* device name */
|
2008-10-21 16:08:51 +02:00
|
|
|
UNICODE_STRING symlink; /* device symlink if any */
|
2008-10-21 15:20:09 +02:00
|
|
|
STORAGE_DEVICE_NUMBER devnum; /* device number info */
|
2008-10-23 15:43:46 +02:00
|
|
|
char *unix_device; /* unix device path */
|
2008-10-21 15:20:09 +02:00
|
|
|
char *unix_mount; /* unix mount point path */
|
2006-10-03 12:40:27 +02:00
|
|
|
};
|
|
|
|
|
2009-07-20 15:56:26 +02:00
|
|
|
struct volume
|
|
|
|
{
|
|
|
|
struct list entry; /* entry in volumes list */
|
|
|
|
struct disk_device *device; /* disk device */
|
|
|
|
char *udi; /* unique identifier for dynamic volumes */
|
2009-07-22 20:55:24 +02:00
|
|
|
unsigned int ref; /* ref count */
|
2009-07-20 16:05:04 +02:00
|
|
|
GUID guid; /* volume uuid */
|
2009-07-20 15:56:26 +02:00
|
|
|
struct mount_point *mount; /* Volume{xxx} mount point */
|
|
|
|
};
|
|
|
|
|
2009-07-20 15:46:34 +02:00
|
|
|
struct dos_drive
|
|
|
|
{
|
|
|
|
struct list entry; /* entry in drives list */
|
2009-07-20 15:56:26 +02:00
|
|
|
struct volume *volume; /* volume for this drive */
|
2009-07-20 15:46:34 +02:00
|
|
|
int drive; /* drive letter (0 = A: etc.) */
|
2009-07-22 17:46:02 +02:00
|
|
|
struct mount_point *mount; /* DosDevices mount point */
|
2009-07-20 15:46:34 +02:00
|
|
|
};
|
|
|
|
|
2006-10-03 12:40:27 +02:00
|
|
|
static struct list drives_list = LIST_INIT(drives_list);
|
2009-07-21 20:30:43 +02:00
|
|
|
static struct list volumes_list = LIST_INIT(volumes_list);
|
2006-10-03 12:40:27 +02:00
|
|
|
|
2008-10-21 15:20:09 +02:00
|
|
|
static DRIVER_OBJECT *harddisk_driver;
|
|
|
|
|
2009-07-22 21:13:00 +02:00
|
|
|
static CRITICAL_SECTION device_section;
|
|
|
|
static CRITICAL_SECTION_DEBUG critsect_debug =
|
|
|
|
{
|
|
|
|
0, 0, &device_section,
|
|
|
|
{ &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
|
|
|
|
0, 0, { (DWORD_PTR)(__FILE__ ": device_section") }
|
|
|
|
};
|
|
|
|
static CRITICAL_SECTION device_section = { &critsect_debug, -1, 0, 0, 0, 0 };
|
|
|
|
|
2008-10-21 15:20:42 +02:00
|
|
|
static char *get_dosdevices_path( char **drive )
|
2006-10-03 12:40:27 +02:00
|
|
|
{
|
|
|
|
const char *config_dir = wine_get_config_dir();
|
|
|
|
size_t len = strlen(config_dir) + sizeof("/dosdevices/a::");
|
|
|
|
char *path = HeapAlloc( GetProcessHeap(), 0, len );
|
|
|
|
if (path)
|
|
|
|
{
|
|
|
|
strcpy( path, config_dir );
|
|
|
|
strcat( path, "/dosdevices/a::" );
|
2008-10-21 15:20:42 +02:00
|
|
|
*drive = path + len - 4;
|
2006-10-03 12:40:27 +02:00
|
|
|
}
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2008-10-23 15:43:46 +02:00
|
|
|
static char *strdupA( const char *str )
|
|
|
|
{
|
|
|
|
char *ret;
|
|
|
|
|
|
|
|
if (!str) return NULL;
|
|
|
|
if ((ret = RtlAllocateHeap( GetProcessHeap(), 0, strlen(str) + 1 ))) strcpy( ret, str );
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-07-20 16:05:04 +02:00
|
|
|
static const GUID *get_default_uuid( int letter )
|
|
|
|
{
|
|
|
|
static GUID guid;
|
|
|
|
|
|
|
|
guid.Data4[7] = 'A' + letter;
|
|
|
|
return &guid;
|
|
|
|
}
|
|
|
|
|
2008-10-21 15:02:10 +02:00
|
|
|
/* read a Unix symlink; returned buffer must be freed by caller */
|
|
|
|
static char *read_symlink( const char *path )
|
|
|
|
{
|
|
|
|
char *buffer;
|
|
|
|
int ret, size = 128;
|
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
if (!(buffer = RtlAllocateHeap( GetProcessHeap(), 0, size )))
|
|
|
|
{
|
|
|
|
SetLastError( ERROR_NOT_ENOUGH_MEMORY );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
ret = readlink( path, buffer, size );
|
|
|
|
if (ret == -1)
|
|
|
|
{
|
|
|
|
RtlFreeHeap( GetProcessHeap(), 0, buffer );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (ret != size)
|
|
|
|
{
|
|
|
|
buffer[ret] = 0;
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
RtlFreeHeap( GetProcessHeap(), 0, buffer );
|
|
|
|
size *= 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-22 17:57:08 +02:00
|
|
|
/* update a symlink if it changed; return TRUE if updated */
|
|
|
|
static void update_symlink( const char *path, const char *dest, const char *orig_dest )
|
|
|
|
{
|
|
|
|
if (dest && dest[0])
|
|
|
|
{
|
|
|
|
if (!orig_dest || strcmp( orig_dest, dest ))
|
|
|
|
{
|
|
|
|
unlink( path );
|
|
|
|
symlink( dest, path );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else unlink( path );
|
|
|
|
}
|
|
|
|
|
2006-10-03 12:40:27 +02:00
|
|
|
/* send notification about a change to a given drive */
|
|
|
|
static void send_notify( int drive, int code )
|
|
|
|
{
|
|
|
|
DEV_BROADCAST_VOLUME info;
|
|
|
|
|
|
|
|
info.dbcv_size = sizeof(info);
|
|
|
|
info.dbcv_devicetype = DBT_DEVTYP_VOLUME;
|
|
|
|
info.dbcv_reserved = 0;
|
|
|
|
info.dbcv_unitmask = 1 << drive;
|
|
|
|
info.dbcv_flags = DBTF_MEDIA;
|
2010-05-08 19:44:22 +02:00
|
|
|
BroadcastSystemMessageW( BSF_FORCEIFHUNG|BSF_QUERY, NULL,
|
|
|
|
WM_DEVICECHANGE, code, (LPARAM)&info );
|
2006-10-03 12:40:27 +02:00
|
|
|
}
|
|
|
|
|
2009-07-20 15:46:34 +02:00
|
|
|
/* create the disk device for a given volume */
|
|
|
|
static NTSTATUS create_disk_device( enum device_type type, struct disk_device **device_ret )
|
2008-10-21 15:02:10 +02:00
|
|
|
{
|
2008-10-21 15:52:47 +02:00
|
|
|
static const WCHAR harddiskvolW[] = {'\\','D','e','v','i','c','e',
|
|
|
|
'\\','H','a','r','d','d','i','s','k','V','o','l','u','m','e','%','u',0};
|
|
|
|
static const WCHAR harddiskW[] = {'\\','D','e','v','i','c','e','\\','H','a','r','d','d','i','s','k','%','u',0};
|
2008-10-21 15:02:10 +02:00
|
|
|
static const WCHAR cdromW[] = {'\\','D','e','v','i','c','e','\\','C','d','R','o','m','%','u',0};
|
|
|
|
static const WCHAR floppyW[] = {'\\','D','e','v','i','c','e','\\','F','l','o','p','p','y','%','u',0};
|
2008-11-12 10:47:49 +01:00
|
|
|
static const WCHAR ramdiskW[] = {'\\','D','e','v','i','c','e','\\','R','a','m','d','i','s','k','%','u',0};
|
2012-01-17 11:41:15 +01:00
|
|
|
static const WCHAR cdromlinkW[] = {'\\','?','?','\\','C','d','R','o','m','%','u',0};
|
2008-10-21 16:08:51 +02:00
|
|
|
static const WCHAR physdriveW[] = {'\\','?','?','\\','P','h','y','s','i','c','a','l','D','r','i','v','e','%','u',0};
|
2008-10-21 15:02:10 +02:00
|
|
|
|
|
|
|
UINT i, first = 0;
|
|
|
|
NTSTATUS status = 0;
|
2008-11-12 10:47:49 +01:00
|
|
|
const WCHAR *format = NULL;
|
2012-01-17 11:41:15 +01:00
|
|
|
const WCHAR *link_format = NULL;
|
2008-10-21 15:02:10 +02:00
|
|
|
UNICODE_STRING name;
|
2008-10-21 15:20:09 +02:00
|
|
|
DEVICE_OBJECT *dev_obj;
|
2009-07-20 15:46:34 +02:00
|
|
|
struct disk_device *device;
|
2008-10-21 15:02:10 +02:00
|
|
|
|
|
|
|
switch(type)
|
|
|
|
{
|
2008-11-12 10:47:49 +01:00
|
|
|
case DEVICE_UNKNOWN:
|
|
|
|
case DEVICE_HARDDISK:
|
|
|
|
case DEVICE_NETWORK: /* FIXME */
|
|
|
|
format = harddiskW;
|
2012-01-17 11:41:15 +01:00
|
|
|
link_format = physdriveW;
|
2008-11-12 10:47:49 +01:00
|
|
|
break;
|
|
|
|
case DEVICE_HARDDISK_VOL:
|
|
|
|
format = harddiskvolW;
|
|
|
|
first = 1; /* harddisk volumes start counting from 1 */
|
|
|
|
break;
|
|
|
|
case DEVICE_FLOPPY:
|
2008-10-21 15:02:10 +02:00
|
|
|
format = floppyW;
|
|
|
|
break;
|
2008-11-12 10:47:49 +01:00
|
|
|
case DEVICE_CDROM:
|
2011-02-08 23:12:54 +01:00
|
|
|
case DEVICE_DVD:
|
2008-10-21 15:02:10 +02:00
|
|
|
format = cdromW;
|
2012-01-17 11:41:15 +01:00
|
|
|
link_format = cdromlinkW;
|
2008-10-21 15:02:10 +02:00
|
|
|
break;
|
2008-11-12 10:47:49 +01:00
|
|
|
case DEVICE_RAMDISK:
|
|
|
|
format = ramdiskW;
|
2008-10-21 15:02:10 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
name.MaximumLength = (strlenW(format) + 10) * sizeof(WCHAR);
|
|
|
|
name.Buffer = RtlAllocateHeap( GetProcessHeap(), 0, name.MaximumLength );
|
|
|
|
for (i = first; i < 32; i++)
|
|
|
|
{
|
|
|
|
sprintfW( name.Buffer, format, i );
|
|
|
|
name.Length = strlenW(name.Buffer) * sizeof(WCHAR);
|
2009-07-20 15:46:34 +02:00
|
|
|
status = IoCreateDevice( harddisk_driver, sizeof(*device), &name, 0, 0, FALSE, &dev_obj );
|
2008-10-21 15:02:10 +02:00
|
|
|
if (status != STATUS_OBJECT_NAME_COLLISION) break;
|
|
|
|
}
|
|
|
|
if (!status)
|
|
|
|
{
|
2009-07-20 15:46:34 +02:00
|
|
|
device = dev_obj->DeviceExtension;
|
|
|
|
device->dev_obj = dev_obj;
|
|
|
|
device->name = name;
|
|
|
|
device->type = type;
|
|
|
|
device->unix_device = NULL;
|
|
|
|
device->unix_mount = NULL;
|
|
|
|
device->symlink.Buffer = NULL;
|
|
|
|
|
2012-01-17 11:41:15 +01:00
|
|
|
if (link_format)
|
|
|
|
{
|
|
|
|
UNICODE_STRING symlink;
|
|
|
|
|
|
|
|
symlink.MaximumLength = (strlenW(link_format) + 10) * sizeof(WCHAR);
|
|
|
|
if ((symlink.Buffer = RtlAllocateHeap( GetProcessHeap(), 0, symlink.MaximumLength)))
|
|
|
|
{
|
|
|
|
sprintfW( symlink.Buffer, link_format, i );
|
|
|
|
symlink.Length = strlenW(symlink.Buffer) * sizeof(WCHAR);
|
|
|
|
if (!IoCreateSymbolicLink( &symlink, &name )) device->symlink = symlink;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-21 15:20:09 +02:00
|
|
|
switch (type)
|
2008-10-21 15:02:10 +02:00
|
|
|
{
|
2008-11-12 10:47:49 +01:00
|
|
|
case DEVICE_FLOPPY:
|
|
|
|
case DEVICE_RAMDISK:
|
2009-07-20 15:46:34 +02:00
|
|
|
device->devnum.DeviceType = FILE_DEVICE_DISK;
|
|
|
|
device->devnum.DeviceNumber = i;
|
|
|
|
device->devnum.PartitionNumber = ~0u;
|
2008-10-21 15:02:10 +02:00
|
|
|
break;
|
2008-11-12 10:47:49 +01:00
|
|
|
case DEVICE_CDROM:
|
2009-07-20 15:46:34 +02:00
|
|
|
device->devnum.DeviceType = FILE_DEVICE_CD_ROM;
|
|
|
|
device->devnum.DeviceNumber = i;
|
|
|
|
device->devnum.PartitionNumber = ~0u;
|
2008-10-21 15:02:10 +02:00
|
|
|
break;
|
2011-02-08 23:12:54 +01:00
|
|
|
case DEVICE_DVD:
|
|
|
|
device->devnum.DeviceType = FILE_DEVICE_DVD;
|
|
|
|
device->devnum.DeviceNumber = i;
|
|
|
|
device->devnum.PartitionNumber = ~0u;
|
|
|
|
break;
|
2008-11-12 10:47:49 +01:00
|
|
|
case DEVICE_UNKNOWN:
|
|
|
|
case DEVICE_HARDDISK:
|
|
|
|
case DEVICE_NETWORK: /* FIXME */
|
2012-01-17 11:41:15 +01:00
|
|
|
device->devnum.DeviceType = FILE_DEVICE_DISK;
|
|
|
|
device->devnum.DeviceNumber = i;
|
|
|
|
device->devnum.PartitionNumber = 0;
|
2008-11-12 10:47:49 +01:00
|
|
|
break;
|
|
|
|
case DEVICE_HARDDISK_VOL:
|
2009-07-20 15:46:34 +02:00
|
|
|
device->devnum.DeviceType = FILE_DEVICE_DISK;
|
|
|
|
device->devnum.DeviceNumber = 0;
|
|
|
|
device->devnum.PartitionNumber = i;
|
2008-10-21 15:02:10 +02:00
|
|
|
break;
|
|
|
|
}
|
2009-07-20 15:46:34 +02:00
|
|
|
*device_ret = device;
|
2008-10-21 15:20:09 +02:00
|
|
|
TRACE( "created device %s\n", debugstr_w(name.Buffer) );
|
2008-10-21 15:02:10 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
FIXME( "IoCreateDevice %s got %x\n", debugstr_w(name.Buffer), status );
|
|
|
|
RtlFreeUnicodeString( &name );
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2008-10-21 15:20:09 +02:00
|
|
|
/* delete the disk device for a given drive */
|
2009-07-20 15:46:34 +02:00
|
|
|
static void delete_disk_device( struct disk_device *device )
|
|
|
|
{
|
|
|
|
TRACE( "deleting device %s\n", debugstr_w(device->name.Buffer) );
|
|
|
|
if (device->symlink.Buffer)
|
|
|
|
{
|
|
|
|
IoDeleteSymbolicLink( &device->symlink );
|
|
|
|
RtlFreeUnicodeString( &device->symlink );
|
|
|
|
}
|
|
|
|
RtlFreeHeap( GetProcessHeap(), 0, device->unix_device );
|
|
|
|
RtlFreeHeap( GetProcessHeap(), 0, device->unix_mount );
|
|
|
|
RtlFreeUnicodeString( &device->name );
|
|
|
|
IoDeleteDevice( device->dev_obj );
|
|
|
|
}
|
|
|
|
|
2009-07-22 20:55:24 +02:00
|
|
|
/* grab another reference to a volume */
|
2009-08-04 12:36:34 +02:00
|
|
|
static struct volume *grab_volume( struct volume *volume )
|
2009-07-22 20:55:24 +02:00
|
|
|
{
|
2009-08-04 12:36:34 +02:00
|
|
|
volume->ref++;
|
|
|
|
return volume;
|
2009-07-22 20:55:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* release a volume and delete the corresponding disk device when refcount is 0 */
|
|
|
|
static unsigned int release_volume( struct volume *volume )
|
|
|
|
{
|
|
|
|
unsigned int ret = --volume->ref;
|
|
|
|
|
|
|
|
if (!ret)
|
|
|
|
{
|
2009-08-04 12:36:34 +02:00
|
|
|
TRACE( "%s udi %s\n", debugstr_guid(&volume->guid), debugstr_a(volume->udi) );
|
2009-07-22 20:55:24 +02:00
|
|
|
assert( !volume->udi );
|
|
|
|
list_remove( &volume->entry );
|
|
|
|
if (volume->mount) delete_mount_point( volume->mount );
|
|
|
|
delete_disk_device( volume->device );
|
|
|
|
RtlFreeHeap( GetProcessHeap(), 0, volume );
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set the volume udi */
|
|
|
|
static void set_volume_udi( struct volume *volume, const char *udi )
|
|
|
|
{
|
|
|
|
if (udi)
|
|
|
|
{
|
|
|
|
assert( !volume->udi );
|
|
|
|
/* having a udi means the HAL side holds an extra reference */
|
|
|
|
if ((volume->udi = strdupA( udi ))) grab_volume( volume );
|
|
|
|
}
|
|
|
|
else if (volume->udi)
|
|
|
|
{
|
|
|
|
RtlFreeHeap( GetProcessHeap(), 0, volume->udi );
|
|
|
|
volume->udi = NULL;
|
|
|
|
release_volume( volume );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-20 15:56:26 +02:00
|
|
|
/* create a disk volume */
|
|
|
|
static NTSTATUS create_volume( const char *udi, enum device_type type, struct volume **volume_ret )
|
|
|
|
{
|
|
|
|
struct volume *volume;
|
|
|
|
NTSTATUS status;
|
|
|
|
|
|
|
|
if (!(volume = RtlAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*volume) )))
|
|
|
|
return STATUS_NO_MEMORY;
|
|
|
|
|
|
|
|
if (!(status = create_disk_device( type, &volume->device )))
|
|
|
|
{
|
2009-07-22 20:55:24 +02:00
|
|
|
if (udi) set_volume_udi( volume, udi );
|
2009-07-21 20:30:43 +02:00
|
|
|
list_add_tail( &volumes_list, &volume->entry );
|
2009-08-04 12:36:34 +02:00
|
|
|
*volume_ret = grab_volume( volume );
|
2009-07-20 15:56:26 +02:00
|
|
|
}
|
2009-07-22 20:55:24 +02:00
|
|
|
else RtlFreeHeap( GetProcessHeap(), 0, volume );
|
2009-07-20 15:56:26 +02:00
|
|
|
|
2009-07-22 20:55:24 +02:00
|
|
|
return status;
|
2009-07-20 15:56:26 +02:00
|
|
|
}
|
|
|
|
|
2009-07-20 15:46:34 +02:00
|
|
|
/* create the disk device for a given volume */
|
2009-07-22 20:56:04 +02:00
|
|
|
static NTSTATUS create_dos_device( struct volume *volume, const char *udi, int letter,
|
|
|
|
enum device_type type, struct dos_drive **drive_ret )
|
2009-07-20 15:46:34 +02:00
|
|
|
{
|
|
|
|
struct dos_drive *drive;
|
|
|
|
NTSTATUS status;
|
|
|
|
|
|
|
|
if (!(drive = RtlAllocateHeap( GetProcessHeap(), 0, sizeof(*drive) ))) return STATUS_NO_MEMORY;
|
2009-07-22 18:25:38 +02:00
|
|
|
drive->drive = letter;
|
2009-07-22 17:46:02 +02:00
|
|
|
drive->mount = NULL;
|
2009-07-20 15:46:34 +02:00
|
|
|
|
2009-07-22 20:56:04 +02:00
|
|
|
if (volume)
|
|
|
|
{
|
|
|
|
if (udi) set_volume_udi( volume, udi );
|
2009-08-04 12:36:34 +02:00
|
|
|
drive->volume = grab_volume( volume );
|
2009-07-22 20:56:04 +02:00
|
|
|
status = STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
else status = create_volume( udi, type, &drive->volume );
|
|
|
|
|
|
|
|
if (status == STATUS_SUCCESS)
|
2009-07-20 15:46:34 +02:00
|
|
|
{
|
|
|
|
list_add_tail( &drives_list, &drive->entry );
|
|
|
|
*drive_ret = drive;
|
|
|
|
}
|
2009-07-20 15:56:26 +02:00
|
|
|
else RtlFreeHeap( GetProcessHeap(), 0, drive );
|
|
|
|
|
2009-07-20 15:46:34 +02:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* delete the disk device for a given drive */
|
|
|
|
static void delete_dos_device( struct dos_drive *drive )
|
2008-10-21 15:20:09 +02:00
|
|
|
{
|
|
|
|
list_remove( &drive->entry );
|
2009-07-22 17:46:02 +02:00
|
|
|
if (drive->mount) delete_mount_point( drive->mount );
|
2009-07-22 20:55:24 +02:00
|
|
|
release_volume( drive->volume );
|
2009-07-20 15:46:34 +02:00
|
|
|
RtlFreeHeap( GetProcessHeap(), 0, drive );
|
2008-10-21 15:20:09 +02:00
|
|
|
}
|
|
|
|
|
2009-07-22 20:56:04 +02:00
|
|
|
/* find a volume that matches the parameters */
|
|
|
|
static struct volume *find_matching_volume( const char *udi, const char *device,
|
|
|
|
const char *mount_point, enum device_type type )
|
|
|
|
{
|
|
|
|
struct volume *volume;
|
|
|
|
struct disk_device *disk_device;
|
|
|
|
|
|
|
|
LIST_FOR_EACH_ENTRY( volume, &volumes_list, struct volume, entry )
|
|
|
|
{
|
2011-12-21 10:56:52 +01:00
|
|
|
int match = 0;
|
|
|
|
|
2009-07-22 20:56:04 +02:00
|
|
|
/* when we have a udi we only match drives added manually */
|
|
|
|
if (udi && volume->udi) continue;
|
|
|
|
/* and when we don't have a udi we only match dynamic drives */
|
|
|
|
if (!udi && !volume->udi) continue;
|
|
|
|
|
|
|
|
disk_device = volume->device;
|
|
|
|
if (disk_device->type != type) continue;
|
2011-12-21 10:56:52 +01:00
|
|
|
if (device && disk_device->unix_device)
|
|
|
|
{
|
|
|
|
if (strcmp( device, disk_device->unix_device )) continue;
|
|
|
|
match++;
|
|
|
|
}
|
|
|
|
if (mount_point && disk_device->unix_mount)
|
|
|
|
{
|
|
|
|
if (strcmp( mount_point, disk_device->unix_mount )) continue;
|
|
|
|
match++;
|
|
|
|
}
|
|
|
|
if (!match) continue;
|
2009-07-22 20:56:04 +02:00
|
|
|
TRACE( "found matching volume %s for device %s mount %s type %u\n",
|
|
|
|
debugstr_guid(&volume->guid), debugstr_a(device), debugstr_a(mount_point), type );
|
2009-08-04 12:36:34 +02:00
|
|
|
return grab_volume( volume );
|
2009-07-22 20:56:04 +02:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-07-21 20:30:43 +02:00
|
|
|
/* change the information for an existing volume */
|
2009-07-22 17:49:39 +02:00
|
|
|
static NTSTATUS set_volume_info( struct volume *volume, struct dos_drive *drive, const char *device,
|
|
|
|
const char *mount_point, enum device_type type, const GUID *guid )
|
2009-07-21 20:30:43 +02:00
|
|
|
{
|
2009-07-22 17:49:39 +02:00
|
|
|
void *id = NULL;
|
|
|
|
unsigned int id_len = 0;
|
2009-07-21 20:30:43 +02:00
|
|
|
struct disk_device *disk_device = volume->device;
|
|
|
|
NTSTATUS status;
|
|
|
|
|
|
|
|
if (type != disk_device->type)
|
|
|
|
{
|
|
|
|
if ((status = create_disk_device( type, &disk_device ))) return status;
|
|
|
|
if (volume->mount)
|
|
|
|
{
|
|
|
|
delete_mount_point( volume->mount );
|
|
|
|
volume->mount = NULL;
|
|
|
|
}
|
2009-07-22 17:49:39 +02:00
|
|
|
if (drive && drive->mount)
|
|
|
|
{
|
|
|
|
delete_mount_point( drive->mount );
|
|
|
|
drive->mount = NULL;
|
|
|
|
}
|
2009-07-21 20:30:43 +02:00
|
|
|
delete_disk_device( volume->device );
|
|
|
|
volume->device = disk_device;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
RtlFreeHeap( GetProcessHeap(), 0, disk_device->unix_device );
|
|
|
|
RtlFreeHeap( GetProcessHeap(), 0, disk_device->unix_mount );
|
|
|
|
}
|
|
|
|
disk_device->unix_device = strdupA( device );
|
|
|
|
disk_device->unix_mount = strdupA( mount_point );
|
|
|
|
|
2009-07-22 20:56:04 +02:00
|
|
|
if (guid && memcmp( &volume->guid, guid, sizeof(volume->guid) ))
|
2009-07-21 20:30:43 +02:00
|
|
|
{
|
|
|
|
volume->guid = *guid;
|
|
|
|
if (volume->mount)
|
|
|
|
{
|
|
|
|
delete_mount_point( volume->mount );
|
|
|
|
volume->mount = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!volume->mount)
|
|
|
|
volume->mount = add_volume_mount_point( disk_device->dev_obj, &disk_device->name, &volume->guid );
|
2009-07-22 17:49:39 +02:00
|
|
|
if (drive && !drive->mount)
|
|
|
|
drive->mount = add_dosdev_mount_point( disk_device->dev_obj, &disk_device->name, drive->drive );
|
2009-07-21 20:30:43 +02:00
|
|
|
|
2009-07-22 17:49:39 +02:00
|
|
|
if (disk_device->unix_mount)
|
2009-07-21 20:30:43 +02:00
|
|
|
{
|
2009-07-22 17:49:39 +02:00
|
|
|
id = disk_device->unix_mount;
|
|
|
|
id_len = strlen( disk_device->unix_mount ) + 1;
|
2009-07-21 20:30:43 +02:00
|
|
|
}
|
2009-07-22 17:49:39 +02:00
|
|
|
if (volume->mount) set_mount_point_id( volume->mount, id, id_len );
|
|
|
|
if (drive && drive->mount) set_mount_point_id( drive->mount, id, id_len );
|
|
|
|
|
2009-07-21 20:30:43 +02:00
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2009-07-22 20:56:04 +02:00
|
|
|
/* change the drive letter or volume for an existing drive */
|
|
|
|
static void set_drive_info( struct dos_drive *drive, int letter, struct volume *volume )
|
2008-10-21 15:20:09 +02:00
|
|
|
{
|
2009-07-22 20:56:04 +02:00
|
|
|
if (drive->drive != letter)
|
|
|
|
{
|
|
|
|
if (drive->mount) delete_mount_point( drive->mount );
|
|
|
|
drive->mount = NULL;
|
|
|
|
drive->drive = letter;
|
|
|
|
}
|
|
|
|
if (drive->volume != volume)
|
|
|
|
{
|
|
|
|
if (drive->mount) delete_mount_point( drive->mount );
|
|
|
|
drive->mount = NULL;
|
|
|
|
grab_volume( volume );
|
|
|
|
release_volume( drive->volume );
|
|
|
|
drive->volume = volume;
|
|
|
|
}
|
2008-10-21 15:20:09 +02:00
|
|
|
}
|
2008-10-21 15:02:10 +02:00
|
|
|
|
2006-10-03 14:54:16 +02:00
|
|
|
static inline int is_valid_device( struct stat *st )
|
|
|
|
{
|
|
|
|
#if defined(linux) || defined(__sun__)
|
|
|
|
return S_ISBLK( st->st_mode );
|
|
|
|
#else
|
|
|
|
/* disks are char devices on *BSD */
|
|
|
|
return S_ISCHR( st->st_mode );
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2006-10-03 12:40:27 +02:00
|
|
|
/* find or create a DOS drive for the corresponding device */
|
2008-11-12 10:47:49 +01:00
|
|
|
static int add_drive( const char *device, enum device_type type )
|
2006-10-03 12:40:27 +02:00
|
|
|
{
|
|
|
|
char *path, *p;
|
|
|
|
char in_use[26];
|
|
|
|
struct stat dev_st, drive_st;
|
|
|
|
int drive, first, last, avail = 0;
|
|
|
|
|
2006-10-03 14:54:16 +02:00
|
|
|
if (stat( device, &dev_st ) == -1 || !is_valid_device( &dev_st )) return -1;
|
2006-10-03 12:40:27 +02:00
|
|
|
|
2008-10-21 15:20:42 +02:00
|
|
|
if (!(path = get_dosdevices_path( &p ))) return -1;
|
2006-10-03 12:40:27 +02:00
|
|
|
|
|
|
|
memset( in_use, 0, sizeof(in_use) );
|
|
|
|
|
2008-11-12 10:47:49 +01:00
|
|
|
switch (type)
|
2006-10-03 12:40:27 +02:00
|
|
|
{
|
2008-11-12 10:47:49 +01:00
|
|
|
case DEVICE_FLOPPY:
|
2006-10-03 12:40:27 +02:00
|
|
|
first = 0;
|
|
|
|
last = 2;
|
2008-11-12 10:47:49 +01:00
|
|
|
break;
|
|
|
|
case DEVICE_CDROM:
|
2011-02-08 23:12:54 +01:00
|
|
|
case DEVICE_DVD:
|
2008-11-12 10:47:49 +01:00
|
|
|
first = 3;
|
|
|
|
last = 26;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
first = 2;
|
|
|
|
last = 26;
|
|
|
|
break;
|
2006-10-03 12:40:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
while (avail != -1)
|
|
|
|
{
|
|
|
|
avail = -1;
|
|
|
|
for (drive = first; drive < last; drive++)
|
|
|
|
{
|
|
|
|
if (in_use[drive]) continue; /* already checked */
|
|
|
|
*p = 'a' + drive;
|
|
|
|
if (stat( path, &drive_st ) == -1)
|
|
|
|
{
|
|
|
|
if (lstat( path, &drive_st ) == -1 && errno == ENOENT) /* this is a candidate */
|
|
|
|
{
|
|
|
|
if (avail == -1)
|
|
|
|
{
|
|
|
|
p[2] = 0;
|
|
|
|
/* if mount point symlink doesn't exist either, it's available */
|
|
|
|
if (lstat( path, &drive_st ) == -1 && errno == ENOENT) avail = drive;
|
|
|
|
p[2] = ':';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else in_use[drive] = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
in_use[drive] = 1;
|
2006-10-03 14:54:16 +02:00
|
|
|
if (!is_valid_device( &drive_st )) continue;
|
2006-10-03 12:40:27 +02:00
|
|
|
if (dev_st.st_rdev == drive_st.st_rdev) goto done;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (avail != -1)
|
|
|
|
{
|
|
|
|
/* try to use the one we found */
|
|
|
|
drive = avail;
|
|
|
|
*p = 'a' + drive;
|
|
|
|
if (symlink( device, path ) != -1) goto done;
|
|
|
|
/* failed, retry the search */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
drive = -1;
|
|
|
|
|
|
|
|
done:
|
|
|
|
HeapFree( GetProcessHeap(), 0, path );
|
|
|
|
return drive;
|
|
|
|
}
|
|
|
|
|
2008-10-21 15:20:09 +02:00
|
|
|
/* create devices for mapped drives */
|
|
|
|
static void create_drive_devices(void)
|
2008-10-21 15:02:10 +02:00
|
|
|
{
|
2008-10-23 15:43:46 +02:00
|
|
|
char *path, *p, *link, *device;
|
2008-10-21 15:20:09 +02:00
|
|
|
struct dos_drive *drive;
|
2009-07-22 20:56:04 +02:00
|
|
|
struct volume *volume;
|
2008-10-21 15:02:10 +02:00
|
|
|
unsigned int i;
|
2008-10-21 15:42:21 +02:00
|
|
|
HKEY drives_key;
|
2008-11-12 10:47:49 +01:00
|
|
|
enum device_type drive_type;
|
2008-10-21 15:42:21 +02:00
|
|
|
WCHAR driveW[] = {'a',':',0};
|
2008-10-21 15:02:10 +02:00
|
|
|
|
2008-10-21 15:20:42 +02:00
|
|
|
if (!(path = get_dosdevices_path( &p ))) return;
|
2008-10-21 15:42:21 +02:00
|
|
|
if (RegOpenKeyW( HKEY_LOCAL_MACHINE, drives_keyW, &drives_key )) drives_key = 0;
|
2008-10-21 15:02:10 +02:00
|
|
|
|
2008-10-21 15:20:09 +02:00
|
|
|
for (i = 0; i < MAX_DOS_DRIVES; i++)
|
|
|
|
{
|
|
|
|
p[0] = 'a' + i;
|
|
|
|
p[2] = 0;
|
|
|
|
if (!(link = read_symlink( path ))) continue;
|
2008-10-23 15:43:46 +02:00
|
|
|
p[2] = ':';
|
|
|
|
device = read_symlink( path );
|
2008-10-21 15:42:21 +02:00
|
|
|
|
2008-11-12 10:47:49 +01:00
|
|
|
drive_type = i < 2 ? DEVICE_FLOPPY : DEVICE_HARDDISK_VOL;
|
2008-10-21 15:42:21 +02:00
|
|
|
if (drives_key)
|
|
|
|
{
|
|
|
|
WCHAR buffer[32];
|
|
|
|
DWORD j, type, size = sizeof(buffer);
|
|
|
|
|
|
|
|
driveW[0] = 'a' + i;
|
|
|
|
if (!RegQueryValueExW( drives_key, driveW, NULL, &type, (BYTE *)buffer, &size ) &&
|
|
|
|
type == REG_SZ)
|
|
|
|
{
|
|
|
|
for (j = 0; j < sizeof(drive_types)/sizeof(drive_types[0]); j++)
|
|
|
|
if (drive_types[j][0] && !strcmpiW( buffer, drive_types[j] ))
|
|
|
|
{
|
|
|
|
drive_type = j;
|
|
|
|
break;
|
|
|
|
}
|
2008-11-12 10:47:49 +01:00
|
|
|
if (drive_type == DEVICE_FLOPPY && i >= 2) drive_type = DEVICE_HARDDISK;
|
2008-10-21 15:42:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-22 20:56:04 +02:00
|
|
|
volume = find_matching_volume( NULL, device, link, drive_type );
|
|
|
|
if (!create_dos_device( volume, NULL, i, drive_type, &drive ))
|
2008-10-21 15:02:10 +02:00
|
|
|
{
|
2009-07-22 20:56:04 +02:00
|
|
|
/* don't reset uuid if we used an existing volume */
|
|
|
|
const GUID *guid = volume ? NULL : get_default_uuid(i);
|
|
|
|
set_volume_info( drive->volume, drive, device, link, drive_type, guid );
|
2008-10-21 15:02:10 +02:00
|
|
|
}
|
2008-10-23 15:43:46 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
RtlFreeHeap( GetProcessHeap(), 0, link );
|
|
|
|
RtlFreeHeap( GetProcessHeap(), 0, device );
|
|
|
|
}
|
2009-08-04 12:36:34 +02:00
|
|
|
if (volume) release_volume( volume );
|
2008-10-21 15:02:10 +02:00
|
|
|
}
|
2008-10-21 15:42:21 +02:00
|
|
|
RegCloseKey( drives_key );
|
2008-10-21 15:20:09 +02:00
|
|
|
RtlFreeHeap( GetProcessHeap(), 0, path );
|
2008-10-21 15:02:10 +02:00
|
|
|
}
|
|
|
|
|
2009-07-21 20:30:43 +02:00
|
|
|
/* create a new disk volume */
|
|
|
|
NTSTATUS add_volume( const char *udi, const char *device, const char *mount_point,
|
|
|
|
enum device_type type, const GUID *guid )
|
|
|
|
{
|
|
|
|
struct volume *volume;
|
2009-07-22 21:13:00 +02:00
|
|
|
NTSTATUS status = STATUS_SUCCESS;
|
2009-07-21 20:30:43 +02:00
|
|
|
|
|
|
|
TRACE( "adding %s device %s mount %s type %u uuid %s\n", debugstr_a(udi),
|
|
|
|
debugstr_a(device), debugstr_a(mount_point), type, debugstr_guid(guid) );
|
|
|
|
|
2009-07-22 21:13:00 +02:00
|
|
|
EnterCriticalSection( &device_section );
|
2009-07-21 20:30:43 +02:00
|
|
|
LIST_FOR_EACH_ENTRY( volume, &volumes_list, struct volume, entry )
|
2011-02-01 00:07:16 +01:00
|
|
|
if (volume->udi && !strcmp( udi, volume->udi ))
|
|
|
|
{
|
|
|
|
grab_volume( volume );
|
|
|
|
goto found;
|
|
|
|
}
|
2009-07-21 20:30:43 +02:00
|
|
|
|
2009-07-22 20:56:04 +02:00
|
|
|
/* udi not found, search for a non-dynamic volume */
|
2009-07-22 21:13:00 +02:00
|
|
|
if ((volume = find_matching_volume( udi, device, mount_point, type ))) set_volume_udi( volume, udi );
|
|
|
|
else status = create_volume( udi, type, &volume );
|
2009-07-21 20:30:43 +02:00
|
|
|
|
|
|
|
found:
|
2009-07-22 21:13:00 +02:00
|
|
|
if (!status) status = set_volume_info( volume, NULL, device, mount_point, type, guid );
|
2009-08-04 12:36:34 +02:00
|
|
|
if (volume) release_volume( volume );
|
2009-07-22 21:13:00 +02:00
|
|
|
LeaveCriticalSection( &device_section );
|
|
|
|
return status;
|
2009-07-21 20:30:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* create a new disk volume */
|
|
|
|
NTSTATUS remove_volume( const char *udi )
|
|
|
|
{
|
2009-07-22 21:13:00 +02:00
|
|
|
NTSTATUS status = STATUS_NO_SUCH_DEVICE;
|
2009-07-21 20:30:43 +02:00
|
|
|
struct volume *volume;
|
|
|
|
|
2009-07-22 21:13:00 +02:00
|
|
|
EnterCriticalSection( &device_section );
|
2009-07-21 20:30:43 +02:00
|
|
|
LIST_FOR_EACH_ENTRY( volume, &volumes_list, struct volume, entry )
|
|
|
|
{
|
|
|
|
if (!volume->udi || strcmp( udi, volume->udi )) continue;
|
2009-07-22 20:55:24 +02:00
|
|
|
set_volume_udi( volume, NULL );
|
2009-07-22 21:13:00 +02:00
|
|
|
status = STATUS_SUCCESS;
|
|
|
|
break;
|
2009-07-21 20:30:43 +02:00
|
|
|
}
|
2009-07-22 21:13:00 +02:00
|
|
|
LeaveCriticalSection( &device_section );
|
|
|
|
return status;
|
2009-07-21 20:30:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-23 15:44:18 +02:00
|
|
|
/* create a new dos drive */
|
|
|
|
NTSTATUS add_dos_device( int letter, const char *udi, const char *device,
|
2009-07-20 16:05:04 +02:00
|
|
|
const char *mount_point, enum device_type type, const GUID *guid )
|
2006-10-03 12:40:27 +02:00
|
|
|
{
|
2009-07-22 18:04:36 +02:00
|
|
|
char *path, *p;
|
2009-07-22 18:25:38 +02:00
|
|
|
HKEY hkey;
|
2009-07-22 18:04:36 +02:00
|
|
|
NTSTATUS status = STATUS_SUCCESS;
|
2008-10-21 15:20:09 +02:00
|
|
|
struct dos_drive *drive, *next;
|
2009-07-22 21:13:00 +02:00
|
|
|
struct volume *volume;
|
|
|
|
int notify = -1;
|
2006-10-03 12:40:27 +02:00
|
|
|
|
2009-07-22 18:04:36 +02:00
|
|
|
if (!(path = get_dosdevices_path( &p ))) return STATUS_NO_MEMORY;
|
|
|
|
|
2009-07-22 21:13:00 +02:00
|
|
|
EnterCriticalSection( &device_section );
|
|
|
|
volume = find_matching_volume( udi, device, mount_point, type );
|
|
|
|
|
2008-10-23 15:44:18 +02:00
|
|
|
if (letter == -1) /* auto-assign a letter */
|
|
|
|
{
|
|
|
|
letter = add_drive( device, type );
|
2009-07-22 18:04:36 +02:00
|
|
|
if (letter == -1)
|
|
|
|
{
|
|
|
|
status = STATUS_OBJECT_NAME_COLLISION;
|
|
|
|
goto done;
|
|
|
|
}
|
2009-07-22 19:21:22 +02:00
|
|
|
|
|
|
|
LIST_FOR_EACH_ENTRY_SAFE( drive, next, &drives_list, struct dos_drive, entry )
|
|
|
|
{
|
|
|
|
if (drive->volume->udi && !strcmp( udi, drive->volume->udi )) goto found;
|
|
|
|
if (drive->drive == letter) delete_dos_device( drive );
|
|
|
|
}
|
2008-10-23 15:44:18 +02:00
|
|
|
}
|
|
|
|
else /* simply reset the device symlink */
|
|
|
|
{
|
2009-07-22 19:21:22 +02:00
|
|
|
LIST_FOR_EACH_ENTRY( drive, &drives_list, struct dos_drive, entry )
|
2009-07-22 20:56:04 +02:00
|
|
|
if (drive->drive == letter) break;
|
|
|
|
|
|
|
|
*p = 'a' + letter;
|
|
|
|
if (&drive->entry == &drives_list) update_symlink( path, device, NULL );
|
|
|
|
else
|
2008-10-21 15:20:09 +02:00
|
|
|
{
|
2009-07-22 19:21:22 +02:00
|
|
|
update_symlink( path, device, drive->volume->device->unix_device );
|
2009-07-22 20:56:04 +02:00
|
|
|
delete_dos_device( drive );
|
2008-10-21 15:20:09 +02:00
|
|
|
}
|
2006-10-03 12:40:27 +02:00
|
|
|
}
|
2008-10-21 15:20:09 +02:00
|
|
|
|
2009-07-22 20:56:04 +02:00
|
|
|
if ((status = create_dos_device( volume, udi, letter, type, &drive ))) goto done;
|
2006-10-03 12:40:27 +02:00
|
|
|
|
|
|
|
found:
|
2009-07-22 20:56:04 +02:00
|
|
|
if (!guid && !volume) guid = get_default_uuid( letter );
|
2009-08-04 12:36:34 +02:00
|
|
|
if (!volume) volume = grab_volume( drive->volume );
|
2009-07-22 20:56:04 +02:00
|
|
|
set_drive_info( drive, letter, volume );
|
2009-07-22 18:04:36 +02:00
|
|
|
p[0] = 'a' + drive->drive;
|
|
|
|
p[2] = 0;
|
2009-07-22 20:56:04 +02:00
|
|
|
update_symlink( path, mount_point, volume->device->unix_mount );
|
|
|
|
set_volume_info( volume, drive, device, mount_point, type, guid );
|
2008-10-21 15:20:09 +02:00
|
|
|
|
2009-07-22 18:25:38 +02:00
|
|
|
TRACE( "added device %c: udi %s for %s on %s type %u\n",
|
|
|
|
'a' + drive->drive, wine_dbgstr_a(udi), wine_dbgstr_a(device),
|
|
|
|
wine_dbgstr_a(mount_point), type );
|
|
|
|
|
|
|
|
/* hack: force the drive type in the registry */
|
|
|
|
if (!RegCreateKeyW( HKEY_LOCAL_MACHINE, drives_keyW, &hkey ))
|
2006-10-03 12:40:27 +02:00
|
|
|
{
|
2009-07-22 18:25:38 +02:00
|
|
|
const WCHAR *type_name = drive_types[type];
|
2011-11-21 19:19:49 +01:00
|
|
|
WCHAR name[] = {'a',':',0};
|
2009-07-22 18:25:38 +02:00
|
|
|
|
|
|
|
name[0] += drive->drive;
|
|
|
|
if (!type_name[0] && type == DEVICE_HARDDISK) type_name = drive_types[DEVICE_FLOPPY];
|
|
|
|
if (type_name[0])
|
|
|
|
RegSetValueExW( hkey, name, 0, REG_SZ, (const BYTE *)type_name,
|
|
|
|
(strlenW(type_name) + 1) * sizeof(WCHAR) );
|
|
|
|
else
|
|
|
|
RegDeleteValueW( hkey, name );
|
|
|
|
RegCloseKey( hkey );
|
|
|
|
}
|
2006-10-03 12:40:27 +02:00
|
|
|
|
2009-07-22 21:13:00 +02:00
|
|
|
if (udi) notify = drive->drive;
|
2006-10-03 12:40:27 +02:00
|
|
|
|
2009-07-22 18:04:36 +02:00
|
|
|
done:
|
2009-08-04 12:36:34 +02:00
|
|
|
if (volume) release_volume( volume );
|
2009-07-22 21:13:00 +02:00
|
|
|
LeaveCriticalSection( &device_section );
|
2009-07-22 18:04:36 +02:00
|
|
|
RtlFreeHeap( GetProcessHeap(), 0, path );
|
2009-07-22 21:13:00 +02:00
|
|
|
if (notify != -1) send_notify( notify, DBT_DEVICEARRIVAL );
|
2009-07-22 18:04:36 +02:00
|
|
|
return status;
|
2006-10-03 12:40:27 +02:00
|
|
|
}
|
|
|
|
|
2008-10-23 15:44:18 +02:00
|
|
|
/* remove an existing dos drive, by letter or udi */
|
|
|
|
NTSTATUS remove_dos_device( int letter, const char *udi )
|
2006-10-03 12:40:27 +02:00
|
|
|
{
|
2009-07-22 21:13:00 +02:00
|
|
|
NTSTATUS status = STATUS_NO_SUCH_DEVICE;
|
2006-10-03 12:40:27 +02:00
|
|
|
HKEY hkey;
|
|
|
|
struct dos_drive *drive;
|
2009-07-22 17:57:08 +02:00
|
|
|
char *path, *p;
|
2009-07-22 21:13:00 +02:00
|
|
|
int notify = -1;
|
2006-10-03 12:40:27 +02:00
|
|
|
|
2009-07-22 21:13:00 +02:00
|
|
|
EnterCriticalSection( &device_section );
|
2006-10-03 12:40:27 +02:00
|
|
|
LIST_FOR_EACH_ENTRY( drive, &drives_list, struct dos_drive, entry )
|
|
|
|
{
|
2008-10-23 15:44:18 +02:00
|
|
|
if (udi)
|
|
|
|
{
|
2009-07-20 15:56:26 +02:00
|
|
|
if (!drive->volume->udi) continue;
|
|
|
|
if (strcmp( udi, drive->volume->udi )) continue;
|
2009-07-22 20:55:24 +02:00
|
|
|
set_volume_udi( drive->volume, NULL );
|
2008-10-23 15:44:18 +02:00
|
|
|
}
|
2009-07-22 17:57:08 +02:00
|
|
|
else if (drive->drive != letter) continue;
|
2006-10-03 12:40:27 +02:00
|
|
|
|
2009-07-22 17:57:08 +02:00
|
|
|
if ((path = get_dosdevices_path( &p )))
|
2006-10-03 12:40:27 +02:00
|
|
|
{
|
2009-07-22 17:57:08 +02:00
|
|
|
p[0] = 'a' + drive->drive;
|
|
|
|
p[2] = 0;
|
|
|
|
unlink( path );
|
|
|
|
RtlFreeHeap( GetProcessHeap(), 0, path );
|
|
|
|
}
|
2006-10-03 12:40:27 +02:00
|
|
|
|
2009-07-22 17:57:08 +02:00
|
|
|
/* clear the registry key too */
|
|
|
|
if (!RegOpenKeyW( HKEY_LOCAL_MACHINE, drives_keyW, &hkey ))
|
|
|
|
{
|
2011-11-21 19:19:49 +01:00
|
|
|
WCHAR name[] = {'a',':',0};
|
2009-07-22 17:57:08 +02:00
|
|
|
name[0] += drive->drive;
|
|
|
|
RegDeleteValueW( hkey, name );
|
|
|
|
RegCloseKey( hkey );
|
2006-10-03 12:40:27 +02:00
|
|
|
}
|
2009-07-22 17:57:08 +02:00
|
|
|
|
2009-07-22 21:13:00 +02:00
|
|
|
if (udi && drive->volume->device->unix_mount) notify = drive->drive;
|
2009-07-22 17:57:08 +02:00
|
|
|
|
2009-07-20 15:46:34 +02:00
|
|
|
delete_dos_device( drive );
|
2009-07-22 21:13:00 +02:00
|
|
|
status = STATUS_SUCCESS;
|
|
|
|
break;
|
2006-10-03 12:40:27 +02:00
|
|
|
}
|
2009-07-22 21:13:00 +02:00
|
|
|
LeaveCriticalSection( &device_section );
|
|
|
|
if (notify != -1) send_notify( notify, DBT_DEVICEREMOVECOMPLETE );
|
|
|
|
return status;
|
2006-10-03 12:40:27 +02:00
|
|
|
}
|
2008-10-21 15:02:10 +02:00
|
|
|
|
2008-10-23 15:52:39 +02:00
|
|
|
/* query information about an existing dos drive, by letter or udi */
|
2009-07-22 21:02:47 +02:00
|
|
|
NTSTATUS query_dos_device( int letter, enum device_type *type, char **device, char **mount_point )
|
2008-10-23 15:52:39 +02:00
|
|
|
{
|
2009-07-22 21:13:00 +02:00
|
|
|
NTSTATUS status = STATUS_NO_SUCH_DEVICE;
|
2008-10-23 15:52:39 +02:00
|
|
|
struct dos_drive *drive;
|
2009-07-20 15:46:34 +02:00
|
|
|
struct disk_device *disk_device;
|
2008-10-23 15:52:39 +02:00
|
|
|
|
2009-07-22 21:13:00 +02:00
|
|
|
EnterCriticalSection( &device_section );
|
2008-10-23 15:52:39 +02:00
|
|
|
LIST_FOR_EACH_ENTRY( drive, &drives_list, struct dos_drive, entry )
|
|
|
|
{
|
|
|
|
if (drive->drive != letter) continue;
|
2009-07-20 15:56:26 +02:00
|
|
|
disk_device = drive->volume->device;
|
2009-07-20 15:46:34 +02:00
|
|
|
if (type) *type = disk_device->type;
|
2009-07-22 21:02:47 +02:00
|
|
|
if (device) *device = strdupA( disk_device->unix_device );
|
|
|
|
if (mount_point) *mount_point = strdupA( disk_device->unix_mount );
|
2009-07-22 21:13:00 +02:00
|
|
|
status = STATUS_SUCCESS;
|
|
|
|
break;
|
2008-10-23 15:52:39 +02:00
|
|
|
}
|
2009-07-22 21:13:00 +02:00
|
|
|
LeaveCriticalSection( &device_section );
|
|
|
|
return status;
|
2008-10-23 15:52:39 +02:00
|
|
|
}
|
|
|
|
|
2008-10-21 15:02:10 +02:00
|
|
|
/* handler for ioctls on the harddisk device */
|
|
|
|
static NTSTATUS WINAPI harddisk_ioctl( DEVICE_OBJECT *device, IRP *irp )
|
|
|
|
{
|
2010-03-23 17:08:03 +01:00
|
|
|
IO_STACK_LOCATION *irpsp = IoGetCurrentIrpStackLocation( irp );
|
2009-07-20 15:46:34 +02:00
|
|
|
struct disk_device *dev = device->DeviceExtension;
|
2008-10-21 15:02:10 +02:00
|
|
|
|
|
|
|
TRACE( "ioctl %x insize %u outsize %u\n",
|
|
|
|
irpsp->Parameters.DeviceIoControl.IoControlCode,
|
|
|
|
irpsp->Parameters.DeviceIoControl.InputBufferLength,
|
|
|
|
irpsp->Parameters.DeviceIoControl.OutputBufferLength );
|
|
|
|
|
2009-07-22 21:13:00 +02:00
|
|
|
EnterCriticalSection( &device_section );
|
|
|
|
|
2008-10-21 15:02:10 +02:00
|
|
|
switch(irpsp->Parameters.DeviceIoControl.IoControlCode)
|
|
|
|
{
|
|
|
|
case IOCTL_DISK_GET_DRIVE_GEOMETRY:
|
|
|
|
{
|
|
|
|
DISK_GEOMETRY info;
|
|
|
|
DWORD len = min( sizeof(info), irpsp->Parameters.DeviceIoControl.OutputBufferLength );
|
|
|
|
|
|
|
|
info.Cylinders.QuadPart = 10000;
|
2009-07-20 15:46:34 +02:00
|
|
|
info.MediaType = (dev->devnum.DeviceType == FILE_DEVICE_DISK) ? FixedMedia : RemovableMedia;
|
2008-10-21 15:02:10 +02:00
|
|
|
info.TracksPerCylinder = 255;
|
|
|
|
info.SectorsPerTrack = 63;
|
|
|
|
info.BytesPerSector = 512;
|
2011-11-01 13:03:12 +01:00
|
|
|
memcpy( irp->AssociatedIrp.SystemBuffer, &info, len );
|
2008-10-21 15:02:10 +02:00
|
|
|
irp->IoStatus.Information = len;
|
|
|
|
irp->IoStatus.u.Status = STATUS_SUCCESS;
|
|
|
|
break;
|
|
|
|
}
|
2011-03-09 20:45:18 +01:00
|
|
|
case IOCTL_DISK_GET_DRIVE_GEOMETRY_EX:
|
|
|
|
{
|
|
|
|
DISK_GEOMETRY_EX info;
|
|
|
|
DWORD len = min( sizeof(info), irpsp->Parameters.DeviceIoControl.OutputBufferLength );
|
|
|
|
|
|
|
|
FIXME("The DISK_PARTITION_INFO and DISK_DETECTION_INFO structures will not be filled\n");
|
|
|
|
|
|
|
|
info.Geometry.Cylinders.QuadPart = 10000;
|
|
|
|
info.Geometry.MediaType = (dev->devnum.DeviceType == FILE_DEVICE_DISK) ? FixedMedia : RemovableMedia;
|
|
|
|
info.Geometry.TracksPerCylinder = 255;
|
|
|
|
info.Geometry.SectorsPerTrack = 63;
|
|
|
|
info.Geometry.BytesPerSector = 512;
|
|
|
|
info.DiskSize.QuadPart = info.Geometry.Cylinders.QuadPart * info.Geometry.TracksPerCylinder *
|
|
|
|
info.Geometry.SectorsPerTrack * info.Geometry.BytesPerSector;
|
|
|
|
info.Data[0] = 0;
|
2011-11-01 13:03:12 +01:00
|
|
|
memcpy( irp->AssociatedIrp.SystemBuffer, &info, len );
|
2011-03-09 20:45:18 +01:00
|
|
|
irp->IoStatus.Information = len;
|
|
|
|
irp->IoStatus.u.Status = STATUS_SUCCESS;
|
|
|
|
break;
|
|
|
|
}
|
2008-10-21 15:02:10 +02:00
|
|
|
case IOCTL_STORAGE_GET_DEVICE_NUMBER:
|
|
|
|
{
|
2009-07-20 15:46:34 +02:00
|
|
|
DWORD len = min( sizeof(dev->devnum), irpsp->Parameters.DeviceIoControl.OutputBufferLength );
|
2008-10-21 15:02:10 +02:00
|
|
|
|
2011-11-01 13:03:12 +01:00
|
|
|
memcpy( irp->AssociatedIrp.SystemBuffer, &dev->devnum, len );
|
2008-10-21 15:02:10 +02:00
|
|
|
irp->IoStatus.Information = len;
|
|
|
|
irp->IoStatus.u.Status = STATUS_SUCCESS;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case IOCTL_CDROM_READ_TOC:
|
|
|
|
irp->IoStatus.u.Status = STATUS_INVALID_DEVICE_REQUEST;
|
|
|
|
break;
|
2011-01-31 19:44:22 +01:00
|
|
|
case IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS:
|
2011-01-31 11:31:46 +01:00
|
|
|
{
|
|
|
|
DWORD len = min( 32, irpsp->Parameters.DeviceIoControl.OutputBufferLength );
|
|
|
|
|
2011-01-31 19:44:22 +01:00
|
|
|
FIXME( "returning zero-filled buffer for IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS\n" );
|
2011-11-01 13:03:12 +01:00
|
|
|
memset( irp->AssociatedIrp.SystemBuffer, 0, len );
|
2011-01-31 11:31:46 +01:00
|
|
|
irp->IoStatus.Information = len;
|
|
|
|
irp->IoStatus.u.Status = STATUS_SUCCESS;
|
|
|
|
break;
|
|
|
|
}
|
2008-10-21 15:02:10 +02:00
|
|
|
default:
|
2011-07-04 22:06:24 +02:00
|
|
|
{
|
|
|
|
ULONG code = irpsp->Parameters.DeviceIoControl.IoControlCode;
|
|
|
|
FIXME("Unsupported ioctl %x (device=%x access=%x func=%x method=%x)\n",
|
|
|
|
code, code >> 16, (code >> 14) & 3, (code >> 2) & 0xfff, code & 3);
|
2008-10-21 15:02:10 +02:00
|
|
|
irp->IoStatus.u.Status = STATUS_NOT_SUPPORTED;
|
|
|
|
break;
|
|
|
|
}
|
2011-07-04 22:06:24 +02:00
|
|
|
}
|
2009-07-22 21:13:00 +02:00
|
|
|
|
|
|
|
LeaveCriticalSection( &device_section );
|
2010-03-23 17:08:03 +01:00
|
|
|
IoCompleteRequest( irp, IO_NO_INCREMENT );
|
2008-10-21 15:02:10 +02:00
|
|
|
return irp->IoStatus.u.Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* driver entry point for the harddisk driver */
|
|
|
|
NTSTATUS WINAPI harddisk_driver_entry( DRIVER_OBJECT *driver, UNICODE_STRING *path )
|
|
|
|
{
|
2009-07-20 15:46:34 +02:00
|
|
|
struct disk_device *device;
|
2008-10-21 15:02:10 +02:00
|
|
|
|
2008-10-21 15:20:09 +02:00
|
|
|
harddisk_driver = driver;
|
2008-10-21 15:02:10 +02:00
|
|
|
driver->MajorFunction[IRP_MJ_DEVICE_CONTROL] = harddisk_ioctl;
|
|
|
|
|
2008-10-21 16:11:51 +02:00
|
|
|
/* create a harddisk0 device that isn't assigned to any drive */
|
2009-07-20 15:46:34 +02:00
|
|
|
create_disk_device( DEVICE_HARDDISK, &device );
|
2008-10-21 15:20:09 +02:00
|
|
|
|
|
|
|
create_drive_devices();
|
2008-10-21 15:02:10 +02:00
|
|
|
|
2008-10-21 16:11:51 +02:00
|
|
|
return STATUS_SUCCESS;
|
2008-10-21 15:02:10 +02:00
|
|
|
}
|