/xbmc/cores/DllLoader/dll_tracker_file.cpp

http://github.com/xbmc/xbmc · C++ · 132 lines · 107 code · 18 blank · 7 comment · 25 complexity · a0cc8877b746af79108a442ed2b0660e MD5 · raw file

  1. /*
  2. * Copyright (C) 2005-2018 Team Kodi
  3. * This file is part of Kodi - https://kodi.tv
  4. *
  5. * SPDX-License-Identifier: GPL-2.0-or-later
  6. * See LICENSES/README.md for more information.
  7. */
  8. #include "dll_tracker_file.h"
  9. #include "dll_tracker.h"
  10. #include "DllLoader.h"
  11. #include "threads/SingleLock.h"
  12. #include "utils/log.h"
  13. #include <stdlib.h>
  14. #ifdef TARGET_POSIX
  15. #define dll_open open
  16. #define dll_fopen fopen
  17. #define dll_close close
  18. #define dll_fclose fclose
  19. #define dll_freopen freopen
  20. #else
  21. #include "exports/emu_msvcrt.h"
  22. #include <io.h>
  23. #endif
  24. extern "C" void tracker_file_track(uintptr_t caller, uintptr_t handle, TrackedFileType type, const char* sFile)
  25. {
  26. DllTrackInfo* pInfo = tracker_get_dlltrackinfo(caller);
  27. if (pInfo)
  28. {
  29. CSingleLock lock(g_trackerLock);
  30. TrackedFile* file = new TrackedFile;
  31. file->handle = handle;
  32. file->type = type;
  33. file->name = strdup(sFile);
  34. pInfo->fileList.push_back(file);
  35. }
  36. }
  37. extern "C" void tracker_file_free(uintptr_t caller, uintptr_t handle, TrackedFileType type)
  38. {
  39. DllTrackInfo* pInfo = tracker_get_dlltrackinfo(caller);
  40. if (pInfo)
  41. {
  42. CSingleLock lock(g_trackerLock);
  43. for (FileListIter it = pInfo->fileList.begin(); it != pInfo->fileList.end(); ++it)
  44. {
  45. TrackedFile* file = *it;
  46. if (file->handle == handle && file->type == type)
  47. {
  48. free(file->name);
  49. delete file;
  50. pInfo->fileList.erase(it);
  51. return;
  52. }
  53. }
  54. }
  55. CLog::Log(LOGWARNING, "unable to remove tracked file from tracker");
  56. }
  57. extern "C" void tracker_file_free_all(DllTrackInfo* pInfo)
  58. {
  59. if (!pInfo->fileList.empty())
  60. {
  61. CSingleLock lock(g_trackerLock);
  62. CLog::Log(LOGDEBUG, "{0}: Detected open files: {1}", pInfo->pDll->GetFileName(), pInfo->fileList.size());
  63. for (FileListIter it = pInfo->fileList.begin(); it != pInfo->fileList.end(); ++it)
  64. {
  65. TrackedFile* file = *it;
  66. CLog::Log(LOGDEBUG, "%s", file->name);
  67. free(file->name);
  68. if (file->type == FILE_XBMC_OPEN) dll_close(file->handle);
  69. else if (file->type == FILE_XBMC_FOPEN) dll_fclose((FILE*)file->handle);
  70. else if (file->type == FILE_OPEN) close(file->handle);
  71. else if (file->type == FILE_FOPEN) fclose((FILE*)file->handle);
  72. delete file;
  73. }
  74. }
  75. pInfo->fileList.erase(pInfo->fileList.begin(), pInfo->fileList.end());
  76. }
  77. extern "C"
  78. {
  79. int track_open(const char* sFileName, int iMode)
  80. {
  81. uintptr_t loc = (uintptr_t)_ReturnAddress();
  82. int fd = dll_open(sFileName, iMode);
  83. if (fd >= 0) tracker_file_track(loc, fd, FILE_XBMC_OPEN, sFileName);
  84. return fd;
  85. }
  86. int track_close(int fd)
  87. {
  88. uintptr_t loc = (uintptr_t)_ReturnAddress();
  89. tracker_file_free(loc, fd, FILE_XBMC_OPEN);
  90. return dll_close(fd);
  91. }
  92. FILE* track_fopen(const char* sFileName, const char* mode)
  93. {
  94. uintptr_t loc = (uintptr_t)_ReturnAddress();
  95. FILE* fd = dll_fopen(sFileName, mode);
  96. if (fd) tracker_file_track(loc, (uintptr_t)fd, FILE_XBMC_FOPEN, sFileName);
  97. return fd;
  98. }
  99. int track_fclose(FILE* stream)
  100. {
  101. uintptr_t loc = (uintptr_t)_ReturnAddress();
  102. tracker_file_free(loc, (uintptr_t)stream, FILE_XBMC_FOPEN);
  103. return dll_fclose(stream);
  104. }
  105. FILE* track_freopen(const char *path, const char *mode, FILE *stream)
  106. {
  107. uintptr_t loc = (uintptr_t)_ReturnAddress();
  108. tracker_file_free(loc, (uintptr_t)stream, FILE_XBMC_FOPEN);
  109. stream = dll_freopen(path, mode, stream);
  110. if (stream)
  111. tracker_file_track(loc, (uintptr_t)stream, FILE_XBMC_FOPEN, path);
  112. return stream;
  113. }
  114. }