File: muscand.cc

package info (click to toggle)
museek%2B 1%3A0.2%2Bsvn20100315.r1208-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 7,664 kB
  • ctags: 6,895
  • sloc: cpp: 28,853; python: 28,014; ansic: 538; makefile: 128; sh: 117
file content (367 lines) | stat: -rw-r--r-- 7,724 bytes parent folder | download | duplicates (3)
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
#include <system.h>
#include <fam.h>

#include <muscan/scanner.hh>
#include <Muhelp/Muconf.hh>
#include <NewNet/nnlog.h>

#include <iostream>
#include <cstdlib>
#include <algorithm>

using std::string;
using std::map;
using std::cout;
using std::endl;
using std::cerr;
using std::vector;

class FAMDirScanner;

class FAMHandler
{
public:
	FAMHandler(const string& config_file, bool doBuddy, bool doReload);
	~FAMHandler();
	
	int load();
	int run();
	void save();
	
	void remove(FAMDirScanner *);
	
private:
	string shares, state;
	FAMConnection fc;
	vector<FAMDirScanner *> nodes, pending;
	time_t save_at;
	FAMDirScanner *root;
	bool m_doReload;
	
	void add(FAMDirScanner *);
	void exists(FAMDirScanner *ds, const char *filename, bool force_file);
	void deleted(FAMDirScanner *ds, const char *filename);
};

class FAMDirScanner : public DirScanner {
public:
	FAMDirScanner(FAMHandler *_fh, bool _f = true) : DirScanner(_f), fh(_fh) { };
	FAMDirScanner(FAMHandler *_fh, const string& _p) : DirScanner(_p), fh(_fh) { };
	~FAMDirScanner() { if(! fake) fh->remove(this); }
	
	DirEntry* new_folder(bool fake) { return new FAMDirScanner(fh, fake); }
	DirEntry* new_folder(const string& path) { return new FAMDirScanner(fh, path); }
	
	FAMHandler *fh;
	FAMRequest famRequest;
	
	bool isFake() { return fake; }
};

FAMHandler::FAMHandler(const string& config_file, bool doBuddy, bool doReload)
           : save_at(0), root(0)
{
	FAMCONNECTION_GETFD(&fc) = -1;

	if (Scanner_Verbosity >= 2){
	    NNLOG.logEvent.connect(new NewNet::ConsoleOutput);
    	NNLOG.enable("ALL");
    }
 
	m_doReload = doReload;
   
	Muconf config(config_file);
	if(! config.hasDomain("shares") || ! config["shares"].hasKey("database")) {
		cerr << "config file '" << config_file << "' incomplete or corrupt shares" << endl;
		exit(-1);
	}
	if(! config.hasDomain("buddy.shares") || ! config["buddy.shares"].hasKey("database")) {
		cerr << "config file '" << config_file << "' incomplete or corrupt buddy shares" << endl;
		exit(-1);
	}
	if (doBuddy)
		{
		string tmp = config["buddy.shares"]["database"];
		shares = tmp;
		state = shares + ".state";
	}
	else {
		string tmp = config["shares"]["database"];
		shares = tmp;
		state = shares + ".state";
	}

}

FAMHandler::~FAMHandler()
{
	if(root)
		delete root;

	if(FAMCONNECTION_GETFD(&fc) != -1)
		FAMClose(&fc);
}

int
FAMHandler::load()
{
	if(root)
		delete root;

	if(FAMCONNECTION_GETFD(&fc) != -1)
		FAMClose(&fc);
	
	if(FAMOpen2(&fc, "muscand") < 0)
	{
		cerr << "couldn't connect to FAM" << endl;
		return -1;
	}
	
	root = new FAMDirScanner(this);
	root->load(state);
	add(root);

	return 0;
}

void
FAMHandler::add(FAMDirScanner *h)
{
	if(find(nodes.begin(), nodes.end(), h) != nodes.end())
		cerr << "double insertion" << endl;
	else if(h->isFake())
		cerr << "it's fake.." << endl;
	else
	{
		if(! h->path.empty())
		{
			cerr << "Registering " << h->path << endl;
			pending.push_back(h);
		}
	}
	
	map<string,DirEntry*>::iterator it = h->folders.begin(), end = h->folders.end();
	for(; it != end; ++it)
		add((FAMDirScanner*)(*it).second);
}

void
FAMHandler::remove(FAMDirScanner *h)
{
	if(h->isFake())
	{
		cerr << "trying to remove a fake entry" << endl;
		return;
	}
	
	vector<FAMDirScanner*>::iterator it = find(nodes.begin(), nodes.end(), h);
	if(it == nodes.end())
		cerr << "Possible corruption (removal of non-existing entry)" << endl;
	else
	{
		FAMCancelMonitor(&fc, &h->famRequest);
		nodes.erase(it);
	}
	
	vector<FAMDirScanner *>::iterator it2 = find(pending.begin(), pending.end(), h);
	if(it2 != pending.end())
	{
		cerr << "Removing pending registration" << endl;
		pending.erase(it2);
	}
}

