ManaPlus
Functions
VirtFs::FsDir Namespace Reference

Functions

FileopenInternal (FsEntry *const entry, const std::string &filename, const char *mode)
 
FileopenRead (FsEntry *const entry, std::string filename)
 
FileopenWrite (FsEntry *const entry, const std::string &filename)
 
FileopenAppend (FsEntry *const entry, const std::string &filename)
 
void deinit ()
 
void init (const std::string &name)
 
void initFuncs (FsFuncs *const ptr)
 
FsFuncsgetFuncs ()
 
const char * getBaseDir ()
 
const char * getUserDir ()
 
bool getRealDir (FsEntry *const entry, std::string filename, std::string dirName, std::string &realDir)
 
bool exists (FsEntry *const entry, std::string fileName, std::string dirName)
 
void enumerate (FsEntry *const entry, std::string dirName, StringVect &names)
 
bool isDirectory (FsEntry *const entry, std::string dirName, bool &isDirFlag)
 
bool isSymbolicLink (std::string name)
 
void freeList (List *const handle)
 
bool setWriteDir (std::string newDir)
 
bool mkdir (std::string dirname)
 
bool remove (std::string filename)
 
void permitLinks (const bool val)
 
int close (File *const file)
 
int64_t read (File *const file, void *const buffer, const uint32_t objSize, const uint32_t objCount)
 
int64_t write (File *const file, const void *const buffer, const uint32_t objSize, const uint32_t objCount)
 
int64_t fileLength (File *const file)
 
int64_t tell (File *const file)
 
int seek (File *const file, const uint64_t pos)
 
int eof (File *const file)
 
const char * loadFile (FsEntry *const entry, std::string filename, int &fileSize)
 
void getFiles (FsEntry *const entry, std::string dirName, StringVect &names)
 
void getFilesWithDir (FsEntry *const entry, const std::string &dirName, StringVect &names)
 
void getDirs (FsEntry *const entry, std::string dirName, StringVect &names)
 
int32_t rwops_seek (SDL_RWops *const rw, const int32_t offset, const int whence)
 
int rwops_read (SDL_RWops *const rw, void *const ptr, const int size, const int maxnum)
 
int rwops_write (SDL_RWops *const rw, const void *const ptr, const int size, const int maxnum)
 
int rwops_close (SDL_RWops *const rw)
 

Function Documentation

◆ close()

int VirtFs::FsDir::close ( File *const  file)

Definition at line 312 of file fsdir.cpp.

313  {
314  if (file == nullptr)
315  return 0;
316  delete file;
317  return 1;
318  }

Referenced by initFuncs().

◆ deinit()

void VirtFs::FsDir::deinit ( )

Definition at line 101 of file fsdir.cpp.

102  {
103  }

Referenced by VirtFs::deinit().

◆ enumerate()

void VirtFs::FsDir::enumerate ( FsEntry *const  entry,
std::string  dirName,
StringVect names 
)

Definition at line 192 of file fsdir.cpp.

195  {
196  const std::string path = static_cast<DirEntry*>(entry)->rootSubDir +
197  dirName;
198  const dirent *next_file = nullptr;
199  DIR *const dir = opendir(path.c_str());
200  if (dir != nullptr)
201  {
202  while ((next_file = readdir(dir)) != nullptr)
203  {
204  const std::string file = next_file->d_name;
205  if (file == "." || file == "..")
206  continue;
207 #ifndef WIN32
208  if (mPermitLinks == false)
209  {
210  struct stat statbuf;
211  if (lstat(path.c_str(), &statbuf) == 0 &&
212  S_ISLNK(statbuf.st_mode) != 0)
213  {
214  continue;
215  }
216  }
217 #endif // WIN32
218 
219  bool found(false);
220  FOR_EACH (StringVectCIter, itn, names)
221  {
222  if (*itn == file)
223  {
224  found = true;
225  break;
226  }
227  }
228  if (found == false)
229  names.push_back(file);
230  }
231  closedir(dir);
232  }
233  }
#define FOR_EACH(type, iter, array)
Definition: foreach.h:25
StringVect::const_iterator StringVectCIter
Definition: stringvector.h:31

References FOR_EACH, and VirtFs::anonymous_namespace{fsdir.cpp}::mPermitLinks.

Referenced by initFuncs().

◆ eof()

int VirtFs::FsDir::eof ( File *const  file)

Definition at line 436 of file fsdir.cpp.

