2000-02-17 20:38:33 +01:00
|
|
|
/* fhandler_floppy.cc. See fhandler.h for a description of the
|
|
|
|
fhandler classes.
|
|
|
|
|
2013-01-21 05:38:31 +01:00
|
|
|
Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
|
2013-03-31 14:37:45 +02:00
|
|
|
2011, 2012, 2013 Red Hat, Inc.
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
This file is part of Cygwin.
|
|
|
|
|
|
|
|
This software is a copyrighted work licensed under the terms of the
|
|
|
|
Cygwin license. Please consult the file "CYGWIN_LICENSE" for
|
|
|
|
details. */
|
|
|
|
|
2000-08-02 18:28:18 +02:00
|
|
|
#include "winsup.h"
|
2011-01-12 10:16:51 +01:00
|
|
|
#include <alloca.h>
|
2000-02-17 20:38:33 +01:00
|
|
|
#include <unistd.h>
|
2012-03-08 10:36:11 +01:00
|
|
|
#include <sys/param.h>
|
2001-07-26 21:22:24 +02:00
|
|
|
#include <winioctl.h>
|
2005-09-28 21:33:18 +02:00
|
|
|
#include <cygwin/rdevio.h>
|
2002-11-07 15:16:29 +01:00
|
|
|
#include <cygwin/hdreg.h>
|
|
|
|
#include <cygwin/fs.h>
|
2005-01-12 23:40:46 +01:00
|
|
|
#include "cygerrno.h"
|
2001-07-26 21:22:24 +02:00
|
|
|
#include "security.h"
|
* devices.cc: New file.
* devices.gperf: New file.
* devices.shilka: New file.
* cygwin-gperf: New file.
* cygwin-shilka: New file.
* fhandler_fifo.cc: New file.
* fhandler_nodevice.cc : New file. Reorganize headers so that path.h precedes
fhandler.h throughout. Remove device argument and unit arguments from fhandler
constructors throughout. Remove pc arguments to fhandler functions and use
internal pc element instead, throughout. Use dev element in pc throughout.
Use major/minor elements rather than units and device numbers previously in
fhandler class. Use correct methods for fhandler file names rather than
directly accessing file name variables, throughout.
* Makefile.in (DLL_OFILES): Add devices.o, fhandler_fifo.o
* dcrt0.cc (dll_crt0_1): Call device::init.
* devices.h: Renumber devices based on more Linux-like major/minor numbers.
Add more devices. Declare standard device storage.
(device): Declare struct.
* dir.cc (opendir): Use new 'build_fh_name' to construct a fhandler_* type.
* dtable.cc (dtable::get_debugger_info): Ditto.
(cygwin_attach_handle_to_fd): Ditto.
(dtable::release): Remove special FH_SOCKET case in favor of generic
"need_fixup_before" test.
(dtable::init_std_file_from_handle): Use either build_fh_dev or build_fh_name
to build standard fhandler.
(dtable::build_fh_name): Renamed from dtable::build_fhandler_from_name. Move
out of dtable class. Don't accept a path_conv argument. Just build it here
and pass it to:
(build_fh_pc): Renamed from dtable::build_fhandler. Move out of dtable class.
Use intrinsic device type in path_conv to create new fhandler.
(build_fh_dev): Renamed from dtable::build_fhandler. Move out of dtable class.
Simplify arguments to just take new 'device' type and a name. Just return
pointer to fhandler rather than trying to insert into dtable.
(dtable::dup_worker): Accommodate above build_fh name changes.
(dtable::find_fifo): New (currently broken) function.
(handle_to_fn): Use strechr for efficiency.
* dtable.h: Reflect above build_fh name changes and argument differences.
(fhandler_base *&operator []): Return self rather than copy of self.
* fhandler.cc (fhandler_base::operator =): Use pc element to set normalized
path.
(fhandler_base::set_name): Ditto.
(fhandler_base::raw_read): Use method to access name.
(fhandler_base::write): Correctly use get_output_handle rather than get_handle.
(handler_base::device_access_denied): New function.
(fhandler_base::open): Eliminate pc argument and use pc element of
fhandler_base throughout.
(fhandler_base::fstat): Detect if device is based in filesystem and use
fstat_fs to calculate stat, if so.
(fhandler_base::fhandler_base): Eliminate handling of file names and, instead,
just free appropriate component from pc.
(fhandler_base::opendir): Remove path_conv parameter.
* fhandler.h: Remove all device flags.
(fhandler_base::pc): New element.
(fhandler_base::set_name): Change argument to path_conv.
(fhandler_base::error): New function.
(fhandler_base::exists): New function.
(fhandler_base::pc_binmode): New function.
(fhandler_base::dev): New function.
(fhandler_base::open_fs): New function.
(fhandler_base::fstat_fs): New function.
(fhandler_base::fstat_by_name): New function.
(fhandler_base::fstat_by_handle): New function.
(fhandler_base::isfifo): New function.
(fhandler_base::is_slow): New function.
(fhandler_base::is_auto_device): New function.
(fhandler_base::is_fs_special): New function.
(fhandler_base::device_access_denied): New function.
(fhandler_base::operator DWORD&): New operator.
(fhandler_base::get_name): Return normalized path from pc.
(fhandler_base::get_win32_name): Return windows path from pc.
(fhandler_base::isdevice): Renamed from is_device.
(fhandler_base::get_native_name): Return device format.
(fhandler_fifo): New class.
(fhandler_nodevice): New class.
(select_stuff::device_specific): Remove array.
(select_stuff::device_specific_pipe): New class element.
(select_stuff::device_specific_socket): New class element.
(select_stuff::device_specific_serial): New class element.
(select_stuff::select_stuff): Initialize new elements.
* fhandler_disk_file.cc (fhandler_base::fstat_by_handle): Move to base class
from fhandler_disk_file.
(fhandler_base::fstat_by_name): Ditto.
(fhandler_base::fstat_by_name): Ditto.
(fhandler_disk_file::open): Move most functionality into
fhandler_base::open_fs.
(fhandler_base::open_fs): New function.
(fhandler_disk_file::close): Move most functionality into
fhandler_base::close_fs.
(fhandler_base::close_fs): New function.
* fhandler_mem.cc (fhandler_dev_mem::open): Use device name in debugging
output.
* fhandler_socket.cc (fhandler_socket::set_connect_secret): Copy standard
urandom device into appropriate place.
(fhandler_socket::accept): Reflect change in fdsock return value.
* fhandler_tty.cc: See "throughouts" above.
* net.cc: Accommodate fdsock change throughout.
(fdsock): Return success or failure, accept fd argument and device argument.
* path.cc (symlink_info::major): New element.
(symlink_info::minor): New element.
(symlink_info::parse_device): Declare new function.
(fs_info::update): Accommodate changes in path_conv class.
(path_conv::fillin): Ditto.
(path_conv::return_and_clear_normalized_path): Eliminate.
(path_conv::set_normalized_path): New function.
(path_conv::path_conv): Set info in dev element. Use path_conv methods Check
for FH_FS rather than FH_BAD to indicate when to fill in filesystem stuff.
where appropriate rather than direct access. Use set_normalized_path to set
normalized path.
(windows_device_names): Eliminate.
(get_dev): Ditto.
(get_raw_device_number): Ditto.
(get_device_number): Ditto.
(win32_device_name): Call new device name parser to do most of the heavy
lifting.
(mount_info::conv_to_win32_path): Fill in dev field as appropriate.
(symlink_worker): Handle new device files.
(symlink_info::check): Ditto.
(symlink_info::parse_device): Define new function.
* path.h (executable_states): Move here from fhandler.h.
(fs_info): Rename variables to *_storage and create methods for accessing same.
(path_conv): Add dev element, remove devn and unit and adjust inline methods to
accommodate.
(set_normalized_path): Declare new function.
* pinfo.cc (_pinfo::commune_recv): Add broken support for handling fifos.
(_pinfo::commune_send): Ditto.
* pipe.cc (fhandler_pipe::close): check for existence of handle before closing
it.
(handler_pipe::create): Rename from make_pipe. Change arguments to accept
fhandler_pipe array. Accommodate fifos.
(pipe): Rework to deal with fhandler_pipe::create changes.
(_pipe): Ditto.
* select.cc: Use individual device_specific types throughout rather than
indexing with obsolete device number.
(set_bits): Use is_socket call rather than checking device number.
* shared_info.h (CURR_MOUNT_MAGIC): Update.
(conv_to_win32_path): Reflect addition of device argument.
* syscalls.cc (mknod_worker): New function.
(open): Use build_fh_name to build fhandler.
(chown_worker): Detect if this is an 'auto' device rather than an on-filesystem
device and handle appropriately.
(chmod_device): New function.
(chmod): Detect if this is an 'auto' device rather than an on-filesystem device
and handle appropriately. Use chmod_device to set mode of in-filesystem
devices.
(stat_worker): Eliminate path_conv argument. Call build_fh_name to construct
fhandler. Use fh->error() rather than pc->error to detect errors in fhandler
construction.
(access_worker): New function pulled from access. Accommodate in-filesystem
devices.
(access): Use access_worker.
(fpathconf): Detect if this is an 'auto' device rather than an on-filesystem
device and handle appropriately.
(mknod_worker): New function.
(mknod32): New function.
(chroot): Free normalized path -- assuming it was actually cmalloced.
* tty.cc (create_tty_master): Tweak for new device class.
(tty::common_init): Ditto.
* winsup.h (stat_worker): Remove.
(symlink_worker): Declare.
* exceptions.cc (set_process_mask): Just call sig_dispatch_pending and don't
worry about pending_signals since sig_dispatch_pending should always do the
right thing now.
(sig_handle): Reorganize SIGCONT handling to more closely conform to SUSv3.
* pinfo.h: Move __SIG enum to sigproc.h.
(PICOM_FIFO): New enum element.
(_pinfo): Remove 'thread2signal' stuff throughout class.
(_pinfo::commune_send): Make varargs.
(_pinfo::sigtodo): Eliminate.
(_pinfo::thread2signal): Ditto.
* signal.cc (kill_worker): Eliminate call to setthread2signal.
* sigproc.cc (local_sigtodo): Eliminate.
(getlocal_sigtodo): Ditto.
(sigelem): New class.
(pending_signals): New class.
(sigqueue): New variable, start of sigqueue linked list.
(sigcatch_nonmain): Eliminate.
(sigcatch_main): Eliminate.
(sigcatch_nosync): Eliminate.
(sigcomplete_nonmain): Eliminate.
(pending_signals): Eliminate.
(sig_clear): Call signal thread to clear pending signals, unless already in
signal thread.
(sigpending): Call signal thread to get pending signals.
(sig_dispatch_pending): Eliminate use of pending_signals and just check
sigqueue.
(sigproc_terminate): Eliminate all of the obsolete semaphore stuff. Close
signal pipe handle.
(sig_send): Eliminate all of the obsolete semaphore stuff and use pipe to send
signals.
(getevent): Eliminate.
(pending_signals::add): New function.
(pending_signals::del): New function.
(pending_signals::next): New function.
(wait_sig): Eliminate all of the obsolete semaphore stuff. Use pipe to
communicate and maintain a linked list of signals.
* sigproc.h: Move __SIG defines here. Add __SIGPENDING.
(sig_dispatch_pending): Remove "C" specifier.
(sig_handle): Accept a mask argument.
* thread.cc: Remove signal handling considerations throughout.
2003-09-25 02:37:18 +02:00
|
|
|
#include "path.h"
|
2000-08-22 07:10:20 +02:00
|
|
|
#include "fhandler.h"
|
2006-03-24 15:52:08 +01:00
|
|
|
#include "ntdll.h"
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2005-09-28 21:33:18 +02:00
|
|
|
#define IS_EOM(err) ((err) == ERROR_INVALID_PARAMETER \
|
|
|
|
|| (err) == ERROR_SEEK \
|
|
|
|
|| (err) == ERROR_SECTOR_NOT_FOUND)
|
|
|
|
|
2012-10-13 14:34:18 +02:00
|
|
|
#define bytes_per_sector devbufalign
|
|
|
|
|
2000-02-17 20:38:33 +01:00
|
|
|
/**********************************************************************/
|
|
|
|
/* fhandler_dev_floppy */
|
|
|
|
|
* devices.cc: New file.
* devices.gperf: New file.
* devices.shilka: New file.
* cygwin-gperf: New file.
* cygwin-shilka: New file.
* fhandler_fifo.cc: New file.
* fhandler_nodevice.cc : New file. Reorganize headers so that path.h precedes
fhandler.h throughout. Remove device argument and unit arguments from fhandler
constructors throughout. Remove pc arguments to fhandler functions and use
internal pc element instead, throughout. Use dev element in pc throughout.
Use major/minor elements rather than units and device numbers previously in
fhandler class. Use correct methods for fhandler file names rather than
directly accessing file name variables, throughout.
* Makefile.in (DLL_OFILES): Add devices.o, fhandler_fifo.o
* dcrt0.cc (dll_crt0_1): Call device::init.
* devices.h: Renumber devices based on more Linux-like major/minor numbers.
Add more devices. Declare standard device storage.
(device): Declare struct.
* dir.cc (opendir): Use new 'build_fh_name' to construct a fhandler_* type.
* dtable.cc (dtable::get_debugger_info): Ditto.
(cygwin_attach_handle_to_fd): Ditto.
(dtable::release): Remove special FH_SOCKET case in favor of generic
"need_fixup_before" test.
(dtable::init_std_file_from_handle): Use either build_fh_dev or build_fh_name
to build standard fhandler.
(dtable::build_fh_name): Renamed from dtable::build_fhandler_from_name. Move
out of dtable class. Don't accept a path_conv argument. Just build it here
and pass it to:
(build_fh_pc): Renamed from dtable::build_fhandler. Move out of dtable class.
Use intrinsic device type in path_conv to create new fhandler.
(build_fh_dev): Renamed from dtable::build_fhandler. Move out of dtable class.
Simplify arguments to just take new 'device' type and a name. Just return
pointer to fhandler rather than trying to insert into dtable.
(dtable::dup_worker): Accommodate above build_fh name changes.
(dtable::find_fifo): New (currently broken) function.
(handle_to_fn): Use strechr for efficiency.
* dtable.h: Reflect above build_fh name changes and argument differences.
(fhandler_base *&operator []): Return self rather than copy of self.
* fhandler.cc (fhandler_base::operator =): Use pc element to set normalized
path.
(fhandler_base::set_name): Ditto.
(fhandler_base::raw_read): Use method to access name.
(fhandler_base::write): Correctly use get_output_handle rather than get_handle.
(handler_base::device_access_denied): New function.
(fhandler_base::open): Eliminate pc argument and use pc element of
fhandler_base throughout.
(fhandler_base::fstat): Detect if device is based in filesystem and use
fstat_fs to calculate stat, if so.
(fhandler_base::fhandler_base): Eliminate handling of file names and, instead,
just free appropriate component from pc.
(fhandler_base::opendir): Remove path_conv parameter.
* fhandler.h: Remove all device flags.
(fhandler_base::pc): New element.
(fhandler_base::set_name): Change argument to path_conv.
(fhandler_base::error): New function.
(fhandler_base::exists): New function.
(fhandler_base::pc_binmode): New function.
(fhandler_base::dev): New function.
(fhandler_base::open_fs): New function.
(fhandler_base::fstat_fs): New function.
(fhandler_base::fstat_by_name): New function.
(fhandler_base::fstat_by_handle): New function.
(fhandler_base::isfifo): New function.
(fhandler_base::is_slow): New function.
(fhandler_base::is_auto_device): New function.
(fhandler_base::is_fs_special): New function.
(fhandler_base::device_access_denied): New function.
(fhandler_base::operator DWORD&): New operator.
(fhandler_base::get_name): Return normalized path from pc.
(fhandler_base::get_win32_name): Return windows path from pc.
(fhandler_base::isdevice): Renamed from is_device.
(fhandler_base::get_native_name): Return device format.
(fhandler_fifo): New class.
(fhandler_nodevice): New class.
(select_stuff::device_specific): Remove array.
(select_stuff::device_specific_pipe): New class element.
(select_stuff::device_specific_socket): New class element.
(select_stuff::device_specific_serial): New class element.
(select_stuff::select_stuff): Initialize new elements.
* fhandler_disk_file.cc (fhandler_base::fstat_by_handle): Move to base class
from fhandler_disk_file.
(fhandler_base::fstat_by_name): Ditto.
(fhandler_base::fstat_by_name): Ditto.
(fhandler_disk_file::open): Move most functionality into
fhandler_base::open_fs.
(fhandler_base::open_fs): New function.
(fhandler_disk_file::close): Move most functionality into
fhandler_base::close_fs.
(fhandler_base::close_fs): New function.
* fhandler_mem.cc (fhandler_dev_mem::open): Use device name in debugging
output.
* fhandler_socket.cc (fhandler_socket::set_connect_secret): Copy standard
urandom device into appropriate place.
(fhandler_socket::accept): Reflect change in fdsock return value.
* fhandler_tty.cc: See "throughouts" above.
* net.cc: Accommodate fdsock change throughout.
(fdsock): Return success or failure, accept fd argument and device argument.
* path.cc (symlink_info::major): New element.
(symlink_info::minor): New element.
(symlink_info::parse_device): Declare new function.
(fs_info::update): Accommodate changes in path_conv class.
(path_conv::fillin): Ditto.
(path_conv::return_and_clear_normalized_path): Eliminate.
(path_conv::set_normalized_path): New function.
(path_conv::path_conv): Set info in dev element. Use path_conv methods Check
for FH_FS rather than FH_BAD to indicate when to fill in filesystem stuff.
where appropriate rather than direct access. Use set_normalized_path to set
normalized path.
(windows_device_names): Eliminate.
(get_dev): Ditto.
(get_raw_device_number): Ditto.
(get_device_number): Ditto.
(win32_device_name): Call new device name parser to do most of the heavy
lifting.
(mount_info::conv_to_win32_path): Fill in dev field as appropriate.
(symlink_worker): Handle new device files.
(symlink_info::check): Ditto.
(symlink_info::parse_device): Define new function.
* path.h (executable_states): Move here from fhandler.h.
(fs_info): Rename variables to *_storage and create methods for accessing same.
(path_conv): Add dev element, remove devn and unit and adjust inline methods to
accommodate.
(set_normalized_path): Declare new function.
* pinfo.cc (_pinfo::commune_recv): Add broken support for handling fifos.
(_pinfo::commune_send): Ditto.
* pipe.cc (fhandler_pipe::close): check for existence of handle before closing
it.
(handler_pipe::create): Rename from make_pipe. Change arguments to accept
fhandler_pipe array. Accommodate fifos.
(pipe): Rework to deal with fhandler_pipe::create changes.
(_pipe): Ditto.
* select.cc: Use individual device_specific types throughout rather than
indexing with obsolete device number.
(set_bits): Use is_socket call rather than checking device number.
* shared_info.h (CURR_MOUNT_MAGIC): Update.
(conv_to_win32_path): Reflect addition of device argument.
* syscalls.cc (mknod_worker): New function.
(open): Use build_fh_name to build fhandler.
(chown_worker): Detect if this is an 'auto' device rather than an on-filesystem
device and handle appropriately.
(chmod_device): New function.
(chmod): Detect if this is an 'auto' device rather than an on-filesystem device
and handle appropriately. Use chmod_device to set mode of in-filesystem
devices.
(stat_worker): Eliminate path_conv argument. Call build_fh_name to construct
fhandler. Use fh->error() rather than pc->error to detect errors in fhandler
construction.
(access_worker): New function pulled from access. Accommodate in-filesystem
devices.
(access): Use access_worker.
(fpathconf): Detect if this is an 'auto' device rather than an on-filesystem
device and handle appropriately.
(mknod_worker): New function.
(mknod32): New function.
(chroot): Free normalized path -- assuming it was actually cmalloced.
* tty.cc (create_tty_master): Tweak for new device class.
(tty::common_init): Ditto.
* winsup.h (stat_worker): Remove.
(symlink_worker): Declare.
* exceptions.cc (set_process_mask): Just call sig_dispatch_pending and don't
worry about pending_signals since sig_dispatch_pending should always do the
right thing now.
(sig_handle): Reorganize SIGCONT handling to more closely conform to SUSv3.
* pinfo.h: Move __SIG enum to sigproc.h.
(PICOM_FIFO): New enum element.
(_pinfo): Remove 'thread2signal' stuff throughout class.
(_pinfo::commune_send): Make varargs.
(_pinfo::sigtodo): Eliminate.
(_pinfo::thread2signal): Ditto.
* signal.cc (kill_worker): Eliminate call to setthread2signal.
* sigproc.cc (local_sigtodo): Eliminate.
(getlocal_sigtodo): Ditto.
(sigelem): New class.
(pending_signals): New class.
(sigqueue): New variable, start of sigqueue linked list.
(sigcatch_nonmain): Eliminate.
(sigcatch_main): Eliminate.
(sigcatch_nosync): Eliminate.
(sigcomplete_nonmain): Eliminate.
(pending_signals): Eliminate.
(sig_clear): Call signal thread to clear pending signals, unless already in
signal thread.
(sigpending): Call signal thread to get pending signals.
(sig_dispatch_pending): Eliminate use of pending_signals and just check
sigqueue.
(sigproc_terminate): Eliminate all of the obsolete semaphore stuff. Close
signal pipe handle.
(sig_send): Eliminate all of the obsolete semaphore stuff and use pipe to send
signals.
(getevent): Eliminate.
(pending_signals::add): New function.
(pending_signals::del): New function.
(pending_signals::next): New function.
(wait_sig): Eliminate all of the obsolete semaphore stuff. Use pipe to
communicate and maintain a linked list of signals.
* sigproc.h: Move __SIG defines here. Add __SIGPENDING.
(sig_dispatch_pending): Remove "C" specifier.
(sig_handle): Accept a mask argument.
* thread.cc: Remove signal handling considerations throughout.
2003-09-25 02:37:18 +02:00
|
|
|
fhandler_dev_floppy::fhandler_dev_floppy ()
|
2005-09-28 21:33:18 +02:00
|
|
|
: fhandler_dev_raw (), status ()
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2005-09-26 16:55:40 +02:00
|
|
|
fhandler_dev_floppy::get_drive_info (struct hd_geometry *geo)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2005-09-27 17:33:02 +02:00
|
|
|
char dbuf[256];
|
|
|
|
char pbuf[256];
|
|
|
|
|
2011-03-29 12:42:11 +02:00
|
|
|
DISK_GEOMETRY_EX *dix = NULL;
|
2006-03-24 15:52:08 +01:00
|
|
|
DISK_GEOMETRY *di = NULL;
|
2005-09-27 17:33:02 +02:00
|
|
|
PARTITION_INFORMATION_EX *pix = NULL;
|
|
|
|
PARTITION_INFORMATION *pi = NULL;
|
2005-09-26 16:55:40 +02:00
|
|
|
DWORD bytes_read = 0;
|
2001-09-05 12:14:15 +02:00
|
|
|
|
2005-09-27 17:33:02 +02:00
|
|
|
/* Always try using the new EX ioctls first (>= XP). If not available,
|
2005-09-30 12:54:45 +02:00
|
|
|
fall back to trying the old non-EX ioctls.
|
|
|
|
Unfortunately the EX ioctls are not implemented in the floppy driver. */
|
2013-04-23 11:44:36 +02:00
|
|
|
if (get_major () != DEV_FLOPPY_MAJOR)
|
2005-09-28 21:33:18 +02:00
|
|
|
{
|
|
|
|
if (!DeviceIoControl (get_handle (),
|
|
|
|
IOCTL_DISK_GET_DRIVE_GEOMETRY_EX, NULL, 0,
|
|
|
|
dbuf, 256, &bytes_read, NULL))
|
2006-03-24 15:52:08 +01:00
|
|
|
__seterrno ();
|
|
|
|
else
|
2005-09-28 21:33:18 +02:00
|
|
|
{
|
2011-03-29 12:42:11 +02:00
|
|
|
dix = (DISK_GEOMETRY_EX *) dbuf;
|
|
|
|
di = &dix->Geometry;
|
2006-03-24 15:52:08 +01:00
|
|
|
if (!DeviceIoControl (get_handle (),
|
|
|
|
IOCTL_DISK_GET_PARTITION_INFO_EX, NULL, 0,
|
|
|
|
pbuf, 256, &bytes_read, NULL))
|
|
|
|
__seterrno ();
|
|
|
|
else
|
|
|
|
pix = (PARTITION_INFORMATION_EX *) pbuf;
|
2005-09-28 21:33:18 +02:00
|
|
|
}
|
|
|
|
}
|
2006-03-24 15:52:08 +01:00
|
|
|
if (!di)
|
2001-02-05 17:10:06 +01:00
|
|
|
{
|
2005-09-27 17:33:02 +02:00
|
|
|
if (!DeviceIoControl (get_handle (),
|
|
|
|
IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0,
|
|
|
|
dbuf, 256, &bytes_read, NULL))
|
|
|
|
{
|
2013-04-23 11:44:36 +02:00
|
|
|
__seterrno ();
|
|
|
|
return -1;
|
2005-09-27 17:33:02 +02:00
|
|
|
}
|
2013-04-23 11:44:36 +02:00
|
|
|
di = (DISK_GEOMETRY *) dbuf;
|
|
|
|
if (!DeviceIoControl (get_handle (),
|
|
|
|
IOCTL_DISK_GET_PARTITION_INFO, NULL, 0,
|
|
|
|
pbuf, 256, &bytes_read, NULL))
|
|
|
|
__seterrno ();
|
|
|
|
else
|
|
|
|
pi = (PARTITION_INFORMATION *) pbuf;
|
2005-09-27 17:33:02 +02:00
|
|
|
}
|
2013-04-23 11:44:36 +02:00
|
|
|
debug_printf ("disk geometry: (%D cyl)*(%u trk)*(%u sec)*(%u bps)",
|
|
|
|
di->Cylinders.QuadPart,
|
|
|
|
di->TracksPerCylinder,
|
|
|
|
di->SectorsPerTrack,
|
|
|
|
di->BytesPerSector);
|
|
|
|
bytes_per_sector = di->BytesPerSector;
|
|
|
|
if (pix)
|
2001-09-05 12:14:15 +02:00
|
|
|
{
|
2013-04-23 11:44:36 +02:00
|
|
|
debug_printf ("partition info: offset %D length %D",
|
|
|
|
pix->StartingOffset.QuadPart,
|
|
|
|
pix->PartitionLength.QuadPart);
|
|
|
|
drive_size = pix->PartitionLength.QuadPart;
|
2001-09-05 12:14:15 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-04-23 11:44:36 +02:00
|
|
|
debug_printf ("partition info: offset %D length %D",
|
|
|
|
pi->StartingOffset.QuadPart,
|
|
|
|
pi->PartitionLength.QuadPart);
|
|
|
|
drive_size = pi->PartitionLength.QuadPart;
|
|
|
|
}
|
|
|
|
if (geo)
|
|
|
|
{
|
|
|
|
geo->heads = di->TracksPerCylinder;
|
|
|
|
geo->sectors = di->SectorsPerTrack;
|
|
|
|
geo->cylinders = di->Cylinders.LowPart;
|
2005-09-27 17:33:02 +02:00
|
|
|
if (pix)
|
2013-04-23 11:44:36 +02:00
|
|
|
geo->start = pix->StartingOffset.QuadPart >> 9ULL;
|
2005-09-27 17:33:02 +02:00
|
|
|
else if (pi)
|
2013-04-23 11:44:36 +02:00
|
|
|
geo->start = pi->StartingOffset.QuadPart >> 9ULL;
|
2005-09-27 17:33:02 +02:00
|
|
|
else
|
2013-04-23 11:44:36 +02:00
|
|
|
geo->start = 0;
|
2005-09-26 16:55:40 +02:00
|
|
|
}
|
2006-03-24 15:52:08 +01:00
|
|
|
debug_printf ("drive size: %D", drive_size);
|
2006-05-28 17:50:14 +02:00
|
|
|
|
2005-09-26 16:55:40 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-09-28 21:33:18 +02:00
|
|
|
/* Wrapper functions for ReadFile and WriteFile to simplify error handling. */
|
|
|
|
BOOL
|
|
|
|
fhandler_dev_floppy::read_file (void *buf, DWORD to_read, DWORD *read, int *err)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
|
|
|
|
*err = 0;
|
|
|
|
if (!(ret = ReadFile (get_handle (), buf, to_read, read, 0)))
|
|
|
|
*err = GetLastError ();
|
2013-04-23 11:44:36 +02:00
|
|
|
syscall_printf ("%d (err %d) = ReadFile (%p, %p, to_read %u, read %u, 0)",
|
2005-09-28 21:33:18 +02:00
|
|
|
ret, *err, get_handle (), buf, to_read, *read);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-01-12 10:16:51 +01:00
|
|
|
/* See comment in write_file below. */
|
|
|
|
BOOL
|
|
|
|
fhandler_dev_floppy::lock_partition (DWORD to_write)
|
|
|
|
{
|
|
|
|
DWORD bytes_read;
|
|
|
|
|
2011-06-06 07:02:13 +02:00
|
|
|
/* The simple case. We have only a single partition open anyway.
|
2011-01-12 10:16:51 +01:00
|
|
|
Try to lock the partition so that a subsequent write succeeds.
|
|
|
|
If there's some file handle open on one of the affected partitions,
|
|
|
|
this fails, but that's how it works on Vista and later... */
|
|
|
|
if (get_minor () % 16 != 0)
|
|
|
|
{
|
|
|
|
if (!DeviceIoControl (get_handle (), FSCTL_LOCK_VOLUME,
|
|
|
|
NULL, 0, NULL, 0, &bytes_read, NULL))
|
|
|
|
{
|
|
|
|
debug_printf ("DeviceIoControl (FSCTL_LOCK_VOLUME) failed, %E");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The tricky case. We're writing to the entire disk. What this code
|
|
|
|
basically does is to find out if the current write operation affects
|
|
|
|
one or more partitions on the disk. If so, it tries to lock all these
|
|
|
|
partitions and stores the handles for a subsequent close(). */
|
|
|
|
NTSTATUS status;
|
|
|
|
IO_STATUS_BLOCK io;
|
|
|
|
FILE_POSITION_INFORMATION fpi;
|
|
|
|
/* Allocate space for 4 times the maximum partition count we can handle.
|
|
|
|
The reason is that for *every* single logical drive in an extended
|
|
|
|
partition on an MBR drive, 3 filler entries with partition number set
|
|
|
|
to 0 are added into the partition table returned by
|
|
|
|
IOCTL_DISK_GET_DRIVE_LAYOUT_EX. The first of them reproduces the data
|
|
|
|
of the next partition entry, if any, except for the partiton number.
|
|
|
|
Then two entries with everything set to 0 follow. Well, the
|
|
|
|
documentation states that for MBR drives the number of partition entries
|
|
|
|
in the PARTITION_INFORMATION_EX array is always a multiple of 4, but,
|
|
|
|
nevertheless, how crappy is that layout? */
|
|
|
|
const DWORD size = sizeof (DRIVE_LAYOUT_INFORMATION_EX)
|
|
|
|
+ 4 * MAX_PARTITIONS * sizeof (PARTITION_INFORMATION_EX);
|
|
|
|
PDRIVE_LAYOUT_INFORMATION_EX pdlix = (PDRIVE_LAYOUT_INFORMATION_EX)
|
|
|
|
alloca (size);
|
|
|
|
BOOL found = FALSE;
|
2011-06-06 07:02:13 +02:00
|
|
|
|
2011-01-12 10:16:51 +01:00
|
|
|
/* Fetch current file pointer position on disk. */
|
|
|
|
status = NtQueryInformationFile (get_handle (), &io, &fpi, sizeof fpi,
|
|
|
|
FilePositionInformation);
|
|
|
|
if (!NT_SUCCESS (status))
|
|
|
|
{
|
2013-04-23 11:44:36 +02:00
|
|
|
debug_printf ("NtQueryInformationFile(FilePositionInformation): %y",
|
2011-01-12 10:16:51 +01:00
|
|
|
status);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
/* Fetch drive layout to get start and end positions of partitions on disk. */
|
|
|
|
if (!DeviceIoControl (get_handle (), IOCTL_DISK_GET_DRIVE_LAYOUT_EX, NULL, 0,
|
2011-06-06 07:02:13 +02:00
|
|
|
pdlix, size, &bytes_read, NULL))
|
2011-01-12 10:16:51 +01:00
|
|
|
{
|
|
|
|
debug_printf ("DeviceIoControl(IOCTL_DISK_GET_DRIVE_LAYOUT_EX): %E");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
/* Scan through partition info to find the partition(s) into which we're
|
|
|
|
currently trying to write. */
|
|
|
|
PARTITION_INFORMATION_EX *ppie = pdlix->PartitionEntry;
|
|
|
|
for (DWORD i = 0; i < pdlix->PartitionCount; ++i, ++ppie)
|
|
|
|
{
|
|
|
|
/* A partition number of 0 denotes an extended partition or one of the
|
|
|
|
aforementioned filler entries. Just skip. */
|
|
|
|
if (ppie->PartitionNumber == 0)
|
2011-06-06 07:02:13 +02:00
|
|
|
continue;
|
2011-01-12 10:16:51 +01:00
|
|
|
/* Check if our writing range affects this partition. */
|
|
|
|
if (fpi.CurrentByteOffset.QuadPart < ppie->StartingOffset.QuadPart
|
|
|
|
+ ppie->PartitionLength.QuadPart
|
|
|
|
&& ppie->StartingOffset.QuadPart < fpi.CurrentByteOffset.QuadPart
|
|
|
|
+ to_write)
|
|
|
|
{
|
|
|
|
/* Yes. Now check if we can handle it. We can only handle
|
|
|
|
up to MAX_PARTITIONS partitions. The partition numbering is
|
|
|
|
one-based, so we decrement the partition number by 1 when using
|
|
|
|
as index into the partition array. */
|
|
|
|
DWORD &part_no = ppie->PartitionNumber;
|
|
|
|
if (part_no >= MAX_PARTITIONS)
|
|
|
|
return FALSE;
|
|
|
|
found = TRUE;
|
2013-04-23 11:44:36 +02:00
|
|
|
debug_printf ("%u %D->%D : %D->%D", part_no,
|
2011-01-12 10:16:51 +01:00
|
|
|
ppie->StartingOffset.QuadPart,
|
|
|
|
ppie->StartingOffset.QuadPart
|
|
|
|
+ ppie->PartitionLength.QuadPart,
|
|
|
|
fpi.CurrentByteOffset.QuadPart,
|
|
|
|
fpi.CurrentByteOffset.QuadPart + to_write);
|
|
|
|
/* Do we already have partitions? If not, create it. */
|
|
|
|
if (!partitions)
|
|
|
|
{
|
|
|
|
partitions = (part_t *) ccalloc_abort (HEAP_FHANDLER, 1,
|
|
|
|
sizeof (part_t));
|
|
|
|
partitions->refcnt = 1;
|
|
|
|
}
|
|
|
|
/* Next, check if the partition is already open. If so, skip it. */
|
|
|
|
if (partitions->hdl[part_no - 1])
|
|
|
|
continue;
|
|
|
|
/* Now open the partition and lock it. */
|
|
|
|
WCHAR part[MAX_PATH], *p;
|
|
|
|
NTSTATUS status;
|
|
|
|
UNICODE_STRING upart;
|
|
|
|
OBJECT_ATTRIBUTES attr;
|
|
|
|
IO_STATUS_BLOCK io;
|
|
|
|
|
|
|
|
sys_mbstowcs (part, MAX_PATH, get_win32_name ());
|
|
|
|
p = wcschr (part, L'\0') - 1;
|
|
|
|
__small_swprintf (p, L"%d", part_no);
|
|
|
|
RtlInitUnicodeString (&upart, part);
|
|
|
|
InitializeObjectAttributes (&attr, &upart,
|
|
|
|
OBJ_CASE_INSENSITIVE
|
|
|
|
| ((get_flags () & O_CLOEXEC)
|
|
|
|
? 0 : OBJ_INHERIT),
|
|
|
|
NULL, NULL);
|
|
|
|
status = NtOpenFile (&partitions->hdl[part_no - 1],
|
|
|
|
GENERIC_READ | GENERIC_WRITE, &attr,
|
|
|
|
&io, FILE_SHARE_READ | FILE_SHARE_WRITE, 0);
|
|
|
|
if (!NT_SUCCESS (status))
|
|
|
|
{
|
2013-04-23 11:44:36 +02:00
|
|
|
debug_printf ("NtCreateFile(%W): %y", part, status);
|
2011-01-12 10:16:51 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
if (!DeviceIoControl (partitions->hdl[part_no - 1], FSCTL_LOCK_VOLUME,
|
|
|
|
NULL, 0, NULL, 0, &bytes_read, NULL))
|
|
|
|
{
|
|
|
|
debug_printf ("DeviceIoControl (%W, FSCTL_LOCK_VOLUME) "
|
|
|
|
"failed, %E", part);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* If we didn't find a single matching partition, the "Access denied"
|
|
|
|
had another reason, so return FALSE in that case. */
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
2005-09-28 21:33:18 +02:00
|
|
|
BOOL
|
|
|
|
fhandler_dev_floppy::write_file (const void *buf, DWORD to_write,
|
|
|
|
DWORD *written, int *err)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
|
|
|
|
*err = 0;
|
|
|
|
if (!(ret = WriteFile (get_handle (), buf, to_write, written, 0)))
|
|
|
|
*err = GetLastError ();
|
2011-01-12 10:16:51 +01:00
|
|
|
/* When writing to a disk or partition on Vista, an "Access denied" error
|
|
|
|
is potentially a result of the raw disk write restriction. See
|
|
|
|
http://support.microsoft.com/kb/942448 for details. What we have to
|
|
|
|
do here is to lock the partition and retry. The previous solution
|
|
|
|
locked one or all partitions immediately in open. Which is overly
|
|
|
|
wasteful, given that the user might only want to change, say, the boot
|
|
|
|
sector. */
|
|
|
|
if (*err == ERROR_ACCESS_DENIED
|
|
|
|
&& wincap.has_restricted_raw_disk_access ()
|
|
|
|
&& get_major () != DEV_FLOPPY_MAJOR
|
|
|
|
&& get_major () != DEV_CDROM_MAJOR
|
|
|
|
&& (get_flags () & O_ACCMODE) != O_RDONLY
|
|
|
|
&& lock_partition (to_write))
|
|
|
|
{
|
|
|
|
*err = 0;
|
|
|
|
if (!(ret = WriteFile (get_handle (), buf, to_write, written, 0)))
|
|
|
|
*err = GetLastError ();
|
|
|
|
}
|
2013-04-23 11:44:36 +02:00
|
|
|
syscall_printf ("%d (err %d) = WriteFile (%p, %p, write %u, written %u, 0)",
|
2005-09-28 21:33:18 +02:00
|
|
|
ret, *err, get_handle (), buf, to_write, *written);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2005-09-26 16:55:40 +02:00
|
|
|
int
|
|
|
|
fhandler_dev_floppy::open (int flags, mode_t)
|
|
|
|
{
|
2009-02-09 19:28:53 +01:00
|
|
|
int ret = fhandler_dev_raw::open (flags);
|
2005-09-26 16:55:40 +02:00
|
|
|
|
2009-02-09 19:28:53 +01:00
|
|
|
if (ret)
|
2005-09-26 16:55:40 +02:00
|
|
|
{
|
2009-02-09 19:28:53 +01:00
|
|
|
DWORD bytes_read;
|
|
|
|
|
|
|
|
if (get_drive_info (NULL))
|
|
|
|
{
|
|
|
|
close ();
|
|
|
|
return 0;
|
|
|
|
}
|
2012-10-13 14:34:18 +02:00
|
|
|
if (!(flags & O_DIRECT))
|
|
|
|
{
|
|
|
|
/* Create sector-aligned buffer. As default buffer size, we're using
|
|
|
|
some big, sector-aligned value. Since direct blockdev IO is
|
|
|
|
usually non-buffered and non-cached, the performance without
|
|
|
|
buffering is worse than access to a file system on same device.
|
|
|
|
Whoever uses O_DIRECT has my condolences. */
|
|
|
|
devbufsiz = MAX (16 * bytes_per_sector, 65536);
|
|
|
|
devbufalloc = new char [devbufsiz + devbufalign];
|
2013-04-23 11:44:36 +02:00
|
|
|
devbuf = (char *) roundup2 ((uintptr_t) devbufalloc,
|
|
|
|
(uintptr_t) devbufalign);
|
2012-10-13 14:34:18 +02:00
|
|
|
}
|
|
|
|
|
2012-10-15 16:58:39 +02:00
|
|
|
/* If we're not trying to access a floppy disk, make sure we're actually
|
|
|
|
allowed to read *all* of the device or volume. This is actually
|
|
|
|
documented in the MSDN CreateFile man page. */
|
2009-02-09 19:28:53 +01:00
|
|
|
if (get_major () != DEV_FLOPPY_MAJOR
|
|
|
|
&& !DeviceIoControl (get_handle (), FSCTL_ALLOW_EXTENDED_DASD_IO,
|
|
|
|
NULL, 0, NULL, 0, &bytes_read, NULL))
|
|
|
|
debug_printf ("DeviceIoControl (FSCTL_ALLOW_EXTENDED_DASD_IO) "
|
|
|
|
"failed, %E");
|
2005-09-26 16:55:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-01-11 15:50:45 +01:00
|
|
|
int
|
|
|
|
fhandler_dev_floppy::close ()
|
|
|
|
{
|
|
|
|
int ret = fhandler_dev_raw::close ();
|
|
|
|
|
2011-01-12 10:16:51 +01:00
|
|
|
if (partitions && InterlockedDecrement (&partitions->refcnt) == 0)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < MAX_PARTITIONS; ++i)
|
|
|
|
if (partitions->hdl[i])
|
|
|
|
NtClose (partitions->hdl[i]);
|
|
|
|
cfree (partitions);
|
|
|
|
}
|
2011-01-11 15:50:45 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2005-09-28 21:33:18 +02:00
|
|
|
int
|
2011-10-16 00:37:30 +02:00
|
|
|
fhandler_dev_floppy::dup (fhandler_base *child, int flags)
|
2005-09-28 21:33:18 +02:00
|
|
|
{
|
2011-10-16 00:37:30 +02:00
|
|
|
int ret = fhandler_dev_raw::dup (child, flags);
|
2005-09-28 21:33:18 +02:00
|
|
|
|
2011-07-31 14:37:52 +02:00
|
|
|
if (!ret && partitions)
|
|
|
|
InterlockedIncrement (&partitions->refcnt);
|
2005-09-28 21:33:18 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-04-23 11:44:36 +02:00
|
|
|
inline off_t
|
2005-09-28 21:33:18 +02:00
|
|
|
fhandler_dev_floppy::get_current_position ()
|
|
|
|
{
|
|
|
|
LARGE_INTEGER off = { QuadPart: 0LL };
|
|
|
|
off.LowPart = SetFilePointer (get_handle (), 0, &off.HighPart, FILE_CURRENT);
|
|
|
|
return off.QuadPart;
|
|
|
|
}
|
|
|
|
|
2013-05-01 03:20:37 +02:00
|
|
|
void __reg3
|
2005-09-28 21:33:18 +02:00
|
|
|
fhandler_dev_floppy::raw_read (void *ptr, size_t& ulen)
|
|
|
|
{
|
|
|
|
DWORD bytes_read = 0;
|
|
|
|
DWORD read2;
|
2008-11-21 14:46:17 +01:00
|
|
|
DWORD bytes_to_read;
|
2005-09-28 21:33:18 +02:00
|
|
|
int ret;
|
|
|
|
size_t len = ulen;
|
|
|
|
char *tgt;
|
|
|
|
char *p = (char *) ptr;
|
|
|
|
|
|
|
|
/* Checking a previous end of media */
|
|
|
|
if (eom_detected () && !lastblk_to_read ())
|
|
|
|
{
|
|
|
|
set_errno (ENOSPC);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (devbuf)
|
|
|
|
{
|
|
|
|
while (len > 0)
|
|
|
|
{
|
|
|
|
if (devbufstart < devbufend)
|
|
|
|
{
|
2012-03-08 10:36:11 +01:00
|
|
|
bytes_to_read = MIN (len, devbufend - devbufstart);
|
2013-04-23 11:44:36 +02:00
|
|
|
debug_printf ("read %u bytes from buffer (rest %u)",
|
2005-09-28 21:33:18 +02:00
|
|
|
bytes_to_read,
|
|
|
|
devbufend - devbufstart - bytes_to_read);
|
|
|
|
memcpy (p, devbuf + devbufstart, bytes_to_read);
|
|
|
|
len -= bytes_to_read;
|
|
|
|
p += bytes_to_read;
|
|
|
|
bytes_read += bytes_to_read;
|
|
|
|
devbufstart += bytes_to_read;
|
|
|
|
|
|
|
|
if (lastblk_to_read ())
|
|
|
|
{
|
|
|
|
lastblk_to_read (false);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (len > 0)
|
|
|
|
{
|
|
|
|
if (len >= devbufsiz)
|
|
|
|
{
|
|
|
|
bytes_to_read = (len / bytes_per_sector) * bytes_per_sector;
|
|
|
|
tgt = p;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tgt = devbuf;
|
|
|
|
bytes_to_read = devbufsiz;
|
|
|
|
}
|
2013-04-23 11:44:36 +02:00
|
|
|
off_t current_position = get_current_position ();
|
2005-09-28 21:33:18 +02:00
|
|
|
if (current_position + bytes_to_read >= drive_size)
|
|
|
|
bytes_to_read = drive_size - current_position;
|
|
|
|
if (!bytes_to_read)
|
2008-08-09 13:09:02 +02:00
|
|
|
break;
|
2005-09-28 21:33:18 +02:00
|
|
|
|
2013-04-23 11:44:36 +02:00
|
|
|
debug_printf ("read %u bytes from pos %U %s", bytes_to_read,
|
2009-02-09 19:28:53 +01:00
|
|
|
current_position,
|
2005-09-28 21:33:18 +02:00
|
|
|
len < devbufsiz ? "into buffer" : "directly");
|
|
|
|
if (!read_file (tgt, bytes_to_read, &read2, &ret))
|
|
|
|
{
|
|
|
|
if (!IS_EOM (ret))
|
|
|
|
{
|
|
|
|
__seterrno ();
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
eom_detected (true);
|
|
|
|
|
|
|
|
if (!read2)
|
|
|
|
{
|
|
|
|
if (!bytes_read)
|
|
|
|
{
|
|
|
|
debug_printf ("return -1, set errno to ENOSPC");
|
|
|
|
set_errno (ENOSPC);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
lastblk_to_read (true);
|
|
|
|
}
|
|
|
|
if (!read2)
|
|
|
|
break;
|
|
|
|
if (tgt == devbuf)
|
|
|
|
{
|
|
|
|
devbufstart = 0;
|
|
|
|
devbufend = read2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
len -= read2;
|
|
|
|
p += read2;
|
|
|
|
bytes_read += read2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-10-09 14:41:52 +02:00
|
|
|
else
|
2005-09-28 21:33:18 +02:00
|
|
|
{
|
2013-04-23 11:44:36 +02:00
|
|
|
off_t current_position = get_current_position ();
|
2008-11-21 14:46:17 +01:00
|
|
|
bytes_to_read = len;
|
2008-10-09 14:41:52 +02:00
|
|
|
if (current_position + bytes_to_read >= drive_size)
|
|
|
|
bytes_to_read = drive_size - current_position;
|
2013-04-23 11:44:36 +02:00
|
|
|
debug_printf ("read %u bytes from pos %U directly", bytes_to_read,
|
2009-02-09 19:28:53 +01:00
|
|
|
current_position);
|
2008-11-21 14:46:17 +01:00
|
|
|
if (bytes_to_read && !read_file (p, bytes_to_read, &bytes_read, &ret))
|
2005-09-28 21:33:18 +02:00
|
|
|
{
|
2008-10-09 14:41:52 +02:00
|
|
|
if (!IS_EOM (ret))
|
|
|
|
{
|
|
|
|
__seterrno ();
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if (bytes_read)
|
|
|
|
eom_detected (true);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
debug_printf ("return -1, set errno to ENOSPC");
|
|
|
|
set_errno (ENOSPC);
|
|
|
|
goto err;
|
|
|
|
}
|
2005-09-28 21:33:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ulen = (size_t) bytes_read;
|
|
|
|
return;
|
|
|
|
|
|
|
|
err:
|
|
|
|
ulen = (size_t) -1;
|
|
|
|
}
|
|
|
|
|
2013-05-01 03:20:37 +02:00
|
|
|
ssize_t __reg3
|
2005-09-28 21:33:18 +02:00
|
|
|
fhandler_dev_floppy::raw_write (const void *ptr, size_t len)
|
|
|
|
{
|
|
|
|
DWORD bytes_written = 0;
|
|
|
|
char *p = (char *) ptr;
|
|
|
|
int ret;
|
|
|
|
|
2012-10-13 14:34:18 +02:00
|
|
|
/* Checking a previous end of media */
|
2005-09-28 21:33:18 +02:00
|
|
|
if (eom_detected ())
|
|
|
|
{
|
|
|
|
set_errno (ENOSPC);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-10-13 14:34:18 +02:00
|
|
|
if (!len)
|
|
|
|
return 0;
|
2005-09-28 21:33:18 +02:00
|
|
|
|
2012-10-13 14:34:18 +02:00
|
|
|
if (devbuf)
|
2005-09-28 21:33:18 +02:00
|
|
|
{
|
2012-10-13 14:34:18 +02:00
|
|
|
DWORD cplen, written;
|
|
|
|
|
|
|
|
/* First check if we have an active read buffer. If so, try to fit in
|
|
|
|
the start of the input buffer and write out the entire result.
|
|
|
|
This also covers the situation after lseek since lseek fills the read
|
|
|
|
buffer in case we seek to an address which is not sector aligned. */
|
|
|
|
if (devbufend && devbufstart < devbufend)
|
|
|
|
{
|
2013-04-23 11:44:36 +02:00
|
|
|
off_t current_pos = get_current_position ();
|
2012-10-13 14:34:18 +02:00
|
|
|
cplen = MIN (len, devbufend - devbufstart);
|
|
|
|
memcpy (devbuf + devbufstart, p, cplen);
|
|
|
|
LARGE_INTEGER off = { QuadPart:current_pos - devbufend };
|
|
|
|
if (!SetFilePointerEx (get_handle (), off, NULL, FILE_BEGIN))
|
2005-09-28 21:33:18 +02:00
|
|
|
{
|
2012-10-13 14:34:18 +02:00
|
|
|
devbufstart = devbufend = 0;
|
2005-09-28 21:33:18 +02:00
|
|
|
__seterrno ();
|
|
|
|
return -1;
|
|
|
|
}
|
2012-10-13 14:34:18 +02:00
|
|
|
if (!write_file (devbuf, devbufend, &written, &ret))
|
2005-09-28 21:33:18 +02:00
|
|
|
{
|
2012-10-13 14:34:18 +02:00
|
|
|
devbufstart = devbufend = 0;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
/* Align pointers, lengths, etc. */
|
|
|
|
cplen = MIN (cplen, written);
|
|
|
|
devbufstart += cplen;
|
|
|
|
p += cplen;
|
|
|
|
len -= cplen;
|
|
|
|
bytes_written += cplen;
|
|
|
|
if (len)
|
|
|
|
devbufstart = devbufend = 0;
|
|
|
|
}
|
|
|
|
/* As long as there's still something left in the input buffer ... */
|
|
|
|
while (len)
|
|
|
|
{
|
|
|
|
/* Compute the length to write. The problem is that the underlying
|
|
|
|
driver may require sector aligned read/write. So we copy the data
|
|
|
|
over to devbuf, which is guaranteed to be sector aligned. */
|
|
|
|
cplen = MIN (len, devbufsiz);
|
|
|
|
if (cplen >= bytes_per_sector)
|
|
|
|
/* If the remaining len is >= sector size, write out the maximum
|
|
|
|
possible multiple of the sector size which fits into devbuf. */
|
|
|
|
cplen = rounddown (cplen, bytes_per_sector);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* If len < sector size, read in the next sector, seek back,
|
|
|
|
and just copy the new data over the old one before writing. */
|
|
|
|
LARGE_INTEGER off = { QuadPart:get_current_position () };
|
|
|
|
if (!read_file (devbuf, bytes_per_sector, &written, &ret))
|
|
|
|
goto err;
|
|
|
|
if (!SetFilePointerEx (get_handle (), off, NULL, FILE_BEGIN))
|
|
|
|
{
|
|
|
|
__seterrno ();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
memcpy (devbuf, p, cplen);
|
|
|
|
if (!write_file (devbuf, MAX (cplen, bytes_per_sector), &written,
|
|
|
|
&ret))
|
|
|
|
{
|
|
|
|
bytes_written += MIN (cplen, written);
|
|
|
|
goto err;
|
2005-09-28 21:33:18 +02:00
|
|
|
}
|
2012-10-13 14:34:18 +02:00
|
|
|
cplen = MIN (cplen, written);
|
|
|
|
p += cplen;
|
|
|
|
len -= cplen;
|
|
|
|
bytes_written += cplen;
|
2005-09-28 21:33:18 +02:00
|
|
|
}
|
2012-10-13 14:34:18 +02:00
|
|
|
return bytes_written;
|
|
|
|
}
|
2013-04-23 11:44:36 +02:00
|
|
|
|
2012-10-13 14:34:18 +02:00
|
|
|
/* In O_DIRECT case, just write. */
|
|
|
|
if (write_file (p, len, &bytes_written, &ret))
|
|
|
|
return bytes_written;
|
|
|
|
|
|
|
|
err:
|
|
|
|
if (IS_EOM (ret))
|
|
|
|
{
|
|
|
|
eom_detected (true);
|
|
|
|
if (!bytes_written)
|
|
|
|
set_errno (ENOSPC);
|
2005-09-28 21:33:18 +02:00
|
|
|
}
|
2012-10-13 14:34:18 +02:00
|
|
|
else if (!bytes_written)
|
|
|
|
__seterrno ();
|
|
|
|
return bytes_written ?: -1;
|
2005-09-28 21:33:18 +02:00
|
|
|
}
|
|
|
|
|
2013-04-23 11:44:36 +02:00
|
|
|
off_t
|
|
|
|
fhandler_dev_floppy::lseek (off_t offset, int whence)
|
2005-09-26 16:55:40 +02:00
|
|
|
{
|
2007-05-21 11:11:27 +02:00
|
|
|
char buf[bytes_per_sector];
|
2013-04-23 11:44:36 +02:00
|
|
|
off_t current_pos = (off_t) -1;
|
2005-09-28 21:33:18 +02:00
|
|
|
LARGE_INTEGER sector_aligned_offset;
|
2007-05-21 11:11:27 +02:00
|
|
|
size_t bytes_left;
|
2001-02-28 10:59:54 +01:00
|
|
|
|
2005-09-28 21:33:18 +02:00
|
|
|
if (whence == SEEK_END)
|
2001-02-05 17:10:06 +01:00
|
|
|
{
|
2012-10-16 11:52:26 +02:00
|
|
|
offset += drive_size;
|
2001-03-03 04:56:34 +01:00
|
|
|
whence = SEEK_SET;
|
2001-02-28 10:59:54 +01:00
|
|
|
}
|
2005-09-28 21:33:18 +02:00
|
|
|
else if (whence == SEEK_CUR)
|
2001-02-28 10:59:54 +01:00
|
|
|
{
|
2007-05-22 09:16:19 +02:00
|
|
|
current_pos = get_current_position ();
|
2013-04-23 11:44:36 +02:00
|
|
|
off_t exact_pos = current_pos - (devbufend - devbufstart);
|
2012-10-16 11:52:26 +02:00
|
|
|
/* Shortcut when used to get current position. */
|
|
|
|
if (offset == 0)
|
|
|
|
return exact_pos;
|
|
|
|
offset += exact_pos;
|
2001-02-28 10:59:54 +01:00
|
|
|
whence = SEEK_SET;
|
|
|
|
}
|
|
|
|
|
2012-10-16 11:52:26 +02:00
|
|
|
if (whence != SEEK_SET || offset < 0 || offset > drive_size)
|
2001-02-28 10:59:54 +01:00
|
|
|
{
|
|
|
|
set_errno (EINVAL);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-05-22 09:16:19 +02:00
|
|
|
/* If new position is in buffered range, adjust buffer and return */
|
|
|
|
if (devbufstart < devbufend)
|
|
|
|
{
|
2013-04-23 11:44:36 +02:00
|
|
|
if (current_pos == (off_t) -1)
|
2007-05-22 09:16:19 +02:00
|
|
|
current_pos = get_current_position ();
|
2012-10-16 11:52:26 +02:00
|
|
|
if (current_pos - devbufend <= offset && offset <= current_pos)
|
2007-05-22 09:16:19 +02:00
|
|
|
{
|
2012-10-16 11:52:26 +02:00
|
|
|
devbufstart = devbufend - (current_pos - offset);
|
|
|
|
return offset;
|
2007-05-22 09:16:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-16 11:52:26 +02:00
|
|
|
sector_aligned_offset.QuadPart = rounddown (offset, bytes_per_sector);
|
|
|
|
bytes_left = offset - sector_aligned_offset.QuadPart;
|
2001-02-28 10:59:54 +01:00
|
|
|
|
2005-09-26 16:55:40 +02:00
|
|
|
/* Invalidate buffer. */
|
|
|
|
devbufstart = devbufend = 0;
|
2001-02-05 17:10:06 +01:00
|
|
|
|
2012-10-13 14:34:18 +02:00
|
|
|
if (!SetFilePointerEx (get_handle (), sector_aligned_offset, NULL,
|
|
|
|
FILE_BEGIN))
|
2005-09-26 16:55:40 +02:00
|
|
|
{
|
|
|
|
__seterrno ();
|
|
|
|
return -1;
|
|
|
|
}
|
2002-12-14 05:01:32 +01:00
|
|
|
|
2005-09-26 16:55:40 +02:00
|
|
|
eom_detected (false);
|
2005-09-28 21:33:18 +02:00
|
|
|
|
2005-09-26 16:55:40 +02:00
|
|
|
if (bytes_left)
|
|
|
|
{
|
2007-05-21 11:11:27 +02:00
|
|
|
raw_read (buf, bytes_left);
|
|
|
|
if (bytes_left == (size_t) -1)
|
|
|
|
return -1;
|
2001-02-05 17:10:06 +01:00
|
|
|
}
|
2007-05-21 11:11:27 +02:00
|
|
|
|
2005-09-28 21:33:18 +02:00
|
|
|
return sector_aligned_offset.QuadPart + bytes_left;
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fhandler_dev_floppy::ioctl (unsigned int cmd, void *buf)
|
|
|
|
{
|
2012-10-13 14:34:18 +02:00
|
|
|
int ret = 0;
|
2002-11-07 15:16:29 +01:00
|
|
|
DWORD bytes_read;
|
2012-10-13 14:34:18 +02:00
|
|
|
|
2002-11-07 15:16:29 +01:00
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case HDIO_GETGEO:
|
2012-10-13 14:34:18 +02:00
|
|
|
debug_printf ("HDIO_GETGEO");
|
|
|
|
ret = get_drive_info ((struct hd_geometry *) buf);
|
|
|
|
break;
|
2002-11-07 15:16:29 +01:00
|
|
|
case BLKGETSIZE:
|
|
|
|
case BLKGETSIZE64:
|
2012-10-13 14:34:18 +02:00
|
|
|
debug_printf ("BLKGETSIZE");
|
|
|
|
if (cmd == BLKGETSIZE)
|
|
|
|
*(long *)buf = drive_size >> 9UL;
|
|
|
|
else
|
2013-04-23 11:44:36 +02:00
|
|
|
*(off_t *)buf = drive_size;
|
2012-10-13 14:34:18 +02:00
|
|
|
break;
|
2002-11-07 15:16:29 +01:00
|
|
|
case BLKRRPART:
|
2012-10-13 14:34:18 +02:00
|
|
|
debug_printf ("BLKRRPART");
|
|
|
|
if (!DeviceIoControl (get_handle (), IOCTL_DISK_UPDATE_PROPERTIES,
|
|
|
|
NULL, 0, NULL, 0, &bytes_read, NULL))
|
|
|
|
{
|
|
|
|
__seterrno ();
|
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
else
|
2005-09-26 16:55:40 +02:00
|
|
|
get_drive_info (NULL);
|
2012-10-13 14:34:18 +02:00
|
|
|
break;
|
2002-11-07 15:16:29 +01:00
|
|
|
case BLKSSZGET:
|
2012-10-13 14:34:18 +02:00
|
|
|
debug_printf ("BLKSSZGET");
|
2013-04-23 11:44:36 +02:00
|
|
|
*(int *)buf = (int) bytes_per_sector;
|
2012-10-13 14:34:18 +02:00
|
|
|
break;
|
|
|
|
case BLKIOMIN:
|
|
|
|
debug_printf ("BLKIOMIN");
|
2013-04-23 11:44:36 +02:00
|
|
|
*(int *)buf = (int) bytes_per_sector;
|
2012-10-13 14:34:18 +02:00
|
|
|
break;
|
|
|
|
case BLKIOOPT:
|
|
|
|
debug_printf ("BLKIOOPT");
|
2013-04-23 11:44:36 +02:00
|
|
|
*(int *)buf = (int) bytes_per_sector;
|
2012-10-13 14:34:18 +02:00
|
|
|
break;
|
|
|
|
case BLKPBSZGET:
|
|
|
|
debug_printf ("BLKPBSZGET");
|
2013-04-23 11:44:36 +02:00
|
|
|
*(int *)buf = (int) bytes_per_sector;
|
2012-10-13 14:34:18 +02:00
|
|
|
break;
|
|
|
|
case BLKALIGNOFF:
|
|
|
|
debug_printf ("BLKALIGNOFF");
|
|
|
|
*(int *)buf = 0;
|
|
|
|
break;
|
2002-11-07 15:16:29 +01:00
|
|
|
default:
|
2012-10-13 14:34:18 +02:00
|
|
|
ret = fhandler_dev_raw::ioctl (cmd, buf);
|
|
|
|
break;
|
2002-11-07 15:16:29 +01:00
|
|
|
}
|
2012-10-13 14:34:18 +02:00
|
|
|
return ret;
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|