MythTV  master
bdiowrapper.cpp
Go to the documentation of this file.
1 #include "config.h"
2 
3 #include <cstdio>
4 #include <fcntl.h>
5 #include <strings.h>
6 #include <sys/stat.h>
7 #include <sys/types.h>
8 
9 #if CONFIG_LIBBLURAY_EXTERNAL
10 #include "libbluray/filesystem.h"
11 #else
12 #include "file/filesystem.h"
13 #endif
14 
15 #include "mythiowrapper.h"
16 #include "bdiowrapper.h"
17 
18 #include "mythlogging.h"
19 
20 #define LOC QString("BDIOWrapper: ")
21 
22 static BD_FILE_OPEN sDefaultFileOpen = nullptr;
23 static BD_DIR_OPEN sDefaultDirOpen = nullptr;
24 
25 static void dir_close_mythiowrapper(BD_DIR_H *dir)
26 {
27  if (dir)
28  {
29  mythdir_closedir((int)(intptr_t)dir->internal);
30 
31  LOG(VB_FILE, LOG_DEBUG, LOC + "Closed mythdir dir");
32 
33  free(dir);
34  }
35 }
36 
37 static int dir_read_mythiowrapper(BD_DIR_H *dir, BD_DIRENT *entry)
38 {
39  char *filename = mythdir_readdir((int)(intptr_t)dir->internal);
40  if (filename)
41  {
42  entry->d_name[255] = '\0';
43  strncpy(entry->d_name, filename, 255);
44  free(filename);
45  return 0;
46  }
47 
48  return 1;
49 }
50 
51 static BD_DIR_H *dir_open_mythiowrapper(const char* dirname)
52 {
53  if ((strncmp(dirname, "myth://", 7) != 0) && (sDefaultDirOpen != nullptr))
54  {
55  // Use the original directory handling for directories that are
56  // not in a storage group.
57  return sDefaultDirOpen(dirname);
58  }
59 
60  BD_DIR_H *dir = (BD_DIR_H*)calloc(1, sizeof(BD_DIR_H));
61 
62  LOG(VB_FILE, LOG_DEBUG, LOC + QString("Opening mythdir dir %1...").arg(dirname));
63  dir->close = dir_close_mythiowrapper;
64  dir->read = dir_read_mythiowrapper;
65 
66  int dirID = 0;
67  if ((dirID = mythdir_opendir(dirname)))
68  {
69  dir->internal = (void *)(intptr_t)dirID;
70  return dir;
71  }
72 
73  LOG(VB_FILE, LOG_DEBUG, LOC + "Error opening dir!");
74 
75  free(dir);
76 
77  return nullptr;
78 }
79 
80 
81 static void file_close_mythiowrapper(BD_FILE_H *file)
82 {
83  if (file)
84  {
85  mythfile_close((int)(intptr_t)file->internal);
86 
87  LOG(VB_FILE, LOG_DEBUG, LOC + "Closed mythfile file");
88 
89  free(file);
90  }
91 }
92 
93 static int64_t file_seek_mythiowrapper(BD_FILE_H *file, int64_t offset, int32_t origin)
94 {
95  return mythfile_seek((int)(intptr_t)file->internal, offset, origin);
96 }
97 
98 static int64_t file_tell_mythiowrapper(BD_FILE_H *file)
99 {
100  return mythfile_tell((int)(intptr_t)file->internal);
101 }
102 
103 static int64_t file_read_mythiowrapper(BD_FILE_H *file, uint8_t *buf, int64_t size)
104 {
105  return mythfile_read((int)(intptr_t)file->internal, buf, size);
106 }
107 
108 static int64_t file_write_mythiowrapper(BD_FILE_H *file, const uint8_t *buf, int64_t size)
109 {
110  return mythfile_write((int)(intptr_t)file->internal, (void *)buf, size);
111 }
112 
113 static BD_FILE_H *file_open_mythiowrapper(const char* filename, const char *cmode)
114 {
115  if ((strncmp(filename, "myth://", 7) != 0) && (sDefaultFileOpen != nullptr))
116  {
117  // Use the original file handling for files that are
118  // not in a storage group.
119  return sDefaultFileOpen(filename, cmode);
120  }
121 
122  BD_FILE_H *file = (BD_FILE_H*)calloc(1, sizeof(BD_FILE_H));
123 
124  LOG(VB_FILE, LOG_DEBUG, LOC + QString("Opening mythfile file %1...").arg(filename));
125  file->close = file_close_mythiowrapper;
126  file->seek = file_seek_mythiowrapper;
127  file->read = file_read_mythiowrapper;
128  file->write = file_write_mythiowrapper;
129  file->tell = file_tell_mythiowrapper;
130  file->eof = nullptr;
131 
132  int fd;
133  int intMode = O_RDONLY;
134  if (strcasecmp(cmode, "wb") == 0)
135  intMode = O_WRONLY;
136 
137  if ((fd = mythfile_open(filename, intMode)) >= 0)
138  {
139  file->internal = (void*)(intptr_t)fd;
140 
141  return file;
142  }
143 
144  LOG(VB_FILE, LOG_DEBUG, LOC + "Error opening file!");
145 
146  free(file);
147 
148  return nullptr;
149 }
150 
152 {
153  BD_FILE_OPEN origFile = bd_register_file(file_open_mythiowrapper);
154  BD_DIR_OPEN origDir = bd_register_dir(dir_open_mythiowrapper);
155 
156  if (sDefaultFileOpen == nullptr)
157  sDefaultFileOpen = origFile;
158 
159  if (sDefaultDirOpen == nullptr)
160  sDefaultDirOpen = origDir;
161 }
static BD_DIR_OPEN sDefaultDirOpen
Definition: bdiowrapper.cpp:23
int mythfile_open(const char *pathname, int flags)
static void file_close_mythiowrapper(BD_FILE_H *file)
Definition: bdiowrapper.cpp:81
char * mythdir_readdir(int dirID)
static void dir_close_mythiowrapper(BD_DIR_H *dir)
Definition: bdiowrapper.cpp:25
ssize_t mythfile_write(int fileID, void *buf, size_t count)
int mythdir_closedir(int dirID)
int mythdir_opendir(const char *dirname)
static int dir_read_mythiowrapper(BD_DIR_H *dir, BD_DIRENT *entry)
Definition: bdiowrapper.cpp:37
#define LOC
Definition: bdiowrapper.cpp:20
off_t mythfile_seek(int fileID, off_t offset, int whence)
static int64_t file_write_mythiowrapper(BD_FILE_H *file, const uint8_t *buf, int64_t size)
void redirectBDIO()
ssize_t mythfile_read(int fileID, void *buf, size_t count)
static BD_FILE_OPEN sDefaultFileOpen
Definition: bdiowrapper.cpp:22
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: mythlogging.h:41
static int64_t file_seek_mythiowrapper(BD_FILE_H *file, int64_t offset, int32_t origin)
Definition: bdiowrapper.cpp:93
off_t mythfile_tell(int fileID)
static int64_t file_tell_mythiowrapper(BD_FILE_H *file)
Definition: bdiowrapper.cpp:98
static BD_FILE_H * file_open_mythiowrapper(const char *filename, const char *cmode)
static int64_t file_read_mythiowrapper(BD_FILE_H *file, uint8_t *buf, int64_t size)
static BD_DIR_H * dir_open_mythiowrapper(const char *dirname)
Definition: bdiowrapper.cpp:51
int mythfile_close(int fileID)