437  {
438  if (file == nullptr)
439  return -1;
440 
441  FILEHTYPE fd = file->mFd;
442  if (fd == FILEHDEFAULT)
443  {
444  reportAlways("FsDir::eof file not opened.")
445  return 0;
446  }
447 #ifdef USE_FILE_FOPEN
448  const int flag = feof(fd);
449  if (flag != 0)
450  return 1;
451  const int64_t pos = ftell(fd);
452  const int64_t len = fileLength(file);
453 #else // USE_FILE_FOPEN
454  const int64_t pos = lseek(fd, 0, SEEK_CUR);
455  struct stat statbuf;
456  if (fstat(fd, &statbuf) == -1)
457  {
458  reportAlways("FsDir::fileLength error.")
459  return -1;
460  }
461  const int64_t len = static_cast<int64_t>(statbuf.st_size);
462 #endif // USE_FILE_FOPEN
463  return static_cast<int>(pos < 0 || len < 0 || pos >= len);
464  }
#define reportAlways(...)
Definition: checkutils.h:253
#define FILEHDEFAULT
Definition: fileapi.h:30
#define FILEHTYPE
Definition: fileapi.h:28
int64_t fileLength(File *const file)
Definition: fsdir.cpp:368

References FILEHDEFAULT, FILEHTYPE, fileLength(), and reportAlways.

Referenced by initFuncs().

◆ exists()

bool VirtFs::FsDir::exists ( FsEntry *const  entry,
std::string  fileName,
std::string  dirName 
)

Definition at line 184 of file fsdir.cpp.

187  {
188  return Files::existsLocal(static_cast<DirEntry*>(entry)->rootSubDir +
189  fileName);
190  }
bool existsLocal(const std::string &path)
Definition: files.cpp:209
std::string fileName
Definition: testmain.cpp:39

References Files::existsLocal(), and fileName.

Referenced by initFuncs().

◆ fileLength()

int64_t VirtFs::FsDir::fileLength ( File *const  file)

Definition at line 368 of file fsdir.cpp.

369  {
370  if (file == nullptr)
371  return -1;
372  FILEHTYPE fd = file->mFd;
373  if (fd == FILEHDEFAULT)
374  {
375  reportAlways("FsDir::fileLength file not opened.")
376  return 0;
377  }
378 #ifdef USE_FILE_FOPEN
379  const long pos = ftell(fd);
380  if (pos < 0)
381  {
382  reportAlways("FsDir::fileLength ftell error.")
383  return -1;
384  }
385  fseek(fd, 0, SEEK_END);
386  const long sz = ftell(fd);
387  fseek(fd, pos, SEEK_SET);
388  return sz;
389 #else // USE_FILE_FOPEN
390  struct stat statbuf;
391  if (fstat(fd, &statbuf) == -1)
392  {
393  reportAlways("FsDir::fileLength error.")
394  return -1;
395  }
396  return static_cast<int64_t>(statbuf.st_size);
397 #endif // USE_FILE_FOPEN
398  }

References FILEHDEFAULT, FILEHTYPE, and reportAlways.

Referenced by eof(), and initFuncs().

◆ freeList()

void VirtFs::FsDir::freeList ( List *const  handle)

Definition at line 271 of file fsdir.cpp.

272  {
273  delete handle;
274  }

◆ getBaseDir()

const char * VirtFs::FsDir::getBaseDir ( )

Definition at line 160 of file fsdir.cpp.

161  {
162  return mBaseDir.c_str();
163  }

References VirtFs::anonymous_namespace{fsdir.cpp}::mBaseDir.

Referenced by VirtFs::getBaseDir().

◆ getDirs()

void VirtFs::FsDir::getDirs ( FsEntry *const  entry,
std::string  dirName,
StringVect names 
)

Definition at line 636 of file fsdir.cpp.

639  {
640  const std::string path = static_cast<DirEntry*>(entry)->rootSubDir +
641  dirName;
642  const dirent *next_file = nullptr;
643  DIR *const dir = opendir(path.c_str());
644  if (dir != nullptr)
645  {
646  while ((next_file = readdir(dir)) != nullptr)
647  {
648  struct stat statbuf;
649  const std::string file = next_file->d_name;
650  if (file == "." || file == "..")
651  continue;
652 #ifndef WIN32
653  if (mPermitLinks == false)
654  {
655  if (lstat(path.c_str(), &statbuf) == 0 &&
656  S_ISLNK(statbuf.st_mode) != 0)
657  {
658  continue;
659  }
660  }
661 #endif // WIN32
662 
663  const std::string filePath = pathJoin(path, file);
664  if (stat(filePath.c_str(), &statbuf) == 0)
665  {
666  if (S_ISDIR(statbuf.st_mode) == 0)
667  continue;
668  }
669 
670  bool found(false);
671  FOR_EACH (StringVectCIter, itn, names)
672  {
673  if (*itn == file)
674  {
675  found = true;
676  break;
677  }
678  }
679  if (found == false)
680  names.push_back(file);
681  }
682  closedir(dir);
683  }
684  }
std::string pathJoin(std::string str1, const std::string &str2)

References FOR_EACH, VirtFs::anonymous_namespace{fsdir.cpp}::mPermitLinks, and pathJoin().

Referenced by initFuncs().

◆ getFiles()

void VirtFs::FsDir::getFiles ( FsEntry *const  entry,
std::string  dirName,
StringVect names 
)

Definition at line 536 of file fsdir.cpp.

