UCommon
fsys.h
Go to the documentation of this file.
1 // Copyright (C) 2006-2014 David Sugar, Tycho Softworks.
2 // Copyright (C) 2015 Cherokees of Idaho.
3 //
4 // This file is part of GNU uCommon C++.
5 //
6 // GNU uCommon C++ is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU Lesser General Public License as published
8 // by the Free Software Foundation, either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // GNU uCommon C++ is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public License
17 // along with GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>.
18 
28 #ifndef _UCOMMON_FSYS_H_
29 #define _UCOMMON_FSYS_H_
30 
31 #ifndef _UCOMMON_CONFIG_H_
32 #include <ucommon/platform.h>
33 #endif
34 
35 #ifndef _UCOMMON_PROTOCOLS_H_
36 #include <ucommon/protocols.h>
37 #endif
38 
39 #ifndef _UCOMMON_THREAD_H_
40 #include <ucommon/thread.h>
41 #endif
42 
43 #ifndef _UCOMMON_STRING_H_
44 #include <ucommon/string.h>
45 #endif
46 
47 #ifndef _UCOMMON_TYPEREF_H_
48 #include <ucommon/typeref.h>
49 #endif
50 
51 #ifndef _UCOMMON_MEMORY_H_
52 #include <ucommon/memory.h>
53 #endif
54 
55 #ifndef _MSWINDOWS_
56 #include <sys/stat.h>
57 #else
58 #include <io.h>
59 #ifndef R_OK
60 #define F_OK 0
61 #define X_OK 1
62 #define W_OK 2
63 #define R_OK 4
64 #endif
65 #endif
66 
67 #include <errno.h>
68 #include <stdio.h>
69 
70 #ifndef __S_ISTYPE
71 #define __S_ISTYPE(mode, mask) (((mode) & S_IFMT) == (mask))
72 #endif
73 
74 #if !defined(S_ISDIR) && defined(S_IFDIR)
75 #define S_ISDIR(mode) __S_ISTYPE((mode), S_IFDIR)
76 #endif
77 
78 #if !defined(S_ISCHR) && defined(S_IFCHR)
79 #define S_ISCHR(mode) __S_ISTYPE((mode), S_IFCHR)
80 #elif !defined(S_ISCHR)
81 #define S_ISCHR(mode) 0
82 #endif
83 
84 #if !defined(S_ISBLK) && defined(S_IFBLK)
85 #define S_ISBLK(mode) __S_ISTYPE((mode), S_IFBLK)
86 #elif !defined(S_ISBLK)
87 #define S_ISBLK(mode) 0
88 #endif
89 
90 #if !defined(S_ISREG) && defined(S_IFREG)
91 #define S_ISREG(mode) __S_ISTYPE((mode), S_IFREG)
92 #elif !defined(S_ISREG)
93 #define S_ISREG(mode) 1
94 #endif
95 
96 #if !defined(S_ISSOCK) && defined(S_IFSOCK)
97 #define S_ISSOCK(mode) __S_ISTYPE((mode), S_IFSOCK)
98 #elif !defined(S_ISSOCK)
99 #define S_ISSOCK(mode) (0)
100 #endif
101 
102 #if !defined(S_ISFIFO) && defined(S_IFIFO)
103 #define S_ISFIFO(mode) __S_ISTYPE((mode), S_IFIFO)
104 #elif !defined(S_ISFIFO)
105 #define S_ISFIFO(mode) (0)
106 #endif
107 
108 #if !defined(S_ISLNK) && defined(S_IFLNK)
109 #define S_ISLNK(mode) __S_ISTYPE((mode), S_IFLNK)
110 #elif !defined(S_ISLNK)
111 #define S_ISLNK(mode) (0)
112 #endif
113 
114 namespace ucommon {
115 
119 typedef void *mem_t;
120 
129 class __EXPORT fsys
130 {
131 protected:
132  fd_t fd;
133  mutable int error;
134 
135 public:
139  enum {
140  OWNER_READONLY = 0400,
141  GROUP_READONLY = 0440,
142  PUBLIC_READONLY = 0444,
143  OWNER_PRIVATE = 0600,
144  OWNER_PUBLIC = 0644,
145  GROUP_PRIVATE = 0660,
146  GROUP_PUBLIC = 0664,
147  EVERYONE = 0666,
148  DIR_TEMPORARY = 01777
149  };
150 
151  typedef struct stat fileinfo_t;
152 
153 #ifdef _MSWINDOWS_
154  static int remapError(void);
155 #else
156  inline static int remapError(void) {
157  return errno;
158  }
159 #endif
160 
164  typedef enum {
165  RDONLY,
166  WRONLY,
167  REWRITE,
168  RDWR = REWRITE,
169  APPEND,
170  SHARED,
171  EXCLUSIVE,
172  DEVICE,
173  STREAM,
174  RANDOM
175  } access_t;
176 
180  typedef long offset_t;
181 
185  static const offset_t end;
186 
190  fsys();
191 
195  fsys(fd_t handle);
196 
201  fsys(const fsys& descriptor);
202 
208  fsys(const char *path, access_t access);
209 
216  fsys(const char *path, unsigned permission, access_t access);
217 
221  ~fsys();
222 
227  inline fd_t operator*() const {
228  return fd;
229  }
230 
235  inline operator fd_t() const {
236  return fd;
237  }
238 
242  inline void reset(void) {
243  error = 0;
244  }
245 
250  inline operator bool() const {
251  return fd != INVALID_HANDLE_VALUE;
252  }
253 
258  inline bool operator!() const {
259  return fd == INVALID_HANDLE_VALUE;
260  }
261 
266  void operator=(const fsys& descriptor);
267 
273  void operator*=(fd_t& descriptor);
274 
279  void operator=(fd_t descriptor);
280 
285  inline fd_t handle(void) const {
286  return fd;
287  }
288 
293  void set(fd_t descriptor);
294 
299  fd_t release(void);
300 
306  int seek(offset_t offset);
307 
313  int drop(offset_t size = 0);
314 
319  bool is_tty(void) const;
320 
325  static bool is_tty(fd_t fd);
326 
333  ssize_t read(void *buffer, size_t count);
334 
341  ssize_t write(const void *buffer, size_t count);
342 
348  int info(fileinfo_t *buffer);
349 
356  int trunc(offset_t offset);
357 
362  int sync(void);
363 
369  static int prefix(const char *path);
370 
377  static int prefix(char *path, size_t size);
378 
379  // to be depricated...
380  static string_t prefix(void);
381 
388  static int info(const char *path, fileinfo_t *buffer);
389 
395  static int erase(const char *path);
396 
404  static int copy(const char *source, const char *target, size_t size = 1024);
405 
412  static int rename(const char *oldpath, const char *newpath);
413 
420  static int mode(const char *path, unsigned value);
421 
427  static bool is_exists(const char *path);
428 
434  static bool is_readable(const char *path);
435 
441  static bool is_writable(const char *path);
442 
448  static bool is_executable(const char *path);
449 
455  static bool is_file(const char *path);
456 
462  static bool is_dir(const char *path);
463 
469  static bool is_link(const char *path);
470 
476  static bool is_device(const char *path);
477 
483  static bool is_hidden(const char *path);
484 
490  void open(const char *path, access_t access);
491 
496  inline void assign(fd_t descriptor) {
497  close();
498  fd = descriptor;
499  }
500 
506  inline static void assign(fsys& object, fd_t descriptor) {
507  object.close();
508  object.fd = descriptor;
509  }
510 
517  void open(const char *path, unsigned mode, access_t access);
518 
526  static int unlink(const char *path);
527 
534  static int link(const char *path, const char *target);
535 
542  static int hardlink(const char *path, const char *target);
543 
550  static int linkinfo(const char *path, char *buffer, size_t size);
551 
556  int close(void);
557 
562  inline int err(void) const {
563  return error;
564  }
565 
571  static fd_t input(const char *path);
572 
578  static fd_t output(const char *path);
579 
585  static fd_t append(const char *path);
586 
591  static void release(fd_t descriptor);
592 
600  static int pipe(fd_t& input, fd_t& output, size_t size = 0);
601 
610  static int inherit(fd_t& descriptor, bool enable);
611 
616  static fd_t null(void);
617 
623  static int load(const char *path);
624 
632  static int exec(const char *path, char **argv, char **envp = NULL);
633 
634  static inline bool is_file(struct stat *inode) {
635  return S_ISREG(inode->st_mode);
636  }
637 
638  static inline bool is_dir(struct stat *inode) {
639  return S_ISDIR(inode->st_mode);
640  }
641 
642  static inline bool is_link(struct stat *inode) {
643  return S_ISLNK(inode->st_mode);
644  }
645 
646  static inline bool is_dev(struct stat *inode) {
647  return S_ISBLK(inode->st_mode) || S_ISCHR(inode->st_mode);
648  }
649 
650  static inline bool is_char(struct stat *inode) {
651  return S_ISCHR(inode->st_mode);
652  }
653 
654  static inline bool is_disk(struct stat *inode) {
655  return S_ISBLK(inode->st_mode);
656  }
657 
658  static inline bool is_sys(struct stat *inode) {
659  return S_ISSOCK(inode->st_mode) || S_ISFIFO(inode->st_mode);
660  }
661 };
662 
667 class __EXPORT dso
668 {
669 private:
670  friend class fsys;
671 
672 #ifdef _MSWINDOWS_
673  HINSTANCE ptr;
674 #else
675  void *ptr;
676 #endif
677  int error;
678 
679  __DELETE_COPY(dso);
680 
681 public:
682 #ifdef _MSWINDOWS_
683  typedef int (FAR WINAPI *addr_t)();
684 #else
685  typedef void *addr_t;
686 #endif
687 
691  dso();
692 
697  dso(const char *path);
698 
702  ~dso();
703 
708  void map(const char *path);
709 
713  void release(void);
714 
721  addr_t find(const char *symbol) const;
722 
723  inline int err(void) const {
724  return error;
725  }
726 
727  inline addr_t operator[](const char *symbol) const {
728  return find(symbol);
729  }
730 
731  inline addr_t operator()(const char *symbol) const {
732  return find(symbol);
733  }
734 
735  inline operator bool() const {
736  return ptr != NULL;
737  }
738 
739  inline bool operator!() const {
740  return ptr == NULL;
741  }
742 };
743 
748 class __EXPORT dir : private fsys
749 {
750 private:
751 #ifdef _MSWINDOWS_
752  WIN32_FIND_DATA *ptr;
753  HINSTANCE mem;
754 #else
755  void *ptr;
756 #endif
757 
758 public:
763  dir(const char *path);
764 
768  dir();
769 
773  ~dir();
774 
781  static int create(const char *path, unsigned mode);
782 
788  static int remove(const char *path);
789 
794  void open(const char *path);
795 
802  ssize_t read(char *buffer, size_t count);
803 
807  void close(void);
808 
809  inline int err(void) const {
810  return fsys::err();
811  }
812 
813  inline void reset(void) {
814  fsys::reset();
815  }
816 
821  inline operator bool() const {
822  return ptr != NULL;
823  }
824 
829  inline bool operator!() const {
830  return ptr == NULL;
831  }
832 };
833 
837 typedef fsys fsys_t;
838 
839 typedef dir dir_t;
840 
841 typedef dso dso_t;
842 
843 inline bool is_exists(const char *path)
844 {
845  return fsys::is_exists(path);
846 }
847 
848 inline bool is_readable(const char *path)
849 {
850  return fsys::is_readable(path);
851 }
852 
853 inline bool is_writable(const char *path)
854 {
855  return fsys::is_writable(path);
856 }
857 
858 inline bool is_executable(const char *path)
859 {
860  return fsys::is_executable(path);
861 }
862 
863 inline bool is_file(const char *path)
864 {
865  return fsys::is_file(path);
866 }
867 
868 inline bool is_dir(const char *path)
869 {
870  return fsys::is_dir(path);
871 }
872 
873 inline bool is_link(const char *path)
874 {
875  return fsys::is_link(path);
876 }
877 
878 inline bool is_device(const char *path)
879 {
880  return fsys::is_device(path);
881 }
882 
883 } // namespace ucommon
884 
885 #endif
886 
static void assign(fsys &object, fd_t descriptor)
Assign a descriptor directly.
Definition: fsys.h:506
void access(SharedAccess &object)
Convenience function to access (lock) shared object through it's protocol.
Definition: access.h:271
A common string class and character string support functions.
void release(SharedAccess &object)
Convenience function to unlock shared object through it's protocol.
Definition: access.h:280
Thread classes and sychronization objects.
void * mem_t
Convenience type for loader operations.
Definition: fsys.h:119
fd_t operator*() const
Get the descriptor from the object by pointer reference.
Definition: fsys.h:227
Various miscellaneous platform specific headers and defines.
A thread-safe atomic heap management system.
static bool is_writable(const char *path)
Test if path writable.
Convenience class for directories.
Definition: fsys.h:748
static bool is_file(const char *path)
Test if path is a file.
void reset(void)
Reset error flag.
Definition: fsys.h:242
ObjectProtocol * copy(ObjectProtocol *object)
Convenience function to access object copy.
Definition: object.h:510
A copy-on-write string class that operates by reference count.
Definition: string.h:83
A container for generic and o/s portable threadsafe file system functions.
Definition: fsys.h:129
access_t
Enumerated file access modes.
Definition: fsys.h:164
static bool is_link(const char *path)
Test if path is a symlink.
fsys fsys_t
Convience type for fsys.
Definition: fsys.h:837
Convenience class for library plugins.
Definition: fsys.h:667
Private heaps, pools, and associations.
static const offset_t end
Used to mark "append" in set position operations.
Definition: fsys.h:185
static bool is_readable(const char *path)
Test if path readable.
static bool is_exists(const char *path)
Test if path exists.
long offset_t
File offset type.
Definition: fsys.h:180
Common namespace for all ucommon objects.
Definition: access.h:47
static bool is_device(const char *path)
Test if path is a device path.
void assign(fd_t descriptor)
Assign descriptor directly.
Definition: fsys.h:496
static bool is_dir(const char *path)
Test if path is a directory.
static bool is_executable(const char *path)
Test if path is executable.
bool operator!() const
Test if file descriptor is closed.
Definition: fsys.h:258
int err(void) const
Get last error.
Definition: fsys.h:562
fd_t handle(void) const
Get the native system descriptor handle of the file descriptor.
Definition: fsys.h:285
Abstract interfaces and support.
bool operator!() const
Test if file descriptor is closed.
Definition: fsys.h:829