From: Marcin Haba Date: Sat, 13 Apr 2019 10:05:39 +0000 (+0200) Subject: baculum: Add API endpoint to list files/dirs on client X-Git-Tag: Release-9.4.3~46 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=735327228201b1e577cca0237a2959c16608bb1e;p=thirdparty%2Fbacula.git baculum: Add API endpoint to list files/dirs on client --- diff --git a/gui/baculum/protected/API/Class/Bconsole.php b/gui/baculum/protected/API/Class/Bconsole.php index ae9348a02..b2aef0c5b 100644 --- a/gui/baculum/protected/API/Class/Bconsole.php +++ b/gui/baculum/protected/API/Class/Bconsole.php @@ -3,7 +3,7 @@ * Bacula(R) - The Network Backup Solution * Baculum - Bacula web interface * - * Copyright (C) 2013-2018 Kern Sibbald + * Copyright (C) 2013-2019 Kern Sibbald * * The main author of Baculum is Marcin Haba. * The original author of Bacula is Kern Sibbald, with contributions @@ -68,7 +68,8 @@ class Bconsole extends APIModule { '.pool', '.schedule', '.api', - '.status' + '.status', + '.ls' ); private $config; diff --git a/gui/baculum/protected/API/Class/Ls.php b/gui/baculum/protected/API/Class/Ls.php new file mode 100644 index 000000000..ce8c130d8 --- /dev/null +++ b/gui/baculum/protected/API/Class/Ls.php @@ -0,0 +1,50 @@ +[a-z\-\.]+)\s+(?P\d+)\s+(?P\w+)\s+(?P\w+)\s+(?P\d+)\s+(?P[\d\-]+\s+[\d:]+)\s+(?P(?U:[\S\s]+))(?P(?(?=\s+\-\>\s+)[\S\s]*))$/i'; + + public function parseOutput(array $output) { + $result = array(); + for ($i = 0; $i < count($output); $i++) { + if (preg_match(self::LS_OUTPUT_PATTERN, $output[$i], $match) === 1) { + $type = substr($match['perm'], 0, 1); + $result[] = array( + 'perm' => $match['perm'], + 'nb_hardlink' => intval($match['nb_hardlink']), + 'owner' => $match['owner'], + 'group' => $match['group'], + 'size' => intval($match['size']), + 'mtime' => $match['mtime'], + 'item' => $match['item'], + 'type' => $type, + 'dest' => key_exists('dest', $match) ? $match['dest'] : null + ); + } + } + return $result; + } +} +?> diff --git a/gui/baculum/protected/API/Pages/API/ClientLs.php b/gui/baculum/protected/API/Pages/API/ClientLs.php new file mode 100644 index 000000000..db067719b --- /dev/null +++ b/gui/baculum/protected/API/Pages/API/ClientLs.php @@ -0,0 +1,63 @@ +Request->contains('id') ? intval($this->Request['id']) : 0; + $client = null; + $cli = null; + if ($clientid > 0) { + $cli = $this->getModule('client')->getClientById($clientid); + } + + $result = $this->getModule('bconsole')->bconsoleCommand($this->director, array('.client')); + if ($result->exitcode === 0) { + array_shift($result->output); + if(is_object($cli) && in_array($cli->name, $result->output)) { + $client = $cli->name; + } + } + if (is_null($client)) { + $this->output = ClientError::MSG_ERROR_CLIENT_DOES_NOT_EXISTS; + $this->error = ClientError::ERROR_CLIENT_DOES_NOT_EXISTS; + return; + } + + $path = $this->Request->contains('path') && $this->getModule('misc')->isValidPath($this->Request['path']) ? $this->Request['path'] : null; + + if (is_null($path)) { + $this->output = GenericError::MSG_ERROR_INVALID_PATH; + $this->error = GenericError::ERROR_INVALID_PATH; + return; + } + + $cmd = array('.ls', 'client="' . $client . '"', 'path="' . $path . '"'); + $result = $this->getModule('bconsole')->bconsoleCommand($this->director, $cmd); + if ($result->exitcode === 0) { + $ls = $this->getModule('ls')->parseOutput($result->output); + $this->output = $ls; + $this->error = GenericError::ERROR_NO_ERRORS; + } + } +} +?> diff --git a/gui/baculum/protected/API/Pages/config.xml b/gui/baculum/protected/API/Pages/config.xml index ceb6547d5..2023d39a7 100644 --- a/gui/baculum/protected/API/Pages/config.xml +++ b/gui/baculum/protected/API/Pages/config.xml @@ -38,5 +38,7 @@ + + diff --git a/gui/baculum/protected/API/endpoints.xml b/gui/baculum/protected/API/endpoints.xml index 4b6ec392f..a7f1af015 100644 --- a/gui/baculum/protected/API/endpoints.xml +++ b/gui/baculum/protected/API/endpoints.xml @@ -25,6 +25,7 @@ + diff --git a/gui/baculum/protected/Common/Class/Errors.php b/gui/baculum/protected/Common/Class/Errors.php index da5e615bf..dfaf702d8 100644 --- a/gui/baculum/protected/Common/Class/Errors.php +++ b/gui/baculum/protected/Common/Class/Errors.php @@ -3,7 +3,7 @@ * Bacula(R) - The Network Backup Solution * Baculum - Bacula web interface * - * Copyright (C) 2013-2018 Kern Sibbald + * Copyright (C) 2013-2019 Kern Sibbald * * The main author of Baculum is Marcin Haba. * The original author of Bacula is Kern Sibbald, with contributions @@ -24,10 +24,12 @@ class GenericError { const ERROR_NO_ERRORS = 0; const ERROR_INVALID_COMMAND = 1; const ERROR_INTERNAL_ERROR = 1000; + const ERROR_INVALID_PATH = 8; const MSG_ERROR_NO_ERRORS = ''; const MSG_ERROR_INVALID_COMMAND = 'Invalid command.'; const MSG_ERROR_INTERNAL_ERROR = 'Internal error.'; + const MSG_ERROR_INVALID_PATH = 'Invalid path.'; } class DatabaseError extends GenericError {