The SWORD Project  1.9.0.svnversion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
FileMgr Class Reference

#include <filemgr.h>

+ Inheritance diagram for FileMgr:
+ Collaboration diagram for FileMgr:

Public Member Functions

void close (FileDesc *file)
 
 FileMgr (int maxFiles=35)
 
virtual void flush ()
 
SWBuf getHomeDir ()
 
virtual long lastAccess ()
 
FileDescopen (const char *path, int mode, bool tryDowngrade)
 
FileDescopen (const char *path, unsigned int mode, bool tryDowngrade)
 
FileDescopen (const char *path, unsigned int mode, unsigned int perms=IREAD|IWRITE, bool tryDowngrade=false)
 
FileDescopen (const char *path, int mode, int perms=IREAD|IWRITE, bool tryDowngrade=false)
 
virtual long resourceConsumption ()
 
signed char trunc (FileDesc *file)
 
 ~FileMgr ()
 

Static Public Member Functions

static void closeFile (int fd)
 
static int copyDir (const char *srcDir, const char *destDir)
 
static int copyFile (const char *srcFile, const char *destFile)
 
static int createParent (const char *pName)
 
static int createPathAndFile (const char *fName)
 
static signed char existsDir (const char *ipath, const char *idirName=0)
 
static signed char existsFile (const char *ipath, const char *ifileName=0)
 
static std::vector< struct
DirEntry
getDirList (const char *dirPath, bool includeSize=false, bool includeIsDirectory=true)
 
static SWBuf getEnvValue (const char *variableName)
 
static long getFileSize (const char *path)
 
static char getLine (FileDesc *fDesc, SWBuf &line)
 
static FileMgrgetSystemFileMgr ()
 
static bool hasAccess (const char *path, int mode)
 
static char isDirectory (const char *path)
 
static int openFile (const char *fName, int mode, int perms)
 
static int openFileReadOnly (const char *fName)
 
static int removeDir (const char *targetDir)
 
static int removeFile (const char *fName)
 
static void setSystemFileMgr (FileMgr *newFileMgr)
 
static long write (int fd, const void *buf, long count)
 

Public Attributes

int maxFiles
 

Static Public Attributes

static unsigned int APPEND = O_APPEND
 
static unsigned int CREAT = O_CREAT
 
static unsigned int IREAD = S_IREAD
 
static unsigned int IWRITE = S_IWRITE
 
static unsigned int RDONLY = O_RDONLY
 
static unsigned int RDWR = O_RDWR
 
static unsigned int TRUNC = O_TRUNC
 
static unsigned int WRONLY = O_WRONLY
 

Static Protected Attributes

static FileMgrsystemFileMgr = 0
 

Private Member Functions

int sysOpen (FileDesc *file)
 

Private Attributes

FileDescfiles
 

Friends

class __staticsystemFileMgr
 
class FileDesc
 

Detailed Description

This class isolates all file io for SWORD, making OS level quirks easier to fix. This class is typically copied and replaced if necessary to get SWORD to run on a specific platform (e.g., Windows Mobile), but in the future, statics should be removed to make possible to instead simply subclass and override necessary methods.

This class also provides many convenience methods which make working with data storage easier.

Conceptually, this factory exposes an interface which allows SWORD to 'open' every file it wants, without worrying about OS limits, and takes care of opening and closing the actual file descriptors when necessary.

Definition at line 62 of file filemgr.h.

Constructor & Destructor Documentation

FileMgr::FileMgr ( int  maxFiles = 35)

Constructor.

Parameters
maxFilesThe number of files that this FileMgr may open in parallel, if necessary.

Definition at line 158 of file filemgr.cpp.

158  {
159  this->maxFiles = maxFiles; // must be at least 2
160  files = 0;
161 }
int maxFiles
Definition: filemgr.h:85
FileDesc * files
Definition: filemgr.h:67
FileMgr::~FileMgr ( )

Destructor. Clean things up. Will close all files opened by this FileMgr object.

Definition at line 164 of file filemgr.cpp.

164  {
165  FileDesc *tmp;
166 
167  while(files) {
168  tmp = files->next;
169  delete files;
170  files = tmp;
171  }
172 }
FileDesc * files
Definition: filemgr.h:67
FileDesc * next
Definition: filemgr.h:222

Member Function Documentation

void FileMgr::close ( FileDesc file)

Close a given file and delete its FileDesc object. Will only close the file if it was created by this FileMgr object.

Parameters
fileThe file to close.

Definition at line 196 of file filemgr.cpp.

