GNU CommonC++
file.h
Go to the documentation of this file.
1 // Copyright (C) 1999-2005 Open Source Telecom Corporation.
2 // Copyright (C) 2006-2010 David Sugar, Tycho Softworks.
3 //
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 2 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 //
18 // As a special exception, you may use this file as part of a free software
19 // library without restriction. Specifically, if other files instantiate
20 // templates or use macros or inline functions from this file, or you compile
21 // this file and link it with other files to produce an executable, this
22 // file does not by itself cause the resulting executable to be covered by
23 // the GNU General Public License. This exception does not however
24 // invalidate any other reasons why the executable file might be covered by
25 // the GNU General Public License.
26 //
27 // This exception applies only to the code released under the name GNU
28 // Common C++. If you copy code from other releases into a copy of GNU
29 // Common C++, as the General Public License permits, the exception does
30 // not apply to the code that you add in this way. To avoid misleading
31 // anyone as to the status of such modified files, you must delete
32 // this exception notice from them.
33 //
34 // If you write modifications of your own for GNU Common C++, it is your choice
35 // whether to permit this exception to apply to your modifications.
36 // If you do not wish that, delete this exception notice.
37 //
38 
44 #ifndef CCXX_FILE_H_
45 #define CCXX_FILE_H_
46 
47 #ifndef CCXX_CONFIG_H_
48 #include <cc++/config.h>
49 #endif
50 
51 #ifndef CCXX_MISSING_H_
52 #include <cc++/missing.h>
53 #endif
54 
55 #ifndef CCXX_THREAD_H_
56 #include <cc++/thread.h>
57 #endif
58 
59 #ifndef CCXX_EXCEPTION_H_
60 #include <cc++/exception.h>
61 #endif
62 
63 #ifndef WIN32
64 # ifdef __BORLANDC__
65 # include <stdio.h>
66 # include <sys/types.h>
67 # else
68 # include <cstdio>
69 # endif
70 # include <dirent.h>
71 # include <sys/stat.h>
72 # include <sys/mman.h>
73 #else
74 # if __BORLANDC__ >= 0x0560
75 # include <dirent.h>
76 # include <sys/stat.h>
77 # else
78 # include <direct.h>
79 # endif
80 #endif
81 
82 #ifdef HAVE_SHL_LOAD
83 #include <dl.h>
84 #endif
85 
86 #ifdef HAVE_MACH_DYLD
87 #include <mach-o/dyld.h>
88 #endif
89 
90 #ifdef CCXX_NAMESPACES
91 namespace ost {
92 #endif
93 
94 typedef unsigned long pos_t;
95 #ifndef WIN32
96 #include <sys/types.h>
97 typedef size_t ccxx_size_t;
98 #else
99 #if !defined(__BORLANDC__) || __BORLANDC__ >= 0x0560
100 typedef LONG off_t;
101 #endif
102 typedef void* caddr_t;
103 typedef DWORD ccxx_size_t;
104 #endif
105 
106 #ifndef PATH_MAX
107 #define PATH_MAX 256
108 #endif
109 
110 #ifndef NAME_MAX
111 #define NAME_MAX 64
112 #endif
113 
115 {
116 public:
117  enum Error {
118  errSuccess = 0,
132  errExtended
133  };
134  typedef enum Error Error;
135 
136  enum Access {
137 #ifndef WIN32
138  accessReadOnly = O_RDONLY,
139  accessWriteOnly= O_WRONLY,
140  accessReadWrite = O_RDWR
141 #else
142  accessReadOnly = GENERIC_READ,
143  accessWriteOnly = GENERIC_WRITE,
144  accessReadWrite = GENERIC_READ | GENERIC_WRITE
145 #endif
146  };
147  typedef enum Access Access;
148 
149 protected:
150  typedef struct _fcb {
151  struct _fcb *next;
152  caddr_t address;
153  ccxx_size_t len;
154  off_t pos;
155  bool locked;
156  } fcb_t;
157 
158 public:
159 #ifdef WIN32
160  enum Open {
161  openReadOnly, // = FILE_OPEN_READONLY,
162  openWriteOnly, // = FILE_OPEN_WRITEONLY,
163  openReadWrite, // = FILE_OPEN_READWRITE,
164  openAppend, // = FILE_OPEN_APPEND,
165  openTruncate // = FILE_OPEN_TRUNCATE
166  };
167  #else
168  enum Open {
169  openReadOnly = O_RDONLY,
170  openWriteOnly = O_WRONLY,
171  openReadWrite = O_RDWR,
172  openAppend = O_WRONLY | O_APPEND,
173 #ifdef O_SYNC
174  openSync = O_RDWR | O_SYNC,
175 #else
176  openSync = O_RDWR,
177 #endif
178  openTruncate = O_RDWR | O_TRUNC
179  };
180  typedef enum Open Open;
181 
182 /* to be used in future */
183 
184 #ifndef S_IRUSR
185 #define S_IRUSR 0400
186 #define S_IWUSR 0200
187 #define S_IRGRP 0040
188 #define S_IWGRP 0020
189 #define S_IROTH 0004
190 #define S_IWOTH 0002
191 #endif
192 
193 #endif // !WIN32
194 
195 #ifndef WIN32
196  enum Attr {
197  attrInvalid = 0,
198  attrPrivate = S_IRUSR | S_IWUSR,
199  attrGroup = attrPrivate | S_IRGRP | S_IWGRP,
200  attrPublic = attrGroup | S_IROTH | S_IWOTH
201  };
202  #else // defined WIN32
203  enum Attr {
204  attrInvalid=0,
205  attrPrivate,
206  attrGroup,
207  attrPublic
208  };
209 #endif // !WIN32
210  typedef enum Attr Attr;
211 
212 #ifdef WIN32
213  enum Complete {
214  completionImmediate, // = FILE_COMPLETION_IMMEDIATE,
215  completionDelayed, // = FILE_COMPLETION_DELAYED,
216  completionDeferred // = FILE_COMPLETION_DEFERRED
217  };
218 
219  enum Mapping {
220  mappedRead,
221  mappedWrite,
222  mappedReadWrite
223  };
224 #else
225  enum Mapping {
226  mappedRead = accessReadOnly,
227  mappedWrite = accessWriteOnly,
228  mappedReadWrite = accessReadWrite
229  };
230  enum Complete {
233  completionDeferred
234  };
235 #endif
236  typedef enum Complete Complete;
237  typedef enum Mapping Mapping;
238 
239 public:
240  static const char *getExtension(const char *path);
241  static const char *getFilename(const char *path);
242  static char *getFilename(const char *path, char *buffer, size_t size = NAME_MAX);
243  static char *getDirname(const char *path, char *buffer, size_t size = PATH_MAX);
244  static char *getRealpath(const char *path, char *buffer, size_t size = PATH_MAX);
245 };
246 
255 class __EXPORT Dir : public File
256 {
257 private:
258 #ifndef WIN32
259  DIR *dir;
260 #ifdef HAVE_READDIR_R
261  struct dirent *save;
262  char save_space[sizeof(struct dirent) + PATH_MAX + 1];
263 #endif
264  struct dirent *entry;
265 #else
266  HANDLE hDir;
267  WIN32_FIND_DATA data, fdata;
268  char *name;
269 #endif
270 
271 public:
272  Dir(const char *name = NULL);
273 
274  static bool create(const char *path, Attr attr = attrGroup);
275  static bool remove(const char *path);
276  static bool setPrefix(const char *path);
277  static bool getPrefix(char *path, size_t size = PATH_MAX);
278 
279  void open(const char *name);
280  void close(void);
281 
282  virtual ~Dir();
283 
284  const char *getName(void);
285 
286  const char *operator++()
287  {return getName();};
288 
289  const char *operator++(int)
290  {return getName();};
291 
292  const char *operator*();
293 
294  bool rewind(void);
295 
296  bool operator!()
297 #ifndef WIN32
298  {return !dir;};
299 #else
300  {return hDir != INVALID_HANDLE_VALUE;};
301 #endif
302 
303  bool isValid(void);
304 };
305 
313 {
314 private:
315  char path[PATH_MAX + 1];
316  Dir *dir;
317  unsigned max, current, prefixpos;
318 
319 protected:
329  virtual bool filter(const char *file, struct stat *ino);
330 
331 public:
339  DirTree(const char *prefix, unsigned maxdepth);
340 
346  DirTree(unsigned maxdepth);
347 
348  virtual ~DirTree();
349 
355  void open(const char *prefix);
356 
360  void close(void);
361 
369  char *getPath(void);
370 
380  unsigned perform(const char *prefix);
381 };
382 
393 class __EXPORT RandomFile : protected Mutex, public File
394 {
395 private:
396  Error errid;
397  char *errstr;
398 
399 protected:
400 #ifndef WIN32
401  int fd;
402  // FIXME: WIN32 as no access member
404 #else
405  HANDLE fd;
406 #endif
407  char *pathname;
408 
409  struct {
410  unsigned count : 16;
411  bool thrown : 1;
412  bool initial : 1;
413 #ifndef WIN32
414  bool immediate : 1;
415 #endif
416  bool temp : 1;
417  } flags;
418 
422  RandomFile(const char *name = NULL);
423 
427  RandomFile(const RandomFile &rf);
428 
436  Error error(Error errid, char *errstr = NULL);
437 
444  inline Error error(char *err)
445  {return error(errExtended, err);};
446 
453  inline void setError(bool enable)
454  {flags.thrown = !enable;};
455 
456 #ifndef WIN32
457 
464  Error setCompletion(Complete mode);
465 #endif
466 
473  inline void setTemporary(bool enable)
474  {flags.temp = enable;};
475 
487  virtual Attr initialize(void);
488 
492  void final(void);
493 
494 public:
498  virtual ~RandomFile();
499 
508  bool initial(void);
509 
515  off_t getCapacity(void);
516 
522  virtual Error restart(void);
523 
529  inline Error getErrorNumber(void)
530  {return errid;};
531 
537  inline char *getErrorString(void)
538  {return errstr;};
539 
540  bool operator!(void);
541 };
542 
563 {
564 private:
565  ThreadKey state;
566  fcb_t *first;
567  fcb_t *getFCB(void);
568  Error open(const char *path);
569 
570 public:
577  ThreadFile(const char *path);
578 
582  virtual ~ThreadFile();
583 
589  Error restart(void);
590 
600  Error fetch(caddr_t address = NULL, ccxx_size_t length = 0, off_t position = -1);
601 
611  Error update(caddr_t address = NULL, ccxx_size_t length = 0, off_t position = -1);
612 
618  Error append(caddr_t address = NULL, ccxx_size_t length = 0);
619 
625  off_t getPosition(void);
626 
627  bool operator++(void);
628  bool operator--(void);
629 };
630 
646 {
647 private:
648  fcb_t fcb;
649  Error open(const char *path);
650 
651 public:
658  SharedFile(const char *path);
659 
666  SharedFile(const SharedFile &file);
667 
671  virtual ~SharedFile();
672 
679  {return open(pathname);};
680 
691  Error fetch(caddr_t address = NULL, ccxx_size_t length = 0, off_t position = -1);
692 
703  Error update(caddr_t address = NULL, ccxx_size_t length = 0, off_t position = -1);
704 
713  Error clear(ccxx_size_t length = 0, off_t pos = -1);
714 
721  Error append(caddr_t address = NULL, ccxx_size_t length = 0);
722 
728  off_t getPosition(void);
729 
730  bool operator++(void);
731  bool operator--(void);
732 };
733 
745 {
746 private:
747  fcb_t fcb;
748  int prot;
749 #ifdef WIN32
750  HANDLE map;
751  char mapname[64];
752 #endif
753 
754 public:
762  MappedFile(const char *fname, Access mode);
763 
772  MappedFile(const char *fname, Access mode, size_t size);
773 
784  MappedFile(const char *fname, pos_t offset, size_t size, Access mode);
785 
790  virtual ~MappedFile();
791 
792  // FIXME: not use library function in header ??
798  void sync(void);
799 
806  void sync(caddr_t address, size_t len);
807 
816  void update(size_t offset = 0, size_t len = 0);
817 
825  void update(caddr_t address, size_t len);
826 
833  void release(caddr_t address, size_t len);
834 
843  inline caddr_t fetch(size_t offset = 0)
844  {return ((char *)(fcb.address)) + offset;};
845 
854  caddr_t fetch(off_t pos, size_t len);
855 
861  bool lock(void);
862 
866  void unlock(void);
867 
874  size_t pageAligned(size_t size);
875 };
876 
877 
887 {
888 private:
889  const char *err;
890 #ifdef HAVE_MODULES
891  static Mutex mutex;
892  static DSO *first;
893  static DSO *last;
894  DSO *next, *prev;
895  const char *id;
896 #if defined(HAVE_MACH_DYLD)
897  NSModule oModule;
898 #elif defined(HAVE_SHL_LOAD)
899  shl_t image;
900 #elif defined(WIN32)
901  HINSTANCE hImage;
902 #else
903  void *image;
904 #endif
905  void loader(const char *filename, bool resolve);
906 #endif
907 
908 public:
914 #ifdef HAVE_MODULES
915  DSO(const char *filename)
916  {loader(filename, true);};
917 
918  DSO(const char *filename, bool resolve)
919  {loader(filename, resolve);};
920 #else
921  DSO(const char *filename)
922  {throw this;};
923  DSO(const char *filename, bool resolve)
924  {throw this;};
925 #endif
926 
931  inline const char *getError(void)
932  {return err;};
933 
937 #ifdef HAVE_MODULES
938  virtual ~DSO();
939 #endif
940 
944 #ifdef HAVE_MODULES
945  void* operator[](const char *sym);
946 #else
947  void *operator[](const char *)
948  {return NULL;};
949 #endif
950 
951 #ifdef HAVE_MODULES
952  static void dynunload(void);
953 #else
954  static void dynunload(void)
955  {return;};
956 #endif
957 
963  static DSO *getObject(const char *name);
964 
970  bool isValid(void);
971 
975  static void setDebug(void);
976 };
977 
979 bool __EXPORT isDir(const char *path);
981 bool __EXPORT isFile(const char *path);
982 #ifndef WIN32
983 
984 bool __EXPORT isDevice(const char *path);
985 #else
986 
987 inline bool isDevice(const char *path)
988 { return false; }
989 #endif
990 
991 bool __EXPORT canAccess(const char *path);
993 bool __EXPORT canModify(const char *path);
995 time_t __EXPORT lastModified(const char *path);
997 time_t __EXPORT lastAccessed(const char *path);
998 
999 #ifdef COMMON_STD_EXCEPTION
1000 
1001 class DirException : public IOException
1002 {
1003 public:
1004  DirException(const String &str) : IOException(str) {};
1005 };
1006 
1007 class __EXPORT DSOException : public IOException
1008 {
1009 public:
1010  DSOException(const String &str) : IOException(str) {};
1011 };
1012 
1013 class __EXPORT FileException : public IOException
1014 {
1015 public:
1016  FileException(const String &str) : IOException(str) {};
1017 };
1018 
1019 #endif
1020 
1021 #ifdef CCXX_NAMESPACES
1022 }
1023 #endif
1024 
1025 #endif
1026 
Definition: file.h:126
Error error(char *err)
Post an extended string error message.
Definition: file.h:444
#define PATH_MAX
Definition: file.h:107
Definition: file.h:231
Definition: file.h:114
caddr_t address
Definition: file.h:152
bool locked
Definition: file.h:155
int fd
Definition: file.h:401
#define NAME_MAX
Definition: file.h:111
int HANDLE
Definition: serial.h:60
#define S_IRUSR
Definition: file.h:185
Definition: file.h:131
Definition: file.h:122
#define S_IRGRP
Definition: file.h:187
Open
Definition: file.h:168
Definition: address.h:64
#define __EXPORT
Definition: config.h:980
substitute functions which may be missing in target platform libc.
Definition: file.h:121
caddr_t fetch(size_t offset=0)
Fetch a pointer to an offset within the memory mapped portion of the disk file.
Definition: file.h:843
Definition: file.h:232
off_t pos
Definition: file.h:154
char * pathname
Definition: file.h:407
const char * operator++(int)
Definition: file.h:289
Definition: file.h:123
#define S_IWGRP
Definition: file.h:188
This class defines a database I/O file service that can be shared by multiple processes.
Definition: file.h:645
Definition: file.h:125
GNU Common C++ exception model base classes.
Definition: file.h:120
The purpose of this class is to define a base class for low level random file access that is portable...
Definition: file.h:393
char * getErrorString(void)
Return current error string.
Definition: file.h:537
#define INVALID_HANDLE_VALUE
Definition: serial.h:61
Synchronization and threading services.
DSO(const char *filename)
Construct and load a DSO object file.
Definition: file.h:915
Error getErrorNumber(void)
Return current error id.
Definition: file.h:529
Access access
Definition: file.h:403
Definition: file.h:130
unsigned long pos_t
Definition: file.h:94
#define S_IWUSR
Definition: file.h:186
void setError(bool enable)
Used to enable or disable throwing of exceptions on errors.
Definition: file.h:453
Definition: file.h:129
Access
Definition: file.h:136
bool operator!()
Definition: file.h:296
This class allows the creation of a thread context unique "pointer" that can be set and retrieved and...
Definition: thread.h:1707
const char * getError(void)
Retrieve error indicator associated with DSO failure.
Definition: file.h:931
Definition: file.h:119
struct _fcb * next
Definition: file.h:151
A generic class to walk a hierarchical directory structure.
Definition: file.h:312
Definition: file.h:127
The Mutex class is used to protect a section of code so that at any given time only a single thread c...
Definition: thread.h:186
The DSO dynamic loader class is used to load object files.
Definition: file.h:886
Attr
Definition: file.h:196
This class defines a database I/O file service that can be shared by multiple threads.
Definition: file.h:562
size_t ccxx_size_t
Definition: file.h:97
void setTemporary(bool enable)
Used to set the temporary attribute for the file.
Definition: file.h:473
Create and map a disk file into memory.
Definition: file.h:744
Complete
Definition: file.h:230
Mapping
Definition: file.h:225
Error restart(void)
Restart an existing database; close and re-open.
Definition: file.h:678
DSO(const char *filename, bool resolve)
Definition: file.h:918
Definition: file.h:150
ccxx_size_t len
Definition: file.h:153
Definition: file.h:128
Definition: file.h:124
#define S_IWOTH
Definition: file.h:190
A low level portable directory class.
Definition: file.h:255
const char * operator++()
Definition: file.h:286
#define S_IROTH
Definition: file.h:189
Error
Definition: file.h:117
__EXPORT AppLog & error(AppLog &sl)
Manipulator for error level.
Definition: applog.h:541