create impl for blob

This commit is contained in:
grimhilt 2023-08-04 15:25:51 +02:00
parent cb43a46456
commit d5097727cb
12 changed files with 90 additions and 54 deletions

View File

@ -13,6 +13,7 @@ pub struct AddArgs<'a> {
// todo match deleted files
// todo match weird reg expression
// todo -A == .
pub fn add(args: AddArgs) {
let mut index_file = store::index::open();
let mut added_files: Vec<String> = vec![];
@ -79,6 +80,7 @@ pub fn add(args: AddArgs) {
}
fn add_folder_content(path: PathBuf, added_files: &mut Vec<String>) {
// todo check for changes
let mut folders: Vec<PathBuf> = vec![];
folders.push(path);

View File

@ -10,7 +10,7 @@ use crate::utils::remote::{enumerate_remote, EnumerateOptions};
use crate::global::global::{DIR_PATH, set_dir_path};
use crate::services::api::ApiError;
use crate::services::req_props::{ReqProps, ObjProps};
use crate::store::object::{tree, blob};
use crate::store::object::{tree, blob::Blob};
use crate::commands::config;
use crate::commands::init;
@ -101,7 +101,7 @@ fn save_blob(obj: ObjProps) {
let relative_s = &obj.clone().relative_s.unwrap();
let relative_p = PathBuf::from(&relative_s);
let lastmodified = obj.clone().lastmodified.unwrap().timestamp_millis();
if let Err(err) = blob::add(relative_p, &lastmodified.to_string(), false) {
if let Err(err) = Blob::new(relative_p).create(&lastmodified.to_string(), false) {
eprintln!("err: saving ref of {} ({})", relative_s.clone(), err);
}
}

View File

@ -3,7 +3,7 @@ use std::fs::DirBuilder;
use crate::services::downloader::Downloader;
use crate::services::req_props::ObjProps;
use crate::store::object::blob;
use crate::store::object::blob::Blob;
use crate::store::object::tree;
use crate::utils::api::get_api_props;
use crate::utils::path;
@ -49,7 +49,8 @@ fn update_blob(obj: ObjProps) {
let relative_s = &obj.clone().relative_s.unwrap();
let relative_p = PathBuf::from(&relative_s);
let lastmodified = obj.clone().lastmodified.unwrap().timestamp_millis();
if let Err(err) = blob::add(relative_p, &lastmodified.to_string(), false) {
// todo update function
if let Err(err) = Blob::new(relative_p).create(&lastmodified.to_string(), false) {
eprintln!("err: saving ref of {} ({})", relative_s.clone(), err);
}
}

View File

@ -16,7 +16,7 @@ pub fn push() {
None => {
eprintln!("fatal: no remote set in configuration");
//std::process::exit(1);
String::from("")
String::new()
}
};

View File

@ -3,7 +3,7 @@ use std::io;
use crate::services::api::ApiError;
use crate::services::delete_path::DeletePath;
use crate::store::index;
use crate::store::object::blob;
use crate::store::object::blob::Blob;
use crate::commands::status::LocalObj;
use crate::commands::push::push_factory::{PushState, PushChange, PushFlowState};
@ -42,7 +42,7 @@ impl PushChange for Deleted {
// update tree
// todo date
blob::rm(obj.path.clone())?;
Blob::new(obj.path.clone()).rm()?;
// remove index
index::rm_line(obj.path.to_str().unwrap())?;

View File

@ -4,7 +4,7 @@ use crate::services::api::ApiError;
use crate::services::req_props::ReqProps;
use crate::services::upload_file::UploadFile;
use crate::store::index;
use crate::store::object::blob;
use crate::store::object::blob::Blob;
use crate::commands::status::LocalObj;
use crate::commands::push::push_factory::{PushState, PushChange, PushFlowState};
@ -67,8 +67,8 @@ impl PushChange for New {
let lastmodified = prop.lastmodified.unwrap().timestamp_millis();
// update blob
blob::add(obj.path.clone(), &lastmodified.to_string(), true)?;
// create new blob
Blob::new(obj.path.clone()).create(&lastmodified.to_string(), true)?;
// remove index
index::rm_line(obj.path.to_str().unwrap())?;

View File

@ -16,7 +16,7 @@ impl DownloadFiles {
pub fn new() -> Self {
DownloadFiles {
api_builder: ApiBuilder::new(),
relative_ps: String::from(""),
relative_ps: String::new(),
}
}
@ -74,7 +74,7 @@ impl DownloadFiles {
if res.status().is_success() {
let body = res.bytes().await.map_err(ApiError::EmptyError)?;
match Self::write_file(p, &body.to_vec()) {
Err(_) => Err(ApiError::Unexpected(String::from(""))),
Err(_) => Err(ApiError::Unexpected(String::new())),
Ok(_) => Ok(()),
}
} else {

View File

@ -195,7 +195,7 @@ impl ReqProps {
let mut values: Vec<ObjProps> = vec![];
let mut should_get = false;
let mut val: String = String::from("");
let mut val: String = String::new();
let mut content = ObjProps::new();
for event in parser {

View File

@ -30,7 +30,7 @@ impl Object {
if path == "" {
return Object {
path: PathBuf::from("/"),
hash: String::from(""),
hash: String::new(),
obj_p: head::path(),
ts: None,
}

View File

@ -1,52 +1,85 @@
use std::io;
use std::path::PathBuf;
use std::fs;
use crypto::sha1::Sha1;
use crypto::digest::Digest;
use crate::utils::path;
use crate::store::head;
use crate::store::object::{update_dates, parse_path, add_node, create_obj, rm_node};
pub fn add(path: PathBuf, date: &str, up_parent: bool) -> io::Result<()> {
let (line, hash, name) = parse_path(path.clone(), true);
// add blob reference to parent
if path.iter().count() == 1 {
head::add_line(line)?;
} else {
add_node(path.parent().unwrap(), &line)?;
pub struct Blob {
path: PathBuf,
hash: String,
obj_p: PathBuf,
}
let mut content = name.clone().to_owned();
content.push_str(" ");
content.push_str(date);
impl Blob {
pub fn new(path: PathBuf) -> Blob {
let mut hasher = Sha1::new();
hasher.input_str(path.to_str().unwrap());
let hash = hasher.result_str();
let (dir, res) = hash.split_at(2);
let mut obj_p = path::objects();
obj_p.push(dir);
obj_p.push(res);
Blob {
path,
hash,
obj_p,
}
}
fn get_line_filename(&mut self) -> (String, String) {
let file_name = self.path.file_name().unwrap().to_str().unwrap().to_owned();
let mut line = String::from("blob");
line.push_str(" ");
line.push_str(&self.hash);
line.push_str(" ");
line.push_str(&file_name);
(line, file_name)
}
pub fn create(&mut self, ts: &str, up_parent: bool) -> io::Result<()> {
let (line, file_name) = self.get_line_filename();
// add blob reference to parent
if self.path.iter().count() == 1 {
head::add_line(line)?;
} else {
add_node(self.path.parent().unwrap(), &line)?;
}
// create blob object
create_obj(hash, &content)?;
let mut content = file_name.clone();
content.push_str(" ");
content.push_str(ts);
// todo hash ts (bis)
create_obj(self.hash.clone(), &content)?;
// update date for all parent
if up_parent {
update_dates(path, date)?;
update_dates(self.path.clone(), ts)?;
}
Ok(())
}
pub fn rm(path: PathBuf) -> io::Result<()> {
let (line, hash, _) = parse_path(path.clone(), true);
pub fn rm(&mut self) -> io::Result<()> {
let (line, _) = self.get_line_filename();
// remove blob reference to parent
if path.iter().count() == 1 {
if self.path.iter().count() == 1 {
head::rm_line(&line)?;
} else {
rm_node(path.parent().unwrap(), &line)?;
rm_node(self.path.parent().unwrap(), &line)?;
}
// remove blob object
let mut root = path::objects();
let c = hash.clone();
let (dir, rest) = c.split_at(2);
root.push(dir);
root.push(rest);
fs::remove_file(root)?;
fs::remove_file(self.obj_p.clone())?;
Ok(())
}
}

View File

@ -78,7 +78,7 @@ pub fn read(tree: String) -> Option<(String, io::Lines<io::BufReader<File>>)> {
Ok(mut reader) => {
let name = match reader.next() {
Some(Ok(line)) => line,
_ => String::from(""),
_ => String::new(),
};
Some((name, reader))
},

View File

@ -20,7 +20,7 @@ pub fn enumerate_remote(
let relative_s = match folder.relative_s {
Some(relative_s) => relative_s,
None => options.relative_s.clone().unwrap_or(String::from("")),
None => options.relative_s.clone().unwrap_or(String::new())
};
// request folder content
@ -88,6 +88,6 @@ pub fn enumerate_remote(
}
fn calc_depth(obj: &ObjProps) -> u16 {
obj.relative_s.clone().unwrap_or(String::from("")).split("/").count() as u16
obj.relative_s.clone().unwrap_or(String::new()).split("/").count() as u16
}