MythTV  master
fileutils.cpp
Go to the documentation of this file.
1 // libmyth* headers
6 
7 // local headers
8 #include "fileutils.h"
9 
11 {
12  int result = GENERIC_EXIT_OK;
13 
14  if (cmdline.toString("infile").isEmpty())
15  {
16  LOG(VB_GENERAL, LOG_ERR, "Missing --infile option");
18  }
19  QString src = cmdline.toString("infile");
20 
21  if (cmdline.toString("outfile").isEmpty())
22  {
23  LOG(VB_GENERAL, LOG_ERR, "Missing --outfile option");
25  }
26  QString dest = cmdline.toString("outfile");
27 
28  const int readSize = 2 * 1024 * 1024;
29  char *buf = new char[readSize];
30  if (!buf)
31  {
32  LOG(VB_GENERAL, LOG_ERR, "ERROR, unable to allocate copy buffer ");
33  return GENERIC_EXIT_NOT_OK;
34  }
35 
36  LOG(VB_GENERAL, LOG_INFO, QString("Copying %1 to %2").arg(src, dest));
37  MythMediaBuffer *srcbuffer = MythMediaBuffer::Create(src, false);
38  if (!srcbuffer)
39  {
40  LOG(VB_GENERAL, LOG_ERR, "ERROR, couldn't create Read RingBuffer");
41  delete[] buf;
42  return GENERIC_EXIT_NOT_OK;
43  }
44 
45  if (!srcbuffer->IsOpen())
46  {
47  LOG(VB_GENERAL, LOG_ERR, "ERROR, srcRB is not open");
48  delete[] buf;
49  delete srcbuffer;
50  return GENERIC_EXIT_NOT_OK;
51  }
52 
53  MythMediaBuffer *destbuffer = MythMediaBuffer::Create(dest, true);
54  if (!destbuffer)
55  {
56  LOG(VB_GENERAL, LOG_ERR, "ERROR, couldn't create Write RingBuffer");
57  delete[] buf;
58  delete srcbuffer;
59  return GENERIC_EXIT_NOT_OK;
60  }
61 
62  if (!destbuffer->IsOpen())
63  {
64  LOG(VB_GENERAL, LOG_ERR, "ERROR, destRB is not open");
65  delete[] buf;
66  delete srcbuffer;
67  delete destbuffer;
68  return GENERIC_EXIT_NOT_OK;
69  }
70  destbuffer->WriterSetBlocking(true);
71 
72  long long totalBytes = srcbuffer->GetRealFileSize();
73  long long totalBytesCopied = 0;
74  bool ok = true;
75  int r = 0;
76  while (ok && ((r = srcbuffer->Read(buf, readSize)) > 0))
77  {
78  int ret = destbuffer->Write(buf, r);
79  if (ret < 0)
80  {
81  LOG(VB_GENERAL, LOG_ERR,
82  QString("ERROR, couldn't write at offset %1")
83  .arg(totalBytesCopied));
84  ok = false;
85  }
86  else
87  {
88  totalBytesCopied += ret;
89  }
90 
91  int percentComplete = totalBytesCopied * 100 / totalBytes;
92  if ((percentComplete % 5) == 0)
93  {
94  LOG(VB_GENERAL, LOG_INFO,
95  QString("%1 bytes copied, %2%% complete")
96  .arg(totalBytesCopied).arg(percentComplete));
97  }
98  }
99 
100  LOG(VB_GENERAL, LOG_INFO,
101  QString("Wrote %1 bytes total").arg(totalBytesCopied));
102 
103  LOG(VB_GENERAL, LOG_INFO, "Waiting for write buffer to flush");
104 
105  delete[] buf;
106  delete srcbuffer;
107  delete destbuffer;
108 
109  if (!ok)
110  result = GENERIC_EXIT_NOT_OK;
111 
112  return result;
113 }
114 
116 {
117  int result = GENERIC_EXIT_OK;
118 
119  if (cmdline.toString("infile").isEmpty())
120  {
121  LOG(VB_GENERAL, LOG_ERR, "Missing --infile option");
123  }
124  QString url = cmdline.toString("infile");
125 
126  if (cmdline.toString("outfile").isEmpty())
127  {
128  LOG(VB_GENERAL, LOG_ERR, "Missing --outfile option");
130  }
131  QString dest = cmdline.toString("outfile");
132 
133  bool ok = GetMythDownloadManager()->download(url, dest);
134 
135  if (!ok)
136  {
137  LOG(VB_GENERAL, LOG_INFO, "Error downloading file.");
138  result = GENERIC_EXIT_NOT_OK;
139  }
140  else
141  {
142  LOG(VB_GENERAL, LOG_INFO, "File downloaded.");
143  }
144 
145  return result;
146 }
147 
149 {
150  utilMap["copyfile"] = &CopyFile;
151  utilMap["download"] = &DownloadFile;
152 }
153 
154 /* vim: set expandtab tabstop=4 shiftwidth=4: */
build_compdb.dest
dest
Definition: build_compdb.py:9
DownloadFile
static int DownloadFile(const MythUtilCommandLineParser &cmdline)
Definition: fileutils.cpp:115
cmdline
MythCommFlagCommandLineParser cmdline
Definition: mythcommflag.cpp:63
CopyFile
static int CopyFile(const MythUtilCommandLineParser &cmdline)
Definition: fileutils.cpp:10
MythMediaBuffer::WriterSetBlocking
bool WriterSetBlocking(bool Lock=true)
Calls ThreadedFileWriter::SetBlocking(bool)
Definition: mythmediabuffer.cpp:1718
MythMediaBuffer::Write
int Write(const void *Buffer, uint Count)
Writes buffer to ThreadedFileWriter::Write(const void*,uint)
Definition: mythmediabuffer.cpp:1634
MythMediaBuffer
Definition: mythmediabuffer.h:59
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39
fileutils.h
MythUtilCommandLineParser
Definition: mythutil_commandlineparser.h:8
MythMediaBuffer::GetRealFileSize
long long GetRealFileSize(void) const
Definition: mythmediabuffer.cpp:468
GENERIC_EXIT_INVALID_CMDLINE
@ GENERIC_EXIT_INVALID_CMDLINE
Command line parse error.
Definition: exitcodes.h:18
mythlogging.h
GENERIC_EXIT_OK
@ GENERIC_EXIT_OK
Exited with no error.
Definition: exitcodes.h:13
registerFileUtils
void registerFileUtils(UtilMap &utilMap)
Definition: fileutils.cpp:148
MythDownloadManager::download
bool download(const QString &url, const QString &dest, bool reload=false)
Downloads a URL to a file in blocking mode.
Definition: mythdownloadmanager.cpp:431
mythmediabuffer.h
MythMediaBuffer::Read
int Read(void *Buffer, int Count)
This is the public method for reading from a file, it calls the appropriate read method if the file i...
Definition: mythmediabuffer.cpp:1496
UtilMap
QMap< QString, UtilFunc > UtilMap
Definition: mythutil.h:15
MythCommandLineParser::toString
QString toString(const QString &key) const
Returns stored QVariant as a QString, falling to default if not provided.
Definition: mythcommandlineparser.cpp:2360
GENERIC_EXIT_NOT_OK
@ GENERIC_EXIT_NOT_OK
Exited with error.
Definition: exitcodes.h:14
MythMediaBuffer::Create
static MythMediaBuffer * Create(const QString &Filename, bool Write, bool UseReadAhead=true, std::chrono::milliseconds Timeout=kDefaultOpenTimeout, bool StreamOnly=false)
Creates a RingBuffer instance.
Definition: mythmediabuffer.cpp:99
exitcodes.h
mythdownloadmanager.h
MythMediaBuffer::IsOpen
virtual bool IsOpen(void) const =0
GetMythDownloadManager
MythDownloadManager * GetMythDownloadManager(void)
Gets the pointer to the MythDownloadManager singleton.
Definition: mythdownloadmanager.cpp:146