Skip to content

Commit

Permalink
More linux port for LocatorAPI. Part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
q4a committed May 19, 2018
1 parent 4742925 commit e75eea6
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 4 deletions.
44 changes: 43 additions & 1 deletion src/xrCore/LocatorAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
#pragma warning(disable : 4995)
#ifdef WINDOWS
#include <direct.h>
#include <fcntl.h>
#include <sys/stat.h>
#else
#include <sys/mman.h>
#endif
#include <fcntl.h>
#pragma warning(pop)

#include "FS_internal.h"
Expand Down Expand Up @@ -159,10 +161,14 @@ CLocatorAPI::CLocatorAPI() : bNoRecurse(true), m_auth_code(0),
#endif // CONFIG_PROFILE_LOCKS
{
m_Flags.zero();
#ifdef WINDOWS
// get page size
SYSTEM_INFO sys_inf;
GetSystemInfo(&sys_inf);
dwAllocGranularity = sys_inf.dwAllocationGranularity;
#else
dwAllocGranularity = sysconf(_SC_PAGE_SIZE);
#endif
m_iLockRescan = 0;
dwOpenCounter = 0;
}
Expand Down Expand Up @@ -253,24 +259,43 @@ 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
rewind(ptr);
#endif
while (true)
{
#ifdef WINDOWS
bool res = ReadFile(ptr, &dwType, 4, &read_byte, nullptr);
#else
read_byte = read(ptr, &dwType, 4);
bool res = true;
#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 = true;
#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 = true;
#endif
VERIFY(res && (read_byte == dwSize));
if (dwType & CFS_CompressMark)
{
Expand All @@ -282,9 +307,11 @@ 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;
#endif
}
return nullptr;
};
Expand Down Expand Up @@ -383,20 +410,35 @@ void CLocatorAPI::archive::open()
if (hSrcFile && hSrcMap)
return;

#ifdef 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);
R_ASSERT(size > 0);
#else
hSrcFile = ::open(*path, O_RDONLY|O_NONBLOCK);
struct stat statbuf;
int rc = fstat(hSrcFile, &statbuf);
hSrcMap = mmap(0, statbuf.st_size, PROT_READ, MAP_SHARED, hSrcFile, 0);
size = (rc == 0 ? statbuf.st_size : -1);
#endif
}

void CLocatorAPI::archive::close()
{
#ifdef WINDOWS
CloseHandle(hSrcMap);
hSrcMap = nullptr;
CloseHandle(hSrcFile);
hSrcFile = nullptr;
#else
munmap(hSrcMap, size);
hSrcMap = nullptr;
::close(hSrcFile);
hSrcFile = nullptr;
#endif
}

void CLocatorAPI::ProcessArchive(pcstr _path)
Expand Down
24 changes: 21 additions & 3 deletions src/xrCore/LocatorAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,29 @@
#pragma warning(disable : 4995)
#ifdef WINDOWS
#include <io.h>
#else
#define _finddata_t _finddata64i32_t
#else // WINDOWS
#include <inttypes.h>
#include <sys/types.h> /* To get time_t. */

#ifndef FILENAME_MAX
#define FILENAME_MAX (260)
#endif
#pragma warning(pop)

#define _finddata_t _finddata64i32_t
typedef int64_t __int64;
typedef __int64 __time64_t;
typedef unsigned long _fsize_t;

struct _finddata64i32_t {
unsigned attrib;
__time64_t time_create;
__time64_t time_access;
__time64_t time_write;
_fsize_t size;
char name[FILENAME_MAX];
};
#endif // WINDOWS
#pragma warning(pop)

#include "Common/Util.hpp"
#include "LocatorAPI_defs.h"
Expand Down

0 comments on commit e75eea6

Please sign in to comment.