client-core 0.1.0

Duck Client 核心库
Documentation
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
// client-core/src/patch_executor/mod.rs
//! 增量升级补丁执行器模块
//!
//! 本模块负责处理增量升级的核心逻辑,包括:
//! - 文件操作执行器:安全的文件替换、删除和回滚
//! - 补丁包处理器:下载、验证和解压补丁包
//! - 主补丁执行器:协调整个补丁应用流程

pub mod error;
pub mod file_operations;
pub mod patch_processor;

// 重新导出主要接口
pub use error::PatchExecutorError;
pub use file_operations::FileOperationExecutor;
pub use patch_processor::PatchProcessor;

use crate::api_types::{PatchOperations, PatchPackageInfo};
use std::path::{Path, PathBuf};
use tracing::{debug, error, info, warn};

/// 主补丁执行器
///
/// 负责协调整个补丁应用流程,包括下载、验证、解压和应用补丁
pub struct PatchExecutor {
    /// 工作目录
    work_dir: PathBuf,
    /// 文件操作执行器
    file_executor: FileOperationExecutor,
    /// 补丁处理器
    patch_processor: PatchProcessor,
    /// 是否启用了备份
    backup_enabled: bool,
}

impl PatchExecutor {
    /// 创建新的补丁执行器
    pub fn new(work_dir: PathBuf) -> Result<Self, PatchExecutorError> {
        let file_executor = FileOperationExecutor::new(work_dir.clone())?;
        let patch_processor = PatchProcessor::new()?;

        Ok(Self {
            work_dir,
            file_executor,
            patch_processor,
            backup_enabled: false,
        })
    }

    /// 启用备份模式(支持回滚)
    pub fn enable_backup(&mut self) -> Result<(), PatchExecutorError> {
        self.file_executor.enable_backup()?;
        self.backup_enabled = true;
        info!("Patch execution backup mode enabled");
        Ok(())
    }

    /// 应用补丁包
    ///
    /// # 参数
    /// * `patch_info` - 补丁包信息
    /// * `operations` - 补丁操作定义
    /// * `progress_callback` - 进度回调函数
    pub async fn apply_patch<F>(
        &mut self,
        patch_info: &PatchPackageInfo,
        operations: &PatchOperations,
        progress_callback: F,
    ) -> Result<(), PatchExecutorError>
    where
        F: Fn(f64) + Send + Sync,
    {
        info!("Starting to apply incremental patch...");
        progress_callback(0.0);

        // 验证前置条件
        self.validate_preconditions(operations)?;
        progress_callback(0.05);

        // 执行补丁应用流程
        match self
            .execute_patch_pipeline(patch_info, operations, &progress_callback)
            .await
        {
            Ok(_) => {
                progress_callback(1.0);
                info!("Incremental patch applied successfully");
                Ok(())
            }
            Err(e) => {
                error!("Patch application failed: {}", e);

                // 根据错误类型决定是否回滚
                if e.requires_rollback() && self.backup_enabled {
                    warn!("Starting automatic rollback...");
                    if let Err(rollback_err) = self.rollback().await {
                        error!("Rollback failed: {}", rollback_err);
                        return Err(PatchExecutorError::rollback_failed(format!(
                            "Original error: {e}, rollback error: {rollback_err}"
                        )));
                    }
                    info!("Automatic rollback completed");
                }

                Err(e)
            }
        }
    }

    /// 验证前置条件
    fn validate_preconditions(
        &self,
        operations: &PatchOperations,
    ) -> Result<(), PatchExecutorError> {
        debug!("Validating patch application preconditions");

        // 验证工作目录存在且可写
        if !self.work_dir.exists() {
            return Err(PatchExecutorError::path_error(format!(
                "Working directory does not exist: {:?}",
                self.work_dir
            )));
        }

        // 验证操作不为空
        let total_operations = operations.total_operations();

        if total_operations == 0 {
            return Err(PatchExecutorError::custom("Patch operations are empty"));
        }

        debug!("Preconditions validated, total {} operations", total_operations);
        Ok(())
    }

