00001 /* 00002 Copyright 2003 by Marc J. Rochkind. All rights reserved. 00003 May be copied only for purposes and under conditions described 00004 on the Web page www.basepath.com/aup/copyright.htm. 00005 00006 The Example Files are provided "as is," without any warranty; 00007 without even the implied warranty of merchantability or fitness 00008 for a particular purpose. The author and his publisher are not 00009 responsible for any damages, direct or incidental, resulting 00010 from the use or non-use of these Example Files. 00011 00012 The Example Files may contain defects, and some contain deliberate 00013 coding mistakes that were included for educational reasons. 00014 You are responsible for determining if and how the Example Files 00015 are to be used. 00016 00017 */ 00018 #ifndef _UXFILE_HPP_ 00019 #define _UXFILE_HPP_ 00020 00021 #include <sys/uio.h> 00022 #include <utime.h> 00023 #include <poll.h> 00024 #ifdef FREEBSD 00025 typedef unsigned int nfds_t; /* for NuWait_poll */ 00026 #endif 00027 00028 #ifndef _POSIX_SYNCHRONIZED_IO 00029 #define O_SYNC 0 // for sync default argument 00030 #endif 00031 00032 namespace Ux { 00033 00034 /** 00035 \ingroup Ux 00036 */ 00037 class File : public Base { 00038 protected: 00039 int fd; 00040 const char *path; 00041 ssize_t size; 00042 00043 public: 00044 File(int f = -1, const char *p = NULL, ssize_t s = -1) 00045 : fd(f), path(p), size(s) 00046 { } 00047 File(const char *p, ssize_t s = -1) 00048 : fd(-1), path(p), size(s) 00049 { } 00050 void set(int f = -1, char *p = NULL, ssize_t s = -1) 00051 { 00052 fd = f; 00053 path = p; 00054 size = s; 00055 } 00056 void set(char *p, ssize_t s = -1) 00057 { 00058 fd = -1; 00059 path = p; 00060 size = s; 00061 } 00062 ssize_t get_size(void) const 00063 { return size; } 00064 int get_fd(void) const 00065 { return fd; } 00066 operator const char*() 00067 { return path; } 00068 00069 void alloc(ssize_t n = -1); 00070 void free(void); 00071 size_t get_max_name(const char *path = "/"); 00072 00073 int access(int what, bool want_throw = true); 00074 void chmod(mode_t mode); 00075 void chown(uid_t uid, gid_t gid); 00076 void close(void); 00077 File dup(void); 00078 File dup2(int fd2); 00079 int fcntl(int op, ...); 00080 void fsync(int op = O_SYNC); 00081 void lchown(uid_t uid, gid_t gid); 00082 void link(const char *newpath); 00083 void lockf(int op, off_t len); 00084 void lstat(struct stat *buf); 00085 void mkfifo(mode_t perms = PERM_FILE); 00086 void mknod(mode_t mode, dev_t dev); 00087 void mkstemp(void); 00088 void open(int flags, mode_t perms = PERM_FILE); 00089 long pathconf(int name, long default_val = -1); 00090 static void pipe(File pf[2]); 00091 static int poll(struct pollfd fdinfo[], nfds_t nfds, int timeout = -1); 00092 static int pselect(int nfds, fd_set *readset, fd_set *writeset = NULL, fd_set *errorset = NULL, const struct timespec *timeout = NULL, const sigset_t *sigmask = NULL); 00093 ssize_t read(void *buf, size_t nbytes, off_t offset = -1); 00094 ssize_t readlink(char *buf, size_t bufsize); 00095 ssize_t readv(const struct iovec *iov, int iovcnt); 00096 void rename(const char *newpath); 00097 off_t seek(off_t pos, int whence); 00098 static int select(int nfds, fd_set *readset, fd_set *writeset = NULL, fd_set *errorset = NULL, struct timeval *timeout = NULL); 00099 void stat(struct stat *buf); 00100 void statvfs(struct STATVFS_NAME *buf); 00101 void symlink(const char *newpath); 00102 static void sync(void); 00103 void truncate(off_t length); 00104 void unlink(void); 00105 void utime(const struct utimbuf *timbuf); 00106 ssize_t writev(const struct iovec *iov, int iovcnt); 00107 ssize_t write(const void *buf, size_t nbytes, off_t offset = -1); 00108 }; 00109 00110 } // namespace 00111 00112 #endif // _UXFILE_HPP_