539  {
540  const std::string path = static_cast<DirEntry*>(entry)->rootSubDir +
541  dirName;
542  const dirent *next_file = nullptr;
543  DIR *const dir = opendir(path.c_str());
544  if (dir != nullptr)
545  {
546  while ((next_file = readdir(dir)) != nullptr)
547  {
548  struct stat statbuf;
549  const std::string file = next_file->d_name;
550  if (file == "." || file == "..")
551  continue;
552 #ifndef WIN32
553  if (mPermitLinks == false)
554  {
555  if (lstat(path.c_str(), &statbuf) == 0 &&
556  S_ISLNK(statbuf.st_mode) != 0)
557  {
558  continue;
559  }
560  }
561 #endif // WIN32
562 
563  const std::string filePath = pathJoin(path, file);
564  if (stat(filePath.c_str(), &statbuf) == 0)
565  {
566  if (S_ISDIR(statbuf.st_mode) != 0)
567  continue;
568  }
569 
570  bool found(false);
571  FOR_EACH (StringVectCIter, itn, names)
572  {
573  if (*itn == file)
574  {
575  found = true;
576  break;
577  }
578  }
579  if (found == false)
580  names.push_back(file);
581  }
582  closedir(dir);
583  }
584  }

References FOR_EACH, VirtFs::anonymous_namespace{fsdir.cpp}::mPermitLinks, and pathJoin().

Referenced by initFuncs().

◆ getFilesWithDir()

void VirtFs::FsDir::getFilesWithDir ( FsEntry *const  entry,
const std::string &  dirName,
StringVect names 
)

Definition at line 586 of file fsdir.cpp.

589  {
590  const std::string path = static_cast<DirEntry*>(entry)->rootSubDir +
591  dirName;
592  const dirent *next_file = nullptr;
593  DIR *const dir = opendir(path.c_str());
594  if (dir != nullptr)
595  {
596  while ((next_file = readdir(dir)) != nullptr)
597  {
598  struct stat statbuf;
599  const std::string file = next_file->d_name;
600  if (file == "." || file == "..")
601  continue;
602 #ifndef WIN32
603  if (mPermitLinks == false)
604  {
605  if (lstat(path.c_str(), &statbuf) == 0 &&
606  S_ISLNK(statbuf.st_mode) != 0)
607  {
608  continue;
609  }
610  }
611 #endif // WIN32
612 
613  const std::string filePath = pathJoin(path, file);
614  if (stat(filePath.c_str(), &statbuf) == 0)
615  {
616  if (S_ISDIR(statbuf.st_mode) != 0)
617  continue;
618  }
619 
620  bool found(false);
621  FOR_EACH (StringVectCIter, itn, names)
622  {
623  if (*itn == file)
624  {
625  found = true;
626  break;
627  }
628  }
629  if (found == false)
630  names.push_back(pathJoin(dirName, file));
631  }
632  closedir(dir);
633  }
634  }

References FOR_EACH, VirtFs::anonymous_namespace{fsdir.cpp}::mPermitLinks, and pathJoin().

Referenced by initFuncs().

◆ getFuncs()

FsFuncs * VirtFs::FsDir::getFuncs ( )

Definition at line 155 of file fsdir.cpp.

156  {
157  return &funcs;
158  }

References VirtFs::anonymous_namespace{fsdir.cpp}::funcs.

Referenced by VirtFs::mountDirInternal().

◆ getRealDir()

bool VirtFs::FsDir::getRealDir ( FsEntry *const  entry,
std::string  filename,
std::string  dirName,
std::string &  realDir 
)

Definition at line 170 of file fsdir.cpp.

174  {
175  const DirEntry *const dirEntry = static_cast<const DirEntry*>(entry);
176  if (Files::existsLocal(dirEntry->rootSubDir + filename))
177  {
178  realDir = dirEntry->userDir;
179  return true;
180  }
181  return false;
182  }

References Files::existsLocal(), VirtFs::DirEntry::rootSubDir, and VirtFs::DirEntry::userDir.

Referenced by initFuncs().

◆ getUserDir()

const char * VirtFs::FsDir::getUserDir ( )

Definition at line 165 of file fsdir.cpp.

166  {
167  return mUserDir.c_str();
168  }

References VirtFs::anonymous_namespace{fsdir.cpp}::mUserDir.

Referenced by VirtFs::getUserDir().

◆ init()

void VirtFs::FsDir::init ( const std::string &  name)

Definition at line 115 of file fsdir.cpp.

116  {
118 #endif // defined(__native_client__)
119 
121  mUserDir = getHomePath();
123  initFuncs(&funcs);
124  }
void initFuncs(FsFuncs *const ptr)
Definition: fsdir.cpp:126
std::string getRealPath(const std::string &str)
Definition: paths.cpp:84
std::string getHomePath()
Definition: paths.cpp:252
void prepareFsPath(std::string &path)
Definition: paths.cpp:132
std::string getFileDir(const std::string &path)

References VirtFs::anonymous_namespace{fsdir.cpp}::funcs, getFileDir(), getHomePath(), getRealPath(), initFuncs(), VirtFs::anonymous_namespace{fsdir.cpp}::mBaseDir, VirtFs::anonymous_namespace{fsdir.cpp}::mUserDir, and prepareFsPath().

