blob: 20b6377889d62417af2e6946d400cf21ab112c58 [file] [log] [blame]
Brad Bishop6e60e8b2018-02-01 10:27:11 -05001From 7ae4fcf290ffb0b76374efafeaee575456ac9023 Mon Sep 17 00:00:00 2001
2From: Khem Raj <raj.khem@gmail.com>
3Date: Sun, 6 Nov 2016 23:08:27 -0800
4Subject: [PATCH 01/10] Fix file_Emu on musl
5
6Signed-off-by: Khem Raj <raj.khem@gmail.com>
7---
8 xbmc/cores/DllLoader/exports/emu_msvcrt.cpp | 28 ++--
9 xbmc/cores/DllLoader/exports/emu_msvcrt.h | 2 +-
10 .../DllLoader/exports/util/EmuFileWrapper.cpp | 172 +++++++++------------
11 xbmc/cores/DllLoader/exports/util/EmuFileWrapper.h | 27 ++--
12 xbmc/cores/DllLoader/exports/wrapper.c | 4 +-
13 5 files changed, 99 insertions(+), 134 deletions(-)
14
15diff --git a/xbmc/cores/DllLoader/exports/emu_msvcrt.cpp b/xbmc/cores/DllLoader/exports/emu_msvcrt.cpp
16index ab14942..a39014a 100644
17--- a/xbmc/cores/DllLoader/exports/emu_msvcrt.cpp
18+++ b/xbmc/cores/DllLoader/exports/emu_msvcrt.cpp
19@@ -51,6 +51,7 @@
20 #include <fcntl.h>
21 #include <time.h>
22 #include <signal.h>
23+#include <paths.h>
24 #ifdef TARGET_POSIX
25 #include "PlatformDefs.h" // for __stat64
26 #include "XFileUtils.h"
27@@ -476,13 +477,10 @@ extern "C"
28 EmuFileObject* o = g_emuFileWrapper.GetFileObjectByDescriptor(fd);
29 if (o)
30 {
31- if(!o->used)
32- return NULL;
33-
34 int nmode = convert_fmode(mode);
35 if( (o->mode & nmode) != nmode)
36 CLog::Log(LOGWARNING, "dll_fdopen - mode 0x%x differs from fd mode 0x%x", nmode, o->mode);
37- return &o->file_emu;
38+ return g_emuFileWrapper.GetStreamByFileObject(o);
39 }
40 else if (!IS_STD_DESCRIPTOR(fd))
41 {
42@@ -545,7 +543,7 @@ extern "C"
43 return -1;
44 }
45 object->mode = iMode;
46- return g_emuFileWrapper.GetDescriptorByStream(&object->file_emu);
47+ return g_emuFileWrapper.GetDescriptorByFileObject(object);
48 }
49 delete pFile;
50 return -1;
51@@ -1214,8 +1212,8 @@ extern "C"
52 {
53 FILE* file = NULL;
54 #if defined(TARGET_LINUX) && !defined(TARGET_ANDROID)
55- if (strcmp(filename, MOUNTED) == 0
56- || strcmp(filename, MNTTAB) == 0)
57+ if (strcmp(filename, _PATH_MOUNTED) == 0
58+ || strcmp(filename, _PATH_MNTTAB) == 0)
59 {
60 CLog::Log(LOGINFO, "%s - something opened the mount file, let's hope it knows what it's doing", __FUNCTION__);
61 return fopen(filename, mode);
62@@ -1622,7 +1620,7 @@ extern "C"
63 int ret;
64
65 ret = dll_fgetpos64(stream, &tmpPos);
66-#if !defined(TARGET_POSIX) || defined(TARGET_DARWIN) || defined(TARGET_FREEBSD) || defined(TARGET_ANDROID)
67+#if !defined(__GLIBC__) || defined(TARGET_DARWIN) || defined(TARGET_FREEBSD) || defined(TARGET_ANDROID)
68 *pos = (fpos_t)tmpPos;
69 #else
70 pos->__pos = (off_t)tmpPos.__pos;
71@@ -1635,8 +1633,9 @@ extern "C"
72 CFile* pFile = g_emuFileWrapper.GetFileXbmcByStream(stream);
73 if (pFile != NULL)
74 {
75-#if !defined(TARGET_POSIX) || defined(TARGET_DARWIN) || defined(TARGET_FREEBSD) || defined(TARGET_ANDROID)
76- *pos = pFile->GetPosition();
77+#if !defined(__GLIBC__) || defined(TARGET_DARWIN) || defined(TARGET_FREEBSD) || defined(TARGET_ANDROID)
78+ uint64_t *ppos = (uint64_t *) pos;
79+ *ppos = pFile->GetPosition();
80 #else
81 pos->__pos = pFile->GetPosition();
82 #endif
83@@ -1657,8 +1656,9 @@ extern "C"
84 int fd = g_emuFileWrapper.GetDescriptorByStream(stream);
85 if (fd >= 0)
86 {
87-#if !defined(TARGET_POSIX) || defined(TARGET_DARWIN) || defined(TARGET_FREEBSD) || defined(TARGET_ANDROID)
88- if (dll_lseeki64(fd, *pos, SEEK_SET) >= 0)
89+#if !defined(__GLIBC__) || defined(TARGET_DARWIN) || defined(TARGET_FREEBSD) || defined(TARGET_ANDROID)
90+ const uint64_t *ppos = (const uint64_t *) pos;
91+ if (dll_lseeki64(fd, *ppos, SEEK_SET) >= 0)
92 #else
93 if (dll_lseeki64(fd, (__off64_t)pos->__pos, SEEK_SET) >= 0)
94 #endif
95@@ -1674,7 +1674,7 @@ extern "C"
96 {
97 // it might be something else than a file, or the file is not emulated
98 // let the operating system handle it
99-#if !defined(TARGET_POSIX) || defined(TARGET_DARWIN) || defined(TARGET_FREEBSD) || defined(TARGET_ANDROID)
100+#if !defined(__GLIBC__) || defined(TARGET_DARWIN) || defined(TARGET_FREEBSD) || defined(TARGET_ANDROID)
101 return fsetpos(stream, pos);
102 #else
103 return fsetpos64(stream, pos);
104@@ -1690,7 +1690,7 @@ extern "C"
105 if (fd >= 0)
106 {
107 fpos64_t tmpPos;
108-#if !defined(TARGET_POSIX) || defined(TARGET_DARWIN) || defined(TARGET_FREEBSD) || defined(TARGET_ANDROID)
109+#if !defined(__GLIBC__) || defined(TARGET_DARWIN) || defined(TARGET_FREEBSD) || defined(TARGET_ANDROID)
110 tmpPos= *pos;
111 #else
112 tmpPos.__pos = (off64_t)(pos->__pos);
113diff --git a/xbmc/cores/DllLoader/exports/emu_msvcrt.h b/xbmc/cores/DllLoader/exports/emu_msvcrt.h
114index 3294d9a..c7c483f 100644
115--- a/xbmc/cores/DllLoader/exports/emu_msvcrt.h
116+++ b/xbmc/cores/DllLoader/exports/emu_msvcrt.h
117@@ -24,7 +24,7 @@
118 #define _onexit_t void*
119 #endif
120
121-#if defined(TARGET_DARWIN) || defined(TARGET_FREEBSD) || defined(TARGET_ANDROID)
122+#if defined(TARGET_DARWIN) || defined(TARGET_FREEBSD) || defined(TARGET_ANDROID) || !defined(__GLIBC__)
123 typedef off_t __off_t;
124 typedef int64_t off64_t;
125 typedef off64_t __off64_t;
126diff --git a/xbmc/cores/DllLoader/exports/util/EmuFileWrapper.cpp b/xbmc/cores/DllLoader/exports/util/EmuFileWrapper.cpp
127index 8927d41..e9a2ab0 100644
128--- a/xbmc/cores/DllLoader/exports/util/EmuFileWrapper.cpp
129+++ b/xbmc/cores/DllLoader/exports/util/EmuFileWrapper.cpp
130@@ -52,16 +52,7 @@ constexpr bool isValidFilePtr(FILE* f)
131 }
132 CEmuFileWrapper::CEmuFileWrapper()
133 {
134- // since we always use dlls we might just initialize it directly
135- for (int i = 0; i < MAX_EMULATED_FILES; i++)
136- {
137- memset(&m_files[i], 0, sizeof(EmuFileObject));
138- m_files[i].used = false;
139-#if defined(TARGET_WINDOWS) && (_MSC_VER >= 1900)
140- m_files[i].file_emu._Placeholder = new kodi_iobuf();
141-#endif
142- FileDescriptor(m_files[i].file_emu)->_file = -1;
143- }
144+ memset(m_files, 0, sizeof(m_files));
145 }
146
147 CEmuFileWrapper::~CEmuFileWrapper()
148@@ -73,29 +64,7 @@ void CEmuFileWrapper::CleanUp()
149 {
150 CSingleLock lock(m_criticalSection);
151 for (int i = 0; i < MAX_EMULATED_FILES; i++)
152- {
153- if (m_files[i].used)
154- {
155- m_files[i].file_xbmc->Close();
156- delete m_files[i].file_xbmc;
157-
158- if (m_files[i].file_lock)
159- {
160- delete m_files[i].file_lock;
161- m_files[i].file_lock = nullptr;
162- }
163-#if !defined(TARGET_WINDOWS)
164- //Don't memset on Windows as it overwrites our pointer
165- memset(&m_files[i], 0, sizeof(EmuFileObject));
166-#endif
167- m_files[i].used = false;
168- FileDescriptor(m_files[i].file_emu)->_file = -1;
169- }
170-#if defined(TARGET_WINDOWS) && (_MSC_VER >= 1900)
171- delete static_cast<kodi_iobuf*>(m_files[i].file_emu._Placeholder);
172- m_files[i].file_emu._Placeholder = nullptr;
173-#endif
174- }
175+ UnRegisterFileObject(&m_files[i], true);
176 }
177
178 EmuFileObject* CEmuFileWrapper::RegisterFileObject(XFILE::CFile* pFile)
179@@ -106,13 +75,11 @@ EmuFileObject* CEmuFileWrapper::RegisterFileObject(XFILE::CFile* pFile)
180
181 for (int i = 0; i < MAX_EMULATED_FILES; i++)
182 {
183- if (!m_files[i].used)
184+ if (!m_files[i].file_xbmc)
185 {
186 // found a free location
187 object = &m_files[i];
188- object->used = true;
189 object->file_xbmc = pFile;
190- FileDescriptor(object->file_emu)->_file = (i + FILE_WRAPPER_OFFSET);
191 object->file_lock = new CCriticalSection();
192 break;
193 }
194@@ -121,82 +88,74 @@ EmuFileObject* CEmuFileWrapper::RegisterFileObject(XFILE::CFile* pFile)
195 return object;
196 }
197
198-void CEmuFileWrapper::UnRegisterFileObjectByDescriptor(int fd)
199+void CEmuFileWrapper::UnRegisterFileObject(EmuFileObject *object, bool free_file)
200+
201 {
202- int i = fd - FILE_WRAPPER_OFFSET;
203- if (! (i >= 0 && i < MAX_EMULATED_FILES))
204- return;
205+ if (object && object->file_xbmc)
206+ {
207+ if (object->file_xbmc && free_file)
208+ {
209+ object->file_xbmc->Close();
210+ delete object->file_xbmc;
211+ }
212+ if (object->file_lock)
213+ {
214+ delete object->file_lock;
215+ }
216
217- if (!m_files[i].used)
218- return;
219+ memset(object, 0, sizeof(*object));
220+ }
221+}
222
223+void CEmuFileWrapper::UnRegisterFileObjectByDescriptor(int fd)
224+{
225 CSingleLock lock(m_criticalSection);
226-
227- // we assume the emulated function alreay deleted the CFile object
228- if (m_files[i].file_lock)
229- {
230- delete m_files[i].file_lock;
231- m_files[i].file_lock = nullptr;
232- }
233-#if !defined(TARGET_WINDOWS)
234- //Don't memset on Windows as it overwrites our pointer
235- memset(&m_files[i], 0, sizeof(EmuFileObject));
236-#endif
237- m_files[i].used = false;
238- FileDescriptor(m_files[i].file_emu)->_file = -1;
239+ UnRegisterFileObject(GetFileObjectByDescriptor(fd), false);
240 }
241
242 void CEmuFileWrapper::UnRegisterFileObjectByStream(FILE* stream)
243 {
244 if (isValidFilePtr(stream))
245 {
246- return UnRegisterFileObjectByDescriptor(FileDescriptor(*stream)->_file);
247+ CSingleLock lock(m_criticalSection);
248+ UnRegisterFileObject(GetFileObjectByStream(stream), false);
249 }
250 }
251
252 void CEmuFileWrapper::LockFileObjectByDescriptor(int fd)
253 {
254- int i = fd - FILE_WRAPPER_OFFSET;
255- if (i >= 0 && i < MAX_EMULATED_FILES)
256+ EmuFileObject* object = GetFileObjectByDescriptor(fd);
257+ if (object && object->file_xbmc)
258 {
259- if (m_files[i].used)
260- {
261- m_files[i].file_lock->lock();
262- }
263+ object->file_lock->lock();
264 }
265 }
266
267 bool CEmuFileWrapper::TryLockFileObjectByDescriptor(int fd)
268 {
269- int i = fd - FILE_WRAPPER_OFFSET;
270- if (i >= 0 && i < MAX_EMULATED_FILES)
271+ EmuFileObject* object = GetFileObjectByDescriptor(fd);
272+ if (object && object->file_xbmc)
273 {
274- if (m_files[i].used)
275- {
276- return m_files[i].file_lock->try_lock();
277- }
278+ return object->file_lock->try_lock();
279 }
280 return false;
281 }
282
283 void CEmuFileWrapper::UnlockFileObjectByDescriptor(int fd)
284 {
285- int i = fd - FILE_WRAPPER_OFFSET;
286- if (i >= 0 && i < MAX_EMULATED_FILES)
287+ EmuFileObject* object = GetFileObjectByDescriptor(fd);
288+ if (object && object->file_xbmc)
289 {
290- if (m_files[i].used)
291- {
292- m_files[i].file_lock->unlock();
293- }
294+ object->file_lock->unlock();
295 }
296 }
297
298 EmuFileObject* CEmuFileWrapper::GetFileObjectByDescriptor(int fd)
299 {
300- int i = fd - FILE_WRAPPER_OFFSET;
301+ int i = fd - 0x7000000;
302 if (i >= 0 && i < MAX_EMULATED_FILES)
303 {
304- if (m_files[i].used)
305+ if (m_files[i].file_xbmc)
306 {
307 return &m_files[i];
308 }
309@@ -204,20 +163,39 @@ EmuFileObject* CEmuFileWrapper::GetFileObjectByDescriptor(int fd)
310 return nullptr;
311 }
312
313+int CEmuFileWrapper::GetDescriptorByFileObject(EmuFileObject *object)
314+{
315+ int i = object - m_files;
316+ if (i >= 0 && i < MAX_EMULATED_FILES)
317+ {
318+ return 0x7000000 + i;
319+ }
320+
321+ return -1;
322+}
323+
324 EmuFileObject* CEmuFileWrapper::GetFileObjectByStream(FILE* stream)
325 {
326- if (isValidFilePtr(stream))
327+ EmuFileObject *object = (EmuFileObject*) stream;
328+ if (object >= &m_files[0] || object < &m_files[MAX_EMULATED_FILES])
329 {
330- return GetFileObjectByDescriptor(FileDescriptor(*stream)->_file);
331+ if (object->file_xbmc)
332+ {
333+ return object;
334+ }
335 }
336+ return NULL;
337+}
338
339- return nullptr;
340+FILE* CEmuFileWrapper::GetStreamByFileObject(EmuFileObject *object)
341+{
342+ return (FILE*) object;
343 }
344
345 XFILE::CFile* CEmuFileWrapper::GetFileXbmcByDescriptor(int fd)
346 {
347 auto object = GetFileObjectByDescriptor(fd);
348- if (object != nullptr && object->used)
349+ if (object != nullptr)
350 {
351 return object->file_xbmc;
352 }
353@@ -228,8 +206,9 @@ XFILE::CFile* CEmuFileWrapper::GetFileXbmcByStream(FILE* stream)
354 {
355 if (isValidFilePtr(stream))
356 {
357- auto object = GetFileObjectByDescriptor(FileDescriptor(*stream)->_file);
358- if (object != nullptr && object->used)
359+ EmuFileObject* object = GetFileObjectByStream(stream);
360+ if (object != NULL)
361+
362 {
363 return object->file_xbmc;
364 }
365@@ -239,32 +218,21 @@ XFILE::CFile* CEmuFileWrapper::GetFileXbmcByStream(FILE* stream)
366
367 int CEmuFileWrapper::GetDescriptorByStream(FILE* stream)
368 {
369- if (isValidFilePtr(stream))
370- {
371- int i = FileDescriptor(*stream)->_file - FILE_WRAPPER_OFFSET;
372- if (i >= 0 && i < MAX_EMULATED_FILES)
373- {
374- return i + FILE_WRAPPER_OFFSET;
375- }
376- }
377- return -1;
378+ return GetDescriptorByFileObject(GetFileObjectByStream(stream));
379 }
380
381 FILE* CEmuFileWrapper::GetStreamByDescriptor(int fd)
382 {
383- auto object = GetFileObjectByDescriptor(fd);
384- if (object != nullptr && object->used)
385- {
386- return &object->file_emu;
387- }
388- return nullptr;
389+ return GetStreamByFileObject(GetFileObjectByDescriptor(fd));
390+}
391+
392+bool CEmuFileWrapper::DescriptorIsEmulatedFile(int fd)
393+{
394+ return GetFileObjectByDescriptor(fd) != NULL;
395 }
396
397 bool CEmuFileWrapper::StreamIsEmulatedFile(FILE* stream)
398 {
399- if (isValidFilePtr(stream))
400- {
401- return DescriptorIsEmulatedFile(FileDescriptor(*stream)->_file);
402- }
403- return false;
404+ return GetFileObjectByStream(stream) != NULL;
405 }
406+
407diff --git a/xbmc/cores/DllLoader/exports/util/EmuFileWrapper.h b/xbmc/cores/DllLoader/exports/util/EmuFileWrapper.h
408index 786fa85..311a5cf 100644
409--- a/xbmc/cores/DllLoader/exports/util/EmuFileWrapper.h
410+++ b/xbmc/cores/DllLoader/exports/util/EmuFileWrapper.h
411@@ -25,14 +25,14 @@
412 #include "system.h"
413 #include "threads/CriticalSection.h"
414
415-#if defined(TARGET_POSIX) && !defined(TARGET_DARWIN) && !defined(TARGET_FREEBSD) && !defined(TARGET_ANDROID) && !defined(__UCLIBC__)
416-#define _file _fileno
417-#elif defined(__UCLIBC__)
418-#define _file __filedes
419-#endif
420+//#if defined(TARGET_POSIX) && !defined(TARGET_DARWIN) && !defined(TARGET_FREEBSD) && !defined(TARGET_ANDROID) && !defined(__UCLIBC__)
421+//#define _file _fileno
422+//#elif defined(__UCLIBC__)
423+//#define _file __filedes
424+//#endif
425
426 #define MAX_EMULATED_FILES 50
427-#define FILE_WRAPPER_OFFSET 0x00000200
428+//#define FILE_WRAPPER_OFFSET 0x00000200
429
430 namespace XFILE
431 {
432@@ -47,12 +47,9 @@ struct kodi_iobuf {
433
434 typedef struct stEmuFileObject
435 {
436- FILE file_emu;
437 XFILE::CFile* file_xbmc;
438 CCriticalSection *file_lock;
439 int mode;
440- //Stick this last to avoid 3-7 bytes of padding
441- bool used;
442 } EmuFileObject;
443
444 class CEmuFileWrapper
445@@ -67,22 +64,22 @@ public:
446 void CleanUp();
447
448 EmuFileObject* RegisterFileObject(XFILE::CFile* pFile);
449+ void UnRegisterFileObject(EmuFileObject*, bool free_file);
450 void UnRegisterFileObjectByDescriptor(int fd);
451 void UnRegisterFileObjectByStream(FILE* stream);
452 void LockFileObjectByDescriptor(int fd);
453 bool TryLockFileObjectByDescriptor(int fd);
454 void UnlockFileObjectByDescriptor(int fd);
455 EmuFileObject* GetFileObjectByDescriptor(int fd);
456+ int GetDescriptorByFileObject(EmuFileObject*);
457 EmuFileObject* GetFileObjectByStream(FILE* stream);
458+ FILE* GetStreamByFileObject(EmuFileObject*);
459 XFILE::CFile* GetFileXbmcByDescriptor(int fd);
460 XFILE::CFile* GetFileXbmcByStream(FILE* stream);
461- static int GetDescriptorByStream(FILE* stream);
462+ int GetDescriptorByStream(FILE* stream);
463 FILE* GetStreamByDescriptor(int fd);
464- static constexpr bool DescriptorIsEmulatedFile(int fd)
465- {
466- return fd >= FILE_WRAPPER_OFFSET && fd < FILE_WRAPPER_OFFSET + MAX_EMULATED_FILES;
467- }
468- static bool StreamIsEmulatedFile(FILE* stream);
469+ bool DescriptorIsEmulatedFile(int fd);
470+ bool StreamIsEmulatedFile(FILE* stream);
471 private:
472 EmuFileObject m_files[MAX_EMULATED_FILES];
473 CCriticalSection m_criticalSection;
474diff --git a/xbmc/cores/DllLoader/exports/wrapper.c b/xbmc/cores/DllLoader/exports/wrapper.c
475index e363662..07825f3 100644
476--- a/xbmc/cores/DllLoader/exports/wrapper.c
477+++ b/xbmc/cores/DllLoader/exports/wrapper.c
478@@ -39,13 +39,13 @@
479 #endif
480 #include <dlfcn.h>
481
482-#if defined(TARGET_DARWIN) || defined(TARGET_FREEBSD) || defined(TARGET_ANDROID)
483+#if defined(TARGET_DARWIN) || defined(TARGET_FREEBSD) || defined(TARGET_ANDROID) || !defined(__GLIBC__)
484 typedef off_t __off_t;
485 typedef int64_t off64_t;
486 typedef off64_t __off64_t;
487 typedef fpos_t fpos64_t;
488 #define stat64 stat
489-#if defined(TARGET_DARWIN) || defined(TARGET_ANDROID)
490+#if defined(TARGET_DARWIN) || defined(TARGET_ANDROID) || !defined(__GLIBC__)
491 #define _G_va_list va_list
492 #endif
493 #endif
494--
4952.10.2
496