Return a list of files that are included.
This commit is contained in:
45
src/files.rs
45
src/files.rs
@@ -1,21 +1,43 @@
|
||||
use std::fs;
|
||||
pub fn read_file(file_name: &str, depth: u8) -> Vec<u8>
|
||||
extern crate notify;
|
||||
use notify::{
|
||||
DebouncedEvent,
|
||||
RecommendedWatcher,
|
||||
RecursiveMode,
|
||||
Watcher
|
||||
};
|
||||
use std::
|
||||
{
|
||||
fs,
|
||||
time::Duration,
|
||||
path::Path,
|
||||
sync::mpsc::channel,
|
||||
};
|
||||
|
||||
pub fn read_file(file_name: &str, depth: u8) ->
|
||||
(
|
||||
Vec<u8>,
|
||||
Vec<String>,
|
||||
)
|
||||
{
|
||||
if let Ok(file_contents) = fs::read_to_string(format!("root/{}",file_name))
|
||||
{
|
||||
let bytes = encode_npon(&file_contents, depth);
|
||||
return bytes
|
||||
let (bytes, mut file_list) = encode_npon(&file_contents, depth);
|
||||
return (bytes, file_list)
|
||||
}
|
||||
return vec![]
|
||||
|
||||
return (vec![], vec![])
|
||||
}
|
||||
|
||||
fn encode_npon(file_contents: &String, depth: u8) -> Vec<u8>
|
||||
fn encode_npon(file_contents: &String, depth: u8) ->
|
||||
(
|
||||
Vec<u8>,
|
||||
Vec<String>,
|
||||
)
|
||||
{
|
||||
let mut bytes: Vec<u8> = vec![];
|
||||
let mut file_list: Vec<String> = vec![];
|
||||
if depth > 63 {
|
||||
println!("Depth level safety reached (63), recursion error suspected");
|
||||
return vec![];
|
||||
return (vec![], file_list);
|
||||
}
|
||||
if depth == 0 {
|
||||
bytes.push(01);
|
||||
@@ -117,7 +139,9 @@ fn encode_npon(file_contents: &String, depth: u8) -> Vec<u8>
|
||||
{
|
||||
58 =>
|
||||
{
|
||||
let new_bytes = read_file(&file_contents[index_flag..index], depth+1);
|
||||
file_list.push((&file_contents[index_flag..index.clone()]).to_string());
|
||||
let (new_bytes, mut new_file_list) = read_file(&file_contents[index_flag..index], depth+1);
|
||||
file_list.append(&mut new_file_list);
|
||||
for byte in new_bytes.iter()
|
||||
{
|
||||
bytes.push(*byte);
|
||||
@@ -135,5 +159,6 @@ fn encode_npon(file_contents: &String, depth: u8) -> Vec<u8>
|
||||
bytes.push(02);
|
||||
}
|
||||
|
||||
return bytes
|
||||
return (bytes, file_list)
|
||||
}
|
||||
|
||||
|
||||
14
src/main.rs
14
src/main.rs
@@ -1,6 +1,6 @@
|
||||
use std::io;
|
||||
use std::
|
||||
{
|
||||
io,
|
||||
io::{BufReader, prelude::*},
|
||||
net::{TcpListener, TcpStream},
|
||||
};
|
||||
@@ -10,23 +10,25 @@ mod files;
|
||||
fn main() -> io::Result<()>
|
||||
{
|
||||
let bind_address: &str = "127.0.0.1:2025";
|
||||
let index_file: &str = "index.npon";
|
||||
let listener = TcpListener::bind(bind_address).unwrap();
|
||||
println!("Listening at {}", bind_address);
|
||||
let mut file_list: Vec<String> = vec![index_file.to_string()];
|
||||
let (bytes, mut new_file_list) = files::read_file(index_file, 0);
|
||||
file_list.append(&mut new_file_list);
|
||||
println!("Files encoded: {:?}", file_list);
|
||||
|
||||
for stream in listener.incoming() {
|
||||
let stream = stream.unwrap();
|
||||
handle_connection(stream);
|
||||
handle_connection(stream, &bytes);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn handle_connection(mut stream: TcpStream)
|
||||
fn handle_connection(mut stream: TcpStream, bytes: &Vec<u8>)
|
||||
{
|
||||
let buf_reader = BufReader::new(&stream);
|
||||
println!("CONNECTION: {buf_reader:#?}");
|
||||
let bytes = files::read_file("index.npon", 0);
|
||||
stream.write_all(bytes.as_slice()).unwrap();
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user