| 1 |
// Copyright 2018 Yuya Nishihara <[email protected]> |
| 2 |
// |
| 3 |
// This software may be used and distributed according to the terms of the |
| 4 |
// GNU General Public License version 2 or any later version. |
| 5 |
|
| 6 |
//! cHg extensions to command server client. |
| 7 |
|
| 8 |
use std::ffi::OsStr; |
| 9 |
use std::io; |
| 10 |
use std::mem; |
| 11 |
use std::os::unix::ffi::OsStrExt; |
| 12 |
use std::os::unix::io::AsRawFd; |
| 13 |
use std::path::Path; |
| 14 |
|
| 15 |
use bytes::BufMut; |
| 16 |
use bytes::BytesMut; |
| 17 |
use tokio_hglib::UnixClient; |
| 18 |
|
| 19 |
use crate::attachio; |
| 20 |
use crate::message::Instruction; |
| 21 |
use crate::message::ServerSpec; |
| 22 |
use crate::message::{self}; |
| 23 |
use crate::runcommand; |
| 24 |
use crate::uihandler::SystemHandler; |
| 25 |
|
| 26 |
/// Command-server client that also supports cHg extensions. |
| 27 |
pub struct ChgClient { |
| 28 |
client: UnixClient, |
| 29 |
} |
| 30 |
|
| 31 |
impl ChgClient { |
| 32 |
/// Connects to a command server listening at the specified socket path. |
| 33 |
pub async fn connect(path: impl AsRef<Path>) -> io::Result<Self> { |
| 34 |
let client = UnixClient::connect(path).await?; |
| 35 |
Ok(ChgClient { client }) |
| 36 |
} |
| 37 |
|
| 38 |
/// Server capabilities, encoding, etc. |
| 39 |
pub fn server_spec(&self) -> &ServerSpec { |
| 40 |
self.client.server_spec() |
| 41 |
} |
| 42 |
|
| 43 |
/// Attaches the client file descriptors to the server. |
| 44 |
pub async fn attach_io( |
| 45 |
&mut self, |
| 46 |
stdin: &impl AsRawFd, |
| 47 |
stdout: &impl AsRawFd, |
| 48 |
stderr: &impl AsRawFd, |
| 49 |
) -> io::Result<()> { |
| 50 |
attachio::attach_io( |
| 51 |
self.client.borrow_protocol_mut(), |
| 52 |
stdin, |
| 53 |
stdout, |
| 54 |
stderr, |
| 55 |
) |
| 56 |
.await |
| 57 |
} |
| 58 |
|
| 59 |
/// Changes the working directory of the server. |
| 60 |
pub async fn set_current_dir( |
| 61 |
&mut self, |
| 62 |
dir: impl AsRef<Path>, |
| 63 |
) -> io::Result<()> { |
| 64 |
let dir_bytes = dir.as_ref().as_os_str().as_bytes().to_owned(); |
| 65 |
self.client |
| 66 |
.borrow_protocol_mut() |
| 67 |
.send_command_with_args("chdir", dir_bytes) |
| 68 |
.await |
| 69 |
} |
| 70 |
|
| 71 |
/// Updates the environment variables of the server. |
| 72 |
pub async fn set_env_vars_os( |
| 73 |
&mut self, |
| 74 |
vars: impl IntoIterator<Item = (impl AsRef<OsStr>, impl AsRef<OsStr>)>, |
| 75 |
) -> io::Result<()> { |
| 76 |
self.client |
| 77 |
.borrow_protocol_mut() |
| 78 |
.send_command_with_args("setenv", message::pack_env_vars_os(vars)) |
| 79 |
.await |
| 80 |
} |
| 81 |
|
| 82 |
/// Changes the process title of the server. |
| 83 |
pub async fn set_process_name( |
| 84 |
&mut self, |
| 85 |
name: impl AsRef<OsStr>, |
| 86 |
) -> io::Result<()> { |
| 87 |
let name_bytes = name.as_ref().as_bytes().to_owned(); |
| 88 |
self.client |
| 89 |
.borrow_protocol_mut() |
| 90 |
.send_command_with_args("setprocname", name_bytes) |
| 91 |
.await |
| 92 |
} |
| 93 |
|
| 94 |
/// Changes the umask of the server process. |
| 95 |
pub async fn set_umask(&mut self, mask: u32) -> io::Result<()> { |
| 96 |
let mut mask_bytes = BytesMut::with_capacity(mem::size_of_val(&mask)); |
| 97 |
mask_bytes.put_u32(mask); |
| 98 |
self.client |
| 99 |
.borrow_protocol_mut() |
| 100 |
.send_command_with_args("setumask2", mask_bytes) |
| 101 |
.await |
| 102 |
} |
| 103 |
|
| 104 |
/// Runs the specified Mercurial command with cHg extension. |
| 105 |
pub async fn run_command_chg( |
| 106 |
&mut self, |
| 107 |
handler: &mut impl SystemHandler, |
| 108 |
args: impl IntoIterator<Item = impl AsRef<OsStr>>, |
| 109 |
) -> io::Result<i32> { |
| 110 |
runcommand::run_command( |
| 111 |
self.client.borrow_protocol_mut(), |
| 112 |
handler, |
| 113 |
message::pack_args_os(args), |
| 114 |
) |
| 115 |
.await |
| 116 |
} |
| 117 |
|
| 118 |
/// Validates if the server can run Mercurial commands with the expected |
| 119 |
/// configuration. |
| 120 |
/// |
| 121 |
/// The `args` should contain early command arguments such as `--config` |
| 122 |
/// and `-R`. |
| 123 |
/// |
| 124 |
/// Client-side environment must be sent prior to this request, by |
| 125 |
/// `set_current_dir()` and `set_env_vars_os()`. |
| 126 |
pub async fn validate( |
| 127 |
&mut self, |
| 128 |
args: impl IntoIterator<Item = impl AsRef<OsStr>>, |
| 129 |
) -> io::Result<Vec<Instruction>> { |
| 130 |
let data = self |
| 131 |
.client |
| 132 |
.borrow_protocol_mut() |
| 133 |
.query_with_args("validate", message::pack_args_os(args)) |
| 134 |
.await?; |
| 135 |
message::parse_instructions(data) |
| 136 |
} |
| 137 |
} |