    /// 执行补丁应用管道
    async fn execute_patch_pipeline<F>(
        &mut self,
        patch_info: &PatchPackageInfo,
        operations: &PatchOperations,
        progress_callback: &F,
    ) -> Result<(), PatchExecutorError>
    where
        F: Fn(f64) + Send + Sync,
    {
        // 1. 下载并验证补丁包
        info!("Downloading patch package...");
        let patch_path = self.patch_processor.download_patch(patch_info).await?;
        progress_callback(0.25);

        // 2. 验证补丁完整性和签名
        info!("Verifying patch integrity...");
        self.patch_processor
            .verify_patch_integrity(&patch_path, patch_info)
            .await?;
        progress_callback(0.35);

        // 3. 解压补丁包
        info!("Extracting patch package...");
        let extracted_path = self.patch_processor.extract_patch(&patch_path).await?;
        progress_callback(0.45);

        // 4. 验证解压后的文件结构
        info!("Verifying patch file structure...");
        self.validate_patch_structure(&extracted_path, operations)
            .await?;
        progress_callback(0.5);

        // 5. 应用补丁操作
        info!("Applying patch operations...");
        self.apply_patch_operations(&extracted_path, operations, progress_callback)
            .await?;

        Ok(())
    }

    /// 验证补丁文件结构
    async fn validate_patch_structure(
        &self,
        extracted_path: &Path,
        operations: &PatchOperations,
    ) -> Result<(), PatchExecutorError> {
        // 收集所有需要的文件
        let mut required_files = Vec::new();

        // 添加需要替换的文件
        if let Some(replace) = &operations.replace {
            for file in &replace.files {
                required_files.push(file.clone());
            }
            // 添加需要替换的目录(检查目录是否存在)
            for dir in &replace.directories {
                let dir_path = extracted_path.join(dir);
                if !dir_path.exists() || !dir_path.is_dir() {
                    return Err(PatchExecutorError::verification_failed(format!(
                        "Required directory missing in patch: {dir}"
                    )));
                }
            }
        }

        // 验证文件结构
        self.patch_processor
            .validate_extracted_structure(&required_files)
            .await?;

        debug!("Patch file structure verified");
        Ok(())
    }

    /// 应用补丁操作
    async fn apply_patch_operations<F>(
        &mut self,
        extracted_path: &Path,
        operations: &PatchOperations,
        progress_callback: &F,
    ) -> Result<(), PatchExecutorError>
    where
        F: Fn(f64) + Send + Sync,
    {
        // 设置补丁源目录
        self.file_executor.set_patch_source(extracted_path)?;

        // 计算总操作数用于进度计算
        let total_operations = operations.total_operations();

        let mut completed_operations = 0;

        let base_progress = 0.5; // 前面的步骤已经完成50%
        let operations_progress_range = 0.5; // 操作占50%进度

        // 执行文件替换
        if let Some(replace) = &operations.replace {
            // 如果有文件需要替换
            if !replace.files.is_empty() {
                info!("Replacing {} files", &replace.files.len());
                self.file_executor.replace_files(&replace.files).await?;
                completed_operations += replace.files.len();
                let progress = base_progress
                    + (completed_operations as f64 / total_operations as f64)
                        * operations_progress_range;
                progress_callback(progress);
            }

            // 执行目录替换
            if !replace.directories.is_empty() {
                info!("Replacing {} directories", &replace.directories.len());
                self.file_executor
                    .replace_directories(&replace.directories)
                    .await?;
                completed_operations += replace.directories.len();
                let progress = base_progress
                    + (completed_operations as f64 / total_operations as f64)
                        * operations_progress_range;
                progress_callback(progress);
            }
        }

        // 执行删除操作
        if let Some(delete) = &operations.delete {
            // 如果有文件需要删除
            if !delete.files.is_empty() {
                info!("Deleting {} items", &delete.files.len());
                self.file_executor.delete_items(&delete.files).await?;
                completed_operations += &delete.files.len();
                let progress = base_progress
                    + (completed_operations as f64 / total_operations as f64)
                        * operations_progress_range;
                progress_callback(progress);
            }
            // 如果有目录需要删除
            if !delete.directories.is_empty() {
                info!("Deleting {} directories", &delete.directories.len());
                self.file_executor.delete_items(&delete.directories).await?;
                completed_operations += &delete.directories.len();
                let progress = base_progress
                    + (completed_operations as f64 / total_operations as f64)
                        * operations_progress_range;
                progress_callback(progress);
            }
        }

        info!("Patch operations applied");
        Ok(())
    }

