]> git.proxmox.com Git - proxmox-backup.git/blobdiff - src/bin/proxmox-backup-client.rs
client: use hyper-tls for now
[proxmox-backup.git] / src / bin / proxmox-backup-client.rs
index 1ad3a1c0282d41014169001cf22b1cb6460655fe..cbd6953fe276fb11a4974783eac16d87508c9c04 100644 (file)
@@ -1,7 +1,7 @@
 extern crate proxmox_backup;
 
 use failure::*;
-use std::os::unix::io::AsRawFd;
+//use std::os::unix::io::AsRawFd;
 
 use proxmox_backup::tools;
 use proxmox_backup::cli::command::*;
@@ -13,7 +13,7 @@ use proxmox_backup::client::catar_backup_stream::*;
 //use proxmox_backup::backup::image_index::*;
 //use proxmox_backup::config::datastore;
 //use proxmox_backup::catar::encoder::*;
-use proxmox_backup::backup::datastore::*;
+//use proxmox_backup::backup::datastore::*;
 
 use serde_json::{Value};
 use hyper::Body;
@@ -32,7 +32,7 @@ fn backup_directory(body: Body, store: &str, archive_name: &str) -> Result<(), E
         .append_pair("time", &epoch.to_string())
         .finish();
 
-    let path = format!("api3/json/admin/datastore/{}/upload_catar?{}", store, query);
+    let path = format!("api2/json/admin/datastore/{}/catar?{}", store, query);
 
     client.upload("application/x-proxmox-backup-catar", body, &path)?;
 
@@ -45,11 +45,11 @@ fn backup_image(datastore: &DataStore, file: &std::fs::File, size: usize, target
     let mut target = PathBuf::from(target);
 
     if let Some(ext) = target.extension() {
-        if ext != "iidx" {
-            bail!("got wrong file extension - expected '.iidx'");
+        if ext != "fidx" {
+            bail!("got wrong file extension - expected '.fidx'");
         }
     } else {
-        target.set_extension("iidx");
+        target.set_extension("fidx");
     }
 
     let mut index = datastore.create_image_writer(&target, size, chunk_size)?;
@@ -65,19 +65,40 @@ fn backup_image(datastore: &DataStore, file: &std::fs::File, size: usize, target
 }
 */
 
-fn create_backup(param: Value, _info: &ApiMethod) -> Result<Value, Error> {
+fn list_backups(
+    param: Value,
+    _info: &ApiMethod,
+    _rpcenv: &mut RpcEnvironment,
+) -> Result<Value, Error> {
+
+    let store = tools::required_string_param(&param, "store")?;
+
+    let client = HttpClient::new("localhost");
+
+    let path = format!("api2/json/admin/datastore/{}/backups", store);
+
+    let result = client.get(&path)?;
+
+    Ok(result)
+}
+
+fn create_backup(
+    param: Value,
+    _info: &ApiMethod,
+    _rpcenv: &mut RpcEnvironment,
+) -> Result<Value, Error> {
 
     let filename = tools::required_string_param(&param, "filename")?;
     let store = tools::required_string_param(&param, "store")?;
     let target = tools::required_string_param(&param, "target")?;
 
-    let mut chunk_size = 4*1024*1024;
+    let mut _chunk_size = 4*1024*1024;
 
     if let Some(size) = param["chunk-size"].as_u64() {
         static SIZES: [u64; 7] = [64, 128, 256, 512, 1024, 2048, 4096];
 
         if SIZES.contains(&size) {
-            chunk_size = (size as usize) * 1024;
+            _chunk_size = (size as usize) * 1024;
         } else {
             bail!("Got unsupported chunk size '{}'", size);
         }
@@ -101,7 +122,7 @@ fn create_backup(param: Value, _info: &ApiMethod) -> Result<Value, Error> {
         println!("Backup image '{}' to '{}'", filename, store);
 
         if stat.st_size <= 0 { bail!("got strange file size '{}'", stat.st_size); }
-        let size = stat.st_size as usize;
+        let _size = stat.st_size as usize;
 
         panic!("implement me");
 
@@ -119,65 +140,9 @@ fn create_backup(param: Value, _info: &ApiMethod) -> Result<Value, Error> {
     Ok(Value::Null)
 }
 
-
-pub fn complete_file_name(arg: &str) -> Vec<String> {
-
-    let mut result = vec![];
-
-    use nix::fcntl::OFlag;
-    use nix::sys::stat::Mode;
-    use nix::fcntl::AtFlags;
-
-    let mut dirname = std::path::PathBuf::from(arg);
-
-    let is_dir = match nix::sys::stat::fstatat(libc::AT_FDCWD, &dirname, AtFlags::empty()) {
-        Ok(stat) => (stat.st_mode & libc::S_IFMT) == libc::S_IFDIR,
-        Err(_) => false,
-    };
-
-    if !is_dir {
-        if let Some(parent) = dirname.parent() {
-            dirname = parent.to_owned();
-        }
-    }
-
-    let mut dir = match nix::dir::Dir::openat(libc::AT_FDCWD, &dirname, OFlag::O_DIRECTORY, Mode::empty()) {
-        Ok(d) => d,
-        Err(err) => {
-            return result;
-        }
-    };
-
-    for item in dir.iter() {
-        if let Ok(entry) = item {
-            if let Ok(name) = entry.file_name().to_str() {
-                if name == "." || name == ".." { continue; }
-                let mut newpath = dirname.clone();
-                newpath.push(name);
-
-                if let Ok(stat) = nix::sys::stat::fstatat(libc::AT_FDCWD, &newpath, AtFlags::empty()) {
-                    if (stat.st_mode & libc::S_IFMT) == libc::S_IFDIR {
-                        newpath.push("");
-                        if let Some(newpath) = newpath.to_str() {
-                            result.push(newpath.to_owned());
-                        }
-                        continue;
-                     }
-                }
-                if let Some(newpath) = newpath.to_str() {
-                    result.push(newpath.to_owned());
-                }
-
-             }
-        }
-    }
-
-    result
-}
-
 fn main() {
 
-    let cmd_def = CliCommand::new(
+    let create_cmd_def = CliCommand::new(
         ApiMethod::new(
             create_backup,
             ObjectSchema::new("Create backup.")
@@ -193,10 +158,23 @@ fn main() {
                 )
         ))
         .arg_param(vec!["filename", "target"])
-        .completion_cb("filename", complete_file_name)
+        .completion_cb("filename", tools::complete_file_name)
+        .completion_cb("store", proxmox_backup::config::datastore::complete_datastore_name);
+
+    let list_cmd_def = CliCommand::new(
+        ApiMethod::new(
+            list_backups,
+            ObjectSchema::new("List backups.")
+                .required("store", StringSchema::new("Datastore name."))
+        ))
+        .arg_param(vec!["store"])
         .completion_cb("store", proxmox_backup::config::datastore::complete_datastore_name);
 
 
+    let cmd_def = CliCommandMap::new()
+        .insert("create".to_owned(), create_cmd_def.into())
+        .insert("list".to_owned(), list_cmd_def.into());
+
     if let Err(err) = run_cli_command(&cmd_def.into()) {
         eprintln!("Error: {}", err);
         if err.downcast::<UsageError>().is_ok() {