Referenced by VirtFs::init().

◆ initFuncs()

void VirtFs::FsDir::initFuncs ( FsFuncs *const  ptr)

Definition at line 126 of file fsdir.cpp.

127  {
128  ptr->close = &FsDir::close;
129  ptr->read = &FsDir::read;
130  ptr->write = &FsDir::write;
131  ptr->fileLength = &FsDir::fileLength;
132  ptr->tell = &FsDir::tell;
133  ptr->seek = &FsDir::seek;
134  ptr->eof = &FsDir::eof;
135  ptr->exists = &FsDir::exists;
136  ptr->getRealDir = &FsDir::getRealDir;
137  ptr->enumerate = &FsDir::enumerate;
138  ptr->isDirectory = &FsDir::isDirectory;
139  ptr->openRead = &FsDir::openRead;
140  ptr->openWrite = &FsDir::openWrite;
141  ptr->openAppend = &FsDir::openAppend;
142  ptr->loadFile = &FsDir::loadFile;
143  ptr->getFiles = &FsDir::getFiles;
144  ptr->getFilesWithDir = &FsDir::getFilesWithDir;
145  ptr->getDirs = &FsDir::getDirs;
146  ptr->rwops_seek = &FsDir::rwops_seek;
147  ptr->rwops_read = &FsDir::rwops_read;
148  ptr->rwops_write = &FsDir::rwops_write;
149  ptr->rwops_close = &FsDir::rwops_close;
150 #ifdef USE_SDL2
151  ptr->rwops_size = &FsDir::rwops_size;
152 #endif // USE_SDL2
153  }
File * openRead(FsEntry *const entry, std::string filename)
Definition: fsdir.cpp:83
File * openAppend(FsEntry *const entry, const std::string &filename)
Definition: fsdir.cpp:95
bool exists(FsEntry *const entry, std::string fileName, std::string dirName)
Definition: fsdir.cpp:184
int rwops_write(SDL_RWops *const rw, const void *const ptr, const int size, const int maxnum)
Definition: fsdirrwops.cpp:196
int32_t rwops_seek(SDL_RWops *const rw, const int32_t offset, const int whence)
Definition: fsdirrwops.cpp:41
int rwops_close(SDL_RWops *const rw)
Definition: fsdirrwops.cpp:233
int close(File *const file)
Definition: fsdir.cpp:312
int eof(File *const file)
Definition: fsdir.cpp:436
void getFilesWithDir(FsEntry *const entry, const std::string &dirName, StringVect &names)
Definition: fsdir.cpp:586
File * openWrite(FsEntry *const entry, const std::string &filename)
Definition: fsdir.cpp:89
int64_t read(File *const file, void *const buffer, const uint32_t objSize, const uint32_t objCount)
Definition: fsdir.cpp:320
bool getRealDir(FsEntry *const entry, std::string filename, std::string dirName, std::string &realDir)
Definition: fsdir.cpp:170
void getDirs(FsEntry *const entry, std::string dirName, StringVect &names)
Definition: fsdir.cpp:636
const char * loadFile(FsEntry *const entry, std::string filename, int &fileSize)
Definition: fsdir.cpp:466
int seek(File *const file, const uint64_t pos)
Definition: fsdir.cpp:418
int rwops_read(SDL_RWops *const rw, void *const ptr, const int size, const int maxnum)
Definition: fsdirrwops.cpp:160
bool isDirectory(FsEntry *const entry, std::string dirName, bool &isDirFlag)
Definition: fsdir.cpp:235
void getFiles(FsEntry *const entry, std::string dirName, StringVect &names)
Definition: fsdir.cpp:536
void enumerate(FsEntry *const entry, std::string dirName, StringVect &names)
Definition: fsdir.cpp:192
int64_t tell(File *const file)
Definition: fsdir.cpp:400
int64_t write(File *const file, const void *const buffer, const uint32_t objSize, const uint32_t objCount)
Definition: fsdir.cpp:344

References close(), enumerate(), eof(), exists(), fileLength(), getDirs(), getFiles(), getFilesWithDir(), getRealDir(), isDirectory(), loadFile(), openAppend(), openRead(), openWrite(), read(), rwops_close(), rwops_read(), rwops_seek(), rwops_write(), seek(), tell(), and write().

Referenced by init().

◆ isDirectory()

bool VirtFs::FsDir::isDirectory ( FsEntry *const  entry,
std::string  dirName,
bool &  isDirFlag 
)

Definition at line 235 of file fsdir.cpp.

238  {
239  std::string path = static_cast<DirEntry*>(entry)->rootSubDir + dirName;
240 
241  struct stat statbuf;
242  if (stat(path.c_str(), &statbuf) == 0)
243  {
244  isDirFlag = (S_ISDIR(statbuf.st_mode) != 0);
245  return true;
246  }
247  return false;
248  }

Referenced by initFuncs().

◆ isSymbolicLink()

bool VirtFs::FsDir::isSymbolicLink ( std::string  name)

