1
/******************************************************************************
2
* filemgr.h - definition of class FileMgr used for pooling file handles
4
* $Id: filemgr.h 2295 2009-03-29 17:11:27Z scribe $
6
* Copyright 1998 CrossWire Bible Society (http://www.crosswire.org)
7
* CrossWire Bible Society
11
* This program is free software; you can redistribute it and/or modify it
12
* under the terms of the GNU General Public License as published by the
13
* Free Software Foundation version 2.
15
* This program is distributed in the hope that it will be useful, but
16
* WITHOUT ANY WARRANTY; without even the implied warranty of
17
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18
* General Public License for more details.
34
class SWDLLEXPORT FileMgr;
36
struct SWDLLEXPORT DirEntry {
43
* This class represents one file. It works with the FileMgr object.
45
class SWDLLEXPORT FileDesc {
50
int fd; // -77 closed;
54
FileDesc(FileMgr * parent, const char *path, int mode, int perms, bool tryDowngrade);
58
/** @return File handle.
62
long seek(long offset, int whence);
63
long read(void *buf, long count);
64
long write(const void *buf, long count);
81
* This class ist used make file access operations easier.
82
* It keeps a list of all open files internally and closes them
83
* when the destructor is called.
85
class SWDLLEXPORT FileMgr : public SWCacher {
87
friend class FileDesc;
88
friend class __staticsystemFileMgr;
91
int sysOpen(FileDesc * file);
93
static FileMgr *systemFileMgr;
104
/** Maximum number of open files set in the constructor.
105
* determines the max number of real system files that
106
* filemgr will open. Adjust for tuning.
110
static FileMgr *getSystemFileMgr();
111
static void setSystemFileMgr(FileMgr *newFileMgr);
114
* @param maxFiles The number of files that this FileMgr may open in parallel, if necessary.
116
FileMgr(int maxFiles = 35);
119
* Destructor. Clean things up. Will close all files opened by this FileMgr object.
123
/** Open a file and return a FileDesc for it.
124
* The file itself will only be opened when FileDesc::getFd() is called.
125
* @param path Filename.
126
* @param mode File access mode.
127
* @param tryDowngrade
128
* @return FileDesc object for the requested file.
130
FileDesc *open(const char *path, int mode, bool tryDowngrade);
132
/** Open a file and return a FileDesc for it.
133
* The file itself will only be opened when FileDesc::getFd() is called.
134
* @param path Filename.
135
* @param mode File access mode.
136
* @param perms Permissions.
137
* @param tryDowngrade
138
* @return FileDesc object for the requested file.
140
FileDesc *open(const char *path, int mode, int perms = IREAD | IWRITE, bool tryDowngrade = false);
142
/** Close a given file and delete its FileDesc object.
143
* Will only close the file if it was created by this FileMgr object.
144
* @param file The file to close.
146
void close(FileDesc *file);
148
/** Cacher methods overridden
150
virtual void flush();
151
virtual long resourceConsumption();
153
/** Checks for the existence of a file.
154
* @param ipath Path to file.
155
* @param ifileName Name of file to check for.
157
static signed char existsFile(const char *ipath, const char *ifileName = 0);
159
/** Checks for the existence of a directory.
160
* @param ipath Path to directory.
161
* @param idirName Name of directory to check for.
163
static signed char existsDir(const char *ipath, const char *idirName = 0);
165
/** Truncate a file at its current position
166
* leaving byte at current possition intact deleting everything afterward.
167
* @param file The file to operate on.
169
signed char trunc(FileDesc *file);
171
static char isDirectory(const char *path);
172
static int createParent(const char *pName);
173
static int createPathAndFile(const char *fName);
175
/** attempts to open a file readonly
176
* @param fName filename to open
177
* @return fd; < 0 = error
179
static int openFileReadOnly(const char *fName);
180
static int copyFile(const char *srcFile, const char *destFile);
181
static int copyDir(const char *srcDir, const char *destDir);
182
static int removeDir(const char *targetDir);
183
static int removeFile(const char *fName);
184
static char getLine(FileDesc *fDesc, SWBuf &line);