/
mydirentry.cpp
406 lines (347 loc) · 10.3 KB
/
mydirentry.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
#include "mydirentry.h"
//#include "main.h"
#include <sys/time.h>
#include <pwd.h>
#include <grp.h>
#include <QtCrypto>
#include <QSet>
extern QString hostName;
MyDirEntry::MyDirEntry(const QFileInfo& info) :
name(info.fileName()),
owner(info.owner()),
group(info.group()),
lastModified(info.lastModified()),
permissions(info.permissions()),
lastModifiedBy(hostName),
parent(0)
{};
MyDir::~MyDir() {
for (QMap<QString, MyDirEntry*>::iterator i = contents.begin(); i != contents.end(); ++i)
delete i.value();
}
void MyDir::dump() const {
for (QMap<QString, MyDirEntry*>::const_iterator i = contents.constBegin(); i != contents.constEnd(); ++i) {
if (i.value()->isDir()) {
qDebug("Dir %s", i.key().toLatin1().constData());
((MyDir*)i.value())->dump();
} else {
qDebug("File %s -> %s", i.key().toLatin1().constData(), ((MyFile*)i.value())->getArchiveName().toLatin1().constData());
}
}
}
bool MyDir::isModified() const {
if (modified) return true;
foreach (MyDirEntry* entry, contents)
if (entry->isDir() && ((MyDir*)entry)->isModified()) return true;
return false;
};
void MyDir::resetModified() {
modified = false;
foreach (MyDirEntry* entry, contents)
if (((MyDir*)entry)->isModified()) ((MyDir*)entry)->resetModified();
}
void MyDirEntry::writeToStream(QDataStream &out) const{
out << owner << group << lastModified << lastModifiedBy << (qint32)permissions;
}
void MyDirEntry::readFromStream(QDataStream &in, int) {
qint32 perm;
in >> owner >> group >> lastModified >> lastModifiedBy >> perm;
permissions = QFlag(perm);
}
/*void MyDirEntry::update(const QFileInfo& info) {
owner = info.owner();
group = info.group();
lastModified = info.lastModified();
permissions = info.permissions();
lastModifiedBy = hostName;
} */
bool MyDirEntry::equals(const QFileInfo& info) const{
return ( isDir() == info.isDir()
&& owner == info.owner()
&& lastModified == info.lastModified()
&& permissions == info.permissions());
}
bool MyDirEntry::equals(const MyDirEntry* other) const {
/*return ( isDir() == other->isDir()
&& owner == other->owner
&& lastModified == other->lastModified
&& permissions == other->permissions
&& lastModifiedBy == other->lastModifiedBy);*/
if (isDir() != other->isDir() || isSymLink() != other->isSymLink()) {
qDebug("type mismatch");
return false;
}
if (owner != other->owner) {
qDebug("owner mismatch");
return false;
}
if (lastModified != other->lastModified) {
qDebug("lastModified mismatch");
return false;
}
if (permissions != other->permissions) {
qDebug("permissions mismatch");
return false;
}
if (lastModifiedBy != other->lastModifiedBy) {
qDebug("lastModifiedBy mismatch");
return false;
}
return true;
}
int MyDirEntry::row() const{
if (parent) return parent->indexOf(this);
return 0;
}
bool MyDir::equals(const MyDirEntry* other) const {
if (!other->isDir()) {
qDebug("type mismatch");
return false;
}
if (owner != other->getOwner()) {
qDebug("owner mismatch");
return false;
}
if (permissions != other->getPermissions()) {
qDebug("permissions mismatch");
return false;
}
return true;
}
bool MyFile::equals(const MyDirEntry* other) const {
return ( MyDirEntry::equals(other)
&& archiveName == ((MyFile*)other)->archiveName );
}
MyDirEntry* MyDirEntry::fromStream(QDataStream &in) {
qint8 type;
MyDirEntry *retval=0;
in >> type;
switch (type) {
case 1:
retval = new MyFile();
break;
case 2:
retval = new MyDir();
break;
case 3:
retval = new MyRoot();
break;
case 4:
retval = new MySymlink();
break;
default:
qDebug("Invalid type found in root file: %d", type);
return 0;
}
retval->readFromStream(in, type);
return retval;
}
void MyDirEntry::setMetadata(const QString& path) const{
QFile file(path);
file.setPermissions(permissions);
struct timeval tv[2];
tv[0].tv_sec=lastModified.toTime_t();
tv[0].tv_usec=0;
tv[1].tv_sec=lastModified.toTime_t();
tv[1].tv_usec=0;
lutimes(QFile::encodeName(file.fileName()), tv);
lchown(QFile::encodeName(file.fileName()), uid(), gid());
}
uid_t MyDirEntry::uid() const {
struct passwd *pwd = getpwnam(owner.toLatin1().constData());
return pwd->pw_uid;
}
gid_t MyDirEntry::gid() const {
struct group *grp = getgrnam(group.toLatin1().constData());
return grp->gr_gid;
}
int MyDirEntry::columnCount() const {
return 7;
}
QVariant MyDirEntry::data(int column) const {
switch (column) {
case 0: return name;
case 1: return lastModified;
case 2: return lastModifiedBy;
default: return QVariant();
}
};
QVariant MyFile::data(int column) const {
switch (column-3) {
case 0: return origSize;
case 1: return compressedSize;
case 2: return archiveName;
case 3: return QCA::arrayToHex(md5);
default: return MyDirEntry::data(column);
}
};
void MyFile::writeToStream(QDataStream &out) const{
out << (qint8)1;
MyDirEntry::writeToStream(out);
out << archiveName << origSize << compressedSize << md5;
}
void MyFile::readFromStream(QDataStream &in, int type) {
MyDirEntry::readFromStream(in, type);
in >> archiveName >> origSize >> compressedSize >> md5;
}
int MyFile::childCount() const {
return history.size();
}
MyDirEntry *MyFile::entryAt(int index) {
return index < history.size() ? history[index] : 0;
}
int MyFile::indexOf(const MyDirEntry* entry) {
return history.indexOf((MyFile*)entry);
}
void MyFile::addHistoryFrom(MyFile* other, bool addSelf) {
int x = other->childCount();
if (addSelf) {
MyFile *first = new MyFile();
first->setArchiveName(other->getArchiveName());
first->setCompressedSize(other->getCompressedSize());
first->setOrigSize(other->getOrigSize());
first->setMd5(other->getMd5());
first->setLastModified(other->getLastModified());
first->setLastModifiedBy(other->getLastModifiedBy());
first->setPermissions(other->getPermissions());
first->setOwner(other->getOwner());
first->setGroup(other->getGroup());
first->setParent(this);
first->setName(QString("V%1").arg(x+1));
history << first;
}
for (x=0; x<other->getHistory().size(); ++x) {
MyFile *entry = other->getHistory()[x];
entry->setParent(this);
history << entry;
}
other->getHistory().clear();
}
int MyDir::childCount() const {
return contents.size();
}
void MyDir::writeToStream(QDataStream &out) const{
out << (qint8)2;
MyDirEntry::writeToStream(out);
out << (qint32)contents.size();
for (QMap<QString, MyDirEntry*>::const_iterator i = contents.constBegin(); i != contents.constEnd(); ++i) {
out << i.key();
i.value()->writeToStream(out);
}
}
void MyDir::readFromStream(QDataStream &in, int type) {
qint32 size;
MyDirEntry::readFromStream(in, type);
in >> size;
for (qint32 i=0; i<size; ++i) {
QString key;
in >> key;
MyDirEntry *entry = MyDirEntry::fromStream(in);
entry->setParent(this);
entry->setName(key);
contents[key]=entry;
displayContents[entry->displayKey()]=entry;
}
}
void MyDir::addEntry(const QString& entryName, MyDirEntry* entry) {
if (!modified) qDebug("%s: dirty by adding %s", name.toLatin1().constData(),
entryName.toLatin1().constData());
modified = true;
lastModifiedBy = hostName;
contents[entryName] = entry;
displayContents[entry->displayKey()] = entry;
entry->setParent(this);
}
MyDirEntry *MyDir::entryAt(int index) {
MyDirEntry *retval=0;
QMap<QString,MyDirEntry*>::const_iterator it = displayContents.constBegin() + index;
if (it != displayContents.constEnd()) retval = it.value();
return retval;
}
int MyDir::indexOf(const MyDirEntry* entry) {
int i=0;
QMap<QString,MyDirEntry*>::const_iterator it = displayContents.constBegin();
while (it.value() != entry && it != displayContents.constEnd()) {
++i;
++it;
}
return i;
}
void MyDir::removeEntry(const QString& entryName) {
if (!modified) qDebug("%s: dirty by removing %s", name.toLatin1().constData(),
entryName.toLatin1().constData());
modified = true;
lastModifiedBy = hostName;
MyDirEntry *entry=contents.value(entryName, 0);
contents.remove(entryName);
if (entry)displayContents.remove(entry->displayKey());
}
MyDir* MyDir::deletedItems() {
MyDirEntry *entry = contents.value(" deleted ", 0);
if(!entry) {
entry = new MyDir;
entry->setName(" deleted ");
addEntry(" deleted ", entry);
}
return (MyDir*) entry;
}
void MyDir::removeAllUnchanged(MyDir *other) {
QMap<QString, MyDirEntry*>::iterator it = contents.begin();
while (it != contents.end()) {
if (it.value()->getName() == " deleted ") {
(static_cast<MyDir*>(it.value()))->removeAllUnchanged(other);
it=contents.erase(it);
} else {
MyDirEntry *otherEntry=other->entryAt(it.value()->getName());
if (otherEntry) {
if (it.value()->isDir() && otherEntry->isDir()) {
(static_cast<MyDir*>(it.value()))->removeAllUnchanged(static_cast<MyDir*>(otherEntry));
}
if (otherEntry->equals(it.value())) {
if (it.value()->isFile() && otherEntry->isFile()) (static_cast<MyFile*>(otherEntry))->addHistoryFrom((static_cast<MyFile*>(it.value())), false);
if (!it.value()->childCount()) {
displayContents.remove(it.value()->displayKey());
it=contents.erase(it);
} else ++it;
} else {
if (it.value()->isFile() && otherEntry->isFile()) (static_cast<MyFile*>(otherEntry))->addHistoryFrom((static_cast<MyFile*>(it.value())), true);
++it;
}
} else {
other->deletedItems()->addEntry(it.key(), it.value());
++it;
}
}
}
}
void MyRoot::writeToStream(QDataStream &out) const {
out << (qint8)3;
out << lastRootName;
MyDir::writeToStream(out);
}
void MyRoot::readFromStream(QDataStream &in, int) {
in >> lastRootName;
qint8 dummytype;
in >> dummytype; // type info written by MyDir::writeToStream
MyDir::readFromStream(in, dummytype);
}
QString MyDir::displayKey() const {
return QString("1") + name.toLower();
}
QString MyRoot::displayKey() const {
return name.split('-').last();
}
QString MyFile::displayKey() const {
return QString("3") + name.toLower();
}
QSet<QString> MyFile::allArchiveFileNames() const {
QSet<QString> ret;
ret << archiveName;
return ret;
}
QSet<QString> MyDir::allArchiveFileNames() const {
QSet<QString> ret;
foreach (MyDirEntry* entry, contents)
ret += entry->allArchiveFileNames();
return ret;
}