-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathmulti_edit.rs
More file actions
388 lines (335 loc) · 11.3 KB
/
multi_edit.rs
File metadata and controls
388 lines (335 loc) · 11.3 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
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
//! Multi-file editing operations with file locking.
//!
//! This module provides safe multi-file editing using:
//! - Process-level mutex locks to prevent concurrent access to the same file
//! - Atomic writes (write to temp file, then rename) for data integrity
//! - Proper error handling and cleanup on failures
use crate::{BatchError, Result};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex};
use tokio::fs;
use tokio::sync::Mutex as AsyncMutex;
use tracing::{debug, info, warn};
/// Global file lock manager for multi-edit operations.
/// Prevents concurrent modifications to the same file within the process.
static FILE_LOCKS: once_cell::sync::Lazy<Mutex<HashMap<PathBuf, Arc<AsyncMutex<()>>>>> =
once_cell::sync::Lazy::new(|| Mutex::new(HashMap::new()));
/// Acquire an async lock for a specific file path.
fn get_file_lock(path: &Path) -> Arc<AsyncMutex<()>> {
let canonical = path.canonicalize().unwrap_or_else(|_| path.to_path_buf());
let mut locks = FILE_LOCKS.lock().unwrap();
locks
.entry(canonical)
.or_insert_with(|| Arc::new(AsyncMutex::new(())))
.clone()
}
/// Perform an atomic write operation: write to temp file, then rename.
/// This ensures readers never see partial content.
async fn atomic_write(path: &Path, content: &[u8]) -> std::io::Result<()> {
let parent = path.parent().ok_or_else(|| {
std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"Cannot determine parent directory",
)
})?;
// Ensure parent directory exists
if !parent.exists() {
fs::create_dir_all(parent).await?;
}
// Create temp file name in same directory for same-filesystem rename
let temp_path = parent.join(format!(
".{}.tmp.{}",
path.file_name().and_then(|n| n.to_str()).unwrap_or("file"),
std::process::id()
));
// Write content to temp file
fs::write(&temp_path, content).await?;
// Atomic rename - on Windows, we may need retries due to file locking
let mut retries = 5;
loop {
// On Windows, we may need to remove the target first
#[cfg(windows)]
if path.exists() {
let _ = fs::remove_file(path).await;
}
match fs::rename(&temp_path, path).await {
Ok(()) => break,
Err(_e) if retries > 0 => {
retries -= 1;
tokio::time::sleep(tokio::time::Duration::from_millis(50)).await;
continue;
}
Err(e) => {
let _ = fs::remove_file(&temp_path).await;
return Err(e);
}
}
}
Ok(())
}
/// A single edit operation.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EditOperation {
/// File path to edit.
pub file_path: PathBuf,
/// Old text to find.
pub old_text: String,
/// New text to replace with.
pub new_text: String,
/// Whether to replace all occurrences.
#[serde(default)]
pub replace_all: bool,
/// Optional description.
pub description: Option<String>,
}
impl EditOperation {
pub fn new(
file_path: impl Into<PathBuf>,
old_text: impl Into<String>,
new_text: impl Into<String>,
) -> Self {
Self {
file_path: file_path.into(),
old_text: old_text.into(),
new_text: new_text.into(),
replace_all: false,
description: None,
}
}
pub fn replace_all(mut self) -> Self {
self.replace_all = true;
self
}
pub fn with_description(mut self, desc: impl Into<String>) -> Self {
self.description = Some(desc.into());
self
}
}
/// Result of a single edit.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EditResult {
pub file_path: PathBuf,
pub success: bool,
pub replacements: usize,
pub error: Option<String>,
}
/// Result of multi-edit operation.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MultiEditResult {
pub total_files: usize,
pub successful: usize,
pub failed: usize,
pub total_replacements: usize,
pub results: Vec<EditResult>,
}
impl MultiEditResult {
pub fn new() -> Self {
Self {
total_files: 0,
successful: 0,
failed: 0,
total_replacements: 0,
results: Vec::new(),
}
}
pub fn add_success(&mut self, file_path: PathBuf, replacements: usize) {
self.total_files += 1;
self.successful += 1;
self.total_replacements += replacements;
self.results.push(EditResult {
file_path,
success: true,
replacements,
error: None,
});
}
pub fn add_failure(&mut self, file_path: PathBuf, error: String) {
self.total_files += 1;
self.failed += 1;
self.results.push(EditResult {
file_path,
success: false,
replacements: 0,
error: Some(error),
});
}
pub fn is_success(&self) -> bool {
self.failed == 0
}
}
impl Default for MultiEditResult {
fn default() -> Self {
Self::new()
}
}
/// Multi-file editor.
pub struct MultiEdit {
/// Dry run mode (don't actually modify files).
dry_run: bool,
/// Create backups before editing.
backup: bool,
/// Backup suffix.
backup_suffix: String,
}
impl MultiEdit {
pub fn new() -> Self {
Self {
dry_run: false,
backup: false,
backup_suffix: ".bak".to_string(),
}
}
pub fn dry_run(mut self, enabled: bool) -> Self {
self.dry_run = enabled;
self
}
pub fn with_backup(mut self, enabled: bool) -> Self {
self.backup = enabled;
self
}
pub fn backup_suffix(mut self, suffix: impl Into<String>) -> Self {
self.backup_suffix = suffix.into();
self
}
/// Execute multiple edit operations.
pub async fn execute(&self, operations: Vec<EditOperation>) -> Result<MultiEditResult> {
let mut result = MultiEditResult::new();
// Group operations by file
let mut by_file: HashMap<PathBuf, Vec<EditOperation>> = HashMap::new();
for op in operations {
by_file.entry(op.file_path.clone()).or_default().push(op);
}
for (file_path, ops) in by_file {
match self.edit_file(&file_path, ops).await {
Ok(replacements) => {
result.add_success(file_path, replacements);
}
Err(e) => {
result.add_failure(file_path, e.to_string());
}
}
}
info!(
"MultiEdit complete: {} files, {} successful, {} failed, {} replacements",
result.total_files, result.successful, result.failed, result.total_replacements
);
Ok(result)
}
/// Edit a single file with multiple operations.
/// Uses file locking and atomic writes to prevent race conditions.
async fn edit_file(&self, path: &Path, operations: Vec<EditOperation>) -> Result<usize> {
// Acquire lock for this file to prevent concurrent edits
let lock = get_file_lock(path);
let _guard = lock.lock().await;
if !path.exists() {
return Err(BatchError::FileNotFound(path.display().to_string()));
}
// Read file while holding the lock
let mut content = fs::read_to_string(path).await?;
let original = content.clone();
let mut total_replacements = 0;
for op in operations {
let (new_content, count) = if op.replace_all {
let count = content.matches(&op.old_text).count();
(content.replace(&op.old_text, &op.new_text), count)
} else if content.contains(&op.old_text) {
(content.replacen(&op.old_text, &op.new_text, 1), 1)
} else {
(content.clone(), 0)
};
if count == 0 {
warn!(
"Pattern not found in {}: {:?}",
path.display(),
op.old_text.chars().take(50).collect::<String>()
);
}
content = new_content;
total_replacements += count;
}
if content != original && !self.dry_run {
// Create backup if enabled
if self.backup {
let backup_path = path.with_extension(format!(
"{}{}",
path.extension()
.map(|e| e.to_string_lossy())
.unwrap_or_default(),
self.backup_suffix
));
fs::copy(path, &backup_path).await?;
debug!("Created backup: {}", backup_path.display());
}
// Use atomic write to ensure file is never partially written
atomic_write(path, content.as_bytes()).await?;
debug!(
"Edited {}: {} replacements",
path.display(),
total_replacements
);
}
Ok(total_replacements)
}
/// Execute a single edit operation.
pub async fn edit_single(&self, operation: EditOperation) -> Result<EditResult> {
let file_path = operation.file_path.clone();
match self.edit_file(&file_path, vec![operation]).await {
Ok(replacements) => Ok(EditResult {
file_path,
success: true,
replacements,
error: None,
}),
Err(e) => Ok(EditResult {
file_path,
success: false,
replacements: 0,
error: Some(e.to_string()),
}),
}
}
}
impl Default for MultiEdit {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
use tempfile::tempdir;
#[tokio::test]
async fn test_multi_edit() {
let dir = tempdir().unwrap();
let file1 = dir.path().join("test1.txt");
let file2 = dir.path().join("test2.txt");
fs::write(&file1, "Hello World").await.unwrap();
fs::write(&file2, "Hello World").await.unwrap();
let editor = MultiEdit::new();
let ops = vec![
EditOperation::new(&file1, "World", "Rust"),
EditOperation::new(&file2, "World", "Cortex"),
];
let result = editor.execute(ops).await.unwrap();
assert_eq!(result.successful, 2);
assert_eq!(result.total_replacements, 2);
assert_eq!(fs::read_to_string(&file1).await.unwrap(), "Hello Rust");
assert_eq!(fs::read_to_string(&file2).await.unwrap(), "Hello Cortex");
}
#[tokio::test]
async fn test_replace_all() {
let dir = tempdir().unwrap();
let file = dir.path().join("test.txt");
fs::write(&file, "foo bar foo baz foo").await.unwrap();
let editor = MultiEdit::new();
let ops = vec![EditOperation::new(&file, "foo", "qux").replace_all()];
let result = editor.execute(ops).await.unwrap();
assert_eq!(result.total_replacements, 3);
assert_eq!(
fs::read_to_string(&file).await.unwrap(),
"qux bar qux baz qux"
);
}
}