-
Notifications
You must be signed in to change notification settings - Fork 163
/
Platform.cpp
146 lines (123 loc) · 3.42 KB
/
Platform.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include "Platform.h"
#include <QMessageBox>
#include <QProgressDialog>
#include <sys/mount.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#define BLOCKSIZE 1048576
Platform::Platform(bool kioskMode, bool unsafe)
{
mKioskMode = kioskMode;
mUnsafe = unsafe;
}
bool
Platform::removeDeviceFromList(const QString &displayName)
{
DeviceItem *item = NULL;
QLinkedList<DeviceItem *>::iterator i;
for (i = itemList.begin(); i != itemList.end(); ++i)
{
if ((*i)->getDisplayString() == displayName)
{
item = (*i);
itemList.erase(i);
}
}
if (item == NULL)
return(false);
delete item;
return(true);
}
DeviceItem *
Platform::findDeviceInList(const QString &displayName)
{
DeviceItem *retItem = NULL;
QLinkedList<DeviceItem *>::iterator i;
for (i = itemList.begin(); i != itemList.end(); ++i)
{
if ((*i)->getDisplayString() == displayName)
retItem = (*i);
}
return(retItem);
}
int
Platform::open(DeviceItem* item)
{
return ::open(item->getPath().toLocal8Bit().data(), O_WRONLY|O_SYNC|O_LARGEFILE);
}
// TODO make this routine not be shit
void
Platform::writeData(DeviceItem* item, QString fileName)
{
QFileInfo info(fileName);
qint64 realSize = info.size();
if (realSize > item->getSize())
{
QMessageBox msgBox;
msgBox.setText(QObject::tr("The image you are trying to write is larger than your USB stick."));
msgBox.exec();
return;
}
qint64 i = 0;
char *buffer = (char *) malloc(BLOCKSIZE);
qint64 read = 0;
qint64 written = 0;
int ofd = -1;
int ifd = -1;
int percentWritten, megsWritten, megsTotal;
megsTotal = realSize / 1048576;
// Open the file to read from
if ((ifd = ::open(fileName.toLocal8Bit().data(), O_RDONLY|O_LARGEFILE)) == -1)
{
QMessageBox msgBox;
msgBox.setText(QObject::tr("Couldn't open ") + fileName + ": " + strerror(errno));
msgBox.exec();
return;
}
if ((ofd = open(item)) == -1)
{
QMessageBox msgBox;
msgBox.setText(QObject::tr("Couldn't open ") + item->getPath() + ": " + strerror(errno));
msgBox.exec();
::close(ifd);
return;
}
QProgressDialog progress(" ", "Cancel", 0, 100);
progress.setMinimumDuration(0);
progress.setWindowModality(Qt::WindowModal);
progress.setValue(100);
progress.setWindowTitle(QObject::tr("Writing"));
for (i = 0; i <= realSize; i++)
{
if ((read = ::read(ifd, buffer, BLOCKSIZE)) == -1)
{
QMessageBox msgBox;
msgBox.setText(QObject::tr("Read failure"));
msgBox.exec();
break;
}
written = ::write(ofd, buffer, read);
if (written == -1)
{
QMessageBox msgBox;
msgBox.setText(QObject::tr("Write failure") + ": " + strerror(errno));
msgBox.exec();
break;
}
i += written;
percentWritten = (i*100)/realSize;
megsWritten = i / 1048576;
progress.setValue(percentWritten);
progress.setLabelText(QObject::tr("Written %1MB out of %2MB").arg(megsWritten).arg(megsTotal));
qApp->processEvents();
if (progress.wasCanceled())
break;
}
::close(ofd);
::close(ifd);
free(buffer);
progress.setValue(100);
}