Definition at line 250 of file fsdir.cpp.

251  {
252  prepareFsPath(name);
253  if (checkPath(name) == false)
254  {
255  reportAlways("FsDir::isSymbolicLink invalid path: %s",
256  name.c_str())
257  return false;
258  }
259 #ifndef WIN32
260  if (mPermitLinks == false)
261  return false;
262 
263  struct stat statbuf;
264  return lstat(name.c_str(), &statbuf) == 0 &&
265  S_ISLNK(statbuf.st_mode) != 0;
266 #else
267  return false;
268 #endif // WIN32
269  }
bool checkPath(const std::string &path)
Definition: paths.cpp:121

References checkPath(), VirtFs::anonymous_namespace{fsdir.cpp}::mPermitLinks, prepareFsPath(), and reportAlways.

Referenced by VirtFs::isSymbolicLink().

◆ loadFile()

const char * VirtFs::FsDir::loadFile ( FsEntry *const  entry,
std::string  filename,
int &  fileSize 
)

Definition at line 466 of file fsdir.cpp.

469  {
470  const DirEntry *const dirEntry = static_cast<DirEntry*>(entry);
471  const std::string path = dirEntry->rootSubDir + filename;
472  if (Files::existsLocal(path) == false)
473  return nullptr;
474  FILEHTYPE fd = FILEOPEN(path.c_str(),
476  if (fd == FILEHDEFAULT)
477  {
478  reportAlways("VirtFs::loadFile file open error: %s",
479  filename.c_str())
480  return nullptr;
481  }
482 
483  logger->log("Loaded %s/%s",
484  dirEntry->userDir.c_str(),
485  filename.c_str());
486 
487 #ifdef USE_FILE_FOPEN
488  fseek(fd, 0, SEEK_END);
489  const long sz = ftell(fd);
490  if (sz < 0)
491  {
492  reportAlways("FsDir::fileLength ftell error.")
493  if (fd != FILEHDEFAULT)
494  FILECLOSE(fd);
495  return nullptr;
496  }
497  fseek(fd, 0, SEEK_SET);
498  fileSize = static_cast<int>(sz);
499 #else // USE_FILE_FOPEN
500  struct stat statbuf;
501  if (fstat(fd, &statbuf) == -1)
502  {
503  reportAlways("FsDir::fileLength error.")
504  if (fd != FILEHDEFAULT)
505  FILECLOSE(fd);
506  return nullptr;
507  }
508  fileSize = static_cast<int>(statbuf.st_size);
509 #endif // USE_FILE_FOPEN
510 
511  // Allocate memory and load the file
512  char *restrict const buffer = new char[CAST_SIZE(fileSize)];
513  if (fileSize > 0)
514  buffer[fileSize - 1] = 0;
515 
516 #ifdef USE_FILE_FOPEN
517  const int cnt = CAST_S32(fread(buffer, 1, fileSize, fd));
518 #else // USE_FILE_FOPEN
519  const int cnt = ::read(fd, buffer, fileSize);
520 #endif // USE_FILE_FOPEN
521 
522  if (cnt <= 0)
523  {
524  delete [] buffer;
525  if (fd != FILEHDEFAULT)
526  FILECLOSE(fd);
527  return nullptr;
528  }
529 
530  if (fd != FILEHDEFAULT)
531  FILECLOSE(fd);
532 
533  return buffer;
534  }
#define CAST_S32
Definition: cast.h:30
#define CAST_SIZE
Definition: cast.h:34
#define FILEOPEN(path, mode)
Definition: fileapi.h:31
#define FILEOPEN_FLAG_READ
Definition: fileapi.h:34
#define FILECLOSE
Definition: fileapi.h:32
if(!vert) return
#define restrict
Definition: localconsts.h:165
Logger * logger
Definition: logger.cpp:89

References CAST_S32, CAST_SIZE, Files::existsLocal(), FILECLOSE, FILEHDEFAULT, FILEHTYPE, FILEOPEN, FILEOPEN_FLAG_READ, Logger::log(), logger, read(), reportAlways, restrict, VirtFs::DirEntry::rootSubDir, and VirtFs::DirEntry::userDir.

Referenced by initFuncs().

◆ mkdir()

bool VirtFs::FsDir::mkdir ( std::string  dirname)

Definition at line 285 of file fsdir.cpp.

286  {
287  prepareFsPath(dirname);
288  if (mWriteDir.empty())
289  {
290  reportAlways("FsDir::mkdir write dir is empty")
291  return false;
292  }
293  return mkdir_r((mWriteDir + dirname).c_str()) != -1;
294  }
int mkdir_r(const char *const pathname)
Create a directory, making leading components first if necessary.
Definition: mkdir.cpp:109

References mkdir_r(), VirtFs::anonymous_namespace{fsdir.cpp}::mWriteDir, prepareFsPath(), and reportAlways.

Referenced by VirtFs::mkdir().

◆ openAppend()

File * VirtFs::FsDir::openAppend ( FsEntry *const  entry,
const std::string &  filename 
)

Definition at line 95 of file fsdir.cpp.

97  {
98  return openInternal(entry, filename, FILEOPEN_FLAG_APPEND);
99  }
#define FILEOPEN_FLAG_APPEND
Definition: fileapi.h:36
File * openInternal(FsEntry *const entry, const std::string &filename, const char *mode)
Definition: fsdir.cpp:64

References FILEOPEN_FLAG_APPEND, and openInternal().

Referenced by initFuncs().

◆ openInternal()

File * VirtFs::FsDir::openInternal ( FsEntry *const  entry,
const std::string &  filename,
const char *  mode 
)

Definition at line 64 of file fsdir.cpp.

67  {
68  const std::string path = static_cast<DirEntry*>(entry)->rootSubDir +
69  filename;
70  if (Files::existsLocal(path) == false)
71  return nullptr;
72  FILEHTYPE fd = FILEOPEN(path.c_str(),
73  mode);
74  if (fd == FILEHDEFAULT)
75  {
76  reportAlways("VirtFs::open file open error: %s",
77  filename.c_str())
78  return nullptr;
79  }
80  return new File(&funcs, fd);
81  }
#define new
Definition: debug_new.h:147

References Files::existsLocal(), FILEHDEFAULT, FILEHTYPE, FILEOPEN, VirtFs::anonymous_namespace{fsdir.cpp}::funcs, and reportAlways.

Referenced by openAppend(), openRead(), and openWrite().

◆ openRead()

File * VirtFs::FsDir::openRead ( FsEntry *const  entry,
std::string  filename 
)

Definition at line 83 of file fsdir.cpp.

85  {
86  return openInternal(entry, filename, FILEOPEN_FLAG_READ);
87  }

References FILEOPEN_FLAG_READ, and openInternal().

Referenced by initFuncs().

◆ openWrite()

File * VirtFs::FsDir::openWrite ( FsEntry *const  entry,
const std::string &  filename 
)

Definition at line 89 of file fsdir.cpp.

91  {
92  return openInternal(entry, filename, FILEOPEN_FLAG_WRITE);
93  }
#define FILEOPEN_FLAG_WRITE
Definition: fileapi.h:35

References FILEOPEN_FLAG_WRITE, and openInternal().

Referenced by initFuncs().

◆ permitLinks()

void VirtFs::FsDir::permitLinks ( const bool  val)

Definition at line 307 of file fsdir.cpp.

308  {
309  mPermitLinks = val;
310  }

References VirtFs::anonymous_namespace{fsdir.cpp}::mPermitLinks.

Referenced by VirtFs::permitLinks().

◆ read()

int64_t VirtFs::FsDir::read ( File *const  file,
void *const  buffer,
const uint32_t  objSize,
const uint32_t  objCount 
)

Definition at line 320 of file fsdir.cpp.

324  {
325  if (file == nullptr)
326  return 0;
327  FILEHTYPE fd = file->mFd;
328  if (fd == FILEHDEFAULT)
329  {
330  reportAlways("FsDir::read file not opened.")
331  return 0;
332  }
333 #ifdef USE_FILE_FOPEN
334  return fread(buffer, objSize, objCount, fd);
335 #else // USE_FILE_FOPEN
336  int max = objSize * objCount;
337  int cnt = ::read(fd, buffer, max);
338  if (cnt <= 0)
339  return cnt;
340  return cnt / objSize;
341 #endif // USE_FILE_FOPEN
342  }

References FILEHDEFAULT, FILEHTYPE, and reportAlways.

Referenced by initFuncs(), loadFile(), and rwops_read().

◆ remove()

bool VirtFs::FsDir::remove ( std::string  filename)

Definition at line 296 of file fsdir.cpp.

297  {
298  prepareFsPath(filename);
299  if (mWriteDir.empty())
300  {
301  reportAlways("FsDir::remove write dir is empty")
302  return false;
303  }
304  return ::remove((mWriteDir + filename).c_str()) != 0;
305  }
bool remove(std::string filename)
Definition: fsdir.cpp:296

References VirtFs::anonymous_namespace{fsdir.cpp}::mWriteDir, prepareFsPath(), and reportAlways.

Referenced by VirtFs::remove().

◆ rwops_close()

int VirtFs::FsDir::rwops_close ( SDL_RWops *const  rw)

Definition at line 233 of file fsdirrwops.cpp.

234  {
235  if (rw == nullptr)
236  return 0;
237  File *const handle = static_cast<File*>(
238  rw->hidden.unknown.data1);
239  delete handle;
240  SDL_FreeRW(rw);
241  return 0;
242  }

Referenced by initFuncs().

◆ rwops_read()

int VirtFs::FsDir::rwops_read ( SDL_RWops *const  rw,
void *const  ptr,
const int  size,
const int  maxnum 
)

Definition at line 160 of file fsdirrwops.cpp.

164  {
165  if (rw == nullptr)
166  return 0;
167  File *const handle = static_cast<File *>(
168  rw->hidden.unknown.data1);
169  FILEHTYPE fd = handle->mFd;
170 
171 #ifdef USE_FILE_FOPEN
172  const int64_t rc = fread(ptr, size, maxnum, fd);
173 #else // USE_FILE_FOPEN
174  int max = size * maxnum;
175  int cnt = ::read(fd, ptr, max);
176  if (cnt <= 0)
177  return cnt;
178  const int64_t rc = cnt / size;
179 #endif // USE_FILE_FOPEN
180 
181 #ifndef USE_FILE_FOPEN
182  if (rc != static_cast<int64_t>(maxnum))
183  {
184  const int64_t pos = lseek(fd, 0, SEEK_CUR);
185  struct stat statbuf;
186  if (fstat(fd, &statbuf) == -1)
187  {
188  reportAlways("FsDir::fileLength error.")
189  return CAST_S32(rc);
190  }
191  }
192 #endif // USE_FILE_FOPEN
193  return CAST_S32(rc);
194  }
int size()
Definition: emotedb.cpp:306
int64_t read(File *const file, void *const buffer, const uint32_t objSize, const uint32_t objCount)
Definition: fs.cpp:815

References CAST_S32, FILEHTYPE, VirtFs::File::mFd, read(), reportAlways, and EmoteDB::size().

Referenced by initFuncs().

◆ rwops_seek()

int32_t VirtFs::FsDir::rwops_seek ( SDL_RWops *const  rw,
const int32_t  offset,
const int  whence 
)

Definition at line 41 of file fsdirrwops.cpp.

44  {
45  if (rw == nullptr)
46  return -1;
47  File *const handle = static_cast<File *>(
48  rw->hidden.unknown.data1);
49  FILEHTYPE fd = handle->mFd;
50  RWOPSINT pos = 0;
51 
52  if (whence == SEEK_SET)
53  {
54  pos = offset;
55  }
56  else if (whence == SEEK_CUR)
57  {
58 #ifdef USE_FILE_FOPEN
59  const int64_t current = ftell(fd);
60 #else // USE_FILE_FOPEN
61  const int64_t current = lseek(fd, 0, SEEK_CUR);
62 #endif // USE_FILE_FOPEN
63 
64  if (current == -1)
65  {
67  "VirtFs::rwops_seek: Can't find position in file.");
68  return -1;
69  }
70 
71  pos = CAST_S32(current);
72  if (static_cast<int64_t>(pos) != current)
73  {
74  logger->assertLog("VirtFs::rwops_seek: "
75  "Can't fit current file position in an int!");
76  return -1;
77  }
78 
79  if (offset == 0) /* this is a "tell" call. We're done. */
80  return pos;
81 
82  pos += offset;
83  }
84  else if (whence == SEEK_END)
85  {
86  int64_t len = 0;
87 #ifdef USE_FILE_FOPEN
88  const long curpos = ftell(fd);
89  if (curpos < 0)
90  {
91  reportAlways("FsDir::fileLength ftell error.")
92  return -1;
93  }
94  fseek(fd, 0, SEEK_END);
95  len = ftell(fd);
96 // fseek(fd, curpos, SEEK_SET);
97 #else // USE_FILE_FOPEN
98  struct stat statbuf;
99  if (fstat(fd, &statbuf) == -1)
100  {
101  reportAlways("FsDir::fileLength error.")
102  len = -1;
103  }
104  else
105  {
106  len = static_cast<int64_t>(statbuf.st_size);
107  }
108 #endif // USE_FILE_FOPEN
109 
110  if (len == -1)
111  {
112 #ifdef USE_FILE_FOPEN
113  if (fseek(fd, curpos, SEEK_SET) < 0)
114  {
115  reportAlways("FsDir::fileLength fseek error.")
116  }
117 #endif // USE_FILE_FOPEN
118  logger->assertLog(
119  "VirtFs::rwops_seek:Can't find end of file.");
120  return -1;
121  }
122 
123  pos = static_cast<RWOPSINT>(len);
124  if (static_cast<int64_t>(pos) != len)
125  {
126 #ifdef USE_FILE_FOPEN
127  fseek(fd, curpos, SEEK_SET);
128 #endif // USE_FILE_FOPEN
129  logger->assertLog("VirtFs::rwops_seek: "
130  "Can't fit end-of-file position in an int!");
131  return -1;
132  }
133 
134  pos += offset;
135  }
136  else
137  {
138  logger->assertLog(
139  "VirtFs::rwops_seek: Invalid 'whence' parameter.");
140  return -1;
141  }
142 
143  if (pos < 0)
144  {
145  logger->assertLog("VirtFs::rwops_seek: "
146  "Attempt to seek past start of file.");
147  return -1;
148  }
149 
150  const int64_t res = FILESEEK(fd, pos, SEEK_SET);
151  if (res == -1)
152  {
153  logger->assertLog("VirtFs::rwops_seek: seek error.");
154  return -1;
155  }
156 
157  return pos;
158  }
void assertLog(const char *const log_text,...)
Definition: logger.cpp:316
#define FILESEEK
Definition: fileapi.h:33
#define RWOPSINT
Definition: rwopstypes.h:31

