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 
235  inline operator bool() const
236  {return fd != INVALID_HANDLE_VALUE;}
237 
242  inline bool operator!() const
243  {return fd == INVALID_HANDLE_VALUE;}
244 
249  void operator=(const fsys& descriptor);
250 
256  void operator*=(fd_t& descriptor);
257 
262  void operator=(fd_t descriptor);
263 
268  inline fd_t handle(void) const
269  {return fd;};
270 
275  void set(fd_t descriptor);
276 
281  fd_t release(void);
282 
288  int seek(offset_t offset);
289 
295  int drop(offset_t size = 0);
296 
301  bool is_tty(void);
302 
307  static bool is_tty(fd_t fd);
308 
315  ssize_t read(void *buffer, size_t count);
316 
323  ssize_t write(const void *buffer, size_t count);
324 
330  int info(fileinfo_t *buffer);
331 
338  int trunc(offset_t offset);
339 
344  int sync(void);
345 
351  static int prefix(const char *path);
352 
359  static int prefix(char *path, size_t size);
360 
361  static string_t prefix(void);
362 
369  static int info(const char *path, fileinfo_t *buffer);
370 
376  static int erase(const char *path);
377 
385  static int copy(const char *source, const char *target, size_t size = 1024);
386 
393  static int rename(const char *oldpath, const char *newpath);
394 
401  static int mode(const char *path, unsigned value);
402 
408  static bool is_exists(const char *path);
409 
415  static bool is_readable(const char *path);
416 
422  static bool is_writable(const char *path);
423 
429  static bool is_executable(const char *path);
430 
436  static bool is_file(const char *path);
437 
443  static bool is_dir(const char *path);
444 
450  static bool is_link(const char *path);
451 
457  static bool is_device(const char *path);
458 
464  static bool is_hidden(const char *path);
465 
471  void open(const char *path, access_t access);
472 
477  inline void assign(fd_t descriptor)
478  {close(); fd = descriptor;};
479 
485  inline static void assign(fsys& object, fd_t descriptor)
486  {object.close(); object.fd = descriptor;};
487 
494  void open(const char *path, unsigned mode, access_t access);
495 
503  static int unlink(const char *path);
504 
511  static int link(const char *path, const char *target);
512 
519  static int hardlink(const char *path, const char *target);
520 
527  static int linkinfo(const char *path, char *buffer, size_t size);
528 
533  int close(void);
534 
539  inline int err(void) const
540  {return error;}
541 
547  static fd_t input(const char *path);
548 
554  static fd_t output(const char *path);
555 
561  static fd_t append(const char *path);
562 
567  static void release(fd_t descriptor);
568 
576  static int pipe(fd_t& input, fd_t& output, size_t size = 0);
577 
586  static int inherit(fd_t& descriptor, bool enable);
587 
592  static fd_t null(void);
593 
599  static int load(const char *path);
600 
608  static int exec(const char *path, char **argv, char **envp = NULL);
609 
610  static inline bool is_file(struct stat *inode)
611  {return S_ISREG(inode->st_mode);}
612 
613  static inline bool is_dir(struct stat *inode)
614  {return S_ISDIR(inode->st_mode);}
615 
616  static inline bool is_link(struct stat *inode)
617  {return S_ISLNK(inode->st_mode);}
618 
619  static inline bool is_dev(struct stat *inode)
620  {return S_ISBLK(inode->st_mode) || S_ISCHR(inode->st_mode);}
621 
622  static inline bool is_char(struct stat *inode)
623  {return S_ISCHR(inode->st_mode);}
624 
625  static inline bool is_disk(struct stat *inode)
626  {return S_ISBLK(inode->st_mode);}
627 
628  static inline bool is_sys(struct stat *inode)
629  {return S_ISSOCK(inode->st_mode) || S_ISFIFO(inode->st_mode);}
630 };
631 
636 class __EXPORT dso
637 {
638 private:
639  friend class fsys;
640 
641 #ifdef _MSWINDOWS_
642  HINSTANCE ptr;
643 #else
644  void *ptr;
645 #endif
646  int error;
647 
648 public:
649 #ifdef _MSWINDOWS_
650  typedef int (FAR WINAPI *addr_t)();
651 #else
652  typedef void *addr_t;
653 #endif
654 
658  dso();
659 
664  dso(const char *path);
665 
669  ~dso();
670 
675  void map(const char *path);
676 
680  void release(void);
681 
688  addr_t find(const char *symbol) const;
689 
690  inline int err(void) const
691  {return error;}
692 
693  inline addr_t operator[](const char *symbol) const
694  {return find(symbol);}
695 
696  inline addr_t operator()(const char *symbol) const
697  {return find(symbol);}
698 
699  inline operator bool()
700  {return ptr != NULL;}
701 
702  inline bool operator!()
703  {return ptr == NULL;}
704 };
705 
710 class __EXPORT dir : private fsys
711 {
712 private:
713 #ifdef _MSWINDOWS_
714  WIN32_FIND_DATA *ptr;
715  HINSTANCE mem;
716 #else
717  void *ptr;
718 #endif
719 
720 public:
725  dir(const char *path);
726 
730  dir();
731 
735  ~dir();
736 
743  static int create(const char *path, unsigned mode);
744 
750  static int remove(const char *path);
751 
756  void open(const char *path);
757 
764  ssize_t read(char *buffer, size_t count);
765 
769  void close(void);
770 
771  inline int err(void) const
772  {return fsys::err();}
773 
778  inline operator bool() const
779  {return ptr != NULL;};
780 
785  inline bool operator!() const
786  {return ptr == NULL;};
787 };
788 
792 typedef fsys fsys_t;
793 
794 typedef dir dir_t;
795 
796 typedef dso dso_t;
797 
798 inline bool is_exists(const char *path)
799  {return fsys::is_exists(path);}
800 
801 inline bool is_readable(const char *path)
802  {return fsys::is_readable(path);}
803 
804 inline bool is_writable(const char *path)
805  {return fsys::is_writable(path);}
806 
807 inline bool is_executable(const char *path)
808  {return fsys::is_executable(path);}
809 
810 inline bool is_file(const char *path)
811  {return fsys::is_file(path);}
812 
813 inline bool is_dir(const char *path)
814  {return fsys::is_dir(path);}
815 
816 inline bool is_link(const char *path)
817  {return fsys::is_link(path);}
818 
819 inline bool is_device(const char *path)
820  {return fsys::is_device(path);}
821 
822 END_NAMESPACE
823 
824 #endif
825