zipora 3.1.2

High-performance Rust implementation providing advanced data structures and compression algorithms with memory safety guarantees. Features LRU page cache, sophisticated caching layer, fiber-based concurrency, real-time compression, secure memory pools, SIMD optimizations, and complete C FFI for migration from C++.
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
//! # Process Management Utilities
//!
//! High-performance process spawning, control, and bidirectional communication.
//! Inspired by production-grade process management with async/await integration.

use std::process::{Command, Stdio};
use std::sync::Arc;
use std::collections::HashMap;
use tokio::sync::{Mutex, RwLock};
use tokio::process::{Command as AsyncCommand};
use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader as AsyncBufReader};
use crate::error::{Result, ZiporaError};

/// Process execution configuration
#[derive(Debug, Clone)]
pub struct ProcessConfig {
    /// Working directory for the process
    pub working_dir: Option<String>,
    /// Environment variables
    pub env_vars: HashMap<String, String>,
    /// Timeout in milliseconds (None = no timeout)
    pub timeout_ms: Option<u64>,
    /// Enable bidirectional communication
    pub bidirectional: bool,
    /// Buffer size for I/O operations
    pub buffer_size: usize,
}

impl Default for ProcessConfig {
    fn default() -> Self {
        Self {
            working_dir: None,
            env_vars: HashMap::new(),
            timeout_ms: Some(30_000), // 30 second default timeout
            bidirectional: false,
            buffer_size: 8192,
        }
    }
}

/// Process execution result
#[derive(Debug, Clone)]
pub struct ProcessResult {
    /// Exit code of the process
    pub exit_code: i32,
    /// Standard output
    pub stdout: String,
    /// Standard error
    pub stderr: String,
    /// Execution time in milliseconds
    pub execution_time_ms: u64,
}

/// Bidirectional pipe for process communication
pub struct BidirectionalPipe {
    process: Arc<Mutex<Option<tokio::process::Child>>>,
    stdin_writer: Arc<Mutex<Option<tokio::process::ChildStdin>>>,
    stdout_reader: Arc<Mutex<Option<AsyncBufReader<tokio::process::ChildStdout>>>>,
    stderr_reader: Arc<Mutex<Option<AsyncBufReader<tokio::process::ChildStderr>>>>,
    config: ProcessConfig,
}

impl BidirectionalPipe {
    /// Create a new bidirectional pipe for a command
    pub async fn new(command: &str, args: &[&str], config: ProcessConfig) -> Result<Self> {
        let mut cmd = AsyncCommand::new(command);
        cmd.args(args);
        cmd.stdin(Stdio::piped());
        cmd.stdout(Stdio::piped());
        cmd.stderr(Stdio::piped());

        // Set working directory if specified
        if let Some(ref dir) = config.working_dir {
            cmd.current_dir(dir);
        }

        // Set environment variables
        for (key, value) in &config.env_vars {
            cmd.env(key, value);
        }

        // Spawn the process
        let mut child = cmd.spawn()
            .map_err(|e| ZiporaError::invalid_data(&format!("Failed to spawn process: {}", e)))?;

        // Extract pipes
        let stdin = child.stdin.take()
            .ok_or_else(|| ZiporaError::invalid_data("Failed to get stdin pipe"))?;
        let stdout = child.stdout.take()
            .ok_or_else(|| ZiporaError::invalid_data("Failed to get stdout pipe"))?;
        let stderr = child.stderr.take()
            .ok_or_else(|| ZiporaError::invalid_data("Failed to get stderr pipe"))?;

        Ok(Self {
            process: Arc::new(Mutex::new(Some(child))),
            stdin_writer: Arc::new(Mutex::new(Some(stdin))),
            stdout_reader: Arc::new(Mutex::new(Some(AsyncBufReader::new(stdout)))),
            stderr_reader: Arc::new(Mutex::new(Some(AsyncBufReader::new(stderr)))),
            config,
        })
    }

    /// Write data to the process stdin
    pub async fn write_line(&self, line: &str) -> Result<()> {
        let mut stdin_guard = self.stdin_writer.lock().await;
        if let Some(ref mut stdin) = *stdin_guard {
            stdin.write_all(line.as_bytes()).await
                .map_err(|e| ZiporaError::invalid_data(&format!("Failed to write to stdin: {}", e)))?;
            stdin.write_all(b"\n").await
                .map_err(|e| ZiporaError::invalid_data(&format!("Failed to write newline: {}", e)))?;
            stdin.flush().await
                .map_err(|e| ZiporaError::invalid_data(&format!("Failed to flush stdin: {}", e)))?;
            Ok(())
        } else {
            Err(ZiporaError::invalid_data("Stdin pipe is not available"))
        }
    }

