44 #ifndef COMMONCPP_FILE_H_
45 #define COMMONCPP_FILE_H_
47 #ifndef COMMONCPP_CONFIG_H_
48 #include <commoncpp/config.h>
51 #ifndef COMMONCPP_THREAD_H_
55 #ifndef COMMONCPP_EXCEPTION_H_
62 # include <sys/types.h>
68 # include <sys/stat.h>
69 # include <sys/mman.h>
71 # if __BORLANDC__ >= 0x0560
73 # include <sys/stat.h>
81 typedef unsigned long pos_t;
86 #define caddr_t char *
87 typedef size_t ccxx_size_t;
89 typedef DWORD ccxx_size_t;
120 typedef enum Error Error;
124 accessReadOnly = O_RDONLY,
125 accessWriteOnly= O_WRONLY,
126 accessReadWrite = O_RDWR
128 accessReadOnly = GENERIC_READ,
129 accessWriteOnly = GENERIC_WRITE,
130 accessReadWrite = GENERIC_READ | GENERIC_WRITE
133 typedef enum Access Access;
136 typedef struct _fcb {
155 openReadOnly = O_RDONLY,
156 openWriteOnly = O_WRONLY,
157 openReadWrite = O_RDWR,
158 openAppend = O_WRONLY | O_APPEND,
160 openSync = O_RDWR | O_SYNC,
164 openTruncate = O_RDWR | O_TRUNC
166 typedef enum Open Open;
184 attrPrivate = S_IRUSR | S_IWUSR,
185 attrGroup = attrPrivate | S_IRGRP | S_IWGRP,
186 attrPublic = attrGroup | S_IROTH | S_IWOTH
188 #else // defined WIN32
196 typedef enum Attr Attr;
212 mappedRead = accessReadOnly,
213 mappedWrite = accessWriteOnly,
214 mappedReadWrite = accessReadWrite
222 typedef enum Complete Complete;
223 typedef enum Mapping Mapping;
226 static const char *getExtension(
const char *path);
227 static const char *getFilename(
const char *path);
228 static char *getFilename(
const char *path,
char *buffer,
size_t size = NAME_MAX);
229 static char *getDirname(
const char *path,
char *buffer,
size_t size = PATH_MAX);
230 static char *getRealpath(
const char *path,
char *buffer,
size_t size = PATH_MAX);
241 class __EXPORT
Dir :
public File
247 char save_space[
sizeof(
struct dirent) + PATH_MAX + 1];
248 struct dirent *entry;
251 WIN32_FIND_DATA data, fdata;
256 Dir(
const char *name = NULL);
258 static bool create(
const char *path, Attr attr = attrGroup);
259 static bool remove(
const char *path);
260 static bool setPrefix(
const char *path);
261 static bool getPrefix(
char *path,
size_t size = PATH_MAX);
263 void open(
const char *name);
268 const char *getName(
void);
270 const char *operator++()
273 const char *operator++(
int)
276 const char *operator*();
284 {
return hDir != INVALID_HANDLE_VALUE;};
299 char path[PATH_MAX + 1];
301 unsigned max, current, prefixpos;
313 virtual bool filter(
const char *file,
struct stat *ino);
323 DirTree(
const char *prefix,
unsigned maxdepth);
339 void open(
const char *prefix);
364 unsigned perform(
const char *prefix);
420 Error error(Error errid,
char *errstr = NULL);
429 {
return error(errExtended, err);};
438 {flags.thrown = !enable;};
448 Error setCompletion(Complete mode);
458 {flags.temp = enable;};
471 virtual Attr initialize(
void);
499 off_t getCapacity(
void);
506 virtual Error restart(
void);
524 bool operator!(
void);
545 Error open(
const char *path);
575 {
return open(pathname);};
587 Error fetch(caddr_t address = NULL, ccxx_size_t length = 0, off_t position = -1);
599 Error update(caddr_t address = NULL, ccxx_size_t length = 0, off_t position = -1);
609 Error clear(ccxx_size_t length = 0, off_t pos = -1);
617 Error append(caddr_t address = NULL, ccxx_size_t length = 0);
624 off_t getPosition(
void);
626 bool operator++(
void);
627 bool operator--(
void);
668 MappedFile(
const char *fname, Access mode,
size_t size);
680 MappedFile(
const char *fname, pos_t offset,
size_t size, Access mode);
702 void sync(caddr_t address,
size_t len);
712 void update(
size_t offset = 0,
size_t len = 0);
721 void update(caddr_t address,
size_t len);
729 void release(caddr_t address,
size_t len);
739 inline caddr_t
fetch(
size_t offset = 0)
740 {
return ((
char *)(fcb.address)) + offset;};
750 caddr_t fetch(off_t pos,
size_t len);
770 size_t pageAligned(
size_t size);
793 typedef ucommon::dso::addr_t addr_t;
796 void loader(
const char *filename,
bool resolve);
805 {loader(filename,
true);};
807 DSO(
const char *filename,
bool resolve)
808 {loader(filename, resolve);};
825 addr_t operator[](
const char *sym);
827 static void dynunload(
void);
834 static DSO *getObject(
const char *name);
846 static void setDebug(
void);
850 bool __EXPORT
isDir(
const char *path);
852 bool __EXPORT
isFile(
const char *path);
855 bool __EXPORT
isDevice(
const char *path);
858 inline bool isDevice(
const char *path)
862 bool __EXPORT
canAccess(
const char *path);
864 bool __EXPORT
canModify(
const char *path);
870 #ifdef COMMON_STD_EXCEPTION
872 class DirException :
public IOException
875 DirException(
const String &str) : IOException(str) {};
878 class __EXPORT DSOException :
public IOException
881 DSOException(
const String &str) : IOException(str) {};
884 class __EXPORT FileException :
public IOException
887 FileException(
const String &str) : IOException(str) {};
void unlock(void)
Unlock the conditional's supporting mutex.
T &() max(T &o1, T &o2)
Convenience function to return max of two objects.
The purpose of this class is to define a base class for low level random file access that is portable...
A low level portable directory class.
time_t lastModified(const char *path)
This class defines a database I/O file service that can be shared by multiple processes.
Error restart(void)
Restart an existing database; close and re-open.
void access(SharedAccess &object)
Convenience function to access (lock) shared object through it's protocol.
char * fetch(size_t offset=0)
Fetch a pointer to an offset within the memory mapped portion of the disk file.
DSO(const char *filename)
Construct and load a DSO object file.
bool canModify(const char *path)
bool canAccess(const char *path)
time_t lastAccessed(const char *path)
void setError(bool enable)
Used to enable or disable throwing of exceptions on errors.
void setTemporary(bool enable)
Used to set the temporary attribute for the file.
const char * getError(void)
Retrieve error indicator associated with DSO failure.
Common C++ thread class and sychronization objects.
Create and map a disk file into memory.
bool isDevice(const char *path)
The DSO dynamic loader class is used to load object files.
Error error(char *err)
Post an extended string error message.
GNU Common C++ exception model base classes.
A generic class to walk a hierarchical directory structure.
bool isDir(const char *path)
bool isFile(const char *path)
Error getErrorNumber(void)
Return current error id.
void release(void)
Release or decrease locking.
char * getErrorString(void)
Return current error string.
void lock(void)
Acquire or increase locking.