2021-04-22 22:01:32 +08:00
|
|
|
// Copyright (c) 2018-2019 HALX99.
|
2022-04-25 19:15:46 +08:00
|
|
|
// Copyright (c) 2020 C4games Ltd
|
2021-04-22 22:01:32 +08:00
|
|
|
#include "platform/CCPosixFileStream.h"
|
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
#if AX_TARGET_PLATFORM == AX_PLATFORM_ANDROID
|
2021-12-25 10:04:45 +08:00
|
|
|
# include "base/ZipUtils.h"
|
2021-04-22 22:01:32 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
2022-11-10 13:45:51 +08:00
|
|
|
#if defined(_WIN32)
|
|
|
|
int _ftruncate(int fd, int64_t size)
|
|
|
|
{
|
|
|
|
auto handle = (HANDLE)_get_osfhandle(fd);
|
|
|
|
if (handle == INVALID_HANDLE_VALUE)
|
|
|
|
return -1;
|
|
|
|
LARGE_INTEGER offset;
|
|
|
|
offset.QuadPart = size;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if (!::SetFilePointerEx(handle, offset, nullptr, FILE_BEGIN))
|
|
|
|
break;
|
|
|
|
if (!::SetEndOfFile(handle))
|
|
|
|
break;
|
|
|
|
return 0;
|
|
|
|
} while (false);
|
|
|
|
errno = GetLastError();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BEGIN
|
2021-04-22 22:01:32 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
struct PXIoF
|
|
|
|
{
|
|
|
|
int (*read)(PXFileHandle& handle, void*, unsigned int);
|
|
|
|
int64_t (*seek)(PXFileHandle& handle, int64_t, int);
|
|
|
|
int (*close)(PXFileHandle& handle);
|
|
|
|
long long (*size)(PXFileHandle& handle);
|
2021-04-22 22:01:32 +08:00
|
|
|
};
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
static int pfs_posix_open(std::string_view path, FileStream::Mode mode, PXFileHandle& handle)
|
2021-04-22 22:01:32 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
switch (mode)
|
|
|
|
{
|
2021-04-22 22:01:32 +08:00
|
|
|
case FileStream::Mode::READ:
|
2021-07-04 21:18:14 +08:00
|
|
|
handle._fd = posix_open_cxx(path, O_READ_FLAGS);
|
2021-04-22 22:01:32 +08:00
|
|
|
break;
|
|
|
|
case FileStream::Mode::WRITE:
|
2021-07-04 21:18:14 +08:00
|
|
|
handle._fd = posix_open_cxx(path, O_WRITE_FLAGS);
|
2021-04-22 22:01:32 +08:00
|
|
|
break;
|
|
|
|
case FileStream::Mode::APPEND:
|
2021-07-04 21:18:14 +08:00
|
|
|
handle._fd = posix_open_cxx(path, O_APPEND_FLAGS);
|
2021-04-22 22:01:32 +08:00
|
|
|
break;
|
2021-07-04 16:40:34 +08:00
|
|
|
case FileStream::Mode::OVERLAPPED:
|
2021-07-04 21:18:14 +08:00
|
|
|
handle._fd = posix_open_cxx(path, O_OVERLAP_FLAGS);
|
2021-07-04 16:40:34 +08:00
|
|
|
break;
|
2021-04-22 22:01:32 +08:00
|
|
|
default:
|
|
|
|
handle._fd = -1;
|
|
|
|
}
|
|
|
|
return handle._fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
// posix standard wrappers
|
2021-12-25 10:04:45 +08:00
|
|
|
static int pfs_posix_read(PXFileHandle& handle, void* buf, unsigned int size)
|
|
|
|
{
|
|
|
|
return static_cast<int>(posix_read(handle._fd, buf, size));
|
|
|
|
}
|
|
|
|
static int64_t pfs_posix_seek(PXFileHandle& handle, int64_t offst, int origin)
|
|
|
|
{
|
|
|
|
return posix_lseek64(handle._fd, offst, origin);
|
|
|
|
}
|
|
|
|
static int pfs_posix_close(PXFileHandle& handle)
|
|
|
|
{
|
2021-04-22 22:01:32 +08:00
|
|
|
int fd = handle._fd;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (fd != -1)
|
|
|
|
{
|
2021-04-22 22:01:32 +08:00
|
|
|
handle._fd = -1;
|
|
|
|
return posix_close(fd);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2021-05-31 15:08:44 +08:00
|
|
|
static long long pfs_posix_size(PXFileHandle& handle)
|
|
|
|
{
|
|
|
|
#if defined(_WIN32)
|
|
|
|
return _filelengthi64(handle._fd);
|
|
|
|
#else
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
if (fstat(handle._fd, &st) == 0)
|
|
|
|
return st.st_size;
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
static PXIoF pfs_posix_iof = {pfs_posix_read, pfs_posix_seek, pfs_posix_close, pfs_posix_size};
|
2021-04-22 22:01:32 +08:00
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
#if AX_TARGET_PLATFORM == AX_PLATFORM_ANDROID
|
2021-04-22 22:01:32 +08:00
|
|
|
// android AssetManager wrappers
|
2021-12-25 10:04:45 +08:00
|
|
|
static int pfs_asset_read(PXFileHandle& handle, void* buf, unsigned int size)
|
|
|
|
{
|
|
|
|
return AAsset_read(handle._asset, buf, size);
|
|
|
|
}
|
|
|
|
static int64_t pfs_asset_seek(PXFileHandle& handle, int64_t offst, int origin)
|
|
|
|
{
|
|
|
|
return AAsset_seek(handle._asset, offst, origin);
|
|
|
|
}
|
|
|
|
static int pfs_asset_close(PXFileHandle& handle)
|
|
|
|
{
|
|
|
|
if (handle._asset != nullptr)
|
|
|
|
{
|
2021-04-22 22:01:32 +08:00
|
|
|
AAsset_close(handle._asset);
|
|
|
|
handle._asset = nullptr;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2021-05-31 15:08:44 +08:00
|
|
|
static long long pfs_asset_size(PXFileHandle& handle)
|
|
|
|
{
|
|
|
|
return AAsset_getLength64(handle._asset);
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
static PXIoF pfs_asset_iof = {pfs_asset_read, pfs_asset_seek, pfs_asset_close, pfs_asset_size};
|
2021-04-22 22:01:32 +08:00
|
|
|
|
|
|
|
// android obb
|
2021-12-25 10:04:45 +08:00
|
|
|
static int pfs_obb_read(PXFileHandle& handle, void* buf, unsigned int size)
|
|
|
|
{
|
|
|
|
return FileUtilsAndroid::getObbFile()->zfread(&handle._zfs, buf, size);
|
|
|
|
}
|
|
|
|
static int64_t pfs_obb_seek(PXFileHandle& handle, int64_t offset, int origin)
|
|
|
|
{
|
|
|
|
return FileUtilsAndroid::getObbFile()->zfseek(&handle._zfs, offset, origin);
|
|
|
|
}
|
|
|
|
static int pfs_obb_close(PXFileHandle& handle)
|
|
|
|
{
|
2021-04-22 22:01:32 +08:00
|
|
|
FileUtilsAndroid::getObbFile()->zfclose(&handle._zfs);
|
|
|
|
return 0;
|
|
|
|
}
|
2021-05-31 15:08:44 +08:00
|
|
|
static long long pfs_obb_size(PXFileHandle& handle)
|
|
|
|
{
|
|
|
|
return FileUtilsAndroid::getObbFile()->zfsize(&handle._zfs);
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
static PXIoF pfs_obb_iof = {pfs_obb_read, pfs_obb_seek, pfs_obb_close, pfs_obb_size};
|
2021-04-22 22:01:32 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
PosixFileStream::~PosixFileStream()
|
|
|
|
{
|
|
|
|
internalClose();
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
bool PosixFileStream::open(std::string_view path, FileStream::Mode mode)
|
2021-04-22 22:01:32 +08:00
|
|
|
{
|
|
|
|
bool ok = false;
|
2022-07-16 10:43:05 +08:00
|
|
|
#if AX_TARGET_PLATFORM != AX_PLATFORM_ANDROID
|
2021-04-22 22:01:32 +08:00
|
|
|
ok = pfs_posix_open(path, mode, _handle) != -1;
|
2021-12-25 10:04:45 +08:00
|
|
|
#else // Android
|
|
|
|
if (path[0] != '/')
|
|
|
|
{ // from package, always readonly
|
2021-04-22 22:01:32 +08:00
|
|
|
std::string relativePath;
|
|
|
|
size_t position = path.find("assets/");
|
|
|
|
if (0 == position)
|
|
|
|
{
|
|
|
|
// "assets/" is at the beginning of the path and we don't want it
|
|
|
|
relativePath = path.substr(sizeof("assets/") - 1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
relativePath = path;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto obb = FileUtilsAndroid::getObbFile();
|
2021-12-25 10:04:45 +08:00
|
|
|
ok = obb != nullptr && obb->zfopen(relativePath, &_handle._zfs);
|
|
|
|
if (ok)
|
|
|
|
{
|
2021-04-22 22:01:32 +08:00
|
|
|
this->_iof = &pfs_obb_iof;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else
|
|
|
|
{
|
2021-04-22 22:01:32 +08:00
|
|
|
AAssetManager* asMgr = FileUtilsAndroid::getAssetManager();
|
2021-12-25 10:04:45 +08:00
|
|
|
AAsset* asset = AAssetManager_open(asMgr, relativePath.c_str(), AASSET_MODE_UNKNOWN);
|
|
|
|
ok = !!asset;
|
|
|
|
if (ok)
|
|
|
|
{
|
2021-04-22 22:01:32 +08:00
|
|
|
_handle._asset = asset;
|
|
|
|
// setup file read/seek/close at here
|
|
|
|
this->_iof = &pfs_asset_iof;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else
|
|
|
|
{ // otherwise, as a absolutely path
|
2021-04-22 22:01:32 +08:00
|
|
|
ok = pfs_posix_open(path, mode, _handle) != -1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (ok && !_iof)
|
|
|
|
_iof = &pfs_posix_iof;
|
|
|
|
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
int PosixFileStream::internalClose()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_iof)
|
|
|
|
{
|
2021-04-22 22:01:32 +08:00
|
|
|
int ret = _iof->close(_handle);
|
2021-04-24 07:22:27 +08:00
|
|
|
reset();
|
2021-04-22 22:01:32 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int PosixFileStream::close()
|
|
|
|
{
|
|
|
|
return internalClose();
|
|
|
|
}
|
|
|
|
|
2021-09-05 20:45:32 +08:00
|
|
|
int PosixFileStream::seek(int64_t offset, int origin)
|
2021-04-22 22:01:32 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
const auto result = _iof->seek(_handle, static_cast<int32_t>(offset),
|
|
|
|
origin); // this returns -1 for error, and resulting offset on success
|
|
|
|
return result < 0 ? -1 : 0; // return 0 for success
|
2021-04-22 22:01:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int PosixFileStream::read(void* buf, unsigned int size)
|
|
|
|
{
|
|
|
|
return _iof->read(_handle, buf, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
int PosixFileStream::write(const void* buf, unsigned int size)
|
|
|
|
{
|
2021-08-20 01:20:17 +08:00
|
|
|
return posix_write(_handle._fd, buf, size);
|
2021-04-22 22:01:32 +08:00
|
|
|
}
|
|
|
|
|
2021-08-20 01:20:17 +08:00
|
|
|
int64_t PosixFileStream::tell()
|
2021-04-22 22:01:32 +08:00
|
|
|
{
|
2021-08-20 01:20:17 +08:00
|
|
|
return _iof->seek(_handle, 0, SEEK_CUR);
|
2021-04-22 22:01:32 +08:00
|
|
|
}
|
|
|
|
|
2021-08-20 01:20:17 +08:00
|
|
|
int64_t PosixFileStream::size()
|
2021-05-31 15:08:44 +08:00
|
|
|
{
|
|
|
|
return _iof->size(_handle);
|
|
|
|
}
|
|
|
|
|
2021-04-22 22:01:32 +08:00
|
|
|
bool PosixFileStream::isOpen() const
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
#if AX_TARGET_PLATFORM != AX_PLATFORM_ANDROID
|
2021-04-22 22:01:32 +08:00
|
|
|
return _handle._fd != -1;
|
|
|
|
#else
|
|
|
|
return _handle._fd != -1 && _handle._asset != nullptr;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void PosixFileStream::reset()
|
|
|
|
{
|
|
|
|
memset(&_handle, 0, sizeof(_handle));
|
2021-04-24 07:22:27 +08:00
|
|
|
_handle._fd = -1;
|
2021-12-25 10:04:45 +08:00
|
|
|
_iof = nullptr;
|
2021-04-22 22:01:32 +08:00
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_END
|