fix: fix periodic freeze in print stack
This commit is contained in:
@@ -188,6 +188,34 @@ inline void sanitize_field(char* s) {
|
||||
}
|
||||
}
|
||||
|
||||
inline bool read_text_file_into_buffer(const char* path, char* out, int out_len) {
|
||||
if (path == nullptr || out == nullptr || out_len <= 0) return false;
|
||||
FILE* f = fopen(path, "rb");
|
||||
if (!f) return false;
|
||||
|
||||
size_t n = fread(out, 1, (size_t)out_len - 1, f);
|
||||
fclose(f);
|
||||
out[n] = '\0';
|
||||
return n > 0;
|
||||
}
|
||||
|
||||
template <typename Fn>
|
||||
inline void parse_key_value_lines(char* text, Fn fn) {
|
||||
if (text == nullptr) return;
|
||||
char* cursor = text;
|
||||
while (*cursor) {
|
||||
char* line = cursor;
|
||||
while (*cursor && *cursor != '\n') cursor++;
|
||||
if (*cursor == '\n') *cursor++ = '\0';
|
||||
|
||||
trim_line(line);
|
||||
char* eq = strchr(line, '=');
|
||||
if (!eq) continue;
|
||||
*eq++ = '\0';
|
||||
fn(line, eq);
|
||||
}
|
||||
}
|
||||
|
||||
inline void now_string(char* out, int out_len) {
|
||||
if (out == nullptr || out_len <= 0) return;
|
||||
Montauk::DateTime dt = {};
|
||||
@@ -264,13 +292,15 @@ inline void make_doc_file_path(const char* id, char* out, int out_len) {
|
||||
|
||||
inline bool read_text_file_line(const char* path, char* out, int out_len) {
|
||||
if (path == nullptr || out == nullptr || out_len <= 0) return false;
|
||||
FILE* f = fopen(path, "rb");
|
||||
if (!f) return false;
|
||||
out[0] = '\0';
|
||||
bool ok = fgets(out, out_len, f) != nullptr;
|
||||
fclose(f);
|
||||
if (ok) trim_line(out);
|
||||
return ok;
|
||||
if (!read_text_file_into_buffer(path, out, out_len)) return false;
|
||||
for (int i = 0; out[i] != '\0'; i++) {
|
||||
if (out[i] == '\r' || out[i] == '\n') {
|
||||
out[i] = '\0';
|
||||
break;
|
||||
}
|
||||
}
|
||||
trim_line(out);
|
||||
return true;
|
||||
}
|
||||
|
||||
inline bool write_text_file_atomic(const char* path, const char* text) {
|
||||
@@ -430,23 +460,15 @@ inline bool load_known_printer_from_path(const char* path, KnownPrinter* printer
|
||||
if (path == nullptr || printer == nullptr) return false;
|
||||
zero_known_printer(printer);
|
||||
|
||||
FILE* f = fopen(path, "rb");
|
||||
if (!f) return false;
|
||||
|
||||
char line[384];
|
||||
while (fgets(line, sizeof(line), f) != nullptr) {
|
||||
trim_line(line);
|
||||
char* eq = strchr(line, '=');
|
||||
if (!eq) continue;
|
||||
*eq++ = '\0';
|
||||
|
||||
char text[1024];
|
||||
if (!read_text_file_into_buffer(path, text, sizeof(text))) return false;
|
||||
parse_key_value_lines(text, [&](const char* line, const char* eq) {
|
||||
if (strcmp(line, "uri") == 0) safe_copy(printer->uri, sizeof(printer->uri), eq);
|
||||
else if (strcmp(line, "display_name") == 0) safe_copy(printer->display_name, sizeof(printer->display_name), eq);
|
||||
else if (strcmp(line, "last_seen") == 0) safe_copy(printer->last_seen, sizeof(printer->last_seen), eq);
|
||||
else if (strcmp(line, "status_message") == 0) safe_copy(printer->status_message, sizeof(printer->status_message), eq);
|
||||
else if (strcmp(line, "ok") == 0) printer->ok = atoi(eq) != 0;
|
||||
}
|
||||
fclose(f);
|
||||
});
|
||||
return printer->uri[0] != '\0';
|
||||
}
|
||||
|
||||
@@ -826,16 +848,9 @@ inline bool load_job_from_path(const char* path, JobMeta* job) {
|
||||
if (path == nullptr || job == nullptr) return false;
|
||||
zero_job(job);
|
||||
|
||||
FILE* f = fopen(path, "rb");
|
||||
if (!f) return false;
|
||||
|
||||
char line[384];
|
||||
while (fgets(line, sizeof(line), f) != nullptr) {
|
||||
trim_line(line);
|
||||
char* eq = strchr(line, '=');
|
||||
if (!eq) continue;
|
||||
*eq++ = '\0';
|
||||
|
||||
char text[4096];
|
||||
if (!read_text_file_into_buffer(path, text, sizeof(text))) return false;
|
||||
parse_key_value_lines(text, [&](const char* line, const char* eq) {
|
||||
if (strcmp(line, "id") == 0) safe_copy(job->id, sizeof(job->id), eq);
|
||||
else if (strcmp(line, "state") == 0) safe_copy(job->state, sizeof(job->state), eq);
|
||||
else if (strcmp(line, "source_kind") == 0) safe_copy(job->source_kind, sizeof(job->source_kind), eq);
|
||||
@@ -852,8 +867,7 @@ inline bool load_job_from_path(const char* path, JobMeta* job) {
|
||||
else if (strcmp(line, "remote_job_id") == 0) job->remote_job_id = atoi(eq);
|
||||
else if (strcmp(line, "size_bytes") == 0) job->size_bytes = atoi(eq);
|
||||
else if (strcmp(line, "copies") == 0) job->copies = atoi(eq);
|
||||
}
|
||||
fclose(f);
|
||||
});
|
||||
if (job->copies <= 0) job->copies = 1;
|
||||
return job->id[0] != '\0';
|
||||
}
|
||||
@@ -914,16 +928,9 @@ inline bool load_probe_state(const char* path, ProbeState* state) {
|
||||
if (path == nullptr || state == nullptr) return false;
|
||||
zero_probe_state(state);
|
||||
|
||||
FILE* f = fopen(path, "rb");
|
||||
if (!f) return false;
|
||||
|
||||
char line[512];
|
||||
while (fgets(line, sizeof(line), f) != nullptr) {
|
||||
trim_line(line);
|
||||
char* eq = strchr(line, '=');
|
||||
if (!eq) continue;
|
||||
*eq++ = '\0';
|
||||
|
||||
char text[4096];
|
||||
if (!read_text_file_into_buffer(path, text, sizeof(text))) return false;
|
||||
parse_key_value_lines(text, [&](const char* line, const char* eq) {
|
||||
if (strcmp(line, "ok") == 0) state->ok = atoi(eq) != 0;
|
||||
else if (strcmp(line, "printer_uri") == 0) safe_copy(state->printer_uri, sizeof(state->printer_uri), eq);
|
||||
else if (strcmp(line, "probed_at") == 0) safe_copy(state->probed_at, sizeof(state->probed_at), eq);
|
||||
@@ -946,8 +953,7 @@ inline bool load_probe_state(const char* path, ProbeState* state) {
|
||||
else if (strcmp(line, "caps_supported_formats") == 0) safe_copy(state->caps.supported_formats, sizeof(state->caps.supported_formats), eq);
|
||||
else if (strcmp(line, "caps_default_format") == 0) safe_copy(state->caps.default_format, sizeof(state->caps.default_format), eq);
|
||||
else if (strcmp(line, "caps_preferred_format") == 0) safe_copy(state->caps.preferred_format, sizeof(state->caps.preferred_format), eq);
|
||||
}
|
||||
fclose(f);
|
||||
});
|
||||
return state->printer_uri[0] != '\0' || state->message[0] != '\0';
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user