BinaryFinder.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. declare(strict_types = 1);
  3. /**
  4. * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC;
  8. use OCP\IBinaryFinder;
  9. use OCP\ICache;
  10. use OCP\ICacheFactory;
  11. use OCP\IConfig;
  12. use Symfony\Component\Process\ExecutableFinder;
  13. /**
  14. * Service that find the binary path for a program
  15. */
  16. class BinaryFinder implements IBinaryFinder {
  17. public const DEFAULT_BINARY_SEARCH_PATHS = [
  18. '/usr/local/sbin',
  19. '/usr/local/bin',
  20. '/usr/sbin',
  21. '/usr/bin',
  22. '/sbin',
  23. '/bin',
  24. '/opt/bin',
  25. ];
  26. private ICache $cache;
  27. public function __construct(
  28. ICacheFactory $cacheFactory,
  29. private IConfig $config,
  30. ) {
  31. $this->cache = $cacheFactory->createLocal('findBinaryPath');
  32. }
  33. /**
  34. * Try to find a program
  35. *
  36. * @return false|string
  37. */
  38. public function findBinaryPath(string $program) {
  39. $result = $this->cache->get($program);
  40. if ($result !== null) {
  41. return $result;
  42. }
  43. $result = false;
  44. if (\OCP\Util::isFunctionEnabled('exec')) {
  45. $exeSniffer = new ExecutableFinder();
  46. // Returns null if nothing is found
  47. $result = $exeSniffer->find(
  48. $program,
  49. null,
  50. $this->config->getSystemValue('binary_search_paths', self::DEFAULT_BINARY_SEARCH_PATHS));
  51. if ($result === null) {
  52. $result = false;
  53. }
  54. }
  55. // store the value for 5 minutes
  56. $this->cache->set($program, $result, 300);
  57. return $result;
  58. }
  59. }