int
FAMHandler::run()
{
	int fam_fd = FAMCONNECTION_GETFD(&fc);
	
	while(1)
	{
		if(save_at && time(NULL) >= save_at)
		{
			cerr << "saving updated shares database" << endl;
			save();
			save_at = 0;
		}
		
		fd_set rfds, wfds;
		struct timeval tv;
		
		FD_ZERO(&rfds);
		FD_ZERO(&wfds);
		FD_SET(fam_fd, &rfds);
		if(! pending.empty())
			FD_SET(fam_fd, &wfds);
		
		tv.tv_sec = 5;
		tv.tv_usec = 0;
		
		int retval = select(fam_fd+1, &rfds, &wfds, 0, &tv);
		if(retval == -1)
		{
			FAMClose(&fc);
			cerr << "Select error on FAM socket" << endl;
			return -1;
		}
		if(retval == 0)
			continue;
		
		if(FD_ISSET(fam_fd, &wfds) && pending.size())
		{
			vector<FAMDirScanner *>::iterator it = pending.begin();
			FAMMonitorDirectory(&fc, (*it)->path.c_str(), &(*it)->famRequest, *it);
			nodes.push_back(*it);
			pending.erase(it);
		}
		
		if(FD_ISSET(fam_fd, &rfds))
		{
			while(FAMPending(&fc))
			{
				FAMEvent fe;
				if(FAMNextEvent(&fc, &fe) < 0)
				{
					FAMClose(&fc);
					cerr << "Read error on FAM socket" << endl;
					return -1;
				}
				
				if(fe.code == FAMExists || fe.code == FAMCreated || fe.code == FAMChanged) {
					exists((FAMDirScanner *)fe.userdata, fe.filename, fe.code == FAMChanged);
				} else if(fe.code == FAMDeleted) {
					deleted(static_cast<FAMDirScanner*>(fe.userdata), fe.filename);
				} else
					cout << "ping " << fe.code << fe.filename << endl;
			}
		}
	}
}

void
FAMHandler::save()
{
	root->save(state);

	DirEntry folded;

	root->fold(&folded);
	folded.save(shares);
#ifndef WIN32
	if (m_doReload)
		system("killall -HUP museekd");
#endif // WIN32
}

void
FAMHandler::exists(FAMDirScanner *ds, const char *filename, bool force_file)
{
	std::string path = ds->path + "/" + filename;
	
	if(ds->path == filename || find(nodes.begin(), nodes.end(), ds) == nodes.end())
		return;
	
	struct stat s;
	if(stat(path.c_str(), &s) == -1)
		return;
	
	if(S_ISREG(s.st_mode))
	{
		if(force_file || ds->files.find(filename) == ds->files.end())
		{
			cout << "new file: " << path << endl;
			FileEntry fe = ds->scan_file(path);
			fe.size = s.st_size;
			ds->files[filename] = fe;
			save_at = time(0) + 15;
		}
	}
	else if(S_ISDIR(s.st_mode))
	{
		if(ds->folders.find(path) == ds->folders.end())
		{
			cout << "scanning: " << path << endl;
			ds->add(path);
			FAMDirScanner *ns = static_cast<FAMDirScanner *>(ds->folders[path]);
			ns->scan();
			add(ns);
			save_at = time(NULL) + 15;
		}
	}
}

void
FAMHandler::deleted(FAMDirScanner *ds, const char *filename)
{
	if(ds->path == filename)
	{
		cout << "hard deleting " << ds->path << " -- " << filename << endl;
		int ix = ds->path.rfind('/');
		string parent = ds->path.substr(0, ix), child = ds->path.substr(ix + 1);
		vector<FAMDirScanner *>::iterator it = nodes.begin(), end = nodes.end();
		for(; it != end; ++it)
		{
			if((*it)->path == parent)
			{
				cout << "found it" << endl;
				(*it)->folders.erase(filename);
				delete ds;
				save_at = time(0) + 15;
				return;
			}
		}
		cout << parent << " -- " << child << endl;
		return;
	}
	else
		ds->files.erase(filename);
}

void help() {
	cout << "muscand [-c --config PATH] [-b --buddy] [-h --help] [-v --verbose] [--no-reload]" << endl;
	cout << "Version 0.2.0" << endl;
	exit(-1);
}

int main(int argc, char **argv)
{
#ifdef RELAYED_LIBFAM
	extern int libfam_is_present;
	if(! libfam_is_present)
	{
		cerr << "libfam not found, aborting" << endl;
		return -1;
	}
#endif
	string config_file = string(getenv("HOME")) + "/.museekd/config.xml";
	bool doBuddy = false;
	bool doReload = true;
	for(int i = 1; i < argc; i++) {
		string arg = argv[i];
		if(arg == "-c" || arg == "--config") {
			++i;
			config_file = string(argv[i]);
		} 
		else if(arg == "-b" || arg == "--buddy") {
			++i;
			doBuddy = true;
		} 
		else if(arg == "-h" || arg == "--help") {
			++i;
			help();	
		}
		else if(arg == "-v" || arg == "--verbose") {
			Scanner_Verbosity += 1;
		}
		else if(arg == "--no-reload") {
			doReload = false;
		}
	}
	
	FAMHandler fh(config_file, doBuddy, doReload);
	if(fh.load())
		return -1;
	fh.run();
	
	return 0;
}