196  {
197  FileDesc **loop;
198 
199  for (loop = &files; *loop; loop = &((*loop)->next)) {
200  if (*loop == file) {
201  *loop = (*loop)->next;
202  delete file;
203  break;
204  }
205  }
206 }
FileDesc * files
Definition: filemgr.h:67
FileDesc * next
Definition: filemgr.h:222
void FileMgr::closeFile ( int  fd)
static

Definition at line 491 of file filemgr.cpp.

491  {
492  ::close(fd);
493 }
void close(FileDesc *file)
Definition: filemgr.cpp:196
int FileMgr::copyDir ( const char *  srcDir,
const char *  destDir 
)
static

Definition at line 618 of file filemgr.cpp.

618  {
619  SWBuf baseSrcPath = srcDir;
620  if (!baseSrcPath.endsWith("/") && !baseSrcPath.endsWith("\\")) baseSrcPath += "/";
621  SWBuf baseDestPath = destDir;
622  if (!baseDestPath.endsWith("/") && !baseDestPath.endsWith("\\")) baseDestPath += "/";
623  int retVal = 0;
624  std::vector<DirEntry> dirList = getDirList(srcDir);
625  for (unsigned int i = 0; i < dirList.size() && !retVal; ++i) {
626  SWBuf srcPath = baseSrcPath + dirList[i].name;
627  SWBuf destPath = baseDestPath + dirList[i].name;
628  if (!dirList[i].isDirectory) {
629  retVal = copyFile(srcPath.c_str(), destPath.c_str());
630  }
631  else {
632  retVal = copyDir(srcPath.c_str(), destPath.c_str());
633  }
634  }
635  return retVal;
636 }
Definition: swbuf.h:47
bool endsWith(const SWBuf &postfix) const
Definition: swbuf.h:501
const char * c_str() const
Definition: swbuf.h:158
static int copyFile(const char *srcFile, const char *destFile)
Definition: filemgr.cpp:496
static std::vector< struct DirEntry > getDirList(const char *dirPath, bool includeSize=false, bool includeIsDirectory=true)
Definition: filemgr.cpp:379
static int copyDir(const char *srcDir, const char *destDir)
Definition: filemgr.cpp:618
static char isDirectory(const char *path)
Definition: filemgr.cpp:592
int FileMgr::copyFile ( const char *  srcFile,
const char *  destFile 
)
static

Definition at line 496 of file filemgr.cpp.

496  {
497  int sfd, dfd, len;
498  char buf[4096];
499 
500  if ((sfd = openFile(sourceFile, O_RDONLY|O_BINARY, S_IREAD|S_IWRITE|S_IRGRP|S_IROTH)) < 1)
501  return -1;
502  if ((dfd = createPathAndFile(targetFile)) < 1)
503  return -1;
504 
505  do {
506  len = (int)read(sfd, buf, 4096);
507  if (write(dfd, buf, len) != len) break;
508  }
509  while(len == 4096);
510  ::close(dfd);
511  ::close(sfd);
512 
513  return 0;
514 }
#define S_IRGRP
Definition: filemgr.cpp:52
#define S_IREAD
Definition: filemgr.cpp:70
#define O_BINARY
Definition: filemgr.cpp:48
void close(FileDesc *file)
Definition: filemgr.cpp:196
#define S_IROTH
Definition: filemgr.cpp:56
static long write(int fd, const void *buf, long count)
Definition: filemgr.cpp:115
static int createPathAndFile(const char *fName)
Definition: filemgr.cpp:479
#define S_IWRITE
Definition: filemgr.cpp:73
static int openFile(const char *fName, int mode, int perms)
Definition: filemgr.cpp:463
int FileMgr::createParent ( const char *  pName)
static

Definition at line 426 of file filemgr.cpp.

426  {
427  char *buf = new char [ strlen(pName) + 1 ];
428  int retCode = 0;
429 
430  strcpy(buf, pName);
431  int end = (int)strlen(buf) - 1;
432  while (end) {
433  if ((buf[end] == '/') || (buf[end] == '\\'))
434  break;
435  end--;
436  }
437  buf[end] = 0;
438  if (strlen(buf)>0) {
439  if (!hasAccess(buf, 02)) { // not exists with write access?
440  retCode =
441 #ifndef WIN32
442  mkdir(buf, 0755);
443 #else
444  _wmkdir((const wchar_t *)utf8ToWChar(buf).getRawData());
445 #endif
446  if (retCode) {
447  createParent(buf);
448  retCode =
449 #ifndef WIN32
450  mkdir(buf, 0755);
451 #else
452  _wmkdir((const wchar_t *)utf8ToWChar(buf).getRawData());
453 #endif
454  }
455  }
456  }
457  else retCode = -1;
458  delete [] buf;
459  return retCode;
460 }
SWBuf utf8ToWChar(const char *buf)
Definition: utilstr.cpp:239
static bool hasAccess(const char *path, int mode)
Definition: filemgr.cpp:327
static int createParent(const char *pName)
Definition: filemgr.cpp:426
int FileMgr::createPathAndFile ( const char *  fName)
static

