UCommon
|
00001 // Copyright (C) 2006-2014 David Sugar, Tycho Softworks. 00002 // Copyright (C) 2015 Cherokees of Idaho. 00003 // 00004 // This file is part of GNU uCommon C++. 00005 // 00006 // GNU uCommon C++ is free software: you can redistribute it and/or modify 00007 // it under the terms of the GNU Lesser General Public License as published 00008 // by the Free Software Foundation, either version 3 of the License, or 00009 // (at your option) any later version. 00010 // 00011 // GNU uCommon C++ is distributed in the hope that it will be useful, 00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 // GNU Lesser General Public License for more details. 00015 // 00016 // You should have received a copy of the GNU Lesser General Public License 00017 // along with GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>. 00018 00028 #ifndef _UCOMMON_FSYS_H_ 00029 #define _UCOMMON_FSYS_H_ 00030 00031 #ifndef _UCOMMON_CONFIG_H_ 00032 #include <ucommon/platform.h> 00033 #endif 00034 00035 #ifndef _UCOMMON_PROTOCOLS_H_ 00036 #include <ucommon/protocols.h> 00037 #endif 00038 00039 #ifndef _UCOMMON_THREAD_H_ 00040 #include <ucommon/thread.h> 00041 #endif 00042 00043 #ifndef _UCOMMON_STRING_H_ 00044 #include <ucommon/string.h> 00045 #endif 00046 00047 #ifndef _UCOMMON_MEMORY_H_ 00048 #include <ucommon/memory.h> 00049 #endif 00050 00051 #ifndef _MSWINDOWS_ 00052 #include <sys/stat.h> 00053 #else 00054 #include <io.h> 00055 #ifndef R_OK 00056 #define F_OK 0 00057 #define X_OK 1 00058 #define W_OK 2 00059 #define R_OK 4 00060 #endif 00061 #endif 00062 00063 #include <errno.h> 00064 #include <stdio.h> 00065 00066 #ifndef __S_ISTYPE 00067 #define __S_ISTYPE(mode, mask) (((mode) & S_IFMT) == (mask)) 00068 #endif 00069 00070 #if !defined(S_ISDIR) && defined(S_IFDIR) 00071 #define S_ISDIR(mode) __S_ISTYPE((mode), S_IFDIR) 00072 #endif 00073 00074 #if !defined(S_ISCHR) && defined(S_IFCHR) 00075 #define S_ISCHR(mode) __S_ISTYPE((mode), S_IFCHR) 00076 #elif !defined(S_ISCHR) 00077 #define S_ISCHR(mode) 0 00078 #endif 00079 00080 #if !defined(S_ISBLK) && defined(S_IFBLK) 00081 #define S_ISBLK(mode) __S_ISTYPE((mode), S_IFBLK) 00082 #elif !defined(S_ISBLK) 00083 #define S_ISBLK(mode) 0 00084 #endif 00085 00086 #if !defined(S_ISREG) && defined(S_IFREG) 00087 #define S_ISREG(mode) __S_ISTYPE((mode), S_IFREG) 00088 #elif !defined(S_ISREG) 00089 #define S_ISREG(mode) 1 00090 #endif 00091 00092 #if !defined(S_ISSOCK) && defined(S_IFSOCK) 00093 #define S_ISSOCK(mode) __S_ISTYPE((mode), S_IFSOCK) 00094 #elif !defined(S_ISSOCK) 00095 #define S_ISSOCK(mode) (0) 00096 #endif 00097 00098 #if !defined(S_ISFIFO) && defined(S_IFIFO) 00099 #define S_ISFIFO(mode) __S_ISTYPE((mode), S_IFIFO) 00100 #elif !defined(S_ISFIFO) 00101 #define S_ISFIFO(mode) (0) 00102 #endif 00103 00104 #if !defined(S_ISLNK) && defined(S_IFLNK) 00105 #define S_ISLNK(mode) __S_ISTYPE((mode), S_IFLNK) 00106 #elif !defined(S_ISLNK) 00107 #define S_ISLNK(mode) (0) 00108 #endif 00109 00110 namespace ucommon { 00111 00115 typedef void *mem_t; 00116 00125 class __EXPORT fsys 00126 { 00127 protected: 00128 fd_t fd; 00129 mutable int error; 00130 00131 public: 00135 enum { 00136 OWNER_READONLY = 0400, 00137 GROUP_READONLY = 0440, 00138 PUBLIC_READONLY = 0444, 00139 OWNER_PRIVATE = 0600, 00140 OWNER_PUBLIC = 0644, 00141 GROUP_PRIVATE = 0660, 00142 GROUP_PUBLIC = 0664, 00143 EVERYONE = 0666, 00144 DIR_TEMPORARY = 01777 00145 }; 00146 00147 typedef struct stat fileinfo_t; 00148 00149 #ifdef _MSWINDOWS_ 00150 static int remapError(void); 00151 #else 00152 inline static int remapError(void) 00153 {return errno;} 00154 #endif 00155 00159 typedef enum { 00160 RDONLY, 00161 WRONLY, 00162 REWRITE, 00163 RDWR = REWRITE, 00164 APPEND, 00165 SHARED, 00166 EXCLUSIVE, 00167 DEVICE, 00168 STREAM, 00169 RANDOM 00170 } access_t; 00171 00175 typedef long offset_t; 00176 00180 static const offset_t end; 00181 00185 fsys(); 00186 00190 fsys(fd_t handle); 00191 00196 fsys(const fsys& descriptor); 00197 00203 fsys(const char *path, access_t access); 00204 00211 fsys(const char *path, unsigned permission, access_t access); 00212 00216 ~fsys(); 00217 00222 inline fd_t operator*() const 00223 {return fd;} 00224 00229 inline operator fd_t() const 00230 {return fd;} 00231 00235 inline void reset(void) 00236 {error = 0;} 00237 00242 inline operator bool() const 00243 {return fd != INVALID_HANDLE_VALUE;} 00244 00249 inline bool operator!() const 00250 {return fd == INVALID_HANDLE_VALUE;} 00251 00256 void operator=(const fsys& descriptor); 00257 00263 void operator*=(fd_t& descriptor); 00264 00269 void operator=(fd_t descriptor); 00270 00275 inline fd_t handle(void) const 00276 {return fd;} 00277 00282 void set(fd_t descriptor); 00283 00288 fd_t release(void); 00289 00295 int seek(offset_t offset); 00296 00302 int drop(offset_t size = 0); 00303 00308 bool is_tty(void) const; 00309 00314 static bool is_tty(fd_t fd); 00315 00322 ssize_t read(void *buffer, size_t count); 00323 00330 ssize_t write(const void *buffer, size_t count); 00331 00337 int info(fileinfo_t *buffer); 00338 00345 int trunc(offset_t offset); 00346 00351 int sync(void); 00352 00358 static int prefix(const char *path); 00359 00366 static int prefix(char *path, size_t size); 00367 00368 static string_t prefix(void); 00369 00376 static int info(const char *path, fileinfo_t *buffer); 00377 00383 static int erase(const char *path); 00384 00392 static int copy(const char *source, const char *target, size_t size = 1024); 00393 00400 static int rename(const char *oldpath, const char *newpath); 00401 00408 static int mode(const char *path, unsigned value); 00409 00415 static bool is_exists(const char *path); 00416 00422 static bool is_readable(const char *path); 00423 00429 static bool is_writable(const char *path); 00430 00436 static bool is_executable(const char *path); 00437 00443 static bool is_file(const char *path); 00444 00450 static bool is_dir(const char *path); 00451 00457 static bool is_link(const char *path); 00458 00464 static bool is_device(const char *path); 00465 00471 static bool is_hidden(const char *path); 00472 00478 void open(const char *path, access_t access); 00479 00484 inline void assign(fd_t descriptor) 00485 {close(); fd = descriptor;} 00486 00492 inline static void assign(fsys& object, fd_t descriptor) 00493 {object.close(); object.fd = descriptor;} 00494 00501 void open(const char *path, unsigned mode, access_t access); 00502 00510 static int unlink(const char *path); 00511 00518 static int link(const char *path, const char *target); 00519 00526 static int hardlink(const char *path, const char *target); 00527 00534 static int linkinfo(const char *path, char *buffer, size_t size); 00535 00540 int close(void); 00541 00546 inline int err(void) const 00547 {return error;} 00548 00554 static fd_t input(const char *path); 00555 00561 static fd_t output(const char *path); 00562 00568 static fd_t append(const char *path); 00569 00574 static void release(fd_t descriptor); 00575 00583 static int pipe(fd_t& input, fd_t& output, size_t size = 0); 00584 00593 static int inherit(fd_t& descriptor, bool enable); 00594 00599 static fd_t null(void); 00600 00606 static int load(const char *path); 00607 00615 static int exec(const char *path, char **argv, char **envp = NULL); 00616 00617 static inline bool is_file(struct stat *inode) 00618 {return S_ISREG(inode->st_mode);} 00619 00620 static inline bool is_dir(struct stat *inode) 00621 {return S_ISDIR(inode->st_mode);} 00622 00623 static inline bool is_link(struct stat *inode) 00624 {return S_ISLNK(inode->st_mode);} 00625 00626 static inline bool is_dev(struct stat *inode) 00627 {return S_ISBLK(inode->st_mode) || S_ISCHR(inode->st_mode);} 00628 00629 static inline bool is_char(struct stat *inode) 00630 {return S_ISCHR(inode->st_mode);} 00631 00632 static inline bool is_disk(struct stat *inode) 00633 {return S_ISBLK(inode->st_mode);} 00634 00635 static inline bool is_sys(struct stat *inode) 00636 {return S_ISSOCK(inode->st_mode) || S_ISFIFO(inode->st_mode);} 00637 }; 00638 00643 class __EXPORT dso 00644 { 00645 private: 00646 friend class fsys; 00647 00648 #ifdef _MSWINDOWS_ 00649 HINSTANCE ptr; 00650 #else 00651 void *ptr; 00652 #endif 00653 int error; 00654 00655 public: 00656 #ifdef _MSWINDOWS_ 00657 typedef int (FAR WINAPI *addr_t)(); 00658 #else 00659 typedef void *addr_t; 00660 #endif 00661 00665 dso(); 00666 00671 dso(const char *path); 00672 00676 ~dso(); 00677 00682 void map(const char *path); 00683 00687 void release(void); 00688 00695 addr_t find(const char *symbol) const; 00696 00697 inline int err(void) const 00698 {return error;} 00699 00700 inline addr_t operator[](const char *symbol) const 00701 {return find(symbol);} 00702 00703 inline addr_t operator()(const char *symbol) const 00704 {return find(symbol);} 00705 00706 inline operator bool() const 00707 {return ptr != NULL;} 00708 00709 inline bool operator!() const 00710 {return ptr == NULL;} 00711 }; 00712 00717 class __EXPORT dir : private fsys 00718 { 00719 private: 00720 #ifdef _MSWINDOWS_ 00721 WIN32_FIND_DATA *ptr; 00722 HINSTANCE mem; 00723 #else 00724 void *ptr; 00725 #endif 00726 00727 public: 00732 dir(const char *path); 00733 00737 dir(); 00738 00742 ~dir(); 00743 00750 static int create(const char *path, unsigned mode); 00751 00757 static int remove(const char *path); 00758 00763 void open(const char *path); 00764 00771 ssize_t read(char *buffer, size_t count); 00772 00776 void close(void); 00777 00778 inline int err(void) const 00779 {return fsys::err();} 00780 00781 inline void reset(void) 00782 {fsys::reset();} 00783 00788 inline operator bool() const 00789 {return ptr != NULL;} 00790 00795 inline bool operator!() const 00796 {return ptr == NULL;} 00797 }; 00798 00802 typedef fsys fsys_t; 00803 00804 typedef dir dir_t; 00805 00806 typedef dso dso_t; 00807 00808 inline bool is_exists(const char *path) 00809 {return fsys::is_exists(path);} 00810 00811 inline bool is_readable(const char *path) 00812 {return fsys::is_readable(path);} 00813 00814 inline bool is_writable(const char *path) 00815 {return fsys::is_writable(path);} 00816 00817 inline bool is_executable(const char *path) 00818 {return fsys::is_executable(path);} 00819 00820 inline bool is_file(const char *path) 00821 {return fsys::is_file(path);} 00822 00823 inline bool is_dir(const char *path) 00824 {return fsys::is_dir(path);} 00825 00826 inline bool is_link(const char *path) 00827 {return fsys::is_link(path);} 00828 00829 inline bool is_device(const char *path) 00830 {return fsys::is_device(path);} 00831 00832 } // namespace ucommon 00833 00834 #endif 00835