    /// Read a line from process stdout
    pub async fn read_stdout_line(&self) -> Result<Option<String>> {
        let mut stdout_guard = self.stdout_reader.lock().await;
        if let Some(ref mut stdout) = *stdout_guard {
            let mut line = String::new();
            match stdout.read_line(&mut line).await {
                Ok(0) => Ok(None), // EOF
                Ok(_) => {
                    if line.ends_with('\n') {
                        line.pop();
                        if line.ends_with('\r') {
                            line.pop();
                        }
                    }
                    Ok(Some(line))
                }
                Err(e) => Err(ZiporaError::invalid_data(&format!("Failed to read stdout: {}", e))),
            }
        } else {
            Err(ZiporaError::invalid_data("Stdout pipe is not available"))
        }
    }

    /// Read a line from process stderr
    pub async fn read_stderr_line(&self) -> Result<Option<String>> {
        let mut stderr_guard = self.stderr_reader.lock().await;
        if let Some(ref mut stderr) = *stderr_guard {
            let mut line = String::new();
            match stderr.read_line(&mut line).await {
                Ok(0) => Ok(None), // EOF
                Ok(_) => {
                    if line.ends_with('\n') {
                        line.pop();
                        if line.ends_with('\r') {
                            line.pop();
                        }
                    }
                    Ok(Some(line))
                }
                Err(e) => Err(ZiporaError::invalid_data(&format!("Failed to read stderr: {}", e))),
            }
        } else {
            Err(ZiporaError::invalid_data("Stderr pipe is not available"))
        }
    }

    /// Wait for the process to complete and get the exit status
    pub async fn wait(&self) -> Result<i32> {
        let mut process_guard = self.process.lock().await;
        if let Some(mut child) = process_guard.take() {
            let status = child.wait().await
                .map_err(|e| ZiporaError::invalid_data(&format!("Failed to wait for process: {}", e)))?;
            Ok(status.code().unwrap_or(-1))
        } else {
            Err(ZiporaError::invalid_data("Process is not available"))
        }
    }

    /// Terminate the process forcefully
    pub async fn kill(&self) -> Result<()> {
        let mut process_guard = self.process.lock().await;
        if let Some(ref mut child) = *process_guard {
            child.kill().await
                .map_err(|e| ZiporaError::invalid_data(&format!("Failed to kill process: {}", e)))?;
        }
        Ok(())
    }
}

/// Process executor for simple command execution
pub struct ProcessExecutor {
    config: ProcessConfig,
}

impl ProcessExecutor {
    /// Create a new process executor with default configuration
    pub fn new() -> Self {
        Self {
            config: ProcessConfig::default(),
        }
    }

    /// Create a process executor with custom configuration
    pub fn with_config(config: ProcessConfig) -> Self {
        Self { config }
    }

    /// Execute a command synchronously
    pub fn execute(&self, command: &str, args: &[&str]) -> Result<ProcessResult> {
        let start_time = std::time::Instant::now();

        let mut cmd = Command::new(command);
        cmd.args(args);
        cmd.stdout(Stdio::piped());
        cmd.stderr(Stdio::piped());

        // Set working directory if specified
        if let Some(ref dir) = self.config.working_dir {
            cmd.current_dir(dir);
        }

        // Set environment variables
        for (key, value) in &self.config.env_vars {
            cmd.env(key, value);
        }

        // Execute the command
        let output = cmd.output()
            .map_err(|e| ZiporaError::invalid_data(&format!("Failed to execute command: {}", e)))?;

        let execution_time_ms = start_time.elapsed().as_millis() as u64;

        Ok(ProcessResult {
            exit_code: output.status.code().unwrap_or(-1),
            stdout: String::from_utf8_lossy(&output.stdout).to_string(),
            stderr: String::from_utf8_lossy(&output.stderr).to_string(),
            execution_time_ms,
        })
    }

