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 
21 static BD_FILE_OPEN sDefaultFileOpen = nullptr;
22 static BD_DIR_OPEN sDefaultDirOpen = nullptr;
23 
24 static 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 
34 static 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 
48 static 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  dir->internal = reinterpret_cast<void*>(static_cast<intptr_t>(dirID));
67  return dir;
68  }
69 
70  LOG(VB_FILE, LOG_DEBUG, LOC + QString("Error opening dir '%1'").arg(DirName));
71  free(dir);
72  return nullptr;
73 }
74 
75 
76 static void MythBDFileClose(BD_FILE_H *File)
77 {
78  if (File)
79  {
80  MythfileClose(static_cast<int>(reinterpret_cast<intptr_t>(File->internal)));
81  LOG(VB_FILE, LOG_DEBUG, LOC + "Closed mythfile file");
82  free(File);
83  }
84 }
85 
86 static int64_t MythBDFileSeek(BD_FILE_H *File, int64_t Offset, int32_t Origin)
87 {
88  return MythFileSeek(static_cast<int>(reinterpret_cast<intptr_t>(File->internal)), Offset, Origin);
89 }
90 
91 static int64_t MythBDFileTell(BD_FILE_H *File)
92 {
93  return MythFileTell(static_cast<int>(reinterpret_cast<intptr_t>(File->internal)));
94 }
95 
96 static int64_t MythBDFileRead(BD_FILE_H *File, uint8_t *Buffer, int64_t Size)
97 {
98  return MythFileRead(static_cast<int>(reinterpret_cast<intptr_t>(File->internal)),
99  Buffer, static_cast<size_t>(Size));
100 }
101 
102 static int64_t MythBDFileWrite(BD_FILE_H *File, const uint8_t *Buffer, int64_t Size)
103 {
104  return MythFileWrite(static_cast<int>(reinterpret_cast<intptr_t>(File->internal)),
105  reinterpret_cast<void*>(const_cast<uint8_t*>(Buffer)),
106  static_cast<size_t>(Size));
107 }
108 
109 static BD_FILE_H *MythBDFileOpen(const char* FileName, const char *CMode)
110 {
111  if ((strncmp(FileName, "myth://", 7) != 0) && (sDefaultFileOpen != nullptr))
112  {
113  // Use the original file handling for files that are
114  // not in a storage group.
115  return sDefaultFileOpen(FileName, CMode);
116  }
117 
118  auto *file = static_cast<BD_FILE_H*>(calloc(1, sizeof(BD_FILE_H)));
119 
120  LOG(VB_FILE, LOG_DEBUG, LOC + QString("Opening mythfile file '%1'").arg(FileName));
121  file->close = MythBDFileClose;
122  file->seek = MythBDFileSeek;
123  file->read = MythBDFileRead;
124  file->write = MythBDFileWrite;
125  file->tell = MythBDFileTell;
126  file->eof = nullptr;
127 
128  int intMode = O_RDONLY;
129  if (strcasecmp(CMode, "wb") == 0)
130  intMode = O_WRONLY;
131 
132  int fd = MythFileOpen(FileName, intMode);
133  if (fd >= 0)
134  {
135  file->internal = reinterpret_cast<void*>(static_cast<intptr_t>(fd));
136  return file;
137  }
138 
139  LOG(VB_FILE, LOG_DEBUG, LOC + QString("Error opening file '%1'").arg(FileName));
140  free(file);
141  return nullptr;
142 }
143 
145 {
146  BD_FILE_OPEN origFile = bd_register_file(MythBDFileOpen);
147  BD_DIR_OPEN origDir = bd_register_dir(MythBDDirOpen);
148 
149  if (sDefaultFileOpen == nullptr)
150  sDefaultFileOpen = origFile;
151 
152  if (sDefaultDirOpen == nullptr)
153  sDefaultDirOpen = origDir;
154 }
MythBDFileOpen
static BD_FILE_H * MythBDFileOpen(const char *FileName, const char *CMode)
Definition: mythbdiowrapper.cpp:109
MythDirClose
int MythDirClose(int DirID)
Definition: mythiowrapper.cpp:447
MythBDDirOpen
static BD_DIR_H * MythBDDirOpen(const char *DirName)
Definition: mythbdiowrapper.cpp:48
MythDirRead
char * MythDirRead(int DirID)
Definition: mythiowrapper.cpp:470
MythfileClose
int MythfileClose(int FileID)
Definition: mythiowrapper.cpp:205
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39
build_compdb.file
file
Definition: build_compdb.py:55
MythFileRead
ssize_t MythFileRead(int FileID, void *Buffer, size_t Count)
Definition: mythiowrapper.cpp:285
mythbdiowrapper.h
LOC
#define LOC
Definition: mythbdiowrapper.cpp:19
MythBDFileRead
static int64_t MythBDFileRead(BD_FILE_H *File, uint8_t *Buffer, int64_t Size)
Definition: mythbdiowrapper.cpp:96
mythlogging.h
sDefaultDirOpen
static BD_DIR_OPEN sDefaultDirOpen
Definition: mythbdiowrapper.cpp:22
MythBDFileTell
static int64_t MythBDFileTell(BD_FILE_H *File)
Definition: mythbdiowrapper.cpp:91
MythFileTell
off_t MythFileTell(int FileID)
Definition: mythiowrapper.cpp:262
MythBDDirClose
static void MythBDDirClose(BD_DIR_H *Dir)
Definition: mythbdiowrapper.cpp:24
MythBDFileWrite
static int64_t MythBDFileWrite(BD_FILE_H *File, const uint8_t *Buffer, int64_t Size)
Definition: mythbdiowrapper.cpp:102
Buffer
Definition: MythExternControl.h:36
MythFileOpen
int MythFileOpen(const char *Pathname, int Flags)
Definition: mythiowrapper.cpp:115
MythBDFileClose
static void MythBDFileClose(BD_FILE_H *File)
Definition: mythbdiowrapper.cpp:76
MythDirOpen
int MythDirOpen(const char *DirName)
Definition: mythiowrapper.cpp:395
sDefaultFileOpen
static BD_FILE_OPEN sDefaultFileOpen
Definition: mythbdiowrapper.cpp:21
MythBDDirRead
static int MythBDDirRead(BD_DIR_H *Dir, BD_DIRENT *Entry)
Definition: mythbdiowrapper.cpp:34
build_compdb.filename
filename
Definition: build_compdb.py:21
MythFileWrite
ssize_t MythFileWrite(int FileID, void *Buffer, size_t Count)
Definition: mythiowrapper.cpp:304
MythBDIORedirect
void MythBDIORedirect(void)
Definition: mythbdiowrapper.cpp:144
MythFileSeek
off_t MythFileSeek(int FileID, off_t Offset, int Whence)
Definition: mythiowrapper.cpp:243
MythBDFileSeek
static int64_t MythBDFileSeek(BD_FILE_H *File, int64_t Offset, int32_t Origin)
Definition: mythbdiowrapper.cpp:86
mythiowrapper.h