UCommon
fsys.h
Go to the documentation of this file.
1 // Copyright (C) 2006-2010 David Sugar, Tycho Softworks.
2 //
3 // This file is part of GNU uCommon C++.
4 //
5 // GNU uCommon C++ is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published
7 // by the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // GNU uCommon C++ is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>.
17 
27 #ifndef _UCOMMON_FSYS_H_
28 #define _UCOMMON_FSYS_H_
29 
30 #ifndef _UCOMMON_CONFIG_H_
31 #include <ucommon/platform.h>
32 #endif
33 
34 #ifndef _UCOMMON_PROTOCOLS_H_
35 #include <ucommon/protocols.h>
36 #endif
37 
38 #ifndef _UCOMMON_THREAD_H_
39 #include <ucommon/thread.h>
40 #endif
41 
42 #ifndef _UCOMMON_STRING_H_
43 #include <ucommon/string.h>
44 #endif
45 
46 #ifndef _UCOMMON_MEMORY_H_
47 #include <ucommon/memory.h>
48 #endif
49 
50 #ifndef _MSWINDOWS_
51 #include <sys/stat.h>
52 #else
53 #include <io.h>
54 #ifndef R_OK
55 #define F_OK 0
56 #define X_OK 1
57 #define W_OK 2
58 #define R_OK 4
59 #endif
60 #endif
61 
62 #include <errno.h>
63 #include <stdio.h>
64 
65 #ifndef __S_ISTYPE
66 #define __S_ISTYPE(mode, mask) (((mode) & S_IFMT) == (mask))
67 #endif
68 
69 #if !defined(S_ISDIR) && defined(S_IFDIR)
70 #define S_ISDIR(mode) __S_ISTYPE((mode), S_IFDIR)
71 #endif
72 
73 #if !defined(S_ISCHR) && defined(S_IFCHR)
74 #define S_ISCHR(mode) __S_ISTYPE((mode), S_IFCHR)
75 #elif !defined(S_ISCHR)
76 #define S_ISCHR(mode) 0
77 #endif
78 
79 #if !defined(S_ISBLK) && defined(S_IFBLK)
80 #define S_ISBLK(mode) __S_ISTYPE((mode), S_IFBLK)
81 #elif !defined(S_ISBLK)
82 #define S_ISBLK(mode) 0
83 #endif
84 
85 #if !defined(S_ISREG) && defined(S_IFREG)
86 #define S_ISREG(mode) __S_ISTYPE((mode), S_IFREG)
87 #elif !defined(S_ISREG)
88 #define S_ISREG(mode) 1
89 #endif
90 
91 #if !defined(S_ISSOCK) && defined(S_IFSOCK)
92 #define S_ISSOCK(mode) __S_ISTYPE((mode), S_IFSOCK)
93 #elif !defined(S_ISSOCK)
94 #define S_ISSOCK(mode) (0)
95 #endif
96 
97 #if !defined(S_ISFIFO) && defined(S_IFIFO)
98 #define S_ISFIFO(mode) __S_ISTYPE((mode), S_IFIFO)
99 #elif !defined(S_ISFIFO)
100 #define S_ISFIFO(mode) (0)
101 #endif
102 
103 #if !defined(S_ISLNK) && defined(S_IFLNK)
104 #define S_ISLNK(mode) __S_ISTYPE((mode), S_IFLNK)
105 #elif !defined(S_ISLNK)
106 #define S_ISLNK(mode) (0)
107 #endif
108 
109 NAMESPACE_UCOMMON
110 
114 typedef void *mem_t;
115 
124 class __EXPORT fsys
125 {
126 protected:
127  fd_t fd;
128  int error;
129 
130 public:
134  enum {
135  OWNER_READONLY = 0400,
136  GROUP_READONLY = 0440,
137  PUBLIC_READONLY = 0444,
138  OWNER_PRIVATE = 0600,
139  OWNER_PUBLIC = 0644,
140  GROUP_PRIVATE = 0660,
141  GROUP_PUBLIC = 0664,
142  EVERYONE = 0666,
143  DIR_TEMPORARY = 01777
144  };
145 
146  typedef struct stat fileinfo_t;
147 
148 #ifdef _MSWINDOWS_
149  static int remapError(void);
150 #else
151  inline static int remapError(void)
152  {return errno;};
153 #endif
154 
158  typedef enum {
159  RDONLY,
160  WRONLY,
161  REWRITE,
162  RDWR = REWRITE,
163  APPEND,
164  SHARED,
165  EXCLUSIVE,
166  DEVICE,
167  STREAM,
168  RANDOM
169  } access_t;
170 
174  typedef long offset_t;
175 
179  static const offset_t end;
180 
184  fsys();
185 
189  fsys(fd_t handle);
190 
195  fsys(const fsys& descriptor);
196 
202  fsys(const char *path, access_t access);
203 
210  fsys(const char *path, unsigned permission, access_t access);
211 
215  ~fsys();
216 
221  inline fd_t operator*() const
222  {return fd;};
223 
228  inline operator fd_t() const
229  {return fd;}
230 
234  inline void reset(void)
235  {error = 0;}
236 
241  inline operator bool() const
242  {return fd != INVALID_HANDLE_VALUE;}
243 
248  inline bool operator!() const
249  {return fd == INVALID_HANDLE_VALUE;}
250 
255  void operator=(const fsys& descriptor);
256 
262  void operator*=(fd_t& descriptor);
263 
268  void operator=(fd_t descriptor);
269 
274  inline fd_t handle(void) const
275  {return fd;};
276 
281  void set(fd_t descriptor);
282 
287  fd_t release(void);
288 
294  int seek(offset_t offset);
295 
301  int drop(offset_t size = 0);
302 
307  bool is_tty(void);
308 
313  static bool is_tty(fd_t fd);
314 
321  ssize_t read(void *buffer, size_t count);
322 
329  ssize_t write(const void *buffer, size_t count);
330 
336  int info(fileinfo_t *buffer);
337 
344  int trunc(offset_t offset);
345 
350  int sync(void);
351 
357  static int prefix(const char *path);
358 
365  static int prefix(char *path, size_t size);
366 
367  static string_t prefix(void);
368 
375  static int info(const char *path, fileinfo_t *buffer);
376 
382  static int erase(const char *path);
383 
391  static int copy(const char *source, const char *target, size_t size = 1024);
392 
399  static int rename(const char *oldpath, const char *newpath);
400 
407  static int mode(const char *path, unsigned value);
408 
414  static bool is_exists(const char *path);
415 
421  static bool is_readable(const char *path);
422 
428  static bool is_writable(const char *path);
429 
435  static bool is_executable(const char *path);
436 
442  static bool is_file(const char *path);
443 
449  static bool is_dir(const char *path);
450 
456  static bool is_link(const char *path);
457 
463  static bool is_device(const char *path);
464 
470  static bool is_hidden(const char *path);
471 
477  void open(const char *path, access_t access);
478 
483  inline void assign(fd_t descriptor)
484  {close(); fd = descriptor;};
485 
491  inline static void assign(fsys& object, fd_t descriptor)
492  {object.close(); object.fd = descriptor;};
493 
500  void open(const char *path, unsigned mode, access_t access);
501 
509  static int unlink(const char *path);
510 
517  static int link(const char *path, const char *target);
518 
525  static int hardlink(const char *path, const char *target);
526 
533  static int linkinfo(const char *path, char *buffer, size_t size);
534 
539  int close(void);
540 
545  inline int err(void) const
546  {return error;}
547 
553  static fd_t input(const char *path);
554 
560  static fd_t output(const char *path);
561 
567  static fd_t append(const char *path);
568 
573  static void release(fd_t descriptor);
574 
582  static int pipe(fd_t& input, fd_t& output, size_t size = 0);
583 
592  static int inherit(fd_t& descriptor, bool enable);
593 
598  static fd_t null(void);
599 
605  static int load(const char *path);
606 
614  static int exec(const char *path, char **argv, char **envp = NULL);
615 
616  static inline bool is_file(struct stat *inode)
617  {return S_ISREG(inode->st_mode);}
618 
619  static inline bool is_dir(struct stat *inode)
620  {return S_ISDIR(inode->st_mode);}
621 
622  static inline bool is_link(struct stat *inode)
623  {return S_ISLNK(inode->st_mode);}
624 
625  static inline bool is_dev(struct stat *inode)
626  {return S_ISBLK(inode->st_mode) || S_ISCHR(inode->st_mode);}
627 
628  static inline bool is_char(struct stat *inode)
629  {return S_ISCHR(inode->st_mode);}
630 
631  static inline bool is_disk(struct stat *inode)
632  {return S_ISBLK(inode->st_mode);}
633 
634  static inline bool is_sys(struct stat *inode)
635  {return S_ISSOCK(inode->st_mode) || S_ISFIFO(inode->st_mode);}
636 };
637 
642 class __EXPORT dso
643 {
644 private:
645  friend class fsys;
646 
647 #ifdef _MSWINDOWS_
648  HINSTANCE ptr;
649 #else
650  void *ptr;
651 #endif
652  int error;
653 
654 public:
655 #ifdef _MSWINDOWS_
656  typedef int (FAR WINAPI *addr_t)();
657 #else
658  typedef void *addr_t;
659 #endif
660 
664  dso();
665 
670  dso(const char *path);
671 
675  ~dso();
676 
681  void map(const char *path);
682 
686  void release(void);
687 
694  addr_t find(const char *symbol) const;
695 
696  inline int err(void) const
697  {return error;}
698 
699  inline addr_t operator[](const char *symbol) const
700  {return find(symbol);}
701 
702  inline addr_t operator()(const char *symbol) const
703  {return find(symbol);}
704 
705  inline operator bool()
706  {return ptr != NULL;}
707 
708  inline bool operator!()
709  {return ptr == NULL;}
710 };
711 
716 class __EXPORT dir : private fsys
717 {
718 private:
719 #ifdef _MSWINDOWS_
720  WIN32_FIND_DATA *ptr;
721  HINSTANCE mem;
722 #else
723  void *ptr;
724 #endif
725 
726 public:
731  dir(const char *path);
732 
736  dir();
737 
741  ~dir();
742 
749  static int create(const char *path, unsigned mode);
750 
756  static int remove(const char *path);
757 
762  void open(const char *path);
763 
770  ssize_t read(char *buffer, size_t count);
771 
775  void close(void);
776 
777  inline int err(void) const
778  {return fsys::err();}
779 
780  inline void reset(void)
781  {fsys::reset();}
782 
787  inline operator bool() const
788  {return ptr != NULL;};
789 
794  inline bool operator!() const
795  {return ptr == NULL;};
796 };
797 
801 typedef fsys fsys_t;
802 
803 typedef dir dir_t;
804 
805 typedef dso dso_t;
806 
807 inline bool is_exists(const char *path)
808  {return fsys::is_exists(path);}
809 
810 inline bool is_readable(const char *path)
811  {return fsys::is_readable(path);}
812 
813 inline bool is_writable(const char *path)
814  {return fsys::is_writable(path);}
815 
816 inline bool is_executable(const char *path)
817  {return fsys::is_executable(path);}
818 
819 inline bool is_file(const char *path)
820  {return fsys::is_file(path);}
821 
822 inline bool is_dir(const char *path)
823  {return fsys::is_dir(path);}
824 
825 inline bool is_link(const char *path)
826  {return fsys::is_link(path);}
827 
828 inline bool is_device(const char *path)
829  {return fsys::is_device(path);}
830 
831 END_NAMESPACE
832 
833 #endif
834