use std::fs;
use std::path::PathBuf;
use crate::app::LayoutMode;
use crate::collect::conn::{ConnFilter, ConnSort};
use crate::collect::lan::LanSort;
use crate::collect::proc::ProcSort;
use crate::ui::graph::GraphMode;
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum Lang {
En,
Zh,
}
impl Lang {
pub fn next(self) -> Self {
match self {
Self::En => Self::Zh,
Self::Zh => Self::En,
}
}
pub fn label(self) -> &'static str {
match self {
Self::En => "en",
Self::Zh => "zh",
}
}
fn parse(s: &str) -> Option<Self> {
match s {
"en" => Some(Self::En),
"zh" => Some(Self::Zh),
_ => None,
}
}
pub fn tr(self, k: &str) -> &'static str {
match self {
Self::En => match k {
"quit" => "quit",
"menu" => "menu",
"graph" => "graph",
"layout" => "layout",
"boxes" => "boxes",
"scroll" => "scroll",
"themes" => "themes",
"settings" => "settings",
"help" => "help",
"reset" => "reset",
"graphs" => "graphs",
"about" => "about",
"close" => "close",
"hint" => " \u{2190}/\u{2192} select \u{b7} enter apply \u{b7} esc close ",
"hint_reset" => " letter executes \u{b7} \u{2191}\u{2193} select \u{b7} esc close ",
"letters_hint" => " letter cycles \u{b7} esc close",
"language" => "language",
"net" => "net graphs",
"disk" => "disk graphs",
"refresh" => "refresh",
"log" => "log",
"recording" => "recording",
"log_interval" => "log interval",
"log_retention" => "log retention",
"sweep_interval" => "sweep interval",
"never" => "never",
"sweep" => "sweep",
"tracked_only" => "log tracked only",
"global_kill" => "global kill key",
"pause" => "pause",
"search" => "search",
"paused" => "paused",
"search_hint" => " enter filter \u{b7} esc clear ",
"filter" => "filter",
"lifetime" => "alive",
"uptime" => "up",
"clear" => "clear",
"udp_on" => " udp per-process accounting: eBPF active (ltop-bpf)",
"udp_off" => " udp per-process accounting: off \u{2014} shows up once ltop-bpf runs",
"caliber_head" => " data scopes (mismatch \u{2260} bug)",
"caliber_procs" => " PROCS = payload bytes (eBPF, session); NET box = NIC bytes (headers, retransmits, kernel traffic)",
"caliber_conns" => " CONNS: TCP \u{3a3} counts since connection setup (kernel); UDP \u{3a3} is session; proxied traffic counts under the proxy",
"caliber_keys" => " space pauses sampling \u{b7} / filters procs & conns (enter applies, esc clears)",
"tombstone" => "tombstones",
"proc_groups" => "conns proc groups",
"expanded" => "expanded",
"collapsed" => "collapsed",
"on" => "on",
"off" => "off",
"press_any" => " press any key to close",
"reset_net" => "reset net totals",
"reset_disk" => "reset disk totals",
"reset_all" => "reset all totals",
"config_path" => "config file",
"boxes_close" => "toggle boxes",
"hint_close_short" => "or hide",
"set_both" => "set both",
"detail_desc" => "open item detail",
"zoom_desc" => "zoom focused box (lan / conns / procs)",
"focus_desc" => "focus procs / conns / lan (toggle)",
"order_desc" => "procs sort",
"expand_desc" => "expand all mounts",
"filter_desc" => "conns filter all/lan",
"conn_order_desc" => "conns sort",
"net_io_desc" => "iface / io mode",
"lan_order_desc" => "lan sort (ip / hostname / mac / iface)",
"track_desc" => "track item in detail",
"kill_desc" => "SIGTERM in detail",
"log_desc" => "logging on/off",
"sweep_desc" => "lan sweep on/off",
"refresh_desc" => "refresh interval",
"sel_scroll_desc" => "select / scroll lists",
"quit_menu_desc" => "quit / menu",
"confirm_kill" => " Send SIGTERM to {name} (pid {pid})?",
"confirm_restart" => {
" Restart {name} (pid {pid})? SIGTERM, then relaunch its command line."
}
"yes" => "yes",
"no" => "no",
"about_line" => " local list monitor: net / lan / disk io / conns / procs",
"keys_header" => " \u{2500}\u{2500} keys \u{2500}\u{2500}",
"exit_zoom" => "exit zoom",
"unfocus" => "unfocus",
"zoom" => "zoom",
"kill" => "kill",
"global_log" => "global record key",
"group_general" => "general",
"group_graphs" => "graphs",
"group_recording" => "recording",
"group_lan" => "lan",
"group_procs" => "conns & procs",
"hint_track_on" => "[space] [x] tracked",
"hint_track_off" => "[space] [ ] track",
"hint_copy" => "[y] copy",
"hint_close" => "[esc] close",
"hint_kill" => "[k] kill pid {p}",
"hint_start" => "[s] start/restart",
"running" => "running",
"exited" => "exited",
"restarts_cmd" => "s restarts `{cmd}`",
_ => "",
},
Self::Zh => match k {
"quit" => "\u{9000}\u{51fa}",
"menu" => "\u{83dc}\u{5355}",
"graph" => "\u{56fe}\u{8868}",
"layout" => "\u{5e03}\u{5c40}",
"boxes" => "\u{76d2}\u{5b50}",
"scroll" => "\u{6eda}\u{52a8}",
"themes" => "\u{4e3b}\u{9898}",
"settings" => "\u{8bbe}\u{7f6e}",
"help" => "\u{5e2e}\u{52a9}",
"reset" => "\u{91cd}\u{7f6e}",
"graphs" => "\u{56fe}\u{8868}\u{6837}\u{5f0f}",
"about" => "\u{5173}\u{4e8e}",
"close" => "\u{5173}\u{95ed}",
"hint" => " \u{2190}/\u{2192} \u{9009}\u{62e9} \u{b7} enter \u{786e}\u{8ba4} \u{b7} esc \u{5173}\u{95ed} ",
"hint_reset" => " \u{5b57}\u{6bcd}\u{76f4}\u{63a5}\u{6267}\u{884c} \u{b7} \u{2191}\u{2193} \u{9009}\u{62e9} \u{b7} esc \u{5173}\u{95ed} ",
"letters_hint" => " \u{5b57}\u{6bcd}\u{5faa}\u{73af} \u{b7} esc \u{5173}\u{95ed}",
"language" => "\u{8bed}\u{8a00}",
"net" => "\u{7f51}\u{7edc}\u{56fe}\u{8868}",
"disk" => "\u{786c}\u{76d8}\u{56fe}\u{8868}",
"refresh" => "\u{5237}\u{65b0}",
"log" => "\u{65e5}\u{5fd7}",
"recording" => "\u{5f55}\u{5236}",
"log_interval" => "\u{65e5}\u{5fd7}\u{95f4}\u{9694}",
"log_retention" => "\u{65e5}\u{5fd7}\u{4fdd}\u{7559}",
"sweep_interval" => "\u{626b}\u{63cf}\u{95f4}\u{9694}",
"never" => "\u{6c38}\u{4e0d}\u{5220}\u{9664}",
"sweep" => "\u{626b}\u{63cf}",
"tracked_only" => "\u{4ec5}\u{8bb0}\u{5f55}\u{8ffd}\u{8e2a}\u{9879}",
"global_kill" => "\u{5168}\u{5c40} kill \u{952e}",
"pause" => "\u{6682}\u{505c}",
"search" => "\u{641c}\u{7d22}",
"paused" => "\u{5df2}\u{6682}\u{505c}",
"search_hint" => " enter \u{8fc7}\u{6ee4} \u{b7} esc \u{6e05}\u{9664} ",
"filter" => "\u{8fc7}\u{6ee4}",
"lifetime" => "\u{5b58}\u{6d3b}",
"uptime" => "\u{8fd0}\u{884c}",
"clear" => "\u{6e05}\u{9664}",
"udp_on" => " udp \u{6bcf}\u{8fdb}\u{7a0b}\u{8ba1}\u{8d39}: eBPF \u{6d3b}\u{8dc3}(ltop-bpf)",
"udp_off" => " udp \u{6bcf}\u{8fdb}\u{7a0b}\u{8ba1}\u{8d39}: \u{672a}\u{542f}\u{7528} \u{2014} sudo \u{8fd0}\u{884c} ltop-bpf \u{540e}\u{81ea}\u{52a8}\u{51fa}\u{73b0}",
"caliber_head" => " \u{6570}\u{636e}\u{53e3}\u{5f84}(\u{5bf9}\u{4e0d}\u{4e0a}\u{4e0d}\u{662f} bug)",
"caliber_procs" => " PROCS = \u{5e94}\u{7528}\u{5c42}\u{5b57}\u{8282}(eBPF \u{4f1a}\u{8bdd}\u{7d2f}\u{8ba1});NET \u{76d2} = \u{7f51}\u{5361}\u{5b57}\u{8282}(\u{542b}\u{5305}\u{5934}/\u{91cd}\u{4f20}/\u{5185}\u{6838}\u{6d41}\u{91cf})",
"caliber_conns" => " CONNS:TCP \u{3a3} \u{4e3a}\u{5185}\u{6838}\u{81ea}\u{5efa}\u{8fde}\u{7d2f}\u{8ba1}(\u{542b}\u{542f}\u{52a8}\u{524d});UDP \u{3a3} \u{4e3a}\u{4f1a}\u{8bdd}\u{7d2f}\u{8ba1};\u{4ee3}\u{7406}\u{6d41}\u{91cf}\u{8bb0}\u{5728}\u{4ee3}\u{7406}\u{5934}\u{4e0a}",
"caliber_keys" => " space \u{6682}\u{505c}\u{91c7}\u{6837} \u{b7} / \u{641c}\u{7d22}\u{8fc7}\u{6ee4}\u{8fdb}\u{7a0b}\u{4e0e}\u{8fde}\u{63a5}(enter \u{5e94}\u{7528} esc \u{6e05}\u{9664})",
"tombstone" => "\u{5893}\u{7891}\u{884c}",
"proc_groups" => "conns \u{8fdb}\u{7a0b}\u{5206}\u{7ec4}",
"expanded" => "\u{5c55}\u{5f00}",
"collapsed" => "\u{6298}\u{53e0}",
"on" => "\u{5f00}",
"off" => "\u{5173}",
"press_any" => " \u{6309}\u{4efb}\u{610f}\u{952e}\u{5173}\u{95ed}",
"reset_net" => "\u{6e05}\u{96f6}\u{7f51}\u{7edc}\u{7d2f}\u{8ba1}",
"reset_disk" => "\u{6e05}\u{96f6}\u{786c}\u{76d8}\u{7d2f}\u{8ba1}",
"reset_all" => "\u{5168}\u{90e8}\u{6e05}\u{96f6}",
"config_path" => "\u{914d}\u{7f6e}\u{6587}\u{4ef6}",
"boxes_close" => "\u{76d2}\u{5b50}\u{5f00}\u{5173}",
"hint_close_short" => "\u{6216}\u{9690}\u{85cf}",
"set_both" => "\u{540c}\u{65f6}\u{8bbe}\u{7f6e}",
"detail_desc" => "\u{6253}\u{5f00}\u{6761}\u{76ee}\u{8be6}\u{60c5}",
"zoom_desc" => "\u{5168}\u{5c4f}\u{805a}\u{7126}\u{76d2}\u{5b50}(lan / conns / procs)",
"focus_desc" => "\u{805a}\u{7126} procs / conns / lan(再按取消)",
"order_desc" => "procs \u{6392}\u{5e8f}\u{4e94}\u{6001}",
"expand_desc" => "\u{5c55}\u{5f00}/\u{6298}\u{53e0}\u{6302}\u{8f7d}\u{70b9}",
"filter_desc" => "conns \u{8fc7}\u{6ee4} all/lan",
"conn_order_desc" => "conns \u{6392}\u{5e8f}\u{4e09}\u{6001}",
"net_io_desc" => "\u{5207}\u{7f51}\u{5361} / io \u{6a21}\u{5f0f}",
"lan_order_desc" => "lan \u{6392}\u{5e8f}(ip / \u{4e3b}\u{673a}\u{540d} / mac / \u{63a5}\u{53e3})",
"track_desc" => "\u{8be6}\u{60c5}\u{4e2d}\u{52fe}\u{9009}\u{8ffd}\u{8e2a}",
"kill_desc" => "\u{8be6}\u{60c5}\u{4e2d} SIGTERM",
"log_desc" => "\u{65e5}\u{5fd7}\u{5f55}\u{5236}\u{5f00}/\u{5173}",
"sweep_desc" => "\u{626b}\u{63cf}\u{5f00}/\u{5173}",
"refresh_desc" => "\u{5237}\u{65b0}\u{95f4}\u{9694}",
"sel_scroll_desc" => "\u{9009}\u{4e2d} / \u{6eda}\u{52a8}\u{5217}\u{8868}",
"quit_menu_desc" => "\u{9000}\u{51fa} / \u{83dc}\u{5355}",
"confirm_kill" => " 向 {name}(pid {pid})发送 SIGTERM?",
"confirm_restart" => " 重启 {name}(pid {pid})?先 SIGTERM,再按原命令行拉起。",
"yes" => "是",
"no" => "否",
"about_line" => " 本机监控:网络 / 局域网 / 磁盘 I/O / 连接 / 进程",
"keys_header" => " \u{2500}\u{2500} 键位 \u{2500}\u{2500}",
"exit_zoom" => "退出全屏",
"unfocus" => "取消聚焦",
"zoom" => "全屏",
"kill" => "终止",
"global_log" => "全局录制键",
"group_general" => "通用",
"group_graphs" => "图表",
"group_recording" => "录制",
"group_lan" => "局域网",
"group_procs" => "连接与进程",
"hint_track_on" => "[space] [x] 已跟踪",
"hint_track_off" => "[space] [ ] 跟踪",
"hint_copy" => "[y] 复制",
"hint_close" => "[esc] 关闭",
"hint_kill" => "[k] 终止 pid {p}",
"hint_start" => "[s] 启动/重启",
"running" => "运行中",
"exited" => "已退出",
"restarts_cmd" => "s 重启 `{cmd}`",
_ => "",
},
}
}
}
pub struct Config {
pub lang: Lang,
pub layout: LayoutMode,
pub tick_ms: u64,
pub graph_net: GraphMode,
pub graph_disk: GraphMode,
pub boxes: [bool; 6],
pub log_interval_ms: u64,
pub log_retention_days: u32,
pub sweep_interval_s: u64,
pub sweep: bool,
pub log_tracked_only: bool,
pub allow_global_kill: bool,
pub allow_global_log: bool,
pub lan_sort: LanSort,
pub conn_filter: ConnFilter,
pub tombstone_ttl_ms: u64,
pub theme: String,
pub theme_live_follow: bool,
pub proc_expand_default: bool,
pub proc_exit_retention_min: u64,
pub net_graph_coupled: bool,
pub net_graph_scale_ratio: u32,
pub conn_sort: ConnSort,
pub proc_sort: ProcSort,
pub extra: Vec<String>,
}
impl Default for Config {
fn default() -> Self {
Self {
extra: Vec::new(),
lang: Lang::En,
layout: LayoutMode::Classic,
tick_ms: 1000,
graph_net: GraphMode::Hybrid,
graph_disk: GraphMode::Centered,
boxes: [true; 6],
log_interval_ms: 60_000,
log_retention_days: 7,
sweep_interval_s: 60,
sweep: true,
log_tracked_only: false,
allow_global_kill: false,
lan_sort: LanSort::Ip,
conn_filter: ConnFilter::All,
tombstone_ttl_ms: 5000,
theme: "omarchy".to_string(),
theme_live_follow: false,
proc_expand_default: false,
proc_exit_retention_min: 30,
net_graph_coupled: false,
net_graph_scale_ratio: 5,
conn_sort: ConnSort::Proc,
proc_sort: ProcSort::NetSum,
allow_global_log: false,
}
}
}
fn parse_layout(s: &str) -> Option<LayoutMode> {
match s {
"classic" => Some(LayoutMode::Classic),
"wide" => Some(LayoutMode::Wide),
"tall" => Some(LayoutMode::Tall),
_ => None,
}
}
fn parse_graph(s: &str) -> Option<GraphMode> {
match s {
"bar" => Some(GraphMode::Solid),
"center" => Some(GraphMode::Centered),
"line" => Some(GraphMode::Line),
"heart" => Some(GraphMode::Heartbeat),
"hybrid" => Some(GraphMode::Hybrid),
"block" => Some(GraphMode::Block),
_ => None,
}
}
impl Config {
pub fn config_dir() -> Option<PathBuf> {
let base = match std::env::var("XDG_CONFIG_HOME") {
Ok(x) if !x.is_empty() => PathBuf::from(x),
_ => PathBuf::from(std::env::var("HOME").ok()?).join(".config"),
};
Some(base.join("ltop"))
}
pub fn path() -> Option<PathBuf> {
Self::config_dir().map(|d| d.join("config.conf"))
}
pub fn migrate_legacy_dirs() {
if let Ok(home) = std::env::var("HOME") {
for (old, new) in [
(
PathBuf::from(&home).join(".config/localist-top"),
PathBuf::from(&home).join(".config/ltop"),
),
(
PathBuf::from(&home).join(".local/state/localist-top"),
PathBuf::from(&home).join(".local/state/ltop"),
),
(
PathBuf::from(&home).join(".config/lanmon"),
PathBuf::from(&home).join(".config/ltop"),
),
(
PathBuf::from(&home).join(".local/state/lanmon"),
PathBuf::from(&home).join(".local/state/ltop"),
),
] {
if old.exists() && !new.exists() {
let _ = fs::rename(&old, &new);
}
}
}
}
pub fn load() -> Self {
#[cfg(test)]
{
Self::default()
}
#[cfg(not(test))]
{
Self::load_persisted()
}
}
#[cfg(not(test))]
fn load_persisted() -> Self {
let mut cfg = Self::default();
let Some(path) = Self::path() else {
return cfg;
};
let Ok(content) = fs::read_to_string(path) else {
return cfg;
};
for line in content.lines() {
cfg.apply_line(line);
}
cfg
}
fn apply_line(&mut self, line: &str) {
let Some((k, v)) = line.split_once('=') else {
self.extra.push(line.to_string());
return;
};
let (k, v) = (k.trim(), v.trim());
match k {
"lang" => {
if let Some(x) = Lang::parse(v) {
self.lang = x;
}
}
"layout" => {
if let Some(x) = parse_layout(v) {
self.layout = x;
}
}
"tick_ms" => {
if let Ok(x) = v.parse::<u64>() {
self.tick_ms = x.clamp(250, 5000);
}
}
"graph_net" => {
if let Some(x) = parse_graph(v) {
self.graph_net = x;
}
}
"graph_disk" => {
if let Some(x) = parse_graph(v) {
self.graph_disk = x;
}
}
"boxes" => {
for (i, c) in v.chars().take(6).enumerate() {
self.boxes[i] = c == '1';
}
if !self.boxes.iter().any(|&b| b) {
self.boxes = [true; 6];
}
}
"log_interval_ms" => {
if let Ok(x) = v.parse::<u64>() {
self.log_interval_ms = x.clamp(1000, 3_600_000);
}
}
"log_retention_days" => {
if let Ok(x) = v.parse::<u32>() {
self.log_retention_days = x;
}
}
"sweep_interval_s" => {
if let Ok(x) = v.parse::<u64>() {
self.sweep_interval_s = x.clamp(5, 86_400);
}
}
"sweep" => self.sweep = v == "true",
"log_tracked_only" => self.log_tracked_only = v == "true",
"allow_global_kill" => self.allow_global_kill = v == "true",
"allow_global_log" => self.allow_global_log = v == "true",
"lan_sort" => {
self.lan_sort = match v {
"ip" => LanSort::Ip,
"host" => LanSort::Hostname,
"mac" => LanSort::Mac,
"iface" => LanSort::Iface,
_ => self.lan_sort,
}
}
"tombstone_ttl_ms" => {
if let Ok(v) = v.parse() {
self.tombstone_ttl_ms = v;
}
}
"proc_expand_default" => self.proc_expand_default = v == "true",
"proc_exit_retention_min" => {
if let Ok(v) = v.parse() {
self.proc_exit_retention_min = v;
}
}
"net_graph_coupled" => self.net_graph_coupled = v == "true",
"net_graph_scale_ratio" => {
if let Ok(v) = v.parse::<u32>() {
self.net_graph_scale_ratio = v.clamp(2, 20);
}
}
"theme" => {
if !v.is_empty() {
self.theme = v.to_string();
}
}
"theme_live_follow" => self.theme_live_follow = v == "true",
"conn_filter" => {
self.conn_filter = match v {
"all" => ConnFilter::All,
"lan" => ConnFilter::Lan,
"tcp" => ConnFilter::Tcp,
"udp" => ConnFilter::Udp,
_ => self.conn_filter,
}
}
"conn_sort" => {
self.conn_sort = match v {
"name" => ConnSort::Name,
"rate" => ConnSort::Rate,
"sum" => ConnSort::Sum,
_ => self.conn_sort,
}
}
"proc_sort" => {
self.proc_sort = match v {
"name" => ProcSort::Name,
"net-sum" => ProcSort::NetSum,
"net-rate" => ProcSort::NetRate,
"disk-sum" => ProcSort::DiskSum,
"disk-rate" => ProcSort::DiskRate,
_ => self.proc_sort,
}
}
_ => self.extra.push(line.to_string()),
}
}
pub fn content(&self) -> String {
let mut out = self.content_lines();
for line in &self.extra {
out.push_str(line);
out.push('\n');
}
out
}
fn content_lines(&self) -> String {
let boxes: String = self
.boxes
.iter()
.map(|&b| if b { '1' } else { '0' })
.collect();
format!(
"lang = {}\nlayout = {}\ntick_ms = {}\ngraph_net = {}\ngraph_disk = {}\nboxes = {}\nlog_interval_ms = {}\nlog_retention_days = {}\nsweep_interval_s = {}\nsweep = {}\nlog_tracked_only = {}\nallow_global_kill = {}\nallow_global_log = {}\nlan_sort = {}\nconn_filter = {}\nconn_sort = {}\nproc_sort = {}\ntombstone_ttl_ms = {}\ntheme = {}\ntheme_live_follow = {}\nproc_expand_default = {}\nproc_exit_retention_min = {}\nnet_graph_coupled = {}\nnet_graph_scale_ratio = {}\n",
self.lang.label(),
match self.layout {
LayoutMode::Classic => "classic",
LayoutMode::Wide => "wide",
LayoutMode::Tall => "tall",
},
self.tick_ms,
self.graph_net.label(),
self.graph_disk.label(),
boxes,
self.log_interval_ms,
self.log_retention_days,
self.sweep_interval_s,
self.sweep,
self.log_tracked_only,
self.allow_global_kill,
self.allow_global_log,
self.lan_sort.label(),
self.conn_filter.label(),
self.conn_sort.label(),
self.proc_sort.label(),
self.tombstone_ttl_ms,
self.theme,
self.theme_live_follow,
self.proc_expand_default,
self.proc_exit_retention_min,
self.net_graph_coupled,
self.net_graph_scale_ratio,
)
}
#[cfg(not(test))]
pub fn save(&self) {
if let Some(dir) = Self::config_dir() {
if fs::create_dir_all(&dir).is_ok() {
if let Some(path) = Self::path() {
let _ = Self::write_atomic(&path, &self.content());
}
}
}
}
fn write_atomic(path: &std::path::Path, content: &str) -> std::io::Result<()> {
use std::io::Write;
let tmp = path.with_extension(format!("tmp.{}", std::process::id()));
let mode = fs::metadata(path).ok().map(|m| m.permissions());
{
let mut f = fs::OpenOptions::new()
.write(true)
.create_new(true)
.open(&tmp)?;
f.write_all(content.as_bytes())?;
}
if let Some(p) = mode {
let _ = fs::set_permissions(&tmp, p);
}
match fs::rename(&tmp, path) {
Ok(()) => Ok(()),
Err(e) => {
let _ = fs::remove_file(&tmp);
Err(e)
}
}
}
#[cfg(test)]
pub fn save(&self) {}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn content_preserves_extra_lines() {
let mut cfg = Config::default();
cfg.apply_line("# hand-written note");
cfg.apply_line("custom_thing = 7");
cfg.apply_line("lang = zh");
let out = cfg.content();
assert!(out.contains("# hand-written note"), "注释行要回写");
assert!(out.contains("custom_thing = 7"), "未知键要回写");
assert!(out.contains("lang = zh"));
assert!(
out.starts_with("lang = "),
"已知键仍在固定头部,extra 只追加在尾"
);
}
#[test]
fn write_atomic_replaces_and_leaves_no_tmp() {
use std::sync::atomic::{AtomicUsize, Ordering};
static SEQ: AtomicUsize = AtomicUsize::new(0);
let n = SEQ.fetch_add(1, Ordering::SeqCst);
let dir = std::env::temp_dir().join(format!("ltop-cfg-{}-{n}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let p = dir.join("config.conf");
Config::write_atomic(&p, "first").unwrap();
assert_eq!(std::fs::read_to_string(&p).unwrap(), "first");
Config::write_atomic(&p, "second").unwrap();
assert_eq!(std::fs::read_to_string(&p).unwrap(), "second");
assert!(
!dir.join("config.conf.tmp").exists(),
"tmp 必须被 rename 吃掉"
);
let _ = std::fs::remove_dir_all(&dir);
}
}