27 #ifndef _UCOMMON_FILE_H_
28 #define _UCOMMON_FILE_H_
30 #ifndef _UCOMMON_CONFIG_H_
34 #ifndef _UCOMMON_PROTOCOLS_H_
38 #ifndef _UCOMMON_THREAD_H_
42 #ifndef _UCOMMON_STRING_H_
46 #ifndef _UCOMMON_MEMORY_H_
66 #define __S_ISTYPE(mode, mask) (((mode) & S_IFMT) == (mask))
69 #if !defined(S_ISDIR) && defined(S_IFDIR)
70 #define S_ISDIR(mode) __S_ISTYPE((mode), S_IFDIR)
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
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
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
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)
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)
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)
134 WIN32_FIND_DATA *ptr;
142 typedef struct stat fileinfo_t;
145 static int remapError(
void);
147 inline static int remapError(
void)
158 ACCESS_RDWR = ACCESS_REWRITE,
216 inline fd_t operator*()
const
223 inline operator fd_t()
const
230 inline operator bool()
const
231 {
return fd != INVALID_HANDLE_VALUE || ptr != NULL;};
237 inline bool operator!()
const
238 {
return fd == INVALID_HANDLE_VALUE && ptr == NULL;};
244 void operator=(
const fsys& descriptor);
251 void operator*=(fd_t& descriptor);
257 void operator=(fd_t descriptor);
263 inline fd_t getHandle(
void)
const
270 void set(fd_t descriptor);
283 int seek(offset_t offset);
290 int drop(offset_t size = 0);
302 static bool istty(fd_t fd);
310 ssize_t read(
void *buffer,
size_t count);
318 ssize_t write(
const void *buffer,
size_t count);
325 int fileinfo(fileinfo_t *buffer);
333 int trunc(offset_t offset);
346 static int changeDir(
const char *path);
354 static int getPrefix(
char *path,
size_t size);
362 static int fileinfo(
const char *path, fileinfo_t *buffer);
369 static int remove(
const char *path);
378 static int copy(
const char *source,
const char *target,
size_t size = 1024);
386 static int rename(
const char *oldpath,
const char *newpath);
394 static int change(
const char *path,
unsigned mode);
402 static int access(
const char *path,
unsigned mode);
409 static bool isfile(
const char *path);
416 static bool isdir(
const char *path);
423 static bool islink(
const char *path);
430 static bool ishidden(
const char *path);
439 inline static ssize_t read(
fsys& descriptor,
void *buffer,
size_t count)
440 {
return descriptor.
read(buffer, count);};
449 inline static ssize_t write(
fsys& descriptor,
const void *buffer,
size_t count)
450 {
return descriptor.
write(buffer, count);};
459 {
return descriptor.
seek(offset);};
468 {
return descriptor.
drop(size);};
475 void open(
const char *path, access_t
access);
481 inline void assign(fd_t descriptor)
482 {close(); fd = descriptor;};
489 inline static void assign(
fsys&
object, fd_t descriptor)
490 {
object.close();
object.fd = descriptor;};
498 void create(
const char *path, access_t
access,
unsigned mode);
506 static int createDir(
const char *path,
unsigned mode);
513 static int removeDir(
const char *path);
522 static int unlink(
const char *path);
530 static int link(
const char *path,
const char *target);
538 static int hardlink(
const char *path,
const char *target);
546 static int linkinfo(
const char *path,
char *buffer,
size_t size);
552 inline static void close(
fsys& descriptor)
553 {descriptor.
close();};
564 inline int err(
void)
const
574 {
object.open(path, access);};
581 static fd_t input(
const char *path);
588 static fd_t output(
const char *path);
595 static fd_t append(
const char *path);
601 static void release(fd_t descriptor);
610 static int pipe(fd_t& input, fd_t& output,
size_t size = 0);
620 static int inherit(fd_t& descriptor,
bool enable);
626 static fd_t nullfile(
void);
636 {
object.create(path, access, mode);};
643 static int load(
const char *path);
651 static int exec(
const char *path,
char **argv);
653 static int exec(
const char *path,
char **argv,
char **envp);
660 static void load(
fsys& module,
const char *path);
666 static void unload(
fsys& module);
674 static void *find(
fsys& module,
const char *symbol);
676 static inline bool isfile(
struct stat *inode)
677 {
return S_ISREG(inode->st_mode);}
679 static inline bool isdir(
struct stat *inode)
680 {
return S_ISDIR(inode->st_mode);}
682 static inline bool islink(
struct stat *inode)
683 {
return S_ISLNK(inode->st_mode);}
685 static inline bool isdev(
struct stat *inode)
686 {
return S_ISBLK(inode->st_mode) || S_ISCHR(inode->st_mode);}
688 static inline bool ischar(
struct stat *inode)
689 {
return S_ISCHR(inode->st_mode);}
691 static inline bool isdisk(
struct stat *inode)
692 {
return S_ISBLK(inode->st_mode);}
694 static inline bool issys(
struct stat *inode)
695 {
return S_ISSOCK(inode->st_mode) || S_ISFIFO(inode->st_mode);}
714 typedef ::fpos_t bookmark_t;
721 {fp = file; opened =
false;}
728 charfile(
const char *path,
const char *mode);
744 inline operator bool()
751 inline bool operator !()
754 inline operator FILE *()
762 void open(
const char *path,
const char *mode);
774 size_t put(
const char *
string);
785 size_t readline(
char *
string,
size_t size);
795 size_t readline(
string&
string);
797 inline size_t put(
const void *data,
size_t size)
798 {
return fp == NULL ? 0 : fwrite(data, 1, size, fp);}
800 size_t get(
void *data,
size_t size)
801 {
return fp == NULL ? 0 : fread(data, 1, size, fp);}
803 inline void get(bookmark_t& pos)
804 {
if(fp) fsetpos(fp, &pos);}
806 inline void set(bookmark_t& pos)
807 {
if(fp) fgetpos(fp, &pos);}
813 inline void seek(
long offset)
814 {
if(fp) fseek(fp, offset, SEEK_SET);}
816 inline void move(
long offset)
817 {
if(fp) fseek(fp, offset, SEEK_CUR);}
819 inline void append(
void)
820 {
if (fp) fseek(fp, 0l, SEEK_END);}
822 inline void rewind(
void)
823 {
if(fp) ::rewind(fp);}
825 size_t printf(
const char *format, ...) __PRINTF(2, 3);
837 extern
charfile cstdin, cstdout, cstderr;