Definition at line 479 of file filemgr.cpp.

479  {
480  int fd;
481 
482  fd = openFile(fName, O_CREAT|O_WRONLY|O_BINARY, S_IREAD|S_IWRITE|S_IRGRP|S_IROTH);
483  if (fd < 1) {
484  createParent(fName);
485  fd = openFile(fName, O_CREAT|O_WRONLY|O_BINARY, S_IREAD|S_IWRITE|S_IRGRP|S_IROTH);
486  }
487  return fd;
488 }
#define S_IRGRP
Definition: filemgr.cpp:52
#define S_IREAD
Definition: filemgr.cpp:70
#define O_BINARY
Definition: filemgr.cpp:48
#define S_IROTH
Definition: filemgr.cpp:56
static int createParent(const char *pName)
Definition: filemgr.cpp:426
#define S_IWRITE
Definition: filemgr.cpp:73
static int openFile(const char *fName, int mode, int perms)
Definition: filemgr.cpp:463
signed char FileMgr::existsDir ( const char *  ipath,
const char *  idirName = 0 
)
static

Checks for the existence and readability of a directory.

Parameters
ipathPath to directory.
idirNameName of directory to check for.

Definition at line 357 of file filemgr.cpp.

358 {
359  char *ch;
360  int len = (int)strlen(ipath) + ((idirName)?strlen(idirName):0) + 1;
361  if (idirName)
362  len += strlen(idirName);
363  char *path = new char [ len ];
364  strcpy(path, ipath);
365 
366  if ((path[strlen(path)-1] == '\\') || (path[strlen(path)-1] == '/'))
367  path[strlen(path)-1] = 0;
368 
369  if (idirName) {
370  ch = path + strlen(path);
371  sprintf(ch, "/%s", idirName);
372  }
373  signed char retVal = hasAccess(path, 04) ? 1 : 0;
374  delete [] path;
375  return retVal;
376 }
static bool hasAccess(const char *path, int mode)
Definition: filemgr.cpp:327
signed char FileMgr::existsFile ( const char *  ipath,
const char *  ifileName = 0 
)
static

Checks for the existence and readability of a file.

Parameters
ipathPath to file.
ifileNameName of file to check for.

Definition at line 337 of file filemgr.cpp.

338 {
339  int len = (int)strlen(ipath) + ((ifileName)?strlen(ifileName):0) + 3;
340  char *ch;
341  char *path = new char [ len ];
342  strcpy(path, ipath);
343 
344  if ((path[strlen(path)-1] == '\\') || (path[strlen(path)-1] == '/'))
345  path[strlen(path)-1] = 0;
346 
347  if (ifileName) {
348  ch = path + strlen(path);
349  sprintf(ch, "/%s", ifileName);
350  }
351  signed char retVal = hasAccess(path, 04) ? 1 : 0;
352  delete [] path;
353  return retVal;
354 }
static bool hasAccess(const char *path, int mode)
Definition: filemgr.cpp:327
void FileMgr::flush ( )
virtual

Cacher methods overridden

Reimplemented from SWCacher.

Definition at line 657 of file filemgr.cpp.

657  {
658  FileDesc **loop;
659 
660  for (loop = &files; *loop; loop = &((*loop)->next)) {
661  if ((*loop)->fd > 0) {
662  (*loop)->offset = lseek((*loop)->fd, 0, SEEK_CUR);
663  ::close((*loop)->fd);
664  (*loop)->fd = -77;
665  }
666  }
667 }
#define SEEK_CUR
Definition: zconf.h:245
void close(FileDesc *file)
Definition: filemgr.cpp:196
long offset
Definition: filemgr.h:219
FileDesc * files
Definition: filemgr.h:67
std::vector< struct DirEntry > FileMgr::getDirList ( const char *  dirPath,
bool  includeSize = false,
bool  includeIsDirectory = true 
)
static

Given a directory path, returns contents of directory

Parameters
dirPathPath to directory
includeSizeOptimization flag to allow passing false to skip file size lookup (true forces both size and directory lookup)
includeIsDirectoryOptimization flag to allow passing false to skip isDirectory lookup
Returns
a container of DirEntry records describing contents

Definition at line 379 of file filemgr.cpp.

