1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
|
use std::path::{Path, PathBuf};
use tracing::warn;
use super::snapshot::{
parse_history_snapshot, parse_snapshot, snapshot_file_version, SessionHistorySnapshot,
SessionSnapshot, SNAPSHOT_VERSION,
};
fn session_path() -> PathBuf {
crate::session::data_dir().join("session.json")
}
fn session_history_path() -> PathBuf {
crate::session::data_dir().join("session-history.json")
}
// Follow symlinks manually so a write through a (possibly dangling) symlink
// lands on the target. `fs::canonicalize` requires the target to exist, which
// excludes the dangling-symlink case stow users hit on the very first save.
fn resolve_write_target(path: &Path) -> std::io::Result<PathBuf> {
let mut current = path.to_path_buf();
for _ in 0..16 {
let meta = match std::fs::symlink_metadata(¤t) {
Ok(meta) => meta,
Err(_) => return Ok(current),
};
if !meta.file_type().is_symlink() {
return Ok(current);
}
let link = std::fs::read_link(¤t)?;
current = if link.is_absolute() {
link
} else {
current
.parent()
.unwrap_or_else(|| Path::new("."))
.join(link)
};
}
Ok(current)
}
pub(super) fn save_to_path(path: &Path, snapshot: &SessionSnapshot) -> std::io::Result<()> {
save_json_to_path(path, snapshot)
}
fn save_json_to_path<T: serde::Serialize>(path: &Path, snapshot: &T) -> std::io::Result<()> {
let target = resolve_write_target(path)?;
if let Some(parent) = target.parent() {
std::fs::create_dir_all(parent)?;
}
let json = serde_json::to_string_pretty(snapshot)?;
let tmp_path = target.with_extension("json.tmp");
std::fs::write(&tmp_path, &json)?;
if let Err(err) = std::fs::rename(&tmp_path, &target) {
let _ = std::fs::remove_file(&tmp_path);
return Err(err);
}
Ok(())
}
pub(super) fn save_to_paths(
session_path: &Path,
history_path: &Path,
snapshot: &SessionSnapshot,
history: Option<&SessionHistorySnapshot>,
) -> std::io::Result<()> {
save_to_path(session_path, snapshot)?;
if let Some(history) = history {
save_json_to_path(history_path, history)?;
} else {
clear_path(history_path)?;
}
Ok(())
}
pub(super) fn clear_path(path: &Path) -> std::io::Result<()> {
match std::fs::remove_file(path) {
Ok(()) => Ok(()),
Err(err) if err.kind() == std::io::ErrorKind::NotFound => Ok(()),
Err(err) => Err(err),
}
}
pub fn save(snapshot: &SessionSnapshot, history: Option<&SessionHistorySnapshot>) {
let path = session_path();
let history_path = session_history_path();
if let Err(err) = save_to_paths(&path, &history_path, snapshot, history) {
crate::logging::session_save_failed(&path, &err.to_string());
return;
}
crate::logging::session_saved(&path, snapshot.workspaces.len());
}
pub fn clear() {
let path = session_path();
if let Err(err) = clear_path(&path) {
crate::logging::session_clear_failed(&path, &err.to_string());
return;
}
clear_history();
crate::logging::session_cleared(&path);
}
pub fn clear_history() {
let path = session_history_path();
if let Err(err) = clear_path(&path) {
crate::logging::session_clear_failed(&path, &err.to_string());
}
}
pub fn load() -> Option<SessionSnapshot> {
let path = session_path();
if !path.exists() {
return None;
}
let content = match std::fs::read_to_string(&path) {
Ok(content) => content,
Err(err) => {
warn!(err = %err, "failed to read session file");
return None;
}
};
match parse_snapshot(&content) {
Ok(snapshot) => Some(snapshot),
Err(err) => {
if let Some(version) = snapshot_file_version(&content) {
if version > SNAPSHOT_VERSION {
warn!(
file_version = version,
supported = SNAPSHOT_VERSION,
"session file is from a newer herdr version, ignoring"
);
return None;
}
}
warn!(err = %err, "failed to parse session file, ignoring");
None
}
}
}
pub fn load_history() -> Option<SessionHistorySnapshot> {
let path = session_history_path();
if !path.exists() {
return None;
}
let content = match std::fs::read_to_string(&path) {
Ok(content) => content,
Err(err) => {
warn!(err = %err, "failed to read session history file");
return None;
}
};
match parse_history_snapshot(&content) {
Ok(snapshot) => Some(snapshot),
Err(err) => {
if let Some(version) = snapshot_file_version(&content) {
if version > SNAPSHOT_VERSION {
warn!(
file_version = version,
supported = SNAPSHOT_VERSION,
"session history file is from a newer herdr version, ignoring"
);
return None;
}
}
warn!(err = %err, "failed to parse session history file, ignoring");
None
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::persist::snapshot::{
PaneHistorySnapshot, TabHistorySnapshot, WorkspaceHistorySnapshot,
};
fn temp_session_path(name: &str) -> PathBuf {
let unique = format!(
"herdr-session-tests-{}-{}-{}",
name,
std::process::id(),
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_nanos()
);
std::env::temp_dir().join(unique).join("session.json")
}
fn temp_session_paths(name: &str) -> (PathBuf, PathBuf) {
let session = temp_session_path(name);
let history = session.with_file_name("session-history.json");
(session, history)
}
fn empty_snapshot() -> SessionSnapshot {
SessionSnapshot {
version: SNAPSHOT_VERSION,
workspaces: vec![],
active: None,
selected: 0,
sidebar_width: Some(26),
sidebar_section_split: Some(0.5),
collapsed_space_keys: std::collections::HashSet::new(),
}
}
fn history_snapshot(secret: &str) -> SessionHistorySnapshot {
SessionHistorySnapshot {
version: SNAPSHOT_VERSION,
workspaces: vec![WorkspaceHistorySnapshot {
tabs: vec![TabHistorySnapshot {
panes: std::collections::HashMap::from([(
0,
PaneHistorySnapshot {
ansi: secret.to_string(),
lines: 1,
},
)]),
}],
}],
}
}
#[test]
fn save_to_paths_writes_pane_history_only_to_history_file() {
let (session_path, history_path) = temp_session_paths("split-history");
save_to_paths(
&session_path,
&history_path,
&empty_snapshot(),
Some(&history_snapshot("split-secret")),
)
.unwrap();
let session = std::fs::read_to_string(&session_path).unwrap();
let history = std::fs::read_to_string(&history_path).unwrap();
assert!(!session.contains("split-secret"));
assert!(!session.contains("history"));
assert!(history.contains("split-secret"));
}
#[test]
fn save_to_paths_removes_stale_history_when_history_is_disabled() {
let (session_path, history_path) = temp_session_paths("clear-history");
save_to_paths(
&session_path,
&history_path,
&empty_snapshot(),
Some(&history_snapshot("stale-secret")),
)
.unwrap();
save_to_paths(&session_path, &history_path, &empty_snapshot(), None).unwrap();
assert!(session_path.exists());
assert!(!history_path.exists());
}
#[test]
fn clear_path_removes_existing_session_file() {
let path = temp_session_path("clear-existing");
save_to_path(&path, &empty_snapshot()).unwrap();
clear_path(&path).unwrap();
assert!(!path.exists());
}
#[test]
fn clear_path_ignores_missing_session_file() {
let path = temp_session_path("clear-missing");
clear_path(&path).unwrap();
assert!(!path.exists());
}
#[cfg(unix)]
#[test]
fn save_to_path_preserves_existing_symlink() {
let target = temp_session_path("symlink-target");
let link = target.with_file_name("link.json");
save_to_path(&target, &empty_snapshot()).unwrap();
std::os::unix::fs::symlink(&target, &link).unwrap();
let mut snap = empty_snapshot();
snap.selected = 7;
save_to_path(&link, &snap).unwrap();
assert!(std::fs::symlink_metadata(&link)
.unwrap()
.file_type()
.is_symlink());
let parsed = parse_snapshot(&std::fs::read_to_string(&target).unwrap()).unwrap();
assert_eq!(parsed.selected, 7);
}
#[cfg(unix)]
#[test]
fn save_to_path_writes_through_dangling_symlink() {
let target = temp_session_path("dangling-target");
let link = target.with_file_name("link.json");
std::fs::create_dir_all(target.parent().unwrap()).unwrap();
std::os::unix::fs::symlink(&target, &link).unwrap();
save_to_path(&link, &empty_snapshot()).unwrap();
assert!(std::fs::symlink_metadata(&link)
.unwrap()
.file_type()
.is_symlink());
assert!(target.exists());
}
#[cfg(unix)]
#[test]
fn save_to_path_resolves_relative_symlink() {
let session = temp_session_path("relative-symlink");
let dir = session.parent().unwrap();
std::fs::create_dir_all(dir).unwrap();
let target = dir.join("real.json");
let link = dir.join("link.json");
std::os::unix::fs::symlink("real.json", &link).unwrap();
save_to_path(&link, &empty_snapshot()).unwrap();
assert!(std::fs::symlink_metadata(&link)
.unwrap()
.file_type()
.is_symlink());
assert!(target.exists());
}
}
|