jyavenard

Untitled

Aug 19th, 2013
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.30 KB | None | 0 0
  1. jyaimac:libmythbase jyavenard$ cat remotefile.diff
  2. diff --git a/mythtv/libs/libmythbase/remotefile.cpp b/mythtv/libs/libmythbase/remotefile.cpp
  3. index 7ed591f..bc86309 100644
  4. --- a/mythtv/libs/libmythbase/remotefile.cpp
  5. +++ b/mythtv/libs/libmythbase/remotefile.cpp
  6. @@ -2,6 +2,8 @@
  7. using namespace std;
  8.  
  9. #include <QUrl>
  10. +#include <QFile>
  11. +#include <QFileInfo>
  12.  
  13. #include "mythconfig.h"
  14. #include "mythdb.h"
  15. @@ -22,8 +24,12 @@ RemoteFile::RemoteFile(const QString &_path, bool write, bool useRA,
  16. lock(QMutex::NonRecursive),
  17. controlSock(NULL), sock(NULL),
  18. query("QUERY_FILETRANSFER %1"),
  19. - writemode(write)
  20. + writemode(write), localFile(NULL)
  21. {
  22. + bool is_local =
  23. + (!path.startsWith("/dev")) &&
  24. + ((path.startsWith("/")) || QFile::exists(path));
  25. + isremote = !is_local;
  26. if (writemode)
  27. {
  28. usereadahead = false;
  29. @@ -186,12 +192,33 @@ MythSocket *RemoteFile::openSocket(bool control)
  30. return lsock;
  31. }
  32.  
  33. +bool RemoteFile::isOpen() const
  34. +{
  35. + if (isLocal())
  36. + {
  37. + return localFile;
  38. + }
  39. + return sock && controlSock;
  40. +}
  41. +
  42. bool RemoteFile::Open(void)
  43. {
  44. if (isOpen())
  45. return true;
  46.  
  47. QMutexLocker locker(&lock);
  48. +
  49. + if (isLocal())
  50. + {
  51. + localFile = new QFile(path);
  52. + if (!localFile->open(writemode ? QIODevice::WriteOnly : QIODevice::ReadOnly | QIODevice::Text))
  53. + {
  54. + delete localFile;
  55. + localFile = NULL;
  56. + return false;
  57. + }
  58. + return true;
  59. + }
  60. controlSock = openSocket(true);
  61. if (!controlSock)
  62. return false;
  63. @@ -210,6 +237,10 @@ bool RemoteFile::Open(void)
  64.  
  65. bool RemoteFile::ReOpen(QString newFilename)
  66. {
  67. + if (isLocal())
  68. + {
  69. + return Open();
  70. + }
  71. lock.lock();
  72. if (!sock)
  73. {
  74. @@ -237,6 +268,11 @@ bool RemoteFile::ReOpen(QString newFilename)
  75.  
  76. void RemoteFile::Close(void)
  77. {
  78. + if (isLocal())
  79. + {
  80. + delete localFile;
  81. + localFile = NULL;
  82. + }
  83. if (!controlSock)
  84. return;
  85.  
  86. @@ -266,6 +302,14 @@ void RemoteFile::Close(void)
  87.  
  88. bool RemoteFile::DeleteFile(const QString &url)
  89. {
  90. + bool is_local =
  91. + (!url.startsWith("/dev")) &&
  92. + ((url.startsWith("/")) || QFile::exists(url));
  93. + if (is_local)
  94. + {
  95. + QFile file(url);
  96. + return file.remove();
  97. + }
  98. bool result = false;
  99. QUrl qurl(url);
  100. QString filename = qurl.path();
  101. @@ -300,6 +344,13 @@ bool RemoteFile::Exists(const QString &url)
  102.  
  103. bool RemoteFile::Exists(const QString &url, struct stat *fileinfo)
  104. {
  105. + bool is_local =
  106. + (!url.startsWith("/dev")) &&
  107. + ((url.startsWith("/")) || QFile::exists(url));
  108. + if (is_local)
  109. + {
  110. + return QFile::exists(url);
  111. + }
  112. QUrl qurl(url);
  113. QString filename = qurl.path();
  114. QString sgroup = qurl.userName();
  115. @@ -353,6 +404,13 @@ bool RemoteFile::Exists(const QString &url, struct stat *fileinfo)
  116.  
  117. QString RemoteFile::GetFileHash(const QString &url)
  118. {
  119. + bool is_local =
  120. + (!url.startsWith("/dev")) &&
  121. + ((url.startsWith("/")) || QFile::exists(url));
  122. + if (is_local)
  123. + {
  124. + return url;
  125. + }
  126. QString result;
  127. QUrl qurl(url);
  128. QString filename = qurl.path();
  129. @@ -381,6 +439,8 @@ QString RemoteFile::GetFileHash(const QString &url)
  130.  
  131. void RemoteFile::Reset(void)
  132. {
  133. + if (isLocal())
  134. + return;
  135. QMutexLocker locker(&lock);
  136. if (!sock)
  137. {
  138. @@ -394,6 +454,33 @@ long long RemoteFile::Seek(long long pos, int whence, long long curpos)
  139. {
  140. QMutexLocker locker(&lock);
  141.  
  142. + if (isLocal())
  143. + {
  144. + if (!isOpen())
  145. + {
  146. + LOG(VB_FILE, LOG_ERR, "RemoteFile::Seek(): Called with no file opened");
  147. + return -1;
  148. + }
  149. + long long offset = 0LL;
  150. + if (whence == SEEK_SET)
  151. + {
  152. + offset = min(pos, localFile->size());
  153. + }
  154. + else if (whence == SEEK_END)
  155. + {
  156. + offset = localFile->size() + pos;
  157. + }
  158. + else if (whence == SEEK_CUR)
  159. + {
  160. + offset = localFile->pos() + pos;
  161. + }
  162. + else
  163. + return -1;
  164. + if (!localFile->seek(offset))
  165. + return -1;
  166. + return localFile->pos();
  167. + }
  168. +
  169. if (!sock)
  170. {
  171. LOG(VB_NETWORK, LOG_ERR, "RemoteFile::Seek(): Called with no socket");
  172. @@ -440,6 +527,12 @@ int RemoteFile::Write(const void *data, int size)
  173. "RemoteFile::Write(): Called when not in write mode");
  174. return -1;
  175. }
  176. + if (isLocal())
  177. + {
  178. + LOG(VB_FILE, LOG_ERR,
  179. + "RemoteFile::Write(): Not supported in local mode");
  180. + return -1;
  181. + }
  182.  
  183. QMutexLocker locker(&lock);
  184. if (!sock)
  185. @@ -524,6 +617,16 @@ int RemoteFile::Read(void *data, int size)
  186. bool response = false;
  187.  
  188. QMutexLocker locker(&lock);
  189. +
  190. + if (isLocal())
  191. + {
  192. + if (localFile)
  193. + {
  194. + QDataStream stream(localFile);
  195. + return stream.readRawData(static_cast<char*>(data), size);
  196. + }
  197. + return 0;
  198. + }
  199. if (!sock)
  200. {
  201. LOG(VB_NETWORK, LOG_ERR, "RemoteFile::Read(): Called with no socket");
  202. @@ -611,19 +714,35 @@ int RemoteFile::Read(void *data, int size)
  203. return recv;
  204. }
  205.  
  206. +long long RemoteFile::GetFileSize(void) const
  207. +{
  208. + if (isLocal())
  209. + {
  210. + if (localFile)
  211. + return localFile->size();
  212. + else
  213. + return 0;
  214. + }
  215. + return filesize;
  216. +}
  217. +
  218. bool RemoteFile::SaveAs(QByteArray &data)
  219. {
  220. - if (filesize < 0)
  221. + long long fs = GetFileSize();
  222. +
  223. + if (fs < 0)
  224. return false;
  225.  
  226. - data.resize(filesize);
  227. - Read(data.data(), filesize);
  228. + data.resize(fs);
  229. + Read(data.data(), fs);
  230.  
  231. return true;
  232. }
  233.  
  234. void RemoteFile::SetTimeout(bool fast)
  235. {
  236. + if (isLocal())
  237. + return;
  238. if (timeoutisfast == fast)
  239. return;
  240.  
  241. @@ -649,6 +768,14 @@ void RemoteFile::SetTimeout(bool fast)
  242.  
  243. QDateTime RemoteFile::LastModified(const QString &url)
  244. {
  245. + bool is_local =
  246. + (!url.startsWith("/dev")) &&
  247. + ((url.startsWith("/")) || QFile::exists(url));
  248. + if (is_local)
  249. + {
  250. + QFileInfo info(url);
  251. + return info.lastModified();
  252. + }
  253. QDateTime result;
  254. QUrl qurl(url);
  255. QString filename = qurl.path();
  256. diff --git a/mythtv/libs/libmythbase/remotefile.h b/mythtv/libs/libmythbase/remotefile.h
  257. index 67f72b1..da9976c 100644
  258. --- a/mythtv/libs/libmythbase/remotefile.h
  259. +++ b/mythtv/libs/libmythbase/remotefile.h
  260. @@ -39,10 +39,10 @@ class MBASE_PUBLIC RemoteFile
  261.  
  262. void SetTimeout(bool fast);
  263.  
  264. - bool isOpen(void) const
  265. - { return sock && controlSock; }
  266. - long long GetFileSize(void) const
  267. - { return filesize; }
  268. + bool isOpen(void) const;
  269. + bool isLocal(void) const
  270. + { return !isremote; }
  271. + long long GetFileSize(void) const;
  272.  
  273. QStringList GetAuxiliaryFiles(void) const
  274. { return auxfiles; }
  275. @@ -70,6 +70,8 @@ class MBASE_PUBLIC RemoteFile
  276.  
  277. QStringList possibleauxfiles;
  278. QStringList auxfiles;
  279. + bool isremote;
  280. + QFile *localFile;
  281. };
  282.  
  283. #endif
  284. jyaimac:libmythbase jyavenard$
Advertisement
Add Comment
Please, Sign In to add comment