I added threading (I don't really know what I'm doing)
The bytes update whenever a tracked file is edited/deleted.
This commit is contained in:
224
Cargo.lock
generated
224
Cargo.lock
generated
@@ -14,72 +14,22 @@ version = "2.10.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "812e12b5285cc515a9c72a5c1d3b6d46a19dac5acfef5265968c166106e31dd3"
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "0.1.10"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "1.0.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801"
|
||||
|
||||
[[package]]
|
||||
name = "filetime"
|
||||
version = "0.2.26"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bc0505cd1b6fa6580283f6bdf70a73fcf4aba1184038c90902b92b3dd0df63ed"
|
||||
dependencies = [
|
||||
"cfg-if 1.0.4",
|
||||
"libc",
|
||||
"libredox",
|
||||
"windows-sys 0.60.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "fsevent"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5ab7d1bd1bd33cc98b0889831b72da23c0aa4df9cec7e0702f46ecea04b35db6"
|
||||
dependencies = [
|
||||
"bitflags 1.3.2",
|
||||
"fsevent-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "fsevent-sys"
|
||||
version = "2.0.1"
|
||||
version = "4.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f41b048a94555da0f42f1d632e2e19510084fb8e303b0daa2816e733fb3644a0"
|
||||
checksum = "76ee7a02da4d231650c7cea31349b889be2f45ddb3ef3032d2ec8185f6313fd2"
|
||||
dependencies = [
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "fuchsia-zircon"
|
||||
version = "0.3.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2e9763c69ebaae630ba35f74888db465e49e259ba1bc0eda7d06f4a067615d82"
|
||||
dependencies = [
|
||||
"bitflags 1.3.2",
|
||||
"fuchsia-zircon-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "fuchsia-zircon-sys"
|
||||
version = "0.3.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7"
|
||||
|
||||
[[package]]
|
||||
name = "inotify"
|
||||
version = "0.7.1"
|
||||
version = "0.11.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4816c66d2c8ae673df83366c18341538f234a26d65a9ecea5c348b453ac1d02f"
|
||||
checksum = "f37dccff2791ab604f9babef0ba14fbe0be30bd368dc541e2b08d07c8aa908f3"
|
||||
dependencies = [
|
||||
"bitflags 1.3.2",
|
||||
"bitflags 2.10.0",
|
||||
"inotify-sys",
|
||||
"libc",
|
||||
]
|
||||
@@ -94,29 +44,30 @@ dependencies = [
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "iovec"
|
||||
version = "0.1.4"
|
||||
name = "kqueue"
|
||||
version = "1.1.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b2b3ea6ff95e175473f8ffe6a7eb7c00d054240321b84c57051175fe3c1e075e"
|
||||
checksum = "eac30106d7dce88daf4a3fcb4879ea939476d5074a9b7ddd0fb97fa4bed5596a"
|
||||
dependencies = [
|
||||
"kqueue-sys",
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "kernel32-sys"
|
||||
version = "0.2.2"
|
||||
name = "kqueue-sys"
|
||||
version = "1.0.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d"
|
||||
checksum = "ed9625ffda8729b85e45cf04090035ac368927b8cebc34898e7c120f52e4838b"
|
||||
dependencies = [
|
||||
"winapi 0.2.8",
|
||||
"winapi-build",
|
||||
"bitflags 1.3.2",
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "lazycell"
|
||||
version = "1.3.0"
|
||||
name = "lazy_static"
|
||||
version = "1.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55"
|
||||
checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe"
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
@@ -124,17 +75,6 @@ version = "0.2.177"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2874a2af47a2325c2001a6e6fad9b16a53b802102b528163885171cf92b15976"
|
||||
|
||||
[[package]]
|
||||
name = "libredox"
|
||||
version = "0.1.10"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "416f7e718bdb06000964960ffa43b4335ad4012ae8b99060261aa4a8088d5ccb"
|
||||
dependencies = [
|
||||
"bitflags 2.10.0",
|
||||
"libc",
|
||||
"redox_syscall",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "log"
|
||||
version = "0.4.28"
|
||||
@@ -143,92 +83,48 @@ checksum = "34080505efa8e45a4b816c349525ebe327ceaa8559756f0356cba97ef3bf7432"
|
||||
|
||||
[[package]]
|
||||
name = "mio"
|
||||
version = "0.6.23"
|
||||
version = "1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4afd66f5b91bf2a3bc13fad0e21caedac168ca4c707504e75585648ae80e4cc4"
|
||||
checksum = "69d83b0086dc8ecf3ce9ae2874b2d1290252e2a30720bea58a5c6639b0092873"
|
||||
dependencies = [
|
||||
"cfg-if 0.1.10",
|
||||
"fuchsia-zircon",
|
||||
"fuchsia-zircon-sys",
|
||||
"iovec",
|
||||
"kernel32-sys",
|
||||
"libc",
|
||||
"log",
|
||||
"miow",
|
||||
"net2",
|
||||
"slab",
|
||||
"winapi 0.2.8",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "mio-extras"
|
||||
version = "2.0.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "52403fe290012ce777c4626790c8951324a2b9e3316b3143779c72b029742f19"
|
||||
dependencies = [
|
||||
"lazycell",
|
||||
"log",
|
||||
"mio",
|
||||
"slab",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "miow"
|
||||
version = "0.2.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ebd808424166322d4a38da87083bfddd3ac4c131334ed55856112eb06d46944d"
|
||||
dependencies = [
|
||||
"kernel32-sys",
|
||||
"net2",
|
||||
"winapi 0.2.8",
|
||||
"ws2_32-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "net2"
|
||||
version = "0.2.39"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b13b648036a2339d06de780866fbdfda0dde886de7b3af2ddeba8b14f4ee34ac"
|
||||
dependencies = [
|
||||
"cfg-if 0.1.10",
|
||||
"libc",
|
||||
"winapi 0.3.9",
|
||||
"wasi",
|
||||
"windows-sys 0.61.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "notify"
|
||||
version = "4.0.18"
|
||||
version = "8.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b72dd35279a5dc895a30965e247b0961ba36c233dc48454a2de8ccd459f1afd3"
|
||||
checksum = "4d3d07927151ff8575b7087f245456e549fea62edf0ec4e565a5ee50c8402bc3"
|
||||
dependencies = [
|
||||
"bitflags 1.3.2",
|
||||
"filetime",
|
||||
"fsevent",
|
||||
"bitflags 2.10.0",
|
||||
"fsevent-sys",
|
||||
"inotify",
|
||||
"kqueue",
|
||||
"libc",
|
||||
"log",
|
||||
"mio",
|
||||
"mio-extras",
|
||||
"notify-types",
|
||||
"walkdir",
|
||||
"winapi 0.3.9",
|
||||
"windows-sys 0.60.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "notify-types"
|
||||
version = "2.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5e0826a989adedc2a244799e823aece04662b66609d96af8dff7ac6df9a8925d"
|
||||
|
||||
[[package]]
|
||||
name = "npons"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"lazy_static",
|
||||
"notify",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "redox_syscall"
|
||||
version = "0.5.18"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ed2bf2547551a7053d6fdfafda3f938979645c44812fbfcda098faae3f1a362d"
|
||||
dependencies = [
|
||||
"bitflags 2.10.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "same-file"
|
||||
version = "1.0.6"
|
||||
@@ -238,12 +134,6 @@ dependencies = [
|
||||
"winapi-util",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "slab"
|
||||
version = "0.4.11"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7a2ae44ef20feb57a68b23d846850f861394c2e02dc425a50098ae8c90267589"
|
||||
|
||||
[[package]]
|
||||
name = "walkdir"
|
||||
version = "2.5.0"
|
||||
@@ -255,32 +145,10 @@ dependencies = [
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "winapi"
|
||||
version = "0.2.8"
|
||||
name = "wasi"
|
||||
version = "0.11.1+wasi-snapshot-preview1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a"
|
||||
|
||||
[[package]]
|
||||
name = "winapi"
|
||||
version = "0.3.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
|
||||
dependencies = [
|
||||
"winapi-i686-pc-windows-gnu",
|
||||
"winapi-x86_64-pc-windows-gnu",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "winapi-build"
|
||||
version = "0.1.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc"
|
||||
|
||||
[[package]]
|
||||
name = "winapi-i686-pc-windows-gnu"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
|
||||
checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b"
|
||||
|
||||
[[package]]
|
||||
name = "winapi-util"
|
||||
@@ -291,12 +159,6 @@ dependencies = [
|
||||
"windows-sys 0.61.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "winapi-x86_64-pc-windows-gnu"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
|
||||
|
||||
[[package]]
|
||||
name = "windows-link"
|
||||
version = "0.2.1"
|
||||
@@ -385,13 +247,3 @@ name = "windows_x86_64_msvc"
|
||||
version = "0.53.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d6bbff5f0aada427a1e5a6da5f1f98158182f26556f345ac9e04d36d0ebed650"
|
||||
|
||||
[[package]]
|
||||
name = "ws2_32-sys"
|
||||
version = "0.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d59cefebd0c892fa2dd6de581e937301d8552cb44489cdff035c6187cb63fa5e"
|
||||
dependencies = [
|
||||
"winapi 0.2.8",
|
||||
"winapi-build",
|
||||
]
|
||||
|
||||
@@ -4,4 +4,5 @@ version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
notify = '4'
|
||||
lazy_static = "1.5.0"
|
||||
notify = '8.1.0'
|
||||
|
||||
59
src/files.rs
59
src/files.rs
@@ -1,27 +1,66 @@
|
||||
extern crate notify;
|
||||
use notify::{
|
||||
DebouncedEvent,
|
||||
RecommendedWatcher,
|
||||
RecursiveMode,
|
||||
Watcher
|
||||
Watcher,
|
||||
Result,
|
||||
Event,
|
||||
};
|
||||
use std::
|
||||
{
|
||||
fs,
|
||||
time::Duration,
|
||||
path::Path,
|
||||
sync::mpsc::channel,
|
||||
path::PathBuf,
|
||||
sync::mpsc,
|
||||
};
|
||||
use crate::
|
||||
{
|
||||
ROOT_DIR,
|
||||
};
|
||||
|
||||
pub fn watch_files(file_list: &Vec<PathBuf>) -> u8
|
||||
{
|
||||
let (tx, rx) = mpsc::channel::<Result<Event>>(); // Use recommended_watcher() to automatically select the best implementation
|
||||
|
||||
// for your platform. The `EventHandler` passed to this constructor can be a
|
||||
// closure, a `std::sync::mpsc::Sender`, a `crossbeam_channel::Sender`, or
|
||||
// another type the trait is implemented for.
|
||||
let mut watcher = notify::recommended_watcher(tx).expect("Watcher start failed"); // Add a path to be watched. All files and directories at that path and
|
||||
|
||||
// below will be monitored for changes.
|
||||
watcher.watch(Path::new("root"), RecursiveMode::Recursive).expect("Path watching failed"); // Block forever, printing out events as they come in
|
||||
for res in rx
|
||||
{
|
||||
match res
|
||||
{
|
||||
Ok(event) =>
|
||||
{
|
||||
// Ignore access event kinds as they're irrelevant
|
||||
if ! notify::EventKind::is_access(&event.kind)
|
||||
&& file_list.contains(&event.paths[0])
|
||||
{
|
||||
return 0
|
||||
}
|
||||
},
|
||||
Err(e) =>
|
||||
{
|
||||
println!("watch error: {:?}", e);
|
||||
return 2
|
||||
}
|
||||
}
|
||||
}
|
||||
println!("Watching files...");
|
||||
return 1
|
||||
}
|
||||
|
||||
pub fn read_file(file_name: &str, depth: u8) ->
|
||||
(
|
||||
Vec<u8>,
|
||||
Vec<String>,
|
||||
Vec<PathBuf>,
|
||||
)
|
||||
{
|
||||
if let Ok(file_contents) = fs::read_to_string(format!("root/{}",file_name))
|
||||
{
|
||||
let (bytes, mut file_list) = encode_npon(&file_contents, depth);
|
||||
let (bytes, file_list) = encode_npon(&file_contents, depth);
|
||||
return (bytes, file_list)
|
||||
}
|
||||
return (vec![], vec![])
|
||||
@@ -30,11 +69,11 @@ pub fn read_file(file_name: &str, depth: u8) ->
|
||||
fn encode_npon(file_contents: &String, depth: u8) ->
|
||||
(
|
||||
Vec<u8>,
|
||||
Vec<String>,
|
||||
Vec<PathBuf>,
|
||||
)
|
||||
{
|
||||
let mut bytes: Vec<u8> = vec![];
|
||||
let mut file_list: Vec<String> = vec![];
|
||||
let mut file_list: Vec<PathBuf> = vec![];
|
||||
if depth > 63 {
|
||||
println!("Depth level safety reached (63), recursion error suspected");
|
||||
return (vec![], file_list);
|
||||
@@ -139,7 +178,7 @@ fn encode_npon(file_contents: &String, depth: u8) ->
|
||||
{
|
||||
58 =>
|
||||
{
|
||||
file_list.push((&file_contents[index_flag..index.clone()]).to_string());
|
||||
file_list.push(ROOT_DIR.join(&file_contents[index_flag..index.clone()]));
|
||||
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()
|
||||
|
||||
57
src/main.rs
57
src/main.rs
@@ -1,24 +1,67 @@
|
||||
use std::
|
||||
{
|
||||
io,
|
||||
fs,
|
||||
io::{BufReader, prelude::*},
|
||||
net::{TcpListener, TcpStream},
|
||||
sync::mpsc,
|
||||
thread,
|
||||
path::PathBuf,
|
||||
};
|
||||
use lazy_static::lazy_static;
|
||||
|
||||
|
||||
mod files;
|
||||
|
||||
lazy_static! {
|
||||
static ref ROOT_DIR: PathBuf = fs::canonicalize(PathBuf::from("./root")).unwrap();
|
||||
}
|
||||
// TODO, remove .unwrap()
|
||||
// TODO, make 'code' an enum
|
||||
// TODO, safely modify bytes in thread.
|
||||
// TODO, Makes paths more consistent.
|
||||
// TODO, Avoid copying bytes
|
||||
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);
|
||||
// Default values
|
||||
const BIND_ADDRESS: &str = "127.0.0.1:2008";
|
||||
const INDEX_FILE: &str = "index.npon";
|
||||
|
||||
let (tx, rx) = mpsc::channel();
|
||||
let listener = TcpListener::bind(BIND_ADDRESS).unwrap();
|
||||
println!("Listening at {}", BIND_ADDRESS);
|
||||
|
||||
/*
|
||||
let mut file_list: Vec<std::path::PathBuf> = vec![ROOT_DIR.join(INDEX_FILE)];
|
||||
let (mut bytes, mut new_file_list) = files::read_file(INDEX_FILE, 0);
|
||||
file_list.append(&mut new_file_list);
|
||||
println!("Files encoded: {:?}", file_list);
|
||||
*/
|
||||
// File watch thread
|
||||
thread::spawn(
|
||||
move || {
|
||||
let mut bytes: Vec<u8>;
|
||||
let mut file_list: Vec<PathBuf>;
|
||||
(bytes, file_list) = files::read_file(INDEX_FILE, 0);
|
||||
file_list.push(ROOT_DIR.join(INDEX_FILE));
|
||||
|
||||
tx.send(bytes.clone()).unwrap();
|
||||
'watch_loop: loop
|
||||
{
|
||||
let code = files::watch_files(&file_list);
|
||||
if code == 0
|
||||
{
|
||||
println!("Re encoding index");
|
||||
|
||||
(bytes, file_list) = files::read_file(INDEX_FILE, 0);
|
||||
file_list.push(ROOT_DIR.join(INDEX_FILE));
|
||||
tx.send(bytes.clone()).unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
for stream in listener.incoming() {
|
||||
let bytes = rx.recv().unwrap();
|
||||
let stream = stream.unwrap();
|
||||
handle_connection(stream, &bytes);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user