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
|
/*
* Copyright (C) 2005-2018 Team Kodi
* This file is part of Kodi - https://kodi.tv
*
* SPDX-License-Identifier: GPL-2.0-or-later
* See LICENSES/README.md for more information.
*/
#include "DvdCallback.h"
#include "FileItem.h"
#include "filesystem/Directory.h"
#include "filesystem/File.h"
#include "utils/StringUtils.h"
#include "utils/URIUtils.h"
#include "utils/log.h"
using namespace XFILE;
#ifdef TARGET_WINDOWS
#include "platform/win32/dirent.h"
#else
#include <dirent.h>
#endif
struct SDirState
{
CFileItemList list;
int curr = 0;
};
void CDVDCallback::dvd_logger(void* priv, dvdnav_logger_level_t level, const char* fmt, va_list va)
{
const std::string message = StringUtils::FormatV(fmt, va);
auto logLevel = LOGDEBUG;
switch (level)
{
case DVDNAV_LOGGER_LEVEL_INFO:
logLevel = LOGINFO;
break;
case DVDNAV_LOGGER_LEVEL_ERROR:
logLevel = LOGERROR;
break;
case DVDNAV_LOGGER_LEVEL_WARN:
logLevel = LOGWARNING;
break;
case DVDNAV_LOGGER_LEVEL_DEBUG:
logLevel = LOGDEBUG;
break;
default:
break;
};
CLog::Log(logLevel, "Libdvd: {}", message);
}
void CDVDCallback::dir_close(dvd_dir_h *dir)
{
if (dir)
{
CLog::Log(LOGDEBUG, "CDVDCallback - Closed dir ({})", fmt::ptr(dir));
delete static_cast<SDirState*>(dir->internal);
delete dir;
}
}
dvd_dir_h* CDVDCallback::dir_open(dvdnav_filesystem_h *fs, const char* strDirname)
{
CLog::Log(LOGDEBUG, "CDVDCallback - Opening dir {}", CURL::GetRedacted(strDirname));
SDirState *st = new SDirState();
if (!CDirectory::GetDirectory(strDirname, st->list, "", DIR_FLAG_DEFAULTS))
{
if (!CFile::Exists(strDirname))
CLog::Log(LOGDEBUG, "CDVDCallback - Error opening dir! ({})",
CURL::GetRedacted(strDirname));
delete st;
return nullptr;
}
dvd_dir_h* dir = new dvd_dir_h;
dir->close = dir_close;
dir->read = dir_read;
dir->internal = (void*)st;
return dir;
}
int CDVDCallback::dir_read(dvd_dir_h *dir, dvd_dirent_t *entry)
{
SDirState* state = static_cast<SDirState*>(dir->internal);
if (state->curr >= state->list.Size())
return 1;
strncpy(entry->d_name, state->list[state->curr]->GetLabel().c_str(), sizeof(entry->d_name));
entry->d_name[sizeof(entry->d_name) - 1] = 0;
state->curr++;
return 0;
}
int CDVDCallback::file_close(dvd_file_h *file)
{
if (file)
{
delete static_cast<CFile*>(file->internal);
delete file;
}
return 0;
}
dvd_file_h * CDVDCallback::file_open(dvdnav_filesystem_h *fs, const char *filename)
{
dvd_file_h* file = new dvd_file_h;
file->close = file_close;
file->seek = file_seek;
file->read = file_read;
CFile* fp = new CFile();
if (fp->Open(filename))
{
file->internal = (void*)fp;
return file;
}
CLog::Log(LOGDEBUG, "CDVDCallback - Error opening file! ({})", CURL::GetRedacted(filename));
delete fp;
delete file;
return nullptr;
}
int64_t CDVDCallback::file_seek(dvd_file_h *file, int64_t offset, int32_t origin)
{
return static_cast<CFile*>(file->internal)->Seek(offset, origin);
}
ssize_t CDVDCallback::file_read(dvd_file_h *file, char *buf, size_t size)
{
return static_cast<ssize_t>(static_cast<CFile*>(file->internal)->Read(buf, size));
}
int CDVDCallback::stat(dvdnav_filesystem_h *fs, const char *path, dvdstat_t* statbuff)
{
struct __stat64 tStat;
int result = CFile::Stat(path, &tStat);
statbuff->size = tStat.st_size;
statbuff->st_mode = tStat.st_mode;
return result;
}
|