agentmux_srv_core/backend/transcript/
origin_resign.rs1use crate::backend::storage::filestore::FileStore;
15
16use super::origin_sig::{resign_line, SIG_LEN};
17
18const CHUNK: i64 = 4 * 1024 * 1024;
21
22pub fn resign_buffer(data: &mut [u8], from_owner: &str, to_owner: &str) -> u64 {
25 let (mut line_start, mut signed) = (0usize, 0u64);
26 while line_start < data.len() {
27 let line_end = data[line_start..].iter().position(|b| *b == b'\n').map_or(data.len(), |i| line_start + i + 1);
28 if let Some((sig_at, new)) = resign_line(from_owner, to_owner, &data[line_start..line_end]) {
29 debug_assert_eq!(new.len(), SIG_LEN);
30 let at = line_start + sig_at;
31 data[at..at + SIG_LEN].copy_from_slice(new.as_bytes());
32 signed += 1;
33 }
34 line_start = line_end;
35 }
36 signed
37}
38
39pub fn resign_zone(gfs: &FileStore, zone: &str, file: &str, from_owner: &str, to_owner: &str) -> Result<u64, String> {
41 let Some(stat) = gfs.stat(zone, file).map_err(|e| format!("stat transcript: {e}"))? else { return Ok(0) };
42 let (mut pos, mut signed, mut window) = (0i64, 0u64, CHUNK);
43 while pos < stat.size {
44 let (at, data) = gfs.read_at(zone, file, pos, window).map_err(|e| format!("read transcript: {e}"))?;
45 if data.is_empty() {
46 break;
47 }
48 let end = if at + data.len() as i64 >= stat.size { data.len() } else { data.iter().rposition(|b| *b == b'\n').map_or(0, |i| i + 1) };
50 if end == 0 {
51 window = window.saturating_mul(2);
52 continue;
53 }
54 window = CHUNK;
55 let mut line_start = 0usize;
56 while line_start < end {
57 let line_end = data[line_start..end].iter().position(|b| *b == b'\n').map_or(end, |i| line_start + i + 1);
58 if let Some((sig_at, new)) = resign_line(from_owner, to_owner, &data[line_start..line_end]) {
59 debug_assert_eq!(new.len(), SIG_LEN);
60 gfs.write_at(zone, file, at + (line_start + sig_at) as i64, new.as_bytes()).map_err(|e| format!("sign a copied line: {e}"))?;
61 signed += 1;
62 }
63 line_start = line_end;
64 }
65 pos = at + end as i64;
66 }
67 Ok(signed)
68}
69
70#[cfg(test)]
71#[path = "origin_resign_tests.rs"]
72mod tests;