Replacing the OSDaB-ZIP functionality using unzip utility.
[emufront] / src / utils / fileutil.cpp
1 // EmuFront
2 // Copyright 2010 Mikko Keinänen
3 //
4 // This file is part of EmuFront.
5 //
6 //
7 // EmuFront is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License version 2 as published by
9 // the Free Software Foundation and appearing in the file gpl.txt included in the
10 // packaging of this file.
11 //
12 // EmuFront is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with EmuFront.  If not, see <http://www.gnu.org/licenses/>.
19
20 #include <QDir>
21 #include <QDebug>
22 #include <QProcess>
23 #include "fileutil.h"
24 #include "zlib.h" /* crc32 */
25 //#include "OSDaB-Zip/unzip.h"
26 #include "../exceptions/emufrontexception.h"
27 #include "../dataobjects/setup.h"
28 #include "../dataobjects/mediaimage.h"
29 #include "../dataobjects/mediaimagecontainer.h"
30 #include "../dataobjects/mediatype.h"
31 #include "../dataobjects/platform.h"
32 #include "../db/dbmediaimagecontainer.h"
33
34 //int FileUtil::MIC_BUFFER_SIZE = 50;
35
36 const QString FileUtil::UNZIP_COMMAND = "unzip ";
37 const QString FileUtil::UNZIP_LIST_ARGS = "-lv ";
38
39 FileUtil::FileUtil(QObject *parent) : QObject(parent)
40 {
41     buf = new char[READ_BUFFER];
42 }
43
44 FileUtil::~FileUtil()
45 {
46     delete[] buf;
47 }
48
49 /* Throws EmuFrontException */
50 int FileUtil::scanFilePath(FilePathObject *fp, QStringList filters, DbMediaImageContainer *dbMic)
51 {
52     if (!fp->getSetup()){
53         throw EmuFrontException(tr("Setup not available with %1.").arg(fp->getName()));
54     }
55     else if(!fp->getSetup()->getPlatform()){
56         throw EmuFrontException(tr("No platform object available with %1.")
57             .arg(fp->getSetup()->getName()));
58     }
59     else if (!fp->getSetup()->getMediaType()){
60         throw new EmuFrontException(tr("No media type available with %1.")
61             .arg(fp->getSetup()->getName()));
62     }
63     int count = 0;
64     qDebug() << QString("We have a platform %1, media type %2")
65         .arg(fp->getSetup()->getPlatform()->getName())
66         .arg(fp->getSetup()->getMediaType()->getName());
67     QDir dir(fp->getName());
68     if (!dir.exists() || !dir.isReadable())
69         throw EmuFrontException(tr("Directory %1 doesn't exists or isn't readable!").arg(fp->getName()));
70
71     qDebug() << QString("Scanning directory %1.").arg(fp->getName());
72     dir.setFilter(QDir::Files | QDir::NoSymLinks | QDir::NoDotAndDotDot | QDir::Readable);
73
74     if (filters.count() > 0) dir.setNameFilters(filters);
75
76     // we'll go through the filtered archive files...
77     QFileInfoList list = dir.entryInfoList();
78     // TODO: only a buffer of objects should be kept here,
79     // and write to database each time the buffer is filled.
80     QList<MediaImageContainer*> containers;
81     for (int i = 0; i < list.size(); ++i)
82     {
83         QFileInfo fileInfo = list.at(i);
84         qDebug() << QString("%1 %2").arg(fileInfo.size(), 10).arg(fileInfo.absoluteFilePath());
85
86         //... and collect the contents of each archive
87         QList<MediaImage*> files = listContents(fileInfo.absoluteFilePath(), fp);
88
89         if (files.count() > 0)
90         {
91             // read crc32 checksum for media image container
92             quint32 crc = readCrc32(fileInfo.absoluteFilePath());
93             FilePathObject *fpo = new FilePathObject(*fp);
94             MediaImageContainer *con = new MediaImageContainer (
95                     fileInfo.fileName(),
96                     QString("%1").arg(crc, 0, 16),
97                     fileInfo.size(),
98                     files,
99                     fpo // we need a copy since MediaImageContainers are deleted and the original filepath object would get deleted also.
100                 );
101             containers.append(con);
102             ++count;
103             qDebug() << "We have " << containers.count() << " containers.";
104
105             if (containers.count() >= MIC_BUFFER_SIZE)  {
106                 qDebug() << "We have " << containers.count()
107                     << " containers .. storing to db.";
108                 dbMic->storeContainers(containers, fp);
109                 qDeleteAll(containers);
110                 containers.clear();
111                 qDebug() << "containers now: " << containers.count();
112             }
113             qDebug() << "We have " << containers.size() << " containers.";
114         }
115     }
116     if (containers.count() > 0) {
117         qDebug() << "Storing the rest " << containers.count() << " containers.";
118         dbMic->storeContainers(containers, fp);
119         qDeleteAll(containers);
120         containers.clear();
121
122     }
123     qDebug() << "Done scanning files!";
124     return count;
125 }
126
127 /* Uses crc32 from zlib.h to count crc32 checksum value */
128 quint32 FileUtil::readCrc32(QString filePath)
129 {
130     // todo ... use some crc32 tool for this ... or maybe use md5 or something like that!!!
131     QFile file(filePath);
132     qDebug() << "readCrc32: " << filePath;
133     if (!file.open(QIODevice::ReadOnly)) {
134         throw new EmuFrontException(QString(tr("Failed opening file %1 for reading the checksum!")).arg(filePath));
135     }
136     quint32 crc = crc32(0L, Z_NULL, 0);
137     int read = 0;
138     while((read = file.read(buf, READ_BUFFER))) {
139         crc = crc32(crc, (const Bytef*) buf, read);
140     }
141     file.close();
142     if (crc <= 0)
143         throw new EmuFrontException(QString(tr("Failed reading crc checksum for file %1!")).arg(filePath));
144     qDebug() << QString("readCrc32, crc: %1").arg(crc, 0, 16);
145     return crc;
146 }
147
148 QList<MediaImage*> FileUtil::listContents(const QString filePath, const FilePathObject *fp)
149 {
150     if (!fp->getSetup()){
151         throw EmuFrontException(tr("Setup not available with %1.").arg(fp->getName()));
152     }
153
154     QFile fl(filePath);
155     if (!fl.open(QIODevice::ReadOnly)) {
156         throw new EmuFrontException(tr("Couldn't read file %1.").arg(filePath));
157     }
158
159     Setup *sup = fp->getSetup();
160     QList<MediaImage*>  fileList;
161
162     QProcess proc(this);
163     QString command;
164     command.append(UNZIP_COMMAND);
165     command.append(UNZIP_LIST_ARGS);
166     command.append("\"");
167     command.append(filePath);
168     command.append("\"");
169     qDebug() << command;
170     proc.start(command);
171     bool procOk = proc.waitForFinished();
172     if (!procOk) {
173         throw new EmuFrontException(tr("Listing information from file %1 failed with unzip.").arg(filePath));
174     }
175     QString err = proc.readAllStandardError();
176     QString msg = proc.readAllStandardOutput();
177     qDebug() << "\nErrors:\n" << err << "\nMessage:\n" << msg;
178
179     /*
180
181     The unzip output should have 8 columns, we need to collect the data from
182     size, crc-32 and name columns.
183
184     $ unzip -lv zak.zip
185     Archive:  zak.zip
186      Length   Method    Size  Cmpr    Date    Time   CRC-32   Name
187     --------  ------  ------- ---- ---------- ----- --------  ----
188       174848  Defl:N    21936  88% 1996-12-24 23:32 cd68329c  Zak McKracken and the Alien Mindbenders (1988)(Lucasfilm Games)(Disk 1 of 2 Side A)(Boot).d64
189       174848  Defl:N    21949  87% 1996-12-24 23:32 dc0d89f8  Zak McKracken and the Alien Mindbenders (1988)(Lucasfilm Games)(Disk 1 of 2 Side A)(Boot)[a].d64
190       174848  Defl:N    81818  53% 1996-12-24 23:32 a11bc616  Zak McKracken and the Alien Mindbenders (1988)(Lucasfilm Games)(Disk 1 of 2 Side A)(Boot)[cr ESI].d64
191       174848  Defl:N    48833  72% 1996-12-24 23:32 0815053d  Zak McKracken and the Alien Mindbenders (1988)(Lucasfilm Games)(Disk 1 of 2 Side A)(Boot)[cr SCI].d64
192       174848  Defl:N   105964  39% 1996-12-24 23:32 0c943d80  Zak McKracken and the Alien Mindbenders (1988)(Lucasfilm Games)(Disk 1 of 2 Side A)[cr Ikari].d64
193       174848  Defl:N    17876  90% 1996-12-24 23:32 51397eb8  Zak McKracken and the Alien Mindbenders (1988)(Lucasfilm Games)(Disk 1 of 2 Side B)[cr SCI].d64
194       174848  Defl:N   106231  39% 1996-12-24 23:32 0efadb0a  Zak McKracken and the Alien Mindbenders (1988)(Lucasfilm Games)(Disk 2 of 2 Side A).d64
195       174848  Defl:N   105974  39% 1996-12-24 23:32 6935c3e7  Zak McKracken and the Alien Mindbenders (1988)(Lucasfilm Games)(Disk 2 of 2 Side A)[a].d64
196       174848  Defl:N   105963  39% 1996-12-24 23:32 1e9c31de  Zak McKracken and the Alien Mindbenders (1988)(Lucasfilm Games)(Disk 2 of 2 Side A)[cr ESI].d64
197       174848  Defl:N    26294  85% 1996-12-24 23:32 ba5fdfdd  Zak McKracken and the Alien Mindbenders (1988)(Lucasfilm Games)(Disk 2 of 2 Side A)[cr Ikari].d64
198       174848  Defl:N   117996  33% 1996-12-24 23:32 efbf3fd6  Zak McKracken and the Alien Mindbenders (1988)(Lucasfilm Games)(Disk 2 of 2 Side B).d64
199       174848  Defl:N   118015  33% 1996-12-24 23:32 c9541ecd  Zak McKracken and the Alien Mindbenders (1988)(Lucasfilm Games)(Disk 2 of 2 Side B)[a].d64
200       174848  Defl:N   118010  33% 1996-12-24 23:32 68341056  Zak McKracken and the Alien Mindbenders (1988)(Lucasfilm Games)(Disk 2 of 2 Side B)[cr ESI].d64
201     --------          -------  ---                            -------
202      2273024           996859  56%                            13 files
203
204      Here's a regex tested in VIM matching an entry line
205      /^\s\+\d\+\s\+[A-Za-z:]*\s\+\d\+\s\+\d\{1,3}%\s\+\d\{4}-\d\{2}-\d\{2}\s\+\d\{2}:\d\{2}\s\+[0-9a-f]\{8}\s\+.\+$
206
207      Here's a regex (tested in VIM) picking the three required fields, length, crc-32 and filename:
208      :%s/^\s\+\(\d\+\)\s\+[A-Za-z:]*\s\+\d\+\s\+\d\{1,3}%\s\+\d\{4}-\d\{2}-\d\{2}\s\+\d\{2}:\d\{2}\s\+\([0-9a-f]\{8}\)\s\+\(.*$\)/\1 \2 \3/gc
209      */
210     QStringList lines = msg.split('\n'
211         //QRegExp("^\\s+\\d+\\s+[A-Za-z:]*\\s+\\d+\\s+\\d{1,3}%\\s+\\d{4}-\\d{2}-\\d{2}\\s+\\d{2}:\\d{2}\\s+[0-9a-f]{8}\\s+.+$")
212         );
213     QStringList entries;
214     QRegExp test("^\\s+\\d+\\s+[A-Za-z:]*\\s+\\d+\\s+\\d{1,3}%\\s+\\d{4}-\\d{2}-\\d{2}\\s+\\d{2}:\\d{2}\\s+[0-9a-f]{8}\\s+.+$");
215     QRegExp regExEntries("^\\s+(\\d+)\\s+[A-Za-z:]*\\s+\\d+\\s+\\d{1,3}%\\s+\\d{4}-\\d{2}-\\d{2}\\s+\\d{2}:\\d{2}\\s+([0-9a-f]{8})\\s+(.+)$");
216     foreach(QString ln, lines) {
217         if (!test.exactMatch(ln)) continue;
218         int pos = regExEntries.indexIn(ln);
219         if (regExEntries.captureCount() != 3) {
220             continue;
221             //throw new EmuFrontException(tr("Failed to read needed data from file %1.").arg(filePath));
222         }
223         entries = regExEntries.capturedTexts();
224         if (entries.count() < 4) continue;
225         QString filename = entries[3];
226         QString checksum = entries[2];
227         QString lenStr = entries[1];
228         bool ok;
229         int length = lenStr.toInt(&ok);
230         if (!ok) continue;
231         MediaImage *effo = new MediaImage(filename, checksum, length);
232     }
233
234     /*UnZip uz;
235     UnZip::ErrorCode ec = uz.openArchive(filePath);
236     if (ec != UnZip::Ok)
237         throw EmuFrontException(tr("Error while opening zip-file %1, error code %2").arg(filePath).arg(ec));
238
239     QList<UnZip::ZipEntry> list = uz.entryList();
240     foreach(UnZip::ZipEntry entry, list)
241     {
242         qDebug() << "Zip entry " << entry.filename;
243         if (isSupportedFile(entry.filename, sup->getSupportedFileTypeExtensions()))
244         {
245             QString checksum = QString("%1").arg(entry.crc32, 0, 16);
246             qDebug() << "Checksum " << checksum;
247             MediaImage *effo = new MediaImage(entry.filename,
248                 checksum, entry.uncompressedSize);
249             fileList << effo;
250         }
251     }*/
252
253     qDebug() << "File list has " << fileList.size() << " entries.";
254     return fileList;
255
256 }
257
258 bool FileUtil::isSupportedFile(const QString filename, const QStringList supportedFileExtensions)
259 {
260     QString ext = filename.section('.', -1);
261     return supportedFileExtensions.contains(ext, Qt::CaseInsensitive);
262 }