379  {
380  std::vector<struct DirEntry> dirList;
381  SWBuf basePath = dirPath;
382  if (!basePath.endsWith("/") && !basePath.endsWith("\\")) basePath += "/";
383 
384 #ifndef WIN32
385  DIR *dir;
386  struct dirent *ent;
387  if ((dir = opendir(dirPath))) {
388  rewinddir(dir);
389  while ((ent = readdir(dir))) {
390  if ((strcmp(ent->d_name, ".")) && (strcmp(ent->d_name, ".."))) {
391  struct DirEntry i;
392  i.name = ent->d_name;
393  if (includeIsDirectory || includeSize) i.isDirectory = FileMgr::isDirectory(basePath + ent->d_name);
394  if (!i.isDirectory && includeSize) i.size = FileMgr::getFileSize(basePath + ent->d_name);
395  dirList.push_back(i);
396  }
397  }
398  closedir(dir);
399  }
400 
401 #else
402  // Crappy Windows-specific code because well... They can't be conformant
403  WIN32_FIND_DATAW fileData;
404  SWBuf wcharBuf = utf8ToWChar(basePath+"*");
405  const wchar_t *wcharPath = (const wchar_t *)wcharBuf.getRawData();
406  HANDLE findIterator = FindFirstFileW(wcharPath, &fileData);
407  if (findIterator != INVALID_HANDLE_VALUE) {
408  do {
409  SWBuf dirEntName = wcharToUTF8(fileData.cFileName);
410  if (dirEntName != "." && dirEntName != "..") {
411  struct DirEntry i;
412  i.name = dirEntName;
413  i.isDirectory = fileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
414  if (!i.isDirectory && includeSize) i.size = FileMgr::getFileSize(basePath + i.name);
415  dirList.push_back(i);
416  }
417  } while (FindNextFileW(findIterator, &fileData) != 0);
418  FindClose(findIterator);
419  }
420 #endif
421 
422  return dirList;
423 }
Definition: swbuf.h:47
SWBuf wcharToUTF8(const wchar_t *buf)
Definition: utilstr.cpp:263
SWBuf utf8ToWChar(const char *buf)
Definition: utilstr.cpp:239
Definition: dirent.h:24
bool endsWith(const SWBuf &postfix) const
Definition: swbuf.h:501
Definition: dirent.cpp:27
char * getRawData()
Definition: swbuf.h:379
SWDLLEXPORT void rewinddir(DIR *dir)
Definition: dirent.cpp:119
SWBuf name
Definition: filemgr.h:41
char * d_name
Definition: dirent.h:26
SWDLLEXPORT DIR * opendir(const char *name)
Definition: dirent.cpp:35
SWDLLEXPORT struct dirent * readdir(DIR *dir)
Definition: dirent.cpp:99
unsigned long size() const
Definition: swbuf.h:185
SWDLLEXPORT int closedir(DIR *dir)
Definition: dirent.cpp:76
static long getFileSize(const char *path)
Definition: filemgr.cpp:605
static char isDirectory(const char *path)
Definition: filemgr.cpp:592
SWBuf FileMgr::getEnvValue ( const char *  variableName)
static

Get an environment variable from the OS

Parameters
variableNamethe name of the env variable to retrieve
Returns
variable value from the OS

Definition at line 316 of file filemgr.cpp.

316  {
317  return
318 #ifdef WIN32
319  wcharToUTF8(_wgetenv((const wchar_t *)utf8ToWChar(variableName).getRawData()));
320 #else
321  getenv(variableName);
322 #endif
323 
324 }
SWBuf wcharToUTF8(const wchar_t *buf)
Definition: utilstr.cpp:263
SWBuf utf8ToWChar(const char *buf)
Definition: utilstr.cpp:239
long FileMgr::getFileSize ( const char *  path)
static

Definition at line 605 of file filemgr.cpp.

605  {
606 #ifndef WIN32
607  struct stat stats;
608  int error = stat(path, &stats);
609 #else
610  struct _stat stats;
611  int error = _wstat((const wchar_t *)utf8ToWChar(path).getRawData(), &stats);
612 #endif
613  if (error) return 0;
614  return stats.st_size;
615 }
SWBuf utf8ToWChar(const char *buf)
Definition: utilstr.cpp:239
SWBuf FileMgr::getHomeDir ( )

Determines where SWORD looks for the user's home folder. This is typically used as a place to find any additional personal SWORD modules which a user might wish to be added to a system-wide library (e.g., added from ~/.sword/mods.d/ or ~/sword/mods.d/)

or if a user or UI wishes to override SWORD system configuration settings (e.g., /etc/sword.conf) with a custom configuration (e.g., ~/.sword/sword.conf)

Definition at line 681 of file filemgr.cpp.

