UCommon
fsys.h
Go to the documentation of this file.
1// Copyright (C) 2006-2014 David Sugar, Tycho Softworks.
2// Copyright (C) 2015-2020 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
110namespace ucommon {
111
115typedef void *mem_t;
116
125class __EXPORT fsys
126{
127protected:
128 fd_t fd;
129 mutable int error;
130
131public:
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 {
161 RDONLY,
162 WRONLY,
163 REWRITE,
164 RDWR = REWRITE,
165 APPEND,
166 SHARED,
167 EXCLUSIVE,
168 DEVICE,
169 STREAM,
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
662class __EXPORT dso
663{
664private:
665 friend class fsys;
666
667#ifdef _MSWINDOWS_
668 HINSTANCE ptr;
669#else
670 void *ptr;
671#endif
672 int error;
673
674 __DELETE_COPY(dso);
675
676public:
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
743class __EXPORT dir : private fsys
744{
745private:
746#ifdef _MSWINDOWS_
747 WIN32_FIND_DATA *ptr;
748 HINSTANCE mem;
749#else
750 void *ptr;
751#endif
752
753public:
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
832typedef fsys fsys_t;
833
834typedef dir dir_t;
835
836typedef dso dso_t;
837
838inline bool is_exists(const char *path)
839{
840 return fsys::is_exists(path);
841}
842
843inline bool is_readable(const char *path)
844{
845 return fsys::is_readable(path);
846}
847
848inline bool is_writable(const char *path)
849{
850 return fsys::is_writable(path);
851}
852
853inline bool is_executable(const char *path)
854{
855 return fsys::is_executable(path);
856}
857
858inline bool is_file(const char *path)
859{
860 return fsys::is_file(path);
861}
862
863inline bool is_dir(const char *path)
864{
865 return fsys::is_dir(path);
866}
867
868inline bool is_link(const char *path)
869{
870 return fsys::is_link(path);
871}
872
873inline bool is_device(const char *path)
874{
875 return fsys::is_device(path);
876}
877
878} // namespace ucommon
879
880#endif
881
Private heaps, pools, and associations.
A thread-safe atomic heap management system.
Various miscellaneous platform specific headers and defines.
Abstract interfaces and support.
Common namespace for all ucommon objects.
Definition access.h:47
class __attribute__((visibility("default"))) dir typedef fsys fsys_t
Convenience class for directories.
Definition fsys.h:743
void * mem_t
Convenience type for loader operations.
Definition fsys.h:115
T copy(const T &src)
Convenience function to copy objects.
Definition generics.h:400
Thread classes and sychronization objects.