ucommon
file.h
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 COMMONCPP_FILE_H_
45 #define COMMONCPP_FILE_H_
46 
47 #ifndef COMMONCPP_CONFIG_H_
48 #include <commoncpp/config.h>
49 #endif
50 
51 #ifndef COMMONCPP_THREAD_H_
52 #include <commoncpp/thread.h>
53 #endif
54 
55 #ifndef COMMONCPP_EXCEPTION_H_
56 #include <commoncpp/exception.h>
57 #endif
58 
59 #ifndef WIN32
60 # ifdef __BORLANDC__
61 # include <stdio.h>
62 # include <sys/types.h>
63 # else
64 # include <fcntl.h>
65 # include <cstdio>
66 # endif
67 # include <dirent.h>
68 # include <sys/stat.h>
69 # include <sys/mman.h>
70 #else
71 # if __BORLANDC__ >= 0x0560
72 # include <dirent.h>
73 # include <sys/stat.h>
74 # else
75 # include <direct.h>
76 # endif
77 #endif
78 
79 NAMESPACE_COMMONCPP
80 
81 typedef unsigned long pos_t;
82 #ifndef _MSWINDOWS_
83 // use a define so that if the sys/types.h header already defines caddr_t
84 // as it may on BSD systems, we do not break it by redefining again.
85 #undef caddr_t
86 #define caddr_t char *
87 typedef size_t ccxx_size_t;
88 #else
89 typedef DWORD ccxx_size_t;
90 #endif
91 
92 #ifndef PATH_MAX
93 #define PATH_MAX 256
94 #endif
95 
96 #ifndef NAME_MAX
97 #define NAME_MAX 64
98 #endif
99 
100 class __EXPORT File
101 {
102 public:
103  enum Error {
104  errSuccess = 0,
105  errNotOpened,
106  errMapFailed,
107  errInitFailed,
108  errOpenDenied,
109  errOpenFailed,
110  errOpenInUse,
111  errReadInterrupted,
112  errReadIncomplete,
113  errReadFailure,
114  errWriteInterrupted,
115  errWriteIncomplete,
116  errWriteFailure,
117  errLockFailure,
118  errExtended
119  };
120  typedef enum Error Error;
121 
122  enum Access {
123 #ifndef _MSWINDOWS_
124  accessReadOnly = O_RDONLY,
125  accessWriteOnly= O_WRONLY,
126  accessReadWrite = O_RDWR
127 #else
128  accessReadOnly = GENERIC_READ,
129  accessWriteOnly = GENERIC_WRITE,
130  accessReadWrite = GENERIC_READ | GENERIC_WRITE
131 #endif
132  };
133  typedef enum Access Access;
134 
135 protected:
136  typedef struct _fcb {
137  struct _fcb *next;
138  caddr_t address;
139  ccxx_size_t len;
140  off_t pos;
141  bool locked;
142  } fcb_t;
143 
144 public:
145 #ifdef _MSWINDOWS_
146  enum Open {
147  openReadOnly, // = FILE_OPEN_READONLY,
148  openWriteOnly, // = FILE_OPEN_WRITEONLY,
149  openReadWrite, // = FILE_OPEN_READWRITE,
150  openAppend, // = FILE_OPEN_APPEND,
151  openTruncate // = FILE_OPEN_TRUNCATE
152  };
153 #else
154  enum Open {
155  openReadOnly = O_RDONLY,
156  openWriteOnly = O_WRONLY,
157  openReadWrite = O_RDWR,
158  openAppend = O_WRONLY | O_APPEND,
159 #ifdef O_SYNC
160  openSync = O_RDWR | O_SYNC,
161 #else
162  openSync = O_RDWR,
163 #endif
164  openTruncate = O_RDWR | O_TRUNC
165  };
166  typedef enum Open Open;
167 
168 /* to be used in future */
169 
170 #ifndef S_IRUSR
171 #define S_IRUSR 0400
172 #define S_IWUSR 0200
173 #define S_IRGRP 0040
174 #define S_IWGRP 0020
175 #define S_IROTH 0004
176 #define S_IWOTH 0002
177 #endif
178 
179 #endif // !WIN32
180 
181 #ifndef _MSWINDOWS_
182  enum Attr {
183  attrInvalid = 0,
184  attrPrivate = S_IRUSR | S_IWUSR,
185  attrGroup = attrPrivate | S_IRGRP | S_IWGRP,
186  attrPublic = attrGroup | S_IROTH | S_IWOTH
187  };
188 #else // defined WIN32
189  enum Attr {
190  attrInvalid=0,
191  attrPrivate,
192  attrGroup,
193  attrPublic
194  };
195 #endif // !WIN32
196  typedef enum Attr Attr;
197 
198 #ifdef _MSWINDOWS_
199  enum Complete {
200  completionImmediate, // = FILE_COMPLETION_IMMEDIATE,
201  completionDelayed, // = FILE_COMPLETION_DELAYED,
202  completionDeferred // = FILE_COMPLETION_DEFERRED
203  };
204 
205  enum Mapping {
206  mappedRead,
207  mappedWrite,
208  mappedReadWrite
209  };
210 #else
211  enum Mapping {
212  mappedRead = accessReadOnly,
213  mappedWrite = accessWriteOnly,
214  mappedReadWrite = accessReadWrite
215  };
216  enum Complete {
217  completionImmediate,
218  completionDelayed,
219  completionDeferred
220  };
221 #endif
222  typedef enum Complete Complete;
223  typedef enum Mapping Mapping;
224 
225 public:
226  static const char *getExtension(const char *path);
227  static const char *getFilename(const char *path);
228  static char *getFilename(const char *path, char *buffer, size_t size = NAME_MAX);
229  static char *getDirname(const char *path, char *buffer, size_t size = PATH_MAX);
230  static char *getRealpath(const char *path, char *buffer, size_t size = PATH_MAX);
231 };
232 
241 class __EXPORT Dir : public File
242 {
243 private:
244 #ifndef _MSWINDOWS_
245  DIR *dir;
246  struct dirent *save;
247  char save_space[sizeof(struct dirent) + PATH_MAX + 1];
248  struct dirent *entry;
249 #else
250  HANDLE hDir;
251  WIN32_FIND_DATA data, fdata;
252  char *name;
253 #endif
254 
255 public:
256  Dir(const char *name = NULL);
257 
258  static bool create(const char *path, Attr attr = attrGroup);
259  static bool remove(const char *path);
260  static bool setPrefix(const char *path);
261  static bool getPrefix(char *path, size_t size = PATH_MAX);
262 
263  void open(const char *name);
264  void close(void);
265 
266  virtual ~Dir();
267 
268  const char *getName(void);
269 
270  const char *operator++()
271  {return getName();};
272 
273  const char *operator++(int)
274  {return getName();};
275 
276  const char *operator*();
277 
278  bool rewind(void);
279 
280  bool operator!()
281 #ifndef _MSWINDOWS_
282  {return !dir;};
283 #else
284  {return hDir != INVALID_HANDLE_VALUE;};
285 #endif
286 
287  bool isValid(void);
288 };
289 
296 class __EXPORT DirTree
297 {
298 private:
299  char path[PATH_MAX + 1];
300  Dir *dir;
301  unsigned max, current, prefixpos;
302 
303 protected:
313  virtual bool filter(const char *file, struct stat *ino);
314 
315 public:
323  DirTree(const char *prefix, unsigned maxdepth);
324 
330  DirTree(unsigned maxdepth);
331 
332  virtual ~DirTree();
333 
339  void open(const char *prefix);
340 
344  void close(void);
345 
353  char *getPath(void);
354 
364  unsigned perform(const char *prefix);
365 };
366 
377 class __EXPORT RandomFile : protected Mutex, public File
378 {
379 private:
380  Error errid;
381  char *errstr;
382 
383 protected:
384 #ifndef _MSWINDOWS_
385  int fd;
386  // FIXME: WIN32 as no access member
387  Access access;
388 #else
389  HANDLE fd;
390 #endif
391  char *pathname;
392 
393  struct {
394  unsigned count : 16;
395  bool thrown : 1;
396  bool initial : 1;
397 #ifndef _MSWINDOWS_
398  bool immediate : 1;
399 #endif
400  bool temp : 1;
401  } flags;
402 
406  RandomFile(const char *name = NULL);
407 
411  RandomFile(const RandomFile &rf);
412 
420  Error error(Error errid, char *errstr = NULL);
421 
428  inline Error error(char *err)
429  {return error(errExtended, err);};
430 
437  inline void setError(bool enable)
438  {flags.thrown = !enable;};
439 
440 #ifndef _MSWINDOWS_
441 
448  Error setCompletion(Complete mode);
449 #endif
450 
457  inline void setTemporary(bool enable)
458  {flags.temp = enable;};
459 
471  virtual Attr initialize(void);
472 
476  void final(void);
477 
478 public:
482  virtual ~RandomFile();
483 
492  bool initial(void);
493 
499  off_t getCapacity(void);
500 
506  virtual Error restart(void);
507 
513  inline Error getErrorNumber(void)
514  {return errid;};
515 
521  inline char *getErrorString(void)
522  {return errstr;};
523 
524  bool operator!(void);
525 };
526 
541 class __EXPORT SharedFile : public RandomFile
542 {
543 private:
544  fcb_t fcb;
545  Error open(const char *path);
546 
547 public:
554  SharedFile(const char *path);
555 
562  SharedFile(const SharedFile &file);
563 
567  virtual ~SharedFile();
568 
574  Error restart(void)
575  {return open(pathname);};
576 
587  Error fetch(caddr_t address = NULL, ccxx_size_t length = 0, off_t position = -1);
588 
599  Error update(caddr_t address = NULL, ccxx_size_t length = 0, off_t position = -1);
600 
609  Error clear(ccxx_size_t length = 0, off_t pos = -1);
610 
617  Error append(caddr_t address = NULL, ccxx_size_t length = 0);
618 
624  off_t getPosition(void);
625 
626  bool operator++(void);
627  bool operator--(void);
628 };
629 
640 class __EXPORT MappedFile : public RandomFile
641 {
642 private:
643  fcb_t fcb;
644  int prot;
645 #ifdef _MSWINDOWS_
646  HANDLE map;
647  char mapname[64];
648 #endif
649 
650 public:
658  MappedFile(const char *fname, Access mode);
659 
668  MappedFile(const char *fname, Access mode, size_t size);
669 
680  MappedFile(const char *fname, pos_t offset, size_t size, Access mode);
681 
686  virtual ~MappedFile();
687 
688  // FIXME: not use library function in header ??
694  void sync(void);
695 
702  void sync(caddr_t address, size_t len);
703 
712  void update(size_t offset = 0, size_t len = 0);
713 
721  void update(caddr_t address, size_t len);
722 
729  void release(caddr_t address, size_t len);
730 
739  inline caddr_t fetch(size_t offset = 0)
740  {return ((char *)(fcb.address)) + offset;};
741 
750  caddr_t fetch(off_t pos, size_t len);
751 
757  bool lock(void);
758 
762  void unlock(void);
763 
770  size_t pageAligned(size_t size);
771 };
772 
773 
782 class __EXPORT DSO
783 {
784 private:
785  const char *err;
786  static Mutex mutex;
787  static DSO *first;
788  static DSO *last;
789  DSO *next, *prev;
790  const char *id;
791  void *image;
792 
793  typedef ucommon::dso::addr_t addr_t;
794 
795 protected:
796  void loader(const char *filename, bool resolve);
797 
798 public:
804  DSO(const char *filename)
805  {loader(filename, true);};
806 
807  DSO(const char *filename, bool resolve)
808  {loader(filename, resolve);};
809 
814  inline const char *getError(void)
815  {return err;};
816 
820  virtual ~DSO();
821 
825  addr_t operator[](const char *sym);
826 
827  static void dynunload(void);
828 
834  static DSO *getObject(const char *name);
835 
841  bool isValid(void);
842 
846  static void setDebug(void);
847 };
848 
850 bool __EXPORT isDir(const char *path);
852 bool __EXPORT isFile(const char *path);
853 #ifndef WIN32
854 
855 bool __EXPORT isDevice(const char *path);
856 #else
857 
858 inline bool isDevice(const char *path)
859 { return false; }
860 #endif
861 
862 bool __EXPORT canAccess(const char *path);
864 bool __EXPORT canModify(const char *path);
866 time_t __EXPORT lastModified(const char *path);
868 time_t __EXPORT lastAccessed(const char *path);
869 
870 #ifdef COMMON_STD_EXCEPTION
871 
872 class DirException : public IOException
873 {
874 public:
875  DirException(const String &str) : IOException(str) {};
876 };
877 
878 class __EXPORT DSOException : public IOException
879 {
880 public:
881  DSOException(const String &str) : IOException(str) {};
882 };
883 
884 class __EXPORT FileException : public IOException
885 {
886 public:
887  FileException(const String &str) : IOException(str) {};
888 };
889 
890 #endif
891 
892 END_NAMESPACE
893 
894 #endif
895