SHA256
123 lines
3.5 KiB
Rust
123 lines
3.5 KiB
Rust
// <hive-up - update program fot Kira Linux.>
|
|||
|
|
// Copyright (C) <2026> <Kira Foundation>
|
||
|
|
|
||
|
|
// This program is free software: you can redistribute it and/or modify
|
||
|
|
// it under the terms of the GNU General Public License as published by
|
||
|
|
// the Free Software Foundation, either version 3 of the License, or
|
||
|
|
// (at your option) any later version.
|
||
|
|
|
||
|
|
// This program is distributed in the hope that it will be useful,
|
||
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
|
// GNU General Public License for more details.
|
||
|
|
|
||
|
|
// You should have received a copy of the GNU General Public License
|
||
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||
|
|
|
||
|
|
/*
|
||
|
|
hive-up daemon logic
|
||
|
|
*/
|
||
|
|
|
||
|
|
use std::fs;
|
||
|
|
use std::process::Command;
|
||
|
|
use std::sync::Arc;
|
||
|
|
use std::sync::atomic::{AtomicBool, Ordering};
|
||
|
|
use std::thread;
|
||
|
|
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};
|
||
|
|
use time_format;
|
||
|
|
|
||
|
|
use crate::hive_config::HiveConfig;
|
||
|
|
use crate::hive_db;
|
||
|
|
use crate::hive_system;
|
||
|
|
|
||
|
|
#[derive(Debug)]
|
||
|
|
pub enum DaemonError {
|
||
|
|
IoError(std::io::Error),
|
||
|
|
DBError(hive_db::DBError),
|
||
|
|
ExecCommandError(hive_system::ExecCommandError),
|
||
|
|
TimestampError,
|
||
|
|
}
|
||
|
|
|
||
|
|
// 2. Implement From for each wrapped error type
|
||
|
|
impl From<std::io::Error> for DaemonError {
|
||
|
|
fn from(err: std::io::Error) -> Self {
|
||
|
|
DaemonError::IoError(err)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
impl From<hive_db::DBError> for DaemonError {
|
||
|
|
fn from(err: hive_db::DBError) -> Self {
|
||
|
|
DaemonError::DBError(err)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
impl From<hive_system::ExecCommandError> for DaemonError {
|
||
|
|
fn from(err: hive_system::ExecCommandError) -> Self {
|
||
|
|
DaemonError::ExecCommandError(err)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
impl std::fmt::Display for DaemonError {
|
||
|
|
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||
|
|
match self {
|
||
|
|
DaemonError::IoError(e) => write!(f, "IO Error: {}", e),
|
||
|
|
DaemonError::DBError(e) => {
|
||
|
|
write!(f, "Database error: {}", e)
|
||
|
|
},
|
||
|
|
DaemonError::ExecCommandError(e) => {
|
||
|
|
write!(f, "Exec Command Error: {}", e)
|
||
|
|
},
|
||
|
|
DaemonError::TimestampError => write!(f, "Timestamp Error"),
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// Implement std::error::Error
|
||
|
|
impl std::error::Error for DaemonError {}
|
||
|
|
|
||
|
|
fn get_timestamp_sec() -> u64 {
|
||
|
|
SystemTime::now()
|
||
|
|
.duration_since(UNIX_EPOCH)
|
||
|
|
.expect("Time went backwards")
|
||
|
|
.as_secs()
|
||
|
|
}
|
||
|
|
|
||
|
|
pub fn run(
|
||
|
|
config: HiveConfig,
|
||
|
|
term_flag: Arc<AtomicBool>,
|
||
|
|
reload_flag: Arc<AtomicBool>,
|
||
|
|
) -> Result<(), DaemonError> {
|
||
|
|
let duration = SystemTime::now()
|
||
|
|
.duration_since(UNIX_EPOCH)
|
||
|
|
.expect("Time went backwards");
|
||
|
|
|
||
|
|
let mut bzz_db = hive_db::read_db()?;
|
||
|
|
|
||
|
|
//duration.as_secs()
|
||
|
|
let keep_going = true;
|
||
|
|
|
||
|
|
//last_check.
|
||
|
|
while keep_going {
|
||
|
|
thread::sleep(Duration::from_secs(1));
|
||
|
|
|
||
|
|
// if updates check is turned on
|
||
|
|
if config.update.updates_check_interval != 0 {
|
||
|
|
let timestamp = get_timestamp_sec();
|
||
|
|
let hours_since_update_check = timestamp
|
||
|
|
.checked_sub(bzz_db.last_update_check)
|
||
|
|
.ok_or(DaemonError::TimestampError)?
|
||
|
|
/ 3600;
|
||
|
|
|
||
|
|
if hours_since_update_check > config.update.updates_check_interval {
|
||
|
|
// perform update check
|
||
|
|
let packages_num = hive_system::check_updates()?;
|
||
|
|
// updating las tupdate timestamp in db
|
||
|
|
bzz_db.last_update_check = get_timestamp_sec();
|
||
|
|
hive_db::write_db(&bzz_db)?;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
Ok(())
|
||
|
|
}
|