This repository has no description
0

Configure Feed

Select the types of activity you want to include in your feed.

core / knot2 / crates / knot-lfs / tests / common / mod.rs
3.0 kB 98 lines
1#![allow(dead_code)] 2 3use std::path::{Path, PathBuf}; 4use std::time::{Duration, SystemTime}; 5 6use gix_packetline::blocking_io::encode; 7use knot_lfs::{LfsOid, LfsSize}; 8use knot_types::RepoDid; 9use sha2::{Digest, Sha256}; 10 11pub const SQUID: &str = "did:plc:squid"; 12pub const PKT_DATA_MAX: usize = 65516; 13pub const PEAK_CEILING: u64 = 512 * 1024 * 1024; 14pub const GROWTH_SLACK: u64 = 64 * 1024 * 1024; 15 16pub fn repo() -> RepoDid { 17 RepoDid::new(SQUID).unwrap() 18} 19 20pub fn oid_of(bytes: &[u8]) -> LfsOid { 21 LfsOid::from_digest(Sha256::digest(bytes).into()) 22} 23 24pub fn incompressible(len: usize, seed: u64) -> Vec<u8> { 25 let mut state = seed | 1; 26 (0..len) 27 .map(|_| { 28 state ^= state << 13; 29 state ^= state >> 7; 30 state ^= state << 17; 31 (state & 0xff) as u8 32 }) 33 .collect() 34} 35 36pub fn pointer_blob(oid: &LfsOid, size: LfsSize) -> Vec<u8> { 37 format!("version https://git-lfs.github.com/spec/v1\noid sha256:{oid}\nsize {size}\n") 38 .into_bytes() 39} 40 41pub fn object_path(store_dir: &Path, oid: &LfsOid) -> PathBuf { 42 store_dir 43 .join("plc/sq/uid") 44 .join(&oid.as_str()[0..2]) 45 .join(&oid.as_str()[2..4]) 46 .join(oid.as_str()) 47} 48 49pub fn backdate(path: &Path, past: Duration) { 50 std::fs::OpenOptions::new() 51 .write(true) 52 .open(path) 53 .unwrap() 54 .set_modified(SystemTime::now() - past) 55 .unwrap(); 56} 57 58pub fn put_text(buf: &mut Vec<u8>, line: &str) { 59 encode::data_to_write(format!("{line}\n").as_bytes(), &mut *buf).unwrap(); 60} 61 62pub fn upload_script(body: &[u8]) -> (LfsOid, Vec<u8>) { 63 let oid = oid_of(body); 64 let mut script = Vec::with_capacity(body.len() + 4096); 65 put_text(&mut script, &format!("put-object {oid}")); 66 put_text(&mut script, &format!("size={}", body.len())); 67 encode::delim_to_write(&mut script).unwrap(); 68 body.chunks(PKT_DATA_MAX).for_each(|chunk| { 69 encode::data_to_write(chunk, &mut script).unwrap(); 70 }); 71 encode::flush_to_write(&mut script).unwrap(); 72 put_text(&mut script, &format!("verify-object {oid}")); 73 put_text(&mut script, &format!("size={}", body.len())); 74 encode::flush_to_write(&mut script).unwrap(); 75 put_text(&mut script, "quit"); 76 encode::flush_to_write(&mut script).unwrap(); 77 (oid, script) 78} 79 80pub fn download_script(oid: &LfsOid) -> Vec<u8> { 81 let mut script = Vec::new(); 82 put_text(&mut script, &format!("get-object {oid}")); 83 encode::flush_to_write(&mut script).unwrap(); 84 put_text(&mut script, "quit"); 85 encode::flush_to_write(&mut script).unwrap(); 86 script 87} 88 89pub fn rss_bytes() -> u64 { 90 const PAGE_BYTES: u64 = 4096; 91 let statm = std::fs::read_to_string("/proc/self/statm").expect("/proc/self/statm is readable"); 92 statm 93 .split_whitespace() 94 .nth(1) 95 .and_then(|pages| pages.parse::<u64>().ok()) 96 .map(|pages| pages * PAGE_BYTES) 97 .expect("statm lists the resident page count") 98}