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
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;
258 Dir(
const char *name = NULL);
260 static bool create(
const char *path, Attr attr = attrGroup);
261 static bool remove(
const char *path);
262 static bool setPrefix(
const char *path);
263 static bool getPrefix(
char *path,
size_t size = PATH_MAX);
265 void open(
const char *name);
270 const char *getName(
void);
272 const char *operator++() {
276 const char *operator++(
int) {
280 const char *operator*();
284 bool operator!()
const {
288 return hDir == INVALID_HANDLE_VALUE;
292 operator bool()
const {
296 return hDir != INVALID_HANDLE_VALUE;
300 bool isValid(
void)
const;
312 char path[PATH_MAX + 1];
314 unsigned max, current, prefixpos;
328 virtual bool filter(
const char *file,
struct stat *ino);
338 DirTree(
const char *prefix,
unsigned maxdepth);
435 Error
error(Error errid,
char *errstr = NULL);
444 return error(errExtended, err);
454 flags.thrown = !enable;
544 operator bool()
const;
546 bool operator!(
void)
const;
567 Error open(
const char *path);
597 return open(pathname);
610 Error
fetch(caddr_t address = NULL, ccxx_size_t length = 0, off_t position = -1);
622 Error
update(caddr_t address = NULL, ccxx_size_t length = 0, off_t position = -1);
632 Error
clear(ccxx_size_t length = 0, off_t pos = -1);
640 Error
append(caddr_t address = NULL, ccxx_size_t length = 0);
649 bool operator++(
void);
650 bool operator--(
void);
705 MappedFile(
const char *fname, pos_t offset,
size_t size, Access mode);
727 void sync(caddr_t address,
size_t len);
737 void update(
size_t offset = 0,
size_t len = 0);
746 void update(caddr_t address,
size_t len);
764 inline caddr_t fetch(
size_t offset = 0) {
765 return ((
char *)(fcb.address)) + offset;
776 caddr_t fetch(off_t pos,
size_t len);
819 typedef ucommon::dso::addr_t addr_t;
824 void loader(
const char *filename,
bool resolve);
832 DSO(
const char *filename) {
833 loader(filename,
true);
836 DSO(
const char *filename,
bool resolve) {
837 loader(filename, resolve);
858 static void dynunload(
void);
881 bool __EXPORT isDir(
const char *path);
883 bool __EXPORT isFile(
const char *path);
886 bool __EXPORT isDevice(
const char *path);
889 inline bool isDevice(
const char *path) {
894 bool __EXPORT canAccess(
const char *path);
896 bool __EXPORT canModify(
const char *path);
898 time_t __EXPORT lastModified(
const char *path);
900 time_t __EXPORT lastAccessed(
const char *path);
902 #ifdef COMMON_STD_EXCEPTION
904 class DirException :
public IOException
907 DirException(
const String &str) : IOException(str) {};
910 class __EXPORT DSOException :
public IOException
913 DSOException(
const String &str) : IOException(str) {};
916 class __EXPORT FileException :
public IOException
919 FileException(
const String &str) : IOException(str) {};
AppLog & error(AppLog &sl)
Manipulator for error level.
GNU Common C++ exception model base classes.
T &() max(T &o1, T &o2)
Convenience function to return max of two objects.
A low level portable directory class.
A generic class to walk a hierarchical directory structure.
char * getPath(void)
Extract the next full pathname from the directory walk.
virtual bool filter(const char *file, struct stat *ino)
Virtual method to filter results.
DirTree(const char *prefix, unsigned maxdepth)
Construct a directory tree walk starting at the specified prefix.
DirTree(unsigned maxdepth)
Construct an un-opened directory tree of a known maximum depth.
void close(void)
Close the directory path.
unsigned perform(const char *prefix)
This is used to step through the filter virtual for an entire subtree, and is used for cases where a ...
void open(const char *prefix)
Open a directory tree path.
The purpose of this class is to define a base class for low level random file access that is portable...
void setTemporary(bool enable)
Used to set the temporary attribute for the file.
virtual Attr initialize(void)
This method is used to initialize a newly created file as indicated by the "initial" flag.
Error getErrorNumber(void) const
Return current error id.
Error error(char *err)
Post an extended string error message.
bool initial(void)
This method should be called right after a RandomFile derived object has been created.
Error error(Error errid, char *errstr=NULL)
Post an error event.
char * getErrorString(void) const
Return current error string.
RandomFile(const char *name=NULL)
Create an unopened random access file.
Error setCompletion(Complete mode)
Used to set file completion modes.
virtual Error restart(void)
This method is commonly used to close and re-open an existing database.
virtual ~RandomFile()
Destroy a random access file or it's derived class.
off_t getCapacity(void)
Get current file capacity.
void setError(bool enable)
Used to enable or disable throwing of exceptions on errors.
RandomFile(const RandomFile &rf)
Default copy constructor.
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.
virtual ~SharedFile()
Close and finish a database file.
Error update(char *address=NULL, ccxx_size_t length=0, off_t position=-1)
Update a portion of a file from physical memory.
Error append(char *address=NULL, ccxx_size_t length=0)
Add new data to the end of the file.
Error fetch(char *address=NULL, ccxx_size_t length=0, off_t position=-1)
Lock and Fetch a portion of the file into physical memory.
off_t getPosition(void)
Fetch the current file position marker for this thread.
SharedFile(const SharedFile &file)
Create a shared file as a duplicate of an existing shared file.
Error clear(ccxx_size_t length=0, off_t pos=-1)
Clear a lock held from a previous fetch operation without updating.
SharedFile(const char *path)
Open or create a new database file.
Create and map a disk file into memory.
void sync(char *address, size_t len)
Synchronize a segment of memory mapped from a segment fetch.
MappedFile(const char *fname, Access mode)
Open a file for mapping.
virtual ~MappedFile()
Release a mapped section of memory associated with a file.
void unlock(void)
Unlock a locked mapped portion of a file.
void release(char *address, size_t len)
Release (unmap) a memory segment.
MappedFile(const char *fname, Access mode, size_t size)
Create if not exists, and map a file of specified size into memory.
void update(char *address, size_t len)
Update a mapped region back to disk as specified by address and length.
void update(size_t offset=0, size_t len=0)
Map a portion of the memory mapped from the file back to the file and do not wait for completion.
bool lock(void)
Lock the currently mapped portion of a file.
MappedFile(const char *fname, pos_t offset, size_t size, Access mode)
Map a portion or all of a specified file in the specified shared memory access mode.
void sync(void)
Synchronize the contents of the mapped portion of memory with the disk file and wait for completion.
size_t pageAligned(size_t size)
Compute map size to aligned page boundry.
The DSO dynamic loader class is used to load object files.
bool isValid(void)
See if DSO object is valid.
static void setDebug(void)
Install debug handler...
DSO(const char *filename)
Construct and load a DSO object file.
const char * getError(void) const
Retrieve error indicator associated with DSO failure.
static DSO * getObject(const char *name)
Find a specific DSO object by filename.
virtual ~DSO()
Detach a DSO object from running memory.
addr_t operator[](const char *sym)
Lookup a symbol in the loaded file.
Common C++ thread class and sychronization objects.