Patchwork [6,of,6] rust-chg: abort if server doesn't have enough capabilities

login
register
mail settings
Submitter Yuya Nishihara
Date Oct. 14, 2018, 8:18 a.m.
Message ID <29b826e65d9b738fc29f.1539505103@mimosa>
Download mbox | patch
Permalink /patch/35959/
State Accepted
Headers show

Comments

Yuya Nishihara - Oct. 14, 2018, 8:18 a.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1538874742 -32400
#      Sun Oct 07 10:12:22 2018 +0900
# Node ID 29b826e65d9b738fc29f5600f798e4d048616aa4
# Parent  389e082b48ba5a543a2c0497963cca4231e22504
rust-chg: abort if server doesn't have enough capabilities

It's checked at Locator::connect() since we'll have to do more stuff in
this function.

Patch

diff --git a/rust/chg/src/locator.rs b/rust/chg/src/locator.rs
--- a/rust/chg/src/locator.rs
+++ b/rust/chg/src/locator.rs
@@ -20,8 +20,15 @@  use tokio_hglib::UnixClient;
 use tokio_process::{Child, CommandExt};
 use tokio_timer;
 
+use super::message::ServerSpec;
 use super::procutil;
 
+const REQUIRED_SERVER_CAPABILITIES: &[&str] = &[
+    "attachio",
+    "chdir",
+    "runcommand",
+];
+
 /// Helper to connect to and spawn a server process.
 #[derive(Clone, Debug)]
 pub struct Locator {
@@ -75,6 +82,10 @@  impl Locator {
                     Err(_) => Either::B(self.spawn_connect()),
                 }
             })
+            .and_then(|(loc, client)| {
+                check_server_capabilities(client.server_spec())?;
+                Ok((loc, client))
+            })
     }
 
     /// Spawns new server process and connects to it.
@@ -222,3 +233,15 @@  fn check_secure_dir<P>(path: P) -> io::R
         Err(io::Error::new(io::ErrorKind::Other, "insecure directory"))
     }
 }
+
+fn check_server_capabilities(spec: &ServerSpec) -> io::Result<()> {
+    let unsupported: Vec<_> = REQUIRED_SERVER_CAPABILITIES.iter().cloned()
+        .filter(|&s| !spec.capabilities.contains(s))
+        .collect();
+    if unsupported.is_empty() {
+        Ok(())
+    } else {
+        let msg = format!("insufficient server capabilities: {}", unsupported.join(", "));
+        Err(io::Error::new(io::ErrorKind::Other, msg))
+    }
+}