This commit is contained in:
Pascal Engélibert 2026-07-27 21:35:40 +02:00
commit fbf3d7640a
8 changed files with 110 additions and 38 deletions

View file

@ -11,9 +11,10 @@ use std::{
const REPO_METADATA_FILE_NAME: &str = "meta.bin";
const VERSION: [u8; 1] = [0];
#[derive(Debug)]
#[derive(Clone, Debug)]
pub struct RepoMetadata {
pub date: u64,
pub title: String,
pub repo_url: String,
pub commit_hash: String,
pub content: Vec<u8>,
@ -75,6 +76,10 @@ impl RepoMetadata {
let now = std::time::UNIX_EPOCH.elapsed().unwrap().as_secs();
file.write_all(&now.to_be_bytes())?;
file.write_all(&(self.title.len() as u32).to_be_bytes())?;
file.write_all(self.title.as_bytes())?;
file.write_all(&(self.repo_url.len() as u32).to_be_bytes())?;
file.write_all(self.repo_url.as_bytes())?;
@ -104,6 +109,17 @@ impl RepoMetadata {
file.read_exact(&mut date)?;
let date = u64::from_be_bytes(date);
let mut title_len = [0u8; 4];
file.read_exact(&mut title_len)?;
let title_len = u32::from_be_bytes(title_len) as usize;
if title_len > MAX_URL_SIZE {
return Err(ReadRepoMetadataError::InvalidFormat);
}
let mut title = vec![0; title_len];
file.read_exact(&mut title)?;
let title = String::from_utf8(title)?;
let mut repo_url_len = [0u8; 4];
file.read_exact(&mut repo_url_len)?;
let repo_url_len = u32::from_be_bytes(repo_url_len) as usize;
@ -131,6 +147,7 @@ impl RepoMetadata {
Ok(Self {
date,
title,
repo_url,
commit_hash,
content,