    /// Execute a command asynchronously
    pub async fn execute_async(&self, command: &str, args: &[&str]) -> Result<ProcessResult> {
        let start_time = std::time::Instant::now();

        let mut cmd = AsyncCommand::new(command);
        cmd.args(args);
        cmd.stdout(Stdio::piped());
        cmd.stderr(Stdio::piped());

        // Set working directory if specified
        if let Some(ref dir) = self.config.working_dir {
            cmd.current_dir(dir);
        }

        // Set environment variables
        for (key, value) in &self.config.env_vars {
            cmd.env(key, value);
        }

        // Execute the command with timeout
        let output = if let Some(timeout_ms) = self.config.timeout_ms {
            let timeout_duration = std::time::Duration::from_millis(timeout_ms);
            tokio::time::timeout(timeout_duration, cmd.output()).await
                .map_err(|_| ZiporaError::invalid_data("Process execution timed out"))?
                .map_err(|e| ZiporaError::invalid_data(&format!("Failed to execute command: {}", e)))?
        } else {
            cmd.output().await
                .map_err(|e| ZiporaError::invalid_data(&format!("Failed to execute command: {}", e)))?
        };

        let execution_time_ms = start_time.elapsed().as_millis() as u64;

        Ok(ProcessResult {
            exit_code: output.status.code().unwrap_or(-1),
            stdout: String::from_utf8_lossy(&output.stdout).to_string(),
            stderr: String::from_utf8_lossy(&output.stderr).to_string(),
            execution_time_ms,
        })
    }

    /// Execute a shell command (cross-platform)
    pub fn execute_shell(&self, command: &str) -> Result<ProcessResult> {
        #[cfg(target_os = "windows")]
        {
            self.execute("cmd", &["/C", command])
        }
        #[cfg(not(target_os = "windows"))]
        {
            self.execute("sh", &["-c", command])
        }
    }

    /// Execute a shell command asynchronously (cross-platform)
    pub async fn execute_shell_async(&self, command: &str) -> Result<ProcessResult> {
        #[cfg(target_os = "windows")]
        {
            self.execute_async("cmd", &["/C", command]).await
        }
        #[cfg(not(target_os = "windows"))]
        {
            self.execute_async("sh", &["-c", command]).await
        }
    }
}

impl Default for ProcessExecutor {
    fn default() -> Self {
        Self::new()
    }
}

/// Process pool for managing multiple concurrent processes
pub struct ProcessPool {
    max_concurrent: usize,
    active_processes: Arc<RwLock<HashMap<u64, Arc<Mutex<tokio::process::Child>>>>>,
    next_id: Arc<Mutex<u64>>,
}

impl ProcessPool {
    /// Create a new process pool with a maximum number of concurrent processes
    pub fn new(max_concurrent: usize) -> Self {
        Self {
            max_concurrent,
            active_processes: Arc::new(RwLock::new(HashMap::new())),
            next_id: Arc::new(Mutex::new(0)),
        }
    }

    /// Get the number of active processes
    pub async fn active_count(&self) -> usize {
        self.active_processes.read().await.len()
    }

    /// Check if the pool has capacity for more processes
    pub async fn has_capacity(&self) -> bool {
        self.active_count().await < self.max_concurrent
    }

    /// Spawn a new process and add it to the pool
    pub async fn spawn_process(&self, command: &str, args: &[&str], config: ProcessConfig) -> Result<u64> {
        // Check capacity
        if !self.has_capacity().await {
            return Err(ZiporaError::invalid_data("Process pool at maximum capacity"));
        }

        // Create the command
        let mut cmd = AsyncCommand::new(command);
        cmd.args(args);

        if let Some(ref dir) = config.working_dir {
            cmd.current_dir(dir);
        }

        for (key, value) in &config.env_vars {
            cmd.env(key, value);
        }

        // Spawn the process
        let child = cmd.spawn()
            .map_err(|e| ZiporaError::invalid_data(&format!("Failed to spawn process: {}", e)))?;

        // Generate unique ID
        let mut id_guard = self.next_id.lock().await;
        let process_id = *id_guard;
        *id_guard += 1;

        // Add to active processes
        let mut processes = self.active_processes.write().await;
        processes.insert(process_id, Arc::new(Mutex::new(child)));

        Ok(process_id)
    }

