-
Notifications
You must be signed in to change notification settings - Fork 18
/
diskwriter.cpp
76 lines (62 loc) · 1.54 KB
/
diskwriter.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include "diskwriter.h"
#include "zlib.h"
#include <QCoreApplication>
void DiskWriter::cancelWrite()
{
isCancelled = true;
}
void DiskWriter::writeCompressedImageToRemovableDevice(const QString &filename, const QString &device)
{
int r;
bool ok;
QByteArray buf(512*1024*sizeof(char), 0);
isCancelled = false;
if (!open(device)) {
emit error("Couldn't open " + device);
return;
}
// Open source
gzFile src = gzopen(filename.toStdString().c_str(), "rb");
if (src == NULL) {
emit error("Couldn't open " + filename);
this->close();
return;
}
if (gzbuffer(src, buf.size()) != 0) {
emit error("Failed to set gz buffer size");
gzclose_r(src);
this->close();
return;
}
r = gzread(src, buf.data(), buf.size());
while (r > 0 && ! isCancelled) {
// TODO: Sanity check
ok = this->write(buf);
if (!ok) {
emit error("Failed to write to " + device + "!");
gzclose(src);
this->close();
return;
}
this->sync();
emit bytesWritten(gztell(src));
r = gzread(src, buf.data(), buf.size());
}
if (r < 0) {
emit error("Failed to read from " + filename + "!");
gzclose_r(src);
this->close();
return;
}
emit syncing();
gzclose_r(src);
this->sync();
this->close();
if (isCancelled) {
emit bytesWritten(0);
}
else {
emit finished();
}
isCancelled = false;
}