Skip to content

Commit 3bc3c37

Browse files
committed
- Fix reprogram_file file system handling.
1 parent b5270b0 commit 3bc3c37

File tree

1 file changed

+25
-8
lines changed

1 file changed

+25
-8
lines changed

src/frame_socket_server.rs

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use std::sync::Arc;
1010
use std::sync::atomic::{AtomicBool, Ordering};
1111
use std::sync::mpsc::{Receiver, RecvTimeoutError};
1212
use std::thread::sleep;
13-
use std::time::{Duration, Instant, SystemTime};
13+
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};
1414
use std::{fs, process, thread};
1515
use thread_priority::{ThreadBuilderExt, ThreadPriority};
1616

@@ -249,24 +249,41 @@ fn handle_payload_from_frame_acquire_thread(
249249
if *reconnects == NUM_ATTEMPTS_BEFORE_RESTART_OR_REPROGRAM {
250250
let reprogram_file = "/home/pi/last-rp2040-reprogram";
251251
let last_reprogram_over_1hr_ago = match fs::exists(reprogram_file) {
252-
Ok(_) => {
252+
Ok(true) => {
253253
let metadata = fs::metadata(reprogram_file)
254254
.expect("Failed reading file metadata");
255-
let created = metadata
256-
.created()
257-
.expect("Failed reading file creation time metadata");
255+
let created = metadata.modified().unwrap_or_else(|_| {
256+
let timestamp_str = fs::read(reprogram_file)
257+
.and_then(|file_contents| {
258+
Ok(String::from_utf8(file_contents)
259+
.unwrap_or(String::from("0")))
260+
})
261+
.expect("Should get timestamp string");
262+
let timestamp_seconds =
263+
timestamp_str.parse::<u64>().unwrap_or(0);
264+
let timestamp = Duration::from_secs(timestamp_seconds);
265+
UNIX_EPOCH + timestamp
266+
});
258267
SystemTime::now()
259268
.duration_since(created)
260269
.is_ok_and(|duration| duration > Duration::from_secs(60 * 60))
261270
}
262-
Err(_) => true,
271+
Ok(false) | Err(_) => true,
263272
};
264273
if last_reprogram_over_1hr_ago {
265274
let _ = fs::remove_file(reprogram_file);
266275
match program_rp2040() {
267276
Ok(()) => {
268-
fs::write(reprogram_file, "<placeholder>")
269-
.expect("Failed writing reprogram placeholder file");
277+
let now = SystemTime::now();
278+
let timestamp_seconds = now
279+
.duration_since(UNIX_EPOCH)
280+
.expect("Time went backwards")
281+
.as_secs_f64();
282+
fs::write(
283+
reprogram_file,
284+
String::from(format!("{timestamp_seconds}")),
285+
)
286+
.expect("Failed writing reprogram placeholder file");
270287
process::exit(0)
271288
}
272289
Err(e) => {

0 commit comments

Comments
 (0)