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);
354 void open(
const char *prefix);
379 unsigned perform(
const char *prefix);
435 Error
error(Error errid,
char *errstr = NULL);
444 return error(errExtended, err);
454 flags.thrown = !enable;
465 Error setCompletion(Complete mode);
489 virtual Attr initialize(
void);
517 off_t getCapacity(
void);
524 virtual Error restart(
void);
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);
647 off_t getPosition(
void);
649 bool operator++(
void);
650 bool operator--(
void);
693 MappedFile(
const char *fname, Access mode,
size_t size);
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);
754 void release(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);
796 size_t pageAligned(
size_t size);
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);
856 addr_t operator[](
const char *sym);
858 static void dynunload(
void);
865 static DSO *getObject(
const char *name);
877 static void setDebug(
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) {};
T &() max(T &o1, T &o2)
Convenience function to return max of two objects.
char * getErrorString(void) const
Return current error string.
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.
This class defines a database I/O file service that can be shared by multiple processes.
A generic class to walk a hierarchical directory structure.
Common C++ thread class and sychronization objects.
The DSO dynamic loader class is used to load object files.
AppLog & error(AppLog &sl)
Manipulator for error level.
void setError(bool enable)
Used to enable or disable throwing of exceptions on errors.
DSO(const char *filename)
Construct and load a DSO object file.
Error error(char *err)
Post an extended string error message.
Error restart(void)
Restart an existing database; close and re-open.
A low level portable directory class.
Error getErrorNumber(void) const
Return current error id.
Create and map a disk file into memory.
GNU Common C++ exception model base classes.
const char * getError(void) const
Retrieve error indicator associated with DSO failure.