681  {
682 
683  // figure out 'home' directory for app data
684  SWBuf homeDir = getEnvValue("HOME");
685  if (!homeDir.length()) {
686  // silly windows
687  homeDir = getEnvValue("APPDATA");
688  }
689  if (homeDir.length()) {
690  if ((homeDir[homeDir.length()-1] != '\\') && (homeDir[homeDir.length()-1] != '/')) {
691  homeDir += "/";
692  }
693  }
694 
695  return homeDir;
696 }
Definition: swbuf.h:47
unsigned long length() const
Definition: swbuf.h:197
static SWBuf getEnvValue(const char *variableName)
Definition: filemgr.cpp:316
char FileMgr::getLine ( FileDesc fDesc,
SWBuf line 
)
static

Definition at line 527 of file filemgr.cpp.

527  {
528  int len = 0;
529  bool more = true;
530  char chunk[255];
531 
532  line = "";
533 
534  // assert we have a valid file handle
535  if (fDesc->getFd() < 1)
536  return 0;
537 
538  while (more) {
539  more = false;
540  long index = fDesc->seek(0, SEEK_CUR);
541  len = (int)fDesc->read(chunk, 254);
542 
543  // assert we have a readable file (not a directory)
544  if (len < 1)
545  break;
546 
547  int start = 0;
548  // clean up any preceding white space if we're at the beginning of line
549  if (!line.length()) {
550  for (;start < len; start++) {
551  if ((chunk[start] != 13) && (chunk[start] != ' ') && (chunk[start] != '\t'))
552  break;
553  }
554  }
555 
556  // find the end
557  int end;
558  for (end = start; ((end < (len-1)) && (chunk[end] != 10)); end++);
559 
560  if ((chunk[end] != 10) && (len == 254)) {
561  more = true;
562  }
563  index += (end + 1);
564 
565  // reposition to next valid place to read
566  fDesc->seek(index, SEEK_SET);
567 
568  // clean up any trailing junk on line if we're at the end
569  if (!more) {
570  for (; end > start; end--) {
571  if ((chunk[end] != 10) && (chunk[end] != 13) && (chunk[end] != ' ') && (chunk[end] != '\t')) {
572  if (chunk[end] == '\\') {
573  more = true;
574  end--;
575  }
576  break;
577  }
578  }
579  }
580 
581  int size = (end - start) + 1;
582 
583  if (size > 0) {
584  // line.appendFormatted("%.*s", size, chunk+start);
585  line.append(chunk+start, size);
586  }
587  }
588  return ((len > 0) || line.length());
589 }
long seek(long offset, int whence)
Definition: filemgr.cpp:143
unsigned long length() const
Definition: swbuf.h:197
#define SEEK_CUR
Definition: zconf.h:245
int getFd()
Definition: filemgr.h:231
SWBuf & append(const char *str, long max=-1)
Definition: swbuf.h:274
#define SEEK_SET
Definition: zconf.h:244
int size
Definition: regex.c:5043
long read(void *buf, long count)
Definition: filemgr.cpp:148
FileMgr * FileMgr::getSystemFileMgr ( )
static

Definition at line 101 of file filemgr.cpp.

101  {
102  if (!systemFileMgr)
103  systemFileMgr = new FileMgr();
104 
105  return systemFileMgr;
106 }
static FileMgr * systemFileMgr
Definition: filemgr.h:70
FileMgr(int maxFiles=35)
Definition: filemgr.cpp:158
bool FileMgr::hasAccess ( const char *  path,
int  mode 
)
static

Check if a path can be access with supplied permissions

Parameters
pathPath to the resource
modeDesired access mode
Returns
whether or not the resource can be accessed with the requested mode

Definition at line 327 of file filemgr.cpp.

327  {
328  return
329 #ifdef WIN32
330  !_waccess((const wchar_t *)utf8ToWChar(path).getRawData(), mode);
331 #else
332  !access(path, mode);
333 #endif
334 }
SWBuf utf8ToWChar(const char *buf)
Definition: utilstr.cpp:239
char FileMgr::isDirectory ( const char *  path)
static

Definition at line 592 of file filemgr.cpp.

592  {
593 #ifndef WIN32
594  struct stat stats;
595  int error = stat(path, &stats);
596 #else
597  struct _stat stats;
598  int error = _wstat((const wchar_t *)utf8ToWChar(path).getRawData(), &stats);
599 #endif
600  if (error) return 0;
601  return ((stats.st_mode & S_IFDIR) == S_IFDIR);
602 }
SWBuf utf8ToWChar(const char *buf)
Definition: utilstr.cpp:239
long SWCacher::lastAccess ( )
virtualinherited

Definition at line 46 of file swcacher.cpp.

46  {
47  return 0;
48 }
FileDesc * FileMgr::open ( const char *  path,
int  mode,
bool  tryDowngrade 
)

Open a file and return a FileDesc for it. The file itself will only be opened when FileDesc::getFd() is called.

