MythTV  master
mythbdiowrapper.cpp
Go to the documentation of this file.
1 // Qt
2 #include "mythconfig.h"
3 #include "mythlogging.h"
4 #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 CONFIG_LIBBLURAY_EXTERNAL
16 #include "libbluray/filesystem.h"
17 #else
18 #include "file/filesystem.h"
19 #endif
20 
21 #define LOC QString("BDIOWrapper: ")
22 
23 static BD_FILE_OPEN sDefaultFileOpen = nullptr;
24 static BD_DIR_OPEN sDefaultDirOpen = nullptr;
25 
26 static 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  free(Dir);
33  }
34 }
35 
36 static int MythBDDirRead(BD_DIR_H *Dir, BD_DIRENT *Entry)
37 {
38  char *filename = MythDirRead(static_cast<int>(reinterpret_cast<intptr_t>(Dir->internal)));
39  if (filename)
40  {
41  Entry->d_name[255] = '\0';
42  strncpy(Entry->d_name, filename, 255);
43  free(filename);
44  return 0;
45  }
46 
47  return 1;
48 }
49 
50 static BD_DIR_H *MythBDDirOpen(const char* DirName)
51 {
52  if ((strncmp(DirName, "myth://", 7) != 0) && (sDefaultDirOpen != nullptr))
53  {
54  // Use the original directory handling for directories that are
55  // not in a storage group.
56  return sDefaultDirOpen(DirName);
57  }
58 
59  auto *dir = static_cast<BD_DIR_H*>(calloc(1, sizeof(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 = 0;
66  if ((dirID = MythDirOpen(DirName)))
67  {
68  dir->internal = reinterpret_cast<void*>(static_cast<intptr_t>(dirID));
69  return dir;
70  }
71 
72  LOG(VB_FILE, LOG_DEBUG, LOC + QString("Error opening dir '%1'").arg(DirName));
73  free(dir);
74  return nullptr;
75 }
76 
77 
78 static void MythBDFileClose(BD_FILE_H *File)
79 {
80  if (File)
81  {
82  MythfileClose(static_cast<int>(reinterpret_cast<intptr_t>(File->internal)));
83  LOG(VB_FILE, LOG_DEBUG, LOC + "Closed mythfile file");
84  free(File);
85  }
86 }
87 
88 static int64_t MythBDFileSeek(BD_FILE_H *File, int64_t Offset, int32_t Origin)
89 {
90  return MythFileSeek(static_cast<int>(reinterpret_cast<intptr_t>(File->internal)), Offset, Origin);
91 }
92 
93 static int64_t MythBDFileTell(BD_FILE_H *File)
94 {
95  return MythFileTell(static_cast<int>(reinterpret_cast<intptr_t>(File->internal)));
96 }
97 
98 static int64_t MythBDFileRead(BD_FILE_H *File, uint8_t *Buffer, int64_t Size)
99 {
100  return MythFileRead(static_cast<int>(reinterpret_cast<intptr_t>(File->internal)),
101  Buffer, static_cast<size_t>(Size));
102 }
103 
104 static int64_t MythBDFileWrite(BD_FILE_H *File, const uint8_t *Buffer, int64_t Size)
105 {
106  return MythFileWrite(static_cast<int>(reinterpret_cast<intptr_t>(File->internal)),
107  reinterpret_cast<void*>(const_cast<uint8_t*>(Buffer)),
108  static_cast<size_t>(Size));
109 }
110 
111 static BD_FILE_H *MythBDFileOpen(const char* FileName, const char *CMode)
112 {
113  if ((strncmp(FileName, "myth://", 7) != 0) && (sDefaultFileOpen != nullptr))
114  {
115  // Use the original file handling for files that are
116  // not in a storage group.
117  return sDefaultFileOpen(FileName, CMode);
118  }
119 
120  auto *file = static_cast<BD_FILE_H*>(calloc(1, sizeof(BD_FILE_H)));
121 
122  LOG(VB_FILE, LOG_DEBUG, LOC + QString("Opening mythfile file '%1'").arg(FileName));
123  file->close = MythBDFileClose;
124  file->seek = MythBDFileSeek;
125  file->read = MythBDFileRead;
126  file->write = MythBDFileWrite;
127  file->tell = MythBDFileTell;
128  file->eof = nullptr;
129 
130  int fd = -1;
131  int intMode = O_RDONLY;
132  if (strcasecmp(CMode, "wb") == 0)
133  intMode = O_WRONLY;
134 
135  if ((fd = MythFileOpen(FileName, intMode)) >= 0)
136  {
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 }
MythBDFileOpen
static BD_FILE_H * MythBDFileOpen(const char *FileName, const char *CMode)
Definition: mythbdiowrapper.cpp:111
MythDirClose
int MythDirClose(int DirID)
Definition: mythiowrapper.cpp:444
MythBDDirOpen
static BD_DIR_H * MythBDDirOpen(const char *DirName)
Definition: mythbdiowrapper.cpp:50
MythDirRead
char * MythDirRead(int DirID)
Definition: mythiowrapper.cpp:467
arg
arg(title).arg(filename).arg(doDelete))
MythfileClose
int MythfileClose(int FileID)
Definition: mythiowrapper.cpp:202
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:23
build_compdb.file
file
Definition: build_compdb.py:55
MythFileRead
ssize_t MythFileRead(int FileID, void *Buffer, size_t Count)
Definition: mythiowrapper.cpp:282
mythbdiowrapper.h
LOC
#define LOC
Definition: mythbdiowrapper.cpp:21
MythBDFileRead
static int64_t MythBDFileRead(BD_FILE_H *File, uint8_t *Buffer, int64_t Size)
Definition: mythbdiowrapper.cpp:98
mythlogging.h
sDefaultDirOpen
static BD_DIR_OPEN sDefaultDirOpen
Definition: mythbdiowrapper.cpp:24
MythBDFileTell
static int64_t MythBDFileTell(BD_FILE_H *File)
Definition: mythbdiowrapper.cpp:93
filename
QString filename
Definition: mythplugins/mytharchive/mytharchivehelper/main.cpp:640
MythFileTell
off_t MythFileTell(int FileID)
Definition: mythiowrapper.cpp:259
MythBDDirClose
static void MythBDDirClose(BD_DIR_H *Dir)
Definition: mythbdiowrapper.cpp:26
strings.h
MythBDFileWrite
static int64_t MythBDFileWrite(BD_FILE_H *File, const uint8_t *Buffer, int64_t Size)
Definition: mythbdiowrapper.cpp:104
Buffer
Definition: MythExternControl.h:37
dir
QDir dir
Definition: mythplugins/mytharchive/mytharchivehelper/main.cpp:1177
MythFileOpen
int MythFileOpen(const char *Pathname, int Flags)
Definition: mythiowrapper.cpp:112
MythBDFileClose
static void MythBDFileClose(BD_FILE_H *File)
Definition: mythbdiowrapper.cpp:78
MythDirOpen
int MythDirOpen(const char *DirName)
Definition: mythiowrapper.cpp:392
sDefaultFileOpen
static BD_FILE_OPEN sDefaultFileOpen
Definition: mythbdiowrapper.cpp:23
MythBDDirRead
static int MythBDDirRead(BD_DIR_H *Dir, BD_DIRENT *Entry)
Definition: mythbdiowrapper.cpp:36
MythFileWrite
ssize_t MythFileWrite(int FileID, void *Buffer, size_t Count)
Definition: mythiowrapper.cpp:301
MythBDIORedirect
void MythBDIORedirect(void)
Definition: mythbdiowrapper.cpp:146
MythFileSeek
off_t MythFileSeek(int FileID, off_t Offset, int Whence)
Definition: mythiowrapper.cpp:240
MythBDFileSeek
static int64_t MythBDFileSeek(BD_FILE_H *File, int64_t Offset, int32_t Origin)
Definition: mythbdiowrapper.cpp:88
mythiowrapper.h