Skip to content

Commit

Permalink
POSIX's 'open' returns int.
Browse files Browse the repository at this point in the history
Clang doesn't admit an int -> void* cast for this.
  • Loading branch information
Zegeri committed Sep 24, 2018
1 parent 04893d8 commit 960761c
Show file tree
Hide file tree
Showing 10 changed files with 131 additions and 33 deletions.
4 changes: 4 additions & 0 deletions src/xrCore/FS.h
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,11 @@ class XRCORE_API IReader : public IReaderBase<IReader>
class XRCORE_API CVirtualFileRW : public IReader
{
private:
#if defined(WINDOWS)
void *hSrcFile, *hSrcMap;
#elif defined(LINUX)
int hSrcFile;
#endif

public:
CVirtualFileRW(const char* cFileName);
Expand Down
4 changes: 4 additions & 0 deletions src/xrCore/FS_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,11 @@ class CCompressedReader : public IReader
class CVirtualFileReader : public IReader
{
private:
#if defined(WINDOWS)
void *hSrcFile, *hSrcMap;
#elif defined(LINUX)
int hSrcFile;
#endif

public:
CVirtualFileReader(const char* cFileName);
Expand Down
95 changes: 69 additions & 26 deletions src/xrCore/LocatorAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,47 +256,33 @@ const CLocatorAPI::file* CLocatorAPI::Register(
return &*result;
}

#if defined(WINDOWS)
IReader* open_chunk(void* ptr, u32 ID)
{
u32 dwType, dwSize;
DWORD read_byte;
#ifdef WINDOWS
u32 pt = SetFilePointer(ptr, 0, nullptr, FILE_BEGIN);
VERIFY(pt != INVALID_SET_FILE_POINTER);
#else
::lseek(ptr, 0L, SEEK_SET);
#endif

while (true)
{
#ifdef WINDOWS
bool res = ReadFile(ptr, &dwType, 4, &read_byte, nullptr);
#elif defined(LINUX)
read_byte = ::read(ptr, &dwType, 4);
bool res = (read_byte != -1);
#endif

if (read_byte == 0)
return nullptr;
//. VERIFY(res&&(read_byte==4));

#ifdef WINDOWS
res = ReadFile(ptr, &dwSize, 4, &read_byte, nullptr);
#else
read_byte = ::read(ptr, &dwSize, 4);
res = (read_byte != -1);
#endif

if (read_byte == 0)
return nullptr;
//. VERIFY(res&&(read_byte==4));

if ((dwType & ~CFS_CompressMark) == ID)
{
u8* src_data = xr_alloc<u8>(dwSize);
#ifdef WINDOWS
res = ReadFile(ptr, src_data, dwSize, &read_byte, nullptr);
#else
read_byte = ::read(ptr, src_data, dwSize);
res = (read_byte != -1);
#endif

VERIFY(res && (read_byte == dwSize));
if (dwType & CFS_CompressMark)
{
Expand All @@ -308,17 +294,62 @@ IReader* open_chunk(void* ptr, u32 ID)
}
return new CTempReader(src_data, dwSize, 0);
}
#ifdef WINDOWS

pt = SetFilePointer(ptr, dwSize, nullptr, FILE_CURRENT);
if (pt == INVALID_SET_FILE_POINTER)
return nullptr;
#else
if(-1 == ::lseek(ptr, dwSize, SEEK_CUR))
return nullptr;
}
return nullptr;
};
#endif

#if defined(LINUX)
IReader* open_chunk(int fd, u32 ID)
{
u32 dwType, dwSize;
DWORD read_byte;
::lseek(fd, 0L, SEEK_SET);

while (true)
{
read_byte = ::read(fd, &dwType, 4);
bool res = (read_byte != -1);

if (read_byte == 0)
return nullptr;
//. VERIFY(res&&(read_byte==4));

read_byte = ::read(fd, &dwSize, 4);
res = (read_byte != -1);

if (read_byte == 0)
return nullptr;
//. VERIFY(res&&(read_byte==4));

if ((dwType & ~CFS_CompressMark) == ID)
{
u8* src_data = xr_alloc<u8>(dwSize);
read_byte = ::read(fd, src_data, dwSize);
res = (read_byte != -1);

VERIFY(res && (read_byte == dwSize));
if (dwType & CFS_CompressMark)
{
BYTE* dest;
unsigned dest_sz;
_decompressLZ(&dest, &dest_sz, src_data, dwSize);
xr_free(src_data);
return new CTempReader(dest, dest_sz, 0);
}
return new CTempReader(src_data, dwSize, 0);
}

if(-1 == ::lseek(fd, dwSize, SEEK_CUR))
return nullptr;
}
return nullptr;
};
#endif

void CLocatorAPI::LoadArchive(archive& A, pcstr entrypoint)
{
Expand Down Expand Up @@ -416,17 +447,21 @@ void CLocatorAPI::LoadArchive(archive& A, pcstr entrypoint)

void CLocatorAPI::archive::open()
{
#if defined(WINDOWS)
// Open the file
if (hSrcFile && hSrcMap)
return;

#if defined(WINDOWS)
hSrcFile = CreateFile(*path, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, OPEN_EXISTING, 0, nullptr);
R_ASSERT(hSrcFile != INVALID_HANDLE_VALUE);
hSrcMap = CreateFileMapping(hSrcFile, nullptr, PAGE_READONLY, 0, 0, nullptr);
R_ASSERT(hSrcMap != INVALID_HANDLE_VALUE);
size = GetFileSize(hSrcFile, nullptr);
#elif defined(LINUX)
// Open the file
if (hSrcFile)
return;

hSrcFile = ::open(*path, O_RDONLY, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
R_ASSERT(hSrcFile != -1);
struct stat file_info;
Expand Down Expand Up @@ -505,7 +540,11 @@ bool CLocatorAPI::load_all_unloaded_archives()
bool res = false;
for (auto& archive : m_archives)
{
#if defined(WINDOWS)
if (archive.hSrcFile == nullptr)
#elif defined(LINUX)
if (archive.hSrcFile == 0)
#endif
{
LoadArchive(archive);
res = true;
Expand Down Expand Up @@ -593,7 +632,7 @@ bool ignore_path(const char* _path)
else
return true;
#elif defined(LINUX)
HANDLE h = ::open(_path, O_RDONLY | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
int h = ::open(_path, O_RDONLY | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
if (h != -1)
{
::close(h);
Expand Down Expand Up @@ -1294,7 +1333,11 @@ void CLocatorAPI::file_from_archive(CStreamReader*& R, pcstr fname, const file&
make_string("cannot use stream reading for compressed data %s, do not compress data to be streamed", fname));

R = new CStreamReader();
#if defined(WINDOWS)
R->construct(A.hSrcMap, desc.ptr, desc.size_compressed, A.size, BIG_FILE_READER_WINDOW_SIZE);
#elif defined(LINUX)
R->construct(A.hSrcFile, desc.ptr, desc.size_compressed, A.size, BIG_FILE_READER_WINDOW_SIZE);
#endif
}

void CLocatorAPI::copy_file_to_build(IWriter* W, IReader* r) { W->w(r->pointer(), r->length()); }
Expand Down Expand Up @@ -1506,7 +1549,7 @@ void CLocatorAPI::w_close(IWriter*& S)
Register(fname, 0xffffffff, 0, 0, st.st_size, st.st_size, (u32)st.st_mtime);
#elif defined(LINUX)
struct stat st;
::fstat(fname, &st);
::stat(fname, &st);
Register(fname, 0xffffffff, 0, 0, st.st_size, st.st_size, (u32)st.st_mtime);
#endif
}
Expand Down
15 changes: 10 additions & 5 deletions src/xrCore/LocatorAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,18 @@ class XRCORE_API CLocatorAPI : Noncopyable

struct archive
{
u32 size;
u32 vfs_idx;
u32 size = 0;
u32 vfs_idx = u32(-1);
shared_str path;
void *hSrcFile, *hSrcMap;
CInifile* header;
#if defined(WINDOWS)
void *hSrcFile = nullptr;
void *hSrcMap = nullptr
#elif defined(LINUX)
int hSrcFile = 0;
#endif
CInifile* header = nullptr;

archive() : size(0), vfs_idx(u32(-1)), hSrcFile(nullptr), hSrcMap(nullptr), header(nullptr) {}
archive() = default;
void open();
void close();
};
Expand Down
6 changes: 4 additions & 2 deletions src/xrCore/file_stream_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,20 @@ void CFileStreamReader::construct(LPCSTR file_name, const u32& window_size)
struct stat file_info;
::fstat(m_file_handle, &file_info);
u32 file_size = (u32)file_info.st_size;
inherited::construct(&m_file_handle, 0, file_size, file_size, window_size);
inherited::construct(m_file_handle, 0, file_size, file_size, window_size);
#endif
}

void CFileStreamReader::destroy()
{
#if defined(WINDOWS)
HANDLE file_mapping_handle = this->file_mapping_handle();
inherited::destroy();
#if defined(WINDOWS)
CloseHandle(file_mapping_handle);
CloseHandle(m_file_handle);
#elif defined(LINUX)
int file_mapping_handle = this->file_mapping_handle();
inherited::destroy();
::close(m_file_handle);
m_file_handle = -1;
#endif
Expand Down
4 changes: 4 additions & 0 deletions src/xrCore/file_stream_reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ class CFileStreamReader : public CStreamReader
typedef CStreamReader inherited;

private:
#if defined(WINDOWS)
HANDLE m_file_handle;
#elif defined(LINUX)
int m_file_handle;
#endif

public:
virtual void construct(LPCSTR file_name, const u32& window_size);
Expand Down
14 changes: 14 additions & 0 deletions src/xrCore/stream_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <sys/mman.h>
#endif

#if defined(WINDOWS)
void CStreamReader::construct(const HANDLE& file_mapping_handle, const u32& start_offset, const u32& file_size,
const u32& archive_size, const u32& window_size)
{
Expand All @@ -16,6 +17,19 @@ void CStreamReader::construct(const HANDLE& file_mapping_handle, const u32& star

map(0);
}
#elif defined(LINUX)
void CStreamReader::construct(int file_mapping_handle, const u32& start_offset, const u32& file_size,
const u32& archive_size, const u32& window_size)
{
m_file_mapping_handle = file_mapping_handle;
m_start_offset = start_offset;
m_file_size = file_size;
m_archive_size = archive_size;
m_window_size = _max(window_size, FS.dwAllocGranularity);

map(0);
}
#endif

void CStreamReader::destroy() { unmap(); }
void CStreamReader::map(const u32& new_offset)
Expand Down
13 changes: 13 additions & 0 deletions src/xrCore/stream_reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
class XRCORE_API CStreamReader : public IReaderBase<CStreamReader>
{
private:
#if defined(WINDOWS)
HANDLE m_file_mapping_handle;
#elif defined(LINUX)
int m_file_mapping_handle;
#endif
u32 m_start_offset;
u32 m_file_size;
u32 m_archive_size;
Expand All @@ -31,12 +35,21 @@ class XRCORE_API CStreamReader : public IReaderBase<CStreamReader>
IC CStreamReader();

public:
#if defined(WINDOWS)
virtual void construct(const HANDLE& file_mapping_handle, const u32& start_offset, const u32& file_size,
const u32& archive_size, const u32& window_size);
#elif defined(LINUX)
virtual void construct(int file_mapping_handle, const u32& start_offset, const u32& file_size,
const u32& archive_size, const u32& window_size);
#endif
virtual void destroy();

public:
#if defined(WINDOWS)
IC const HANDLE& file_mapping_handle() const;
#elif defined(LINUX)
IC const int& file_mapping_handle() const;
#endif
IC u32 elapsed() const;
IC const u32& length() const;
IC void seek(const int& offset);
Expand Down
5 changes: 5 additions & 0 deletions src/xrCore/stream_reader_inline.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ IC CStreamReader& CStreamReader::operator=(const CStreamReader&)
return (*this);
}

#if defined(WINDOWS)
IC const HANDLE& CStreamReader::file_mapping_handle() const { return (m_file_mapping_handle); }
#elif defined(LINUX)
IC const int& CStreamReader::file_mapping_handle() const { return (m_file_mapping_handle); }
#endif

#if defined(WINDOWS)
IC void CStreamReader::unmap() { UnmapViewOfFile(m_current_map_view_of_file); }
#else
Expand Down
4 changes: 4 additions & 0 deletions src/xrEngine/x_ray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,11 @@ int CApplication::Level_ID(LPCSTR name, LPCSTR ver, bool bSet)
for (; it != it_e; ++it)
{
CLocatorAPI::archive& A = *it;
#if defined(WINDOWS)
if (A.hSrcFile == nullptr)
#elif defined(LINUX)
if (A.hSrcFile == 0)
#endif
{
LPCSTR ln = A.header->r_string("header", "level_name");
LPCSTR lv = A.header->r_string("header", "level_ver");
Expand Down

0 comments on commit 960761c

Please sign in to comment.