Parameters
pathFilename.
modeFile access mode.
tryDowngradeif we can't open the file for permissions requested, try to open the file with less permissions
Returns
FileDesc object for the requested file.

Definition at line 175 of file filemgr.cpp.

175  {
176  return open(path, mode, S_IREAD|S_IWRITE|S_IRGRP|S_IROTH, tryDowngrade);
177 }
FileDesc * open(const char *path, int mode, bool tryDowngrade)
Definition: filemgr.cpp:175
#define S_IRGRP
Definition: filemgr.cpp:52
#define S_IREAD
Definition: filemgr.cpp:70
#define S_IROTH
Definition: filemgr.cpp:56
#define S_IWRITE
Definition: filemgr.cpp:73
FileDesc* FileMgr::open ( const char *  path,
unsigned int  mode,
bool  tryDowngrade 
)
inline

Definition at line 108 of file filemgr.h.

108 { return this->open(path, (int)mode, tryDowngrade); }
FileDesc * open(const char *path, int mode, bool tryDowngrade)
Definition: filemgr.cpp:175
FileDesc* FileMgr::open ( const char *  path,
unsigned int  mode,
unsigned int  perms = IREAD | IWRITE,
bool  tryDowngrade = false 
)
inline

Open a file and return a FileDesc for it. The file itself will only be opened when FileDesc::getFd() is called.

Parameters
pathFilename.
modeFile access mode.
permsPermissions.
tryDowngrade
Returns
FileDesc object for the requested file.

Definition at line 118 of file filemgr.h.

118 { return this->open(path, (int)mode, (int)perms, tryDowngrade); }
FileDesc * open(const char *path, int mode, bool tryDowngrade)
Definition: filemgr.cpp:175
FileDesc * FileMgr::open ( const char *  path,
int  mode,
int  perms = IREAD | IWRITE,
bool  tryDowngrade = false 
)

Definition at line 180 of file filemgr.cpp.

180  {
181  FileDesc **tmp, *tmp2;
182 
183  for (tmp = &files; *tmp; tmp = &((*tmp)->next)) {
184  if ((*tmp)->fd < 0) // insert as first non-system_open file
185  break;
186  }
187 
188  tmp2 = new FileDesc(this, path, mode, perms, tryDowngrade);
189  tmp2->next = *tmp;
190  *tmp = tmp2;
191 
192  return tmp2;
193 }
friend class FileDesc
Definition: filemgr.h:64
FileDesc * files
Definition: filemgr.h:67
FileDesc * next
Definition: filemgr.h:222
int FileMgr::openFile ( const char *  fName,
int  mode,
int  perms 
)
static

attempts to open a file readonly

Parameters
fNamefilename to open
Returns
fd; < 0 = error

Definition at line 463 of file filemgr.cpp.

463  {
464  int fd =
465 #ifndef WIN32
466  ::open(fName, mode, perms);
467 #else
468  ::_wopen((const wchar_t *)utf8ToWChar(fName).getRawData(), mode, perms);
469 #endif
470  return fd;
471 }
FileDesc * open(const char *path, int mode, bool tryDowngrade)
Definition: filemgr.cpp:175
SWBuf utf8ToWChar(const char *buf)
Definition: utilstr.cpp:239
int FileMgr::openFileReadOnly ( const char *  fName)
static

Definition at line 474 of file filemgr.cpp.

474  {
475  return openFile(fName, O_RDONLY|O_BINARY, S_IREAD|S_IWRITE|S_IRGRP|S_IROTH);
476 }
#define S_IRGRP
Definition: filemgr.cpp:52
#define S_IREAD
Definition: filemgr.cpp:70
#define O_BINARY
Definition: filemgr.cpp:48
#define S_IROTH
Definition: filemgr.cpp:56
#define S_IWRITE
Definition: filemgr.cpp:73
static int openFile(const char *fName, int mode, int perms)
Definition: filemgr.cpp:463
int FileMgr::removeDir ( const char *  targetDir)
static

Definition at line 639 of file filemgr.cpp.

639  {
640  SWBuf basePath = targetDir;
641  if (!basePath.endsWith("/") && !basePath.endsWith("\\")) basePath += "/";
642  std::vector<DirEntry> dirList = getDirList(targetDir);
643  for (unsigned int i = 0; i < dirList.size(); ++i) {
644  SWBuf targetPath = basePath + dirList[i].name;
645  if (!dirList[i].isDirectory) {
646  FileMgr::removeFile(targetPath.c_str());
647  }
648  else {
649  FileMgr::removeDir(targetPath.c_str());
650  }
651  }
652  FileMgr::removeFile(targetDir);
653  return 0;
654 }
static int removeDir(const char *targetDir)
Definition: filemgr.cpp:639
Definition: swbuf.h:47
bool endsWith(const SWBuf &postfix) const
Definition: swbuf.h:501
const char * c_str() const
Definition: swbuf.h:158
static int removeFile(const char *fName)
Definition: filemgr.cpp:517
static std::vector< struct DirEntry > getDirList(const char *dirPath, bool includeSize=false, bool includeIsDirectory=true)
Definition: filemgr.cpp:379
static char isDirectory(const char *path)
Definition: filemgr.cpp:592
int FileMgr::removeFile ( const char *  fName)
static

