@@ -256,47 +256,33 @@ const CLocatorAPI::file* CLocatorAPI::Register(
256256 return &*result;
257257}
258258
259+ #if defined(WINDOWS)
259260IReader* open_chunk (void * ptr, u32 ID)
260261{
261262 u32 dwType, dwSize;
262263 DWORD read_byte;
263- #ifdef WINDOWS
264264 u32 pt = SetFilePointer (ptr, 0 , nullptr , FILE_BEGIN);
265265 VERIFY (pt != INVALID_SET_FILE_POINTER);
266- #else
267- ::lseek (ptr, 0L , SEEK_SET);
268- #endif
266+
269267 while (true )
270268 {
271- #ifdef WINDOWS
272269 bool res = ReadFile (ptr, &dwType, 4 , &read_byte, nullptr );
273- #elif defined(LINUX)
274- read_byte = ::read (ptr, &dwType, 4 );
275- bool res = (read_byte != -1 );
276- #endif
270+
277271 if (read_byte == 0 )
278272 return nullptr ;
279273 // . VERIFY(res&&(read_byte==4));
280274
281- #ifdef WINDOWS
282275 res = ReadFile (ptr, &dwSize, 4 , &read_byte, nullptr );
283- #else
284- read_byte = ::read (ptr, &dwSize, 4 );
285- res = (read_byte != -1 );
286- #endif
276+
287277 if (read_byte == 0 )
288278 return nullptr ;
289279 // . VERIFY(res&&(read_byte==4));
290280
291281 if ((dwType & ~CFS_CompressMark) == ID)
292282 {
293283 u8 * src_data = xr_alloc<u8 >(dwSize);
294- #ifdef WINDOWS
295284 res = ReadFile (ptr, src_data, dwSize, &read_byte, nullptr );
296- #else
297- read_byte = ::read (ptr, src_data, dwSize);
298- res = (read_byte != -1 );
299- #endif
285+
300286 VERIFY (res && (read_byte == dwSize));
301287 if (dwType & CFS_CompressMark)
302288 {
@@ -308,17 +294,62 @@ IReader* open_chunk(void* ptr, u32 ID)
308294 }
309295 return new CTempReader (src_data, dwSize, 0 );
310296 }
311- # ifdef WINDOWS
297+
312298 pt = SetFilePointer (ptr, dwSize, nullptr , FILE_CURRENT);
313299 if (pt == INVALID_SET_FILE_POINTER)
314300 return nullptr ;
315- # else
316- if (- 1 == :: lseek (ptr, dwSize, SEEK_CUR))
317- return nullptr ;
301+ }
302+ return nullptr ;
303+ } ;
318304#endif
305+
306+ #if defined(LINUX)
307+ IReader* open_chunk (int fd, u32 ID)
308+ {
309+ u32 dwType, dwSize;
310+ DWORD read_byte;
311+ ::lseek (fd, 0L , SEEK_SET);
312+
313+ while (true )
314+ {
315+ read_byte = ::read (fd, &dwType, 4 );
316+ bool res = (read_byte != -1 );
317+
318+ if (read_byte == 0 )
319+ return nullptr ;
320+ // . VERIFY(res&&(read_byte==4));
321+
322+ read_byte = ::read (fd, &dwSize, 4 );
323+ res = (read_byte != -1 );
324+
325+ if (read_byte == 0 )
326+ return nullptr ;
327+ // . VERIFY(res&&(read_byte==4));
328+
329+ if ((dwType & ~CFS_CompressMark) == ID)
330+ {
331+ u8 * src_data = xr_alloc<u8 >(dwSize);
332+ read_byte = ::read (fd, src_data, dwSize);
333+ res = (read_byte != -1 );
334+
335+ VERIFY (res && (read_byte == dwSize));
336+ if (dwType & CFS_CompressMark)
337+ {
338+ BYTE* dest;
339+ unsigned dest_sz;
340+ _decompressLZ (&dest, &dest_sz, src_data, dwSize);
341+ xr_free (src_data);
342+ return new CTempReader (dest, dest_sz, 0 );
343+ }
344+ return new CTempReader (src_data, dwSize, 0 );
345+ }
346+
347+ if (-1 == ::lseek (fd, dwSize, SEEK_CUR))
348+ return nullptr ;
319349 }
320350 return nullptr ;
321351};
352+ #endif
322353
323354void CLocatorAPI::LoadArchive (archive& A, pcstr entrypoint)
324355{
@@ -416,17 +447,21 @@ void CLocatorAPI::LoadArchive(archive& A, pcstr entrypoint)
416447
417448void CLocatorAPI::archive::open ()
418449{
450+ #if defined(WINDOWS)
419451 // Open the file
420452 if (hSrcFile && hSrcMap)
421453 return ;
422454
423- #if defined(WINDOWS)
424455 hSrcFile = CreateFile (*path, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr , OPEN_EXISTING, 0 , nullptr );
425456 R_ASSERT (hSrcFile != INVALID_HANDLE_VALUE);
426457 hSrcMap = CreateFileMapping (hSrcFile, nullptr , PAGE_READONLY, 0 , 0 , nullptr );
427458 R_ASSERT (hSrcMap != INVALID_HANDLE_VALUE);
428459 size = GetFileSize (hSrcFile, nullptr );
429460#elif defined(LINUX)
461+ // Open the file
462+ if (hSrcFile)
463+ return ;
464+
430465 hSrcFile = ::open (*path, O_RDONLY, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
431466 R_ASSERT (hSrcFile != -1 );
432467 struct stat file_info;
@@ -505,7 +540,11 @@ bool CLocatorAPI::load_all_unloaded_archives()
505540 bool res = false ;
506541 for (auto & archive : m_archives)
507542 {
543+ #if defined(WINDOWS)
508544 if (archive.hSrcFile == nullptr )
545+ #elif defined(LINUX)
546+ if (archive.hSrcFile == 0 )
547+ #endif
509548 {
510549 LoadArchive (archive);
511550 res = true ;
@@ -593,7 +632,7 @@ bool ignore_path(const char* _path)
593632 else
594633 return true ;
595634#elif defined(LINUX)
596- HANDLE h = ::open (_path, O_RDONLY | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
635+ int h = ::open (_path, O_RDONLY | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
597636 if (h != -1 )
598637 {
599638 ::close (h);
@@ -1294,7 +1333,11 @@ void CLocatorAPI::file_from_archive(CStreamReader*& R, pcstr fname, const file&
12941333 make_string (" cannot use stream reading for compressed data %s, do not compress data to be streamed" , fname));
12951334
12961335 R = new CStreamReader ();
1336+ #if defined(WINDOWS)
12971337 R->construct (A.hSrcMap , desc.ptr , desc.size_compressed , A.size , BIG_FILE_READER_WINDOW_SIZE);
1338+ #elif defined(LINUX)
1339+ R->construct (A.hSrcFile , desc.ptr , desc.size_compressed , A.size , BIG_FILE_READER_WINDOW_SIZE);
1340+ #endif
12981341}
12991342
13001343void CLocatorAPI::copy_file_to_build (IWriter* W, IReader* r) { W->w (r->pointer (), r->length ()); }
@@ -1506,7 +1549,7 @@ void CLocatorAPI::w_close(IWriter*& S)
15061549 Register (fname, 0xffffffff , 0 , 0 , st.st_size , st.st_size , (u32 )st.st_mtime );
15071550#elif defined(LINUX)
15081551 struct stat st;
1509- ::fstat (fname, &st);
1552+ ::stat (fname, &st);
15101553 Register (fname, 0xffffffff , 0 , 0 , st.st_size , st.st_size , (u32 )st.st_mtime );
15111554#endif
15121555 }
0 commit comments