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  totalBytesCopied += ret;
88 
89  int percentComplete = totalBytesCopied * 100 / totalBytes;
90  if ((percentComplete % 5) == 0)
91  {
92  LOG(VB_GENERAL, LOG_INFO,
93  QString("%1 bytes copied, %2%% complete")
94  .arg(totalBytesCopied).arg(percentComplete));
95  }
96  }
97 
98  LOG(VB_GENERAL, LOG_INFO,
99  QString("Wrote %1 bytes total").arg(totalBytesCopied));
100 
101  LOG(VB_GENERAL, LOG_INFO, "Waiting for write buffer to flush");
102 
103  delete[] buf;
104  delete srcbuffer;
105  delete destbuffer;
106 
107  if (!ok)
108  result = GENERIC_EXIT_NOT_OK;
109 
110  return result;
111 }
112 
114 {
115  int result = GENERIC_EXIT_OK;
116 
117  if (cmdline.toString("infile").isEmpty())
118  {
119  LOG(VB_GENERAL, LOG_ERR, "Missing --infile option");
121  }
122  QString url = cmdline.toString("infile");
123 
124  if (cmdline.toString("outfile").isEmpty())
125  {
126  LOG(VB_GENERAL, LOG_ERR, "Missing --outfile option");
128  }
129  QString dest = cmdline.toString("outfile");
130 
131  bool ok = GetMythDownloadManager()->download(url, dest);
132 
133  if (!ok)
134  {
135  LOG(VB_GENERAL, LOG_INFO, "Error downloading file.");
136  result = GENERIC_EXIT_NOT_OK;
137  }
138  else
139  {
140  LOG(VB_GENERAL, LOG_INFO, "File downloaded.");
141  }
142 
143  return result;
144 }
145 
147 {
148  utilMap["copyfile"] = &CopyFile;
149  utilMap["download"] = &DownloadFile;
150 }
151 
152 /* 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:113
cmdline
MythCommFlagCommandLineParser cmdline
Definition: mythcommflag.cpp:72
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:1709
MythMediaBuffer::Write
int Write(const void *Buffer, uint Count)
Writes buffer to ThreadedFileWriter::Write(const void*,uint)
Definition: mythmediabuffer.cpp:1625
MythMediaBuffer
Definition: mythmediabuffer.h:50
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39
fileutils.h
GENERIC_EXIT_OK
@ GENERIC_EXIT_OK
Exited with no error.
Definition: exitcodes.h:11
MythUtilCommandLineParser
Definition: mythutil_commandlineparser.h:8
MythMediaBuffer::GetRealFileSize
long long GetRealFileSize(void) const
Definition: mythmediabuffer.cpp:462
mythlogging.h
registerFileUtils
void registerFileUtils(UtilMap &utilMap)
Definition: fileutils.cpp:146
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:430
GENERIC_EXIT_NOT_OK
@ GENERIC_EXIT_NOT_OK
Exited with error.
Definition: exitcodes.h:12
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:1487
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:2344
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:98
exitcodes.h
mythdownloadmanager.h
GENERIC_EXIT_INVALID_CMDLINE
@ GENERIC_EXIT_INVALID_CMDLINE
Command line parse error.
Definition: exitcodes.h:16
MythMediaBuffer::IsOpen
virtual bool IsOpen(void) const =0
GetMythDownloadManager
MythDownloadManager * GetMythDownloadManager(void)
Gets the pointer to the MythDownloadManager singleton.
Definition: mythdownloadmanager.cpp:145