| From 7ae4fcf290ffb0b76374efafeaee575456ac9023 Mon Sep 17 00:00:00 2001 |
| From: Khem Raj <raj.khem@gmail.com> |
| Date: Sun, 6 Nov 2016 23:08:27 -0800 |
| Subject: [PATCH 01/10] Fix file_Emu on musl |
| |
| Signed-off-by: Khem Raj <raj.khem@gmail.com> |
| --- |
| xbmc/cores/DllLoader/exports/emu_msvcrt.cpp | 28 ++-- |
| xbmc/cores/DllLoader/exports/emu_msvcrt.h | 2 +- |
| .../DllLoader/exports/util/EmuFileWrapper.cpp | 172 +++++++++------------ |
| xbmc/cores/DllLoader/exports/util/EmuFileWrapper.h | 27 ++-- |
| xbmc/cores/DllLoader/exports/wrapper.c | 4 +- |
| 5 files changed, 99 insertions(+), 134 deletions(-) |
| |
| diff --git a/xbmc/cores/DllLoader/exports/emu_msvcrt.cpp b/xbmc/cores/DllLoader/exports/emu_msvcrt.cpp |
| index ab14942..a39014a 100644 |
| --- a/xbmc/cores/DllLoader/exports/emu_msvcrt.cpp |
| +++ b/xbmc/cores/DllLoader/exports/emu_msvcrt.cpp |
| @@ -51,6 +51,7 @@ |
| #include <fcntl.h> |
| #include <time.h> |
| #include <signal.h> |
| +#include <paths.h> |
| #ifdef TARGET_POSIX |
| #include "PlatformDefs.h" // for __stat64 |
| #include "XFileUtils.h" |
| @@ -476,13 +477,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)) |
| { |
| @@ -545,7 +543,7 @@ extern "C" |
| return -1; |
| } |
| object->mode = iMode; |
| - return g_emuFileWrapper.GetDescriptorByStream(&object->file_emu); |
| + return g_emuFileWrapper.GetDescriptorByFileObject(object); |
| } |
| delete pFile; |
| return -1; |
| @@ -1214,8 +1212,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); |
| @@ -1622,7 +1620,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; |
| @@ -1635,8 +1633,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 |
| @@ -1657,8 +1656,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 |
| @@ -1674,7 +1674,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); |
| @@ -1690,7 +1690,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 3294d9a..c7c483f 100644 |
| --- a/xbmc/cores/DllLoader/exports/emu_msvcrt.h |
| +++ b/xbmc/cores/DllLoader/exports/emu_msvcrt.h |
| @@ -24,7 +24,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 8927d41..e9a2ab0 100644 |
| --- a/xbmc/cores/DllLoader/exports/util/EmuFileWrapper.cpp |
| +++ b/xbmc/cores/DllLoader/exports/util/EmuFileWrapper.cpp |
| @@ -52,16 +52,7 @@ constexpr bool isValidFilePtr(FILE* f) |
| } |
| 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; |
| -#if defined(TARGET_WINDOWS) && (_MSC_VER >= 1900) |
| - m_files[i].file_emu._Placeholder = new kodi_iobuf(); |
| -#endif |
| - FileDescriptor(m_files[i].file_emu)->_file = -1; |
| - } |
| + memset(m_files, 0, sizeof(m_files)); |
| } |
| |
| CEmuFileWrapper::~CEmuFileWrapper() |
| @@ -73,29 +64,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 = nullptr; |
| - } |
| -#if !defined(TARGET_WINDOWS) |
| - //Don't memset on Windows as it overwrites our pointer |
| - memset(&m_files[i], 0, sizeof(EmuFileObject)); |
| -#endif |
| - m_files[i].used = false; |
| - FileDescriptor(m_files[i].file_emu)->_file = -1; |
| - } |
| -#if defined(TARGET_WINDOWS) && (_MSC_VER >= 1900) |
| - delete static_cast<kodi_iobuf*>(m_files[i].file_emu._Placeholder); |
| - m_files[i].file_emu._Placeholder = nullptr; |
| -#endif |
| - } |
| + UnRegisterFileObject(&m_files[i], true); |
| } |
| |
| EmuFileObject* CEmuFileWrapper::RegisterFileObject(XFILE::CFile* pFile) |
| @@ -106,13 +75,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; |
| - FileDescriptor(object->file_emu)->_file = (i + FILE_WRAPPER_OFFSET); |
| object->file_lock = new CCriticalSection(); |
| break; |
| } |
| @@ -121,82 +88,74 @@ 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)) |
| - return; |
| + if (object && object->file_xbmc) |
| + { |
| + if (object->file_xbmc && free_file) |
| + { |
| + object->file_xbmc->Close(); |
| + delete object->file_xbmc; |
| + } |
| + if (object->file_lock) |
| + { |
| + delete object->file_lock; |
| + } |
| |
| - if (!m_files[i].used) |
| - return; |
| + memset(object, 0, sizeof(*object)); |
| + } |
| +} |
| |
| +void CEmuFileWrapper::UnRegisterFileObjectByDescriptor(int fd) |
| +{ |
| CSingleLock lock(m_criticalSection); |
| - |
| - // we assume the emulated function alreay deleted the CFile object |
| - if (m_files[i].file_lock) |
| - { |
| - delete m_files[i].file_lock; |
| - m_files[i].file_lock = nullptr; |
| - } |
| -#if !defined(TARGET_WINDOWS) |
| - //Don't memset on Windows as it overwrites our pointer |
| - memset(&m_files[i], 0, sizeof(EmuFileObject)); |
| -#endif |
| - m_files[i].used = false; |
| - FileDescriptor(m_files[i].file_emu)->_file = -1; |
| + UnRegisterFileObject(GetFileObjectByDescriptor(fd), false); |
| } |
| |
| void CEmuFileWrapper::UnRegisterFileObjectByStream(FILE* stream) |
| { |
| if (isValidFilePtr(stream)) |
| { |
| - return UnRegisterFileObjectByDescriptor(FileDescriptor(*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) |
| + EmuFileObject* object = GetFileObjectByDescriptor(fd); |
| + if (object && object->file_xbmc) |
| { |
| - if (m_files[i].used) |
| - { |
| - return m_files[i].file_lock->try_lock(); |
| - } |
| + 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) |
| + EmuFileObject* object = GetFileObjectByDescriptor(fd); |
| + if (object && object->file_xbmc) |
| { |
| - if (m_files[i].used) |
| - { |
| - m_files[i].file_lock->unlock(); |
| - } |
| + 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]; |
| } |
| @@ -204,20 +163,39 @@ EmuFileObject* CEmuFileWrapper::GetFileObjectByDescriptor(int fd) |
| return nullptr; |
| } |
| |
| +int CEmuFileWrapper::GetDescriptorByFileObject(EmuFileObject *object) |
| +{ |
| + int i = object - m_files; |
| + if (i >= 0 && i < MAX_EMULATED_FILES) |
| + { |
| + return 0x7000000 + i; |
| + } |
| + |
| + return -1; |
| +} |
| + |
| EmuFileObject* CEmuFileWrapper::GetFileObjectByStream(FILE* stream) |
| { |
| - if (isValidFilePtr(stream)) |
| + EmuFileObject *object = (EmuFileObject*) stream; |
| + if (object >= &m_files[0] || object < &m_files[MAX_EMULATED_FILES]) |
| { |
| - return GetFileObjectByDescriptor(FileDescriptor(*stream)->_file); |
| + if (object->file_xbmc) |
| + { |
| + return object; |
| + } |
| } |
| + return NULL; |
| +} |
| |
| - return nullptr; |
| +FILE* CEmuFileWrapper::GetStreamByFileObject(EmuFileObject *object) |
| +{ |
| + return (FILE*) object; |
| } |
| |
| XFILE::CFile* CEmuFileWrapper::GetFileXbmcByDescriptor(int fd) |
| { |
| auto object = GetFileObjectByDescriptor(fd); |
| - if (object != nullptr && object->used) |
| + if (object != nullptr) |
| { |
| return object->file_xbmc; |
| } |
| @@ -228,8 +206,9 @@ XFILE::CFile* CEmuFileWrapper::GetFileXbmcByStream(FILE* stream) |
| { |
| if (isValidFilePtr(stream)) |
| { |
| - auto object = GetFileObjectByDescriptor(FileDescriptor(*stream)->_file); |
| - if (object != nullptr && object->used) |
| + EmuFileObject* object = GetFileObjectByStream(stream); |
| + if (object != NULL) |
| + |
| { |
| return object->file_xbmc; |
| } |
| @@ -239,32 +218,21 @@ XFILE::CFile* CEmuFileWrapper::GetFileXbmcByStream(FILE* stream) |
| |
| int CEmuFileWrapper::GetDescriptorByStream(FILE* stream) |
| { |
| - if (isValidFilePtr(stream)) |
| - { |
| - int i = FileDescriptor(*stream)->_file - FILE_WRAPPER_OFFSET; |
| - if (i >= 0 && i < MAX_EMULATED_FILES) |
| - { |
| - return i + FILE_WRAPPER_OFFSET; |
| - } |
| - } |
| - return -1; |
| + return GetDescriptorByFileObject(GetFileObjectByStream(stream)); |
| } |
| |
| FILE* CEmuFileWrapper::GetStreamByDescriptor(int fd) |
| { |
| - auto object = GetFileObjectByDescriptor(fd); |
| - if (object != nullptr && object->used) |
| - { |
| - return &object->file_emu; |
| - } |
| - return nullptr; |
| + return GetStreamByFileObject(GetFileObjectByDescriptor(fd)); |
| +} |
| + |
| +bool CEmuFileWrapper::DescriptorIsEmulatedFile(int fd) |
| +{ |
| + return GetFileObjectByDescriptor(fd) != NULL; |
| } |
| |
| bool CEmuFileWrapper::StreamIsEmulatedFile(FILE* stream) |
| { |
| - if (isValidFilePtr(stream)) |
| - { |
| - return DescriptorIsEmulatedFile(FileDescriptor(*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 786fa85..311a5cf 100644 |
| --- a/xbmc/cores/DllLoader/exports/util/EmuFileWrapper.h |
| +++ b/xbmc/cores/DllLoader/exports/util/EmuFileWrapper.h |
| @@ -25,14 +25,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 0x00000200 |
| +//#define FILE_WRAPPER_OFFSET 0x00000200 |
| |
| namespace XFILE |
| { |
| @@ -47,12 +47,9 @@ struct kodi_iobuf { |
| |
| typedef struct stEmuFileObject |
| { |
| - FILE file_emu; |
| XFILE::CFile* file_xbmc; |
| CCriticalSection *file_lock; |
| int mode; |
| - //Stick this last to avoid 3-7 bytes of padding |
| - bool used; |
| } EmuFileObject; |
| |
| class CEmuFileWrapper |
| @@ -67,22 +64,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); |
| + 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 constexpr bool DescriptorIsEmulatedFile(int fd) |
| - { |
| - return fd >= FILE_WRAPPER_OFFSET && fd < FILE_WRAPPER_OFFSET + MAX_EMULATED_FILES; |
| - } |
| - 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 e363662..07825f3 100644 |
| --- a/xbmc/cores/DllLoader/exports/wrapper.c |
| +++ b/xbmc/cores/DllLoader/exports/wrapper.c |
| @@ -39,13 +39,13 @@ |
| #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; |
| typedef fpos_t fpos64_t; |
| #define stat64 stat |
| -#if defined(TARGET_DARWIN) || defined(TARGET_ANDROID) |
| +#if defined(TARGET_DARWIN) || defined(TARGET_ANDROID) || !defined(__GLIBC__) |
| #define _G_va_list va_list |
| #endif |
| #endif |
| -- |
| 2.10.2 |
| |