1
0

MaintenanceAndRepair.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  1. <?php
  2. /* $Id: MaintenanceAndRepair.php 3214 2007-09-03 15:59:06Z b4rt $ */
  3. /*******************************************************************************
  4. LICENSE
  5. This program is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License (GPL)
  7. as published by the Free Software Foundation; either version 2
  8. of the License, or (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. To read the license please visit http://www.gnu.org/copyleft/gpl.html
  14. *******************************************************************************/
  15. // states
  16. define('MAINTENANCEANDREPAIR_STATE_NULL', 0); // null
  17. define('MAINTENANCEANDREPAIR_STATE_OK', 1); // ok
  18. define('MAINTENANCEANDREPAIR_STATE_ERROR', -1); // error
  19. // modes
  20. define('MAINTENANCEANDREPAIR_MODE_CLI', 1); // cli
  21. define('MAINTENANCEANDREPAIR_MODE_WEB', 2); // web
  22. // types
  23. define('MAINTENANCEANDREPAIR_TYPE_INT', 0); // internal
  24. define('MAINTENANCEANDREPAIR_TYPE_STD', 1); // standard
  25. define('MAINTENANCEANDREPAIR_TYPE_EXT', 2); // extended
  26. /**
  27. * MaintenanceAndRepair
  28. */
  29. class MaintenanceAndRepair
  30. {
  31. // public fields
  32. var $name = "MaintenanceAndRepair";
  33. // state
  34. var $state = MAINTENANCEANDREPAIR_STATE_NULL;
  35. // messages-array
  36. var $messages = array();
  37. // private fields
  38. // mode
  39. var $_mode = 0;
  40. // transfer fields
  41. var $_bogusTransfers = array();
  42. var $_fixedTransfers = array();
  43. var $_restartTransfers = false;
  44. // counter
  45. var $_count = 0;
  46. var $_countProblems = 0;
  47. var $_countFixed = 0;
  48. // =========================================================================
  49. // public static methods
  50. // =========================================================================
  51. /**
  52. * accessor for singleton
  53. *
  54. * @return MaintenanceAndRepair
  55. */
  56. function getInstance() {
  57. global $instanceMaintenanceAndRepair;
  58. // initialize
  59. MaintenanceAndRepair::initialize();
  60. // return instance
  61. return $instanceMaintenanceAndRepair;
  62. }
  63. /**
  64. * initialize MaintenanceAndRepair.
  65. */
  66. function initialize() {
  67. global $instanceMaintenanceAndRepair;
  68. // create instance
  69. if (!isset($instanceMaintenanceAndRepair))
  70. $instanceMaintenanceAndRepair = new MaintenanceAndRepair();
  71. }
  72. /**
  73. * accessor for state
  74. *
  75. * @return int
  76. */
  77. function getState() {
  78. global $instanceMaintenanceAndRepair;
  79. return (isset($instanceMaintenanceAndRepair))
  80. ? $instanceMaintenanceAndRepair->state
  81. : MAINTENANCEANDREPAIR_STATE_NULL;
  82. }
  83. /**
  84. * accessor for messages
  85. *
  86. * @return array
  87. */
  88. function getMessages() {
  89. global $instanceMaintenanceAndRepair;
  90. return (isset($instanceMaintenanceAndRepair))
  91. ? $instanceMaintenanceAndRepair->messages
  92. : array();
  93. }
  94. /**
  95. * maintenance
  96. *
  97. * @param $trestart
  98. */
  99. function maintenance($type = MAINTENANCEANDREPAIR_TYPE_STD) {
  100. global $instanceMaintenanceAndRepair;
  101. // initialize
  102. MaintenanceAndRepair::initialize();
  103. // maintenance run
  104. $instanceMaintenanceAndRepair->instance_maintenance($type);
  105. }
  106. /**
  107. * repair
  108. */
  109. function repair() {
  110. global $instanceMaintenanceAndRepair;
  111. // initialize
  112. MaintenanceAndRepair::initialize();
  113. // repair run
  114. $instanceMaintenanceAndRepair->instance_repair();
  115. }
  116. // =========================================================================
  117. // ctor
  118. // =========================================================================
  119. /**
  120. * do not use direct, use the factory-method !
  121. *
  122. * @return MaintenanceAndRepair
  123. */
  124. function MaintenanceAndRepair() {
  125. global $argv;
  126. // messages
  127. $this->messages = array();
  128. // cli/web
  129. $this->_mode = (empty($argv[0]))
  130. ? MAINTENANCEANDREPAIR_MODE_WEB
  131. : MAINTENANCEANDREPAIR_MODE_CLI;
  132. }
  133. // =========================================================================
  134. // public methods
  135. // =========================================================================
  136. /**
  137. * instance_maintenance
  138. *
  139. * @param $type
  140. */
  141. function instance_maintenance($type = MAINTENANCEANDREPAIR_TYPE_STD) {
  142. // (re)set state
  143. $this->state = MAINTENANCEANDREPAIR_STATE_NULL;
  144. // output
  145. $this->_outputMessage("Running Maintenance...\n");
  146. if ($type == MAINTENANCEANDREPAIR_TYPE_INT) {
  147. // prune database
  148. $this->_maintenanceDatabasePrune();
  149. } else {
  150. // fluxd
  151. $this->_maintenanceFluxd();
  152. // fluazu
  153. $this->_maintenanceFluazu();
  154. // transfers
  155. $this->_maintenanceTransfers($type == MAINTENANCEANDREPAIR_TYPE_EXT);
  156. // database
  157. $this->_maintenanceDatabase();
  158. }
  159. // output
  160. $this->_outputMessage("Maintenance done.\n");
  161. // state
  162. $this->state = MAINTENANCEANDREPAIR_STATE_OK;
  163. }
  164. /**
  165. * instance_repair
  166. */
  167. function instance_repair() {
  168. // (re)set state
  169. $this->state = MAINTENANCEANDREPAIR_STATE_NULL;
  170. // output
  171. $this->_outputMessage("Running Repair...\n");
  172. // fluxd
  173. $this->_maintenanceFluxd();
  174. // fluazu
  175. $this->_maintenanceFluazu();
  176. // repair app
  177. $this->_repairApp();
  178. // database
  179. $this->_maintenanceDatabase();
  180. /* done */
  181. $this->_outputMessage("Repair done.\n");
  182. // state
  183. $this->state = MAINTENANCEANDREPAIR_STATE_OK;
  184. }
  185. // =========================================================================
  186. // private methods
  187. // =========================================================================
  188. /* maintenance-methods */
  189. /**
  190. * _maintenanceFluxd
  191. * delete leftovers of fluxd (only do this if daemon is not running)
  192. */
  193. function _maintenanceFluxd() {
  194. global $cfg;
  195. // output
  196. $this->_outputMessage("fluxd-maintenance...\n");
  197. // files
  198. $fdp = $cfg["path"].'.fluxd/fluxd.pid';
  199. $fds = $cfg["path"].'.fluxd/fluxd.sock';
  200. $fdpe = file_exists($fdp);
  201. $fdse = file_exists($fds);
  202. $fluxdLeftoversFound = false;
  203. $fctr = 0;
  204. if ($fdpe)
  205. $fctr++;
  206. if ($fdse)
  207. $fctr++;
  208. if ($fctr > 0) {
  209. if ("1" != @trim(shell_exec("ps x -o pid='' -o ppid='' -o command='' -ww 2> /dev/null | ".$cfg['bin_grep']." -v grep | ".$cfg['bin_grep']." 'fluxd running' | ".$cfg['bin_grep']." -c ".tfb_shellencode($cfg["docroot"]))))
  210. $fluxdLeftoversFound = true;
  211. }
  212. if ($fluxdLeftoversFound) {
  213. // problems
  214. $this->_outputMessage("found and removing fluxd-leftovers...\n");
  215. // pid
  216. if ($fdpe)
  217. @unlink($fdp);
  218. // socket
  219. if ($fdse)
  220. @unlink($fds);
  221. // DEBUG : log the repair
  222. if ($cfg['debuglevel'] > 0)
  223. AuditAction($cfg["constants"]["debug"], "fluxd-maintenance : found and removed fluxd-leftovers.");
  224. // output
  225. $this->_outputMessage("done.\n");
  226. } else {
  227. // no problems
  228. $this->_outputMessage("no problems found.\n");
  229. }
  230. /* done */
  231. $this->_outputMessage("fluxd-maintenance done.\n");
  232. }
  233. /**
  234. * _maintenanceFluazu
  235. * delete leftovers of fluazu (only do this if daemon is not running)
  236. */
  237. function _maintenanceFluazu() {
  238. global $cfg;
  239. // output
  240. $this->_outputMessage("fluazu-maintenance...\n");
  241. // files
  242. $fdp = $cfg["path"].'.fluazu/fluazu.pid';
  243. $fds = $cfg["path"].'.fluazu/fluazu.stat';
  244. $fdc = $cfg["path"].'.fluazu/fluazu.cmd';
  245. $fdpe = file_exists($fdp);
  246. $fdse = file_exists($fds);
  247. $fdce = file_exists($fdc);
  248. $leftoversFound = false;
  249. $fctr = 0;
  250. if ($fdpe)
  251. $fctr++;
  252. if ($fdse)
  253. $fctr++;
  254. if ($fdce)
  255. $fctr++;
  256. if ($fctr > 0) {
  257. if ("1" != @trim(shell_exec("ps x -o pid='' -o ppid='' -o command='' -ww 2> /dev/null | ".$cfg['bin_grep']." -v grep | ".$cfg['bin_grep']." 'fluazu.py' | ".$cfg['bin_grep']." -c ".tfb_shellencode($cfg["path"]))))
  258. $leftoversFound = true;
  259. }
  260. if ($leftoversFound) {
  261. // problems
  262. $this->_outputMessage("found and removing fluazu-leftovers...\n");
  263. // pid
  264. if ($fdpe)
  265. @unlink($fdp);
  266. // stat
  267. if ($fdse)
  268. @unlink($fds);
  269. // command
  270. if ($fdce)
  271. @unlink($fdc);
  272. // DEBUG : log the repair
  273. if ($cfg['debuglevel'] > 0)
  274. AuditAction($cfg["constants"]["debug"], "fluazu-maintenance : found and removed fluazu-leftovers.");
  275. // output
  276. $this->_outputMessage("done.\n");
  277. } else {
  278. // no problems
  279. $this->_outputMessage("no problems found.\n");
  280. }
  281. /* done */
  282. $this->_outputMessage("fluazu-maintenance done.\n");
  283. }
  284. /**
  285. * _maintenanceTransfers
  286. *
  287. * @param $trestart
  288. * @return boolean
  289. */
  290. function _maintenanceTransfers($trestart = false) {
  291. global $cfg, $db, $transfers;
  292. // set var
  293. $this->_restartTransfers = $trestart;
  294. // output
  295. $this->_outputMessage("transfers-maintenance...\n");
  296. // sanity-check for transfers-dir
  297. if (!is_dir($cfg["transfer_file_path"])) {
  298. $this->state = MAINTENANCEANDREPAIR_STATE_ERROR;
  299. $msg = "invalid dir-settings. no dir : ".$cfg["transfer_file_path"];
  300. array_push($this->messages , $msg);
  301. $this->_outputError($msg."\n");
  302. return false;
  303. }
  304. // pid-files of transfer-clients
  305. $pidFiles = array();
  306. if ($dirHandle = @opendir($cfg["transfer_file_path"])) {
  307. while (false !== ($file = @readdir($dirHandle))) {
  308. if ((strlen($file) > 3) && ((substr($file, -4, 4)) == ".pid"))
  309. array_push($pidFiles, $file);
  310. }
  311. @closedir($dirHandle);
  312. }
  313. // return if no pid-files found
  314. if (count($pidFiles) < 1) {
  315. $this->_outputMessage("no pid-files found.\n");
  316. $this->_outputMessage("transfers-maintenance done.\n");
  317. return true;
  318. }
  319. // get process-list
  320. $psString = trim(shell_exec("ps x -o pid='' -o ppid='' -o command='' -ww"));
  321. // test if client for pid is still up
  322. $this->_bogusTransfers = array();
  323. foreach ($pidFiles as $pidFile) {
  324. $transfer = (substr($pidFile, 0, -4));
  325. if (stristr($psString, $transfer) === false) {
  326. if (getTransferClient($transfer) != "azureus")
  327. array_push($this->_bogusTransfers, $transfer);
  328. }
  329. }
  330. // return if no stale pid-files
  331. $this->_countProblems = count($this->_bogusTransfers);
  332. if ($this->_countProblems < 1) {
  333. $this->_outputMessage("no stale pid-files found.\n");
  334. $this->_outputMessage("transfers-maintenance done.\n");
  335. return true;
  336. }
  337. /* repair the bogus clients */
  338. $this->_countFixed = 0;
  339. $this->_outputMessage("repairing died clients...\n");
  340. foreach ($this->_bogusTransfers as $transfer) {
  341. // output
  342. $this->_outputMessage("repairing ".$transfer." ...\n");
  343. // set stopped flag in db
  344. stopTransferSettings($transfer);
  345. // rewrite stat-file
  346. $sf = new StatFile($transfer, getOwner($transfer));
  347. $sf->running = 0;
  348. $sf->percent_done = -100.0;
  349. $sf->time_left = 'Transfer Died';
  350. $sf->down_speed = 0;
  351. $sf->up_speed = 0;
  352. $sf->seeds = 0;
  353. $sf->peers = 0;
  354. $sf->write();
  355. // delete pid-file
  356. @unlink($cfg["transfer_file_path"].$transfer.".pid");
  357. // DEBUG : log the repair of the bogus transfer
  358. if ($cfg['debuglevel'] > 0)
  359. AuditAction($cfg["constants"]["debug"], "transfers-maintenance : transfer repaired : ".$transfer);
  360. // output
  361. $this->_outputMessage("done.\n");
  362. // count
  363. $this->_countFixed++;
  364. }
  365. // output
  366. if ($this->_countProblems > 0)
  367. $this->_outputMessage("repaired transfers : ".$this->_countFixed."/".$this->_countProblems."\n");
  368. /* restart transfers */
  369. if ($this->_restartTransfers) {
  370. $this->_fixedTransfers = array();
  371. $this->_outputMessage("restarting died clients...\n");
  372. // hold current user
  373. $whoami = ($this->_mode == MAINTENANCEANDREPAIR_MODE_CLI) ? GetSuperAdmin() : $cfg["user"];
  374. foreach ($this->_bogusTransfers as $transfer) {
  375. // output
  376. $this->_outputMessage("Starting ".$transfer." ...\n");
  377. // set current user to transfer-owner
  378. $cfg["user"] = getOwner($transfer);
  379. // clientHandler + start
  380. $ch = ClientHandler::getInstance(getTransferClient($transfer));
  381. $ch->start($transfer, false, FluxdQmgr::isRunning());
  382. // DEBUG : log the restart of the died transfer
  383. if ($cfg['debuglevel'] > 0) {
  384. $staret = ($ch->state == CLIENTHANDLER_STATE_OK) ? "OK" : "FAILED";
  385. AuditAction($cfg["constants"]["debug"], "transfers-maintenance : restarted transfer ".$transfer." by ".$whoami." : ".$staret);
  386. }
  387. if ($ch->state == CLIENTHANDLER_STATE_OK) {
  388. // output
  389. $this->_outputMessage("done.\n");
  390. // add to ary
  391. array_push($this->_fixedTransfers, $transfer);
  392. // count
  393. $this->_countFixed++;
  394. } else {
  395. $this->messages = array_merge($this->messages, $ch->messages);
  396. $this->_outputError(implode("\n", $ch->messages)."\n");
  397. }
  398. }
  399. // set user back
  400. $cfg["user"] = $whoami;
  401. // output
  402. $this->_countFixed = count($this->_fixedTransfers);
  403. if ($this->_countFixed > 0)
  404. $this->_outputMessage("restarted transfers : ".$this->_countFixed."/".$this->_countProblems."\n");
  405. }
  406. /* done */
  407. $this->_outputMessage("transfers-maintenance done.\n");
  408. // return
  409. return true;
  410. }
  411. /**
  412. * _maintenanceDatabase
  413. */
  414. function _maintenanceDatabase() {
  415. global $cfg, $db;
  416. // output
  417. $this->_outputMessage("database-maintenance...\n");
  418. /* tf_transfers */
  419. $this->_countProblems = 0;
  420. $this->_countFixed = 0;
  421. // output
  422. $this->_outputMessage("table-maintenance : tf_transfers\n");
  423. // running-flag
  424. $sql = "SELECT transfer FROM tf_transfers WHERE running = '1'";
  425. $recordset = $db->Execute($sql);
  426. if ($db->ErrorNo() != 0) dbError($sql);
  427. $rc = $recordset->RecordCount();
  428. if ($rc > 0) {
  429. while (list($tname) = $recordset->FetchRow()) {
  430. if (!isTransferRunning($tname)) {
  431. $this->_countProblems++;
  432. // t is not running, reset running-flag
  433. $this->_outputMessage("reset of running-flag for transfer which is not running : ".$tname."\n");
  434. $sql = "UPDATE tf_transfers SET running = '0' WHERE transfer = ".$db->qstr($tname);
  435. $db->Execute($sql);
  436. $this->_countFixed++;
  437. // output
  438. $this->_outputMessage("done.\n");
  439. }
  440. }
  441. }
  442. // empty hash
  443. $sql = "SELECT transfer FROM tf_transfers WHERE hash = ''";
  444. $recordset = $db->Execute($sql);
  445. if ($db->ErrorNo() != 0) dbError($sql);
  446. $rc = $recordset->RecordCount();
  447. if ($rc > 0) {
  448. $this->_countProblems += $rc;
  449. while (list($tname) = $recordset->FetchRow()) {
  450. // t has no hash, update
  451. $this->_outputMessage("updating transfer which has empty hash : ".$tname."\n");
  452. // get hash
  453. $thash = getTransferHash($tname);
  454. // update
  455. if (!empty($thash)) {
  456. $sql = "UPDATE tf_transfers SET hash = ".$db->qstr($thash)." WHERE transfer = ".$db->qstr($tname);
  457. $db->Execute($sql);
  458. $this->_countFixed++;
  459. // output
  460. $this->_outputMessage("done.\n");
  461. }
  462. }
  463. }
  464. // empty datapath
  465. $sql = "SELECT transfer FROM tf_transfers WHERE datapath = ''";
  466. $recordset = $db->Execute($sql);
  467. if ($db->ErrorNo() != 0) dbError($sql);
  468. $rc = $recordset->RecordCount();
  469. if ($rc > 0) {
  470. $this->_countProblems += $rc;
  471. while (list($tname) = $recordset->FetchRow()) {
  472. // t has no datapath, update
  473. $this->_outputMessage("updating transfer which has empty datapath : ".$tname."\n");
  474. // get datapath
  475. $tDatapath = getTransferDatapath($tname);
  476. // update
  477. if ($tDatapath != "") {
  478. $sql = "UPDATE tf_transfers SET datapath = ".$db->qstr($tDatapath)." WHERE transfer = ".$db->qstr($tname);
  479. $db->Execute($sql);
  480. $this->_countFixed++;
  481. // output
  482. $this->_outputMessage("done.\n");
  483. } else {
  484. // output
  485. $this->_outputMessage("cannot get datapath for ".$tname.".\n");
  486. }
  487. }
  488. }
  489. // output + log
  490. if ($this->_countProblems == 0) {
  491. // output
  492. $this->_outputMessage("no problems found.\n");
  493. } else {
  494. // DEBUG : log
  495. $msg = "found and fixed problems in tf_transfers : ".$this->_countFixed."/".$this->_countProblems;
  496. if ($cfg['debuglevel'] > 0)
  497. AuditAction($cfg["constants"]["debug"], "database-maintenance : table-maintenance : ".$msg);
  498. // output
  499. $this->_outputMessage($msg."\n");
  500. }
  501. /* tf_transfer_totals */
  502. $this->_countProblems = 0;
  503. $this->_countFixed = 0;
  504. // output
  505. $this->_outputMessage("table-maintenance : tf_transfer_totals\n");
  506. $this->_countProblems = $db->GetOne("SELECT COUNT(*) FROM tf_transfer_totals WHERE tid = ''");
  507. if (($this->_countProblems !== false) && ($this->_countProblems > 0)) {
  508. // output
  509. $this->_outputMessage("found ".$this->_countProblems." invalid entries, deleting...\n");
  510. $sql = "DELETE FROM tf_transfer_totals WHERE tid = ''";
  511. $result = $db->Execute($sql);
  512. if ($db->ErrorNo() != 0) dbError($sql);
  513. $this->_countFixed = $db->Affected_Rows();
  514. // output
  515. $this->_outputMessage("done.\n");
  516. $rCount = ($this->_countFixed !== false) ? $this->_countFixed : $this->_countProblems;
  517. // DEBUG : log
  518. $msg = "found and removed invalid totals-entries from tf_transfer_totals : ".$rCount."/".$this->_countProblems;
  519. if ($cfg['debuglevel'] > 0)
  520. AuditAction($cfg["constants"]["debug"], "database-maintenance : table-maintenance : ".$msg);
  521. // output
  522. $this->_outputMessage($msg."\n");
  523. } else {
  524. // output
  525. $this->_outputMessage("no problems found.\n");
  526. }
  527. // prune db
  528. $this->_maintenanceDatabasePrune();
  529. /* done */
  530. $this->_outputMessage("database-maintenance done.\n");
  531. }
  532. /**
  533. * prune database
  534. */
  535. function _maintenanceDatabasePrune() {
  536. global $cfg, $db;
  537. // output
  538. $this->_outputMessage("pruning database...\n");
  539. $this->_outputMessage("table : tf_log\n");
  540. // Prune LOG
  541. $this->_count = 0;
  542. $testTime = time() - ($cfg['days_to_keep'] * 86400); // 86400 is one day in seconds
  543. $sql = "delete from tf_log where time < ".$db->qstr($testTime);
  544. $result = $db->Execute($sql);
  545. if ($db->ErrorNo() != 0) dbError($sql);
  546. $this->_count += $db->Affected_Rows();
  547. unset($result);
  548. $testTime = time() - ($cfg['minutes_to_keep'] * 60);
  549. $sql = "delete from tf_log where time < ".$db->qstr($testTime)." and action=".$db->qstr($cfg["constants"]["hit"]);
  550. $result = $db->Execute($sql);
  551. if ($db->ErrorNo() != 0) dbError($sql);
  552. $this->_count += $db->Affected_Rows();
  553. unset($result);
  554. /* done */
  555. if ($this->_count > 0)
  556. $this->_outputMessage("deleted entries from tf_log : ".$this->_count."\n");
  557. else
  558. $this->_outputMessage("no entries deleted.\n");
  559. $this->_outputMessage("prune database done.\n");
  560. }
  561. /* repair-methods */
  562. /**
  563. * _repairApp
  564. */
  565. function _repairApp() {
  566. global $cfg, $db;
  567. // output
  568. $this->_outputMessage("repairing app...\n");
  569. // sanity-check for transfers-dir
  570. if (!is_dir($cfg["transfer_file_path"])) {
  571. $this->state = MAINTENANCEANDREPAIR_STATE_ERROR;
  572. $msg = "invalid dir-settings. no dir : ".$cfg["transfer_file_path"];
  573. array_push($this->messages , $msg);
  574. $this->_outputError($msg."\n");
  575. return false;
  576. }
  577. // delete pid-files of clients
  578. if ($dirHandle = opendir($cfg["transfer_file_path"])) {
  579. while (false !== ($file = readdir($dirHandle))) {
  580. if ((strlen($file) > 3) && ((substr($file, -4, 4)) == ".pid"))
  581. @unlink($cfg["transfer_file_path"].$file);
  582. }
  583. closedir($dirHandle);
  584. }
  585. // rewrite stat-files
  586. $arList = getTransferArray();
  587. foreach ($arList as $transfer) {
  588. $sf = new StatFile($transfer, getOwner($transfer));
  589. // output
  590. $this->_outputMessage("rewrite stat-file for ".$transfer." ...\n");
  591. $sf->running = 0;
  592. $sf->percent_done = -100.0;
  593. $sf->time_left = 'repaired';
  594. $sf->down_speed = 0;
  595. $sf->up_speed = 0;
  596. $sf->seeds = 0;
  597. $sf->peers = 0;
  598. $sf->write();
  599. // output
  600. $this->_outputMessage("done.\n");
  601. }
  602. // set flags in db
  603. $this->_outputMessage("reset running-flag in database...\n");
  604. $db->Execute("UPDATE tf_transfers SET running = '0'");
  605. // output
  606. $this->_outputMessage("done.\n");
  607. /* done */
  608. $this->_outputMessage("repair app done.\n");
  609. }
  610. /* output-methods */
  611. /**
  612. * output message
  613. *
  614. * @param $message
  615. */
  616. function _outputMessage($message) {
  617. // only in cli-mode
  618. if ($this->_mode == MAINTENANCEANDREPAIR_MODE_CLI)
  619. printMessage($this->name, $message);
  620. }
  621. /**
  622. * output error
  623. *
  624. * @param $message
  625. */
  626. function _outputError($message) {
  627. // only in cli-mode
  628. if ($this->_mode == MAINTENANCEANDREPAIR_MODE_CLI)
  629. printError($this->name, $message);
  630. }
  631. }
  632. ?>