Skip to content

Commit e75eea6

Browse files
committed
More linux port for LocatorAPI. Part 1
1 parent 4742925 commit e75eea6

File tree

2 files changed

+64
-4
lines changed

2 files changed

+64
-4
lines changed

src/xrCore/LocatorAPI.cpp

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@
99
#pragma warning(disable : 4995)
1010
#ifdef WINDOWS
1111
#include <direct.h>
12-
#include <fcntl.h>
1312
#include <sys/stat.h>
13+
#else
14+
#include <sys/mman.h>
1415
#endif
16+
#include <fcntl.h>
1517
#pragma warning(pop)
1618

1719
#include "FS_internal.h"
@@ -159,10 +161,14 @@ CLocatorAPI::CLocatorAPI() : bNoRecurse(true), m_auth_code(0),
159161
#endif // CONFIG_PROFILE_LOCKS
160162
{
161163
m_Flags.zero();
164+
#ifdef WINDOWS
162165
// get page size
163166
SYSTEM_INFO sys_inf;
164167
GetSystemInfo(&sys_inf);
165168
dwAllocGranularity = sys_inf.dwAllocationGranularity;
169+
#else
170+
dwAllocGranularity = sysconf(_SC_PAGE_SIZE);
171+
#endif
166172
m_iLockRescan = 0;
167173
dwOpenCounter = 0;
168174
}
@@ -253,24 +259,43 @@ IReader* open_chunk(void* ptr, u32 ID)
253259
{
254260
u32 dwType, dwSize;
255261
DWORD read_byte;
262+
#ifdef WINDOWS
256263
u32 pt = SetFilePointer(ptr, 0, nullptr, FILE_BEGIN);
257264
VERIFY(pt != INVALID_SET_FILE_POINTER);
265+
#else
266+
rewind(ptr);
267+
#endif
258268
while (true)
259269
{
270+
#ifdef WINDOWS
260271
bool res = ReadFile(ptr, &dwType, 4, &read_byte, nullptr);
272+
#else
273+
read_byte = read(ptr, &dwType, 4);
274+
bool res = true;
275+
#endif
261276
if (read_byte == 0)
262277
return nullptr;
263278
//. VERIFY(res&&(read_byte==4));
264279

280+
#ifdef WINDOWS
265281
res = ReadFile(ptr, &dwSize, 4, &read_byte, nullptr);
282+
#else
283+
read_byte = read(ptr, &dwSize, 4);
284+
res = true;
285+
#endif
266286
if (read_byte == 0)
267287
return nullptr;
268288
//. VERIFY(res&&(read_byte==4));
269289

270290
if ((dwType & ~CFS_CompressMark) == ID)
271291
{
272292
u8* src_data = xr_alloc<u8>(dwSize);
293+
#ifdef WINDOWS
273294
res = ReadFile(ptr, src_data, dwSize, &read_byte, nullptr);
295+
#else
296+
read_byte = read(ptr, src_data, dwSize);
297+
res = true;
298+
#endif
274299
VERIFY(res && (read_byte == dwSize));
275300
if (dwType & CFS_CompressMark)
276301
{
@@ -282,9 +307,11 @@ IReader* open_chunk(void* ptr, u32 ID)
282307
}
283308
return new CTempReader(src_data, dwSize, 0);
284309
}
310+
#ifdef WINDOWS
285311
pt = SetFilePointer(ptr, dwSize, nullptr, FILE_CURRENT);
286312
if (pt == INVALID_SET_FILE_POINTER)
287313
return nullptr;
314+
#endif
288315
}
289316
return nullptr;
290317
};
@@ -383,20 +410,35 @@ void CLocatorAPI::archive::open()
383410
if (hSrcFile && hSrcMap)
384411
return;
385412

413+
#ifdef WINDOWS
386414
hSrcFile = CreateFile(*path, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, OPEN_EXISTING, 0, nullptr);
387415
R_ASSERT(hSrcFile != INVALID_HANDLE_VALUE);
388416
hSrcMap = CreateFileMapping(hSrcFile, nullptr, PAGE_READONLY, 0, 0, nullptr);
389417
R_ASSERT(hSrcMap != INVALID_HANDLE_VALUE);
390418
size = GetFileSize(hSrcFile, nullptr);
391419
R_ASSERT(size > 0);
420+
#else
421+
hSrcFile = ::open(*path, O_RDONLY|O_NONBLOCK);
422+
struct stat statbuf;
423+
int rc = fstat(hSrcFile, &statbuf);
424+
hSrcMap = mmap(0, statbuf.st_size, PROT_READ, MAP_SHARED, hSrcFile, 0);
425+
size = (rc == 0 ? statbuf.st_size : -1);
426+
#endif
392427
}
393428

394429
void CLocatorAPI::archive::close()
395430
{
431+
#ifdef WINDOWS
396432
CloseHandle(hSrcMap);
397433
hSrcMap = nullptr;
398434
CloseHandle(hSrcFile);
399435
hSrcFile = nullptr;
436+
#else
437+
munmap(hSrcMap, size);
438+
hSrcMap = nullptr;
439+
::close(hSrcFile);
440+
hSrcFile = nullptr;
441+
#endif
400442
}
401443

402444
void CLocatorAPI::ProcessArchive(pcstr _path)

src/xrCore/LocatorAPI.h

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,29 @@
44
#pragma warning(disable : 4995)
55
#ifdef WINDOWS
66
#include <io.h>
7-
#else
8-
#define _finddata_t _finddata64i32_t
7+
#else // WINDOWS
8+
#include <inttypes.h>
9+
#include <sys/types.h> /* To get time_t. */
10+
11+
#ifndef FILENAME_MAX
12+
#define FILENAME_MAX (260)
913
#endif
10-
#pragma warning(pop)
1114

15+
#define _finddata_t _finddata64i32_t
16+
typedef int64_t __int64;
17+
typedef __int64 __time64_t;
18+
typedef unsigned long _fsize_t;
19+
20+
struct _finddata64i32_t {
21+
unsigned attrib;
22+
__time64_t time_create;
23+
__time64_t time_access;
24+
__time64_t time_write;
25+
_fsize_t size;
26+
char name[FILENAME_MAX];
27+
};
28+
#endif // WINDOWS
29+
#pragma warning(pop)
1230

1331
#include "Common/Util.hpp"
1432
#include "LocatorAPI_defs.h"

0 commit comments

Comments
 (0)