diff options
author | Baitinq <manuelpalenzuelamerino@gmail.com> | 2024-08-19 20:35:51 +0200 |
---|---|---|
committer | Baitinq <manuelpalenzuelamerino@gmail.com> | 2024-08-19 20:35:51 +0200 |
commit | 0e958a2066f6babbfe1edd35f7b467ba7be1d71a (patch) | |
tree | 36886534fc80db492ca4145ffd3c31d3da4373ef | |
parent | misc: add todo (diff) | |
download | fs-tracer-0e958a2066f6babbfe1edd35f7b467ba7be1d71a.tar.gz fs-tracer-0e958a2066f6babbfe1edd35f7b467ba7be1d71a.tar.bz2 fs-tracer-0e958a2066f6babbfe1edd35f7b467ba7be1d71a.zip |
fs-tracer: add file restoring ops
-rw-r--r-- | fs-tracer/src/main.rs | 41 | ||||
-rw-r--r-- | fs-tracer/src/syscall_handler.rs | 1 |
2 files changed, 39 insertions, 3 deletions
diff --git a/fs-tracer/src/main.rs b/fs-tracer/src/main.rs index 827f562..ba52ff3 100644 --- a/fs-tracer/src/main.rs +++ b/fs-tracer/src/main.rs @@ -8,8 +8,10 @@ use bytes::BytesMut; use core::panic; use fs_tracer_common::SyscallInfo; use log::{debug, info, warn}; +use serde::Deserialize; use serde::Serialize; use std::env; +use std::io::Write; use std::sync::{ atomic::{AtomicBool, Ordering}, Arc, @@ -109,6 +111,42 @@ async fn main() -> Result<(), anyhow::Error> { })); } + //TODO: Create thread getting restored files every X secs and actually restore them + let thread_api_key = fs_tracer_api_key.clone(); + tokio::spawn(async move { + loop { + let resp = ureq::get(&format!("http://leunam.dev:9999/api/v1/restored-files/")) + .set("API_KEY", &thread_api_key) + .call() + .expect("failed to get files to restore") + .into_string() + .unwrap(); + + println!("hello boi! {}", resp); + + if resp != "null" { + let files_to_restore: Vec<FSTracerFile> = + serde_json::from_str(&resp).expect("failed to deserialize files"); + + println!("hello boi! {}", files_to_restore.len()); + + for file in files_to_restore { + println!("Will restore file: {}", file.absolute_path); + let mut f = std::fs::OpenOptions::new() + .write(true) + .truncate(true) + .open(file.absolute_path) + .unwrap(); + f.write_all(file.contents.as_bytes()); + f.flush(); + println!("Restored file"); + } + } + + std::thread::sleep(Duration::from_secs(20)); + } + }); + drop(resolved_files_send); let batch_timeout = Duration::from_secs(7); @@ -137,12 +175,11 @@ async fn main() -> Result<(), anyhow::Error> { Ok(()) } -#[derive(Serialize, Debug)] +#[derive(Serialize, Deserialize, Debug)] struct FSTracerFile { timestamp: String, absolute_path: String, contents: String, - offset: i64, } fn send_request(url: &str, fs_tracer_api_key: &str, files: &Vec<FSTracerFile>) { diff --git a/fs-tracer/src/syscall_handler.rs b/fs-tracer/src/syscall_handler.rs index 34a5dab..4b2f335 100644 --- a/fs-tracer/src/syscall_handler.rs +++ b/fs-tracer/src/syscall_handler.rs @@ -89,7 +89,6 @@ impl SyscallHandler { timestamp: chrono::Utc::now().to_rfc3339(), absolute_path: open_file.filename.to_string(), contents: new_contents.clone(), - offset: open_file.offset, }) .expect("Failed to send file to the resolved_files channel!"); self.open_files |