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
|
/*
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version
2 of the License, or (at your option) any later version.
*/
#include <QVarLengthArray>
#include <QMutexLocker>
#include <QDateTime>
#include <QDir>
#include <QSemaphore>
#include <sys/inotify.h>
#include <stropts.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <errno.h>
#include "smdirwatcher.h"
#include "smtreeitem.h"
#include "helper.h"
extern int errno;
SmDirWatcher::SmDirWatcher(QObject *parent) : QThread(parent), mFd(0), mDescr(0) {
mBufLen = 1024 * (sizeof(struct inotify_event) + 16);
mINdata = new char[mBufLen];
mFd = inotify_init();
mCollector = new SmDataColletor(8, this);
mSemFree = new QSemaphore(1024);
mSemUsed = new QSemaphore;
mDataQueue = new QQueue<QPair<QString, DWEvent> >();
mCollector->init(mSemFree, mSemUsed, mDataQueue);
mCollector->start();
}
SmDirWatcher::~SmDirWatcher(){
mCollector->terminate();
mCollector->wait();
if(mFd && mDescr){
inotify_rm_watch(mFd, mDescr);
}
delete mINdata;
}
void SmDirWatcher::setDir(const QString &dir){
if(mDescr){
inotify_rm_watch(mFd, mDescr); //generates IN_IGNORE ???
}
/* mask rationale:
* IN_DELETE_SELF cannot happen since we're only
* watching one directory at all time.
* We don't care about the other events
*/
mDescr = inotify_add_watch(mFd, qPrintable(dir), IN_CLOSE_WRITE | IN_CREATE | IN_DELETE | IN_MODIFY | IN_MOVED_FROM | IN_MOVED_TO);
mCurrent = dir;
mSemFree->acquire();
QPair<QString, DWEvent> c = qMakePair(dir, Populate);
mDataQueue->enqueue(c);
mSemUsed->release();
}
void SmDirWatcher::run(){
int r = read(mFd, mINdata, mBufLen);
if(r <= 0){
return;
}
int i = 0;
while(i < r){
inotify_event *e = reinterpret_cast<inotify_event*>(&mINdata[i]);
if((!e->len) || (e->mask & IN_IGNORED)){
i += sizeof(inotify_event) + e->len;
continue;
}
mSemFree->acquire();
quint32 mask = e->mask;
QString name = QString("%1/%2").arg(mCurrent).arg(e->name);
DWEvent curEvent = None;
if(mask & IN_CREATE || mask & IN_MOVED_TO){
curEvent = Added;
}
if(mask & IN_DELETE || mask & IN_MOVED_FROM){
curEvent = Deleted;
}
if(mask & IN_CLOSE_WRITE || e->mask & IN_MODIFY){
curEvent = Modified;
}
QPair<QString, DWEvent> c = qMakePair(name, curEvent);
mDataQueue->enqueue(c);
mSemUsed->release();
i += sizeof(inotify_event) + e->len;
}
}
SmDataColletor::SmDataColletor(const int numFields, QObject *parent) : QThread(parent), mSemFree(0), mSemUsed(0), mDataQueue(0), mNumFields(numFields) {}
void SmDataColletor::init(QSemaphore *set, QSemaphore *get, QQueue<QPair<QString, SmDirWatcher::DWEvent> > *data){
mSemFree = set;
mSemUsed = get;
mDataQueue = data;
}
void SmDataColletor::run(){
forever {
mSemUsed->acquire();
QPair<QString, SmDirWatcher::DWEvent> cur = mDataQueue->dequeue();
if(cur.second == SmDirWatcher::Populate){
SmTreeItem *i = populate(cur.first);
emit population(i);
mSemFree->release();
continue;
}
QFileInfo fi(cur.first);
QList<QVariant> fd = fileData(fi);
emit newData(fd, cur.second);
mSemFree->release();
}
}
SmTreeItem * SmDataColletor::populate(const QString &dir){
SmTreeItem *retval = new SmTreeItem(mNumFields);
QDir d = QDir(dir);
foreach(QFileInfo fi, d.entryInfoList()){
if(fi.fileName() == "."){
continue;
}
QList<QVariant> data = fileData(fi);
SmTreeItem *newItem = new SmTreeItem(data, retval);
retval->appendChild(newItem);
}
return retval;
}
const QList<QVariant> SmDataColletor::fileData(const QFileInfo &fi) const{
QList<QVariant> data;
data << fi.fileName() << fi.size();
QString mime = Helper::mimeType(fi.absoluteFilePath());
data << mime;
data << fi.lastModified();
QList<QVariant> si = QList<QVariant>() << QVariant() << QVariant();
if(mime.startsWith("video")){
si = Helper::duration(fi.absoluteFilePath());
}
data << Helper::md5Sum(fi.absoluteFilePath());
data << si << fi.absoluteFilePath();
return data;
}
|