The SWORD Project  1.9.0.svnversion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
filemgr.h
Go to the documentation of this file.
1 /******************************************************************************
2  *
3  * filemgr.h - class FileMgr: encapsulates all file IO and
4  * platform-specific eccentricities.
5  *
6  * $Id: filemgr.h 3812 2020-10-15 17:32:41Z scribe $
7  *
8  * Copyright 1998-2013 CrossWire Bible Society (http://www.crosswire.org)
9  * CrossWire Bible Society
10  * P. O. Box 2528
11  * Tempe, AZ 85280-2528
12  *
13  * This program is free software; you can redistribute it and/or modify it
14  * under the terms of the GNU General Public License as published by the
15  * Free Software Foundation version 2.
16  *
17  * This program is distributed in the hope that it will be useful, but
18  * WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20  * General Public License for more details.
21  *
22  */
23 
24 #ifndef FILEMGR_H
25 #define FILEMGR_H
26 
27 #include <sys/stat.h>
28 #include <fcntl.h>
29 
30 #include <defs.h>
31 #include <swcacher.h>
32 #include <swbuf.h>
33 #include <vector>
34 
36 
38 
40 public:
42  unsigned long size;
44 };
45 
62 class SWDLLEXPORT FileMgr : public SWCacher {
63 
64  friend class FileDesc;
65  friend class __staticsystemFileMgr;
66 
68  int sysOpen(FileDesc * file);
69 protected:
71 public:
72  static unsigned int CREAT;
73  static unsigned int APPEND;
74  static unsigned int TRUNC;
75  static unsigned int RDONLY;
76  static unsigned int RDWR;
77  static unsigned int WRONLY;
78  static unsigned int IREAD;
79  static unsigned int IWRITE;
80 
85  int maxFiles;
86 
87  static FileMgr *getSystemFileMgr();
88  static void setSystemFileMgr(FileMgr *newFileMgr);
89 
93  FileMgr(int maxFiles = 35);
94 
98  ~FileMgr();
99 
107  FileDesc *open(const char *path, int mode, bool tryDowngrade);
108  FileDesc *open(const char *path, unsigned int mode, bool tryDowngrade) { return this->open(path, (int)mode, tryDowngrade); }
109 
118  FileDesc *open(const char *path, unsigned int mode, unsigned int perms = IREAD | IWRITE, bool tryDowngrade = false) { return this->open(path, (int)mode, (int)perms, tryDowngrade); }
119  FileDesc *open(const char *path, int mode, int perms = IREAD | IWRITE, bool tryDowngrade = false);
120 
125  void close(FileDesc *file);
126 
129  virtual void flush();
130  virtual long resourceConsumption();
131 
137  static SWBuf getEnvValue(const char *variableName);
138 
146  static bool hasAccess(const char *path, int mode);
147 
152  static signed char existsFile(const char *ipath, const char *ifileName = 0);
153 
158  static signed char existsDir(const char *ipath, const char *idirName = 0);
159 
169  static std::vector<struct DirEntry> getDirList(const char *dirPath, bool includeSize = false, bool includeIsDirectory = true);
170 
171 
176  signed char trunc(FileDesc *file);
177 
178  static char isDirectory(const char *path);
179  static long getFileSize(const char *path);
180  static int createParent(const char *pName);
181  static int createPathAndFile(const char *fName);
182 
187  static int openFile(const char *fName, int mode, int perms);
188  static int openFileReadOnly(const char *fName);
189  static void closeFile(int fd);
190  static long write(int fd, const void *buf, long count);
191 
192  static int copyFile(const char *srcFile, const char *destFile);
193  static int copyDir(const char *srcDir, const char *destDir);
194  static int removeDir(const char *targetDir);
195  static int removeFile(const char *fName);
196  static char getLine(FileDesc *fDesc, SWBuf &line);
197 
208  SWBuf getHomeDir();
209 
210 };
211 
216 
217  friend class FileMgr;
218 
219  long offset;
220  int fd; // -77 closed;
223 
224  FileDesc(FileMgr * parent, const char *path, int mode, int perms, bool tryDowngrade);
225  virtual ~FileDesc();
226 
227 public:
231  inline int getFd() {
232  if (fd == -77)
233  fd = parent->sysOpen(this);
234 // if ((fd < -1) && (fd != -77)) // kludge to handle ce
235 // return 777;
236  return fd;
237  }
238 
239  long seek(long offset, int whence);
240  long read(void *buf, long count);
241  long write(const void *buf, long count);
242 
245  char *path;
248  int mode;
251  int perms;
255 };
256 
257 
259 #endif
bool isDirectory
Definition: filemgr.h:43
#define SWORD_NAMESPACE_START
Definition: defs.h:39
Definition: swbuf.h:47
unsigned long size
Definition: filemgr.h:42
#define SWDLLEXPORT
Definition: defs.h:171
static unsigned int RDONLY
Definition: filemgr.h:75
int fd
Definition: filemgr.h:220
int perms
Definition: filemgr.h:251
static unsigned int RDWR
Definition: filemgr.h:76
static unsigned int TRUNC
Definition: filemgr.h:74
int mode
Definition: filemgr.h:248
FileDesc * open(const char *path, unsigned int mode, unsigned int perms=IREAD|IWRITE, bool tryDowngrade=false)
Definition: filemgr.h:118
FileDesc * open(const char *path, unsigned int mode, bool tryDowngrade)
Definition: filemgr.h:108
int getFd()
Definition: filemgr.h:231
char * path
Definition: filemgr.h:245
int maxFiles
Definition: filemgr.h:85
virtual long resourceConsumption()
Definition: swcacher.cpp:42
SWBuf name
Definition: filemgr.h:41
static unsigned int APPEND
Definition: filemgr.h:73
SWORD_NAMESPACE_START class SWDLLEXPORT FileDesc
Definition: filemgr.h:37
long offset
Definition: filemgr.h:219
static long write(int fd, const void *buf, long count)
Definition: filemgr.cpp:115
static unsigned int CREAT
Definition: filemgr.h:72
virtual void flush()
Definition: swcacher.cpp:39
static FileMgr * systemFileMgr
Definition: filemgr.h:70
static unsigned int IWRITE
Definition: filemgr.h:79
static unsigned int WRONLY
Definition: filemgr.h:77
FileDesc * files
Definition: filemgr.h:67
static unsigned int IREAD
Definition: filemgr.h:78
FileDesc * next
Definition: filemgr.h:222
#define SWORD_NAMESPACE_END
Definition: defs.h:40
FileMgr * parent
Definition: filemgr.h:221
bool tryDowngrade
Definition: filemgr.h:254