    /// 回滚补丁操作
    pub async fn rollback(&mut self) -> Result<(), PatchExecutorError> {
        if !self.backup_enabled {
            return Err(PatchExecutorError::BackupNotEnabled);
        }

        warn!("Starting rollback of patch operations...");
        self.file_executor.rollback().await?;
        info!("Patch rollback completed");
        Ok(())
    }

    /// 获取工作目录
    pub fn work_dir(&self) -> &Path {
        &self.work_dir
    }

    /// 检查是否启用了备份
    pub fn is_backup_enabled(&self) -> bool {
        self.backup_enabled
    }

    /// 获取操作摘要
    pub fn get_operation_summary(&self, operations: &PatchOperations) -> String {
        let mut replace_file_count = 0;
        let mut replace_dir_count = 0;
        let mut delete_file_count = 0;
        let mut delete_dir_count = 0;
        if let Some(replace) = &operations.replace {
            replace_file_count = replace.files.len();
            replace_dir_count = replace.directories.len();
        }
        if let Some(delete) = &operations.delete {
            delete_file_count = delete.files.len();
            delete_dir_count = delete.directories.len();
        }
        let total = operations.total_operations();
        format!(
            "Patch operation summary: {} total operations (file replacements: {}, directory replacements: {}, file deletions: {}, directory deletions: {})",
            total, replace_file_count, replace_dir_count, delete_file_count, delete_dir_count
        )
    }

    /// 获取补丁处理器的临时目录(用于调试)
    pub fn temp_dir(&self) -> &Path {
        self.patch_processor.temp_dir()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::api_types::ReplaceOperations;
    use tempfile::TempDir;

    #[tokio::test]
    async fn test_patch_executor_creation() {
        let temp_dir = TempDir::new().unwrap();
        let executor = PatchExecutor::new(temp_dir.path().to_owned());
        assert!(executor.is_ok());
    }

    #[tokio::test]
    async fn test_enable_backup() {
        let temp_dir = TempDir::new().unwrap();
        let mut executor = PatchExecutor::new(temp_dir.path().to_owned()).unwrap();

        assert!(!executor.is_backup_enabled());
        let result = executor.enable_backup();
        assert!(result.is_ok());
        assert!(executor.is_backup_enabled());
    }

    #[tokio::test]
    async fn test_validate_preconditions() {
        let temp_dir = TempDir::new().unwrap();
        let executor = PatchExecutor::new(temp_dir.path().to_owned()).unwrap();

        // 测试有效的操作
        let valid_operations = PatchOperations {
            replace: Some(ReplaceOperations {
                files: vec!["test.txt".to_string()],
                directories: vec!["test_dir".to_string()],
            }),
            delete: Some(ReplaceOperations {
                files: vec!["test.txt".to_string()],
                directories: vec!["test_dir".to_string()],
            }),
        };

        let result = executor.validate_preconditions(&valid_operations);
        assert!(result.is_ok());

        // 测试空操作
        let empty_operations = PatchOperations {
            replace: Some(ReplaceOperations {
                files: vec![],
                directories: vec![],
            }),
            delete: Some(ReplaceOperations {
                files: vec![],
                directories: vec![],
            }),
        };

        let result = executor.validate_preconditions(&empty_operations);
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_operation_summary() {
        let temp_dir = TempDir::new().unwrap();
        let executor = PatchExecutor::new(temp_dir.path().to_owned()).unwrap();

        let operations = PatchOperations {
            replace: Some(ReplaceOperations {
                files: vec!["file1.txt".to_string(), "file2.txt".to_string()],
                directories: vec!["dir1".to_string()],
            }),
            delete: Some(ReplaceOperations {
                files: vec!["old_file.txt".to_string()],
                directories: vec![],
            }),
        };

        let summary = executor.get_operation_summary(&operations);
        assert!(summary.contains("4 total operations"));
        assert!(summary.contains("file replacements: 2"));
        assert!(summary.contains("directory replacements: 1"));
        assert!(summary.contains("deletions: 1"));
    }

    #[tokio::test]
    async fn test_rollback_without_backup() {
        let temp_dir = TempDir::new().unwrap();
        let mut executor = PatchExecutor::new(temp_dir.path().to_owned()).unwrap();

        // 测试未启用备份时的回滚
        let result = executor.rollback().await;
        assert!(result.is_err());
        assert!(matches!(
            result.unwrap_err(),
            PatchExecutorError::BackupNotEnabled
        ));
    }
}