##// END OF EJS Templates
rust-chg: add struct holding information needed to spawn server process...
Yuya Nishihara -
r40325:7d3285f7 default
parent child Browse files
Show More
@@ -6,13 +6,54 b''
6 //! Utility for locating command-server process.
6 //! Utility for locating command-server process.
7
7
8 use std::env;
8 use std::env;
9 use std::ffi::{OsStr, OsString};
9 use std::fs::{self, DirBuilder};
10 use std::fs::{self, DirBuilder};
10 use std::io;
11 use std::io;
12 use std::os::unix::ffi::{OsStrExt, OsStringExt};
11 use std::os::unix::fs::{DirBuilderExt, MetadataExt};
13 use std::os::unix::fs::{DirBuilderExt, MetadataExt};
12 use std::path::{Path, PathBuf};
14 use std::path::{Path, PathBuf};
15 use std::process;
16 use std::time::Duration;
13
17
14 use super::procutil;
18 use super::procutil;
15
19
20 /// Helper to connect to and spawn a server process.
21 #[derive(Clone, Debug)]
22 pub struct Locator {
23 hg_command: OsString,
24 current_dir: PathBuf,
25 env_vars: Vec<(OsString, OsString)>,
26 process_id: u32,
27 base_sock_path: PathBuf,
28 timeout: Duration,
29 }
30
31 impl Locator {
32 /// Creates locator capturing the current process environment.
33 ///
34 /// If no `$CHGSOCKNAME` is specified, the socket directory will be
35 /// created as necessary.
36 pub fn prepare_from_env() -> io::Result<Locator> {
37 Ok(Locator {
38 hg_command: default_hg_command(),
39 current_dir: env::current_dir()?,
40 env_vars: env::vars_os().collect(),
41 process_id: process::id(),
42 base_sock_path: prepare_server_socket_path()?,
43 timeout: default_timeout(),
44 })
45 }
46
47 /// Temporary socket path for this client process.
48 fn temp_sock_path(&self) -> PathBuf {
49 let src = self.base_sock_path.as_os_str().as_bytes();
50 let mut buf = Vec::with_capacity(src.len() + 6);
51 buf.extend_from_slice(src);
52 buf.extend_from_slice(format!(".{}", self.process_id).as_bytes());
53 OsString::from_vec(buf).into()
54 }
55 }
56
16 /// Determines the server socket to connect to.
57 /// Determines the server socket to connect to.
17 ///
58 ///
18 /// If no `$CHGSOCKNAME` is specified, the socket directory will be created
59 /// If no `$CHGSOCKNAME` is specified, the socket directory will be created
@@ -47,6 +88,17 b' pub fn default_server_socket_dir() -> Pa'
47 }
88 }
48 }
89 }
49
90
91 /// Determines the default hg command.
92 pub fn default_hg_command() -> OsString {
93 // TODO: maybe allow embedding the path at compile time (or load from hgrc)
94 env::var_os("CHGHG").or(env::var_os("HG")).unwrap_or(OsStr::new("hg").to_owned())
95 }
96
97 fn default_timeout() -> Duration {
98 let secs = env::var("CHGTIMEOUT").ok().and_then(|s| s.parse().ok()).unwrap_or(60);
99 Duration::from_secs(secs)
100 }
101
50 /// Creates a directory which the other users cannot access to.
102 /// Creates a directory which the other users cannot access to.
51 ///
103 ///
52 /// If the directory already exists, tests its permission.
104 /// If the directory already exists, tests its permission.
General Comments 0
You need to be logged in to leave comments. Login now