    /// Wait for a specific process to complete
    pub async fn wait_for_process(&self, process_id: u64) -> Result<i32> {
        let process_arc = {
            let processes = self.active_processes.read().await;
            processes.get(&process_id).cloned()
                .ok_or_else(|| ZiporaError::invalid_data("Process not found in pool"))?
        };

        let mut child = process_arc.lock().await;
        let status = child.wait().await
            .map_err(|e| ZiporaError::invalid_data(&format!("Failed to wait for process: {}", e)))?;

        // Remove from active processes
        let mut processes = self.active_processes.write().await;
        processes.remove(&process_id);

        Ok(status.code().unwrap_or(-1))
    }

    /// Kill a specific process
    pub async fn kill_process(&self, process_id: u64) -> Result<()> {
        let process_arc = {
            let processes = self.active_processes.read().await;
            processes.get(&process_id).cloned()
                .ok_or_else(|| ZiporaError::invalid_data("Process not found in pool"))?
        };

        let mut child = process_arc.lock().await;
        child.kill().await
            .map_err(|e| ZiporaError::invalid_data(&format!("Failed to kill process: {}", e)))?;

        // Remove from active processes
        let mut processes = self.active_processes.write().await;
        processes.remove(&process_id);

        Ok(())
    }

    /// Kill all active processes
    pub async fn kill_all(&self) -> Result<()> {
        let process_ids: Vec<u64> = {
            let processes = self.active_processes.read().await;
            processes.keys().cloned().collect()
        };

        for process_id in process_ids {
            let _ = self.kill_process(process_id).await; // Ignore errors for individual kills
        }

        Ok(())
    }
}

/// Process manager for high-level process management operations
pub struct ProcessManager {
    executor: ProcessExecutor,
    pool: ProcessPool,
}

impl ProcessManager {
    /// Create a new process manager
    pub fn new(max_concurrent: usize) -> Self {
        Self {
            executor: ProcessExecutor::new(),
            pool: ProcessPool::new(max_concurrent),
        }
    }

    /// Create a process manager with custom configuration
    pub fn with_config(config: ProcessConfig, max_concurrent: usize) -> Self {
        Self {
            executor: ProcessExecutor::with_config(config),
            pool: ProcessPool::new(max_concurrent),
        }
    }

    /// Get a reference to the process executor
    pub fn executor(&self) -> &ProcessExecutor {
        &self.executor
    }

    /// Get a reference to the process pool
    pub fn pool(&self) -> &ProcessPool {
        &self.pool
    }

    /// Execute a command with automatic resource management
    pub async fn execute_managed(&self, command: &str, args: &[&str]) -> Result<ProcessResult> {
        self.executor.execute_async(command, args).await
    }

    /// Create a bidirectional pipe for interactive processes
    pub async fn create_pipe(&self, command: &str, args: &[&str], config: ProcessConfig) -> Result<BidirectionalPipe> {
        BidirectionalPipe::new(command, args, config).await
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    async fn test_process_executor() {
        let executor = ProcessExecutor::new();
        
        // Test simple command execution
        let result = executor.execute_shell("echo hello").unwrap();
        assert_eq!(result.exit_code, 0);
        assert!(result.stdout.contains("hello"));
    }

    #[tokio::test]
    async fn test_process_executor_async() {
        let executor = ProcessExecutor::new();
        
        // Test async command execution
        let result = executor.execute_shell_async("echo async_hello").await.unwrap();
        assert_eq!(result.exit_code, 0);
        assert!(result.stdout.contains("async_hello"));
    }

    #[tokio::test]
    async fn test_process_pool() {
        let pool = ProcessPool::new(2);
        
        // Test capacity checking
        assert!(pool.has_capacity().await);
        assert_eq!(pool.active_count().await, 0);
    }

    #[test]
    fn test_process_config() {
        let mut config = ProcessConfig::default();
        config.env_vars.insert("TEST_VAR".to_string(), "test_value".to_string());
        config.working_dir = Some("/tmp".to_string());
        
        assert_eq!(config.env_vars.get("TEST_VAR"), Some(&"test_value".to_string()));
        assert_eq!(config.working_dir.as_ref(), Some(&"/tmp".to_string()));
    }

    #[tokio::test]
    async fn test_process_manager() {
        let manager = ProcessManager::new(5);
        
        // Test managed execution
        let result = manager.execute_managed("echo", &["manager_test"]).await.unwrap();
        assert_eq!(result.exit_code, 0);
        assert!(result.stdout.contains("manager_test"));
    }
}