create_or_load_uuid

This commit is contained in:
alex 2025-12-25 16:31:36 +08:00
parent b58b18d9c3
commit 4dc46d2ab2

View File

@ -1,4 +1,5 @@
use crate::config; use crate::config;
use crate::utils::SDLanError;
use dashmap::DashMap; use dashmap::DashMap;
use std::net::{IpAddr, SocketAddr}; use std::net::{IpAddr, SocketAddr};
use std::path::Path; 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::fs::{File, OpenOptions};
use std::io::{Read, Write}; use std::io::{Read, Write};
pub fn create_or_load_uuid(mut idfile: &str) -> Result<String> { 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<u8>) -> Result<String> {
if idfile.len() == 0 { if idfile.len() == 0 {
idfile = "./.id"; idfile = "./.id";
} }
@ -64,7 +79,10 @@ pub fn create_or_load_uuid(mut idfile: &str) -> Result<String> {
let result = result.trim().to_string(); let result = result.trim().to_string();
return Ok(result); return Ok(result);
} else { } else {
let uuid = gen_uuid(); let mut uuid = gen_uuid();
if let Some(size) = size {
uuid.truncate(size as usize);
}
OpenOptions::new() OpenOptions::new()
.create(true) .create(true)
.write(true) .write(true)