summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAndrei Karas <akaras@inbox.ru>2017-03-06 18:08:27 +0300
committerAndrei Karas <akaras@inbox.ru>2017-03-06 18:08:27 +0300
commitc7b2d6baf29cd2a248e2d8dc09f7916643845f97 (patch)
tree16ac981c403eb48baccaca10290b7d7bc1d03eab /src
parentb1241b3e98f72b50f38dad602da3d3f4b4edc7bf (diff)
downloadmanaverse-c7b2d6baf29cd2a248e2d8dc09f7916643845f97.tar.gz
manaverse-c7b2d6baf29cd2a248e2d8dc09f7916643845f97.tar.bz2
manaverse-c7b2d6baf29cd2a248e2d8dc09f7916643845f97.tar.xz
manaverse-c7b2d6baf29cd2a248e2d8dc09f7916643845f97.zip
Add support for standard library file api in virtfs.s20170306
Because posix file api broken in mingw.
Diffstat (limited to 'src')
-rw-r--r--src/fs/virtfs/virtfileprivate.cpp10
-rw-r--r--src/fs/virtfs/virtfileprivate.h10
-rw-r--r--src/fs/virtfs/virtfs_unittest.cc30
-rw-r--r--src/fs/virtfs/virtfsdir.cpp73
-rw-r--r--src/fs/virtfs/virtfsdir.h4
-rw-r--r--src/localconsts.h2
6 files changed, 85 insertions, 44 deletions
diff --git a/src/fs/virtfs/virtfileprivate.cpp b/src/fs/virtfs/virtfileprivate.cpp
index 459a17ca9..682992c68 100644
--- a/src/fs/virtfs/virtfileprivate.cpp
+++ b/src/fs/virtfs/virtfileprivate.cpp
@@ -30,11 +30,11 @@ VirtFilePrivate::VirtFilePrivate() :
mBuf(nullptr),
mPos(0U),
mSize(0U),
- mFd(-1)
+ mFd(FILEHDEFAULT)
{
}
-VirtFilePrivate::VirtFilePrivate(const int fd) :
+VirtFilePrivate::VirtFilePrivate(FILEHTYPE fd) :
mBuf(nullptr),
mPos(0U),
mSize(0U),
@@ -47,14 +47,14 @@ VirtFilePrivate::VirtFilePrivate(uint8_t *restrict const buf,
mBuf(buf),
mPos(0U),
mSize(sz),
- mFd(-1)
+ mFd(FILEHDEFAULT)
{
}
VirtFilePrivate::~VirtFilePrivate()
{
- if (mFd != -1)
- close(mFd);
+ if (mFd != FILEHDEFAULT)
+ FILECLOSE(mFd);
if (mBuf)
delete [] mBuf;
}
diff --git a/src/fs/virtfs/virtfileprivate.h b/src/fs/virtfs/virtfileprivate.h
index d074d13bc..08c6a3311 100644
--- a/src/fs/virtfs/virtfileprivate.h
+++ b/src/fs/virtfs/virtfileprivate.h
@@ -24,11 +24,17 @@
#include "localconsts.h"
+#include "fs/virtfs/fileapi.h"
+
+#ifdef USE_FILE_FOPEN
+#include <stdio.h>
+#endif // USE_FILE_FOPEN
+
struct VirtFilePrivate final
{
VirtFilePrivate();
- explicit VirtFilePrivate(const int fd);
+ explicit VirtFilePrivate(FILEHTYPE fd);
VirtFilePrivate(uint8_t *restrict const buf,
const size_t sz);
@@ -45,7 +51,7 @@ struct VirtFilePrivate final
size_t mSize;
// dirfs fields
- int mFd;
+ FILEHTYPE mFd;
};
#endif // USE_PHYSFS
diff --git a/src/fs/virtfs/virtfs_unittest.cc b/src/fs/virtfs/virtfs_unittest.cc
index 7e23227b9..cece63a04 100644
--- a/src/fs/virtfs/virtfs_unittest.cc
+++ b/src/fs/virtfs/virtfs_unittest.cc
@@ -1267,17 +1267,17 @@ TEST_CASE("VirtFs1 read1")
REQUIRE(strcmp(static_cast<char*>(buffer),
"test line 1\ntest line 2") == 0);
REQUIRE(VirtFs::tell(file) == fileSize);
- REQUIRE(VirtFs::eof(file) == true);
+ REQUIRE(VirtFs::eof(file) != 0);
free(buffer);
buffer = calloc(fileSize + 1, 1);
REQUIRE(VirtFs::seek(file, 12) != 0);
- REQUIRE(VirtFs::eof(file) == false);
+ REQUIRE(VirtFs::eof(file) == 0);
REQUIRE(VirtFs::tell(file) == 12);
REQUIRE(VirtFs::read(file, buffer, 1, 11) == 11);
REQUIRE(strcmp(static_cast<char*>(buffer),
"test line 2") == 0);
- REQUIRE(VirtFs::eof(file) == true);
+ REQUIRE(VirtFs::eof(file) != 0);
VirtFs::close(file);
free(buffer);
@@ -1313,7 +1313,7 @@ TEST_CASE("VirtFs1 read2")
REQUIRE(strcmp(static_cast<char*>(buffer),
"test line 1\ntest line 2") == 0);
REQUIRE(VirtFs::tell(file) == fileSize);
- REQUIRE(VirtFs::eof(file) == true);
+ REQUIRE(VirtFs::eof(file) != 0);
}
SECTION("test 2")
@@ -1325,12 +1325,12 @@ TEST_CASE("VirtFs1 read2")
buffer = calloc(fileSize + 1, 1);
REQUIRE(VirtFs::seek(file, 12) != 0);
- REQUIRE(VirtFs::eof(file) == false);
+ REQUIRE(VirtFs::eof(file) == 0);
REQUIRE(VirtFs::tell(file) == 12);
REQUIRE(VirtFs::read(file, buffer, 1, 11) == 11);
REQUIRE(strcmp(static_cast<char*>(buffer),
"test line 2") == 0);
- REQUIRE(VirtFs::eof(file) == true);
+ REQUIRE(VirtFs::eof(file) != 0);
}
SECTION("test 3")
@@ -1343,7 +1343,7 @@ TEST_CASE("VirtFs1 read2")
for (int f = 0; f < fileSize; f ++)
{
REQUIRE(VirtFs::seek(file, f) != 0);
- REQUIRE(VirtFs::eof(file) == false);
+ REQUIRE(VirtFs::eof(file) == 0);
REQUIRE(VirtFs::tell(file) == f);
}
}
@@ -1359,12 +1359,12 @@ TEST_CASE("VirtFs1 read2")
{
REQUIRE(VirtFs::read(file, buffer, 1, 1) == 1);
REQUIRE(static_cast<char*>(buffer)[0] == str[f]);
- REQUIRE(VirtFs::eof(file) == false);
+ REQUIRE(VirtFs::eof(file) == 0);
REQUIRE(VirtFs::tell(file) == f + 1);
}
REQUIRE(VirtFs::read(file, buffer, 1, 1) == 1);
REQUIRE(static_cast<char*>(buffer)[0] == str[22]);
- REQUIRE(VirtFs::eof(file) == true);
+ REQUIRE(VirtFs::eof(file) != 0);
REQUIRE(VirtFs::tell(file) == fileSize);
}
@@ -1380,13 +1380,13 @@ TEST_CASE("VirtFs1 read2")
REQUIRE(VirtFs::read(file, buffer, 2, 1) == 1);
REQUIRE(static_cast<char*>(buffer)[0] == str[f]);
REQUIRE(static_cast<char*>(buffer)[1] == str[f + 1]);
- REQUIRE(VirtFs::eof(file) == false);
+ REQUIRE(VirtFs::eof(file) == 0);
REQUIRE(VirtFs::tell(file) == f + 2);
}
- REQUIRE(VirtFs::eof(file) == false);
+ REQUIRE(VirtFs::eof(file) == 0);
REQUIRE(VirtFs::tell(file) == 22);
REQUIRE(VirtFs::read(file, buffer, 2, 1) == 0);
- REQUIRE(VirtFs::eof(file) == false);
+ REQUIRE(VirtFs::eof(file) == 0);
}
SECTION("test 6")
@@ -1401,14 +1401,14 @@ TEST_CASE("VirtFs1 read2")
REQUIRE(VirtFs::read(file, buffer, 1, 2) == 2);
REQUIRE(static_cast<char*>(buffer)[0] == str[f]);
REQUIRE(static_cast<char*>(buffer)[1] == str[f + 1]);
- REQUIRE(VirtFs::eof(file) == false);
+ REQUIRE(VirtFs::eof(file) == 0);
REQUIRE(VirtFs::tell(file) == f + 2);
}
- REQUIRE(VirtFs::eof(file) == false);
+ REQUIRE(VirtFs::eof(file) == 0);
REQUIRE(VirtFs::tell(file) == 22);
REQUIRE(VirtFs::read(file, buffer, 1, 2) == 1);
REQUIRE(static_cast<char*>(buffer)[0] == str[22]);
- REQUIRE(VirtFs::eof(file) == true);
+ REQUIRE(VirtFs::eof(file) != 0);
}
VirtFs::close(file);
diff --git a/src/fs/virtfs/virtfsdir.cpp b/src/fs/virtfs/virtfsdir.cpp
index 0158dec2a..ba320ae2e 100644
--- a/src/fs/virtfs/virtfsdir.cpp
+++ b/src/fs/virtfs/virtfsdir.cpp
@@ -42,6 +42,10 @@
#include <iostream>
#include <unistd.h>
+#ifdef USE_FILE_FOPEN
+#include <stdio.h>
+#endif // USE_FILE_FOPEN
+
#include <sys/types.h>
#include <sys/stat.h>
@@ -62,15 +66,14 @@ namespace VirtFsDir
{
VirtFile *openInternal(VirtFsEntry *restrict const entry,
const std::string &filename,
- const int mode)
+ const FILEMTYPE mode)
{
const std::string path = entry->root + filename;
if (Files::existsLocal(path) == false)
return nullptr;
- const int fd = open(path.c_str(),
- mode,
- S_IRUSR | S_IWUSR);
- if (fd == -1)
+ FILEHTYPE fd = FILEOPEN(path.c_str(),
+ mode);
+ if (fd == FILEHDEFAULT)
{
reportAlways("VirtFs::open file open error: %s",
filename.c_str());
@@ -85,19 +88,19 @@ namespace VirtFsDir
VirtFile *openRead(VirtFsEntry *restrict const entry,
const std::string &filename)
{
- return openInternal(entry, filename, O_RDONLY);
+ return openInternal(entry, filename, FILEOPEN_FLAG_READ);
}
VirtFile *openWrite(VirtFsEntry *restrict const entry,
const std::string &filename)
{
- return openInternal(entry, filename, O_WRONLY | O_CREAT | O_TRUNC);
+ return openInternal(entry, filename, FILEOPEN_FLAG_WRITE);
}
VirtFile *openAppend(VirtFsEntry *restrict const entry,
const std::string &filename)
{
- return openInternal(entry, filename, O_WRONLY | O_CREAT | O_APPEND);
+ return openInternal(entry, filename, FILEOPEN_FLAG_APPEND);
}
void deinit()
@@ -314,17 +317,21 @@ namespace VirtFsDir
{
if (file == nullptr)
return 0;
- const int fd = file->mPrivate->mFd;
- if (fd == -1)
+ FILEHTYPE fd = file->mPrivate->mFd;
+ if (fd == FILEHDEFAULT)
{
reportAlways("VirtFsDir::read file not opened.");
return 0;
}
+#ifdef USE_FILE_FOPEN
+ return fread(buffer, objSize, objCount, fd);
+#else // USE_FILE_FOPEN
int max = objSize * objCount;
int cnt = ::read(fd, buffer, max);
if (cnt <= 0)
return cnt;
return cnt / objSize;
+#endif // USE_FILE_FOPEN
}
int64_t write(VirtFile *restrict const file,
@@ -334,29 +341,40 @@ namespace VirtFsDir
{
if (file == nullptr)
return 0;
- const int fd = file->mPrivate->mFd;
- if (fd == -1)
+ FILEHTYPE fd = file->mPrivate->mFd;
+ if (fd == FILEHDEFAULT)
{
reportAlways("VirtFsDir::write file not opened.");
return 0;
}
+#ifdef USE_FILE_FOPEN
+ return fwrite(buffer, objSize, objCount, fd);
+#else // USE_FILE_FOPEN
int max = objSize * objCount;
int cnt = ::write(fd, buffer, max);
if (cnt <= 0)
return cnt;
return cnt / objSize;
+#endif // USE_FILE_FOPEN
}
int64_t fileLength(VirtFile *restrict const file)
{
if (file == nullptr)
return -1;
- const int fd = file->mPrivate->mFd;
- if (fd == -1)
+ FILEHTYPE fd = file->mPrivate->mFd;
+ if (fd == FILEHDEFAULT)
{
reportAlways("VirtFsDir::fileLength file not opened.");
return 0;
}
+#ifdef USE_FILE_FOPEN
+ const long pos = ftell(fd);
+ fseek(fd, 0, SEEK_END);
+ const long sz = ftell(fd);
+ fseek(fd, pos, SEEK_SET);
+ return sz;
+#else // USE_FILE_FOPEN
struct stat statbuf;
if (fstat(fd, &statbuf) == -1)
{
@@ -364,6 +382,7 @@ namespace VirtFsDir
return -1;
}
return static_cast<int64_t>(statbuf.st_size);
+#endif // USE_FILE_FOPEN
}
int64_t tell(VirtFile *restrict const file)
@@ -371,13 +390,17 @@ namespace VirtFsDir
if (file == nullptr)
return -1;
- const int fd = file->mPrivate->mFd;
- if (fd == -1)
+ FILEHTYPE fd = file->mPrivate->mFd;
+ if (fd == FILEHDEFAULT)
{
reportAlways("VirtFsDir::tell file not opened.");
return 0;
}
+#ifdef USE_FILE_FOPEN
+ const int64_t pos = ftell(fd);
+#else // USE_FILE_FOPEN
const int64_t pos = lseek(fd, 0, SEEK_CUR);
+#endif // USE_FILE_FOPEN
return pos;
}
@@ -387,13 +410,13 @@ namespace VirtFsDir
if (file == nullptr)
return 0;
- const int fd = file->mPrivate->mFd;
- if (fd == -1)
+ FILEHTYPE fd = file->mPrivate->mFd;
+ if (fd == FILEHDEFAULT)
{
reportAlways("VirtFsDir::seek file not opened.");
return 0;
}
- const int64_t res = lseek(fd, pos, SEEK_SET);
+ const int64_t res = FILESEEK(fd, pos, SEEK_SET);
if (res == -1)
return 0;
return 1;
@@ -404,12 +427,19 @@ namespace VirtFsDir
if (file == nullptr)
return -1;
- const int fd = file->mPrivate->mFd;
- if (fd == -1)
+ FILEHTYPE fd = file->mPrivate->mFd;
+ if (fd == FILEHDEFAULT)
{
reportAlways("VirtFsDir::eof file not opened.");
return 0;
}
+#ifdef USE_FILE_FOPEN
+ const int flag = feof(fd);
+ if (flag != 0)
+ return 1;
+ const int64_t pos = ftell(fd);
+ const int64_t len = fileLength(file);
+#else // USE_FILE_FOPEN
const int64_t pos = lseek(fd, 0, SEEK_CUR);
struct stat statbuf;
if (fstat(fd, &statbuf) == -1)
@@ -418,6 +448,7 @@ namespace VirtFsDir
return -1;
}
const int64_t len = static_cast<int64_t>(statbuf.st_size);
+#endif // USE_FILE_FOPEN
return pos < 0 || len < 0 || pos >= len;
}
} // namespace VirtFs
diff --git a/src/fs/virtfs/virtfsdir.h b/src/fs/virtfs/virtfsdir.h
index 826d4dde4..e689fcd95 100644
--- a/src/fs/virtfs/virtfsdir.h
+++ b/src/fs/virtfs/virtfsdir.h
@@ -25,6 +25,8 @@
#include "enums/simpletypes/append.h"
#include "enums/simpletypes/skiperror.h"
+#include "fs/virtfs/fileapi.h"
+
#include "utils/stringvector.h"
#include "localconsts.h"
@@ -38,7 +40,7 @@ namespace VirtFsDir
{
VirtFile *openInternal(VirtFsEntry *restrict const entry,
const std::string &filename,
- const int mode);
+ const FILEMTYPE mode);
VirtFile *openRead(VirtFsEntry *restrict const entry,
const std::string &filename);
VirtFile *openWrite(VirtFsEntry *restrict const entry,
diff --git a/src/localconsts.h b/src/localconsts.h
index 11c120ee2..4734ebd50 100644
--- a/src/localconsts.h
+++ b/src/localconsts.h
@@ -349,6 +349,8 @@ PRAGMA45(GCC diagnostic pop)
// debug animations
// #define DEBUG_ANIMATIONS 1
+#define USE_FILE_FOPEN 1
+
#ifdef DYECMD
#undef USE_FUZZER
#endif // DYECMD