Definition at line 517 of file filemgr.cpp.

517  {
518  return
519 #ifndef WIN32
520  ::remove(fName);
521 #else
522  ::_wremove((const wchar_t *)utf8ToWChar(fName).getRawData());
523 #endif
524 }
SWBuf utf8ToWChar(const char *buf)
Definition: utilstr.cpp:239
long FileMgr::resourceConsumption ( )
virtual

Reimplemented from SWCacher.

Definition at line 669 of file filemgr.cpp.

669  {
670  long count = 0;
671  FileDesc **loop;
672  for (loop = &files; *loop; loop = &((*loop)->next)) {
673  if ((*loop)->fd > 0) {
674  count++;
675  }
676  }
677  return count;
678 }
FileDesc * files
Definition: filemgr.h:67
void FileMgr::setSystemFileMgr ( FileMgr newFileMgr)
static

Definition at line 109 of file filemgr.cpp.

109  {
110  if (systemFileMgr)
111  delete systemFileMgr;
112  systemFileMgr = newFileMgr;
113 }
static FileMgr * systemFileMgr
Definition: filemgr.h:70
int FileMgr::sysOpen ( FileDesc file)
private

Definition at line 209 of file filemgr.cpp.

209  {
210  FileDesc **loop;
211  int openCount = 1; // because we are presently opening 1 file, and we need to be sure to close files to accomodate, if necessary
212 
213  for (loop = &files; *loop; loop = &((*loop)->next)) {
214 
215  if ((*loop)->fd > 0) {
216  if (++openCount > maxFiles) {
217  (*loop)->offset = lseek((*loop)->fd, 0, SEEK_CUR);
218  ::close((*loop)->fd);
219  (*loop)->fd = -77;
220  }
221  }
222 
223  if (*loop == file) {
224  if (*loop != files) {
225  *loop = (*loop)->next;
226  file->next = files;
227  files = file;
228  }
229  if ((hasAccess(file->path, 04)) || ((file->mode & O_CREAT) == O_CREAT)) { // check for at least file exists / read access before we try to open
230  char tries = (((file->mode & O_RDWR) == O_RDWR) && (file->tryDowngrade)) ? 2 : 1; // try read/write if possible
231  for (int i = 0; i < tries; i++) {
232  if (i > 0) {
233  file->mode = (file->mode & ~O_RDWR); // remove write access
234  file->mode = (file->mode | O_RDONLY);// add read access
235  }
236  file->fd = openFile(file->path, file->mode|O_BINARY, file->perms);
237  if (file->fd >= 0)
238  break;
239  }
240 
241  if (file->fd >= 0)
242  lseek(file->fd, file->offset, SEEK_SET);
243  }
244  else file->fd = -1;
245  if (!*loop)
246  break;
247  }
248  }
249  return file->fd;
250 }
#define SEEK_CUR
Definition: zconf.h:245
int fd
Definition: filemgr.h:220
int perms
Definition: filemgr.h:251
int mode
Definition: filemgr.h:248
#define O_BINARY
Definition: filemgr.cpp:48
char * path
Definition: filemgr.h:245
int maxFiles
Definition: filemgr.h:85
void close(FileDesc *file)
Definition: filemgr.cpp:196
static bool hasAccess(const char *path, int mode)
Definition: filemgr.cpp:327
long offset
Definition: filemgr.h:219
#define SEEK_SET
Definition: zconf.h:244
FileDesc * files
Definition: filemgr.h:67
static int openFile(const char *fName, int mode, int perms)
Definition: filemgr.cpp:463
FileDesc * next
Definition: filemgr.h:222
bool tryDowngrade
Definition: filemgr.h:254
signed char FileMgr::trunc ( FileDesc file)

Truncate a file at its current position leaving byte at current possition intact deleting everything afterward.

Parameters
fileThe file to operate on.

Definition at line 256 of file filemgr.cpp.

