2000-02-18 03:38:33 +08:00
|
|
|
/* fhandler.cc. See console.cc for fhandler_console functions.
|
|
|
|
|
2002-01-15 04:39:59 +08:00
|
|
|
Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002 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 <errno.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
2000-09-08 10:56:55 +08:00
|
|
|
#include <sys/cygwin.h>
|
2002-08-30 23:47:10 +08:00
|
|
|
#include <sys/uio.h>
|
2000-09-08 10:56:55 +08:00
|
|
|
#include <signal.h>
|
2000-08-22 11:58:47 +08:00
|
|
|
#include "cygerrno.h"
|
2001-03-05 14:28:25 +08:00
|
|
|
#include "perprocess.h"
|
2001-07-27 03:22:24 +08:00
|
|
|
#include "security.h"
|
2001-08-07 08:01:42 +08:00
|
|
|
#include "cygwin/version.h"
|
2000-08-22 13:10:20 +08:00
|
|
|
#include "fhandler.h"
|
2001-10-01 12:10:07 +08:00
|
|
|
#include "path.h"
|
2001-04-19 05:10:15 +08:00
|
|
|
#include "dtable.h"
|
|
|
|
#include "cygheap.h"
|
2000-09-08 00:23:51 +08:00
|
|
|
#include "shared_info.h"
|
2001-10-05 12:21:41 +08:00
|
|
|
#include "pinfo.h"
|
2001-10-01 12:10:07 +08:00
|
|
|
#include <assert.h>
|
2002-02-26 01:47:51 +08:00
|
|
|
#include <limits.h>
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
static NO_COPY const int CHUNK_SIZE = 1024; /* Used for crlf conversions */
|
|
|
|
|
2001-09-06 12:41:59 +08:00
|
|
|
struct __cygwin_perfile *perfile_table;
|
2000-07-09 13:29:51 +08:00
|
|
|
|
2001-09-06 12:41:59 +08:00
|
|
|
DWORD binmode;
|
2000-07-01 11:51:55 +08:00
|
|
|
|
2000-09-03 12:16:35 +08:00
|
|
|
inline fhandler_base&
|
|
|
|
fhandler_base::operator =(fhandler_base &x)
|
|
|
|
{
|
|
|
|
memcpy (this, &x, sizeof *this);
|
2001-04-24 10:07:58 +08:00
|
|
|
unix_path_name = x.unix_path_name ? cstrdup (x.unix_path_name) : NULL;
|
|
|
|
win32_path_name = x.win32_path_name ? cstrdup (x.win32_path_name) : NULL;
|
2000-09-03 12:16:35 +08:00
|
|
|
rabuf = NULL;
|
|
|
|
ralen = 0;
|
|
|
|
raixget = 0;
|
|
|
|
raixput = 0;
|
|
|
|
rabuflen = 0;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
int
|
2002-02-20 06:06:50 +08:00
|
|
|
fhandler_base::puts_readahead (const char *s, size_t len)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
int success = 1;
|
2000-05-10 05:55:11 +08:00
|
|
|
while ((*s || (len != (size_t) -1 && len--))
|
2000-09-03 12:16:35 +08:00
|
|
|
&& (success = put_readahead (*s++) > 0))
|
2000-02-18 03:38:33 +08:00
|
|
|
continue;
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fhandler_base::put_readahead (char value)
|
|
|
|
{
|
|
|
|
char *newrabuf;
|
|
|
|
if (raixput < rabuflen)
|
|
|
|
/* Nothing to do */;
|
|
|
|
else if ((newrabuf = (char *) realloc (rabuf, rabuflen += 32)))
|
|
|
|
rabuf = newrabuf;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
rabuf[raixput++] = value;
|
|
|
|
ralen++;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fhandler_base::get_readahead ()
|
|
|
|
{
|
|
|
|
int chret = -1;
|
|
|
|
if (raixget < ralen)
|
2001-04-19 05:10:15 +08:00
|
|
|
chret = ((unsigned char) rabuf[raixget++]) & 0xff;
|
2000-02-18 03:38:33 +08:00
|
|
|
/* FIXME - not thread safe */
|
|
|
|
if (raixget >= ralen)
|
|
|
|
raixget = raixput = ralen = 0;
|
|
|
|
return chret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fhandler_base::peek_readahead (int queryput)
|
|
|
|
{
|
|
|
|
int chret = -1;
|
|
|
|
if (!queryput && raixget < ralen)
|
|
|
|
chret = ((unsigned char) rabuf[raixget]) & 0xff;
|
|
|
|
else if (queryput && raixput > 0)
|
|
|
|
chret = ((unsigned char) rabuf[raixput - 1]) & 0xff;
|
|
|
|
return chret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2002-02-20 06:06:50 +08:00
|
|
|
fhandler_base::set_readahead_valid (int val, int ch)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
if (!val)
|
|
|
|
ralen = raixget = raixput = 0;
|
|
|
|
if (ch != -1)
|
2002-09-22 11:38:57 +08:00
|
|
|
put_readahead (ch);
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fhandler_base::eat_readahead (int n)
|
|
|
|
{
|
|
|
|
int oralen = ralen;
|
|
|
|
if (n < 0)
|
|
|
|
n = ralen;
|
|
|
|
if (n > 0 && ralen)
|
|
|
|
{
|
2000-02-21 13:20:38 +08:00
|
|
|
if ((int) (ralen -= n) < 0)
|
2000-02-18 03:38:33 +08:00
|
|
|
ralen = 0;
|
|
|
|
|
|
|
|
if (raixget >= ralen)
|
|
|
|
raixget = raixput = ralen = 0;
|
|
|
|
else if (raixput > ralen)
|
|
|
|
raixput = ralen;
|
|
|
|
}
|
|
|
|
|
|
|
|
return oralen;
|
|
|
|
}
|
|
|
|
|
2000-03-12 14:29:54 +08:00
|
|
|
int
|
|
|
|
fhandler_base::get_readahead_into_buffer (char *buf, size_t buflen)
|
|
|
|
{
|
|
|
|
int ch;
|
|
|
|
int copied_chars = 0;
|
|
|
|
|
|
|
|
while (buflen)
|
|
|
|
if ((ch = get_readahead ()) < 0)
|
|
|
|
break;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
buf[copied_chars++] = (unsigned char)(ch & 0xff);
|
|
|
|
buflen--;
|
|
|
|
}
|
|
|
|
|
|
|
|
return copied_chars;
|
|
|
|
}
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
/* Record the file name.
|
|
|
|
Filenames are used mostly for debugging messages, and it's hoped that
|
|
|
|
in cases where the name is really required, the filename wouldn't ever
|
2002-05-02 12:13:48 +08:00
|
|
|
be too long (e.g. devices or some such).
|
|
|
|
The unix_path_name is also used by virtual fhandlers. */
|
2000-02-18 03:38:33 +08:00
|
|
|
void
|
2000-06-19 01:43:45 +08:00
|
|
|
fhandler_base::set_name (const char *unix_path, const char *win32_path, int unit)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2000-06-19 01:43:45 +08:00
|
|
|
if (unix_path == NULL || !*unix_path)
|
2000-02-18 03:38:33 +08:00
|
|
|
return;
|
|
|
|
|
2000-06-19 01:43:45 +08:00
|
|
|
if (win32_path)
|
2001-04-24 10:07:58 +08:00
|
|
|
win32_path_name = cstrdup (win32_path);
|
2000-02-18 03:38:33 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
const char *fmt = get_native_name ();
|
2002-09-22 11:38:57 +08:00
|
|
|
char *w = (char *) cmalloc (HEAP_STR, strlen (fmt) + 16);
|
2002-05-24 13:44:10 +08:00
|
|
|
__small_sprintf (w, fmt, unit);
|
|
|
|
win32_path_name = w;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2001-04-24 10:07:58 +08:00
|
|
|
if (win32_path_name == NULL)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
system_printf ("fatal error. strdup failed");
|
|
|
|
exit (ENOMEM);
|
|
|
|
}
|
2001-10-01 12:10:07 +08:00
|
|
|
|
|
|
|
assert (unix_path_name == NULL);
|
|
|
|
/* FIXME: This isn't really right. It ignores the first argument if we're
|
|
|
|
building names for a device and just converts the device name from the
|
|
|
|
win32 name since it has theoretically been previously detected by
|
|
|
|
path_conv. Ideally, we should pass in a format string and build the
|
|
|
|
unix_path, too. */
|
|
|
|
if (!is_device () || *win32_path_name != '\\')
|
2002-05-24 13:44:10 +08:00
|
|
|
unix_path_name = unix_path;
|
2001-10-01 12:10:07 +08:00
|
|
|
else
|
|
|
|
{
|
2002-05-24 13:44:10 +08:00
|
|
|
char *p = cstrdup (win32_path_name);
|
|
|
|
unix_path_name = p;
|
|
|
|
while ((p = strchr (p, '\\')) != NULL)
|
|
|
|
*p++ = '/';
|
|
|
|
if (unix_path)
|
|
|
|
cfree ((void *) unix_path);
|
2001-10-01 12:10:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (unix_path_name == NULL)
|
|
|
|
{
|
|
|
|
system_printf ("fatal error. strdup failed");
|
|
|
|
exit (ENOMEM);
|
|
|
|
}
|
2001-10-30 04:30:47 +08:00
|
|
|
namehash = hash_path_name (0, win32_path_name);
|
2001-10-01 12:10:07 +08:00
|
|
|
}
|
|
|
|
|
2000-11-27 05:45:16 +08:00
|
|
|
/* Detect if we are sitting at EOF for conditions where Windows
|
|
|
|
returns an error but UNIX doesn't. */
|
|
|
|
static int __stdcall
|
|
|
|
is_at_eof (HANDLE h, DWORD err)
|
|
|
|
{
|
|
|
|
DWORD size, upper1, curr;
|
|
|
|
|
|
|
|
size = GetFileSize (h, &upper1);
|
|
|
|
if (upper1 != 0xffffffff || GetLastError () == NO_ERROR)
|
|
|
|
{
|
|
|
|
LONG upper2 = 0;
|
|
|
|
curr = SetFilePointer (h, 0, &upper2, FILE_CURRENT);
|
|
|
|
if (curr == size && upper1 == (DWORD) upper2)
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
SetLastError (err);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-06-20 08:36:40 +08:00
|
|
|
void
|
|
|
|
fhandler_base::set_flags (int flags, int supplied_bin)
|
|
|
|
{
|
|
|
|
int bin;
|
|
|
|
int fmode;
|
|
|
|
debug_printf ("flags %p, supplied_bin %p", flags, supplied_bin);
|
|
|
|
if ((bin = flags & (O_BINARY | O_TEXT)))
|
|
|
|
debug_printf ("O_TEXT/O_BINARY set in flags %p", bin);
|
|
|
|
else if (get_r_binset () && get_w_binset ())
|
|
|
|
bin = get_r_binary () ? O_BINARY : O_TEXT; // FIXME: Not quite right
|
|
|
|
else if ((fmode = get_default_fmode (flags)) & O_BINARY)
|
|
|
|
bin = O_BINARY;
|
|
|
|
else if (fmode & O_TEXT)
|
|
|
|
bin = O_TEXT;
|
|
|
|
else if (supplied_bin)
|
|
|
|
bin = supplied_bin;
|
|
|
|
else
|
2002-09-19 11:30:20 +08:00
|
|
|
bin = get_w_binary () || get_r_binary () || (binmode != O_TEXT)
|
|
|
|
? O_BINARY : O_TEXT;
|
2002-06-20 08:36:40 +08:00
|
|
|
|
|
|
|
openflags = flags | bin;
|
|
|
|
|
|
|
|
bin &= O_BINARY;
|
|
|
|
set_r_binary (bin);
|
|
|
|
set_w_binary (bin);
|
|
|
|
syscall_printf ("filemode set to %s", bin ? "binary" : "text");
|
|
|
|
}
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
/* Normal file i/o handlers. */
|
|
|
|
|
|
|
|
/* Cover function to ReadFile to achieve (as much as possible) Posix style
|
|
|
|
semantics and use of errno. */
|
2002-12-14 12:01:32 +08:00
|
|
|
void
|
|
|
|
fhandler_base::raw_read (void *ptr, size_t& ulen)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2002-12-14 12:01:32 +08:00
|
|
|
#define bytes_read ((ssize_t) ulen)
|
|
|
|
|
2002-12-15 02:01:08 +08:00
|
|
|
HANDLE h = NULL; /* grumble */
|
|
|
|
int prio = 0; /* ditto */
|
2002-12-14 12:01:32 +08:00
|
|
|
DWORD len = ulen;
|
2002-12-15 02:01:08 +08:00
|
|
|
|
2002-12-14 12:01:32 +08:00
|
|
|
(ssize_t) ulen = -1;
|
|
|
|
if (read_state)
|
2002-12-15 02:01:08 +08:00
|
|
|
{
|
|
|
|
h = GetCurrentThread ();
|
|
|
|
prio = GetThreadPriority (h);
|
|
|
|
(void) SetThreadPriority (h, THREAD_PRIORITY_TIME_CRITICAL);
|
|
|
|
SetEvent (read_state);
|
|
|
|
}
|
2002-12-14 12:01:32 +08:00
|
|
|
BOOL res = ReadFile (get_handle (), ptr, len, (DWORD *) &ulen, 0);
|
|
|
|
if (read_state)
|
2002-12-15 02:01:08 +08:00
|
|
|
{
|
|
|
|
SetEvent (read_state);
|
|
|
|
(void) SetThreadPriority (h, prio);
|
|
|
|
}
|
2002-12-14 12:01:32 +08:00
|
|
|
if (!res)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
/* Some errors are not really errors. Detect such cases here. */
|
|
|
|
|
2002-12-14 12:01:32 +08:00
|
|
|
DWORD errcode = GetLastError ();
|
2000-02-18 03:38:33 +08:00
|
|
|
switch (errcode)
|
|
|
|
{
|
|
|
|
case ERROR_BROKEN_PIPE:
|
|
|
|
/* This is really EOF. */
|
|
|
|
bytes_read = 0;
|
|
|
|
break;
|
|
|
|
case ERROR_MORE_DATA:
|
|
|
|
/* `bytes_read' is supposedly valid. */
|
|
|
|
break;
|
2000-11-27 05:45:16 +08:00
|
|
|
case ERROR_NOACCESS:
|
|
|
|
if (is_at_eof (get_handle (), errcode))
|
2002-12-14 12:01:32 +08:00
|
|
|
{
|
|
|
|
bytes_read = 0;
|
|
|
|
break;
|
|
|
|
}
|
2001-10-01 12:10:07 +08:00
|
|
|
case ERROR_INVALID_FUNCTION:
|
|
|
|
case ERROR_INVALID_PARAMETER:
|
2002-09-19 11:30:20 +08:00
|
|
|
case ERROR_INVALID_HANDLE:
|
2001-10-01 12:10:07 +08:00
|
|
|
if (openflags & O_DIROPEN)
|
|
|
|
{
|
|
|
|
set_errno (EISDIR);
|
2002-12-14 12:01:32 +08:00
|
|
|
bytes_read = -1;
|
|
|
|
break;
|
2001-10-01 12:10:07 +08:00
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
default:
|
2001-04-24 10:07:58 +08:00
|
|
|
syscall_printf ("ReadFile %s failed, %E", unix_path_name);
|
2000-09-03 12:16:35 +08:00
|
|
|
__seterrno_from_win_error (errcode);
|
2002-12-14 12:01:32 +08:00
|
|
|
bytes_read = -1;
|
2000-02-18 03:38:33 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2002-12-14 12:01:32 +08:00
|
|
|
#undef bytes_read
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Cover function to WriteFile to provide Posix interface and semantics
|
|
|
|
(as much as possible). */
|
|
|
|
int
|
|
|
|
fhandler_base::raw_write (const void *ptr, size_t len)
|
|
|
|
{
|
|
|
|
DWORD bytes_written;
|
|
|
|
|
2002-09-22 11:38:57 +08:00
|
|
|
if (!WriteFile (get_handle (), ptr, len, &bytes_written, 0))
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
if (GetLastError () == ERROR_DISK_FULL && bytes_written > 0)
|
|
|
|
return bytes_written;
|
|
|
|
__seterrno ();
|
|
|
|
if (get_errno () == EPIPE)
|
|
|
|
raise (SIGPIPE);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return bytes_written;
|
|
|
|
}
|
|
|
|
|
2000-07-09 13:29:51 +08:00
|
|
|
#define ACCFLAGS(x) (x & (O_RDONLY | O_WRONLY | O_RDWR))
|
|
|
|
int
|
|
|
|
fhandler_base::get_default_fmode (int flags)
|
|
|
|
{
|
2002-07-14 04:00:27 +08:00
|
|
|
int fmode = __fmode;
|
2000-07-09 13:29:51 +08:00
|
|
|
if (perfile_table)
|
|
|
|
{
|
|
|
|
size_t nlen = strlen (get_name ());
|
|
|
|
unsigned accflags = ACCFLAGS (flags);
|
|
|
|
for (__cygwin_perfile *pf = perfile_table; pf->name; pf++)
|
2000-11-03 12:27:03 +08:00
|
|
|
if (!*pf->name && ACCFLAGS (pf->flags) == accflags)
|
2002-07-14 04:00:27 +08:00
|
|
|
{
|
|
|
|
fmode = pf->flags & ~(O_RDONLY | O_WRONLY | O_RDWR);
|
|
|
|
break;
|
|
|
|
}
|
2000-11-03 12:27:03 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
size_t pflen = strlen (pf->name);
|
|
|
|
const char *stem = get_name () + nlen - pflen;
|
|
|
|
if (pflen > nlen || (stem != get_name () && !isdirsep (stem[-1])))
|
|
|
|
continue;
|
|
|
|
else if (ACCFLAGS (pf->flags) == accflags && strcasematch (stem, pf->name))
|
2002-07-14 04:00:27 +08:00
|
|
|
{
|
|
|
|
fmode = pf->flags & ~(O_RDONLY | O_WRONLY | O_RDWR);
|
|
|
|
break;
|
|
|
|
}
|
2000-11-03 12:27:03 +08:00
|
|
|
}
|
2000-07-09 13:29:51 +08:00
|
|
|
}
|
2002-07-14 04:00:27 +08:00
|
|
|
return fmode;
|
2000-07-09 13:29:51 +08:00
|
|
|
}
|
|
|
|
|
2001-10-04 10:34:20 +08:00
|
|
|
/* Open system call handler function. */
|
2001-09-23 05:44:07 +08:00
|
|
|
int
|
2002-05-28 09:55:40 +08:00
|
|
|
fhandler_base::open (path_conv *pc, int flags, mode_t mode)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
int res = 0;
|
|
|
|
HANDLE x;
|
|
|
|
int file_attributes;
|
|
|
|
int shared;
|
|
|
|
int creation_distribution;
|
2001-08-07 23:09:54 +08:00
|
|
|
SECURITY_ATTRIBUTES sa = sec_none;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2002-05-28 09:55:40 +08:00
|
|
|
syscall_printf ("(%s, %p) query_open %d", get_win32_name (), flags, get_query_open ());
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
if (get_win32_name () == NULL)
|
|
|
|
{
|
|
|
|
set_errno (ENOENT);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2001-06-15 02:21:17 +08:00
|
|
|
if (get_query_open ())
|
2001-10-01 12:10:07 +08:00
|
|
|
access = 0;
|
2001-06-15 02:21:17 +08:00
|
|
|
else if (get_device () == FH_TAPE)
|
2001-10-01 12:10:07 +08:00
|
|
|
access = GENERIC_READ | GENERIC_WRITE;
|
2000-02-18 03:38:33 +08:00
|
|
|
else if ((flags & (O_RDONLY | O_WRONLY | O_RDWR)) == O_RDONLY)
|
2001-10-01 12:10:07 +08:00
|
|
|
access = GENERIC_READ;
|
2000-02-18 03:38:33 +08:00
|
|
|
else if ((flags & (O_RDONLY | O_WRONLY | O_RDWR)) == O_WRONLY)
|
2001-10-01 12:10:07 +08:00
|
|
|
access = GENERIC_WRITE;
|
2000-02-18 03:38:33 +08:00
|
|
|
else
|
2001-10-01 12:10:07 +08:00
|
|
|
access = GENERIC_READ | GENERIC_WRITE;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2001-02-06 00:10:06 +08:00
|
|
|
/* Allow reliable lseek on disk devices. */
|
|
|
|
if (get_device () == FH_FLOPPY)
|
2001-10-01 12:10:07 +08:00
|
|
|
access |= GENERIC_READ;
|
2001-02-06 00:10:06 +08:00
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
/* FIXME: O_EXCL handling? */
|
|
|
|
|
|
|
|
if ((flags & O_TRUNC) && ((flags & O_ACCMODE) != O_RDONLY))
|
|
|
|
{
|
|
|
|
if (flags & O_CREAT)
|
2001-10-01 12:10:07 +08:00
|
|
|
creation_distribution = CREATE_ALWAYS;
|
2000-02-18 03:38:33 +08:00
|
|
|
else
|
2001-10-01 12:10:07 +08:00
|
|
|
creation_distribution = TRUNCATE_EXISTING;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
else if (flags & O_CREAT)
|
|
|
|
creation_distribution = OPEN_ALWAYS;
|
|
|
|
else
|
|
|
|
creation_distribution = OPEN_EXISTING;
|
|
|
|
|
|
|
|
if ((flags & O_EXCL) && (flags & O_CREAT))
|
2001-10-01 12:10:07 +08:00
|
|
|
creation_distribution = CREATE_NEW;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
if (flags & O_APPEND)
|
2002-09-22 11:38:57 +08:00
|
|
|
set_append_p ();
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
/* These flags are host dependent. */
|
* Makefile.in: Build wincap.o.
* wincap.cc: New file.
* wincap.h: Ditto.
* autoload.cc: Add dynamic load statement for `CreateHardLinkA'.
* dcrt0.cc (os_being_run): Eliminated.
(osname): Ditto.
(iswinnt): Ditto.
(set_os_type): Ditto.
(dll_crt0_1): Call wincap.init() instead of set_os_type().
(_dll_crt0): Ditto.
* environ.cc (set_chunksize): New function.
(parse_thing): `forkchunk' setting now invokes function `set_chunksize'.
* fork.cc (chunksize): Eliminated. Moved to be member of wincap.
* host_dependent.h: Removed.
* syscalls.cc (_link): Try using `CreateHardLinkA' first, if available.
* cygheap.cc, dcrt0.cc, delqueue.cc, dir.cc,
environ.cc, fhandler.cc, fhandler.h, fhandler_console.cc,
fhandler_mem.cc, fork.cc, mmap.cc, net.cc, pinfo.cc, pinfo.h,
security.cc, syscalls.cc, sysconf.cc, syslog.cc, thread.cc,
times.cc, tty.cc, uinfo.cc, uname.cc, winsup.h: Use new wincap
capability check throughout.
* winsup.h: Include wincap.h. Eliminate extern declarations of
`os_being_run' and `iswinnt'. Eliminate `os_type" definition.
* include/cygwin/version.h: Bump version to 1.3.4.
2001-09-13 01:46:37 +08:00
|
|
|
shared = wincap.shared ();
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
file_attributes = FILE_ATTRIBUTE_NORMAL;
|
|
|
|
if (flags & O_DIROPEN)
|
|
|
|
file_attributes |= FILE_FLAG_BACKUP_SEMANTICS;
|
|
|
|
if (get_device () == FH_SERIAL)
|
|
|
|
file_attributes |= FILE_FLAG_OVERLAPPED;
|
|
|
|
|
2001-11-14 05:49:06 +08:00
|
|
|
#ifdef HIDDEN_DOT_FILES
|
|
|
|
if (flags & O_CREAT && get_device () == FH_DISK)
|
|
|
|
{
|
|
|
|
char *c = strrchr (get_win32_name (), '\\');
|
|
|
|
if ((c && c[1] == '.') || *get_win32_name () == '.')
|
2002-05-28 09:55:40 +08:00
|
|
|
file_attributes |= FILE_ATTRIBUTE_HIDDEN;
|
2001-11-14 05:49:06 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2001-06-20 15:44:33 +08:00
|
|
|
/* CreateFile() with dwDesiredAccess == 0 when called on remote
|
|
|
|
share returns some handle, even if file doesn't exist. This code
|
|
|
|
works around this bug. */
|
2002-06-05 09:42:28 +08:00
|
|
|
if (get_query_open () && isremote () &&
|
|
|
|
creation_distribution == OPEN_EXISTING && pc && !pc->exists ())
|
2001-06-20 15:44:33 +08:00
|
|
|
{
|
|
|
|
set_errno (ENOENT);
|
|
|
|
goto done;
|
|
|
|
}
|
2001-08-07 23:09:54 +08:00
|
|
|
|
2002-04-09 21:01:00 +08:00
|
|
|
/* If mode has no write bits set, we set the R/O attribute. */
|
|
|
|
if (!(mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
|
|
|
|
file_attributes |= FILE_ATTRIBUTE_READONLY;
|
|
|
|
|
2001-08-07 23:09:54 +08:00
|
|
|
/* If the file should actually be created and ntsec is on,
|
|
|
|
set files attributes. */
|
|
|
|
if (flags & O_CREAT && get_device () == FH_DISK && allow_ntsec && has_acls ())
|
2001-09-06 11:39:18 +08:00
|
|
|
set_security_attribute (mode, &sa, alloca (4096), 4096);
|
2001-08-07 23:09:54 +08:00
|
|
|
|
2001-10-04 10:34:20 +08:00
|
|
|
x = CreateFile (get_win32_name (), access, shared, &sa, creation_distribution,
|
2001-11-05 14:09:15 +08:00
|
|
|
file_attributes, 0);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2002-09-19 23:12:48 +08:00
|
|
|
syscall_printf ("%p = CreateFile (%s, %p, %p, %p, %p, %p, 0)",
|
2001-10-04 10:34:20 +08:00
|
|
|
x, get_win32_name (), access, shared, &sa,
|
|
|
|
creation_distribution, file_attributes);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
if (x == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
2002-11-01 09:47:29 +08:00
|
|
|
if (!wincap.can_open_directories () && pc && pc->isdir ())
|
2002-09-19 11:30:20 +08:00
|
|
|
{
|
|
|
|
if (mode & (O_CREAT | O_EXCL) == (O_CREAT | O_EXCL))
|
|
|
|
set_errno (EEXIST);
|
|
|
|
else if (mode & (O_WRONLY | O_RDWR))
|
|
|
|
set_errno (EISDIR);
|
|
|
|
else
|
|
|
|
set_nohandle (true);
|
|
|
|
}
|
|
|
|
else if (GetLastError () == ERROR_INVALID_HANDLE)
|
2000-02-18 03:38:33 +08:00
|
|
|
set_errno (ENOENT);
|
|
|
|
else
|
|
|
|
__seterrno ();
|
2002-09-19 11:30:20 +08:00
|
|
|
if (!get_nohandle ())
|
|
|
|
goto done;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2001-08-07 23:09:54 +08:00
|
|
|
/* Attributes may be set only if a file is _really_ created.
|
|
|
|
This code is now only used for ntea here since the files
|
|
|
|
security attributes are set in CreateFile () now. */
|
2000-03-18 01:22:53 +08:00
|
|
|
if (flags & O_CREAT && get_device () == FH_DISK
|
2001-08-07 23:09:54 +08:00
|
|
|
&& GetLastError () != ERROR_ALREADY_EXISTS
|
|
|
|
&& !allow_ntsec && allow_ntea)
|
2000-02-18 03:38:33 +08:00
|
|
|
set_file_attribute (has_acls (), get_win32_name (), mode);
|
|
|
|
|
|
|
|
set_io_handle (x);
|
2002-06-05 09:42:28 +08:00
|
|
|
set_flags (flags, pc ? pc->binmode () : 0);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
res = 1;
|
2001-04-24 10:07:58 +08:00
|
|
|
set_open_status ();
|
2000-02-18 03:38:33 +08:00
|
|
|
done:
|
|
|
|
syscall_printf ("%d = fhandler_base::open (%s, %p)", res, get_win32_name (),
|
|
|
|
flags);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* states:
|
|
|
|
open buffer in binary mode? Just do the read.
|
|
|
|
|
|
|
|
open buffer in text mode? Scan buffer for control zs and handle
|
|
|
|
the first one found. Then scan buffer, converting every \r\n into
|
|
|
|
an \n. If last char is an \r, look ahead one more char, if \n then
|
|
|
|
modify \r, if not, remember char.
|
|
|
|
*/
|
2002-12-14 12:01:32 +08:00
|
|
|
void
|
|
|
|
fhandler_base::read (void *in_ptr, size_t& len)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
char *ptr = (char *) in_ptr;
|
2002-12-14 12:01:32 +08:00
|
|
|
ssize_t copied_chars = 0;
|
2002-12-15 03:11:42 +08:00
|
|
|
bool need_signal = !!read_state;
|
2000-02-18 03:38:33 +08:00
|
|
|
int c;
|
|
|
|
|
|
|
|
while (len)
|
|
|
|
if ((c = get_readahead ()) < 0)
|
|
|
|
break;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ptr[copied_chars++] = (unsigned char) (c & 0xff);
|
|
|
|
len--;
|
|
|
|
}
|
|
|
|
|
2001-11-03 13:42:21 +08:00
|
|
|
if (copied_chars && is_slow ())
|
2002-12-14 12:01:32 +08:00
|
|
|
{
|
|
|
|
len = (size_t) copied_chars;
|
2002-12-15 02:01:08 +08:00
|
|
|
goto out;
|
2002-12-14 12:01:32 +08:00
|
|
|
}
|
2001-11-03 13:42:21 +08:00
|
|
|
|
2002-12-15 02:01:08 +08:00
|
|
|
if (!len)
|
2002-12-14 12:01:32 +08:00
|
|
|
{
|
|
|
|
len = (size_t) copied_chars;
|
2002-12-15 02:01:08 +08:00
|
|
|
goto out;
|
2000-10-08 02:12:11 +08:00
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2002-12-15 02:01:08 +08:00
|
|
|
raw_read (ptr + copied_chars, len);
|
2002-12-15 03:11:42 +08:00
|
|
|
need_signal = false;
|
2002-12-15 02:01:08 +08:00
|
|
|
if (!copied_chars)
|
|
|
|
/* nothing */;
|
|
|
|
else if ((ssize_t) len > 0)
|
|
|
|
len += copied_chars;
|
|
|
|
else
|
|
|
|
len = copied_chars;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2002-12-15 02:01:08 +08:00
|
|
|
if (get_r_binary () || len <= 0)
|
|
|
|
goto out;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
/* Scan buffer and turn \r\n into \n */
|
2002-12-15 02:01:08 +08:00
|
|
|
char *src, *dst, *end;
|
|
|
|
src = (char *) ptr;
|
|
|
|
dst = (char *) ptr;
|
|
|
|
end = src + len - 1;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
/* Read up to the last but one char - the last char needs special handling */
|
|
|
|
while (src < end)
|
|
|
|
{
|
2002-12-14 12:01:32 +08:00
|
|
|
if (*src == '\r' && src[1] == '\n')
|
|
|
|
src++;
|
|
|
|
*dst++ = *src++;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2002-12-15 02:01:08 +08:00
|
|
|
/* If not beyond end and last char is a '\r' then read one more
|
|
|
|
to see if we should translate this one too */
|
|
|
|
if (src > end)
|
|
|
|
/* nothing */;
|
|
|
|
else if (*src != '\r')
|
|
|
|
*dst++ = *src;
|
|
|
|
else
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2002-12-15 02:01:08 +08:00
|
|
|
char c1;
|
|
|
|
size_t c1len = 1;
|
|
|
|
raw_read (&c1, c1len);
|
|
|
|
if (c1len <= 0)
|
2000-02-18 03:38:33 +08:00
|
|
|
/* nothing */;
|
2002-12-15 02:01:08 +08:00
|
|
|
else if (c1 == '\n')
|
|
|
|
*dst++ = '\n';
|
2000-02-18 03:38:33 +08:00
|
|
|
else
|
2002-12-14 12:01:32 +08:00
|
|
|
{
|
2002-12-15 02:01:08 +08:00
|
|
|
set_readahead_valid (1, c1);
|
|
|
|
*dst++ = *src;
|
2002-12-14 12:01:32 +08:00
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2002-12-15 02:01:08 +08:00
|
|
|
len = dst - (char *) ptr;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
#ifndef NOSTRACE
|
2000-03-07 13:33:09 +08:00
|
|
|
if (strace.active)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
char buf[16 * 6 + 1];
|
|
|
|
char *p = buf;
|
|
|
|
|
|
|
|
for (int i = 0; i < copied_chars && i < 16; ++i)
|
|
|
|
{
|
|
|
|
unsigned char c = ((unsigned char *) ptr)[i];
|
|
|
|
/* >= 33 so space prints in hex */
|
|
|
|
__small_sprintf (p, c >= 33 && c <= 127 ? " %c" : " %p", c);
|
|
|
|
p += strlen (p);
|
|
|
|
}
|
|
|
|
debug_printf ("read %d bytes (%s%s)", copied_chars, buf,
|
|
|
|
copied_chars > 16 ? " ..." : "");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2002-12-15 02:01:08 +08:00
|
|
|
out:
|
2002-12-15 03:11:42 +08:00
|
|
|
if (need_signal)
|
|
|
|
SetEvent (read_state);
|
|
|
|
|
2002-12-15 02:01:08 +08:00
|
|
|
debug_printf ("returning %d, %s mode", len,
|
|
|
|
get_r_binary () ? "binary" : "text");
|
2002-12-14 12:01:32 +08:00
|
|
|
return;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fhandler_base::write (const void *ptr, size_t len)
|
|
|
|
{
|
|
|
|
int res;
|
|
|
|
|
|
|
|
if (get_append_p ())
|
2002-09-22 11:38:57 +08:00
|
|
|
SetFilePointer (get_handle (), 0, 0, FILE_END);
|
* Makefile.in: Build wincap.o.
* wincap.cc: New file.
* wincap.h: Ditto.
* autoload.cc: Add dynamic load statement for `CreateHardLinkA'.
* dcrt0.cc (os_being_run): Eliminated.
(osname): Ditto.
(iswinnt): Ditto.
(set_os_type): Ditto.
(dll_crt0_1): Call wincap.init() instead of set_os_type().
(_dll_crt0): Ditto.
* environ.cc (set_chunksize): New function.
(parse_thing): `forkchunk' setting now invokes function `set_chunksize'.
* fork.cc (chunksize): Eliminated. Moved to be member of wincap.
* host_dependent.h: Removed.
* syscalls.cc (_link): Try using `CreateHardLinkA' first, if available.
* cygheap.cc, dcrt0.cc, delqueue.cc, dir.cc,
environ.cc, fhandler.cc, fhandler.h, fhandler_console.cc,
fhandler_mem.cc, fork.cc, mmap.cc, net.cc, pinfo.cc, pinfo.h,
security.cc, syscalls.cc, sysconf.cc, syslog.cc, thread.cc,
times.cc, tty.cc, uinfo.cc, uname.cc, winsup.h: Use new wincap
capability check throughout.
* winsup.h: Include wincap.h. Eliminate extern declarations of
`os_being_run' and `iswinnt'. Eliminate `os_type" definition.
* include/cygwin/version.h: Bump version to 1.3.4.
2001-09-13 01:46:37 +08:00
|
|
|
else if (wincap.has_lseek_bug () && get_check_win95_lseek_bug ())
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
/* Note: this bug doesn't happen on NT4, even though the documentation
|
|
|
|
for WriteFile() says that it *may* happen on any OS. */
|
|
|
|
int actual_length, current_position;
|
|
|
|
set_check_win95_lseek_bug (0); /* don't do it again */
|
|
|
|
actual_length = GetFileSize (get_handle (), NULL);
|
|
|
|
current_position = SetFilePointer (get_handle (), 0, 0, FILE_CURRENT);
|
|
|
|
if (current_position > actual_length)
|
|
|
|
{
|
|
|
|
/* Oops, this is the bug case - Win95 uses whatever is on the disk
|
|
|
|
instead of some known (safe) value, so we must seek back and
|
|
|
|
fill in the gap with zeros. - DJ */
|
|
|
|
char zeros[512];
|
|
|
|
int number_of_zeros_to_write = current_position - actual_length;
|
2002-09-23 08:31:31 +08:00
|
|
|
memset (zeros, 0, 512);
|
2000-02-18 03:38:33 +08:00
|
|
|
SetFilePointer (get_handle (), 0, 0, FILE_END);
|
|
|
|
while (number_of_zeros_to_write > 0)
|
|
|
|
{
|
|
|
|
DWORD zeros_this_time = (number_of_zeros_to_write > 512
|
|
|
|
? 512 : number_of_zeros_to_write);
|
|
|
|
DWORD written;
|
|
|
|
if (!WriteFile (get_handle (), zeros, zeros_this_time, &written,
|
|
|
|
NULL))
|
|
|
|
{
|
|
|
|
__seterrno ();
|
|
|
|
if (get_errno () == EPIPE)
|
|
|
|
raise (SIGPIPE);
|
|
|
|
/* This might fail, but it's the best we can hope for */
|
|
|
|
SetFilePointer (get_handle (), current_position, 0, FILE_BEGIN);
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
}
|
|
|
|
if (written < zeros_this_time) /* just in case */
|
|
|
|
{
|
|
|
|
set_errno (ENOSPC);
|
|
|
|
/* This might fail, but it's the best we can hope for */
|
|
|
|
SetFilePointer (get_handle (), current_position, 0, FILE_BEGIN);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
number_of_zeros_to_write -= written;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (get_w_binary ())
|
|
|
|
{
|
2000-10-08 02:12:11 +08:00
|
|
|
debug_printf ("binary write");
|
2000-02-18 03:38:33 +08:00
|
|
|
res = raw_write (ptr, len);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2000-10-08 02:12:11 +08:00
|
|
|
debug_printf ("text write");
|
2000-02-18 03:38:33 +08:00
|
|
|
/* This is the Microsoft/DJGPP way. Still not ideal, but it's
|
2000-10-08 02:12:11 +08:00
|
|
|
compatible.
|
|
|
|
Modified slightly by CGF 2000-10-07 */
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
int left_in_data = len;
|
|
|
|
char *data = (char *)ptr;
|
2000-10-08 02:12:11 +08:00
|
|
|
res = 0;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
while (left_in_data > 0)
|
|
|
|
{
|
2000-10-08 02:12:11 +08:00
|
|
|
char buf[CHUNK_SIZE + 1], *buf_ptr = buf;
|
2000-02-18 03:38:33 +08:00
|
|
|
int left_in_buf = CHUNK_SIZE;
|
|
|
|
|
|
|
|
while (left_in_buf > 0 && left_in_data > 0)
|
|
|
|
{
|
2000-10-08 02:12:11 +08:00
|
|
|
char ch = *data++;
|
|
|
|
if (ch == '\n')
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
*buf_ptr++ = '\r';
|
|
|
|
left_in_buf--;
|
|
|
|
}
|
2000-10-08 02:12:11 +08:00
|
|
|
*buf_ptr++ = ch;
|
2000-02-18 03:38:33 +08:00
|
|
|
left_in_buf--;
|
|
|
|
left_in_data--;
|
2000-10-08 02:12:11 +08:00
|
|
|
if (left_in_data > 0 && ch == '\r' && *data == '\n')
|
|
|
|
{
|
|
|
|
*buf_ptr++ = *data++;
|
|
|
|
left_in_buf--;
|
|
|
|
left_in_data--;
|
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* We've got a buffer-full, or we're out of data. Write it out */
|
2000-10-08 02:12:11 +08:00
|
|
|
int nbytes;
|
2000-02-18 03:38:33 +08:00
|
|
|
int want = buf_ptr - buf;
|
2000-10-08 02:12:11 +08:00
|
|
|
if ((nbytes = raw_write (buf, want)) == want)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2000-10-08 02:12:11 +08:00
|
|
|
/* Keep track of how much written not counting additional \r's */
|
|
|
|
res = data - (char *)ptr;
|
|
|
|
continue;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2000-10-08 02:12:11 +08:00
|
|
|
if (nbytes == -1)
|
|
|
|
res = -1; /* Error */
|
|
|
|
else
|
|
|
|
res += nbytes; /* Partial write. Return total bytes written. */
|
|
|
|
break; /* All done */
|
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
2000-10-08 02:12:11 +08:00
|
|
|
|
|
|
|
debug_printf ("%d = write (%p, %d)", res, ptr, len);
|
2000-02-18 03:38:33 +08:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2002-08-30 23:47:10 +08:00
|
|
|
ssize_t
|
|
|
|
fhandler_base::readv (const struct iovec *const iov, const int iovcnt,
|
|
|
|
ssize_t tot)
|
|
|
|
{
|
|
|
|
assert (iov);
|
|
|
|
assert (iovcnt >= 1);
|
|
|
|
|
|
|
|
if (iovcnt == 1)
|
2002-12-14 12:01:32 +08:00
|
|
|
{
|
|
|
|
size_t len = iov->iov_len;
|
|
|
|
read (iov->iov_base, len);
|
|
|
|
return len;
|
|
|
|
}
|
2002-08-30 23:47:10 +08:00
|
|
|
|
|
|
|
if (tot == -1) // i.e. if not pre-calculated by the caller.
|
|
|
|
{
|
|
|
|
tot = 0;
|
|
|
|
const struct iovec *iovptr = iov + iovcnt;
|
2002-09-19 11:30:20 +08:00
|
|
|
do
|
2002-08-30 23:47:10 +08:00
|
|
|
{
|
|
|
|
iovptr -= 1;
|
|
|
|
tot += iovptr->iov_len;
|
|
|
|
}
|
|
|
|
while (iovptr != iov);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert (tot >= 0);
|
|
|
|
|
|
|
|
if (tot == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
char *buf = (char *) alloca (tot);
|
|
|
|
|
|
|
|
if (!buf)
|
|
|
|
{
|
|
|
|
set_errno (ENOMEM);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2002-12-14 12:01:32 +08:00
|
|
|
read (buf, (size_t) tot);
|
2002-08-30 23:47:10 +08:00
|
|
|
|
|
|
|
const struct iovec *iovptr = iov;
|
2002-12-14 12:01:32 +08:00
|
|
|
int nbytes = tot;
|
2002-08-30 23:47:10 +08:00
|
|
|
|
|
|
|
while (nbytes > 0)
|
|
|
|
{
|
|
|
|
const int frag = min (nbytes, (ssize_t) iovptr->iov_len);
|
|
|
|
memcpy (iovptr->iov_base, buf, frag);
|
|
|
|
buf += frag;
|
|
|
|
iovptr += 1;
|
|
|
|
nbytes -= frag;
|
|
|
|
}
|
|
|
|
|
2002-12-14 12:01:32 +08:00
|
|
|
return tot;
|
2002-08-30 23:47:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t
|
|
|
|
fhandler_base::writev (const struct iovec *const iov, const int iovcnt,
|
|
|
|
ssize_t tot)
|
|
|
|
{
|
|
|
|
assert (iov);
|
|
|
|
assert (iovcnt >= 1);
|
|
|
|
|
|
|
|
if (iovcnt == 1)
|
|
|
|
return write (iov->iov_base, iov->iov_len);
|
|
|
|
|
|
|
|
if (tot == -1) // i.e. if not pre-calculated by the caller.
|
|
|
|
{
|
|
|
|
tot = 0;
|
|
|
|
const struct iovec *iovptr = iov + iovcnt;
|
2002-09-19 11:30:20 +08:00
|
|
|
do
|
2002-08-30 23:47:10 +08:00
|
|
|
{
|
|
|
|
iovptr -= 1;
|
|
|
|
tot += iovptr->iov_len;
|
|
|
|
}
|
|
|
|
while (iovptr != iov);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert (tot >= 0);
|
|
|
|
|
|
|
|
if (tot == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
char *const buf = (char *) alloca (tot);
|
|
|
|
|
|
|
|
if (!buf)
|
|
|
|
{
|
|
|
|
set_errno (ENOMEM);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *bufptr = buf;
|
|
|
|
const struct iovec *iovptr = iov;
|
|
|
|
int nbytes = tot;
|
|
|
|
|
|
|
|
while (nbytes != 0)
|
|
|
|
{
|
|
|
|
const int frag = min (nbytes, (ssize_t) iovptr->iov_len);
|
|
|
|
memcpy (bufptr, iovptr->iov_base, frag);
|
|
|
|
bufptr += frag;
|
|
|
|
iovptr += 1;
|
|
|
|
nbytes -= frag;
|
|
|
|
}
|
|
|
|
|
|
|
|
return write (buf, tot);
|
|
|
|
}
|
|
|
|
|
2002-02-26 01:47:51 +08:00
|
|
|
__off64_t
|
|
|
|
fhandler_base::lseek (__off64_t offset, int whence)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2002-02-26 01:47:51 +08:00
|
|
|
__off64_t res;
|
|
|
|
|
|
|
|
/* 9x/Me doesn't support 64bit offsets. We trap that here and return
|
|
|
|
EINVAL. It doesn't make sense to simulate bigger offsets by a
|
|
|
|
SetFilePointer sequence since FAT and FAT32 don't support file
|
|
|
|
size >= 4GB anyway. */
|
|
|
|
if (!wincap.has_64bit_file_access ()
|
|
|
|
&& (offset < LONG_MIN || offset > LONG_MAX))
|
|
|
|
{
|
|
|
|
debug_printf ("Win9x, offset not 32 bit.");
|
|
|
|
set_errno (EINVAL);
|
|
|
|
return (__off64_t)-1;
|
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
/* Seeks on text files is tough, we rewind and read till we get to the
|
|
|
|
right place. */
|
|
|
|
|
|
|
|
if (whence != SEEK_CUR || offset != 0)
|
|
|
|
{
|
|
|
|
if (whence == SEEK_CUR)
|
|
|
|
offset -= ralen - raixget;
|
|
|
|
set_readahead_valid (0);
|
|
|
|
}
|
|
|
|
|
2002-02-26 01:47:51 +08:00
|
|
|
debug_printf ("lseek (%s, %D, %d)", unix_path_name, offset, whence);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
DWORD win32_whence = whence == SEEK_SET ? FILE_BEGIN
|
|
|
|
: (whence == SEEK_CUR ? FILE_CURRENT : FILE_END);
|
|
|
|
|
2002-02-26 01:47:51 +08:00
|
|
|
LONG off_low = offset & 0xffffffff;
|
2002-05-18 03:30:52 +08:00
|
|
|
LONG *poff_high, off_high;
|
|
|
|
if (!wincap.has_64bit_file_access ())
|
|
|
|
poff_high = NULL;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
off_high = offset >> 32;
|
|
|
|
poff_high = &off_high;
|
|
|
|
}
|
2002-02-26 01:47:51 +08:00
|
|
|
|
2002-05-18 03:30:52 +08:00
|
|
|
debug_printf ("setting file pointer to %u (high), %u (low)", off_high, off_low);
|
2002-09-22 11:38:57 +08:00
|
|
|
res = SetFilePointer (get_handle (), off_low, poff_high, win32_whence);
|
2002-02-26 01:47:51 +08:00
|
|
|
if (res == INVALID_SET_FILE_POINTER && GetLastError ())
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
__seterrno ();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* When next we write(), we will check to see if *this* seek went beyond
|
|
|
|
the end of the file, and back-seek and fill with zeros if so - DJ */
|
|
|
|
set_check_win95_lseek_bug ();
|
|
|
|
|
|
|
|
/* If this was a SEEK_CUR with offset 0, we still might have
|
|
|
|
readahead that we have to take into account when calculating
|
|
|
|
the actual position for the application. */
|
|
|
|
if (whence == SEEK_CUR)
|
|
|
|
res -= ralen - raixget;
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2001-09-01 13:17:34 +08:00
|
|
|
fhandler_base::close ()
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
int res = -1;
|
|
|
|
|
2002-09-22 11:38:57 +08:00
|
|
|
syscall_printf ("closing '%s' handle %p", get_name (), get_handle ());
|
|
|
|
if (get_nohandle () || CloseHandle (get_handle ()))
|
2000-02-18 03:38:33 +08:00
|
|
|
res = 0;
|
|
|
|
else
|
|
|
|
{
|
2002-09-22 11:38:57 +08:00
|
|
|
paranoid_printf ("CloseHandle (%d <%s>) failed", get_handle (),
|
2000-02-18 03:38:33 +08:00
|
|
|
get_name ());
|
|
|
|
|
|
|
|
__seterrno ();
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fhandler_base::ioctl (unsigned int cmd, void *buf)
|
|
|
|
{
|
|
|
|
if (cmd == FIONBIO)
|
|
|
|
syscall_printf ("ioctl (FIONBIO, %p)", buf);
|
|
|
|
else
|
|
|
|
syscall_printf ("ioctl (%x, %p)", cmd, buf);
|
|
|
|
|
|
|
|
set_errno (EINVAL);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fhandler_base::lock (int, struct flock *)
|
|
|
|
{
|
2002-09-19 11:30:20 +08:00
|
|
|
set_errno (EINVAL);
|
2000-02-18 03:38:33 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" char * __stdcall
|
2002-09-22 11:38:57 +08:00
|
|
|
rootdir (char *full_path)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
/* Possible choices:
|
|
|
|
* d:... -> d:/
|
|
|
|
* \\server\share... -> \\server\share\
|
|
|
|
* else current drive.
|
|
|
|
*/
|
2000-10-17 07:55:58 +08:00
|
|
|
char *root = full_path;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
if (full_path[1] == ':')
|
|
|
|
strcpy (full_path + 2, "\\");
|
|
|
|
else if (full_path[0] == '\\' && full_path[1] == '\\')
|
|
|
|
{
|
|
|
|
char *cp = full_path + 2;
|
|
|
|
while (*cp && *cp != '\\')
|
|
|
|
cp++;
|
|
|
|
if (!*cp)
|
|
|
|
{
|
|
|
|
set_errno (ENOTDIR);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
cp++;
|
|
|
|
while (*cp && *cp != '\\')
|
|
|
|
cp++;
|
|
|
|
strcpy (cp, "\\");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
root = NULL;
|
|
|
|
|
|
|
|
return root;
|
|
|
|
}
|
|
|
|
|
2001-10-05 12:21:41 +08:00
|
|
|
int __stdcall
|
2002-02-26 01:47:51 +08:00
|
|
|
fhandler_base::fstat (struct __stat64 *buf, path_conv *)
|
2001-10-05 12:21:41 +08:00
|
|
|
{
|
2002-07-14 04:00:27 +08:00
|
|
|
debug_printf ("here");
|
2001-10-05 12:21:41 +08:00
|
|
|
switch (get_device ())
|
|
|
|
{
|
2002-06-24 10:23:14 +08:00
|
|
|
case FH_PIPE:
|
|
|
|
buf->st_mode = S_IFIFO | STD_RBITS | STD_WBITS | S_IWGRP | S_IWOTH;
|
|
|
|
break;
|
2001-10-05 12:21:41 +08:00
|
|
|
case FH_PIPEW:
|
2002-06-24 10:23:14 +08:00
|
|
|
buf->st_mode = S_IFIFO | STD_WBITS | S_IWGRP | S_IWOTH;
|
2001-10-05 12:21:41 +08:00
|
|
|
break;
|
|
|
|
case FH_PIPER:
|
2002-06-24 10:23:14 +08:00
|
|
|
buf->st_mode = S_IFIFO | STD_RBITS;
|
|
|
|
break;
|
|
|
|
case FH_FLOPPY:
|
|
|
|
buf->st_mode = S_IFBLK | STD_RBITS | STD_WBITS | S_IWGRP | S_IWOTH;
|
2001-10-05 12:21:41 +08:00
|
|
|
break;
|
|
|
|
default:
|
2002-06-24 10:23:14 +08:00
|
|
|
buf->st_mode = S_IFCHR | STD_RBITS | STD_WBITS | S_IWGRP | S_IWOTH;
|
2001-10-05 12:21:41 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf->st_nlink = 1;
|
|
|
|
buf->st_blksize = S_BLKSIZE;
|
2002-06-06 23:35:09 +08:00
|
|
|
time_as_timestruc_t (&buf->st_ctim);
|
|
|
|
buf->st_atim = buf->st_mtim = buf->st_ctim;
|
2001-10-05 12:21:41 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
void
|
|
|
|
fhandler_base::init (HANDLE f, DWORD a, mode_t bin)
|
|
|
|
{
|
|
|
|
set_io_handle (f);
|
2001-04-24 10:07:58 +08:00
|
|
|
access = a;
|
2000-02-18 03:38:33 +08:00
|
|
|
a &= GENERIC_READ | GENERIC_WRITE;
|
2002-06-05 09:42:28 +08:00
|
|
|
int flags = 0;
|
2000-02-18 03:38:33 +08:00
|
|
|
if (a == GENERIC_READ)
|
2002-06-05 09:42:28 +08:00
|
|
|
flags = O_RDONLY;
|
2002-06-01 04:48:14 +08:00
|
|
|
else if (a == GENERIC_WRITE)
|
2002-06-05 09:42:28 +08:00
|
|
|
flags = O_WRONLY;
|
2002-06-01 04:48:14 +08:00
|
|
|
else if (a == (GENERIC_READ | GENERIC_WRITE))
|
2002-06-05 09:42:28 +08:00
|
|
|
flags = O_RDWR;
|
2002-07-04 02:02:54 +08:00
|
|
|
set_flags (flags | bin);
|
2001-04-24 10:07:58 +08:00
|
|
|
set_open_status ();
|
2002-06-04 09:40:53 +08:00
|
|
|
debug_printf ("created new fhandler_base for handle %p, bin %d", f, get_r_binary ());
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
fhandler_base::dump (void)
|
|
|
|
{
|
|
|
|
paranoid_printf ("here");
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fhandler_base::dup (fhandler_base *child)
|
|
|
|
{
|
|
|
|
debug_printf ("in fhandler_base dup");
|
|
|
|
|
|
|
|
HANDLE nh;
|
2002-09-22 11:38:57 +08:00
|
|
|
if (!get_nohandle ())
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2002-09-22 11:38:57 +08:00
|
|
|
if (!DuplicateHandle (hMainProc, get_handle (), hMainProc, &nh, 0, TRUE,
|
|
|
|
DUPLICATE_SAME_ACCESS))
|
|
|
|
{
|
|
|
|
system_printf ("dup(%s) failed, handle %x, %E",
|
|
|
|
get_name (), get_handle ());
|
|
|
|
__seterrno ();
|
|
|
|
return -1;
|
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2002-09-22 11:38:57 +08:00
|
|
|
child->set_io_handle (nh);
|
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2000-10-24 04:16:52 +08:00
|
|
|
int fhandler_base::fcntl (int cmd, void *arg)
|
|
|
|
{
|
|
|
|
int res;
|
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case F_GETFD:
|
|
|
|
res = get_close_on_exec () ? FD_CLOEXEC : 0;
|
|
|
|
break;
|
|
|
|
case F_SETFD:
|
|
|
|
set_close_on_exec ((int) arg);
|
|
|
|
res = 0;
|
|
|
|
break;
|
|
|
|
case F_GETFL:
|
|
|
|
res = get_flags ();
|
2000-10-25 02:15:25 +08:00
|
|
|
debug_printf ("GETFL: %d", res);
|
2000-10-24 04:16:52 +08:00
|
|
|
break;
|
|
|
|
case F_SETFL:
|
2000-10-24 04:29:31 +08:00
|
|
|
{
|
|
|
|
/*
|
2000-10-25 02:15:25 +08:00
|
|
|
* Only O_APPEND, O_ASYNC and O_NONBLOCK/O_NDELAY are allowed.
|
2000-10-24 04:29:31 +08:00
|
|
|
* Each other flag will be ignored.
|
|
|
|
* Since O_ASYNC isn't defined in fcntl.h it's currently
|
|
|
|
* ignored as well.
|
|
|
|
*/
|
2001-09-08 05:32:07 +08:00
|
|
|
const int allowed_flags = O_APPEND | O_NONBLOCK_MASK;
|
2001-08-07 08:01:42 +08:00
|
|
|
int new_flags = (int) arg & allowed_flags;
|
2001-09-08 05:32:07 +08:00
|
|
|
/* Carefully test for the O_NONBLOCK or deprecated OLD_O_NDELAY flag.
|
2001-08-07 08:01:42 +08:00
|
|
|
Set only the flag that has been passed in. If both are set, just
|
|
|
|
record O_NONBLOCK. */
|
|
|
|
if ((new_flags & OLD_O_NDELAY) && (new_flags & O_NONBLOCK))
|
|
|
|
new_flags = O_NONBLOCK;
|
|
|
|
set_flags ((get_flags () & ~allowed_flags) | new_flags);
|
2000-10-24 04:29:31 +08:00
|
|
|
}
|
2000-10-24 04:16:52 +08:00
|
|
|
res = 0;
|
|
|
|
break;
|
|
|
|
case F_GETLK:
|
|
|
|
case F_SETLK:
|
|
|
|
case F_SETLKW:
|
|
|
|
res = lock (cmd, (struct flock *) arg);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
set_errno (EINVAL);
|
|
|
|
res = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
/* Base terminal handlers. These just return errors. */
|
|
|
|
|
|
|
|
int
|
2000-02-21 13:20:38 +08:00
|
|
|
fhandler_base::tcflush (int)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
set_errno (ENOTTY);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2000-02-21 13:20:38 +08:00
|
|
|
fhandler_base::tcsendbreak (int)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
set_errno (ENOTTY);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fhandler_base::tcdrain (void)
|
|
|
|
{
|
|
|
|
set_errno (ENOTTY);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2000-02-21 13:20:38 +08:00
|
|
|
fhandler_base::tcflow (int)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
set_errno (ENOTTY);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2000-02-21 13:20:38 +08:00
|
|
|
fhandler_base::tcsetattr (int, const struct termios *)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
set_errno (ENOTTY);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2000-02-21 13:20:38 +08:00
|
|
|
fhandler_base::tcgetattr (struct termios *)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
set_errno (ENOTTY);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2000-02-21 13:20:38 +08:00
|
|
|
fhandler_base::tcsetpgrp (const pid_t)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
set_errno (ENOTTY);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fhandler_base::tcgetpgrp (void)
|
|
|
|
{
|
|
|
|
set_errno (ENOTTY);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2000-09-03 12:16:35 +08:00
|
|
|
void
|
|
|
|
fhandler_base::operator delete (void *p)
|
|
|
|
{
|
|
|
|
cfree (p);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
/* Normal I/O constructor */
|
2001-10-14 01:23:35 +08:00
|
|
|
fhandler_base::fhandler_base (DWORD devtype, int unit):
|
2001-10-23 02:39:22 +08:00
|
|
|
status (devtype),
|
2001-04-24 10:07:58 +08:00
|
|
|
access (0),
|
2000-02-18 03:38:33 +08:00
|
|
|
io_handle (NULL),
|
2001-04-24 10:07:58 +08:00
|
|
|
namehash (0),
|
|
|
|
openflags (0),
|
2000-02-18 03:38:33 +08:00
|
|
|
rabuf (NULL),
|
|
|
|
ralen (0),
|
|
|
|
raixget (0),
|
|
|
|
raixput (0),
|
2001-04-24 10:07:58 +08:00
|
|
|
rabuflen (0),
|
2001-10-01 12:10:07 +08:00
|
|
|
unix_path_name (NULL),
|
|
|
|
win32_path_name (NULL),
|
2002-12-14 12:01:32 +08:00
|
|
|
open_status (0),
|
|
|
|
read_state (NULL)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Normal I/O destructor */
|
|
|
|
fhandler_base::~fhandler_base (void)
|
|
|
|
{
|
2001-10-01 12:10:07 +08:00
|
|
|
if (unix_path_name != NULL)
|
2002-05-24 13:44:10 +08:00
|
|
|
cfree ((void *) unix_path_name);
|
2001-10-01 12:10:07 +08:00
|
|
|
if (win32_path_name != NULL)
|
2002-05-24 13:44:10 +08:00
|
|
|
cfree ((void *) win32_path_name);
|
2000-09-03 12:16:35 +08:00
|
|
|
if (rabuf)
|
|
|
|
free (rabuf);
|
2001-04-24 10:07:58 +08:00
|
|
|
unix_path_name = win32_path_name = NULL;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**********************************************************************/
|
|
|
|
/* /dev/null */
|
|
|
|
|
2001-10-14 01:23:35 +08:00
|
|
|
fhandler_dev_null::fhandler_dev_null () :
|
|
|
|
fhandler_base (FH_NULL)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
fhandler_dev_null::dump (void)
|
|
|
|
{
|
|
|
|
paranoid_printf ("here");
|
|
|
|
}
|
|
|
|
|
2000-04-20 12:38:10 +08:00
|
|
|
void
|
2001-09-21 04:58:29 +08:00
|
|
|
fhandler_base::set_inheritance (HANDLE &h, int not_inheriting)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2002-07-15 03:15:32 +08:00
|
|
|
#ifdef DEBUGGING_AND_FDS_PROTECTED
|
2001-10-23 05:09:41 +08:00
|
|
|
HANDLE oh = h;
|
2001-10-24 12:16:45 +08:00
|
|
|
#endif
|
2001-09-21 04:58:29 +08:00
|
|
|
/* Note that we could use SetHandleInformation here but it is not available
|
|
|
|
on all platforms. Test cases seem to indicate that using DuplicateHandle
|
|
|
|
in this fashion does not actually close the original handle, which is
|
|
|
|
what we want. If this changes in the future, we may be forced to use
|
|
|
|
SetHandleInformation on newer OS's */
|
|
|
|
if (!DuplicateHandle (hMainProc, h, hMainProc, &h, 0, !not_inheriting,
|
|
|
|
DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE))
|
|
|
|
debug_printf ("DuplicateHandle failed, %E");
|
2002-07-15 03:15:32 +08:00
|
|
|
#ifdef DEBUGGING_AND_FDS_PROTECTED
|
2001-10-24 12:16:45 +08:00
|
|
|
if (h)
|
2002-07-14 04:00:27 +08:00
|
|
|
setclexec (oh, h, not_inheriting);
|
2000-02-18 03:38:33 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
fhandler_base::fork_fixup (HANDLE parent, HANDLE &h, const char *name)
|
|
|
|
{
|
2001-10-14 12:14:24 +08:00
|
|
|
if (/* !is_socket () && */ !get_close_on_exec ())
|
2001-07-21 11:20:01 +08:00
|
|
|
debug_printf ("handle %p already opened", h);
|
2001-10-14 12:14:24 +08:00
|
|
|
else if (!DuplicateHandle (parent, h, hMainProc, &h, 0, !get_close_on_exec (),
|
2001-10-06 09:04:24 +08:00
|
|
|
DUPLICATE_SAME_ACCESS))
|
2000-02-18 03:38:33 +08:00
|
|
|
system_printf ("%s - %E, handle %s<%p>", get_name (), name, h);
|
2002-07-15 03:15:32 +08:00
|
|
|
#ifdef DEBUGGING_AND_FDS_PROTECTED
|
|
|
|
else if (get_close_on_exec ())
|
|
|
|
ProtectHandle (h); /* would have to be fancier than this */
|
2001-10-06 09:04:24 +08:00
|
|
|
else
|
2002-07-15 03:15:32 +08:00
|
|
|
/* ProtectHandleINH (h) */; /* Should already be protected */
|
2001-10-06 09:04:24 +08:00
|
|
|
#endif
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
fhandler_base::set_close_on_exec (int val)
|
|
|
|
{
|
2002-09-19 11:30:20 +08:00
|
|
|
if (!get_nohandle ())
|
|
|
|
set_inheritance (io_handle, val);
|
2000-02-18 03:38:33 +08:00
|
|
|
set_close_on_exec_flag (val);
|
|
|
|
debug_printf ("set close_on_exec for %s to %d", get_name (), val);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
fhandler_base::fixup_after_fork (HANDLE parent)
|
|
|
|
{
|
|
|
|
debug_printf ("inheriting '%s' from parent", get_name ());
|
2002-09-19 11:30:20 +08:00
|
|
|
if (!get_nohandle ())
|
|
|
|
fork_fixup (parent, io_handle, "io_handle");
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
2001-08-15 15:49:15 +08:00
|
|
|
|
2001-11-24 11:11:39 +08:00
|
|
|
bool
|
2001-08-15 15:49:15 +08:00
|
|
|
fhandler_base::is_nonblocking ()
|
|
|
|
{
|
|
|
|
return (openflags & O_NONBLOCK_MASK) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
fhandler_base::set_nonblocking (int yes)
|
|
|
|
{
|
|
|
|
int current = openflags & O_NONBLOCK_MASK;
|
|
|
|
int new_flags = yes ? (!current ? O_NONBLOCK : current) : 0;
|
|
|
|
openflags = (openflags & ~O_NONBLOCK_MASK) | new_flags;
|
|
|
|
}
|
2001-11-21 14:47:57 +08:00
|
|
|
|
|
|
|
DIR *
|
2001-11-22 13:59:07 +08:00
|
|
|
fhandler_base::opendir (path_conv&)
|
2001-11-21 14:47:57 +08:00
|
|
|
{
|
|
|
|
set_errno (ENOTDIR);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct dirent *
|
|
|
|
fhandler_base::readdir (DIR *)
|
|
|
|
{
|
|
|
|
set_errno (ENOTDIR);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2002-02-26 01:47:51 +08:00
|
|
|
__off64_t
|
2001-11-21 14:47:57 +08:00
|
|
|
fhandler_base::telldir (DIR *)
|
|
|
|
{
|
|
|
|
set_errno (ENOTDIR);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2002-02-26 01:47:51 +08:00
|
|
|
fhandler_base::seekdir (DIR *, __off64_t)
|
2001-11-21 14:47:57 +08:00
|
|
|
{
|
|
|
|
set_errno (ENOTDIR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
fhandler_base::rewinddir (DIR *)
|
|
|
|
{
|
|
|
|
set_errno (ENOTDIR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fhandler_base::closedir (DIR *)
|
|
|
|
{
|
|
|
|
set_errno (ENOTDIR);
|
|
|
|
return -1;
|
|
|
|
}
|