MythTV master
mythbdiowrapper.cpp
Go to the documentation of this file.
2#include "io/mythiowrapper.h"
4
5// Std
6#include <cstdio>
7#include <fcntl.h>
8#include <strings.h>
9#include <sys/stat.h>
10#include <sys/types.h>
11
12// Bluray
13#ifdef HAVE_LIBBLURAY
14#include <libbluray/filesystem.h>
15#else
16#include "file/filesystem.h"
17#endif
18
19#define LOC QString("BDIOWrapper: ")
20
21static BD_FILE_OPEN sDefaultFileOpen = nullptr;
22static BD_DIR_OPEN sDefaultDirOpen = nullptr;
23
24static void MythBDDirClose(BD_DIR_H *Dir)
25{
26 if (Dir)
27 {
28 MythDirClose(static_cast<int>(reinterpret_cast<intptr_t>(Dir->internal)));
29 LOG(VB_FILE, LOG_DEBUG, LOC + "Closed mythdir dir");
30 free(Dir);
31 }
32}
33
34static int MythBDDirRead(BD_DIR_H *Dir, BD_DIRENT *Entry)
35{
36 char *filename = MythDirRead(static_cast<int>(reinterpret_cast<intptr_t>(Dir->internal)));
37 if (filename)
38 {
39 Entry->d_name[255] = '\0';
40 strncpy(Entry->d_name, filename, 255);
41 free(filename);
42 return 0;
43 }
44
45 return 1;
46}
47
48static BD_DIR_H *MythBDDirOpen(const char* DirName)
49{
50 if ((strncmp(DirName, "myth://", 7) != 0) && (sDefaultDirOpen != nullptr))
51 {
52 // Use the original directory handling for directories that are
53 // not in a storage group.
54 return sDefaultDirOpen(DirName);
55 }
56
57 auto *dir = static_cast<BD_DIR_H*>(calloc(1, sizeof(BD_DIR_H)));
58
59 LOG(VB_FILE, LOG_DEBUG, LOC + QString("Opening mythdir '%1'").arg(DirName));
60 dir->close = MythBDDirClose;
61 dir->read = MythBDDirRead;
62
63 int dirID = MythDirOpen(DirName);
64 if (dirID != 0)
65 {
66 // NOLINTNEXTLINE(performance-no-int-to-ptr)
67 dir->internal = reinterpret_cast<void*>(static_cast<intptr_t>(dirID));
68 return dir;
69 }
70
71 LOG(VB_FILE, LOG_DEBUG, LOC + QString("Error opening dir '%1'").arg(DirName));
72 free(dir);
73 return nullptr;
74}
75
76
77static void MythBDFileClose(BD_FILE_H *File)
78{
79 if (File)
80 {
81 MythfileClose(static_cast<int>(reinterpret_cast<intptr_t>(File->internal)));
82 LOG(VB_FILE, LOG_DEBUG, LOC + "Closed mythfile file");
83 free(File);
84 }
85}
86
87static int64_t MythBDFileSeek(BD_FILE_H *File, int64_t Offset, int32_t Origin)
88{
89 return MythFileSeek(static_cast<int>(reinterpret_cast<intptr_t>(File->internal)), Offset, Origin);
90}
91
92static int64_t MythBDFileTell(BD_FILE_H *File)
93{
94 return MythFileTell(static_cast<int>(reinterpret_cast<intptr_t>(File->internal)));
95}
96
97static int64_t MythBDFileRead(BD_FILE_H *File, uint8_t *Buffer, int64_t Size)
98{
99 return MythFileRead(static_cast<int>(reinterpret_cast<intptr_t>(File->internal)),
100 Buffer, static_cast<size_t>(Size));
101}
102
103static int64_t MythBDFileWrite(BD_FILE_H *File, const uint8_t *Buffer, int64_t Size)
104{
105 return MythFileWrite(static_cast<int>(reinterpret_cast<intptr_t>(File->internal)),
106 reinterpret_cast<void*>(const_cast<uint8_t*>(Buffer)),
107 static_cast<size_t>(Size));
108}
109
110static BD_FILE_H *MythBDFileOpen(const char* FileName, const char *CMode)
111{
112 if ((strncmp(FileName, "myth://", 7) != 0) && (sDefaultFileOpen != nullptr))
113 {
114 // Use the original file handling for files that are
115 // not in a storage group.
116 return sDefaultFileOpen(FileName, CMode);
117 }
118
119 auto *file = static_cast<BD_FILE_H*>(calloc(1, sizeof(BD_FILE_H)));
120
121 LOG(VB_FILE, LOG_DEBUG, LOC + QString("Opening mythfile file '%1'").arg(FileName));
122 file->close = MythBDFileClose;
123 file->seek = MythBDFileSeek;
124 file->read = MythBDFileRead;
125 file->write = MythBDFileWrite;
126 file->tell = MythBDFileTell;
127 file->eof = nullptr;
128
129 int intMode = O_RDONLY;
130 if (strcasecmp(CMode, "wb") == 0)
131 intMode = O_WRONLY;
132
133 int fd = MythFileOpen(FileName, intMode);
134 if (fd >= 0)
135 {
136 // NOLINTNEXTLINE(performance-no-int-to-ptr)
137 file->internal = reinterpret_cast<void*>(static_cast<intptr_t>(fd));
138 return file;
139 }
140
141 LOG(VB_FILE, LOG_DEBUG, LOC + QString("Error opening file '%1'").arg(FileName));
142 free(file);
143 return nullptr;
144}
145
147{
148 BD_FILE_OPEN origFile = bd_register_file(MythBDFileOpen);
149 BD_DIR_OPEN origDir = bd_register_dir(MythBDDirOpen);
150
151 if (sDefaultFileOpen == nullptr)
152 sDefaultFileOpen = origFile;
153
154 if (sDefaultDirOpen == nullptr)
155 sDefaultDirOpen = origDir;
156}
static int64_t MythBDFileWrite(BD_FILE_H *File, const uint8_t *Buffer, int64_t Size)
#define LOC
static int64_t MythBDFileRead(BD_FILE_H *File, uint8_t *Buffer, int64_t Size)
static void MythBDDirClose(BD_DIR_H *Dir)
static BD_FILE_OPEN sDefaultFileOpen
static int MythBDDirRead(BD_DIR_H *Dir, BD_DIRENT *Entry)
void MythBDIORedirect(void)
static BD_DIR_H * MythBDDirOpen(const char *DirName)
static int64_t MythBDFileSeek(BD_FILE_H *File, int64_t Offset, int32_t Origin)
static int64_t MythBDFileTell(BD_FILE_H *File)
static void MythBDFileClose(BD_FILE_H *File)
static BD_FILE_H * MythBDFileOpen(const char *FileName, const char *CMode)
static BD_DIR_OPEN sDefaultDirOpen
char * MythDirRead(int DirID)
int MythDirClose(int DirID)
ssize_t MythFileRead(int FileID, void *Buffer, size_t Count)
int MythDirOpen(const char *DirName)
off_t MythFileSeek(int FileID, off_t Offset, int Whence)
int MythfileClose(int FileID)
int MythFileOpen(const char *Pathname, int Flags)
ssize_t MythFileWrite(int FileID, void *Buffer, size_t Count)
off_t MythFileTell(int FileID)
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39