256  {
257 
258  static const char *writeTest = "x";
259  long size = file->seek(1, SEEK_CUR);
260  if (size == 1) // was empty
261  size = 0;
262  char nibble [ 32767 ];
263  bool writable = file->write(writeTest, 1);
264  int bytes = 0;
265 
266  if (writable) {
267  // get tmpfilename
268  char *buf = new char [ strlen(file->path) + 10 ];
269  int i;
270  for (i = 0; i < 9999; i++) {
271  sprintf(buf, "%stmp%.4d", file->path, i);
272  if (!existsFile(buf))
273  break;
274  }
275  if (i == 9999)
276  return -2;
277 
278  FileDesc *fd = open(buf, CREAT|RDWR);
279  if (!fd || fd->getFd() < 0)
280  return -3;
281 
282  file->seek(0, SEEK_SET);
283  while (size > 0) {
284  bytes = (int)file->read(nibble, 32767);
285  bytes = (bytes < size)?bytes:(int)size;
286  if (fd->write(nibble, bytes) != bytes) { break; }
287  size -= bytes;
288  }
289  if (size < 1) {
290  // zero out the file
291  ::close(file->fd);
292  file->fd = openFile(file->path, O_TRUNC, S_IREAD|S_IWRITE|S_IRGRP|S_IROTH);
293  ::close(file->fd);
294  file->fd = -77; // force file open by filemgr
295  // copy tmp file back (dumb, but must preserve file permissions)
296  fd->seek(0, SEEK_SET);
297  do {
298  bytes = fd->read(nibble, 32767);
299  file->write(nibble, bytes);
300  } while (bytes == 32767);
301  }
302 
303  close(fd);
304  ::close(file->fd);
305  removeFile(buf); // remove our tmp file
306  file->fd = -77; // causes file to be swapped out forcing open on next call to getFd()
307  }
308  else { // put offset back and return failure
309  file->seek(-1, SEEK_CUR);
310  return -1;
311  }
312  return 0;
313 }
static signed char existsFile(const char *ipath, const char *ifileName=0)
Definition: filemgr.cpp:337
long seek(long offset, int whence)
Definition: filemgr.cpp:143
FileDesc * open(const char *path, int mode, bool tryDowngrade)
Definition: filemgr.cpp:175
#define SEEK_CUR
Definition: zconf.h:245
#define S_IRGRP
Definition: filemgr.cpp:52
int fd
Definition: filemgr.h:220
static unsigned int RDWR
Definition: filemgr.h:76
#define S_IREAD
Definition: filemgr.cpp:70
int getFd()
Definition: filemgr.h:231
char * path
Definition: filemgr.h:245
long write(const void *buf, long count)
Definition: filemgr.cpp:153
void close(FileDesc *file)
Definition: filemgr.cpp:196
static int removeFile(const char *fName)
Definition: filemgr.cpp:517
#define S_IROTH
Definition: filemgr.cpp:56
#define SEEK_SET
Definition: zconf.h:244
int size
Definition: regex.c:5043
static unsigned int CREAT
Definition: filemgr.h:72
#define S_IWRITE
Definition: filemgr.cpp:73
long read(void *buf, long count)
Definition: filemgr.cpp:148
static int openFile(const char *fName, int mode, int perms)
Definition: filemgr.cpp:463
long FileMgr::write ( int  fd,
const void *  buf,
long  count 
)
static

Definition at line 115 of file filemgr.cpp.

115  {
116  return ::write(fd, buf, count);
117 }

Friends And Related Function Documentation

friend class __staticsystemFileMgr
friend

Definition at line 65 of file filemgr.h.

friend class FileDesc
friend

Definition at line 64 of file filemgr.h.

Member Data Documentation

unsigned int FileMgr::APPEND = O_APPEND
static

Definition at line 73 of file filemgr.h.

SWORD_NAMESPACE_START unsigned int FileMgr::CREAT = O_CREAT
static

Definition at line 72 of file filemgr.h.

FileDesc* FileMgr::files
private

Definition at line 67 of file filemgr.h.

unsigned int FileMgr::IREAD = S_IREAD
static

Definition at line 78 of file filemgr.h.

unsigned int FileMgr::IWRITE = S_IWRITE
static

Definition at line 79 of file filemgr.h.

int FileMgr::maxFiles

Maximum number of open files set in the constructor. determines the max number of real system files that filemgr will open. Adjust for tuning.

Definition at line 85 of file filemgr.h.

unsigned int FileMgr::RDONLY = O_RDONLY
static

Definition at line 75 of file filemgr.h.

unsigned int FileMgr::RDWR = O_RDWR
static

Definition at line 76 of file filemgr.h.

FileMgr * FileMgr::systemFileMgr = 0
staticprotected

Definition at line 70 of file filemgr.h.

unsigned int FileMgr::TRUNC = O_TRUNC
static

Definition at line 74 of file filemgr.h.

unsigned int FileMgr::WRONLY = O_WRONLY
static

Definition at line 77 of file filemgr.h.


The documentation for this class was generated from the following files: