BinaryFinder.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. declare(strict_types = 1);
  3. /**
  4. * @copyright 2022 Carl Schwan <carl@carlschwan.eu>
  5. * @license GNU AGPL version 3 or any later version
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as
  9. * published by the Free Software Foundation, either version 3 of the
  10. * License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. namespace OC;
  22. use OCP\ICache;
  23. use OCP\ICacheFactory;
  24. use OCP\IBinaryFinder;
  25. use Symfony\Component\Process\ExecutableFinder;
  26. /**
  27. * Service that find the binary path for a program
  28. */
  29. class BinaryFinder implements IBinaryFinder {
  30. private ICache $cache;
  31. public function __construct(ICacheFactory $cacheFactory) {
  32. $this->cache = $cacheFactory->createLocal('findBinaryPath');
  33. }
  34. /**
  35. * Try to find a program
  36. *
  37. * @return false|string
  38. */
  39. public function findBinaryPath(string $program) {
  40. $result = $this->cache->get($program);
  41. if ($result !== null) {
  42. return $result;
  43. }
  44. $result = false;
  45. if (\OCP\Util::isFunctionEnabled('exec')) {
  46. $exeSniffer = new ExecutableFinder();
  47. // Returns null if nothing is found
  48. $result = $exeSniffer->find($program, null, [
  49. '/usr/local/sbin',
  50. '/usr/local/bin',
  51. '/usr/sbin',
  52. '/usr/bin',
  53. '/sbin',
  54. '/bin',
  55. '/opt/bin',
  56. ]);
  57. if ($result === null) {
  58. $result = false;
  59. }
  60. }
  61. // store the value for 5 minutes
  62. $this->cache->set($program, $result, 300);
  63. return $result;
  64. }
  65. }