feat(structs): set wrappers to path
This commit is contained in:
parent
1df9c3fba5
commit
bc6a23b76b
@ -1,5 +1,6 @@
|
|||||||
use crate::config::config::Config;
|
use crate::config::config::Config;
|
||||||
use crate::store::{
|
use crate::store::{
|
||||||
|
structs::to_obj_path,
|
||||||
ignorer::Ignorer,
|
ignorer::Ignorer,
|
||||||
indexer::Indexer,
|
indexer::Indexer,
|
||||||
nsobject::{self, NsObject},
|
nsobject::{self, NsObject},
|
||||||
@ -43,7 +44,7 @@ pub fn exec(args: AddArgs, config: Config) {
|
|||||||
} else {
|
} else {
|
||||||
indexer.index_file(path_to_add);
|
indexer.index_file(path_to_add);
|
||||||
}
|
}
|
||||||
} else if NsObject::from_local_path(&path_to_add).exists() {
|
} else if NsObject::from_local_path(&to_obj_path(&path_to_add)).exists() {
|
||||||
indexer.index_file(path_to_add);
|
indexer.index_file(path_to_add);
|
||||||
} else {
|
} else {
|
||||||
// try globbing
|
// try globbing
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
use crate::config::config::Config;
|
use crate::config::config::Config;
|
||||||
use crate::store::{
|
use crate::store::{
|
||||||
|
structs::to_obj_path,
|
||||||
indexer::Indexer,
|
indexer::Indexer,
|
||||||
nsobject::NsObject,
|
nsobject::NsObject,
|
||||||
object::{Obj, ObjStatus, ObjType},
|
object::{Obj, ObjStatus, ObjType},
|
||||||
@ -235,8 +236,7 @@ fn compare_dir(
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let repo_relative_entry = path::to_repo_relative(&entry.path(), root_path);
|
let local_obj = Obj::from_local_path(&entry.path().into());
|
||||||
let local_obj = Obj::from_local_path(&repo_relative_entry);
|
|
||||||
|
|
||||||
if entry.path().is_dir() {
|
if entry.path().is_dir() {
|
||||||
if entry.path().ends_with(".nextsync") {
|
if entry.path().ends_with(".nextsync") {
|
||||||
@ -280,7 +280,7 @@ fn compare_dir(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Read ns objects to find deleted
|
// Read ns objects to find deleted
|
||||||
let entries = NsObject::from_local_path(&path);
|
let entries = NsObject::from_local_path(&to_obj_path(&path));
|
||||||
for entry in entries.iter() {
|
for entry in entries.iter() {
|
||||||
if entry.is_file() {
|
if entry.is_file() {
|
||||||
match local_files.get(entry.get_obj_path().to_str().unwrap()) {
|
match local_files.get(entry.get_obj_path().to_str().unwrap()) {
|
||||||
@ -324,8 +324,7 @@ fn add_childs(root_path: &PathBuf, path: &PathBuf, res: Arc<ObjStatuses>) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let repo_relative_entry = path::to_repo_relative(&entry.path(), root_path);
|
let local_obj = Obj::from_local_path(&entry.path().into());
|
||||||
let local_obj = Obj::from_local_path(&repo_relative_entry);
|
|
||||||
if entry.path().is_dir() {
|
if entry.path().is_dir() {
|
||||||
if entry.path().ends_with(".nextsync") {
|
if entry.path().ends_with(".nextsync") {
|
||||||
continue;
|
continue;
|
||||||
|
@ -3,3 +3,4 @@ pub mod indexer;
|
|||||||
pub mod nsignore;
|
pub mod nsignore;
|
||||||
pub mod nsobject;
|
pub mod nsobject;
|
||||||
pub mod object;
|
pub mod object;
|
||||||
|
pub mod structs;
|
||||||
|
@ -1,66 +1,43 @@
|
|||||||
use crate::store::object::{Obj, ObjMetadata, ObjType};
|
use crate::store::{
|
||||||
use crypto::digest::Digest;
|
object::{Obj, ObjMetadata, ObjType},
|
||||||
use crypto::sha1::Sha1;
|
structs::{NsObjPath, ObjPath},
|
||||||
|
};
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::sync::OnceLock;
|
use std::sync::OnceLock;
|
||||||
|
|
||||||
pub static REPO_ROOT: OnceLock<PathBuf> = OnceLock::new();
|
pub static REPO_ROOT: OnceLock<PathBuf> = OnceLock::new();
|
||||||
|
|
||||||
pub fn init(repo_root: &PathBuf) {
|
pub fn init(repo_root: &PathBuf) {
|
||||||
REPO_ROOT.set(repo_root.clone());
|
let _ = REPO_ROOT.set(repo_root.clone());
|
||||||
}
|
}
|
||||||
|
|
||||||
type NsObjectChilds = Vec<Box<NsObject>>;
|
pub fn get_repo_root() -> PathBuf {
|
||||||
|
match REPO_ROOT.get() {
|
||||||
struct NsObjectPath {
|
|
||||||
path: PathBuf,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<&str> for NsObjectPath {
|
|
||||||
fn from(hash: &str) -> Self {
|
|
||||||
let (dir, res) = hash.split_at(2);
|
|
||||||
|
|
||||||
let mut ns_obj_path = match REPO_ROOT.get() {
|
|
||||||
Some(path) => path.clone(),
|
Some(path) => path.clone(),
|
||||||
None => {
|
None => {
|
||||||
panic!("fatal: 'REPO_ROOT' not set, you must initialize nsobject before using it!")
|
panic!("fatal: 'REPO_ROOT' not set, you must initialize nsobject before using it!")
|
||||||
}
|
}
|
||||||
};
|
|
||||||
ns_obj_path.push(dir);
|
|
||||||
ns_obj_path.push(res);
|
|
||||||
|
|
||||||
NsObjectPath { path: ns_obj_path }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<&PathBuf> for NsObjectPath {
|
type NsObjectChilds = Vec<Box<NsObject>>;
|
||||||
fn from(obj_path: &PathBuf) -> Self {
|
|
||||||
let mut hasher = Sha1::new();
|
|
||||||
hasher.input_str(
|
|
||||||
obj_path
|
|
||||||
.to_str()
|
|
||||||
.expect("Cannot contains non UTF-8 char in path"),
|
|
||||||
);
|
|
||||||
NsObjectPath::from(hasher.result_str().as_str())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct NsObject {
|
pub struct NsObject {
|
||||||
pub obj_type: ObjType,
|
pub obj_type: ObjType,
|
||||||
obj_path: OnceLock<PathBuf>,
|
/// path of the obj in the repo
|
||||||
nsobj_path: OnceLock<PathBuf>,
|
obj_path: OnceLock<ObjPath>,
|
||||||
|
/// path of the nsobj file in the store
|
||||||
|
nsobj_path: OnceLock<NsObjPath>,
|
||||||
childs: OnceLock<NsObjectChilds>,
|
childs: OnceLock<NsObjectChilds>,
|
||||||
index: usize,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl NsObject {
|
impl NsObject {
|
||||||
pub fn from_local_path(path: &PathBuf) -> Self {
|
pub fn from_local_path(path: &ObjPath) -> Self {
|
||||||
NsObject {
|
NsObject {
|
||||||
obj_type: ObjType::Obj,
|
obj_type: ObjType::Obj,
|
||||||
obj_path: OnceLock::from(path.to_path_buf()),
|
obj_path: OnceLock::from(path.clone()),
|
||||||
nsobj_path: OnceLock::new(),
|
nsobj_path: OnceLock::new(),
|
||||||
childs: OnceLock::new(),
|
childs: OnceLock::new(),
|
||||||
index: 0,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -68,19 +45,18 @@ impl NsObject {
|
|||||||
NsObject {
|
NsObject {
|
||||||
obj_type: ObjType::Obj,
|
obj_type: ObjType::Obj,
|
||||||
obj_path: OnceLock::new(),
|
obj_path: OnceLock::new(),
|
||||||
nsobj_path: OnceLock::from(NsObjectPath::from(hash).path),
|
nsobj_path: OnceLock::from(NsObjPath::from(hash)),
|
||||||
childs: OnceLock::new(),
|
childs: OnceLock::new(),
|
||||||
index: 0,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_obj_path(&self) -> &PathBuf {
|
pub fn get_obj_path(&self) -> &ObjPath {
|
||||||
self.obj_path.get_or_init(|| todo!())
|
self.obj_path.get_or_init(|| todo!())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_nsobj_path(&self) -> &PathBuf {
|
fn get_nsobj_path(&self) -> &NsObjPath {
|
||||||
self.nsobj_path
|
self.nsobj_path
|
||||||
.get_or_init(|| NsObjectPath::from(self.get_obj_path()).path)
|
.get_or_init(|| NsObjPath::from(self.get_obj_path()))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return the existence of the nsobj in the store
|
/// Return the existence of the nsobj in the store
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
use crate::store::nsobject::NsObject;
|
use crate::store::{structs::ObjPath, nsobject::NsObject};
|
||||||
use crate::utils::path;
|
use crate::utils::path;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
@ -55,16 +55,16 @@ pub struct Obj {
|
|||||||
obj_type: ObjType,
|
obj_type: ObjType,
|
||||||
status: OnceLock<ObjStatus>,
|
status: OnceLock<ObjStatus>,
|
||||||
/// path of the object from root
|
/// path of the object from root
|
||||||
obj_path: PathBuf,
|
obj_path: ObjPath,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Obj {
|
impl Obj {
|
||||||
pub fn from_local_path(path: &PathBuf) -> Self {
|
pub fn from_local_path(path: &ObjPath) -> Self {
|
||||||
// todo set state
|
// todo set state
|
||||||
Obj {
|
Obj {
|
||||||
obj_type: ObjType::Obj,
|
obj_type: ObjType::Obj,
|
||||||
status: OnceLock::new(),
|
status: OnceLock::new(),
|
||||||
obj_path: path.to_path_buf(),
|
obj_path: path.clone(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -101,7 +101,7 @@ impl Obj {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_metadata(&self) -> Option<ObjMetadata> {
|
pub fn get_metadata(&self) -> Option<ObjMetadata> {
|
||||||
let metadata = match fs::metadata(&self.obj_path) {
|
let metadata = match fs::metadata(&*self.obj_path) {
|
||||||
Ok(m) => m,
|
Ok(m) => m,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
eprintln!(
|
eprintln!(
|
||||||
|
74
src/store/structs.rs
Normal file
74
src/store/structs.rs
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
use crate::store::nsobject::get_repo_root;
|
||||||
|
use std::path::PathBuf;
|
||||||
|
use crypto::digest::Digest;
|
||||||
|
use crypto::sha1::Sha1;
|
||||||
|
use std::ops::{Deref, DerefMut};
|
||||||
|
use crate::utils::path;
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq, Clone)]
|
||||||
|
pub struct ObjPath {
|
||||||
|
path: PathBuf
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Deref for ObjPath {
|
||||||
|
type Target = PathBuf;
|
||||||
|
fn deref(&self) -> &PathBuf { &self.path }
|
||||||
|
}
|
||||||
|
|
||||||
|
impl DerefMut for ObjPath {
|
||||||
|
fn deref_mut(&mut self) -> &mut PathBuf { &mut self.path }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn to_obj_path(path: &PathBuf) -> ObjPath {
|
||||||
|
ObjPath { path: path.clone() }
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Into<ObjPath> for &PathBuf {
|
||||||
|
fn into(self) -> ObjPath {
|
||||||
|
ObjPath { path: path::to_repo_relative(self, &get_repo_root())}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Into<ObjPath> for PathBuf {
|
||||||
|
fn into(self) -> ObjPath {
|
||||||
|
ObjPath { path: path::to_repo_relative(&self, &get_repo_root())}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq)]
|
||||||
|
pub struct NsObjPath {
|
||||||
|
path: PathBuf
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Deref for NsObjPath {
|
||||||
|
type Target = PathBuf;
|
||||||
|
fn deref(&self) -> &PathBuf { &self.path }
|
||||||
|
}
|
||||||
|
|
||||||
|
impl DerefMut for NsObjPath {
|
||||||
|
fn deref_mut(&mut self) -> &mut PathBuf { &mut self.path }
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<&str> for NsObjPath {
|
||||||
|
fn from(hash: &str) -> Self {
|
||||||
|
let (dir, res) = hash.split_at(2);
|
||||||
|
|
||||||
|
let mut ns_obj_path = get_repo_root();
|
||||||
|
ns_obj_path.push(dir);
|
||||||
|
ns_obj_path.push(res);
|
||||||
|
|
||||||
|
NsObjPath { path: ns_obj_path }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<&ObjPath> for NsObjPath {
|
||||||
|
fn from(obj_path: &ObjPath) -> Self {
|
||||||
|
let mut hasher = Sha1::new();
|
||||||
|
hasher.input_str(
|
||||||
|
obj_path
|
||||||
|
.to_str()
|
||||||
|
.expect("Cannot contains non UTF-8 char in path"),
|
||||||
|
);
|
||||||
|
NsObjPath::from(hasher.result_str().as_str())
|
||||||
|
}
|
||||||
|
}
|
@ -103,3 +103,6 @@ fn part_of_folder() -> io::Result<()> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ../folder/file add
|
// ../folder/file add
|
||||||
|
// force add ignored file
|
||||||
|
// status without ignored file
|
||||||
|
// all folder without ignored file
|
||||||
|
Loading…
Reference in New Issue
Block a user