-
Notifications
You must be signed in to change notification settings - Fork 1
/
fuse_tracker.cpp
690 lines (564 loc) · 18.4 KB
/
fuse_tracker.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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
#include <QDateTime>
#include <QMessageBox>
#include <QFile>
#include <stdio.h>
#include <stdlib.h>
#include "fuse_tracker.h"
#include "svn_xml_reader.h"
//Called to run a CLI program
int FuseTracker::runCmd( QString prog )
{
int result;
//Run the users command
result = system( QString("/bin/bash -c \"%1 &> /dev/null\"").arg( prog ).toAscii().data() );
qDebug( "%s", QString("/bin/bash -c \"%1 &> /dev/null\"").arg( prog ).toAscii().data() );
return result;
}
//Init my desktop tracker
FuseTracker::FuseTracker()
{
//Ensure all my pointers are safely set
Config = NULL;
Server = NULL;
Svn_Update = NULL;
Status_Timer = NULL;
//My other variables
Last_Task_Count = 0;
My_Port = -1;
Timer_Commit_Started = false;
Timer_Count = 0;
Thread_Running = false;
Thread_Idle = true;
//create my common regex
RegEx_Dup.setPattern("^/[.]dupfs_sync");
RegEx_Special.setPattern("([!$# ])");
//Set my status to not doing anything
Status = WATCHING;
New_Status = WATCHING;
//Set my operation mode to normal
Op_Mode = OP_SYNC_MODE;
}
//Return the status of the system
int FuseTracker::status()
{
return Status;
}
//Return the operation mode of the system
FuseTracker::OperationMode FuseTracker::opMode()
{
return Op_Mode;
}
//Return the config
OrmLight* FuseTracker::config()
{
return Config;
}
//Store my new config
OrmLight* FuseTracker::setConfig( OrmLight* config )
{
//Delete any configs I already had
if ( Config != NULL ) delete Config;
//Store my config and return it
return Config = config;
}
//Start a server on the requested port
bool FuseTracker::startServer( int port )
{
//Don't let the user create more than one
if ( Server != NULL )
return false;
//Create my server
Server = new QTcpServer(this);
//Connect my new connection slot
connect( Server, SIGNAL(newConnection()), this, SLOT(newConnection()) );
//Open up a port
My_Port = port;
while ( !Server->listen( QHostAddress::Any, My_Port ) )
My_Port++;
//Craete my svn update timer
Svn_Update = new QTimer();
connect( Svn_Update, SIGNAL(timeout()), this, SLOT(updateSVN()) );
//Start my timer
Svn_Update->start( SVN_TIMEOUT );
//Start my status update timer
Status_Timer = new QTimer();
connect( Status_Timer, SIGNAL(timeout()), this, SLOT(updateStatus()) );
//Start my timer
Status_Timer->start( STATUS_TIMEOUT );
//Create my UDP listen socket
Udp_Socket = new QUdpSocket(this);
Udp_Socket->bind( (*Config)["external_udp_port"].toInt() );
//Connect my udp listen data ready event
connect(Udp_Socket, SIGNAL(readyRead()), this, SLOT(readPendingUdpRequest()));
//Emit my operation mode when the system starts
emit opModeChanged( Op_Mode );
//Connect my loaded fuse timer
QTimer::singleShot( LOADED_FUSE_COUNT, this, SLOT(loadAfterFuseBooted()));
return true;
}
//Return the port we connect on
int FuseTracker::getPort()
{
return My_Port;
}
//Called when a new command is sent to me
void FuseTracker::gotCommand( FuseCppInterface::NotableAction action,
QString path )
{
int rm;
int add;
QString my_path;
bool avoid_update = false;
//If the path is doubling up on the dupfs sync, then kill one
path = path.replace( RegEx_Dup, "");
my_path = QString("%1%2").arg( Mounted).arg( path).replace( RegEx_Special, "\\\\\\1");
//handle an actino
switch ( action )
{
//Delete
case FuseCppInterface::UNLINK:
case FuseCppInterface::RMDIR:
addStatus( ADDING_ITEMS );
//Run the svn command
rm = runCmd( QString("/usr/bin/svn remove %1").arg(my_path) );
//Add this new item
if ( rm != 0 )
Updated_Items.remove(my_path);
break;
//Add new files
case FuseCppInterface::SYMLINK:
case FuseCppInterface::HARDLINK:
case FuseCppInterface::MKNOD:
case FuseCppInterface::MKDIR:
case FuseCppInterface::CREATE:
case FuseCppInterface::OPEN:
addStatus( ADDING_ITEMS );
//Run the svn command
add = runCmd( QString("/usr/bin/svn add %1").arg(my_path) );
//Add this new item
if ( add == 0 )
Updated_Items[my_path] = OrmLight();
break;
//These operations mean we need to update our data at some point
case FuseCppInterface::CHMOD:
case FuseCppInterface::CHOWN:
case FuseCppInterface::TRUNCATE:
case FuseCppInterface::FTRUNCATE:
case FuseCppInterface::UTIMES:
case FuseCppInterface::WRITE:
case FuseCppInterface::FLUSH:
case FuseCppInterface::CLOSE:
case FuseCppInterface::FSYNC:
case FuseCppInterface::SETXATTR:
case FuseCppInterface::REMOVEXATTR:
//Add this new item for update
Updated_Items[my_path] = OrmLight();
break;
//Handle an svn update
case FuseCppInterface::SVN_COMMIT:
//qDebug("Running svn commit");
//Don't allow this action to cause a new commit
avoid_update = true;
removeStatus( SYNC_PUSH_REQUIRED );
addStatus( SYNC_PUSH );
//If there aren't any updated items, quit out now
if ( Updated_Items.count() <= 0 )
return;
//handle our sync mode
if ( Op_Mode == OP_SYNC_MODE )
{
updateRev();
//Conduct an update before we commit to avoid conflicts
QStringList list = updateFiles();
if ( list.size() > 0 )
runCmd( QString("/usr/bin/svn update --force --non-interactive --accept mine-full %1").arg(list.join(" ")));
//Make a list of all the files to be updated
QString files = QStringList( Updated_Items.keys() ).join(" ");
//Make it happen
runCmd( QString("/usr/bin/svn ci -m \\\"Updated %1 Items\\\" --non-interactive --depth immediates %2").arg( Updated_Items.count() ).arg( files ) );
//Clear out all the now updated items
QFile::remove( QString("%1/.dupfs_action_log").
arg((*Config)["svn_dir"]));
Updated_Items.clear();
}
//Store a local file of the changes to be made at a later time
else if ( Op_Mode == OP_OFFLINE_MODE )
{
//save my list of files requiring a change to the FS
QString filename = QString("%1/.dupfs_action_log").
arg((*Config)["svn_dir"]);
Updated_Items.saveToFile( filename );
}
//Remove my push stats
removeStatus( SYNC_PUSH );
break;
//Check if its a special command
case FuseCppInterface::SVN_UPDATE:
//qDebug("Running svn update");
//Don't allow this action to cause a new commit
avoid_update = true;
//Only do this if we aren't offline
if ( Op_Mode != OP_OFFLINE_MODE )
{
removeStatus( SYNC_PULL_REQUIRED );
addStatus( SYNC_PULL );
//Issue my update command
QStringList list = updateFiles();
runCmd( QString("/usr/bin/svn update --force --non-interactive --accept mine-full %1").arg(list.join(" ")));
removeStatus( SYNC_PULL );
}
break;
//not sure, just ignore
case FuseCppInterface::RENAME:
default:
return;
break;
};
//If we got here, we know we got a valid command
//If timer isn't started, start it and then quit out
if ( !avoid_update && !Timer_Commit_Started )
{
addStatus( SYNC_PUSH_REQUIRED );
Timer_Count = 0;
Timer_Commit_Started = true;
}
}
//Store the moutned directories
void FuseTracker::setMounted( QString mounted )
{
Mounted = mounted;
}
//Runs a thread
void FuseTracker::run()
{
QPair<FuseCppInterface::NotableAction, QString> item;
QStringList list;
int fails = 0;
//My main thread loopto handle data
Thread_Running = true;
Thread_Idle = true;
while ( Thread_Running )
{
//Run these commands while there is data
while ( Data_Read.count() > 0 )
{
//Set that the thread isn't idel right now
Thread_Idle = false;
//Reset my failed read count
fails = 0;
//Pull a line from the data list
if ( (item = Data_Read.takeFirst()).second.isEmpty() )
continue;
//Remove any existances of this guy from the hash
Data_Hash.remove( item.second );
//Handle the command
gotCommand( item.first, item.second );
}
//Set that the thread isn't doing anything
Thread_Idle = true;
//Remove any status stuff that might exists
removeStatus( SYNC_PUSH );
removeStatus( SYNC_PULL );
removeStatus( ADDING_ITEMS );
//Sleep for a little while waiting for more data
usleep( THREAD_SLEEP );
//Check if we've failed too many times
if ( ++fails >= THREAD_FAILED )
Thread_Running = false;
}
//Ensure that the thread is idle, this is over kill and probably not needed
Thread_Idle = true;
}
//Called one time after the fuse interface has booted
void FuseTracker::loadAfterFuseBooted()
{
//Attempt to load an old sync file from another time
loadSyncLog( true );
//Issue an svn update to ensure we are synced
forceUpdate();
}
//Load up a sync log file
void FuseTracker::loadSyncLog( bool change_state )
{
OrmLight *result = NULL;
QString filename = QString("%1/.dupfs_action_log").arg((*Config)["svn_dir"]);
//Attempt to load our sync_log file
try {
result = OrmLight::loadFromFile( filename, &Updated_Items);
}
catch ( QString str ) {
qDebug( "%s", str.toAscii().data() );
}
//If we aren't going to change states, just quit now
if ( !change_state || result == NULL )
return;
//Update my state information since we got new data
setOpMode( OP_OFFLINE_MODE );
//If timer isn't started, start it and then quit out
if ( !Timer_Commit_Started )
{
addStatus( SYNC_PUSH_REQUIRED );
Timer_Count = 0;
Timer_Commit_Started = true;
}
}
//Called to touch the rev file
void FuseTracker::updateRev()
{
QString my_path = QString("%1/%2").arg(Mounted)
.arg((*Config)["track_filename"])
.replace( RegEx_Special, "\\\\\\1");
QFile file( my_path );
int add;
//Write out the file
if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
return;
//Update the file with some shiz
file.write( QDateTime::currentDateTime().toString().toAscii() );
file.close();
//Ensure this file is added
add = runCmd( QString("/usr/bin/svn add %1").arg(my_path) );
//Add myself to the update shizzel
Updated_Items[my_path] = OrmLight();
}
//Called to figure out what files are out of date
QStringList FuseTracker::updateFiles()
{
int i;
int r;
QString my_path = QString("%1/%2").arg(Mounted)
.arg((*Config)["track_filename"])
.replace( RegEx_Special, "\\\\\\1");
OrmLight revs;
QStringList files;
QHash<QString, bool> dup;
QString value;
//Figure out what files we need
revs = SvnXmlReader::readSvnXml(
QString("/usr/bin/svn log --xml -v -r BASE:HEAD %1").arg(my_path),
SvnXmlReader::SVN_XML_LOG );
//Load up the revs into a string list and sort it
QStringList list = QStringList( revs.keys() );
//qDebug( "%s", list.join(" ").toAscii().data() );
list.sort();
//Now we go through all revs that we need to update nad make a file list
//We skip the first one since that is always the current log entry
for ( i = 1; i < list.size(); i++ )
for ( r = 0; r < revs[list[i]]["paths"].size(); r++ )
{
value = QString("%1%2").arg(Mounted).arg(revs[list[i]]["paths"][r]).replace( RegEx_Special, "\\\\\\1");
if ( !dup.contains( value ) )
{
dup[value] = true;
files.push_back( value );
}
}
//Always push my track file onto the list of updates
files.push_back( my_path );
return files;
}
//Set the oepration mode of the system
void FuseTracker::setOpMode( OperationMode mode )
{
//Check for invalid modes
if ( mode == OPERATION_MODE_COUNT )
return;
//Check if we should emit a signal
if ( Op_Mode != mode )
emit opModeChanged( mode );
//Store the new op mode
Op_Mode = mode;
}
//Set the status of our system
void FuseTracker::setStatus( SystemStatus status )
{
//If this is a new status, set it
if ( New_Status != status )
New_Status = status;
}
//Add a status to the current one
void FuseTracker::addStatus( SystemStatus status )
{
if ( !(New_Status & status) )
New_Status = (New_Status | status);
}
//Remove a status to the current one
void FuseTracker::removeStatus( SystemStatus status )
{
if ( (New_Status & status) )
New_Status = (New_Status ^ status);
}
//Got a new connection
void FuseTracker::newConnection()
{
Client = Server->nextPendingConnection();
connect( Client, SIGNAL(readyRead()), this, SLOT(readyRead()) );
}
//Called when there is new data from the filesystem
void FuseTracker::readyRead()
{
FuseCppInterface::NotableAction action;
QStringList list;
QString path;
QString from;
int i;
int len;
//Loop while there is data to read
do {
QStringList list;
//Read a line of data from the socket
QString line = QString( Client->readLine()).replace(QRegExp("[\n\r]"), "");
//qDebug("%s", line.toAscii().data() );
//Quit if this line isn't valid
if ( line.indexOf( QRegExp("^[0-9]+,.+$")) < 0 )
continue;
//Split up the data and ensure we have enough items, otherwise read more
list = line.split(QRegExp(","));
if ( list.count() < 2 )
continue;
//If my thread isn't started then start it
if ( !Thread_Running )
this->start();
//Pull out my action and path
action = (FuseCppInterface::NotableAction)list[0].toInt();
path = list[1];
//Run a first pass on my actino type
switch ( action )
{
//Handle the from clause for a rename first, then the normal way
case FuseCppInterface::RENAME:
//Now we lie and say the action is a create action
action = FuseCppInterface::CREATE;
//If we have the from param, handle that before we handle path
if ( list.count() == 3 )
{
from = list[2];
//If this guy exists, remove any instance of him first
if ( Data_Hash.contains( from ) )
{
//Kill any occurance of path existing
for ( i = 0, len = Data_Read.count(); i < len; i++ )
if ( Data_Read[i].second == from )
{
Data_Read.removeAt(i);
--i;
--len;
}
}
//Set that this path does currently have a dangerous action pending
Data_Hash[from] = true;
//Add the dangerous action that is pending
Data_Read.push_back( qMakePair( FuseCppInterface::UNLINK, from) );
}
//None safe operations
case FuseCppInterface::UNLINK:
case FuseCppInterface::RMDIR:
case FuseCppInterface::SYMLINK:
case FuseCppInterface::HARDLINK:
case FuseCppInterface::MKNOD:
case FuseCppInterface::MKDIR:
case FuseCppInterface::CREATE:
case FuseCppInterface::OPEN:
//If this guy exists, remove any instance of him first
if ( Data_Hash.contains( path ) )
{
//Kill any occurance of path existing
for ( i = 0, len = Data_Read.count(); i < len; i++ )
if ( Data_Read[i].second == path )
{
Data_Read.removeAt(i);
--i;
--len;
}
}
//Set that this path does currently have a dangerous action pending
Data_Hash[path] = true;
//The standard way of handling everything
default:
Data_Read.push_back( qMakePair( action, path) );
break;
}
} while ( Client->canReadLine() );
}
//Called when we are suppose to update svn
void FuseTracker::updateSVN()
{
//Increate my counter if it should be updated
if ( Timer_Commit_Started && Timer_Count < TIMER_COUNT_MAX )
Timer_Count++;
//If my timer count is too big, issue and svn update
if ( Timer_Count >= TIMER_COUNT_MAX && Thread_Idle )
forceCommit();
}
//Called to update any listeners to changes in the trackers status info
void FuseTracker::updateStatus()
{
//If our new status isn't know, then set it to watching
if ( New_Status == UNKNOWN )
New_Status = WATCHING;
//Update my status variable
if ( Status != New_Status )
{
Status = New_Status;
emit statusChanged( Status );
}
//If there was a change in the task count alert the user
if ( Last_Task_Count != Data_Read.count() )
{
Last_Task_Count = Data_Read.count();
emit tasksRemaining( Last_Task_Count );
}
}
//When called, a commit command is issued right now
void FuseTracker::forceCommit()
{
//qDebug("Fork Commit has been cizalled");
//Push a special command onto the stack
Data_Read.push_back( qMakePair( FuseCppInterface::SVN_COMMIT,
QString::fromUtf8("NONE")) );
//If my thread isn't started then start it
if ( !Thread_Running )
this->start();
//Reset my variables
Timer_Commit_Started = false;
Timer_Count = 0;
}
//When called, a commit command is issued right now
void FuseTracker::forceUpdate()
{
//Push a special command onto the stack
addStatus( SYNC_PULL_REQUIRED );
Data_Read.push_back( qMakePair( FuseCppInterface::SVN_UPDATE,
QString::fromUtf8("NONE")) );
//If my thread isn't started then start it
if ( !Thread_Running )
this->start();
}
//Read pending udp request
void FuseTracker::readPendingUdpRequest()
{
//Loop while we have valid data
while (Udp_Socket->hasPendingDatagrams())
{
QHostAddress sender;
quint16 senderPort;
QByteArray datagram;
datagram.resize( Udp_Socket->pendingDatagramSize());
//Read the datagram waiting for me
Udp_Socket->readDatagram(datagram.data(), datagram.size(),
&sender, &senderPort);
//Check if we are requested to update
QRegExp rx("^SVN UPDATE REQUESTED (\\d+)");
if ( rx.indexIn(QString(datagram.data())) >= 0 )
{
QStringList list = rx.capturedTexts();
//TODO protect agains a dick sending a bunch of udp update commands
//Push a special command onto the stack
addStatus( SYNC_PULL_REQUIRED );
Data_Read.push_back( qMakePair( FuseCppInterface::SVN_UPDATE, rx.cap(1)));
//If my thread isn't started then start it
if ( !Thread_Running )
this->start();
}
}
}