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