2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2019-08-23 01:30:14 -04:00
|
|
|
use super::dispatch_json::{Deserialize, JsonOp, Value};
|
2020-02-23 14:51:29 -05:00
|
|
|
use crate::op_error::OpError;
|
2020-02-08 14:34:31 -05:00
|
|
|
use crate::state::State;
|
2020-04-23 05:51:07 -04:00
|
|
|
use deno_core::CoreIsolate;
|
|
|
|
use deno_core::ZeroCopyBuf;
|
2019-08-23 01:30:14 -04:00
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::env;
|
2019-12-15 00:14:20 -05:00
|
|
|
use std::io::{Error, ErrorKind};
|
2019-08-14 11:03:02 -04:00
|
|
|
use url::Url;
|
|
|
|
|
2020-04-23 05:51:07 -04:00
|
|
|
pub fn init(i: &mut CoreIsolate, s: &State) {
|
2020-02-25 09:14:27 -05:00
|
|
|
i.register_op("op_exit", s.stateful_json_op(op_exit));
|
|
|
|
i.register_op("op_env", s.stateful_json_op(op_env));
|
|
|
|
i.register_op("op_exec_path", s.stateful_json_op(op_exec_path));
|
|
|
|
i.register_op("op_set_env", s.stateful_json_op(op_set_env));
|
|
|
|
i.register_op("op_get_env", s.stateful_json_op(op_get_env));
|
2020-06-09 08:58:30 -04:00
|
|
|
i.register_op("op_delete_env", s.stateful_json_op(op_delete_env));
|
2020-02-25 09:14:27 -05:00
|
|
|
i.register_op("op_get_dir", s.stateful_json_op(op_get_dir));
|
|
|
|
i.register_op("op_hostname", s.stateful_json_op(op_hostname));
|
|
|
|
i.register_op("op_loadavg", s.stateful_json_op(op_loadavg));
|
|
|
|
i.register_op("op_os_release", s.stateful_json_op(op_os_release));
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
|
|
|
|
2019-12-15 00:14:20 -05:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct GetDirArgs {
|
2019-12-18 09:29:00 -05:00
|
|
|
kind: std::string::String,
|
2019-12-15 00:14:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn op_get_dir(
|
2020-02-08 14:34:31 -05:00
|
|
|
state: &State,
|
2019-12-15 00:14:20 -05:00
|
|
|
args: Value,
|
2020-06-01 14:20:47 -04:00
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-02-23 14:51:29 -05:00
|
|
|
) -> Result<JsonOp, OpError> {
|
2020-04-27 10:53:56 -04:00
|
|
|
state.check_unstable("Deno.dir");
|
2019-08-14 11:03:02 -04:00
|
|
|
state.check_env()?;
|
2019-12-15 00:14:20 -05:00
|
|
|
let args: GetDirArgs = serde_json::from_value(args)?;
|
|
|
|
|
2019-12-18 09:29:00 -05:00
|
|
|
let path = match args.kind.as_str() {
|
2019-12-15 00:14:20 -05:00
|
|
|
"home" => dirs::home_dir(),
|
|
|
|
"config" => dirs::config_dir(),
|
|
|
|
"cache" => dirs::cache_dir(),
|
2019-12-21 06:30:13 -05:00
|
|
|
"executable" => dirs::executable_dir(),
|
2019-12-15 00:14:20 -05:00
|
|
|
"data" => dirs::data_dir(),
|
|
|
|
"data_local" => dirs::data_local_dir(),
|
|
|
|
"audio" => dirs::audio_dir(),
|
|
|
|
"desktop" => dirs::desktop_dir(),
|
|
|
|
"document" => dirs::document_dir(),
|
|
|
|
"download" => dirs::download_dir(),
|
|
|
|
"font" => dirs::font_dir(),
|
|
|
|
"picture" => dirs::picture_dir(),
|
|
|
|
"public" => dirs::public_dir(),
|
|
|
|
"template" => dirs::template_dir(),
|
2020-03-01 19:05:04 -05:00
|
|
|
"tmp" => Some(std::env::temp_dir()),
|
2019-12-15 00:14:20 -05:00
|
|
|
"video" => dirs::video_dir(),
|
|
|
|
_ => {
|
2020-02-23 14:51:29 -05:00
|
|
|
return Err(
|
|
|
|
Error::new(
|
|
|
|
ErrorKind::InvalidInput,
|
|
|
|
format!("Invalid dir type `{}`", args.kind.as_str()),
|
|
|
|
)
|
|
|
|
.into(),
|
|
|
|
)
|
2019-12-15 00:14:20 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if path == None {
|
2020-02-23 14:51:29 -05:00
|
|
|
Err(OpError::not_found(format!(
|
|
|
|
"Could not get user {} directory.",
|
|
|
|
args.kind.as_str()
|
2019-12-15 00:14:20 -05:00
|
|
|
)))
|
|
|
|
} else {
|
|
|
|
Ok(JsonOp::Sync(json!(path
|
|
|
|
.unwrap_or_default()
|
|
|
|
.into_os_string()
|
|
|
|
.into_string()
|
|
|
|
.unwrap_or_default())))
|
|
|
|
}
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
|
|
|
|
2019-10-11 14:41:54 -04:00
|
|
|
fn op_exec_path(
|
2020-02-08 14:34:31 -05:00
|
|
|
state: &State,
|
2019-08-23 01:30:14 -04:00
|
|
|
_args: Value,
|
2020-06-01 14:20:47 -04:00
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-02-23 14:51:29 -05:00
|
|
|
) -> Result<JsonOp, OpError> {
|
2019-08-23 01:30:14 -04:00
|
|
|
let current_exe = env::current_exe().unwrap();
|
2020-05-29 11:27:43 -04:00
|
|
|
state.check_read_blind(¤t_exe, "exec_path")?;
|
2019-08-23 01:30:14 -04:00
|
|
|
// Now apply URL parser to current exe to get fully resolved path, otherwise
|
|
|
|
// we might get `./` and `../` bits in `exec_path`
|
2019-08-14 11:03:02 -04:00
|
|
|
let exe_url = Url::from_file_path(current_exe).unwrap();
|
2019-08-23 01:30:14 -04:00
|
|
|
let path = exe_url.to_file_path().unwrap();
|
|
|
|
Ok(JsonOp::Sync(json!(path)))
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
|
|
|
|
2019-08-26 08:50:21 -04:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct SetEnv {
|
|
|
|
key: String,
|
|
|
|
value: String,
|
|
|
|
}
|
|
|
|
|
2019-10-11 14:41:54 -04:00
|
|
|
fn op_set_env(
|
2020-02-08 14:34:31 -05:00
|
|
|
state: &State,
|
2019-08-26 08:50:21 -04:00
|
|
|
args: Value,
|
2020-06-01 14:20:47 -04:00
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-02-23 14:51:29 -05:00
|
|
|
) -> Result<JsonOp, OpError> {
|
2019-08-26 08:50:21 -04:00
|
|
|
let args: SetEnv = serde_json::from_value(args)?;
|
2019-08-14 11:03:02 -04:00
|
|
|
state.check_env()?;
|
2019-08-26 08:50:21 -04:00
|
|
|
env::set_var(args.key, args.value);
|
|
|
|
Ok(JsonOp::Sync(json!({})))
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
|
|
|
|
2019-10-11 14:41:54 -04:00
|
|
|
fn op_env(
|
2020-02-08 14:34:31 -05:00
|
|
|
state: &State,
|
2019-08-23 01:30:14 -04:00
|
|
|
_args: Value,
|
2020-06-01 14:20:47 -04:00
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-02-23 14:51:29 -05:00
|
|
|
) -> Result<JsonOp, OpError> {
|
2019-08-14 11:03:02 -04:00
|
|
|
state.check_env()?;
|
2019-08-23 01:30:14 -04:00
|
|
|
let v = env::vars().collect::<HashMap<String, String>>();
|
|
|
|
Ok(JsonOp::Sync(json!(v)))
|
|
|
|
}
|
2019-08-14 11:03:02 -04:00
|
|
|
|
2019-10-02 11:55:28 -04:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct GetEnv {
|
|
|
|
key: String,
|
|
|
|
}
|
|
|
|
|
2019-10-11 14:41:54 -04:00
|
|
|
fn op_get_env(
|
2020-02-08 14:34:31 -05:00
|
|
|
state: &State,
|
2019-10-02 11:55:28 -04:00
|
|
|
args: Value,
|
2020-06-01 14:20:47 -04:00
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-02-23 14:51:29 -05:00
|
|
|
) -> Result<JsonOp, OpError> {
|
2019-10-02 11:55:28 -04:00
|
|
|
let args: GetEnv = serde_json::from_value(args)?;
|
|
|
|
state.check_env()?;
|
|
|
|
let r = match env::var(args.key) {
|
|
|
|
Err(env::VarError::NotPresent) => json!([]),
|
|
|
|
v => json!([v?]),
|
|
|
|
};
|
|
|
|
Ok(JsonOp::Sync(r))
|
|
|
|
}
|
|
|
|
|
2020-06-09 08:58:30 -04:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct DeleteEnv {
|
|
|
|
key: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn op_delete_env(
|
|
|
|
state: &State,
|
|
|
|
args: Value,
|
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
|
|
|
) -> Result<JsonOp, OpError> {
|
|
|
|
let args: DeleteEnv = serde_json::from_value(args)?;
|
|
|
|
state.check_env()?;
|
|
|
|
env::remove_var(args.key);
|
|
|
|
Ok(JsonOp::Sync(json!({})))
|
|
|
|
}
|
|
|
|
|
2019-08-23 01:30:14 -04:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct Exit {
|
|
|
|
code: i32,
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
|
|
|
|
2019-10-11 14:41:54 -04:00
|
|
|
fn op_exit(
|
2020-02-08 14:34:31 -05:00
|
|
|
_s: &State,
|
2019-08-23 01:30:14 -04:00
|
|
|
args: Value,
|
2020-06-01 14:20:47 -04:00
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-02-23 14:51:29 -05:00
|
|
|
) -> Result<JsonOp, OpError> {
|
2019-08-23 01:30:14 -04:00
|
|
|
let args: Exit = serde_json::from_value(args)?;
|
|
|
|
std::process::exit(args.code)
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
|
|
|
|
2020-02-22 18:46:52 -05:00
|
|
|
fn op_loadavg(
|
|
|
|
state: &State,
|
|
|
|
_args: Value,
|
2020-06-01 14:20:47 -04:00
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-02-23 14:51:29 -05:00
|
|
|
) -> Result<JsonOp, OpError> {
|
2020-04-27 18:00:19 -04:00
|
|
|
state.check_unstable("Deno.loadavg");
|
2020-02-22 18:46:52 -05:00
|
|
|
state.check_env()?;
|
|
|
|
match sys_info::loadavg() {
|
|
|
|
Ok(loadavg) => Ok(JsonOp::Sync(json!([
|
|
|
|
loadavg.one,
|
|
|
|
loadavg.five,
|
|
|
|
loadavg.fifteen
|
|
|
|
]))),
|
|
|
|
Err(_) => Ok(JsonOp::Sync(json!([0f64, 0f64, 0f64]))),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-11 14:41:54 -04:00
|
|
|
fn op_hostname(
|
2020-02-08 14:34:31 -05:00
|
|
|
state: &State,
|
2019-09-27 19:09:42 -04:00
|
|
|
_args: Value,
|
2020-06-01 14:20:47 -04:00
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-02-23 14:51:29 -05:00
|
|
|
) -> Result<JsonOp, OpError> {
|
2020-05-09 12:44:35 -04:00
|
|
|
state.check_unstable("Deno.hostname");
|
2019-09-27 19:09:42 -04:00
|
|
|
state.check_env()?;
|
2020-02-24 08:35:45 -05:00
|
|
|
let hostname = sys_info::hostname().unwrap_or_else(|_| "".to_string());
|
2019-09-27 19:09:42 -04:00
|
|
|
Ok(JsonOp::Sync(json!(hostname)))
|
|
|
|
}
|
2020-02-24 08:35:45 -05:00
|
|
|
|
|
|
|
fn op_os_release(
|
|
|
|
state: &State,
|
|
|
|
_args: Value,
|
2020-06-01 14:20:47 -04:00
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-02-24 08:35:45 -05:00
|
|
|
) -> Result<JsonOp, OpError> {
|
2020-04-27 18:00:19 -04:00
|
|
|
state.check_unstable("Deno.osRelease");
|
2020-02-24 08:35:45 -05:00
|
|
|
state.check_env()?;
|
|
|
|
let release = sys_info::os_release().unwrap_or_else(|_| "".to_string());
|
|
|
|
Ok(JsonOp::Sync(json!(release)))
|
|
|
|
}
|