00001
00002 #include "exitcodes.h"
00003 #include "mythlogging.h"
00004 #include "ringbuffer.h"
00005
00006
00007 #include "fileutils.h"
00008
00009 static int CopyFile(const MythUtilCommandLineParser &cmdline)
00010 {
00011 int result = GENERIC_EXIT_OK;
00012
00013 if (cmdline.toString("infile").isEmpty())
00014 {
00015 LOG(VB_GENERAL, LOG_ERR, "Missing --infile option");
00016 return GENERIC_EXIT_INVALID_CMDLINE;
00017 }
00018 QString src = cmdline.toString("infile");
00019
00020 if (cmdline.toString("outfile").isEmpty())
00021 {
00022 LOG(VB_GENERAL, LOG_ERR, "Missing --outfile option");
00023 return GENERIC_EXIT_INVALID_CMDLINE;
00024 }
00025 QString dest = cmdline.toString("outfile");
00026
00027 const int readSize = 2 * 1024 * 1024;
00028 char *buf = new char[readSize];
00029 if (!buf)
00030 {
00031 LOG(VB_GENERAL, LOG_ERR, "ERROR, unable to allocate copy buffer ");
00032 return GENERIC_EXIT_NOT_OK;
00033 }
00034
00035 LOG(VB_GENERAL, LOG_INFO, QString("Copying %1 to %2").arg(src).arg(dest));
00036 RingBuffer *srcRB = RingBuffer::Create(src, false);
00037 if (!srcRB)
00038 {
00039 LOG(VB_GENERAL, LOG_ERR, "ERROR, couldn't create Read RingBuffer");
00040 delete[] buf;
00041 return GENERIC_EXIT_NOT_OK;
00042 }
00043
00044 if (!srcRB->IsOpen())
00045 {
00046 LOG(VB_GENERAL, LOG_ERR, "ERROR, srcRB is not open");
00047 delete[] buf;
00048 delete srcRB;
00049 return GENERIC_EXIT_NOT_OK;
00050 }
00051
00052 RingBuffer *destRB = RingBuffer::Create(dest, true);
00053 if (!destRB)
00054 {
00055 LOG(VB_GENERAL, LOG_ERR, "ERROR, couldn't create Write RingBuffer");
00056 delete[] buf;
00057 delete srcRB;
00058 return GENERIC_EXIT_NOT_OK;
00059 }
00060
00061 if (!destRB->IsOpen())
00062 {
00063 LOG(VB_GENERAL, LOG_ERR, "ERROR, destRB is not open");
00064 delete[] buf;
00065 delete srcRB;
00066 delete destRB;
00067 return GENERIC_EXIT_NOT_OK;
00068 }
00069
00070 long long totalBytes = srcRB->GetRealFileSize();
00071 long long totalBytesCopied = 0;
00072 int percentComplete = 0;
00073 bool ok = true;
00074 int r;
00075 int ret;
00076 while (ok && ((r = srcRB->Read(buf, readSize)) > 0))
00077 {
00078 ret = destRB->Write(buf, r);
00079 if (ret < 0)
00080 {
00081 LOG(VB_GENERAL, LOG_ERR,
00082 QString("ERROR, couldn't write at offset %1")
00083 .arg(totalBytesCopied));
00084 ok = false;
00085 }
00086 else
00087 totalBytesCopied += ret;
00088
00089 percentComplete = totalBytesCopied * 100 / totalBytes;
00090 if ((percentComplete % 5) == 0)
00091 LOG(VB_GENERAL, LOG_INFO,
00092 QString("%1 bytes copied, %2%% complete")
00093 .arg(totalBytesCopied).arg(percentComplete));
00094 }
00095
00096 LOG(VB_GENERAL, LOG_INFO,
00097 QString("Wrote %1 bytes total").arg(totalBytesCopied));
00098
00099 LOG(VB_GENERAL, LOG_INFO, "Waiting for write buffer to flush");
00100
00101 delete[] buf;
00102 delete srcRB;
00103 delete destRB;
00104
00105 if (!ok)
00106 result = GENERIC_EXIT_NOT_OK;
00107
00108 return result;
00109 }
00110
00111 void registerFileUtils(UtilMap &utilMap)
00112 {
00113 utilMap["copyfile"] = &CopyFile;
00114 }
00115
00116