References Logger::assertLog(), CAST_S32, FILEHTYPE, FILESEEK, logger, VirtFs::File::mFd, reportAlways, and RWOPSINT.

Referenced by initFuncs().

◆ rwops_write()

int VirtFs::FsDir::rwops_write ( SDL_RWops *const  rw,
const void *const  ptr,
const int  size,
const int  maxnum 
)

Definition at line 196 of file fsdirrwops.cpp.

200  {
201  if (rw == nullptr)
202  return 0;
203  File *const handle = static_cast<File *>(
204  rw->hidden.unknown.data1);
205  FILEHTYPE fd = handle->mFd;
206 
207 #ifdef USE_FILE_FOPEN
208  const int64_t rc = fwrite(ptr, size, maxnum, fd);
209 #else // USE_FILE_FOPEN
210  int max = size * maxnum;
211  int cnt = ::write(fd, ptr, max);
212  if (cnt <= 0)
213  return cnt;
214  const int64_t rc = cnt / size;
215 #endif // USE_FILE_FOPEN
216 
217 #ifndef USE_FILE_FOPEN
218  if (rc != static_cast<int64_t>(maxnum))
219  {
220  const int64_t pos = lseek(fd, 0, SEEK_CUR);
221  struct stat statbuf;
222  if (fstat(fd, &statbuf) == -1)
223  {
224  reportAlways("FsDir::fileLength error.")
225  return CAST_S32(rc);
226  }
227  }
228 #endif // USE_FILE_FOPEN
229 
230  return CAST_S32(rc);
231  }
int64_t write(File *const file, const void *const buffer, const uint32_t objSize, const uint32_t objCount)
Definition: fs.cpp:826

