00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00043 #ifndef CCXX_FILE_H_
00044 #define CCXX_FILE_H_
00045
00046 #ifndef CCXX_CONFIG_H_
00047 #include <cc++/config.h>
00048 #endif
00049
00050 #ifndef CCXX_MISSING_H_
00051 #include <cc++/missing.h>
00052 #endif
00053
00054 #ifndef CCXX_THREAD_H_
00055 #include <cc++/thread.h>
00056 #endif
00057
00058 #ifndef CCXX_EXCEPTION_H_
00059 #include <cc++/exception.h>
00060 #endif
00061
00062 #ifndef WIN32
00063 # ifdef __BORLANDC__
00064 # include <stdio.h>
00065 # include <sys/types.h>
00066 # else
00067 # include <cstdio>
00068 # endif
00069 # include <dirent.h>
00070 # include <sys/stat.h>
00071 # include <sys/mman.h>
00072 #else
00073 # if __BORLANDC__ >= 0x0560
00074 # include <dirent.h>
00075 # include <sys/stat.h>
00076 # else
00077 # include <direct.h>
00078 # endif
00079 #endif
00080
00081 #ifdef HAVE_SHL_LOAD
00082 #include <dl.h>
00083 #endif
00084
00085 #ifdef HAVE_MACH_DYLD
00086 #include <mach-o/dyld.h>
00087 #endif
00088
00089 #ifdef CCXX_NAMESPACES
00090 namespace ost {
00091 #endif
00092
00093 typedef unsigned long pos_t;
00094 #ifndef WIN32
00095
00096
00097 #undef caddr_t
00098 #define caddr_t char *
00099 typedef size_t ccxx_size_t;
00100 #else
00101 #if !defined(__BORLANDC__) || __BORLANDC__ >= 0x0560
00102 typedef LONG off_t;
00103 #endif
00104 typedef void* caddr_t;
00105 typedef DWORD ccxx_size_t;
00106 #endif
00107
00108 #ifndef PATH_MAX
00109 #define PATH_MAX 256
00110 #endif
00111
00112 #ifndef NAME_MAX
00113 #define NAME_MAX 64
00114 #endif
00115
00116 class __EXPORT File
00117 {
00118 public:
00119 enum Error
00120 {
00121 errSuccess = 0,
00122 errNotOpened,
00123 errMapFailed,
00124 errInitFailed,
00125 errOpenDenied,
00126 errOpenFailed,
00127 errOpenInUse,
00128 errReadInterrupted,
00129 errReadIncomplete,
00130 errReadFailure,
00131 errWriteInterrupted,
00132 errWriteIncomplete,
00133 errWriteFailure,
00134 errExtended
00135 };
00136 typedef enum Error Error;
00137
00138 enum Access
00139 {
00140 #ifndef WIN32
00141 accessReadOnly = O_RDONLY,
00142 accessWriteOnly= O_WRONLY,
00143 accessReadWrite = O_RDWR
00144 #else
00145 accessReadOnly = GENERIC_READ,
00146 accessWriteOnly = GENERIC_WRITE,
00147 accessReadWrite = GENERIC_READ | GENERIC_WRITE
00148 #endif
00149 };
00150 typedef enum Access Access;
00151
00152 protected:
00153 typedef struct _fcb
00154 {
00155 struct _fcb *next;
00156 caddr_t address;
00157 ccxx_size_t len;
00158 off_t pos;
00159 bool locked;
00160 } fcb_t;
00161
00162 public:
00163 #ifdef WIN32
00164 enum Open
00165 {
00166 openReadOnly,
00167 openWriteOnly,
00168 openReadWrite,
00169 openAppend,
00170 openTruncate
00171 };
00172 #else
00173 enum Open
00174 {
00175 openReadOnly = O_RDONLY,
00176 openWriteOnly = O_WRONLY,
00177 openReadWrite = O_RDWR,
00178 openAppend = O_WRONLY | O_APPEND,
00179 #ifdef O_SYNC
00180 openSync = O_RDWR | O_SYNC,
00181 #else
00182 openSync = O_RDWR,
00183 #endif
00184 openTruncate = O_RDWR | O_TRUNC
00185 };
00186 typedef enum Open Open;
00187
00188
00189
00190 #ifndef S_IRUSR
00191 #define S_IRUSR 0400
00192 #define S_IWUSR 0200
00193 #define S_IRGRP 0040
00194 #define S_IWGRP 0020
00195 #define S_IROTH 0004
00196 #define S_IWOTH 0002
00197 #endif
00198
00199 #endif // !WIN32
00200
00201 #ifndef WIN32
00202 enum Attr
00203 {
00204 attrInvalid = 0,
00205 attrPrivate = S_IRUSR | S_IWUSR,
00206 attrGroup = attrPrivate | S_IRGRP | S_IWGRP,
00207 attrPublic = attrGroup | S_IROTH | S_IWOTH
00208 };
00209 #else // defined WIN32
00210 enum Attr {
00211 attrInvalid=0,
00212 attrPrivate,
00213 attrGroup,
00214 attrPublic
00215 };
00216 #endif // !WIN32
00217 typedef enum Attr Attr;
00218
00219 #ifdef WIN32
00220 enum Complete
00221 {
00222 completionImmediate,
00223 completionDelayed,
00224 completionDeferred
00225 };
00226
00227 enum Mapping
00228 {
00229 mappedRead,
00230 mappedWrite,
00231 mappedReadWrite
00232 };
00233 #else
00234 enum Mapping
00235 {
00236 mappedRead = accessReadOnly,
00237 mappedWrite = accessWriteOnly,
00238 mappedReadWrite = accessReadWrite
00239 };
00240 enum Complete
00241 {
00242 completionImmediate,
00243 completionDelayed,
00244 completionDeferred
00245 };
00246 #endif
00247 typedef enum Complete Complete;
00248 typedef enum Mapping Mapping;
00249
00250 public:
00251 static const char *getExtension(const char *path);
00252 static const char *getFilename(const char *path);
00253 static char *getFilename(const char *path, char *buffer, size_t size = NAME_MAX);
00254 static char *getDirname(const char *path, char *buffer, size_t size = PATH_MAX);
00255 static char *getRealpath(const char *path, char *buffer, size_t size = PATH_MAX);
00256 };
00257
00266 class __EXPORT Dir : public File
00267 {
00268 private:
00269 #ifndef WIN32
00270 DIR *dir;
00271 #ifdef HAVE_READDIR_R
00272 struct dirent *save;
00273 char save_space[sizeof(struct dirent) + PATH_MAX + 1];
00274 #endif
00275 struct dirent *entry;
00276 #else
00277 HANDLE hDir;
00278 WIN32_FIND_DATA data, fdata;
00279 char *name;
00280 #endif
00281
00282 public:
00283 Dir(const char *name = NULL);
00284
00285 static bool create(const char *path, Attr attr = attrGroup);
00286 static bool remove(const char *path);
00287 static bool setPrefix(const char *path);
00288 static bool getPrefix(char *path, size_t size = PATH_MAX);
00289
00290 void open(const char *name);
00291 void close(void);
00292
00293 virtual ~Dir();
00294
00295 const char *getName(void);
00296
00297 const char *operator++()
00298 {return getName();};
00299
00300 const char *operator++(int)
00301 {return getName();};
00302
00303 const char *operator*();
00304
00305 bool rewind(void);
00306
00307 bool operator!()
00308 #ifndef WIN32
00309 {return !dir;};
00310 #else
00311 {return hDir != INVALID_HANDLE_VALUE;};
00312 #endif
00313
00314 bool isValid(void);
00315 };
00316
00323 class __EXPORT DirTree
00324 {
00325 private:
00326 char path[PATH_MAX + 1];
00327 Dir *dir;
00328 unsigned max, current, prefixpos;
00329
00330 protected:
00340 virtual bool filter(const char *file, struct stat *ino);
00341
00342 public:
00350 DirTree(const char *prefix, unsigned maxdepth);
00351
00357 DirTree(unsigned maxdepth);
00358
00359 virtual ~DirTree();
00360
00366 void open(const char *prefix);
00367
00371 void close(void);
00372
00380 char *getPath(void);
00381
00391 unsigned perform(const char *prefix);
00392 };
00393
00404 class __EXPORT RandomFile : protected Mutex, public File
00405 {
00406 private:
00407 Error errid;
00408 char *errstr;
00409
00410 protected:
00411 #ifndef WIN32
00412 int fd;
00413
00414 Access access;
00415 #else
00416 HANDLE fd;
00417 #endif
00418 char *pathname;
00419
00420 struct
00421 {
00422 unsigned count : 16;
00423 bool thrown : 1;
00424 bool initial : 1;
00425 #ifndef WIN32
00426 bool immediate : 1;
00427 #endif
00428 bool temp : 1;
00429 } flags;
00430
00434 RandomFile(const char *name = NULL);
00435
00439 RandomFile(const RandomFile &rf);
00440
00448 Error error(Error errid, char *errstr = NULL);
00449
00456 inline Error error(char *err)
00457 {return error(errExtended, err);};
00458
00465 inline void setError(bool enable)
00466 {flags.thrown = !enable;};
00467
00468 #ifndef WIN32
00469
00476 Error setCompletion(Complete mode);
00477 #endif
00478
00485 inline void setTemporary(bool enable)
00486 {flags.temp = enable;};
00487
00499 virtual Attr initialize(void);
00500
00504 void final(void);
00505
00506 public:
00510 virtual ~RandomFile();
00511
00520 bool initial(void);
00521
00527 off_t getCapacity(void);
00528
00534 virtual Error restart(void);
00535
00541 inline Error getErrorNumber(void)
00542 {return errid;};
00543
00549 inline char *getErrorString(void)
00550 {return errstr;};
00551
00552 bool operator!(void);
00553 };
00554
00574 class __EXPORT ThreadFile : public RandomFile
00575 {
00576 private:
00577 ThreadKey state;
00578 fcb_t *first;
00579 fcb_t *getFCB(void);
00580 Error open(const char *path);
00581
00582 public:
00589 ThreadFile(const char *path);
00590
00594 virtual ~ThreadFile();
00595
00601 Error restart(void);
00602
00612 Error fetch(caddr_t address = NULL, ccxx_size_t length = 0, off_t position = -1);
00613
00623 Error update(caddr_t address = NULL, ccxx_size_t length = 0, off_t position = -1);
00624
00630 Error append(caddr_t address = NULL, ccxx_size_t length = 0);
00631
00637 off_t getPosition(void);
00638
00639 bool operator++(void);
00640 bool operator--(void);
00641 };
00642
00657 class __EXPORT SharedFile : public RandomFile
00658 {
00659 private:
00660 fcb_t fcb;
00661 Error open(const char *path);
00662
00663 public:
00670 SharedFile(const char *path);
00671
00678 SharedFile(const SharedFile &file);
00679
00683 virtual ~SharedFile();
00684
00690 Error restart(void)
00691 {return open(pathname);};
00692
00703 Error fetch(caddr_t address = NULL, ccxx_size_t length = 0, off_t position = -1);
00704
00715 Error update(caddr_t address = NULL, ccxx_size_t length = 0, off_t position = -1);
00716
00725 Error clear(ccxx_size_t length = 0, off_t pos = -1);
00726
00733 Error append(caddr_t address = NULL, ccxx_size_t length = 0);
00734
00740 off_t getPosition(void);
00741
00742 bool operator++(void);
00743 bool operator--(void);
00744 };
00745
00756 class __EXPORT MappedFile : public RandomFile
00757 {
00758 private:
00759 fcb_t fcb;
00760 int prot;
00761 #ifdef WIN32
00762 HANDLE map;
00763 char mapname[64];
00764 #endif
00765
00766 public:
00774 MappedFile(const char *fname, Access mode);
00775
00784 MappedFile(const char *fname, Access mode, size_t size);
00785
00796 MappedFile(const char *fname, pos_t offset, size_t size, Access mode);
00797
00802 virtual ~MappedFile();
00803
00804
00810 void sync(void);
00811
00818 void sync(caddr_t address, size_t len);
00819
00828 void update(size_t offset = 0, size_t len = 0);
00829
00837 void update(caddr_t address, size_t len);
00838
00845 void release(caddr_t address, size_t len);
00846
00855 inline caddr_t fetch(size_t offset = 0)
00856 {return ((char *)(fcb.address)) + offset;};
00857
00866 caddr_t fetch(off_t pos, size_t len);
00867
00873 bool lock(void);
00874
00878 void unlock(void);
00879
00886 size_t pageAligned(size_t size);
00887 };
00888
00889
00898 class __EXPORT DSO
00899 {
00900 private:
00901 const char *err;
00902 #ifdef HAVE_MODULES
00903 static Mutex mutex;
00904 static DSO *first;
00905 static DSO *last;
00906 DSO *next, *prev;
00907 const char *id;
00908 #if defined(HAVE_MACH_DYLD)
00909 NSModule oModule;
00910 #elif defined(HAVE_SHL_LOAD)
00911 shl_t image;
00912 #elif defined(WIN32)
00913 HINSTANCE hImage;
00914 #else
00915 void *image;
00916 #endif
00917 void loader(const char *filename, bool resolve);
00918 #endif
00919
00920 public:
00926 #ifdef HAVE_MODULES
00927 DSO(const char *filename)
00928 {loader(filename, true);};
00929
00930 DSO(const char *filename, bool resolve)
00931 {loader(filename, resolve);};
00932 #else
00933 DSO(const char *filename)
00934 {throw this;};
00935 DSO(const char *filename, bool resolve)
00936 {throw this;};
00937 #endif
00938
00943 inline const char *getError(void)
00944 {return err;};
00945
00949 #ifdef HAVE_MODULES
00950 virtual ~DSO();
00951 #endif
00952
00956 #ifdef HAVE_MODULES
00957 void* operator[](const char *sym);
00958 #else
00959 void *operator[](const char *)
00960 {return NULL;};
00961 #endif
00962
00963 #ifdef HAVE_MODULES
00964 static void dynunload(void);
00965 #else
00966 static void dynunload(void)
00967 {return;};
00968 #endif
00969
00975 static DSO *getObject(const char *name);
00976
00982 bool isValid(void);
00983
00987 static void setDebug(void);
00988 };
00989
00991 bool __EXPORT isDir(const char *path);
00993 bool __EXPORT isFile(const char *path);
00994 #ifndef WIN32
00995
00996 bool __EXPORT isDevice(const char *path);
00997 #else
00998
00999 inline bool isDevice(const char *path)
01000 { return false; }
01001 #endif
01002
01003 bool __EXPORT canAccess(const char *path);
01005 bool __EXPORT canModify(const char *path);
01007 time_t __EXPORT lastModified(const char *path);
01009 time_t __EXPORT lastAccessed(const char *path);
01010
01011 #ifdef COMMON_STD_EXCEPTION
01012
01013 class DirException : public IOException
01014 {
01015 public:
01016 DirException(const String &str) : IOException(str) {};
01017 };
01018
01019 class __EXPORT DSOException : public IOException
01020 {
01021 public:
01022 DSOException(const String &str) : IOException(str) {};
01023 };
01024
01025 class __EXPORT FileException : public IOException
01026 {
01027 public:
01028 FileException(const String &str) : IOException(str) {};
01029 };
01030
01031 #endif
01032
01033 #ifdef CCXX_NAMESPACES
01034 }
01035 #endif
01036
01037 #endif
01038