]> git.ipfire.org Git - thirdparty/bacula.git/commitdiff
baculum: Add API endpoint to list files/dirs on client
authorMarcin Haba <marcin.haba@bacula.pl>
Sat, 13 Apr 2019 10:05:39 +0000 (12:05 +0200)
committerMarcin Haba <marcin.haba@bacula.pl>
Mon, 15 Apr 2019 19:00:21 +0000 (21:00 +0200)
gui/baculum/protected/API/Class/Bconsole.php
gui/baculum/protected/API/Class/Ls.php [new file with mode: 0644]
gui/baculum/protected/API/Pages/API/ClientLs.php [new file with mode: 0644]
gui/baculum/protected/API/Pages/config.xml
gui/baculum/protected/API/endpoints.xml
gui/baculum/protected/Common/Class/Errors.php

index ae9348a02761f21f388d68f710df9ba704bd9710..b2aef0c5b29009d16cebf732ae63bfee13a3f9f2 100644 (file)
@@ -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 (file)
index 0000000..ce8c130
--- /dev/null
@@ -0,0 +1,50 @@
+<?php
+/*
+ * Bacula(R) - The Network Backup Solution
+ * Baculum   - Bacula web interface
+ *
+ * Copyright (C) 2013-2019 Kern Sibbald
+ *
+ * The main author of Baculum is Marcin Haba.
+ * The original author of Bacula is Kern Sibbald, with contributions
+ * from many others, a complete list can be found in the file AUTHORS.
+ *
+ * You may use this file and others of this release according to the
+ * license defined in the LICENSE file, which includes the Affero General
+ * Public License, v3.0 ("AGPLv3") and some additional permissions and
+ * terms pursuant to its AGPLv3 Section 7.
+ *
+ * This notice must be preserved when any source code is
+ * conveyed and/or propagated.
+ *
+ * Bacula(R) is a registered trademark of Kern Sibbald.
+ */
+
+
+Prado::using('Application.API.Class.APIModule');
+
+class Ls extends APIModule {
+       const LS_OUTPUT_PATTERN = '/^(?P<perm>[a-z\-\.]+)\s+(?P<nb_hardlink>\d+)\s+(?P<owner>\w+)\s+(?P<group>\w+)\s+(?P<size>\d+)\s+(?P<mtime>[\d\-]+\s+[\d:]+)\s+(?P<item>(?U:[\S\s]+))(?P<dest>(?(?=\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 (file)
index 0000000..db06771
--- /dev/null
@@ -0,0 +1,63 @@
+<?php
+/*
+ * Bacula(R) - The Network Backup Solution
+ * Baculum   - Bacula web interface
+ *
+ * Copyright (C) 2013-2019 Kern Sibbald
+ *
+ * The main author of Baculum is Marcin Haba.
+ * The original author of Bacula is Kern Sibbald, with contributions
+ * from many others, a complete list can be found in the file AUTHORS.
+ *
+ * You may use this file and others of this release according to the
+ * license defined in the LICENSE file, which includes the Affero General
+ * Public License, v3.0 ("AGPLv3") and some additional permissions and
+ * terms pursuant to its AGPLv3 Section 7.
+ *
+ * This notice must be preserved when any source code is
+ * conveyed and/or propagated.
+ *
+ * Bacula(R) is a registered trademark of Kern Sibbald.
+ */
+
+class ClientLs extends BaculumAPIServer {
+
+       public function get() {
+               $clientid = $this->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;
+               }
+       }
+}
+?>
index ceb6547d5cd76029fedf50928906eae99eefd1c0..2023d39a7671a3dc0c7dae9aee14db1531d7df50 100644 (file)
@@ -38,5 +38,7 @@
                <!-- component status modules -->
                <module id="status_dir" class="Application.API.Class.StatusDirector" />
                <module id="status_sd" class="Application.API.Class.StatusStorage" />
+               <!-- misc modules -->
+               <module id="ls" class="Application.API.Class.Ls" />
        </modules>
 </configuration>
index 4b6ec392f219ea821f28f86f24138e82abeae778..a7f1af0157285cccea88044de0d6e2cc6045791d 100644 (file)
@@ -25,6 +25,7 @@
        <url ServiceParameter="API.ClientShow" pattern="api/v1/clients/{id}/show/" parameters.id="\d+" />
        <url ServiceParameter="API.ClientStatus" pattern="api/v1/clients/{id}/status/" parameters.id="\d+" />
        <url ServiceParameter="API.JobsForClient" pattern="api/v1/clients/{id}/jobs/" parameters.id="\d+" />
+       <url ServiceParameter="API.ClientLs" pattern="api/v1/clients/{id}/ls/" parameters.id="\d+" />
        <!-- storages (storage daemons) endpoints -->
        <url ServiceParameter="API.Storages" pattern="api/v1/storages/" />
        <url ServiceParameter="API.Storage" pattern="api/v1/storages/{id}/" parameters.id="\d+" />
index da5e615bfa244f24e91078aab9f60be92cc39d8f..dfaf702d84d106a1837b89bec6c431ff7fdde69c 100644 (file)
@@ -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 {