recs/lib.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
#[path = "system/array.rs"]
mod array;
#[path = "system/array_retrive.rs"]
mod array_tools;
#[path = "auth.rs"]
mod auth;
#[path = "system/config.rs"]
mod config;
#[path = "system/encrypt.rs"]
mod encrypt;
// pub mod errors;
#[path = "enviornment.rs"]
mod local_env;
#[path = "system/secrets.rs"]
mod secret;
use dusa_collection_utils::{
errors::{OkWarning, UnifiedResult as uf},
functions::{create_hash, path_present},
log,
log::LogLevel,
types::PathType,
};
use local_env::VERSION;
use secret::{read_raw, write_raw};
use std::{
fs::{File, OpenOptions},
io::{Read, Write},
};
use crate::{
array::{index_system_array, ChunkMap},
array_tools::fetch_chunk,
config::{ARRAY_LEN, CHUNK_SIZE},
local_env::{set_system, SystemPaths},
secret::{forget, read, write},
};
/// Debugging should be set while initializing the lib. If not defined, the default is disabled.
pub static mut DEBUGGING: Option<bool> = None;
/// This value is set by set_prog. It is used for logging, creating paths, and other functions.
/// To handle its creation or modification, use set_prog() to avoid wrapping.
pub static mut PROGNAME: &str = "";
/// Changes some mandatory logging functions and enables longer outputs in logs
///
/// # Arguments
///
/// * `option` - A boolean value to enable or disable debugging.
pub fn set_debug(option: bool) {
// Enables longer backtraces and enables more verbose logging
match option {
true => unsafe { DEBUGGING = Some(true) },
false => unsafe { DEBUGGING = Some(false) },
}
}
/// This function handles setting the PROGNAME variable.
///
/// # Arguments
///
/// * `data` - A static string slice representing the program name.
pub fn set_prog(data: &'static str) {
unsafe { PROGNAME = data };
}
/// Initialize checks the progname and debugging values and ensures the lib is ready to function
///
/// # Arguments
///
/// * `errors` - An ErrorArray to capture errors.
/// * `warnings` - A WarningArray to capture warnings.
///
/// # Returns
///
/// * `uf<()>` - A unified result indicating success or failure.
pub async fn initialize(temporary_path: bool) -> uf<()> {
let debugging: bool = match unsafe { DEBUGGING } {
Some(d) => match d {
true => true,
false => false,
},
None => false,
};
let debug: bool = match &debugging {
true => {
use std::env;
env::set_var("RUST_BACKTRACE", "1");
true
}
false => false,
};
log!(LogLevel::Info, "RECS started");
SystemPaths::set_current(temporary_path).await;
if let Err(e) = ensure_system_path(debug).await.uf_unwrap() {
return uf::new(Err(e));
}
if let Err(e) = ensure_max_map_exists().await.uf_unwrap() {
return uf::new(Err(e));
}
uf::new(Ok(()))
}
/// Ensures that the system path exists, if not reinitializes it.
///
/// # Arguments
///
/// * `debug` - A boolean indicating if debugging is enabled.
/// * `errors` - An ErrorArray to capture errors.
/// * `warnings` - A WarningArray to capture warnings.
///
/// # Returns
///
/// * `uf<()>` - A unified result indicating success or failure.
async fn ensure_system_path(debug: bool) -> uf<()> {
let system_paths = SystemPaths::read_current().await;
match path_present(&system_paths.SYSTEM_ARRAY_LOCATION).uf_unwrap() {
Ok(true) => (),
Ok(false) => {
log!(
LogLevel::Info,
"System array file does not exist, reinitialize recs"
);
if let Err(e) = set_system(debug).await.uf_unwrap() {
return uf::new(Err(e));
}
}
Err(e) => return uf::new(Err(e)),
}
uf::new(Ok(()))
}
/// Ensures that the maximum map exists, if not indexes the system array.
///
/// # Arguments
///
/// * `errors` - An ErrorArray to capture errors.
/// * `warnings` - A WarningArray to capture warnings.
///
/// # Returns
///
/// * `uf<()>` - A unified result indicating success or failure.
async fn ensure_max_map_exists() -> uf<()> {
let system_paths = SystemPaths::read_current().await;
let max_map = ARRAY_LEN / CHUNK_SIZE;
let max_map_path = PathType::Content(format!("{}/{}.map", system_paths.MAPS, max_map - 1));
match path_present(&max_map_path).uf_unwrap() {
Ok(true) => uf::new(Ok(())),
Ok(false) => match index_system_array().await.uf_unwrap() {
Ok(_d) => uf::new(Ok(())),
Err(e) => return uf::new(Err(e)),
},
Err(e) => uf::new(Err(e)),
}
}
/// Inserts a file by encrypting and storing it.
///
/// # Arguments
///
/// * `filename` - The relative path of the file to be stored.
/// * `owner` - The owner of the file.
/// * `name` - The name of the file.
/// * `errors` - An ErrorArray to capture errors.
/// * `warnings` - A WarningArray to capture warnings.
///
/// # Returns
///
/// * `uf<()>` - A unified result indicating success or failure.
pub async fn store(filename: PathType, owner: String, name: String) -> uf<()> {
match write(filename, owner, name, false).await.uf_unwrap() {
Ok(d) => {
log!(LogLevel::Info, "Stored: value:{}, count: {} ", d.0, d.1);
return uf::new(Ok(()));
}
Err(e) => return uf::new(Err(e)),
}
}
/// Retrieves a file by decrypting it.
///
/// # Arguments
///
/// * `owner` - The owner of the file.
/// * `name` - The name of the file.
/// * `uid` - The unique identifier for the file.
/// * `errors` - An ErrorArray to capture errors.
/// * `warnings` - A WarningArray to capture warnings.
///
/// # Returns
///
/// * `uf<OkWarning<(PathType, PathType)>>` - A unified result containing the decrypted file paths or an error.
pub async fn retrieve(
owner: String,
name: String,
uid: u32,
) -> uf<OkWarning<(PathType, PathType)>> {
read(owner, name, uid, false).await
}
/// Removes a file.
///
/// # Arguments
///
/// * `owner` - The owner of the file.
/// * `name` - The name of the file.
/// * `errors` - An ErrorArray to capture errors.
/// * `warnings` - A WarningArray to capture warnings.
///
/// # Returns
///
/// * `uf<()>` - A unified result indicating success or failure.
pub async fn remove(owner: String, name: String) -> uf<()> {
match forget(owner, name).await {
Ok(_) => uf::new(Ok(())),
Err(err) => uf::new(Err(err)),
}
}
/// Checks if a file exists.
///
/// # Arguments
///
/// * `owner` - The owner of the file.
/// * `name` - The name of the file.
/// * `errors` - An ErrorArray to capture errors.
///
/// # Returns
///
/// * `uf<bool>` - A unified result indicating if the file exists or not.
pub async fn ping(owner: String, name: String) -> uf<bool> {
let system_paths: SystemPaths = SystemPaths::read_current().await;
let secret_map_path = PathType::Content(format!(
"{}/{owner}-{name}.meta",
system_paths.META,
owner = owner,
name = name
));
path_present(&secret_map_path)
}
/// Encrypts raw data.
///
/// # Arguments
///
/// * `data` - The data to be encrypted.
/// * `errors` - An ErrorArray to capture errors.
/// * `warnings` - A WarningArray to capture warnings.
///
/// # Returns
///
/// * `uf<(String, String, usize)>` - A unified result containing the encrypted data, key, and chunk size.
pub async fn encrypt_raw(data: String) -> uf<(String, String, usize)> {
write_raw(data.into()).await
}
/// Decrypts raw data.
///
/// # Arguments
///
/// * `recs_data` - The encrypted data.
/// * `recs_key` - The key for decryption.
/// * `recs_chunks` - The number of chunks.
/// * `errors` - An ErrorArray to capture errors.
/// * `warnings` - A WarningArray to capture warnings.
///
/// # Returns
///
/// * `uf<OkWarning<Vec<u8>>>` - A unified result containing the decrypted data or an error.
pub fn decrypt_raw(recs_data: String, recs_key: String, recs_chunks: usize) -> uf<Vec<u8>> {
read_raw(recs_data, recs_key, recs_chunks)
}
/// Updates the map with new data.
///
/// # Arguments
///
/// * `map_num` - The map number to be updated.
/// * `errors` - An ErrorArray to capture errors.
/// * `warnings` - A WarningArray to capture warnings.
///
/// # Returns
///
/// * `bool` - A boolean indicating if the update was successful.
pub async fn update_map(map_num: u32) -> bool {
let system_paths: SystemPaths = SystemPaths::read_current().await;
// Add a result to return errors from this
// ? Getting the current map data
let map_path: PathType =
PathType::Content(format!("{}/chunk_{}.map", system_paths.MAPS, map_num));
// ? Reading the map
let mut map_file = File::open(&map_path).expect("File could not be opened");
let mut map_data: String = String::new();
map_file
.read_to_string(&mut map_data)
.expect("Could not read the map file !");
// ? unpacking to the chunk map struct
let pretty_map_data: ChunkMap = serde_json::from_str(&map_data).unwrap();
// ? calculating new hash
let chunk_data: (bool, Option<String>) = match fetch_chunk(map_num).await.uf_unwrap() {
Ok(data) => (true, Some(data)),
Err(_) => (false, None),
};
let new_hash: Option<String> = match chunk_data {
(true, None) => None,
(true, Some(chunk)) => Some(create_hash(chunk)),
(false, None) => None,
(false, Some(_)) => None,
};
if new_hash == None {
log!(
LogLevel::Error,
"Failed to fetch chunk data for number {}",
&map_num
);
}
// making new map
let new_map: ChunkMap = ChunkMap {
location: pretty_map_data.location,
version: VERSION.to_string(),
chunk_num: pretty_map_data.chunk_num,
chunk_hsh: new_hash.unwrap(),
chunk_beg: pretty_map_data.chunk_beg,
chunk_end: pretty_map_data.chunk_end,
};
// write the new map file
let _ = map_path.delete();
let updated_map = serde_json::to_string_pretty(&new_map).unwrap();
let mut map_file = OpenOptions::new()
.create_new(true)
.write(true)
.append(true)
.open(map_path)
.expect("File could not written to");
if let Err(_e) = writeln!(map_file, "{}", updated_map) {
log!(LogLevel::Error, "Could save map data to file");
};
return true;
}