2000-02-17 20:38:33 +01:00
|
|
|
/* fhandler_raw.cc. See fhandler.h for a description of the fhandler classes.
|
|
|
|
|
2002-06-05 06:01:43 +02:00
|
|
|
Copyright 1999, 2000, 2001, 2002 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"
|
2000-02-17 20:38:33 +01:00
|
|
|
#include <sys/termios.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <cygwin/rdevio.h>
|
|
|
|
#include <sys/mtio.h>
|
2001-10-16 16:53:26 +02:00
|
|
|
#include <ntdef.h>
|
2000-08-22 05:58:47 +02:00
|
|
|
#include "cygerrno.h"
|
2001-03-05 07:28:25 +01:00
|
|
|
#include "perprocess.h"
|
2001-07-26 21:22:24 +02:00
|
|
|
#include "security.h"
|
2000-08-22 07:10:20 +02:00
|
|
|
#include "fhandler.h"
|
2001-10-01 06:10:07 +02:00
|
|
|
#include "path.h"
|
2001-04-18 23:10:15 +02:00
|
|
|
#include "dtable.h"
|
|
|
|
#include "cygheap.h"
|
2001-10-16 16:53:26 +02:00
|
|
|
#include "ntdll.h"
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2000-05-12 20:34:50 +02:00
|
|
|
/* static wrapper functions to hide the effect of media changes and
|
|
|
|
bus resets which occurs after a new media is inserted. This is
|
|
|
|
also related to the used tape device. */
|
|
|
|
|
|
|
|
static BOOL write_file (HANDLE fh, const void *buf, DWORD to_write,
|
2000-09-03 06:16:35 +02:00
|
|
|
DWORD *written, int *err)
|
2000-05-12 20:34:50 +02:00
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
|
|
|
|
*err = 0;
|
|
|
|
if (!(ret = WriteFile (fh, buf, to_write, written, 0)))
|
|
|
|
{
|
|
|
|
if ((*err = GetLastError ()) == ERROR_MEDIA_CHANGED
|
2000-09-03 06:16:35 +02:00
|
|
|
|| *err == ERROR_BUS_RESET)
|
|
|
|
{
|
|
|
|
*err = 0;
|
|
|
|
if (!(ret = WriteFile (fh, buf, to_write, written, 0)))
|
|
|
|
*err = GetLastError ();
|
|
|
|
}
|
2000-05-12 20:34:50 +02:00
|
|
|
}
|
2000-07-23 21:33:44 +02:00
|
|
|
syscall_printf ("%d (err %d) = WriteFile (%d, %d, write %d, written %d, 0)",
|
2000-09-03 06:16:35 +02:00
|
|
|
ret, *err, fh, buf, to_write, *written);
|
2000-05-12 20:34:50 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL read_file (HANDLE fh, void *buf, DWORD to_read,
|
2000-09-03 06:16:35 +02:00
|
|
|
DWORD *read, int *err)
|
2000-05-12 20:34:50 +02:00
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
|
|
|
|
*err = 0;
|
2002-09-22 05:38:57 +02:00
|
|
|
if (!(ret = ReadFile (fh, buf, to_read, read, 0)))
|
2000-05-12 20:34:50 +02:00
|
|
|
{
|
|
|
|
if ((*err = GetLastError ()) == ERROR_MEDIA_CHANGED
|
2000-09-03 06:16:35 +02:00
|
|
|
|| *err == ERROR_BUS_RESET)
|
|
|
|
{
|
|
|
|
*err = 0;
|
|
|
|
if (!(ret = ReadFile (fh, buf, to_read, read, 0)))
|
|
|
|
*err = GetLastError ();
|
|
|
|
}
|
2000-05-12 20:34:50 +02:00
|
|
|
}
|
2000-07-23 21:33:44 +02:00
|
|
|
syscall_printf ("%d (err %d) = ReadFile (%d, %d, to_read %d, read %d, 0)",
|
2000-09-03 06:16:35 +02:00
|
|
|
ret, *err, fh, buf, to_read, *read);
|
2000-05-12 20:34:50 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2000-02-17 20:38:33 +01:00
|
|
|
/**********************************************************************/
|
|
|
|
/* fhandler_dev_raw */
|
|
|
|
|
|
|
|
void
|
|
|
|
fhandler_dev_raw::clear (void)
|
|
|
|
{
|
|
|
|
devbuf = NULL;
|
|
|
|
devbufsiz = 0;
|
|
|
|
devbufstart = 0;
|
|
|
|
devbufend = 0;
|
|
|
|
eom_detected = 0;
|
|
|
|
eof_detected = 0;
|
|
|
|
lastblk_to_read = 0;
|
2000-07-23 21:33:44 +02:00
|
|
|
varblkop = 0;
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fhandler_dev_raw::writebuf (void)
|
|
|
|
{
|
|
|
|
DWORD written;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if (is_writing && devbuf && devbufend)
|
|
|
|
{
|
2000-07-23 21:33:44 +02:00
|
|
|
DWORD to_write;
|
|
|
|
int ret = 0;
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2000-07-23 21:33:44 +02:00
|
|
|
memset (devbuf + devbufend, 0, devbufsiz - devbufend);
|
|
|
|
if (get_device () != FH_TAPE)
|
2000-09-03 06:16:35 +02:00
|
|
|
to_write = ((devbufend - 1) / 512 + 1) * 512;
|
2000-07-23 21:33:44 +02:00
|
|
|
else if (varblkop)
|
2000-09-03 06:16:35 +02:00
|
|
|
to_write = devbufend;
|
2000-07-23 21:33:44 +02:00
|
|
|
else
|
2000-09-03 06:16:35 +02:00
|
|
|
to_write = devbufsiz;
|
2000-07-23 21:33:44 +02:00
|
|
|
if (!write_file (get_handle (), devbuf, to_write, &written, &ret)
|
2000-09-03 06:16:35 +02:00
|
|
|
&& is_eom (ret))
|
2000-07-23 21:33:44 +02:00
|
|
|
eom_detected = 1;
|
2000-02-17 20:38:33 +01:00
|
|
|
if (written)
|
|
|
|
has_written = 1;
|
|
|
|
devbufstart = devbufend = 0;
|
|
|
|
}
|
|
|
|
is_writing = 0;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2001-10-22 20:39:22 +02:00
|
|
|
fhandler_dev_raw::fhandler_dev_raw (DWORD devtype, int nunit)
|
|
|
|
: fhandler_base (devtype), unit (nunit)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
|
|
|
clear ();
|
2000-10-31 23:20:59 +01:00
|
|
|
set_need_fork_fixup ();
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fhandler_dev_raw::~fhandler_dev_raw (void)
|
|
|
|
{
|
2000-10-31 23:20:59 +01:00
|
|
|
if (devbufsiz > 1L)
|
|
|
|
delete [] devbuf;
|
2000-02-17 20:38:33 +01:00
|
|
|
clear ();
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2001-10-16 16:53:26 +02:00
|
|
|
fhandler_dev_raw::open (path_conv *real_path, int flags, mode_t)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2001-10-16 16:53:26 +02:00
|
|
|
if (!wincap.has_raw_devices ())
|
|
|
|
{
|
|
|
|
set_errno (ENOENT);
|
2002-09-22 05:38:57 +02:00
|
|
|
debug_printf ("%s is accessible under NT/W2K only",real_path->get_win32());
|
2001-10-16 16:53:26 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for illegal flags. */
|
|
|
|
if (flags & (O_APPEND | O_EXCL))
|
|
|
|
{
|
|
|
|
set_errno (EINVAL);
|
|
|
|
return 0;
|
|
|
|
}
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2001-02-05 17:10:06 +01:00
|
|
|
/* Always open a raw device existing and binary. */
|
|
|
|
flags &= ~(O_CREAT | O_TRUNC);
|
|
|
|
flags |= O_BINARY;
|
2001-10-16 16:53:26 +02:00
|
|
|
|
2001-10-16 22:17:23 +02:00
|
|
|
DWORD access = GENERIC_READ | SYNCHRONIZE;
|
|
|
|
if (get_device () == FH_TAPE
|
|
|
|
|| (flags & (O_RDONLY | O_WRONLY | O_RDWR)) == O_WRONLY
|
|
|
|
|| (flags & (O_RDONLY | O_WRONLY | O_RDWR)) == O_RDWR)
|
|
|
|
access |= GENERIC_WRITE;
|
|
|
|
|
|
|
|
extern void str2buf2uni (UNICODE_STRING &, WCHAR *, const char *);
|
|
|
|
UNICODE_STRING dev;
|
|
|
|
WCHAR devname[MAX_PATH + 1];
|
|
|
|
str2buf2uni (dev, devname, real_path->get_win32 ());
|
|
|
|
OBJECT_ATTRIBUTES attr;
|
|
|
|
InitializeObjectAttributes (&attr, &dev, OBJ_CASE_INSENSITIVE, NULL, NULL);
|
|
|
|
|
|
|
|
HANDLE h;
|
|
|
|
IO_STATUS_BLOCK io;
|
|
|
|
NTSTATUS status = NtOpenFile (&h, access, &attr, &io, wincap.shared (),
|
|
|
|
FILE_SYNCHRONOUS_IO_NONALERT);
|
|
|
|
if (!NT_SUCCESS (status))
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2002-05-17 11:32:45 +02:00
|
|
|
__seterrno_from_win_error (RtlNtStatusToDosError (status));
|
2001-10-16 22:17:23 +02:00
|
|
|
return 0;
|
2001-10-16 16:53:26 +02:00
|
|
|
}
|
|
|
|
|
2001-10-16 22:17:23 +02:00
|
|
|
set_io_handle (h);
|
2002-07-01 21:03:26 +02:00
|
|
|
set_flags ((flags & ~O_TEXT) | O_BINARY);
|
2001-10-16 16:53:26 +02:00
|
|
|
|
|
|
|
if (devbufsiz > 1L)
|
|
|
|
devbuf = new char [devbufsiz];
|
|
|
|
|
|
|
|
return 1;
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fhandler_dev_raw::close (void)
|
|
|
|
{
|
|
|
|
return fhandler_base::close ();
|
|
|
|
}
|
|
|
|
|
2002-12-14 05:01:32 +01:00
|
|
|
void
|
|
|
|
fhandler_dev_raw::raw_read (void *ptr, size_t& ulen)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
|
|
|
DWORD bytes_read = 0;
|
|
|
|
DWORD read2;
|
|
|
|
DWORD bytes_to_read;
|
|
|
|
int ret;
|
|
|
|
size_t len = ulen;
|
|
|
|
char *tgt;
|
|
|
|
|
|
|
|
/* In mode O_RDWR the buffer has to be written to device first */
|
|
|
|
ret = writebuf ();
|
|
|
|
if (ret)
|
|
|
|
{
|
2003-08-17 19:12:18 +02:00
|
|
|
if (is_eom (ret))
|
|
|
|
set_errno (ENOSPC);
|
|
|
|
else
|
|
|
|
__seterrno ();
|
2002-12-14 05:01:32 +01:00
|
|
|
goto err;
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Checking a previous end of file */
|
|
|
|
if (eof_detected && !lastblk_to_read)
|
|
|
|
{
|
|
|
|
eof_detected = 0;
|
2002-12-14 05:01:32 +01:00
|
|
|
ulen = 0;
|
|
|
|
return;
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Checking a previous end of media */
|
|
|
|
if (eom_detected && !lastblk_to_read)
|
|
|
|
{
|
|
|
|
set_errno (ENOSPC);
|
2002-12-14 05:01:32 +01:00
|
|
|
goto err;
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (devbuf)
|
|
|
|
{
|
|
|
|
while (len > 0)
|
|
|
|
{
|
|
|
|
if (devbufstart < devbufend)
|
|
|
|
{
|
|
|
|
bytes_to_read = min (len, devbufend - devbufstart);
|
2000-07-23 21:33:44 +02:00
|
|
|
debug_printf ("read %d bytes from buffer (rest %d)",
|
2000-02-17 20:38:33 +01:00
|
|
|
bytes_to_read, devbufstart - devbufend);
|
|
|
|
memcpy (ptr, devbuf + devbufstart, bytes_to_read);
|
|
|
|
len -= bytes_to_read;
|
|
|
|
ptr = (void *) ((char *) ptr + bytes_to_read);
|
|
|
|
bytes_read += bytes_to_read;
|
|
|
|
devbufstart += bytes_to_read;
|
|
|
|
|
|
|
|
if (lastblk_to_read)
|
|
|
|
{
|
|
|
|
lastblk_to_read = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (len > 0)
|
|
|
|
{
|
2000-07-23 21:33:44 +02:00
|
|
|
if (!varblkop && len >= devbufsiz)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2000-09-03 06:16:35 +02:00
|
|
|
if (get_device () == FH_TAPE)
|
2000-07-23 21:33:44 +02:00
|
|
|
bytes_to_read = (len / devbufsiz) * devbufsiz;
|
2000-09-03 06:16:35 +02:00
|
|
|
else
|
2000-07-23 21:33:44 +02:00
|
|
|
bytes_to_read = (len / 512) * 512;
|
2000-02-17 20:38:33 +01:00
|
|
|
tgt = (char *) ptr;
|
2000-07-23 21:33:44 +02:00
|
|
|
debug_printf ("read %d bytes direct from file",bytes_to_read);
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bytes_to_read = devbufsiz;
|
|
|
|
tgt = devbuf;
|
2000-09-03 06:16:35 +02:00
|
|
|
if (varblkop)
|
2000-07-23 21:33:44 +02:00
|
|
|
debug_printf ("read variable bytes from file into buffer");
|
2000-09-03 06:16:35 +02:00
|
|
|
else
|
2000-07-23 21:33:44 +02:00
|
|
|
debug_printf ("read %d bytes from file into buffer",
|
|
|
|
bytes_to_read);
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
2000-05-12 20:34:50 +02:00
|
|
|
if (!read_file (get_handle (), tgt, bytes_to_read, &read2, &ret))
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
|
|
|
if (!is_eof (ret) && !is_eom (ret))
|
|
|
|
{
|
2003-08-17 19:12:18 +02:00
|
|
|
__seterrno ();
|
2002-12-14 05:01:32 +01:00
|
|
|
goto err;
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (is_eof (ret))
|
|
|
|
eof_detected = 1;
|
|
|
|
else
|
|
|
|
eom_detected = 1;
|
|
|
|
|
|
|
|
if (!read2)
|
|
|
|
{
|
|
|
|
if (!bytes_read && is_eom (ret))
|
|
|
|
{
|
2000-09-03 06:16:35 +02:00
|
|
|
debug_printf ("return -1, set errno to ENOSPC");
|
2000-02-17 20:38:33 +01:00
|
|
|
set_errno (ENOSPC);
|
2002-12-14 05:01:32 +01:00
|
|
|
goto err;
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
lastblk_to_read = 1;
|
|
|
|
}
|
|
|
|
if (! read2)
|
|
|
|
break;
|
|
|
|
if (tgt == devbuf)
|
|
|
|
{
|
|
|
|
devbufstart = 0;
|
|
|
|
devbufend = read2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
len -= bytes_to_read;
|
|
|
|
ptr = (void *) ((char *) ptr + bytes_to_read);
|
|
|
|
bytes_read += bytes_to_read;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2000-05-12 20:34:50 +02:00
|
|
|
else if (!read_file (get_handle (), ptr, len, &bytes_read, &ret))
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
|
|
|
if (!is_eof (ret) && !is_eom (ret))
|
|
|
|
{
|
2003-08-17 19:12:18 +02:00
|
|
|
__seterrno ();
|
2002-12-14 05:01:32 +01:00
|
|
|
goto err;
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
if (bytes_read)
|
|
|
|
{
|
|
|
|
if (is_eof (ret))
|
|
|
|
eof_detected = 1;
|
|
|
|
else
|
|
|
|
eom_detected = 1;
|
|
|
|
}
|
|
|
|
else if (is_eom (ret))
|
|
|
|
{
|
2000-09-03 06:16:35 +02:00
|
|
|
debug_printf ("return -1, set errno to ENOSPC");
|
2000-02-17 20:38:33 +01:00
|
|
|
set_errno (ENOSPC);
|
2002-12-14 05:01:32 +01:00
|
|
|
goto err;
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-12-14 05:01:32 +01:00
|
|
|
(ssize_t) ulen = bytes_read;
|
|
|
|
return;
|
|
|
|
|
|
|
|
err:
|
|
|
|
(ssize_t) ulen = -1;
|
|
|
|
return;
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fhandler_dev_raw::raw_write (const void *ptr, size_t len)
|
|
|
|
{
|
|
|
|
DWORD bytes_written = 0;
|
|
|
|
DWORD bytes_to_write;
|
|
|
|
DWORD written;
|
|
|
|
char *p = (char *) ptr;
|
|
|
|
char *tgt;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Checking a previous end of media on tape */
|
|
|
|
if (eom_detected)
|
|
|
|
{
|
|
|
|
set_errno (ENOSPC);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_writing)
|
2001-02-05 17:10:06 +01:00
|
|
|
{
|
|
|
|
devbufend = devbufstart;
|
|
|
|
devbufstart = 0;
|
|
|
|
}
|
2000-02-17 20:38:33 +01:00
|
|
|
is_writing = 1;
|
|
|
|
|
|
|
|
if (devbuf)
|
|
|
|
{
|
|
|
|
while (len > 0)
|
|
|
|
{
|
2000-07-23 21:33:44 +02:00
|
|
|
if (!varblkop &&
|
2000-09-03 06:16:35 +02:00
|
|
|
(len < devbufsiz || devbufend > 0) && devbufend < devbufsiz)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
|
|
|
bytes_to_write = min (len, devbufsiz - devbufend);
|
|
|
|
memcpy (devbuf + devbufend, p, bytes_to_write);
|
|
|
|
bytes_written += bytes_to_write;
|
|
|
|
devbufend += bytes_to_write;
|
|
|
|
p += bytes_to_write;
|
|
|
|
len -= bytes_to_write;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2000-07-23 21:33:44 +02:00
|
|
|
if (varblkop)
|
|
|
|
{
|
|
|
|
bytes_to_write = len;
|
|
|
|
tgt = p;
|
|
|
|
}
|
|
|
|
else if (devbufend == devbufsiz)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
|
|
|
bytes_to_write = devbufsiz;
|
|
|
|
tgt = devbuf;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bytes_to_write = (len / devbufsiz) * devbufsiz;
|
|
|
|
tgt = p;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
2000-05-12 20:34:50 +02:00
|
|
|
write_file (get_handle (), tgt, bytes_to_write, &written, &ret);
|
2000-02-17 20:38:33 +01:00
|
|
|
if (written)
|
|
|
|
has_written = 1;
|
|
|
|
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
if (!is_eom (ret))
|
|
|
|
{
|
|
|
|
__seterrno ();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
eom_detected = 1;
|
|
|
|
|
|
|
|
if (!written && !bytes_written)
|
|
|
|
{
|
|
|
|
set_errno (ENOSPC);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tgt == p)
|
|
|
|
bytes_written += written;
|
|
|
|
|
|
|
|
break; // from while (len > 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tgt == devbuf)
|
|
|
|
{
|
|
|
|
if (written != devbufsiz)
|
|
|
|
memmove (devbuf, devbuf + written, devbufsiz - written);
|
|
|
|
devbufend = devbufsiz - written;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
len -= written;
|
|
|
|
p += written;
|
|
|
|
bytes_written += written;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (len > 0)
|
|
|
|
{
|
2000-05-12 20:34:50 +02:00
|
|
|
if (!write_file (get_handle (), ptr, len, &bytes_written, &ret))
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
|
|
|
if (bytes_written)
|
|
|
|
has_written = 1;
|
|
|
|
if (!is_eom (ret))
|
|
|
|
{
|
2003-08-17 19:12:18 +02:00
|
|
|
__seterrno ();
|
2000-02-17 20:38:33 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
eom_detected = 1;
|
|
|
|
if (!bytes_written)
|
|
|
|
{
|
|
|
|
set_errno (ENOSPC);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
has_written = 1;
|
|
|
|
}
|
|
|
|
return bytes_written;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fhandler_dev_raw::dup (fhandler_base *child)
|
|
|
|
{
|
|
|
|
int ret = fhandler_base::dup (child);
|
|
|
|
|
|
|
|
if (! ret)
|
|
|
|
{
|
|
|
|
fhandler_dev_raw *fhc = (fhandler_dev_raw *) child;
|
|
|
|
|
|
|
|
fhc->devbufsiz = devbufsiz;
|
|
|
|
if (devbufsiz > 1L)
|
2000-10-31 23:20:59 +01:00
|
|
|
fhc->devbuf = new char [devbufsiz];
|
|
|
|
fhc->devbufstart = 0;
|
|
|
|
fhc->devbufend = 0;
|
2000-02-17 20:38:33 +01:00
|
|
|
fhc->eom_detected = eom_detected;
|
|
|
|
fhc->eof_detected = eof_detected;
|
2000-10-31 23:20:59 +01:00
|
|
|
fhc->lastblk_to_read = 0;
|
2000-07-23 21:33:44 +02:00
|
|
|
fhc->varblkop = varblkop;
|
2000-02-17 20:38:33 +01:00
|
|
|
fhc->unit = unit;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2000-10-31 23:20:59 +01:00
|
|
|
void
|
|
|
|
fhandler_dev_raw::fixup_after_fork (HANDLE)
|
2001-09-20 13:29:48 +02:00
|
|
|
{
|
|
|
|
devbufstart = 0;
|
|
|
|
devbufend = 0;
|
|
|
|
lastblk_to_read = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
fhandler_dev_raw::fixup_after_exec (HANDLE)
|
2000-10-31 23:20:59 +01:00
|
|
|
{
|
|
|
|
if (devbufsiz > 1L)
|
|
|
|
devbuf = new char [devbufsiz];
|
|
|
|
devbufstart = 0;
|
|
|
|
devbufend = 0;
|
|
|
|
lastblk_to_read = 0;
|
|
|
|
}
|
|
|
|
|
2000-02-17 20:38:33 +01:00
|
|
|
int
|
|
|
|
fhandler_dev_raw::ioctl (unsigned int cmd, void *buf)
|
|
|
|
{
|
|
|
|
int ret = NO_ERROR;
|
|
|
|
|
|
|
|
if (cmd == RDIOCDOP)
|
|
|
|
{
|
|
|
|
struct rdop *op = (struct rdop *) buf;
|
|
|
|
|
|
|
|
if (!op)
|
|
|
|
ret = ERROR_INVALID_PARAMETER;
|
|
|
|
else
|
|
|
|
switch (op->rd_op)
|
|
|
|
{
|
|
|
|
case RDSETBLK:
|
|
|
|
if (get_device () == FH_TAPE)
|
|
|
|
{
|
|
|
|
struct mtop mop;
|
|
|
|
|
|
|
|
mop.mt_op = MTSETBLK;
|
|
|
|
mop.mt_count = op->rd_parm;
|
|
|
|
ret = ioctl (MTIOCTOP, &mop);
|
|
|
|
}
|
|
|
|
else if (op->rd_parm % 512)
|
|
|
|
ret = ERROR_INVALID_PARAMETER;
|
|
|
|
else if (devbuf && op->rd_parm < devbufend - devbufstart)
|
|
|
|
ret = ERROR_INVALID_PARAMETER;
|
|
|
|
else if (!devbuf || op->rd_parm != devbufsiz)
|
|
|
|
{
|
2000-10-31 23:20:59 +01:00
|
|
|
char *buf = new char [op->rd_parm];
|
|
|
|
if (devbufsiz > 1L)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
|
|
|
memcpy (buf, devbuf + devbufstart, devbufend - devbufstart);
|
|
|
|
devbufend -= devbufstart;
|
2000-10-31 23:20:59 +01:00
|
|
|
delete [] devbuf;
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
devbufend = 0;
|
|
|
|
|
|
|
|
devbufstart = 0;
|
|
|
|
devbuf = buf;
|
|
|
|
devbufsiz = op->rd_parm;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (cmd == RDIOCGET)
|
|
|
|
{
|
|
|
|
struct rdget *get = (struct rdget *) buf;
|
|
|
|
|
|
|
|
if (!get)
|
|
|
|
ret = ERROR_INVALID_PARAMETER;
|
|
|
|
else
|
|
|
|
get->bufsiz = devbufsiz ? devbufsiz : 1L;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return fhandler_base::ioctl (cmd, buf);
|
|
|
|
|
|
|
|
if (ret != NO_ERROR)
|
|
|
|
{
|
|
|
|
SetLastError (ret);
|
|
|
|
__seterrno ();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|