PageRenderTime 22ms CodeModel.GetById 11ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

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