]> git.proxmox.com Git - proxmox-backup.git/blame_incremental - src/api2/node/time.rs
update to nix 0.14, use code from proxmox:tools
[proxmox-backup.git] / src / api2 / node / time.rs
... / ...
CommitLineData
1use failure::*;
2use proxmox::tools::fs::{file_read_firstline, file_set_contents};
3
4use crate::api_schema::*;
5use crate::api_schema::router::*;
6use crate::api2::types::*;
7
8use serde_json::{json, Value};
9
10use chrono::prelude::*;
11
12fn read_etc_localtime() -> Result<String, Error> {
13 // use /etc/timezone
14 if let Ok(line) = file_read_firstline("/etc/timezone") {
15 return Ok(line.trim().to_owned());
16 }
17
18 // otherwise guess from the /etc/localtime symlink
19 let mut buf: [u8; 64] = unsafe { std::mem::uninitialized() };
20 let len = unsafe {
21 libc::readlink("/etc/localtime".as_ptr() as *const _, buf.as_mut_ptr() as *mut _, buf.len())
22 };
23 if len <= 0 {
24 bail!("failed to guess timezone");
25 }
26 let len = len as usize;
27 buf[len] = 0;
28 let link = std::str::from_utf8(&buf[..len])?;
29 match link.rfind("/zoneinfo/") {
30 Some(pos) => Ok(link[(pos + 10)..].to_string()),
31 None => Ok(link.to_string()),
32 }
33}
34
35fn get_time(
36 _param: Value,
37 _info: &ApiMethod,
38 _rpcenv: &mut dyn RpcEnvironment,
39) -> Result<Value, Error> {
40
41 let datetime = Local::now();
42 let offset = datetime.offset();
43 let time = datetime.timestamp();
44 let localtime = time + (offset.fix().local_minus_utc() as i64);
45
46 Ok(json!({
47 "timezone": read_etc_localtime()?,
48 "time": time,
49 "localtime": localtime,
50 }))
51}
52
53fn set_timezone(
54 param: Value,
55 _info: &ApiMethod,
56 _rpcenv: &mut dyn RpcEnvironment,
57) -> Result<Value, Error> {
58
59 let timezone = crate::tools::required_string_param(&param, "timezone")?;
60
61 let path = std::path::PathBuf::from(format!("/usr/share/zoneinfo/{}", timezone));
62
63 if !path.exists() {
64 bail!("No such timezone.");
65 }
66
67 file_set_contents("/etc/timezone", timezone.as_bytes(), None)?;
68
69 let _ = std::fs::remove_file("/etc/localtime");
70
71 use std::os::unix::fs::symlink;
72 symlink(path, "/etc/localtime")?;
73
74 Ok(Value::Null)
75}
76
77pub fn router() -> Router {
78
79 let route = Router::new()
80 .get(
81 ApiMethod::new(
82 get_time,
83 ObjectSchema::new("Read server time and time zone settings.")
84 .required("node", NODE_SCHEMA.clone())
85 ).returns(
86 ObjectSchema::new("Returns server time and timezone.")
87 .required("timezone", StringSchema::new("Time zone"))
88 .required("time", IntegerSchema::new("Seconds since 1970-01-01 00:00:00 UTC.")
89 .minimum(1297163644))
90 .required("localtime", IntegerSchema::new("Seconds since 1970-01-01 00:00:00 UTC. (local time)")
91 .minimum(1297163644))
92 )
93 )
94 .put(
95 ApiMethod::new(
96 set_timezone,
97 ObjectSchema::new("Set time zone.")
98 .required("node", NODE_SCHEMA.clone())
99 .required("timezone", StringSchema::new(
100 "Time zone. The file '/usr/share/zoneinfo/zone.tab' contains the list of valid names."))
101 ).protected(true).reload_timezone(true)
102 );
103
104
105 route
106}