filemgr.h

00001 /******************************************************************************
00002 *  filemgr.h   - definition of class FileMgr used for pooling file handles
00003 *
00004 * $Id: filemgr.h 2080 2007-09-17 06:21:29Z scribe $
00005 *
00006 * Copyright 1998 CrossWire Bible Society (http://www.crosswire.org)
00007 *       CrossWire Bible Society
00008 *       P. O. Box 2528
00009 *       Tempe, AZ  85280-2528
00010 *
00011 * This program is free software; you can redistribute it and/or modify it
00012 * under the terms of the GNU General Public License as published by the
00013 * Free Software Foundation version 2.
00014 *
00015 * This program is distributed in the hope that it will be useful, but
00016 * WITHOUT ANY WARRANTY; without even the implied warranty of
00017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 * General Public License for more details.
00019 *
00020 */
00021 
00022 #ifndef FILEMGR_H
00023 #define FILEMGR_H
00024 
00025 #include <sys/stat.h>
00026 #include <fcntl.h>
00027 
00028 #include <defs.h>
00029 #include <swcacher.h>
00030 #include <swbuf.h>
00031 
00032 SWORD_NAMESPACE_START
00033 
00034 class SWDLLEXPORT FileMgr;
00035 
00036 struct SWDLLEXPORT DirEntry {
00037 public:
00038         SWBuf name;
00039         unsigned long size;
00040         bool isDirectory;
00041 };
00045 class SWDLLEXPORT FileDesc {
00046 
00047         friend class FileMgr;
00048 
00049         long offset;
00050         int fd;                 // -77 closed;
00051         FileMgr *parent;
00052         FileDesc *next;
00053 
00054         FileDesc(FileMgr * parent, const char *path, int mode, int perms, bool tryDowngrade);
00055         virtual ~FileDesc();
00056 
00057 public:
00060         int getFd();
00061 
00062         long seek(long offset, int whence);
00063         long read(void *buf, long count);
00064         long write(const void *buf, long count);
00065 
00068         char *path;
00071         int mode;
00074         int perms;
00077         bool tryDowngrade;
00078 };
00079 
00085 class SWDLLEXPORT FileMgr : public SWCacher {
00086 
00087         friend class FileDesc;
00088         friend class __staticsystemFileMgr;
00089 
00090         FileDesc *files;
00091         int sysOpen(FileDesc * file);
00092 protected:
00093         static FileMgr *systemFileMgr;
00094 public:
00095         static int CREAT;
00096         static int APPEND;
00097         static int TRUNC;
00098         static int RDONLY;
00099         static int RDWR;
00100         static int WRONLY;
00101         static int IREAD;
00102         static int IWRITE;
00103 
00108         int maxFiles;
00109         
00110         static FileMgr *getSystemFileMgr();
00111         static void setSystemFileMgr(FileMgr *newFileMgr);
00112 
00116         FileMgr(int maxFiles = 35);
00117 
00121         ~FileMgr();
00122 
00130         FileDesc *open(const char *path, int mode, bool tryDowngrade);
00131 
00140         FileDesc *open(const char *path, int mode, int perms = IREAD | IWRITE, bool tryDowngrade = false);
00141 
00146         void close(FileDesc * file);
00147 
00150         virtual void flush();
00151         virtual long resourceConsumption();
00152 
00157         static signed char existsFile(const char *ipath, const char *ifileName = 0);
00158 
00163         static signed char existsDir(const char *ipath, const char *idirName = 0);
00164 
00169         signed char trunc(FileDesc *file);
00170 
00171         static char isDirectory(const char *path);
00172         static int createParent(const char *pName);
00173         static int createPathAndFile(const char *fName);
00174         static int copyFile(const char *srcFile, const char *destFile);
00175         static int copyDir(const char *srcDir, const char *destDir);
00176         static int removeDir(const char *targetDir);
00177         static int removeFile(const char *fName);
00178         static char getLine(FileDesc *fDesc, SWBuf &line);
00179 
00180 };
00181 
00182 
00183 SWORD_NAMESPACE_END
00184 #endif