00001
00002 #include "mythcoreutil.h"
00003
00004
00005 #include <unistd.h>
00006 #include <fcntl.h>
00007
00008
00009 #include "compat.h"
00010
00011 #ifdef linux
00012 #include <sys/vfs.h>
00013 #include <sys/sysinfo.h>
00014 #endif
00015
00016 #if CONFIG_DARWIN
00017 #include <mach/mach.h>
00018 #endif
00019
00020 #ifdef BSD
00021 #include <sys/mount.h>
00022 #include <sys/sysctl.h>
00023 #endif
00024
00025
00026 #include <QByteArray>
00027 #include <QStringList>
00028
00029
00030 #include "mythcorecontext.h"
00031 #include "mythlogging.h"
00032 #include "unzip.h"
00033
00039 int64_t getDiskSpace(const QString &file_on_disk,
00040 int64_t &total, int64_t &used)
00041 {
00042 struct statfs statbuf;
00043 memset(&statbuf, 0, sizeof(statbuf));
00044 int64_t freespace = -1;
00045 QByteArray cstr = file_on_disk.toLocal8Bit();
00046
00047 total = used = -1;
00048
00049
00050
00051
00052
00053 if ((statfs(cstr.constData(), &statbuf) == 0) &&
00054 (statbuf.f_blocks > 0) &&
00055 (statbuf.f_bsize > 0))
00056 {
00057 total = statbuf.f_blocks;
00058 total *= statbuf.f_bsize;
00059 total = total >> 10;
00060
00061 freespace = statbuf.f_bavail;
00062 freespace *= statbuf.f_bsize;
00063 freespace = freespace >> 10;
00064
00065 used = total - freespace;
00066 }
00067
00068 return freespace;
00069 }
00070
00071 bool extractZIP(const QString &zipFile, const QString &outDir)
00072 {
00073 UnZip uz;
00074 UnZip::ErrorCode ec = uz.openArchive(zipFile);
00075
00076 if (ec != UnZip::Ok)
00077 {
00078 LOG(VB_GENERAL, LOG_ERR,
00079 QString("extractZIP(): Unable to open ZIP file %1")
00080 .arg(zipFile));
00081 return false;
00082 }
00083
00084 ec = uz.extractAll(outDir);
00085
00086 if (ec != UnZip::Ok)
00087 {
00088 LOG(VB_GENERAL, LOG_ERR,
00089 QString("extractZIP(): Error extracting ZIP file %1")
00090 .arg(zipFile));
00091 return false;
00092 }
00093
00094 uz.closeArchive();
00095
00096 return true;
00097 }
00098
00099 static QString downloadRemoteFile(const QString &cmd, const QString &url,
00100 const QString &storageGroup,
00101 const QString &filename)
00102 {
00103 QStringList strlist(cmd);
00104 strlist << url;
00105 strlist << storageGroup;
00106 strlist << filename;
00107
00108 bool ok = gCoreContext->SendReceiveStringList(strlist);
00109
00110 if (!ok || strlist.size() < 2 || strlist[0] != "OK")
00111 {
00112 LOG(VB_GENERAL, LOG_ERR,
00113 "downloadRemoteFile(): " + cmd + " returned ERROR!");
00114 return QString();
00115 }
00116
00117 return strlist[1];
00118 }
00119
00120 QString RemoteDownloadFile(const QString &url,
00121 const QString &storageGroup,
00122 const QString &filename)
00123 {
00124 return downloadRemoteFile("DOWNLOAD_FILE", url, storageGroup, filename);
00125 }
00126
00127 QString RemoteDownloadFileNow(const QString &url,
00128 const QString &storageGroup,
00129 const QString &filename)
00130 {
00131 return downloadRemoteFile("DOWNLOAD_FILE_NOW", url, storageGroup, filename);
00132 }
00133
00134