MCPcopy Index your code
hub / github.com/Firstyear/opensuse-proxy-cache / insert

Method insert

arc-disk-cache/src/lib.rs:566–679  ·  view source on GitHub ↗

Add an item?

(&self, k: K, d: D, mut fh: NamedTempFile)

Source from the content-addressed store, hash-verified

564
565 // Add an item?
566 pub fn insert(&self, k: K, d: D, mut fh: NamedTempFile) {
567 let file = fh.as_file_mut();
568
569 let amt = match file.metadata().map(|m| m.len() as usize) {
570 Ok(a) => a,
571 Err(e) => {
572 error!("Unable to access metadata -> {:?}", e);
573 return;
574 }
575 };
576
577 let crc = match crc32c_len(file) {
578 Ok(v) => v,
579 Err(_) => return,
580 };
581
582 // Need to salt the file path so that we don't accidently collide.
583 let mut rng = rand::thread_rng();
584 let mut salt: [u8; 16] = [0; 16];
585 rng.fill(&mut salt);
586
587 let k_path: &Path = k.as_ref();
588 let k_osstr: &OsStr = k_path.as_ref();
589 let k_slice = k_osstr.as_encoded_bytes();
590
591 let mut hasher = Sha256::new();
592
593 hasher.update(k_slice);
594 hasher.update(salt);
595
596 let adapted_k = hasher.finalize();
597
598 trace!(adapted_k_len = %adapted_k.len());
599
600 let i: u8 = adapted_k[0];
601 let key_str = hex::encode(adapted_k);
602
603 let c_path = &self.u8_to_path[i as usize];
604
605 let path = c_path.join(&key_str);
606 let mut meta_str = key_str.clone();
607 meta_str.push_str(".meta");
608 let meta_path = c_path.join(&meta_str);
609
610 trace!(?path);
611 trace!(?meta_path);
612
613 let objmeta = CacheObjMeta {
614 key: k.clone(),
615 path: path.clone(),
616 crc,
617 userdata: d.clone(),
618 };
619
620 if meta_path.exists() {
621 warn!(
622 immediate = true,
623 "file collision detected, skipping write of {}", meta_str

Callers 10

insert_bytesMethod · 0.80
disk_cache_test_basicFunction · 0.80
write_fileFunction · 0.80
foundFunction · 0.80
trace_client_req_sizeFunction · 0.80
login_viewFunction · 0.80
oauth_viewFunction · 0.80
cache_mgrFunction · 0.80
cache_mgrFunction · 0.80

Calls 2

crc32c_lenFunction · 0.85
updateMethod · 0.80

Tested by 1

disk_cache_test_basicFunction · 0.64