src/api2/node/time.rs: avoid custom unsafe readlink implementations

This commit is contained in:
Dietmar Maurer 2020-05-15 06:50:07 +02:00
parent 00491c0230
commit 30f577248b

View File

@ -1,7 +1,5 @@
use std::mem::{self, MaybeUninit};
use chrono::prelude::*; use chrono::prelude::*;
use anyhow::{bail, Error}; use anyhow::{bail, format_err, Error};
use serde_json::{json, Value}; use serde_json::{json, Value};
use proxmox::api::{api, Router, Permission}; use proxmox::api::{api, Router, Permission};
@ -17,23 +15,10 @@ fn read_etc_localtime() -> Result<String, Error> {
} }
// otherwise guess from the /etc/localtime symlink // otherwise guess from the /etc/localtime symlink
let mut buf = MaybeUninit::<[u8; 64]>::uninit(); let link = std::fs::read_link("/etc/localtime").
let len = unsafe { map_err(|err| format_err!("failed to guess timezone - {}", err))?;
libc::readlink(
"/etc/localtime".as_ptr() as *const _, let link = link.to_string_lossy();
buf.as_mut_ptr() as *mut _,
mem::size_of_val(&buf),
)
};
if len <= 0 {
bail!("failed to guess timezone");
}
let len = len as usize;
let buf = unsafe {
(*buf.as_mut_ptr())[len] = 0;
buf.assume_init()
};
let link = std::str::from_utf8(&buf[..len])?;
match link.rfind("/zoneinfo/") { match link.rfind("/zoneinfo/") {
Some(pos) => Ok(link[(pos + 10)..].to_string()), Some(pos) => Ok(link[(pos + 10)..].to_string()),
None => Ok(link.to_string()), None => Ok(link.to_string()),