From 4dc46d2ab20dbc07e74390530734720871a9a153 Mon Sep 17 00:00:00 2001 From: alex Date: Thu, 25 Dec 2025 16:31:36 +0800 Subject: [PATCH] create_or_load_uuid --- src/utils/helper.rs | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/utils/helper.rs b/src/utils/helper.rs index c337f45..33adc50 100644 --- a/src/utils/helper.rs +++ b/src/utils/helper.rs @@ -1,4 +1,5 @@ use crate::config; +use crate::utils::SDLanError; use dashmap::DashMap; use std::net::{IpAddr, SocketAddr}; use std::path::Path; @@ -53,7 +54,21 @@ use super::{gen_uuid, Mac, Result, BROADCAST_MAC, IPV6_MULTICAST_MAC, MULTICAST_ use std::fs::{File, OpenOptions}; use std::io::{Read, Write}; -pub fn create_or_load_uuid(mut idfile: &str) -> Result { +pub fn save_to_file(idfile: &str, content: &str) -> Result<()> { + if idfile.len() == 0 { + return Err(SDLanError::IOError("file is empty".to_owned())); + } + + let filepath = Path::new(idfile); + OpenOptions::new() + .create(true) + .write(true) + .open(filepath)? + .write_all(content.as_bytes())?; + Ok(()) +} + +pub fn create_or_load_uuid(mut idfile: &str, size: Option) -> Result { if idfile.len() == 0 { idfile = "./.id"; } @@ -64,7 +79,10 @@ pub fn create_or_load_uuid(mut idfile: &str) -> Result { let result = result.trim().to_string(); return Ok(result); } else { - let uuid = gen_uuid(); + let mut uuid = gen_uuid(); + if let Some(size) = size { + uuid.truncate(size as usize); + } OpenOptions::new() .create(true) .write(true)