📄 fhandler.h
字号:
/* fhandler.h Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.This file is part of Cygwin.This software is a copyrighted work licensed under the terms of theCygwin license. Please consult the file "CYGWIN_LICENSE" fordetails. */#ifndef _FHANDLER_H_#define _FHANDLER_H_#include <sys/ioctl.h>#include <fcntl.h>enum{ FH_RBINARY = 0x00001000, /* binary read mode */ FH_WBINARY = 0x00002000, /* binary write mode */ FH_CLOEXEC = 0x00004000, /* close-on-exec */ FH_RBINSET = 0x00008000, /* binary read mode has been explicitly set */ FH_WBINSET = 0x00010000, /* binary write mode has been explicitly set */ FH_APPEND = 0x00020000, /* always append */ FH_ASYNC = 0x00040000, /* async I/O */ FH_SIGCLOSE = 0x00080000, /* signal handler should close fd on interrupt */ FH_SYMLINK = 0x00100000, /* is a symlink */ FH_EXECABL = 0x00200000, /* file looked like it would run: * ends in .exe or .bat or begins with #! */ FH_W95LSBUG = 0x00400000, /* set when lseek is called as a flag that * _write should check if we've moved beyond * EOF, zero filling if so. */ FH_NOHANDLE = 0x00800000, /* No handle associated with fhandler. */ FH_NOEINTR = 0x01000000, /* Set if I/O should be uninterruptible. */ FH_FFIXUP = 0x02000000, /* Set if need to fixup after fork. */ FH_LOCAL = 0x04000000, /* File is unix domain socket */ FH_SHUTRD = 0x08000000, /* Socket saw a SHUT_RD */ FH_SHUTWR = 0x10000000, /* Socket saw a SHUT_WR */ FH_ISREMOTE = 0x10000000, /* File is on a remote drive */ FH_DCEXEC = 0x20000000, /* Don't care if this is executable */ FH_HASACLS = 0x40000000, /* True if fs of file has ACLS */ FH_QUERYOPEN = 0x80000000, /* open file without requesting either read or write access */ /* Device flags */ /* Slow devices */ FH_CONSOLE = 0x00000001, /* is a console */ FH_CONIN = 0x00000002, /* console input */ FH_CONOUT = 0x00000003, /* console output */ FH_TTYM = 0x00000004, /* is a tty master */ FH_TTYS = 0x00000005, /* is a tty slave */ FH_PTYM = 0x00000006, /* is a pty master */ FH_SERIAL = 0x00000007, /* is a serial port */ FH_PIPE = 0x00000008, /* is a pipe */ FH_PIPER = 0x00000009, /* read end of a pipe */ FH_PIPEW = 0x0000000a, /* write end of a pipe */ FH_SOCKET = 0x0000000b, /* is a socket */ FH_WINDOWS = 0x0000000c, /* is a window */ FH_SLOW = 0x00000010, /* "slow" device if below this */ /* Fast devices */ FH_DISK = 0x00000010, /* is a disk */ FH_FLOPPY = 0x00000011, /* is a floppy */ FH_TAPE = 0x00000012, /* is a tape */ FH_NULL = 0x00000013, /* is the null device */ FH_ZERO = 0x00000014, /* is the zero device */ FH_RANDOM = 0x00000015, /* is a random device */ FH_MEM = 0x00000016, /* is a mem device */ FH_CLIPBOARD = 0x00000017, /* is a clipboard device */ FH_OSS_DSP = 0x00000018, /* is a dsp audio device */ FH_CYGDRIVE= 0x00000019, /* /cygdrive/x */ FH_PROC = 0x0000001a, /* /proc */ FH_REGISTRY =0x0000001b, /* /proc/registry */ FH_PROCESS = 0x0000001c, /* /proc/<n> */ FH_NDEV = 0x0000001d, /* Maximum number of devices */ FH_DEVMASK = 0x00000fff, /* devices live here */ FH_BAD = 0xffffffff};#define FHDEVN(n) ((n) & FH_DEVMASK)#define FHISSETF(x) __ISSETF (this, x, FH)#define FHSETF(x) __SETF (this, x, FH)#define FHCLEARF(x) __CLEARF (this, x, FH)#define FHCONDSETF(n, x) __CONDSETF(n, this, x, FH)#define FHSTATOFF 0/* fcntl flags used only internaly. */#define O_NOSYMLINK 0x080000#define O_DIROPEN 0x100000/* newlib used to define O_NDELAY differently from O_NONBLOCK. Now it properly defines both to be the same. Unfortunately, we have to behave properly the old version, too, to accommodate older executables. */#define OLD_O_NDELAY (CYGWIN_VERSION_CHECK_FOR_OLD_O_NONBLOCK ? 4 : 0)/* Care for the old O_NDELAY flag. If one of the flags is set, both flags are set. */#define O_NONBLOCK_MASK (O_NONBLOCK | OLD_O_NDELAY)#define UNCONNECTED 0#define CONNECT_PENDING 1#define CONNECTED 2extern const char *windows_device_names[];extern struct __cygwin_perfile *perfile_table;#define __fmode (*(user_data->fmode_ptr))extern const char proc[];extern const int proc_len;class select_record;class path_conv;class fhandler_disk_file;typedef struct __DIR DIR;struct dirent;struct iovec;enum bg_check_types{ bg_error = -1, bg_eof = 0, bg_ok = 1, bg_signalled = 2};enum executable_states{ is_executable, dont_care_if_executable, not_executable = dont_care_if_executable, dont_know_if_executable};class fhandler_base{ protected: DWORD status; private: int access; HANDLE io_handle; unsigned long namehash; /* hashed filename, used as inode num */ protected: /* Full unix path name of this file */ /* File open flags from open () and fcntl () calls */ int openflags; char *rabuf; /* used for crlf conversion in text files */ size_t ralen; size_t raixget; size_t raixput; size_t rabuflen; const char *unix_path_name; const char *win32_path_name; DWORD open_status; public: void set_name (const char * unix_path, const char *win32_path = NULL, int unit = 0); virtual fhandler_base& operator =(fhandler_base &x); fhandler_base (DWORD dev, int unit = 0); virtual ~fhandler_base (); /* Non-virtual simple accessor functions. */ void set_io_handle (HANDLE x) { io_handle = x; } DWORD get_device () { return status & FH_DEVMASK; } virtual int get_unit () { return 0; } virtual BOOL is_slow () { return get_device () < FH_SLOW; } int get_access () { return access; } void set_access (int x) { access = x; } bool get_async () { return FHISSETF (ASYNC); } void set_async (int x) { FHCONDSETF (x, ASYNC); } int get_flags () { return openflags; } void set_flags (int x, int supplied_bin = 0); bool is_nonblocking (); void set_nonblocking (int yes); bool get_w_binary () { return FHISSETF (WBINSET) ? FHISSETF (WBINARY) : 1; } bool get_r_binary () { return FHISSETF (RBINSET) ? FHISSETF (RBINARY) : 1; } bool get_w_binset () { return FHISSETF (WBINSET); } bool get_r_binset () { return FHISSETF (RBINSET); } void set_w_binary (int b) { FHCONDSETF (b, WBINARY); FHSETF (WBINSET); } void set_r_binary (int b) { FHCONDSETF (b, RBINARY); FHSETF (RBINSET); } void clear_w_binary () {FHCLEARF (WBINARY); FHCLEARF (WBINSET); } void clear_r_binary () {FHCLEARF (RBINARY); FHCLEARF (RBINSET); } bool get_nohandle () { return FHISSETF (NOHANDLE); } void set_nohandle (int x) { FHCONDSETF (x, NOHANDLE); } void set_open_status () {open_status = status;} DWORD get_open_status () {return open_status;} void reset_to_open_binmode () { set_flags ((get_flags () & ~(O_TEXT | O_BINARY)) | ((open_status & (FH_WBINARY | FH_RBINARY) ? O_BINARY : O_TEXT))); } int get_default_fmode (int flags); bool get_r_no_interrupt () { return FHISSETF (NOEINTR); } void set_r_no_interrupt (int b) { FHCONDSETF (b, NOEINTR); } bool get_close_on_exec () { return FHISSETF (CLOEXEC); } int set_close_on_exec_flag (int b) { return FHCONDSETF (b, CLOEXEC); } LPSECURITY_ATTRIBUTES get_inheritance (bool all = 0) { if (all) return get_close_on_exec () ? &sec_all_nih : &sec_all; else return get_close_on_exec () ? &sec_none_nih : &sec_none; } void set_check_win95_lseek_bug (int b = 1) { FHCONDSETF (b, W95LSBUG); } bool get_check_win95_lseek_bug () { return FHISSETF (W95LSBUG); } bool get_need_fork_fixup () { return FHISSETF (FFIXUP); } void set_need_fork_fixup () { FHSETF (FFIXUP); } virtual void set_close_on_exec (int val); virtual void fixup_before_fork_exec (DWORD) {} virtual void fixup_after_fork (HANDLE); virtual void fixup_after_exec (HANDLE) {} bool get_symlink_p () { return FHISSETF (SYMLINK); } void set_symlink_p (int val) { FHCONDSETF (val, SYMLINK); } void set_symlink_p () { FHSETF (SYMLINK); } bool get_socket_p () { return FHISSETF (LOCAL); } void set_socket_p (int val) { FHCONDSETF (val, LOCAL); } void set_socket_p () { FHSETF (LOCAL); } bool get_execable_p () { return FHISSETF (EXECABL); } void set_execable_p (executable_states val) { FHCONDSETF (val == is_executable, EXECABL); FHCONDSETF (val == dont_care_if_executable, DCEXEC); } void set_execable_p () { FHSETF (EXECABL); } bool dont_care_if_execable () { return FHISSETF (DCEXEC); } bool exec_state_isknown () { return FHISSETF (DCEXEC) || FHISSETF (EXECABL); } bool get_append_p () { return FHISSETF (APPEND); } void set_append_p (int val) { FHCONDSETF (val, APPEND); } void set_append_p () { FHSETF (APPEND); } bool get_query_open () { return FHISSETF (QUERYOPEN); } void set_query_open (bool val) { FHCONDSETF (val, QUERYOPEN); } bool get_readahead_valid () { return raixget < ralen; } int puts_readahead (const char *s, size_t len = (size_t) -1); int put_readahead (char value); int get_readahead (); int peek_readahead (int queryput = 0); int eat_readahead (int n); void set_readahead_valid (int val, int ch = -1); int get_readahead_into_buffer (char *buf, size_t buflen); bool has_acls () { return FHISSETF (HASACLS); } void set_has_acls (int val) { FHCONDSETF (val, HASACLS); } bool isremote () { return FHISSETF (ISREMOTE); } void set_isremote (int val) { FHCONDSETF (val, ISREMOTE); } const char *get_name () { return unix_path_name; } const char *get_win32_name () { return win32_path_name; } unsigned long get_namehash () { return namehash; } virtual void hclose (HANDLE h) {CloseHandle (h);} virtual void set_inheritance (HANDLE &h, int not_inheriting); /* fixup fd possibly non-inherited handles after fork */ void fork_fixup (HANDLE parent, HANDLE &h, const char *name); virtual int open (path_conv *real_path, int flags, mode_t mode = 0); virtual int close (); virtual int __stdcall fstat (struct __stat64 *buf, path_conv *) __attribute__ ((regparm (3))); virtual int ioctl (unsigned int cmd, void *); virtual int fcntl (int cmd, void *); virtual char const * ttyname () { return get_name(); } virtual int __stdcall read (void *ptr, size_t len) __attribute__ ((regparm (3))); virtual int write (const void *ptr, size_t len); virtual ssize_t readv (const struct iovec *, int iovcnt, ssize_t tot = -1); virtual ssize_t writev (const struct iovec *, int iovcnt, ssize_t tot = -1); virtual __off64_t lseek (__off64_t offset, int whence); virtual int lock (int, struct flock *); virtual void dump (); virtual int dup (fhandler_base *child); virtual HANDLE mmap (caddr_t *addr, size_t len, DWORD access, int flags, __off64_t off); virtual int munmap (HANDLE h, caddr_t addr, size_t len); virtual int msync (HANDLE h, caddr_t addr, size_t len, int flags); virtual BOOL fixup_mmap_after_fork (HANDLE h, DWORD access, DWORD offset, DWORD size, void *address); void *operator new (size_t, void *p) {return p;} virtual void init (HANDLE, DWORD, mode_t); virtual int tcflush (int); virtual int tcsendbreak (int); virtual int tcdrain (); virtual int tcflow (int); virtual int tcsetattr (int a, const struct termios *t); virtual int tcgetattr (struct termios *t); virtual int tcsetpgrp (const pid_t pid); virtual int tcgetpgrp (); virtual int is_tty () { return 0; } virtual BOOL is_device () { return TRUE; } virtual char *ptsname () { return NULL;} virtual class fhandler_socket *is_socket () { return 0; } virtual class fhandler_console *is_console () { return 0; } virtual int is_windows () {return 0; } virtual int raw_read (void *ptr, size_t ulen); virtual int raw_write (const void *ptr, size_t ulen); /* Virtual accessor functions to hide the fact that some fd's have two handles. */ virtual HANDLE& get_handle () { return io_handle; } virtual HANDLE& get_io_handle () { return io_handle; } virtual HANDLE& get_output_handle () { return io_handle; } virtual bool hit_eof () {return FALSE;} virtual select_record *select_read (select_record *s); virtual select_record *select_write (select_record *s); virtual select_record *select_except (select_record *s); virtual int ready_for_read (int fd, DWORD howlong); virtual const char * get_native_name () { return windows_device_names[FHDEVN (status)]; } virtual bg_check_types bg_check (int) {return bg_ok;} void clear_readahead () { raixput = raixget = ralen = rabuflen = 0; rabuf = NULL; } void operator delete (void *); virtual HANDLE get_guard () const {return NULL;} virtual void set_eof () {} virtual DIR *opendir (path_conv& pc); virtual dirent *readdir (DIR *); virtual __off64_t telldir (DIR *); virtual void seekdir (DIR *, __off64_t); virtual void rewinddir (DIR *); virtual int closedir (DIR *);};class fhandler_socket: public fhandler_base{ private: int addr_family; int type; int connect_secret [4]; HANDLE secret_event; struct _WSAPROTOCOL_INFOA *prot_info_ptr; char *sun_path; int had_connect_or_listen; public: fhandler_socket (); ~fhandler_socket (); int get_socket () { return (int) get_handle(); } fhandler_socket * is_socket () { return this; } bool saw_shutdown_read () const {return FHISSETF (SHUTRD);} bool saw_shutdown_write () const {return FHISSETF (SHUTWR);} void set_shutdown_read () {FHSETF (SHUTRD);} void set_shutdown_write () {FHSETF (SHUTWR);} bool is_unconnected () const {return had_connect_or_listen == UNCONNECTED;} bool is_connect_pending () const {return had_connect_or_listen == CONNECT_PENDING;} bool is_connected () const {return had_connect_or_listen == CONNECTED;} void set_connect_state (int newstate) { had_connect_or_listen = newstate; } int bind (const struct sockaddr *name, int namelen); int connect (const struct sockaddr *name, int namelen); int listen (int backlog); int accept (struct sockaddr *peer, int *len); int getsockname (struct sockaddr *name, int *namelen); int getpeername (struct sockaddr *name, int *namelen); ssize_t readv (const struct iovec *, int iovcnt, ssize_t tot = -1); int recvfrom (void *ptr, size_t len, int flags, struct sockaddr *from, int *fromlen); int recvmsg (struct msghdr *msg, int flags, ssize_t tot = -1); ssize_t writev (const struct iovec *, int iovcnt, ssize_t tot = -1); int sendto (const void *ptr, size_t len, int flags, const struct sockaddr *to, int tolen); int sendmsg (const struct msghdr *msg, int flags, ssize_t tot = -1); int ioctl (unsigned int cmd, void *);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -