|
|
|
@ -16,6 +16,7 @@ static STORAGE_FOLDER: Lazy<String> = Lazy::new(|| "storage".to_string() );
|
|
|
|
|
static STORAGE_PATH: Lazy<String> = Lazy::new(|| {
|
|
|
|
|
format!("{}/db.mps", &*STORAGE_FOLDER) |
|
|
|
|
}); |
|
|
|
|
static PASSWORD_TEST_SALT: &str = "MyPasswordStorage1234567890"; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub struct Item { |
|
|
|
@ -90,6 +91,10 @@ impl Encoder {
|
|
|
|
|
Err(e) => Err(io::Error::new(io::ErrorKind::InvalidData, e)) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
pub fn get_encoded_test_passphrase(&self) -> String { |
|
|
|
|
// TODO: encode SALT const with passphrase
|
|
|
|
|
self.passphrase.clone() |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pub struct Storage { |
|
|
|
@ -98,15 +103,27 @@ pub struct Storage {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
impl Storage { |
|
|
|
|
// TODO: make path as String too
|
|
|
|
|
pub fn new(password: String) -> io::Result<Storage> { |
|
|
|
|
let encoder = Encoder::from(password); |
|
|
|
|
pub fn new(passphrase: String) -> Storage { |
|
|
|
|
Storage { |
|
|
|
|
items: HashSet::<Item>::new(), |
|
|
|
|
encoder: Encoder::from(passphrase) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
pub fn from_db(passphrase: String) -> io::Result<Storage> { |
|
|
|
|
let encoder = Encoder::from(passphrase); |
|
|
|
|
// TODO: throw error is password is incorrect
|
|
|
|
|
let file = fs::File::open(&*STORAGE_PATH)?; |
|
|
|
|
let reader = io::BufReader::new(file); |
|
|
|
|
let mut items = HashSet::<Item>::new(); |
|
|
|
|
let mut id: Option<String> = None; |
|
|
|
|
for line in reader.lines() { |
|
|
|
|
let mut lines = reader.lines(); |
|
|
|
|
// TODO: uncomment when innit saving implemented
|
|
|
|
|
let passtest = match lines.next() { |
|
|
|
|
Some(line) => line?, |
|
|
|
|
None => return Err(io::Error::new(io::ErrorKind::InvalidData, "Bad storage db format: no passphrase in the beginnning")), |
|
|
|
|
}; |
|
|
|
|
println!("pass: {}", passtest); |
|
|
|
|
for line in lines { |
|
|
|
|
match line { |
|
|
|
|
Ok(line) => { |
|
|
|
|
if id.is_none() { |
|
|
|
@ -128,14 +145,16 @@ impl Storage {
|
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pub fn init(_passphrase: String) -> io::Result<()> { |
|
|
|
|
// TODO: use pasphrase
|
|
|
|
|
pub fn init(passphrase: String) -> io::Result<()> { |
|
|
|
|
fs::create_dir(&*STORAGE_FOLDER)?; |
|
|
|
|
println!("Storage folder created"); |
|
|
|
|
//let mut db = DB::init(&*STORAGE_PATH, pass)?;
|
|
|
|
|
|
|
|
|
|
fs::File::create(&*STORAGE_PATH)?; |
|
|
|
|
println!("Storage db created."); |
|
|
|
|
let st = Storage::new(passphrase); |
|
|
|
|
st.dump()?; |
|
|
|
|
|
|
|
|
|
println!("Initialization complete."); |
|
|
|
|
println!(""); |
|
|
|
|
println!("Now it's required to add folder `{}` under git manually.", &*STORAGE_FOLDER); |
|
|
|
@ -165,13 +184,13 @@ impl Storage {
|
|
|
|
|
.write(true) |
|
|
|
|
.append(false) |
|
|
|
|
.open(&*STORAGE_PATH)?; |
|
|
|
|
writeln!(file, "{}", self.encoder.get_encoded_test_passphrase())?; |
|
|
|
|
|
|
|
|
|
for item in self.items.iter() { |
|
|
|
|
writeln!(file, "{}", item.id)?; |
|
|
|
|
let content = self.encoder.encode(item.content.clone()); |
|
|
|
|
writeln!(file, "{}", content)?; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Ok(()) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|