References CAST_S32, FILEHTYPE, VirtFs::File::mFd, reportAlways, EmoteDB::size(), and write().

Referenced by initFuncs().

◆ seek()

int VirtFs::FsDir::seek ( File *const  file,
const uint64_t  pos 
)

Definition at line 418 of file fsdir.cpp.

420  {
421  if (file == nullptr)
422  return 0;
423 
424  FILEHTYPE fd = file->mFd;
425  if (fd == FILEHDEFAULT)
426  {
427  reportAlways("FsDir::seek file not opened.")
428  return 0;
429  }
430  const int64_t res = FILESEEK(fd, pos, SEEK_SET);
431  if (res == -1)
432  return 0;
433  return 1;
434  }

References FILEHDEFAULT, FILEHTYPE, FILESEEK, and reportAlways.

Referenced by initFuncs().

◆ setWriteDir()

bool VirtFs::FsDir::setWriteDir ( std::string  newDir)

Definition at line 276 of file fsdir.cpp.

277  {
278  prepareFsPath(newDir);
279  mWriteDir = STD_MOVE(newDir);
280  if (findLast(mWriteDir, std::string(dirSeparator)) == false)
282  return true;
283  }
const char * dirSeparator
Definition: fs.cpp:43
#define STD_MOVE(var)
Definition: stdmove.h:28
bool findLast(const std::string &str1, const std::string &str2)

