Go to the documentation of this file.
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;
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) {};
virtual ~RandomFile()
Destroy a random access file or it's derived class.
virtual ~SharedFile()
Close and finish a database file.
void sync(void)
Synchronize the contents of the mapped portion of memory with the disk file and wait for completion.
MappedFile(const char *fname, Access mode, size_t size)
Create if not exists, and map a file of specified size into memory.
void release(char *address, size_t len)
Release (unmap) a memory segment.
virtual Error restart(void)
This method is commonly used to close and re-open an existing database.
bool initial(void)
This method should be called right after a RandomFile derived object has been created.
bool lock(void)
Lock the currently mapped portion of a file.
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 ...
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 setTemporary(bool enable)
Used to set the temporary attribute for the file.
Common C++ thread class and sychronization objects.
Error error(char *err)
Post an extended string error message.
bool isValid(void)
See if DSO object is valid.
virtual ~DSO()
Detach a DSO object from running memory.
size_t pageAligned(size_t size)
Compute map size to aligned page boundry.
Error append(char *address=NULL, ccxx_size_t length=0)
Add new data to the end of the file.
static void setDebug(void)
Install debug handler...
DSO(const char *filename)
Construct and load a DSO object file.
AppLog & error(AppLog &sl)
Manipulator for error level.
Error update(char *address=NULL, ccxx_size_t length=0, off_t position=-1)
Update a portion of a file from physical memory.
RandomFile(const RandomFile &rf)
Default copy constructor.
virtual Attr initialize(void)
This method is used to initialize a newly created file as indicated by the "initial" flag.
char * getErrorString(void) const
Return current error string.
char * getPath(void)
Extract the next full pathname from the directory walk.
SharedFile(const char *path)
Open or create a new database file.
void setError(bool enable)
Used to enable or disable throwing of exceptions on errors.
off_t getCapacity(void)
Get current file capacity.
MappedFile(const char *fname, Access mode)
Open a file for mapping.
T &() max(T &o1, T &o2)
Convenience function to return max of two objects.
off_t getPosition(void)
Fetch the current file position marker for this thread.
virtual bool filter(const char *file, struct stat *ino)
Virtual method to filter results.
GNU Common C++ exception model base classes.
Error clear(ccxx_size_t length=0, off_t pos=-1)
Clear a lock held from a previous fetch operation without updating.
static DSO * getObject(const char *name)
Find a specific DSO object by filename.
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.
DirTree(unsigned maxdepth)
Construct an un-opened directory tree of a known maximum depth.
virtual ~MappedFile()
Release a mapped section of memory associated with a file.
void unlock(void)
Unlock a locked mapped portion of a file.
DirTree(const char *prefix, unsigned maxdepth)
Construct a directory tree walk starting at the specified prefix.
void update(char *address, size_t len)
Update a mapped region back to disk as specified by address and length.
Error restart(void)
Restart an existing database; close and re-open.
void close(void)
Close the directory path.
void open(const char *prefix)
Open a directory tree path.
Create and map a disk file into memory.
The purpose of this class is to define a base class for low level random file access that is portable...
const char * getError(void) const
Retrieve error indicator associated with DSO failure.
void sync(char *address, size_t len)
Synchronize a segment of memory mapped from a segment fetch.
The DSO dynamic loader class is used to load object files.
Error setCompletion(Complete mode)
Used to set file completion modes.
A low level portable directory class.
SharedFile(const SharedFile &file)
Create a shared file as a duplicate of an existing shared file.
Error error(Error errid, char *errstr=NULL)
Post an error event.
RandomFile(const char *name=NULL)
Create an unopened random access file.
This class defines a database I/O file service that can be shared by multiple processes.
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.
addr_t operator[](const char *sym)
Lookup a symbol in the loaded file.
A generic class to walk a hierarchical directory structure.
Error getErrorNumber(void) const
Return current error id.