summaryrefslogtreecommitdiff
path: root/distfoldd/watcher.cc
blob: 664ea58ddd392306c9e0c9775f216652d528d5a2 (plain)
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
#include <QtCore/QDebug>

#include <sys/errno.h>
#include <sys/fcntl.h>
#include <sys/inotify.h>

#include "watcher.h"


Watcher::Watcher(const QString& path, QObject *parent) :
    QObject(parent)
{
	_fd = inotify_init();
	fcntl(_fd, F_SETFL, O_NONBLOCK);
	_buffer.resize(sizeof(struct inotify_event) + NAME_MAX + 1);
	_mask = IN_CLOSE_WRITE | IN_CREATE | IN_DELETE |
	        IN_MOVED_FROM | IN_MOVED_TO | IN_ONLYDIR;

	_notifier = new QSocketNotifier(_fd, QSocketNotifier::Read, this);
	connect(_notifier, SIGNAL(activated(int)), SLOT(readInotify()));

	QStringList l = scanDirs(QDir(path));
	foreach (const QString& s, l) {
		addWatch(s);
	}
}

void Watcher::readInotify()
{
	inotify_event *event;
	const ssize_t struct_size = sizeof(struct inotify_event);
	ssize_t pos, nread;

	do {
		nread = read(_fd, _buffer.data(), _buffer.size());
		if (nread < 0) {
			int err = errno;
			if (err == EWOULDBLOCK) return;
			qWarning() << "Error while reading from inotify" << err;
		}

		pos = 0;
		while (pos + struct_size <= nread) {
			event = reinterpret_cast<inotify_event*>(&(_buffer.data()[pos]));
			if (event->wd == -1) {
				// Special treatment
				// TODO
				continue;
			}

			QString path = _watches[event->wd];
			QString name;
			QString filePath = path;
			if (event->len > 0) {
				name = QString::fromLocal8Bit(event->name);
				filePath += "/" + name;
			}

			if (event->mask & (IN_CREATE|IN_MOVED_TO)) {
				if (event->mask & IN_ISDIR) {
					// TODO There might already be folders in here.
					addWatch(filePath);
				}
				emit pathAdded(filePath);
			}
			if (event->mask & IN_CLOSE_WRITE) {
				emit pathChanged(filePath);
			}
			if (event->mask & (IN_MOVED_FROM|IN_DELETE)) {
				if (event->mask & IN_ISDIR) {
					removeWatch(filePath);
				}
				emit pathRemoved(filePath);
			}

			pos += struct_size + event->len;
		}
	} while (nread > 0);
}

QStringList Watcher::scanDirs(const QDir &dir)
{
	Q_ASSERT(dir.isReadable());
	QStringList l(dir.absolutePath());
	QStringList sub_dirs = dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot);

	foreach (const QString& s, sub_dirs) {
		const QString abs_path = dir.absoluteFilePath(s);
		l << scanDirs(QDir(abs_path));
	}

	return l;
}

void Watcher::addWatch(const QString& path)
{
	int wd = inotify_add_watch(_fd, path.toLocal8Bit().constData(), _mask);
	if (wd == -1) {
		qWarning() << "Failed to add watch for" << path;
		return;
	}
	if (_watches.contains(wd)) {
		qWarning() << "Watch for" << path << "was already setup";
		return;
	}

	qDebug() << "Watching" << path;
	_watches[wd] = path;
	_dirs[path] = wd;
}

void Watcher::removeWatch(const QString& path)
{
	if (!_dirs.contains(path)) {
		qWarning() << "Watch for" << path << "not found";
		return;
	}
	int wd = _dirs[path];
	removeWatch(wd);
}

void Watcher::removeWatch(int wd)
{
	if (!_watches.contains(wd)) {
		qWarning() << "Watch" << wd << "not found";
	}
	QString path = _watches[wd];
	qDebug() << "Unwatching" << path;
	inotify_rm_watch(_fd, wd);
	_dirs.remove(path);
	_watches.remove(wd);
}