2000-02-17 20:38:33 +01:00
|
|
|
/* fhandler_tape.cc. See fhandler.h for a description of the fhandler
|
|
|
|
classes.
|
|
|
|
|
2013-01-21 05:34:52 +01:00
|
|
|
Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
|
2015-02-25 18:50:13 +01:00
|
|
|
2010, 2011, 2012, 2013, 2014, 2015 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"
|
2005-07-03 04:40:30 +02:00
|
|
|
#include "cygtls.h"
|
2004-03-14 19:01:45 +01:00
|
|
|
#include <stdlib.h>
|
2000-02-17 20:38:33 +01:00
|
|
|
#include <sys/mtio.h>
|
2004-03-26 22:43:49 +01:00
|
|
|
#include <sys/param.h>
|
2012-06-28 21:25:22 +02:00
|
|
|
#include <devioctl.h>
|
|
|
|
#include <ntddstor.h>
|
2001-07-26 21:22:24 +02:00
|
|
|
#include "security.h"
|
2001-10-01 06:10:07 +02:00
|
|
|
#include "path.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 "fhandler.h"
|
2001-04-18 23:10:15 +02:00
|
|
|
#include "dtable.h"
|
|
|
|
#include "cygheap.h"
|
2004-03-26 22:43:49 +01:00
|
|
|
#include "shared_info.h"
|
|
|
|
#include "sigproc.h"
|
2011-11-14 02:29:49 +01:00
|
|
|
#include "child_info.h"
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2004-03-13 19:15:06 +01:00
|
|
|
/* Media changes and bus resets are sometimes reported and the function
|
|
|
|
hasn't been executed. We repeat all functions which return with one
|
|
|
|
of these error codes. */
|
2004-03-26 22:43:49 +01:00
|
|
|
#define TAPE_FUNC(func) while ((lasterr = (func)) == ERROR_MEDIA_CHANGED) \
|
|
|
|
{ \
|
|
|
|
initialize (drive, false); \
|
|
|
|
part (partition)->initialize (0); \
|
|
|
|
}
|
2004-03-13 19:15:06 +01:00
|
|
|
|
2004-03-26 22:43:49 +01:00
|
|
|
#define IS_BOT(err) ((err) == ERROR_BEGINNING_OF_MEDIA)
|
2004-03-13 19:15:06 +01:00
|
|
|
|
|
|
|
#define IS_EOF(err) ((err) == ERROR_FILEMARK_DETECTED \
|
|
|
|
|| (err) == ERROR_SETMARK_DETECTED)
|
|
|
|
|
2004-03-26 22:43:49 +01:00
|
|
|
#define IS_SM(err) ((err) == ERROR_SETMARK_DETECTED)
|
|
|
|
|
|
|
|
#define IS_EOD(err) ((err) == ERROR_END_OF_MEDIA \
|
|
|
|
|| (err) == ERROR_EOM_OVERFLOW \
|
|
|
|
|| (err) == ERROR_NO_DATA_DETECTED)
|
|
|
|
|
|
|
|
#define IS_EOM(err) ((err) == ERROR_END_OF_MEDIA \
|
|
|
|
|| (err) == ERROR_EOM_OVERFLOW)
|
|
|
|
|
2000-02-17 20:38:33 +01:00
|
|
|
/**********************************************************************/
|
2004-03-26 22:43:49 +01:00
|
|
|
/* mtinfo_part */
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
void
|
2013-08-26 18:14:40 +02:00
|
|
|
mtinfo_part::initialize (int64_t nblock)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2004-03-26 22:43:49 +01:00
|
|
|
block = nblock;
|
|
|
|
if (block == 0)
|
|
|
|
file = fblock = 0;
|
|
|
|
else
|
|
|
|
file = fblock = -1;
|
|
|
|
smark = false;
|
|
|
|
emark = no_eof;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**********************************************************************/
|
|
|
|
/* mtinfo_drive */
|
|
|
|
|
|
|
|
void
|
|
|
|
mtinfo_drive::initialize (int num, bool first_time)
|
|
|
|
{
|
|
|
|
drive = num;
|
|
|
|
partition = 0;
|
|
|
|
block = -1;
|
|
|
|
lock = unlocked;
|
|
|
|
if (first_time)
|
|
|
|
{
|
2004-04-09 22:39:19 +02:00
|
|
|
buffer_writes (true);
|
2004-04-19 21:29:10 +02:00
|
|
|
async_writes (false);
|
2004-04-09 22:39:19 +02:00
|
|
|
two_fm (false);
|
|
|
|
fast_eom (false);
|
|
|
|
auto_lock (false);
|
|
|
|
sysv (false);
|
|
|
|
nowait (false);
|
2004-03-26 22:43:49 +01:00
|
|
|
}
|
|
|
|
for (int i = 0; i < MAX_PARTITION_NUM; ++i)
|
|
|
|
part (i)->initialize ();
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2004-03-26 22:43:49 +01:00
|
|
|
mtinfo_drive::get_dp (HANDLE mt)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2004-03-26 22:43:49 +01:00
|
|
|
DWORD len = sizeof _dp;
|
|
|
|
TAPE_FUNC (GetTapeParameters (mt, GET_TAPE_DRIVE_INFORMATION, &len, &_dp));
|
|
|
|
return error ("get_dp");
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2004-03-26 22:43:49 +01:00
|
|
|
mtinfo_drive::get_mp (HANDLE mt)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2004-03-26 22:43:49 +01:00
|
|
|
DWORD len = sizeof _mp;
|
|
|
|
TAPE_FUNC (GetTapeParameters (mt, GET_TAPE_MEDIA_INFORMATION, &len, &_mp));
|
|
|
|
return error ("get_mp");
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
2004-03-26 22:43:49 +01:00
|
|
|
int
|
|
|
|
mtinfo_drive::open (HANDLE mt)
|
2004-03-13 19:15:06 +01:00
|
|
|
{
|
2013-08-30 19:39:11 +02:00
|
|
|
/* First access after opening the device can return BUS RESET, but we
|
|
|
|
need the drive parameters, so just try again. */
|
|
|
|
while (get_dp (mt) == ERROR_BUS_RESET)
|
|
|
|
;
|
2004-03-26 22:43:49 +01:00
|
|
|
get_mp (mt);
|
|
|
|
get_pos (mt);
|
|
|
|
if (partition < MAX_PARTITION_NUM && part (partition)->block != block)
|
|
|
|
part (partition)->initialize (block);
|
|
|
|
/* The following rewind in position 0 solves a problem which appears
|
|
|
|
* in case of multi volume archives (at least on NT4): The last ReadFile
|
|
|
|
* on the previous medium returns ERROR_NO_DATA_DETECTED. After media
|
|
|
|
* change, all subsequent ReadFile calls return ERROR_NO_DATA_DETECTED,
|
|
|
|
* too. The call to set_pos apparently reset some internal flags.
|
|
|
|
* FIXME: Is that really true or based on a misinterpretation? */
|
|
|
|
if (!block)
|
|
|
|
{
|
|
|
|
debug_printf ("rewind in position 0");
|
|
|
|
set_pos (mt, TAPE_REWIND, 0, false);
|
|
|
|
}
|
|
|
|
return error ("open");
|
|
|
|
}
|
2004-03-13 19:15:06 +01:00
|
|
|
|
2004-03-26 22:43:49 +01:00
|
|
|
int
|
|
|
|
mtinfo_drive::close (HANDLE mt, bool rewind)
|
|
|
|
{
|
|
|
|
lasterr = 0;
|
|
|
|
if (GetTapeStatus (mt) == ERROR_NO_MEDIA_IN_DRIVE)
|
|
|
|
dirty = clean;
|
2004-04-19 21:29:10 +02:00
|
|
|
if (dirty >= has_written)
|
2004-03-13 19:15:06 +01:00
|
|
|
{
|
2004-04-19 21:29:10 +02:00
|
|
|
/* If an async write is still pending, wait for completion. */
|
|
|
|
if (dirty == async_write_pending)
|
|
|
|
lasterr = async_wait (mt, NULL);
|
|
|
|
if (!lasterr)
|
2004-05-28 21:50:07 +02:00
|
|
|
{
|
2004-04-19 21:29:10 +02:00
|
|
|
/* if last operation was writing, write a filemark */
|
|
|
|
debug_printf ("writing filemark");
|
|
|
|
write_marks (mt, TAPE_FILEMARKS, two_fm () ? 2 : 1);
|
|
|
|
if (two_fm () && !lasterr && !rewind) /* Backspace over 2nd fmark. */
|
|
|
|
{
|
|
|
|
set_pos (mt, TAPE_SPACE_FILEMARKS, -1, false);
|
|
|
|
if (!lasterr)
|
|
|
|
part (partition)->fblock = 0; /* That's obvious, isn't it? */
|
|
|
|
}
|
2004-03-26 22:43:49 +01:00
|
|
|
}
|
2004-03-13 19:15:06 +01:00
|
|
|
}
|
2004-04-09 22:39:19 +02:00
|
|
|
else if (dirty == has_read && !rewind)
|
2004-03-26 22:43:49 +01:00
|
|
|
{
|
2004-04-09 22:39:19 +02:00
|
|
|
if (sysv ())
|
2004-05-28 21:50:07 +02:00
|
|
|
{
|
2004-03-26 22:43:49 +01:00
|
|
|
/* Under SYSV semantics, the tape is moved past the next file mark
|
|
|
|
after read. */
|
|
|
|
if (part (partition)->emark == no_eof)
|
|
|
|
set_pos (mt, TAPE_SPACE_FILEMARKS, 1, false);
|
|
|
|
else if (part (partition)->emark == eof_hit)
|
|
|
|
part (partition)->emark = eof;
|
|
|
|
}
|
|
|
|
else
|
2004-05-28 21:50:07 +02:00
|
|
|
{
|
2004-03-26 22:43:49 +01:00
|
|
|
/* Under BSD semantics, we must check if the filemark has been
|
|
|
|
inadvertendly crossed. If so cross the filemark backwards
|
|
|
|
and position the tape right before EOF. */
|
|
|
|
if (part (partition)->emark == eof_hit)
|
|
|
|
set_pos (mt, TAPE_SPACE_FILEMARKS, -1, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (rewind)
|
|
|
|
{
|
|
|
|
debug_printf ("rewinding");
|
|
|
|
set_pos (mt, TAPE_REWIND, 0, false);
|
|
|
|
}
|
2004-04-09 22:39:19 +02:00
|
|
|
if (auto_lock () && lock == auto_locked)
|
2004-03-26 22:43:49 +01:00
|
|
|
prepare (mt, TAPE_UNLOCK);
|
|
|
|
dirty = clean;
|
|
|
|
return error ("close");
|
2004-03-13 19:15:06 +01:00
|
|
|
}
|
|
|
|
|
2004-03-26 22:43:49 +01:00
|
|
|
int
|
2013-04-23 11:44:36 +02:00
|
|
|
mtinfo_drive::read (HANDLE mt, LPOVERLAPPED pov, void *ptr, size_t &ulen)
|
2004-03-13 19:15:06 +01:00
|
|
|
{
|
|
|
|
BOOL ret;
|
2004-03-26 22:43:49 +01:00
|
|
|
DWORD bytes_read = 0;
|
2004-03-13 19:15:06 +01:00
|
|
|
|
2004-03-26 22:43:49 +01:00
|
|
|
if (GetTapeStatus (mt) == ERROR_NO_MEDIA_IN_DRIVE)
|
|
|
|
return lasterr = ERROR_NO_MEDIA_IN_DRIVE;
|
|
|
|
if (lasterr == ERROR_BUS_RESET)
|
2004-03-13 19:15:06 +01:00
|
|
|
{
|
2004-03-26 22:43:49 +01:00
|
|
|
ulen = 0;
|
|
|
|
goto out;
|
2004-03-13 19:15:06 +01:00
|
|
|
}
|
2004-04-19 21:29:10 +02:00
|
|
|
/* If an async write is still pending, wait for completion. */
|
|
|
|
if (dirty == async_write_pending)
|
|
|
|
lasterr = async_wait (mt, NULL);
|
2004-03-26 22:43:49 +01:00
|
|
|
dirty = clean;
|
|
|
|
if (part (partition)->emark == eof_hit)
|
|
|
|
{
|
|
|
|
part (partition)->emark = eof;
|
|
|
|
lasterr = ulen = 0;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
else if (part (partition)->emark == eod_hit)
|
|
|
|
{
|
|
|
|
part (partition)->emark = eod;
|
|
|
|
lasterr = ulen = 0;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
else if (part (partition)->emark == eod)
|
|
|
|
{
|
|
|
|
lasterr = ERROR_NO_DATA_DETECTED;
|
|
|
|
ulen = (size_t) -1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
else if (part (partition)->emark == eom_hit)
|
|
|
|
{
|
|
|
|
part (partition)->emark = eom;
|
|
|
|
lasterr = ulen = 0;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
else if (part (partition)->emark == eom)
|
|
|
|
{
|
|
|
|
lasterr = ERROR_END_OF_MEDIA;
|
|
|
|
ulen = (size_t) -1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
part (partition)->smark = false;
|
2004-04-09 22:39:19 +02:00
|
|
|
if (auto_lock () && lock < auto_locked)
|
2004-03-26 22:43:49 +01:00
|
|
|
prepare (mt, TAPE_LOCK, true);
|
2013-04-23 11:44:36 +02:00
|
|
|
ov = pov;
|
|
|
|
ov->Offset = ov->OffsetHigh = 0;
|
|
|
|
ret = ReadFile (mt, ptr, ulen, &bytes_read, ov);
|
2004-03-26 22:43:49 +01:00
|
|
|
lasterr = ret ? 0 : GetLastError ();
|
2004-04-19 21:29:10 +02:00
|
|
|
if (lasterr == ERROR_IO_PENDING)
|
|
|
|
lasterr = async_wait (mt, &bytes_read);
|
2004-03-26 22:43:49 +01:00
|
|
|
ulen = (size_t) bytes_read;
|
|
|
|
if (bytes_read > 0)
|
|
|
|
{
|
2013-04-23 11:44:36 +02:00
|
|
|
int32_t blocks_read = mp ()->BlockSize == 0
|
|
|
|
? 1 : howmany (bytes_read, mp ()->BlockSize);
|
2004-03-26 22:43:49 +01:00
|
|
|
block += blocks_read;
|
|
|
|
part (partition)->block += blocks_read;
|
|
|
|
if (part (partition)->fblock >= 0)
|
|
|
|
part (partition)->fblock += blocks_read;
|
|
|
|
}
|
|
|
|
if (IS_EOF (lasterr))
|
|
|
|
{
|
|
|
|
block++;
|
|
|
|
part (partition)->block++;
|
|
|
|
if (part (partition)->file >= 0)
|
|
|
|
part (partition)->file++;
|
|
|
|
part (partition)->fblock = 0;
|
|
|
|
part (partition)->smark = IS_SM (lasterr);
|
|
|
|
part (partition)->emark = bytes_read > 0 ? eof_hit : eof;
|
|
|
|
lasterr = 0;
|
|
|
|
}
|
|
|
|
else if (IS_EOD (lasterr))
|
|
|
|
{
|
|
|
|
if (part (partition)->emark == eof)
|
2004-05-28 21:50:07 +02:00
|
|
|
part (partition)->emark = IS_EOM (lasterr) ? eom : eod;
|
2004-03-26 22:43:49 +01:00
|
|
|
else
|
2004-05-28 21:50:07 +02:00
|
|
|
{
|
2004-03-26 22:43:49 +01:00
|
|
|
part (partition)->emark = IS_EOM (lasterr) ? eom_hit : eod_hit;
|
|
|
|
lasterr = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
part (partition)->emark = no_eof;
|
|
|
|
/* This happens if the buffer is too small when in variable block
|
|
|
|
size mode. Linux returns ENOMEM here. We're doing the same. */
|
|
|
|
if (lasterr == ERROR_MORE_DATA)
|
2004-05-28 21:50:07 +02:00
|
|
|
lasterr = ERROR_NOT_ENOUGH_MEMORY;
|
2004-03-26 22:43:49 +01:00
|
|
|
}
|
|
|
|
if (!lasterr)
|
|
|
|
dirty = has_read;
|
|
|
|
out:
|
|
|
|
return error ("read");
|
2004-03-13 19:15:06 +01:00
|
|
|
}
|
|
|
|
|
2004-03-26 22:43:49 +01:00
|
|
|
int
|
2004-04-19 21:29:10 +02:00
|
|
|
mtinfo_drive::async_wait (HANDLE mt, DWORD *bytes_written)
|
|
|
|
{
|
|
|
|
DWORD written;
|
|
|
|
|
2013-04-23 11:44:36 +02:00
|
|
|
bool ret = GetOverlappedResult (mt, ov, &written, TRUE);
|
2004-04-19 21:29:10 +02:00
|
|
|
if (bytes_written)
|
|
|
|
*bytes_written = written;
|
|
|
|
return ret ? 0 : GetLastError ();
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2013-04-23 11:44:36 +02:00
|
|
|
mtinfo_drive::write (HANDLE mt, LPOVERLAPPED pov, const void *ptr, size_t &len)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2004-03-26 22:43:49 +01:00
|
|
|
BOOL ret;
|
|
|
|
DWORD bytes_written = 0;
|
2004-04-19 21:29:10 +02:00
|
|
|
int async_err = 0;
|
2004-03-26 22:43:49 +01:00
|
|
|
|
|
|
|
if (GetTapeStatus (mt) == ERROR_NO_MEDIA_IN_DRIVE)
|
|
|
|
return lasterr = ERROR_NO_MEDIA_IN_DRIVE;
|
|
|
|
if (lasterr == ERROR_BUS_RESET)
|
|
|
|
{
|
|
|
|
len = 0;
|
|
|
|
return error ("write");
|
|
|
|
}
|
2004-04-19 21:29:10 +02:00
|
|
|
if (dirty == async_write_pending)
|
|
|
|
async_err = async_wait (mt, &bytes_written);
|
2004-03-26 22:43:49 +01:00
|
|
|
dirty = clean;
|
|
|
|
part (partition)->smark = false;
|
2004-04-09 22:39:19 +02:00
|
|
|
if (auto_lock () && lock < auto_locked)
|
2004-03-26 22:43:49 +01:00
|
|
|
prepare (mt, TAPE_LOCK, true);
|
2013-04-23 11:44:36 +02:00
|
|
|
ov = pov;
|
|
|
|
ov->Offset = ov->OffsetHigh = 0;
|
|
|
|
ret = WriteFile (mt, ptr, len, &bytes_written, ov);
|
2004-03-26 22:43:49 +01:00
|
|
|
lasterr = ret ? 0: GetLastError ();
|
2004-04-19 21:29:10 +02:00
|
|
|
if (lasterr == ERROR_IO_PENDING)
|
|
|
|
{
|
|
|
|
if (async_writes () && mp ()->BlockSize == 0)
|
2004-05-28 21:50:07 +02:00
|
|
|
dirty = async_write_pending;
|
2004-04-19 21:29:10 +02:00
|
|
|
else
|
|
|
|
/* Wait for completion if a non-async write. */
|
|
|
|
lasterr = async_wait (mt, &bytes_written);
|
|
|
|
}
|
2004-03-26 22:43:49 +01:00
|
|
|
len = (size_t) bytes_written;
|
|
|
|
if (bytes_written > 0)
|
|
|
|
{
|
2013-04-23 11:44:36 +02:00
|
|
|
int32_t blocks_written = mp ()->BlockSize == 0
|
|
|
|
? 1 : howmany (bytes_written, mp ()->BlockSize);
|
2004-03-26 22:43:49 +01:00
|
|
|
block += blocks_written;
|
|
|
|
part (partition)->block += blocks_written;
|
|
|
|
if (part (partition)->fblock >= 0)
|
|
|
|
part (partition)->fblock += blocks_written;
|
|
|
|
}
|
2004-04-19 21:29:10 +02:00
|
|
|
if (!lasterr && async_err)
|
|
|
|
lasterr = async_err;
|
2004-03-26 22:43:49 +01:00
|
|
|
if (lasterr == ERROR_EOM_OVERFLOW)
|
|
|
|
part (partition)->emark = eom;
|
|
|
|
else if (lasterr == ERROR_END_OF_MEDIA)
|
|
|
|
; // FIXME?: part (partition)->emark = eom_hit;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
part (partition)->emark = no_eof;
|
|
|
|
if (!lasterr)
|
2004-05-28 21:50:07 +02:00
|
|
|
dirty = has_written;
|
2004-04-19 21:29:10 +02:00
|
|
|
else if (lasterr == ERROR_IO_PENDING)
|
2004-05-28 21:50:07 +02:00
|
|
|
dirty = async_write_pending;
|
2004-03-26 22:43:49 +01:00
|
|
|
}
|
|
|
|
return error ("write");
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2013-08-26 18:14:40 +02:00
|
|
|
mtinfo_drive::get_pos (HANDLE mt, int32_t *ppartition, int64_t *pblock)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2013-08-26 18:14:40 +02:00
|
|
|
DWORD p;
|
|
|
|
ULARGE_INTEGER b;
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2013-08-26 18:14:40 +02:00
|
|
|
TAPE_FUNC (GetTapePosition (mt, TAPE_LOGICAL_POSITION, &p,
|
|
|
|
&b.LowPart, &b.HighPart));
|
2004-03-26 22:43:49 +01:00
|
|
|
if (lasterr == ERROR_INVALID_FUNCTION)
|
2013-08-26 18:14:40 +02:00
|
|
|
TAPE_FUNC (GetTapePosition (mt, TAPE_ABSOLUTE_POSITION, &p,
|
|
|
|
&b.LowPart, &b.HighPart));
|
2004-03-26 22:43:49 +01:00
|
|
|
if (!lasterr)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2004-03-31 14:04:07 +02:00
|
|
|
if (p > 0)
|
2013-04-23 11:44:36 +02:00
|
|
|
partition = (int32_t) p - 1;
|
2013-08-26 18:14:40 +02:00
|
|
|
block = (int64_t) b.QuadPart;
|
2004-03-26 22:43:49 +01:00
|
|
|
if (ppartition)
|
2004-05-28 21:50:07 +02:00
|
|
|
*ppartition= partition;
|
2004-03-26 22:43:49 +01:00
|
|
|
if (pblock)
|
2004-05-28 21:50:07 +02:00
|
|
|
*pblock = block;
|
2004-03-26 22:43:49 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
partition = 0;
|
|
|
|
block = -1;
|
|
|
|
}
|
|
|
|
return error ("get_pos");
|
|
|
|
}
|
2004-03-13 19:15:06 +01:00
|
|
|
|
2004-03-26 22:43:49 +01:00
|
|
|
int
|
2013-08-26 18:14:40 +02:00
|
|
|
mtinfo_drive::_set_pos (HANDLE mt, int mode, int64_t count, int partition,
|
2004-04-19 15:07:55 +02:00
|
|
|
BOOL dont_wait)
|
2004-03-26 22:43:49 +01:00
|
|
|
{
|
2004-04-19 21:29:10 +02:00
|
|
|
/* If an async write is still pending, wait for completion. */
|
|
|
|
if (dirty == async_write_pending)
|
|
|
|
lasterr = async_wait (mt, NULL);
|
|
|
|
dirty = clean;
|
2013-08-26 18:14:40 +02:00
|
|
|
LARGE_INTEGER c = { QuadPart:count };
|
|
|
|
TAPE_FUNC (SetTapePosition (mt, mode, partition, c.LowPart, c.HighPart,
|
2004-04-19 15:07:55 +02:00
|
|
|
dont_wait));
|
2004-03-26 22:43:49 +01:00
|
|
|
return lasterr;
|
|
|
|
}
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2004-03-26 22:43:49 +01:00
|
|
|
int
|
2013-08-26 18:14:40 +02:00
|
|
|
mtinfo_drive::set_pos (HANDLE mt, int mode, int64_t count, bool sfm_func)
|
2004-03-26 22:43:49 +01:00
|
|
|
{
|
|
|
|
int err = 0;
|
2013-08-26 18:14:40 +02:00
|
|
|
int64_t undone = count;
|
2004-03-26 22:43:49 +01:00
|
|
|
BOOL dont_wait = FALSE;
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2004-03-26 22:43:49 +01:00
|
|
|
switch (mode)
|
|
|
|
{
|
|
|
|
case TAPE_SPACE_RELATIVE_BLOCKS:
|
|
|
|
case TAPE_SPACE_FILEMARKS:
|
|
|
|
case TAPE_SPACE_SETMARKS:
|
2004-05-28 21:50:07 +02:00
|
|
|
if (!count)
|
2004-03-26 22:43:49 +01:00
|
|
|
{
|
|
|
|
lasterr = 0;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case TAPE_ABSOLUTE_BLOCK:
|
|
|
|
case TAPE_LOGICAL_BLOCK:
|
|
|
|
case TAPE_REWIND:
|
2004-05-28 21:50:07 +02:00
|
|
|
dont_wait = nowait () ? TRUE : FALSE;
|
2004-03-26 22:43:49 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (mode == TAPE_SPACE_FILEMARKS)
|
|
|
|
{
|
|
|
|
while (!err && undone > 0)
|
2004-04-19 15:07:55 +02:00
|
|
|
if (!(err = _set_pos (mt, mode, 1, 0, FALSE)) || IS_SM (err))
|
2004-03-26 22:43:49 +01:00
|
|
|
--undone;
|
|
|
|
while (!err && undone < 0)
|
2004-04-19 15:07:55 +02:00
|
|
|
if (!(err = _set_pos (mt, mode, -1, 0, FALSE)) || IS_SM (err))
|
2004-03-26 22:43:49 +01:00
|
|
|
++undone;
|
|
|
|
}
|
|
|
|
else
|
2004-04-19 15:07:55 +02:00
|
|
|
err = _set_pos (mt, mode, count, 0, dont_wait);
|
2004-03-26 22:43:49 +01:00
|
|
|
switch (mode)
|
|
|
|
{
|
|
|
|
case TAPE_ABSOLUTE_BLOCK:
|
|
|
|
case TAPE_LOGICAL_BLOCK:
|
|
|
|
get_pos (mt);
|
|
|
|
part (partition)->initialize (block);
|
2004-05-28 21:50:07 +02:00
|
|
|
break;
|
2004-03-26 22:43:49 +01:00
|
|
|
case TAPE_REWIND:
|
2004-05-28 21:50:07 +02:00
|
|
|
if (!err)
|
2004-03-26 22:43:49 +01:00
|
|
|
{
|
|
|
|
block = 0;
|
|
|
|
part (partition)->initialize (0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
get_pos (mt);
|
|
|
|
part (partition)->initialize (block);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case TAPE_SPACE_END_OF_DATA:
|
|
|
|
get_pos (mt);
|
|
|
|
part (partition)->initialize (block);
|
|
|
|
part (partition)->emark = IS_EOM (err) ? eom : eod;
|
2004-05-28 21:50:07 +02:00
|
|
|
break;
|
2004-03-26 22:43:49 +01:00
|
|
|
case TAPE_SPACE_FILEMARKS:
|
|
|
|
if (!err || IS_SM (err))
|
|
|
|
{
|
|
|
|
get_pos (mt);
|
|
|
|
part (partition)->block = block;
|
|
|
|
if (count > 0)
|
|
|
|
{
|
|
|
|
if (part (partition)->file >= 0)
|
|
|
|
part (partition)->file += count - undone;
|
|
|
|
part (partition)->fblock = 0;
|
|
|
|
part (partition)->smark = IS_SM (err);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2004-05-28 21:50:07 +02:00
|
|
|
if (part (partition)->file >= 0)
|
2004-03-26 22:43:49 +01:00
|
|
|
part (partition)->file += count - undone;
|
|
|
|
part (partition)->fblock = -1;
|
|
|
|
part (partition)->smark = false;
|
|
|
|
}
|
|
|
|
if (sfm_func)
|
|
|
|
err = set_pos (mt, mode, count > 0 ? -1 : 1, false);
|
|
|
|
else
|
|
|
|
part (partition)->emark = count > 0 ? eof : no_eof;
|
|
|
|
}
|
|
|
|
else if (IS_EOD (err))
|
|
|
|
{
|
|
|
|
get_pos (mt);
|
|
|
|
part (partition)->block = block;
|
|
|
|
if (part (partition)->file >= 0)
|
|
|
|
part (partition)->file += count - undone;
|
|
|
|
part (partition)->fblock = -1;
|
|
|
|
part (partition)->smark = false;
|
|
|
|
part (partition)->emark = IS_EOM (err) ? eom : eod;
|
|
|
|
}
|
|
|
|
else if (IS_BOT (err))
|
|
|
|
{
|
|
|
|
block = 0;
|
|
|
|
part (partition)->initialize (0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
get_pos (mt);
|
|
|
|
part (partition)->initialize (block);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case TAPE_SPACE_RELATIVE_BLOCKS:
|
|
|
|
if (!err)
|
|
|
|
{
|
|
|
|
block += count;
|
|
|
|
part (partition)->block += count;
|
|
|
|
if (part (partition)->fblock >= 0)
|
|
|
|
part (partition)->fblock += count;
|
|
|
|
part (partition)->smark = false;
|
|
|
|
part (partition)->emark = no_eof;
|
|
|
|
}
|
|
|
|
else if (IS_EOF (err))
|
|
|
|
{
|
|
|
|
get_pos (mt);
|
|
|
|
part (partition)->block = block;
|
|
|
|
if (part (partition)->file >= 0)
|
|
|
|
part (partition)->file += count > 0 ? 1 : -1;
|
|
|
|
part (partition)->fblock = count > 0 ? 0 : -1;
|
|
|
|
part (partition)->smark = (count > 0 && IS_SM (err));
|
|
|
|
part (partition)->emark = count > 0 ? eof : no_eof;
|
|
|
|
}
|
|
|
|
else if (IS_EOD (err))
|
|
|
|
{
|
|
|
|
get_pos (mt);
|
|
|
|
part (partition)->fblock = block - part (partition)->block;
|
|
|
|
part (partition)->block = block;
|
|
|
|
part (partition)->smark = false;
|
|
|
|
part (partition)->emark = IS_EOM (err) ? eom : eod;
|
|
|
|
}
|
|
|
|
else if (IS_BOT (err))
|
|
|
|
{
|
|
|
|
block = 0;
|
|
|
|
part (partition)->initialize (0);
|
|
|
|
}
|
2004-05-28 21:50:07 +02:00
|
|
|
break;
|
2004-03-26 22:43:49 +01:00
|
|
|
case TAPE_SPACE_SETMARKS:
|
|
|
|
get_pos (mt);
|
|
|
|
part (partition)->block = block;
|
2004-05-28 21:50:07 +02:00
|
|
|
if (!err)
|
2004-03-26 22:43:49 +01:00
|
|
|
{
|
|
|
|
part (partition)->file = -1;
|
|
|
|
part (partition)->fblock = -1;
|
|
|
|
part (partition)->smark = true;
|
|
|
|
}
|
2004-05-28 21:50:07 +02:00
|
|
|
break;
|
2004-03-26 22:43:49 +01:00
|
|
|
}
|
|
|
|
lasterr = err;
|
|
|
|
out:
|
|
|
|
return error ("set_pos");
|
|
|
|
}
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2004-03-26 22:43:49 +01:00
|
|
|
int
|
2013-04-23 11:44:36 +02:00
|
|
|
mtinfo_drive::create_partitions (HANDLE mt, int32_t count)
|
2004-03-26 22:43:49 +01:00
|
|
|
{
|
|
|
|
if (dp ()->MaximumPartitionCount <= 1)
|
|
|
|
return ERROR_INVALID_PARAMETER;
|
|
|
|
if (set_pos (mt, TAPE_REWIND, 0, false))
|
|
|
|
goto out;
|
2004-03-31 14:04:07 +02:00
|
|
|
partition = 0;
|
|
|
|
part (partition)->initialize (0);
|
2004-03-26 22:43:49 +01:00
|
|
|
debug_printf ("Format tape with %s partition(s)", count <= 0 ? "one" : "two");
|
2004-03-31 14:04:07 +02:00
|
|
|
if (get_feature (TAPE_DRIVE_INITIATOR))
|
|
|
|
{
|
2013-08-20 22:17:14 +02:00
|
|
|
TAPE_FUNC (CreateTapePartition (mt, TAPE_INITIATOR_PARTITIONS,
|
|
|
|
count <= 0 ? 0 : 2, (DWORD) count));
|
2004-03-31 14:04:07 +02:00
|
|
|
}
|
2013-08-21 22:28:12 +02:00
|
|
|
else if (get_feature (TAPE_DRIVE_SELECT))
|
|
|
|
{
|
|
|
|
TAPE_FUNC (CreateTapePartition (mt, TAPE_SELECT_PARTITIONS,
|
|
|
|
count <= 0 ? 0 : 2, 0));
|
|
|
|
}
|
2004-03-31 14:04:07 +02:00
|
|
|
else if (get_feature (TAPE_DRIVE_FIXED))
|
|
|
|
{
|
|
|
|
/* This is supposed to work for Tandberg SLR drivers up to version
|
2004-05-28 21:50:07 +02:00
|
|
|
1.6 which missed to set the TAPE_DRIVE_INITIATOR flag. According
|
2004-03-31 14:04:07 +02:00
|
|
|
to Tandberg, CreateTapePartition(TAPE_FIXED_PARTITIONS) apparently
|
|
|
|
does not ignore the dwCount parameter. Go figure! */
|
|
|
|
TAPE_FUNC (CreateTapePartition (mt, TAPE_FIXED_PARTITIONS,
|
|
|
|
count <= 0 ? 0 : 2, (DWORD) count));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
lasterr = ERROR_INVALID_PARAMETER;
|
2004-03-26 22:43:49 +01:00
|
|
|
out:
|
|
|
|
return error ("partition");
|
|
|
|
}
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2004-03-26 22:43:49 +01:00
|
|
|
int
|
2013-04-23 11:44:36 +02:00
|
|
|
mtinfo_drive::set_partition (HANDLE mt, int32_t count)
|
2004-03-26 22:43:49 +01:00
|
|
|
{
|
2013-04-23 11:44:36 +02:00
|
|
|
if (count < 0 || (uint32_t) count >= MAX_PARTITION_NUM)
|
2004-03-26 22:43:49 +01:00
|
|
|
lasterr = ERROR_INVALID_PARAMETER;
|
|
|
|
else if ((DWORD) count >= dp ()->MaximumPartitionCount)
|
|
|
|
lasterr = ERROR_IO_DEVICE;
|
|
|
|
else
|
|
|
|
{
|
2013-08-26 18:14:40 +02:00
|
|
|
uint64_t part_block = part (count)->block >= 0 ? part (count)->block : 0;
|
2004-04-19 15:07:55 +02:00
|
|
|
int err = _set_pos (mt, TAPE_LOGICAL_BLOCK, part_block, count + 1, FALSE);
|
2004-03-26 22:43:49 +01:00
|
|
|
if (err)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2013-08-26 18:14:40 +02:00
|
|
|
int64_t sav_block = block;
|
|
|
|
int32_t sav_partition = partition;
|
2004-03-26 22:43:49 +01:00
|
|
|
get_pos (mt);
|
|
|
|
if (sav_partition != partition)
|
|
|
|
{
|
|
|
|
if (partition < MAX_PARTITION_NUM
|
2004-05-28 21:50:07 +02:00
|
|
|
&& part (partition)->block != block)
|
2004-03-26 22:43:49 +01:00
|
|
|
part (partition)->initialize (block);
|
|
|
|
}
|
|
|
|
else if (sav_block != block && partition < MAX_PARTITION_NUM)
|
|
|
|
part (partition)->initialize (block);
|
|
|
|
lasterr = err;
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
2004-03-26 22:43:49 +01:00
|
|
|
else
|
2004-05-28 21:50:07 +02:00
|
|
|
{
|
2004-03-31 14:04:07 +02:00
|
|
|
partition = count;
|
|
|
|
if (part (partition)->block == -1)
|
|
|
|
part (partition)->initialize (0);
|
|
|
|
}
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
2004-03-26 22:43:49 +01:00
|
|
|
return error ("set_partition");
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2004-03-26 22:43:49 +01:00
|
|
|
mtinfo_drive::write_marks (HANDLE mt, int marktype, DWORD count)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2004-04-19 21:29:10 +02:00
|
|
|
/* If an async write is still pending, wait for completion. */
|
|
|
|
if (dirty == async_write_pending)
|
|
|
|
{
|
|
|
|
lasterr = async_wait (mt, NULL);
|
|
|
|
dirty = has_written;
|
|
|
|
}
|
2004-03-26 22:43:49 +01:00
|
|
|
if (marktype != TAPE_SETMARKS)
|
|
|
|
dirty = clean;
|
|
|
|
if (marktype == TAPE_FILEMARKS
|
|
|
|
&& !get_feature (TAPE_DRIVE_WRITE_FILEMARKS))
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2004-03-26 22:43:49 +01:00
|
|
|
if (get_feature (TAPE_DRIVE_WRITE_LONG_FMKS))
|
|
|
|
marktype = TAPE_LONG_FILEMARKS;
|
|
|
|
else
|
|
|
|
marktype = TAPE_SHORT_FILEMARKS;
|
|
|
|
}
|
|
|
|
TAPE_FUNC (WriteTapemark (mt, marktype, count, FALSE));
|
|
|
|
int err = lasterr;
|
|
|
|
if (!err)
|
|
|
|
{
|
|
|
|
block += count;
|
|
|
|
part (partition)->block += count;
|
|
|
|
if (part (partition)->file >= 0)
|
|
|
|
part (partition)->file += count;
|
|
|
|
part (partition)->fblock = 0;
|
|
|
|
part (partition)->emark = eof;
|
|
|
|
part (partition)->smark = (marktype == TAPE_SETMARKS);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-08-26 18:14:40 +02:00
|
|
|
int64_t sav_block = block;
|
|
|
|
int32_t sav_partition = partition;
|
2004-03-26 22:43:49 +01:00
|
|
|
get_pos (mt);
|
|
|
|
if (sav_partition != partition)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2004-03-26 22:43:49 +01:00
|
|
|
if (partition < MAX_PARTITION_NUM
|
|
|
|
&& part (partition)->block != block)
|
|
|
|
part (partition)->initialize (block);
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
2004-03-26 22:43:49 +01:00
|
|
|
else if (sav_block != block && partition < MAX_PARTITION_NUM)
|
|
|
|
part (partition)->initialize (block);
|
|
|
|
lasterr = err;
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
2004-03-26 22:43:49 +01:00
|
|
|
return error ("write_marks");
|
|
|
|
}
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2004-03-26 22:43:49 +01:00
|
|
|
int
|
|
|
|
mtinfo_drive::erase (HANDLE mt, int mode)
|
|
|
|
{
|
|
|
|
switch (mode)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2004-03-26 22:43:49 +01:00
|
|
|
case TAPE_ERASE_SHORT:
|
|
|
|
if (!get_feature (TAPE_DRIVE_ERASE_SHORT))
|
|
|
|
mode = TAPE_ERASE_LONG;
|
|
|
|
break;
|
|
|
|
case TAPE_ERASE_LONG:
|
|
|
|
if (!get_feature (TAPE_DRIVE_ERASE_LONG))
|
|
|
|
mode = TAPE_ERASE_SHORT;
|
|
|
|
break;
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
2004-04-09 22:39:19 +02:00
|
|
|
TAPE_FUNC (EraseTape (mt, mode, nowait () ? TRUE : FALSE));
|
2004-03-26 22:43:49 +01:00
|
|
|
part (partition)->initialize (0);
|
|
|
|
return error ("erase");
|
|
|
|
}
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2004-03-26 22:43:49 +01:00
|
|
|
int
|
|
|
|
mtinfo_drive::prepare (HANDLE mt, int action, bool is_auto)
|
|
|
|
{
|
|
|
|
BOOL dont_wait = FALSE;
|
|
|
|
|
2004-04-19 21:29:10 +02:00
|
|
|
/* If an async write is still pending, wait for completion. */
|
|
|
|
if (dirty == async_write_pending)
|
|
|
|
lasterr = async_wait (mt, NULL);
|
2004-03-26 22:43:49 +01:00
|
|
|
dirty = clean;
|
|
|
|
if (action == TAPE_UNLOAD || action == TAPE_LOAD || action == TAPE_TENSION)
|
2004-04-09 22:39:19 +02:00
|
|
|
dont_wait = nowait () ? TRUE : FALSE;
|
2004-03-26 22:43:49 +01:00
|
|
|
TAPE_FUNC (PrepareTape (mt, action, dont_wait));
|
|
|
|
/* Reset buffer after all successful preparations but lock and unlock. */
|
|
|
|
switch (action)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2004-03-26 22:43:49 +01:00
|
|
|
case TAPE_FORMAT:
|
|
|
|
case TAPE_UNLOAD:
|
|
|
|
case TAPE_LOAD:
|
|
|
|
initialize (drive, false);
|
|
|
|
break;
|
|
|
|
case TAPE_TENSION:
|
|
|
|
part (partition)->initialize (0);
|
|
|
|
break;
|
|
|
|
case TAPE_LOCK:
|
|
|
|
lock = lasterr ? lock_error : is_auto ? auto_locked : locked;
|
2004-05-28 21:50:07 +02:00
|
|
|
break;
|
2004-03-26 22:43:49 +01:00
|
|
|
case TAPE_UNLOCK:
|
2004-05-28 21:50:07 +02:00
|
|
|
lock = lasterr ? lock_error : unlocked;
|
2004-03-26 22:43:49 +01:00
|
|
|
break;
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
2004-03-26 22:43:49 +01:00
|
|
|
return error ("prepare");
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2013-04-23 11:44:36 +02:00
|
|
|
mtinfo_drive::set_compression (HANDLE mt, int32_t count)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2004-03-26 22:43:49 +01:00
|
|
|
if (!get_feature (TAPE_DRIVE_SET_COMPRESSION))
|
|
|
|
return ERROR_INVALID_PARAMETER;
|
|
|
|
TAPE_SET_DRIVE_PARAMETERS sdp =
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2004-03-26 22:43:49 +01:00
|
|
|
dp ()->ECC,
|
2013-04-23 11:44:36 +02:00
|
|
|
(BOOLEAN) (count ? TRUE : FALSE),
|
2004-03-26 22:43:49 +01:00
|
|
|
dp ()->DataPadding,
|
|
|
|
dp ()->ReportSetmarks,
|
|
|
|
dp ()->EOTWarningZoneSize
|
|
|
|
};
|
|
|
|
TAPE_FUNC (SetTapeParameters (mt, SET_TAPE_DRIVE_INFORMATION, &sdp));
|
|
|
|
int err = lasterr;
|
|
|
|
if (!err)
|
|
|
|
dp ()->Compression = sdp.Compression;
|
|
|
|
else
|
|
|
|
get_dp (mt);
|
|
|
|
lasterr = err;
|
|
|
|
return error ("set_compression");
|
|
|
|
}
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2004-03-26 22:43:49 +01:00
|
|
|
int
|
2013-04-23 11:44:36 +02:00
|
|
|
mtinfo_drive::set_blocksize (HANDLE mt, DWORD count)
|
2004-03-26 22:43:49 +01:00
|
|
|
{
|
|
|
|
TAPE_SET_MEDIA_PARAMETERS smp = {count};
|
|
|
|
TAPE_FUNC (SetTapeParameters (mt, SET_TAPE_MEDIA_INFORMATION, &smp));
|
2013-08-20 22:17:14 +02:00
|
|
|
/* Make sure to update blocksize info! */
|
|
|
|
return lasterr ? error ("set_blocksize") : get_mp (mt);
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
2004-03-26 22:43:49 +01:00
|
|
|
int
|
2004-04-09 22:39:19 +02:00
|
|
|
mtinfo_drive::get_status (HANDLE mt, struct mtget *get)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2004-03-26 22:43:49 +01:00
|
|
|
int notape = 0;
|
|
|
|
DWORD tstat;
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2004-03-26 22:43:49 +01:00
|
|
|
if (!get)
|
|
|
|
return ERROR_INVALID_PARAMETER;
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2013-08-26 18:14:40 +02:00
|
|
|
if ((tstat = GetTapeStatus (mt)) == ERROR_NO_MEDIA_IN_DRIVE
|
|
|
|
|| get_mp (mt) == ERROR_NO_MEDIA_IN_DRIVE)
|
2004-03-26 22:43:49 +01:00
|
|
|
notape = 1;
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2004-03-26 22:43:49 +01:00
|
|
|
memset (get, 0, sizeof *get);
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2004-03-26 22:43:49 +01:00
|
|
|
get->mt_type = MT_ISUNKNOWN;
|
|
|
|
|
|
|
|
if (!notape && get_feature (TAPE_DRIVE_SET_BLOCK_SIZE))
|
|
|
|
get->mt_dsreg = (mp ()->BlockSize << MT_ST_BLKSIZE_SHIFT)
|
|
|
|
& MT_ST_BLKSIZE_MASK;
|
|
|
|
else
|
|
|
|
get->mt_dsreg = (dp ()->DefaultBlockSize << MT_ST_BLKSIZE_SHIFT)
|
|
|
|
& MT_ST_BLKSIZE_MASK;
|
|
|
|
|
2013-04-23 11:44:36 +02:00
|
|
|
DWORD size = sizeof (GET_MEDIA_TYPES) + 10 * sizeof (DEVICE_MEDIA_INFO);
|
|
|
|
void *buf = alloca (size);
|
|
|
|
if (DeviceIoControl (mt, IOCTL_STORAGE_GET_MEDIA_TYPES_EX,
|
|
|
|
NULL, 0, buf, size, &size, NULL)
|
|
|
|
|| GetLastError () == ERROR_MORE_DATA)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2013-04-23 11:44:36 +02:00
|
|
|
PGET_MEDIA_TYPES gmt = (PGET_MEDIA_TYPES) buf;
|
|
|
|
for (DWORD i = 0; i < gmt->MediaInfoCount; ++i)
|
2004-03-26 22:43:49 +01:00
|
|
|
{
|
2013-04-23 11:44:36 +02:00
|
|
|
PDEVICE_MEDIA_INFO dmi = &gmt->MediaInfo[i];
|
|
|
|
get->mt_type = dmi->DeviceSpecific.TapeInfo.MediaType;
|
|
|
|
#define TINFO DeviceSpecific.TapeInfo
|
|
|
|
if (dmi->TINFO.MediaCharacteristics & MEDIA_CURRENTLY_MOUNTED)
|
2004-03-26 22:43:49 +01:00
|
|
|
{
|
|
|
|
get->mt_type = dmi->DeviceSpecific.TapeInfo.MediaType;
|
2013-04-23 11:44:36 +02:00
|
|
|
if (dmi->TINFO.BusType == BusTypeScsi)
|
|
|
|
get->mt_dsreg |=
|
|
|
|
(dmi->TINFO.BusSpecificData.ScsiInformation.DensityCode
|
|
|
|
<< MT_ST_DENSITY_SHIFT)
|
|
|
|
& MT_ST_DENSITY_MASK;
|
|
|
|
break;
|
2004-03-26 22:43:49 +01:00
|
|
|
}
|
2013-04-23 11:44:36 +02:00
|
|
|
#undef TINFO
|
2004-03-26 22:43:49 +01:00
|
|
|
}
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
2004-03-26 22:43:49 +01:00
|
|
|
if (!notape)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2013-08-21 22:28:12 +02:00
|
|
|
get->mt_resid = (partition & 0xffff)
|
|
|
|
| ((mp ()->PartitionCount & 0xffff) << 16);
|
2004-03-26 22:43:49 +01:00
|
|
|
get->mt_fileno = part (partition)->file;
|
|
|
|
get->mt_blkno = part (partition)->fblock;
|
|
|
|
|
2008-09-11 06:34:24 +02:00
|
|
|
if (get->mt_blkno != 0)
|
|
|
|
/* nothing to do */;
|
|
|
|
else if (get->mt_fileno == 0)
|
|
|
|
get->mt_gstat |= GMT_BOT (-1);
|
|
|
|
else
|
|
|
|
get->mt_gstat |= GMT_EOF (-1);
|
2004-03-26 22:43:49 +01:00
|
|
|
if (part (partition)->emark >= eod_hit)
|
2004-05-28 21:50:07 +02:00
|
|
|
get->mt_gstat |= GMT_EOD (-1);
|
2004-03-26 22:43:49 +01:00
|
|
|
if (part (partition)->emark >= eom_hit)
|
2004-05-28 21:50:07 +02:00
|
|
|
get->mt_gstat |= GMT_EOT (-1);
|
2004-03-26 22:43:49 +01:00
|
|
|
|
|
|
|
if (part (partition)->smark)
|
2004-05-28 21:50:07 +02:00
|
|
|
get->mt_gstat |= GMT_SM (-1);
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2004-03-26 22:43:49 +01:00
|
|
|
get->mt_gstat |= GMT_ONLINE (-1);
|
|
|
|
|
|
|
|
if (mp ()->WriteProtected)
|
|
|
|
get->mt_gstat |= GMT_WR_PROT (-1);
|
|
|
|
|
2013-08-19 12:24:37 +02:00
|
|
|
get->mt_capacity = mp ()->Capacity.QuadPart;
|
|
|
|
get->mt_remaining = mp ()->Remaining.QuadPart;
|
2004-03-26 22:43:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (notape)
|
|
|
|
get->mt_gstat |= GMT_DR_OPEN (-1);
|
|
|
|
|
2004-04-09 22:39:19 +02:00
|
|
|
if (buffer_writes ())
|
2004-03-26 22:43:49 +01:00
|
|
|
get->mt_gstat |= GMT_IM_REP_EN (-1); /* TODO: Async writes */
|
|
|
|
|
2004-04-19 21:29:10 +02:00
|
|
|
if (tstat == ERROR_DEVICE_REQUIRES_CLEANING)
|
2004-03-26 22:43:49 +01:00
|
|
|
get->mt_gstat |= GMT_CLN (-1);
|
|
|
|
|
|
|
|
/* Cygwin specials: */
|
|
|
|
if (dp ()->ReportSetmarks)
|
|
|
|
get->mt_gstat |= GMT_REP_SM (-1);
|
|
|
|
if (dp ()->DataPadding)
|
|
|
|
get->mt_gstat |= GMT_PADDING (-1);
|
|
|
|
if (dp ()->ECC)
|
|
|
|
get->mt_gstat |= GMT_HW_ECC (-1);
|
|
|
|
if (dp ()->Compression)
|
|
|
|
get->mt_gstat |= GMT_HW_COMP (-1);
|
2004-04-09 22:39:19 +02:00
|
|
|
if (two_fm ())
|
2004-03-26 22:43:49 +01:00
|
|
|
get->mt_gstat |= GMT_TWO_FM (-1);
|
2004-04-09 22:39:19 +02:00
|
|
|
if (fast_eom ())
|
2004-03-26 22:43:49 +01:00
|
|
|
get->mt_gstat |= GMT_FAST_MTEOM (-1);
|
2004-04-09 22:39:19 +02:00
|
|
|
if (auto_lock ())
|
2004-03-26 22:43:49 +01:00
|
|
|
get->mt_gstat |= GMT_AUTO_LOCK (-1);
|
2004-04-09 22:39:19 +02:00
|
|
|
if (sysv ())
|
2004-03-26 22:43:49 +01:00
|
|
|
get->mt_gstat |= GMT_SYSV (-1);
|
2004-04-09 22:39:19 +02:00
|
|
|
if (nowait ())
|
2004-03-26 22:43:49 +01:00
|
|
|
get->mt_gstat |= GMT_NOWAIT (-1);
|
2004-04-19 21:29:10 +02:00
|
|
|
if (async_writes ())
|
|
|
|
get->mt_gstat |= GMT_ASYNC (-1);
|
2004-03-26 22:43:49 +01:00
|
|
|
|
|
|
|
get->mt_erreg = 0; /* FIXME: No softerr counting */
|
|
|
|
|
|
|
|
get->mt_minblksize = dp ()->MinimumBlockSize;
|
|
|
|
get->mt_maxblksize = dp ()->MaximumBlockSize;
|
|
|
|
get->mt_defblksize = dp ()->DefaultBlockSize;
|
|
|
|
get->mt_featureslow = dp ()->FeaturesLow;
|
|
|
|
get->mt_featureshigh = dp ()->FeaturesHigh;
|
|
|
|
get->mt_eotwarningzonesize = dp ()->EOTWarningZoneSize;
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2004-03-26 22:43:49 +01:00
|
|
|
return 0;
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2013-04-23 11:44:36 +02:00
|
|
|
mtinfo_drive::set_options (HANDLE mt, int32_t options)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2013-04-23 11:44:36 +02:00
|
|
|
int32_t what = (options & MT_ST_OPTIONS);
|
2004-03-26 22:43:49 +01:00
|
|
|
bool call_setparams = false;
|
|
|
|
bool set;
|
|
|
|
TAPE_SET_DRIVE_PARAMETERS sdp =
|
|
|
|
{
|
|
|
|
dp ()->ECC,
|
|
|
|
dp ()->Compression,
|
|
|
|
dp ()->DataPadding,
|
|
|
|
dp ()->ReportSetmarks,
|
|
|
|
dp ()->EOTWarningZoneSize
|
|
|
|
};
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2004-03-26 22:43:49 +01:00
|
|
|
lasterr = 0;
|
|
|
|
switch (what)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
if (options == 0 || options == 1)
|
|
|
|
{
|
2004-04-09 22:39:19 +02:00
|
|
|
buffer_writes ((options == 1));
|
2004-03-26 22:43:49 +01:00
|
|
|
}
|
2004-05-28 21:50:07 +02:00
|
|
|
break;
|
2004-03-26 22:43:49 +01:00
|
|
|
case MT_ST_BOOLEANS:
|
2004-04-09 22:39:19 +02:00
|
|
|
buffer_writes (!!(options & MT_ST_BUFFER_WRITES));
|
2004-04-19 21:29:10 +02:00
|
|
|
async_writes (!!(options & MT_ST_ASYNC_WRITES));
|
2004-04-09 22:39:19 +02:00
|
|
|
two_fm (!!(options & MT_ST_TWO_FM));
|
|
|
|
fast_eom (!!(options & MT_ST_FAST_MTEOM));
|
|
|
|
auto_lock (!!(options & MT_ST_AUTO_LOCK));
|
|
|
|
sysv (!!(options & MT_ST_SYSV));
|
|
|
|
nowait (!!(options & MT_ST_NOWAIT));
|
2004-03-26 22:43:49 +01:00
|
|
|
if (get_feature (TAPE_DRIVE_SET_ECC))
|
|
|
|
sdp.ECC = !!(options & MT_ST_ECC);
|
|
|
|
if (get_feature (TAPE_DRIVE_SET_PADDING))
|
|
|
|
sdp.DataPadding = !!(options & MT_ST_PADDING);
|
|
|
|
if (get_feature (TAPE_DRIVE_SET_REPORT_SMKS))
|
|
|
|
sdp.ReportSetmarks = !!(options & MT_ST_REPORT_SM);
|
|
|
|
if (sdp.ECC != dp ()->ECC || sdp.DataPadding != dp ()->DataPadding
|
|
|
|
|| sdp.ReportSetmarks != dp ()->ReportSetmarks)
|
|
|
|
call_setparams = true;
|
2004-05-28 21:50:07 +02:00
|
|
|
break;
|
2004-03-26 22:43:49 +01:00
|
|
|
case MT_ST_SETBOOLEANS:
|
|
|
|
case MT_ST_CLEARBOOLEANS:
|
2004-05-28 21:50:07 +02:00
|
|
|
set = (what == MT_ST_SETBOOLEANS);
|
2004-03-26 22:43:49 +01:00
|
|
|
if (options & MT_ST_BUFFER_WRITES)
|
2004-04-09 22:39:19 +02:00
|
|
|
buffer_writes (set);
|
2004-04-19 21:29:10 +02:00
|
|
|
if (options & MT_ST_ASYNC_WRITES)
|
|
|
|
async_writes (set);
|
2004-03-26 22:43:49 +01:00
|
|
|
if (options & MT_ST_TWO_FM)
|
2004-04-09 22:39:19 +02:00
|
|
|
two_fm (set);
|
2004-03-26 22:43:49 +01:00
|
|
|
if (options & MT_ST_FAST_MTEOM)
|
2004-04-09 22:39:19 +02:00
|
|
|
fast_eom (set);
|
2004-03-26 22:43:49 +01:00
|
|
|
if (options & MT_ST_AUTO_LOCK)
|
2004-04-09 22:39:19 +02:00
|
|
|
auto_lock (set);
|
2004-03-26 22:43:49 +01:00
|
|
|
if (options & MT_ST_SYSV)
|
2004-04-09 22:39:19 +02:00
|
|
|
sysv (set);
|
2004-03-26 22:43:49 +01:00
|
|
|
if (options & MT_ST_NOWAIT)
|
2004-04-09 22:39:19 +02:00
|
|
|
nowait (set);
|
2004-03-26 22:43:49 +01:00
|
|
|
if (options & MT_ST_ECC)
|
|
|
|
sdp.ECC = set;
|
|
|
|
if (options & MT_ST_PADDING)
|
|
|
|
sdp.DataPadding = set;
|
|
|
|
if (options & MT_ST_REPORT_SM)
|
|
|
|
sdp.ReportSetmarks = set;
|
|
|
|
if (sdp.ECC != dp ()->ECC || sdp.DataPadding != dp ()->DataPadding
|
|
|
|
|| sdp.ReportSetmarks != dp ()->ReportSetmarks)
|
|
|
|
call_setparams = true;
|
2004-05-28 21:50:07 +02:00
|
|
|
break;
|
2004-03-26 22:43:49 +01:00
|
|
|
case MT_ST_EOT_WZ_SIZE:
|
|
|
|
if (get_feature (TAPE_DRIVE_SET_EOT_WZ_SIZE))
|
|
|
|
{
|
|
|
|
sdp.EOTWarningZoneSize = (options & ~MT_ST_OPTIONS);
|
|
|
|
if (sdp.EOTWarningZoneSize != dp ()->EOTWarningZoneSize)
|
|
|
|
call_setparams = true;
|
|
|
|
}
|
2004-05-28 21:50:07 +02:00
|
|
|
break;
|
2004-03-26 22:43:49 +01:00
|
|
|
}
|
|
|
|
if (call_setparams)
|
|
|
|
{
|
|
|
|
TAPE_FUNC (SetTapeParameters (mt, SET_TAPE_DRIVE_INFORMATION, &sdp));
|
|
|
|
int err = lasterr;
|
|
|
|
if (!err)
|
2004-05-28 21:50:07 +02:00
|
|
|
{
|
2004-03-26 22:43:49 +01:00
|
|
|
dp ()->ECC = sdp.ECC;
|
|
|
|
dp ()->DataPadding = sdp.DataPadding;
|
|
|
|
dp ()->ReportSetmarks = sdp.ReportSetmarks;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
get_dp (mt);
|
|
|
|
lasterr = err;
|
|
|
|
}
|
|
|
|
return error ("set_options");
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2004-03-26 22:43:49 +01:00
|
|
|
mtinfo_drive::ioctl (HANDLE mt, unsigned int cmd, void *buf)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2014-08-22 11:21:33 +02:00
|
|
|
__try
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2014-08-22 11:21:33 +02:00
|
|
|
if (cmd == MTIOCTOP)
|
2004-05-28 21:50:07 +02:00
|
|
|
{
|
2014-08-22 11:21:33 +02:00
|
|
|
struct mtop *op = (struct mtop *) buf;
|
|
|
|
if (lasterr == ERROR_BUS_RESET)
|
2004-03-26 22:43:49 +01:00
|
|
|
{
|
2014-08-22 11:21:33 +02:00
|
|
|
/* If a bus reset occurs, block further access to this device
|
|
|
|
until the user rewinds, unloads or in any other way tries
|
|
|
|
to maintain a well-known tape position. */
|
|
|
|
if (op->mt_op != MTREW && op->mt_op != MTOFFL
|
|
|
|
&& op->mt_op != MTRETEN && op->mt_op != MTERASE
|
|
|
|
&& op->mt_op != MTSEEK && op->mt_op != MTEOM)
|
|
|
|
return ERROR_BUS_RESET;
|
|
|
|
/* Try to maintain last lock state after bus reset. */
|
|
|
|
if (lock >= auto_locked && PrepareTape (mt, TAPE_LOCK, FALSE))
|
|
|
|
{
|
|
|
|
debug_printf ("Couldn't relock drive after bus reset.");
|
|
|
|
lock = unlocked;
|
|
|
|
}
|
2004-03-26 22:43:49 +01:00
|
|
|
}
|
2014-08-22 11:21:33 +02:00
|
|
|
switch (op->mt_op)
|
|
|
|
{
|
|
|
|
case MTRESET:
|
2004-03-14 19:01:45 +01:00
|
|
|
break;
|
2014-08-22 11:21:33 +02:00
|
|
|
case MTFSF:
|
|
|
|
set_pos (mt, TAPE_SPACE_FILEMARKS, op->mt_count, false);
|
|
|
|
break;
|
|
|
|
case MTBSF:
|
|
|
|
set_pos (mt, TAPE_SPACE_FILEMARKS, -op->mt_count, false);
|
|
|
|
break;
|
|
|
|
case MTFSR:
|
|
|
|
set_pos (mt, TAPE_SPACE_RELATIVE_BLOCKS, op->mt_count, false);
|
|
|
|
break;
|
|
|
|
case MTBSR:
|
|
|
|
set_pos (mt, TAPE_SPACE_RELATIVE_BLOCKS, -op->mt_count, false);
|
|
|
|
break;
|
|
|
|
case MTWEOF:
|
|
|
|
write_marks (mt, TAPE_FILEMARKS, op->mt_count);
|
|
|
|
break;
|
|
|
|
case MTREW:
|
|
|
|
set_pos (mt, TAPE_REWIND, 0, false);
|
|
|
|
break;
|
|
|
|
case MTOFFL:
|
|
|
|
case MTUNLOAD:
|
|
|
|
prepare (mt, TAPE_UNLOAD);
|
|
|
|
break;
|
|
|
|
case MTNOP:
|
2004-03-26 22:43:49 +01:00
|
|
|
lasterr = 0;
|
2004-03-14 19:01:45 +01:00
|
|
|
break;
|
2014-08-22 11:21:33 +02:00
|
|
|
case MTRETEN:
|
|
|
|
if (!get_feature (TAPE_DRIVE_TENSION))
|
|
|
|
lasterr = ERROR_INVALID_PARAMETER;
|
|
|
|
else if (!set_pos (mt, TAPE_REWIND, 0, false))
|
|
|
|
prepare (mt, TAPE_TENSION);
|
|
|
|
break;
|
|
|
|
case MTBSFM:
|
|
|
|
set_pos (mt, TAPE_SPACE_FILEMARKS, -op->mt_count, true);
|
|
|
|
break;
|
|
|
|
case MTFSFM:
|
|
|
|
set_pos (mt, TAPE_SPACE_FILEMARKS, op->mt_count, true);
|
|
|
|
break;
|
|
|
|
case MTEOM:
|
|
|
|
if (fast_eom () && get_feature (TAPE_DRIVE_END_OF_DATA))
|
|
|
|
set_pos (mt, TAPE_SPACE_END_OF_DATA, 0, false);
|
|
|
|
else
|
|
|
|
set_pos (mt, TAPE_SPACE_FILEMARKS, 32767, false);
|
|
|
|
break;
|
|
|
|
case MTERASE:
|
|
|
|
erase (mt, TAPE_ERASE_LONG);
|
|
|
|
break;
|
|
|
|
case MTRAS1:
|
|
|
|
case MTRAS2:
|
|
|
|
case MTRAS3:
|
2004-03-26 22:43:49 +01:00
|
|
|
lasterr = ERROR_INVALID_PARAMETER;
|
2004-03-14 19:01:45 +01:00
|
|
|
break;
|
2014-08-22 11:21:33 +02:00
|
|
|
case MTSETBLK:
|
|
|
|
if (!get_feature (TAPE_DRIVE_SET_BLOCK_SIZE))
|
|
|
|
{
|
|
|
|
lasterr = ERROR_INVALID_PARAMETER;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if ((DWORD) op->mt_count == mp ()->BlockSize)
|
|
|
|
{
|
|
|
|
/* Nothing has changed. */
|
|
|
|
lasterr = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if ((op->mt_count == 0 && !get_feature (TAPE_DRIVE_VARIABLE_BLOCK))
|
|
|
|
|| (op->mt_count > 0
|
|
|
|
&& ((DWORD) op->mt_count < dp ()->MinimumBlockSize
|
|
|
|
|| (DWORD) op->mt_count > dp ()->MaximumBlockSize)))
|
|
|
|
{
|
|
|
|
lasterr = ERROR_INVALID_PARAMETER;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (set_blocksize (mt, op->mt_count)
|
|
|
|
&& lasterr == ERROR_INVALID_FUNCTION)
|
|
|
|
lasterr = ERROR_INVALID_BLOCK_LENGTH;
|
|
|
|
break;
|
|
|
|
case MTSEEK:
|
|
|
|
if (get_feature (TAPE_DRIVE_LOGICAL_BLK))
|
|
|
|
set_pos (mt, TAPE_LOGICAL_BLOCK, op->mt_count, false);
|
|
|
|
else if (!get_pos (mt))
|
|
|
|
set_pos (mt, TAPE_SPACE_RELATIVE_BLOCKS,
|
|
|
|
op->mt_count - block, false);
|
|
|
|
break;
|
|
|
|
case MTTELL:
|
|
|
|
if (!get_pos (mt))
|
|
|
|
op->mt_count = (int) block;
|
|
|
|
break;
|
|
|
|
case MTFSS:
|
|
|
|
set_pos (mt, TAPE_SPACE_SETMARKS, op->mt_count, false);
|
|
|
|
break;
|
|
|
|
case MTBSS:
|
|
|
|
set_pos (mt, TAPE_SPACE_SETMARKS, -op->mt_count, false);
|
|
|
|
break;
|
|
|
|
case MTWSM:
|
|
|
|
write_marks (mt, TAPE_SETMARKS, op->mt_count);
|
|
|
|
break;
|
|
|
|
case MTLOCK:
|
|
|
|
prepare (mt, TAPE_LOCK);
|
|
|
|
break;
|
|
|
|
case MTUNLOCK:
|
|
|
|
prepare (mt, TAPE_UNLOCK);
|
|
|
|
break;
|
|
|
|
case MTLOAD:
|
|
|
|
prepare (mt, TAPE_LOAD);
|
|
|
|
break;
|
|
|
|
case MTCOMPRESSION:
|
|
|
|
set_compression (mt, op->mt_count);
|
|
|
|
break;
|
|
|
|
case MTSETPART:
|
|
|
|
set_partition (mt, op->mt_count);
|
|
|
|
break;
|
|
|
|
case MTMKPART:
|
|
|
|
create_partitions (mt, op->mt_count);
|
|
|
|
break;
|
|
|
|
case MTSETDRVBUFFER:
|
|
|
|
set_options (mt, op->mt_count);
|
|
|
|
break;
|
|
|
|
case MTSETDENSITY:
|
|
|
|
default:
|
|
|
|
lasterr = ERROR_INVALID_PARAMETER;
|
|
|
|
break;
|
|
|
|
}
|
2004-03-26 22:43:49 +01:00
|
|
|
}
|
2014-08-22 11:21:33 +02:00
|
|
|
else if (cmd == MTIOCGET)
|
|
|
|
get_status (mt, (struct mtget *) buf);
|
|
|
|
else if (cmd == MTIOCPOS && !get_pos (mt))
|
|
|
|
((struct mtpos *) buf)->mt_blkno = (long) block;
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
2014-08-22 11:21:33 +02:00
|
|
|
__except (NO_ERROR)
|
|
|
|
{
|
|
|
|
lasterr = ERROR_NOACCESS;
|
|
|
|
}
|
|
|
|
__endtry
|
2004-03-26 22:43:49 +01:00
|
|
|
return lasterr;
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
2004-03-26 22:43:49 +01:00
|
|
|
/**********************************************************************/
|
|
|
|
/* mtinfo */
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2004-03-26 22:43:49 +01:00
|
|
|
void
|
2005-07-05 05:16:46 +02:00
|
|
|
mtinfo::initialize ()
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2008-07-15 13:42:45 +02:00
|
|
|
for (unsigned i = 0; i < MAX_DRIVE_NUM; ++i)
|
|
|
|
drive (i)->initialize (i, true);
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
2004-03-26 22:43:49 +01:00
|
|
|
/**********************************************************************/
|
|
|
|
/* fhandler_dev_tape */
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2008-04-18 22:13:37 +02:00
|
|
|
#define mt (cygwin_shared->mt)
|
|
|
|
|
2011-05-04 14:23:00 +02:00
|
|
|
#define lock(err_ret_val) if (!_lock (false)) return (err_ret_val);
|
2004-03-13 19:15:06 +01:00
|
|
|
|
2004-03-26 22:43:49 +01:00
|
|
|
inline bool
|
2011-05-04 14:23:00 +02:00
|
|
|
fhandler_dev_tape::_lock (bool cancelable)
|
2004-03-26 22:43:49 +01:00
|
|
|
{
|
2011-05-04 14:23:00 +02:00
|
|
|
/* O_NONBLOCK is only valid in a read or write call. Only those are
|
|
|
|
cancelable. */
|
|
|
|
DWORD timeout = cancelable && is_nonblocking () ? 0 : INFINITE;
|
2015-02-25 18:50:13 +01:00
|
|
|
switch (cygwait (mt_mtx, timeout,
|
|
|
|
cw_sig | cw_sig_restart | cw_cancel | cw_cancel_self))
|
2004-03-15 12:37:37 +01:00
|
|
|
{
|
2011-05-04 14:23:00 +02:00
|
|
|
case WAIT_OBJECT_0:
|
|
|
|
return true;
|
|
|
|
case WAIT_TIMEOUT:
|
|
|
|
set_errno (EAGAIN);
|
|
|
|
return false;
|
|
|
|
default:
|
|
|
|
__seterrno ();
|
|
|
|
return false;
|
2004-03-15 12:37:37 +01:00
|
|
|
}
|
2004-03-26 22:43:49 +01:00
|
|
|
}
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2004-03-26 22:43:49 +01:00
|
|
|
inline int
|
|
|
|
fhandler_dev_tape::unlock (int ret)
|
|
|
|
{
|
|
|
|
ReleaseMutex (mt_mtx);
|
|
|
|
return ret;
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
2004-03-26 22:43:49 +01:00
|
|
|
fhandler_dev_tape::fhandler_dev_tape ()
|
|
|
|
: fhandler_dev_raw ()
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2011-05-28 20:17:09 +02:00
|
|
|
debug_printf ("unit: %d", dev ().get_minor ());
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2004-03-26 22:43:49 +01:00
|
|
|
fhandler_dev_tape::open (int flags, mode_t)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2004-03-26 22:43:49 +01:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (driveno () >= MAX_DRIVE_NUM)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2004-03-26 22:43:49 +01:00
|
|
|
set_errno (ENOENT);
|
|
|
|
return 0;
|
2004-03-14 19:01:45 +01:00
|
|
|
}
|
2010-01-14 19:46:02 +01:00
|
|
|
if (!(mt_mtx = CreateMutex (&sec_all, !!(flags & O_CLOEXEC), NULL)))
|
2004-03-14 19:01:45 +01:00
|
|
|
{
|
2004-03-26 22:43:49 +01:00
|
|
|
__seterrno ();
|
|
|
|
return 0;
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
2005-12-14 16:54:33 +01:00
|
|
|
|
|
|
|
/* The O_SYNC flag is not supported by the tape driver. Use the
|
|
|
|
MT_ST_BUFFER_WRITES and MT_ST_ASYNC_WRITES flags in the drive
|
|
|
|
settings instead. In turn, the MT_ST_BUFFER_WRITES is translated
|
|
|
|
into O_SYNC, which controls the FILE_WRITE_THROUGH flag in the
|
|
|
|
NtCreateFile call in fhandler_base::open. */
|
|
|
|
flags &= ~O_SYNC;
|
2006-07-26 17:59:39 +02:00
|
|
|
if (!mt.drive (driveno ())->buffer_writes ())
|
2005-12-14 16:54:33 +01:00
|
|
|
flags |= O_SYNC;
|
|
|
|
|
2004-03-26 22:43:49 +01:00
|
|
|
ret = fhandler_dev_raw::open (flags);
|
|
|
|
if (ret)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2006-07-26 17:59:39 +02:00
|
|
|
mt.drive (driveno ())->open (get_handle ());
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2004-03-26 22:43:49 +01:00
|
|
|
/* In append mode, seek to beginning of next filemark */
|
|
|
|
if (flags & O_APPEND)
|
2006-07-26 17:59:39 +02:00
|
|
|
mt.drive (driveno ())->set_pos (get_handle (),
|
2004-03-26 22:43:49 +01:00
|
|
|
TAPE_SPACE_FILEMARKS, 1, true);
|
|
|
|
|
2005-12-14 16:54:33 +01:00
|
|
|
if (!(flags & O_DIRECT))
|
2005-12-22 06:57:54 +01:00
|
|
|
{
|
2006-07-26 17:59:39 +02:00
|
|
|
devbufsiz = mt.drive (driveno ())->dp ()->MaximumBlockSize;
|
2012-10-13 14:34:18 +02:00
|
|
|
devbufalign = 1;
|
|
|
|
devbufalloc = devbuf = new char [devbufsiz];
|
2005-12-14 16:54:33 +01:00
|
|
|
}
|
2004-03-26 22:43:49 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
ReleaseMutex (mt_mtx);
|
|
|
|
return ret;
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2005-07-05 05:16:46 +02:00
|
|
|
fhandler_dev_tape::close ()
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2005-07-05 09:28:52 +02:00
|
|
|
int ret = 0;
|
|
|
|
int cret = 0;
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2011-11-14 02:29:49 +01:00
|
|
|
if (!have_execed)
|
2005-07-05 09:28:52 +02:00
|
|
|
{
|
|
|
|
lock (-1);
|
2006-07-26 17:59:39 +02:00
|
|
|
ret = mt.drive (driveno ())->close (get_handle (), is_rewind_device ());
|
2005-07-05 09:28:52 +02:00
|
|
|
if (ret)
|
|
|
|
__seterrno_from_win_error (ret);
|
|
|
|
cret = fhandler_dev_raw::close ();
|
|
|
|
unlock (0);
|
|
|
|
}
|
2013-04-23 11:44:36 +02:00
|
|
|
if (ov.hEvent)
|
|
|
|
CloseHandle (ov.hEvent);
|
2005-06-22 21:59:19 +02:00
|
|
|
CloseHandle (mt_mtx);
|
2005-07-05 09:28:52 +02:00
|
|
|
return ret ? -1 : cret;
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
2013-05-01 03:20:37 +02:00
|
|
|
void __reg3
|
2004-03-26 22:43:49 +01:00
|
|
|
fhandler_dev_tape::raw_read (void *ptr, size_t &ulen)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2004-03-26 22:43:49 +01:00
|
|
|
char *buf = (char *) ptr;
|
|
|
|
size_t len = ulen;
|
|
|
|
size_t block_size;
|
|
|
|
size_t bytes_to_read;
|
|
|
|
size_t bytes_read = 0;
|
|
|
|
int ret = 0;
|
2004-03-15 12:37:37 +01:00
|
|
|
|
2004-04-09 22:39:19 +02:00
|
|
|
if (lastblk_to_read ())
|
2004-03-14 19:01:45 +01:00
|
|
|
{
|
2004-04-09 22:39:19 +02:00
|
|
|
lastblk_to_read (false);
|
2004-03-26 22:43:49 +01:00
|
|
|
ulen = 0;
|
|
|
|
return;
|
|
|
|
}
|
2011-05-04 14:23:00 +02:00
|
|
|
if (!_lock (true))
|
2004-03-26 22:43:49 +01:00
|
|
|
{
|
|
|
|
ulen = (size_t) -1;
|
|
|
|
return;
|
|
|
|
}
|
2006-07-26 17:59:39 +02:00
|
|
|
block_size = mt.drive (driveno ())->mp ()->BlockSize;
|
2005-12-14 16:54:33 +01:00
|
|
|
if (devbuf)
|
2004-03-26 22:43:49 +01:00
|
|
|
{
|
2005-12-14 16:54:33 +01:00
|
|
|
if (devbufend > devbufstart)
|
2004-05-28 21:50:07 +02:00
|
|
|
{
|
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 %lu bytes from buffer (rest %lu)",
|
2005-12-14 16:54:33 +01:00
|
|
|
bytes_to_read, devbufend - devbufstart - bytes_to_read);
|
|
|
|
memcpy (buf, devbuf + devbufstart, bytes_to_read);
|
|
|
|
len -= bytes_to_read;
|
|
|
|
bytes_read += bytes_to_read;
|
|
|
|
buf += bytes_to_read;
|
|
|
|
devbufstart += bytes_to_read;
|
|
|
|
if (devbufstart == devbufend)
|
|
|
|
devbufstart = devbufend = 0;
|
|
|
|
/* If a switch to variable block_size occured, just return the buffer
|
|
|
|
remains until the buffer is empty, then proceed with usual variable
|
|
|
|
block size handling (one block per read call). */
|
|
|
|
if (!block_size)
|
2004-03-26 22:43:49 +01:00
|
|
|
len = 0;
|
|
|
|
}
|
2005-12-14 16:54:33 +01:00
|
|
|
if (len > 0)
|
2004-05-28 21:50:07 +02:00
|
|
|
{
|
2013-04-23 11:44:36 +02:00
|
|
|
if (!ov.hEvent
|
|
|
|
&& !(ov.hEvent = CreateEvent (&sec_none, TRUE, FALSE, NULL)))
|
2005-12-14 16:54:33 +01:00
|
|
|
debug_printf ("Creating event failed, %E");
|
|
|
|
size_t block_fit = !block_size ? len : rounddown(len, block_size);
|
|
|
|
if (block_fit)
|
|
|
|
{
|
2013-04-23 11:44:36 +02:00
|
|
|
debug_printf ("read %lu bytes from tape (rest %lu)",
|
2005-12-14 16:54:33 +01:00
|
|
|
block_fit, len - block_fit);
|
2013-04-23 11:44:36 +02:00
|
|
|
ret = mt.drive (driveno ())->read (get_handle (), &ov, buf,
|
|
|
|
block_fit);
|
2005-12-14 16:54:33 +01:00
|
|
|
if (ret)
|
|
|
|
__seterrno_from_win_error (ret);
|
|
|
|
else if (block_fit)
|
|
|
|
{
|
|
|
|
len -= block_fit;
|
|
|
|
bytes_read += block_fit;
|
|
|
|
buf += block_fit;
|
|
|
|
/* Only one block in each read call, please. */
|
|
|
|
if (!block_size)
|
|
|
|
len = 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
len = 0;
|
|
|
|
if (bytes_read)
|
|
|
|
lastblk_to_read (true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!ret && len > 0)
|
2004-03-26 22:43:49 +01:00
|
|
|
{
|
2013-04-23 11:44:36 +02:00
|
|
|
debug_printf ("read %lu bytes from tape (one block)", block_size);
|
|
|
|
ret = mt.drive (driveno ())->read (get_handle (), &ov, devbuf,
|
|
|
|
block_size);
|
2005-12-14 16:54:33 +01:00
|
|
|
if (ret)
|
|
|
|
__seterrno_from_win_error (ret);
|
|
|
|
else if (block_size)
|
|
|
|
{
|
|
|
|
devbufstart = len;
|
|
|
|
devbufend = block_size;
|
|
|
|
bytes_read += len;
|
|
|
|
memcpy (buf, devbuf, len);
|
|
|
|
}
|
|
|
|
else if (bytes_read)
|
|
|
|
lastblk_to_read (true);
|
2004-03-14 19:01:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-12-14 16:54:33 +01:00
|
|
|
else
|
|
|
|
{
|
2013-04-23 11:44:36 +02:00
|
|
|
if (!ov.hEvent
|
|
|
|
&& !(ov.hEvent = CreateEvent (&sec_none, TRUE, FALSE, NULL)))
|
2005-12-14 16:54:33 +01:00
|
|
|
debug_printf ("Creating event failed, %E");
|
|
|
|
bytes_read = ulen;
|
2013-04-23 11:44:36 +02:00
|
|
|
ret = mt.drive (driveno ())->read (get_handle (), &ov, ptr, bytes_read);
|
2005-12-14 16:54:33 +01:00
|
|
|
}
|
2004-08-20 15:40:35 +02:00
|
|
|
ulen = (ret ? (size_t) -1 : bytes_read);
|
2004-03-26 22:43:49 +01:00
|
|
|
unlock ();
|
|
|
|
}
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2013-05-01 03:20:37 +02:00
|
|
|
ssize_t __reg3
|
2004-03-26 22:43:49 +01:00
|
|
|
fhandler_dev_tape::raw_write (const void *ptr, size_t len)
|
|
|
|
{
|
2011-05-04 14:23:00 +02:00
|
|
|
if (!_lock (true))
|
|
|
|
return -1;
|
2013-04-23 11:44:36 +02:00
|
|
|
if (!ov.hEvent && !(ov.hEvent = CreateEvent (&sec_none, TRUE, FALSE, NULL)))
|
2004-09-03 03:53:12 +02:00
|
|
|
debug_printf ("Creating event failed, %E");
|
2013-04-23 11:44:36 +02:00
|
|
|
int ret = mt.drive (driveno ())->write (get_handle (), &ov, ptr, len);
|
2004-04-20 13:01:22 +02:00
|
|
|
if (ret)
|
2004-03-26 22:43:49 +01:00
|
|
|
__seterrno_from_win_error (ret);
|
2004-08-20 15:40:35 +02:00
|
|
|
return unlock (ret ? -1 : (int) len);
|
2004-03-26 22:43:49 +01:00
|
|
|
}
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2013-04-23 11:44:36 +02:00
|
|
|
off_t
|
|
|
|
fhandler_dev_tape::lseek (off_t offset, int whence)
|
2004-03-26 22:43:49 +01:00
|
|
|
{
|
2013-10-24 19:51:41 +02:00
|
|
|
#if 1
|
|
|
|
/* On Linux lseek on tapes is a no-op. For now, let's keep the old code
|
|
|
|
intact but commented out, should incompatibilities arise. */
|
|
|
|
return 0;
|
|
|
|
#else
|
2004-03-26 22:43:49 +01:00
|
|
|
struct mtop op;
|
|
|
|
struct mtpos pos;
|
|
|
|
DWORD block_size;
|
2013-04-23 11:44:36 +02:00
|
|
|
off_t ret = ILLEGAL_SEEK;
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2004-03-26 22:43:49 +01:00
|
|
|
lock (ILLEGAL_SEEK);
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2013-04-23 11:44:36 +02:00
|
|
|
debug_printf ("lseek (%s, %D, %d)", get_name (), offset, whence);
|
2004-03-15 12:37:37 +01:00
|
|
|
|
2006-07-26 17:59:39 +02:00
|
|
|
block_size = mt.drive (driveno ())->mp ()->BlockSize;
|
2004-03-26 22:43:49 +01:00
|
|
|
if (block_size == 0)
|
|
|
|
{
|
|
|
|
set_errno (EIO);
|
|
|
|
goto out;
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
2004-03-26 22:43:49 +01:00
|
|
|
if (ioctl (MTIOCPOS, &pos))
|
|
|
|
goto out;
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2004-03-26 22:43:49 +01:00
|
|
|
switch (whence)
|
|
|
|
{
|
|
|
|
case SEEK_END:
|
|
|
|
op.mt_op = MTFSF;
|
|
|
|
op.mt_count = 1;
|
|
|
|
if (ioctl (MTIOCTOP, &op))
|
|
|
|
goto out;
|
|
|
|
break;
|
|
|
|
case SEEK_SET:
|
|
|
|
if (whence == SEEK_SET && offset < 0)
|
|
|
|
{
|
|
|
|
set_errno (EINVAL);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SEEK_CUR:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
set_errno (EINVAL);
|
|
|
|
goto out;
|
|
|
|
}
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2004-03-26 22:43:49 +01:00
|
|
|
op.mt_op = MTFSR;
|
|
|
|
op.mt_count = offset / block_size
|
|
|
|
- (whence == SEEK_SET ? pos.mt_blkno : 0);
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2004-03-26 22:43:49 +01:00
|
|
|
if (op.mt_count < 0)
|
|
|
|
{
|
|
|
|
op.mt_op = MTBSR;
|
|
|
|
op.mt_count = -op.mt_count;
|
|
|
|
}
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2004-03-26 22:43:49 +01:00
|
|
|
if (ioctl (MTIOCTOP, &op) || ioctl (MTIOCPOS, &pos))
|
|
|
|
goto out;
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2004-03-26 22:43:49 +01:00
|
|
|
ret = pos.mt_blkno * block_size;
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2004-03-26 22:43:49 +01:00
|
|
|
out:
|
|
|
|
return unlock (ret);
|
2013-10-24 19:51:41 +02:00
|
|
|
#endif
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
2013-01-21 05:34:52 +01:00
|
|
|
int __reg2
|
2013-04-23 11:44:36 +02:00
|
|
|
fhandler_dev_tape::fstat (struct stat *buf)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2004-03-26 22:43:49 +01:00
|
|
|
int ret;
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2004-03-26 22:43:49 +01:00
|
|
|
if (driveno () >= MAX_DRIVE_NUM)
|
|
|
|
{
|
|
|
|
set_errno (ENOENT);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (!(ret = fhandler_base::fstat (buf)))
|
|
|
|
buf->st_blocks = 0;
|
|
|
|
return ret;
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
2004-03-15 12:37:37 +01:00
|
|
|
int
|
2011-10-16 00:37:30 +02:00
|
|
|
fhandler_dev_tape::dup (fhandler_base *child, int flags)
|
2004-03-15 12:37:37 +01:00
|
|
|
{
|
2004-03-26 22:43:49 +01:00
|
|
|
lock (-1);
|
2005-06-22 21:59:19 +02:00
|
|
|
fhandler_dev_tape *fh = (fhandler_dev_tape *) child;
|
2009-12-18 21:32:04 +01:00
|
|
|
if (!DuplicateHandle (GetCurrentProcess (), mt_mtx,
|
2011-06-06 07:02:13 +02:00
|
|
|
GetCurrentProcess (), &fh->mt_mtx,
|
2009-12-18 21:32:04 +01:00
|
|
|
0, TRUE, DUPLICATE_SAME_ACCESS))
|
2005-06-22 21:59:19 +02:00
|
|
|
{
|
2013-04-23 11:44:36 +02:00
|
|
|
debug_printf ("dup(%s) failed, mutex handle %p, %E",
|
2005-06-22 21:59:19 +02:00
|
|
|
get_name (), mt_mtx);
|
|
|
|
__seterrno ();
|
|
|
|
return unlock (-1);
|
|
|
|
}
|
2013-04-23 11:44:36 +02:00
|
|
|
fh->ov.hEvent = NULL;
|
|
|
|
if (ov.hEvent &&
|
|
|
|
!DuplicateHandle (GetCurrentProcess (), ov.hEvent,
|
|
|
|
GetCurrentProcess (), &fh->ov.hEvent,
|
2009-12-18 21:32:04 +01:00
|
|
|
0, TRUE, DUPLICATE_SAME_ACCESS))
|
2005-06-22 21:59:19 +02:00
|
|
|
{
|
2013-04-23 11:44:36 +02:00
|
|
|
debug_printf ("dup(%s) failed, event handle %p, %E",
|
|
|
|
get_name (), ov.hEvent);
|
2005-06-22 21:59:19 +02:00
|
|
|
__seterrno ();
|
|
|
|
return unlock (-1);
|
|
|
|
}
|
2011-10-16 00:37:30 +02:00
|
|
|
return unlock (fhandler_dev_raw::dup (child, flags));
|
2004-03-15 12:37:37 +01:00
|
|
|
}
|
|
|
|
|
2005-06-22 21:59:19 +02:00
|
|
|
void
|
|
|
|
fhandler_dev_tape::fixup_after_fork (HANDLE parent)
|
|
|
|
{
|
|
|
|
fhandler_dev_raw::fixup_after_fork (parent);
|
|
|
|
fork_fixup (parent, mt_mtx, "mt_mtx");
|
2013-04-23 11:44:36 +02:00
|
|
|
if (ov.hEvent)
|
|
|
|
fork_fixup (parent, ov.hEvent, "ov.hEvent");
|
2005-06-22 21:59:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
fhandler_dev_tape::set_close_on_exec (bool val)
|
|
|
|
{
|
|
|
|
fhandler_dev_raw::set_close_on_exec (val);
|
|
|
|
set_no_inheritance (mt_mtx, val);
|
2013-04-23 11:44:36 +02:00
|
|
|
if (ov.hEvent)
|
|
|
|
set_no_inheritance (ov.hEvent, val);
|
2005-06-22 21:59:19 +02:00
|
|
|
}
|
|
|
|
|
2004-03-15 12:37:37 +01:00
|
|
|
int
|
2004-03-26 22:43:49 +01:00
|
|
|
fhandler_dev_tape::ioctl (unsigned int cmd, void *buf)
|
2004-03-15 12:37:37 +01:00
|
|
|
{
|
2004-03-26 22:43:49 +01:00
|
|
|
int ret = 0;
|
|
|
|
lock (-1);
|
|
|
|
if (cmd == MTIOCTOP || cmd == MTIOCGET || cmd == MTIOCPOS)
|
|
|
|
{
|
2006-07-26 17:59:39 +02:00
|
|
|
ret = mt.drive (driveno ())->ioctl (get_handle (), cmd, buf);
|
2004-03-26 22:43:49 +01:00
|
|
|
if (ret)
|
|
|
|
__seterrno_from_win_error (ret);
|
|
|
|
return unlock (ret ? -1 : 0);
|
|
|
|
}
|
|
|
|
return unlock (fhandler_dev_raw::ioctl (cmd, buf));
|
2004-03-15 12:37:37 +01:00
|
|
|
}
|