2000-02-18 03:38:33 +08:00
|
|
|
/* fhandler_floppy.cc. See fhandler.h for a description of the
|
|
|
|
fhandler classes.
|
|
|
|
|
2004-02-09 12:04:24 +08:00
|
|
|
Copyright 1999, 2000, 2001, 2002, 2003, 2004 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"
|
2000-02-18 03:38:33 +08:00
|
|
|
#include <sys/termios.h>
|
|
|
|
#include <unistd.h>
|
2001-07-27 03:22:24 +08:00
|
|
|
#include <winioctl.h>
|
2002-11-07 22:16:29 +08:00
|
|
|
#include <asm/socket.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 <ntdef.h>
|
|
|
|
#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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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 ();
|
|
|
|
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;
|
2005-09-26 22:55:40 +08:00
|
|
|
int ret = fhandler_dev_raw::open (flags);
|
|
|
|
|
|
|
|
if (ret && get_drive_info (NULL))
|
|
|
|
{
|
|
|
|
close ();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2005-09-29 03:33:18 +08:00
|
|
|
int
|
|
|
|
fhandler_dev_floppy::dup (fhandler_base *child)
|
|
|
|
{
|
|
|
|
int ret = fhandler_dev_raw::dup (child);
|
|
|
|
|
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
fhandler_dev_floppy *fhc = (fhandler_dev_floppy *) child;
|
|
|
|
|
|
|
|
fhc->drive_size = drive_size;
|
|
|
|
fhc->bytes_per_sector = bytes_per_sector;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
fhandler_dev_floppy::raw_read (void *ptr, size_t& ulen)
|
|
|
|
{
|
|
|
|
DWORD bytes_read = 0;
|
|
|
|
DWORD read2;
|
|
|
|
DWORD bytes_to_read;
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
eom_detected (true);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
debug_printf ("read %d bytes %s", bytes_to_read,
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (!read_file (p, len, &bytes_read, &ret))
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ulen = (size_t) bytes_read;
|
|
|
|
return;
|
|
|
|
|
|
|
|
err:
|
|
|
|
ulen = (size_t) -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
char buf[512];
|
|
|
|
_off64_t lloffset = offset;
|
2005-09-29 03:33:18 +08:00
|
|
|
LARGE_INTEGER sector_aligned_offset;
|
2005-09-26 22:55:40 +08:00
|
|
|
_off64_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
|
|
|
{
|
2005-09-29 03:33:18 +08:00
|
|
|
lloffset += get_current_position () - (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;
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2002-12-14 12:01:32 +08:00
|
|
|
size_t len = bytes_left;
|
|
|
|
raw_read (buf, len);
|
2001-02-06 00:10:06 +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
|
|
|
}
|
|
|
|
|