Modified the license text comment type.
[emufront] / src / utils / unziphelper.cpp
1 /*
2 ** EmuFront
3 ** Copyright 2010 Mikko Keinänen
4 **
5 ** This file is part of EmuFront.
6 **
7 **
8 ** EmuFront is free software: you can redistribute it and/or modify
9 ** it under the terms of the GNU General Public License version 2 as published by
10 ** the Free Software Foundation and appearing in the file gpl.txt included in the
11 ** packaging of this file.
12 **
13 ** EmuFront is distributed in the hope that it will be useful,
14 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 ** GNU General Public License for more details.
17 **
18 ** You should have received a copy of the GNU General Public License
19 ** along with EmuFront.  If not, see <http://www.gnu.org/licenses/>.
20 */
21 #include <QFileInfo>
22 #include <QDebug>
23 #include "unziphelper.h"
24 #include "mediaimage.h"
25 #include "filepathobject.h"
26 #include "emufrontexception.h"
27
28 const QString UnzipHelper::UNZIP_COMMAND = "unzip ";
29 const QString UnzipHelper::UNZIP_LIST_ARGS = "-lv ";
30 const QString UnzipHelper::UNZIP_EXTRACT_ARGS= "-qqo ";
31
32 UnzipHelper::UnzipHelper(QObject *parent) :
33     ProcessHelper(parent)
34 {
35 }
36
37 /* Throws EmuFrontException */
38 QMap<QString, EmuFrontObject*> UnzipHelper::listContents(const QString filePath, const FilePathObject *fp)
39 {
40     if (!fp->getSetup()){
41         throw EmuFrontException(tr("Setup not available with %1.").arg(fp->getName()));
42     }
43
44     QFile fl(filePath);
45     if (!fl.open(QIODevice::ReadOnly)) {
46         throw EmuFrontException(tr("Couldn't read file %1.").arg(filePath));
47     }
48
49     //Setup *sup = fp->getSetup();
50     QMap<QString, EmuFrontObject*>  fileList;
51
52     QString command;
53     command.append(UNZIP_COMMAND);
54     command.append(UNZIP_LIST_ARGS);
55     command.append("\"");
56     command.append(filePath);
57     command.append("\"");
58     //qDebug() << command;
59     start(command);
60     // TODO: slot(s) for (start and) error signal(s)
61     bool procOk = waitForFinished();
62     if (!procOk) {
63         throw EmuFrontException(tr("Listing information from file %1 failed with unzip.").arg(filePath));
64     }
65     QString err = readAllStandardError();
66     QString msg = readAllStandardOutput();
67     //qDebug() << "\nErrors:\n" << err << "\nMessage:\n" << msg;
68
69     /*
70
71     The unzip output should have 8 columns, we need to collect the data from
72     size, crc-32 and name columns.
73
74     $ unzip -lv zak.zip
75     Archive:  zak.zip
76      Length   Method    Size  Cmpr    Date    Time   CRC-32   Name
77     --------  ------  ------- ---- ---------- ----- --------  ----
78       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
79       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
80       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
81       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
82       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
83       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
84       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
85       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
86       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
87       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
88       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
89       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
90       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
91     --------          -------  ---                            -------
92      2273024           996859  56%                            13 files
93
94      Here's a regex tested in VIM matching an entry line
95      /^\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\+.\+$
96
97      Here's a regex (tested in VIM) picking the three required fields, length, crc-32 and filename:
98      :%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
99      */
100     QStringList lines = msg.split('\n'
101         //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+.+$")
102         );
103     QStringList entries;
104     //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+.+$");
105     QRegExp regExEntries(
106         "^"             // line starts
107         "\\s*"          // 1st empty space is optional!
108         "(\\d+)"        // uncompressed "Length" in digits
109         "\\s+"
110         "[A-Za-z:]*"    // "Method"
111         "\\s+"
112         "\\d+"          // compressed "Size"
113         "\\s+"
114         "\\d{1,3}%"     // compression ratio
115         "\\s+"
116         "\\d{2,4}-\\d{2}-\\d{2,4}" // date
117         "\\s+"
118         "\\d{2}:\\d{2}" // time
119         "\\s+"
120         "([0-9a-f]{8})" // CRC-32
121         "\\s+"
122         "(\\S.*)"       // at least one non whitespace character + optional other chars (including whitespace)
123         "$"             // line ends
124         );
125     foreach(QString ln, lines) {
126         //if (!test.exactMatch(ln)) continue;
127         //qDebug() << "Current line is " << ln;
128         int pos = regExEntries.indexIn(ln);
129         if (pos == -1) {
130             //qDebug() << "Regex didn't match any entries.";
131             continue; // > no entries
132         }
133         entries = regExEntries.capturedTexts();
134         //qDebug() << "Got " << entries.count() << " entries.";
135         if (entries.count() < 4) continue;
136         QString filename = entries[3];
137         QString checksum = entries[2];
138         QString lenStr = entries[1];
139         bool ok;
140         int length = lenStr.toInt(&ok);
141         /*qDebug() << "Filename is " << filename << " checksum "
142             << checksum << " length " << length;*/
143         if (!ok) continue;
144         MediaImage *effo = new MediaImage(filename, checksum, length);
145         fileList[checksum] = effo;
146     }
147
148     //qDebug() << "File list has " << fileList.size() << " entries.";
149     return fileList;
150 }
151
152 /*
153     Returns the exit code of the unzip process.
154     Throws EmuFrontException if filePath is not readable
155     or targetPath is not writable.
156 */
157 int UnzipHelper::extractAll(QString filePath, QString targetPath)
158 {
159     QFileInfo fp(filePath);
160     if (!fp.isReadable()) {
161         throw EmuFrontException(tr("Cannot read file %1.").arg(filePath));
162     }
163     QFileInfo tp(targetPath);
164     if (!tp.isWritable()) {
165         throw EmuFrontException(tr("Cannot write to %1.").arg(targetPath));
166     }
167
168     // unzip filepath -d targetpath
169     QString command;
170     command.append(UNZIP_COMMAND);
171     command.append(UNZIP_EXTRACT_ARGS);
172     command.append("\"");
173     command.append(filePath);
174     command.append("\"");
175     command.append(" -d ");
176     command.append(targetPath);
177     //qDebug() << "Starting unzip command: " << command;
178     start(command);
179     bool procOk = waitForFinished( ); // TODO: set timeout, now using default 30000ms
180     if (!procOk) {
181         throw EmuFrontException(tr("Failed unzipping file '%1' to '%2'.").arg(filePath).arg(targetPath));
182     }
183     return exitCode();
184 }