43 #ifndef COMMONCPP_FILE_H_
44 #define COMMONCPP_FILE_H_
46 #ifndef COMMONCPP_CONFIG_H_
47 #include <commoncpp/config.h>
50 #ifndef COMMONCPP_THREAD_H_
54 #ifndef COMMONCPP_EXCEPTION_H_
61 # include <sys/types.h>
67 # include <sys/stat.h>
68 # include <sys/mman.h>
70 # if __BORLANDC__ >= 0x0560
72 # include <sys/stat.h>
80 typedef unsigned long pos_t;
85 #define caddr_t char *
86 typedef size_t ccxx_size_t;
88 typedef DWORD ccxx_size_t;
119 typedef enum Error Error;
123 accessReadOnly = O_RDONLY,
124 accessWriteOnly= O_WRONLY,
125 accessReadWrite = O_RDWR
127 accessReadOnly = GENERIC_READ,
128 accessWriteOnly = GENERIC_WRITE,
129 accessReadWrite = GENERIC_READ | GENERIC_WRITE
132 typedef enum Access Access;
135 typedef struct _fcb {
154 openReadOnly = O_RDONLY,
155 openWriteOnly = O_WRONLY,
156 openReadWrite = O_RDWR,
157 openAppend = O_WRONLY | O_APPEND,
159 openSync = O_RDWR | O_SYNC,
163 openTruncate = O_RDWR | O_TRUNC
165 typedef enum Open Open;
183 attrPrivate = S_IRUSR | S_IWUSR,
184 attrGroup = attrPrivate | S_IRGRP | S_IWGRP,
185 attrPublic = attrGroup | S_IROTH | S_IWOTH
187 #else // defined WIN32
195 typedef enum Attr Attr;
211 mappedRead = accessReadOnly,
212 mappedWrite = accessWriteOnly,
213 mappedReadWrite = accessReadWrite
221 typedef enum Complete Complete;
222 typedef enum Mapping Mapping;
225 static const char *getExtension(
const char *path);
226 static const char *getFilename(
const char *path);
227 static char *getFilename(
const char *path,
char *buffer,
size_t size = NAME_MAX);
228 static char *getDirname(
const char *path,
char *buffer,
size_t size = PATH_MAX);
229 static char *getRealpath(
const char *path,
char *buffer,
size_t size = PATH_MAX);
240 class __EXPORT
Dir :
public File
246 char save_space[
sizeof(
struct dirent) + PATH_MAX + 1];
247 struct dirent *entry;
250 WIN32_FIND_DATA data, fdata;
255 Dir(
const char *name = NULL);
257 static bool create(
const char *path, Attr attr = attrGroup);
258 static bool remove(
const char *path);
259 static bool setPrefix(
const char *path);
260 static bool getPrefix(
char *path,
size_t size = PATH_MAX);
262 void open(
const char *name);
267 const char *getName(
void);
269 const char *operator++()
272 const char *operator++(
int)
275 const char *operator*();
283 {
return hDir != INVALID_HANDLE_VALUE;};
298 char path[PATH_MAX + 1];
300 unsigned max, current, prefixpos;
312 virtual bool filter(
const char *file,
struct stat *ino);
322 DirTree(
const char *prefix,
unsigned maxdepth);
338 void open(
const char *prefix);
363 unsigned perform(
const char *prefix);
419 Error error(Error errid,
char *errstr = NULL);
428 {
return error(errExtended, err);};
437 {flags.thrown = !enable;};
447 Error setCompletion(Complete mode);
457 {flags.temp = enable;};
470 virtual Attr initialize(
void);
498 off_t getCapacity(
void);
505 virtual Error restart(
void);
523 bool operator!(
void);
544 Error open(
const char *path);
574 {
return open(pathname);};
586 Error fetch(caddr_t address = NULL, ccxx_size_t length = 0, off_t position = -1);
598 Error update(caddr_t address = NULL, ccxx_size_t length = 0, off_t position = -1);
608 Error clear(ccxx_size_t length = 0, off_t pos = -1);
616 Error append(caddr_t address = NULL, ccxx_size_t length = 0);
623 off_t getPosition(
void);
625 bool operator++(
void);
626 bool operator--(
void);
667 MappedFile(
const char *fname, Access mode,
size_t size);
679 MappedFile(
const char *fname, pos_t offset,
size_t size, Access mode);
701 void sync(caddr_t address,
size_t len);
711 void update(
size_t offset = 0,
size_t len = 0);
720 void update(caddr_t address,
size_t len);
728 void release(caddr_t address,
size_t len);
738 inline caddr_t
fetch(
size_t offset = 0)
739 {
return ((
char *)(fcb.address)) + offset;};
749 caddr_t fetch(off_t pos,
size_t len);
769 size_t pageAligned(
size_t size);
792 typedef ucommon::dso::addr_t addr_t;
795 void loader(
const char *filename,
bool resolve);
804 {loader(filename,
true);};
806 DSO(
const char *filename,
bool resolve)
807 {loader(filename, resolve);};
824 addr_t operator[](
const char *sym);
826 static void dynunload(
void);
833 static DSO *getObject(
const char *name);
845 static void setDebug(
void);
849 bool __EXPORT
isDir(
const char *path);
851 bool __EXPORT
isFile(
const char *path);
854 bool __EXPORT
isDevice(
const char *path);
857 inline bool isDevice(
const char *path)
861 bool __EXPORT
canAccess(
const char *path);
863 bool __EXPORT
canModify(
const char *path);
869 #ifdef COMMON_STD_EXCEPTION
871 class DirException :
public IOException
874 DirException(
const String &str) : IOException(str) {};
877 class __EXPORT DSOException :
public IOException
880 DSOException(
const String &str) : IOException(str) {};
883 class __EXPORT FileException :
public IOException
886 FileException(
const String &str) : IOException(str) {};
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.
T &() max(T &o1, T &o2)
Convenience function to return max of two objects.
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.
GNU Common C++ exception model base classes.
void lock(void)
Acquire or increase locking.
bool canModify(const char *path)
bool canAccess(const char *path)
time_t lastAccessed(const char *path)
void access(SharedAccess &object)
Convenience function to access (lock) shared object through it's protocol.
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.
void release(void)
Release or decrease locking.
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.
A generic class to walk a hierarchical directory structure.
void unlock(void)
Unlock the conditional's supporting mutex.
bool isDir(const char *path)
bool isFile(const char *path)
Error getErrorNumber(void)
Return current error id.
char * getErrorString(void)
Return current error string.