ucommon
|
00001 // Copyright (C) 2006-2014 David Sugar, Tycho Softworks. 00002 // 00003 // This file is part of GNU uCommon C++. 00004 // 00005 // GNU uCommon C++ is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU Lesser General Public License as published 00007 // by the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // GNU uCommon C++ is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU Lesser General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU Lesser General Public License 00016 // along with GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>. 00017 00027 #ifndef _UCOMMON_FSYS_H_ 00028 #define _UCOMMON_FSYS_H_ 00029 00030 #ifndef _UCOMMON_CONFIG_H_ 00031 #include <ucommon/platform.h> 00032 #endif 00033 00034 #ifndef _UCOMMON_PROTOCOLS_H_ 00035 #include <ucommon/protocols.h> 00036 #endif 00037 00038 #ifndef _UCOMMON_THREAD_H_ 00039 #include <ucommon/thread.h> 00040 #endif 00041 00042 #ifndef _UCOMMON_STRING_H_ 00043 #include <ucommon/string.h> 00044 #endif 00045 00046 #ifndef _UCOMMON_MEMORY_H_ 00047 #include <ucommon/memory.h> 00048 #endif 00049 00050 #ifndef _MSWINDOWS_ 00051 #include <sys/stat.h> 00052 #else 00053 #include <io.h> 00054 #ifndef R_OK 00055 #define F_OK 0 00056 #define X_OK 1 00057 #define W_OK 2 00058 #define R_OK 4 00059 #endif 00060 #endif 00061 00062 #include <errno.h> 00063 #include <stdio.h> 00064 00065 #ifndef __S_ISTYPE 00066 #define __S_ISTYPE(mode, mask) (((mode) & S_IFMT) == (mask)) 00067 #endif 00068 00069 #if !defined(S_ISDIR) && defined(S_IFDIR) 00070 #define S_ISDIR(mode) __S_ISTYPE((mode), S_IFDIR) 00071 #endif 00072 00073 #if !defined(S_ISCHR) && defined(S_IFCHR) 00074 #define S_ISCHR(mode) __S_ISTYPE((mode), S_IFCHR) 00075 #elif !defined(S_ISCHR) 00076 #define S_ISCHR(mode) 0 00077 #endif 00078 00079 #if !defined(S_ISBLK) && defined(S_IFBLK) 00080 #define S_ISBLK(mode) __S_ISTYPE((mode), S_IFBLK) 00081 #elif !defined(S_ISBLK) 00082 #define S_ISBLK(mode) 0 00083 #endif 00084 00085 #if !defined(S_ISREG) && defined(S_IFREG) 00086 #define S_ISREG(mode) __S_ISTYPE((mode), S_IFREG) 00087 #elif !defined(S_ISREG) 00088 #define S_ISREG(mode) 1 00089 #endif 00090 00091 #if !defined(S_ISSOCK) && defined(S_IFSOCK) 00092 #define S_ISSOCK(mode) __S_ISTYPE((mode), S_IFSOCK) 00093 #elif !defined(S_ISSOCK) 00094 #define S_ISSOCK(mode) (0) 00095 #endif 00096 00097 #if !defined(S_ISFIFO) && defined(S_IFIFO) 00098 #define S_ISFIFO(mode) __S_ISTYPE((mode), S_IFIFO) 00099 #elif !defined(S_ISFIFO) 00100 #define S_ISFIFO(mode) (0) 00101 #endif 00102 00103 #if !defined(S_ISLNK) && defined(S_IFLNK) 00104 #define S_ISLNK(mode) __S_ISTYPE((mode), S_IFLNK) 00105 #elif !defined(S_ISLNK) 00106 #define S_ISLNK(mode) (0) 00107 #endif 00108 00109 namespace ucommon { 00110 00114 typedef void *mem_t; 00115 00124 class __EXPORT fsys 00125 { 00126 protected: 00127 fd_t fd; 00128 int error; 00129 00130 public: 00134 enum { 00135 OWNER_READONLY = 0400, 00136 GROUP_READONLY = 0440, 00137 PUBLIC_READONLY = 0444, 00138 OWNER_PRIVATE = 0600, 00139 OWNER_PUBLIC = 0644, 00140 GROUP_PRIVATE = 0660, 00141 GROUP_PUBLIC = 0664, 00142 EVERYONE = 0666, 00143 DIR_TEMPORARY = 01777 00144 }; 00145 00146 typedef struct stat fileinfo_t; 00147 00148 #ifdef _MSWINDOWS_ 00149 static int remapError(void); 00150 #else 00151 inline static int remapError(void) 00152 {return errno;} 00153 #endif 00154 00158 typedef enum { 00159 RDONLY, 00160 WRONLY, 00161 REWRITE, 00162 RDWR = REWRITE, 00163 APPEND, 00164 SHARED, 00165 EXCLUSIVE, 00166 DEVICE, 00167 STREAM, 00168 RANDOM 00169 } access_t; 00170 00174 typedef long offset_t; 00175 00179 static const offset_t end; 00180 00184 fsys(); 00185 00189 fsys(fd_t handle); 00190 00195 fsys(const fsys& descriptor); 00196 00202 fsys(const char *path, access_t access); 00203 00210 fsys(const char *path, unsigned permission, access_t access); 00211 00215 ~fsys(); 00216 00221 inline fd_t operator*() const 00222 {return fd;} 00223 00228 inline operator fd_t() const 00229 {return fd;} 00230 00234 inline void reset(void) 00235 {error = 0;} 00236 00241 inline operator bool() const 00242 {return fd != INVALID_HANDLE_VALUE;} 00243 00248 inline bool operator!() const 00249 {return fd == INVALID_HANDLE_VALUE;} 00250 00255 void operator=(const fsys& descriptor); 00256 00262 void operator*=(fd_t& descriptor); 00263 00268 void operator=(fd_t descriptor); 00269 00274 inline fd_t handle(void) const 00275 {return fd;} 00276 00281 void set(fd_t descriptor); 00282 00287 fd_t release(void); 00288 00294 int seek(offset_t offset); 00295 00301 int drop(offset_t size = 0); 00302 00307 bool is_tty(void); 00308 00313 static bool is_tty(fd_t fd); 00314 00321 ssize_t read(void *buffer, size_t count); 00322 00329 ssize_t write(const void *buffer, size_t count); 00330 00336 int info(fileinfo_t *buffer); 00337 00344 int trunc(offset_t offset); 00345 00350 int sync(void); 00351 00357 static int prefix(const char *path); 00358 00365 static int prefix(char *path, size_t size); 00366 00367 static string_t prefix(void); 00368 00375 static int info(const char *path, fileinfo_t *buffer); 00376 00382 static int erase(const char *path); 00383 00391 static int copy(const char *source, const char *target, size_t size = 1024); 00392 00399 static int rename(const char *oldpath, const char *newpath); 00400 00407 static int mode(const char *path, unsigned value); 00408 00414 static bool is_exists(const char *path); 00415 00421 static bool is_readable(const char *path); 00422 00428 static bool is_writable(const char *path); 00429 00435 static bool is_executable(const char *path); 00436 00442 static bool is_file(const char *path); 00443 00449 static bool is_dir(const char *path); 00450 00456 static bool is_link(const char *path); 00457 00463 static bool is_device(const char *path); 00464 00470 static bool is_hidden(const char *path); 00471 00477 void open(const char *path, access_t access); 00478 00483 inline void assign(fd_t descriptor) 00484 {close(); fd = descriptor;} 00485 00491 inline static void assign(fsys& object, fd_t descriptor) 00492 {object.close(); object.fd = descriptor;} 00493 00500 void open(const char *path, unsigned mode, access_t access); 00501 00509 static int unlink(const char *path); 00510 00517 static int link(const char *path, const char *target); 00518 00525 static int hardlink(const char *path, const char *target); 00526 00533 static int linkinfo(const char *path, char *buffer, size_t size); 00534 00539 int close(void); 00540 00545 inline int err(void) const 00546 {return error;} 00547 00553 static fd_t input(const char *path); 00554 00560 static fd_t output(const char *path); 00561 00567 static fd_t append(const char *path); 00568 00573 static void release(fd_t descriptor); 00574 00582 static int pipe(fd_t& input, fd_t& output, size_t size = 0); 00583 00592 static int inherit(fd_t& descriptor, bool enable); 00593 00598 static fd_t null(void); 00599 00605 static int load(const char *path); 00606 00614 static int exec(const char *path, char **argv, char **envp = NULL); 00615 00616 static inline bool is_file(struct stat *inode) 00617 {return S_ISREG(inode->st_mode);} 00618 00619 static inline bool is_dir(struct stat *inode) 00620 {return S_ISDIR(inode->st_mode);} 00621 00622 static inline bool is_link(struct stat *inode) 00623 {return S_ISLNK(inode->st_mode);} 00624 00625 static inline bool is_dev(struct stat *inode) 00626 {return S_ISBLK(inode->st_mode) || S_ISCHR(inode->st_mode);} 00627 00628 static inline bool is_char(struct stat *inode) 00629 {return S_ISCHR(inode->st_mode);} 00630 00631 static inline bool is_disk(struct stat *inode) 00632 {return S_ISBLK(inode->st_mode);} 00633 00634 static inline bool is_sys(struct stat *inode) 00635 {return S_ISSOCK(inode->st_mode) || S_ISFIFO(inode->st_mode);} 00636 }; 00637 00642 class __EXPORT dso 00643 { 00644 private: 00645 friend class fsys; 00646 00647 #ifdef _MSWINDOWS_ 00648 HINSTANCE ptr; 00649 #else 00650 void *ptr; 00651 #endif 00652 int error; 00653 00654 public: 00655 #ifdef _MSWINDOWS_ 00656 typedef int (FAR WINAPI *addr_t)(); 00657 #else 00658 typedef void *addr_t; 00659 #endif 00660 00664 dso(); 00665 00670 dso(const char *path); 00671 00675 ~dso(); 00676 00681 void map(const char *path); 00682 00686 void release(void); 00687 00694 addr_t find(const char *symbol) const; 00695 00696 inline int err(void) const 00697 {return error;} 00698 00699 inline addr_t operator[](const char *symbol) const 00700 {return find(symbol);} 00701 00702 inline addr_t operator()(const char *symbol) const 00703 {return find(symbol);} 00704 00705 inline operator bool() 00706 {return ptr != NULL;} 00707 00708 inline bool operator!() 00709 {return ptr == NULL;} 00710 }; 00711 00716 class __EXPORT dir : private fsys 00717 { 00718 private: 00719 #ifdef _MSWINDOWS_ 00720 WIN32_FIND_DATA *ptr; 00721 HINSTANCE mem; 00722 #else 00723 void *ptr; 00724 #endif 00725 00726 public: 00731 dir(const char *path); 00732 00736 dir(); 00737 00741 ~dir(); 00742 00749 static int create(const char *path, unsigned mode); 00750 00756 static int remove(const char *path); 00757 00762 void open(const char *path); 00763 00770 ssize_t read(char *buffer, size_t count); 00771 00775 void close(void); 00776 00777 inline int err(void) const 00778 {return fsys::err();} 00779 00780 inline void reset(void) 00781 {fsys::reset();} 00782 00787 inline operator bool() const 00788 {return ptr != NULL;} 00789 00794 inline bool operator!() const 00795 {return ptr == NULL;} 00796 }; 00797 00801 typedef fsys fsys_t; 00802 00803 typedef dir dir_t; 00804 00805 typedef dso dso_t; 00806 00807 inline bool is_exists(const char *path) 00808 {return fsys::is_exists(path);} 00809 00810 inline bool is_readable(const char *path) 00811 {return fsys::is_readable(path);} 00812 00813 inline bool is_writable(const char *path) 00814 {return fsys::is_writable(path);} 00815 00816 inline bool is_executable(const char *path) 00817 {return fsys::is_executable(path);} 00818 00819 inline bool is_file(const char *path) 00820 {return fsys::is_file(path);} 00821 00822 inline bool is_dir(const char *path) 00823 {return fsys::is_dir(path);} 00824 00825 inline bool is_link(const char *path) 00826 {return fsys::is_link(path);} 00827 00828 inline bool is_device(const char *path) 00829 {return fsys::is_device(path);} 00830 00831 } // namespace ucommon 00832 00833 #endif 00834