ucommon  7.0.0
About: GNU uCommon C++ is a portable and optimized class framework for writing C++ applications that need to use threads and support concurrent synchronization, and that use sockets, XML parsing, object serialization, thread-optimized string and data structure classes, etc..
  Fossies Dox: ucommon-7.0.0.tar.gz  ("inofficial" and yet experimental doxygen-generated source code documentation)  

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_TYPEREF_H_
44 #include <ucommon/typeref.h>
45 #endif
46 
47 #ifndef _UCOMMON_MEMORY_H_
48 #include <ucommon/memory.h>
49 #endif
50 
51 #ifndef _MSWINDOWS_
52 #include <sys/stat.h>
53 #else
54 #include <io.h>
55 #ifndef R_OK
56 #define F_OK 0
57 #define X_OK 1
58 #define W_OK 2
59 #define R_OK 4
60 #endif
61 #endif
62 
63 #include <errno.h>
64 #include <stdio.h>
65 
66 #ifndef __S_ISTYPE
67 #define __S_ISTYPE(mode, mask) (((mode) & S_IFMT) == (mask))
68 #endif
69 
70 #if !defined(S_ISDIR) && defined(S_IFDIR)
71 #define S_ISDIR(mode) __S_ISTYPE((mode), S_IFDIR)
72 #endif
73 
74 #if !defined(S_ISCHR) && defined(S_IFCHR)
75 #define S_ISCHR(mode) __S_ISTYPE((mode), S_IFCHR)
76 #elif !defined(S_ISCHR)
77 #define S_ISCHR(mode) 0
78 #endif
79 
80 #if !defined(S_ISBLK) && defined(S_IFBLK)
81 #define S_ISBLK(mode) __S_ISTYPE((mode), S_IFBLK)
82 #elif !defined(S_ISBLK)
83 #define S_ISBLK(mode) 0
84 #endif
85 
86 #if !defined(S_ISREG) && defined(S_IFREG)
87 #define S_ISREG(mode) __S_ISTYPE((mode), S_IFREG)
88 #elif !defined(S_ISREG)
89 #define S_ISREG(mode) 1
90 #endif
91 
92 #if !defined(S_ISSOCK) && defined(S_IFSOCK)
93 #define S_ISSOCK(mode) __S_ISTYPE((mode), S_IFSOCK)
94 #elif !defined(S_ISSOCK)
95 #define S_ISSOCK(mode) (0)
96 #endif
97 
98 #if !defined(S_ISFIFO) && defined(S_IFIFO)
99 #define S_ISFIFO(mode) __S_ISTYPE((mode), S_IFIFO)
100 #elif !defined(S_ISFIFO)
101 #define S_ISFIFO(mode) (0)
102 #endif
103 
104 #if !defined(S_ISLNK) && defined(S_IFLNK)
105 #define S_ISLNK(mode) __S_ISTYPE((mode), S_IFLNK)
106 #elif !defined(S_ISLNK)
107 #define S_ISLNK(mode) (0)
108 #endif
109 
110 namespace ucommon {
111 
115 typedef void *mem_t;
116 
126 {
127 protected:
129  mutable int error;
130 
131 public:
135  enum {
136  OWNER_READONLY = 0400,
137  GROUP_READONLY = 0440,
138  PUBLIC_READONLY = 0444,
139  OWNER_PRIVATE = 0600,
140  OWNER_PUBLIC = 0644,
141  GROUP_PRIVATE = 0660,
142  GROUP_PUBLIC = 0664,
143  EVERYONE = 0666,
144  DIR_TEMPORARY = 01777
145  };
146 
147  typedef struct stat fileinfo_t;
148 
149 #ifdef _MSWINDOWS_
150  static int remapError(void);
151 #else
152  inline static int remapError(void) {
153  return errno;
154  }
155 #endif
156 
160  typedef enum {
164  RDWR = REWRITE,
170  RANDOM
171  } access_t;
172 
176  typedef long offset_t;
177 
181  static const offset_t end;
182 
186  fsys();
187 
191  fsys(fd_t handle);
192 
197  fsys(const fsys& descriptor);
198 
204  fsys(const char *path, access_t access);
205 
212  fsys(const char *path, unsigned permission, access_t access);
213 
217  ~fsys();
218 
223  inline fd_t operator*() const {
224  return fd;
225  }
226 
231  inline operator fd_t() const {
232  return fd;
233  }
234 
238  inline void reset(void) {
239  error = 0;
240  }
241 
246  inline operator bool() const {
247  return fd != INVALID_HANDLE_VALUE;
248  }
249 
254  inline bool operator!() const {
255  return fd == INVALID_HANDLE_VALUE;
256  }
257 
262  fsys& operator=(const fsys& descriptor);
263 
269  fsys& operator*=(fd_t& descriptor);
270 
275  fsys& operator=(fd_t descriptor);
276 
281  inline fd_t handle(void) const {
282  return fd;
283  }
284 
289  void set(fd_t descriptor);
290 
295  fd_t release(void);
296 
302  int seek(offset_t offset);
303 
309  int drop(offset_t size = 0);
310 
315  bool is_tty(void) const;
316 
321  static bool is_tty(fd_t fd);
322 
329  ssize_t read(void *buffer, size_t count);
330 
337  ssize_t write(const void *buffer, size_t count);
338 
344  int info(fileinfo_t *buffer);
345 
352  int trunc(offset_t offset);
353 
358  int sync(void);
359 
365  static int prefix(const char *path);
366 
373  static int prefix(char *path, size_t size);
374 
375  static stringref_t prefix(void);
376 
383  static int info(const char *path, fileinfo_t *buffer);
384 
390  static int erase(const char *path);
391 
399  static int copy(const char *source, const char *target, size_t size = 1024);
400 
407  static int rename(const char *oldpath, const char *newpath);
408 
415  static int mode(const char *path, unsigned value);
416 
422  static bool is_exists(const char *path);
423 
429  static bool is_readable(const char *path);
430 
436  static bool is_writable(const char *path);
437 
443  static bool is_executable(const char *path);
444 
450  static bool is_file(const char *path);
451 
457  static bool is_dir(const char *path);
458 
464  static bool is_link(const char *path);
465 
471  static bool is_device(const char *path);
472 
478  static bool is_hidden(const char *path);
479 
485  void open(const char *path, access_t access);
486 
491  inline void assign(fd_t descriptor) {
492  close();
493  fd = descriptor;
494  }
495 
501  inline static void assign(fsys& object, fd_t descriptor) {
502  object.close();
503  object.fd = descriptor;
504  }
505 
512  void open(const char *path, unsigned mode, access_t access);
513 
521  static int unlink(const char *path);
522 
529  static int link(const char *path, const char *target);
530 
537  static int hardlink(const char *path, const char *target);
538 
545  static int linkinfo(const char *path, char *buffer, size_t size);
546 
551  int close(void);
552 
557  inline int err(void) const {
558  return error;
559  }
560 
566  static fd_t input(const char *path);
567 
573  static fd_t output(const char *path);
574 
580  static fd_t append(const char *path);
581 
586  static void release(fd_t descriptor);
587 
595  static int pipe(fd_t& input, fd_t& output, size_t size = 0);
596 
605  static int inherit(fd_t& descriptor, bool enable);
606 
611  static fd_t null(void);
612 
618  static int load(const char *path);
619 
627  static int exec(const char *path, char **argv, char **envp = NULL);
628 
629  static inline bool is_file(struct stat *inode) {
630  return S_ISREG(inode->st_mode);
631  }
632 
633  static inline bool is_dir(struct stat *inode) {
634  return S_ISDIR(inode->st_mode);
635  }
636 
637  static inline bool is_link(struct stat *inode) {
638  return S_ISLNK(inode->st_mode);
639  }
640 
641  static inline bool is_dev(struct stat *inode) {
642  return S_ISBLK(inode->st_mode) || S_ISCHR(inode->st_mode);
643  }
644 
645  static inline bool is_char(struct stat *inode) {
646  return S_ISCHR(inode->st_mode);
647  }
648 
649  static inline bool is_disk(struct stat *inode) {
650  return S_ISBLK(inode->st_mode);
651  }
652 
653  static inline bool is_sys(struct stat *inode) {
654  return S_ISSOCK(inode->st_mode) || S_ISFIFO(inode->st_mode);
655  }
656 };
657 
663 {
664 private:
665  friend class fsys;
666 
667 #ifdef _MSWINDOWS_
668  HINSTANCE ptr;
669 #else
670  void *ptr;
671 #endif
672  int error;
673 
675 
676 public:
677 #ifdef _MSWINDOWS_
678  typedef int (FAR WINAPI *addr_t)();
679 #else
680  typedef void *addr_t;
681 #endif
682 
686  dso();
687 
692  dso(const char *path);
693 
697  ~dso();
698 
703  void map(const char *path);
704 
708  void release(void);
709 
716  addr_t find(const char *symbol) const;
717 
718  inline int err(void) const {
719  return error;
720  }
721 
722  inline addr_t operator[](const char *symbol) const {
723  return find(symbol);
724  }
725 
726  inline addr_t operator()(const char *symbol) const {
727  return find(symbol);
728  }
729 
730  inline operator bool() const {
731  return ptr != NULL;
732  }
733 
734  inline bool operator!() const {
735  return ptr == NULL;
736  }
737 };
738 
743 class __EXPORT dir : private fsys
744 {
745 private:
746 #ifdef _MSWINDOWS_
747  WIN32_FIND_DATA *ptr;
748  HINSTANCE mem;
749 #else
750  void *ptr;
751 #endif
752 
753 public:
758  dir(const char *path);
759 
763  dir();
764 
768  ~dir();
769 
776  static int create(const char *path, unsigned mode);
777 
783  static int remove(const char *path);
784 
789  void open(const char *path);
790 
797  ssize_t read(char *buffer, size_t count);
798 
802  void close(void);
803 
804  inline int err(void) const {
805  return fsys::err();
806  }
807 
808  inline void reset(void) {
809  fsys::reset();
810  }
811 
816  inline operator bool() const {
817  return ptr != NULL;
818  }
819 
824  inline bool operator!() const {
825  return ptr == NULL;
826  }
827 };
828 
832 typedef fsys fsys_t;
833 
834 typedef dir dir_t;
835 
836 typedef dso dso_t;
837 
838 inline bool is_exists(const char *path)
839 {
840  return fsys::is_exists(path);
841 }
842 
843 inline bool is_readable(const char *path)
844 {
845  return fsys::is_readable(path);
846 }
847 
848 inline bool is_writable(const char *path)
849 {
850  return fsys::is_writable(path);
851 }
852 
853 inline bool is_executable(const char *path)
854 {
855  return fsys::is_executable(path);
856 }
857 
858 inline bool is_file(const char *path)
859 {
860  return fsys::is_file(path);
861 }
862 
863 inline bool is_dir(const char *path)
864 {
865  return fsys::is_dir(path);
866 }
867 
868 inline bool is_link(const char *path)
869 {
870  return fsys::is_link(path);
871 }
872 
873 inline bool is_device(const char *path)
874 {
875  return fsys::is_device(path);
876 }
877 
878 } // namespace ucommon
879 
880 #endif
881 
ucommon::fsys::APPEND
Definition: fsys.h:165
ucommon::fsys::operator!
bool operator!() const
Definition: fsys.h:254
ucommon::fsys::REWRITE
Definition: fsys.h:163
ucommon::fsys::fileinfo_t
struct stat fileinfo_t
Definition: fsys.h:147
ucommon::dir::operator!
bool operator!() const
Definition: fsys.h:824
S_ISLNK
#define S_ISLNK(mode)
Definition: fsys.h:107
ucommon::fsys::access_t
access_t
Definition: fsys.h:160
fd_t
int fd_t
Definition: platform.h:415
ucommon::dso::err
int err(void) const
Definition: fsys.h:718
ucommon::fsys::handle
fd_t handle(void) const
Definition: fsys.h:281
S_ISCHR
#define S_ISCHR(mode)
Definition: fsys.h:77
ucommon::fsys::is_readable
static bool is_readable(const char *path)
Definition: fsys.cpp:1034
ucommon
Definition: access.cpp:23
ucommon::fsys::assign
static void assign(fsys &object, fd_t descriptor)
Definition: fsys.h:501
ucommon::dso::operator!
bool operator!() const
Definition: fsys.h:734
ucommon::fsys::is_link
static bool is_link(const char *path)
Definition: fsys.cpp:1478
ucommon::is_exists
bool is_exists(const char *path)
Definition: fsys.h:838
protocols.h
error
static shell::stringopt error('e', "--error", _TEXT("stderr path to use"), "filename")
S_ISFIFO
#define S_ISFIFO(mode)
Definition: fsys.h:101
INVALID_HANDLE_VALUE
#define INVALID_HANDLE_VALUE
Definition: platform.h:417
ucommon::fsys::err
int err(void) const
Definition: fsys.h:557
ucommon::fsys::is_dev
static bool is_dev(struct stat *inode)
Definition: fsys.h:641
ucommon::dir_t
dir dir_t
Definition: fsys.h:834
ucommon::fsys::remapError
static int remapError(void)
Definition: fsys.h:152
ucommon::fsys::EXCLUSIVE
Definition: fsys.h:167
ucommon::copy
T copy(const T &src)
Definition: generics.h:395
ucommon::fsys::offset_t
long offset_t
Definition: fsys.h:176
ucommon::is_dir
bool is_dir(const char *path)
Definition: fsys.h:863
ucommon::dso::ptr
void * ptr
Definition: fsys.h:670
S_ISSOCK
#define S_ISSOCK(mode)
Definition: fsys.h:95
ucommon::fsys::reset
void reset(void)
Definition: fsys.h:238
ucommon::dso::error
int error
Definition: fsys.h:672
ucommon::fsys::is_dir
static bool is_dir(struct stat *inode)
Definition: fsys.h:633
ost::info
__EXPORT AppLog & info(AppLog &sl)
Definition: applog.h:576
output
static FILE * output
Definition: car.cpp:43
ucommon::is_readable
bool is_readable(const char *path)
Definition: fsys.h:843
__EXPORT
#define __EXPORT
Definition: config.h:49
ucommon::fsys::SHARED
Definition: fsys.h:166
ucommon::fsys::error
int error
Definition: fsys.h:129
ucommon::fsys::is_sys
static bool is_sys(struct stat *inode)
Definition: fsys.h:653
thread.h
ucommon::fsys::is_device
static bool is_device(const char *path)
Definition: fsys.cpp:1697
ucommon::dir::ptr
void * ptr
Definition: fsys.h:750
input
static shell::stringopt input('i', "--input", _TEXT("stdin path to use"), "filename")
ucommon::fsys::assign
void assign(fd_t descriptor)
Definition: fsys.h:491
ucommon::fsys::is_exists
static bool is_exists(const char *path)
Definition: fsys.cpp:1026
ucommon::fsys::is_disk
static bool is_disk(struct stat *inode)
Definition: fsys.h:649
__DELETE_COPY
#define __DELETE_COPY(x)
Definition: platform.h:160
platform.h
ucommon::is_device
bool is_device(const char *path)
Definition: fsys.h:873
ucommon::is_executable
bool is_executable(const char *path)
Definition: fsys.h:853
ucommon::is_file
bool is_file(const char *path)
Definition: fsys.h:858
ucommon::fsys::DEVICE
Definition: fsys.h:168
ucommon::dir
Definition: fsys.h:743
ucommon::dso_t
dso dso_t
Definition: fsys.h:836
ucommon::dir::reset
void reset(void)
Definition: fsys.h:808
ucommon::fsys::is_dir
static bool is_dir(const char *path)
Definition: fsys.cpp:1500
buffer
static uint8_t buffer[65536]
Definition: zerofill.cpp:27
ucommon::typeref
Definition: typeref.h:326
ucommon::fsys::operator*
fd_t operator*() const
Definition: fsys.h:223
ucommon::fsys::is_writable
static bool is_writable(const char *path)
Definition: fsys.cpp:1042
ucommon::fsys_t
fsys fsys_t
Definition: fsys.h:832
ucommon::fsys::STREAM
Definition: fsys.h:169
ucommon::fsys
Definition: fsys.h:125
S_ISBLK
#define S_ISBLK(mode)
Definition: fsys.h:83
prefix
static char prefix[80]
Definition: args.cpp:33
ucommon::fsys::is_link
static bool is_link(struct stat *inode)
Definition: fsys.h:637
ucommon::fsys::fd
fd_t fd
Definition: fsys.h:128
ucommon::is_writable
bool is_writable(const char *path)
Definition: fsys.h:848
ucommon::fsys::RDONLY
Definition: fsys.h:161
ucommon::fsys::end
static const offset_t end
Definition: fsys.h:181
ucommon::fsys::is_executable
static bool is_executable(const char *path)
Definition: fsys.cpp:1050
ucommon::fsys::is_file
static bool is_file(struct stat *inode)
Definition: fsys.h:629
S_ISREG
#define S_ISREG(mode)
Definition: fsys.h:89
ucommon::fsys::WRONLY
Definition: fsys.h:162
ucommon::dso::operator()
addr_t operator()(const char *symbol) const
Definition: fsys.h:726
ucommon::dso
Definition: fsys.h:662
ucommon::fsys::is_file
static bool is_file(const char *path)
Definition: fsys.cpp:1453
memory.h
ucommon::dso::operator[]
addr_t operator[](const char *symbol) const
Definition: fsys.h:722
ucommon::is_link
bool is_link(const char *path)
Definition: fsys.h:868
ucommon::dir::err
int err(void) const
Definition: fsys.h:804
ucommon::fsys::is_char
static bool is_char(struct stat *inode)
Definition: fsys.h:645
ucommon::mem_t
void * mem_t
Definition: fsys.h:115
ucommon::dso::addr_t
void * addr_t
Definition: fsys.h:680
typeref.h