aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorHimbeerserverDE <himbeerserverde@gmail.com>2023-11-13 19:09:08 +0100
committerHimbeerserverDE <himbeerserverde@gmail.com>2023-11-13 19:09:08 +0100
commit9f7af642b94407ea5a4cae0bdfe4122d22b58fae (patch)
treeade32c8875dea000dbdfd8cfe7eaec25a47a117c /src
parent6af5eb82bfd23842f33b8cf46c207845f1b88fa7 (diff)
try to load system time from disk before sync is possible
Diffstat (limited to 'src')
-rw-r--r--src/main.rs21
1 files changed, 19 insertions, 2 deletions
diff --git a/src/main.rs b/src/main.rs
index 8619555..0a10d11 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,9 +1,8 @@
-use std::io;
use std::net::{self, IpAddr, SocketAddr};
-use std::num;
use std::path::Path;
use std::thread;
use std::time::{self, Duration, SystemTime};
+use std::{array, io, num};
use tokio::fs;
use tokio::signal::unix::{signal, SignalKind};
@@ -34,6 +33,8 @@ enum Error {
SystemTime(#[from] time::SystemTimeError),
#[error("integer doesn't fit: {0}")]
TryFromInt(#[from] num::TryFromIntError),
+ #[error("slice length does not equal array length: {0}")]
+ TryFromSlice(#[from] array::TryFromSliceError),
#[error("chrono parse: {0}")]
ChronoParse(#[from] chrono::ParseError),
@@ -49,6 +50,13 @@ type Result<T> = std::result::Result<T, Error>;
#[tokio::main]
async fn main() -> Result<()> {
+ println!("init");
+
+ match disk_to_sys().await {
+ Ok(_) => println!("load system time"),
+ Err(e) => eprintln!("can't load system time: {}", e),
+ }
+
let ds_config = Path::new(rsdsl_ip_config::LOCATION);
while !ds_config.exists() {
println!("wait for pppoe");
@@ -92,6 +100,15 @@ async fn sysnow_to_disk() -> Result<()> {
Ok(())
}
+async fn disk_to_sys() -> Result<()> {
+ let t = i64::from_be_bytes(fs::read("/data/ntp.last_unix").await?[..8].try_into()?);
+ let timespec = TimeSpec::new(t, 0);
+
+ nix::time::clock_settime(ClockId::CLOCK_REALTIME, timespec)?;
+
+ Ok(())
+}
+
async fn sync_time(server: &str) -> Result<()> {
let last = last_time_unix()
.await