-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathclient.php
More file actions
executable file
·229 lines (181 loc) · 8 KB
/
client.php
File metadata and controls
executable file
·229 lines (181 loc) · 8 KB
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
#!/usr/bin/php -q
<?php
// We like errors at the moment
error_reporting(E_ALL);
// Allowed arguments & their defaults
$runmode = array(
'no-daemon' => false,
'help' => false,
'write-initd' => false,
);
// Scan command line attributes for allowed arguments
foreach ($argv as $k=>$arg) {
if (substr($arg, 0, 2) == '--' && isset($runmode[substr($arg, 2)])) {
$runmode[substr($arg, 2)] = true;
}
}
// Help mode. Shows allowed argumentents and quit directly
if ($runmode['help'] == true) {
echo 'Usage: '.$argv[0].' [runmode]' . "\n";
echo 'Available runmodes:' . "\n";
foreach ($runmode as $runmod=>$val) {
echo ' --'.$runmod . "\n";
}
die();
}
// require PEAR Daemon files - provided in source
require_once("lib/System/Daemon.php");
// Setup
$options = array(
'appName' => 'sugarsyncclient',
'appDir' => dirname(__FILE__),
'appDescription' => 'SugarSync Linux Client',
'authorName' => 'Mark Willis',
'authorEmail' => 'mark.w@immat.co.uk',
'sysMaxExecutionTime' => '0',
'sysMaxInputTime' => '0',
'sysMemoryLimit' => '1024M',
'usePEAR' => false,
'logVerbosity' => 7, # not working?
'logLocation' => dirname(__FILE__)."/debug.log"
);
System_Daemon::setOptions($options); // setup all options
// Overrule the signal handler with any function
/** This doesn't work with CTRL+C on terminal in ubuntu 10.04 - not sure why though **/
System_Daemon::setSigHandler(SIGTERM, 'sig_handler');
System_Daemon::setSigHandler(SIGINT, 'sig_handler');
/********************************* Writing init scripts is off at the moment - didn't work outside of PEAR (didn't test thoroughly as not at that point yet)
if (!$runmode['write-initd']) {
System_Daemon::info('not writing an init.d script this time');
} else {
System_Daemon::info('init.d script creation OFF at this time');
/*
if (($initd_location = System_Daemon::writeAutoRun()) === false) {
System_Daemon::notice('unable to write init.d script');
} else {
System_Daemon::info(
'sucessfully written startup script: %s',
$initd_location
);
} *//*
}
*/
// Spawn Deamon!
if (!$runmode['no-daemon']) {
// Spawn Daemon
System_Daemon::start();
System_Daemon::log(System_Daemon::LOG_INFO, "Daemon: '".System_Daemon::getOption("appName"). "' spawned! This will be written to ". System_Daemon::getOption("logLocation"));
}
require_once("lib/setup.php");
// Run your code
// Check for first run instance - any records in database - execute recursive download
$first_run = false;
if(!$db->check_db()) {
$xml->recurse_download($user_data_xml);
$first_run = true;
}
System_Daemon::info("Run initial upload");
// we run the upload first - then download the new data - if any
$startup_upload = $xml->check_if_upload_needed();
//print_r($startup_upload);
if(is_array($startup_upload)) {
foreach($startup_upload as $file) {
$upload_parts = explode("/",$file);
$upload_filename = array_pop($upload_parts);
$upload_folder = implode("/",$upload_parts);
$xml->check_upload_new_file($upload_filename,$upload_folder);
}
}
//die;
/** If not first run then download after doing upload **/
if(!$first_run) {
$xml->recurse_download($user_data_xml);
}
/** We watch for these event triggers to detect wether a file has been added/renamed/deleted/updated **/
$inotify->active_watches = IN_CLOSE_WRITE | IN_MOVED_TO | IN_MOVED_FROM | IN_CREATE | IN_DELETE;
$inotify->recurse_watch_dir(SYNC_DIR);
// This variable gives your own code the ability to breakdown the daemon:
$runningOkay = true;
// This variable keeps track of how many 'runs' or 'loops' your daemon has
// done so far. For example purposes, we're quitting on 3.
$cnt = 1;
/*********************** Main system loop *************/
while (!System_Daemon::isDying() && $runningOkay && $cnt <=100) { /** Limit to 50 recursions for the moment **/
/** sugarsync client bits
* This is where we need to setup
* -> inotify watches
* -> check for new downloads
*/
if($inotify->check_queue()) {
System_Daemon::info("iNotify Event Found");
// Read events
$events = $inotify->read_events();
/** we need to modify this to loop to group events into a cookie key array then see what is inside each item,
* to decide what we want to do with the data.
* This means we can work out if a rename event occurs (delete -> move_in) without deleting data
*
**/
$cookie_based_events = array();
foreach($events as $event) {
System_Daemon::info("Mask: ".$inotify->get_mask_type($event["mask"]));
System_Daemon::info("Event Dir: ".$inotify->get_event_dir($event["wd"]));
print_r ($event);
$cookie_based_events[$event["cookie"]][] = $event;
}
foreach($cookie_based_events as $cookie_events) { // loop through a cookie - detect type of event
/** Rename Files:
* are a combination of a "delete" then a "move_in"
* This could be improved by using the "cookie" value of the events list
* and send a file name request to sugarsync rather than a delete and an upload (as this could be dreadful with large files)
**/
if(count($cookie_events) == 2) { # should only be needed for rename events
$from_folder = $inotify->get_event_dir($cookie_events[0]["wd"]);
$from = $cookie_events[0]["name"];
$to_folder = $inotify->get_event_dir($cookie_events[1]["wd"]);
$to = $cookie_events[1]["name"];
$xml->rename_file($from_folder,$from,$to_folder,$to);
} else {
foreach($cookie_events as $event) {
switch($event["mask"]) {
case 8: # write finished - upload fine
case 128: # File moved in (drag/drop) - upload fine
System_Daemon::info("Write File: ".$inotify->get_mask_type($event["mask"]));
$xml->check_upload_new_file($event["name"],$inotify->get_event_dir($event["wd"]));
break;
/** Delete File **/
case 64:
case 512:
System_Daemon::info("Delete File - TODO: ".$inotify->get_mask_type($event["mask"]));
$xml->delete_file($event["name"],$inotify->get_event_dir($event["wd"]));
break;
default:
System_Daemon::info("No Action Taken");
break;
}
}
}
}
} else {
// System_Daemon::info("Sleep ($cnt)");
System_Daemon::info("$cnt) Checking for updates from server - requires a bit more testing");
//$xml->recurse_download($user_data_xml);
}
$runningOkay = true;
if (!$runningOkay) {
System_Daemon::err('Something produced an error, so this will be my last run');
}
// Relax the system by sleeping for a little bit
// iterate also clears statcache
System_Daemon::iterate(2);
$cnt++;
}
end_program();
function sig_handler($signal) {
System_Daemon::info('Sig:'.$signal);
if ($signal === SIGTERM || $signal === SIGINT) {
System_Daemon::warning('I received the termination signal. ' . $sig);
// Execute some final code
// and be sure to:
end_program();
}
}