References dirSeparator, findLast(), VirtFs::anonymous_namespace{fsdir.cpp}::mWriteDir, prepareFsPath(), and STD_MOVE.

Referenced by VirtFs::setWriteDir().

◆ tell()

int64_t VirtFs::FsDir::tell ( File *const  file)

Definition at line 400 of file fsdir.cpp.

401  {
402  if (file == nullptr)
403  return -1;
404 
405  FILEHTYPE fd = file->mFd;
406  if (fd == FILEHDEFAULT)
407  {
408  reportAlways("FsDir::tell file not opened.")
409  return 0;
410  }
411 #ifdef USE_FILE_FOPEN
412  return ftell(fd);
413 #else // USE_FILE_FOPEN
414  return lseek(fd, 0, SEEK_CUR);
415 #endif // USE_FILE_FOPEN
416  }

References FILEHDEFAULT, FILEHTYPE, and reportAlways.

Referenced by initFuncs().

◆ write()

int64_t VirtFs::FsDir::write ( File *const  file,
const void *const  buffer,
const uint32_t  objSize,
const uint32_t  objCount 
)

Definition at line 344 of file fsdir.cpp.

348  {
349  if (file == nullptr)
350  return 0;
351  FILEHTYPE fd = file->mFd;
352  if (fd == FILEHDEFAULT)
353  {
354  reportAlways("FsDir::write file not opened.")
355  return 0;
356  }
357 #ifdef USE_FILE_FOPEN
358  return fwrite(buffer, objSize, objCount, fd);
359 #else // USE_FILE_FOPEN
360  int max = objSize * objCount;
361  int cnt = ::write(fd, buffer, max);
362  if (cnt <= 0)
363  return cnt;
364  return cnt / objSize;
365 #endif // USE_FILE_FOPEN
366  }

References FILEHDEFAULT, FILEHTYPE, and reportAlways.

Referenced by initFuncs(), and rwops_write().