-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfs_ops.rs
More file actions
253 lines (228 loc) · 7.38 KB
/
Copy pathfs_ops.rs
File metadata and controls
253 lines (228 loc) · 7.38 KB
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
use std::collections::BTreeSet;
use std::fs;
use std::path::Path;
use crate::errors::{AppError, AppResult};
use super::paths::{
autosave_timestamp, get_auto_save_root, get_current_profile_file, list_profile_dirs,
utc_timestamp, ACTIVE_MARKER_FILE,
};
fn should_ignore_entry(name: &str) -> bool {
matches!(name, ".DS_Store" | ACTIVE_MARKER_FILE)
}
pub fn read_text_stripped(path: &Path) -> String {
fs::read_to_string(path)
.map(|content| content.trim().to_string())
.unwrap_or_default()
}
pub fn remove_path(path: &Path) -> AppResult<()> {
if !path.exists() && !path.is_symlink() {
return Ok(());
}
if path.is_dir() && !path.is_symlink() {
fs::remove_dir_all(path).map_err(|error| {
AppError::new(
"FS_REMOVE_FAILED",
format!("Failed to remove directory {}: {error}", path.display()),
)
})
} else {
fs::remove_file(path).map_err(|error| {
AppError::new(
"FS_REMOVE_FAILED",
format!("Failed to remove file {}: {error}", path.display()),
)
})
}
}
pub fn copy_entry(src: &Path, dst: &Path) -> AppResult<()> {
if src.is_dir() {
replace_tree(src, dst)
} else {
remove_path(dst)?;
if let Some(parent) = dst.parent() {
fs::create_dir_all(parent).map_err(|error| {
AppError::new(
"FS_CREATE_FAILED",
format!(
"Failed to create parent directory {}: {error}",
parent.display()
),
)
})?;
}
fs::copy(src, dst).map_err(|error| {
AppError::new(
"FS_COPY_FAILED",
format!(
"Failed to copy {} -> {}: {error}",
src.display(),
dst.display()
),
)
})?;
Ok(())
}
}
pub fn replace_tree(src: &Path, dst: &Path) -> AppResult<()> {
remove_path(dst)?;
fs::create_dir_all(dst).map_err(|error| {
AppError::new(
"FS_CREATE_FAILED",
format!("Failed to create directory {}: {error}", dst.display()),
)
})?;
for entry in fs::read_dir(src).map_err(|error| {
AppError::new(
"FS_READ_FAILED",
format!("Failed to read directory {}: {error}", src.display()),
)
})? {
let entry = entry.map_err(|error| {
AppError::new(
"FS_READ_FAILED",
format!("Failed to read directory entry {}: {error}", src.display()),
)
})?;
let source_path = entry.path();
let target_path = dst.join(entry.file_name());
copy_entry(&source_path, &target_path)?;
}
Ok(())
}
pub fn overlay_directory_contents(source_dir: &Path, target_dir: &Path) -> AppResult<()> {
fs::create_dir_all(target_dir).map_err(|error| {
AppError::new(
"FS_CREATE_FAILED",
format!(
"Failed to create directory {}: {error}",
target_dir.display()
),
)
})?;
for entry in fs::read_dir(source_dir).map_err(|error| {
AppError::new(
"FS_READ_FAILED",
format!("Failed to read directory {}: {error}", source_dir.display()),
)
})? {
let entry = entry.map_err(|error| {
AppError::new(
"FS_READ_FAILED",
format!(
"Failed to read directory entry {}: {error}",
source_dir.display()
),
)
})?;
let name = entry.file_name();
let Some(name) = name.to_str() else {
continue;
};
if should_ignore_entry(name) {
continue;
}
copy_entry(&entry.path(), &target_dir.join(name))?;
}
Ok(())
}
pub fn backup_root_state_to_profile(
profile: &str,
codex_home: &Path,
backup_root: &Path,
) -> AppResult<()> {
let profile_dir = backup_root.join(profile);
if !profile_dir.is_dir() {
return Ok(());
}
let mut managed_names = BTreeSet::from(["auth.json".to_string()]);
for entry in fs::read_dir(&profile_dir).map_err(|error| {
AppError::new(
"FS_READ_FAILED",
format!(
"Failed to read directory {}: {error}",
profile_dir.display()
),
)
})? {
let entry = entry.map_err(|error| {
AppError::new(
"FS_READ_FAILED",
format!(
"Failed to read directory entry {}: {error}",
profile_dir.display()
),
)
})?;
let name = entry.file_name();
let Some(name) = name.to_str() else {
continue;
};
if should_ignore_entry(name) {
continue;
}
managed_names.insert(name.to_string());
}
for name in managed_names {
let src = codex_home.join(&name);
let dst = profile_dir.join(&name);
if src.is_dir() || src.is_file() {
copy_entry(&src, &dst)?;
} else {
remove_path(&dst)?;
}
}
Ok(())
}
pub fn autosave_auth(codex_home: &Path) -> AppResult<()> {
let auth_file = codex_home.join("auth.json");
if !auth_file.is_file() {
return Ok(());
}
let snapshot_dir = get_auto_save_root(Some(codex_home)).join(autosave_timestamp());
fs::create_dir_all(&snapshot_dir).map_err(|error| {
AppError::new(
"FS_CREATE_FAILED",
format!(
"Failed to create autosave directory {}: {error}",
snapshot_dir.display()
),
)
})?;
copy_entry(&auth_file, &snapshot_dir.join("auth.json"))
}
pub fn set_active_marker(profile: &str, backup_root: &Path) -> AppResult<()> {
for profile_dir in list_profile_dirs(backup_root) {
remove_path(&profile_dir.join(ACTIVE_MARKER_FILE))?;
}
let marker = backup_root.join(profile).join(ACTIVE_MARKER_FILE);
fs::write(&marker, format!("activated_at={}\n", utc_timestamp())).map_err(|error| {
AppError::new(
"FS_WRITE_FAILED",
format!(
"Failed to write active marker {}: {error}",
marker.display()
),
)
})?;
let current_profile_file = get_current_profile_file(backup_root.parent());
fs::write(¤t_profile_file, format!("{profile}\n")).map_err(|error| {
AppError::new(
"FS_WRITE_FAILED",
format!(
"Failed to write current profile marker {}: {error}",
current_profile_file.display()
),
)
})
}
/// Clear every active-profile marker and the `.current_profile` pointer,
/// leaving no profile flagged as current. Used when the live `~/.codex`
/// account has drifted to an account no managed profile owns: keeping a stale
/// marker would make the dashboard show a wrong "current" card, so we drop the
/// pointer entirely and let the UI surface the unmanaged-account prompt.
pub fn clear_active_markers(backup_root: &Path) -> AppResult<()> {
for profile_dir in list_profile_dirs(backup_root) {
remove_path(&profile_dir.join(ACTIVE_MARKER_FILE))?;
}
remove_path(&get_current_profile_file(backup_root.parent()))
}