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