2000-02-18 03:38:33 +08:00
|
|
|
/* fhandler_floppy.cc. See fhandler.h for a description of the
|
|
|
|
fhandler classes.
|
|
|
|
|
2009-02-10 02:28:53 +08:00
|
|
|
Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
|
2011-01-11 22:50:45 +08:00
|
|
|
2009, 2011 Red Hat, Inc.
|
2000-02-18 03:38:33 +08: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-03 00:28:18 +08:00
|
|
|
#include "winsup.h"
|
2011-01-12 17:16:51 +08:00
|
|
|
#include <alloca.h>
|
2000-02-18 03:38:33 +08:00
|
|
|
#include <unistd.h>
|
2001-07-27 03:22:24 +08:00
|
|
|
#include <winioctl.h>
|
2005-09-29 03:33:18 +08:00
|
|
|
#include <cygwin/rdevio.h>
|
2002-11-07 22:16:29 +08:00
|
|
|
#include <cygwin/hdreg.h>
|
|
|
|
#include <cygwin/fs.h>
|
2005-01-13 06:40:46 +08:00
|
|
|
#include "cygerrno.h"
|
2001-07-27 03:22:24 +08: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 08:37:18 +08:00
|
|
|
#include "path.h"
|
2000-08-22 13:10:20 +08:00
|
|
|
#include "fhandler.h"
|
2006-03-24 22:52:08 +08:00
|
|
|
#include "ntdll.h"
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2005-09-29 03:33:18 +08:00
|
|
|
#define IS_EOM(err) ((err) == ERROR_INVALID_PARAMETER \
|
|
|
|
|| (err) == ERROR_SEEK \
|
|
|
|
|| (err) == ERROR_SECTOR_NOT_FOUND)
|
|
|
|
|
2000-02-18 03:38:33 +08: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 08:37:18 +08:00
|
|
|
fhandler_dev_floppy::fhandler_dev_floppy ()
|
2005-09-29 03:33:18 +08:00
|
|
|
: fhandler_dev_raw (), status ()
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2005-09-26 22:55:40 +08:00
|
|
|
fhandler_dev_floppy::get_drive_info (struct hd_geometry *geo)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2005-09-27 23:33:02 +08:00
|
|
|
char dbuf[256];
|
|
|
|
char pbuf[256];
|
|
|
|
|
2006-03-24 22:52:08 +08:00
|
|
|
DISK_GEOMETRY *di = NULL;
|
2005-09-27 23:33:02 +08:00
|
|
|
PARTITION_INFORMATION_EX *pix = NULL;
|
|
|
|
PARTITION_INFORMATION *pi = NULL;
|
2005-09-26 22:55:40 +08:00
|
|
|
DWORD bytes_read = 0;
|
2001-09-05 18:14:15 +08:00
|
|
|
|
2005-09-27 23:33:02 +08:00
|
|
|
/* Always try using the new EX ioctls first (>= XP). If not available,
|
2005-09-30 18:54:45 +08:00
|
|
|
fall back to trying the old non-EX ioctls.
|
|
|
|
Unfortunately the EX ioctls are not implemented in the floppy driver. */
|
|
|
|
if (wincap.has_disk_ex_ioctls () && get_major () != DEV_FLOPPY_MAJOR)
|
2005-09-29 03:33:18 +08:00
|
|
|
{
|
|
|
|
if (!DeviceIoControl (get_handle (),
|
|
|
|
IOCTL_DISK_GET_DRIVE_GEOMETRY_EX, NULL, 0,
|
|
|
|
dbuf, 256, &bytes_read, NULL))
|
2006-03-24 22:52:08 +08:00
|
|
|
__seterrno ();
|
|
|
|
else
|
2005-09-29 03:33:18 +08:00
|
|
|
{
|
2006-03-24 22:52:08 +08:00
|
|
|
di = &((DISK_GEOMETRY_EX *) dbuf)->Geometry;
|
|
|
|
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-29 03:33:18 +08:00
|
|
|
}
|
|
|
|
}
|
2006-03-24 22:52:08 +08:00
|
|
|
if (!di)
|
2001-02-06 00:10:06 +08:00
|
|
|
{
|
2005-09-27 23:33:02 +08:00
|
|
|
if (!DeviceIoControl (get_handle (),
|
|
|
|
IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0,
|
|
|
|
dbuf, 256, &bytes_read, NULL))
|
2006-03-24 22:52:08 +08:00
|
|
|
__seterrno ();
|
|
|
|
else
|
2005-09-27 23:33:02 +08:00
|
|
|
{
|
2006-03-24 22:52:08 +08: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 23:33:02 +08:00
|
|
|
}
|
|
|
|
}
|
2006-03-24 22:52:08 +08:00
|
|
|
if (!di)
|
2001-09-05 18:14:15 +08:00
|
|
|
{
|
2006-03-24 22:52:08 +08:00
|
|
|
/* Up to Win2K, even IOCTL_DISK_GET_DRIVE_GEOMETRY fails when trying
|
2006-05-28 23:50:14 +08:00
|
|
|
it on CD or DVD drives. In that case fall back to requesting
|
2006-03-24 22:52:08 +08:00
|
|
|
simple file system information. */
|
|
|
|
NTSTATUS status;
|
|
|
|
IO_STATUS_BLOCK io;
|
|
|
|
FILE_FS_SIZE_INFORMATION ffsi;
|
2006-05-28 23:50:14 +08:00
|
|
|
|
2006-03-24 22:52:08 +08:00
|
|
|
status = NtQueryVolumeInformationFile (get_handle (), &io, &ffsi,
|
|
|
|
sizeof ffsi,
|
|
|
|
FileFsSizeInformation);
|
|
|
|
if (!NT_SUCCESS (status))
|
2006-05-28 23:50:14 +08:00
|
|
|
{
|
2006-03-24 22:52:08 +08:00
|
|
|
__seterrno_from_nt_status (status);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
debug_printf ("fsys geometry: (%D units)*(%u sec)*(%u bps)",
|
|
|
|
ffsi.TotalAllocationUnits.QuadPart,
|
|
|
|
ffsi.SectorsPerAllocationUnit,
|
|
|
|
ffsi.BytesPerSector);
|
|
|
|
bytes_per_sector = ffsi.BytesPerSector;
|
|
|
|
drive_size = ffsi.TotalAllocationUnits.QuadPart
|
|
|
|
* ffsi.SectorsPerAllocationUnit
|
2006-05-28 23:50:14 +08:00
|
|
|
* ffsi.BytesPerSector;
|
2006-03-24 22:52:08 +08:00
|
|
|
if (geo)
|
2006-05-28 23:50:14 +08:00
|
|
|
{
|
2006-03-24 22:52:08 +08:00
|
|
|
geo->heads = 1;
|
|
|
|
geo->sectors = ffsi.SectorsPerAllocationUnit;
|
|
|
|
geo->cylinders = ffsi.TotalAllocationUnits.LowPart;
|
|
|
|
geo->start = 0;
|
|
|
|
}
|
2001-09-05 18:14:15 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-03-24 22:52:08 +08: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;
|
2005-09-27 23:33:02 +08:00
|
|
|
if (pix)
|
2006-03-24 22:52:08 +08:00
|
|
|
{
|
|
|
|
debug_printf ("partition info: offset %D length %D",
|
|
|
|
pix->StartingOffset.QuadPart,
|
|
|
|
pix->PartitionLength.QuadPart);
|
|
|
|
drive_size = pix->PartitionLength.QuadPart;
|
|
|
|
}
|
2005-09-27 23:33:02 +08:00
|
|
|
else if (pi)
|
2006-03-24 22:52:08 +08:00
|
|
|
{
|
|
|
|
debug_printf ("partition info: offset %D length %D",
|
|
|
|
pi->StartingOffset.QuadPart,
|
|
|
|
pi->PartitionLength.QuadPart);
|
|
|
|
drive_size = pi->PartitionLength.QuadPart;
|
|
|
|
}
|
2005-09-27 23:33:02 +08:00
|
|
|
else
|
2006-03-24 22:52:08 +08:00
|
|
|
{
|
|
|
|
/* Getting the partition size by using the drive geometry information
|
|
|
|
looks wrong, but this is a historical necessity. NT4 didn't
|
|
|
|
maintain partition information for the whole drive (aka
|
|
|
|
"partition 0"), but returned ERROR_INVALID_HANDLE instead. That
|
|
|
|
got fixed in W2K. */
|
|
|
|
drive_size = di->Cylinders.QuadPart * di->TracksPerCylinder
|
|
|
|
* di->SectorsPerTrack * di->BytesPerSector;
|
|
|
|
}
|
|
|
|
if (geo)
|
|
|
|
{
|
|
|
|
geo->heads = di->TracksPerCylinder;
|
|
|
|
geo->sectors = di->SectorsPerTrack;
|
|
|
|
geo->cylinders = di->Cylinders.LowPart;
|
|
|
|
if (pix)
|
|
|
|
geo->start = pix->StartingOffset.QuadPart >> 9ULL;
|
|
|
|
else if (pi)
|
|
|
|
geo->start = pi->StartingOffset.QuadPart >> 9ULL;
|
|
|
|
else
|
|
|
|
geo->start = 0;
|
|
|
|
}
|
2005-09-26 22:55:40 +08:00
|
|
|
}
|
2006-03-24 22:52:08 +08:00
|
|
|
debug_printf ("drive size: %D", drive_size);
|
2006-05-28 23:50:14 +08:00
|
|
|
|
2005-09-26 22:55:40 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-09-29 03:33:18 +08: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 ();
|
|
|
|
syscall_printf ("%d (err %d) = ReadFile (%d, %d, to_read %d, read %d, 0)",
|
|
|
|
ret, *err, get_handle (), buf, to_read, *read);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-01-12 17:16:51 +08:00
|
|
|
/* See comment in write_file below. */
|
|
|
|
BOOL
|
|
|
|
fhandler_dev_floppy::lock_partition (DWORD to_write)
|
|
|
|
{
|
|
|
|
DWORD bytes_read;
|
|
|
|
|
|
|
|
/* The simple case. We have only a single partition open anyway.
|
|
|
|
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;
|
|
|
|
|
|
|
|
/* Fetch current file pointer position on disk. */
|
|
|
|
status = NtQueryInformationFile (get_handle (), &io, &fpi, sizeof fpi,
|
|
|
|
FilePositionInformation);
|
|
|
|
if (!NT_SUCCESS (status))
|
|
|
|
{
|
|
|
|
debug_printf ("NtQueryInformationFile(FilePositionInformation): %p",
|
|
|
|
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,
|
|
|
|
pdlix, size, &bytes_read, NULL))
|
|
|
|
{
|
|
|
|
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)
|
|
|
|
continue;
|
|
|
|
/* 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;
|
|
|
|
debug_printf ("%d %D->%D : %D->%D", part_no,
|
|
|
|
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))
|
|
|
|
{
|
|
|
|
debug_printf ("NtCreateFile(%W): %p", part, status);
|
|
|
|
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-29 03:33:18 +08: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 17:16:51 +08: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 ();
|
|
|
|
}
|
2005-09-29 03:33:18 +08:00
|
|
|
syscall_printf ("%d (err %d) = WriteFile (%d, %d, write %d, written %d, 0)",
|
|
|
|
ret, *err, get_handle (), buf, to_write, *written);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2005-09-26 22:55:40 +08:00
|
|
|
int
|
|
|
|
fhandler_dev_floppy::open (int flags, mode_t)
|
|
|
|
{
|
2005-09-29 03:33:18 +08:00
|
|
|
/* The correct size of the buffer would be 512 bytes, which is the atomic
|
|
|
|
size, supported by WinNT. Unfortunately, the performance is worse than
|
|
|
|
access to file system on same device! Setting buffer size to a
|
|
|
|
relatively big value increases performance by means. The new ioctl call
|
|
|
|
with 'rdevio.h' header file supports changing this value.
|
|
|
|
|
2005-12-14 23:54:33 +08:00
|
|
|
As default buffer size, we're using some value which is a multiple of
|
|
|
|
the typical tar and cpio buffer sizes, Except O_DIRECT is set, in which
|
|
|
|
case we're not buffering at all. */
|
|
|
|
devbufsiz = (flags & O_DIRECT) ? 0L : 61440L;
|
2009-02-10 02:28:53 +08:00
|
|
|
int ret = fhandler_dev_raw::open (flags);
|
2005-09-26 22:55:40 +08:00
|
|
|
|
2009-02-10 02:28:53 +08:00
|
|
|
if (ret)
|
2005-09-26 22:55:40 +08:00
|
|
|
{
|
2009-02-10 02:28:53 +08:00
|
|
|
DWORD bytes_read;
|
|
|
|
|
|
|
|
if (get_drive_info (NULL))
|
|
|
|
{
|
|
|
|
close ();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* If we're trying to access a CD/DVD drive, or an entire disk,
|
|
|
|
make sure we're actually allowed to read *all* of the device.
|
|
|
|
This is actually documented in the MSDN CreateFile man page. */
|
|
|
|
if (get_major () != DEV_FLOPPY_MAJOR
|
2011-01-11 22:50:45 +08:00
|
|
|
&& (get_major () == DEV_CDROM_MAJOR || get_minor () % 16 == 0)
|
2009-02-10 02:28:53 +08:00
|
|
|
&& !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 22:55:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-01-11 22:50:45 +08:00
|
|
|
int
|
|
|
|
fhandler_dev_floppy::close ()
|
|
|
|
{
|
|
|
|
int ret = fhandler_dev_raw::close ();
|
|
|
|
|
2011-01-12 17:16:51 +08: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 22:50:45 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2005-09-29 03:33:18 +08:00
|
|
|
int
|
|
|
|
fhandler_dev_floppy::dup (fhandler_base *child)
|
|
|
|
{
|
2011-01-11 22:50:45 +08:00
|
|
|
fhandler_dev_floppy *fhc = (fhandler_dev_floppy *) child;
|
|
|
|
|
2005-09-29 03:33:18 +08:00
|
|
|
int ret = fhandler_dev_raw::dup (child);
|
|
|
|
|
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
fhc->drive_size = drive_size;
|
|
|
|
fhc->bytes_per_sector = bytes_per_sector;
|
2011-01-12 17:16:51 +08:00
|
|
|
if (partitions)
|
|
|
|
{
|
|
|
|
InterlockedIncrement (&partitions->refcnt);
|
|
|
|
fhc->partitions = partitions;
|
|
|
|
}
|
2005-09-29 03:33:18 +08:00
|
|
|
fhc->eom_detected (eom_detected ());
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline _off64_t
|
|
|
|
fhandler_dev_floppy::get_current_position ()
|
|
|
|
{
|
|
|
|
LARGE_INTEGER off = { QuadPart: 0LL };
|
|
|
|
off.LowPart = SetFilePointer (get_handle (), 0, &off.HighPart, FILE_CURRENT);
|
|
|
|
return off.QuadPart;
|
|
|
|
}
|
|
|
|
|
2009-07-25 04:54:33 +08:00
|
|
|
void __stdcall
|
2005-09-29 03:33:18 +08:00
|
|
|
fhandler_dev_floppy::raw_read (void *ptr, size_t& ulen)
|
|
|
|
{
|
|
|
|
DWORD bytes_read = 0;
|
|
|
|
DWORD read2;
|
2008-11-21 21:46:17 +08:00
|
|
|
DWORD bytes_to_read;
|
2005-09-29 03:33:18 +08: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)
|
|
|
|
{
|
|
|
|
bytes_to_read = min (len, devbufend - devbufstart);
|
|
|
|
debug_printf ("read %d bytes from buffer (rest %d)",
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
_off64_t current_position = get_current_position ();
|
|
|
|
if (current_position + bytes_to_read >= drive_size)
|
|
|
|
bytes_to_read = drive_size - current_position;
|
|
|
|
if (!bytes_to_read)
|
2008-08-09 19:09:02 +08:00
|
|
|
break;
|
2005-09-29 03:33:18 +08:00
|
|
|
|
2009-02-10 02:28:53 +08:00
|
|
|
debug_printf ("read %d bytes from pos %U %s", bytes_to_read,
|
|
|
|
current_position,
|
2005-09-29 03:33:18 +08: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 20:41:52 +08:00
|
|
|
else
|
2005-09-29 03:33:18 +08:00
|
|
|
{
|
2008-10-09 20:41:52 +08:00
|
|
|
_off64_t current_position = get_current_position ();
|
2008-11-21 21:46:17 +08:00
|
|
|
bytes_to_read = len;
|
2008-10-09 20:41:52 +08:00
|
|
|
if (current_position + bytes_to_read >= drive_size)
|
|
|
|
bytes_to_read = drive_size - current_position;
|
2009-02-10 02:28:53 +08:00
|
|
|
debug_printf ("read %d bytes from pos %U directly", bytes_to_read,
|
|
|
|
current_position);
|
2008-11-21 21:46:17 +08:00
|
|
|
if (bytes_to_read && !read_file (p, bytes_to_read, &bytes_read, &ret))
|
2005-09-29 03:33:18 +08:00
|
|
|
{
|
2008-10-09 20:41:52 +08: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-29 03:33:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ulen = (size_t) bytes_read;
|
|
|
|
return;
|
|
|
|
|
|
|
|
err:
|
|
|
|
ulen = (size_t) -1;
|
|
|
|
}
|
|
|
|
|
2009-07-25 04:54:33 +08:00
|
|
|
int __stdcall
|
2005-09-29 03:33:18 +08:00
|
|
|
fhandler_dev_floppy::raw_write (const void *ptr, size_t len)
|
|
|
|
{
|
|
|
|
DWORD bytes_written = 0;
|
|
|
|
char *p = (char *) ptr;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Checking a previous end of media on tape */
|
|
|
|
if (eom_detected ())
|
|
|
|
{
|
|
|
|
set_errno (ENOSPC);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Invalidate buffer. */
|
|
|
|
devbufstart = devbufend = 0;
|
|
|
|
|
|
|
|
if (len > 0)
|
|
|
|
{
|
|
|
|
if (!write_file (p, len, &bytes_written, &ret))
|
|
|
|
{
|
|
|
|
if (!IS_EOM (ret))
|
|
|
|
{
|
|
|
|
__seterrno ();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
eom_detected (true);
|
|
|
|
if (!bytes_written)
|
|
|
|
{
|
|
|
|
set_errno (ENOSPC);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return bytes_written;
|
|
|
|
}
|
|
|
|
|
2005-09-26 22:55:40 +08:00
|
|
|
_off64_t
|
|
|
|
fhandler_dev_floppy::lseek (_off64_t offset, int whence)
|
|
|
|
{
|
2007-05-21 17:11:27 +08:00
|
|
|
char buf[bytes_per_sector];
|
2005-09-26 22:55:40 +08:00
|
|
|
_off64_t lloffset = offset;
|
2007-05-22 15:16:19 +08:00
|
|
|
_off64_t current_pos = (_off64_t) -1;
|
2005-09-29 03:33:18 +08:00
|
|
|
LARGE_INTEGER sector_aligned_offset;
|
2007-05-21 17:11:27 +08:00
|
|
|
size_t bytes_left;
|
2001-02-28 17:59:54 +08:00
|
|
|
|
2005-09-29 03:33:18 +08:00
|
|
|
if (whence == SEEK_END)
|
2001-02-06 00:10:06 +08:00
|
|
|
{
|
2005-09-29 03:33:18 +08:00
|
|
|
lloffset += drive_size;
|
2001-03-03 11:56:34 +08:00
|
|
|
whence = SEEK_SET;
|
2001-02-28 17:59:54 +08:00
|
|
|
}
|
2005-09-29 03:33:18 +08:00
|
|
|
else if (whence == SEEK_CUR)
|
2001-02-28 17:59:54 +08:00
|
|
|
{
|
2007-05-22 15:16:19 +08:00
|
|
|
current_pos = get_current_position ();
|
|
|
|
lloffset += current_pos - (devbufend - devbufstart);
|
2001-02-28 17:59:54 +08:00
|
|
|
whence = SEEK_SET;
|
|
|
|
}
|
|
|
|
|
2005-12-08 02:11:10 +08:00
|
|
|
if (whence != SEEK_SET || lloffset < 0 || lloffset > drive_size)
|
2001-02-28 17:59:54 +08:00
|
|
|
{
|
|
|
|
set_errno (EINVAL);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-05-22 15:16:19 +08:00
|
|
|
/* If new position is in buffered range, adjust buffer and return */
|
|
|
|
if (devbufstart < devbufend)
|
|
|
|
{
|
|
|
|
if (current_pos == (_off64_t) -1)
|
|
|
|
current_pos = get_current_position ();
|
|
|
|
if (current_pos - devbufend <= lloffset && lloffset <= current_pos)
|
|
|
|
{
|
|
|
|
devbufstart = devbufend - (current_pos - lloffset);
|
|
|
|
return lloffset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-09-29 03:33:18 +08:00
|
|
|
sector_aligned_offset.QuadPart = (lloffset / bytes_per_sector)
|
|
|
|
* bytes_per_sector;
|
|
|
|
bytes_left = lloffset - sector_aligned_offset.QuadPart;
|
2001-02-28 17:59:54 +08:00
|
|
|
|
2005-09-26 22:55:40 +08:00
|
|
|
/* Invalidate buffer. */
|
|
|
|
devbufstart = devbufend = 0;
|
2001-02-06 00:10:06 +08:00
|
|
|
|
2005-09-29 03:33:18 +08:00
|
|
|
sector_aligned_offset.LowPart =
|
|
|
|
SetFilePointer (get_handle (),
|
|
|
|
sector_aligned_offset.LowPart,
|
|
|
|
§or_aligned_offset.HighPart,
|
|
|
|
FILE_BEGIN);
|
|
|
|
if (sector_aligned_offset.LowPart == INVALID_SET_FILE_POINTER
|
|
|
|
&& GetLastError ())
|
2005-09-26 22:55:40 +08:00
|
|
|
{
|
|
|
|
__seterrno ();
|
|
|
|
return -1;
|
|
|
|
}
|
2002-12-14 12:01:32 +08:00
|
|
|
|
2005-09-26 22:55:40 +08:00
|
|
|
eom_detected (false);
|
2005-09-29 03:33:18 +08:00
|
|
|
|
2005-09-26 22:55:40 +08:00
|
|
|
if (bytes_left)
|
|
|
|
{
|
2007-05-21 17:11:27 +08:00
|
|
|
raw_read (buf, bytes_left);
|
|
|
|
if (bytes_left == (size_t) -1)
|
|
|
|
return -1;
|
2001-02-06 00:10:06 +08:00
|
|
|
}
|
2007-05-21 17:11:27 +08:00
|
|
|
|
2005-09-29 03:33:18 +08:00
|
|
|
return sector_aligned_offset.QuadPart + bytes_left;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fhandler_dev_floppy::ioctl (unsigned int cmd, void *buf)
|
|
|
|
{
|
2002-11-07 22:16:29 +08:00
|
|
|
DISK_GEOMETRY di;
|
|
|
|
DWORD bytes_read;
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case HDIO_GETGEO:
|
|
|
|
{
|
2003-01-26 14:42:40 +08:00
|
|
|
debug_printf ("HDIO_GETGEO");
|
2005-09-26 22:55:40 +08:00
|
|
|
return get_drive_info ((struct hd_geometry *) buf);
|
2002-11-07 22:16:29 +08:00
|
|
|
}
|
|
|
|
case BLKGETSIZE:
|
|
|
|
case BLKGETSIZE64:
|
|
|
|
{
|
2003-01-26 14:42:40 +08:00
|
|
|
debug_printf ("BLKGETSIZE");
|
|
|
|
if (cmd == BLKGETSIZE)
|
|
|
|
*(long *)buf = drive_size >> 9UL;
|
|
|
|
else
|
2003-04-02 00:11:41 +08:00
|
|
|
*(_off64_t *)buf = drive_size;
|
2003-01-26 14:42:40 +08:00
|
|
|
return 0;
|
2002-11-07 22:16:29 +08:00
|
|
|
}
|
|
|
|
case BLKRRPART:
|
|
|
|
{
|
2003-01-26 14:42:40 +08:00
|
|
|
debug_printf ("BLKRRPART");
|
|
|
|
if (!DeviceIoControl (get_handle (),
|
|
|
|
IOCTL_DISK_UPDATE_DRIVE_SIZE,
|
|
|
|
NULL, 0,
|
|
|
|
&di, sizeof (di),
|
|
|
|
&bytes_read, NULL))
|
|
|
|
{
|
|
|
|
__seterrno ();
|
|
|
|
return -1;
|
|
|
|
}
|
2005-09-26 22:55:40 +08:00
|
|
|
get_drive_info (NULL);
|
2003-01-26 14:42:40 +08:00
|
|
|
return 0;
|
2002-11-07 22:16:29 +08:00
|
|
|
}
|
|
|
|
case BLKSSZGET:
|
|
|
|
{
|
2003-01-26 14:42:40 +08:00
|
|
|
debug_printf ("BLKSSZGET");
|
2005-09-26 22:55:40 +08:00
|
|
|
*(int *)buf = bytes_per_sector;
|
2003-01-26 14:42:40 +08:00
|
|
|
return 0;
|
2002-11-07 22:16:29 +08:00
|
|
|
}
|
2005-09-29 03:33:18 +08:00
|
|
|
case RDSETBLK:
|
|
|
|
/* Just check the restriction that blocksize must be a multiple
|
2005-12-22 13:57:54 +08:00
|
|
|
of the sector size of the underlying volume sector size,
|
2005-09-29 03:33:18 +08:00
|
|
|
then fall through to fhandler_dev_raw::ioctl. */
|
2006-07-18 03:30:30 +08:00
|
|
|
if (((struct rdop *) buf)->rd_parm % bytes_per_sector)
|
2005-09-29 03:33:18 +08:00
|
|
|
{
|
|
|
|
SetLastError (ERROR_INVALID_PARAMETER);
|
|
|
|
__seterrno ();
|
|
|
|
return -1;
|
|
|
|
}
|
2006-07-18 20:56:37 +08:00
|
|
|
/*FALLTHRU*/
|
2002-11-07 22:16:29 +08:00
|
|
|
default:
|
|
|
|
return fhandler_dev_raw::ioctl (cmd, buf);
|
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|