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