-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiskFormatter.php
More file actions
executable file
·523 lines (448 loc) · 10.5 KB
/
Copy pathdiskFormatter.php
File metadata and controls
executable file
·523 lines (448 loc) · 10.5 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
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
#!/usr/bin/php -q
<?php
// Copyright Eskild Schroll-Fleischer 2012
// Version 1.0.0
// eskild.sf@coderer.com
// [-standalone] Use memory attached to current
// process as database.
// [-memcache] Use memcached database.
// [-flatfile] Use flatfile database.
// [-s] Run as slave to a master process.
// [-mac] Run with Mac driver.
// [-ubuntu] Run with ubuntu driver.
#// [-ask] Ask before formatting.
define('s',chr(10));
define('sPause', 2.5);
define('sFile', './db.txt');
define('dMaster', 1);
define('dSlave', 2);
define('sMemcachedKey', 'knownDisks');
define('sMemcachedIP', '127.0.0.1');
define('sMemcachedPort', '11211');
define('sAskBeforeFormatting', FALSE);
define('sDirToCopyFrom', './filesToCopy/');
//
// CLI Specific
//
exec('whoami', $output);
if ( $output[0] != 'root' ) {
echo 'This script must be run as root.'.s;
exit;
}
function getUserResponse() {
echo ':';
$handle = fopen('php://stdin','r');
$userResponse = trim(fgets($handle));
return $userResponse;
}
function presentHelp() {
global $argv;
}
function isOptionSet($option) {
global $argv;
$return = FALSE;
if ( is_string($option) ) {
foreach ( $argv as $arg ) {
if ( $arg == $option ) {
$return = TRUE;
}
}
}
return $return;
}
//
// Objects
//
class Disks {
public $disks = array();
function addDisk($disk) {
$this->disks[] = $disk;
}
function removeDiskById($id) {
$newDisks = array();
foreach ( $this->disks as $disk ) {
if ( $disk->id != $id ) {
$newDisks[] = $disk;
}
}
$this->disks = $newDisks;
}
function diskIsKnown($id) {
$return = FALSE;
foreach ( $this->disks as $disk ) {
if ( $disk->id == $id ) {
$return = TRUE;
}
}
return $return;
}
}
class Disk {
public $id;
public $mountPoint;
public $information;
public $location;
public $timestamp;
function __construct($id, $mountPoint, $information) {
$this->id = $id;
$this->mountPoint = $mountPoint;
$this->information = $information;
$location = explode(' ', $this->mountPoint);
$this->location = $location[0].'/';
}
}
class Files {
public $files;
function __construct($path = FALSE) {
$this->files = FALSE;
if ( $path != FALSE && $handle = opendir($path)) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
$this->files[] = new File($path.$entry);
}
}
closedir($handle);
}
}
function getStringOfFiles() {
$return = ''; $s = '';
if ( $this->files != FALSE ) {
foreach ( $this->files as $file ) {
$return .= $s.$file->name;
$s = s;
}
$return .= s;
}
return $return;
}
}
class File {
public $absolutePath;
public $name;
public $md5hash;
function __construct($absPath) {
$this->absolutePath = $absPath;
$this->md5hash = md5_file($this->absolutePath);
$name = explode('/', $this->absolutePath);
$this->name = end($name);
}
}
class Statistics {
public $treatedDisks;
function __construct() {
$this->treatedDisks = new Disks();
}
function addTreatedDisk($disk) {
$disk->timestamp = mktime();
$this->treatedDisks->addDIsk($disk);
}
function __destruct() {
print_r($this->treatedDisks);
}
}
//
// Drivers
//
if ( class_exists('Memcache') ) {
class memcacheDriver {
public $conn;
function __construct() {
$this->conn = new Memcache();
$this->conn->connect(sMemcachedIP, sMemcachedPort);
}
function __destruct() {
$this->conn->close();
}
function clear() {
$this->conn->flush();
}
function getKnownDisks() {
return $this->conn->get(sMemcachedKey);
}
function setKnownDisks($disks) {
if ( !$this->conn->replace(sMemcachedKey, $disks, 0, 0) ) {
$this->conn->add(sMemcachedKey, $disks, 0, 0);
}
}
function addKnownDisk($disk) {
$knownDisks = $this->getKnownDisks();
$knownDisks->addDisk($disk);
$this->setKnownDisks($knownDisks);
}
function removeKnownDiskById($id) {
$knownDisks = $this->getKnownDisks();
$knownDisks->removeDiskById($id);
$this->setKnownDisks($knownDisks);
}
}
}
class flatFileDriver {
public $file = sFile;
function read() {
/*
$return = FALSE;
$fileHandle = fopen($this->file, 'r');
$return = fread($fileHandle, filesize($this->file));
fclose($fileHandle);
*/
$return = file_get_contents($this->file);
return unserialize($return);
}
function write($object) {
$data = serialize($object);
$fileHandle = fopen($this->file, 'w');
flock($fileHandle, LOCK_EX);
fwrite($fileHandle, $data);
flock($fileHandle, LOCK_UN);
fclose($fileHandle);
}
function clear() {
file_put_contents($this->file, '');
}
function getKnownDisks() {
return $this->read();
}
function setKnownDisks($disks) {
$this->write($disks);
}
function addKnownDisk($disk) {
$knownDisks = $this->getKnownDisks();
$knownDisks->addDisk($disk);
$this->setKnownDisks($knownDisks);
}
function removeKnownDiskById($id) {
$knownDisks = $this->getKnownDisks();
$knownDisks->removeDiskById($id);
$this->setKnownDisks($knownDisks);
}
}
class standaloneDriver {
public $knownDisks;
function __construct() {
$this->knowDisks = new Disks();
}
function clear() {
$this->knownDisks = NULL;
}
function getKnownDisks() {
return $this->knownDisks;
}
function setKnownDisks($disks) {
$this->knownDisks = $disks;
}
function addKnownDisk($disk) {
$this->knownDisks->addDisk($disk);
}
function removeKnownDiskById($id) {
$this->knownDisks->removeDiskById($id);
}
}
if ( isOptionSet('-memcache') && class_exists('Memcache') ) {
class Model extends memcacheDriver {}
} elseif ( isOptionSet('-flatfile') ) {
class Model extends flatFileDriver {}
} elseif ( isOptionSet('-standalone') ) {
class Model extends standaloneDriver {}
} else {
class Model extends standaloneDriver {}
}
//
// Platform specific controller
//
class ubuntuDriver {
function getConnectedDisks() {
$disks = new Disks();
exec('mount',$output);
foreach ( $output as $line ) {
if ( stristr($line, '/dev/') ) {
$disk = getDiskFromMountOutput($line);
$disks->addDisk($disk);
}
}
return $disks;
}
function unmountDisk($disk) {
$output = array();
exec('umount -fv '.$disk->id, $output);
echoOutput($output);
if ( file_exists($disk->location) ) {
rmdir($disk->location);
}
}
function formatDisk($disk) {
$output = array();
exec('mkfs.vfat -F 32 -I '.$disk->id, $output);
echoOutput($output);
}
function mountDisk($disk) {
if ( !file_exists($disk->location) ) {
mkdir($disk->location);
}
$output = array();
exec('mount '.$disk->id.' '.$disk->location, $output);
echoOutput($output);
}
}
class macDriver {
function getConnectedDisks() {
$disks = new Disks();
exec('mount',$output);
foreach ( $output as $line ) {
if ( stristr($line, '/dev/') ) {
$disk = getDiskFromMountOutput($line);
$disks->addDisk($disk);
}
}
return $disks;
}
function unmountDisk($disk) {
$output = array();
exec('umount -fv '.$disk->id, $output);
echoOutput($output);
}
function formatDisk($disk) {
$output = array();
exec('diskutil reformat '.$disk->id, $output);
echoOutput($output);
}
function mountDisk($disk) {
if ( !file_exists($disk->location) ) {
mkdir($disk->location);
}
$output = array();
exec('mount '.$disk->id.' '.$disk->location, $output);
echoOutput($output);
}
}
if ( isOptionSet('-mac') ) {
class ControllerExtender extends macDriver {}
} elseif ( isOptionSet('-ubuntu') ) {
class ControllerExtender extends ubuntuDriver {}
} else {
if ( stristr(PHP_OS, 'darwin') ) {
class ControllerExtender extends macDriver {}
} elseif ( stristr(PHP_OS, 'linux') ) {
class ControllerExtender extends ubuntuDriver {}
}
}
class Controller extends controllerExtender {
public $m;
public $f;
//public $s;
public $role = dMaster;
function __construct() {
$this->m = new Model();
$this->f = new Files(sDirToCopyFrom);
//$this->s = new Statistics();
}
function getInitialDisks() {
$return = FALSE;
if ( $this->role == dMaster ) {
$return = $this->getConnectedDisks();
$this->m->setKnownDisks($return);
} elseif ( $this->role == dSlave ) {
$return = $this->m->getKnownDisks();
}
return $return;
}
function copyFilesToDisk($disk) {
if ( $this->f->files != FALSE ) {
foreach ( $this->f->files as $file ) {
copy($file->absolutePath, $disk->location.$file->name);
if ( $file->md5hash != md5_file($disk->location.$file->name) ) {
echo $file->name.' did not copy correctly.'.s;
getUserResponse();
}
}
}
}
function echoContentsOfDisk($disk) {
$files = new Files($disk->location);
echo 'Contents of disk:';
echo s;
echo $files->getStringOfFiles();
}
function detectNewDisks() {
while ( true ) {
$newDisks = new Disks();
$disks = $this->getConnectedDisks();
$knownDisks = $this->m->getKnownDisks();
foreach ( $disks->disks as $disk ) {
if ( !$knownDisks->diskIsKnown($disk->id) ) {
$newDisks->addDisk($disk);
}
}
if ( $newDisks->disks != array() ) {
$currentDisk = $newDisks->disks[0];
$this->m->addKnownDisk($currentDisk);
$response = 'y';
if ( sAskBeforeFormatting ) {
echo 'Do you wish to treat '.$currentDisk->id.' on '.$currentDisk->mountPoint.'?'.s;
$response = getUserResponse();
}
if ( !stristr($response,'n') ) {
$this->treatDisk($currentDisk);
break;
}
} else {
presentDisks($newDisks);
}
sleep(sPause);
}
}
function treatDisk($disk) {
echo s;
$this->unmountDisk($disk);
$this->formatDisk($disk);
if ( $this->f->files != FALSE ) {
$this->mountDisk($disk);
$this->copyFilesToDisk($disk);
$this->echoContentsOfDisk($disk);
$this->unmountDisk($disk);
}
echo $disk->id.' has been treated.'.s.s;
$this->m->removeKnownDiskById($disk->id);
$this->detectNewDisks();
}
}
//
// Communication
//
function getDiskFromMountOutput($line) {
$info = explode(' on ', $line);
$id = $info[0];
$info = explode(' (', $info[1]);
$mountPoint = $info[0];
$information = '('.$info[1];
$disk = new Disk($id, $mountPoint, $information);
return $disk;
}
function presentDisks($disks) {
if ( $disks->disks == array() ) {
echo 'There are no disks.'.s;
} else {
foreach ( $disks->disks as $disk ) {
echo $disk->id.' on '.$disk->mountPoint.' with '.$disk->information.s;
}
}
}
function echoOutput($output) {
foreach ( $output as $line ) {
echo $line.s;
}
}
//
// Run
//
$c = new Controller();
if ( isOptionSet('-s') ) {
$c->role = dSlave;
}
$d = $c->getInitialDisks();
echo 'These are the devices that are currently connected.'.s.s;
presentDisks($d);
echo s;
echo 'They will not be touched.'.s;
echo 'The runloop will now start.'.s.s;
$c->detectNewDisks();
?>